feat: 多系统新增扫码中间件与 AGV 对接,优化拧紧与退库流程

新增扫码中间件支持网络型扫码枪键盘注入,AGV 对接从 WMS 迁至 MES 统一管理;新增拧紧不合格人工处置与 PLC 对账机制,优化退库单关联与工位终端功能,同时清理旧 AGV 任务表,完善数据库与配置项。
This commit is contained in:
SunYF
2026-10-14 17:24:59 +08:00
parent 8e6048c64b
commit f65a52d9e1
170 changed files with 15415 additions and 10572 deletions
+179 -10
View File
@@ -31,19 +31,26 @@ func New(baseURL, token string) *Client {
}
}
// MaterialRequest MES 备料单行
// MaterialRequest MES 叫料单行(B5:不绑工单/不绑排产;七态状态机)
type MaterialRequest struct {
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"`
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"`
}
@@ -261,10 +268,30 @@ func (c *Client) UpsertMaterial(ctx context.Context, m MaterialUpsert) error {
return nil
}
// 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))
// 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 关单并回补叫料池额度。
func (c *Client) CallReturnConfirm(ctx context.Context, requestNo, operator string) error {
b, _ := json.Marshal(map[string]any{"requestNo": requestNo, "operator": operator})
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
}
@@ -278,7 +305,6 @@ func (c *Client) MarkStatus(ctx context.Context, requestNo, status string) error
if resp.StatusCode >= 300 {
return &RespError{Status: resp.StatusCode}
}
// MES 业务错误以 HTTP 200 + body{code≠0} 返回,必须解析 body 才能发现回写失败(避免静默吞错)
var env struct {
Code int `json:"code"`
Msg string `json:"msg"`
@@ -287,7 +313,7 @@ func (c *Client) MarkStatus(ctx context.Context, requestNo, status string) error
return err
}
if env.Code != 0 {
return fmt.Errorf("MES 回写备料单状态失败: %s", env.Msg)
return fmt.Errorf("%s: %s", errPrefix, env.Msg)
}
return nil
}
@@ -299,3 +325,146 @@ type RespError struct {
func (e *RespError) Error() string {
return "mes call failed with status " + fmt.Sprintf("%d", e.Status)
}
// ============ AGV 调度代理(2026-10-14 AGV 唯一真源迁 MES) ============
// AgvSubmitRow MES 下发 AGV 行(字段与 MES AgvSubmitRequest 对齐)
type AgvSubmitRow struct {
RequestNo string `json:"requestNo"`
MaterialCode string `json:"materialCode"`
MaterialName string `json:"materialName"`
Unit string `json:"unit"`
Qty float64 `json:"qty"`
TargetDock string `json:"targetDock"`
}
// postInternalJSON 通用内部 POST:带 X-API-TOKEN;解析 MES 业务错误(HTTP 200 + body{code≠0} 必须判读)。
// 与 postInternal 区别:正确读取 message 字段(MES 统一返回 {code,message,data})。
func (c *Client) postInternalJSON(ctx context.Context, path string, body []byte, out any) 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"`
Message string `json:"message"`
Data json.RawMessage `json:"data"`
}
if err := json.NewDecoder(resp.Body).Decode(&env); err != nil {
return err
}
if env.Code != 0 {
msg := env.Message
if msg == "" {
msg = "MES 返回失败"
}
return fmt.Errorf("%s", msg)
}
if out != nil && len(env.Data) > 0 {
return json.Unmarshal(env.Data, out)
}
return nil
}
// getInternalJSON 通用内部 GET:带 X-API-TOKEN;解析 MES 业务错误。
func (c *Client) getInternalJSON(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()
if resp.StatusCode >= 300 {
return &RespError{Status: resp.StatusCode}
}
var env struct {
Code int `json:"code"`
Message string `json:"message"`
Data json.RawMessage `json:"data"`
}
if err := json.NewDecoder(resp.Body).Decode(&env); err != nil {
return err
}
if env.Code != 0 {
msg := env.Message
if msg == "" {
msg = "MES 返回失败"
}
return fmt.Errorf("%s", msg)
}
if out != nil && len(env.Data) > 0 {
return json.Unmarshal(env.Data, out)
}
return nil
}
// SubmitAgv 仓管下发 AGV:代理 MES /api/internal/agv/submit(MES 为 AGV 唯一真源,调度海康 RCS)
func (c *Client) SubmitAgv(ctx context.Context, rows []AgvSubmitRow, sourceDock, operator string) (int, []string, error) {
b, _ := json.Marshal(map[string]any{"rows": rows, "sourceDock": sourceDock, "operator": operator})
var data struct {
Created int `json:"created"`
Failed []string `json:"failed"`
}
if err := c.postInternalJSON(ctx, "/api/internal/agv/submit", b, &data); err != nil {
return 0, nil, err
}
return data.Created, data.Failed, nil
}
// ListAgvTasks 查询 AGV 任务(代理 MES,分页契约透传 {list,total,page,pageSize})
func (c *Client) ListAgvTasks(ctx context.Context, status, targetDock string, page, size int) (map[string]any, error) {
q := url.Values{}
if status != "" {
q.Set("status", status)
}
if targetDock != "" {
q.Set("targetDock", targetDock)
}
q.Set("page", strconv.Itoa(page))
q.Set("pageSize", strconv.Itoa(size))
var data map[string]any
if err := c.getInternalJSON(ctx, "/api/internal/agv/tasks?"+q.Encode(), &data); err != nil {
return nil, err
}
return data, nil
}
// RefreshAgvTasks 轮询海康 RCS 刷新未终态任务(代理 MES)
func (c *Client) RefreshAgvTasks(ctx context.Context) (int, error) {
var data struct {
Changed int `json:"changed"`
}
if err := c.postInternalJSON(ctx, "/api/internal/agv/refresh", []byte("{}"), &data); err != nil {
return 0, err
}
return data.Changed, nil
}
// CancelAgvTask 取消 AGV 任务(代理 MES)
func (c *Client) CancelAgvTask(ctx context.Context, taskNo, reason, operator string) error {
b, _ := json.Marshal(map[string]any{"taskNo": taskNo, "reason": reason, "operator": operator})
return c.postInternalJSON(ctx, "/api/internal/agv/cancel", b, nil)
}
// AgvMode 返回 AGV 对接模式(代理 MES)
func (c *Client) AgvMode(ctx context.Context) (map[string]any, error) {
var data map[string]any
if err := c.getInternalJSON(ctx, "/api/internal/agv/mode", &data); err != nil {
return nil, err
}
return data, nil
}