task_info_service.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. package service
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "github.com/xuri/excelize/v2"
  7. "strconv"
  8. "youngee_b_api/app/dao"
  9. "youngee_b_api/app/entity"
  10. "youngee_b_api/app/vo"
  11. )
  12. type TaskInfoService struct{}
  13. // 带货待发货、待签收、已签收统计
  14. func (t TaskInfoService) LogisticsSelectionTalentCount(param *vo.LogisticsSelectionTalentParam) map[string]int64 {
  15. res := make(map[string]int64)
  16. var needDelivery int64
  17. var needReceive int64
  18. var received int64
  19. dao.Db.Model(&entity.SelectionTaskInfo{}).Where("selection_id = ? AND free_stage = ?", param.SelectionId, 3).Count(&needDelivery)
  20. dao.Db.Model(&entity.SelectionTaskInfo{}).Where("selection_id = ? AND free_stage = ?", param.SelectionId, 4).Count(&needReceive)
  21. dao.Db.Model(&entity.SelectionTaskInfo{}).Where("selection_id = ? AND free_stage = ?", param.SelectionId, 5).Count(&received)
  22. res["needDelivery"] = needDelivery
  23. res["needReceive"] = needReceive
  24. res["received"] = received
  25. return res
  26. }
  27. // 达人物流管理
  28. func (t TaskInfoService) LogisticsTalentList(param *vo.LogisticsTalentParam) (*vo.ResultVO, error) {
  29. if param.Page <= 0 {
  30. param.Page = 1
  31. }
  32. if param.PageSize <= 0 {
  33. param.PageSize = 10
  34. }
  35. var reLogisticsTalents []*vo.ReLogisticsTalent
  36. var total int64
  37. result := vo.ResultVO{
  38. Page: param.Page,
  39. PageSize: param.PageSize,
  40. Total: total,
  41. Data: reLogisticsTalents,
  42. }
  43. var projectTaskInfos []*entity.ProjectTaskInfo
  44. var err error
  45. projectId := param.ProjectId
  46. if param.Status == 1 { // 待发货
  47. projectTaskInfos, total, err = dao.ProjectTaskInfoDao{}.GetListByTaskStage(projectId, 4, "", param.Page, param.PageSize, param.Nickname)
  48. } else if param.Status == 2 { // 待签收
  49. projectTaskInfos, total, err = dao.ProjectTaskInfoDao{}.GetListByTaskStage(projectId, 5, param.DeliveryTime, param.Page, param.PageSize, param.Nickname)
  50. } else if param.Status == 3 { // 已签收
  51. projectTaskInfos, total, err = dao.ProjectTaskInfoDao{}.GetListByTaskStage(projectId, 6, "", param.Page, param.PageSize, param.Nickname)
  52. }
  53. if err != nil {
  54. return nil, err
  55. }
  56. for _, projectTaskInfo := range projectTaskInfos {
  57. // 获取达人信息
  58. platformKuaishouUserInfo, _ := dao.PlatformKuaishouUserInfoDao{}.GetUserInfo(projectTaskInfo.OpenID)
  59. taskLogistics, err := dao.TaskLogisticsDao{}.SelectTaskLogistics(projectTaskInfo.TaskID)
  60. if err != nil {
  61. return nil, err
  62. }
  63. talentPreview := &vo.TalentPreview{
  64. TalentId: projectTaskInfo.TalentID,
  65. TalentPhoto: platformKuaishouUserInfo.HeadUri,
  66. TalentName: platformKuaishouUserInfo.NickName,
  67. TaskId: projectTaskInfo.TaskID,
  68. Account: platformKuaishouUserInfo.OpenID,
  69. PlatformId: platformKuaishouUserInfo.PlatformID,
  70. Location: platformKuaishouUserInfo.City,
  71. Gender: platformKuaishouUserInfo.Gender,
  72. }
  73. reLogisticsTalent := &vo.ReLogisticsTalent{
  74. TaskId: projectTaskInfo.TaskID,
  75. TalentPostAddrSnap: projectTaskInfo.TalentPostAddrSnap,
  76. ReTalentPreview: talentPreview,
  77. LogisticsId: taskLogistics.LogisticsID,
  78. CompanyName: taskLogistics.CompanyName,
  79. LogisticsNumber: taskLogistics.LogisticsNumber,
  80. Operator: "",
  81. //DeliveryTime: taskLogistics.DeliveryTime.Format("2006-01-02 15:04:05"),
  82. //SignedTime: taskLogistics.SignedTime.Format("2006-01-02 15:04:05"),
  83. }
  84. if param.Status == 2 {
  85. reLogisticsTalent.DeliveryTime = projectTaskInfo.DeliveryDate.Format("2006-01-02 15:04:05")
  86. }
  87. if param.Status == 3 {
  88. reLogisticsTalent.SignedTime = projectTaskInfo.SignedTime.Format("2006-01-02 15:04:05")
  89. }
  90. reLogisticsTalents = append(reLogisticsTalents, reLogisticsTalent)
  91. }
  92. result = vo.ResultVO{
  93. Page: param.Page,
  94. PageSize: param.PageSize,
  95. Total: total,
  96. Data: reLogisticsTalents,
  97. }
  98. return &result, nil
  99. }
  100. // 导出种草达人物流数据
  101. func (t TaskInfoService) LogisticsExport(param *vo.LogisticsTalentParam) (*excelize.File, error) {
  102. // 准备数据
  103. var logisticsExports []*vo.LogisticsExport
  104. param.Page = 1
  105. param.PageSize = 1<<31 - 1
  106. var projectTaskInfos []*entity.ProjectTaskInfo
  107. var err error
  108. projectId := param.ProjectId
  109. if param.Status == 1 { // 待发货
  110. projectTaskInfos, _, err = dao.ProjectTaskInfoDao{}.GetListByTaskStage(projectId, 4, "", param.Page, param.PageSize, param.Nickname)
  111. } else if param.Status == 2 { // 待签收
  112. projectTaskInfos, _, err = dao.ProjectTaskInfoDao{}.GetListByTaskStage(projectId, 5, param.DeliveryTime, param.Page, param.PageSize, param.Nickname)
  113. } else if param.Status == 3 { // 已签收
  114. projectTaskInfos, _, err = dao.ProjectTaskInfoDao{}.GetListByTaskStage(projectId, 6, "", param.Page, param.PageSize, param.Nickname)
  115. }
  116. if err != nil {
  117. return nil, err
  118. }
  119. for _, projectTaskInfo := range projectTaskInfos {
  120. var talentDeliveryAddress entity.TalentDeliveryAddress
  121. err = json.Unmarshal([]byte(projectTaskInfo.TalentPostAddrSnap), &talentDeliveryAddress)
  122. if err != nil {
  123. fmt.Println("解析 JSON 失败:", err)
  124. return nil, err
  125. }
  126. taskLogistics, err := dao.TaskLogisticsDao{}.SelectTaskLogistics(projectTaskInfo.TaskID)
  127. if err != nil {
  128. return nil, err
  129. }
  130. logisticsExport := &vo.LogisticsExport{
  131. TalentId: projectTaskInfo.TalentID,
  132. TalentName: projectTaskInfo.TalentName,
  133. ReceiverName: talentDeliveryAddress.ReceiverName,
  134. PhoneNumber: talentDeliveryAddress.PhoneNumber,
  135. Province: strconv.Itoa(talentDeliveryAddress.RegionCode),
  136. City: strconv.Itoa(talentDeliveryAddress.RegionCode),
  137. County: strconv.Itoa(talentDeliveryAddress.RegionCode),
  138. DetailAddr: talentDeliveryAddress.DetailAddr,
  139. CompanyName: taskLogistics.CompanyName,
  140. LogisticsNumber: taskLogistics.LogisticsNumber,
  141. Operator: "",
  142. }
  143. if param.Status == 2 {
  144. logisticsExport.Time = projectTaskInfo.DeliveryDate.Format("2006-01-02 15:04:05")
  145. }
  146. if param.Status == 3 {
  147. logisticsExport.Time = projectTaskInfo.SignedTime.Format("2006-01-02 15:04:05")
  148. }
  149. logisticsExports = append(logisticsExports, logisticsExport)
  150. }
  151. // 打开 Excel 模板
  152. templatePath := "export_template.xlsx"
  153. f, err10 := excelize.OpenFile(templatePath)
  154. if err10 != nil {
  155. return nil, errors.New(fmt.Sprintf("加载模板失败: %s", err10))
  156. }
  157. // 写入数据
  158. sheet := "Sheet1"
  159. startRow := 2
  160. for i, logisticsExport := range logisticsExports {
  161. row := strconv.Itoa(startRow + i)
  162. _ = f.SetCellValue(sheet, "A"+row, logisticsExport.TalentId)
  163. _ = f.SetCellValue(sheet, "B"+row, logisticsExport.TalentName)
  164. _ = f.SetCellValue(sheet, "C"+row, logisticsExport.ReceiverName)
  165. _ = f.SetCellValue(sheet, "D"+row, logisticsExport.PhoneNumber)
  166. _ = f.SetCellValue(sheet, "E"+row, logisticsExport.Province)
  167. _ = f.SetCellValue(sheet, "F"+row, logisticsExport.City)
  168. _ = f.SetCellValue(sheet, "G"+row, logisticsExport.County)
  169. _ = f.SetCellValue(sheet, "H"+row, logisticsExport.DetailAddr)
  170. _ = f.SetCellValue(sheet, "I"+row, logisticsExport.CompanyName)
  171. _ = f.SetCellValue(sheet, "J"+row, logisticsExport.LogisticsNumber)
  172. _ = f.SetCellValue(sheet, "K"+row, logisticsExport.Operator)
  173. _ = f.SetCellValue(sheet, "L"+row, logisticsExport.Time)
  174. }
  175. return f, nil
  176. }
  177. // 种草待发货、待签收、已签收统计
  178. func (t TaskInfoService) LogisticsTalentCount(param *vo.LogisticsTalentParam) map[string]int64 {
  179. res := make(map[string]int64)
  180. var needDelivery int64
  181. var needReceive int64
  182. var received int64
  183. dao.Db.Model(&entity.ProjectTaskInfo{}).Where("project_id = ? AND task_stage = ?", param.ProjectId, 4).Count(&needDelivery)
  184. dao.Db.Model(&entity.ProjectTaskInfo{}).Where("project_id = ? AND task_stage = ?", param.ProjectId, 5).Count(&needReceive)
  185. dao.Db.Model(&entity.ProjectTaskInfo{}).Where("project_id = ? AND task_stage >= ?", param.ProjectId, 6).Count(&received)
  186. res["needDelivery"] = needDelivery
  187. res["needReceive"] = needReceive
  188. res["received"] = received
  189. return res
  190. }
  191. // 电商带货执行中-悬赏兑现
  192. func (t TaskInfoService) SelectionRewardCashDetail(param *vo.SelectionRewardCashParam) (*vo.ReSelectionRewardCash, error) {
  193. if param.Page == 0 {
  194. param.Page = 1
  195. }
  196. if param.PageSize == 0 {
  197. param.PageSize = 10
  198. }
  199. selectionInfo, err := dao.SelectionInfoDAO{}.GetSelectionInfoById(param.SelectionId)
  200. if err != nil {
  201. return nil, err
  202. }
  203. rewardPoolAmount := selectionInfo.EstimatedCost
  204. rewardPoolCashed := selectionInfo.TaskReward
  205. selectionTaskInfos, total, err1 := dao.SelectionTaskInfoDao{}.GetRewardDetailByRewardStage(param.SelectionId, 2, param.Order, param.Page, param.PageSize)
  206. if err1 != nil {
  207. return nil, err1
  208. }
  209. var talentRewardMsgs []vo.TalentRewardMsg
  210. for _, selectionTaskInfo := range selectionTaskInfos {
  211. platformKuaishouUserInfo, _ := dao.PlatformKuaishouUserInfoDao{}.GetUserInfo(selectionTaskInfo.OpenID)
  212. talentRewardMsg := vo.TalentRewardMsg{
  213. PhotoUrl: platformKuaishouUserInfo.HeadUri,
  214. Nickname: platformKuaishouUserInfo.NickName,
  215. Account: platformKuaishouUserInfo.OpenID,
  216. PlatformId: platformKuaishouUserInfo.PlatformID,
  217. City: platformKuaishouUserInfo.City,
  218. Gender: platformKuaishouUserInfo.Gender,
  219. OrderNum: selectionTaskInfo.SaleActual,
  220. CashTime: selectionTaskInfo.WithdrawDate.Format("2006-01-02 15:04:05"),
  221. TaskId: selectionTaskInfo.TaskID,
  222. }
  223. talentRewardMsgs = append(talentRewardMsgs, talentRewardMsg)
  224. }
  225. talentMsgList := vo.ResultVO{
  226. Page: param.Page,
  227. PageSize: param.PageSize,
  228. Total: total,
  229. Data: talentRewardMsgs,
  230. }
  231. reSelectionRewardCash := &vo.ReSelectionRewardCash{
  232. RewardPoolAmount: rewardPoolAmount,
  233. RewardPoolCashed: rewardPoolCashed,
  234. CashedRate: rewardPoolCashed / rewardPoolAmount,
  235. TalentNum: total,
  236. TalentMsgList: talentMsgList,
  237. }
  238. return reSelectionRewardCash, nil
  239. }