Files
bj_power/bj_power_mes/internal/logic/line_clearance.go
T
SunYF 88875c1f13 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 全量更新
2026-11-28 13:44:51 +08:00

182 lines
8.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package logic
import (
"context"
"fmt"
"strings"
"time"
"bj_power_mes/ent/agvtask"
"bj_power_mes/ent/callpool"
"bj_power_mes/ent/linepoint"
"bj_power_mes/ent/plcsendlog"
"bj_power_mes/ent/workpiece"
)
// ============ 清线确认单(换绑前产线状态检查,2026-11-18 定稿) ============
// 改工位绑定 / 快捷绑定工艺写库前,检查产线五项台账:
// 在制工件 / 余料 / 托盘占位 / PLC 未完成任务 / AGV 在途。
// 五项全部来自系统台账记录(托盘占位 = line_point 点位台账,不是 PLC 传感器实时读数),
// 与现场实物可能脱节:台账说有、现场没有(或只是空托盘)都可能出现,
// 所以每项带「现场已处理」人工确认,确认放行写 event_log 留痕。
// 原「混产模板 / 可生产清单」机制已整体删除(2026-11-18 用户拍板)。
// LineClearanceItem 清线检查单项。
type LineClearanceItem struct {
Key string `json:"key"`
Label string `json:"label"`
Count int `json:"count"`
Cleared bool `json:"cleared"`
How string `json:"how"`
Route string `json:"route"`
// HardBlock=true 表示该项不能靠「现场已处理」勾选放行——必须先现场清掉实物。
// 2026-11-27 新增:PLC 传感器实时有件属硬拒绝(保存时 SaveStation 同样硬拦),
// 若做成可勾选放行,用户勾了以为能过、点保存还是被拦,反而更困惑。
HardBlock bool `json:"hardBlock"`
}
// LineClearanceResult 清线检查结果(passed=true 表示五项全清或已逐项确认放行)。
type LineClearanceResult struct {
Items []LineClearanceItem `json:"items"`
NeedClearance bool `json:"needClearance"`
Passed bool `json:"passed"`
}
// CheckLineClearance 只读检查五项台账(不改任何数据)。
func (s *Service) CheckLineClearance(ctx context.Context, confirmed []string, operator string) (*LineClearanceResult, error) {
res := &LineClearanceResult{Items: []LineClearanceItem{}}
confirmedSet := map[string]bool{}
for _, k := range confirmed {
confirmedSet[k] = true
}
type probe struct {
key string
label string
how string // 怎么解决(人话,点名道姓到页面和动作)
route string // 前端路由,点「去处理」直达
count func() (int, error)
}
probes := []probe{
{"workpiece", "工位上还有在制工件",
"到「在制品追踪」页把这些工件完工或暂存退库,台面清空后再回来点确认", "/wip",
func() (int, error) {
return s.ctx.EntClient.Workpiece.Query().Where(workpiece.AtStationNoGT(0)).Count(ctx)
}},
{"residual", "工位还有未用完的料",
"到工位终端做「退料」把余料退回库房,或在「生产退料」页确认/取消待处理退料单", "/transfer-return",
func() (int, error) {
return s.ctx.EntClient.CallPool.Query().
Where(callpool.Or(callpool.SentQtyGT(0), callpool.ReservedQtyGT(0))).Count(ctx)
}},
{"pallet", "产线点位上还压着带工件的托盘",
"用「人工派车 → 托盘回库」把托盘(连同工件)拉走,或把工件完工/退库;空托盘(上面没工件)不算占位、不用处理", "/agv-manual",
func() (int, error) {
return s.ctx.EntClient.LinePoint.Query().
Where(linepoint.CurrentSnNEQ("")).Count(ctx)
}},
{"plc", "PLC 还有没执行完的指令",
"到「工位下发」页核对这些指令是否已到位;确实清完再回来勾确认。PLC 离线时可现场目视确认后勾选放行(会留痕)", "/plc-send",
func() (int, error) {
return s.ctx.EntClient.PlcSendLog.Query().
Where(plcsendlog.StatusIn("SENT", "PENDING")).Count(ctx)
}},
{"agv", "还有 AGV 任务没结束",
"到「AGV管理」页把这些任务结束或取消;海康后台遗留的非本系统任务请到海康 RCS 后台处理", "/agv",
func() (int, error) {
return s.ctx.EntClient.AgvTask.Query().
Where(agvtask.StatusIn("PENDING", "1", "2", "ARRIVED")).Count(ctx)
}},
}
unresolved := []string{}
for _, p := range probes {
n, e := p.count()
if e != nil {
// fail-close:查不清就当没清完,绝不放行
return nil, fmt.Errorf("清线检查「%s」失败,禁止继续:%v", p.label, e)
}
item := LineClearanceItem{Key: p.key, Label: p.label, Count: n, Cleared: confirmedSet[p.key], How: p.how, Route: p.route}
res.Items = append(res.Items, item)
if n > 0 && !item.Cleared {
unresolved = append(unresolved, fmt.Sprintf("%s(%d 项)", p.label, n))
}
}
// 第六项(2026-11-27 用户拍板补进应用前校验):PLC 传感器实时读数——工位上有没有实物。
// 前五项「托盘占位」读的是 line_point 台账、不是传感器实时值,导致「工位上放着工件」在
// 应用前校验里看不出来、点保存才被 PLC 双检拦。这里提前读一次快照(与监控页/预警共用 2s 缓存),
// 把「传感器说有件」的工位一次性列在预检里,不能等到保存才冒出来。
// PLC 离线/读失败:不 fail-close(离线由第五项 plc 指令 + 保存时的 3021 现场确认兜底),
// 只在文案里注明「读不到实时状态」,避免把通讯故障当成「无件」误导。
scanLive := func() (int, bool) {
bits, e := plcHardCut(ctx, 2500*time.Millisecond, func(c context.Context) ([]bool, error) {
return s.ctx.PLC.ReadBitsCached(c)
})
if e != nil || len(bits) < 12 {
return 0, false
}
n := 0
for k := 0; k < 12; k++ {
if bits[k] {
n++
}
}
return n, true
}
liveCount, liveOK := scanLive()
if !liveOK {
// 通讯异常:作为一项「无法核对」列出,允许现场目视确认后勾「现场已处理」放行
// (与保存时 PLC 离线 3021 现场确认同口径:通讯断了不能永久卡死)
res.Items = append(res.Items, LineClearanceItem{
Key: "plc_sensor", Label: "PLC 传感器读不到实时状态",
Count: 1, Cleared: confirmedSet["plc_sensor"],
How: "PLC 通讯异常,无法自动核对工位有无工件;请现场目视确认各工位台面清空后勾「现场已处理」,或到 PLC 管理页确认连接恢复再重新检查",
Route: "/plc-monitor",
})
if !confirmedSet["plc_sensor"] {
unresolved = append(unresolved, "PLC 传感器读不到实时状态(1 项)")
}
} else if liveCount > 0 {
// 传感器确实看到工位有实物:硬阻塞,不能勾确认放行(保存时同样硬拦)。
res.Items = append(res.Items, LineClearanceItem{
Key: "plc_sensor", Label: "PLC 传感器实时显示工位上有工件",
Count: liveCount, Cleared: false, HardBlock: true,
How: "必须先到现场把工位台面上的工件搬走/完工/退库,传感器检测不到托盘后再点「重新检查」;此项不能靠勾选放行",
Route: "/plc-monitor",
})
unresolved = append(unresolved, fmt.Sprintf("PLC 传感器实时显示工位上有工件(%d 项,不能勾选放行)", liveCount))
} else {
res.Items = append(res.Items, LineClearanceItem{
Key: "plc_sensor", Label: "PLC 传感器实时显示工位上有工件",
Count: 0, Cleared: true,
How: "无需处理", Route: "/plc-monitor",
})
}
res.NeedClearance = len(unresolved) > 0
res.Passed = !res.NeedClearance
if res.Passed && len(confirmed) > 0 {
// 人工确认放行必须留痕:清线确认单是「准备运行」的可审计证据
s.ctx.EventLog.Write(ctx, "line.clearance.confirm", "", operator, "station", "",
"清线确认(人工确认放行)", map[string]any{"confirmed": confirmed, "items": res.Items})
}
return res, nil
}
// GuardLineClearanceWithConfirm 换绑定前清线确认(不清干净需逐项现场确认放行,留痕 event_log)。
func (s *Service) GuardLineClearanceWithConfirm(ctx context.Context, confirmed []string, operator string) error {
res, err := s.CheckLineClearance(ctx, confirmed, operator)
if err != nil {
return err
}
if res.Passed {
return nil
}
lines := []string{}
for _, it := range res.Items {
if it.Count > 0 && !it.Cleared {
lines = append(lines, fmt.Sprintf("%s:%d 项", it.Label, it.Count))
}
}
return fmt.Errorf("产线还没清干净(逐项确认后才可继续):\n· %s", strings.Join(lines, "\n· "))
}