selection_info_dao.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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.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. }
  78. // sample_mode 1、2、3分别表示免费领样(有领样策略)、垫付领样(3.0不用)、不提供样品(无领样策略)
  79. if param.FreeFlag == 1 {
  80. query = query.Where("sample_mode = ?", 1)
  81. } else if param.FreeFlag == 2 {
  82. query = query.Where("sample_mode = ?", 3)
  83. }
  84. // task_mode 1、2分别表示悬赏任务(有悬赏策略)、纯佣带货(无悬赏策略)
  85. if param.RewardFlag == 1 {
  86. query = query.Where("task_mode = ?", 1)
  87. } else if param.RewardFlag == 2 {
  88. query = query.Where("task_mode = ?", 2)
  89. }
  90. if param.SelectionId != "" {
  91. query = query.Where("selection_id = ?", param.SelectionId)
  92. }
  93. if param.SelectionName != "" {
  94. query = query.Where("selection_name LIKE ?", "%"+param.SelectionName+"%")
  95. }
  96. query.Count(&total)
  97. 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")
  98. offset := (param.Page - 1) * param.PageSize
  99. if err := query.Order("created_at asc").Offset(offset).Limit(param.PageSize).Find(&selectionInfos).Error; err != nil {
  100. return nil, 0, err
  101. }
  102. for _, selectionInfo := range selectionInfos {
  103. reSelectionTaskPreview := vo.ReSelectionTaskPreview{
  104. EnterpriseId: selectionInfo.EnterpriseID,
  105. SubAccountId: selectionInfo.SubAccountId,
  106. SelectionId: selectionInfo.SelectionID,
  107. SelectionPlatform: selectionInfo.Platform,
  108. SelectionStatus: selectionInfo.SelectionStatus,
  109. CreatedAt: selectionInfo.CreatedAt.Format("2006-01-02 15:04:05"),
  110. TaskDdl: selectionInfo.TaskDdl.Format("2006-01-02 15:04:05"),
  111. SampleNum: selectionInfo.SampleNum,
  112. EnrollNum: selectionInfo.EnrollNum,
  113. ChooseNum: selectionInfo.ChooseNum,
  114. ProductId: selectionInfo.ProductID,
  115. }
  116. reSelectionTaskPreviews = append(reSelectionTaskPreviews, reSelectionTaskPreview)
  117. }
  118. return reSelectionTaskPreviews, total, nil
  119. }
  120. // 删除带货任务
  121. func (d SelectionInfoDAO) DeleteSelection(selectionId string) (*string, error) {
  122. if selectionId == "" {
  123. return &selectionId, nil
  124. }
  125. err := Db.Where("selection_id = ?", selectionId).Delete(&entity.SelectionInfo{}).Error
  126. if err != nil {
  127. return nil, err
  128. }
  129. return &selectionId, nil
  130. }
  131. // 获取草稿箱——电商带货任务列表
  132. func (d SelectionInfoDAO) GetSelectionDraftList(param *vo.SelectionDraftParam) ([]vo.ReSelectionTaskPreview, int64, error) {
  133. var reSelectionTaskPreviews []vo.ReSelectionTaskPreview
  134. var selectionInfos []entity.SelectionInfo
  135. var total int64
  136. query := Db.Model(&entity.SelectionInfo{}).Where("selection_status = ?", 1)
  137. // 动态添加查询条件
  138. if param.SubAccountId == 0 {
  139. if param.EnterpriseId == "" {
  140. return reSelectionTaskPreviews, 0, errors.New("enterpriseId is empty")
  141. }
  142. query = query.Where("enterprise_id = ?", param.EnterpriseId)
  143. } else {
  144. query = query.Where("sub_account_id = ?", param.SubAccountId)
  145. }
  146. if param.SelectionPlatform != 0 {
  147. query = query.Where("platform = ?", param.SelectionPlatform)
  148. }
  149. query.Count(&total)
  150. query = query.Select("enterprise_id, sub_account_id, selection_id, platform, created_at, product_id")
  151. offset := (param.Page - 1) * param.PageSize
  152. if err := query.Order("created_at asc").Offset(offset).Limit(param.PageSize).Find(&selectionInfos).Error; err != nil {
  153. return nil, 0, err
  154. }
  155. for _, selectionInfo := range selectionInfos {
  156. reSelectionTaskPreview := vo.ReSelectionTaskPreview{
  157. EnterpriseId: selectionInfo.EnterpriseID,
  158. SubAccountId: selectionInfo.SubAccountId,
  159. SelectionId: selectionInfo.SelectionID,
  160. SelectionPlatform: selectionInfo.Platform,
  161. CreatedAt: selectionInfo.CreatedAt.Format("2006-01-02 15:04:05"),
  162. ProductId: selectionInfo.ProductID,
  163. }
  164. reSelectionTaskPreviews = append(reSelectionTaskPreviews, reSelectionTaskPreview)
  165. }
  166. return reSelectionTaskPreviews, total, nil
  167. }
  168. // 获取电商带货悬赏任务中全部指定状态值的项目
  169. func (d SelectionInfoDAO) GetSelectionInfoList(value int64, fieldName string) ([]*entity.SelectionInfo, error) {
  170. var selectionInfos []*entity.SelectionInfo
  171. err := Db.Model(entity.SelectionInfo{}).Where(fmt.Sprintf("task_mode = ? AND %s = ? ", fieldName), 1, value).Find(&selectionInfos).Error
  172. if err != nil {
  173. return nil, err
  174. }
  175. return selectionInfos, nil
  176. }
  177. // 获取电商带货冻结中的任务
  178. func (d SelectionInfoDAO) GetSelectionFrozenList(enterpriseId string) ([]*entity.SelectionInfo, error) {
  179. var selectionInfos []*entity.SelectionInfo
  180. query := Db.Debug().Model(entity.SelectionInfo{})
  181. query.Select("selection_id, product_id, enterprise_id, sub_account_id, platform, estimated_cost, pay_at") // 冻结金额:estimated_cost
  182. err := query.Where(fmt.Sprintf("enterprise_id = ? AND (selection_status between 5 and 6) "), enterpriseId).Find(&selectionInfos).Error
  183. if err != nil {
  184. if errors.Is(err, gorm.ErrRecordNotFound) {
  185. return selectionInfos, nil
  186. } else {
  187. return nil, err
  188. }
  189. }
  190. return selectionInfos, nil
  191. }
  192. // 获取电商带货冻结解除的任务
  193. func (d SelectionInfoDAO) GetSelectionFrozenCancelList(enterpriseId string) ([]*entity.SelectionInfo, error) {
  194. var selectionInfos []*entity.SelectionInfo
  195. query := Db.Debug().Model(entity.SelectionInfo{})
  196. query.Select("selection_id, product_id, enterprise_id, sub_account_id, platform, settlement_amount, pay_at") // 解冻金额:settlement_amount
  197. err := query.Where(fmt.Sprintf("enterprise_id = ? AND (selection_status between 7 and 8) "), enterpriseId).Find(&selectionInfos).Error
  198. if err != nil {
  199. if errors.Is(err, gorm.ErrRecordNotFound) {
  200. return selectionInfos, nil
  201. } else {
  202. return nil, err
  203. }
  204. }
  205. return selectionInfos, nil
  206. }
  207. // 获取带货账单列表
  208. func (d SelectionInfoDAO) GetBillSelectionPreviews(param *vo.SelectionSearchParam) ([]vo.ReBillSelectionTaskPreview, int64, error) {
  209. var reBillSelectionTaskPreviews []vo.ReBillSelectionTaskPreview
  210. var selectionInfos []entity.SelectionInfo
  211. var total int64
  212. query := Db.Model(&entity.SelectionInfo{})
  213. // 动态添加查询条件
  214. if param.SubAccountId == 0 {
  215. if param.EnterpriseId == "" {
  216. return reBillSelectionTaskPreviews, 0, errors.New("enterpriseId is empty")
  217. }
  218. query = query.Where("enterprise_id = ?", param.EnterpriseId)
  219. } else {
  220. query = query.Where("sub_account_id = ?", param.SubAccountId)
  221. }
  222. if param.SelectionPlatform != 0 {
  223. query = query.Where("platform = ?", param.SelectionPlatform)
  224. }
  225. if param.SelectionStatus != 0 {
  226. query = query.Where("selection_status = ?", param.SelectionStatus)
  227. }
  228. if param.SelectionId != "" {
  229. query = query.Where("selection_id = ?", param.SelectionId)
  230. }
  231. if param.SelectionName != "" {
  232. query = query.Where("selection_name LIKE ?", "%"+param.SelectionName+"%")
  233. }
  234. query.Count(&total)
  235. query = query.Select("enterprise_id, sub_account_id, selection_id, platform, selection_status, created_at, task_ddl, product_id, settlement_amount")
  236. offset := (param.Page - 1) * param.PageSize
  237. if err := query.Order("created_at asc").Offset(offset).Limit(param.PageSize).Find(&selectionInfos).Error; err != nil {
  238. return nil, 0, err
  239. }
  240. for _, selectionInfo := range selectionInfos {
  241. reBillSelectionTaskPreview := vo.ReBillSelectionTaskPreview{
  242. EnterpriseId: selectionInfo.EnterpriseID,
  243. SubAccountId: selectionInfo.SubAccountId,
  244. SelectionId: selectionInfo.SelectionID,
  245. SelectionPlatform: selectionInfo.Platform,
  246. SelectionStatus: selectionInfo.SelectionStatus,
  247. CreatedAt: selectionInfo.CreatedAt.Format("2006-01-02 15:04:05"),
  248. TaskDdl: selectionInfo.TaskDdl.Format("2006-01-02 15:04:05"),
  249. ProductId: selectionInfo.ProductID,
  250. CashAmount: selectionInfo.SettlementAmount,
  251. }
  252. reBillSelectionTaskPreviews = append(reBillSelectionTaskPreviews, reBillSelectionTaskPreview)
  253. }
  254. return reBillSelectionTaskPreviews, total, nil
  255. }