feat(mes/wms): 接驳台编码统一为GW点位码 + 签字模型改报检呼叫 + 台账续流修复 + 末级库位强校验(2026-12-01 终版整改)
【全系统 · 接驳台编码终版统一(12-01 用户拍板)】
- 新增 dockcode 包作为唯一真源:工位接驳台 = GW{工位号}{Z|Y}(Z=左/Y=右,工位1~10同一规则,废除左右互换),库房 = JBT21 不变
- 编码即海康 RCS 地图点位码:落库/界面/下发 RCS/读 PLC 传感器全用同一套;旧 DOCK01~20 全量废弃,系统内无第二套编号、无换算层
- dock 表去 side 字段;agv_task.target_dock、line_point、看板点位态与中文名、dockLabel 工具全部改 GW 规则
- 产线布局说明.md 同步终版(图纸名 GxZ/GxY 与 AGV 码 GWxZ/GWxY 一一对应;注明 RCS 图与主线流向图相差 180°,读图一律以 GW 码为准)
【MES · 签字模型二次重构(11-30 实测后定稿)】
- _OPERATOR 签字整体废除:工艺步骤删 needOperatorSign / needAudit / auditRole 字段,签字角色改 CALL(工人报检呼叫)+ INSPECTOR(检验员审核签)
- 呼叫时落步骤录入快照(stepValue/stepText/stepChecked/processId),供检验员查看与指纹比对;「待签任务」只认检验员签名步骤
- 「签字复核」(AuditPending 页面 + produce.audit 菜单 + 路由)整套删除
【MES · 台账续流根因修复(11-30 对账发现)】
- 流转目标「G6」与 line_point.point_no「6」对不上导致到位落账查不到、却先释放源位→工件从调度消失;新增 ledgerPointByTarget 归一
- settleDirect/settleInTransit 改为先占目标位、成功才释放源位;目标被占原样保留,工件进缓存位排队(实测 C2 排队不再丢件)
【WMS · 末级库位强校验与上架移库】
- 新增 LocationCascade 四级联动选择器(区/架/层/号,只从启用 level=4 节点派生);手工入库、半成品/成品入库、隔离库位一律四级必填且命中启用节点
- 新增上架/移库 relocate:只改四级库位与 updated_at,不动质量状态/数量/锁定量;不合格与锁定行同样允许移库;支持 ids 逐行指定或 scope 按物料+原库位整组搬
- MES 新增 WMS 自动回流库位配置(半成品/成品各一组末级库位,默认值 1区/1架/1层/1号,存 sys_config 可改)
【MES · 统一口径与工程整改】
- 新增 workpiece_location:工单详情工件列表/WIP/追溯三处共用位置判定(SCRAPPED→DONE→SEMI_STORED→在 StationNo→在途→待流转)
- 新增 workorder_close_warn:工单完工/取消时汇总各工位实物余料出一条预警,不阻断关单
- 分页下沉:新增 pagination 工具,同步失败台账(两端)改 Count+LIMIT/OFFSET 真分页,列表不再全量切片
- 全网对账/孤儿扫描(问题临时.md 第25/26节):10 类关系边断言实测,给出 8 条可重复巡检项
【文档】
- 产线布局说明.md 按 GW 编码终版重写;问题临时.md 新增 24.7 闭环实测、25 全网对账、26 补料→配送→报工人库闭环
This commit is contained in:
@@ -181,6 +181,25 @@ func applyPreMigratePatches(ctx context.Context) {
|
||||
`ALTER TABLE return_orders ADD COLUMN IF NOT EXISTS adjusted_at bigint NOT NULL DEFAULT 0`,
|
||||
// 2026-11-28 退料三态:AGV 到位停稳(卸货放行)时间,区别于库管确认收货 receive_at。
|
||||
`ALTER TABLE return_orders ADD COLUMN IF NOT EXISTS arrived_at bigint NOT NULL DEFAULT 0`,
|
||||
// 2026-12-01 接驳台编码 GW 化终版:WMS 单据存量引用由旧 DOCK 编号改 GW 码(幂等)。
|
||||
// 映射:旧 DOCK 编号 = PLC 托盘传感器位序 1~20 → dockcode.CodeOfSeq(位序);DOCK21 → JBT21。
|
||||
// 接驳台主数据真源在 MES(由 MES seed 流程 migrateDockGwCodes 迁移),WMS 只改自家单据引用。
|
||||
`UPDATE outbound_orders SET target_dock = m.gw FROM (VALUES
|
||||
('DOCK01','GW1Z'),('DOCK02','GW1Y'),('DOCK03','GW2Y'),('DOCK04','GW2Z'),
|
||||
('DOCK05','GW3Z'),('DOCK06','GW3Y'),('DOCK07','GW4Y'),('DOCK08','GW4Z'),
|
||||
('DOCK09','GW5Z'),('DOCK10','GW5Y'),('DOCK11','GW6Y'),('DOCK12','GW6Z'),
|
||||
('DOCK13','GW7Z'),('DOCK14','GW7Y'),('DOCK15','GW8Y'),('DOCK16','GW8Z'),
|
||||
('DOCK17','GW9Z'),('DOCK18','GW9Y'),('DOCK19','GW10Y'),('DOCK20','GW10Z'),
|
||||
('DOCK21','JBT21')
|
||||
) AS m(old, gw) WHERE outbound_orders.target_dock = m.old`,
|
||||
`UPDATE return_orders SET return_dock = m.gw FROM (VALUES
|
||||
('DOCK01','GW1Z'),('DOCK02','GW1Y'),('DOCK03','GW2Y'),('DOCK04','GW2Z'),
|
||||
('DOCK05','GW3Z'),('DOCK06','GW3Y'),('DOCK07','GW4Y'),('DOCK08','GW4Z'),
|
||||
('DOCK09','GW5Z'),('DOCK10','GW5Y'),('DOCK11','GW6Y'),('DOCK12','GW6Z'),
|
||||
('DOCK13','GW7Z'),('DOCK14','GW7Y'),('DOCK15','GW8Y'),('DOCK16','GW8Z'),
|
||||
('DOCK17','GW9Z'),('DOCK18','GW9Y'),('DOCK19','GW10Y'),('DOCK20','GW10Z'),
|
||||
('DOCK21','JBT21')
|
||||
) AS m(old, gw) WHERE return_orders.return_dock = m.old`,
|
||||
}
|
||||
for _, s := range stmts {
|
||||
if _, err := rawDB.ExecContext(ctx, s); err != nil {
|
||||
|
||||
@@ -9,8 +9,8 @@ import (
|
||||
|
||||
"bj_power_wms/ent/alert"
|
||||
"bj_power_wms/ent/alertrule"
|
||||
"bj_power_wms/ent/inventory"
|
||||
"bj_power_wms/ent/inspectionrecord"
|
||||
"bj_power_wms/ent/inventory"
|
||||
"bj_power_wms/ent/material"
|
||||
"bj_power_wms/internal/svc"
|
||||
|
||||
@@ -405,12 +405,30 @@ func resolveAllAlertsHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
// listAlertRulesHandler GET /api/alert-rules 列出规则(含启用/阈值)。
|
||||
func listAlertRulesHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
rules, err := ctx.EntClient.AlertRule.Query().Order(alertrule.ByCode()).All(ctx0())
|
||||
query := r.URL.Query()
|
||||
q := ctx.EntClient.AlertRule.Query()
|
||||
// 旧调用方(不带分页参数)保持原结构=规则数组;前端显式传 page/pageSize 才走真分页。
|
||||
if !query.Has("page") && !query.Has("pageSize") {
|
||||
rules, err := q.Order(alertrule.ByCode()).All(ctx0())
|
||||
if err != nil {
|
||||
fail(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
ok(w, rules)
|
||||
return
|
||||
}
|
||||
page, pageSize := normalizePage(query.Get("page"), query.Get("pageSize"))
|
||||
total, err := q.Count(ctx0())
|
||||
if err != nil {
|
||||
fail(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
ok(w, rules)
|
||||
rules, err := q.Order(alertrule.ByCode()).Offset((page - 1) * pageSize).Limit(pageSize).All(ctx0())
|
||||
if err != nil {
|
||||
fail(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
ok(w, map[string]any{"list": rules, "total": total, "page": page, "pageSize": pageSize})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -38,6 +38,20 @@ func int64Ptr(v int64) *int64 {
|
||||
return &v
|
||||
}
|
||||
|
||||
// normalizePage 列表真分页参数归一:page 从 1 起,pageSize 1~200,默认 20。
|
||||
// 仅用于「显式带 page/pageSize」的新调用方;旧调用方不带参数时保持原全量结构。
|
||||
func normalizePage(pageStr, pageSizeStr string) (int, int) {
|
||||
page := atoi(pageStr, 1)
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
pageSize := atoi(pageSizeStr, 20)
|
||||
if pageSize < 1 || pageSize > 200 {
|
||||
pageSize = 20
|
||||
}
|
||||
return page, pageSize
|
||||
}
|
||||
|
||||
func atoi(s string, def int) int {
|
||||
if s == "" {
|
||||
return def
|
||||
|
||||
@@ -760,8 +760,14 @@ func deliveryDispatchHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
rows := make([]mesclient.DeliveryAgvItem, 0, len(items))
|
||||
// 2026-12-01 修复:requestNo 填明细行真叫料单号(it.RequestNo),此前误填配送单号
|
||||
// (DL 前缀),MES 已收累加按该号查叫料单永远查不到 → 静默失败漏记已收账。
|
||||
for _, it := range items {
|
||||
rows = append(rows, mesclient.DeliveryAgvItem{RequestNo: o.DeliveryNo,
|
||||
reqNo := it.RequestNo
|
||||
if reqNo == "" {
|
||||
reqNo = o.DeliveryNo
|
||||
}
|
||||
rows = append(rows, mesclient.DeliveryAgvItem{RequestNo: reqNo,
|
||||
MaterialCode: it.MaterialCode, MaterialName: it.MaterialName,
|
||||
Unit: it.Unit, Qty: it.Qty})
|
||||
}
|
||||
|
||||
@@ -406,46 +406,31 @@ func excelInboundHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
} else {
|
||||
// 批次号一律系统自动生成(物料编码-日期-流水号);同一导入内多行自动取得递增流水,天然唯一
|
||||
// 批次号一律系统生成、全局唯一(2026-11-30 口径):一行导入=一次收货=一个批次,
|
||||
// 库位随行独立,不存在"同批次追加"(追加会把新库位丢掉、账实不符)。
|
||||
batchNo := genBatchNo(ctx, pr.code)
|
||||
existing, e2 := tx.Inventory.Query().
|
||||
Where(inventory.SnModeEQ(snModeBatch), inventory.BatchNoEQ(batchNo)).Only(ctx0())
|
||||
if e2 == nil {
|
||||
if _, e = tx.Inventory.UpdateOneID(existing.ID).
|
||||
AddQuantity(pr.qty).
|
||||
SetNillableSupplier(strPtr(pr.supplier)).
|
||||
SetNillableProductWorkNo(strPtr(pr.productWorkNo)).
|
||||
SetNillableShelfNo(strPtr(pr.shelfNo)).
|
||||
SetNillableLayerNo(strPtr(pr.layerNo)).
|
||||
SetNillablePositionNo(strPtr(pr.positionNo)).
|
||||
Save(ctx0()); e != nil {
|
||||
fail(w, http.StatusInternalServerError, "追加批次失败: "+e.Error())
|
||||
return
|
||||
}
|
||||
} else {
|
||||
if _, e = tx.Inventory.Create().
|
||||
SetManageMode(m.ManageMode).
|
||||
SetSnMode(m.SnMode).
|
||||
SetBatchNo(batchNo).
|
||||
SetMaterialCode(pr.code).
|
||||
SetNillableMaterialName(strPtr(m.Name)).
|
||||
SetQuantity(pr.qty).SetLockedQty(0).
|
||||
SetNillableProductionDate(strPtr(pr.prodDate)).
|
||||
SetNillableSupplier(strPtr(pr.supplier)).
|
||||
SetQualityStatus("未检").
|
||||
SetNillableZoneCode(strPtr(pr.zone)).
|
||||
SetNillableShelfNo(strPtr(pr.shelfNo)).
|
||||
SetNillableLayerNo(strPtr(pr.layerNo)).
|
||||
SetNillablePositionNo(strPtr(pr.positionNo)).
|
||||
SetNillableProductWorkNo(strPtr(pr.productWorkNo)).
|
||||
SetNillableOwnershipType(strPtr(pr.ownershipType)).
|
||||
SetNillableOwnershipNo(strPtr(pr.ownershipNo)).
|
||||
SetStatus("在库").
|
||||
SetInboundNo(inboundNo).
|
||||
Save(ctx0()); e != nil {
|
||||
fail(w, http.StatusInternalServerError, "创建库存失败: "+e.Error())
|
||||
return
|
||||
}
|
||||
if _, e = tx.Inventory.Create().
|
||||
SetManageMode(m.ManageMode).
|
||||
SetSnMode(m.SnMode).
|
||||
SetBatchNo(batchNo).
|
||||
SetMaterialCode(pr.code).
|
||||
SetNillableMaterialName(strPtr(m.Name)).
|
||||
SetQuantity(pr.qty).SetLockedQty(0).
|
||||
SetNillableProductionDate(strPtr(pr.prodDate)).
|
||||
SetNillableSupplier(strPtr(pr.supplier)).
|
||||
SetQualityStatus("未检").
|
||||
SetNillableZoneCode(strPtr(pr.zone)).
|
||||
SetNillableShelfNo(strPtr(pr.shelfNo)).
|
||||
SetNillableLayerNo(strPtr(pr.layerNo)).
|
||||
SetNillablePositionNo(strPtr(pr.positionNo)).
|
||||
SetNillableProductWorkNo(strPtr(pr.productWorkNo)).
|
||||
SetNillableOwnershipType(strPtr(pr.ownershipType)).
|
||||
SetNillableOwnershipNo(strPtr(pr.ownershipNo)).
|
||||
SetStatus("在库").
|
||||
SetInboundNo(inboundNo).
|
||||
Save(ctx0()); e != nil {
|
||||
fail(w, http.StatusInternalServerError, "创建库存失败: "+e.Error())
|
||||
return
|
||||
}
|
||||
if _, e = tx.InboundDetail.Create().
|
||||
SetInboundNo(inboundNo).SetBatchNo(batchNo).SetMaterialCode(pr.code).SetQuantity(pr.qty).
|
||||
@@ -610,6 +595,14 @@ func parseInboundImportPlan(ctx *svc.ServiceContext, rows [][]string, mode strin
|
||||
errs = append(errs, importErr{pr.row, pr.code, "货位不完整(货架/层/位置均为必填)"})
|
||||
continue
|
||||
}
|
||||
// 末级库位存在性:必须命中 zones 表启用的 level=4 节点(与手工录入同一口径)。
|
||||
// 写入端用归一化后的四级字段,禁止自由文本/停用库位随导入落库。
|
||||
if loc, le := requireLeafLocation(ctx, pr.zone, pr.shelfNo, pr.layerNo, pr.positionNo); le != nil {
|
||||
errs = append(errs, importErr{pr.row, pr.code, le.Error()})
|
||||
continue
|
||||
} else {
|
||||
pr.zone, pr.shelfNo, pr.layerNo, pr.positionNo = loc.ZoneCode, loc.ShelfNo, loc.LayerNo, loc.PositionNo
|
||||
}
|
||||
// 归属校验(与手动录入口径一致:workorder/project 必填编号;other/空 不需要)
|
||||
pr.ownershipType = ownershipTypeOrNone(pr.ownershipType)
|
||||
if pr.ownershipType == "workorder" || pr.ownershipType == "project" {
|
||||
|
||||
@@ -33,6 +33,11 @@ func createFinishedInventory(ctx *svc.ServiceContext, req *finishedInboundReques
|
||||
if req.Sn == "" || req.ProductCode == "" {
|
||||
return nil, http.StatusBadRequest, errors.New("sn 与 productCode 必填")
|
||||
}
|
||||
// 末级库位(四级)必填且必须命中启用节点:手工入库与 MES 完工回流共用同一校验口径。
|
||||
loc, err := requireLeafLocation(ctx, req.ZoneCode, req.ShelfNo, req.LayerNo, req.PositionNo)
|
||||
if err != nil {
|
||||
return nil, http.StatusBadRequest, err
|
||||
}
|
||||
// 质量状态归一:合法枚举值原样尊重;空值或非枚举值(如 MES 误传「已通过/部分通过」)按产品状态派生兜底,
|
||||
// 避免脏值落库(库存质量状态仅认 合格/不合格/未检/待退)。
|
||||
quality := normalizeQualityStatus(req.QualityStatus, req.ProductStatus)
|
||||
@@ -79,7 +84,7 @@ func createFinishedInventory(ctx *svc.ServiceContext, req *finishedInboundReques
|
||||
remark = "成品完工回流(SN来源:" + snSourceLabel(req.SnSource) + ")"
|
||||
}
|
||||
|
||||
rec, err := ctx.EntClient.Inventory.Create().
|
||||
rec, err := setLeafLocationCreate(ctx.EntClient.Inventory.Create().
|
||||
SetManageMode(matType).
|
||||
SetSnMode(snModeSN).
|
||||
SetCategory(invCatFinished).
|
||||
@@ -88,7 +93,6 @@ func createFinishedInventory(ctx *svc.ServiceContext, req *finishedInboundReques
|
||||
SetSnCode(req.Sn).
|
||||
SetQuantity(1).
|
||||
SetQualityStatus(quality).
|
||||
SetNillableZoneCode(strPtr(req.ZoneCode)).
|
||||
SetStatus("在库").
|
||||
SetNillableInboundNo(strPtr(inboundNo)).
|
||||
SetOrderNo(req.OrderNo).
|
||||
@@ -103,8 +107,7 @@ func createFinishedInventory(ctx *svc.ServiceContext, req *finishedInboundReques
|
||||
// 2026-11-13 拍板:记录员必须留当前登录人的结构化 user_id(服务端写入,前端只读)。
|
||||
SetRecordedByID(req.RecordedByID).
|
||||
SetNillableRecordedAt(int64Ptr(recordedAt)).
|
||||
SetRemark(remark).
|
||||
Save(ctx0())
|
||||
SetRemark(remark), loc).Save(ctx0())
|
||||
if err != nil {
|
||||
if isUniqueViolation(err) {
|
||||
return nil, http.StatusConflict, errors.New("成品 SN 已存在库存: " + req.Sn)
|
||||
@@ -182,9 +185,11 @@ func finishedLookupHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
|
||||
// manualFinishedInboundHandler POST /api/finished/inbound 手工成品入库(登录态,权限 wms.finished:edit)。
|
||||
// 2026-11-13 拍板:
|
||||
// ① 扫 SN 反查 MES;MES 不可达 / SN 查不到 → 硬拒入库(502/404)。
|
||||
// ② 图号/产品名称/工单/产品状态一律由 MES 透传,服务端强制覆写,禁止库管手选或前端伪造。
|
||||
// ③ 记录员锁死,后端按 JWT 的 X-UserID + X-Username 写入 recorded_by_id / recorded_by。
|
||||
//
|
||||
// ① 扫 SN 反查 MES;MES 不可达 / SN 查不到 → 硬拒入库(502/404)。
|
||||
// ② 图号/产品名称/工单/产品状态一律由 MES 透传,服务端强制覆写,禁止库管手选或前端伪造。
|
||||
// ③ 记录员锁死,后端按 JWT 的 X-UserID + X-Username 写入 recorded_by_id / recorded_by。
|
||||
//
|
||||
// 【红线】本拦截只放在这个产品专用 handler 内:绝不挂全局中间件、绝不改 createFinishedInventory
|
||||
// (MES 完工检自动回流共用)、绝不改 /api/inbound/* 物料入库链——物料入库不调用 MES,宕机完全不受影响。
|
||||
func manualFinishedInboundHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
|
||||
@@ -192,6 +192,13 @@ func createInboundHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
fail(w, http.StatusBadRequest, "货架/层/位置必填,不允许只填区域不填货架")
|
||||
return
|
||||
}
|
||||
// 四级非空之外还要命中真实启用库位节点(level=4),防止自由文本/停用库位写入库存。
|
||||
loc, lerr := requireLeafLocation(ctx, req.ZoneCode, req.ShelfNo, req.LayerNo, req.PositionNo)
|
||||
if lerr != nil {
|
||||
fail(w, http.StatusBadRequest, lerr.Error())
|
||||
return
|
||||
}
|
||||
req.ZoneCode, req.ShelfNo, req.LayerNo, req.PositionNo = loc.ZoneCode, loc.ShelfNo, loc.LayerNo, loc.PositionNo
|
||||
if req.InspectionNo == "" {
|
||||
fail(w, http.StatusBadRequest, "检测单号必填,请填写入库检报告单号")
|
||||
return
|
||||
@@ -304,6 +311,7 @@ func createInboundHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
}
|
||||
|
||||
totalQty := 0
|
||||
createdBatchNo := "" // 序列号=否时回传系统生成的批次号(批次不再由前端填)
|
||||
|
||||
// 序列号=否(sn_mode=2,按数量记账;类型可以是结构件/电气件/其他任意一种)
|
||||
if m.SnMode == snModeBatch {
|
||||
@@ -312,10 +320,15 @@ func createInboundHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
fail(w, http.StatusBadRequest, "该物料序列号=否(按数量管理),不应提交 SN 清单,请使用『序列号=否入库』")
|
||||
return
|
||||
}
|
||||
batchNo := req.BatchNo
|
||||
if batchNo == "" {
|
||||
batchNo = genBatchNo(ctx, req.MaterialCode)
|
||||
// 2026-11-30 拍板(批次口径):批次号一律系统生成、全局唯一——批次是系统内部的"收货批次"
|
||||
// 标识,不是业务编号,现场不填也不该填。一次收货=一个批次,库位/质量随行,天然不冲突;
|
||||
// 同时杜绝"同批次换库位入库被并成一行、库位被静默丢弃"的账实不符。
|
||||
if strings.TrimSpace(req.BatchNo) != "" {
|
||||
fail(w, http.StatusBadRequest, "批次号由系统自动生成,无需填写;同一物料分批到货请分两笔入库")
|
||||
return
|
||||
}
|
||||
batchNo := genBatchNo(ctx, req.MaterialCode)
|
||||
createdBatchNo = batchNo
|
||||
// 回写入库单头:便于「入库记录」列表直接展示批次号(库存行/明细已含批次,此处补齐表头)
|
||||
if _, e := tx.InboundOrder.UpdateOneID(ib.ID).SetBatchNo(batchNo).Save(ctx0()); e != nil {
|
||||
fail(w, http.StatusInternalServerError, "回写入库单批次号失败: "+e.Error())
|
||||
@@ -327,50 +340,27 @@ func createInboundHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
// 同批次追加(多批到货追加)
|
||||
// 批次号唯一性以「物料+批次」为准:同号不同料 = 占用冲突(防跨物料串批)
|
||||
existing, e := tx.Inventory.Query().
|
||||
Where(inventory.SnModeEQ(snModeBatch), inventory.BatchNoEQ(batchNo)).Only(ctx0())
|
||||
if e == nil {
|
||||
if existing.MaterialCode != req.MaterialCode {
|
||||
fail(w, http.StatusConflict, "批次号 "+batchNo+" 已被物料 "+existing.MaterialCode+" 占用,请换批次号")
|
||||
return
|
||||
}
|
||||
// 盘点冻结守卫(问题2):盘点期间库存量冻结,禁止向已锁定批次追加入库。
|
||||
if existing.StocktakeNo != "" {
|
||||
fail(w, http.StatusConflict, "批次 "+batchNo+" 正在盘点单 "+existing.StocktakeNo+" 中冻结,盘点期间禁止入库追加")
|
||||
return
|
||||
}
|
||||
if _, err = tx.Inventory.UpdateOneID(existing.ID).
|
||||
AddQuantity(qty).
|
||||
SetNillableSupplier(strPtr(req.Supplier)).
|
||||
SetNillableProductWorkNo(strPtr(req.ProductWorkNo)).
|
||||
Save(ctx0()); err != nil {
|
||||
fail(w, http.StatusInternalServerError, "追加批次失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
} else {
|
||||
if _, err = tx.Inventory.Create().
|
||||
SetManageMode(m.ManageMode).
|
||||
SetSnMode(m.SnMode).
|
||||
SetMaterialCode(req.MaterialCode).
|
||||
SetNillableMaterialName(strPtr(m.Name)).
|
||||
SetBatchNo(batchNo).
|
||||
SetQuantity(qty).SetLockedQty(0).
|
||||
SetNillableProductionDate(strPtr(req.ProductionDate)).
|
||||
SetNillableSupplier(strPtr(req.Supplier)).
|
||||
SetQualityStatus("未检").
|
||||
SetNillableZoneCode(strPtr(req.ZoneCode)).
|
||||
SetNillableShelfNo(strPtr(req.ShelfNo)).
|
||||
SetNillableLayerNo(strPtr(req.LayerNo)).
|
||||
SetNillablePositionNo(strPtr(req.PositionNo)).
|
||||
SetStatus("在库").
|
||||
SetInboundNo(inboundNo).
|
||||
SetNillableProductWorkNo(strPtr(req.ProductWorkNo)).
|
||||
Save(ctx0()); err != nil {
|
||||
fail(w, http.StatusInternalServerError, "创建库存行失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
// 一次收货一行库存:不再"同批次追加"(批次唯一=一次收货),库位与本次入库一致。
|
||||
if _, err = tx.Inventory.Create().
|
||||
SetManageMode(m.ManageMode).
|
||||
SetSnMode(m.SnMode).
|
||||
SetMaterialCode(req.MaterialCode).
|
||||
SetNillableMaterialName(strPtr(m.Name)).
|
||||
SetBatchNo(batchNo).
|
||||
SetQuantity(qty).SetLockedQty(0).
|
||||
SetNillableProductionDate(strPtr(req.ProductionDate)).
|
||||
SetNillableSupplier(strPtr(req.Supplier)).
|
||||
SetQualityStatus("未检").
|
||||
SetNillableZoneCode(strPtr(req.ZoneCode)).
|
||||
SetNillableShelfNo(strPtr(req.ShelfNo)).
|
||||
SetNillableLayerNo(strPtr(req.LayerNo)).
|
||||
SetNillablePositionNo(strPtr(req.PositionNo)).
|
||||
SetStatus("在库").
|
||||
SetInboundNo(inboundNo).
|
||||
SetNillableProductWorkNo(strPtr(req.ProductWorkNo)).
|
||||
Save(ctx0()); err != nil {
|
||||
fail(w, http.StatusInternalServerError, "创建库存行失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// 明细
|
||||
@@ -459,7 +449,7 @@ func createInboundHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
ok(w, map[string]any{
|
||||
"inboundNo": inboundNo,
|
||||
"quantity": totalQty,
|
||||
"batchNo": req.BatchNo,
|
||||
"batchNo": createdBatchNo,
|
||||
"snCount": len(req.SnList),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -329,6 +329,11 @@ func batchFlipInspectionHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
SetQuantity(d.qty).SetLockedQty(0).
|
||||
SetQualityStatus(ngQuality).SetStatus(ngStatus).
|
||||
SetNillableZoneCode(strPtr(it.inv.ZoneCode)).
|
||||
// 2026-11-30 修:隔离行必须继承原行完整四级库位,否则不合格料只剩"区域"、
|
||||
// 货架/层/位置为空,隔离区拣货、盘点、出库四级库位校验全部对不上。
|
||||
SetNillableShelfNo(strPtr(it.inv.ShelfNo)).
|
||||
SetNillableLayerNo(strPtr(it.inv.LayerNo)).
|
||||
SetNillablePositionNo(strPtr(it.inv.PositionNo)).
|
||||
SetNillableInboundNo(strPtr(it.inv.InboundNo)).
|
||||
SetRemark(remark).
|
||||
Save(ctx0()); err != nil {
|
||||
|
||||
@@ -530,7 +530,10 @@ type finishedInboundRequest struct {
|
||||
ProductName string `json:"productName"` // 产品名称(档案缺失时兜底展示)
|
||||
Sn string `json:"sn"` // 成品 SN(系统生成或手填,全局唯一)
|
||||
OrderNo string `json:"orderNo"` // 关联工单号
|
||||
ZoneCode string `json:"zoneCode"` // 入库区域(可空=未分区)
|
||||
ZoneCode string `json:"zoneCode"` // 入库区域(末级库位四级之一,必填)
|
||||
ShelfNo string `json:"shelfNo"` // 货架号(末级库位四级之一,必填)
|
||||
LayerNo string `json:"layerNo"` // 层号(末级库位四级之一,必填)
|
||||
PositionNo string `json:"positionNo"` // 位置号(末级库位四级之一,必填)
|
||||
QualityStatus string `json:"qualityStatus"` // 质量状态(默认 合格;MES 完工检按产品状态传入:已通过→合格/部分通过→合格/返修→不合格,后端尊重传入值不重复映射)
|
||||
SnSource string `json:"snSource"` // AUTO=系统生成 / MANUAL=手填,仅记录
|
||||
Operator string `json:"operator"`
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
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)
|
||||
}
|
||||
@@ -39,6 +39,9 @@ func semiInboundHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
MaterialCode string `json:"materialCode"`
|
||||
CompletedText string `json:"completedText"` // 已完成至工位(展示快照,空=未报工)
|
||||
ZoneCode string `json:"zoneCode"`
|
||||
ShelfNo string `json:"shelfNo"`
|
||||
LayerNo string `json:"layerNo"`
|
||||
PositionNo string `json:"positionNo"`
|
||||
OrderNo string `json:"orderNo"`
|
||||
ProductWorkNo string `json:"productWorkNo"` // 工单号(A1 五页带出;MES 回流可顺带传)
|
||||
Operator string `json:"operator"`
|
||||
@@ -59,6 +62,12 @@ func semiInboundHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
fail(w, http.StatusBadRequest, "sn 必填")
|
||||
return
|
||||
}
|
||||
// 末级库位(四级)必填且必须命中启用的 level=4 库位节点;写库时四级一次性落全。
|
||||
loc, lerr := requireLeafLocation(ctx, req.ZoneCode, req.ShelfNo, req.LayerNo, req.PositionNo)
|
||||
if lerr != nil {
|
||||
fail(w, http.StatusBadRequest, lerr.Error())
|
||||
return
|
||||
}
|
||||
category := req.Category
|
||||
if category != invCatFinished {
|
||||
category = invCatSemi
|
||||
@@ -106,7 +115,7 @@ func semiInboundHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
rec, err := ctx.EntClient.Inventory.Create().
|
||||
rec, err := setLeafLocationCreate(ctx.EntClient.Inventory.Create().
|
||||
SetManageMode(matType).
|
||||
SetSnMode(snModeSN).
|
||||
SetCategory(category).
|
||||
@@ -115,7 +124,6 @@ func semiInboundHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
SetMaterialName(materialName).
|
||||
SetQuantity(1).
|
||||
SetQualityStatus("未检").
|
||||
SetNillableZoneCode(strPtr(req.ZoneCode)).
|
||||
SetStatus("在库").
|
||||
SetNillableInboundNo(strPtr(req.OrderNo)).
|
||||
SetOrderNo(req.OrderNo).
|
||||
@@ -128,8 +136,7 @@ func semiInboundHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
SetNillableTestRecordNo(strPtr(req.TestRecordNo)).
|
||||
SetNillableRecordedBy(strPtr(req.RecordedBy)).
|
||||
SetNillableRecordedAt(int64Ptr(req.RecordedAt)).
|
||||
SetRemark(catLabel + "完工暂存回流").
|
||||
Save(ctx0())
|
||||
SetRemark(catLabel+"完工暂存回流"), loc).Save(ctx0())
|
||||
if err != nil {
|
||||
fail(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
@@ -375,4 +382,3 @@ func querySemiHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -56,6 +56,12 @@ func createProductionReturnHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
fail(w, http.StatusBadRequest, "质量原因(reason)必填")
|
||||
return
|
||||
}
|
||||
// 隔离库位必须落到末级(区域/货架/层/位置全填且为启用的 level=4 节点),四级一次性写全。
|
||||
loc, lerr := requireLeafLocation(ctx, req.ZoneCode, req.ShelfNo, req.LayerNo, req.PositionNo)
|
||||
if lerr != nil {
|
||||
fail(w, http.StatusBadRequest, lerr.Error())
|
||||
return
|
||||
}
|
||||
operator := r.Header.Get("X-Username")
|
||||
if operator == "" {
|
||||
operator = "库管"
|
||||
@@ -149,30 +155,18 @@ func createProductionReturnHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
fail(w, http.StatusConflict, "SN "+sn+" 正在盘点单 "+row.StocktakeNo+" 中冻结,禁止退不良加回")
|
||||
return
|
||||
}
|
||||
upd := tx.Inventory.UpdateOneID(row.ID).
|
||||
upd := setLeafLocationUpdate(tx.Inventory.UpdateOneID(row.ID).
|
||||
SetStatus("在库").SetLockedQty(0).
|
||||
SetQualityStatus(quality).
|
||||
SetInboundNo("PRD:" + returnNo).
|
||||
SetUpdatedAt(time.Now().Unix())
|
||||
if req.ZoneCode != "" {
|
||||
upd = upd.SetZoneCode(req.ZoneCode)
|
||||
}
|
||||
if req.ShelfNo != "" {
|
||||
upd = upd.SetShelfNo(req.ShelfNo)
|
||||
}
|
||||
if req.LayerNo != "" {
|
||||
upd = upd.SetLayerNo(req.LayerNo)
|
||||
}
|
||||
if req.PositionNo != "" {
|
||||
upd = upd.SetPositionNo(req.PositionNo)
|
||||
}
|
||||
SetInboundNo("PRD:"+returnNo).
|
||||
SetUpdatedAt(time.Now().Unix()), loc)
|
||||
if _, err = upd.Save(ctx0()); err != nil {
|
||||
fail(w, http.StatusInternalServerError, "SN "+sn+" 库存重建失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
continue
|
||||
}
|
||||
cr := tx.Inventory.Create().
|
||||
cr := setLeafLocationCreate(tx.Inventory.Create().
|
||||
SetManageMode(m.ManageMode).
|
||||
SetSnMode(m.SnMode).
|
||||
SetMaterialCode(req.MaterialCode).
|
||||
@@ -181,19 +175,7 @@ func createProductionReturnHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
SetQuantity(1).SetLockedQty(0).
|
||||
SetQualityStatus(quality).
|
||||
SetStatus("在库").
|
||||
SetInboundNo("PRD:" + returnNo)
|
||||
if req.ZoneCode != "" {
|
||||
cr = cr.SetZoneCode(req.ZoneCode)
|
||||
}
|
||||
if req.ShelfNo != "" {
|
||||
cr = cr.SetShelfNo(req.ShelfNo)
|
||||
}
|
||||
if req.LayerNo != "" {
|
||||
cr = cr.SetLayerNo(req.LayerNo)
|
||||
}
|
||||
if req.PositionNo != "" {
|
||||
cr = cr.SetPositionNo(req.PositionNo)
|
||||
}
|
||||
SetInboundNo("PRD:"+returnNo), loc)
|
||||
if req.Reason != "" {
|
||||
cr = cr.SetRemark(req.Reason)
|
||||
}
|
||||
@@ -206,19 +188,9 @@ func createProductionReturnHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
// 序列号=否:按 质量(不合格)+四级库位+批次 精确匹配同料同状态货位追加;否则新建隔离行
|
||||
q := tx.Inventory.Query().Where(
|
||||
inventory.SnModeEQ(snModeBatch), inventory.MaterialCodeEQ(req.MaterialCode),
|
||||
inventory.StatusEQ("在库"), inventory.QualityStatusEQ(quality))
|
||||
if req.ZoneCode != "" {
|
||||
q = q.Where(inventory.ZoneCodeEQ(req.ZoneCode))
|
||||
}
|
||||
if req.ShelfNo != "" {
|
||||
q = q.Where(inventory.ShelfNoEQ(req.ShelfNo))
|
||||
}
|
||||
if req.LayerNo != "" {
|
||||
q = q.Where(inventory.LayerNoEQ(req.LayerNo))
|
||||
}
|
||||
if req.PositionNo != "" {
|
||||
q = q.Where(inventory.PositionNoEQ(req.PositionNo))
|
||||
}
|
||||
inventory.StatusEQ("在库"), inventory.QualityStatusEQ(quality),
|
||||
inventory.ZoneCodeEQ(loc.ZoneCode), inventory.ShelfNoEQ(loc.ShelfNo),
|
||||
inventory.LayerNoEQ(loc.LayerNo), inventory.PositionNoEQ(loc.PositionNo))
|
||||
if req.BatchNo != "" {
|
||||
q = q.Where(inventory.BatchNoEQ(req.BatchNo))
|
||||
}
|
||||
@@ -239,7 +211,7 @@ func createProductionReturnHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
if bn == "" {
|
||||
bn = genBatchNo(ctx, req.MaterialCode)
|
||||
}
|
||||
cr := tx.Inventory.Create().
|
||||
cr := setLeafLocationCreate(tx.Inventory.Create().
|
||||
SetManageMode(m.ManageMode).
|
||||
SetSnMode(m.SnMode).
|
||||
SetMaterialCode(req.MaterialCode).
|
||||
@@ -248,19 +220,7 @@ func createProductionReturnHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
SetQuantity(req.Qty).SetLockedQty(0).
|
||||
SetQualityStatus(quality).
|
||||
SetStatus("在库").
|
||||
SetInboundNo("PRD:" + returnNo)
|
||||
if req.ZoneCode != "" {
|
||||
cr = cr.SetZoneCode(req.ZoneCode)
|
||||
}
|
||||
if req.ShelfNo != "" {
|
||||
cr = cr.SetShelfNo(req.ShelfNo)
|
||||
}
|
||||
if req.LayerNo != "" {
|
||||
cr = cr.SetLayerNo(req.LayerNo)
|
||||
}
|
||||
if req.PositionNo != "" {
|
||||
cr = cr.SetPositionNo(req.PositionNo)
|
||||
}
|
||||
SetInboundNo("PRD:"+returnNo), loc)
|
||||
if req.Reason != "" {
|
||||
cr = cr.SetRemark(req.Reason)
|
||||
}
|
||||
@@ -277,7 +237,7 @@ func createProductionReturnHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
}
|
||||
committed = true
|
||||
|
||||
locStr := strings.Join([]string{req.ZoneCode, req.ShelfNo, req.LayerNo, req.PositionNo}, "/")
|
||||
locStr := strings.Join([]string{loc.ZoneCode, loc.ShelfNo, loc.LayerNo, loc.PositionNo}, "/")
|
||||
ctx.EventLog.Write(ctx0(), "production.return", operator, "return_order", returnNo,
|
||||
"生产退不良 "+req.MaterialCode+" x"+strconv.Itoa(req.Qty)+" 已重建隔离库存(不合格)",
|
||||
map[string]any{"orderNo": req.OrderNo, "stationNo": req.StationNo,
|
||||
|
||||
@@ -72,6 +72,7 @@ var seedPermissions = []seedPerm{
|
||||
{"inspection:create", "录入检验", "BUTTON", "", "", 220, "inspection:view"},
|
||||
{"inspection:export", "导出", "BUTTON", "", "", 221, "inspection:view"},
|
||||
{"inventory:export", "导出", "BUTTON", "", "", 204, "inventory:view"},
|
||||
{"inventory:relocate", "上架/移库", "BUTTON", "", "", 205, "inventory:view"},
|
||||
{"stocktake:start", "发起盘点", "BUTTON", "", "", 230, "stocktake:view"},
|
||||
{"stocktake:writeback", "差异写回", "BUTTON", "", "", 231, "stocktake:view"},
|
||||
{"material:create", "新增物料", "BUTTON", "", "", 240, "material:view"},
|
||||
@@ -317,18 +318,36 @@ func seedRbacHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
// listRolesHandler GET /api/roles 角色列表(含权限码)
|
||||
func listRolesHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
return requirePerm(ctx, "role:manage", func(w http.ResponseWriter, r *http.Request) {
|
||||
keyword := strings.TrimSpace(r.URL.Query().Get("keyword"))
|
||||
q := ctx.EntClient.Role.Query().Order(ent.Asc("id"))
|
||||
query := r.URL.Query()
|
||||
keyword := strings.TrimSpace(query.Get("keyword"))
|
||||
q := ctx.EntClient.Role.Query()
|
||||
if keyword != "" {
|
||||
kw := keyword
|
||||
q = q.Where(role.Or(role.NameContainsFold(kw), role.CodeContainsFold(kw)))
|
||||
}
|
||||
list, err := q.All(ctx0())
|
||||
// 不带 page/pageSize 的旧调用方仍拿全量 {list};带分页参数才返回真分页结构。
|
||||
paged := query.Has("page") || query.Has("pageSize")
|
||||
if !paged {
|
||||
list, err := q.Order(ent.Asc("id")).All(ctx0())
|
||||
if err != nil {
|
||||
fail(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
ok(w, map[string]any{"list": list})
|
||||
return
|
||||
}
|
||||
page, pageSize := normalizePage(query.Get("page"), query.Get("pageSize"))
|
||||
total, err := q.Count(ctx0())
|
||||
if err != nil {
|
||||
fail(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
ok(w, map[string]any{"list": list})
|
||||
list, err := q.Order(ent.Asc("id")).Offset((page - 1) * pageSize).Limit(pageSize).All(ctx0())
|
||||
if err != nil {
|
||||
fail(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
ok(w, map[string]any{"list": list, "total": total, "page": page, "pageSize": pageSize})
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,146 @@
|
||||
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})
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -177,6 +177,8 @@ func RegisterHandlers(server *rest.Server, ctx *svc.ServiceContext) {
|
||||
{Method: http.MethodPost, Path: "/api/stock/lock", Handler: lockStockHandler(ctx)},
|
||||
{Method: http.MethodPost, Path: "/api/stock/unlock", Handler: unlockStockHandler(ctx)},
|
||||
{Method: http.MethodPost, Path: "/api/stock/check", Handler: checkStockHandler(ctx)},
|
||||
// 上架/移库:只改四级库位,不动质量状态/数量/锁定量;不合格与锁定行同样允许(2026-12-01 拍板)
|
||||
{Method: http.MethodPost, Path: "/api/stock/relocate", Handler: requirePerm(ctx, "inventory:relocate", relocateStockHandler(ctx))},
|
||||
{Method: http.MethodGet, Path: "/api/stock/shortage", Handler: shortageHandler(ctx)},
|
||||
{Method: http.MethodGet, Path: "/api/stock/material-demand", Handler: materialDemandHandler(ctx)},
|
||||
},
|
||||
@@ -267,6 +269,7 @@ func RegisterHandlers(server *rest.Server, ctx *svc.ServiceContext) {
|
||||
{Method: http.MethodPost, Path: "/api/return-order/manual", Handler: createReturnOrderHandler(ctx, true)},
|
||||
// Q10:库管派车退料单(工位只发起需求并把料放接驳台)
|
||||
{Method: http.MethodPost, Path: "/api/return-order/dispatch", Handler: requirePerm(ctx, "return:confirm", dispatchReturnOrderByWarehouseHandler(ctx))},
|
||||
{Method: http.MethodPost, Path: "/api/return-order/receive-package", Handler: requirePerm(ctx, "return:confirm", receiveReturnPackageHandler(ctx))},
|
||||
// 取消 PENDING 退料单(2026-11-04 拍板:库房可取消工位申请;已收货不可取消;联动作废未完成 AGV 任务)
|
||||
{Method: http.MethodPost, Path: "/api/return-order/cancel", Handler: cancelReturnOrderHandler(ctx)},
|
||||
// 余量参考值(2026-11-04 定稿:剩余余量=应剩余量−已退余量−已损耗,转发 MES 聚合;仅展示不阻断)
|
||||
@@ -406,6 +409,7 @@ func RegisterHandlers(server *rest.Server, ctx *svc.ServiceContext) {
|
||||
{Method: http.MethodGet, Path: "/api/internal/return-order/station-list", Handler: wrapInternal(ctx)(stationReturnListInternalHandler(ctx))},
|
||||
{Method: http.MethodGet, Path: "/api/internal/return-order/pending-count", Handler: wrapInternal(ctx)(returnOrderPendingCountInternalHandler(ctx))},
|
||||
{Method: http.MethodGet, Path: "/api/internal/return-order/returned-summary", Handler: wrapInternal(ctx)(returnedSummaryInternalHandler(ctx))},
|
||||
{Method: http.MethodGet, Path: "/api/internal/return-order/active-package", Handler: wrapInternal(ctx)(activeReturnPackageCheckHandler(ctx))},
|
||||
// MES 质量检验(过程检/完工检)判不合格 → 联动 WMS 在库处置(按物料+SN/批次定位库存行)
|
||||
{Method: http.MethodPost, Path: "/api/internal/inventory/dispose", Handler: wrapInternal(ctx)(internalInventoryDisposeHandler(ctx))},
|
||||
// AGV 到位回写(MES AGV 回调调用):出库单 已出库 → 已送达(补齐状态机无写者的断头路)
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"bj_power_wms/ent"
|
||||
"bj_power_wms/ent/inventory"
|
||||
"bj_power_wms/ent/outbounddetail"
|
||||
"bj_power_wms/ent/outboundorder"
|
||||
"bj_power_wms/ent/inventorylock"
|
||||
"bj_power_wms/ent/material"
|
||||
"bj_power_wms/ent/predicate"
|
||||
@@ -1638,9 +1639,12 @@ func bindValuesCheckInternalHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
// signedOutSNsInternalHandler 查"已签收出库、尚未退回"的 SN 清单(2026-11-28 坑2 SN 清单推导)。
|
||||
// 入参:outboundNos=本工位已签收叫料单的出库单号(逗号分隔),materialCode=物料编码。
|
||||
// 口径:出库明细中的 SN,剔除当前已回"在库"的(=已退过/重新入库),剩下的即工位手上未退的实物 SN。
|
||||
// signedOutSNsInternalHandler 查"已送达出库、尚未退回"的 SN 清单(2026-11-28 坑2 SN 清单推导)。
|
||||
// 入参:outboundNos=本工位叫料单关联的全部出库单号(逗号分隔,不限签收状态),materialCode=物料编码。
|
||||
// 2026-12-01 口径放宽(用户拍板 B 方案):不再要求叫料单已签收——出库单状态=「已送达」(货物理到工位)
|
||||
// 即算可退来源,未签收/部分配送(叫10送5)的已送达部分均可退;「已出库」未送达的仍在途,不可退。
|
||||
// 口径:已送达出库明细中的 SN,剔除当前已回"在库"的(=已退过/重新入库),剩下的即工位手上未退的实物 SN。
|
||||
// 返回逐颗带来源出库单号({"sn","outboundNo"}),由 MES 按叫料单签收状态补「已签收/未签收」标注。
|
||||
// MES 拿到后再减掉已绑定到工件的 SN,得"可退 SN 清单"推给终端(工人不手选)。
|
||||
func signedOutSNsInternalHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -1660,16 +1664,37 @@ func signedOutSNsInternalHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
if len(outNos) == 0 {
|
||||
ok(w, map[string]any{"snList": []string{}})
|
||||
ok(w, map[string]any{"snList": []map[string]any{}})
|
||||
return
|
||||
}
|
||||
// 只认已送达的出库单(货到了工位才能退);重复/未送达的单直接排除
|
||||
delivered, derr := ctx.EntClient.OutboundOrder.Query().
|
||||
Where(outboundorder.OutboundNoIn(outNos...), outboundorder.StatusEQ("已送达")).All(ctx0())
|
||||
if derr != nil {
|
||||
fail(w, http.StatusInternalServerError, derr.Error())
|
||||
return
|
||||
}
|
||||
deliveredNos := make([]string, 0, len(delivered))
|
||||
deliveredSet := map[string]bool{}
|
||||
for _, o := range delivered {
|
||||
deliveredNos = append(deliveredNos, o.OutboundNo)
|
||||
deliveredSet[o.OutboundNo] = true
|
||||
}
|
||||
if len(deliveredNos) == 0 {
|
||||
ok(w, map[string]any{"snList": []map[string]any{}})
|
||||
return
|
||||
}
|
||||
rows, err := ctx.EntClient.OutboundDetail.Query().
|
||||
Where(outbounddetail.OutboundNoIn(outNos...), outbounddetail.MaterialCodeEQ(mc)).All(ctx0())
|
||||
Where(outbounddetail.OutboundNoIn(deliveredNos...), outbounddetail.MaterialCodeEQ(mc)).All(ctx0())
|
||||
if err != nil {
|
||||
fail(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
candidates := make([]string, 0, len(rows))
|
||||
type snItem struct {
|
||||
SN string `json:"sn"`
|
||||
OutboundNo string `json:"outboundNo"`
|
||||
}
|
||||
candidates := make([]snItem, 0, len(rows))
|
||||
candSet := map[string]bool{}
|
||||
for _, row := range rows {
|
||||
sn := strings.TrimSpace(row.SnCode)
|
||||
@@ -1677,25 +1702,31 @@ func signedOutSNsInternalHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
continue
|
||||
}
|
||||
candSet[sn] = true
|
||||
candidates = append(candidates, sn)
|
||||
candidates = append(candidates, snItem{SN: sn, OutboundNo: row.OutboundNo})
|
||||
}
|
||||
// 剔除已回到在库的 SN(已退过库或已重新入库,不再是工位手上的未退实物)
|
||||
returnable := make([]string, 0, len(candidates))
|
||||
returnable := make([]snItem, 0, len(candidates))
|
||||
if len(candidates) > 0 {
|
||||
back, berr := ctx.EntClient.Inventory.Query().
|
||||
Where(inventory.SnCodeIn(candidates...), inventory.StatusEQ("在库")).All(ctx0())
|
||||
Where(inventory.SnCodeIn(func() []string {
|
||||
ids := make([]string, 0, len(candidates))
|
||||
for _, c := range candidates {
|
||||
ids = append(ids, c.SN)
|
||||
}
|
||||
return ids
|
||||
}()...), inventory.StatusEQ("在库")).All(ctx0())
|
||||
if berr == nil {
|
||||
for _, b := range back {
|
||||
delete(candSet, strings.TrimSpace(b.SnCode))
|
||||
}
|
||||
}
|
||||
for _, sn := range candidates {
|
||||
if candSet[sn] {
|
||||
returnable = append(returnable, sn)
|
||||
for _, it := range candidates {
|
||||
if candSet[it.SN] {
|
||||
returnable = append(returnable, it)
|
||||
}
|
||||
}
|
||||
}
|
||||
sort.Strings(returnable)
|
||||
sort.Slice(returnable, func(i, j int) bool { return returnable[i].SN < returnable[j].SN })
|
||||
ok(w, map[string]any{"snList": returnable})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,12 +12,25 @@ import (
|
||||
// syncFailureListHandler GET /api/sync-failures 同步失败台账列表(人工可见,不自动重试)
|
||||
func syncFailureListHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
rows, err := syncfailure.List(ctx.SQLDB, r.URL.Query().Get("status"), r.URL.Query().Get("bizType"))
|
||||
query := r.URL.Query()
|
||||
status, bizType := query.Get("status"), query.Get("bizType")
|
||||
// 旧调用方不带分页参数时保持原结构=台账数组(上限 500 条);带 page/pageSize 走真分页。
|
||||
if !query.Has("page") && !query.Has("pageSize") {
|
||||
rows, err := syncfailure.List(ctx.SQLDB, status, bizType)
|
||||
if err != nil {
|
||||
fail(w, http.StatusInternalServerError, "查询同步失败台账失败(部署时请先手工执行迁移 SQL 建表): "+err.Error())
|
||||
return
|
||||
}
|
||||
ok(w, rows)
|
||||
return
|
||||
}
|
||||
page, pageSize := normalizePage(query.Get("page"), query.Get("pageSize"))
|
||||
rows, total, err := syncfailure.ListPage(ctx.SQLDB, status, bizType, page, pageSize)
|
||||
if err != nil {
|
||||
fail(w, http.StatusInternalServerError, "查询同步失败台账失败(部署时请先手工执行迁移 SQL 建表): "+err.Error())
|
||||
return
|
||||
}
|
||||
ok(w, rows)
|
||||
ok(w, map[string]any{"list": rows, "total": total, "page": page, "pageSize": pageSize})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -101,6 +101,61 @@ func List(db *sql.DB, status, bizType string) ([]Row, error) {
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// ListPage 台账真分页:按筛选条件先 Count 再按 id 倒序 LIMIT/OFFSET 取当页。
|
||||
// page 从 1 起,pageSize<=0 时由调用方归一;db 为空按空结果返回。
|
||||
func ListPage(db *sql.DB, status, bizType string, page, pageSize int) ([]Row, int, error) {
|
||||
if db == nil {
|
||||
return []Row{}, 0, nil
|
||||
}
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
if pageSize < 1 {
|
||||
pageSize = 20
|
||||
}
|
||||
where, args := listWhere(status, bizType)
|
||||
var total int
|
||||
if err := db.QueryRow("SELECT COUNT(*) FROM sync_failure WHERE 1=1"+where, args...).Scan(&total); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
q := `SELECT id, direction, biz_type, biz_key, endpoint, payload_json, error_msg,
|
||||
idempotency_key, status, retry_count, created_at, last_error_at, handled_by
|
||||
FROM sync_failure WHERE 1=1` + where +
|
||||
" ORDER BY id DESC LIMIT $" + itoa(len(args)+1) + " OFFSET $" + itoa(len(args)+2)
|
||||
pageArgs := append(append([]any{}, args...), pageSize, (page-1)*pageSize)
|
||||
rows, err := db.Query(q, pageArgs...)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
defer rows.Close()
|
||||
out := []Row{}
|
||||
for rows.Next() {
|
||||
var r Row
|
||||
if err := rows.Scan(&r.ID, &r.Direction, &r.BizType, &r.BizKey, &r.Endpoint,
|
||||
&r.PayloadJSON, &r.ErrorMsg, &r.IdempotencyKey, &r.Status, &r.RetryCount,
|
||||
&r.CreatedAt, &r.LastErrorAt, &r.HandledBy); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
out = append(out, r)
|
||||
}
|
||||
return out, total, rows.Err()
|
||||
}
|
||||
|
||||
// listWhere 台账列表/计数的共用筛选条件(status/bizType 均为精确匹配,空=不过滤)。
|
||||
func listWhere(status, bizType string) (string, []any) {
|
||||
where := ""
|
||||
args := []any{}
|
||||
if strings.TrimSpace(status) != "" {
|
||||
args = append(args, strings.TrimSpace(status))
|
||||
where += " AND status = $" + itoa(len(args))
|
||||
}
|
||||
if strings.TrimSpace(bizType) != "" {
|
||||
args = append(args, strings.TrimSpace(bizType))
|
||||
where += " AND biz_type = $" + itoa(len(args))
|
||||
}
|
||||
return where, args
|
||||
}
|
||||
|
||||
// Get 按 ID 取一条(重试前读取 endpoint/payload_json)。
|
||||
func Get(db *sql.DB, id int64) (Row, error) {
|
||||
var r Row
|
||||
|
||||
Reference in New Issue
Block a user