selection_info_dao.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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", "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, selection_name, 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. SelectionName: selectionInfo.SelectionName,
  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.Model(&entity.SelectionInfo{}).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. if param.Others != "" {
  157. query = query.Where("enterprise_id = ? or selection_id = ? or selection_name LIKE ?", param.Others, param.Others, "%"+param.Others+"%")
  158. }
  159. query.Count(&total)
  160. query = query.Select("enterprise_id, sub_account_id, selection_id, selection_name, platform, created_at, product_id")
  161. offset := (param.Page - 1) * param.PageSize
  162. if err := query.Order("created_at asc").Offset(offset).Limit(param.PageSize).Find(&selectionInfos).Error; err != nil {
  163. return nil, 0, err
  164. }
  165. for _, selectionInfo := range selectionInfos {
  166. reSelectionTaskPreview := vo.ReSelectionTaskPreview{
  167. EnterpriseId: selectionInfo.EnterpriseID,
  168. SubAccountId: selectionInfo.SubAccountId,
  169. SelectionId: selectionInfo.SelectionID,
  170. SelectionName: selectionInfo.SelectionName,
  171. SelectionPlatform: selectionInfo.Platform,
  172. CreatedAt: selectionInfo.CreatedAt.Format("2006-01-02 15:04:05"),
  173. ProductId: selectionInfo.ProductID,
  174. }
  175. reSelectionTaskPreviews = append(reSelectionTaskPreviews, reSelectionTaskPreview)
  176. }
  177. return reSelectionTaskPreviews, total, nil
  178. }
  179. // 获取电商带货悬赏任务中全部指定状态值的项目
  180. func (d SelectionInfoDAO) GetSelectionInfoList(value int64, fieldName string) ([]*entity.SelectionInfo, error) {
  181. var selectionInfos []*entity.SelectionInfo
  182. err := Db.Model(entity.SelectionInfo{}).Where(fmt.Sprintf("task_mode = ? AND %s = ? ", fieldName), 1, value).Find(&selectionInfos).Error
  183. if err != nil {
  184. return nil, err
  185. }
  186. return selectionInfos, nil
  187. }
  188. // 获取电商带货冻结中的任务
  189. func (d SelectionInfoDAO) GetSelectionFrozenList(enterpriseId string) ([]*entity.SelectionInfo, error) {
  190. var selectionInfos []*entity.SelectionInfo
  191. query := Db.Debug().Model(entity.SelectionInfo{})
  192. query.Select("selection_id, product_id, enterprise_id, sub_account_id, platform, estimated_cost, pay_at") // 冻结金额:estimated_cost
  193. err := query.Where(fmt.Sprintf("enterprise_id = ? AND (selection_status between 5 and 6) "), enterpriseId).Find(&selectionInfos).Error
  194. if err != nil {
  195. if errors.Is(err, gorm.ErrRecordNotFound) {
  196. return selectionInfos, nil
  197. } else {
  198. return nil, err
  199. }
  200. }
  201. return selectionInfos, nil
  202. }
  203. // 获取电商带货冻结解除的任务
  204. func (d SelectionInfoDAO) GetSelectionFrozenCancelList(enterpriseId string) ([]*entity.SelectionInfo, error) {
  205. var selectionInfos []*entity.SelectionInfo
  206. query := Db.Debug().Model(entity.SelectionInfo{})
  207. query.Select("selection_id, product_id, enterprise_id, sub_account_id, platform, settlement_amount, pay_at") // 解冻金额:settlement_amount
  208. err := query.Where(fmt.Sprintf("enterprise_id = ? AND (selection_status between 7 and 8) "), enterpriseId).Find(&selectionInfos).Error
  209. if err != nil {
  210. if errors.Is(err, gorm.ErrRecordNotFound) {
  211. return selectionInfos, nil
  212. } else {
  213. return nil, err
  214. }
  215. }
  216. return selectionInfos, nil
  217. }
  218. // 获取带货账单列表
  219. func (d SelectionInfoDAO) GetBillSelectionPreviews(param *vo.SelectionSearchParam) ([]vo.ReBillSelectionTaskPreview, int64, error) {
  220. var reBillSelectionTaskPreviews []vo.ReBillSelectionTaskPreview
  221. var selectionInfos []entity.SelectionInfo
  222. var total int64
  223. query := Db.Model(&entity.SelectionInfo{})
  224. // 动态添加查询条件
  225. if param.SubAccountId == 0 {
  226. if param.EnterpriseId == "" {
  227. return reBillSelectionTaskPreviews, 0, errors.New("enterpriseId is empty")
  228. }
  229. query = query.Where("enterprise_id = ?", param.EnterpriseId)
  230. } else {
  231. query = query.Where("sub_account_id = ?", param.SubAccountId)
  232. }
  233. if param.SelectionPlatform != 0 {
  234. query = query.Where("platform = ?", param.SelectionPlatform)
  235. }
  236. if param.SelectionStatus != 0 {
  237. query = query.Where("selection_status = ?", param.SelectionStatus)
  238. }
  239. if param.Others != "" {
  240. query = query.Where("enterprise_id = ? or selection_id = ? or selection_name LIKE ?", param.Others, param.Others, "%"+param.Others+"%")
  241. }
  242. query.Count(&total)
  243. query = query.Select("enterprise_id, sub_account_id, selection_id, selection_name, platform, selection_status, created_at, task_ddl, product_id, settlement_amount")
  244. offset := (param.Page - 1) * param.PageSize
  245. if err := query.Order("created_at asc").Offset(offset).Limit(param.PageSize).Find(&selectionInfos).Error; err != nil {
  246. return nil, 0, err
  247. }
  248. for _, selectionInfo := range selectionInfos {
  249. reBillSelectionTaskPreview := vo.ReBillSelectionTaskPreview{
  250. EnterpriseId: selectionInfo.EnterpriseID,
  251. SubAccountId: selectionInfo.SubAccountId,
  252. SelectionId: selectionInfo.SelectionID,
  253. SelectionName: selectionInfo.SelectionName,
  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. }
  265. // 电商带货任务待办
  266. func (d SelectionInfoDAO) GetSelectionToDo(enterpriseId string, subAccountId int64, platform int64) (map[string]int64, error) {
  267. resultMap := make(map[string]int64)
  268. var needReview int64
  269. var needPay int64
  270. var needProcess int64
  271. var selectionInfos []entity.SelectionInfo
  272. //query := Db.Model(&entity.SelectionInfo{}).Where("enterprise_id = ? and platform = ?", enterpriseId, platform)
  273. if subAccountId == 0 {
  274. // 待审核、待支付、达人未处理
  275. query1 := Db.Model(&entity.SelectionInfo{}).Where("enterprise_id = ? and platform = ?", enterpriseId, platform)
  276. query1.Where("selection_status = ?", 2).Count(&needReview)
  277. query2 := Db.Model(&entity.SelectionInfo{}).Where("enterprise_id = ? and platform = ?", enterpriseId, platform)
  278. query2.Where("selection_status = ?", 4).Count(&needPay)
  279. query3 := Db.Model(&entity.SelectionInfo{}).Where("enterprise_id = ? and platform = ?", enterpriseId, platform)
  280. err := query3.Where("selection_status = ? and sample_mode = ?", 6, 1).Select("selection_id").Find(&selectionInfos).Error
  281. if err != nil {
  282. if errors.Is(err, gorm.ErrRecordNotFound) {
  283. needProcess = 0
  284. } else {
  285. return resultMap, err
  286. }
  287. } else {
  288. var selectionIDs []string
  289. for _, info := range selectionInfos {
  290. selectionIDs = append(selectionIDs, info.SelectionID)
  291. }
  292. if len(selectionIDs) > 0 {
  293. err1 := Db.Model(&entity.SelectionTaskInfo{}).Where("selection_id in ? and task_status = ?", selectionIDs, 1).Count(&needProcess).Error // task_status=1待选
  294. if err1 != nil {
  295. needProcess = 0
  296. }
  297. }
  298. }
  299. } else {
  300. // 待审核、待支付、达人未处理
  301. query1 := Db.Model(&entity.SelectionInfo{}).Where("enterprise_id = ? and platform = ?", enterpriseId, platform)
  302. query1.Where("sub_account_id = ? and selection_status = ?", subAccountId, 2).Count(&needReview)
  303. query2 := Db.Model(&entity.SelectionInfo{}).Where("enterprise_id = ? and platform = ?", enterpriseId, platform)
  304. query2.Where("sub_account_id = ? and selection_status = ?", subAccountId, 4).Count(&needPay)
  305. query3 := Db.Model(&entity.SelectionInfo{}).Where("enterprise_id = ? and platform = ?", enterpriseId, platform)
  306. err := query3.Where("sub_account_id = ? and selection_status = ? and sample_mode = ?", subAccountId, 6, 1).Select("selection_id").Find(&selectionInfos).Error
  307. if err != nil {
  308. if errors.Is(err, gorm.ErrRecordNotFound) {
  309. needProcess = 0
  310. } else {
  311. return resultMap, err
  312. }
  313. } else {
  314. var selectionIDs []string
  315. for _, info := range selectionInfos {
  316. selectionIDs = append(selectionIDs, info.SelectionID)
  317. }
  318. if len(selectionIDs) > 0 {
  319. err1 := Db.Model(&entity.SelectionTaskInfo{}).Where("selection_id in ? and task_status = ?", selectionIDs, 1).Count(&needProcess).Error // task_status=1待选
  320. if err1 != nil {
  321. needProcess = 0
  322. }
  323. }
  324. }
  325. }
  326. resultMap["needReview"] = needReview
  327. resultMap["needPay"] = needPay
  328. resultMap["needProcess"] = needProcess
  329. return resultMap, nil
  330. }
  331. // 寄样物流任务待办
  332. func (d SelectionInfoDAO) GetLogisticsToDo(enterpriseId string, subAccountId int64, platform int64) (map[string]int64, error) {
  333. resultMap := make(map[string]int64)
  334. var needDelivery int64
  335. var needReceive int64
  336. var selectionInfos []entity.SelectionInfo
  337. //query := Db.Model(&entity.SelectionInfo{}).Where("enterprise_id = ? and platform = ?", enterpriseId, platform)
  338. if subAccountId == 0 {
  339. // 待发货、待签收
  340. query1 := Db.Model(&entity.SelectionInfo{}).Where("enterprise_id = ? and platform = ?", enterpriseId, platform)
  341. err := query1.Where("selection_status = ? and sample_mode = ?", 6, 1).Select("selection_id").Find(&selectionInfos).Error
  342. if err != nil {
  343. if errors.Is(err, gorm.ErrRecordNotFound) {
  344. needDelivery = 0
  345. needReceive = 0
  346. } else {
  347. return resultMap, err
  348. }
  349. } else {
  350. var selectionIDs []string
  351. for _, info := range selectionInfos {
  352. selectionIDs = append(selectionIDs, info.SelectionID)
  353. }
  354. if len(selectionIDs) > 0 {
  355. err1 := Db.Model(&entity.SelectionTaskInfo{}).Where("selection_id in ? and logistics_status = ?", selectionIDs, 1).Count(&needDelivery).Error // logistics_status=1待发货
  356. if err1 != nil {
  357. needDelivery = 0
  358. }
  359. err2 := Db.Model(&entity.SelectionTaskInfo{}).Where("selection_id in ? and logistics_status = ?", selectionIDs, 2).Count(&needReceive).Error // logistics_status=2待签收
  360. if err2 != nil {
  361. needReceive = 0
  362. }
  363. }
  364. }
  365. } else {
  366. query1 := Db.Model(&entity.SelectionInfo{}).Where("enterprise_id = ? and platform = ?", enterpriseId, platform)
  367. err := query1.Where("sub_account_id = ? and selection_status = ? and sample_mode = ?", subAccountId, 6, 1).Select("selection_id").Find(&selectionInfos).Error
  368. if err != nil {
  369. if errors.Is(err, gorm.ErrRecordNotFound) {
  370. needDelivery = 0
  371. needReceive = 0
  372. } else {
  373. return resultMap, err
  374. }
  375. } else {
  376. var selectionIDs []string
  377. for _, info := range selectionInfos {
  378. selectionIDs = append(selectionIDs, info.SelectionID)
  379. }
  380. if len(selectionIDs) > 0 {
  381. err1 := Db.Model(&entity.SelectionTaskInfo{}).Where("selection_id in ? and logistics_status = ?", selectionIDs, 1).Count(&needDelivery).Error // logistics_status=1待发货
  382. if err1 != nil {
  383. needDelivery = 0
  384. }
  385. err2 := Db.Model(&entity.SelectionTaskInfo{}).Where("selection_id in ? and logistics_status = ?", selectionIDs, 2).Count(&needReceive).Error // logistics_status=2待签收
  386. if err2 != nil {
  387. needReceive = 0
  388. }
  389. }
  390. }
  391. }
  392. resultMap["needDelivery"] = needDelivery
  393. resultMap["needReceive"] = needReceive
  394. return resultMap, nil
  395. }
  396. // 获取指定商家已结案的指定开票状态数据
  397. func (d SelectionInfoDAO) GetSelectionFinished(enterpriseId string, invoiceStatus int64) (float64, error) {
  398. var selectionAmount float64
  399. err := Db.Model(&entity.SelectionInfo{}).Where("enterprise_id = ? and selection_status = ? and invoice_status = ?", enterpriseId, 8, invoiceStatus).Select("COALESCE(SUM(settlement_amount), 0)").Scan(&selectionAmount).Error
  400. if err != nil {
  401. return 0, err
  402. }
  403. return selectionAmount, err
  404. }