- 移除工位模板体系(handler/logic 全删),同步路由与 seed - AGV 链路增强:点位锁、车辆状态、退料派车调整,新增海康 RCS Postman 调试集合 - MES 产品BOM/混产模板完善:新增 ProductBOMDialog 组件,调整 handler/logic;日排产达成对比调整 - WMS 业务完善:出入库、检验、备料、退料、成品入库、缺料代报等 handler 与前端页面调整 - 前端通用:双端新增 RemotePageSelect 组件,统一 nextStep 规则/帮助文档/布局/请求拦截 - 文档:更新产线API/案例/部署手册/问题记录等
304 lines
12 KiB
Go
304 lines
12 KiB
Go
package logic
|
||
|
||
import (
|
||
"context"
|
||
"fmt"
|
||
"strings"
|
||
|
||
"bj_power_mes/ent"
|
||
"bj_power_mes/ent/agvtask"
|
||
"bj_power_mes/ent/callpool"
|
||
"bj_power_mes/ent/linepoint"
|
||
"bj_power_mes/ent/mixproductiontemplate"
|
||
"bj_power_mes/ent/plcsendlog"
|
||
"bj_power_mes/ent/workpiece"
|
||
)
|
||
|
||
// ============ 混产模板 + 清线门禁(2026-11-13 拍板) ============
|
||
// 「生成工位模板」的原始需求是快捷操作 + 支持多工单(多成品)混产,不是换线工具。
|
||
// 同模板内的产品切换靠扫码防错,不强制停机清线;
|
||
// 引入模板外的新产品才触发 GuardLineClearance(在制品 / 余料 / 托盘 / PLC 未完成任务 / AGV 在途 五项)。
|
||
// 2026-11-13 终审补充:必须记录「当前产线在用哪个混产模板」,排产/建工单据此校验;
|
||
// 触发点必须包含「应用工位绑定变更」;Count 查询失败一律 fail-close;人工确认放行要留痕。
|
||
|
||
// CfgActiveMixTemplate 当前产线在用的混产模板名(sys_config,页面/接口可切换)。
|
||
const CfgActiveMixTemplate = "line.active_mix_template"
|
||
|
||
// ListMixTemplates 混产模板列表(带 isActive:是否为当前产线在用模板)。
|
||
func (s *Service) ListMixTemplates(ctx context.Context) ([]map[string]any, error) {
|
||
activeName := s.CfgStr(ctx, CfgActiveMixTemplate, "")
|
||
rows, err := s.ctx.EntClient.MixProductionTemplate.Query().All(ctx)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
out := make([]map[string]any, 0, len(rows))
|
||
for _, r := range rows {
|
||
codes := []string{}
|
||
for _, c := range strings.Split(r.ProductCodes, ",") {
|
||
if c = strings.TrimSpace(c); c != "" {
|
||
codes = append(codes, c)
|
||
}
|
||
}
|
||
out = append(out, map[string]any{
|
||
"id": r.ID, "name": r.Name, "productCodes": r.ProductCodes,
|
||
"products": codes, "remark": r.Remark, "status": r.Status,
|
||
"isActive": r.Name == activeName,
|
||
})
|
||
}
|
||
return out, nil
|
||
}
|
||
|
||
// SaveMixTemplate 新增/编辑混产模板(圈定哪几个产品可混线生产)。
|
||
// 新建并启用时自动设为「当前产线在用模板」。
|
||
func (s *Service) SaveMixTemplate(ctx context.Context, id int, name, productCodes, remark, status, operator string) error {
|
||
name = strings.TrimSpace(name)
|
||
if name == "" {
|
||
return fmt.Errorf("模板名称必填")
|
||
}
|
||
seen := map[string]bool{}
|
||
clean := []string{}
|
||
for _, c := range strings.Split(productCodes, ",") {
|
||
if c = strings.TrimSpace(c); c != "" && !seen[c] {
|
||
seen[c] = true
|
||
clean = append(clean, c)
|
||
}
|
||
}
|
||
if len(clean) < 2 {
|
||
return fmt.Errorf("混产模板至少需要 2 个产品(单产品不需要混产模板)")
|
||
}
|
||
if status == "" {
|
||
status = "ACTIVE"
|
||
}
|
||
if id > 0 {
|
||
_, err := s.ctx.EntClient.MixProductionTemplate.UpdateOneID(id).
|
||
SetName(name).SetProductCodes(strings.Join(clean, ",")).SetRemark(remark).SetStatus(status).Save(ctx)
|
||
if err == nil {
|
||
s.ctx.EventLog.Write(ctx, "mix.template.save", "", operator, "mix_production_template", fmt.Sprint(id), "编辑混产模板", nil)
|
||
}
|
||
return err
|
||
}
|
||
_, err := s.ctx.EntClient.MixProductionTemplate.Create().
|
||
SetName(name).SetProductCodes(strings.Join(clean, ",")).SetRemark(remark).SetStatus(status).Save(ctx)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
s.ctx.EventLog.Write(ctx, "mix.template.save", "", operator, "mix_production_template", name,
|
||
"保存当前绑定为混产模板", map[string]any{"products": clean})
|
||
if status == "ACTIVE" {
|
||
return s.SetActiveMixTemplate(ctx, name, operator)
|
||
}
|
||
return nil
|
||
}
|
||
|
||
// SetActiveMixTemplate 切换「当前产线在用的混产模板」(排产/建工单据此校验产品范围)。
|
||
func (s *Service) SetActiveMixTemplate(ctx context.Context, name, operator string) error {
|
||
name = strings.TrimSpace(name)
|
||
if name == "" {
|
||
return fmt.Errorf("模板名称必填")
|
||
}
|
||
n, err := s.ctx.EntClient.MixProductionTemplate.Query().
|
||
Where(mixproductiontemplate.Name(name), mixproductiontemplate.Status("ACTIVE")).Count(ctx)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
if n == 0 {
|
||
return fmt.Errorf("启用中的混产模板不存在:%s", name)
|
||
}
|
||
if err := s.upsertConfig(ctx, CfgActiveMixTemplate, name, "当前产线在用的混产模板"); err != nil {
|
||
return err
|
||
}
|
||
s.ctx.EventLog.Write(ctx, "mix.template.active", "", operator, "sys_config", CfgActiveMixTemplate,
|
||
"切换当前在用混产模板:"+name, nil)
|
||
return nil
|
||
}
|
||
|
||
// ActiveMixTemplate 取当前产线在用模板;未显式设置时,只有一个启用模板才自动视为当前模板。
|
||
func (s *Service) ActiveMixTemplate(ctx context.Context) (*ent.MixProductionTemplate, error) {
|
||
name := strings.TrimSpace(s.CfgStr(ctx, CfgActiveMixTemplate, ""))
|
||
if name != "" {
|
||
return s.ctx.EntClient.MixProductionTemplate.Query().
|
||
Where(mixproductiontemplate.Name(name), mixproductiontemplate.Status("ACTIVE")).Only(ctx)
|
||
}
|
||
rows, err := s.ctx.EntClient.MixProductionTemplate.Query().
|
||
Where(mixproductiontemplate.Status("ACTIVE")).All(ctx)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
if len(rows) == 1 {
|
||
return rows[0], nil
|
||
}
|
||
return nil, nil // 多个模板且未指定当前模板 → 不允许「随便算同模板」
|
||
}
|
||
|
||
// ActiveMixProducts 当前模板圈定的成品集合(排产/建工单校验用)。
|
||
func (s *Service) ActiveMixProducts(ctx context.Context) (string, []string, error) {
|
||
tpl, err := s.ActiveMixTemplate(ctx)
|
||
if err != nil || tpl == nil {
|
||
return "", []string{}, err
|
||
}
|
||
out := []string{}
|
||
for _, c := range strings.Split(tpl.ProductCodes, ",") {
|
||
if c = strings.TrimSpace(c); c != "" {
|
||
out = append(out, c)
|
||
}
|
||
}
|
||
return tpl.Name, out, nil
|
||
}
|
||
|
||
// ProductInActiveMixTemplate 判断某成品是否属于「当前在用混产模板」。
|
||
// 只有当前模板内的产品切换才免清线;跨模板/无当前模板一律强清线。
|
||
func (s *Service) ProductInActiveMixTemplate(ctx context.Context, productCode string) (bool, error) {
|
||
productCode = strings.TrimSpace(productCode)
|
||
if productCode == "" {
|
||
return false, nil
|
||
}
|
||
_, products, err := s.ActiveMixProducts(ctx)
|
||
if err != nil {
|
||
return false, err
|
||
}
|
||
for _, c := range products {
|
||
if c == productCode {
|
||
return true, nil
|
||
}
|
||
}
|
||
return false, nil
|
||
}
|
||
|
||
// ============ 清线门禁(五项 + fail-close + 逐项确认留痕) ============
|
||
|
||
// LineClearanceItem 清线确认单的一行。
|
||
type LineClearanceItem struct {
|
||
Key string `json:"key"` // 稳定 key,前端逐项打勾回传
|
||
Label string `json:"label"` // 人话说明:为什么挡住
|
||
Count int `json:"count"` // 实测数量(0=已清)
|
||
Cleared bool `json:"cleared"` // 人工确认放行(PLC 离线等场景)
|
||
How string `json:"how"` // 怎么解决:去哪个页面做什么(2026-11-17 透明化,不再是黑盒墙)
|
||
Route string `json:"route"` // 前端路由:点「去处理」直接跳转
|
||
}
|
||
|
||
// LineClearanceResult 清线检查结果(needClearance=false 表示同模板切换,免清线)。
|
||
type LineClearanceResult struct {
|
||
NeedClearance bool `json:"needClearance"`
|
||
ProductCode string `json:"productCode"`
|
||
TemplateName string `json:"templateName"`
|
||
Items []LineClearanceItem `json:"items"`
|
||
Passed bool `json:"passed"`
|
||
}
|
||
|
||
// CheckLineClearance 只读检查五项(2026-11-13 拍板:含 PLC 未完成任务)。
|
||
// 任一 Count 出错立即返回 error(fail-close),绝不像旧版那样吞掉错误按 0 放行。
|
||
// confirmed 是用户逐项打勾/人工确认放行的 key;确认结果会写 event_log 形成可审计的「清线确认单」。
|
||
func (s *Service) CheckLineClearance(ctx context.Context, productCode string, confirmed []string, operator string) (*LineClearanceResult, error) {
|
||
res := &LineClearanceResult{ProductCode: productCode, Items: []LineClearanceItem{}}
|
||
tpl, err := s.ActiveMixTemplate(ctx)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
if tpl != nil {
|
||
res.TemplateName = tpl.Name
|
||
}
|
||
inTemplate, err := s.ProductInActiveMixTemplate(ctx, productCode)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
if inTemplate {
|
||
// 同模板内多成品混产切换:不强制停机清线
|
||
res.NeedClearance = false
|
||
res.Passed = true
|
||
return res, nil
|
||
}
|
||
res.NeedClearance = true
|
||
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.Or(linepoint.CurrentSnNEQ(""), linepoint.Occupied(true))).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管理」页把这些任务结束或取消(非本系统的任务可用「取消海康任务」按任务号取消)", "/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.Passed = len(unresolved) == 0
|
||
if res.Passed && len(confirmed) > 0 {
|
||
// 人工确认放行必须留痕:清线确认单是「准备运行」的可审计证据
|
||
s.ctx.EventLog.Write(ctx, "line.clearance.confirm", "", operator, "mix_production_template",
|
||
res.TemplateName, "清线确认单(人工确认放行)",
|
||
map[string]any{"productCode": productCode, "confirmed": confirmed, "items": res.Items})
|
||
}
|
||
return res, nil
|
||
}
|
||
|
||
// GuardLineClearance 清线门禁(不带人工确认):引入模板外新产品时硬拦。
|
||
func (s *Service) GuardLineClearance(ctx context.Context, productCode string) error {
|
||
return s.guardLineClearance(ctx, productCode, nil, "")
|
||
}
|
||
|
||
// GuardLineClearanceWithConfirm 带逐项确认的清线门禁(应用工位绑定变更/应用模板时)。
|
||
func (s *Service) GuardLineClearanceWithConfirm(ctx context.Context, productCode string, confirmed []string, operator string) error {
|
||
return s.guardLineClearance(ctx, productCode, confirmed, operator)
|
||
}
|
||
|
||
func (s *Service) guardLineClearance(ctx context.Context, productCode string, confirmed []string, operator string) error {
|
||
res, err := s.CheckLineClearance(ctx, productCode, 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("引入模板外成品 %s 需要先清线(逐项确认后才可继续):\n· %s",
|
||
productCode, strings.Join(lines, "\n· "))
|
||
}
|