task_info_service.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. talentInfo, err := dao.TalentInfoDao{}.SelectTalentInfo(projectTaskInfo.TalentID)
  59. if err != nil {
  60. return nil, err
  61. }
  62. regionName, err := dao.RegionInfoDao{}.SelectRegion(talentInfo.VisitStoreRegion)
  63. if err != nil {
  64. regionName = ""
  65. }
  66. taskLogistics, err := dao.TaskLogisticsDao{}.SelectTaskLogistics(projectTaskInfo.TaskID)
  67. if err != nil {
  68. return nil, err
  69. }
  70. talentPreview := &vo.TalentPreview{
  71. TalentId: projectTaskInfo.TalentID,
  72. TalentPhoto: talentInfo.Avatar,
  73. TalentName: talentInfo.TalentWxNickname,
  74. Account: "",
  75. Location: regionName,
  76. Gender: talentInfo.Sex,
  77. }
  78. reLogisticsTalent := &vo.ReLogisticsTalent{
  79. TaskId: projectTaskInfo.TaskID,
  80. TalentPostAddrSnap: projectTaskInfo.TalentPostAddrSnap,
  81. ReTalentPreview: talentPreview,
  82. LogisticsId: taskLogistics.LogisticsID,
  83. CompanyName: taskLogistics.CompanyName,
  84. LogisticsNumber: taskLogistics.LogisticsNumber,
  85. Operator: "",
  86. //DeliveryTime: taskLogistics.DeliveryTime.Format("2006-01-02 15:04:05"),
  87. //SignedTime: taskLogistics.SignedTime.Format("2006-01-02 15:04:05"),
  88. }
  89. if param.Status == 2 {
  90. reLogisticsTalent.DeliveryTime = projectTaskInfo.DeliveryDate.Format("2006-01-02 15:04:05")
  91. }
  92. if param.Status == 3 {
  93. reLogisticsTalent.SignedTime = projectTaskInfo.SignedTime.Format("2006-01-02 15:04:05")
  94. }
  95. reLogisticsTalents = append(reLogisticsTalents, reLogisticsTalent)
  96. }
  97. result = vo.ResultVO{
  98. Page: param.Page,
  99. PageSize: param.PageSize,
  100. Total: total,
  101. Data: reLogisticsTalents,
  102. }
  103. return &result, nil
  104. }
  105. // 导出种草达人物流数据
  106. func (t TaskInfoService) LogisticsExport(param *vo.LogisticsTalentParam) (*excelize.File, error) {
  107. // 准备数据
  108. var logisticsExports []*vo.LogisticsExport
  109. param.Page = 1
  110. param.PageSize = 1<<31 - 1
  111. var projectTaskInfos []*entity.ProjectTaskInfo
  112. var err error
  113. projectId := param.ProjectId
  114. if param.Status == 1 { // 待发货
  115. projectTaskInfos, _, err = dao.ProjectTaskInfoDao{}.GetListByTaskStage(projectId, 4, "", param.Page, param.PageSize, param.Nickname)
  116. } else if param.Status == 2 { // 待签收
  117. projectTaskInfos, _, err = dao.ProjectTaskInfoDao{}.GetListByTaskStage(projectId, 5, param.DeliveryTime, param.Page, param.PageSize, param.Nickname)
  118. } else if param.Status == 3 { // 已签收
  119. projectTaskInfos, _, err = dao.ProjectTaskInfoDao{}.GetListByTaskStage(projectId, 6, "", param.Page, param.PageSize, param.Nickname)
  120. }
  121. if err != nil {
  122. return nil, err
  123. }
  124. for _, projectTaskInfo := range projectTaskInfos {
  125. var talentDeliveryAddress entity.TalentDeliveryAddress
  126. err = json.Unmarshal([]byte(projectTaskInfo.TalentPostAddrSnap), &talentDeliveryAddress)
  127. if err != nil {
  128. fmt.Println("解析 JSON 失败:", err)
  129. return nil, err
  130. }
  131. taskLogistics, err := dao.TaskLogisticsDao{}.SelectTaskLogistics(projectTaskInfo.TaskID)
  132. if err != nil {
  133. return nil, err
  134. }
  135. logisticsExport := &vo.LogisticsExport{
  136. TalentId: projectTaskInfo.TalentID,
  137. TalentName: projectTaskInfo.TalentName,
  138. ReceiverName: talentDeliveryAddress.ReceiverName,
  139. PhoneNumber: talentDeliveryAddress.PhoneNumber,
  140. Province: strconv.Itoa(talentDeliveryAddress.RegionCode),
  141. City: strconv.Itoa(talentDeliveryAddress.RegionCode),
  142. County: strconv.Itoa(talentDeliveryAddress.RegionCode),
  143. DetailAddr: talentDeliveryAddress.DetailAddr,
  144. CompanyName: taskLogistics.CompanyName,
  145. LogisticsNumber: taskLogistics.LogisticsNumber,
  146. Operator: "",
  147. }
  148. if param.Status == 2 {
  149. logisticsExport.Time = projectTaskInfo.DeliveryDate.Format("2006-01-02 15:04:05")
  150. }
  151. if param.Status == 3 {
  152. logisticsExport.Time = projectTaskInfo.SignedTime.Format("2006-01-02 15:04:05")
  153. }
  154. logisticsExports = append(logisticsExports, logisticsExport)
  155. }
  156. // 打开 Excel 模板
  157. templatePath := "export_template.xlsx"
  158. f, err10 := excelize.OpenFile(templatePath)
  159. if err10 != nil {
  160. return nil, errors.New(fmt.Sprintf("加载模板失败: %s", err10))
  161. }
  162. // 写入数据
  163. sheet := "Sheet1"
  164. startRow := 2
  165. for i, logisticsExport := range logisticsExports {
  166. row := strconv.Itoa(startRow + i)
  167. _ = f.SetCellValue(sheet, "A"+row, logisticsExport.TalentId)
  168. _ = f.SetCellValue(sheet, "B"+row, logisticsExport.TalentName)
  169. _ = f.SetCellValue(sheet, "C"+row, logisticsExport.ReceiverName)
  170. _ = f.SetCellValue(sheet, "D"+row, logisticsExport.PhoneNumber)
  171. _ = f.SetCellValue(sheet, "E"+row, logisticsExport.Province)
  172. _ = f.SetCellValue(sheet, "F"+row, logisticsExport.City)
  173. _ = f.SetCellValue(sheet, "G"+row, logisticsExport.County)
  174. _ = f.SetCellValue(sheet, "H"+row, logisticsExport.DetailAddr)
  175. _ = f.SetCellValue(sheet, "I"+row, logisticsExport.CompanyName)
  176. _ = f.SetCellValue(sheet, "J"+row, logisticsExport.LogisticsNumber)
  177. _ = f.SetCellValue(sheet, "K"+row, logisticsExport.Operator)
  178. _ = f.SetCellValue(sheet, "L"+row, logisticsExport.Time)
  179. }
  180. return f, nil
  181. }
  182. // 种草待发货、待签收、已签收统计
  183. func (t TaskInfoService) LogisticsTalentCount(param *vo.LogisticsTalentParam) map[string]int64 {
  184. res := make(map[string]int64)
  185. var needDelivery int64
  186. var needReceive int64
  187. var received int64
  188. dao.Db.Model(&entity.ProjectTaskInfo{}).Where("project_id = ? AND task_stage = ?", param.ProjectId, 4).Count(&needDelivery)
  189. dao.Db.Model(&entity.ProjectTaskInfo{}).Where("project_id = ? AND task_stage = ?", param.ProjectId, 5).Count(&needReceive)
  190. dao.Db.Model(&entity.ProjectTaskInfo{}).Where("project_id = ? AND task_stage >= ?", param.ProjectId, 6).Count(&received)
  191. res["needDelivery"] = needDelivery
  192. res["needReceive"] = needReceive
  193. res["received"] = received
  194. return res
  195. }
  196. func (t TaskInfoService) SelectionRewardCashDetail(param *vo.SelectionRewardCashParam) (*vo.ReSelectionRewardCash, error) {
  197. if param.Page == 0 {
  198. param.Page = 1
  199. }
  200. if param.PageSize == 0 {
  201. param.PageSize = 10
  202. }
  203. selectionInfo, err := dao.SelectionInfoDAO{}.GetSelectionInfoById(param.SelectionId)
  204. if err != nil {
  205. return nil, err
  206. }
  207. rewardPoolAmount := selectionInfo.EstimatedCost
  208. rewardPoolCashed := selectionInfo.TaskReward
  209. selectionTaskInfos, total, err1 := dao.SelectionTaskInfoDao{}.GetRewardDetailByRewardStage(param.SelectionId, 2, param.Order, param.Page, param.PageSize)
  210. if err1 != nil {
  211. return nil, err1
  212. }
  213. var talentRewardMsgs []vo.TalentRewardMsg
  214. for _, selectionTaskInfo := range selectionTaskInfos {
  215. talentInfo, _ := dao.TalentInfoDao{}.SelectTalentInfo(selectionTaskInfo.TalentID)
  216. talentRewardMsg := vo.TalentRewardMsg{
  217. PhotoUrl: talentInfo.Avatar,
  218. Nickname: talentInfo.TalentNickname,
  219. Account: talentInfo.TalentWxOpenid,
  220. Gender: talentInfo.Sex,
  221. OrderNum: selectionTaskInfo.SaleActual,
  222. CashTime: selectionTaskInfo.WithdrawDate.Format("2006-01-02 15:04:05"),
  223. }
  224. talentRewardMsgs = append(talentRewardMsgs, talentRewardMsg)
  225. }
  226. talentMsgList := vo.ResultVO{
  227. Page: param.Page,
  228. PageSize: param.PageSize,
  229. Total: total,
  230. Data: talentRewardMsgs,
  231. }
  232. reSelectionRewardCash := &vo.ReSelectionRewardCash{
  233. RewardPoolAmount: rewardPoolAmount,
  234. RewardPoolCashed: rewardPoolCashed,
  235. CashedRate: rewardPoolCashed / rewardPoolAmount,
  236. TalentNum: total,
  237. TalentMsgList: talentMsgList,
  238. }
  239. return reSelectionRewardCash, nil
  240. }