logistics.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "youngee_m_api/db"
  7. "youngee_m_api/model/gorm_model"
  8. "youngee_m_api/model/http_model"
  9. "github.com/caixw/lib.go/conv"
  10. "github.com/gin-gonic/gin"
  11. "github.com/sirupsen/logrus"
  12. )
  13. var Logistics *logistics
  14. type logistics struct {
  15. }
  16. // Create 在物流信息表插入记录
  17. func (*logistics) Create(ctx context.Context, newLogistics http_model.CreateLogisticsRequest) (*http_model.CreateLogisticsData, error) {
  18. ThingsType := newLogistics.ThingsType
  19. StrategyID := newLogistics.StrategyID
  20. Logistics := gorm_model.YoungeeTaskLogistics{
  21. LogisticsID: newLogistics.LogisticsID,
  22. TaskID: newLogistics.TaskID,
  23. ThingsType: int64(ThingsType),
  24. ExplorestoreStarttime: time.Now(),
  25. ExplorestoreEndtime: time.Now(),
  26. DeliveryTime: time.Now(),
  27. }
  28. //fmt.Println("ThingsType:", ThingsType)
  29. //实物
  30. if ThingsType == 1 {
  31. Logistics.CompanyName = newLogistics.CompanyName
  32. Logistics.LogisticsNumber = newLogistics.LogisticsNumber
  33. Logistics.DeliveryTime = time.Now()
  34. } else if ThingsType == 3 {
  35. fmt.Println("开始时间:", newLogistics.ExplorestoreStarttime)
  36. fmt.Println("结束时间:", newLogistics.ExplorestoreEndtime)
  37. ExplorestoreStarttime, _ := time.ParseInLocation("2006-01-02 15:04:05", newLogistics.ExplorestoreStarttime, time.Local)
  38. ExplorestoreEndtime, _ := time.ParseInLocation("2006-01-02 15:04:05", newLogistics.ExplorestoreEndtime, time.Local)
  39. Logistics.ExplorestoreStarttime = ExplorestoreStarttime
  40. Logistics.ExplorestoreEndtime = ExplorestoreEndtime
  41. } else {
  42. Logistics.CouponCodeInformation = newLogistics.CouponCodeInformation
  43. }
  44. logisticsID, err := db.CreateLogistics(ctx, Logistics, StrategyID)
  45. if err != nil {
  46. logrus.WithContext(ctx).Errorf("[logistics service] call CreateLogistics error,err:%+v", err)
  47. return nil, err
  48. }
  49. projectId, err1 := db.GetProjectIdByTaskId(ctx, newLogistics.TaskID)
  50. if err1 != nil {
  51. logrus.WithContext(ctx).Errorf("[project service] call GetProjectIdByTaskId error,err:%+v", err1)
  52. return nil, err1
  53. }
  54. // 查询StrategyID 通过 StrategyID 和 projectId
  55. RecruitStrategyId, err2 := db.GetRecruitStrategyIdByTS(ctx, *projectId, StrategyID)
  56. if err2 != nil {
  57. logrus.WithContext(ctx).Errorf("[project service] call GetStrategyIDByTS error,err:%+v", err1)
  58. return nil, err2
  59. }
  60. fmt.Println("RecruitStrategyId: ", *RecruitStrategyId)
  61. // 修改招募策略中已签收数量
  62. err = db.UpdateLogisticsNumber(ctx, *RecruitStrategyId, 1, -1, 0)
  63. if err != nil {
  64. logrus.WithContext(ctx).Errorf("[project service] call UpdateLogisticsNumber error,err:%+v", err)
  65. return nil, err
  66. }
  67. // 修改task_info中发货状态
  68. err = db.UpdateLogisticsStatus(ctx, Logistics.TaskID, 2)
  69. if err != nil {
  70. logrus.WithContext(ctx).Errorf("[logistics service] call UpdateLogisticsStatus error,err:%+v", err)
  71. return nil, err
  72. }
  73. // 修改task_info中发货时间
  74. err = db.UpdateLogisticsDate(ctx, Logistics.TaskID)
  75. if err != nil {
  76. logrus.WithContext(ctx).Errorf("[logistics service] call UpdateLogisticsDate error,err:%+v", err)
  77. return nil, err
  78. }
  79. // 修改task_info中任务阶段
  80. err = db.UpdateTaskStageByTaskId(ctx, Logistics.TaskID, 2, 5) //修改为待传初稿
  81. if err != nil {
  82. logrus.WithContext(ctx).Errorf("[logistics service] call UpdateLogisticsDate error,err:%+v", err)
  83. return nil, err
  84. }
  85. // 对应招募策略待发货--,已发货++
  86. // 记录任务日志-发货
  87. err = db.CreateTaskLog(ctx, Logistics.TaskID, "发货时间")
  88. if err != nil {
  89. logrus.WithContext(ctx).Errorf("[logistics service] call CreateTaskLog error,err:%+v", err)
  90. return nil, err
  91. }
  92. err = db.CreateMessageByTaskId(ctx, 8, 2, Logistics.TaskID)
  93. if err != nil {
  94. logrus.WithContext(ctx).Errorf("[logistics service] call CreateMessageByTaskId error,err:%+v", err)
  95. return nil, err
  96. }
  97. res := &http_model.CreateLogisticsData{
  98. LogisticsID: *logisticsID,
  99. }
  100. return res, nil
  101. }
  102. // 修改物流信息表
  103. func (*logistics) Update(ctx context.Context, newLogistics http_model.CreateLogisticsRequest) (*http_model.CreateLogisticsData, error) {
  104. ThingsType := newLogistics.ThingsType
  105. Logistics := gorm_model.YoungeeTaskLogistics{
  106. LogisticsID: newLogistics.LogisticsID,
  107. TaskID: newLogistics.TaskID,
  108. ThingsType: int64(ThingsType),
  109. DeliveryTime: time.Now(),
  110. }
  111. //实物
  112. if ThingsType == 1 {
  113. Logistics.CompanyName = newLogistics.CompanyName
  114. Logistics.LogisticsNumber = newLogistics.LogisticsNumber
  115. } else if ThingsType == 3 {
  116. fmt.Println("开始时间:", newLogistics.ExplorestoreStarttime)
  117. fmt.Println("结束时间:", newLogistics.ExplorestoreEndtime)
  118. ExplorestoreStarttime, _ := time.ParseInLocation("2006-01-02 15:04:05", newLogistics.ExplorestoreStarttime, time.Local)
  119. ExplorestoreEndtime, _ := time.ParseInLocation("2006-01-02 15:04:05", newLogistics.ExplorestoreEndtime, time.Local)
  120. Logistics.ExplorestoreStarttime = ExplorestoreStarttime
  121. Logistics.ExplorestoreEndtime = ExplorestoreEndtime
  122. } else {
  123. Logistics.CouponCodeInformation = newLogistics.CouponCodeInformation
  124. }
  125. logisticsID, err := db.UpdateLogistics(ctx, Logistics)
  126. if err != nil {
  127. logrus.WithContext(ctx).Errorf("[logistics service] call UpdateLogistics error,err:%+v", err)
  128. return nil, err
  129. }
  130. res := &http_model.CreateLogisticsData{
  131. LogisticsID: *logisticsID,
  132. }
  133. return res, nil
  134. }
  135. // 签收
  136. func (*logistics) SignForReceipt(ctx *gin.Context, data http_model.SignForReceiptRequest) interface{} {
  137. projectId, err1 := db.GetProjectIdByTaskId(ctx, data.TaskStrategyIds[0].TaskId)
  138. if err1 != nil {
  139. logrus.WithContext(ctx).Errorf("[project service] call GetProjectIdByTaskId error,err:%+v", err1)
  140. return err1
  141. }
  142. // 签收时更新任务阶段
  143. project, err3 := db.GetProjectDetail(ctx, *projectId)
  144. if err3 != nil {
  145. logrus.WithContext(ctx).Errorf("[project service] call GetPorjectDetail error,err:%+v", err3)
  146. return err3
  147. }
  148. if project.ContentType == 1 {
  149. err := db.UpdateTaskStageByProjectId(ctx, *projectId, 2, 9)
  150. if err != nil {
  151. logrus.WithContext(ctx).Errorf("[projectPay service] call UpdateTaskStatusPaying error,err:%+v", err)
  152. return err
  153. }
  154. } else {
  155. err := db.UpdateTaskStageByProjectId(ctx, *projectId, 2, 7)
  156. if err != nil {
  157. logrus.WithContext(ctx).Errorf("[projectPay service] call UpdateTaskStatusPaying error,err:%+v", err)
  158. return err
  159. }
  160. }
  161. for _, value := range data.TaskStrategyIds {
  162. taskId := value.TaskId
  163. strategyId := conv.MustInt64(value.StrategyId, 0)
  164. err := db.UpdateLogisticsStatus(ctx, taskId, 3)
  165. if err != nil {
  166. logrus.WithContext(ctx).Errorf("[project service] call UpdateLogisticsStatus error,err:%+v", err)
  167. return err
  168. }
  169. // 签收时间
  170. err = db.SignForReceipt(ctx, taskId)
  171. if err != nil {
  172. logrus.WithContext(ctx).Errorf("[project service] call SignForReceipt error,err:%+v", err)
  173. return err
  174. }
  175. // 查询StrategyID 通过 StrategyID 和 projectId
  176. StrategyID, err2 := db.GetRecruitStrategyIdByTS(ctx, *projectId, strategyId)
  177. if err2 != nil {
  178. logrus.WithContext(ctx).Errorf("[project service] call GetStrategyIDByTS error,err:%+v", err1)
  179. return err2
  180. }
  181. // 修改招募策略中已签收数量
  182. err = db.UpdateLogisticsNumber(ctx, *StrategyID, 0, 0, 1)
  183. if err != nil {
  184. logrus.WithContext(ctx).Errorf("[project service] call UpdateLogisticsNumber error,err:%+v", err)
  185. return err
  186. }
  187. // 记录任务日志
  188. err = db.CreateTaskLog(ctx, taskId, "签收时间")
  189. if err != nil {
  190. logrus.WithContext(ctx).Errorf("[logistics service] call CreateTaskLog error,err:%+v", err)
  191. return err
  192. }
  193. err = db.CreateMessageByTaskId(ctx, 9, 2, taskId)
  194. if err != nil {
  195. logrus.WithContext(ctx).Errorf("[logistics service] call CreateMessageByTaskId error,err:%+v", err)
  196. return err
  197. }
  198. }
  199. return nil
  200. }