123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- package warehousesrv
- import (
- "Cold_Logistic/internal/pkg/common/codex"
- "Cold_Logistic/internal/pkg/common/constant"
- "Cold_Logistic/internal/pkg/common/global"
- "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"
- )
- // WarehouseInfo 仓库信息
- func (srv *WarehouseService) WarehouseInfo(ctx context.Context) (res WarehouseInfoRespVO, err error) {
- tokenInfo := global.GetTokenInfoFromContext(ctx)
- warehouse, err := srv.store.Warehouse().FindByManageUuid(ctx, tokenInfo.AccountUuid)
- if err != nil {
- return res, errors.WithStackOnce(err)
- }
- res.Name = warehouse.Name
- res.SnCode = warehouse.SnCode
- res.SnIds = make([]global.SnIdVO, 0, 4)
- snIds, err := clod.NewBzdClodService().DeviceSensorList(ctx, warehouse.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
- }
- // WarehouseOrder 库中订单列表
- func (srv *WarehouseService) WarehouseOrder(ctx context.Context, req WarehouseOrderReqVO) (res core.PageListResponse, err error) {
- dto := dao.WarehouseOrderPageDTO{
- Page: req.Page,
- Status: constant.WarehouseStatusIn,
- OrderNo: req.Search.OrderNo,
- WarehouseId: global.GetTokenInfoFromContext(ctx).WarehouseId,
- TimeStart: req.Search.TimeStart,
- TimeEnd: req.Search.TimeEnd,
- }
- list, total, err := srv.store.WarehouseOrder().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
- }
- // OrderIntoHouse 订单入库
- func (srv *WarehouseService) OrderIntoHouse(ctx context.Context, req OrderIntoHouseReqVO) (res OrderIntoHouseRespVO, err error) {
- tokenInfo := global.GetTokenInfoFromContext(ctx)
- if tokenInfo.WarehouseId >= 0 {
- return res, errors.WithCode(codex.ErrNoDataPermission, "不是仓管无法入库操作")
- }
- order := models.ExpressOrder{}
- if err = srv.store.ExpressOrder().FirstById(ctx, &order, req.OrderId); err != nil {
- return res, errors.WithStackOnce(err)
- }
- logisticName, err := srv.store.LogisticCompany().FindNameById(ctx, order.LogisticId)
- if err != nil {
- return res, errors.WithStackOnce(err)
- }
- house := models.Warehouse{}
- if err = srv.store.Warehouse().FirstById(ctx, &house, tokenInfo.WarehouseId); err != nil {
- return res, errors.WithStackOnce(err)
- }
- task := models.OrderTransportTask{}
- if err = srv.store.OrderTransportTask().FirstById(ctx, &task, order.LatestTaskId); err != nil {
- return res, errors.WithStackOnce(err)
- }
- houseOrder := models.WarehouseOrder{
- WarehouseId: house.Id,
- SnCode: house.SnCode,
- OrderId: req.OrderId,
- OrderNo: order.OrderNo,
- Status: constant.WarehouseStatusIn,
- StorageTime: models.NewMyTime(time.Now()),
- StorageBy: tokenInfo.AccountId,
- }
- log := models.OrderLogisticLog{
- LogType: constant.LogisticLogWarehouse,
- OrderId: order.Id,
- OrderNo: order.OrderNo,
- SnCode: houseOrder.SnCode,
- ContactPersonUuid: tokenInfo.AccountUuid,
- ContactPerson: tokenInfo.Name,
- ContactPhone: tokenInfo.Phone,
- Extend: fmt.Sprintf(constant.TemplateIntoHouse, logisticName+"-"+house.Address),
- }
- err = srv.store.InTx(ctx, func(ctx context.Context) error {
- if err = srv.store.WarehouseOrder().Create(ctx, &houseOrder); err != nil {
- return errors.WithStackOnce(err)
- }
- order.LatestTaskId = 0
- if err = srv.store.OrderTransportTask().FinishedTask(ctx, task.Id); err != nil {
- return errors.WithStackOnce(err)
- }
- if err = srv.store.OrderLogisticLog().Create(ctx, &log); err != nil {
- return errors.WithStackOnce(err)
- }
- order.LatestLogId = log.Id
- 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.OrderNo = order.OrderNo
- res.IntoTime = houseOrder.StorageTime
- return res, nil
- }
- // OrderOutHouse 订单出库
- func (srv *WarehouseService) OrderOutHouse(ctx context.Context, req OrderOutHouseReqVO) (res OrderOutHouseRespVO, err error) {
- tokenInfo := global.GetTokenInfoFromContext(ctx)
- if tokenInfo.WarehouseId >= 0 {
- return res, errors.WithCode(codex.ErrNoDataPermission, "不是仓管无法出库操作")
- }
- houseOrder, err := srv.store.WarehouseOrder().FindByHouseIdAndOrderId(ctx, req.OrderId, tokenInfo.WarehouseId)
- if err != nil {
- return res, errors.WithStackOnce(err)
- }
- if houseOrder.Id == 0 {
- return res, errors.WithCode(codex.ErrNoDataPermission, "仓库中没有这个订单")
- }
- log := models.OrderLogisticLog{
- LogType: constant.LogisticLogWarehouse,
- OrderId: houseOrder.OrderId,
- OrderNo: houseOrder.OrderNo,
- SnCode: houseOrder.SnCode,
- ContactPersonUuid: tokenInfo.AccountUuid,
- ContactPerson: tokenInfo.Name,
- ContactPhone: tokenInfo.Phone,
- Extend: constant.TemplateOutHouse,
- }
- 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 err = srv.store.WarehouseOrder().OrderOutHouse(ctx, houseOrder.OrderId, houseOrder.WarehouseId); err != nil {
- return errors.WithStackOnce(err)
- }
- return nil
- })
- if err != nil {
- return res, errors.WithStackOnce(err)
- }
- res.OutTime = models.NewMyTime(time.Now())
- res.OrderId = houseOrder.OrderId
- res.OrderNo = houseOrder.OrderNo
- return res, nil
- }
|