logistics.go 14 KB

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