2026-10-14 17:24:59 +08:00
|
|
|
|
package handler
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"bytes"
|
2026-11-26 17:18:58 +08:00
|
|
|
|
"context"
|
2026-10-14 17:24:59 +08:00
|
|
|
|
"encoding/json"
|
|
|
|
|
|
"errors"
|
|
|
|
|
|
"net/http"
|
|
|
|
|
|
"net/http/httptest"
|
|
|
|
|
|
"strconv"
|
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
|
|
"bj_power_wms/ent"
|
|
|
|
|
|
"bj_power_wms/ent/inventory"
|
2026-11-09 18:58:47 +08:00
|
|
|
|
"bj_power_wms/ent/outboundorder"
|
2026-10-14 17:24:59 +08:00
|
|
|
|
"bj_power_wms/internal/mesclient"
|
|
|
|
|
|
"bj_power_wms/internal/svc"
|
2026-11-09 18:58:47 +08:00
|
|
|
|
"bj_power_wms/internal/syncfailure"
|
2026-10-14 17:24:59 +08:00
|
|
|
|
|
|
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// ============ B5 叫料队列(仓库端,2026-10-13 定稿) ============
|
|
|
|
|
|
// 入口:WMS「叫料队列」页(JWT + call:view/call:manage)。
|
|
|
|
|
|
// 完成备料 = WMS 本地 FIFO 选库存 → 通用出库(类别「叫料配送」)→ 通知 MES /call/complete;
|
|
|
|
|
|
// MES 不反向调 WMS 出库(主控裁定:库存动作归 WMS,减少环、降低失败面)。
|
|
|
|
|
|
|
2026-11-26 17:18:58 +08:00
|
|
|
|
// callPendingHandler GET /api/call/pending 叫料队列(代理 MES 内部列表)。
|
|
|
|
|
|
// 2026-11-26 用户拍板:一屏显示待备料(PENDING)+已备料(PREPARED),无页签切换;
|
|
|
|
|
|
// 一次请求带 statuses=PENDING,PREPARED 由 MES StatusIn 过滤,废单(DONE/CANCELLED/RETURNING)不进队列。
|
2026-10-14 17:24:59 +08:00
|
|
|
|
// 支持 stationNo / materialCode / requestNo 过滤(不区分大小写由 MES 侧处理)。
|
|
|
|
|
|
func callPendingHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
if ctx.Mes == nil {
|
|
|
|
|
|
fail(w, http.StatusInternalServerError, "MES 未配置,无法拉取叫料队列")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2026-11-26 17:18:58 +08:00
|
|
|
|
list, err := ctx.Mes.ListMaterialRequestsByStatuses(r.Context(), "PENDING,PREPARED")
|
2026-10-14 17:24:59 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
fail(w, http.StatusBadGateway, "拉取 MES 叫料队列失败: "+err.Error())
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
stationNo := strings.TrimSpace(r.URL.Query().Get("stationNo"))
|
|
|
|
|
|
materialCode := strings.TrimSpace(r.URL.Query().Get("materialCode"))
|
|
|
|
|
|
requestNo := strings.TrimSpace(r.URL.Query().Get("requestNo"))
|
|
|
|
|
|
out := make([]mesclient.MaterialRequest, 0, len(list))
|
|
|
|
|
|
for _, m := range list {
|
|
|
|
|
|
if stationNo != "" && strconv.Itoa(m.StationNo) != stationNo {
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
if materialCode != "" && !strings.Contains(strings.ToLower(m.MaterialCode), strings.ToLower(materialCode)) {
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
if requestNo != "" && !strings.Contains(strings.ToLower(m.RequestNo), strings.ToLower(requestNo)) {
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
out = append(out, m)
|
|
|
|
|
|
}
|
|
|
|
|
|
ok(w, map[string]any{"total": len(out), "list": out})
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// callCompleteHandler POST /api/call/complete 仓库完成备料(点「完成」一键:选库存→出库→通知 MES→派车)。
|
|
|
|
|
|
// body: {requestNo}。实发量由 WMS 库存实况确定(缺多少发多少,差额 MES 自动释放)。
|
2026-11-26 17:18:58 +08:00
|
|
|
|
// 2026-11-26:核心逻辑抽到 completeOneCall,单条与批量共用同一份,行为与状态码保持不变。
|
2026-10-14 17:24:59 +08:00
|
|
|
|
func callCompleteHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
if ctx.Mes == nil {
|
|
|
|
|
|
fail(w, http.StatusInternalServerError, "MES 未配置,无法完成备料")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
var req struct {
|
|
|
|
|
|
RequestNo string `json:"requestNo"`
|
|
|
|
|
|
}
|
|
|
|
|
|
if err := parseJSON(r, &req); err != nil || strings.TrimSpace(req.RequestNo) == "" {
|
|
|
|
|
|
fail(w, http.StatusBadRequest, "缺少叫料单号")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
operator := r.Header.Get("X-Username")
|
|
|
|
|
|
if operator == "" {
|
|
|
|
|
|
operator = "warehouse"
|
|
|
|
|
|
}
|
|
|
|
|
|
list, err := ctx.Mes.ListMaterialRequests(r.Context(), "PENDING")
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
fail(w, http.StatusBadGateway, "拉取 MES 叫料单失败: "+err.Error())
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
var call *mesclient.MaterialRequest
|
|
|
|
|
|
for i := range list {
|
|
|
|
|
|
if list[i].RequestNo == req.RequestNo {
|
|
|
|
|
|
call = &list[i]
|
|
|
|
|
|
break
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if call == nil {
|
|
|
|
|
|
fail(w, http.StatusNotFound, "叫料单不存在或已不是待备料状态(可能已被其他仓库处理)")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2026-11-26 17:18:58 +08:00
|
|
|
|
result, cerr := completeOneCall(ctx, r.Context(), call, operator)
|
|
|
|
|
|
if cerr != nil {
|
|
|
|
|
|
fail(w, cerr.status, cerr.message)
|
2026-10-14 17:24:59 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
2026-11-26 17:18:58 +08:00
|
|
|
|
ok(w, result)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-10-14 17:24:59 +08:00
|
|
|
|
|
2026-11-26 17:18:58 +08:00
|
|
|
|
// callBizError 带原 HTTP 状态码的业务错误(抽函数后状态码逐字对齐,前端提示不降级)。
|
|
|
|
|
|
type callBizError struct {
|
|
|
|
|
|
status int
|
|
|
|
|
|
message string
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (e *callBizError) Error() string { return e.message }
|
|
|
|
|
|
|
|
|
|
|
|
// callPick 单条 FIFO 选料结果(批次或 SN)。
|
|
|
|
|
|
type callPick struct {
|
|
|
|
|
|
batchNo string
|
|
|
|
|
|
sn string
|
|
|
|
|
|
take int
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// pickCallInventory 只读 FIFO 选料,口径与旧 callCompleteHandler 步骤2完全一致:
|
|
|
|
|
|
// 在库 + 合格 + 未锁定,按 batch_no/created_at 排序;SN 逐件、批次按量。
|
|
|
|
|
|
// 批量预检与单条执行共用,避免两份逻辑漂移;不产生任何写操作。
|
|
|
|
|
|
func pickCallInventory(ctx *svc.ServiceContext, call *mesclient.MaterialRequest, need int) ([]callPick, int, error) {
|
|
|
|
|
|
rows, err := ctx.EntClient.Inventory.Query().
|
|
|
|
|
|
Where(
|
|
|
|
|
|
inventory.MaterialCodeEQ(call.MaterialCode),
|
|
|
|
|
|
inventory.QuantityGT(0),
|
|
|
|
|
|
inventory.QualityStatusIn(QStatusPass),
|
|
|
|
|
|
inventory.StatusEQ("在库"),
|
|
|
|
|
|
).
|
|
|
|
|
|
Order(ent.Asc("batch_no"), ent.Asc("created_at")).
|
|
|
|
|
|
All(ctx0())
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, 0, err
|
|
|
|
|
|
}
|
|
|
|
|
|
picks := make([]callPick, 0, 4)
|
|
|
|
|
|
remain := need
|
|
|
|
|
|
totalAvail := 0
|
|
|
|
|
|
for _, inv := range rows {
|
|
|
|
|
|
if remain <= 0 {
|
|
|
|
|
|
break
|
|
|
|
|
|
}
|
|
|
|
|
|
avail := inv.Quantity - inv.LockedQty
|
|
|
|
|
|
if avail <= 0 {
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
take := avail
|
|
|
|
|
|
if take > remain {
|
|
|
|
|
|
take = remain
|
|
|
|
|
|
}
|
|
|
|
|
|
if call.SnMode == 1 {
|
|
|
|
|
|
if inv.SnCode == "" {
|
2026-11-02 19:17:05 +08:00
|
|
|
|
continue
|
|
|
|
|
|
}
|
2026-11-26 17:18:58 +08:00
|
|
|
|
picks = append(picks, callPick{sn: inv.SnCode, take: 1})
|
|
|
|
|
|
totalAvail++
|
|
|
|
|
|
remain--
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
if inv.SnMode == snModeBatch {
|
|
|
|
|
|
picks = append(picks, callPick{batchNo: inv.BatchNo, take: take})
|
|
|
|
|
|
totalAvail += avail
|
|
|
|
|
|
remain -= take
|
2026-11-02 19:17:05 +08:00
|
|
|
|
}
|
2026-11-26 17:18:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
return picks, totalAvail, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// completeOneCall 完成一单备料:幂等重发 → FIFO 选料 → 逐批通用出库 → 通知 MES。
|
|
|
|
|
|
// call 由调用方从 MES PENDING 列表查出(批量一次拉、逐单传入,不重复请求);
|
|
|
|
|
|
// logCtx 用请求 context(保留 ctx0() 的出库/日志语义,见 doGeneralOutbound)。
|
|
|
|
|
|
func completeOneCall(ctx *svc.ServiceContext, logCtx context.Context, call *mesclient.MaterialRequest, operator string) (map[string]any, *callBizError) {
|
|
|
|
|
|
requestNo := call.RequestNo
|
2026-11-02 19:17:05 +08:00
|
|
|
|
|
2026-11-26 17:18:58 +08:00
|
|
|
|
// 幂等守卫(2026-11-08 Bug2):WMS 已出过库只重发通知,绝不重跑 doGeneralOutbound。
|
|
|
|
|
|
prior, perr := ctx.EntClient.OutboundOrder.Query().
|
|
|
|
|
|
Where(outboundorder.RemarkEQ("叫料单 "+strings.TrimSpace(requestNo)),
|
|
|
|
|
|
outboundorder.VoidedEQ(false)).All(logCtx)
|
|
|
|
|
|
if perr == nil && len(prior) > 0 {
|
|
|
|
|
|
priorSent := 0
|
|
|
|
|
|
priorNos := make([]string, 0, len(prior))
|
|
|
|
|
|
for _, ob := range prior {
|
|
|
|
|
|
priorSent += ob.Quantity
|
|
|
|
|
|
priorNos = append(priorNos, ob.OutboundNo)
|
|
|
|
|
|
}
|
|
|
|
|
|
priorJoined := strings.Join(priorNos, ",")
|
|
|
|
|
|
notify := map[string]any{
|
|
|
|
|
|
"requestNo": requestNo, "sentQty": priorSent,
|
|
|
|
|
|
"outboundNo": priorJoined, "operator": operator,
|
|
|
|
|
|
}
|
|
|
|
|
|
if cerr := ctx.Mes.CallComplete(logCtx, requestNo, float64(priorSent), priorJoined, operator); cerr != nil {
|
|
|
|
|
|
syncfailure.Record(ctx.SQLDB, "WMS→MES", "call.complete", requestNo,
|
|
|
|
|
|
"/api/internal/call/complete", notify, cerr.Error())
|
|
|
|
|
|
return nil, &callBizError{http.StatusBadGateway, "已出库,通知MES失败,请在同步失败台账重试通知: " + cerr.Error()}
|
|
|
|
|
|
}
|
|
|
|
|
|
return map[string]any{
|
|
|
|
|
|
"requestNo": requestNo, "sentQty": priorSent, "reqQty": priorSent,
|
|
|
|
|
|
"outboundNos": priorJoined, "short": 0, "agvNote": "(已出库,仅重发通知)",
|
|
|
|
|
|
}, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 2026-11-09 用户拍板 A:完成备料层允许 TargetDock 为空(实际接驳台建配送单时选,只能空台)。
|
|
|
|
|
|
need := int(call.ReqQty + 0.5)
|
|
|
|
|
|
if need <= 0 {
|
|
|
|
|
|
return nil, &callBizError{http.StatusBadRequest, "叫料单请求量异常"}
|
|
|
|
|
|
}
|
|
|
|
|
|
borrowForWMS := map[string]int{}
|
|
|
|
|
|
for k, v := range call.BorrowFrom {
|
|
|
|
|
|
if k == strconv.Itoa(call.StationNo) {
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
if v > 0 {
|
|
|
|
|
|
borrowForWMS[k] = v
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
picks, totalAvail, err := pickCallInventory(ctx, call, need)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, &callBizError{http.StatusInternalServerError, "查询库存失败: " + err.Error()}
|
|
|
|
|
|
}
|
|
|
|
|
|
if len(picks) == 0 {
|
|
|
|
|
|
return nil, &callBizError{http.StatusConflict, "缺料无法完成备料:物料「" + call.MaterialName + "(" + call.MaterialCode + ")」工位 " + strconv.Itoa(call.StationNo) + " 请求量 " + itoa(need) + ",合格可用量 " + itoa(totalAvail) + ",请先补入库或走补料审批"}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
outboundNos := make([]string, 0, len(picks))
|
|
|
|
|
|
sentTotal := 0
|
|
|
|
|
|
for _, p := range picks {
|
|
|
|
|
|
payload := generalOutboundRequest{
|
|
|
|
|
|
MaterialCode: call.MaterialCode,
|
|
|
|
|
|
Qty: p.take,
|
|
|
|
|
|
Operator: operator,
|
|
|
|
|
|
OutboundCategory: "call",
|
|
|
|
|
|
Remark: "叫料单 " + call.RequestNo,
|
|
|
|
|
|
TargetDock: call.TargetDock,
|
|
|
|
|
|
TargetStation: strconv.Itoa(call.StationNo),
|
|
|
|
|
|
NeedAgv: call.NeedAgv,
|
|
|
|
|
|
BorrowFrom: borrowForWMS,
|
|
|
|
|
|
}
|
|
|
|
|
|
if p.sn != "" {
|
|
|
|
|
|
payload.SnList = []string{p.sn}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
payload.BatchNo = p.batchNo
|
|
|
|
|
|
}
|
|
|
|
|
|
obNo, obQty, gerr := doGeneralOutbound(ctx, payload)
|
|
|
|
|
|
if gerr != nil {
|
|
|
|
|
|
return nil, &callBizError{http.StatusInternalServerError, "出库失败(物料 " + call.MaterialCode + "):" + gerr.Error()}
|
|
|
|
|
|
}
|
|
|
|
|
|
outboundNos = append(outboundNos, obNo)
|
|
|
|
|
|
sentTotal += obQty
|
|
|
|
|
|
}
|
|
|
|
|
|
if sentTotal <= 0 {
|
|
|
|
|
|
return nil, &callBizError{http.StatusInternalServerError, "出库结果异常:实发量为 0"}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
joined := strings.Join(outboundNos, ",")
|
|
|
|
|
|
if cerr := ctx.Mes.CallComplete(logCtx, call.RequestNo, float64(sentTotal), joined, operator); cerr != nil {
|
|
|
|
|
|
logx.Errorf("call complete notify MES failed: requestNo=%s err=%v", call.RequestNo, cerr)
|
|
|
|
|
|
syncfailure.Record(ctx.SQLDB, "WMS→MES", "call.complete", call.RequestNo,
|
|
|
|
|
|
"/api/internal/call/complete", map[string]any{
|
|
|
|
|
|
"requestNo": call.RequestNo, "sentQty": sentTotal,
|
|
|
|
|
|
"outboundNo": joined, "operator": operator,
|
|
|
|
|
|
}, cerr.Error())
|
|
|
|
|
|
return nil, &callBizError{http.StatusBadGateway, "已出库(单号 " + joined + "),通知MES失败,请在同步失败台账重试通知: " + cerr.Error()}
|
|
|
|
|
|
}
|
|
|
|
|
|
return map[string]any{
|
|
|
|
|
|
"requestNo": call.RequestNo, "sentQty": sentTotal, "reqQty": need,
|
|
|
|
|
|
"outboundNos": joined, "short": need - sentTotal, "agvNote": "(AGV 由 MES 自动派车)",
|
|
|
|
|
|
}, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// callBatchCompleteHandler POST /api/call/complete-batch 批量完成备料(2026-11-26 拍板)。
|
|
|
|
|
|
// body: {requestNos:[...]}。只接受同一非空 packageNo、同一工位的 PENDING 单;空包号老单禁批量。
|
|
|
|
|
|
// 架构口径(用户终审):不改底层出库函数、不做跨库全事务——
|
|
|
|
|
|
// 1) 一次拉 MES PENDING 列表,逐单核对,避免逐单重复请求与状态漂移;
|
|
|
|
|
|
// 2) 先逐单幂等守卫 + 整包预检(按物料汇总需求对 FIFO 可用量,防同批内互相抢库存);
|
|
|
|
|
|
// 3) 预检通过才逐单调 completeOneCall(每单仍是自己独立出库事务 + 独立 MES 通知,与单条点按钮完全同构);
|
|
|
|
|
|
// 4) 某单执行失败不回滚已成功的单,逐单结果返回供重试;跨请求并发维持既有先到先得口径。
|
|
|
|
|
|
func callBatchCompleteHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
if ctx.Mes == nil {
|
|
|
|
|
|
fail(w, http.StatusInternalServerError, "MES 未配置,无法批量完成备料")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
var req struct {
|
|
|
|
|
|
RequestNos []string `json:"requestNos"`
|
|
|
|
|
|
}
|
|
|
|
|
|
if err := parseJSON(r, &req); err != nil || len(req.RequestNos) == 0 {
|
|
|
|
|
|
fail(w, http.StatusBadRequest, "请至少选择一条叫料单")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
operator := r.Header.Get("X-Username")
|
|
|
|
|
|
if operator == "" {
|
|
|
|
|
|
operator = "warehouse"
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
list, err := ctx.Mes.ListMaterialRequests(r.Context(), "PENDING")
|
2026-10-14 17:24:59 +08:00
|
|
|
|
if err != nil {
|
2026-11-26 17:18:58 +08:00
|
|
|
|
fail(w, http.StatusBadGateway, "拉取 MES 叫料单失败: "+err.Error())
|
2026-10-14 17:24:59 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
2026-11-26 17:18:58 +08:00
|
|
|
|
byNo := make(map[string]*mesclient.MaterialRequest, len(list))
|
|
|
|
|
|
for i := range list {
|
|
|
|
|
|
byNo[list[i].RequestNo] = &list[i]
|
2026-10-14 17:24:59 +08:00
|
|
|
|
}
|
2026-11-26 17:18:58 +08:00
|
|
|
|
|
|
|
|
|
|
seen := make(map[string]bool, len(req.RequestNos))
|
|
|
|
|
|
calls := make([]*mesclient.MaterialRequest, 0, len(req.RequestNos))
|
|
|
|
|
|
pkg, station := "", 0
|
|
|
|
|
|
for _, raw := range req.RequestNos {
|
|
|
|
|
|
no := strings.TrimSpace(raw)
|
|
|
|
|
|
if no == "" || seen[no] {
|
2026-10-14 17:24:59 +08:00
|
|
|
|
continue
|
|
|
|
|
|
}
|
2026-11-26 17:18:58 +08:00
|
|
|
|
seen[no] = true
|
|
|
|
|
|
c := byNo[no]
|
|
|
|
|
|
if c == nil {
|
|
|
|
|
|
fail(w, http.StatusConflict, "叫料单「"+no+"」不存在或已不是待备料状态,请刷新后重试")
|
|
|
|
|
|
return
|
2026-10-14 17:24:59 +08:00
|
|
|
|
}
|
2026-11-26 17:18:58 +08:00
|
|
|
|
if c.PackageNo == "" {
|
|
|
|
|
|
fail(w, http.StatusBadRequest, "叫料单「"+no+"」无包号(老单),请单条完成备料")
|
|
|
|
|
|
return
|
2026-10-14 17:24:59 +08:00
|
|
|
|
}
|
2026-11-26 17:18:58 +08:00
|
|
|
|
if pkg == "" {
|
|
|
|
|
|
pkg, station = c.PackageNo, c.StationNo
|
|
|
|
|
|
} else if c.PackageNo != pkg || c.StationNo != station {
|
|
|
|
|
|
fail(w, http.StatusBadRequest, "只能批量处理同一叫料包、同一工位的单据,请把不同包分开操作")
|
|
|
|
|
|
return
|
2026-10-14 17:24:59 +08:00
|
|
|
|
}
|
2026-11-26 17:18:58 +08:00
|
|
|
|
calls = append(calls, c)
|
2026-10-14 17:24:59 +08:00
|
|
|
|
}
|
2026-11-26 17:18:58 +08:00
|
|
|
|
if len(calls) == 0 {
|
|
|
|
|
|
fail(w, http.StatusBadRequest, "没有可处理的叫料单")
|
2026-10-14 17:24:59 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-26 17:18:58 +08:00
|
|
|
|
// 幂等守卫:任一单在 WMS 已有未作废出库单 → 整批拒绝(提示该单号),防连点重复出库。
|
|
|
|
|
|
for _, c := range calls {
|
|
|
|
|
|
prior, perr := ctx.EntClient.OutboundOrder.Query().
|
|
|
|
|
|
Where(outboundorder.RemarkEQ("叫料单 "+c.RequestNo), outboundorder.VoidedEQ(false)).All(r.Context())
|
|
|
|
|
|
if perr == nil && len(prior) > 0 {
|
|
|
|
|
|
fail(w, http.StatusConflict, "叫料单「"+c.RequestNo+"」已出过库,请勿重复提交;如需重发通知请在同步失败台账处理")
|
|
|
|
|
|
return
|
2026-10-14 17:24:59 +08:00
|
|
|
|
}
|
2026-11-26 17:18:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 整包预检:按物料汇总需求,与该物料的 FIFO 合格可用量比较。
|
|
|
|
|
|
// 同包多条常是同一物料,若逐单各自预检会「各自看着都够、实际互相抢光」;汇总口径从根上挡住。
|
|
|
|
|
|
demand := make(map[string]int, len(calls))
|
|
|
|
|
|
firstOf := make(map[string]*mesclient.MaterialRequest, len(calls))
|
|
|
|
|
|
for _, c := range calls {
|
|
|
|
|
|
need := int(c.ReqQty + 0.5)
|
|
|
|
|
|
if need <= 0 {
|
|
|
|
|
|
fail(w, http.StatusBadRequest, "叫料单「"+c.RequestNo+"」请求量异常")
|
2026-10-14 17:24:59 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
2026-11-26 17:18:58 +08:00
|
|
|
|
demand[c.MaterialCode] += need
|
|
|
|
|
|
if _, ok := firstOf[c.MaterialCode]; !ok {
|
|
|
|
|
|
firstOf[c.MaterialCode] = c
|
|
|
|
|
|
}
|
2026-10-14 17:24:59 +08:00
|
|
|
|
}
|
2026-11-26 17:18:58 +08:00
|
|
|
|
for code, needSum := range demand {
|
|
|
|
|
|
_, avail, perr := pickCallInventory(ctx, firstOf[code], needSum)
|
|
|
|
|
|
if perr != nil {
|
|
|
|
|
|
fail(w, http.StatusInternalServerError, "预检库存失败(物料 "+code+"):"+perr.Error())
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
if avail < needSum {
|
|
|
|
|
|
fail(w, http.StatusConflict, "整包预检不通过:物料「"+firstOf[code].MaterialName+"("+code+")」同包合计需要 "+itoa(needSum)+",合格可用量仅 "+itoa(avail)+",未扣任何库存,请补入库后再批量备料")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2026-10-14 17:24:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-26 17:18:58 +08:00
|
|
|
|
// 逐单执行:复用单条逻辑,每单独立出库事务 + 独立 MES 通知;失败单保留 PENDING 可重试。
|
|
|
|
|
|
type oneResult struct {
|
|
|
|
|
|
RequestNo string `json:"requestNo"`
|
|
|
|
|
|
OK bool `json:"ok"`
|
|
|
|
|
|
Result map[string]any `json:"result,omitempty"`
|
|
|
|
|
|
Error string `json:"error,omitempty"`
|
|
|
|
|
|
}
|
|
|
|
|
|
results := make([]oneResult, 0, len(calls))
|
|
|
|
|
|
succeeded := 0
|
|
|
|
|
|
for _, c := range calls {
|
|
|
|
|
|
res, cerr := completeOneCall(ctx, r.Context(), c, operator)
|
|
|
|
|
|
if cerr != nil {
|
|
|
|
|
|
results = append(results, oneResult{RequestNo: c.RequestNo, OK: false, Error: cerr.message})
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
succeeded++
|
|
|
|
|
|
results = append(results, oneResult{RequestNo: c.RequestNo, OK: true, Result: res})
|
2026-10-14 17:24:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
ok(w, map[string]any{
|
2026-11-26 17:18:58 +08:00
|
|
|
|
"total": len(calls), "succeeded": succeeded, "failed": len(calls) - succeeded,
|
|
|
|
|
|
"results": results,
|
2026-10-14 17:24:59 +08:00
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// callRejectHandler POST /api/call/reject 仓库驳回叫料单(仅待备料;理由必填,异常标记:非当日等)。
|
|
|
|
|
|
func callRejectHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
if ctx.Mes == nil {
|
|
|
|
|
|
fail(w, http.StatusInternalServerError, "MES 未配置")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
var req struct {
|
|
|
|
|
|
RequestNo string `json:"requestNo"`
|
|
|
|
|
|
Reason string `json:"reason"`
|
|
|
|
|
|
}
|
|
|
|
|
|
if err := parseJSON(r, &req); err != nil || strings.TrimSpace(req.RequestNo) == "" {
|
|
|
|
|
|
fail(w, http.StatusBadRequest, "缺少叫料单号")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
if strings.TrimSpace(req.Reason) == "" {
|
|
|
|
|
|
fail(w, http.StatusBadRequest, "驳回必须填写理由")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
operator := r.Header.Get("X-Username")
|
|
|
|
|
|
if operator == "" {
|
|
|
|
|
|
operator = "warehouse"
|
|
|
|
|
|
}
|
|
|
|
|
|
if err := ctx.Mes.CallReject(r.Context(), req.RequestNo, strings.TrimSpace(req.Reason), operator); err != nil {
|
|
|
|
|
|
fail(w, http.StatusBadGateway, "驳回失败: "+err.Error())
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
ok(w, map[string]any{"requestNo": req.RequestNo})
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// doGeneralOutbound 进程内复用通用出库全链路(事务/扣减/单号/冻结守卫零重复实现)。
|
|
|
|
|
|
// 返回出库单号与实发量;HTTP 层错误以 error 返回(message 透出)。
|
|
|
|
|
|
func doGeneralOutbound(ctx *svc.ServiceContext, req generalOutboundRequest) (string, int, error) {
|
|
|
|
|
|
body, merr := json.Marshal(req)
|
|
|
|
|
|
if merr != nil {
|
|
|
|
|
|
return "", 0, merr
|
|
|
|
|
|
}
|
|
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
|
|
httpReq := httptest.NewRequest(http.MethodPost, "/api/outbound/general", bytes.NewReader(body))
|
|
|
|
|
|
httpReq.Header.Set("Content-Type", "application/json")
|
2026-10-31 15:41:27 +08:00
|
|
|
|
makeOutboundHandler(ctx, "general")(rec, httpReq)
|
2026-10-14 17:24:59 +08:00
|
|
|
|
var env struct {
|
|
|
|
|
|
Code int `json:"code"`
|
|
|
|
|
|
Message string `json:"message"`
|
|
|
|
|
|
Data struct {
|
|
|
|
|
|
OutboundNo string `json:"outboundNo"`
|
|
|
|
|
|
Qty int `json:"qty"`
|
|
|
|
|
|
} `json:"data"`
|
|
|
|
|
|
}
|
|
|
|
|
|
if derr := json.Unmarshal(rec.Body.Bytes(), &env); derr != nil {
|
|
|
|
|
|
return "", 0, derr
|
|
|
|
|
|
}
|
|
|
|
|
|
if env.Code != 0 {
|
|
|
|
|
|
return "", 0, errors.New(env.Message)
|
|
|
|
|
|
}
|
|
|
|
|
|
return env.Data.OutboundNo, env.Data.Qty, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// dispatchAgv 已移除(2026-10-14 AGV 唯一真源迁 MES):WMS 完成备料后由 MES 在 /api/internal/call/complete
|
|
|
|
|
|
// 内部统一触发海康 RCS 派车,WMS 不再本地持有 RCS 客户端,避免双真源/双派车。
|
|
|
|
|
|
|