selection_info_dao.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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("task_ddl", "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.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) GetSelectionPreviews(param *vo.SelectionSearchParam) ([]vo.ReSelectionTaskPreview, int64, error) {
  54. var reSelectionTaskPreviews []vo.ReSelectionTaskPreview
  55. var selectionInfos []entity.SelectionInfo
  56. var total int64
  57. query := Db.Model(&entity.SelectionInfo{})
  58. // 动态添加查询条件
  59. if param.SubAccountId == 0 {
  60. if param.EnterpriseId == "" {
  61. return reSelectionTaskPreviews, 0, errors.New("enterpriseId is empty")
  62. }
  63. query = query.Where("enterprise_id = ?", param.EnterpriseId)
  64. } else {
  65. query = query.Where("sub_account_id = ?", param.SubAccountId)
  66. }
  67. if param.SelectionPlatform != 0 {
  68. query = query.Where("platform = ?", param.SelectionPlatform)
  69. }
  70. if param.SelectionStatus != 0 {
  71. query = query.Where("selection_status = ?", param.SelectionStatus)
  72. }
  73. // sample_mode 1、2、3分别表示免费领样(有领样策略)、垫付领样(3.0不用)、不提供样品(无领样策略)
  74. if param.FreeFlag == 1 {
  75. query = query.Where("sample_mode = ?", 1)
  76. } else if param.FreeFlag == 2 {
  77. query = query.Where("sample_mode = ?", 3)
  78. }
  79. // task_mode 1、2分别表示悬赏任务(有悬赏策略)、纯佣带货(无悬赏策略)
  80. if param.RewardFlag == 1 {
  81. query = query.Where("task_mode = ?", 1)
  82. } else if param.RewardFlag == 2 {
  83. query = query.Where("task_mode = ?", 2)
  84. }
  85. query.Count(&total)
  86. 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")
  87. offset := (param.Page - 1) * param.PageSize
  88. if err := query.Order("created_at asc").Offset(offset).Limit(param.PageSize).Find(&selectionInfos).Error; err != nil {
  89. return nil, 0, err
  90. }
  91. for _, selectionInfo := range selectionInfos {
  92. reSelectionTaskPreview := vo.ReSelectionTaskPreview{
  93. EnterpriseId: selectionInfo.EnterpriseID,
  94. SubAccountId: selectionInfo.SubAccountId,
  95. SelectionId: selectionInfo.SelectionID,
  96. SelectionPlatform: selectionInfo.Platform,
  97. SelectionStatus: selectionInfo.SelectionStatus,
  98. CreatedAt: selectionInfo.CreatedAt.Format("2006-01-02 15:04:05"),
  99. TaskDdl: selectionInfo.TaskDdl.Format("2006-01-02 15:04:05"),
  100. SampleNum: selectionInfo.SampleNum,
  101. EnrollNum: selectionInfo.EnrollNum,
  102. ChooseNum: selectionInfo.ChooseNum,
  103. ProductId: selectionInfo.ProductID,
  104. }
  105. reSelectionTaskPreviews = append(reSelectionTaskPreviews, reSelectionTaskPreview)
  106. }
  107. return reSelectionTaskPreviews, total, nil
  108. }
  109. // 删除带货任务
  110. func (d SelectionInfoDAO) DeleteSelection(selectionId string) (*string, error) {
  111. if selectionId == "" {
  112. return &selectionId, nil
  113. }
  114. err := Db.Where("selection_id = ?", selectionId).Delete(&entity.SelectionInfo{}).Error
  115. if err != nil {
  116. return nil, err
  117. }
  118. return &selectionId, nil
  119. }
  120. // 获取草稿箱——电商带货任务列表
  121. func (d SelectionInfoDAO) GetSelectionDraftList(param *vo.SelectionDraftParam) ([]vo.ReSelectionTaskPreview, int64, error) {
  122. var reSelectionTaskPreviews []vo.ReSelectionTaskPreview
  123. var selectionInfos []entity.SelectionInfo
  124. var total int64
  125. query := Db.Model(&entity.SelectionInfo{}).Where("selection_status = ?", 1)
  126. // 动态添加查询条件
  127. if param.SubAccountId == 0 {
  128. if param.EnterpriseId == "" {
  129. return reSelectionTaskPreviews, 0, errors.New("enterpriseId is empty")
  130. }
  131. query = query.Where("enterprise_id = ?", param.EnterpriseId)
  132. } else {
  133. query = query.Where("sub_account_id = ?", param.SubAccountId)
  134. }
  135. if param.SelectionPlatform != 0 {
  136. query = query.Where("platform = ?", param.SelectionPlatform)
  137. }
  138. query.Count(&total)
  139. query = query.Select("enterprise_id, sub_account_id, selection_id, platform, created_at, product_id")
  140. offset := (param.Page - 1) * param.PageSize
  141. if err := query.Order("created_at asc").Offset(offset).Limit(param.PageSize).Find(&selectionInfos).Error; err != nil {
  142. return nil, 0, err
  143. }
  144. for _, selectionInfo := range selectionInfos {
  145. reSelectionTaskPreview := vo.ReSelectionTaskPreview{
  146. EnterpriseId: selectionInfo.EnterpriseID,
  147. SubAccountId: selectionInfo.SubAccountId,
  148. SelectionId: selectionInfo.SelectionID,
  149. SelectionPlatform: selectionInfo.Platform,
  150. CreatedAt: selectionInfo.CreatedAt.Format("2006-01-02 15:04:05"),
  151. ProductId: selectionInfo.ProductID,
  152. }
  153. reSelectionTaskPreviews = append(reSelectionTaskPreviews, reSelectionTaskPreview)
  154. }
  155. return reSelectionTaskPreviews, total, nil
  156. }
  157. // 获取电商带货悬赏任务中全部指定状态值的项目
  158. func (d SelectionInfoDAO) GetSelectionInfoList(value int64, fieldName string) ([]*entity.SelectionInfo, error) {
  159. var selectionInfos []*entity.SelectionInfo
  160. err := Db.Model(entity.SelectionInfo{}).Where(fmt.Sprintf("task_mode = ? AND %s = ? ", fieldName), 1, value).Find(&selectionInfos).Error
  161. if err != nil {
  162. return nil, err
  163. }
  164. return selectionInfos, nil
  165. }
  166. // 获取电商带货冻结中的任务
  167. func (d SelectionInfoDAO) GetSelectionFrozenList(enterpriseId string) ([]*entity.SelectionInfo, error) {
  168. var selectionInfos []*entity.SelectionInfo
  169. query := Db.Debug().Model(entity.SelectionInfo{})
  170. query.Select("selection_id, product_id, enterprise_id, sub_account_id, platform, estimated_cost, pay_at") // 冻结金额:estimated_cost
  171. err := query.Where(fmt.Sprintf("enterprise_id = ? AND (selection_status between 5 and 6) "), enterpriseId).Find(&selectionInfos).Error
  172. if err != nil {
  173. if errors.Is(err, gorm.ErrRecordNotFound) {
  174. return selectionInfos, nil
  175. } else {
  176. return nil, err
  177. }
  178. }
  179. return selectionInfos, nil
  180. }
  181. // 获取电商带货冻结解除的任务
  182. func (d SelectionInfoDAO) GetSelectionFrozenCancelList(enterpriseId string) ([]*entity.SelectionInfo, error) {
  183. var selectionInfos []*entity.SelectionInfo
  184. query := Db.Debug().Model(entity.SelectionInfo{})
  185. query.Select("selection_id, product_id, enterprise_id, sub_account_id, platform, settlement_amount, pay_at") // 解冻金额:settlement_amount
  186. err := query.Where(fmt.Sprintf("enterprise_id = ? AND (selection_status between 7 and 8) "), enterpriseId).Find(&selectionInfos).Error
  187. if err != nil {
  188. if errors.Is(err, gorm.ErrRecordNotFound) {
  189. return selectionInfos, nil
  190. } else {
  191. return nil, err
  192. }
  193. }
  194. return selectionInfos, nil
  195. }