147 lines
5.4 KiB
Go
147 lines
5.4 KiB
Go
package handler
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"net/http"
|
||
|
|
"strconv"
|
||
|
|
"strings"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"bj_power_wms/ent/inventory"
|
||
|
|
"bj_power_wms/internal/svc"
|
||
|
|
)
|
||
|
|
|
||
|
|
// relocateStockHandler 上架/移库:把库存行搬到完整的末级库位(区域/货架/层/位置号)。
|
||
|
|
// 只改四级库位与 updated_at,不动质量状态/数量/锁定量/生命周期状态;
|
||
|
|
// 不合格与锁定行同样允许移库(2026-12-01 用户拍板:允许,只改物理位置)。
|
||
|
|
// 定位二选一:ids=批次/SN 明细抽屉里逐行指定;scope=库位明细聚合行,按物料+原四级库位+snMode 整组搬。
|
||
|
|
func relocateStockHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||
|
|
type relocateScope struct {
|
||
|
|
MaterialCode string `json:"materialCode"`
|
||
|
|
ZoneCode string `json:"zoneCode"`
|
||
|
|
ShelfNo string `json:"shelfNo"`
|
||
|
|
LayerNo string `json:"layerNo"`
|
||
|
|
PositionNo string `json:"positionNo"`
|
||
|
|
SnMode int `json:"snMode"`
|
||
|
|
Category int `json:"category"`
|
||
|
|
}
|
||
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
||
|
|
var req struct {
|
||
|
|
IDs []int `json:"ids"`
|
||
|
|
Scope relocateScope `json:"scope"`
|
||
|
|
ZoneCode string `json:"zoneCode"`
|
||
|
|
ShelfNo string `json:"shelfNo"`
|
||
|
|
LayerNo string `json:"layerNo"`
|
||
|
|
PositionNo string `json:"positionNo"`
|
||
|
|
}
|
||
|
|
if err := parseJSON(r, &req); err != nil {
|
||
|
|
fail(w, http.StatusBadRequest, "参数错误: "+err.Error())
|
||
|
|
return
|
||
|
|
}
|
||
|
|
// 目标库位强制末级四级:四级必填 + zones 表存在启用的 level=4 节点,禁止只填区域。
|
||
|
|
loc, err := requireLeafLocation(ctx, req.ZoneCode, req.ShelfNo, req.LayerNo, req.PositionNo)
|
||
|
|
if err != nil {
|
||
|
|
fail(w, http.StatusBadRequest, err.Error())
|
||
|
|
return
|
||
|
|
}
|
||
|
|
// 聚合行 scope 的「未分区」是前端展示占位,回落为空串查真实 NULL/空值行。
|
||
|
|
if req.Scope.ZoneCode == "未分区" {
|
||
|
|
req.Scope.ZoneCode = ""
|
||
|
|
}
|
||
|
|
|
||
|
|
q := ctx.EntClient.Inventory.Query().Where(inventory.StatusIn("在库", "锁定"))
|
||
|
|
if len(req.IDs) > 0 {
|
||
|
|
q = q.Where(inventory.IDIn(req.IDs...))
|
||
|
|
} else {
|
||
|
|
if strings.TrimSpace(req.Scope.MaterialCode) == "" {
|
||
|
|
fail(w, http.StatusBadRequest, "请指定要移动的库存行")
|
||
|
|
return
|
||
|
|
}
|
||
|
|
q = q.Where(inventory.MaterialCodeEqualFold(strings.TrimSpace(req.Scope.MaterialCode)))
|
||
|
|
// 空值字段同时兼容 NULL 与空串(存量脏行 shelf_no 为 NULL)。
|
||
|
|
if req.Scope.ZoneCode == "" {
|
||
|
|
q = q.Where(inventory.Or(inventory.ZoneCodeEQ(""), inventory.ZoneCodeIsNil()))
|
||
|
|
} else {
|
||
|
|
q = q.Where(inventory.ZoneCodeEqualFold(req.Scope.ZoneCode))
|
||
|
|
}
|
||
|
|
if req.Scope.ShelfNo == "" {
|
||
|
|
q = q.Where(inventory.Or(inventory.ShelfNoEQ(""), inventory.ShelfNoIsNil()))
|
||
|
|
} else {
|
||
|
|
q = q.Where(inventory.ShelfNoEqualFold(req.Scope.ShelfNo))
|
||
|
|
}
|
||
|
|
if req.Scope.LayerNo == "" {
|
||
|
|
q = q.Where(inventory.Or(inventory.LayerNoEQ(""), inventory.LayerNoIsNil()))
|
||
|
|
} else {
|
||
|
|
q = q.Where(inventory.LayerNoEqualFold(req.Scope.LayerNo))
|
||
|
|
}
|
||
|
|
if req.Scope.PositionNo == "" {
|
||
|
|
q = q.Where(inventory.Or(inventory.PositionNoEQ(""), inventory.PositionNoIsNil()))
|
||
|
|
} else {
|
||
|
|
q = q.Where(inventory.PositionNoEqualFold(req.Scope.PositionNo))
|
||
|
|
}
|
||
|
|
if req.Scope.SnMode > 0 {
|
||
|
|
q = q.Where(inventory.SnModeEQ(req.Scope.SnMode))
|
||
|
|
}
|
||
|
|
if req.Scope.Category > 0 {
|
||
|
|
q = q.Where(inventory.CategoryEQ(req.Scope.Category))
|
||
|
|
}
|
||
|
|
}
|
||
|
|
rows, err := q.All(ctx0())
|
||
|
|
if err != nil {
|
||
|
|
fail(w, http.StatusInternalServerError, "查询待移动库存行失败: "+err.Error())
|
||
|
|
return
|
||
|
|
}
|
||
|
|
if len(rows) == 0 {
|
||
|
|
fail(w, http.StatusNotFound, "没有找到可移动的库存行(仅「在库/锁定」行可移库)")
|
||
|
|
return
|
||
|
|
}
|
||
|
|
if len(rows) > 500 {
|
||
|
|
fail(w, http.StatusBadRequest, "一次最多移动 500 行,请缩小筛选范围后重试")
|
||
|
|
return
|
||
|
|
}
|
||
|
|
// 盘点冻结守卫:盘点期间库位被快照锁定,禁止移库污染快照。
|
||
|
|
for _, row := range rows {
|
||
|
|
if fno := frozenByStocktake(ctx, row.BatchNo, row.SnCode, ""); fno != "" {
|
||
|
|
fail(w, http.StatusConflict, "库存行 "+strconv.Itoa(row.ID)+" 正在盘点单 "+fno+" 中冻结,盘点期间禁止移库")
|
||
|
|
return
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
oldLocs := make([]map[string]string, 0, len(rows))
|
||
|
|
for _, row := range rows {
|
||
|
|
oldLocs = append(oldLocs, map[string]string{
|
||
|
|
"id": strconv.Itoa(row.ID), "zoneCode": row.ZoneCode,
|
||
|
|
"shelfNo": row.ShelfNo, "layerNo": row.LayerNo, "positionNo": row.PositionNo,
|
||
|
|
})
|
||
|
|
}
|
||
|
|
tx, err := ctx.EntClient.Tx(ctx0())
|
||
|
|
if err != nil {
|
||
|
|
fail(w, http.StatusInternalServerError, "开启事务失败: "+err.Error())
|
||
|
|
return
|
||
|
|
}
|
||
|
|
committed := false
|
||
|
|
defer func() {
|
||
|
|
if !committed {
|
||
|
|
_ = tx.Rollback()
|
||
|
|
}
|
||
|
|
}()
|
||
|
|
for _, row := range rows {
|
||
|
|
if _, err := setLeafLocationUpdate(tx.Inventory.UpdateOneID(row.ID), loc).
|
||
|
|
SetUpdatedAt(time.Now().Unix()).Save(ctx0()); err != nil {
|
||
|
|
fail(w, http.StatusInternalServerError, "库存行 "+strconv.Itoa(row.ID)+" 移库失败: "+err.Error())
|
||
|
|
return
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if err := tx.Commit(); err != nil {
|
||
|
|
fail(w, http.StatusInternalServerError, "提交移库失败: "+err.Error())
|
||
|
|
return
|
||
|
|
}
|
||
|
|
committed = true
|
||
|
|
|
||
|
|
newLoc := strings.Join([]string{loc.ZoneCode, loc.ShelfNo, loc.LayerNo, loc.PositionNo}, "/")
|
||
|
|
ctx.EventLog.Write(ctx0(), "inventory.relocate", r.Header.Get("X-Username"), "inventory",
|
||
|
|
strconv.Itoa(rows[0].ID), "上架/移库 "+strconv.Itoa(len(rows))+" 行到 "+newLoc,
|
||
|
|
map[string]any{"from": oldLocs, "to": newLoc, "count": len(rows)})
|
||
|
|
ok(w, map[string]any{"moved": len(rows), "location": newLoc})
|
||
|
|
}
|
||
|
|
}
|