selection_info_dao.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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, estimated_cost")
  98. offset := (param.Page - 1) * param.PageSize
  99. if param.Order == 1 {
  100. if err := query.Order("selection_status desc").Order("task_ddl asc").Offset(offset).Limit(param.PageSize).Find(&selectionInfos).Error; err != nil {
  101. return nil, 0, err
  102. }
  103. } else {
  104. if err := query.Order("selection_status asc").Order("task_ddl desc").Offset(offset).Limit(param.PageSize).Find(&selectionInfos).Error; err != nil {
  105. return nil, 0, err
  106. }
  107. }
  108. for _, selectionInfo := range selectionInfos {
  109. reSelectionTaskPreview := vo.ReSelectionTaskPreview{
  110. EnterpriseId: selectionInfo.EnterpriseID,
  111. SubAccountId: selectionInfo.SubAccountId,
  112. SelectionId: selectionInfo.SelectionID,
  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.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. query.Count(&total)
  157. query = query.Select("enterprise_id, sub_account_id, selection_id, platform, created_at, product_id")
  158. offset := (param.Page - 1) * param.PageSize
  159. if err := query.Order("created_at asc").Offset(offset).Limit(param.PageSize).Find(&selectionInfos).Error; err != nil {
  160. return nil, 0, err
  161. }
  162. for _, selectionInfo := range selectionInfos {
  163. reSelectionTaskPreview := vo.ReSelectionTaskPreview{
  164. EnterpriseId: selectionInfo.EnterpriseID,
  165. SubAccountId: selectionInfo.SubAccountId,
  166. SelectionId: selectionInfo.SelectionID,
  167. SelectionPlatform: selectionInfo.Platform,
  168. CreatedAt: selectionInfo.CreatedAt.Format("2006-01-02 15:04:05"),
  169. ProductId: selectionInfo.ProductID,
  170. }
  171. reSelectionTaskPreviews = append(reSelectionTaskPreviews, reSelectionTaskPreview)
  172. }
  173. return reSelectionTaskPreviews, total, nil
  174. }
  175. // 获取电商带货悬赏任务中全部指定状态值的项目
  176. func (d SelectionInfoDAO) GetSelectionInfoList(value int64, fieldName string) ([]*entity.SelectionInfo, error) {
  177. var selectionInfos []*entity.SelectionInfo
  178. err := Db.Model(entity.SelectionInfo{}).Where(fmt.Sprintf("task_mode = ? AND %s = ? ", fieldName), 1, value).Find(&selectionInfos).Error
  179. if err != nil {
  180. return nil, err
  181. }
  182. return selectionInfos, nil
  183. }
  184. // 获取电商带货冻结中的任务
  185. func (d SelectionInfoDAO) GetSelectionFrozenList(enterpriseId string) ([]*entity.SelectionInfo, error) {
  186. var selectionInfos []*entity.SelectionInfo
  187. query := Db.Debug().Model(entity.SelectionInfo{})
  188. query.Select("selection_id, product_id, enterprise_id, sub_account_id, platform, estimated_cost, pay_at") // 冻结金额:estimated_cost
  189. err := query.Where(fmt.Sprintf("enterprise_id = ? AND (selection_status between 5 and 6) "), enterpriseId).Find(&selectionInfos).Error
  190. if err != nil {
  191. if errors.Is(err, gorm.ErrRecordNotFound) {
  192. return selectionInfos, nil
  193. } else {
  194. return nil, err
  195. }
  196. }
  197. return selectionInfos, nil
  198. }
  199. // 获取电商带货冻结解除的任务
  200. func (d SelectionInfoDAO) GetSelectionFrozenCancelList(enterpriseId string) ([]*entity.SelectionInfo, error) {
  201. var selectionInfos []*entity.SelectionInfo
  202. query := Db.Debug().Model(entity.SelectionInfo{})
  203. query.Select("selection_id, product_id, enterprise_id, sub_account_id, platform, settlement_amount, pay_at") // 解冻金额:settlement_amount
  204. err := query.Where(fmt.Sprintf("enterprise_id = ? AND (selection_status between 7 and 8) "), enterpriseId).Find(&selectionInfos).Error
  205. if err != nil {
  206. if errors.Is(err, gorm.ErrRecordNotFound) {
  207. return selectionInfos, nil
  208. } else {
  209. return nil, err
  210. }
  211. }
  212. return selectionInfos, nil
  213. }
  214. // 获取带货账单列表
  215. func (d SelectionInfoDAO) GetBillSelectionPreviews(param *vo.SelectionSearchParam) ([]vo.ReBillSelectionTaskPreview, int64, error) {
  216. var reBillSelectionTaskPreviews []vo.ReBillSelectionTaskPreview
  217. var selectionInfos []entity.SelectionInfo
  218. var total int64
  219. query := Db.Model(&entity.SelectionInfo{})
  220. // 动态添加查询条件
  221. if param.SubAccountId == 0 {
  222. if param.EnterpriseId == "" {
  223. return reBillSelectionTaskPreviews, 0, errors.New("enterpriseId is empty")
  224. }
  225. query = query.Where("enterprise_id = ?", param.EnterpriseId)
  226. } else {
  227. query = query.Where("sub_account_id = ?", param.SubAccountId)
  228. }
  229. if param.SelectionPlatform != 0 {
  230. query = query.Where("platform = ?", param.SelectionPlatform)
  231. }
  232. if param.SelectionStatus != 0 {
  233. query = query.Where("selection_status = ?", param.SelectionStatus)
  234. }
  235. if param.SelectionId != "" {
  236. query = query.Where("selection_id = ?", param.SelectionId)
  237. }
  238. if param.SelectionName != "" {
  239. query = query.Where("selection_name LIKE ?", "%"+param.SelectionName+"%")
  240. }
  241. query.Count(&total)
  242. query = query.Select("enterprise_id, sub_account_id, selection_id, platform, selection_status, created_at, task_ddl, product_id, settlement_amount")
  243. offset := (param.Page - 1) * param.PageSize
  244. if err := query.Order("created_at asc").Offset(offset).Limit(param.PageSize).Find(&selectionInfos).Error; err != nil {
  245. return nil, 0, err
  246. }
  247. for _, selectionInfo := range selectionInfos {
  248. reBillSelectionTaskPreview := vo.ReBillSelectionTaskPreview{
  249. EnterpriseId: selectionInfo.EnterpriseID,
  250. SubAccountId: selectionInfo.SubAccountId,
  251. SelectionId: selectionInfo.SelectionID,
  252. SelectionPlatform: selectionInfo.Platform,
  253. SelectionStatus: selectionInfo.SelectionStatus,
  254. CreatedAt: selectionInfo.CreatedAt.Format("2006-01-02 15:04:05"),
  255. TaskDdl: selectionInfo.TaskDdl.Format("2006-01-02 15:04:05"),
  256. ProductId: selectionInfo.ProductID,
  257. CashAmount: selectionInfo.SettlementAmount,
  258. }
  259. reBillSelectionTaskPreviews = append(reBillSelectionTaskPreviews, reBillSelectionTaskPreview)
  260. }
  261. return reBillSelectionTaskPreviews, total, nil
  262. }