94 lines
2.5 KiB
Go
94 lines
2.5 KiB
Go
|
|
// 库区服务
|
|||
|
|
// 职责:库区CRUD、状态更新(联动库位状态)
|
|||
|
|
// 调用链:UpdateStatus → BatchUpdateLocationStatus(状态联动)
|
|||
|
|
// 紧密耦合:dao.Zone、dao.Location(级联状态更新、删除前检查)
|
|||
|
|
// 注意:删除前检查是否存在库位,启用/禁用联动库位状态
|
|||
|
|
package service
|
|||
|
|
|
|||
|
|
import (
|
|||
|
|
"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 zone struct{}
|
|||
|
|
|
|||
|
|
var Zone = new(zone)
|
|||
|
|
|
|||
|
|
func (s *zone) Create(ctx context.Context, req *dto.CreateZoneReq) (res *dto.CreateZoneRes, err error) {
|
|||
|
|
ids, err := dao.Zone.Insert(ctx, req)
|
|||
|
|
if err != nil {
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
id := ids[0].(bson.ObjectID)
|
|||
|
|
res = &dto.CreateZoneRes{
|
|||
|
|
Id: &id,
|
|||
|
|
}
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func (s *zone) Update(ctx context.Context, req *dto.UpdateZoneReq) error {
|
|||
|
|
return dao.Zone.Update(ctx, req)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func (s *zone) Delete(ctx context.Context, req *dto.DeleteZoneReq) error {
|
|||
|
|
zoneId := req.Id.Hex()
|
|||
|
|
// 删除前检查:是否存在关联的库位
|
|||
|
|
count, err := dao.Zone.CountLocationsByZoneId(ctx, zoneId)
|
|||
|
|
if err != nil {
|
|||
|
|
return err
|
|||
|
|
}
|
|||
|
|
if count > 0 {
|
|||
|
|
return gerror.Newf("库区下存在%d个库位,无法删除", count)
|
|||
|
|
}
|
|||
|
|
return dao.Zone.DeleteFake(ctx, req)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// UpdateStatus 更新库区状态(单独的状态修改接口,联动子表)
|
|||
|
|
func (s *zone) UpdateStatus(ctx context.Context, req *dto.UpdateZoneStatusReq) (err error) {
|
|||
|
|
zoneId := req.Id.Hex()
|
|||
|
|
// 1. 更新库区状态
|
|||
|
|
if err = dao.Zone.UpdateStatus(ctx, req); err != nil {
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
// 2. 联动更新库位状态
|
|||
|
|
if req.Status == stock.ZoneStatusEnabled {
|
|||
|
|
// 启用:只恢复Disabled状态的库位,保留InUse等其他状态
|
|||
|
|
if _, err = dao.Zone.BatchUpdateLocationStatus(ctx, zoneId, stock.LocationStatusIdle, stock.LocationStatusDisabled); err != nil {
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
} else {
|
|||
|
|
// 禁用:所有库位统一设为Disabled
|
|||
|
|
if _, err = dao.Zone.BatchUpdateLocationStatus(ctx, zoneId, stock.LocationStatusDisabled); err != nil {
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func (s *zone) GetOne(ctx context.Context, req *dto.GetZoneReq) (res *dto.GetZoneRes, err error) {
|
|||
|
|
one, err := dao.Zone.GetOne(ctx, req)
|
|||
|
|
if err != nil {
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
err = utils.Struct(one, &res)
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func (s *zone) List(ctx context.Context, req *dto.ListZoneReq) (res *dto.ListZoneRes, err error) {
|
|||
|
|
list, total, err := dao.Zone.List(ctx, req)
|
|||
|
|
if err != nil {
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
res = &dto.ListZoneRes{
|
|||
|
|
Total: total,
|
|||
|
|
}
|
|||
|
|
err = utils.Struct(list, &res.List)
|
|||
|
|
return
|
|||
|
|
}
|