feat: 多系统新增扫码中间件与 AGV 对接,优化拧紧与退库流程
新增扫码中间件支持网络型扫码枪键盘注入,AGV 对接从 WMS 迁至 MES 统一管理;新增拧紧不合格人工处置与 PLC 对账机制,优化退库单关联与工位终端功能,同时清理旧 AGV 任务表,完善数据库与配置项。
This commit is contained in:
@@ -5,7 +5,6 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
@@ -352,11 +351,13 @@ func (c *Client) ExportProductTypes(ctx context.Context, code, name, spec, isAct
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
// CreateReturnOrder 调 WMS /api/internal/return-order 预建退库单(工位退料回库房,待 WMS 确认收货)。
|
||||
func (c *Client) CreateReturnOrder(ctx context.Context, orderNo, sn, materialCode, materialName, spec, reason, operator string, stationNo, qty int, unit string) error {
|
||||
// CreateReturnOrder 调 WMS /api/internal/return-order 预建退库单(工位退料/叫料单退库,待 WMS 确认收货)。
|
||||
// refNo 非空 = 叫料单退库(B5):携带叫料单号,WMS 确认收回后凭此回调 MES /call/return-confirm。
|
||||
func (c *Client) CreateReturnOrder(ctx context.Context, orderNo, sn, materialCode, materialName, spec, reason, operator string, stationNo, qty int, unit, refNo string) error {
|
||||
return c.post(ctx, "/api/internal/return-order", map[string]any{
|
||||
"orderNo": orderNo, "sn": sn, "materialCode": materialCode, "materialName": materialName,
|
||||
"spec": spec, "qty": qty, "unit": unit, "reason": reason, "operator": operator, "stationNo": stationNo,
|
||||
"refNo": refNo,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -531,25 +532,6 @@ func (c *Client) MaterialManageModes(ctx context.Context, codes []string) (map[s
|
||||
return body.Data.ManageModes, nil
|
||||
}
|
||||
|
||||
// BackflushItem 反冲单条物料(已含损耗的单件消耗 * 完成件数)
|
||||
type BackflushItem struct {
|
||||
MaterialCode string `json:"materialCode"`
|
||||
Qty int `json:"qty"`
|
||||
}
|
||||
|
||||
// Backflush 调 WMS /api/internal/material/backflush 反冲扣账。
|
||||
// 物料在该道工序被消耗:报工完成时 MES 算好「工单产品BOM + 当前工序」消耗明细,
|
||||
// WMS 负责 FIFO 扣库存 + 生成 backflush 出库单。失败返回 err,由调用方降级(仅记日志不阻塞产线)。
|
||||
func (c *Client) Backflush(ctx context.Context, orderNo string, stationNo, processCode int, operator string, items []BackflushItem) error {
|
||||
return c.post(ctx, "/api/internal/material/backflush", map[string]any{
|
||||
"orderNo": orderNo,
|
||||
"stationNo": stationNo,
|
||||
"processCode": processCode,
|
||||
"operator": operator,
|
||||
"items": items,
|
||||
})
|
||||
}
|
||||
|
||||
// StockBatch WMS 可用批次(结构件)
|
||||
type StockBatch struct {
|
||||
BatchNo string `json:"batchNo"`
|
||||
@@ -591,166 +573,6 @@ func (c *Client) StockBatches(ctx context.Context, materialCode string) ([]Stock
|
||||
return body.Data.Batches, body.Data.SnList, nil
|
||||
}
|
||||
|
||||
// StockDeductOpts 备料出库可选参数(一个字段一个语义,避免位置参数越加越长)。
|
||||
type StockDeductOpts struct {
|
||||
BatchNo string // 批次出库(结构件),与 SnList 二选一
|
||||
SnList []string // SN 出库(电气件),与 BatchNo 二选一
|
||||
Qty int // 出库数量
|
||||
Operator string
|
||||
TargetDock string
|
||||
ZoneCode string
|
||||
OutboundType string // workorder(备料,默认) / refill(补料)
|
||||
NeedAgv bool // 是否叫 AGV 配送到接驳台
|
||||
OverReason string // 超出 BOM 需求时的超领原因(损耗/报废/返修/其他);空=超领将被 WMS 拦截
|
||||
// IdempotencyKey 幂等键(安全审计 C2):同一键重复提交 WMS 返回首次结果,避免网络超时重试导致重复扣减
|
||||
IdempotencyKey string
|
||||
}
|
||||
|
||||
// StockDeduct 调 WMS /api/internal/stock/deduct 执行一笔备料出库(批次或 SN 二选一)。
|
||||
// 返回该笔出库单号、工单台账状态(等于「领料完结」表示该物料已领完)、是否幂等复用(replayed)。
|
||||
// replayed=true 表示 WMS 已存在相同幂等键的出库单、本次未实际扣减(网络超时重试场景),
|
||||
// 调用方据此不要重复累加 sentQty,避免双计。失败返回 err(含 WMS 业务错误文案,
|
||||
// 由调用方降级为保持待备料并预警)。
|
||||
func (c *Client) StockDeduct(ctx context.Context, orderNo, materialCode string, o StockDeductOpts) (string, string, bool, error) {
|
||||
payload := map[string]any{
|
||||
"orderNo": orderNo, "materialCode": materialCode,
|
||||
"qty": o.Qty, "operator": o.Operator,
|
||||
"targetDock": o.TargetDock, "zoneCode": o.ZoneCode,
|
||||
}
|
||||
if o.BatchNo != "" {
|
||||
payload["batchNo"] = o.BatchNo
|
||||
}
|
||||
if len(o.SnList) > 0 {
|
||||
payload["snList"] = o.SnList
|
||||
}
|
||||
if o.OutboundType != "" {
|
||||
payload["outboundType"] = o.OutboundType
|
||||
}
|
||||
if o.NeedAgv {
|
||||
payload["needAgv"] = true
|
||||
}
|
||||
if o.OverReason != "" {
|
||||
payload["overReason"] = o.OverReason
|
||||
}
|
||||
if o.IdempotencyKey != "" {
|
||||
payload["idempotencyKey"] = o.IdempotencyKey
|
||||
}
|
||||
b, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
return "", "", false, err
|
||||
}
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.BaseURL()+"/api/internal/stock/deduct", bytes.NewReader(b))
|
||||
if err != nil {
|
||||
return "", "", false, err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("X-API-TOKEN", c.token)
|
||||
resp, err := c.hc.Do(req)
|
||||
if err != nil {
|
||||
return "", "", false, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
var body struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Data struct {
|
||||
OutboundNo string `json:"outboundNo"`
|
||||
Status string `json:"status"`
|
||||
Replayed bool `json:"idempotent"`
|
||||
} `json:"data"`
|
||||
}
|
||||
if err := json.NewDecoder(resp.Body).Decode(&body); err != nil {
|
||||
return "", "", false, err
|
||||
}
|
||||
if resp.StatusCode >= 300 || body.Code != 0 {
|
||||
return "", "", false, errors.New(body.Message)
|
||||
}
|
||||
return body.Data.OutboundNo, body.Data.Status, body.Data.Replayed, nil
|
||||
}
|
||||
|
||||
// LedgerProgressRow 工单台账配送进度行(WMS 侧四段量)。
|
||||
// 在途 / 线边由 MES 用本地已接料量派生(在途 = outQty − receivedQty;线边 = receivedQty − consumedQty),
|
||||
// WMS 不猜 MES 的接料量,避免跨系统重复记账。
|
||||
type LedgerProgressRow struct {
|
||||
OrderNo string `json:"orderNo"`
|
||||
MaterialCode string `json:"materialCode"`
|
||||
MaterialName string `json:"materialName"`
|
||||
TargetStation string `json:"targetStation"`
|
||||
TotalQty int `json:"totalQty"`
|
||||
OutQty int `json:"outQty"`
|
||||
ReturnedQty int `json:"returnedQty"`
|
||||
ConsumedQty int `json:"consumedQty"`
|
||||
NetOut int `json:"netOut"`
|
||||
Gap int `json:"gap"`
|
||||
Status string `json:"status"`
|
||||
CompletedAt int64 `json:"completedAt"`
|
||||
}
|
||||
|
||||
// LedgerProgress 拉取 WMS 工单物料台账配送进度(只读聚合,WMS 是台账唯一真源)。
|
||||
func (c *Client) LedgerProgress(ctx context.Context, orderNo, materialCode string, page, pageSize int) ([]LedgerProgressRow, int, error) {
|
||||
u := fmt.Sprintf("/api/internal/ledger/progress?orderNo=%s&materialCode=%s&page=%d&pageSize=%d",
|
||||
url.QueryEscape(orderNo), url.QueryEscape(materialCode), page, pageSize)
|
||||
var out struct {
|
||||
Total int `json:"total"`
|
||||
List []LedgerProgressRow `json:"list"`
|
||||
}
|
||||
if err := c.get(ctx, u, &out); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
return out.List, out.Total, nil
|
||||
}
|
||||
|
||||
// LedgerSyncItem 工单物料台账同步项(工单级需求量,一个物料一行)。
|
||||
type LedgerSyncItem struct {
|
||||
MaterialCode string `json:"materialCode"`
|
||||
MaterialName string `json:"materialName"`
|
||||
TotalQty int `json:"totalQty"`
|
||||
}
|
||||
|
||||
// LedgerSync 向 WMS 下发「该工单每种物料总共要多少料」(工单级需求量)。
|
||||
//
|
||||
// 需求10根因修复:MES 生成备料单后必须把工单级需求量同步给 WMS,
|
||||
// WMS 才知道 total_qty(台账需求量),否则出库/缺口/领料完结判定都无据可依。
|
||||
// WMS 侧按 orderNo+materialCode 幂等 upsert total_qty(详见 ledgerSyncHandler)。
|
||||
func (c *Client) LedgerSync(ctx context.Context, orderNo, productCode, operator string, items []LedgerSyncItem) error {
|
||||
if orderNo == "" || len(items) == 0 {
|
||||
return errors.New("orderNo 和 items 必填")
|
||||
}
|
||||
payload := map[string]any{
|
||||
"orderNo": orderNo,
|
||||
"productCode": productCode,
|
||||
"operator": operator,
|
||||
"items": items,
|
||||
}
|
||||
b, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.BaseURL()+"/api/internal/ledger/sync", bytes.NewReader(b))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("X-API-TOKEN", c.token)
|
||||
resp, err := c.hc.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
var body struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
_ = json.NewDecoder(resp.Body).Decode(&body)
|
||||
if resp.StatusCode >= 300 || body.Code != 0 {
|
||||
if body.Message != "" {
|
||||
return errors.New(body.Message)
|
||||
}
|
||||
return &RespError{Status: resp.StatusCode}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) post(ctx context.Context, path string, body any) error {
|
||||
b, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user