feat: 新增工件生命周期状态流水表及工单状态流转逻辑
新增工件生命周期状态流水表,记录所有工件状态变更动作,替代原半成品质检计数源 调整工单状态流转,将「下发工单」与「开始执行」合并为「下发并开工」,自动启用关联排产 新增工件物理位置字段,区分业务位置与实际物理工位,解决工位对账滞后问题 统一物料类别处理,避免 WMS 同步时覆盖合法值或错误设置为 0 移除已废弃的返修状态,更新相关显示与逻辑
This commit is contained in:
@@ -80,6 +80,7 @@ type OnlineReq struct {
|
||||
Sn string `json:"sn"`
|
||||
OrderNo string `json:"orderNo"`
|
||||
WorkOrderId int `json:"workOrderId"`
|
||||
StationNo int `json:"stationNo"` // 建档落点工位(二.4/二.11);0/空=沿用旧虚拟位模型(终端 0/13 重构未完成时兼容)
|
||||
}
|
||||
|
||||
// OnlineWorkpiece 工件进线登记(返回提示文案,供响应透出)。
|
||||
@@ -100,10 +101,16 @@ func (s *Service) OnlineWorkpiece(ctx context.Context, req OnlineReq, operator s
|
||||
return "", err
|
||||
}
|
||||
if exist != nil {
|
||||
if exist.Status == "DONE" || exist.Status == "SCRAPPED" {
|
||||
switch exist.Status {
|
||||
case "DONE", "SCRAPPED":
|
||||
return "", errors.New("工件已完工/报废,不可重复上线登记")
|
||||
case "NEW":
|
||||
// 预生成 SN 首次上线建档:NEW→ONLINE,不涉及 WMS 出库;写物理位置+资格校验
|
||||
return s.onlineNewWorkpiece(ctx, exist, req, operator)
|
||||
default:
|
||||
// ONLINE/PROCESSING/SEMI_STORED:重上线继续(半成品出库解锁一次上线资格)
|
||||
return s.reonlineWorkpiece(ctx, exist, req, operator)
|
||||
}
|
||||
return s.reonlineWorkpiece(ctx, exist, operator)
|
||||
}
|
||||
}
|
||||
// 解析工单(sn_mode 挂在产品型号/物料档案上,必须先定位工单→产品)
|
||||
@@ -156,11 +163,13 @@ func (s *Service) OnlineWorkpiece(ctx context.Context, req OnlineReq, operator s
|
||||
}
|
||||
if err := s.ctx.EntClient.Workpiece.Create().
|
||||
SetSn(req.Sn).SetOrderNo(req.OrderNo).SetWorkOrderId(req.WorkOrderId).
|
||||
SetStatus("ONLINE").SetOnlineAt(time.Now()).Exec(ctx); err != nil {
|
||||
SetStatus("ONLINE").SetOnlineAt(time.Now()).SetAtStationNo(req.StationNo).Exec(ctx); err != nil {
|
||||
return "", err
|
||||
}
|
||||
// 二.2 物理位置:建档落点写入 at_station;三.2 状态流水 NEW→ONLINE
|
||||
s.writeLifecycle(ctx, req.Sn, req.OrderNo, "NEW", "ONLINE", req.StationNo, 0, operator, "workstation", "上线建档")
|
||||
s.ctx.EventLog.Write(ctx, "workpiece.online", req.OrderNo, operator, "workpiece", req.Sn,
|
||||
"工件进线登记", map[string]any{"snMode": snMode,
|
||||
"工件进线登记", map[string]any{"snMode": snMode, "stationNo": req.StationNo,
|
||||
"snSource": map[int]string{1: "SCANNED", 2: "AUTO"}[snMode]})
|
||||
s.notifyDashboard()
|
||||
return "进线登记成功", nil
|
||||
@@ -173,7 +182,47 @@ func (s *Service) OnlineWorkpiece(ctx context.Context, req OnlineReq, operator s
|
||||
// 顺序保证:校验全部通过后才产生出库这一副作用,避免"已出库却登记失败"的悬挂状态;
|
||||
//
|
||||
// ③ 下一站工位当天停单时提示"工位N已停止接单"(复用 GetStationPaused 既有停单判定,不阻断登记)。
|
||||
func (s *Service) reonlineWorkpiece(ctx context.Context, wp *ent.Workpiece, operator string) (string, error) {
|
||||
// onlineNewWorkpiece 预生成 SN 首次上线建档(NEW→ONLINE)。
|
||||
// 二.4 资格校验:新件必须落点在工序1工位(工艺组合中工位号最小者);落点未传(终端 0/13 重构未完成)时放行但不校验。
|
||||
// 不涉及 WMS 出库(从未入半成品库);写物理位置 at_station=落点,记生命周期 NEW→ONLINE。
|
||||
func (s *Service) onlineNewWorkpiece(ctx context.Context, wp *ent.Workpiece, req OnlineReq, operator string) (string, error) {
|
||||
wo := s.workpieceWorkOrder(ctx, wp)
|
||||
items, _ := RouteStationsFromWO(ctx, s.ctx.EntClient, wo)
|
||||
if len(items) == 0 {
|
||||
return "", errors.New("工单未配置工艺组合")
|
||||
}
|
||||
first := items[0].StationNo
|
||||
for _, it := range items {
|
||||
if it.StationNo < first {
|
||||
first = it.StationNo
|
||||
}
|
||||
}
|
||||
if req.StationNo > 0 && req.StationNo != first {
|
||||
return "", fmt.Errorf("新件建档必须落在工序1工位(工位%d),当前落点工位%d不符合资格", first, req.StationNo)
|
||||
}
|
||||
station := req.StationNo
|
||||
if station == 0 {
|
||||
station = first // 终端未传落点时按工序1兜底,避免 at_station 失真
|
||||
}
|
||||
if _, e := s.ctx.EntClient.Workpiece.UpdateOneID(wp.ID).
|
||||
SetStatus("ONLINE").SetOnlineAt(time.Now()).SetAtStationNo(station).Save(ctx); e != nil {
|
||||
return "", e
|
||||
}
|
||||
// 二.2 物理位置 + 三.2 状态流水 NEW→ONLINE
|
||||
s.writeLifecycle(ctx, wp.Sn, wp.OrderNo, "NEW", "ONLINE", station, 0, operator, "workstation", "上线建档")
|
||||
s.ctx.EventLog.Write(ctx, "workpiece.online", wp.OrderNo, operator, "workpiece", wp.Sn,
|
||||
"预生成SN上线建档", map[string]any{"stationNo": station})
|
||||
s.notifyDashboard()
|
||||
return "进线登记成功", nil
|
||||
}
|
||||
|
||||
// reonlineWorkpiece 重上线(暂存退库后再次进线):
|
||||
// ① 先校验:工单组合非空且存在下一站(stationNo > currentStationNo),无副作用不通过即拒绝;
|
||||
// ② 再调 SemiFlow(OUTBOUND) 让 WMS 半成品出库,失败则报错不继续(文案含 WMS 原文)——
|
||||
// 顺序保证:校验全部通过后才产生出库这一副作用,避免"已出库却登记失败"的悬挂状态;
|
||||
// ③ 下一站工位当天停单时提示"工位N已停止接单"(复用 GetStationPaused 既有停单判定,不阻断登记)。
|
||||
// 出库一次解锁一次(一.5):置回 ONLINE 并写物理位置 at_station=下一站;记生命周期 SEMI_OUT。
|
||||
func (s *Service) reonlineWorkpiece(ctx context.Context, wp *ent.Workpiece, req OnlineReq, operator string) (string, error) {
|
||||
items, _ := RouteStationsFromWO(ctx, s.ctx.EntClient, s.workpieceWorkOrder(ctx, wp))
|
||||
if len(items) == 0 {
|
||||
return "", errors.New("工单未配置工艺组合")
|
||||
@@ -187,9 +236,20 @@ func (s *Service) reonlineWorkpiece(ctx context.Context, wp *ent.Workpiece, oper
|
||||
if next == 0 {
|
||||
return "", fmt.Errorf("工件已报工至工位%d,工艺组合中无下一站", wp.CurrentStationNo)
|
||||
}
|
||||
// 二.4 资格校验:半成品(SEMI_STORED)重上线落点须为后续工序工位(=下一站);未传落点时按下一站兜底
|
||||
if req.StationNo > 0 && req.StationNo != next {
|
||||
return "", fmt.Errorf("半成品重上线须落在后续工序工位(工位%d),当前落点工位%d不符合资格", next, req.StationNo)
|
||||
}
|
||||
if err := s.SemiFlow(ctx, SemiReq{Sn: wp.Sn, OrderNo: wp.OrderNo, Action: "OUTBOUND"}, operator); err != nil {
|
||||
return "", fmt.Errorf("重上线失败:%s", err.Error())
|
||||
}
|
||||
// 出库一次解锁一次(问题.md 一.5):置回 ONLINE,写物理位置=下一站,等待在所选工位重新上线建档。
|
||||
if _, e := s.ctx.EntClient.Workpiece.UpdateOneID(wp.ID).
|
||||
SetStatus("ONLINE").SetAtStationNo(next).Save(ctx); e != nil {
|
||||
return "", 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) + "已停止接单)"
|
||||
@@ -200,6 +260,41 @@ func (s *Service) reonlineWorkpiece(ctx context.Context, wp *ent.Workpiece, oper
|
||||
return msg, nil
|
||||
}
|
||||
|
||||
// writeLifecycle 写工件生命周期状态流水(三.2 / P2 定稿:所有 status 变更点同事务写)。
|
||||
// action 值域:NEW/ONLINE/REPORT/SEMI_IN/SEMI_OUT/DONE/SCRAPPED;from/to 为变更前后状态。
|
||||
// 入库次数统一 count(action='SEMI_IN'),不再依赖 semiflow(CountSemiStored 已删)。
|
||||
func (s *Service) writeLifecycle(ctx context.Context, sn, orderNo, from, to string, stationNo, flowSeq int, operator, source, remark string) {
|
||||
if sn == "" {
|
||||
return
|
||||
}
|
||||
_, _ = s.ctx.EntClient.WorkpieceLifecycleLog.Create().
|
||||
SetSn(sn).SetWorkOrderNo(orderNo).SetAction(toAction(to, from)).
|
||||
SetFromStatus(from).SetToStatus(to).SetStationNo(stationNo).SetFlowSeq(flowSeq).
|
||||
SetOperator(operator).SetSource(source).SetRemark(remark).Save(ctx)
|
||||
}
|
||||
|
||||
// toAction 由 状态变更 推导 lifecycle action(三.2 枚举)。
|
||||
func toAction(to, from string) string {
|
||||
switch to {
|
||||
case "ONLINE":
|
||||
return "ONLINE"
|
||||
case "PROCESSING":
|
||||
return "REPORT"
|
||||
case "SEMI_STORED":
|
||||
return "SEMI_IN"
|
||||
case "DONE":
|
||||
return "DONE"
|
||||
case "SCRAPPED":
|
||||
return "SCRAPPED"
|
||||
case "NEW":
|
||||
return "NEW"
|
||||
}
|
||||
if from == "SEMI_STORED" && to == "ONLINE" {
|
||||
return "SEMI_OUT"
|
||||
}
|
||||
return to
|
||||
}
|
||||
|
||||
// workpieceWorkOrder 取工件所属工单:优先 workOrderId,缺失按 orderNo 兜底解析
|
||||
func (s *Service) workpieceWorkOrder(ctx context.Context, wp *ent.Workpiece) *ent.WorkOrder {
|
||||
if wp.WorkOrderId > 0 {
|
||||
@@ -257,7 +352,7 @@ func (s *Service) ReportProcess(ctx context.Context, req ReportProcessReq, opera
|
||||
if err != nil {
|
||||
return errors.New("工件未进线登记")
|
||||
}
|
||||
// 严格校验(业务口径已确认):仅“在制”工件可报工;DONE/REPAIR/SCRAPPED 一律拒绝。
|
||||
// 严格校验(业务口径已确认):仅“在制”工件可报工;DONE/SCRAPPED 一律拒绝(REPAIR 已退役)。
|
||||
if wp.Status != "ONLINE" && wp.Status != "PROCESSING" {
|
||||
return errors.New("工件当前状态为「" + wp.Status + "」,非在制,不允许报工")
|
||||
}
|
||||
@@ -387,7 +482,14 @@ func (s *Service) ReportProcess(ctx context.Context, req ReportProcessReq, opera
|
||||
return err
|
||||
}
|
||||
if _, err := client.Workpiece.UpdateOneID(wp.ID).
|
||||
SetCurrentStationNo(req.StationNo).SetStatus("PROCESSING").Save(ctx); err != nil {
|
||||
SetCurrentStationNo(req.StationNo).SetAtStationNo(req.StationNo).SetStatus("PROCESSING").Save(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
// 二.2 物理位置随报工推进 + 三.2 状态流水 REPORT(同事务)
|
||||
if _, err := client.WorkpieceLifecycleLog.Create().
|
||||
SetSn(req.Sn).SetWorkOrderNo(wp.OrderNo).SetAction("REPORT").
|
||||
SetFromStatus(wp.Status).SetToStatus("PROCESSING").SetStationNo(req.StationNo).
|
||||
SetOperator(operator).SetSource("workstation").SetRemark("工位报工").Save(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
@@ -608,36 +710,58 @@ func (s *Service) DoneWorkpiece(ctx context.Context, req DoneReq, operator strin
|
||||
} else if miss != "" {
|
||||
return errors.New("完工前必须绑齐全部装配物料:" + miss)
|
||||
}
|
||||
now := time.Now()
|
||||
_, _ = s.ctx.EntClient.Workpiece.UpdateOneID(wp.ID).
|
||||
SetStatus("DONE").SetDoneAt(now).Save(ctx)
|
||||
|
||||
existID, _ := s.ctx.EntClient.AssociationTrace.Query().
|
||||
Where(associationtrace.FinishedSn(req.Sn)).FirstID(ctx)
|
||||
if existID > 0 {
|
||||
_, _ = s.ctx.EntClient.AssociationTrace.UpdateOneID(existID).
|
||||
SetBatchItems(req.BatchItems).SetSerialItems(req.SerialItems).SetOperator(operator).Save(ctx)
|
||||
} else {
|
||||
routeStr := ""
|
||||
if items, _ := RouteStationsFromWO(ctx, s.ctx.EntClient, wo); len(items) > 0 {
|
||||
routeStr = RouteStationsStrOfWO(items)
|
||||
}
|
||||
_, err = s.ctx.EntClient.AssociationTrace.Create().
|
||||
SetFinishedSn(req.Sn).SetOrderNo(wp.OrderNo).SetProcessCode(routeStr).
|
||||
SetBatchItems(req.BatchItems).SetSerialItems(req.SerialItems).
|
||||
SetOperator(operator).Save(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// 下线完工分流(问题.md 二.2 + 一.7):不强制最后一道工序,任何工序均可下线;
|
||||
// 成品工艺已全部报工→DONE(完工);未全部完成→SEMI_STORED(半成品入库)。
|
||||
// 二.2 物理位置:完工/入库均清空 at_station(件已离产线/入半成品库)。
|
||||
// 三.2 入库次数统一 count(action='SEMI_IN'),不再依赖 semiflow(CountSemiStored 已删)。
|
||||
routeItems, _ := RouteStationsFromWO(ctx, s.ctx.EntClient, wo)
|
||||
routeCount := len(routeItems)
|
||||
reported := 0
|
||||
if routeCount > 0 {
|
||||
reported, _ = s.ctx.EntClient.WorkpieceProcess.Query().
|
||||
Where(workpieceprocess.Sn(req.Sn), workpieceprocess.Status("DONE")).Count(ctx)
|
||||
}
|
||||
status := "DONE"
|
||||
if routeCount > 0 && reported < routeCount {
|
||||
status = "SEMI_STORED"
|
||||
}
|
||||
now := time.Now()
|
||||
upd := s.ctx.EntClient.Workpiece.UpdateOneID(wp.ID).SetStatus(status).SetAtStationNo(0)
|
||||
if status == "DONE" {
|
||||
upd = upd.SetDoneAt(now)
|
||||
}
|
||||
if _, err := upd.Save(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
// 三.2 状态流水:DONE→DONE;SEMI_STORED→补 INBOUND 缺口(SEMI_IN)
|
||||
s.writeLifecycle(ctx, req.Sn, wp.OrderNo, wp.Status, status, 0, 0, operator, "workstation", "下线完工")
|
||||
if status == "DONE" {
|
||||
existID, _ := s.ctx.EntClient.AssociationTrace.Query().
|
||||
Where(associationtrace.FinishedSn(req.Sn)).FirstID(ctx)
|
||||
if existID > 0 {
|
||||
_, _ = s.ctx.EntClient.AssociationTrace.UpdateOneID(existID).
|
||||
SetBatchItems(req.BatchItems).SetSerialItems(req.SerialItems).SetOperator(operator).Save(ctx)
|
||||
} else {
|
||||
routeStr := ""
|
||||
if items, _ := RouteStationsFromWO(ctx, s.ctx.EntClient, wo); len(items) > 0 {
|
||||
routeStr = RouteStationsStrOfWO(items)
|
||||
}
|
||||
if _, cerr := s.ctx.EntClient.AssociationTrace.Create().
|
||||
SetFinishedSn(req.Sn).SetOrderNo(wp.OrderNo).SetProcessCode(routeStr).
|
||||
SetBatchItems(req.BatchItems).SetSerialItems(req.SerialItems).
|
||||
SetOperator(operator).Save(ctx); cerr != nil {
|
||||
return cerr
|
||||
}
|
||||
}
|
||||
s.bumpWorkOrderProgress(ctx, wp.OrderNo, req.Sn)
|
||||
s.bumpDailyPlanCompleted(ctx, wp.OrderNo, now)
|
||||
// 反冲扣账:物料在当道工序被消耗,完工即按整份物料清单反冲扣 WMS 库存。
|
||||
// 失败仅记 eventlog 不阻塞完工。
|
||||
s.backflushWorkpiece(ctx, wp, operator)
|
||||
s.ctx.EventLog.Write(ctx, "workpiece.done", wp.OrderNo, operator, "workpiece", req.Sn, "完工+关联追溯", map[string]any{"batchItems": req.BatchItems, "serialItems": req.SerialItems})
|
||||
} else {
|
||||
s.ctx.EventLog.Write(ctx, "workpiece.semi_stored", wp.OrderNo, operator, "workpiece", req.Sn, "下线完工(半成品入库)", map[string]any{"routeCount": routeCount, "reported": reported})
|
||||
}
|
||||
s.bumpWorkOrderProgress(ctx, wp.OrderNo, req.Sn)
|
||||
s.bumpDailyPlanCompleted(ctx, wp.OrderNo, now)
|
||||
s.ctx.EventLog.Write(ctx, "workpiece.done", wp.OrderNo, operator, "workpiece", req.Sn, "完工+关联追溯", map[string]any{"batchItems": req.BatchItems, "serialItems": req.SerialItems})
|
||||
// 完工确认 = 生产完成(方案A定稿):此处不再回流 WMS 入库,成品改由「完工检」结论触发——
|
||||
// 完工检合格(已通过/部分通过)才回流入库,见 inspection.go finishedInspectionInbound。
|
||||
// 反冲扣账:物料在当道工序被消耗,完工即按整份物料清单反冲扣 WMS 库存。
|
||||
// 失败仅记 eventlog 不阻塞完工。
|
||||
s.backflushWorkpiece(ctx, wp, operator)
|
||||
s.notifyDashboard()
|
||||
return nil
|
||||
}
|
||||
@@ -712,6 +836,7 @@ func (s *Service) Trace(ctx context.Context, sn string) (map[string]any, error)
|
||||
"torqueRecords": torque,
|
||||
"bindTrace": bindVO,
|
||||
"semiFlows": semiFlows,
|
||||
"semiStoredCount": s.CountSemiIn(ctx, sn), // 半成品入库次数(三.3:count(action='SEMI_IN'),一.5 可追溯)
|
||||
"inspections": inspections,
|
||||
"signs": signs,
|
||||
}, nil
|
||||
@@ -849,8 +974,8 @@ type InProcessItem struct {
|
||||
TotalSteps int `json:"totalSteps"`
|
||||
}
|
||||
|
||||
// WorkpiecesInProcess 列出停在各工位、尚未完工的在制品(ONLINE/PROCESSING/REPAIR)。
|
||||
// stationNo<=0 返回全部;stationNo>0 仅返回当前停在该工位(0=上线位, 13=下线位)的工件。
|
||||
// WorkpiecesInProcess 列出停在各工位、尚未完工的在制品(ONLINE/PROCESSING)。
|
||||
// stationNo<=0 返回全部;stationNo>0 仅返回当前停在该工位的工件。
|
||||
// orderNo 非空时按工单号模糊过滤(客户第12条「记录查看加一项看工单号查询」)。
|
||||
// 已进线未完工的工件跨天继续,日排产仅排「未开工数量」,不会重复排产。
|
||||
func (s *Service) WorkpiecesInProcess(ctx context.Context, stationNo int, orderNo string) ([]InProcessItem, error) {
|
||||
@@ -858,7 +983,6 @@ func (s *Service) WorkpiecesInProcess(ctx context.Context, stationNo int, orderN
|
||||
Where(workpiece.Or(
|
||||
workpiece.Status("ONLINE"),
|
||||
workpiece.Status("PROCESSING"),
|
||||
workpiece.Status("REPAIR"),
|
||||
))
|
||||
if orderNo != "" {
|
||||
q = q.Where(workpiece.OrderNoContainsFold(orderNo))
|
||||
@@ -893,9 +1017,9 @@ func (s *Service) WorkpiecesInProcess(ctx context.Context, stationNo int, orderN
|
||||
maxStation = c
|
||||
}
|
||||
}
|
||||
// 推算当前物理工位:ONLINE 在 上线位(0);PROCESSING 且已到末端 → 下线位(13)
|
||||
// 推算当前物理工位:ONLINE 在未分配工位;PROCESSING 且已到末端 → 对应下线位
|
||||
cur := 0
|
||||
if wp.Status == "PROCESSING" || wp.Status == "REPAIR" {
|
||||
if wp.Status == "PROCESSING" {
|
||||
cp := wp.CurrentStationNo
|
||||
if cp <= 0 {
|
||||
cp = maxStation
|
||||
|
||||
Reference in New Issue
Block a user