logistics.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "youngee_m_api/db"
  7. "youngee_m_api/model/gorm_model"
  8. "youngee_m_api/model/http_model"
  9. "github.com/caixw/lib.go/conv"
  10. "github.com/gin-gonic/gin"
  11. "github.com/sirupsen/logrus"
  12. )
  13. var Logistics *logistics
  14. type logistics struct{}
  15. // Create 全流程物流信息表插入记录
  16. func (*logistics) Create(ctx context.Context, newLogistics http_model.CreateLogisticsRequest) (*http_model.CreateLogisticsData, error) {
  17. ThingsType := newLogistics.ThingsType
  18. StrategyID := newLogistics.StrategyID
  19. Logistics := gorm_model.YoungeeTaskLogistics{
  20. LogisticsID: newLogistics.LogisticsID,
  21. TaskID: newLogistics.TaskID,
  22. ThingsType: int64(ThingsType),
  23. ExplorestoreStarttime: time.Now(),
  24. ExplorestoreEndtime: time.Now(),
  25. DeliveryTime: time.Now(),
  26. }
  27. //fmt.Println("ThingsType:", ThingsType)
  28. //实物
  29. if ThingsType == 1 {
  30. Logistics.CompanyName = newLogistics.CompanyName
  31. Logistics.LogisticsNumber = newLogistics.LogisticsNumber
  32. Logistics.DeliveryTime = time.Now()
  33. } else if ThingsType == 3 {
  34. fmt.Println("开始时间:", newLogistics.ExplorestoreStarttime)
  35. fmt.Println("结束时间:", newLogistics.ExplorestoreEndtime)
  36. ExplorestoreStarttime, _ := time.ParseInLocation("2006-01-02 15:04:05", newLogistics.ExplorestoreStarttime, time.Local)
  37. ExplorestoreEndtime, _ := time.ParseInLocation("2006-01-02 15:04:05", newLogistics.ExplorestoreEndtime, time.Local)
  38. Logistics.ExplorestoreStarttime = ExplorestoreStarttime
  39. Logistics.ExplorestoreEndtime = ExplorestoreEndtime
  40. } else {
  41. Logistics.CouponCodeInformation = newLogistics.CouponCodeInformation
  42. }
  43. logisticsID, err := db.CreateLogistics(ctx, Logistics, StrategyID)
  44. if err != nil {
  45. logrus.WithContext(ctx).Errorf("[logistics service] call CreateLogistics error,err:%+v", err)
  46. return nil, err
  47. }
  48. projectId, err1 := db.GetProjectIdByTaskId(ctx, newLogistics.TaskID)
  49. if err1 != nil {
  50. logrus.WithContext(ctx).Errorf("[project service] call GetProjectIdByTaskId error,err:%+v", err1)
  51. return nil, err1
  52. }
  53. // 查询StrategyID 通过 StrategyID 和 projectId
  54. RecruitStrategyId, err2 := db.GetRecruitStrategyIdByTS(ctx, *projectId, StrategyID)
  55. if err2 != nil {
  56. logrus.WithContext(ctx).Errorf("[project service] call GetStrategyIDByTS error,err:%+v", err1)
  57. return nil, err2
  58. }
  59. fmt.Println("RecruitStrategyId: ", *RecruitStrategyId)
  60. // 修改招募策略中已签收数量
  61. err = db.UpdateLogisticsNumber(ctx, *RecruitStrategyId, 1, -1, 0)
  62. if err != nil {
  63. logrus.WithContext(ctx).Errorf("[project service] call UpdateLogisticsNumber error,err:%+v", err)
  64. return nil, err
  65. }
  66. // 修改task_info中发货状态
  67. err = db.UpdateLogisticsStatus(ctx, Logistics.TaskID, 2)
  68. if err != nil {
  69. logrus.WithContext(ctx).Errorf("[logistics service] call UpdateLogisticsStatus error,err:%+v", err)
  70. return nil, err
  71. }
  72. // 修改task_info中发货时间
  73. err = db.UpdateLogisticsDate(ctx, Logistics.TaskID)
  74. if err != nil {
  75. logrus.WithContext(ctx).Errorf("[logistics service] call UpdateLogisticsDate error,err:%+v", err)
  76. return nil, err
  77. }
  78. // 修改task_info中任务阶段
  79. err = db.UpdateTaskStageByTaskId(ctx, Logistics.TaskID, 2, 5) //修改为待传初稿
  80. if err != nil {
  81. logrus.WithContext(ctx).Errorf("[logistics service] call UpdateLogisticsDate error,err:%+v", err)
  82. return nil, err
  83. }
  84. // 对应招募策略待发货--,已发货++
  85. // 记录任务日志-发货
  86. err = db.CreateTaskLog(ctx, Logistics.TaskID, "发货时间")
  87. if err != nil {
  88. logrus.WithContext(ctx).Errorf("[logistics service] call CreateTaskLog error,err:%+v", err)
  89. return nil, err
  90. }
  91. err = db.CreateMessageByTaskId(ctx, 8, 2, Logistics.TaskID)
  92. if err != nil {
  93. logrus.WithContext(ctx).Errorf("[logistics service] call CreateMessageByTaskId error,err:%+v", err)
  94. return nil, err
  95. }
  96. res := &http_model.CreateLogisticsData{
  97. LogisticsID: *logisticsID,
  98. }
  99. return res, nil
  100. }
  101. func (l *logistics) CreateSpecialLogistics(ctx context.Context, newLogistics http_model.CreateSpecialLogisticsRequest) (*http_model.SpecialLogisticsData, error) {
  102. ThingsType := newLogistics.ThingsType
  103. Logistics := gorm_model.YoungeeTaskLogistics{
  104. LogisticsID: newLogistics.LogisticsID,
  105. TaskID: newLogistics.TaskID,
  106. ThingsType: int64(ThingsType),
  107. ExplorestoreStarttime: time.Now(),
  108. ExplorestoreEndtime: time.Now(),
  109. DeliveryTime: time.Now(),
  110. }
  111. //实物
  112. if ThingsType == 1 {
  113. Logistics.CompanyName = newLogistics.CompanyName
  114. Logistics.LogisticsNumber = newLogistics.LogisticsNumber
  115. Logistics.DeliveryTime = time.Now()
  116. } else if ThingsType == 3 {
  117. ExplorestoreStarttime, _ := time.ParseInLocation("2006-01-02 15:04:05", newLogistics.ExplorestoreStarttime, time.Local)
  118. ExplorestoreEndtime, _ := time.ParseInLocation("2006-01-02 15:04:05", newLogistics.ExplorestoreEndtime, time.Local)
  119. Logistics.ExplorestoreStarttime = ExplorestoreStarttime
  120. Logistics.ExplorestoreEndtime = ExplorestoreEndtime
  121. } else {
  122. Logistics.CouponCodeInformation = newLogistics.CouponCodeInformation
  123. }
  124. logisticsID, err := db.CreateLogistics(ctx, Logistics, 0)
  125. if err != nil {
  126. logrus.WithContext(ctx).Errorf("[logistics service] call CreateLogistics error,err:%+v", err)
  127. return nil, err
  128. }
  129. // 修改task_info中发货时间
  130. err = db.UpdateLogisticsDate(ctx, Logistics.TaskID)
  131. if err != nil {
  132. logrus.WithContext(ctx).Errorf("[logistics service] call UpdateLogisticsDate error,err:%+v", err)
  133. return nil, err
  134. }
  135. // 修改task_info中发货状态
  136. err = db.UpdateLogisticsStatus(ctx, Logistics.TaskID, 2)
  137. if err != nil {
  138. logrus.WithContext(ctx).Errorf("[logistics service] call UpdateLogisticsStatus error,err:%+v", err)
  139. return nil, err
  140. }
  141. // 修改task_info中任务阶段
  142. err = db.UpdateTaskStageByTaskId(ctx, Logistics.TaskID, 2, 5) //修改为待传初稿
  143. if err != nil {
  144. logrus.WithContext(ctx).Errorf("[logistics service] call UpdateLogisticsDate error,err:%+v", err)
  145. return nil, err
  146. }
  147. // 记录任务日志-发货
  148. err = db.CreateTaskLog(ctx, Logistics.TaskID, "发货时间")
  149. if err != nil {
  150. logrus.WithContext(ctx).Errorf("[logistics service] call CreateTaskLog error,err:%+v", err)
  151. return nil, err
  152. }
  153. err = db.CreateMessageByTaskId(ctx, 8, 2, Logistics.TaskID)
  154. if err != nil {
  155. logrus.WithContext(ctx).Errorf("[logistics service] call CreateMessageByTaskId error,err:%+v", err)
  156. return nil, err
  157. }
  158. res := &http_model.SpecialLogisticsData{
  159. LogisticsID: *logisticsID,
  160. }
  161. return res, nil
  162. }
  163. // 修改物流信息表
  164. func (*logistics) Update(ctx context.Context, newLogistics http_model.CreateLogisticsRequest) (*http_model.CreateLogisticsData, error) {
  165. ThingsType := newLogistics.ThingsType
  166. Logistics := gorm_model.YoungeeTaskLogistics{
  167. LogisticsID: newLogistics.LogisticsID,
  168. TaskID: newLogistics.TaskID,
  169. ThingsType: int64(ThingsType),
  170. DeliveryTime: time.Now(),
  171. }
  172. //实物
  173. if ThingsType == 1 {
  174. Logistics.CompanyName = newLogistics.CompanyName
  175. Logistics.LogisticsNumber = newLogistics.LogisticsNumber
  176. } else if ThingsType == 3 {
  177. fmt.Println("开始时间:", newLogistics.ExplorestoreStarttime)
  178. fmt.Println("结束时间:", newLogistics.ExplorestoreEndtime)
  179. ExplorestoreStarttime, _ := time.ParseInLocation("2006-01-02 15:04:05", newLogistics.ExplorestoreStarttime, time.Local)
  180. ExplorestoreEndtime, _ := time.ParseInLocation("2006-01-02 15:04:05", newLogistics.ExplorestoreEndtime, time.Local)
  181. Logistics.ExplorestoreStarttime = ExplorestoreStarttime
  182. Logistics.ExplorestoreEndtime = ExplorestoreEndtime
  183. } else {
  184. Logistics.CouponCodeInformation = newLogistics.CouponCodeInformation
  185. }
  186. logisticsID, err := db.UpdateLogistics(ctx, Logistics)
  187. if err != nil {
  188. logrus.WithContext(ctx).Errorf("[logistics service] call UpdateLogistics error,err:%+v", err)
  189. return nil, err
  190. }
  191. res := &http_model.CreateLogisticsData{
  192. LogisticsID: *logisticsID,
  193. }
  194. return res, nil
  195. }
  196. func (*logistics) UpdateSpecialLogistics(ctx context.Context, newLogistics http_model.CreateSpecialLogisticsRequest) (*http_model.SpecialLogisticsData, error) {
  197. ThingsType := newLogistics.ThingsType
  198. Logistics := gorm_model.YoungeeTaskLogistics{
  199. LogisticsID: newLogistics.LogisticsID,
  200. TaskID: newLogistics.TaskID,
  201. ThingsType: int64(ThingsType),
  202. DeliveryTime: time.Now(),
  203. }
  204. //实物
  205. if ThingsType == 1 {
  206. Logistics.CompanyName = newLogistics.CompanyName
  207. Logistics.LogisticsNumber = newLogistics.LogisticsNumber
  208. } else if ThingsType == 3 {
  209. ExplorestoreStarttime, _ := time.ParseInLocation("2006-01-02 15:04:05", newLogistics.ExplorestoreStarttime, time.Local)
  210. ExplorestoreEndtime, _ := time.ParseInLocation("2006-01-02 15:04:05", newLogistics.ExplorestoreEndtime, time.Local)
  211. Logistics.ExplorestoreStarttime = ExplorestoreStarttime
  212. Logistics.ExplorestoreEndtime = ExplorestoreEndtime
  213. } else {
  214. Logistics.CouponCodeInformation = newLogistics.CouponCodeInformation
  215. }
  216. logisticsID, err := db.UpdateLogistics(ctx, Logistics)
  217. if err != nil {
  218. logrus.WithContext(ctx).Errorf("[logistics service] call UpdateLogistics error,err:%+v", err)
  219. return nil, err
  220. }
  221. res := &http_model.SpecialLogisticsData{
  222. LogisticsID: *logisticsID,
  223. }
  224. return res, nil
  225. }
  226. // 签收
  227. func (*logistics) SignForReceipt(ctx *gin.Context, data http_model.SignForReceiptRequest) interface{} {
  228. projectId, err1 := db.GetProjectIdByTaskId(ctx, data.TaskStrategyIds[0].TaskId)
  229. if err1 != nil {
  230. logrus.WithContext(ctx).Errorf("[project service] call GetProjectIdByTaskId error,err:%+v", err1)
  231. return err1
  232. }
  233. // 签收时更新任务阶段
  234. project, err3 := db.GetProjectDetail(ctx, *projectId)
  235. if err3 != nil {
  236. logrus.WithContext(ctx).Errorf("[project service] call GetPorjectDetail error,err:%+v", err3)
  237. return err3
  238. }
  239. if project.ContentType == 1 {
  240. err := db.UpdateTaskStageByProjectId(ctx, *projectId, 2, 9)
  241. if err != nil {
  242. logrus.WithContext(ctx).Errorf("[projectPay service] call UpdateTaskStatusPaying error,err:%+v", err)
  243. return err
  244. }
  245. } else {
  246. err := db.UpdateTaskStageByProjectId(ctx, *projectId, 2, 7)
  247. if err != nil {
  248. logrus.WithContext(ctx).Errorf("[projectPay service] call UpdateTaskStatusPaying error,err:%+v", err)
  249. return err
  250. }
  251. }
  252. for _, value := range data.TaskStrategyIds {
  253. taskId := value.TaskId
  254. strategyId := conv.MustInt64(value.StrategyId, 0)
  255. err := db.UpdateLogisticsStatus(ctx, taskId, 3)
  256. if err != nil {
  257. logrus.WithContext(ctx).Errorf("[project service] call UpdateLogisticsStatus error,err:%+v", err)
  258. return err
  259. }
  260. // 签收时间
  261. err = db.SignForReceipt(ctx, taskId)
  262. if err != nil {
  263. logrus.WithContext(ctx).Errorf("[project service] call SignForReceipt error,err:%+v", err)
  264. return err
  265. }
  266. // 查询StrategyID 通过 StrategyID 和 projectId
  267. StrategyID, err2 := db.GetRecruitStrategyIdByTS(ctx, *projectId, strategyId)
  268. if err2 != nil {
  269. logrus.WithContext(ctx).Errorf("[project service] call GetStrategyIDByTS error,err:%+v", err1)
  270. return err2
  271. }
  272. // 修改招募策略中已签收数量
  273. err = db.UpdateLogisticsNumber(ctx, *StrategyID, 0, 0, 1)
  274. if err != nil {
  275. logrus.WithContext(ctx).Errorf("[project service] call UpdateLogisticsNumber error,err:%+v", err)
  276. return err
  277. }
  278. // 记录任务日志
  279. err = db.CreateTaskLog(ctx, taskId, "签收时间")
  280. if err != nil {
  281. logrus.WithContext(ctx).Errorf("[logistics service] call CreateTaskLog error,err:%+v", err)
  282. return err
  283. }
  284. err = db.CreateMessageByTaskId(ctx, 9, 2, taskId)
  285. if err != nil {
  286. logrus.WithContext(ctx).Errorf("[logistics service] call CreateMessageByTaskId error,err:%+v", err)
  287. return err
  288. }
  289. }
  290. return nil
  291. }
  292. func (l *logistics) SignForSpecialLogistic(ctx *gin.Context, logisticRequest http_model.SignForSpecialLogisticRequest) error {
  293. projectId, err1 := db.GetProjectIdByTaskId(ctx, logisticRequest.TaskId)
  294. if err1 != nil {
  295. logrus.WithContext(ctx).Errorf("[project service] call GetProjectIdByTaskId error,err:%+v", err1)
  296. return err1
  297. }
  298. // 签收时更新任务阶段
  299. project, err3 := db.GetProjectDetail(ctx, *projectId)
  300. if err3 != nil {
  301. logrus.WithContext(ctx).Errorf("[project service] call GetPorjectDetail error,err:%+v", err3)
  302. return err3
  303. }
  304. if project.ContentType == 1 {
  305. err := db.UpdateTaskStageByProjectId(ctx, *projectId, 2, 9)
  306. if err != nil {
  307. logrus.WithContext(ctx).Errorf("[projectPay service] call UpdateTaskStatusPaying error,err:%+v", err)
  308. return err
  309. }
  310. } else {
  311. err := db.UpdateTaskStageByProjectId(ctx, *projectId, 2, 7)
  312. if err != nil {
  313. logrus.WithContext(ctx).Errorf("[projectPay service] call UpdateTaskStatusPaying error,err:%+v", err)
  314. return err
  315. }
  316. }
  317. taskId := logisticRequest.TaskId
  318. err := db.UpdateLogisticsStatus(ctx, taskId, 3)
  319. if err != nil {
  320. logrus.WithContext(ctx).Errorf("[project service] call UpdateLogisticsStatus error,err:%+v", err)
  321. return err
  322. }
  323. // 签收时间
  324. err = db.SignForReceipt(ctx, taskId)
  325. if err != nil {
  326. logrus.WithContext(ctx).Errorf("[project service] call SignForReceipt error,err:%+v", err)
  327. return err
  328. }
  329. // 记录任务日志
  330. err = db.CreateTaskLog(ctx, taskId, "签收时间")
  331. if err != nil {
  332. logrus.WithContext(ctx).Errorf("[logistics service] call CreateTaskLog error,err:%+v", err)
  333. return err
  334. }
  335. err = db.CreateMessageByTaskId(ctx, 9, 2, taskId)
  336. if err != nil {
  337. logrus.WithContext(ctx).Errorf("[logistics service] call CreateMessageByTaskId error,err:%+v", err)
  338. return err
  339. }
  340. return nil
  341. }