service.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. package driversrv
  2. import (
  3. "Cold_Logistic/internal/pkg/common/codex"
  4. "Cold_Logistic/internal/pkg/common/constant"
  5. "Cold_Logistic/internal/pkg/common/global"
  6. "Cold_Logistic/internal/server/domain/domainservice"
  7. "Cold_Logistic/internal/server/infra/dao"
  8. "Cold_Logistic/internal/server/infra/models"
  9. "Cold_Logistic/internal/server/infra/thirdparty/internalservice/clod"
  10. "context"
  11. "fmt"
  12. "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/core"
  13. "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/errors"
  14. "time"
  15. )
  16. type DriverService struct {
  17. store *dao.DataStore
  18. }
  19. func NewDriverService(store *dao.DataStore) *DriverService {
  20. return &DriverService{store: store}
  21. }
  22. // DriverCarInfo 司机车辆信息
  23. func (srv *DriverService) DriverCarInfo(ctx context.Context) (res DriverCarInfoRespVO, err error) {
  24. tokenInfo := global.GetTokenInfoFromContext(ctx)
  25. car, err := srv.store.Car().FindByUUId(ctx, tokenInfo.AccountUuid)
  26. if err != nil {
  27. return res, errors.WithStackOnce(err)
  28. }
  29. res.CarNumber = car.CarNumber
  30. res.SnCode = car.SnCode
  31. res.SnIds = make([]global.SnIdVO, 0, 4)
  32. snIds, err := clod.NewBzdClodService().DeviceSensorList(ctx, car.SnCode)
  33. if err != nil {
  34. return res, errors.WithStackOnce(err)
  35. }
  36. for i := range snIds {
  37. if i >= 4 {
  38. break
  39. }
  40. res.SnIds = append(res.SnIds, global.SnIdVO{
  41. Id: snIds[i].TId,
  42. Name: snIds[i].TName,
  43. })
  44. }
  45. return res, nil
  46. }
  47. // TaskPage 司机订单列表
  48. func (srv *DriverService) TaskPage(ctx context.Context, req TaskPageReqVO) (res core.PageListResponse, err error) {
  49. dto := dao.OrderTransportTaskPageDTO{
  50. Page: req.Page,
  51. OrderNo: req.Search.OrderNo,
  52. Status: req.Status,
  53. }
  54. list, total, err := srv.store.OrderTransportTask().Page(ctx, dto)
  55. if err != nil {
  56. return res, nil
  57. }
  58. res.Total = total
  59. res.TotalPage = total / int64(req.Page.Size)
  60. res.Count = len(list)
  61. res.List = list
  62. return res, nil
  63. }
  64. // OrderIntoCar 订单装车
  65. func (srv *DriverService) OrderIntoCar(ctx context.Context, req OrderIntoCarReqVO) (res OrderIntoCarRespVO, err error) {
  66. tokenInfo := global.GetTokenInfoFromContext(ctx)
  67. if tokenInfo.CarId <= 0 {
  68. return res, errors.WithCode(codex.ErrParamValidate, "还未给你分配运输车,请联系管理员")
  69. }
  70. order := models.ExpressOrder{}
  71. err = srv.store.ExpressOrder().FirstById(ctx, &order, req.OrderId)
  72. if err != nil {
  73. return res, errors.WithStackOnce(err)
  74. }
  75. if order.OrderStatus == constant.OrderStatusPending {
  76. return res, errors.WithCode(codex.ErrParamValidate, "订单还未分派揽件员,请耐心等待")
  77. }
  78. key := fmt.Sprintf("%s_%s", order.OrderNo, req.OrderSubNo)
  79. client := global.CommonConnectRepoInst.Redis
  80. exist, err := client.Exists(ctx, key).Result()
  81. if err != nil {
  82. return res, errors.WithStackOnce(err)
  83. }
  84. if exist != 1 {
  85. return res, errors.WithCode(codex.ErrParamValidate, "揽件码错误,请确认!")
  86. }
  87. task := models.OrderTransportTask{}
  88. if order.LatestTaskId > 0 {
  89. if err = srv.store.OrderTransportTask().FirstById(ctx, &task, order.LatestTaskId); err != nil {
  90. if errors.IsErrRecordNotFound(err) {
  91. return res, errors.WithCode(codex.ErrParamValidate, "订单还未分派揽件员")
  92. }
  93. return res, errors.WithStackOnce(err)
  94. }
  95. if task.ExecutorAccountId != global.GetTokenInfoFromContext(ctx).AccountId {
  96. return res, errors.WithCode(codex.ErrParamValidate, "不属于你的订单不能操作")
  97. }
  98. }
  99. log := models.OrderLogisticLog{
  100. LogType: constant.LogisticLogTraffic,
  101. OrderId: order.Id,
  102. OrderNo: order.OrderNo,
  103. ContactPersonUuid: tokenInfo.AccountUuid,
  104. ContactPerson: tokenInfo.Name,
  105. ContactPhone: tokenInfo.Phone,
  106. Extend: fmt.Sprintf(constant.TemplateIntoCar, tokenInfo.Name),
  107. }
  108. if task.Id > 0 {
  109. log.SnCode = task.SnCode
  110. } else {
  111. log.SnCode, err = srv.store.Car().FindSnCodeById(ctx, tokenInfo.CarId)
  112. if err != nil {
  113. return res, errors.WithStackOnce(err)
  114. }
  115. }
  116. err = srv.store.InTx(ctx, func(ctx context.Context) error {
  117. if task.Id > 0 {
  118. task.Status = constant.TaskStatusExecuting
  119. task.StartTime = models.NewMyTime(time.Now())
  120. if err = srv.store.OrderTransportTask().UpdateById(ctx, &task); err != nil {
  121. return errors.WithStackOnce(err)
  122. }
  123. }
  124. if err = srv.store.OrderLogisticLog().Create(ctx, &log); err != nil {
  125. return errors.WithStackOnce(err)
  126. }
  127. if order.OrderStatus < constant.OrderStatusInTransit {
  128. err = srv.store.ExpressOrder().UpdateInTransitById(ctx, task.OrderId, log.Id)
  129. if err != nil {
  130. return errors.WithStackOnce(err)
  131. }
  132. }
  133. return nil
  134. })
  135. if err != nil {
  136. return res, errors.WithStackOnce(err)
  137. }
  138. _ = domainservice.DelOrderSubNo(ctx, key)
  139. res.IntoTime = task.StartTime
  140. res.OrderId = task.OrderId
  141. return res, nil
  142. }
  143. // ScanIntoCar 扫码装车
  144. func (srv *DriverService) ScanIntoCar(ctx context.Context, req ScanIntoReqVO) (res OrderIntoCarRespVO, err error) {
  145. tokenInfo := global.GetTokenInfoFromContext(ctx)
  146. if tokenInfo.CarId <= 0 {
  147. return res, errors.WithCode(codex.ErrParamValidate, "还未给你分配运输车,请联系管理员")
  148. }
  149. order, err := srv.store.ExpressOrder().FindByOrderNo(ctx, req.OrderNo)
  150. if err != nil {
  151. return res, errors.WithStackOnce(err)
  152. }
  153. if order.OrderStatus == constant.OrderStatusPending {
  154. return res, errors.WithCode(codex.ErrParamValidate, "订单还未分派揽件员,请耐心等待")
  155. }
  156. task := models.OrderTransportTask{}
  157. if order.LatestTaskId > 0 {
  158. if err = srv.store.OrderTransportTask().FirstById(ctx, &task, order.LatestTaskId); err != nil {
  159. if errors.IsErrRecordNotFound(err) {
  160. return res, errors.WithCode(codex.ErrParamValidate, "订单还未分派揽件员")
  161. }
  162. return res, errors.WithStackOnce(err)
  163. }
  164. if task.ExecutorAccountId != global.GetTokenInfoFromContext(ctx).AccountId {
  165. return res, errors.WithCode(codex.ErrParamValidate, "不属于你的订单不能操作")
  166. }
  167. } else {
  168. task = models.OrderTransportTask{
  169. ExecutorAccountId: tokenInfo.AccountId,
  170. CarId: tokenInfo.CarId,
  171. OrderId: order.Id,
  172. OrderNo: order.OrderNo,
  173. Status: constant.TaskStatusExecuting,
  174. StartTime: models.NewMyTime(time.Now()),
  175. }
  176. task.SnCode, err = srv.store.Car().FindSnCodeById(ctx, tokenInfo.CarId)
  177. if err != nil {
  178. return res, errors.WithStackOnce(err)
  179. }
  180. }
  181. log := models.OrderLogisticLog{
  182. LogType: constant.LogisticLogTraffic,
  183. OrderId: order.Id,
  184. OrderNo: order.OrderNo,
  185. SnCode: task.SnCode,
  186. ContactPersonUuid: tokenInfo.AccountUuid,
  187. ContactPerson: tokenInfo.Name,
  188. ContactPhone: tokenInfo.Phone,
  189. Extend: fmt.Sprintf(constant.TemplateIntoCar, tokenInfo.Name),
  190. }
  191. err = srv.store.InTx(ctx, func(ctx context.Context) error {
  192. if task.Id > 0 {
  193. task.Status = constant.TaskStatusExecuting
  194. task.StartTime = models.NewMyTime(time.Now())
  195. if err = srv.store.OrderTransportTask().UpdateById(ctx, &task); err != nil {
  196. return errors.WithStackOnce(err)
  197. }
  198. } else {
  199. if err = srv.store.OrderTransportTask().Create(ctx, &task); err != nil {
  200. return errors.WithStackOnce(err)
  201. }
  202. }
  203. if err = srv.store.OrderLogisticLog().Create(ctx, &log); err != nil {
  204. return errors.WithStackOnce(err)
  205. }
  206. if order.OrderStatus < constant.OrderStatusInTransit {
  207. err = srv.store.ExpressOrder().UpdateInTransitById(ctx, task.OrderId, log.Id)
  208. if err != nil {
  209. return errors.WithStackOnce(err)
  210. }
  211. }
  212. return nil
  213. })
  214. if err != nil {
  215. return res, errors.WithStackOnce(err)
  216. }
  217. res.IntoTime = task.StartTime
  218. res.OrderId = task.OrderId
  219. return res, nil
  220. }
  221. // SignReceipt 订单签收
  222. func (srv *DriverService) SignReceipt(ctx context.Context, req ScanIntoReqVO) (res SignReceiptRespVO, err error) {
  223. tokenInfo := global.GetTokenInfoFromContext(ctx)
  224. order, err := srv.store.ExpressOrder().FindByOrderNo(ctx, req.OrderNo)
  225. if err != nil {
  226. return res, errors.WithStackOnce(err)
  227. }
  228. snCode, err := srv.store.Car().FindSnCodeById(ctx, tokenInfo.CarId)
  229. if err != nil {
  230. return res, errors.WithStackOnce(err)
  231. }
  232. log := models.OrderLogisticLog{
  233. LogType: constant.LogisticLogTraffic,
  234. OrderId: order.Id,
  235. OrderNo: order.OrderNo,
  236. SnCode: snCode,
  237. ContactPersonUuid: tokenInfo.AccountUuid,
  238. ContactPerson: tokenInfo.Name,
  239. ContactPhone: tokenInfo.Phone,
  240. Extend: constant.TemplateSignReceipt,
  241. }
  242. order.OrderStatus = constant.OrderStatusReceived
  243. order.LatestLogId = log.Id
  244. order.SignReceiptTime = models.NewMyTime(time.Now())
  245. order.SignReceiptBy = tokenInfo.AccountId
  246. err = srv.store.InTx(ctx, func(ctx context.Context) error {
  247. if err = srv.store.OrderLogisticLog().Create(ctx, &log); err != nil {
  248. return errors.WithStackOnce(err)
  249. }
  250. if order.LatestTaskId > 0 {
  251. if err = srv.store.OrderTransportTask().FinishedTask(ctx, order.LatestTaskId); err != nil {
  252. return errors.WithStackOnce(err)
  253. }
  254. }
  255. if err = srv.store.ExpressOrder().UpdateById(ctx, &order); err != nil {
  256. return errors.WithStackOnce(err)
  257. }
  258. return nil
  259. })
  260. if err != nil {
  261. return res, errors.WithStackOnce(err)
  262. }
  263. res.OrderId = order.Id
  264. res.SingTime = order.SignReceiptTime
  265. return res, nil
  266. }