logistics.go 15 KB

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