一、MES — 叫料单扩展(schema + 逻辑) material_request 新增字段:packageNo(叫料包号,一次提交=一包)、callDate(叫料日,默认当天)、planBreakdown(jsonb,工单×排产日明细 []CallPlanBreakdown);targetDock 语义降级为"期望接驳台(可空,配送时 WMS 确定实际台)"。 新增 internal/logic/call_plan.go:实时计算「叫料需求明细(工单×排产日×物料)」,派生两类提示(当日剩余未叫 / 跨日量明细),不落库、不拦提交;接驳台空台校验 EnsureStationDocksEmpty、托盘回库 DispatchPalletReturnManual、人工清台 ClearDockLedger、配送单派车 DispatchDeliveryAgv、工位物料明细 StationMaterialDetail。 新增 internal/handler/production/call_pkg.go:8 个 MES 内部端点(X-API-TOKEN 网关),供 WMS 调用——叫料需求明细、按包查单、接驳台状态、托盘回库、人工清台、配送单派车、叫料池原始行、工位物料明细、WMS 代叫料。 internal/handler/production/call.go、internal/logic/call.go、agv.go、qty_report.go、routes_production.go、wmsclient/client.go、db.go 同步接入上述逻辑。 二、WMS — 配送单(新功能,叫料与送料彻底拆开) 新增 schema/delivery_order.go + delivery_order_item.go 及 ent 生成代码:delivery_order(PENDING→DISPATCHED→DONE / CANCELLED,一单=一次托盘配送)、delivery_order_item(配送物料明细快照:工单/工艺/排产日)。 新增 internal/handler/delivery.go + 路由 + DeliveryOrders.vue 页面(仓储作业 → 配送单)。 mesclient/client.go 扩展:MES 叫料单结构补 packageNo/callDate/workOrderNo/planBreakdown,新增 PoolRow/DockPalletState/DeliveryAgvItem/getJSON 等。 CallQueue.vue:按叫料包展示,新增「代叫料」「建配送单」按钮与对话框;出库单回写所属配送单号(outbound_order 加字段);AGV 派送改由配送单驱动。 三、工位终端 — 叫料体验升级 MaterialPanel.vue:新增「本工位物料明细」卡(工艺物料全集×工位账四数,量为 0 也显示);叫料改为「叫料包」模式(默认当天、可勾选分批、可选其他排产日、期望接驳台可空),新增叫料日选择器、两条提示横幅(当日剩余未叫 / 跨日量明细)、托盘回库按钮。 InspectPanel.vue/Main.vue/OnlinePanel.vue/OfflinePanel.vue/WorkPanel.vue/SyncQueueDialog.vue/api/index.js/internal/handler/line.go/routes.go 同步接入接驳台状态、托盘回库、下班双台空台校验等。 web/static/index.html 构建刷新。 四、文档 案例.md:重写第 5/6/8/9 节与新「配送单」流程,补「模拟模式跳过接驳台校验」说明,库管员菜单加「配送单」;并修正"本单箱体"等措辞歧义。 客户问题.md:同步本轮拍板口径。 五、构建产物 / 同步 多个 MES 前端页面(.vue)随本次改动统一重新构建刷新;ent 生成代码(MES material_request、WMS delivery_order/outbound_order 相关)随 schema 更新重新生成。
825 lines
29 KiB
Go
825 lines
29 KiB
Go
// Package mesclient MES 内部接口客户端(备料单拉取 / 状态回写 / 接驳台与工单主数据只读)。
|
||
// AGV 配送真源已在 MES,2026-10-17 移除 WMS 侧 AGV 代理方法与 /api/agv/* 路由。
|
||
package mesclient
|
||
|
||
import (
|
||
"bytes"
|
||
"context"
|
||
"encoding/json"
|
||
"errors"
|
||
"fmt"
|
||
"io"
|
||
"net/http"
|
||
"net/url"
|
||
"strconv"
|
||
"strings"
|
||
"sync"
|
||
"time"
|
||
)
|
||
|
||
// Client MES 内部接口客户端
|
||
type Client struct {
|
||
mu sync.RWMutex
|
||
baseURL string
|
||
// initialBaseURL yaml 启动配置;页面把 MES 地址清空时回退到该值
|
||
initialBaseURL string
|
||
token string
|
||
hc *http.Client
|
||
}
|
||
|
||
func New(baseURL, token string) *Client {
|
||
if baseURL == "" {
|
||
baseURL = "http://127.0.0.1:8888"
|
||
}
|
||
return &Client{
|
||
baseURL: baseURL,
|
||
initialBaseURL: baseURL,
|
||
token: token,
|
||
hc: &http.Client{Timeout: 6 * time.Second},
|
||
}
|
||
}
|
||
|
||
// BaseURL 当前生效的 MES 地址(页面可热改,保存即生效)。
|
||
func (c *Client) BaseURL() string {
|
||
c.mu.RLock()
|
||
defer c.mu.RUnlock()
|
||
return c.baseURL
|
||
}
|
||
|
||
// SetBaseURL 热更新 MES 地址;传空串回退到 yaml 启动配置。
|
||
func (c *Client) SetBaseURL(u string) {
|
||
c.mu.Lock()
|
||
defer c.mu.Unlock()
|
||
u = strings.TrimRight(strings.TrimSpace(u), "/")
|
||
if u == "" {
|
||
u = c.initialBaseURL
|
||
}
|
||
c.baseURL = u
|
||
}
|
||
|
||
// Ping 连通性测试:调 MES 内部工单查询接口(轻量、无副作用),HTTP 200 即视为可达。
|
||
func (c *Client) Ping(ctx context.Context) error {
|
||
u := c.BaseURL() + "/api/internal/order/query?orderNo=__ping__"
|
||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u, nil)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
req.Header.Set("X-API-TOKEN", c.token)
|
||
cli := &http.Client{Timeout: 4 * time.Second}
|
||
resp, err := cli.Do(req)
|
||
if err != nil {
|
||
return fmt.Errorf("无法连接 MES(%s):%v", c.BaseURL(), err)
|
||
}
|
||
defer resp.Body.Close()
|
||
if resp.StatusCode != http.StatusOK {
|
||
return fmt.Errorf("MES 返回非 200(HTTP %d):请检查地址与 token", resp.StatusCode)
|
||
}
|
||
return nil
|
||
}
|
||
|
||
// MaterialRequest MES 叫料单行(B5:不绑工单/不绑排产;七态状态机)
|
||
type MaterialRequest struct {
|
||
ID int `json:"id"`
|
||
RequestNo string `json:"requestNo"`
|
||
MaterialCode string `json:"materialCode"`
|
||
MaterialName string `json:"materialName"`
|
||
Unit string `json:"unit"`
|
||
SnMode int `json:"snMode"`
|
||
ReqQty float64 `json:"reqQty"`
|
||
SentQty float64 `json:"sentQty"`
|
||
ReturnedQty float64 `json:"returnedQty"`
|
||
Status string `json:"status"`
|
||
TargetDock string `json:"targetDock"`
|
||
StationNo int `json:"stationNo"`
|
||
Source string `json:"source"`
|
||
RefReportNo string `json:"refReportNo"`
|
||
NeedAgv bool `json:"needAgv"`
|
||
CrossDay bool `json:"crossDay"`
|
||
OutboundNo string `json:"outboundNo"`
|
||
RejectReason string `json:"rejectReason"`
|
||
Operator string `json:"operator"`
|
||
CreatedAt string `json:"createdAt"` // RFC3339(MES ent 序列化),大屏叫料池显示 HH:MM 用
|
||
BorrowFrom map[string]int `json:"borrowFrom"` // 跨工位借料来源(MES 记账):stationNo→借用量;含本工位自取
|
||
// 2026-11-05 叫料包拍板:一次提交=一包;物料挂工单(工单×排产日明细)
|
||
PackageNo string `json:"packageNo"`
|
||
CallDate string `json:"callDate"`
|
||
WorkOrderNo string `json:"workOrderNo"`
|
||
PlanBreakdown []CallPlanBreakdown `json:"planBreakdown"`
|
||
}
|
||
|
||
// CallPlanBreakdown 叫料单的 工单×排产日 明细行(MES schema.CallPlanBreakdown 同构)
|
||
type CallPlanBreakdown struct {
|
||
OrderNo string `json:"orderNo"`
|
||
PlanDate string `json:"planDate"`
|
||
FlowID int `json:"flowId"`
|
||
FlowName string `json:"flowName"`
|
||
Qty float64 `json:"qty"`
|
||
}
|
||
|
||
// PoolRow 叫料池行(工位×物料 四数账;WMS 代叫料对话框数据源)
|
||
type PoolRow struct {
|
||
StationNo int `json:"stationNo"`
|
||
MaterialCode string `json:"materialCode"`
|
||
MaterialName string `json:"materialName"`
|
||
Unit string `json:"unit"`
|
||
SnMode int `json:"snMode"`
|
||
ReqQty int `json:"reqQty"`
|
||
AdjQty int `json:"adjQty"`
|
||
SentQty int `json:"sentQty"`
|
||
ReservedQty int `json:"reservedQty"`
|
||
AvailQty int `json:"availQty"`
|
||
CrossDay bool `json:"crossDay"`
|
||
PlanDates string `json:"planDates"`
|
||
}
|
||
|
||
// DockPalletState 接驳台托盘状态(实时传感器为准;plcOk=false 为台账回退值)
|
||
type DockPalletState struct {
|
||
DockCode string `json:"dockCode"`
|
||
Name string `json:"name"`
|
||
Side string `json:"side"`
|
||
HasPallet bool `json:"hasPallet"`
|
||
LedgerPallet bool `json:"ledgerPallet"`
|
||
PlcOk bool `json:"plcOk"`
|
||
}
|
||
|
||
// DeliveryAgvItem 配送单派车物料行(转发 MES /api/internal/delivery/dispatch)
|
||
type DeliveryAgvItem struct {
|
||
RequestNo string `json:"requestNo"`
|
||
MaterialCode string `json:"materialCode"`
|
||
MaterialName string `json:"materialName"`
|
||
Unit string `json:"unit"`
|
||
Qty float64 `json:"qty"`
|
||
}
|
||
|
||
// getJSON MES GET 通用(带 token;返回 body 的 data 字段)
|
||
func (c *Client) getJSON(ctx context.Context, path string, out any) error {
|
||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.baseURL+path, nil)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
req.Header.Set("X-API-TOKEN", c.token)
|
||
resp, err := c.hc.Do(req)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
defer resp.Body.Close()
|
||
raw, _ := io.ReadAll(resp.Body)
|
||
if resp.StatusCode >= 300 {
|
||
var br struct {
|
||
Message string `json:"message"`
|
||
}
|
||
_ = json.Unmarshal(raw, &br)
|
||
if br.Message != "" {
|
||
return errors.New(br.Message)
|
||
}
|
||
return fmt.Errorf("MES 返回 HTTP %d", resp.StatusCode)
|
||
}
|
||
var env struct {
|
||
Code int `json:"code"`
|
||
Data json.RawMessage `json:"data"`
|
||
}
|
||
if err := json.Unmarshal(raw, &env); err != nil {
|
||
return err
|
||
}
|
||
if out != nil && len(env.Data) > 0 {
|
||
return json.Unmarshal(env.Data, out)
|
||
}
|
||
return nil
|
||
}
|
||
|
||
// postJSON MES POST 通用(带 token;非 2xx 透出业务错误原文)
|
||
func (c *Client) postJSON(ctx context.Context, path string, body any) error {
|
||
b, _ := json.Marshal(body)
|
||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.baseURL+path, 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()
|
||
raw, _ := io.ReadAll(resp.Body)
|
||
if resp.StatusCode >= 300 {
|
||
var br struct {
|
||
Message string `json:"message"`
|
||
}
|
||
_ = json.Unmarshal(raw, &br)
|
||
if br.Message != "" {
|
||
return errors.New(br.Message)
|
||
}
|
||
return fmt.Errorf("MES 返回 HTTP %d", resp.StatusCode)
|
||
}
|
||
return nil
|
||
}
|
||
|
||
// ListMaterialRequestsByPackage 按叫料包号查叫料单(WMS 建配送单数据源,含工单×排产明细)
|
||
func (c *Client) ListMaterialRequestsByPackage(ctx context.Context, packageNo string) ([]MaterialRequest, error) {
|
||
out := []MaterialRequest{}
|
||
if err := c.getJSON(ctx, "/api/internal/call/package?packageNo="+url.QueryEscape(packageNo), &out); err != nil {
|
||
return nil, err
|
||
}
|
||
return out, nil
|
||
}
|
||
|
||
// StationPoolRaw 拉取叫料池原始行(WMS 代叫料对话框)
|
||
func (c *Client) StationPoolRaw(ctx context.Context, stationNo int) ([]PoolRow, error) {
|
||
out := []PoolRow{}
|
||
if err := c.getJSON(ctx, "/api/internal/call/pool-raw?stationNo="+strconv.Itoa(stationNo), &out); err != nil {
|
||
return nil, err
|
||
}
|
||
return out, nil
|
||
}
|
||
|
||
// StationDockPallets 按工位批量读左右接驳台实时传感器(配送空台校验)
|
||
func (c *Client) StationDockPallets(ctx context.Context, stationNo int) ([]DockPalletState, error) {
|
||
out := []DockPalletState{}
|
||
if err := c.getJSON(ctx, "/api/internal/dock/station-pallets?stationNo="+strconv.Itoa(stationNo), &out); err != nil {
|
||
return nil, err
|
||
}
|
||
return out, nil
|
||
}
|
||
|
||
// DispatchDelivery 配送单派车(转发 MES:AGV 派车改配送单驱动)
|
||
func (c *Client) DispatchDelivery(ctx context.Context, deliveryNo, targetDock, operator string, items []DeliveryAgvItem) error {
|
||
return c.postJSON(ctx, "/api/internal/delivery/dispatch", map[string]any{
|
||
"deliveryNo": deliveryNo, "targetDock": targetDock, "operator": operator, "items": items,
|
||
})
|
||
}
|
||
|
||
// CreateCall WMS 代叫料(与工位终端同一套 CreateCallOrders 逻辑)
|
||
func (c *Client) CreateCall(ctx context.Context, stationNo int, callDate string, force bool, items []map[string]any, operator string) error {
|
||
return c.postJSON(ctx, "/api/internal/call/create-internal", map[string]any{
|
||
"stationNo": stationNo, "callDate": callDate, "force": force, "items": items, "operator": operator, "source": "CALL",
|
||
})
|
||
}
|
||
|
||
// DockClear 人工清台(托盘被人工卸走后台账卡死兜底)
|
||
func (c *Client) DockClear(ctx context.Context, dockCode, operator, reason string) error {
|
||
return c.postJSON(ctx, "/api/internal/dock/clear", map[string]any{
|
||
"dockCode": dockCode, "operator": operator, "reason": reason,
|
||
})
|
||
}
|
||
|
||
// ListMaterialRequests 拉取备料单(含 target_dock)
|
||
// status 空=全部;传 PENDING 可只取待发料。
|
||
func (c *Client) ListMaterialRequests(ctx context.Context, status string) ([]MaterialRequest, error) {
|
||
u := c.baseURL + "/api/internal/material-requests"
|
||
q := ""
|
||
if status != "" {
|
||
q = "?status=" + status
|
||
}
|
||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u+q, nil)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
req.Header.Set("X-API-TOKEN", c.token)
|
||
resp, err := c.hc.Do(req)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
defer resp.Body.Close()
|
||
var env struct {
|
||
Code int `json:"code"`
|
||
Data []MaterialRequest `json:"data"`
|
||
Msg string `json:"message"`
|
||
}
|
||
dec := json.NewDecoder(resp.Body)
|
||
// MES 的 data 可能是 [object] 数组
|
||
if err := dec.Decode(&env); err != nil {
|
||
return nil, err
|
||
}
|
||
if env.Data == nil {
|
||
return []MaterialRequest{}, nil
|
||
}
|
||
return env.Data, nil
|
||
}
|
||
|
||
// MaterialDemandRow MES 排产物料需求行(按物料汇总,未来 N 天)
|
||
// WMS「缺料补货」视图拉此数据,对比本地可用库存得出排产缺料量。
|
||
type MaterialDemandRow struct {
|
||
MaterialCode string `json:"materialCode"`
|
||
MaterialName string `json:"materialName"`
|
||
Unit string `json:"unit"`
|
||
DemandQty int `json:"demandQty"` // 未来 N 天排产总需求(台数×单台用量,向上取整)
|
||
StartDate string `json:"startDate"` // 需求窗口起(YYYY-MM-DD)
|
||
EndDate string `json:"endDate"` // 需求窗口止(YYYY-MM-DD)
|
||
}
|
||
|
||
// FetchMaterialDemand 实时向 MES 拉取「排产物料需求量」(供 WMS 缺料补货视图)。
|
||
// 两系统之间不做定时/批量同步:WMS 打开页面时按需拉一次,MES 现算现返。
|
||
// days 不传默认 5(与历史未来5天口径一致);MES 内部 /api/internal/material-demand 支持 ?days 覆盖。
|
||
func (c *Client) FetchMaterialDemand(ctx context.Context, days int) ([]MaterialDemandRow, error) {
|
||
u := c.baseURL + "/api/internal/material-demand"
|
||
if days > 0 {
|
||
u += "?days=" + strconv.Itoa(days)
|
||
}
|
||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u, nil)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
req.Header.Set("X-API-TOKEN", c.token)
|
||
resp, err := c.hc.Do(req)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
defer resp.Body.Close()
|
||
var env struct {
|
||
Code int `json:"code"`
|
||
Data []MaterialDemandRow `json:"data"`
|
||
Msg string `json:"message"`
|
||
}
|
||
if err := json.NewDecoder(resp.Body).Decode(&env); err != nil {
|
||
return nil, err
|
||
}
|
||
if env.Code != 0 {
|
||
msg := env.Msg
|
||
if msg == "" {
|
||
msg = "MES 返回失败"
|
||
}
|
||
return nil, errors.New(msg)
|
||
}
|
||
if env.Data == nil {
|
||
return []MaterialDemandRow{}, nil
|
||
}
|
||
return env.Data, nil
|
||
}
|
||
|
||
// ---------- P1 备料清单(2026-11-04 用户拍板) ----------
|
||
|
||
// PreparationRow 备料清单行(MES 实时算返回):一行 = 某排产日 × 某工单 × 某工位 × 某物料。
|
||
type PreparationRow struct {
|
||
PlanDate string `json:"planDate"`
|
||
OrderNo string `json:"orderNo"`
|
||
ProductName string `json:"productName"`
|
||
StationNo int `json:"stationNo"`
|
||
StationQty int `json:"stationQty"` // 该工位分产量(台)
|
||
MaterialCode string `json:"materialCode"` // 物料编码(图号)
|
||
MaterialName string `json:"materialName"`
|
||
Spec string `json:"spec"`
|
||
Unit string `json:"unit"`
|
||
Qty int `json:"qty"` // 需求量 = ceil(分产量×单台用量)
|
||
UnitQty float64 `json:"unitQty"` // 单台用量
|
||
Status string `json:"status"` // 排产状态
|
||
}
|
||
|
||
// PreparationList 备料清单返回。
|
||
type PreparationList struct {
|
||
Rows []PreparationRow `json:"rows"`
|
||
Total int `json:"total"`
|
||
Range string `json:"range"`
|
||
PlanCnt int `json:"planCnt"`
|
||
}
|
||
|
||
// FetchPreparationList 实时向 MES 拉取备料清单(只读;MES 现算现返,工艺改动刷新即联动)。
|
||
func (c *Client) FetchPreparationList(ctx context.Context) (*PreparationList, error) {
|
||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.baseURL+"/api/internal/preparation-list", nil)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
req.Header.Set("X-API-TOKEN", c.token)
|
||
resp, err := c.hc.Do(req)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
defer resp.Body.Close()
|
||
var env struct {
|
||
Code int `json:"code"`
|
||
Data *PreparationList `json:"data"`
|
||
Msg string `json:"message"`
|
||
}
|
||
if err := json.NewDecoder(resp.Body).Decode(&env); err != nil {
|
||
return nil, err
|
||
}
|
||
if env.Code != 0 {
|
||
msg := env.Msg
|
||
if msg == "" {
|
||
msg = "MES 返回失败"
|
||
}
|
||
return nil, errors.New(msg)
|
||
}
|
||
if env.Data == nil {
|
||
return &PreparationList{Rows: []PreparationRow{}}, nil
|
||
}
|
||
return env.Data, nil
|
||
}
|
||
|
||
// ---------- E4 托盘余料明细(2026-11-04 拍板) ----------
|
||
|
||
// PalletResidualRow 接驳台托盘余料行(MES 从最近送达的 AGV 运输段 materials_json 解析)。
|
||
type PalletResidualRow struct {
|
||
RequestNo string `json:"requestNo"`
|
||
MaterialCode string `json:"materialCode"`
|
||
MaterialName string `json:"materialName"`
|
||
Unit string `json:"unit"`
|
||
Qty float64 `json:"qty"`
|
||
}
|
||
|
||
// FetchPalletResidual 拉取指定接驳台托盘余料明细(供 WMS 托盘工作台代退料弹窗自动带出)。
|
||
func (c *Client) FetchPalletResidual(ctx context.Context, dock string) ([]PalletResidualRow, error) {
|
||
u := c.baseURL + "/api/internal/pallet/residual?dock=" + url.QueryEscape(dock)
|
||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u, nil)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
req.Header.Set("X-API-TOKEN", c.token)
|
||
resp, err := c.hc.Do(req)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
defer resp.Body.Close()
|
||
var env struct {
|
||
Code int `json:"code"`
|
||
Data []PalletResidualRow `json:"data"`
|
||
Msg string `json:"message"`
|
||
}
|
||
if err := json.NewDecoder(resp.Body).Decode(&env); err != nil {
|
||
return nil, err
|
||
}
|
||
if env.Code != 0 {
|
||
msg := env.Msg
|
||
if msg == "" {
|
||
msg = "MES 返回失败"
|
||
}
|
||
return nil, errors.New(msg)
|
||
}
|
||
if env.Data == nil {
|
||
return []PalletResidualRow{}, nil
|
||
}
|
||
return env.Data, nil
|
||
}
|
||
|
||
// DockRow MES 接驳台主数据行(dock 真源已迁 MES,WMS 仅只读取用)。
|
||
type DockRow struct {
|
||
ID int64 `json:"id"`
|
||
DockCode string `json:"dockCode"`
|
||
Name string `json:"name"`
|
||
DockType string `json:"dockType"`
|
||
StationNo int `json:"stationNo"`
|
||
Side string `json:"side"`
|
||
Status string `json:"status"`
|
||
HasPallet bool `json:"hasPallet"`
|
||
PalletRef string `json:"palletRef"`
|
||
// Cleared 台面清空(R19 2026-11-03 拍板):= !HasPallet,转产退料确认收货闸门依据,
|
||
// 与 MES GuardTransferReroute(校验 Dock.HasPallet==false)同口径。
|
||
Cleared bool `json:"cleared"`
|
||
}
|
||
|
||
// ListDocks 拉取 MES 接驳台主数据(dock 真源在 MES)。
|
||
// WMS 出库目标工位下拉 / AGV 页接驳台列表均读此接口,仓库不再本地维护 dock 表。
|
||
func (c *Client) ListDocks(ctx context.Context) ([]DockRow, error) {
|
||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.baseURL+"/api/internal/docks", nil)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
req.Header.Set("X-API-TOKEN", c.token)
|
||
resp, err := c.hc.Do(req)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
defer resp.Body.Close()
|
||
var env struct {
|
||
Code int `json:"code"`
|
||
Data struct {
|
||
List []DockRow `json:"list"`
|
||
} `json:"data"`
|
||
Msg string `json:"message"`
|
||
}
|
||
if err := json.NewDecoder(resp.Body).Decode(&env); err != nil {
|
||
return nil, err
|
||
}
|
||
if env.Code != 0 {
|
||
msg := env.Msg
|
||
if msg == "" {
|
||
msg = "MES 返回失败"
|
||
}
|
||
return nil, errors.New(msg)
|
||
}
|
||
if env.Data.List == nil {
|
||
return []DockRow{}, nil
|
||
}
|
||
// Cleared 派生:无托盘即台面已清空(MES 侧不单独下发 cleared 字段)
|
||
for i := range env.Data.List {
|
||
env.Data.List[i].Cleared = !env.Data.List[i].HasPallet
|
||
}
|
||
return env.Data.List, nil
|
||
}
|
||
|
||
// AgvConfirm AGV 三段链确认(2026-11-03 三层机制):库管「确认装车并送货」等动作
|
||
// 经 MES internal /api/internal/agv/confirm;bizNo=叫料单号(MES 按 ARRIVED GO_LOAD 段定位)。
|
||
func (c *Client) AgvConfirm(ctx context.Context, bizNo, action, nextDock, operator string) error {
|
||
body, _ := json.Marshal(map[string]any{
|
||
"bizNo": bizNo, "action": action, "nextDock": nextDock, "operator": operator,
|
||
})
|
||
return c.postInternal(ctx, "/api/internal/agv/confirm", body, "AGV 确认动作失败")
|
||
}
|
||
|
||
// AgvDispatch 库管人工派车(2026-11-03 定论:废除备料完成自动派车):
|
||
// 叫料队列点「送货」→ MES internal /api/internal/agv/dispatch-call 下发空车去程段。
|
||
func (c *Client) AgvDispatch(ctx context.Context, requestNo, operator string) error {
|
||
body, _ := json.Marshal(map[string]any{"requestNo": requestNo, "operator": operator})
|
||
return c.postInternal(ctx, "/api/internal/agv/dispatch-call", body, "AGV 派车失败")
|
||
}
|
||
|
||
// PalletDispatch 托盘回收派车(WMS 托盘工作台):空车前往工位接驳台拉托盘(业务自动触发免弹窗)。
|
||
func (c *Client) PalletDispatch(ctx context.Context, dockCode, operator string) error {
|
||
body, _ := json.Marshal(map[string]any{"dockCode": dockCode, "operator": operator})
|
||
return c.postInternal(ctx, "/api/internal/agv/pallet-dispatch", body, "托盘回收派车失败")
|
||
}
|
||
|
||
// ---------- 退料 AGV 自动派车 / 取消联动 / 损耗落账 / 余量参考值(2026-11-04 口径定稿) ----------
|
||
|
||
// Jbt21Free 查询库房接驳台 JBT21 是否有空位(无空位不派车——托盘回收按钮禁用/自动派车等待)。
|
||
func (c *Client) Jbt21Free(ctx context.Context) (bool, string, error) {
|
||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.baseURL+"/api/internal/agv/jbt21-free", nil)
|
||
if err != nil {
|
||
return false, "", err
|
||
}
|
||
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 env struct {
|
||
Code int `json:"code"`
|
||
Data struct {
|
||
Free bool `json:"free"`
|
||
Reason string `json:"reason"`
|
||
} `json:"data"`
|
||
Msg string `json:"message"`
|
||
}
|
||
if err := json.NewDecoder(resp.Body).Decode(&env); err != nil {
|
||
return false, "", err
|
||
}
|
||
if env.Code != 0 {
|
||
return false, "", errors.New(env.Msg)
|
||
}
|
||
return env.Data.Free, env.Data.Reason, nil
|
||
}
|
||
|
||
// CancelAgvByDock 按接驳台取消未终态 PALLET 回收任务(退料单取消联动:作废未完成 AGV 任务)。
|
||
// 返回 hasInTransit=true 表示该接驳台存在已装车运输中的段(调用方应拒绝取消退料单)。
|
||
func (c *Client) CancelAgvByDock(ctx context.Context, dockCode, reason string) (map[string]any, error) {
|
||
body, _ := json.Marshal(map[string]any{"dockCode": dockCode, "reason": reason})
|
||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.baseURL+"/api/internal/agv/cancel-by-dock", bytes.NewReader(body))
|
||
if err != nil {
|
||
return nil, 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 nil, err
|
||
}
|
||
defer resp.Body.Close()
|
||
var env struct {
|
||
Code int `json:"code"`
|
||
Data map[string]any `json:"data"`
|
||
Msg string `json:"message"`
|
||
}
|
||
if err := json.NewDecoder(resp.Body).Decode(&env); err != nil {
|
||
return nil, err
|
||
}
|
||
if env.Code != 0 {
|
||
return nil, errors.New(env.Msg)
|
||
}
|
||
return env.Data, nil
|
||
}
|
||
|
||
// LossConfirm WMS 确认收货实收<应退时就地损耗落账(损耗确认移 WMS,MES 只读展示)。
|
||
func (c *Client) LossConfirm(ctx context.Context, returnNo, orderNo string, stationNo int,
|
||
materialCode, materialName string, theoryQty, actualQty, lossQty float64, reason, operator string) error {
|
||
body, _ := json.Marshal(map[string]any{
|
||
"returnNo": returnNo, "orderNo": orderNo, "stationNo": stationNo,
|
||
"materialCode": materialCode, "materialName": materialName,
|
||
"theoryQty": theoryQty, "actualQty": actualQty, "lossQty": lossQty,
|
||
"reason": reason, "operator": operator,
|
||
})
|
||
return c.postInternal(ctx, "/api/internal/loss/confirm", body, "损耗落账失败")
|
||
}
|
||
|
||
// ReturnRefRow 余量参考值行(MES 聚合返回,N4 定稿:应剩余量=实际备料量−已消耗−已退−已损耗)。
|
||
type ReturnRefRow struct {
|
||
OrderNo string `json:"orderNo"`
|
||
MaterialCode string `json:"materialCode"`
|
||
MaterialName string `json:"materialName"`
|
||
Unit string `json:"unit"`
|
||
TheoryQty float64 `json:"theoryQty"`
|
||
PreparedQty float64 `json:"preparedQty"` // 实际备料量
|
||
ConsumedQty float64 `json:"consumedQty"` // 已消耗
|
||
LossQty float64 `json:"lossQty"`
|
||
ReturnedQty float64 `json:"returnedQty"`
|
||
RefLeft float64 `json:"refLeft"`
|
||
}
|
||
|
||
// StationReturnGuardResult WMS 代退料前置校验结果(拍板2 三态):
|
||
// Allowed=true 放行;NeedForce=true = PLC 离线(前端弹风险提示,ForceConfirm 重提);
|
||
// Allowed=false & NeedForce=false = 硬拒(Reason 为拒绝文案)。
|
||
type StationReturnGuardResult struct {
|
||
Allowed bool `json:"allowed"`
|
||
NeedForce bool `json:"needForce"`
|
||
Reason string `json:"reason"`
|
||
}
|
||
|
||
// StationReturnGuard WMS 代退料前置校验(N5 2026-11-04 拍板 + 拍板2):工位传感器无托盘
|
||
// (工件已完工离开工位)才允许退料;PLC 离线返回 NeedForce=true 供人工强行确认。
|
||
func (c *Client) StationReturnGuard(ctx context.Context, stationNo int) (*StationReturnGuardResult, error) {
|
||
u := fmt.Sprintf("%s/api/internal/station/return-guard?stationNo=%d", c.baseURL, stationNo)
|
||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u, nil)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
req.Header.Set("X-API-TOKEN", c.token)
|
||
resp, err := c.hc.Do(req)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
defer resp.Body.Close()
|
||
var env struct {
|
||
Code int `json:"code"`
|
||
Data StationReturnGuardResult `json:"data"`
|
||
Msg string `json:"message"`
|
||
}
|
||
if err := json.NewDecoder(resp.Body).Decode(&env); err != nil {
|
||
return nil, err
|
||
}
|
||
if env.Code != 0 {
|
||
return nil, errors.New(env.Msg)
|
||
}
|
||
return &env.Data, nil
|
||
}
|
||
|
||
// FetchReturnReference 拉取某工位余量参考值列表(WMS 代退料展示参考值)。
|
||
func (c *Client) FetchReturnReference(ctx context.Context, stationNo int) ([]ReturnRefRow, error) {
|
||
u := fmt.Sprintf("%s/api/internal/return-reference?stationNo=%d", c.baseURL, stationNo)
|
||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u, nil)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
req.Header.Set("X-API-TOKEN", c.token)
|
||
resp, err := c.hc.Do(req)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
defer resp.Body.Close()
|
||
var env struct {
|
||
Code int `json:"code"`
|
||
Data struct {
|
||
Rows []ReturnRefRow `json:"rows"`
|
||
} `json:"data"`
|
||
Msg string `json:"message"`
|
||
}
|
||
if err := json.NewDecoder(resp.Body).Decode(&env); err != nil {
|
||
return nil, err
|
||
}
|
||
if env.Code != 0 {
|
||
return nil, errors.New(env.Msg)
|
||
}
|
||
return env.Data.Rows, nil
|
||
}
|
||
|
||
// WorkOrderContract MES 工单归属解析结果(A2 半成品入库继承产线工单)
|
||
type WorkOrderContract struct {
|
||
WorkOrderNo string `json:"workOrderNo"`
|
||
ContractNo string `json:"contractNo"`
|
||
ProjectNo string `json:"projectNo"`
|
||
}
|
||
|
||
// GetWorkOrder 调 MES 内部接口解析工单归属(合同号/工程编号)。
|
||
// 找不到(工单不存在或 MES 不可达)返回 (nil, nil),调用方静默降级、不阻断入库。
|
||
func (c *Client) GetWorkOrder(ctx context.Context, orderNo string) (*WorkOrderContract, error) {
|
||
if orderNo == "" {
|
||
return nil, nil
|
||
}
|
||
req, err := http.NewRequestWithContext(ctx, http.MethodGet,
|
||
c.baseURL+"/api/internal/work-orders/"+url.PathEscape(orderNo), nil)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
req.Header.Set("X-API-TOKEN", c.token)
|
||
resp, err := c.hc.Do(req)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
defer resp.Body.Close()
|
||
var env struct {
|
||
Code int `json:"code"`
|
||
Data WorkOrderContract `json:"data"`
|
||
Msg string `json:"message"`
|
||
}
|
||
if err := json.NewDecoder(resp.Body).Decode(&env); err != nil {
|
||
return nil, err
|
||
}
|
||
if env.Code != 0 {
|
||
return nil, nil
|
||
}
|
||
return &env.Data, nil
|
||
}
|
||
|
||
// MaterialUpsert WMS→MES 物料推送载荷(需求一 M4:WMS 落库后异步推送 MES 本地副本;按 code 幂等)。
|
||
// SnMode(2026-09-30 与 MES 侧契约一致):0未选择/1序列号=是/2序列号=否。
|
||
type MaterialUpsert struct {
|
||
Code string `json:"code"`
|
||
Name string `json:"name"`
|
||
Spec string `json:"spec"`
|
||
Unit string `json:"unit"`
|
||
Description string `json:"description"`
|
||
ItemType int `json:"itemType"`
|
||
ManageMode int `json:"manageMode"`
|
||
SnMode int `json:"snMode"`
|
||
TemplateCode string `json:"templateCode"`
|
||
SafetyStock int `json:"safetyStock"`
|
||
IsActive *bool `json:"isActive"`
|
||
}
|
||
|
||
// UpsertMaterial 推送物料到 MES 本地副本(POST /api/internal/materials,按 code 幂等 upsert)。
|
||
// MES 业务错误为 HTTP 200 + code≠0,此处解包判断。
|
||
func (c *Client) UpsertMaterial(ctx context.Context, m MaterialUpsert) error {
|
||
b, _ := json.Marshal(m)
|
||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.baseURL+"/api/internal/materials", 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 env struct {
|
||
Code int `json:"code"`
|
||
Msg string `json:"message"`
|
||
}
|
||
if err := json.NewDecoder(resp.Body).Decode(&env); err != nil {
|
||
return err
|
||
}
|
||
if env.Code != 0 {
|
||
return fmt.Errorf("MES 返回错误: %s", env.Msg)
|
||
}
|
||
return nil
|
||
}
|
||
|
||
// CallComplete 通知 MES 仓库已完成备料(B5):实发量 + WMS 出库单号。
|
||
// MES 侧只做状态迁移(PENDING→SHIPPED)与四数账记账,不反向调 WMS 出库。
|
||
func (c *Client) CallComplete(ctx context.Context, requestNo string, sentQty float64, outboundNo, operator string) error {
|
||
b, _ := json.Marshal(map[string]any{
|
||
"requestNo": requestNo, "sentQty": sentQty, "outboundNo": outboundNo, "operator": operator,
|
||
})
|
||
return c.postInternal(ctx, "/api/internal/call/complete", b, "MES 叫料单完成备料回写失败")
|
||
}
|
||
|
||
// CallReject 通知 MES 仓库驳回叫料单(B5):带理由(异常标记:非当日等)。
|
||
func (c *Client) CallReject(ctx context.Context, requestNo, reason, operator string) error {
|
||
b, _ := json.Marshal(map[string]any{"requestNo": requestNo, "reason": reason, "operator": operator})
|
||
return c.postInternal(ctx, "/api/internal/call/reject", b, "MES 叫料单驳回回写失败")
|
||
}
|
||
|
||
// CallReturnConfirm 通知 MES 仓库已确认收回退库(B5 退库闭环终点):MES 关单并回补叫料池额度。
|
||
// 2026-11-03 拍板(R18):带回实收量/收货差异/备注,MES 落「待核」差异,不自动进损耗台账。
|
||
func (c *Client) CallReturnConfirm(ctx context.Context, requestNo, operator string, receiveQty, receiveDiff int, receiveNote string) error {
|
||
b, _ := json.Marshal(map[string]any{"requestNo": requestNo, "operator": operator,
|
||
"receiveQty": receiveQty, "receiveDiff": receiveDiff, "receiveNote": receiveNote})
|
||
return c.postInternal(ctx, "/api/internal/call/return-confirm", b, "MES 叫料单退库确认回写失败")
|
||
}
|
||
|
||
// postInternal 通用内部 POST:带 X-API-TOKEN;解析 MES 业务错误(HTTP 200 + body{code≠0} 必须判读)
|
||
func (c *Client) postInternal(ctx context.Context, path string, body []byte, errPrefix string) error {
|
||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.baseURL+path, bytes.NewReader(body))
|
||
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()
|
||
if resp.StatusCode >= 300 {
|
||
return &RespError{Status: resp.StatusCode}
|
||
}
|
||
var env struct {
|
||
Code int `json:"code"`
|
||
Msg string `json:"msg"`
|
||
}
|
||
if err := json.NewDecoder(resp.Body).Decode(&env); err != nil {
|
||
return err
|
||
}
|
||
if env.Code != 0 {
|
||
return fmt.Errorf("%s: %s", errPrefix, env.Msg)
|
||
}
|
||
return nil
|
||
}
|
||
|
||
type RespError struct {
|
||
Status int
|
||
}
|
||
|
||
func (e *RespError) Error() string {
|
||
return "mes call failed with status " + fmt.Sprintf("%d", e.Status)
|
||
}
|