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
+90 -54
View File
@@ -28,6 +28,15 @@ import (
func itoa(n int) string { return strconv.Itoa(n) }
// joinIntsLocal 整数切片转逗号串(reonline 候选工位提示用,避免跨包依赖)。
func joinIntsLocal(ns []int) string {
p := make([]string, 0, len(ns))
for _, v := range ns {
p = append(p, itoa(v))
}
return strings.Join(p, ",")
}
// evalLogic 依据考核逻辑判定
func evalLogic(c *ent.StepCriterion, v float64) bool {
switch c.Logic {
@@ -210,17 +219,15 @@ func (s *Service) onlineNewWorkpiece(ctx context.Context, wp *ent.Workpiece, req
if len(items) == 0 {
return "", nil, errors.New("工单未配置工艺组合")
}
first := items[0].StationNo
for _, it := range items {
if it.StationNo < first {
first = it.StationNo
}
}
if req.StationNo <= 0 {
// 并行候选工位:新件建档可落在「首道工序」的任一候选工位(闲着的工位去哪个)
route := groupFlowRoute(items)
firstStations := route[0].Stations
if req.StationNo == 0 {
return "", nil, errors.New("建档落点工位必填")
}
if req.StationNo != first {
return "", nil, fmt.Errorf("新件建档必须落在工序1工位(工位%d),当前落点工位%d不符合资格", first, req.StationNo)
if !intsContains(firstStations, req.StationNo) {
return "", nil, fmt.Errorf("新件建档必须落在第1道工序的候选工位(%s)之一,当前落点工位%d不符合资格",
joinIntsLocal(firstStations), req.StationNo)
}
station := req.StationNo
if _, e := s.ctx.EntClient.Workpiece.UpdateOneID(wp.ID).
@@ -252,42 +259,51 @@ func (s *Service) reonlineWorkpiece(ctx context.Context, wp *ent.Workpiece, req
if len(items) == 0 {
return "", nil, errors.New("工单未配置工艺组合")
}
next, nextFlow := 0, 0
for _, it := range items {
if it.StationNo > wp.CurrentStationNo && (next == 0 || it.StationNo < next) {
next, nextFlow = it.StationNo, it.FlowId
route := groupFlowRoute(items)
nextSeq, doneAll := s.firstUndoneFlowSeq(ctx, s.ctx.EntClient, wp.Sn, route)
if doneAll {
return "", nil, fmt.Errorf("工件已报工至最后一道工序,工艺组合中无下一站")
}
var nextFlow *FlowRouteStep
for i := range route {
if route[i].Seq == nextSeq {
nextFlow = &route[i]
break
}
}
if next == 0 {
return "", nil, fmt.Errorf("工件已报工至工位%d,工艺组合中无下一站", wp.CurrentStationNo)
}
// 二.4 资格校验:半成品(SEMI_STORED)重上线落点须为后续工序工位(=下一站);落点未传时按下一站执行
if req.StationNo > 0 && req.StationNo != next {
return "", nil, fmt.Errorf("半成品重上线须落在后续工序工位(工位%d),当前落点工位%d不符合资格", next, req.StationNo)
next := nextFlow.MinNo
// 二.4 资格校验:半成品(SEMI_STORED)重上线落点须为下一工序的候选工位之一;落点未传时按下一工序最小工位执行
if req.StationNo > 0 && !intsContains(nextFlow.Stations, req.StationNo) {
return "", nil, fmt.Errorf("半成品重上线须落在下一工序(候选工位%s)内,当前落点工位%d不符合资格",
joinIntsLocal(nextFlow.Stations), req.StationNo)
}
if err := s.SemiFlow(ctx, SemiReq{Sn: wp.Sn, OrderNo: wp.OrderNo, Action: "OUTBOUND"}, operator); err != nil {
return "", nil, fmt.Errorf("重上线失败:%s", err.Error())
}
// 出库一次解锁一次(问题.md 一.5):置回 ONLINE,写物理位置=下一站,等待在所选工位重新上线建档。
// 出库一次解锁一次(问题.md 一.5):置回 ONLINE,写物理位置=所选落点(缺省下一工序最小工位),等待在所选工位重新上线建档。
land := req.StationNo
if land == 0 {
land = next
}
if _, e := s.ctx.EntClient.Workpiece.UpdateOneID(wp.ID).
SetStatus("ONLINE").SetAtStationNo(next).Save(ctx); e != nil {
SetStatus("ONLINE").SetAtStationNo(land).Save(ctx); e != nil {
return "", nil, fmt.Errorf("重上线解锁状态失败:%s", e.Error())
}
// 二.2 物理位置 + 三.2 状态流水 SEMI_STORED→ONLINE(出库 SEMI_OUT)
s.writeLifecycle(ctx, wp.Sn, wp.OrderNo, "SEMI_STORED", "ONLINE", next, 0, operator, "workstation", "半成品出库重上线")
msg := fmt.Sprintf("重上线成功,继续执行:工位%d·%s", next, s.flowNameById(ctx, nextFlow))
if s.GetStationPaused(ctx, next) {
msg += "(注意:工位" + itoa(next) + "已停止接单)"
s.writeLifecycle(ctx, wp.Sn, wp.OrderNo, "SEMI_STORED", "ONLINE", land, 0, operator, "workstation", "半成品出库重上线")
msg := fmt.Sprintf("重上线成功,继续执行:工位%d·%s", land, s.flowNameById(ctx, nextFlow.FlowId))
if s.GetStationPaused(ctx, land) {
msg += "(注意:工位" + itoa(land) + "已停止接单)"
}
s.ctx.EventLog.Write(ctx, "workpiece.reonline", wp.OrderNo, operator, "workpiece", wp.Sn,
"工件重上线(暂存退库后出库继续)", map[string]any{"nextStationNo": next, "nextFlowId": nextFlow, "pointNo": req.PointNo})
"工件重上线(暂存退库后出库继续)", map[string]any{"nextStationNo": land, "nextFlowId": nextFlow.FlowId, "pointNo": req.PointNo})
// 工件当前位置落账(点位台账:重上线即占位)
if err := s.occupyStartPoint(ctx, req.PointNo, wp.Sn, wp.OrderNo, operator); err != nil {
return "", nil, err
}
s.notifyDashboard()
// PLC↔MES 对账(#8 T5 建档提交):提示不拦,仅透出 mismatch 供终端弹窗
mm, _ := s.detectAndRecord(ctx, next, "terminal", wp.OrderNo, operator)
mm, _ := s.detectAndRecord(ctx, land, "terminal", wp.OrderNo, operator)
return msg, mm, nil
}
@@ -385,7 +401,7 @@ func parseClientTime(s string) (time.Time, error) {
// 电气件按单台用量逐颗SN、结构件至少一个批次号;缺料直接拒绝报工并返回缺料清单。
// 顺序校验(工单工艺组合为唯一路线源头):报工工位必须在组合中、工艺匹配,且不可倒序(可跳过未选工位)。
func (s *Service) ReportProcess(ctx context.Context, req ReportProcessReq, operator string, operatorId int) error {
if req.Sn == "" || req.StationNo <= 0 {
if req.Sn == "" || req.StationNo == 0 {
return errors.New("sn 与 stationNo 必填")
}
// PLC↔MES 对账(#8 T4 报工提交):命中 mismatch 则阻断报工,提示先处理该工位托盘位异常
@@ -420,23 +436,33 @@ func (s *Service) ReportProcess(ctx context.Context, req ReportProcessReq, opera
if err != nil || wo == nil {
return errors.New("工件所属工单不存在")
}
items, _ := RouteStationsFromWO(ctx, s.ctx.EntClient, wo)
matched := false
for _, it := range items {
if it.StationNo == req.StationNo {
matched = true
if it.FlowId != flowId {
return fmt.Errorf("工位%d当前工艺与工单工艺组合不匹配", req.StationNo)
}
// 并行候选工位模型(2026-11-02):同工序可配置多个候选工位,工件在任一候选工位报工一次即完成该工序;
// 不可跳工序(须按工序顺序),同工序已报工则不可重复报工。
route, _ := FlowRouteOfWO(ctx, s.ctx.EntClient, wo)
if len(route) == 0 {
return errors.New("工单未配置工艺组合")
}
var cur *FlowRouteStep
for i := range route {
if route[i].FlowId == flowId && intsContains(route[i].Stations, req.StationNo) {
cur = &route[i]
break
}
}
if !matched {
return fmt.Errorf("工位%d不在该工单的工艺组合中,不可报工", req.StationNo)
if cur == nil {
return fmt.Errorf("工位%d不在该工序「%s」的候选工位中,不可报工", req.StationNo, s.flowNameById(ctx, flowId))
}
if req.StationNo <= wp.CurrentStationNo {
return errors.New("不可倒序报工:工件已报工到工位" + itoa(wp.CurrentStationNo) +
",不能在工位" + itoa(req.StationNo) + "重复/回退报工")
// 不可跳工序 / 不可重复:本工序必须是第一个「尚未有 DONE 报工」的工序
nextSeq, doneAll := s.firstUndoneFlowSeq(ctx, s.ctx.EntClient, req.Sn, route)
if doneAll {
return errors.New("该工件所有工序已报工完成")
}
if cur.Seq != nextSeq {
if done, _ := s.ctx.EntClient.WorkpieceProcess.Query().
Where(workpieceprocess.Sn(req.Sn), workpieceprocess.FlowId(flowId), workpieceprocess.Status("DONE")).Exist(ctx); done {
return errors.New("该工序已报工完成,不可重复报工")
}
return errors.New("不可跳工序:请先完成上一道工序")
}
// 报工是"装机绑定→校验齐套→写实绩→写步骤考核→回写结果/工件状态"的多步写,必须整体事务化(方案 U3):
// 任一步失败全部回滚,杜绝"实绩已建但工件状态没推进/结果没落"的半成品脏数据;
@@ -468,7 +494,8 @@ func (s *Service) ReportProcess(ctx context.Context, req ReportProcessReq, opera
// 阶段2:检测确认(needCheck)强校验——服务端为准,前端勾选只是辅助提示。
// 本工位在本工艺组合中的出现序号 k 对应步骤 seq=k,标记“需要检测确认”的步骤须带 checked=true。
// k 由组合派生(第 k 次出现 ↔ 步骤 seq=k),与签字校验同源,避免跨工位误拦。
k := stationSeqInFlow(items, flowId, req.StationNo)
// 并行候选工位:每道工序只报工一次(对应一次步骤序号),与历史「1工序1工位」语义一致,k 恒为 1
k := 1
if err := s.validateNeedCheck(ctx, client, req, flowId, k); err != nil {
return err
}
@@ -605,6 +632,19 @@ func stationSeqInFlow(items []FlowItem, flowId, stationNo int) int {
return k
}
// firstUndoneFlowSeq 返回工件尚未完成(无 DONE 报工记录)的第一道工序序号(route.Seq);
// 全部完成返回 (0, true)。并行候选工位模型下,工序完成以「该工序任一候选工位已报工 DONE」判定。
func (s *Service) firstUndoneFlowSeq(ctx context.Context, client *ent.Client, sn string, route []FlowRouteStep) (int, bool) {
for _, f := range route {
done, _ := client.WorkpieceProcess.Query().
Where(workpieceprocess.Sn(sn), workpieceprocess.FlowId(f.FlowId), workpieceprocess.Status("DONE")).Exist(ctx)
if !done {
return f.Seq, false
}
}
return 0, true
}
// validateNeedSign 签字机制(需求B)服务端强校验,返回本工位在本工艺组合中的出现序号 k:
// 组合条目(按工位号升序)与工艺步骤 seq 一一对应——本工艺在组合中第 k 次出现 ↔ 步骤 seq=k。
// - needOperatorSign:仅校验「本次报工步骤」(seq=k),报工请求必须携带对应操作工签字;
@@ -773,7 +813,7 @@ func (s *Service) DoneWorkpiece(ctx context.Context, req DoneReq, operator strin
// 二.2 物理位置:完工/入库均清空 at_station(件已离产线/入半成品库)。
// 三.2 入库次数统一 count(action='SEMI_IN'),不再依赖 semiflow(CountSemiStored 已删)。
routeItems, _ := RouteStationsFromWO(ctx, s.ctx.EntClient, wo)
routeCount := len(routeItems)
routeCount := DistinctFlowCount(routeItems) // 并行候选工位:按工序数判定,而非工位数
reported := 0
if routeCount > 0 {
reported, _ = s.ctx.EntClient.WorkpieceProcess.Query().
@@ -1063,21 +1103,17 @@ func (s *Service) WorkpiecesInProcess(ctx context.Context, stationNo int, orderN
return wo
}
for _, wp := range list {
// 路线 = 工单工艺组合(唯一路线源头);未配置组合则无路线(maxStation=0)
// 路线 = 工单工艺组合(唯一路线源头);并行候选工位按工序级聚合
items, _ := RouteStationsFromWO(ctx, s.ctx.EntClient, getWO(wp))
seq := RouteStationsOfWO(items)
maxStation := 0
for _, c := range seq {
if c > maxStation {
maxStation = c
}
}
route := groupFlowRoute(items)
total := DistinctFlowCount(items)
// 显示口径按 status 派生(原 0/13 魔法值已废除,业务定稿·一.10):
// 待流转 = ONLINE 未报工(未分配工位)
// 末端待取件 = PROCESSING 且已报工至工艺路线末端工位
// 末端待取件 = PROCESSING 且所有工序已报工完成
// 其余 = 真实工位显示名
cur := wp.CurrentStationNo
atEnd := wp.Status == "PROCESSING" && maxStation > 0 && cur >= maxStation
_, doneAll := s.firstUndoneFlowSeq(ctx, s.ctx.EntClient, wp.Sn, route)
atEnd := wp.Status == "PROCESSING" && doneAll
switch scope {
case "pending":
if wp.Status != "ONLINE" || cur > 0 {
@@ -1133,7 +1169,7 @@ func (s *Service) WorkpiecesInProcess(ctx context.Context, stationNo int, orderN
OnlineAt: onlineAt,
DwellSec: dwell,
DoneSteps: done,
TotalSteps: len(seq),
TotalSteps: total,
})
}
return out, nil