feat: 大屏多订单改为同屏并列展示,物料申请新增跨工位借料来源并移除工单完成日期

This commit is contained in:
SunYF
2026-11-02 19:17:05 +08:00
parent 0c03d32682
commit fd2f0e2767
77 changed files with 1773 additions and 664 deletions
+142 -15
View File
@@ -12,6 +12,7 @@ import (
"bj_power_mes/ent"
"bj_power_mes/ent/productprocessitem"
"bj_power_mes/ent/station"
"bj_power_mes/ent/workorder"
"bj_power_mes/internal/verr"
)
@@ -36,12 +37,13 @@ func RouteStationsFromWO(ctx context.Context, client *ent.Client, wo *ent.WorkOr
return items, nil
}
// flowMapsToItems jsonb 原始值 → 组合条目(过滤 flowId/stationNo 非正的脏条目)
// flowMapsToItems jsonb 原始值 → 组合条目(过滤 flowId 非正 / stationNo 为 0 的脏条目;
// stationNo 允许负数:负数=产线前段虚拟工位,2026-11-02)
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 {
if flowId > 0 && no != 0 {
items = append(items, FlowItem{FlowId: flowId, StationNo: no})
}
}
@@ -84,6 +86,105 @@ func StationFlowMapOfWO(items []FlowItem) map[int]int {
return m
}
// ---------- 并行候选工位:工序级路线(一道工序可配置多个候选工位) ----------
// FlowRouteStep 工序级路线步骤:一道工序(工艺) + 其全部候选工位。
// 并行模型(2026-11-02):工件在任一候选工位报工一次即完成该工序,候选工位间为「可任选」关系而非串行顺序。
type FlowRouteStep struct {
FlowId int // 工艺ID
Stations []int // 候选工位号(升序)
MinNo int // 候选最小工位号(用于工序顺序)
MaxNo int // 候选最大工位号(用于进度推进)
Seq int // 工序顺序(按 MinNo 升序,从1起)
}
// groupFlowRoute 扁平组合条目 → 工序级路线(同 flowId 归并为一道工序,多工位为其候选集)。
// 工序顺序按各工序候选最小工位号升序(产线单向不回头)。
func groupFlowRoute(items []FlowItem) []FlowRouteStep {
byFlow := map[int][]int{}
for _, it := range items {
byFlow[it.FlowId] = append(byFlow[it.FlowId], it.StationNo)
}
steps := make([]FlowRouteStep, 0, len(byFlow))
for fid, sts := range byFlow {
sort.Ints(sts)
steps = append(steps, FlowRouteStep{FlowId: fid, Stations: sts, MinNo: sts[0], MaxNo: sts[len(sts)-1]})
}
sort.SliceStable(steps, func(i, j int) bool { return steps[i].MinNo < steps[j].MinNo })
for i := range steps {
steps[i].Seq = i + 1
}
return steps
}
// FlowRouteOfWO 工单 flow_combination → 工序级路线(含候选工位)。
func FlowRouteOfWO(ctx context.Context, client *ent.Client, wo *ent.WorkOrder) ([]FlowRouteStep, error) {
if wo == nil {
return nil, nil
}
return groupFlowRoute(flowMapsToItems(wo.FlowCombination)), nil
}
// GroupFlowRoute 导出版:扁平组合条目 → 工序级路线(handler 包渲染流程卡用)。
func GroupFlowRoute(items []FlowItem) []FlowRouteStep {
return groupFlowRoute(items)
}
// DistinctFlowCount 路线中工序(工艺)去重计数:并行模型下完工/进度按「工序数」而非「工位数」判定。
func DistinctFlowCount(items []FlowItem) int {
m := map[int]bool{}
for _, it := range items {
m[it.FlowId] = true
}
return len(m)
}
// intsContains 整数切片包含判断(候选工位校验用)。
func intsContains(ns []int, v int) bool {
for _, n := range ns {
if n == v {
return true
}
}
return false
}
// FlowUsedByActiveOrders 工艺是否被「执行中/已暂停」工单的工艺组合引用(2026-11-02 条目12 守卫)。
// 背景:工单 flow_combination 是建单快照,但生产流程卡的定义层(工序清单/步骤门禁)每次
// 请求实时渲染 ProcessFlow/ProcessStep——改基础工艺会改掉在产工单的流程卡展示。
// 与产品工艺规则1同口径:执行中禁改,建议复制新工艺给新工单用。命中返回工单号列表。
func (s *Service) FlowUsedByActiveOrders(ctx context.Context, flowId int) ([]string, error) {
wos, err := s.ctx.EntClient.WorkOrder.Query().
Where(workorder.StatusIn("IN_PROGRESS", "PAUSED")).All(ctx)
if err != nil {
return nil, err
}
var hit []string
for _, w := range wos {
for _, m := range w.FlowCombination {
if m["flowId"] == flowId {
hit = append(hit, w.WorkOrderNo)
break
}
}
}
return hit, nil
}
// guardFlowEditable 基础工艺内容(名称/步骤/绑定/物料清单/删除)变更守卫:
// 被执行中/已暂停工单引用 → 拒绝并给出「怎么办」。
func (s *Service) guardFlowEditable(ctx context.Context, flowId int) error {
hits, err := s.FlowUsedByActiveOrders(ctx, flowId)
if err != nil {
return err
}
if len(hits) > 0 {
return fmt.Errorf("该工艺被执行中/已暂停的工单引用(%s),不能修改或删除;如需调整请复制一份新工艺,新工单使用新工艺",
strings.Join(hits, "、"))
}
return nil
}
// ValidateFlowCombination 保存工单时校验工艺组合(收集式实现,见 ValidateFlowCombinationCollect;
// 保留原签名供既有调用方使用,错误为收集式 ValidationError,handler 经 FailErr 输出结构化清单)。
func (s *Service) ValidateFlowCombination(ctx context.Context, raw []map[string]int) ([]FlowItem, error) {
@@ -166,18 +267,19 @@ func (s *Service) ValidateWorkOrderComboStrict(ctx context.Context, ppId int, co
for _, f := range flows {
flowName[f.ID] = f.Name
}
// 组合按 flowId 归并(正常每道一条;后端不信任前端,取最小工位号代表该道)
// 组合按 flowId 归并(一道工序可配置多个有序工位:基于「产线单向不回头」,
// 本道工序的全部工位必须都在上一道工序的全部工位之后,即各工序的工位区间互不交叉、整体严格递增)。
byFlow := map[int][]int{}
for _, c := range combo {
fid, no := c["flowId"], c["stationNo"]
if fid > 0 && no > 0 {
if fid > 0 && no != 0 {
byFlow[fid] = append(byFlow[fid], no)
}
}
for k := range byFlow {
sort.Ints(byFlow[k])
}
prevSeq, prevName, prevStation := 0, "", 0
prevName, prevMax := "", 0
for _, it := range items {
name := flowName[it.FlowId]
if name == "" {
@@ -188,23 +290,48 @@ func (s *Service) ValidateWorkOrderComboStrict(ctx context.Context, ppId int, co
// 规则①:该道工序未选工位
ve.Add("flowCombination",
fmt.Sprintf("第%d道工序「%s」未选工位", it.Seq, name),
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))
// 同工序内重复工位(脏数据)拦截
seen := map[int]bool{}
dup := false
for _, n := range sts {
if seen[n] {
dup = true
break
}
seen[n] = true
}
prevSeq, prevName, prevStation = it.Seq, name, sel
if dup {
ve.Add("flowCombination",
fmt.Sprintf("第%d道工序「%s」的工位列表存在重复(工位%d)", it.Seq, name, sts[0]),
fmt.Sprintf("请在第%d道工序「%s」中去重工位", it.Seq, name))
continue
}
minS, maxS := sts[0], sts[len(sts)-1]
if prevMax > 0 && minS <= prevMax {
// 规则②:本道工序工位区间未整体在上一道之后(产线单向不能回头 / 工序工位不可交叉)
ve.Add("flowCombination",
fmt.Sprintf("第%d道工序「%s」的工位(%s)未整体排在上一道工序「%s」(工位%d)之后",
it.Seq, name, strings.Join(intsToStrs(sts), ","), prevName, prevMax),
fmt.Sprintf("产线单向不能回头:请把工序「%s」的工位都改到工位%d 之后,或调整工序顺序",
name, prevMax))
}
prevName, prevMax = name, maxS
}
return ve
}
// intsToStrs 整数切片转字符串切片(校验消息用)
func intsToStrs(ns []int) []string {
out := make([]string, 0, len(ns))
for _, n := range ns {
out = append(out, "工位"+strconv.Itoa(n))
}
return out
}
// ValidateWOCombinationForSchedule 排产前校验(工单组合是唯一路线源头,排产只继承不可改):
// 组合为空不可排产;组合里每个工位当前绑定工艺必须与工单一致且工位启用。
func (s *Service) ValidateWOCombinationForSchedule(ctx context.Context, wo *ent.WorkOrder) error {