logistics.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/caixw/lib.go/conv"
  6. "time"
  7. "youngee_m_api/db"
  8. "youngee_m_api/model/gorm_model"
  9. "youngee_m_api/model/http_model"
  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. 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. res := &http_model.CreateLogisticsData{
  91. LogisticsID: *logisticsID,
  92. }
  93. return res, nil
  94. }
  95. // 修改物流信息表
  96. func (*logistics) Update(ctx context.Context, newLogistics http_model.CreateLogisticsRequest) (*http_model.CreateLogisticsData, error) {
  97. ThingsType := newLogistics.ThingsType
  98. Logistics := gorm_model.YoungeeTaskLogistics{
  99. LogisticsID: newLogistics.LogisticsID,
  100. TaskID: newLogistics.TaskID,
  101. ThingsType: int64(ThingsType),
  102. DeliveryTime: time.Now(),
  103. SignedTime: time.Now(),
  104. }
  105. //实物
  106. if ThingsType == 1 {
  107. Logistics.CompanyName = newLogistics.CompanyName
  108. Logistics.LogisticsNumber = newLogistics.LogisticsNumber
  109. } else if ThingsType == 3 {
  110. fmt.Println("开始时间:", newLogistics.ExplorestoreStarttime)
  111. Logistics.ExplorestoreStarttime = newLogistics.ExplorestoreStarttime
  112. Logistics.ExplorestoreEndtime = newLogistics.ExplorestoreEndtime
  113. // Logistics.ExplorestorePeriod = newLogistics.ExplorestorePeriod
  114. } else {
  115. Logistics.CouponCodeInformation = newLogistics.CouponCodeInformation
  116. }
  117. logisticsID, err := db.UpdateLogistics(ctx, Logistics)
  118. if err != nil {
  119. logrus.WithContext(ctx).Errorf("[logistics service] call UpdateLogistics error,err:%+v", err)
  120. return nil, err
  121. }
  122. res := &http_model.CreateLogisticsData{
  123. LogisticsID: *logisticsID,
  124. }
  125. return res, nil
  126. }
  127. // 签收
  128. func (*logistics) SignForReceipt(ctx *gin.Context, data http_model.SignForReceiptRequest) interface{} {
  129. projectId, err1 := db.GetProjectIdByTaskId(ctx, data.TaskStrategyIds[0].TaskId)
  130. if err1 != nil {
  131. logrus.WithContext(ctx).Errorf("[project service] call GetProjectIdByTaskId error,err:%+v", err1)
  132. return err1
  133. }
  134. // 签收时更新任务阶段
  135. project, err3 := db.GetProjectDetail(ctx, *projectId)
  136. if err3 != nil {
  137. logrus.WithContext(ctx).Errorf("[project service] call GetPorjectDetail error,err:%+v", err3)
  138. return err3
  139. }
  140. if project.ContentType == 1 {
  141. err := db.UpdateTaskStageByProjectId(ctx, *projectId, 2, 9)
  142. if err != nil {
  143. logrus.WithContext(ctx).Errorf("[projectPay service] call UpdateTaskStatusPaying error,err:%+v", err)
  144. return err
  145. }
  146. } else {
  147. err := db.UpdateTaskStageByProjectId(ctx, *projectId, 2, 7)
  148. if err != nil {
  149. logrus.WithContext(ctx).Errorf("[projectPay service] call UpdateTaskStatusPaying error,err:%+v", err)
  150. return err
  151. }
  152. }
  153. for _, value := range data.TaskStrategyIds {
  154. taskId := value.TaskId
  155. strategyId := conv.MustInt64(value.StrategyId, 0)
  156. err := db.UpdateLogisticsStatus(ctx, taskId, 3)
  157. if err != nil {
  158. logrus.WithContext(ctx).Errorf("[project service] call UpdateLogisticsStatus error,err:%+v", err)
  159. return err
  160. }
  161. // 签收时间
  162. err = db.SignForReceipt(ctx, taskId)
  163. if err != nil {
  164. logrus.WithContext(ctx).Errorf("[project service] call SignForReceipt error,err:%+v", err)
  165. return err
  166. }
  167. // 查询StrategyID 通过 StrategyID 和 projectId
  168. StrategyID, err2 := db.GetRecruitStrategyIdByTS(ctx, *projectId, strategyId)
  169. if err2 != nil {
  170. logrus.WithContext(ctx).Errorf("[project service] call GetStrategyIDByTS error,err:%+v", err1)
  171. return err2
  172. }
  173. // 修改招募策略中已签收数量
  174. err = db.UpdateLogisticsNumber(ctx, *StrategyID, 0, 0, 1)
  175. if err != nil {
  176. logrus.WithContext(ctx).Errorf("[project service] call UpdateLogisticsNumber error,err:%+v", err)
  177. return err
  178. }
  179. // 记录任务日志
  180. err = db.CreateTaskLog(ctx, taskId, "签收时间")
  181. if err != nil {
  182. logrus.WithContext(ctx).Errorf("[logistics service] call CreateTaskLog error,err:%+v", err)
  183. return err
  184. }
  185. }
  186. return nil
  187. }