logistics.go 7.8 KB

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