selection_info_dao.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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.Others != "" {
  93. query = query.Where("enterprise_id = ? or selection_id = ? or selection_name LIKE ?", param.Others, param.Others, "%"+param.Others+"%")
  94. }
  95. query.Count(&total)
  96. 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")
  97. offset := (param.Page - 1) * param.PageSize
  98. if param.Order == 1 {
  99. if err := query.Order("selection_status desc").Order("task_ddl asc").Offset(offset).Limit(param.PageSize).Find(&selectionInfos).Error; err != nil {
  100. return nil, 0, err
  101. }
  102. } else {
  103. if err := query.Order("selection_status asc").Order("task_ddl desc").Offset(offset).Limit(param.PageSize).Find(&selectionInfos).Error; err != nil {
  104. return nil, 0, err
  105. }
  106. }
  107. for _, selectionInfo := range selectionInfos {
  108. reSelectionTaskPreview := vo.ReSelectionTaskPreview{
  109. EnterpriseId: selectionInfo.EnterpriseID,
  110. SubAccountId: selectionInfo.SubAccountId,
  111. SelectionId: selectionInfo.SelectionID,
  112. SelectionPlatform: selectionInfo.Platform,
  113. SelectionStatus: selectionInfo.SelectionStatus,
  114. CreatedAt: selectionInfo.CreatedAt.Format("2006-01-02 15:04:05"),
  115. TaskDdl: selectionInfo.TaskDdl.Format("2006-01-02 15:04:05"),
  116. SampleNum: selectionInfo.SampleNum,
  117. EnrollNum: selectionInfo.EnrollNum,
  118. ChooseNum: selectionInfo.ChooseNum,
  119. ProductId: selectionInfo.ProductID,
  120. Reward: selectionInfo.EstimatedCost,
  121. }
  122. reSelectionTaskPreviews = append(reSelectionTaskPreviews, reSelectionTaskPreview)
  123. }
  124. return reSelectionTaskPreviews, total, nil
  125. }
  126. // 删除带货任务
  127. func (d SelectionInfoDAO) DeleteSelection(selectionId string) (*string, error) {
  128. if selectionId == "" {
  129. return &selectionId, nil
  130. }
  131. err := Db.Where("selection_id = ?", selectionId).Delete(&entity.SelectionInfo{}).Error
  132. if err != nil {
  133. return nil, err
  134. }
  135. return &selectionId, nil
  136. }
  137. // 获取草稿箱——电商带货任务列表
  138. func (d SelectionInfoDAO) GetSelectionDraftList(param *vo.SelectionDraftParam) ([]vo.ReSelectionTaskPreview, int64, error) {
  139. var reSelectionTaskPreviews []vo.ReSelectionTaskPreview
  140. var selectionInfos []entity.SelectionInfo
  141. var total int64
  142. query := Db.Model(&entity.SelectionInfo{}).Where("selection_status = ?", 1)
  143. // 动态添加查询条件
  144. if param.SubAccountId == 0 {
  145. if param.EnterpriseId == "" {
  146. return reSelectionTaskPreviews, 0, errors.New("enterpriseId is empty")
  147. }
  148. query = query.Where("enterprise_id = ?", param.EnterpriseId)
  149. } else {
  150. query = query.Where("sub_account_id = ?", param.SubAccountId)
  151. }
  152. if param.SelectionPlatform != 0 {
  153. query = query.Where("platform = ?", param.SelectionPlatform)
  154. }
  155. query.Count(&total)
  156. query = query.Select("enterprise_id, sub_account_id, selection_id, platform, created_at, product_id")
  157. offset := (param.Page - 1) * param.PageSize
  158. if err := query.Order("created_at asc").Offset(offset).Limit(param.PageSize).Find(&selectionInfos).Error; err != nil {
  159. return nil, 0, err
  160. }
  161. for _, selectionInfo := range selectionInfos {
  162. reSelectionTaskPreview := vo.ReSelectionTaskPreview{
  163. EnterpriseId: selectionInfo.EnterpriseID,
  164. SubAccountId: selectionInfo.SubAccountId,
  165. SelectionId: selectionInfo.SelectionID,
  166. SelectionPlatform: selectionInfo.Platform,
  167. CreatedAt: selectionInfo.CreatedAt.Format("2006-01-02 15:04:05"),
  168. ProductId: selectionInfo.ProductID,
  169. }
  170. reSelectionTaskPreviews = append(reSelectionTaskPreviews, reSelectionTaskPreview)
  171. }
  172. return reSelectionTaskPreviews, total, nil
  173. }
  174. // 获取电商带货悬赏任务中全部指定状态值的项目
  175. func (d SelectionInfoDAO) GetSelectionInfoList(value int64, fieldName string) ([]*entity.SelectionInfo, error) {
  176. var selectionInfos []*entity.SelectionInfo
  177. err := Db.Model(entity.SelectionInfo{}).Where(fmt.Sprintf("task_mode = ? AND %s = ? ", fieldName), 1, value).Find(&selectionInfos).Error
  178. if err != nil {
  179. return nil, err
  180. }
  181. return selectionInfos, nil
  182. }
  183. // 获取电商带货冻结中的任务
  184. func (d SelectionInfoDAO) GetSelectionFrozenList(enterpriseId string) ([]*entity.SelectionInfo, error) {
  185. var selectionInfos []*entity.SelectionInfo
  186. query := Db.Debug().Model(entity.SelectionInfo{})
  187. query.Select("selection_id, product_id, enterprise_id, sub_account_id, platform, estimated_cost, pay_at") // 冻结金额:estimated_cost
  188. err := query.Where(fmt.Sprintf("enterprise_id = ? AND (selection_status between 5 and 6) "), enterpriseId).Find(&selectionInfos).Error
  189. if err != nil {
  190. if errors.Is(err, gorm.ErrRecordNotFound) {
  191. return selectionInfos, nil
  192. } else {
  193. return nil, err
  194. }
  195. }
  196. return selectionInfos, nil
  197. }
  198. // 获取电商带货冻结解除的任务
  199. func (d SelectionInfoDAO) GetSelectionFrozenCancelList(enterpriseId string) ([]*entity.SelectionInfo, error) {
  200. var selectionInfos []*entity.SelectionInfo
  201. query := Db.Debug().Model(entity.SelectionInfo{})
  202. query.Select("selection_id, product_id, enterprise_id, sub_account_id, platform, settlement_amount, pay_at") // 解冻金额:settlement_amount
  203. err := query.Where(fmt.Sprintf("enterprise_id = ? AND (selection_status between 7 and 8) "), enterpriseId).Find(&selectionInfos).Error
  204. if err != nil {
  205. if errors.Is(err, gorm.ErrRecordNotFound) {
  206. return selectionInfos, nil
  207. } else {
  208. return nil, err
  209. }
  210. }
  211. return selectionInfos, nil
  212. }
  213. // 获取带货账单列表
  214. func (d SelectionInfoDAO) GetBillSelectionPreviews(param *vo.SelectionSearchParam) ([]vo.ReBillSelectionTaskPreview, int64, error) {
  215. var reBillSelectionTaskPreviews []vo.ReBillSelectionTaskPreview
  216. var selectionInfos []entity.SelectionInfo
  217. var total int64
  218. query := Db.Model(&entity.SelectionInfo{})
  219. // 动态添加查询条件
  220. if param.SubAccountId == 0 {
  221. if param.EnterpriseId == "" {
  222. return reBillSelectionTaskPreviews, 0, errors.New("enterpriseId is empty")
  223. }
  224. query = query.Where("enterprise_id = ?", param.EnterpriseId)
  225. } else {
  226. query = query.Where("sub_account_id = ?", param.SubAccountId)
  227. }
  228. if param.SelectionPlatform != 0 {
  229. query = query.Where("platform = ?", param.SelectionPlatform)
  230. }
  231. if param.SelectionStatus != 0 {
  232. query = query.Where("selection_status = ?", param.SelectionStatus)
  233. }
  234. if param.Others != "" {
  235. query = query.Where("enterprise_id = ? or selection_id = ? or selection_name LIKE ?", param.Others, param.Others, "%"+param.Others+"%")
  236. }
  237. query.Count(&total)
  238. query = query.Select("enterprise_id, sub_account_id, selection_id, platform, selection_status, created_at, task_ddl, product_id, settlement_amount")
  239. offset := (param.Page - 1) * param.PageSize
  240. if err := query.Order("created_at asc").Offset(offset).Limit(param.PageSize).Find(&selectionInfos).Error; err != nil {
  241. return nil, 0, err
  242. }
  243. for _, selectionInfo := range selectionInfos {
  244. reBillSelectionTaskPreview := vo.ReBillSelectionTaskPreview{
  245. EnterpriseId: selectionInfo.EnterpriseID,
  246. SubAccountId: selectionInfo.SubAccountId,
  247. SelectionId: selectionInfo.SelectionID,
  248. SelectionPlatform: selectionInfo.Platform,
  249. SelectionStatus: selectionInfo.SelectionStatus,
  250. CreatedAt: selectionInfo.CreatedAt.Format("2006-01-02 15:04:05"),
  251. TaskDdl: selectionInfo.TaskDdl.Format("2006-01-02 15:04:05"),
  252. ProductId: selectionInfo.ProductID,
  253. CashAmount: selectionInfo.SettlementAmount,
  254. }
  255. reBillSelectionTaskPreviews = append(reBillSelectionTaskPreviews, reBillSelectionTaskPreview)
  256. }
  257. return reBillSelectionTaskPreviews, total, nil
  258. }
  259. // 待办
  260. func (d SelectionInfoDAO) GetSelectionToDo(enterpriseId string, subAccountId int64, platform int64) (map[string]int64, error) {
  261. resultMap := make(map[string]int64)
  262. var needReview int64
  263. var needPay int64
  264. var needProcess int64
  265. var selectionInfos []entity.SelectionInfo
  266. //query := Db.Model(&entity.SelectionInfo{}).Where("enterprise_id = ? and platform = ?", enterpriseId, platform)
  267. if subAccountId == 0 {
  268. // 待审核、待支付、达人未处理
  269. query1 := Db.Model(&entity.SelectionInfo{}).Where("enterprise_id = ? and platform = ?", enterpriseId, platform)
  270. query1.Where("selection_status = ?", 2).Count(&needReview)
  271. query2 := Db.Model(&entity.SelectionInfo{}).Where("enterprise_id = ? and platform = ?", enterpriseId, platform)
  272. query2.Where("selection_status = ?", 4).Count(&needPay)
  273. query3 := Db.Model(&entity.SelectionInfo{}).Where("enterprise_id = ? and platform = ?", enterpriseId, platform)
  274. err := query3.Where("selection_status = ? and sample_mode = ?", 6, 1).Select("selection_id").Find(&selectionInfos).Error
  275. if err != nil {
  276. if errors.Is(err, gorm.ErrRecordNotFound) {
  277. needProcess = 0
  278. } else {
  279. return resultMap, err
  280. }
  281. } else {
  282. var selectionIDs []string
  283. for _, info := range selectionInfos {
  284. selectionIDs = append(selectionIDs, info.SelectionID)
  285. }
  286. if len(selectionIDs) > 0 {
  287. err1 := Db.Model(&entity.SecTaskInfo{}).Where("selection_id in ? and task_status = ?", selectionIDs, 1).Count(&needProcess).Error // task_status=1待选
  288. if err1 != nil {
  289. needProcess = 0
  290. }
  291. }
  292. }
  293. } else {
  294. // 待审核、待支付、达人未处理
  295. query1 := Db.Model(&entity.SelectionInfo{}).Where("enterprise_id = ? and platform = ?", enterpriseId, platform)
  296. query1.Where("sub_account_id = ? and selection_status = ?", subAccountId, 2).Count(&needReview)
  297. query2 := Db.Model(&entity.SelectionInfo{}).Where("enterprise_id = ? and platform = ?", enterpriseId, platform)
  298. query2.Where("sub_account_id = ? and selection_status = ?", subAccountId, 4).Count(&needPay)
  299. query3 := Db.Model(&entity.SelectionInfo{}).Where("enterprise_id = ? and platform = ?", enterpriseId, platform)
  300. err := query3.Where("sub_account_id = ? and selection_status = ? and sample_mode = ?", subAccountId, 6, 1).Select("selection_id").Find(&selectionInfos).Error
  301. if err != nil {
  302. if errors.Is(err, gorm.ErrRecordNotFound) {
  303. needProcess = 0
  304. } else {
  305. return resultMap, err
  306. }
  307. } else {
  308. var selectionIDs []string
  309. for _, info := range selectionInfos {
  310. selectionIDs = append(selectionIDs, info.SelectionID)
  311. }
  312. if len(selectionIDs) > 0 {
  313. err1 := Db.Model(&entity.SecTaskInfo{}).Where("selection_id in ? and task_status = ?", selectionIDs, 1).Count(&needProcess).Error // task_status=1待选
  314. if err1 != nil {
  315. needProcess = 0
  316. }
  317. }
  318. }
  319. }
  320. resultMap["needReview"] = needReview
  321. resultMap["needPay"] = needPay
  322. resultMap["needProcess"] = needProcess
  323. return resultMap, nil
  324. }