logistics.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. }
  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. // Logistics.ExplorestorePeriod = newLogistics.ExplorestorePeriod
  123. } else {
  124. Logistics.CouponCodeInformation = newLogistics.CouponCodeInformation
  125. }
  126. logisticsID, err := db.UpdateLogistics(ctx, Logistics)
  127. if err != nil {
  128. logrus.WithContext(ctx).Errorf("[logistics service] call UpdateLogistics error,err:%+v", err)
  129. return nil, err
  130. }
  131. res := &http_model.CreateLogisticsData{
  132. LogisticsID: *logisticsID,
  133. }
  134. return res, nil
  135. }
  136. // 签收
  137. func (*logistics) SignForReceipt(ctx *gin.Context, data http_model.SignForReceiptRequest) interface{} {
  138. projectId, err1 := db.GetProjectIdByTaskId(ctx, data.TaskStrategyIds[0].TaskId)
  139. if err1 != nil {
  140. logrus.WithContext(ctx).Errorf("[project service] call GetProjectIdByTaskId error,err:%+v", err1)
  141. return err1
  142. }
  143. // 签收时更新任务阶段
  144. project, err3 := db.GetProjectDetail(ctx, *projectId)
  145. if err3 != nil {
  146. logrus.WithContext(ctx).Errorf("[project service] call GetPorjectDetail error,err:%+v", err3)
  147. return err3
  148. }
  149. if project.ContentType == 1 {
  150. err := db.UpdateTaskStageByProjectId(ctx, *projectId, 2, 9)
  151. if err != nil {
  152. logrus.WithContext(ctx).Errorf("[projectPay service] call UpdateTaskStatusPaying error,err:%+v", err)
  153. return err
  154. }
  155. } else {
  156. err := db.UpdateTaskStageByProjectId(ctx, *projectId, 2, 7)
  157. if err != nil {
  158. logrus.WithContext(ctx).Errorf("[projectPay service] call UpdateTaskStatusPaying error,err:%+v", err)
  159. return err
  160. }
  161. }
  162. for _, value := range data.TaskStrategyIds {
  163. taskId := value.TaskId
  164. strategyId := conv.MustInt64(value.StrategyId)
  165. err := db.UpdateLogisticsStatus(ctx, taskId, 3)
  166. if err != nil {
  167. logrus.WithContext(ctx).Errorf("[project service] call UpdateLogisticsStatus error,err:%+v", err)
  168. return err
  169. }
  170. // 签收时间
  171. err = db.SignForReceipt(ctx, taskId)
  172. if err != nil {
  173. logrus.WithContext(ctx).Errorf("[project service] call SignForReceipt error,err:%+v", err)
  174. return err
  175. }
  176. // 查询StrategyID 通过 StrategyID 和 projectId
  177. StrategyID, err2 := db.GetRecruitStrategyIdByTS(ctx, *projectId, strategyId)
  178. if err2 != nil {
  179. logrus.WithContext(ctx).Errorf("[project service] call GetStrategyIDByTS error,err:%+v", err1)
  180. return err2
  181. }
  182. // 修改招募策略中已签收数量
  183. err = db.UpdateLogisticsNumber(ctx, *StrategyID, 0, 0, 1)
  184. if err != nil {
  185. logrus.WithContext(ctx).Errorf("[project service] call UpdateLogisticsNumber error,err:%+v", err)
  186. return err
  187. }
  188. // 记录任务日志
  189. err = db.CreateTaskLog(ctx, taskId, "签收时间")
  190. if err != nil {
  191. logrus.WithContext(ctx).Errorf("[logistics service] call CreateTaskLog error,err:%+v", err)
  192. return err
  193. }
  194. err = db.CreateMessageByTaskId(ctx, 9, 2, taskId)
  195. if err != nil {
  196. logrus.WithContext(ctx).Errorf("[logistics service] call CreateMessageByTaskId error,err:%+v", err)
  197. return err
  198. }
  199. }
  200. return nil
  201. }