package driversrv import ( "Cold_Logistic/internal/pkg/common/codex" "Cold_Logistic/internal/pkg/common/constant" "Cold_Logistic/internal/pkg/common/global" "Cold_Logistic/internal/server/domain/domainservice" "Cold_Logistic/internal/server/infra/dao" "Cold_Logistic/internal/server/infra/models" "Cold_Logistic/internal/server/infra/thirdparty/internalservice/clod" "context" "fmt" "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/core" "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/errors" "time" ) type DriverService struct { store *dao.DataStore } func NewDriverService(store *dao.DataStore) *DriverService { return &DriverService{store: store} } // DriverCarInfo 司机车辆信息 func (srv *DriverService) DriverCarInfo(ctx context.Context) (res DriverCarInfoRespVO, err error) { tokenInfo := global.GetTokenInfoFromContext(ctx) car, err := srv.store.Car().FindByUUId(ctx, tokenInfo.AccountUuid) if err != nil { return res, errors.WithStackOnce(err) } res.CarNumber = car.CarNumber res.SnCode = car.SnCode res.SnIds = make([]global.SnIdVO, 0, 4) snIds, err := clod.NewBzdClodService().DeviceSensorList(ctx, car.SnCode) if err != nil { return res, errors.WithStackOnce(err) } for i := range snIds { if i >= 4 { break } res.SnIds = append(res.SnIds, global.SnIdVO{ Id: snIds[i].TId, Name: snIds[i].TName, }) } return res, nil } // TaskPage 司机订单列表 func (srv *DriverService) TaskPage(ctx context.Context, req TaskPageReqVO) (res core.PageListResponse, err error) { dto := dao.OrderTransportTaskPageDTO{ Page: req.Page, OrderNo: req.Search.OrderNo, Status: req.Status, } list, total, err := srv.store.OrderTransportTask().Page(ctx, dto) if err != nil { return res, nil } res.Total = total res.TotalPage = total / int64(req.Page.Size) res.Count = len(list) res.List = list return res, nil } // OrderIntoCar 订单装车 func (srv *DriverService) OrderIntoCar(ctx context.Context, req OrderIntoCarReqVO) (res OrderIntoCarRespVO, err error) { tokenInfo := global.GetTokenInfoFromContext(ctx) if tokenInfo.CarId <= 0 { return res, errors.WithCode(codex.ErrParamValidate, "还未给你分配运输车,请联系管理员") } order := models.ExpressOrder{} err = srv.store.ExpressOrder().FirstById(ctx, &order, req.OrderId) if err != nil { return res, errors.WithStackOnce(err) } if order.OrderStatus == constant.OrderStatusPending { return res, errors.WithCode(codex.ErrParamValidate, "订单还未分派揽件员,请耐心等待") } key := fmt.Sprintf("%s_%s", order.OrderNo, req.OrderSubNo) client := global.CommonConnectRepoInst.Redis exist, err := client.Exists(ctx, key).Result() if err != nil { return res, errors.WithStackOnce(err) } if exist != 1 { return res, errors.WithCode(codex.ErrParamValidate, "揽件码错误,请确认!") } task := models.OrderTransportTask{} if order.LatestTaskId > 0 { if err = srv.store.OrderTransportTask().FirstById(ctx, &task, order.LatestTaskId); err != nil { if errors.IsErrRecordNotFound(err) { return res, errors.WithCode(codex.ErrParamValidate, "订单还未分派揽件员") } return res, errors.WithStackOnce(err) } if task.ExecutorAccountId != global.GetTokenInfoFromContext(ctx).AccountId { return res, errors.WithCode(codex.ErrParamValidate, "不属于你的订单不能操作") } } log := models.OrderLogisticLog{ LogType: constant.LogisticLogTraffic, OrderId: order.Id, OrderNo: order.OrderNo, ContactPersonUuid: tokenInfo.AccountUuid, ContactPerson: tokenInfo.Name, ContactPhone: tokenInfo.Phone, Extend: fmt.Sprintf(constant.TemplateIntoCar, tokenInfo.Name), } if task.Id > 0 { log.SnCode = task.SnCode } else { log.SnCode, err = srv.store.Car().FindSnCodeById(ctx, tokenInfo.CarId) if err != nil { return res, errors.WithStackOnce(err) } } err = srv.store.InTx(ctx, func(ctx context.Context) error { if task.Id > 0 { task.Status = constant.TaskStatusExecuting task.StartTime = models.NewMyTime(time.Now()) if err = srv.store.OrderTransportTask().UpdateById(ctx, &task); err != nil { return errors.WithStackOnce(err) } } if err = srv.store.OrderLogisticLog().Create(ctx, &log); err != nil { return errors.WithStackOnce(err) } if order.OrderStatus < constant.OrderStatusInTransit { err = srv.store.ExpressOrder().UpdateInTransitById(ctx, task.OrderId, log.Id) if err != nil { return errors.WithStackOnce(err) } } return nil }) if err != nil { return res, errors.WithStackOnce(err) } _ = domainservice.DelOrderSubNo(ctx, key) res.IntoTime = task.StartTime res.OrderId = task.OrderId return res, nil } // ScanIntoCar 扫码装车 func (srv *DriverService) ScanIntoCar(ctx context.Context, req ScanIntoReqVO) (res OrderIntoCarRespVO, err error) { tokenInfo := global.GetTokenInfoFromContext(ctx) if tokenInfo.CarId <= 0 { return res, errors.WithCode(codex.ErrParamValidate, "还未给你分配运输车,请联系管理员") } order, err := srv.store.ExpressOrder().FindByOrderNo(ctx, req.OrderNo) if err != nil { return res, errors.WithStackOnce(err) } if order.OrderStatus == constant.OrderStatusPending { return res, errors.WithCode(codex.ErrParamValidate, "订单还未分派揽件员,请耐心等待") } task := models.OrderTransportTask{} if order.LatestTaskId > 0 { if err = srv.store.OrderTransportTask().FirstById(ctx, &task, order.LatestTaskId); err != nil { if errors.IsErrRecordNotFound(err) { return res, errors.WithCode(codex.ErrParamValidate, "订单还未分派揽件员") } return res, errors.WithStackOnce(err) } if task.ExecutorAccountId != global.GetTokenInfoFromContext(ctx).AccountId { return res, errors.WithCode(codex.ErrParamValidate, "不属于你的订单不能操作") } } else { task = models.OrderTransportTask{ ExecutorAccountId: tokenInfo.AccountId, CarId: tokenInfo.CarId, OrderId: order.Id, OrderNo: order.OrderNo, Status: constant.TaskStatusExecuting, StartTime: models.NewMyTime(time.Now()), } task.SnCode, err = srv.store.Car().FindSnCodeById(ctx, tokenInfo.CarId) if err != nil { return res, errors.WithStackOnce(err) } } log := models.OrderLogisticLog{ LogType: constant.LogisticLogTraffic, OrderId: order.Id, OrderNo: order.OrderNo, SnCode: task.SnCode, ContactPersonUuid: tokenInfo.AccountUuid, ContactPerson: tokenInfo.Name, ContactPhone: tokenInfo.Phone, Extend: fmt.Sprintf(constant.TemplateIntoCar, tokenInfo.Name), } err = srv.store.InTx(ctx, func(ctx context.Context) error { if task.Id > 0 { task.Status = constant.TaskStatusExecuting task.StartTime = models.NewMyTime(time.Now()) if err = srv.store.OrderTransportTask().UpdateById(ctx, &task); err != nil { return errors.WithStackOnce(err) } } else { if err = srv.store.OrderTransportTask().Create(ctx, &task); err != nil { return errors.WithStackOnce(err) } } if err = srv.store.OrderLogisticLog().Create(ctx, &log); err != nil { return errors.WithStackOnce(err) } if order.OrderStatus < constant.OrderStatusInTransit { err = srv.store.ExpressOrder().UpdateInTransitById(ctx, task.OrderId, log.Id) if err != nil { return errors.WithStackOnce(err) } } return nil }) if err != nil { return res, errors.WithStackOnce(err) } res.IntoTime = task.StartTime res.OrderId = task.OrderId return res, nil } // SignReceipt 订单签收 func (srv *DriverService) SignReceipt(ctx context.Context, req ScanIntoReqVO) (res SignReceiptRespVO, err error) { tokenInfo := global.GetTokenInfoFromContext(ctx) order, err := srv.store.ExpressOrder().FindByOrderNo(ctx, req.OrderNo) if err != nil { return res, errors.WithStackOnce(err) } snCode, err := srv.store.Car().FindSnCodeById(ctx, tokenInfo.CarId) if err != nil { return res, errors.WithStackOnce(err) } log := models.OrderLogisticLog{ LogType: constant.LogisticLogTraffic, OrderId: order.Id, OrderNo: order.OrderNo, SnCode: snCode, ContactPersonUuid: tokenInfo.AccountUuid, ContactPerson: tokenInfo.Name, ContactPhone: tokenInfo.Phone, Extend: constant.TemplateSignReceipt, } order.OrderStatus = constant.OrderStatusReceived order.LatestLogId = log.Id order.SignReceiptTime = models.NewMyTime(time.Now()) order.SignReceiptBy = tokenInfo.AccountId err = srv.store.InTx(ctx, func(ctx context.Context) error { if err = srv.store.OrderLogisticLog().Create(ctx, &log); err != nil { return errors.WithStackOnce(err) } if order.LatestTaskId > 0 { if err = srv.store.OrderTransportTask().FinishedTask(ctx, order.LatestTaskId); err != nil { return errors.WithStackOnce(err) } } if err = srv.store.ExpressOrder().UpdateById(ctx, &order); err != nil { return errors.WithStackOnce(err) } return nil }) if err != nil { return res, errors.WithStackOnce(err) } res.OrderId = order.Id res.SingTime = order.SignReceiptTime return res, nil }