- MES 新增实体:混产模板、产品BOM、产品工艺物料、试制记录(schema + ent 生成码 + logic/handler/前端页) - WMS 实体增强:检验记录、库存、半成品重构;新增序列号库存与短缺代报 - AGV 与产能:AGV 手动点位与链路、物料守卫、短缺预测、原因字典 - 前端通用能力:新增下一步引导组件 NextStepBar,统一帮助文档/布局/请求拦截与操作列 - 工位终端:界面与各 handler 调整(api/store/绑定/PLC/拧紧/上报等) - 配置与文档:更新 mes-api.yaml、WMS README 及案例/产线API/问题记录/项目说明/流程说明
290 lines
11 KiB
Go
290 lines
11 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 离线等场景)
|
||
}
|
||
|
||
// 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
|
||
count func() (int, error)
|
||
}
|
||
probes := []probe{
|
||
{"workpiece", "工位上还有在制工件(请先完工/暂存退库)", func() (int, error) {
|
||
return s.ctx.EntClient.Workpiece.Query().Where(workpiece.AtStationNoGT(0)).Count(ctx)
|
||
}},
|
||
{"residual", "叫料池还有已发/占用中的余料(请先退库冲正)", func() (int, error) {
|
||
return s.ctx.EntClient.CallPool.Query().
|
||
Where(callpool.Or(callpool.SentQtyGT(0), callpool.ReservedQtyGT(0))).Count(ctx)
|
||
}},
|
||
{"pallet", "产线点位台账还有托盘占用(请先清空托盘)", func() (int, error) {
|
||
return s.ctx.EntClient.LinePoint.Query().
|
||
Where(linepoint.Or(linepoint.CurrentSnNEQ(""), linepoint.Occupied(true))).Count(ctx)
|
||
}},
|
||
{"plc", "PLC 还有未完成的下发指令(请核对位置并处理;离线时可人工确认放行并留痕)", func() (int, error) {
|
||
return s.ctx.EntClient.PlcSendLog.Query().
|
||
Where(plcsendlog.StatusIn("SENT", "PENDING")).Count(ctx)
|
||
}},
|
||
{"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]}
|
||
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· "))
|
||
}
|