2026-09-04 14:18:53 +08:00
|
|
|
|
// Package mesclient MES 内部接口客户端(备料单拉取/状态回写,AGV 配送用)。
|
|
|
|
|
|
package mesclient
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"bytes"
|
|
|
|
|
|
"context"
|
|
|
|
|
|
"encoding/json"
|
2026-09-19 07:50:21 +08:00
|
|
|
|
"errors"
|
2026-09-04 14:18:53 +08:00
|
|
|
|
"fmt"
|
|
|
|
|
|
"net/http"
|
2026-09-29 14:55:03 +08:00
|
|
|
|
"net/url"
|
2026-09-21 14:06:05 +08:00
|
|
|
|
"strconv"
|
2026-09-04 14:18:53 +08:00
|
|
|
|
"time"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// Client MES 内部接口客户端
|
|
|
|
|
|
type Client struct {
|
|
|
|
|
|
baseURL 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,
|
|
|
|
|
|
token: token,
|
|
|
|
|
|
hc: &http.Client{Timeout: 6 * time.Second},
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// MaterialRequest MES 备料单行
|
|
|
|
|
|
type MaterialRequest struct {
|
2026-09-30 17:33:11 +08:00
|
|
|
|
ID int `json:"id"`
|
|
|
|
|
|
RequestNo string `json:"requestNo"`
|
|
|
|
|
|
OrderNo string `json:"orderNo"`
|
|
|
|
|
|
PlanDate string `json:"planDate"`
|
|
|
|
|
|
MaterialCode string `json:"materialCode"`
|
|
|
|
|
|
MaterialName string `json:"materialName"`
|
|
|
|
|
|
Unit string `json:"unit"`
|
|
|
|
|
|
ManageMode string `json:"manageMode"`
|
|
|
|
|
|
ReqQty float64 `json:"reqQty"`
|
|
|
|
|
|
Status string `json:"status"`
|
|
|
|
|
|
TargetDock string `json:"targetDock"`
|
|
|
|
|
|
Operator string `json:"operator"`
|
2026-09-04 14:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 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 {
|
2026-09-30 17:33:11 +08:00
|
|
|
|
Code int `json:"code"`
|
|
|
|
|
|
Data []MaterialRequest `json:"data"`
|
|
|
|
|
|
Msg string `json:"message"`
|
2026-09-04 14:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-09-21 14:06:05 +08:00
|
|
|
|
// MaterialDemandRow MES 排产物料需求行(按物料汇总,未来 N 天)
|
|
|
|
|
|
// WMS「缺料补货」视图拉此数据,对比本地可用库存得出排产缺料量。
|
|
|
|
|
|
type MaterialDemandRow struct {
|
2026-09-19 07:50:21 +08:00
|
|
|
|
MaterialCode string `json:"materialCode"`
|
2026-09-21 14:06:05 +08:00
|
|
|
|
MaterialName string `json:"materialName"`
|
|
|
|
|
|
Unit string `json:"unit"`
|
2026-09-30 17:33:11 +08:00
|
|
|
|
DemandQty int `json:"demandQty"` // 未来 N 天排产总需求(台数×单台用量,向上取整)
|
2026-09-21 14:06:05 +08:00
|
|
|
|
StartDate string `json:"startDate"` // 需求窗口起(YYYY-MM-DD)
|
|
|
|
|
|
EndDate string `json:"endDate"` // 需求窗口止(YYYY-MM-DD)
|
2026-09-19 07:50:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-09-21 14:06:05 +08:00
|
|
|
|
// FetchMaterialDemand 实时向 MES 拉取「排产物料需求量」(供 WMS 缺料补货视图)。
|
2026-09-19 07:50:21 +08:00
|
|
|
|
// 两系统之间不做定时/批量同步:WMS 打开页面时按需拉一次,MES 现算现返。
|
2026-09-21 14:06:05 +08:00
|
|
|
|
// 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)
|
|
|
|
|
|
}
|
2026-09-19 07:50:21 +08:00
|
|
|
|
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 {
|
2026-09-30 17:33:11 +08:00
|
|
|
|
Code int `json:"code"`
|
2026-09-21 14:06:05 +08:00
|
|
|
|
Data []MaterialDemandRow `json:"data"`
|
2026-09-30 17:33:11 +08:00
|
|
|
|
Msg string `json:"message"`
|
2026-09-19 07:50:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
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)
|
|
|
|
|
|
}
|
2026-09-21 14:06:05 +08:00
|
|
|
|
if env.Data == nil {
|
|
|
|
|
|
return []MaterialDemandRow{}, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
return env.Data, nil
|
2026-09-19 07:50:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-09-21 10:48:53 +08:00
|
|
|
|
// 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"`
|
|
|
|
|
|
Status string `json:"status"`
|
|
|
|
|
|
HasPallet bool `json:"hasPallet"`
|
|
|
|
|
|
PalletRef string `json:"palletRef"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
}
|
|
|
|
|
|
return env.Data.List, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-09-29 14:55:03 +08:00
|
|
|
|
// 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 幂等)。
|
2026-09-30 17:33:11 +08:00
|
|
|
|
// SnMode(2026-09-30 与 MES 侧契约一致):0未选择/1序列号件/2批次件。
|
2026-09-29 14:55:03 +08:00
|
|
|
|
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"`
|
2026-09-30 17:33:11 +08:00
|
|
|
|
SnMode int `json:"snMode"`
|
2026-09-29 14:55:03 +08:00
|
|
|
|
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
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-09-04 14:18:53 +08:00
|
|
|
|
// MarkStatus 回写备料单状态(DELIVERING / DONE)
|
|
|
|
|
|
func (c *Client) MarkStatus(ctx context.Context, requestNo, status string) error {
|
|
|
|
|
|
b, _ := json.Marshal(map[string]string{"requestNo": requestNo, "status": status})
|
|
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.baseURL+"/api/internal/material-request/status", 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()
|
|
|
|
|
|
if resp.StatusCode >= 300 {
|
|
|
|
|
|
return &RespError{Status: resp.StatusCode}
|
|
|
|
|
|
}
|
2026-10-05 20:15:38 +08:00
|
|
|
|
// MES 业务错误以 HTTP 200 + body{code≠0} 返回,必须解析 body 才能发现回写失败(避免静默吞错)
|
|
|
|
|
|
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("MES 回写备料单状态失败: %s", env.Msg)
|
|
|
|
|
|
}
|
2026-09-04 14:18:53 +08:00
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type RespError struct {
|
|
|
|
|
|
Status int
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (e *RespError) Error() string {
|
|
|
|
|
|
return "mes call failed with status " + fmt.Sprintf("%d", e.Status)
|
2026-09-30 17:33:11 +08:00
|
|
|
|
}
|