feat&refactor: 2026-09-22 半成品业务模型重构与功能完善

- 重构半成品流转逻辑:进度权威源改为 workpiece.currentStationNo,新增 completedText 快照字段,废除 doneProcessCodes
- 清理物料品类中半成品类型,存量数据幂等清理,调整物料管理方式文案为批次/序列号
- 新增日排产状态专用更新接口,修复工单工艺校验逻辑
- 新增物料档案代理接口、深路径文件下载接口
- 优化前端界面文案与工位选择逻辑,补充追溯页半成品流转展示
- 调整WMS端物料品类校验与入库接口契约
- 新增/更新数据库表结构与实体类代码
This commit is contained in:
SunYF
2026-09-22 14:42:52 +08:00
parent 09cfc6ca1c
commit d3eda6b588
49 changed files with 1155 additions and 1122 deletions
+39
View File
@@ -328,6 +328,45 @@ func (s *Service) DeleteDailyPlan(ctx context.Context, id int) error {
return s.ctx.EntClient.DailyPlan.DeleteOneID(id).Exec(ctx)
}
// SetDailyPlanStatus 日排产启用/停用(人工专用入口,POST /daily-plans/:id/status):
// 仅允许 PENDING(待执行)↔CONFIRMED(已启用) 之间切换;执行中/已完成/已取消由系统联动维护,无人工入口。
// 启用(→CONFIRMED)前校验所属工单(按排产 orderNo 查)状态必须为 RELEASED 或 IN_PROGRESS。
// 只 UPDATE status 字段,绝不触碰计划数量/分产量等其它列。
func (s *Service) SetDailyPlanStatus(ctx context.Context, id int, target, operator string) error {
if id <= 0 {
return errors.New("缺少排产 id")
}
if target != "PENDING" && target != "CONFIRMED" {
return errors.New("仅支持 启用/停用 操作")
}
p, err := s.ctx.EntClient.DailyPlan.Get(ctx, id)
if err != nil {
return errors.New("日排产不存在")
}
if p.Status != "PENDING" && p.Status != "CONFIRMED" {
return errors.New("仅支持 启用/停用 操作")
}
if p.Status == target {
return nil // 幂等:状态相同直接成功
}
if target == "CONFIRMED" {
wo, err := s.ctx.EntClient.WorkOrder.Query().Where(workorder.WorkOrderNo(p.OrderNo)).First(ctx)
if err != nil {
return errors.New("工单不存在")
}
if wo.Status != "RELEASED" && wo.Status != "IN_PROGRESS" {
return errors.New("工单未下发,不能启用排产")
}
}
if err := s.ctx.EntClient.DailyPlan.UpdateOneID(id).SetStatus(target).Exec(ctx); err != nil {
return err
}
s.ctx.EventLog.Write(ctx, "daily.plan.status", p.OrderNo, operator, "daily_plan", p.OrderNo,
"排产启用/停用", map[string]any{"id": id, "from": p.Status, "to": target})
s.notifyDashboard()
return nil
}
// ---------- 自动排产(引导式) ----------
// DateSegment 日期区间 [start, end](含两端),yyyy-MM-dd
+7 -5
View File
@@ -83,7 +83,7 @@ func StationFlowMapOfWO(items []FlowItem) map[int]int {
}
// ValidateFlowCombination 保存工单时校验工艺组合:
// 非空、工艺存在且启用(ACTIVE)、工位存在、工位号不重复;返回按工位号升序的条目。
// 非空、工艺存在且启用(ACTIVE)、工位存在且 ENABLED 且当前绑定工艺与工单一致、工位号不重复;返回按工位号升序的条目。
func (s *Service) ValidateFlowCombination(ctx context.Context, raw []map[string]int) ([]FlowItem, error) {
items := flowMapsToItems(raw)
if len(items) == 0 {
@@ -103,13 +103,15 @@ func (s *Service) ValidateFlowCombination(ctx context.Context, raw []map[string]
if flow.Status != "ACTIVE" {
return nil, fmt.Errorf("工艺「%s」已停用", flow.Name)
}
exist, err := s.ctx.EntClient.Station.Query().Where(station.StationNo(it.StationNo)).Exist(ctx)
exist, err := s.ctx.EntClient.Station.Query().Where(station.StationNo(it.StationNo)).First(ctx)
if err != nil {
return nil, err
}
if !exist {
return nil, fmt.Errorf("工位 %d 不存在", it.StationNo)
}
// 工位当前绑定工艺必须与工单一致且工位启用(与排产校验 ValidateWOCombinationForSchedule 同口径同文案),
// 否则工单下发后工位被改绑/停用会导致路线漂移
if exist.FlowId != it.FlowId || exist.Status != "ENABLED" {
return nil, fmt.Errorf("工位%d当前工艺与工单不匹配", it.StationNo)
}
}
return items, nil
}
+23 -12
View File
@@ -6,17 +6,19 @@ import (
"bj_power_mes/ent"
"bj_power_mes/ent/semiflow"
"bj_power_mes/ent/workpiece"
)
type SemiReq struct {
Sn string `json:"sn"`
OrderNo string `json:"orderNo"`
DoneProcessCodes []int `json:"doneProcessCodes"`
Action string `json:"action"` // INBOUND / OUTBOUND
TargetDock string `json:"targetDock"`
Sn string `json:"sn"`
OrderNo string `json:"orderNo"`
Action string `json:"action"` // INBOUND / OUTBOUND
TargetDock string `json:"targetDock"`
}
// SemiFlow 半成品流转:记录已完成工序,并调用 WMS /api/internal/semi/*
// SemiFlow 半成品流转:进度唯一权威源=workpiece.currentStationNo(最后报工工位号),
// 服务端按 sn 自取进度生成 completedText"已完成至工位N";未报工传空串),
// 并调用 WMS /api/internal/semi/*payload={sn,orderNo,completedText,operator},旧 doneProcessCodes 已废除)。
func (s *Service) SemiFlow(ctx context.Context, req SemiReq, operator string) error {
if req.Sn == "" {
return errors.New("sn 不能为空")
@@ -25,8 +27,17 @@ func (s *Service) SemiFlow(ctx context.Context, req SemiReq, operator string) er
if action == "" {
action = "INBOUND"
}
_, err := s.ctx.EntClient.SemiFlow.Create().
SetSn(req.Sn).SetOrderNo(req.OrderNo).SetDoneProcessCodes(req.DoneProcessCodes).
// 服务端自取进度:工件未上线(无 workpiece 记录)一律拒绝流转
wp, err := s.ctx.EntClient.Workpiece.Query().Where(workpiece.Sn(req.Sn)).First(ctx)
if err != nil {
return errors.New("工件未上线,无法暂存退库")
}
completedText := ""
if wp.CurrentStationNo > 0 {
completedText = "已完成至工位" + itoa(wp.CurrentStationNo)
}
_, err = s.ctx.EntClient.SemiFlow.Create().
SetSn(req.Sn).SetOrderNo(req.OrderNo).SetCompletedText(completedText).
SetAction(action).SetTargetDock(req.TargetDock).SetOperator(operator).Save(ctx)
if err != nil {
return err
@@ -35,9 +46,9 @@ func (s *Service) SemiFlow(ctx context.Context, req SemiReq, operator string) er
// 不再静默吞掉——WMS 不可达或校验失败时必须让调用方感知,才能形成闭环)。
var wmsErr error
if action == "INBOUND" {
wmsErr = s.ctx.Wms.SemiInbound(ctx, req.Sn, req.OrderNo, req.DoneProcessCodes, operator)
wmsErr = s.ctx.Wms.SemiInbound(ctx, req.Sn, req.OrderNo, completedText, operator)
} else {
wmsErr = s.ctx.Wms.SemiOutbound(ctx, req.Sn, req.OrderNo, req.DoneProcessCodes, operator)
wmsErr = s.ctx.Wms.SemiOutbound(ctx, req.Sn, req.OrderNo, completedText, operator)
}
if wmsErr != nil {
s.ctx.EventLog.Write(ctx, "semi.flow.wms_error", req.OrderNo, operator, "semi_flow", req.Sn,
@@ -45,7 +56,7 @@ func (s *Service) SemiFlow(ctx context.Context, req SemiReq, operator string) er
return wmsErr
}
s.ctx.EventLog.Write(ctx, "semi.flow", req.OrderNo, operator, "semi_flow", req.Sn, "半成品流转",
map[string]any{"action": action, "doneProcessCodes": req.DoneProcessCodes})
map[string]any{"action": action, "completedText": completedText})
return nil
}
@@ -64,4 +75,4 @@ func (s *Service) ListSemiFlows(ctx context.Context, sn, orderNo string) ([]*ent
// ListEventLogs 操作日志查询(按工单号/操作人;operators 为候选列表,任一命中即算)
func (s *Service) ListEventLogs(ctx context.Context, orderNo string, operators []string, eventType string, fromMs, toMs int64, page, pageSize int) ([]*ent.EventLog, int, error) {
return s.ctx.EventLog.Query(ctx, eventType, orderNo, operators, fromMs, toMs, page, pageSize)
}
}
+74 -8
View File
@@ -12,6 +12,7 @@ import (
"bj_power_mes/ent"
"bj_power_mes/ent/associationtrace"
"bj_power_mes/ent/processstep"
"bj_power_mes/ent/semiflow"
"bj_power_mes/ent/station"
"bj_power_mes/ent/stepcriterion"
"bj_power_mes/ent/stepdata"
@@ -66,25 +67,86 @@ type OnlineReq struct {
WorkOrderId int `json:"workOrderId"`
}
// OnlineWorkpiece 工件进线登记
// 执行路线 = 工单工艺组合(唯一路线源头),工件只需按组合逐工位报工推进
func (s *Service) OnlineWorkpiece(ctx context.Context, req OnlineReq, operator string) error {
// OnlineWorkpiece 工件进线登记(返回提示文案,供响应透出)。
// 执行路线 = 工单工艺组合(唯一路线源头),工件只需按组合逐工位报工推进
// 重上线识别:workpiece 已存在且未完工 → 先调 SemiFlow(OUTBOUND)WMS 半成品出库,
// 失败报错不继续、文案含 WMS 原文),再按工单工艺组合计算下一站并提示"继续执行:工位N·工艺名";
// 新件路径不变。
func (s *Service) OnlineWorkpiece(ctx context.Context, req OnlineReq, operator string) (string, error) {
if req.Sn == "" {
return errors.New("sn 不能为空")
return "", errors.New("sn 不能为空")
}
exist, err := s.ctx.EntClient.Workpiece.Query().Where(workpiece.Sn(req.Sn)).First(ctx)
if err != nil && !ent.IsNotFound(err) {
return "", err
}
if exist != nil {
if exist.Status == "DONE" || exist.Status == "SCRAPPED" {
return "", errors.New("工件已完工/报废,不可重复上线登记")
}
return s.reonlineWorkpiece(ctx, exist, operator)
}
if req.WorkOrderId == 0 {
if wo, err := s.ctx.EntClient.WorkOrder.Query().Where(workorder.WorkOrderNo(req.OrderNo)).First(ctx); err == nil {
req.WorkOrderId = wo.ID
}
}
err := s.ctx.EntClient.Workpiece.Create().
if err := s.ctx.EntClient.Workpiece.Create().
SetSn(req.Sn).SetOrderNo(req.OrderNo).SetWorkOrderId(req.WorkOrderId).
SetStatus("ONLINE").SetOnlineAt(time.Now()).Exec(ctx)
if err != nil {
return err
SetStatus("ONLINE").SetOnlineAt(time.Now()).Exec(ctx); err != nil {
return "", err
}
s.ctx.EventLog.Write(ctx, "workpiece.online", req.OrderNo, operator, "workpiece", req.Sn, "工件进线登记", nil)
s.notifyDashboard()
return "进线登记成功", nil
}
// reonlineWorkpiece 重上线(暂存退库后再次进线):
// ① 先校验:工单组合非空且存在下一站(stationNo > currentStationNo),无副作用不通过即拒绝;
// ② 再调 SemiFlow(OUTBOUND) 让 WMS 半成品出库,失败则报错不继续(文案含 WMS 原文)——
//
// 顺序保证:校验全部通过后才产生出库这一副作用,避免"已出库却登记失败"的悬挂状态;
//
// ③ 下一站工位当天停单时提示"工位N已停止接单"(复用 GetStationPaused 既有停单判定,不阻断登记)。
func (s *Service) reonlineWorkpiece(ctx context.Context, wp *ent.Workpiece, operator string) (string, error) {
items, _ := RouteStationsFromWO(ctx, s.ctx.EntClient, s.workpieceWorkOrder(ctx, wp))
if len(items) == 0 {
return "", 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
}
}
if next == 0 {
return "", fmt.Errorf("工件已报工至工位%d,工艺组合中无下一站", wp.CurrentStationNo)
}
if err := s.SemiFlow(ctx, SemiReq{Sn: wp.Sn, OrderNo: wp.OrderNo, Action: "OUTBOUND"}, operator); err != nil {
return "", fmt.Errorf("重上线失败:%s", err.Error())
}
msg := fmt.Sprintf("重上线成功,继续执行:工位%d·%s", next, s.flowNameById(ctx, nextFlow))
if s.GetStationPaused(ctx, next) {
msg += "(注意:工位" + itoa(next) + "已停止接单)"
}
s.ctx.EventLog.Write(ctx, "workpiece.reonline", wp.OrderNo, operator, "workpiece", wp.Sn,
"工件重上线(暂存退库后出库继续)", map[string]any{"nextStationNo": next, "nextFlowId": nextFlow})
s.notifyDashboard()
return msg, nil
}
// workpieceWorkOrder 取工件所属工单:优先 workOrderId,缺失按 orderNo 兜底解析
func (s *Service) workpieceWorkOrder(ctx context.Context, wp *ent.Workpiece) *ent.WorkOrder {
if wp.WorkOrderId > 0 {
if wo, err := s.ctx.EntClient.WorkOrder.Get(ctx, wp.WorkOrderId); err == nil {
return wo
}
}
if wp.OrderNo != "" {
if wo, err := s.ctx.EntClient.WorkOrder.Query().Where(workorder.WorkOrderNo(wp.OrderNo)).First(ctx); err == nil {
return wo
}
}
return nil
}
@@ -457,6 +519,9 @@ func (s *Service) Trace(ctx context.Context, sn string) (map[string]any, error)
torque, _ := s.ctx.EntClient.TorqueRecord.Query().
Where(torquerecord.Sn(sn)).Order(ent.Desc(torquerecord.FieldTime)).Limit(200).All(ctx)
bindVO, _ := s.BuildBindTrace(ctx, sn)
// 半成品流转记录(IN=暂存退库 / OUT=重上线),追溯页展示完成至工位快照
semiFlows, _ := s.ctx.EntClient.SemiFlow.Query().
Where(semiflow.Sn(sn)).Order(ent.Desc(semiflow.FieldID)).All(ctx)
return map[string]any{
"workpiece": wp,
@@ -465,6 +530,7 @@ func (s *Service) Trace(ctx context.Context, sn string) (map[string]any, error)
"associationTrace": assoc,
"torqueRecords": torque,
"bindTrace": bindVO,
"semiFlows": semiFlows,
}, nil
}