feat: 工单工艺组合改为按工序工位严格校验

- 新增 ValidateWorkOrderComboStrict 方法,实现工序工位硬校验规则
- 删除旧的 ComboOrderConflict 顺序校验方法
- 前端 UI 改为工序工位选择模式,支持动态推荐和约束
- 更新文档说明,同步工艺组合到工序工位的业务规则
This commit is contained in:
SunYF
2026-10-21 09:55:31 +08:00
parent b0c048d9aa
commit 4b14f9540a
9 changed files with 433 additions and 384 deletions
+1 -55
View File
@@ -449,7 +449,7 @@ func (s *Service) DeriveProductProcess(ctx context.Context, productCode string,
// ComboDeviationWarnings 工单工艺组合与产品工艺的偏差柔性警告(仅提示,不阻断保存):
// 覆盖/个数——产品工艺的每道工艺都须出现在组合中,组合不得包含产品工艺之外的工艺。
// 顺序偏差已于 2026-10-20 客户裁定升级为硬拦截(见 ComboOrderConflict),此处不再重复提示。
// 顺序偏差已于 2026-10-20 客户裁定升级为硬拦截(见 ValidateWorkOrderComboStrict),此处不再重复提示。
func (s *Service) ComboDeviationWarnings(ctx context.Context, ppId int, combo []map[string]int) []string {
if ppId <= 0 {
return nil
@@ -515,60 +515,6 @@ 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)
+65
View File
@@ -10,6 +10,7 @@ import (
"time"
"bj_power_mes/ent"
"bj_power_mes/ent/productprocessitem"
"bj_power_mes/ent/station"
"bj_power_mes/internal/verr"
)
@@ -140,6 +141,70 @@ func (s *Service) ValidateFlowCombinationCollect(ctx context.Context, raw []map[
return items, ve
}
// ValidateWorkOrderComboStrict 工单工艺组合「工序工位」严格校验(2026-10-20 工单建单需求:自动推荐+用户可改+动态约束)。
// 仅在带产品工艺(productProcessId>0)时生效;无产品工艺则交由既有逻辑(组合可选)。
// 三条硬规则,收集式一次列全(与 ValidateFlowCombinationCollect 合并返回):
// ① 每道工序都选了工位(产品工艺的每道工艺在组合中须有对应记录);
// ② 工位号按工序顺序严格递增(产线单向不回头,不允许倒序流转);
// ③ 每个工位确实绑定对应工序 —— 由 ValidateFlowCombinationCollect 同口径兜底(含停用/改绑/不存在,消息带「怎么办」)。
func (s *Service) ValidateWorkOrderComboStrict(ctx context.Context, ppId int, combo []map[string]int) *verr.ValidationError {
ve := verr.New()
if ppId <= 0 {
return ve
}
if _, err := s.ctx.EntClient.ProductProcess.Get(ctx, ppId); err != nil {
return ve // 产品工艺不存在时交给来源工艺存在性校验,避免重复
}
items, err := s.ctx.EntClient.ProductProcessItem.Query().
Where(productprocessitem.ProductProcessId(ppId)).
Order(ent.Asc(productprocessitem.FieldSeq)).All(ctx)
if err != nil || len(items) == 0 {
return ve
}
flows, _ := s.ctx.EntClient.ProcessFlow.Query().All(ctx)
flowName := map[int]string{}
for _, f := range flows {
flowName[f.ID] = f.Name
}
// 组合按 flowId 归并(正常每道一条;后端不信任前端,取最小工位号代表该道)
byFlow := map[int][]int{}
for _, c := range combo {
fid, no := c["flowId"], c["stationNo"]
if fid > 0 && no > 0 {
byFlow[fid] = append(byFlow[fid], no)
}
}
for k := range byFlow {
sort.Ints(byFlow[k])
}
prevSeq, prevName, prevStation := 0, "", 0
for _, it := range items {
name := flowName[it.FlowId]
if name == "" {
name = "工序" + strconv.Itoa(it.FlowId)
}
sts := byFlow[it.FlowId]
if len(sts) == 0 {
// 规则①:该道工序未选工位
ve.Add("flowCombination",
fmt.Sprintf("第%d道工序「%s」未选工位", it.Seq, name),
fmt.Sprintf("请为第%d道工序「%s」选择一个绑定了该工序的工位", it.Seq, name))
continue
}
sel := sts[0]
if prevStation > 0 && sel <= prevStation {
// 规则②:工位号未严格递增(产线单向不能回头)
ve.Add("flowCombination",
fmt.Sprintf("第%d道工序「%s」选的工位%d,不大于第%d道工序「%s」的工位%d",
it.Seq, name, sel, prevSeq, prevName, prevStation),
fmt.Sprintf("产线单向不能回头,请把工序「%s」的工位改为工位%d 之后,或调整工序「%s」的工位",
name, prevStation, prevName))
}
prevSeq, prevName, prevStation = it.Seq, name, sel
}
return ve
}
// ValidateWOCombinationForSchedule 排产前校验(工单组合是唯一路线源头,排产只继承不可改):
// 组合为空不可排产;组合里每个工位当前绑定工艺必须与工单一致且工位启用。
func (s *Service) ValidateWOCombinationForSchedule(ctx context.Context, wo *ent.WorkOrder) error {
+6 -6
View File
@@ -65,9 +65,9 @@ func (s *Service) CreateWorkOrder(ctx context.Context, req WorkOrderReq, operato
items, cve := s.ValidateFlowCombinationCollect(ctx, req.FlowCombination)
comboItems = items
ve.Merge(cve)
// 组合顺序硬校验(2026-10-20 客户裁定):与产品工艺工序顺序不一致直接拒绝,并入收集清单
if msg := s.ComboOrderConflict(ctx, req.ProductProcessId, req.FlowCombination); msg != "" {
ve.Add("flowCombination", msg, "调整组合里的工位顺序与产品工艺一致;或到「产品工艺」调整工序定义")
// 工序工位严格校验(2026-10-20 工单建单需求):每道工序都选、工位号严格递增(无产品工艺则交由既有逻辑)
if sve := s.ValidateWorkOrderComboStrict(ctx, req.ProductProcessId, req.FlowCombination); sve.Len() > 0 {
ve.Merge(sve)
}
}
// 来源产品工艺:传了必须存在(ACTIVE/停用均可落——前端只列启用的,此处宽松校验存在性)
@@ -257,9 +257,9 @@ func (s *Service) UpdateWorkOrder(ctx context.Context, req WorkOrderReq, operato
items, cve := s.ValidateFlowCombinationCollect(ctx, req.FlowCombination)
comboItems = items
ve.Merge(cve)
// 组合顺序硬校验(2026-10-20 客户裁定):并入收集清单
if msg := s.ComboOrderConflict(ctx, req.ProductProcessId, req.FlowCombination); msg != "" {
ve.Add("flowCombination", msg, "调整组合里的工位顺序与产品工艺一致;或到「产品工艺」调整工序定义")
// 工序工位严格校验(2026-10-20 工单建单需求):每道工序都选、工位号严格递增(无产品工艺则交由既有逻辑)
if sve := s.ValidateWorkOrderComboStrict(ctx, req.ProductProcessId, req.FlowCombination); sve.Len() > 0 {
ve.Merge(sve)
}
}
// 来源产品工艺:传了必须存在(恒保留=来源定义,不随组合手改清空)