logistics.go 7.8 KB

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