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