logistics.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. }
  202. // **
  203. func (l *logistics) CreateSpecialLogistics(ctx context.Context, newLogistics http_model.CreateSpecialLogisticsRequest) (*http_model.SpecialLogisticsData, error) {
  204. ThingsType := newLogistics.ThingsType
  205. Logistics := gorm_model.YoungeeTaskLogistics{
  206. LogisticsID: newLogistics.LogisticsID,
  207. TaskID: newLogistics.TaskID,
  208. ThingsType: int64(ThingsType),
  209. ExplorestoreStarttime: time.Now(),
  210. ExplorestoreEndtime: time.Now(),
  211. DeliveryTime: time.Now(),
  212. }
  213. //实物
  214. if ThingsType == 1 {
  215. Logistics.CompanyName = newLogistics.CompanyName
  216. Logistics.LogisticsNumber = newLogistics.LogisticsNumber
  217. Logistics.DeliveryTime = time.Now()
  218. } else if ThingsType == 3 {
  219. ExplorestoreStarttime, _ := time.ParseInLocation("2006-01-02 15:04:05", newLogistics.ExplorestoreStarttime, time.Local)
  220. ExplorestoreEndtime, _ := time.ParseInLocation("2006-01-02 15:04:05", newLogistics.ExplorestoreEndtime, time.Local)
  221. Logistics.ExplorestoreStarttime = ExplorestoreStarttime
  222. Logistics.ExplorestoreEndtime = ExplorestoreEndtime
  223. } else {
  224. Logistics.CouponCodeInformation = newLogistics.CouponCodeInformation
  225. }
  226. logisticsID, err := db.CreateLogistics(ctx, Logistics, 0)
  227. if err != nil {
  228. logrus.WithContext(ctx).Errorf("[logistics service] call CreateLogistics error,err:%+v", err)
  229. return nil, err
  230. }
  231. // 修改task_info中发货时间
  232. err = db.UpdateLogisticsDate(ctx, Logistics.TaskID)
  233. if err != nil {
  234. logrus.WithContext(ctx).Errorf("[logistics service] call UpdateLogisticsDate error,err:%+v", err)
  235. return nil, err
  236. }
  237. // 修改task_info中发货状态
  238. err = db.UpdateLogisticsStatus(ctx, Logistics.TaskID, 2)
  239. if err != nil {
  240. logrus.WithContext(ctx).Errorf("[logistics service] call UpdateLogisticsStatus error,err:%+v", err)
  241. return nil, err
  242. }
  243. // 修改task_info中任务阶段
  244. err = db.UpdateTaskStageByTaskId(ctx, Logistics.TaskID, 2, 5) //修改为待传初稿
  245. if err != nil {
  246. logrus.WithContext(ctx).Errorf("[logistics service] call UpdateLogisticsDate error,err:%+v", err)
  247. return nil, err
  248. }
  249. // 记录任务日志-发货
  250. err = db.CreateTaskLog(ctx, Logistics.TaskID, "发货时间")
  251. if err != nil {
  252. logrus.WithContext(ctx).Errorf("[logistics service] call CreateTaskLog error,err:%+v", err)
  253. return nil, err
  254. }
  255. err = db.CreateMessageByTaskId(ctx, 8, 2, Logistics.TaskID)
  256. if err != nil {
  257. logrus.WithContext(ctx).Errorf("[logistics service] call CreateMessageByTaskId error,err:%+v", err)
  258. return nil, err
  259. }
  260. res := &http_model.SpecialLogisticsData{
  261. LogisticsID: *logisticsID,
  262. }
  263. return res, nil
  264. }
  265. func (*logistics) UpdateSpecialLogistics(ctx context.Context, newLogistics http_model.CreateSpecialLogisticsRequest) (*http_model.SpecialLogisticsData, error) {
  266. ThingsType := newLogistics.ThingsType
  267. Logistics := gorm_model.YoungeeTaskLogistics{
  268. LogisticsID: newLogistics.LogisticsID,
  269. TaskID: newLogistics.TaskID,
  270. ThingsType: int64(ThingsType),
  271. DeliveryTime: time.Now(),
  272. }
  273. //实物
  274. if ThingsType == 1 {
  275. Logistics.CompanyName = newLogistics.CompanyName
  276. Logistics.LogisticsNumber = newLogistics.LogisticsNumber
  277. } else if ThingsType == 3 {
  278. ExplorestoreStarttime, _ := time.ParseInLocation("2006-01-02 15:04:05", newLogistics.ExplorestoreStarttime, time.Local)
  279. ExplorestoreEndtime, _ := time.ParseInLocation("2006-01-02 15:04:05", newLogistics.ExplorestoreEndtime, time.Local)
  280. Logistics.ExplorestoreStarttime = ExplorestoreStarttime
  281. Logistics.ExplorestoreEndtime = ExplorestoreEndtime
  282. } else {
  283. Logistics.CouponCodeInformation = newLogistics.CouponCodeInformation
  284. }
  285. logisticsID, err := db.UpdateLogistics(ctx, Logistics)
  286. if err != nil {
  287. logrus.WithContext(ctx).Errorf("[logistics service] call UpdateLogistics error,err:%+v", err)
  288. return nil, err
  289. }
  290. res := &http_model.SpecialLogisticsData{
  291. LogisticsID: *logisticsID,
  292. }
  293. return res, nil
  294. }
  295. func (l *logistics) SignForSpecialLogistic(ctx *gin.Context, logisticRequest http_model.SignForSpecialLogisticRequest) error {
  296. projectId, err1 := db.GetProjectIdByTaskId(ctx, logisticRequest.TaskId)
  297. if err1 != nil {
  298. logrus.WithContext(ctx).Errorf("[project service] call GetProjectIdByTaskId error,err:%+v", err1)
  299. return err1
  300. }
  301. // 签收时更新任务阶段
  302. project, err3 := db.GetProjectDetail(ctx, *projectId)
  303. if err3 != nil {
  304. logrus.WithContext(ctx).Errorf("[project service] call GetPorjectDetail error,err:%+v", err3)
  305. return err3
  306. }
  307. if project.ContentType == 1 {
  308. err := db.UpdateTaskStageByProjectId(ctx, *projectId, 2, 9)
  309. if err != nil {
  310. logrus.WithContext(ctx).Errorf("[projectPay service] call UpdateTaskStatusPaying error,err:%+v", err)
  311. return err
  312. }
  313. } else {
  314. err := db.UpdateTaskStageByProjectId(ctx, *projectId, 2, 7)
  315. if err != nil {
  316. logrus.WithContext(ctx).Errorf("[projectPay service] call UpdateTaskStatusPaying error,err:%+v", err)
  317. return err
  318. }
  319. }
  320. taskId := logisticRequest.TaskId
  321. err := db.UpdateLogisticsStatus(ctx, taskId, 3)
  322. if err != nil {
  323. logrus.WithContext(ctx).Errorf("[project service] call UpdateLogisticsStatus error,err:%+v", err)
  324. return err
  325. }
  326. // 签收时间
  327. err = db.SignForReceipt(ctx, taskId)
  328. if err != nil {
  329. logrus.WithContext(ctx).Errorf("[project service] call SignForReceipt error,err:%+v", err)
  330. return err
  331. }
  332. // 记录任务日志
  333. err = db.CreateTaskLog(ctx, taskId, "签收时间")
  334. if err != nil {
  335. logrus.WithContext(ctx).Errorf("[logistics service] call CreateTaskLog error,err:%+v", err)
  336. return err
  337. }
  338. err = db.CreateMessageByTaskId(ctx, 9, 2, taskId)
  339. if err != nil {
  340. logrus.WithContext(ctx).Errorf("[logistics service] call CreateMessageByTaskId error,err:%+v", err)
  341. return err
  342. }
  343. return nil
  344. }