feat: 新增收集式校验错误弹窗及工位报警语音播报

新增 ErrorListDialog 组件用于统一展示结构化校验错误信息,实现报错一次收全;新增工位报警语音播报功能,通过浏览器 SpeechSynthesis 播放本工位相关报警;调整物料类别及序列号字段文案,移除「其他」类别;新增工单实际开始字段及采集方式配置;优化 AGV 及 PlcSend 页签刷新逻辑;新增看板物料追溯及工位报警接口;删除 PLC 强制写 M 位及相关测试代码;同步数据库字段注释及补丁。
This commit is contained in:
SunYF
2026-10-20 16:36:26 +08:00
parent f743cf21fb
commit b0c048d9aa
126 changed files with 4603 additions and 1639 deletions
+95 -42
View File
@@ -14,10 +14,12 @@ import (
"bj_power_mes/ent/inspectionrecord"
"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"
"bj_power_mes/ent/workpieceprocess"
"bj_power_mes/internal/verr"
)
// ---------- 工艺流程(块3) ----------
@@ -179,7 +181,7 @@ func (s *Service) SaveFlow(ctx context.Context, req FlowReq, operator string) er
}
rec, err := s.ctx.EntClient.ProcessStep.Create().
SetFlowId(flowID).SetSeq(st.Seq).SetName(st.Name).
SetCollectType(st.CollectType).SetIsTorque(st.IsTorque).SetNeedCheck(st.NeedCheck).
SetCollectType(st.CollectType).SetNeedCheck(st.NeedCheck).
SetNeedOperatorSign(st.NeedOperatorSign).SetNeedInspectorSign(st.NeedInspectorSign).
SetRemark(st.Remark).SetAttachment(st.Attachment).Save(ctx)
if err != nil {
@@ -236,7 +238,7 @@ func (s *Service) flowSteps(ctx context.Context, flowId int) []*ProcessStepTempl
for _, st := range steps {
t := &ProcessStepTemplate{
ID: st.ID, Seq: st.Seq,
Name: st.Name, CollectType: st.CollectType, IsTorque: st.IsTorque, NeedCheck: st.NeedCheck,
Name: st.Name, CollectType: st.CollectType, NeedCheck: st.NeedCheck,
NeedOperatorSign: st.NeedOperatorSign, NeedInspectorSign: st.NeedInspectorSign,
Remark: st.Remark, Attachment: st.Attachment, Criteria: []CriterionVO{},
}
@@ -262,6 +264,8 @@ type StationReq struct {
StationType *string `json:"stationType"`
FlowId *int `json:"flowId"`
VisibleTabs []string `json:"visibleTabs"` // 终端可见页签(空=全部显示)
// 规则3 fail-open:PLC 读取失败/离线时,前端弹「已现场确认,继续」二次确认后带此标记重发,后端放行并留痕
PlcOfflineConfirmed bool `json:"plcOfflineConfirmed"`
}
type StationVO struct {
@@ -311,6 +315,7 @@ func (s *Service) ListStations(ctx context.Context) ([]*StationVO, error) {
// 内置工位(种子/初始化数据,is_builtin=true)需连 PLC 并必须按工位号顺序流转,
// 不允许手动更改工位类型;页面新增的工位一律为非内置,可改类型、可删除。
// 接驳台为独立主数据(MES dock 表),一个工位可绑定多个接驳台,关系以 dock.station_no 为准,页面不维护。
// 校验为收集式(报错一次收全 + 每条带怎么办,2026-10-20):类型/流程/托盘/在途指令全部列出、一次性返回。
func (s *Service) SaveStation(ctx context.Context, req StationReq, operator string) error {
if req.StationNo < 0 {
return errors.New("工位号非法(需 ≥ 0)")
@@ -320,17 +325,29 @@ func (s *Service) SaveStation(ctx context.Context, req StationReq, operator stri
}
st, err := s.ctx.EntClient.Station.Query().Where(station.StationNo(req.StationNo)).First(ctx)
if err != nil {
// 新增工位:工位号不存在即创建
// 新增工位:工位号不存在即创建(字段错误收集式列出)
ve := verr.New()
if req.Name == "" {
return errors.New("请填写工位名称")
ve.Add("name", "请填写工位名称", "在「工位名称」输入框填写名称后保存")
}
stype := "LINE"
if req.StationType != nil {
stype = *req.StationType
if stype != "LINE" && stype != "OFFLINE" {
return errors.New("工位类型仅支持 LINE/OFFLINE")
ve.Add("stationType", "工位类型仅支持 线上位(LINE)/线下位(OFFLINE)", "类型改为 LINE(连 PLC)或 OFFLINE(不连 PLC)")
}
}
if req.FlowId != nil && *req.FlowId > 0 {
flow, fErr := s.ctx.EntClient.ProcessFlow.Get(ctx, *req.FlowId)
if fErr != nil {
ve.Add("flowId", "所选工艺流程不存在", "从绑定流程下拉中重新选择")
} else if flow.Status != "ACTIVE" {
ve.AddAction("flowId", "该工艺流程已停用", "到「工艺」页启用后再绑定,或换选其他流程", "去工艺页", "/process-flow")
}
}
if err := ve.OrNil(); err != nil {
return err
}
cr := s.ctx.EntClient.Station.Create().
SetStationNo(req.StationNo).SetName(req.Name).SetStationType(stype).SetStatus("ENABLED").
// 页面「新增工位」创建的是非内置工位(可改类型、可删除);内置工位只能由种子/初始化数据产生。
@@ -339,13 +356,6 @@ func (s *Service) SaveStation(ctx context.Context, req StationReq, operator stri
cr.SetVisibleTabs(req.VisibleTabs)
}
if req.FlowId != nil && *req.FlowId > 0 {
flow, fErr := s.ctx.EntClient.ProcessFlow.Get(ctx, *req.FlowId)
if fErr != nil {
return errors.New("所选工艺流程不存在")
}
if flow.Status != "ACTIVE" {
return errors.New("该工艺流程已停用,请先启用后再绑定工位")
}
cr.SetFlowId(*req.FlowId)
}
created, cErr := cr.Save(ctx)
@@ -356,48 +366,91 @@ func (s *Service) SaveStation(ctx context.Context, req StationReq, operator stri
"新增工位", map[string]any{"stationNo": created.StationNo, "name": created.Name})
return nil
}
// 仅改名称:flowId 不传(nil)保留原绑定;0=解绑;>0=绑定指定启用流程
upd := s.ctx.EntClient.Station.UpdateOneID(st.ID).SetName(req.Name)
// 更新分支:字段错误 + 规则3守卫(托盘在位/在途指令)全部收集,一次性返回
ve := verr.New()
if req.StationType != nil {
t := *req.StationType
if t != "LINE" && t != "OFFLINE" {
return errors.New("工位类型仅支持 LINE/OFFLINE")
}
// 内置工位对应实体产线固定位置(含 PLC 通讯与顺序流转),工位类型不可手动更改。
if st.IsBuiltin && t != st.StationType {
return errors.New("内置工位不允许修改工位类型")
}
if !st.IsBuiltin {
upd.SetStationType(t)
ve.Add("stationType", "工位类型仅支持 线上位(LINE)/线下位(OFFLINE)", "类型改为 LINE(连 PLC)或 OFFLINE(不连 PLC)")
} else if st.IsBuiltin && t != st.StationType {
ve.Add("stationType", "内置工位不允许修改工位类型", "内置工位对应实体产线固定位置(连 PLC、按顺序流转),只能改名称/绑定/终端页签")
}
}
if req.FlowId != nil {
fid := *req.FlowId
if fid > 0 {
flow, fErr := s.ctx.EntClient.ProcessFlow.Get(ctx, fid)
if fErr != nil {
return errors.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
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 内存态,逻辑一致。
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
}
if flow.Status != "ACTIVE" {
return errors.New("该工艺流程已停用,请先启用后再绑定工位")
}
upd.SetFlowId(fid)
} else {
upd.SetFlowId(0)
} else if has {
ve.Add("flowId", fmt.Sprintf("工位%d 上还有工件,不能修改配置", st.StationNo),
"先完成/移走该工位上的工件(下线完工或流转到下一位),再修改配置")
}
}
// 终端可见页签:传 nil=不改;传空数组=清空(全部显示);传列表=按配置显示
if req.VisibleTabs != nil {
upd.SetVisibleTabs(req.VisibleTabs)
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 := upd.Save(ctx); err != nil {
return err
}
if err := ve.OrNil(); err != nil {
return err
}
upd := s.ctx.EntClient.Station.UpdateOneID(st.ID).SetName(req.Name)
if req.StationType != nil {
t := *req.StationType
if !st.IsBuiltin {
upd.SetStationType(t)
}
s.ctx.EventLog.Write(ctx, "station.save", "", operator, "station", "", "维护工位",
map[string]any{"stationNo": req.StationNo})
return 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
}
// 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 {