logistics.go 7.2 KB

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