97 lines
2.9 KiB
Go
97 lines
2.9 KiB
Go
|
|
// 仓库服务
|
|||
|
|
// 职责:仓库CRUD、状态更新(联动库区/库位状态)
|
|||
|
|
// 调用链:UpdateStatus → BatchUpdateZoneStatus/BatchUpdateLocationStatus(状态联动)
|
|||
|
|
// 紧密耦合:dao.Warehouse、dao.Zone/dao.Location(级联状态更新、删除前检查)
|
|||
|
|
// 注意:删除前检查是否存在库区,启用/禁用联动子表状态
|
|||
|
|
package service
|
|||
|
|
|
|||
|
|
import (
|
|||
|
|
stockConst "assets/consts/stock"
|
|||
|
|
dao "assets/dao/stock"
|
|||
|
|
dto "assets/model/dto/stock"
|
|||
|
|
"context"
|
|||
|
|
|
|||
|
|
"gitea.com/red-future/common/utils"
|
|||
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
|||
|
|
"go.mongodb.org/mongo-driver/v2/bson"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
type warehouse struct{}
|
|||
|
|
|
|||
|
|
var Warehouse = new(warehouse)
|
|||
|
|
|
|||
|
|
func (s *warehouse) Create(ctx context.Context, req *dto.CreateWarehouseReq) (res *dto.CreateWarehouseRes, err error) {
|
|||
|
|
ids, err := dao.Warehouse.Insert(ctx, req)
|
|||
|
|
if err != nil {
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
id := ids[0].(bson.ObjectID)
|
|||
|
|
res = &dto.CreateWarehouseRes{
|
|||
|
|
Id: &id,
|
|||
|
|
}
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func (s *warehouse) Update(ctx context.Context, req *dto.UpdateWarehouseReq) error {
|
|||
|
|
return dao.Warehouse.Update(ctx, req)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func (s *warehouse) Delete(ctx context.Context, req *dto.DeleteWarehouseReq) error {
|
|||
|
|
warehouseId := req.Id.Hex()
|
|||
|
|
count, err := dao.Warehouse.CountZonesByWarehouseId(ctx, warehouseId)
|
|||
|
|
if err != nil {
|
|||
|
|
return err
|
|||
|
|
}
|
|||
|
|
if count > 0 {
|
|||
|
|
return gerror.Newf("仓库下存在%d个库区,无法删除", count)
|
|||
|
|
}
|
|||
|
|
return dao.Warehouse.DeleteFake(ctx, req)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// UpdateStatus 更新仓库状态(单独的状态修改接口,联动子表)
|
|||
|
|
func (s *warehouse) UpdateStatus(ctx context.Context, req *dto.UpdateWarehouseStatusReq) (err error) {
|
|||
|
|
warehouseId := req.Id.Hex()
|
|||
|
|
if err = dao.Warehouse.UpdateStatus(ctx, req); err != nil {
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
if req.Status == stockConst.WarehouseStatusEnabled {
|
|||
|
|
// 启用:只恢复Disabled状态的库区/库位,保留InUse等其他状态
|
|||
|
|
if _, err = dao.Warehouse.BatchUpdateZoneStatus(ctx, warehouseId, stockConst.ZoneStatusEnabled, stockConst.ZoneStatusDisabled); err != nil {
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
if _, err = dao.Warehouse.BatchUpdateLocationStatus(ctx, warehouseId, stockConst.LocationStatusIdle, stockConst.LocationStatusDisabled); err != nil {
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
} else {
|
|||
|
|
// 禁用:所有库区/库位统一设为Disabled
|
|||
|
|
if _, err = dao.Warehouse.BatchUpdateZoneStatus(ctx, warehouseId, stockConst.ZoneStatusDisabled); err != nil {
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
if _, err = dao.Warehouse.BatchUpdateLocationStatus(ctx, warehouseId, stockConst.LocationStatusDisabled); err != nil {
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func (s *warehouse) GetOne(ctx context.Context, req *dto.GetWarehouseReq) (res *dto.GetWarehouseRes, err error) {
|
|||
|
|
one, err := dao.Warehouse.GetOne(ctx, req)
|
|||
|
|
if err != nil {
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
err = utils.Struct(one, &res)
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func (s *warehouse) List(ctx context.Context, req *dto.ListWarehouseReq) (res *dto.ListWarehouseRes, err error) {
|
|||
|
|
list, total, err := dao.Warehouse.List(ctx, req)
|
|||
|
|
if err != nil {
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
res = &dto.ListWarehouseRes{
|
|||
|
|
Total: total,
|
|||
|
|
}
|
|||
|
|
err = utils.Struct(list, &res.List)
|
|||
|
|
return
|
|||
|
|
}
|