feat: 移除工艺物料与混产模板实体、新增产线清线与 PLC 监控实时推送、看板双屏增强

- 移除实体:MES 删除 FlowMaterial(工艺物料)与 MixProductionTemplate(混产模板)全套(schema/ent 生成码/handler/logic),清理关联路由与种子
- 新增功能:产线清线(line_clearance handler + logic)、PLC 监控 WebSocket 实时推送(ws/plc_monitor_hub + handler)
- 看板增强:新增管理屏与 KPI 组件(BoardManage/BoardKpi),调整演示屏 Mock 与各看板面板
- MES 完善:PLC 匹配/扭矩扫描/签收、工艺卡 PDF 与工单、AGV 链路、叫料/备料/在制品/短料预测等逻辑与前端调整
- 工位终端:删除离线面板 OfflinePanel,调整主界面/物料面板与 api/store/handler/路由
- WMS:AGV 托盘、物料与 MES 客户端调整及前端帮助/基础数据/叫料/托盘页
- 文档:更新产线API/客户问题/案例/部署手册/问题记录
This commit is contained in:
SunYF
2026-11-19 08:31:05 +08:00
parent f8482b18f6
commit 4c4cda1d41
148 changed files with 2511 additions and 9735 deletions
+112 -191
View File
@@ -9,12 +9,11 @@ import (
"time"
"bj_power_mes/ent"
"bj_power_mes/ent/flowmaterial"
"bj_power_mes/ent/inspectionrecord"
"bj_power_mes/ent/lossledger"
"bj_power_mes/ent/plcsendlog"
"bj_power_mes/ent/processflow"
"bj_power_mes/ent/processstep"
"bj_power_mes/ent/plcsendlog"
"bj_power_mes/ent/station"
"bj_power_mes/ent/stepcriterion"
"bj_power_mes/ent/workorder"
@@ -38,15 +37,15 @@ type FlowReq struct {
Steps []StepItemReq `json:"steps"`
}
// FlowVO 工艺流程视图(含工序步骤 + 绑定该流程的工位号)
// FlowVO 工艺流程视图(含工艺步骤 + 绑定该流程的工位号)
type FlowVO struct {
Id int `json:"id"`
Name string `json:"name"`
PdfFile string `json:"pdfFile"`
Status string `json:"status"`
Remark string `json:"remark"`
Stations []int `json:"stations"`
Steps []*ProcessStepTemplate `json:"steps"`
Id int `json:"id"`
Name string `json:"name"`
PdfFile string `json:"pdfFile"`
Status string `json:"status"`
Remark string `json:"remark"`
Stations []int `json:"stations"`
Steps []*ProcessStepTemplate `json:"steps"`
// 只读反查:引用本工艺的产品工艺名称列表(产品工艺明细实时反查)
ProductProcesses []string `json:"productProcesses"`
CreatedAt time.Time `json:"createdAt"`
@@ -103,7 +102,7 @@ func (s *Service) ListFlows(ctx context.Context, stationNo int, pp, name, status
if vo.ProductProcesses = ppNames[f.ID]; vo.ProductProcesses == nil {
vo.ProductProcesses = []string{}
}
// 工序数下限过滤(stepMin>0 时只保留工序数≥阈值者)
// 工艺数下限过滤(stepMin>0 时只保留工艺数≥阈值者)
if stepMin > 0 && len(vo.Steps) < stepMin {
continue
}
@@ -191,7 +190,7 @@ func (s *Service) SaveFlow(ctx context.Context, req FlowReq, operator string) er
return err
}
}
// 覆盖式重建该流程的工序步骤及考核标准
// 覆盖式重建该流程的工艺步骤及考核标准
_, _ = s.ctx.EntClient.ProcessStep.Delete().Where(processstep.FlowId(flowID)).Exec(ctx)
for _, st := range req.Steps {
if st.Name == "" {
@@ -252,7 +251,7 @@ func (s *Service) DeleteFlow(ctx context.Context, id int, operator string) error
return s.ctx.EntClient.ProcessFlow.DeleteOneID(id).Exec(ctx)
}
// flowSteps 查询某流程的工序步骤(含考核标准)
// flowSteps 查询某流程的工艺步骤(含考核标准)
func (s *Service) flowSteps(ctx context.Context, flowId int) []*ProcessStepTemplate {
steps, _ := s.ctx.EntClient.ProcessStep.Query().
Where(processstep.FlowId(flowId)).Order(ent.Asc(processstep.FieldSeq)).All(ctx)
@@ -421,91 +420,102 @@ func (s *Service) SaveStation(ctx context.Context, req StationReq, operator stri
return nil
}
// 更新分支:字段错误 + 规则3守卫(托盘在位/在途指令)全部收集,一次性返回
// 2026-11-13 拍板:改「工位绑定」= 应用工位绑定变更,必须过清线门禁五项
//(在制品 / 余料 / 托盘 / PLC 未完成任务 / AGV 在途);逐项确认放行会落 event_log 留痕。
// 2026-11-18 定稿:改「工位绑定」前查产线五项台账(在制工件/余料/带工件托盘/PLC 未完成/AGV 在途),
// 有未清项弹清线确认单,逐项现场确认放行落 event_log 留痕;空托盘不算占位。
if req.FlowId != nil && *req.FlowId != st.FlowId && !req.SkipClearanceCheck {
if err := s.GuardLineClearanceWithConfirm(ctx, "", req.ClearanceConfirmed, operator); err != nil {
if err := s.GuardLineClearanceWithConfirm(ctx, req.ClearanceConfirmed, operator); err != nil {
return err
}
}
ve := verr.New()
// 仅改名称:flowId 不传(nil)保留原绑定;0=解绑;>0=绑定指定启用流程。
// 规则3(2026-10-20 拍板,2026-10-20 下午改 fail-closed→fail-open):改绑定关系(含解绑)前,
// 过「在途指令/托盘在位」双检(两条同时收集,不串行)。
// PLC 读取失败/超时/断连:不再是硬拒绝——未二次确认前返回 ErrPlcOfflineConfirmRequired
//(前端弹「已现场确认,继续」二次确认后带 plcOfflineConfirmed=true 重发→放行并留痕);
// 托盘在位/在途指令仍按原规则硬拒绝。
// 接驳台关系在 dock 表维护、本页不编辑,守卫仅覆盖绑定关系变更。
plcOffline := false
bindFlow := 0
if req.FlowId != nil {
bindFlow = *req.FlowId
// 进行中工单守卫(2026-11-02 工位管理需求):工位正被进行中工单的生产路线引用时,禁止修改/解除绑定工艺。
// 仅当绑定关系真的发生变化(含解绑/换绑)时才拦截;重复保存原绑定不误伤。
if *req.FlowId != st.FlowId {
if busy, bErr := s.stationHasActiveWorkOrder(ctx, st.StationNo); bErr == nil && busy {
ve.Add("flowId", fmt.Sprintf("工位%d 存在正在进行的工单(生产路线包含此工位),禁止修改绑定工艺", st.StationNo),
"请先将该工位的进行中工单完工或取消,再修改绑定工艺")
}
// 未退料闭环校验(2026-11-16 主控包项8):工位有尚未被 WMS 确认的退库单(PENDING)时,
// 禁止改/解绑工艺绑定——强制先走完「清点退料 → WMS 确认收货」。WMS 不可达时 fail-open
// (与 PLC 离线同口径:外部系统宕机不能永久卡死工位配置),仅记日志不硬拦。
if s.ctx.Wms != nil {
if returns, rErr := s.ctx.Wms.StationReturnList(ctx, st.StationNo); rErr != nil {
logx.Errorf("工位%d 改绑定前查询未确认退库单失败(放行):%v", st.StationNo, rErr)
} else {
pending := 0
for _, ro := range returns {
stt := ""
if ro != nil {
if v, ok := ro["status"]; ok && v != nil {
stt = fmt.Sprint(v)
// 规则3(2026-10-20 拍板,2026-10-20 下午改 fail-closed→fail-open):改绑定关系(含解绑)前,
// 过「在途指令/托盘在位」双检(两条同时收集,不串行)。
// PLC 读取失败/超时/断连:不再是硬拒绝——未二次确认前返回 ErrPlcOfflineConfirmRequired
//(前端弹「已现场确认,继续」二次确认后带 plcOfflineConfirmed=true 重发→放行并留痕);
// 托盘在位/在途指令仍按原规则硬拒绝。
// 接驳台关系在 dock 表维护、本页不编辑,守卫仅覆盖绑定关系变更。
plcOffline := false
bindFlow := 0
if req.FlowId != nil {
bindFlow = *req.FlowId
// 进行中工单守卫(2026-11-02 工位管理需求):工位正被进行中工单的生产路线引用时,禁止修改/解除绑定工艺。
// 仅当绑定关系真的发生变化(含解绑/换绑)时才拦截;重复保存原绑定不误伤。
if *req.FlowId != st.FlowId {
// 工位 11/12=末端下料检测位(无终端、无报工入口,2026-11-18 定稿):绑定工艺永远等不到报工,
// 工件会卡死在末端。工位绑定/快捷绑定一律拦下。
if (st.StationNo == 11 || st.StationNo == 12) && bindFlow > 0 {
ve.Add("flowId", fmt.Sprintf("工位%d 为末端下料检测位,无报工入口,不可绑定工艺", st.StationNo),
"请把该道工艺绑定到实体装配工位(1~10)")
}
if busy, bErr := s.stationHasActiveWorkOrder(ctx, st.StationNo); bErr == nil && busy {
ve.Add("flowId", fmt.Sprintf("工位%d 存在正在进行的工单(生产路线包含此工位),禁止修改绑定工艺", st.StationNo),
"请先将该工位的进行中工单完工或取消,再修改绑定工艺")
}
// 未退料闭环校验(2026-11-16 主控包项8):工位有尚未被 WMS 确认的退库单(PENDING)时,
// 禁止改/解绑工艺绑定——强制先走完「清点退料 → WMS 确认收货」。WMS 不可达时 fail-open
// (与 PLC 离线同口径:外部系统宕机不能永久卡死工位配置),仅记日志不硬拦。
if s.ctx.Wms != nil {
if returns, rErr := s.ctx.Wms.StationReturnList(ctx, st.StationNo); rErr != nil {
logx.Errorf("工位%d 改绑定前查询未确认退库单失败(放行):%v", st.StationNo, rErr)
} else {
pending := 0
for _, ro := range returns {
stt := ""
if ro != nil {
if v, ok := ro["status"]; ok && v != nil {
stt = fmt.Sprint(v)
}
}
if stt == "PENDING" {
pending++
}
}
if stt == "PENDING" {
pending++
if pending > 0 {
ve.Add("flowId", fmt.Sprintf("工位%d 有 %d 张未确认的退库单,禁止修改绑定工艺", st.StationNo, pending),
"请先清点退料并在 WMS 退库单收货确认后,再修改绑定工艺")
}
}
if pending > 0 {
ve.Add("flowId", fmt.Sprintf("工位%d 有 %d 张未确认的退库单,禁止修改绑定工艺", st.StationNo, pending),
"请先清点退料并在 WMS 退库单收货确认后,再修改绑定工艺")
}
}
if n, _ := s.ctx.EntClient.PlcSendLog.Query().
Where(plcsendlog.Status("SENT"), plcsendlog.StationNo(st.StationNo)).Count(ctx); n > 0 {
ve.AddAction("flowId", fmt.Sprintf("工位%d 有正在执行的指令,不能修改配置", st.StationNo),
"等指令完成,或到 PLC 手动监控页强制清零后再改配置", "去 PLC 监控", "/plc-monitor")
}
if st.StationType == "LINE" {
// 托盘在位读 PLC G{工位号} 位;mock 模式同样走 QueryStationPallet 内存态,逻辑一致。
// 2026-11-18:走 plcHardCut 2.5s 硬切(与 AGV 防撞/看板/PLC监控/WS快照同一口径),
// 不再裸读卡满 30s——否则前端 axios 30s 先超时弹「服务响应超时」,永远等不到
// 本处的 3021 放行确认框。硬切后断电 2.5s 即返回错误→3021→前端弹现场确认放行。
has, perr := plcHardCut(ctx, 2500*time.Millisecond, func(c context.Context) (bool, error) {
return s.ctx.PLC.QueryStationPallet(c, st.StationNo)
})
if perr != nil {
if req.PlcOfflineConfirmed {
// fail-open:PLC 离线已现场确认 → 放行,置 plcOffline 由下方留痕
plcOffline = true
} else {
// 未确认:短路返回哨兵错误,handler 映射为 3021 触发前端二次确认
return ErrPlcOfflineConfirmRequired
}
} else if has {
ve.Add("flowId", fmt.Sprintf("工位%d 上还有工件,不能修改配置", st.StationNo),
"先完成/移走该工位上的工件(下线完工或流转到下一位),再修改配置")
}
}
if bindFlow > 0 {
flow, fErr := s.ctx.EntClient.ProcessFlow.Get(ctx, bindFlow)
if fErr != nil {
ve.Add("flowId", "所选工艺流程不存在", "从绑定流程下拉中重新选择")
} else if flow.Status != "ACTIVE" {
ve.AddAction("flowId", "该工艺流程已停用", "到「工艺」页启用后再绑定,或换选其他流程", "去工艺页", "/process-flow")
}
}
}
if n, _ := s.ctx.EntClient.PlcSendLog.Query().
Where(plcsendlog.Status("SENT"), plcsendlog.StationNo(st.StationNo)).Count(ctx); n > 0 {
ve.AddAction("flowId", fmt.Sprintf("工位%d 有正在执行的指令,不能修改配置", st.StationNo),
"等指令完成,或到 PLC 手动监控页强制清零后再改配置", "去 PLC 监控", "/plc-monitor")
if err := ve.OrNil(); err != nil {
return err
}
if st.StationType == "LINE" {
// 托盘在位读 PLC G{工位号} 位;mock 模式同样走 QueryStationPallet 内存态,逻辑一致。
has, perr := s.ctx.PLC.QueryStationPallet(ctx, st.StationNo)
if perr != nil {
if req.PlcOfflineConfirmed {
// fail-open:PLC 离线已现场确认 → 放行,置 plcOffline 由下方留痕
plcOffline = true
} else {
// 未确认:短路返回哨兵错误,handler 映射为 3021 触发前端二次确认
return ErrPlcOfflineConfirmRequired
}
} else if has {
ve.Add("flowId", fmt.Sprintf("工位%d 上还有工件,不能修改配置", st.StationNo),
"先完成/移走该工位上的工件(下线完工或流转到下一位),再修改配置")
}
}
if bindFlow > 0 {
flow, fErr := s.ctx.EntClient.ProcessFlow.Get(ctx, bindFlow)
if fErr != nil {
ve.Add("flowId", "所选工艺流程不存在", "从绑定流程下拉中重新选择")
} else if flow.Status != "ACTIVE" {
ve.AddAction("flowId", "该工艺流程已停用", "到「工艺」页启用后再绑定,或换选其他流程", "去工艺页", "/process-flow")
}
}
}
if err := ve.OrNil(); err != nil {
return err
}
upd := s.ctx.EntClient.Station.UpdateOneID(st.ID).SetName(req.Name)
// 类型调整:仅非内置工位允许 LINE↔OFFLINE(2026-11-02:修复存量虚拟工位被存成 LINE 后
// 看板"暂无虚拟工位"无法自愈的问题);内置工位类型锁定不变。
@@ -518,29 +528,30 @@ if err := ve.OrNil(); err != nil {
}
}
if req.FlowId != nil {
upd.SetFlowId(bindFlow) // 0=解绑
}
// 终端可见页签:传 nil=不改;传空数组=清空(全部显示);传列表=按配置显示
if req.VisibleTabs != nil {
upd.SetVisibleTabs(req.VisibleTabs)
}
if _, err := upd.Save(ctx); err != nil {
return err
}
if plcOffline {
// 留痕:PLC 离线放行(工位配置变更,安全相关,须可审计)
s.ctx.EventLog.Write(ctx, "station.config.offline_change", "", operator, "station", "",
fmt.Sprintf("PLC 离线放行工位配置变更(工位%d:改前工艺%d → 改后工艺%d)", st.StationNo, st.FlowId, bindFlow),
map[string]any{"stationNo": st.StationNo, "beforeFlowId": st.FlowId, "afterFlowId": bindFlow, "operator": operator, "plcOffline": true})
}
s.ctx.EventLog.Write(ctx, "station.save", "", operator, "station", "", "维护工位",
map[string]any{"stationNo": req.StationNo})
return nil
upd.SetFlowId(bindFlow) // 0=解绑
}
// 终端可见页签:传 nil=不改;传空数组=清空(全部显示);传列表=按配置显示
if req.VisibleTabs != nil {
upd.SetVisibleTabs(req.VisibleTabs)
}
if _, err := upd.Save(ctx); err != nil {
return err
}
if plcOffline {
// 留痕:PLC 离线放行(工位配置变更,安全相关,须可审计)
s.ctx.EventLog.Write(ctx, "station.config.offline_change", "", operator, "station", "",
fmt.Sprintf("PLC 离线放行工位配置变更(工位%d:改前工艺%d → 改后工艺%d)", st.StationNo, st.FlowId, bindFlow),
map[string]any{"stationNo": st.StationNo, "beforeFlowId": st.FlowId, "afterFlowId": bindFlow, "operator": operator, "plcOffline": true})
}
s.ctx.EventLog.Write(ctx, "station.save", "", operator, "station", "", "维护工位",
map[string]any{"stationNo": req.StationNo})
return nil
}
// ErrPlcOfflineConfirmRequired 哨兵错误:工位 PLC 读取失败/超时/断连且未二次确认时返回;
// handler 映射为 code=3021,前端弹「已现场确认,继续」二次确认后带 plcOfflineConfirmed=true 重发。
var ErrPlcOfflineConfirmRequired = errors.New("PLC离线确认:无法确认工位托盘状态(PLC 读取失败),请到现场确认后重试")
// DeleteStation 删除工位:内置工位不允许删除(对应实体产线位置,需连 PLC 且必须按顺序流转);
// 非内置工位(页面新增的)允许删除。
func (s *Service) DeleteStation(ctx context.Context, id int, operator string) error {
@@ -1010,93 +1021,3 @@ func (s *Service) Workload(ctx context.Context, operator, stationNo, orderNo, fr
"lossByOrder": map[string]any{"total": len(lossList), "list": lossList},
}, nil
}
// ---------- 工艺物料清单(flow_material) ----------
// FlowMaterialReq 工艺物料清单条目(保存为整表覆盖)
type FlowMaterialReq struct {
MaterialCode string `json:"materialCode"`
MaterialName string `json:"materialName"`
Spec string `json:"spec"`
Unit string `json:"unit"`
SnMode int `json:"snMode"` // 是否逐件SN管理 1序列号=是(录SN)/2序列号=否(冗余自物料档案)
UnitQty float64 `json:"unitQty"` // 单台用量
LossRate float64 `json:"lossRate"` // 损耗率(%)
}
// ListFlowMaterials 查某工艺的物料清单(按 ID 升序)
func (s *Service) ListFlowMaterials(ctx context.Context, flowId int) ([]*ent.FlowMaterial, error) {
return s.ctx.EntClient.FlowMaterial.Query().
Where(flowmaterial.FlowId(flowId)).
Order(ent.Asc(flowmaterial.FieldID)).All(ctx)
}
// SaveFlowMaterials 保存工艺物料清单(整表覆盖:先删后插)。
// 物料编码须在 WMS 物料档案存在;WMS 不可达/异常时降级放行并记日志,避免阻塞工艺配置。
func (s *Service) SaveFlowMaterials(ctx context.Context, flowId int, items []FlowMaterialReq, operator string) error {
if flowId <= 0 {
return errors.New("flowId 必填")
}
if _, err := s.ctx.EntClient.ProcessFlow.Get(ctx, flowId); err != nil {
return errors.New("工艺不存在")
}
// 条目12 守卫(2026-11-02):被执行中/已暂停工单引用的工艺,物料清单禁止整表覆盖
//(叫料池/单台用料均实时读本清单,改了会改变在产工单的备料口径)
if err := s.guardFlowEditable(ctx, flowId); err != nil {
return err
}
// WMS 物料存在性校验(缺失拒绝;不可达降级放行)
codes := make([]string, 0, len(items))
seen := map[string]bool{}
for _, it := range items {
if it.MaterialCode == "" {
return errors.New("物料编码不能为空")
}
if seen[it.MaterialCode] {
return errors.New("物料 " + it.MaterialCode + " 重复")
}
seen[it.MaterialCode] = true
codes = append(codes, it.MaterialCode)
}
if len(codes) > 0 && s.ctx.Wms != nil {
if missing, err := s.ctx.Wms.MaterialExists(ctx, codes); err != nil {
s.ctx.EventLog.Write(ctx, "flow.material.wms_skip", "", operator, "process_flow", itoa(flowId),
"WMS 物料校验不可达,已降级放行", map[string]any{"error": err.Error()})
} else if len(missing) > 0 {
return errors.New("以下物料在 WMS 物料档案中不存在:" + strings.Join(missing, "、"))
}
}
tx, err := s.ctx.EntClient.Tx(ctx)
if err != nil {
return err
}
committed := false
defer func() {
if !committed {
_ = tx.Rollback()
}
}()
client := tx.Client()
if _, err := client.FlowMaterial.Delete().Where(flowmaterial.FlowId(flowId)).Exec(ctx); err != nil {
return err
}
for _, it := range items {
if err := client.FlowMaterial.Create().
SetFlowId(flowId).
SetMaterialCode(it.MaterialCode).SetMaterialName(it.MaterialName).
SetSpec(it.Spec).SetUnit(it.Unit).SetSnMode(it.SnMode).
SetUnitQty(it.UnitQty).SetLossRate(it.LossRate).
Exec(ctx); err != nil {
return err
}
}
if err := tx.Commit(); err != nil {
return err
}
committed = true
s.ctx.EventLog.Write(ctx, "flow.material.save", "", operator, "process_flow", itoa(flowId),
"保存工艺物料清单(整表覆盖)", map[string]any{"flowId": flowId, "count": len(items)})
// 叫料池重算(B5:物料清单是用量来源,变更立即对齐)
_, _ = s.RecomputePoolAll(ctx)
return nil
}