selection_info_dao.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. package dao
  2. import (
  3. "errors"
  4. "fmt"
  5. "gorm.io/gorm"
  6. "time"
  7. "youngee_m_api/app/entity"
  8. "youngee_m_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", "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, selection_name, 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. SelectionName: selectionInfo.SelectionName,
  113. SelectionPlatform: selectionInfo.Platform,
  114. SelectionStatus: selectionInfo.SelectionStatus,
  115. CreatedAt: selectionInfo.CreatedAt.Format("2006-01-02 15:04:05"),
  116. TaskDdl: selectionInfo.TaskDdl.Format("2006-01-02 15:04:05"),
  117. SampleNum: selectionInfo.SampleNum,
  118. EnrollNum: selectionInfo.EnrollNum,
  119. ChooseNum: selectionInfo.ChooseNum,
  120. ProductId: selectionInfo.ProductID,
  121. Reward: selectionInfo.EstimatedCost,
  122. }
  123. reSelectionTaskPreviews = append(reSelectionTaskPreviews, reSelectionTaskPreview)
  124. }
  125. return reSelectionTaskPreviews, total, nil
  126. }
  127. // 删除带货任务
  128. func (d SelectionInfoDAO) DeleteSelection(selectionId string) (*string, error) {
  129. if selectionId == "" {
  130. return &selectionId, nil
  131. }
  132. err := Db.Model(&entity.SelectionInfo{}).Where("selection_id = ?", selectionId).Delete(&entity.SelectionInfo{}).Error
  133. if err != nil {
  134. return nil, err
  135. }
  136. return &selectionId, nil
  137. }
  138. // 获取草稿箱——电商带货任务列表
  139. func (d SelectionInfoDAO) GetSelectionDraftList(param *vo.SelectionDraftParam) ([]vo.ReSelectionTaskPreview, int64, error) {
  140. var reSelectionTaskPreviews []vo.ReSelectionTaskPreview
  141. var selectionInfos []entity.SelectionInfo
  142. var total int64
  143. query := Db.Model(&entity.SelectionInfo{}).Where("selection_status = ?", 1)
  144. // 动态添加查询条件
  145. if param.SubAccountId == 0 {
  146. if param.EnterpriseId == "" {
  147. return reSelectionTaskPreviews, 0, errors.New("enterpriseId is empty")
  148. }
  149. query = query.Where("enterprise_id = ?", param.EnterpriseId)
  150. } else {
  151. query = query.Where("sub_account_id = ?", param.SubAccountId)
  152. }
  153. if param.SelectionPlatform != 0 {
  154. query = query.Where("platform = ?", param.SelectionPlatform)
  155. }
  156. if param.Others != "" {
  157. query = query.Where("enterprise_id = ? or selection_id = ? or selection_name LIKE ?", param.Others, param.Others, "%"+param.Others+"%")
  158. }
  159. query.Count(&total)
  160. query = query.Select("enterprise_id, sub_account_id, selection_id, selection_name, platform, created_at, product_id")
  161. offset := (param.Page - 1) * param.PageSize
  162. if err := query.Order("created_at asc").Offset(offset).Limit(param.PageSize).Find(&selectionInfos).Error; err != nil {
  163. return nil, 0, err
  164. }
  165. for _, selectionInfo := range selectionInfos {
  166. reSelectionTaskPreview := vo.ReSelectionTaskPreview{
  167. EnterpriseId: selectionInfo.EnterpriseID,
  168. SubAccountId: selectionInfo.SubAccountId,
  169. SelectionId: selectionInfo.SelectionID,
  170. SelectionName: selectionInfo.SelectionName,
  171. SelectionPlatform: selectionInfo.Platform,
  172. CreatedAt: selectionInfo.CreatedAt.Format("2006-01-02 15:04:05"),
  173. ProductId: selectionInfo.ProductID,
  174. }
  175. reSelectionTaskPreviews = append(reSelectionTaskPreviews, reSelectionTaskPreview)
  176. }
  177. return reSelectionTaskPreviews, total, nil
  178. }
  179. // 获取电商带货悬赏任务中全部指定状态值的项目
  180. func (d SelectionInfoDAO) GetSelectionInfoList(value int64, fieldName string) ([]*entity.SelectionInfo, error) {
  181. var selectionInfos []*entity.SelectionInfo
  182. err := Db.Model(entity.SelectionInfo{}).Where(fmt.Sprintf("task_mode = ? AND %s = ? ", fieldName), 1, value).Find(&selectionInfos).Error
  183. if err != nil {
  184. return nil, err
  185. }
  186. return selectionInfos, nil
  187. }
  188. // 获取电商带货冻结中的任务
  189. func (d SelectionInfoDAO) GetSelectionFrozenList(enterpriseId string) ([]*entity.SelectionInfo, error) {
  190. var selectionInfos []*entity.SelectionInfo
  191. query := Db.Debug().Model(entity.SelectionInfo{})
  192. query.Select("selection_id, product_id, enterprise_id, sub_account_id, platform, estimated_cost, pay_at") // 冻结金额:estimated_cost
  193. err := query.Where(fmt.Sprintf("enterprise_id = ? AND (selection_status between 5 and 6) "), enterpriseId).Find(&selectionInfos).Error
  194. if err != nil {
  195. if errors.Is(err, gorm.ErrRecordNotFound) {
  196. return selectionInfos, nil
  197. } else {
  198. return nil, err
  199. }
  200. }
  201. return selectionInfos, nil
  202. }
  203. // 获取所有电商带货冻结中的任务
  204. func (d SelectionInfoDAO) GetSelectionFrozenListAll() ([]*entity.SelectionInfo, error) {
  205. var selectionInfos []*entity.SelectionInfo
  206. query := Db.Debug().Model(entity.SelectionInfo{})
  207. query.Select("selection_id, product_id, enterprise_id, sub_account_id, platform, estimated_cost, pay_at") // 冻结金额:estimated_cost
  208. err := query.Where(fmt.Sprintf("selection_status between 5 and 6")).Find(&selectionInfos).Error
  209. if err != nil {
  210. if errors.Is(err, gorm.ErrRecordNotFound) {
  211. return selectionInfos, nil
  212. } else {
  213. return nil, err
  214. }
  215. }
  216. return selectionInfos, nil
  217. }
  218. // 获取电商带货冻结解除的任务
  219. func (d SelectionInfoDAO) GetSelectionFrozenCancelList(enterpriseId string) ([]*entity.SelectionInfo, error) {
  220. var selectionInfos []*entity.SelectionInfo
  221. query := Db.Debug().Model(entity.SelectionInfo{})
  222. query.Select("selection_id, product_id, enterprise_id, sub_account_id, platform, settlement_amount, pay_at") // 解冻金额:settlement_amount
  223. err := query.Where(fmt.Sprintf("enterprise_id = ? AND (selection_status between 7 and 8) "), enterpriseId).Find(&selectionInfos).Error
  224. if err != nil {
  225. if errors.Is(err, gorm.ErrRecordNotFound) {
  226. return selectionInfos, nil
  227. } else {
  228. return nil, err
  229. }
  230. }
  231. return selectionInfos, nil
  232. }
  233. // 获取所有电商带货冻结解除的任务
  234. func (d SelectionInfoDAO) GetSelectionFrozenCancelListAll() ([]*entity.SelectionInfo, error) {
  235. var selectionInfos []*entity.SelectionInfo
  236. query := Db.Debug().Model(entity.SelectionInfo{})
  237. query.Select("selection_id, product_id, enterprise_id, sub_account_id, platform, settlement_amount, pay_at") // 解冻金额:settlement_amount
  238. err := query.Where(fmt.Sprintf("selection_status between 7 and 8")).Find(&selectionInfos).Error
  239. if err != nil {
  240. if errors.Is(err, gorm.ErrRecordNotFound) {
  241. return selectionInfos, nil
  242. } else {
  243. return nil, err
  244. }
  245. }
  246. return selectionInfos, nil
  247. }
  248. // 获取带货账单列表
  249. func (d SelectionInfoDAO) GetBillSelectionPreviews(param *vo.SelectionSearchParam) ([]vo.ReBillSelectionTaskPreview, int64, error) {
  250. var reBillSelectionTaskPreviews []vo.ReBillSelectionTaskPreview
  251. var selectionInfos []entity.SelectionInfo
  252. var total int64
  253. query := Db.Model(&entity.SelectionInfo{})
  254. // 动态添加查询条件
  255. //if param.SubAccountId == 0 {
  256. // if param.EnterpriseId == "" {
  257. // return reBillSelectionTaskPreviews, 0, errors.New("enterpriseId is empty")
  258. // }
  259. // query = query.Where("enterprise_id = ?", param.EnterpriseId)
  260. //} else {
  261. // query = query.Where("sub_account_id = ?", param.SubAccountId)
  262. //}
  263. if param.SelectionPlatform != 0 {
  264. query = query.Where("platform = ?", param.SelectionPlatform)
  265. }
  266. if param.SelectionStatus != 0 {
  267. query = query.Where("selection_status = ?", param.SelectionStatus)
  268. }
  269. if param.Others != "" {
  270. query = query.Where("enterprise_id = ? or selection_id = ? or selection_name LIKE ?", param.Others, param.Others, "%"+param.Others+"%")
  271. }
  272. query.Count(&total)
  273. query = query.Select("enterprise_id, sub_account_id, selection_id, selection_name, platform, selection_status, created_at, task_ddl, product_id, settlement_amount")
  274. offset := (param.Page - 1) * param.PageSize
  275. if err := query.Order("created_at asc").Offset(offset).Limit(param.PageSize).Find(&selectionInfos).Error; err != nil {
  276. return nil, 0, err
  277. }
  278. for _, selectionInfo := range selectionInfos {
  279. reBillSelectionTaskPreview := vo.ReBillSelectionTaskPreview{
  280. EnterpriseId: selectionInfo.EnterpriseID,
  281. SubAccountId: selectionInfo.SubAccountId,
  282. SelectionId: selectionInfo.SelectionID,
  283. SelectionName: selectionInfo.SelectionName,
  284. SelectionPlatform: selectionInfo.Platform,
  285. SelectionStatus: selectionInfo.SelectionStatus,
  286. CreatedAt: selectionInfo.CreatedAt.Format("2006-01-02 15:04:05"),
  287. TaskDdl: selectionInfo.TaskDdl.Format("2006-01-02 15:04:05"),
  288. ProductId: selectionInfo.ProductID,
  289. CashAmount: selectionInfo.SettlementAmount,
  290. }
  291. reBillSelectionTaskPreviews = append(reBillSelectionTaskPreviews, reBillSelectionTaskPreview)
  292. }
  293. return reBillSelectionTaskPreviews, total, nil
  294. }
  295. // 电商带货任务待办
  296. func (d SelectionInfoDAO) GetSelectionToDo(enterpriseId string, subAccountId int64, platform int64) (map[string]int64, error) {
  297. resultMap := make(map[string]int64)
  298. var needReview int64
  299. var needPay int64
  300. var needProcess int64
  301. var selectionInfos []entity.SelectionInfo
  302. //query := Db.Model(&entity.SelectionInfo{}).Where("enterprise_id = ? and platform = ?", enterpriseId, platform)
  303. if subAccountId == 0 {
  304. // 待审核、待支付、达人未处理
  305. query1 := Db.Model(&entity.SelectionInfo{}).Where("enterprise_id = ? and platform = ?", enterpriseId, platform)
  306. query1.Where("selection_status = ?", 2).Count(&needReview)
  307. query2 := Db.Model(&entity.SelectionInfo{}).Where("enterprise_id = ? and platform = ?", enterpriseId, platform)
  308. query2.Where("selection_status = ?", 4).Count(&needPay)
  309. query3 := Db.Model(&entity.SelectionInfo{}).Where("enterprise_id = ? and platform = ?", enterpriseId, platform)
  310. err := query3.Where("selection_status = ? and sample_mode = ?", 6, 1).Select("selection_id").Find(&selectionInfos).Error
  311. if err != nil {
  312. if errors.Is(err, gorm.ErrRecordNotFound) {
  313. needProcess = 0
  314. } else {
  315. return resultMap, err
  316. }
  317. } else {
  318. var selectionIDs []string
  319. for _, info := range selectionInfos {
  320. selectionIDs = append(selectionIDs, info.SelectionID)
  321. }
  322. if len(selectionIDs) > 0 {
  323. err1 := Db.Model(&entity.SelectionTaskInfo{}).Where("selection_id in ? and task_status = ?", selectionIDs, 1).Count(&needProcess).Error // task_status=1待选
  324. if err1 != nil {
  325. needProcess = 0
  326. }
  327. }
  328. }
  329. } else {
  330. // 待审核、待支付、达人未处理
  331. query1 := Db.Model(&entity.SelectionInfo{}).Where("enterprise_id = ? and platform = ?", enterpriseId, platform)
  332. query1.Where("sub_account_id = ? and selection_status = ?", subAccountId, 2).Count(&needReview)
  333. query2 := Db.Model(&entity.SelectionInfo{}).Where("enterprise_id = ? and platform = ?", enterpriseId, platform)
  334. query2.Where("sub_account_id = ? and selection_status = ?", subAccountId, 4).Count(&needPay)
  335. query3 := Db.Model(&entity.SelectionInfo{}).Where("enterprise_id = ? and platform = ?", enterpriseId, platform)
  336. err := query3.Where("sub_account_id = ? and selection_status = ? and sample_mode = ?", subAccountId, 6, 1).Select("selection_id").Find(&selectionInfos).Error
  337. if err != nil {
  338. if errors.Is(err, gorm.ErrRecordNotFound) {
  339. needProcess = 0
  340. } else {
  341. return resultMap, err
  342. }
  343. } else {
  344. var selectionIDs []string
  345. for _, info := range selectionInfos {
  346. selectionIDs = append(selectionIDs, info.SelectionID)
  347. }
  348. if len(selectionIDs) > 0 {
  349. err1 := Db.Model(&entity.SelectionTaskInfo{}).Where("selection_id in ? and task_status = ?", selectionIDs, 1).Count(&needProcess).Error // task_status=1待选
  350. if err1 != nil {
  351. needProcess = 0
  352. }
  353. }
  354. }
  355. }
  356. resultMap["needReview"] = needReview
  357. resultMap["needPay"] = needPay
  358. resultMap["needProcess"] = needProcess
  359. return resultMap, nil
  360. }
  361. // 寄样物流任务待办
  362. func (d SelectionInfoDAO) GetLogisticsToDo(enterpriseId string, subAccountId int64, platform int64) (map[string]int64, error) {
  363. resultMap := make(map[string]int64)
  364. var needDelivery int64
  365. var needReceive int64
  366. var selectionInfos []entity.SelectionInfo
  367. //query := Db.Model(&entity.SelectionInfo{}).Where("enterprise_id = ? and platform = ?", enterpriseId, platform)
  368. if subAccountId == 0 {
  369. // 待发货、待签收
  370. query1 := Db.Model(&entity.SelectionInfo{}).Where("enterprise_id = ? and platform = ?", enterpriseId, platform)
  371. err := query1.Where("selection_status = ? and sample_mode = ?", 6, 1).Select("selection_id").Find(&selectionInfos).Error
  372. if err != nil {
  373. if errors.Is(err, gorm.ErrRecordNotFound) {
  374. needDelivery = 0
  375. needReceive = 0
  376. } else {
  377. return resultMap, err
  378. }
  379. } else {
  380. var selectionIDs []string
  381. for _, info := range selectionInfos {
  382. selectionIDs = append(selectionIDs, info.SelectionID)
  383. }
  384. if len(selectionIDs) > 0 {
  385. err1 := Db.Model(&entity.SelectionTaskInfo{}).Where("selection_id in ? and logistics_status = ?", selectionIDs, 1).Count(&needDelivery).Error // logistics_status=1待发货
  386. if err1 != nil {
  387. needDelivery = 0
  388. }
  389. err2 := Db.Model(&entity.SelectionTaskInfo{}).Where("selection_id in ? and logistics_status = ?", selectionIDs, 2).Count(&needReceive).Error // logistics_status=2待签收
  390. if err2 != nil {
  391. needReceive = 0
  392. }
  393. }
  394. }
  395. } else {
  396. query1 := Db.Model(&entity.SelectionInfo{}).Where("enterprise_id = ? and platform = ?", enterpriseId, platform)
  397. err := query1.Where("sub_account_id = ? and selection_status = ? and sample_mode = ?", subAccountId, 6, 1).Select("selection_id").Find(&selectionInfos).Error
  398. if err != nil {
  399. if errors.Is(err, gorm.ErrRecordNotFound) {
  400. needDelivery = 0
  401. needReceive = 0
  402. } else {
  403. return resultMap, err
  404. }
  405. } else {
  406. var selectionIDs []string
  407. for _, info := range selectionInfos {
  408. selectionIDs = append(selectionIDs, info.SelectionID)
  409. }
  410. if len(selectionIDs) > 0 {
  411. err1 := Db.Model(&entity.SelectionTaskInfo{}).Where("selection_id in ? and logistics_status = ?", selectionIDs, 1).Count(&needDelivery).Error // logistics_status=1待发货
  412. if err1 != nil {
  413. needDelivery = 0
  414. }
  415. err2 := Db.Model(&entity.SelectionTaskInfo{}).Where("selection_id in ? and logistics_status = ?", selectionIDs, 2).Count(&needReceive).Error // logistics_status=2待签收
  416. if err2 != nil {
  417. needReceive = 0
  418. }
  419. }
  420. }
  421. }
  422. resultMap["needDelivery"] = needDelivery
  423. resultMap["needReceive"] = needReceive
  424. return resultMap, nil
  425. }
  426. // 获取指定商家已结案的指定开票状态数据
  427. func (d SelectionInfoDAO) GetSelectionFinished(enterpriseId string, invoiceStatus int64) (float64, error) {
  428. var selectionAmount float64
  429. 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
  430. if err != nil {
  431. return 0, err
  432. }
  433. return selectionAmount, err
  434. }