selection_info_dao.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package dao
  2. import (
  3. "errors"
  4. "gorm.io/gorm"
  5. "time"
  6. "youngee_b_api/app/entity"
  7. "youngee_b_api/app/vo"
  8. )
  9. type SelectionInfoDAO struct{}
  10. func (d SelectionInfoDAO) GetSelectionInfoById(selectionId string) (*entity.SelectionInfo, error) {
  11. var selectionInfo entity.SelectionInfo
  12. err := Db.Where("selection_id = ?", selectionId).First(&selectionInfo).Error
  13. if err != nil {
  14. if errors.Is(err, gorm.ErrRecordNotFound) {
  15. return nil, nil
  16. } else {
  17. return nil, err
  18. }
  19. }
  20. return &selectionInfo, err
  21. }
  22. // 根据enterpriseId查询指定某天的所有带货数据
  23. func (d SelectionInfoDAO) GetSelectionInfoListOfDay(enterpriseId string, date time.Time) ([]entity.SelectionInfo, error) {
  24. var selectionInfos []entity.SelectionInfo
  25. // 构建查询
  26. query := Db.Model(&entity.SelectionInfo{})
  27. if enterpriseId != "" {
  28. query = query.Where("enterprise_id = ?", enterpriseId)
  29. }
  30. // 将日期部分提取出来进行匹配
  31. query = query.Where("DATE(created_at) = ?", date.Format("2006-01-02"))
  32. err := query.Find(&selectionInfos).Error
  33. return selectionInfos, err
  34. }
  35. // 创建带货任务
  36. func (d SelectionInfoDAO) CreateSelectionInfo(selectionInfo entity.SelectionInfo) error {
  37. err := Db.Omit("task_ddl", "submit_at", "pass_at", "pay_at", "finish_at", "auto_fail_at").Create(&selectionInfo).Error
  38. if err != nil {
  39. return err
  40. }
  41. return nil
  42. }
  43. // 更新带货任务
  44. func (d SelectionInfoDAO) UpdateSelectionInfo(selectionInfo entity.SelectionInfo) error {
  45. err := Db.Model(&entity.SelectionInfo{}).Where("selection_id = ?", selectionInfo.SelectionID).Updates(selectionInfo).Error
  46. if err != nil {
  47. return err
  48. }
  49. return nil
  50. }
  51. // 获取带货任务列表
  52. func (d ProjectDAO) GetSelectionPreviews(param *vo.SelectionSearchParam) ([]vo.ReSelectionTaskPreview, int64, error) {
  53. var reSelectionTaskPreviews []vo.ReSelectionTaskPreview
  54. var selectionInfos []entity.SelectionInfo
  55. var total int64
  56. query := Db.Model(&entity.SelectionInfo{})
  57. // 动态添加查询条件
  58. if param.SubAccountId == 0 {
  59. if param.EnterpriseId == "" {
  60. return reSelectionTaskPreviews, 0, errors.New("enterpriseId is empty")
  61. }
  62. query = query.Where("enterprise_id = ?", param.EnterpriseId)
  63. } else {
  64. query = query.Where("sub_account_id = ?", param.SubAccountId)
  65. }
  66. if param.SelectionPlatform != 0 {
  67. query = query.Where("platform = ?", param.SelectionPlatform)
  68. }
  69. if param.SelectionStatus != 0 {
  70. query = query.Where("selection_status = ?", param.SelectionStatus)
  71. }
  72. // sample_mode 1、2、3分别表示免费领样(有领样策略)、垫付领样(3.0不用)、不提供样品(无领样策略)
  73. if param.FreeFlag == 1 {
  74. query = query.Where("sample_mode = ?", 1)
  75. } else if param.FreeFlag == 2 {
  76. query = query.Where("sample_mode = ?", 3)
  77. }
  78. // task_mode 1、2分别表示悬赏任务(有悬赏策略)、纯佣带货(无悬赏策略)
  79. if param.RewardFlag == 1 {
  80. query = query.Where("task_mode = ?", 1)
  81. } else if param.RewardFlag == 2 {
  82. query = query.Where("task_mode = ?", 2)
  83. }
  84. query.Count(&total)
  85. 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")
  86. offset := (param.Page - 1) * param.PageSize
  87. if err := query.Order("created_at asc").Offset(offset).Limit(param.PageSize).Find(&selectionInfos).Error; err != nil {
  88. return nil, 0, err
  89. }
  90. for _, selectionInfo := range selectionInfos {
  91. reSelectionTaskPreview := vo.ReSelectionTaskPreview{
  92. EnterpriseId: selectionInfo.EnterpriseID,
  93. SubAccountId: selectionInfo.SubAccountId,
  94. SelectionId: selectionInfo.SelectionID,
  95. SelectionPlatform: selectionInfo.Platform,
  96. SelectionStatus: selectionInfo.SelectionStatus,
  97. CreatedAt: selectionInfo.CreatedAt,
  98. TaskDdl: selectionInfo.TaskDdl,
  99. SampleNum: selectionInfo.SampleNum,
  100. EnrollNum: selectionInfo.EnrollNum,
  101. ChooseNum: selectionInfo.ChooseNum,
  102. ProductId: selectionInfo.ProductID,
  103. }
  104. reSelectionTaskPreviews = append(reSelectionTaskPreviews, reSelectionTaskPreview)
  105. }
  106. return reSelectionTaskPreviews, total, nil
  107. }
  108. // 删除带货任务
  109. func (d SelectionInfoDAO) DeleteSelection(selectionId string) (*string, error) {
  110. if selectionId == "" {
  111. return &selectionId, nil
  112. }
  113. err := Db.Where("selection_id = ?", selectionId).Delete(&entity.SelectionInfo{}).Error
  114. if err != nil {
  115. return nil, err
  116. }
  117. return &selectionId, nil
  118. }