Files
bj_power/bj_power_mes/internal/logic/line_clearance.go
T
SunYF f9eaebad28 feat(mes/wms/终端): 签字循环业务重构 + 叫料池实物账两列 + 工位流水按包真分页 + 下线单据提示条(2026-11-30 现场整改)
【MES · 签字循环重构(业务重构,非修 bug)】
- 旧模型作废(签字随报工落库、检验员分支永不生效的死代码);新口径:
  工人干完点「呼叫签字」→ 检验员巡检端签 OK/NG → OK 后工人再点一次「报工」;NG 整改后重新呼叫(新一轮)
- 内容指纹(SHA256):呼叫时算一次落库,报工时重算比对,变了即「签字后改数据作废本轮」要求重呼;
  只覆盖步骤录入(值/文本/勾选/拧紧结果),装机绑定不进指纹(独立台账)、改备注不触发
- 轮次统一:一次呼叫=一轮,操作工/检验员共用 round;NG/撤回/重呼=新一轮,旧轮作废历史保留
- 三档状态(作业中/签字中/待整改/可报工)从签字记录推导,不建表不存终端
- 报工闸门:本轮需签步骤齐全(操作工+检验员OK)+ 指纹未变才放行
- 撤回呼叫整轮作废;超时巡检每分钟扫描(阈值 30 分钟可在基础设置调),超时落预警中心+异常角标不进铃铛,60 分钟窗口去重
- 工位终端新增签字交互接口与呼叫/撤回/状态查询;巡检端保留 NG 回看

【MES · 叫料池实物账(十五节 Q1/Q16 + 17.6 裁决)】
- call_pool 新增 receivedQty(已收累计,拒收按退库单冲减)/ returnedQty(已退累计,按退库单号防重)两列实物账,与 sentQty 额度账并存
- 台面在手(可用)= 已收 − 已装 − 已退;在途 = 已发 − 已收

【工位终端 · 物料/叫料页重构(十五节 Q1~Q17 拍板落地)】
- 「本工位物料明细」改名「物料明细」,四数列改为:可用/需求/已发/已收/已装,量为 0 也显示不分左右托盘
- 去除「刷新池子」「刷新」按钮(有长连接);「我的叫料单」扩为类银行流水(叫料/退料/等等)
- 叫料/退料数量列直排,提交时弹窗确认数量并选接驳台;管理端叫料与退料 AGV 派车流程统一收归库管
- 工位流水按包真分页(2026-11-29 拍板):后端按包号分组 Count+Limit/Offset,每页 20 包,前端不再内存聚合

【下线 · 单据流转提示条】
- MES/WMS 前端整体删除 NextStepBar / nextStep / nextStepRules(35 条规则表整套下线),页面不再出现常驻「下一步」提示条

【文档】
- 问题临时.md 新增十四节(主控独立审计:终端账号删除/心跳/闸门函数 8 处对码全过)+ 十五节(工位物料叫料 8 条原话逐字留档与 Q1~Q17 拍板底稿)
- 新增《工位终端页面.md》;案例.md 同步更新验收口径
2026-11-30 15:25:29 +08:00

229 lines
9.8 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"
"sort"
"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"`
// 2026-11-30:涉及工位号(去重升序,最多 20 个),前端「去处理」带 ?stationNo= 打开对应页面并定位,
// 不再跳到一个没有任何上下文的全量列表让用户自己找。
StationNos []int `json:"stationNos"`
// 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)
stations func() ([]int, error) // 涉及工位号(可选,供「去处理」定位)
}
probes := []probe{
{"workpiece", "工位上还有在制工件",
"到「在制品追踪」页把这些工件完工或暂存退库,台面清空后再回来点确认", "/wip",
func() (int, error) {
return s.ctx.EntClient.Workpiece.Query().Where(workpiece.AtStationNoGT(0)).Count(ctx)
},
func() ([]int, error) {
return s.ctx.EntClient.Workpiece.Query().Where(workpiece.AtStationNoGT(0)).
Select(workpiece.FieldAtStationNo).Ints(ctx)
}},
{"residual", "工位还有未用完的料",
"到工位终端做「退料」把余料退回库房,或在「生产退料」页确认/取消待处理退料单", "/transfer-return",
func() (int, error) {
return s.ctx.EntClient.CallPool.Query().
Where(callpool.Or(callpool.SentQtyGT(0), callpool.ReservedQtyGT(0))).Count(ctx)
},
func() ([]int, error) {
return s.ctx.EntClient.CallPool.Query().
Where(callpool.Or(callpool.SentQtyGT(0), callpool.ReservedQtyGT(0))).
Select(callpool.FieldStationNo).Ints(ctx)
}},
{"pallet", "产线点位上还压着带工件的托盘",
"用「人工派车 → 托盘回库」把托盘(连同工件)拉走,或把工件完工/退库;空托盘(上面没工件)不算占位、不用处理", "/agv-manual",
func() (int, error) {
return s.ctx.EntClient.LinePoint.Query().
Where(linepoint.CurrentSnNEQ("")).Count(ctx)
},
func() ([]int, error) {
return s.ctx.EntClient.LinePoint.Query().
Where(linepoint.CurrentSnNEQ("")).
Select(linepoint.FieldStationNo).Ints(ctx)
}},
{"plc", "PLC 还有没执行完的指令",
"到「工位下发」页核对这些指令是否已到位;确实清完再回来勾确认。PLC 离线时可现场目视确认后勾选放行(会留痕)", "/plc-send",
func() (int, error) {
return s.ctx.EntClient.PlcSendLog.Query().
Where(plcsendlog.StatusIn("SENT", "PENDING")).Count(ctx)
},
func() ([]int, error) {
return s.ctx.EntClient.PlcSendLog.Query().
Where(plcsendlog.StatusIn("SENT", "PENDING")).
Select(plcsendlog.FieldStationNo).Ints(ctx)
}},
{"agv", "还有 AGV 任务没结束",
"到「AGV管理」页把这些任务结束或取消;海康后台遗留的非本系统任务请到海康 RCS 后台处理", "/agv",
func() (int, error) {
return s.ctx.EntClient.AgvTask.Query().
Where(agvtask.StatusIn("PENDING", "1", "2", "ARRIVED")).Count(ctx)
},
nil},
}
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}
if p.stations != nil {
if nos, se := p.stations(); se == nil {
item.StationNos = normalizeStationNos(nos)
}
}
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, []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, nil, false
}
nos := make([]int, 0, 12)
for k := 0; k < 12; k++ {
if bits[k] {
nos = append(nos, k+1)
}
}
return len(nos), nos, true
}
liveCount, liveNos, 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, StationNos: liveNos,
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
}
// normalizeStationNos 工位号去重升序(丢掉 0/负数,限 20 个,够前端定位用)。
func normalizeStationNos(in []int) []int {
seen := map[int]bool{}
out := make([]int, 0, len(in))
for _, n := range in {
if n > 0 && !seen[n] {
seen[n] = true
out = append(out, n)
}
}
sort.Ints(out)
if len(out) > 20 {
out = out[:20]
}
return out
}
// 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· "))
}