logistics.go 14 KB

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