feat(mes/wms/终端): 工位终端状态感知 + 全厂统一账号 + 单号短号规则 + 退料SN逐颗核对闭环(2026-11-26~28 现场整改)

【MES · 工位终端状态感知(11-28 第十三节拍板)】
- 新增 station_terminal_state / station_terminal_event 两表:当前状态镜像 + 状态变化流水
- 在线判定走既有 SSE 长连接存活:建连/断连/30s ping 心跳,90s 无心跳判离线,零 HTTP 轮询
- 登录/退出/暂停/恢复由终端事件驱动上报;新增「终端状态」管理页(produce.terminalstate,仅超级管理员):看在线/登录/操作人/接单/下班判定/心跳,抽屉看状态流水
- 库管代退料前置条件②「工人已下班」据此判定:未登录或终端离线任一即下班

【工位终端 · 全厂统一账号(11-28 拍板)】
- 删除本地账号表与 seed,登录直连 MES 用户表;本地 SQLite 只剩操作日志+登录态,断网不能换人
- SSE 客户端携带本机固定工位号,MES 据连接存活映射该工位在线状态

【单号短号规则(11-27 拍板)】
- 叫料单 MR / 叫料包 PK / 转产退库 TR / 配送单 DL:改「前缀+YYMMDD+3位当天序号」,每天从 001 归零、超 999 进 4 位
- MR 与 TR 同列不同前缀独立计数不撞号;进程内互斥分配 + DB 唯一索引兜底;历史时间戳号一律不动

【退料闭环(11-28)】
- 退库单新增 sn_list:工位提交的待退 SN 清单落库,收货逐颗扫码核对(不按批次分组、允许跨批次混合,所属批次系统按 SN 带出,库管不必填)
- 收货改大数量必须有实物依据:SN 件以精确扫描的最终清单为准(条数相等且不重复);批次件必须落到真实启用的四级库位,禁止凭空加库存
- 余量参考值口径定稿(N4):应剩余量 = 实际备料 − 已消耗 − 已退 − 已损耗
- 损耗台账取消「后审核批注」:删 status/comment/commenter/commented_at 字段,WMS 确认收货当场落账闭环,MES 台账只读

【日排产 · 两种 PENDING 区分(11-26 口径定稿)】
- daily_plan 新增 discarded 废弃标记:停用打标、新建不打标;额度只算 CONFIRMED + 未废弃 PENDING
- 工单管「要干几台」、日排产管「按天从哪个工位领多少料」,两张账不合并不互倒推;排产合计可小于工单数(缺口靠借料/补料审批摆出,不自动兜底)

【其他】
- SN 查询大小写脱敏(11-27):存储原样不强制大小写,匹配 LOWER(sn)=LOWER(?),扫 ABC-0001 与 abc-0001 命中同一件
- 拧紧枪人工跳过链路:设备未连接时人工跳过上报,「拧紧跳过(设备未连接)」前缀单独列示,不写 torque_record 绝不冒充设备结果
- 单据流转提示规则表全量 35 条(MES 18 + WMS 17):常驻提示条、单号可复制;单号字段逐条对后端响应实测,取不到不显示复制按钮
- PLC 快照缓存/对账、产线清线、绩效与拧紧枪配置页同步修正

【文档】
- 新增《临时结束.md》2026-11-26 日排产/叫料/完工口径大讨论定稿(含状态专项结论与被推翻旧说法留档);问题临时.md 全量更新
This commit is contained in:
SunYF
2026-11-28 13:44:51 +08:00
parent 79a9d7846d
commit 88875c1f13
163 changed files with 11754 additions and 3021 deletions
+116 -47
View File
@@ -135,9 +135,9 @@ func (c *Client) LookupFinishedBySN(ctx context.Context, sn string) (*FinishedLo
return nil, fmt.Errorf("%w: HTTP %d(请检查 MES 地址与内部 token)", ErrMESUnreachable, resp.StatusCode)
}
var env struct {
Code int `json:"code"`
Code int `json:"code"`
Data FinishedLookup `json:"data"`
Msg string `json:"message"`
Msg string `json:"message"`
}
if err := json.NewDecoder(resp.Body).Decode(&env); err != nil {
return nil, fmt.Errorf("%w: 响应解析失败 %v", ErrMESUnreachable, err)
@@ -224,6 +224,38 @@ type DeliveryAgvItem struct {
// getJSON MES GET 通用(带 token;返回 body 的 data 字段)。
// 2026-11-09 缺陷修复:同 postJSON——HTTP 200 + 业务码≠0 也透出错误原文。
// TerminalState 工位终端当前状态(2026-11-28 坑3:库管代退料判下班的数据源,WMS→MES 内部通道)。
type TerminalState struct {
StationNo int `json:"stationNo"`
Online bool `json:"online"`
LoggedIn bool `json:"loggedIn"`
Paused bool `json:"paused"`
Operator string `json:"operator"`
OffDuty bool `json:"offDuty"` // 下班=未登录 或 离线
}
// GetTerminalState 查某工位终端状态;MES 不可达时返回 error,由调用方按"从严"决定处理。
func (c *Client) GetTerminalState(ctx context.Context, stationNo int) (*TerminalState, error) {
q := url.Values{"stationNo": {strconv.Itoa(stationNo)}}
var out TerminalState
if err := c.getJSON(ctx, "/api/internal/station/terminal-state?"+q.Encode(), &out); err != nil {
return nil, err
}
return &out, nil
}
// StationWipCount 查停在某工位的未完工工件数(坑3条件①:台账有在制工件则库管代退料拒绝)。
func (c *Client) StationWipCount(ctx context.Context, stationNo int) (int, error) {
q := url.Values{"stationNo": {strconv.Itoa(stationNo)}}
var out struct {
List []json.RawMessage `json:"list"`
}
if err := c.getJSON(ctx, "/api/internal/workpieces/in-process?"+q.Encode(), &out); err != nil {
return 0, err
}
return len(out.List), nil
}
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 {
@@ -327,9 +359,11 @@ func (c *Client) StationDockPallets(ctx context.Context, stationNo int) ([]DockP
}
// DispatchDelivery 配送单派车(转发 MES:AGV 派车改配送单驱动)
func (c *Client) DispatchDelivery(ctx context.Context, deliveryNo, targetDock, operator string, items []DeliveryAgvItem) error {
// confirmForcePick="1":人工已确认强提风险点位(2026-11-27 补全链路,WMS 派车弹窗勾选后传)。
func (c *Client) DispatchDelivery(ctx context.Context, deliveryNo, targetDock, operator string, items []DeliveryAgvItem, confirmForcePick string) error {
return c.postJSON(ctx, "/api/internal/delivery/dispatch", map[string]any{
"deliveryNo": deliveryNo, "targetDock": targetDock, "operator": operator, "items": items,
"confirmForcePick": confirmForcePick,
})
}
@@ -370,6 +404,67 @@ func (c *Client) ListMaterialRequestsByStatuses(ctx context.Context, statuses st
return c.listMaterialRequests(ctx, "", statuses)
}
// MaterialRequestPage MES 数据库分页结果(paged=1)。
type MaterialRequestPage struct {
Total int `json:"total"`
List []MaterialRequest `json:"list"`
Page int `json:"page"`
PageSize int `json:"pageSize"`
}
// ListMaterialRequestsPage 叫料队列分页拉取:过滤条件与排序都在 MES 数据库层完成,
// WMS 不再把全部历史拉到代理层再切片(2026-11-28 裁决)。
func (c *Client) ListMaterialRequestsPage(ctx context.Context, status, materialCode, stationNo, requestNo string, page, pageSize int) (*MaterialRequestPage, error) {
values := url.Values{}
values.Set("paged", "1")
if status != "" {
values.Set("status", status)
}
if materialCode != "" {
values.Set("materialCode", materialCode)
}
if stationNo != "" {
values.Set("stationNo", stationNo)
}
if requestNo != "" {
values.Set("requestNo", requestNo)
}
if page > 0 {
values.Set("page", strconv.Itoa(page))
}
if pageSize > 0 {
values.Set("pageSize", strconv.Itoa(pageSize))
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.baseURL+"/api/internal/material-requests?"+values.Encode(), 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 *MaterialRequestPage `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, fmt.Errorf("MES 叫料分页查询失败:%s", env.Msg)
}
if env.Data == nil {
return &MaterialRequestPage{List: []MaterialRequest{}}, nil
}
if env.Data.List == nil {
env.Data.List = []MaterialRequest{}
}
return env.Data, nil
}
func (c *Client) listMaterialRequests(ctx context.Context, status, statuses string) ([]MaterialRequest, error) {
u := c.baseURL + "/api/internal/material-requests"
q := ""
@@ -560,17 +655,18 @@ func (c *Client) FetchPalletResidual(ctx context.Context, dock string) ([]Pallet
// 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"`
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"`
SensorHasPallet bool `json:"sensorHasPallet"` // 实时传感器有无托盘(2026-11-18 派车前提示依据)
SensorOk bool `json:"sensorOk"` // 传感器是否可读(false=PLC 离线,回退看台账)
ForcePick bool `json:"forcePick"` // 强提风险标记(2026-11-27 派车弹窗据此提示人工确认)
// Cleared 台面清空(R19 2026-11-03 拍板):= !HasPallet,转产退料确认收货闸门依据,
// 与 MES GuardTransferReroute(校验 Dock.HasPallet==false)同口径。
Cleared bool `json:"cleared"`
@@ -708,43 +804,6 @@ type ReturnRefRow struct {
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)
@@ -759,7 +818,7 @@ func (c *Client) FetchReturnReference(ctx context.Context, stationNo int) ([]Ret
}
defer resp.Body.Close()
var env struct {
Code int `json:"code"`
Code int `json:"code"`
Data struct {
Rows []ReturnRefRow `json:"rows"`
} `json:"data"`
@@ -953,6 +1012,16 @@ func (c *Client) CallReturnConfirm(ctx context.Context, requestNo, operator stri
return c.postInternal(ctx, "/api/internal/call/return-confirm", b, "MES 叫料单退库确认回写失败")
}
// ReturnAdjust 通知 MES 退库单已修正后的最终实收数量。
// 2026-11-28 拍板:传目标值不传差额;MES 按 returnNo+目标值幂等,自行算出与上次已应用值的差额。
func (c *Client) ReturnAdjust(ctx context.Context, payload map[string]any) error {
b, err := json.Marshal(payload)
if err != nil {
return err
}
return c.postInternal(ctx, "/api/internal/call/return-adjust", b, "MES 退库数量修正回写失败")
}
// PostRaw 同步失败台账人工重试:按 endpoint 原样重放通知请求体。
// 只重放通知,绝不重跑任何本地业务(如 doGeneralOutbound 出库扣减)。
func (c *Client) PostRaw(ctx context.Context, path string, payload map[string]any) error {