selection_info_dao.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. package dao
  2. import (
  3. "errors"
  4. "fmt"
  5. "gorm.io/gorm"
  6. "time"
  7. "youngee_b_api/app/entity"
  8. "youngee_b_api/app/vo"
  9. )
  10. type SelectionInfoDAO struct{}
  11. func (d SelectionInfoDAO) GetSelectionInfoById(selectionId string) (*entity.SelectionInfo, error) {
  12. var selectionInfo entity.SelectionInfo
  13. err := Db.Where("selection_id = ?", selectionId).First(&selectionInfo).Error
  14. if err != nil {
  15. if errors.Is(err, gorm.ErrRecordNotFound) {
  16. return nil, nil
  17. } else {
  18. return nil, err
  19. }
  20. }
  21. return &selectionInfo, err
  22. }
  23. // 根据enterpriseId查询指定某天的所有带货数据
  24. func (d SelectionInfoDAO) GetSelectionInfoListOfDay(enterpriseId string, date time.Time) ([]entity.SelectionInfo, error) {
  25. var selectionInfos []entity.SelectionInfo
  26. // 构建查询
  27. query := Db.Model(&entity.SelectionInfo{})
  28. if enterpriseId != "" {
  29. query = query.Where("enterprise_id = ?", enterpriseId)
  30. }
  31. // 将日期部分提取出来进行匹配
  32. query = query.Where("DATE(created_at) = ?", date.Format("2006-01-02"))
  33. err := query.Find(&selectionInfos).Error
  34. return selectionInfos, err
  35. }
  36. // 创建带货任务
  37. func (d SelectionInfoDAO) CreateSelectionInfo(selectionInfo entity.SelectionInfo) error {
  38. err := Db.Omit("submit_at", "pass_at", "pay_at", "finish_at", "auto_fail_at").Create(&selectionInfo).Error
  39. if err != nil {
  40. return err
  41. }
  42. return nil
  43. }
  44. // 更新带货任务
  45. func (d SelectionInfoDAO) UpdateSelectionInfo(selectionInfo entity.SelectionInfo) error {
  46. err := Db.Debug().Model(&entity.SelectionInfo{}).Where("selection_id = ?", selectionInfo.SelectionID).Updates(selectionInfo).Error
  47. if err != nil {
  48. return err
  49. }
  50. return nil
  51. }
  52. // 更新开票状态字段
  53. func (d SelectionInfoDAO) UpdateInvoiceStatus(selectionIDs []string) error {
  54. err := Db.Model(&entity.SelectionInfo{}).Where("selection_id IN ?", selectionIDs).Updates(entity.SelectionInfo{InvoiceStatus: 1}).Error
  55. return err
  56. }
  57. // 获取带货任务列表
  58. func (d SelectionInfoDAO) GetSelectionPreviews(param *vo.SelectionSearchParam) ([]vo.ReSelectionTaskPreview, int64, error) {
  59. var reSelectionTaskPreviews []vo.ReSelectionTaskPreview
  60. var selectionInfos []entity.SelectionInfo
  61. var total int64
  62. query := Db.Model(&entity.SelectionInfo{})
  63. // 动态添加查询条件
  64. if param.SubAccountId == 0 {
  65. if param.EnterpriseId == "" {
  66. return reSelectionTaskPreviews, 0, errors.New("enterpriseId is empty")
  67. }
  68. query = query.Where("enterprise_id = ?", param.EnterpriseId)
  69. } else {
  70. query = query.Where("sub_account_id = ?", param.SubAccountId)
  71. }
  72. if param.SelectionPlatform != 0 {
  73. query = query.Where("platform = ?", param.SelectionPlatform)
  74. }
  75. if param.SelectionStatus != 0 {
  76. query = query.Where("selection_status = ?", param.SelectionStatus)
  77. } else {
  78. query = query.Where("selection_status not in ?", []int{1, 3, 5})
  79. }
  80. // sample_mode 1、2、3分别表示免费领样(有领样策略)、垫付领样(3.0不用)、不提供样品(无领样策略)
  81. if param.FreeFlag == 1 {
  82. query = query.Where("sample_mode = ?", 1)
  83. } else if param.FreeFlag == 2 {
  84. query = query.Where("sample_mode = ?", 3)
  85. }
  86. // task_mode 1、2分别表示悬赏任务(有悬赏策略)、纯佣带货(无悬赏策略)
  87. if param.RewardFlag == 1 {
  88. query = query.Where("task_mode = ?", 1)
  89. } else if param.RewardFlag == 2 {
  90. query = query.Where("task_mode = ?", 2)
  91. }
  92. if param.Others != "" {
  93. query = query.Where("enterprise_id = ? or selection_id = ? or selection_name LIKE ?", param.Others, param.Others, "%"+param.Others+"%")
  94. }
  95. query.Count(&total)
  96. query = query.Select("enterprise_id, sub_account_id, selection_id, platform, selection_status, created_at, task_ddl, sample_num, enroll_num, choose_num, product_id, estimated_cost")
  97. offset := (param.Page - 1) * param.PageSize
  98. if param.Order == 1 {
  99. if err := query.Order("selection_status desc").Order("task_ddl asc").Offset(offset).Limit(param.PageSize).Find(&selectionInfos).Error; err != nil {
  100. return nil, 0, err
  101. }
  102. } else {
  103. if err := query.Order("selection_status asc").Order("task_ddl desc").Offset(offset).Limit(param.PageSize).Find(&selectionInfos).Error; err != nil {
  104. return nil, 0, err
  105. }
  106. }
  107. for _, selectionInfo := range selectionInfos {
  108. reSelectionTaskPreview := vo.ReSelectionTaskPreview{
  109. EnterpriseId: selectionInfo.EnterpriseID,
  110. SubAccountId: selectionInfo.SubAccountId,
  111. SelectionId: selectionInfo.SelectionID,
  112. SelectionPlatform: selectionInfo.Platform,
  113. SelectionStatus: selectionInfo.SelectionStatus,
  114. CreatedAt: selectionInfo.CreatedAt.Format("2006-01-02 15:04:05"),
  115. TaskDdl: selectionInfo.TaskDdl.Format("2006-01-02 15:04:05"),
  116. SampleNum: selectionInfo.SampleNum,
  117. EnrollNum: selectionInfo.EnrollNum,
  118. ChooseNum: selectionInfo.ChooseNum,
  119. ProductId: selectionInfo.ProductID,
  120. Reward: selectionInfo.EstimatedCost,
  121. }
  122. reSelectionTaskPreviews = append(reSelectionTaskPreviews, reSelectionTaskPreview)
  123. }
  124. return reSelectionTaskPreviews, total, nil
  125. }
  126. // 删除带货任务
  127. func (d SelectionInfoDAO) DeleteSelection(selectionId string) (*string, error) {
  128. if selectionId == "" {
  129. return &selectionId, nil
  130. }
  131. err := Db.Model(&entity.SelectionInfo{}).Where("selection_id = ?", selectionId).Delete(&entity.SelectionInfo{}).Error
  132. if err != nil {
  133. return nil, err
  134. }
  135. return &selectionId, nil
  136. }
  137. // 获取草稿箱——电商带货任务列表
  138. func (d SelectionInfoDAO) GetSelectionDraftList(param *vo.SelectionDraftParam) ([]vo.ReSelectionTaskPreview, int64, error) {
  139. var reSelectionTaskPreviews []vo.ReSelectionTaskPreview
  140. var selectionInfos []entity.SelectionInfo
  141. var total int64
  142. query := Db.Model(&entity.SelectionInfo{}).Where("selection_status = ?", 1)
  143. // 动态添加查询条件
  144. if param.SubAccountId == 0 {
  145. if param.EnterpriseId == "" {
  146. return reSelectionTaskPreviews, 0, errors.New("enterpriseId is empty")
  147. }
  148. query = query.Where("enterprise_id = ?", param.EnterpriseId)
  149. } else {
  150. query = query.Where("sub_account_id = ?", param.SubAccountId)
  151. }
  152. if param.SelectionPlatform != 0 {
  153. query = query.Where("platform = ?", param.SelectionPlatform)
  154. }
  155. if param.Others != "" {
  156. query = query.Where("enterprise_id = ? or selection_id = ? or selection_name LIKE ?", param.Others, param.Others, "%"+param.Others+"%")
  157. }
  158. query.Count(&total)
  159. query = query.Select("enterprise_id, sub_account_id, selection_id, platform, created_at, product_id")
  160. offset := (param.Page - 1) * param.PageSize
  161. if err := query.Order("created_at asc").Offset(offset).Limit(param.PageSize).Find(&selectionInfos).Error; err != nil {
  162. return nil, 0, err
  163. }
  164. for _, selectionInfo := range selectionInfos {
  165. reSelectionTaskPreview := vo.ReSelectionTaskPreview{
  166. EnterpriseId: selectionInfo.EnterpriseID,
  167. SubAccountId: selectionInfo.SubAccountId,
  168. SelectionId: selectionInfo.SelectionID,
  169. SelectionPlatform: selectionInfo.Platform,
  170. CreatedAt: selectionInfo.CreatedAt.Format("2006-01-02 15:04:05"),
  171. ProductId: selectionInfo.ProductID,
  172. }
  173. reSelectionTaskPreviews = append(reSelectionTaskPreviews, reSelectionTaskPreview)
  174. }
  175. return reSelectionTaskPreviews, total, nil
  176. }
  177. // 获取电商带货悬赏任务中全部指定状态值的项目
  178. func (d SelectionInfoDAO) GetSelectionInfoList(value int64, fieldName string) ([]*entity.SelectionInfo, error) {
  179. var selectionInfos []*entity.SelectionInfo
  180. err := Db.Model(entity.SelectionInfo{}).Where(fmt.Sprintf("task_mode = ? AND %s = ? ", fieldName), 1, value).Find(&selectionInfos).Error
  181. if err != nil {
  182. return nil, err
  183. }
  184. return selectionInfos, nil
  185. }
  186. // 获取电商带货冻结中的任务
  187. func (d SelectionInfoDAO) GetSelectionFrozenList(enterpriseId string) ([]*entity.SelectionInfo, error) {
  188. var selectionInfos []*entity.SelectionInfo
  189. query := Db.Debug().Model(entity.SelectionInfo{})
  190. query.Select("selection_id, product_id, enterprise_id, sub_account_id, platform, estimated_cost, pay_at") // 冻结金额:estimated_cost
  191. err := query.Where(fmt.Sprintf("enterprise_id = ? AND (selection_status between 5 and 6) "), enterpriseId).Find(&selectionInfos).Error
  192. if err != nil {
  193. if errors.Is(err, gorm.ErrRecordNotFound) {
  194. return selectionInfos, nil
  195. } else {
  196. return nil, err
  197. }
  198. }
  199. return selectionInfos, nil
  200. }
  201. // 获取电商带货冻结解除的任务
  202. func (d SelectionInfoDAO) GetSelectionFrozenCancelList(enterpriseId string) ([]*entity.SelectionInfo, error) {
  203. var selectionInfos []*entity.SelectionInfo
  204. query := Db.Debug().Model(entity.SelectionInfo{})
  205. query.Select("selection_id, product_id, enterprise_id, sub_account_id, platform, settlement_amount, pay_at") // 解冻金额:settlement_amount
  206. err := query.Where(fmt.Sprintf("enterprise_id = ? AND (selection_status between 7 and 8) "), enterpriseId).Find(&selectionInfos).Error
  207. if err != nil {
  208. if errors.Is(err, gorm.ErrRecordNotFound) {
  209. return selectionInfos, nil
  210. } else {
  211. return nil, err
  212. }
  213. }
  214. return selectionInfos, nil
  215. }
  216. // 获取带货账单列表
  217. func (d SelectionInfoDAO) GetBillSelectionPreviews(param *vo.SelectionSearchParam) ([]vo.ReBillSelectionTaskPreview, int64, error) {
  218. var reBillSelectionTaskPreviews []vo.ReBillSelectionTaskPreview
  219. var selectionInfos []entity.SelectionInfo
  220. var total int64
  221. query := Db.Model(&entity.SelectionInfo{})
  222. // 动态添加查询条件
  223. if param.SubAccountId == 0 {
  224. if param.EnterpriseId == "" {
  225. return reBillSelectionTaskPreviews, 0, errors.New("enterpriseId is empty")
  226. }
  227. query = query.Where("enterprise_id = ?", param.EnterpriseId)
  228. } else {
  229. query = query.Where("sub_account_id = ?", param.SubAccountId)
  230. }
  231. if param.SelectionPlatform != 0 {
  232. query = query.Where("platform = ?", param.SelectionPlatform)
  233. }
  234. if param.SelectionStatus != 0 {
  235. query = query.Where("selection_status = ?", param.SelectionStatus)
  236. }
  237. if param.Others != "" {
  238. query = query.Where("enterprise_id = ? or selection_id = ? or selection_name LIKE ?", param.Others, param.Others, "%"+param.Others+"%")
  239. }
  240. query.Count(&total)
  241. query = query.Select("enterprise_id, sub_account_id, selection_id, platform, selection_status, created_at, task_ddl, product_id, settlement_amount")
  242. offset := (param.Page - 1) * param.PageSize
  243. if err := query.Order("created_at asc").Offset(offset).Limit(param.PageSize).Find(&selectionInfos).Error; err != nil {
  244. return nil, 0, err
  245. }
  246. for _, selectionInfo := range selectionInfos {
  247. reBillSelectionTaskPreview := vo.ReBillSelectionTaskPreview{
  248. EnterpriseId: selectionInfo.EnterpriseID,
  249. SubAccountId: selectionInfo.SubAccountId,
  250. SelectionId: selectionInfo.SelectionID,
  251. SelectionPlatform: selectionInfo.Platform,
  252. SelectionStatus: selectionInfo.SelectionStatus,
  253. CreatedAt: selectionInfo.CreatedAt.Format("2006-01-02 15:04:05"),
  254. TaskDdl: selectionInfo.TaskDdl.Format("2006-01-02 15:04:05"),
  255. ProductId: selectionInfo.ProductID,
  256. CashAmount: selectionInfo.SettlementAmount,
  257. }
  258. reBillSelectionTaskPreviews = append(reBillSelectionTaskPreviews, reBillSelectionTaskPreview)
  259. }
  260. return reBillSelectionTaskPreviews, total, nil
  261. }
  262. // 电商带货任务待办
  263. func (d SelectionInfoDAO) GetSelectionToDo(enterpriseId string, subAccountId int64, platform int64) (map[string]int64, error) {
  264. resultMap := make(map[string]int64)
  265. var needReview int64
  266. var needPay int64
  267. var needProcess int64
  268. var selectionInfos []entity.SelectionInfo
  269. //query := Db.Model(&entity.SelectionInfo{}).Where("enterprise_id = ? and platform = ?", enterpriseId, platform)
  270. if subAccountId == 0 {
  271. // 待审核、待支付、达人未处理
  272. query1 := Db.Model(&entity.SelectionInfo{}).Where("enterprise_id = ? and platform = ?", enterpriseId, platform)
  273. query1.Where("selection_status = ?", 2).Count(&needReview)
  274. query2 := Db.Model(&entity.SelectionInfo{}).Where("enterprise_id = ? and platform = ?", enterpriseId, platform)
  275. query2.Where("selection_status = ?", 4).Count(&needPay)
  276. query3 := Db.Model(&entity.SelectionInfo{}).Where("enterprise_id = ? and platform = ?", enterpriseId, platform)
  277. err := query3.Where("selection_status = ? and sample_mode = ?", 6, 1).Select("selection_id").Find(&selectionInfos).Error
  278. if err != nil {
  279. if errors.Is(err, gorm.ErrRecordNotFound) {
  280. needProcess = 0
  281. } else {
  282. return resultMap, err
  283. }
  284. } else {
  285. var selectionIDs []string
  286. for _, info := range selectionInfos {
  287. selectionIDs = append(selectionIDs, info.SelectionID)
  288. }
  289. if len(selectionIDs) > 0 {
  290. err1 := Db.Model(&entity.SelectionTaskInfo{}).Where("selection_id in ? and task_status = ?", selectionIDs, 1).Count(&needProcess).Error // task_status=1待选
  291. if err1 != nil {
  292. needProcess = 0
  293. }
  294. }
  295. }
  296. } else {
  297. // 待审核、待支付、达人未处理
  298. query1 := Db.Model(&entity.SelectionInfo{}).Where("enterprise_id = ? and platform = ?", enterpriseId, platform)
  299. query1.Where("sub_account_id = ? and selection_status = ?", subAccountId, 2).Count(&needReview)
  300. query2 := Db.Model(&entity.SelectionInfo{}).Where("enterprise_id = ? and platform = ?", enterpriseId, platform)
  301. query2.Where("sub_account_id = ? and selection_status = ?", subAccountId, 4).Count(&needPay)
  302. query3 := Db.Model(&entity.SelectionInfo{}).Where("enterprise_id = ? and platform = ?", enterpriseId, platform)
  303. err := query3.Where("sub_account_id = ? and selection_status = ? and sample_mode = ?", subAccountId, 6, 1).Select("selection_id").Find(&selectionInfos).Error
  304. if err != nil {
  305. if errors.Is(err, gorm.ErrRecordNotFound) {
  306. needProcess = 0
  307. } else {
  308. return resultMap, err
  309. }
  310. } else {
  311. var selectionIDs []string
  312. for _, info := range selectionInfos {
  313. selectionIDs = append(selectionIDs, info.SelectionID)
  314. }
  315. if len(selectionIDs) > 0 {
  316. err1 := Db.Model(&entity.SelectionTaskInfo{}).Where("selection_id in ? and task_status = ?", selectionIDs, 1).Count(&needProcess).Error // task_status=1待选
  317. if err1 != nil {
  318. needProcess = 0
  319. }
  320. }
  321. }
  322. }
  323. resultMap["needReview"] = needReview
  324. resultMap["needPay"] = needPay
  325. resultMap["needProcess"] = needProcess
  326. return resultMap, nil
  327. }
  328. // 寄样物流任务待办
  329. func (d SelectionInfoDAO) GetLogisticsToDo(enterpriseId string, subAccountId int64, platform int64) (map[string]int64, error) {
  330. resultMap := make(map[string]int64)
  331. var needDelivery int64
  332. var needReceive int64
  333. var selectionInfos []entity.SelectionInfo
  334. //query := Db.Model(&entity.SelectionInfo{}).Where("enterprise_id = ? and platform = ?", enterpriseId, platform)
  335. if subAccountId == 0 {
  336. // 待发货、待签收
  337. query1 := Db.Model(&entity.SelectionInfo{}).Where("enterprise_id = ? and platform = ?", enterpriseId, platform)
  338. err := query1.Where("selection_status = ? and sample_mode = ?", 6, 1).Select("selection_id").Find(&selectionInfos).Error
  339. if err != nil {
  340. if errors.Is(err, gorm.ErrRecordNotFound) {
  341. needDelivery = 0
  342. needReceive = 0
  343. } else {
  344. return resultMap, err
  345. }
  346. } else {
  347. var selectionIDs []string
  348. for _, info := range selectionInfos {
  349. selectionIDs = append(selectionIDs, info.SelectionID)
  350. }
  351. if len(selectionIDs) > 0 {
  352. err1 := Db.Model(&entity.SelectionTaskInfo{}).Where("selection_id in ? and logistics_status = ?", selectionIDs, 1).Count(&needDelivery).Error // logistics_status=1待发货
  353. if err1 != nil {
  354. needDelivery = 0
  355. }
  356. err2 := Db.Model(&entity.SelectionTaskInfo{}).Where("selection_id in ? and logistics_status = ?", selectionIDs, 2).Count(&needReceive).Error // logistics_status=2待签收
  357. if err2 != nil {
  358. needReceive = 0
  359. }
  360. }
  361. }
  362. } else {
  363. query1 := Db.Model(&entity.SelectionInfo{}).Where("enterprise_id = ? and platform = ?", enterpriseId, platform)
  364. err := query1.Where("sub_account_id = ? and selection_status = ? and sample_mode = ?", subAccountId, 6, 1).Select("selection_id").Find(&selectionInfos).Error
  365. if err != nil {
  366. if errors.Is(err, gorm.ErrRecordNotFound) {
  367. needDelivery = 0
  368. needReceive = 0
  369. } else {
  370. return resultMap, err
  371. }
  372. } else {
  373. var selectionIDs []string
  374. for _, info := range selectionInfos {
  375. selectionIDs = append(selectionIDs, info.SelectionID)
  376. }
  377. if len(selectionIDs) > 0 {
  378. err1 := Db.Model(&entity.SelectionTaskInfo{}).Where("selection_id in ? and logistics_status = ?", selectionIDs, 1).Count(&needDelivery).Error // logistics_status=1待发货
  379. if err1 != nil {
  380. needDelivery = 0
  381. }
  382. err2 := Db.Model(&entity.SelectionTaskInfo{}).Where("selection_id in ? and logistics_status = ?", selectionIDs, 2).Count(&needReceive).Error // logistics_status=2待签收
  383. if err2 != nil {
  384. needReceive = 0
  385. }
  386. }
  387. }
  388. }
  389. resultMap["needDelivery"] = needDelivery
  390. resultMap["needReceive"] = needReceive
  391. return resultMap, nil
  392. }
  393. // 获取指定商家已结案的指定开票状态数据
  394. func (d SelectionInfoDAO) GetSelectionFinished(enterpriseId string, invoiceStatus int64) (float64, error) {
  395. var selectionAmount float64
  396. err := Db.Model(&entity.SelectionInfo{}).Where("enterprise_id = ? and selection_status = ? and invoice_status = ?", enterpriseId, 8, invoiceStatus).Select("COALESCE(SUM(settlement_amount), 0)").Scan(&selectionAmount).Error
  397. if err != nil {
  398. return 0, err
  399. }
  400. return selectionAmount, err
  401. }