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/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" ) // WarehousePage 仓库列表 func (srv *WarehouseService) WarehousePage(ctx context.Context, req WarehousePageReqVO) (res core.PageListResponse, err error) { dto := dao.WarehousePageDTO{ Page: req.Page, Name: req.Search.Name, SnCode: req.Search.SnCode, } ents, total, err := srv.store.Warehouse().Page(ctx, dto) if err != nil { return res, errors.WithStackOnce(err) } warehouseIds := make([]int, 0, len(ents)) accUuids := make([]string, 0, len(ents)) for _, v := range ents { warehouseIds = append(warehouseIds, v.Id) if err = core.DatatypeJSONConvertTo(v.ManagerUuid, &accUuids); err != nil { return res, errors.WithStackOnce(err) } } orderCount, err := srv.store.WarehouseOrder().CountWarehouseOrder(ctx, warehouseIds, constant.WarehouseStatusIn) if err != nil { return res, errors.WithStackOnce(err) } accMap, err := srv.store.Account().FindByUuids(ctx, accUuids) if err != nil { return res, errors.WithStackOnce(err) } list := make([]WarehousePageVO, 0, len(ents)) for _, v := range ents { manager := make([]string, 0, 2) if err = core.DatatypeJSONConvertTo(v.ManagerUuid, &manager); err != nil { return res, errors.WithStackOnce(err) } accList := make([]WarehouseManageVO, 0, len(manager)) for i := range manager { accList = append(accList, WarehouseManageVO{ AccountId: accMap[manager[i]].Id, AccountUuid: manager[i], AccountName: accMap[manager[i]].Name, }) } list = append(list, WarehousePageVO{ WarehouseId: v.Id, Name: v.Name, SnCode: v.SnCode, Address: v.Address, OrderCount: orderCount[v.Id], AccountList: accList, }) } res.Total = total res.TotalPage = total / int64(req.Page.Size) res.Count = len(list) res.List = list return res, nil } // AddWarehouse 添加仓库 func (srv *WarehouseService) AddWarehouse(ctx context.Context, req AddWarehouseReqVO) (res WarehouseIdRespVO, err error) { count, err := srv.store.Warehouse().CountByNameOrSnCode(ctx, req.Name, req.SnCode) if err != nil { return res, errors.WithStackOnce(err) } if count > 0 { return res, errors.WithCode(codex.ErrParamValidate, "名称重复或SN编码已使用") } uuids := make([]string, 0, len(req.Manage)) for _, v := range req.Manage { uuids = append(uuids, v.Uuid) } n, _ := core.ConvertJSONDatatype(uuids) warehouse := models.Warehouse{ Pid: global.GetTokenInfoFromContext(ctx).UsePid, Name: req.Name, SnCode: req.SnCode, ManagerUuid: n, Address: req.Address, Enable: req.Enable, } err = srv.store.InTx(ctx, func(ctx context.Context) error { if err = srv.store.Warehouse().Create(ctx, &warehouse); err != nil { return errors.WithStackOnce(err) } if err = domainservice.ProcessWareHouse(ctx, warehouse.Id, req.Manage); err != nil { return errors.WithStackOnce(err) } return nil }) if err != nil { return res, errors.WithStackOnce(err) } res.WarehouseId = warehouse.Id return res, nil } // UpdateWarehouse 修改仓库 func (srv *WarehouseService) UpdateWarehouse(ctx context.Context, req UpdateWarehouseReqVO) (res WarehouseIdRespVO, err error) { warehouse := models.Warehouse{} if err = srv.store.Warehouse().FirstById(ctx, &warehouse, req.WarehouseId); err != nil { return res, errors.WithStackOnce(err) } if req.Name != warehouse.Name || req.SnCode == warehouse.SnCode { count, err := srv.store.Warehouse().CountByNameOrSnCode(ctx, req.Name, req.SnCode, warehouse.Id) if err != nil { return res, errors.WithStackOnce(err) } if count > 0 { return res, errors.WithCode(codex.ErrParamValidate, "名称重复或SN编码已使用") } } uuids := make([]string, 0, len(req.Manage)) for _, v := range req.Manage { uuids = append(uuids, v.Uuid) } n, _ := core.ConvertJSONDatatype(uuids) warehouse.Name = req.Name warehouse.SnCode = req.SnCode warehouse.ManagerUuid = n warehouse.Address = req.Address warehouse.Enable = req.Enable err = srv.store.InTx(ctx, func(ctx context.Context) error { if err = srv.store.Warehouse().UpdateById(ctx, &warehouse); err != nil { return errors.WithStackOnce(err) } if err = domainservice.ProcessWareHouse(ctx, warehouse.Id, req.Manage); err != nil { return errors.WithStackOnce(err) } return nil }) if err != nil { return res, errors.WithStackOnce(err) } res.WarehouseId = warehouse.Id return res, nil }