service.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package carsrv
  2. import (
  3. "Cold_Logistic/internal/pkg/common/codex"
  4. "Cold_Logistic/internal/pkg/common/global"
  5. "Cold_Logistic/internal/server/domain/domainservice"
  6. "Cold_Logistic/internal/server/infra/dao"
  7. "Cold_Logistic/internal/server/infra/models"
  8. "context"
  9. "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/core"
  10. "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/errors"
  11. "time"
  12. )
  13. type CarService struct {
  14. store *dao.DataStore
  15. }
  16. func NewCarService(store *dao.DataStore) *CarService {
  17. return &CarService{store: store}
  18. }
  19. func (srv *CarService) CarPage(ctx context.Context, req CarPageReqVO) (res core.PageListResponse, err error) {
  20. dto := dao.CarPageDTO{
  21. Page: req.Page,
  22. CarNumber: req.Search.CarNumber,
  23. SnCode: req.Search.SnCode,
  24. DriverAccountUuid: req.Search.DriverAccountUuid,
  25. }
  26. list, total, err := srv.store.Car().Page(ctx, dto)
  27. if err != nil {
  28. return res, errors.WithStackOnce(err)
  29. }
  30. res.Total = total
  31. res.TotalPage = total / int64(req.Page.Size)
  32. res.Count = len(list)
  33. res.List = list
  34. return res, nil
  35. }
  36. func (srv *CarService) AddCar(ctx context.Context, req AddCarReqVO) (res CarIdRespVO, err error) {
  37. count, err := srv.store.Car().CountByCarNumberOrSnCode(ctx, req.CarNumber, req.SnCode)
  38. if err != nil {
  39. return res, errors.WithStackOnce(err)
  40. }
  41. if count > 0 {
  42. return res, errors.WithCode(codex.ErrParamValidate, "车辆或设备已被添加,不可重复")
  43. }
  44. car := models.Car{
  45. CarNumber: req.CarNumber,
  46. Pid: global.GetTokenInfoFromContext(ctx).UsePid,
  47. SnCode: req.SnCode,
  48. DriverUuid: req.Driver.Uuid,
  49. DriverAccountId: 0,
  50. CarType: req.CarType,
  51. Enable: req.Enable,
  52. }
  53. err = srv.store.InTx(ctx, func(ctx context.Context) error {
  54. if err = srv.store.Car().Create(ctx, &car); err != nil {
  55. return errors.WithStackOnce(err)
  56. }
  57. car.DriverAccountId, _, err = domainservice.ProcessDriver(ctx, car.Id, req.Driver)
  58. if err != nil {
  59. return errors.WithStackOnce(err)
  60. }
  61. if err = srv.store.Car().UpdateById(ctx, &car); err != nil {
  62. return errors.WithStackOnce(err)
  63. }
  64. return nil
  65. })
  66. if err != nil {
  67. return res, errors.WithStackOnce(err)
  68. }
  69. res.CarId = car.Id
  70. return res, nil
  71. }
  72. func (srv *CarService) UpdateCar(ctx context.Context, req UpdateCarReqVO) (res CarIdRespVO, err error) {
  73. car := models.Car{}
  74. if err = srv.store.Car().FirstById(ctx, &car, req.CarId); err != nil {
  75. return res, errors.WithStackOnce(err)
  76. }
  77. if req.CarNumber != car.CarNumber || req.SnCode != car.SnCode {
  78. count, err := srv.store.Car().CountByCarNumberOrSnCode(ctx, req.CarNumber, req.SnCode, car.Id)
  79. if err != nil {
  80. return res, errors.WithStackOnce(err)
  81. }
  82. if count > 0 {
  83. return res, errors.WithCode(codex.ErrParamValidate, "车辆或设备已被添加,不可重复")
  84. }
  85. }
  86. car.CarNumber = req.CarNumber
  87. car.SnCode = req.SnCode
  88. car.DriverUuid = req.Driver.Uuid
  89. car.CarType = req.CarType
  90. car.Enable = req.Enable
  91. err = srv.store.InTx(ctx, func(ctx context.Context) error {
  92. // 换司机了
  93. if req.Driver.Uuid != car.DriverUuid && car.DriverAccountId > 0 {
  94. acc := models.Account{}
  95. if err = srv.store.Account().FirstById(ctx, &acc, car.DriverAccountId); err != nil {
  96. return errors.WithStackOnce(err)
  97. }
  98. acc.CarId = 0
  99. if err = srv.store.Account().Save(ctx, &acc); err != nil {
  100. return errors.WithStackOnce(err)
  101. }
  102. car.DriverAccountId, _, err = domainservice.ProcessDriver(ctx, car.Id, req.Driver)
  103. if err != nil {
  104. return errors.WithStackOnce(err)
  105. }
  106. err = srv.store.CarLog().Create(ctx, &models.CarLog{
  107. DriverAccountId: car.DriverAccountId,
  108. DirverName: req.Driver.Name,
  109. HandoverTime: models.NewMyTime(time.Now()),
  110. })
  111. if err != nil {
  112. return errors.WithStackOnce(err)
  113. }
  114. }
  115. if err = srv.store.Car().UpdateById(ctx, &car); err != nil {
  116. return errors.WithStackOnce(err)
  117. }
  118. return nil
  119. })
  120. if err != nil {
  121. return res, errors.WithStackOnce(err)
  122. }
  123. res.CarId = car.Id
  124. return res, nil
  125. }
  126. func (srv *CarService) DriverLog(ctx context.Context, req DriverLogReqVO) (res core.ListResponse, err error) {
  127. data, err := srv.store.CarLog().FindAllByCarId(ctx, req.CarId)
  128. if err != nil {
  129. return res, errors.WithStackOnce(err)
  130. }
  131. list := make([]DriverLogRespVO, 0, len(data))
  132. for _, v := range data {
  133. list = append(list, DriverLogRespVO{
  134. AccountId: v.DriverAccountId,
  135. Name: v.DirverName,
  136. UseTime: v.CreatedTime,
  137. HandoverTime: v.HandoverTime,
  138. })
  139. }
  140. res.List = list
  141. return res, nil
  142. }