feat: 新增收集式校验错误弹窗及工位报警语音播报
新增 ErrorListDialog 组件用于统一展示结构化校验错误信息,实现报错一次收全;新增工位报警语音播报功能,通过浏览器 SpeechSynthesis 播放本工位相关报警;调整物料类别及序列号字段文案,移除「其他」类别;新增工单实际开始字段及采集方式配置;优化 AGV 及 PlcSend 页签刷新逻辑;新增看板物料追溯及工位报警接口;删除 PLC 强制写 M 位及相关测试代码;同步数据库字段注释及补丁。
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -12,6 +13,7 @@ import (
|
||||
"bj_power_mes/ent/productprocess"
|
||||
"bj_power_mes/ent/productprocessitem"
|
||||
"bj_power_mes/ent/workorder"
|
||||
"bj_power_mes/internal/verr"
|
||||
)
|
||||
|
||||
// ---------- 产品工艺(2026-10-09 定稿) ----------
|
||||
@@ -54,8 +56,12 @@ type ProductProcessVO struct {
|
||||
Status string `json:"status"`
|
||||
Remark string `json:"remark"`
|
||||
Flows []ProductProcessItemVO `json:"flows"`
|
||||
// 只读:被工单引用次数(>0 时只读:禁编辑禁删,只能停用+新建)
|
||||
UsedByWorkOrders int `json:"usedByWorkOrders"`
|
||||
// 只读:被工单引用次数(总引用,含历史;展示用)
|
||||
UsedByWorkOrders int `json:"usedByWorkOrders"`
|
||||
// 只读:运行中工单(IN_PROGRESS/PAUSED)引用数 —— >0 时编辑接口拒绝(规则1,2026-10-20)
|
||||
RunningByWorkOrders int `json:"runningByWorkOrders"`
|
||||
// 只读:未开工工单(CREATED)引用数 —— >0 时允许编辑,前端保存前警告确认(规则1)
|
||||
CreatedByWorkOrders int `json:"createdByWorkOrders"`
|
||||
// 只读:可执行状态(所有工艺都有启用工位绑定且顺序可满足)
|
||||
Executable bool `json:"executable"`
|
||||
NotReady string `json:"notReady"` // 不可执行原因(可执行时为空)
|
||||
@@ -123,18 +129,27 @@ func (s *Service) ListProductProcesses(ctx context.Context, productCode, status
|
||||
for _, it := range items {
|
||||
itemsOf[it.ProductProcessId] = append(itemsOf[it.ProductProcessId], it)
|
||||
}
|
||||
// 工单引用计数
|
||||
// 工单引用计数(规则1,2026-10-20):总数 + 运行中(IN_PROGRESS/PAUSED)+ 未开工(CREATED)分档
|
||||
used := map[int]int{}
|
||||
usedRunning := map[int]int{}
|
||||
usedCreated := map[int]int{}
|
||||
wos, _ := s.ctx.EntClient.WorkOrder.Query().Where(workorder.ProductProcessIdGT(0)).All(ctx)
|
||||
for _, w := range wos {
|
||||
used[w.ProductProcessId]++
|
||||
switch w.Status {
|
||||
case "IN_PROGRESS", "PAUSED":
|
||||
usedRunning[w.ProductProcessId]++
|
||||
case "CREATED", "PENDING":
|
||||
usedCreated[w.ProductProcessId]++
|
||||
}
|
||||
}
|
||||
out := make([]*ProductProcessVO, 0, len(pps))
|
||||
for _, p := range pps {
|
||||
vo := &ProductProcessVO{
|
||||
Id: p.ID, ProductCode: p.ProductCode, ProductName: p.ProductName, Name: p.Name,
|
||||
Status: p.Status, Remark: p.Remark,
|
||||
UsedByWorkOrders: used[p.ID], CreatedAt: p.CreatedAt,
|
||||
UsedByWorkOrders: used[p.ID], RunningByWorkOrders: usedRunning[p.ID],
|
||||
CreatedByWorkOrders: usedCreated[p.ID], CreatedAt: p.CreatedAt,
|
||||
Flows: []ProductProcessItemVO{},
|
||||
}
|
||||
for _, it := range itemsOf[p.ID] {
|
||||
@@ -179,29 +194,38 @@ func ppExecutable(vo *ProductProcessVO) (bool, string) {
|
||||
// SaveProductProcess 保存产品工艺(新建或整表覆盖明细)。
|
||||
// 约束:① 产品必填,明细非空;② 同一工艺不能在同一产品工艺内出现两次(客户裁决);
|
||||
// ③ 一个产品对应一个产品工艺(新建时若该产品已存在 product_process,直接报错"该产品已有产品工艺,请直接编辑");
|
||||
// ④ 被工单引用过的只读,禁编辑;⑤ 名称未传时按"${物料编码} ${物料名称} 工艺"兜底。
|
||||
// ④ 规则1(2026-10-20):运行中工单(IN_PROGRESS/PAUSED)引用 → 禁编辑;仅未开工(CREATED)引用 → 允许(前端警告确认);
|
||||
// ⑤ 名称未传时按"${物料编码} ${物料名称} 工艺"兜底。
|
||||
func (s *Service) SaveProductProcess(ctx context.Context, req ProductProcessReq, operator string) error {
|
||||
// 收集式校验(报错一次收全 + 每条带怎么办,2026-10-20):全部问题列出、一次性返回
|
||||
ve := verr.New()
|
||||
if strings.TrimSpace(req.ProductCode) == "" {
|
||||
return errors.New("请选择产品")
|
||||
ve.Add("productCode", "请选择产品", "在「产品」下拉中选择产品(来自 WMS 物料档案,类型=成品)")
|
||||
}
|
||||
// 名称兜底(前端未传时按 物料编码 物料名称 工艺 生成)
|
||||
if strings.TrimSpace(req.Name) == "" {
|
||||
req.Name = strings.TrimSpace(req.ProductCode) + " " + strings.TrimSpace(req.ProductName) + " 工艺"
|
||||
}
|
||||
if len(req.Flows) == 0 {
|
||||
return errors.New("请至少选择一道工艺")
|
||||
ve.Add("flows", "请至少选择一道工艺", "在「工艺流程」中点「加入」加入至少一道工艺,再用上下移按钮排好执行顺序")
|
||||
}
|
||||
seen := map[int]bool{}
|
||||
dupReported := map[int]bool{}
|
||||
for _, f := range req.Flows {
|
||||
if f.FlowId <= 0 {
|
||||
return errors.New("工艺选择无效")
|
||||
ve.Add("flows", "工艺选择无效", "删除无效行后,从工艺下拉中重新选择")
|
||||
continue
|
||||
}
|
||||
if seen[f.FlowId] {
|
||||
return errors.New("同一工艺不能在同一产品工艺内出现两次")
|
||||
if !dupReported[f.FlowId] {
|
||||
ve.Add("flows", "同一工艺在同一产品工艺内出现两次", "重复工艺只保留一处(客户裁决:同一工艺在同一产品工艺内不能重复)")
|
||||
dupReported[f.FlowId] = true
|
||||
}
|
||||
continue
|
||||
}
|
||||
seen[f.FlowId] = true
|
||||
if _, err := s.ctx.EntClient.ProcessFlow.Get(ctx, f.FlowId); err != nil {
|
||||
return errors.New("所选工艺不存在(ID=" + fmt.Sprint(f.FlowId) + ")")
|
||||
ve.Add("flows", fmt.Sprintf("所选工艺不存在(ID=%d)", f.FlowId), "删除该行后,从工艺下拉中重新选择")
|
||||
}
|
||||
}
|
||||
status := req.Status
|
||||
@@ -209,16 +233,31 @@ func (s *Service) SaveProductProcess(ctx context.Context, req ProductProcessReq,
|
||||
status = "ACTIVE"
|
||||
}
|
||||
if status != "ACTIVE" && status != "INACTIVE" {
|
||||
return errors.New("非法状态")
|
||||
ve.Add("status", "非法状态", "状态仅支持 启用/停用,请刷新页面后重试")
|
||||
}
|
||||
if req.Id > 0 {
|
||||
if _, err := s.ctx.EntClient.ProductProcess.Get(ctx, req.Id); err != nil {
|
||||
ve.Add("id", "产品工艺不存在", "列表可能已被他人修改,请关闭弹窗刷新后重试")
|
||||
} else if n, _ := s.ctx.EntClient.WorkOrder.Query().
|
||||
Where(workorder.ProductProcessId(req.Id), workorder.StatusIn("IN_PROGRESS", "PAUSED")).Count(ctx); n > 0 {
|
||||
// 规则1(2026-10-20):运行中工单引用 → 拒绝编辑,给「复制工艺」一键跳转
|
||||
ve.AddAction("productProcessId", "产线上还有运行中的工单,不能修改此工艺。",
|
||||
"如需调整,请复制一份新工艺修改,新工单使用新工艺。",
|
||||
"复制工艺", "/product-process?copy="+strconv.Itoa(req.Id))
|
||||
}
|
||||
} else if strings.TrimSpace(req.ProductCode) != "" {
|
||||
// 一个产品对应一个产品工艺:新建时若已存在启用的则报错(停用的允许并存:复制场景)
|
||||
if n, _ := s.ctx.EntClient.ProductProcess.Query().
|
||||
Where(productprocess.ProductCode(req.ProductCode), productprocess.Status("ACTIVE")).Count(ctx); n > 0 {
|
||||
ve.Add("productCode", "该产品已有启用中的产品工艺,请直接编辑",
|
||||
"在列表中找到该产品的工艺行点「编辑」;如需换版,先停用旧工艺再新建")
|
||||
}
|
||||
}
|
||||
if err := ve.OrNil(); err != nil {
|
||||
return err
|
||||
}
|
||||
var ppID int
|
||||
if req.Id > 0 {
|
||||
if _, err := s.ctx.EntClient.ProductProcess.Get(ctx, req.Id); err != nil {
|
||||
return errors.New("产品工艺不存在")
|
||||
}
|
||||
if n, _ := s.ctx.EntClient.WorkOrder.Query().Where(workorder.ProductProcessId(req.Id)).Count(ctx); n > 0 {
|
||||
return errors.New(fmt.Sprintf("该产品工艺已被 %d 张工单引用,只读不可编辑(如需调整请停用后新建)", n))
|
||||
}
|
||||
if _, err := s.ctx.EntClient.ProductProcess.UpdateOneID(req.Id).
|
||||
SetProductCode(req.ProductCode).SetProductName(req.ProductName).
|
||||
SetName(req.Name).SetStatus(status).SetRemark(req.Remark).
|
||||
@@ -227,10 +266,6 @@ func (s *Service) SaveProductProcess(ctx context.Context, req ProductProcessReq,
|
||||
}
|
||||
ppID = req.Id
|
||||
} else {
|
||||
// 一个产品对应一个产品工艺:新建时若已存在则直接报错
|
||||
if n, _ := s.ctx.EntClient.ProductProcess.Query().Where(productprocess.ProductCode(req.ProductCode)).Count(ctx); n > 0 {
|
||||
return errors.New("该产品已有产品工艺,请直接编辑")
|
||||
}
|
||||
p, err := s.ctx.EntClient.ProductProcess.Create().
|
||||
SetProductCode(req.ProductCode).SetProductName(req.ProductName).
|
||||
SetName(req.Name).SetStatus(status).SetRemark(req.Remark).
|
||||
@@ -303,6 +338,46 @@ func (s *Service) SetProductProcessStatus(ctx context.Context, id int, status, o
|
||||
return nil
|
||||
}
|
||||
|
||||
// CopyProductProcessReq 产品工艺复制请求
|
||||
type CopyProductProcessReq struct {
|
||||
SourceId int `json:"sourceId"`
|
||||
NewName string `json:"newName"`
|
||||
}
|
||||
|
||||
// CopyProductProcess 复制产品工艺(全量深拷贝):主表 + 所有明细步骤(ProductProcessItem 按 seq 顺序)。
|
||||
// 数据模型仅主表+明细两表,无独立"步骤考核标准/物料绑定"子表,故深拷贝二者即覆盖全部内容。
|
||||
// 默认名"源名-副本";按规则1精神,复制是新工艺,不受"一个产品一个产品工艺"约束(同一产品可有多版本)。
|
||||
func (s *Service) CopyProductProcess(ctx context.Context, req CopyProductProcessReq, operator string) (int, error) {
|
||||
if req.SourceId <= 0 {
|
||||
return 0, errors.New("缺少源产品工艺ID")
|
||||
}
|
||||
src, err := s.ctx.EntClient.ProductProcess.Get(ctx, req.SourceId)
|
||||
if err != nil {
|
||||
return 0, errors.New("源产品工艺不存在")
|
||||
}
|
||||
items, _ := s.ctx.EntClient.ProductProcessItem.Query().
|
||||
Where(productprocessitem.ProductProcessId(src.ID)).Order(ent.Asc(productprocessitem.FieldSeq)).All(ctx)
|
||||
newName := strings.TrimSpace(req.NewName)
|
||||
if newName == "" {
|
||||
newName = src.Name + "-副本"
|
||||
}
|
||||
np, err := s.ctx.EntClient.ProductProcess.Create().
|
||||
SetProductCode(src.ProductCode).SetProductName(src.ProductName).
|
||||
SetName(newName).SetStatus(src.Status).SetRemark(src.Remark).Save(ctx)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
for _, it := range items {
|
||||
if err := s.ctx.EntClient.ProductProcessItem.Create().
|
||||
SetProductProcessId(np.ID).SetSeq(it.Seq).SetFlowId(it.FlowId).Exec(ctx); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
s.ctx.EventLog.Write(ctx, "product.process.copy", "", operator, "product_process", fmt.Sprint(np.ID),
|
||||
"复制产品工艺", map[string]any{"fromId": src.ID, "fromName": src.Name, "toId": np.ID, "toName": newName})
|
||||
return np.ID, nil
|
||||
}
|
||||
|
||||
// DeriveProductProcess 建单自动推导:按产品(或指定 id)取该产品的唯一产品工艺,
|
||||
// 每道工艺带出其全部启用的绑定工位(升序),拼成工艺组合与实时警告。
|
||||
// 返回 map{productProcessId,name,items,combination,warnings};无产品工艺时 combination 为空并给提示。
|
||||
@@ -352,7 +427,6 @@ func (s *Service) DeriveProductProcess(ctx context.Context, productCode string,
|
||||
warns := []string{}
|
||||
itemsVO := []map[string]any{}
|
||||
combination := []map[string]int{}
|
||||
prevMax := 0
|
||||
for _, it := range items {
|
||||
stsOfFlow := byFlow[it.FlowId]
|
||||
itemsVO = append(itemsVO, map[string]any{
|
||||
@@ -362,12 +436,6 @@ func (s *Service) DeriveProductProcess(ctx context.Context, productCode string,
|
||||
warns = append(warns, fmt.Sprintf("第%d道工艺「%s」暂无启用的绑定工位,请先在「工位绑定」中绑定", it.Seq, flowName[it.FlowId]))
|
||||
continue
|
||||
}
|
||||
if prevMax > 0 && stsOfFlow[0] <= prevMax {
|
||||
warns = append(warns, fmt.Sprintf("第%d道工艺「%s」(工位%d) 与上一道工位顺序冲突(工位号需按顺序递增)", it.Seq, flowName[it.FlowId], stsOfFlow[0]))
|
||||
}
|
||||
if stsOfFlow[len(stsOfFlow)-1] > prevMax {
|
||||
prevMax = stsOfFlow[len(stsOfFlow)-1]
|
||||
}
|
||||
for _, no := range stsOfFlow {
|
||||
combination = append(combination, map[string]int{"flowId": it.FlowId, "stationNo": no})
|
||||
}
|
||||
@@ -379,9 +447,9 @@ func (s *Service) DeriveProductProcess(ctx context.Context, productCode string,
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// ComboDeviationWarnings 工单工艺组合与产品工艺的偏差警告(仅提示,不阻断保存):
|
||||
// ① 覆盖/个数——产品工艺的每道工艺都须出现在组合中,组合不得包含产品工艺之外的工艺;
|
||||
// ② 顺序——各道工艺在组合中的首个工位号须按 seq 递增。
|
||||
// ComboDeviationWarnings 工单工艺组合与产品工艺的偏差柔性警告(仅提示,不阻断保存):
|
||||
// 覆盖/个数——产品工艺的每道工艺都须出现在组合中,组合不得包含产品工艺之外的工艺。
|
||||
// 顺序偏差已于 2026-10-20 客户裁定升级为硬拦截(见 ComboOrderConflict),此处不再重复提示。
|
||||
func (s *Service) ComboDeviationWarnings(ctx context.Context, ppId int, combo []map[string]int) []string {
|
||||
if ppId <= 0 {
|
||||
return nil
|
||||
@@ -433,18 +501,11 @@ func (s *Service) ComboDeviationWarnings(ctx context.Context, ppId int, combo []
|
||||
warns = append(warns, fmt.Sprintf("工艺组合道数与产品工艺不符:产品工艺共 %d 道,组合包含 %d 道(缺 %d 道、多 %d 道)",
|
||||
len(items), have, missingCnt, extraCnt))
|
||||
}
|
||||
prevFirst := 0
|
||||
for _, it := range items {
|
||||
name := flowName[it.FlowId]
|
||||
sts := inCombo[it.FlowId]
|
||||
if len(sts) == 0 {
|
||||
if len(inCombo[it.FlowId]) == 0 {
|
||||
warns = append(warns, fmt.Sprintf("工艺组合缺少第%d道工艺「%s」", it.Seq, name))
|
||||
continue
|
||||
}
|
||||
if prevFirst > 0 && sts[0] <= prevFirst {
|
||||
warns = append(warns, fmt.Sprintf("工艺组合顺序与产品工艺不一致:第%d道「%s」(工位%d) 不在上一道工艺之后", it.Seq, name, sts[0]))
|
||||
}
|
||||
prevFirst = sts[0]
|
||||
}
|
||||
for fid, sts := range inCombo {
|
||||
if !ppFlowSet[fid] {
|
||||
@@ -454,6 +515,60 @@ func (s *Service) ComboDeviationWarnings(ctx context.Context, ppId int, combo []
|
||||
return warns
|
||||
}
|
||||
|
||||
// ComboOrderConflict 工单工艺组合顺序硬校验(2026-10-20 客户裁定:顺序不符直接拦截保存,不再"已保存+偏差提示")。
|
||||
// 口径:工单工艺顺序以「产品工艺定义的工序顺序(seq)」为准,不再按工位号递增提示;
|
||||
// 校验规则 = 组合中各工艺按其最小工位号排序后,出现顺序必须与产品工艺 seq 顺序一致
|
||||
//(生产按工位号顺序流转,若第2道工艺的工位全在第1道之前,现场将出现倒序流转,故拦截)。
|
||||
// 未选产品工艺 / 组合为空 / 产品工艺已删除 时不拦(建单不强制组合,排产/开工前另有校验)。
|
||||
// 返回空串=通过;非空=人话报错文案。
|
||||
func (s *Service) ComboOrderConflict(ctx context.Context, ppId int, combo []map[string]int) string {
|
||||
ppName := ""
|
||||
if ppId <= 0 || len(combo) == 0 {
|
||||
return ""
|
||||
}
|
||||
if pp, err := s.ctx.EntClient.ProductProcess.Get(ctx, ppId); err != nil {
|
||||
return ""
|
||||
} else {
|
||||
ppName = pp.Name
|
||||
}
|
||||
items, _ := s.ctx.EntClient.ProductProcessItem.Query().
|
||||
Where(productprocessitem.ProductProcessId(ppId)).
|
||||
Order(ent.Asc(productprocessitem.FieldSeq)).All(ctx)
|
||||
if len(items) == 0 {
|
||||
return ""
|
||||
}
|
||||
flows, _ := s.ctx.EntClient.ProcessFlow.Query().All(ctx)
|
||||
flowName := map[int]string{}
|
||||
for _, f := range flows {
|
||||
flowName[f.ID] = f.Name
|
||||
}
|
||||
inCombo := map[int][]int{}
|
||||
for _, c := range combo {
|
||||
fid, no := c["flowId"], c["stationNo"]
|
||||
if fid > 0 && no > 0 {
|
||||
inCombo[fid] = append(inCombo[fid], no)
|
||||
}
|
||||
}
|
||||
for k := range inCombo {
|
||||
sort.Ints(inCombo[k])
|
||||
}
|
||||
prevSeq, prevName, prevStation := 0, "", 0
|
||||
for _, it := range items {
|
||||
sts := inCombo[it.FlowId]
|
||||
if len(sts) == 0 {
|
||||
continue // 缺道属柔性警告(保存后提示),不在硬拦截范围
|
||||
}
|
||||
name := flowName[it.FlowId]
|
||||
if prevStation > 0 && sts[0] <= prevStation {
|
||||
return fmt.Sprintf(
|
||||
"工单工艺顺序与产品工艺不一致,无法保存:第%d道工艺「%s」的首个工位(工位%d)排在第%d道工艺「%s」(工位%d)之前或相同。生产按工位号顺序流转,请调整工艺组合使其与产品工艺「%s」的工序顺序一致后再保存。",
|
||||
it.Seq, name, sts[0], prevSeq, prevName, prevStation, ppName)
|
||||
}
|
||||
prevSeq, prevName, prevStation = it.Seq, name, sts[0]
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// productProcessNamesByFlow 反查:工艺ID → 引用它的产品工艺名称列表(工位绑定/工艺列表只读展示用)
|
||||
func (s *Service) productProcessNamesByFlow(ctx context.Context) map[int][]string {
|
||||
items, _ := s.ctx.EntClient.ProductProcessItem.Query().All(ctx)
|
||||
|
||||
Reference in New Issue
Block a user