126 lines
5.2 KiB
Go
126 lines
5.2 KiB
Go
package logic
|
||||
|
|
|
|||
|
|
import (
|
|||
|
|
"context"
|
|||
|
|
"fmt"
|
|||
|
|
"strings"
|
|||
|
|
|
|||
|
|
"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"`
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 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))
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
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· "))
|
|||
|
|
}
|