package logic import ( "context" "errors" "fmt" "sort" "strconv" "strings" "time" "bj_power_mes/ent" "bj_power_mes/ent/productprocessitem" "bj_power_mes/ent/station" "bj_power_mes/internal/verr" ) // ---------- 工单工艺组合(work_order.flow_combination)路线派生 ---------- // TodayStr 当天日期 yyyy-MM-dd(工位停单只认当天) func TodayStr() string { return time.Now().Format("2006-01-02") } // FlowItem 工单工艺组合条目:工艺 + 工位(工位号即顺序号,按工位号升序执行) type FlowItem struct { FlowId int `json:"flowId"` StationNo int `json:"stationNo"` } // RouteStationsFromWO 解析工单 flow_combination → 组合条目(按工位号升序;空组合返回空切片) func RouteStationsFromWO(ctx context.Context, client *ent.Client, wo *ent.WorkOrder) ([]FlowItem, error) { if wo == nil { return nil, nil } items := flowMapsToItems(wo.FlowCombination) sort.SliceStable(items, func(i, j int) bool { return items[i].StationNo < items[j].StationNo }) return items, nil } // flowMapsToItems jsonb 原始值 → 组合条目(过滤 flowId/stationNo 非正的脏条目) func flowMapsToItems(raw []map[string]int) []FlowItem { items := make([]FlowItem, 0, len(raw)) for _, m := range raw { flowId, no := m["flowId"], m["stationNo"] if flowId > 0 && no > 0 { items = append(items, FlowItem{FlowId: flowId, StationNo: no}) } } return items } // flowItemsToMaps 组合条目 → jsonb 存储结构 func flowItemsToMaps(items []FlowItem) []map[string]int { out := make([]map[string]int, 0, len(items)) for _, it := range items { out = append(out, map[string]int{"flowId": it.FlowId, "stationNo": it.StationNo}) } return out } // RouteStationsOfWO 组合条目 → 工位号列表(升序;未选工位即跳过) func RouteStationsOfWO(items []FlowItem) []int { out := make([]int, 0, len(items)) for _, it := range items { out = append(out, it.StationNo) } return out } // RouteStationsStrOfWO 组合条目 → 路线串 "2,5,6,8" func RouteStationsStrOfWO(items []FlowItem) string { parts := make([]string, 0, len(items)) for _, it := range items { parts = append(parts, strconv.Itoa(it.StationNo)) } return strings.Join(parts, ",") } // StationFlowMapOfWO 组合条目 → 工位号→工艺ID func StationFlowMapOfWO(items []FlowItem) map[int]int { m := make(map[int]int, len(items)) for _, it := range items { m[it.StationNo] = it.FlowId } return m } // ValidateFlowCombination 保存工单时校验工艺组合(收集式实现,见 ValidateFlowCombinationCollect; // 保留原签名供既有调用方使用,错误为收集式 ValidationError,handler 经 FailErr 输出结构化清单)。 func (s *Service) ValidateFlowCombination(ctx context.Context, raw []map[string]int) ([]FlowItem, error) { items, ve := s.ValidateFlowCombinationCollect(ctx, raw) return items, ve.OrNil() } // ValidateFlowCombinationCollect 收集式组合校验(报错一次收全,2026-10-20): // 逐条检查每个组合项,重复/停用/不存在/绑定不匹配等问题全部列出、不串行 return; // 每条错误带「怎么办」,能跳转解决的给 action(去工艺页/去工位管理)。 func (s *Service) ValidateFlowCombinationCollect(ctx context.Context, raw []map[string]int) ([]FlowItem, *verr.ValidationError) { items := flowMapsToItems(raw) ve := verr.New() if len(items) == 0 { ve.Add("flowCombination", "请先选择工艺组合", "至少选择一条「工艺+工位」:从产品工艺带出,或在工单编辑中手动勾选") return nil, ve } sort.SliceStable(items, func(i, j int) bool { return items[i].StationNo < items[j].StationNo }) seen := map[int]bool{} for _, it := range items { if seen[it.StationNo] { ve.Add("flowCombination", fmt.Sprintf("工位 %d 在工艺组合中重复", it.StationNo), fmt.Sprintf("同一工位只能出现一次,删除工位 %d 的重复行", it.StationNo)) continue } seen[it.StationNo] = true flow, err := s.ctx.EntClient.ProcessFlow.Get(ctx, it.FlowId) if err != nil { ve.Add("flowCombination", fmt.Sprintf("工艺不存在(ID=%d)", it.FlowId), "删除该组合行后,从工艺下拉中重新选择") continue } if flow.Status != "ACTIVE" { ve.AddAction("flowCombination", fmt.Sprintf("工艺「%s」已停用", flow.Name), "到「工艺」页启用该工艺,或在组合中移除该工艺行", "去工艺页", "/process-flow") continue } exist, err := s.ctx.EntClient.Station.Query().Where(station.StationNo(it.StationNo)).First(ctx) if err != nil { ve.Add("flowCombination", fmt.Sprintf("工位 %d 不存在", it.StationNo), "在组合中移除该工位行,或到「工位管理」新增该工位") continue } // 工位当前绑定工艺必须与工单一致且工位启用(与排产校验 ValidateWOCombinationForSchedule 同口径), // 否则工单下发后工位被改绑/停用会导致路线漂移 if exist.Status != "ENABLED" { ve.AddAction("flowCombination", fmt.Sprintf("工位%d 已停用", it.StationNo), "到「工位管理」启用该工位,或在组合中移除该工位行", "去工位管理", "/station") } else if exist.FlowId != it.FlowId { ve.AddAction("flowCombination", fmt.Sprintf("工位%d 当前绑定工艺与工单不一致", it.StationNo), fmt.Sprintf("到「工位管理」把工位%d 绑回「%s」,或在组合中改选该工位当前绑定的工艺", it.StationNo, flow.Name), "去工位管理", "/station") } } 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 { items, _ := RouteStationsFromWO(ctx, s.ctx.EntClient, wo) if len(items) == 0 { return errors.New("请先在工单中选择工艺组合,才能排产") } for _, it := range items { st, err := s.ctx.EntClient.Station.Query().Where(station.StationNo(it.StationNo)).First(ctx) if err != nil { return fmt.Errorf("工位 %d 不存在", it.StationNo) } if st.FlowId != it.FlowId || st.Status != "ENABLED" { return fmt.Errorf("工位%d当前工艺与工单不匹配", it.StationNo) } } return nil } // flowNameById 工艺名称(查不到回退「工艺{id}」) func (s *Service) flowNameById(ctx context.Context, id int) string { if id > 0 { if f, err := s.ctx.EntClient.ProcessFlow.Get(ctx, id); err == nil && f.Name != "" { return f.Name } } return "工艺" + strconv.Itoa(id) }