2026-11-19 08:31:05 +08:00
|
|
|
|
package logic
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
|
|
|
|
|
"fmt"
|
|
|
|
|
|
"strings"
|
2026-11-28 13:44:51 +08:00
|
|
|
|
"time"
|
2026-11-19 08:31:05 +08:00
|
|
|
|
|
|
|
|
|
|
"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-28 13:44:51 +08:00
|
|
|
|
// HardBlock=true 表示该项不能靠「现场已处理」勾选放行——必须先现场清掉实物。
|
|
|
|
|
|
// 2026-11-27 新增:PLC 传感器实时有件属硬拒绝(保存时 SaveStation 同样硬拦),
|
|
|
|
|
|
// 若做成可勾选放行,用户勾了以为能过、点保存还是被拦,反而更困惑。
|
|
|
|
|
|
HardBlock bool `json:"hardBlock"`
|
2026-11-19 08:31:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 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-28 13:44:51 +08:00
|
|
|
|
// 第六项(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",
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-19 08:31:05 +08:00
|
|
|
|
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· "))
|
|
|
|
|
|
}
|