Files
bj_power/bj_power_wms/internal/handler/location.go
T

63 lines
2.3 KiB
Go
Raw Normal View History

package handler
import (
"errors"
"fmt"
"strings"
"bj_power_wms/ent"
"bj_power_wms/ent/zone"
"bj_power_wms/internal/svc"
)
// LeafLocation 末级库位(区域/货架/层/位置号四级)。
// 所有写入库存的路径统一用 requireLeafLocation 校验后再落库,禁止「只填区域」或
// 自由文本绕过库位主数据;四级字段一律完整写入,不允许条件写空。
type LeafLocation struct {
ZoneCode string
ShelfNo string
LayerNo string
PositionNo string
}
// requireLeafLocation 校验四级库位:四级均非空,且 zones 表存在 level=4、状态启用的对应节点。
func requireLeafLocation(ctx *svc.ServiceContext, zoneCode, shelfNo, layerNo, positionNo string) (LeafLocation, error) {
loc := LeafLocation{
ZoneCode: strings.TrimSpace(zoneCode),
ShelfNo: strings.TrimSpace(shelfNo),
LayerNo: strings.TrimSpace(layerNo),
PositionNo: strings.TrimSpace(positionNo),
}
if loc.ZoneCode == "" || loc.ShelfNo == "" || loc.LayerNo == "" || loc.PositionNo == "" {
return LeafLocation{}, errors.New("区域/货架/层/位置四级库位必填,请选择到末级位置号")
}
exists, err := ctx.EntClient.Zone.Query().Where(
zone.LevelEQ(4),
zone.ZoneCodeEQ(loc.ZoneCode),
zone.ShelfNoEQ(loc.ShelfNo),
zone.LayerNoEQ(loc.LayerNo),
zone.PositionNoEQ(loc.PositionNo),
zone.StatusEQ("启用"),
).Exist(ctx0())
if err != nil {
return LeafLocation{}, fmt.Errorf("核验末级库位失败: %w", err)
}
if !exists {
return LeafLocation{}, fmt.Errorf("库位 %s/%s/%s/%s 不存在或已停用,请选择启用的末级位置号",
loc.ZoneCode, loc.ShelfNo, loc.LayerNo, loc.PositionNo)
}
return loc, nil
}
// setLeafLocationCreate 库存行创建时完整写入四级库位(不用 Nillable,避免空值条件写入)。
func setLeafLocationCreate(b *ent.InventoryCreate, loc LeafLocation) *ent.InventoryCreate {
return b.SetZoneCode(loc.ZoneCode).SetShelfNo(loc.ShelfNo).
SetLayerNo(loc.LayerNo).SetPositionNo(loc.PositionNo)
}
// setLeafLocationUpdate 库存行更新时完整写入四级库位(覆盖旧库位,不允许只改其中一级)。
func setLeafLocationUpdate(u *ent.InventoryUpdateOne, loc LeafLocation) *ent.InventoryUpdateOne {
return u.SetZoneCode(loc.ZoneCode).SetShelfNo(loc.ShelfNo).
SetLayerNo(loc.LayerNo).SetPositionNo(loc.PositionNo)
}