123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- package carsrv
- import (
- "Cold_Logistic/internal/pkg/common/codex"
- "Cold_Logistic/internal/pkg/common/global"
- "Cold_Logistic/internal/server/domain/domainservice"
- "Cold_Logistic/internal/server/infra/dao"
- "Cold_Logistic/internal/server/infra/models"
- "context"
- "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/core"
- "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/errors"
- "time"
- )
- type CarService struct {
- store *dao.DataStore
- }
- func NewCarService(store *dao.DataStore) *CarService {
- return &CarService{store: store}
- }
- func (srv *CarService) CarPage(ctx context.Context, req CarPageReqVO) (res core.PageListResponse, err error) {
- dto := dao.CarPageDTO{
- Page: req.Page,
- CarNumber: req.Search.CarNumber,
- SnCode: req.Search.SnCode,
- DriverAccountUuid: req.Search.DriverAccountUuid,
- }
- list, total, err := srv.store.Car().Page(ctx, dto)
- if err != nil {
- return res, errors.WithStackOnce(err)
- }
- res.Total = total
- res.TotalPage = total / int64(req.Page.Size)
- res.Count = len(list)
- res.List = list
- return res, nil
- }
- func (srv *CarService) AddCar(ctx context.Context, req AddCarReqVO) (res CarIdRespVO, err error) {
- count, err := srv.store.Car().CountByCarNumberOrSnCode(ctx, req.CarNumber, req.SnCode)
- if err != nil {
- return res, errors.WithStackOnce(err)
- }
- if count > 0 {
- return res, errors.WithCode(codex.ErrParamValidate, "车辆或设备已被添加,不可重复")
- }
- car := models.Car{
- CarNumber: req.CarNumber,
- Pid: global.GetTokenInfoFromContext(ctx).UsePid,
- SnCode: req.SnCode,
- DriverUuid: req.Driver.Uuid,
- DriverAccountId: 0,
- CarType: req.CarType,
- Enable: req.Enable,
- }
- err = srv.store.InTx(ctx, func(ctx context.Context) error {
- if err = srv.store.Car().Create(ctx, &car); err != nil {
- return errors.WithStackOnce(err)
- }
- car.DriverAccountId, _, err = domainservice.ProcessDriver(ctx, car.Id, req.Driver)
- if err != nil {
- return errors.WithStackOnce(err)
- }
- if err = srv.store.Car().UpdateById(ctx, &car); err != nil {
- return errors.WithStackOnce(err)
- }
- return nil
- })
- if err != nil {
- return res, errors.WithStackOnce(err)
- }
- res.CarId = car.Id
- return res, nil
- }
- func (srv *CarService) UpdateCar(ctx context.Context, req UpdateCarReqVO) (res CarIdRespVO, err error) {
- car := models.Car{}
- if err = srv.store.Car().FirstById(ctx, &car, req.CarId); err != nil {
- return res, errors.WithStackOnce(err)
- }
- if req.CarNumber != car.CarNumber || req.SnCode != car.SnCode {
- count, err := srv.store.Car().CountByCarNumberOrSnCode(ctx, req.CarNumber, req.SnCode, car.Id)
- if err != nil {
- return res, errors.WithStackOnce(err)
- }
- if count > 0 {
- return res, errors.WithCode(codex.ErrParamValidate, "车辆或设备已被添加,不可重复")
- }
- }
- car.CarNumber = req.CarNumber
- car.SnCode = req.SnCode
- car.DriverUuid = req.Driver.Uuid
- car.CarType = req.CarType
- car.Enable = req.Enable
- err = srv.store.InTx(ctx, func(ctx context.Context) error {
- // 换司机了
- if req.Driver.Uuid != car.DriverUuid && car.DriverAccountId > 0 {
- acc := models.Account{}
- if err = srv.store.Account().FirstById(ctx, &acc, car.DriverAccountId); err != nil {
- return errors.WithStackOnce(err)
- }
- acc.CarId = 0
- if err = srv.store.Account().Save(ctx, &acc); err != nil {
- return errors.WithStackOnce(err)
- }
- car.DriverAccountId, _, err = domainservice.ProcessDriver(ctx, car.Id, req.Driver)
- if err != nil {
- return errors.WithStackOnce(err)
- }
- err = srv.store.CarLog().Create(ctx, &models.CarLog{
- DriverAccountId: car.DriverAccountId,
- DirverName: req.Driver.Name,
- HandoverTime: models.NewMyTime(time.Now()),
- })
- if err != nil {
- return errors.WithStackOnce(err)
- }
- }
- if err = srv.store.Car().UpdateById(ctx, &car); err != nil {
- return errors.WithStackOnce(err)
- }
- return nil
- })
- if err != nil {
- return res, errors.WithStackOnce(err)
- }
- res.CarId = car.Id
- return res, nil
- }
- func (srv *CarService) DriverLog(ctx context.Context, req DriverLogReqVO) (res core.ListResponse, err error) {
- data, err := srv.store.CarLog().FindAllByCarId(ctx, req.CarId)
- if err != nil {
- return res, errors.WithStackOnce(err)
- }
- list := make([]DriverLogRespVO, 0, len(data))
- for _, v := range data {
- list = append(list, DriverLogRespVO{
- AccountId: v.DriverAccountId,
- Name: v.DirverName,
- UseTime: v.CreatedTime,
- HandoverTime: v.HandoverTime,
- })
- }
- res.List = list
- return res, nil
- }
|