selection_info_dao.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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.SelectionId != "" {
  93. query = query.Where("selection_id = ?", param.SelectionId)
  94. }
  95. if param.SelectionName != "" {
  96. query = query.Where("selection_name LIKE ?", "%"+param.SelectionName+"%")
  97. }
  98. query.Count(&total)
  99. 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")
  100. offset := (param.Page - 1) * param.PageSize
  101. if param.Order == 1 {
  102. if err := query.Order("selection_status desc").Order("task_ddl asc").Offset(offset).Limit(param.PageSize).Find(&selectionInfos).Error; err != nil {
  103. return nil, 0, err
  104. }
  105. } else {
  106. if err := query.Order("selection_status asc").Order("task_ddl desc").Offset(offset).Limit(param.PageSize).Find(&selectionInfos).Error; err != nil {
  107. return nil, 0, err
  108. }
  109. }
  110. for _, selectionInfo := range selectionInfos {
  111. reSelectionTaskPreview := vo.ReSelectionTaskPreview{
  112. EnterpriseId: selectionInfo.EnterpriseID,
  113. SubAccountId: selectionInfo.SubAccountId,
  114. SelectionId: selectionInfo.SelectionID,
  115. SelectionPlatform: selectionInfo.Platform,
  116. SelectionStatus: selectionInfo.SelectionStatus,
  117. CreatedAt: selectionInfo.CreatedAt.Format("2006-01-02 15:04:05"),
  118. TaskDdl: selectionInfo.TaskDdl.Format("2006-01-02 15:04:05"),
  119. SampleNum: selectionInfo.SampleNum,
  120. EnrollNum: selectionInfo.EnrollNum,
  121. ChooseNum: selectionInfo.ChooseNum,
  122. ProductId: selectionInfo.ProductID,
  123. Reward: selectionInfo.EstimatedCost,
  124. }
  125. reSelectionTaskPreviews = append(reSelectionTaskPreviews, reSelectionTaskPreview)
  126. }
  127. return reSelectionTaskPreviews, total, nil
  128. }
  129. // 删除带货任务
  130. func (d SelectionInfoDAO) DeleteSelection(selectionId string) (*string, error) {
  131. if selectionId == "" {
  132. return &selectionId, nil
  133. }
  134. err := Db.Where("selection_id = ?", selectionId).Delete(&entity.SelectionInfo{}).Error
  135. if err != nil {
  136. return nil, err
  137. }
  138. return &selectionId, nil
  139. }
  140. // 获取草稿箱——电商带货任务列表
  141. func (d SelectionInfoDAO) GetSelectionDraftList(param *vo.SelectionDraftParam) ([]vo.ReSelectionTaskPreview, int64, error) {
  142. var reSelectionTaskPreviews []vo.ReSelectionTaskPreview
  143. var selectionInfos []entity.SelectionInfo
  144. var total int64
  145. query := Db.Model(&entity.SelectionInfo{}).Where("selection_status = ?", 1)
  146. // 动态添加查询条件
  147. if param.SubAccountId == 0 {
  148. if param.EnterpriseId == "" {
  149. return reSelectionTaskPreviews, 0, errors.New("enterpriseId is empty")
  150. }
  151. query = query.Where("enterprise_id = ?", param.EnterpriseId)
  152. } else {
  153. query = query.Where("sub_account_id = ?", param.SubAccountId)
  154. }
  155. if param.SelectionPlatform != 0 {
  156. query = query.Where("platform = ?", param.SelectionPlatform)
  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.SelectionId != "" {
  238. query = query.Where("selection_id = ?", param.SelectionId)
  239. }
  240. if param.SelectionName != "" {
  241. query = query.Where("selection_name LIKE ?", "%"+param.SelectionName+"%")
  242. }
  243. query.Count(&total)
  244. query = query.Select("enterprise_id, sub_account_id, selection_id, platform, selection_status, created_at, task_ddl, product_id, settlement_amount")
  245. offset := (param.Page - 1) * param.PageSize
  246. if err := query.Order("created_at asc").Offset(offset).Limit(param.PageSize).Find(&selectionInfos).Error; err != nil {
  247. return nil, 0, err
  248. }
  249. for _, selectionInfo := range selectionInfos {
  250. reBillSelectionTaskPreview := vo.ReBillSelectionTaskPreview{
  251. EnterpriseId: selectionInfo.EnterpriseID,
  252. SubAccountId: selectionInfo.SubAccountId,
  253. SelectionId: selectionInfo.SelectionID,
  254. SelectionPlatform: selectionInfo.Platform,
  255. SelectionStatus: selectionInfo.SelectionStatus,
  256. CreatedAt: selectionInfo.CreatedAt.Format("2006-01-02 15:04:05"),
  257. TaskDdl: selectionInfo.TaskDdl.Format("2006-01-02 15:04:05"),
  258. ProductId: selectionInfo.ProductID,
  259. CashAmount: selectionInfo.SettlementAmount,
  260. }
  261. reBillSelectionTaskPreviews = append(reBillSelectionTaskPreviews, reBillSelectionTaskPreview)
  262. }
  263. return reBillSelectionTaskPreviews, total, nil
  264. }