2026-08-28 15:06:01 +08:00
|
|
|
|
package logic
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
|
|
|
|
|
"errors"
|
2026-11-15 12:17:39 +08:00
|
|
|
|
"fmt"
|
2026-08-28 15:06:01 +08:00
|
|
|
|
|
|
|
|
|
|
"bj_power_mes/ent"
|
2026-11-19 08:31:05 +08:00
|
|
|
|
"bj_power_mes/ent/linepoint"
|
2026-08-28 15:06:01 +08:00
|
|
|
|
"bj_power_mes/ent/semiflow"
|
2026-09-22 14:42:52 +08:00
|
|
|
|
"bj_power_mes/ent/workpiece"
|
2026-10-12 22:32:55 +08:00
|
|
|
|
"bj_power_mes/ent/workpiecelifecyclelog"
|
2026-09-30 17:33:11 +08:00
|
|
|
|
"bj_power_mes/internal/wmsclient"
|
2026-08-28 15:06:01 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type SemiReq struct {
|
2026-09-22 14:42:52 +08:00
|
|
|
|
Sn string `json:"sn"`
|
|
|
|
|
|
OrderNo string `json:"orderNo"`
|
|
|
|
|
|
Action string `json:"action"` // INBOUND / OUTBOUND
|
|
|
|
|
|
TargetDock string `json:"targetDock"`
|
2026-08-28 15:06:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-15 12:17:39 +08:00
|
|
|
|
// SemiFlow 在制品流转:进度唯一权威源=workpiece.currentStationNo(最后报工工位号),
|
2026-09-22 14:42:52 +08:00
|
|
|
|
// 服务端按 sn 自取进度生成 completedText("已完成至工位N";未报工传空串),
|
2026-09-30 17:33:11 +08:00
|
|
|
|
// 并调用 WMS /api/internal/semi/*(payload={sn,orderNo,completedText,operator,ownershipType,ownershipNo,productWorkNo},
|
|
|
|
|
|
// 旧 doneProcessCodes 已废除)。
|
2026-11-18 09:53:42 +08:00
|
|
|
|
// 归属自动继承(2026-09-30 定稿):在制品归属=其工单号的工单归属(合同号/科技项目号 二选一),
|
2026-09-30 17:33:11 +08:00
|
|
|
|
// 从工单自动带出传给 WMS,不做人工手选;工单缺失时归属字段传空,由 WMS 侧按其规则兜底。
|
2026-08-28 15:06:01 +08:00
|
|
|
|
func (s *Service) SemiFlow(ctx context.Context, req SemiReq, operator string) error {
|
|
|
|
|
|
if req.Sn == "" {
|
|
|
|
|
|
return errors.New("sn 不能为空")
|
|
|
|
|
|
}
|
|
|
|
|
|
action := req.Action
|
|
|
|
|
|
if action == "" {
|
|
|
|
|
|
action = "INBOUND"
|
|
|
|
|
|
}
|
2026-09-22 14:42:52 +08:00
|
|
|
|
// 服务端自取进度:工件未上线(无 workpiece 记录)一律拒绝流转
|
|
|
|
|
|
wp, err := s.ctx.EntClient.Workpiece.Query().Where(workpiece.Sn(req.Sn)).First(ctx)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return errors.New("工件未上线,无法暂存退库")
|
|
|
|
|
|
}
|
2026-11-15 12:17:39 +08:00
|
|
|
|
// 2026-11-13 防重修复(在制品接口契约):同一 SN 不允许连续重复同一动作。
|
|
|
|
|
|
// 防的是「重复点击暂存/重上线」以及网络重试导致的重复回流;WMS 侧已有 SN 唯一与状态守卫,
|
|
|
|
|
|
// 这里补上 MES 侧的动作链守卫,避免产生半截流转记录。
|
|
|
|
|
|
last, _ := s.ctx.EntClient.SemiFlow.Query().
|
|
|
|
|
|
Where(semiflow.Sn(req.Sn)).Order(ent.Desc(semiflow.FieldID)).First(ctx)
|
|
|
|
|
|
if last != nil && last.Action == action {
|
|
|
|
|
|
return fmt.Errorf("请勿重复操作:%s 上一次流转已经是 %s(要继续请走相反动作)", req.Sn, action)
|
|
|
|
|
|
}
|
2026-09-22 14:42:52 +08:00
|
|
|
|
completedText := ""
|
2026-11-02 19:17:05 +08:00
|
|
|
|
if wp.CurrentStationNo != 0 {
|
2026-09-22 14:42:52 +08:00
|
|
|
|
completedText = "已完成至工位" + itoa(wp.CurrentStationNo)
|
|
|
|
|
|
}
|
2026-11-18 09:53:42 +08:00
|
|
|
|
// 归属随工单号自动继承:工件 → 工单 → 归属类型/归属编号/工单号
|
2026-09-30 17:33:11 +08:00
|
|
|
|
ownershipType, ownershipNo, productWorkNo := "", "", ""
|
|
|
|
|
|
if wo := s.workpieceWorkOrder(ctx, wp); wo != nil {
|
|
|
|
|
|
ownershipType = wo.OwnershipType
|
|
|
|
|
|
ownershipNo = wo.ContractNo
|
|
|
|
|
|
productWorkNo = wo.WorkOrderNo
|
|
|
|
|
|
}
|
2026-11-15 12:17:39 +08:00
|
|
|
|
// 调用 WMS 内部接口:WMS 不可达或校验失败时如实返回,
|
|
|
|
|
|
// 且本地不落流转记录(旧版先写本地再调 WMS,失败后会留下一条「假成功」记录,
|
|
|
|
|
|
// 重试又被 WMS 409 拒绝,形成半截数据——2026-11-13 防重修复)。
|
2026-09-30 17:33:11 +08:00
|
|
|
|
payload := wmsclient.SemiPayload{
|
|
|
|
|
|
Sn: req.Sn, OrderNo: req.OrderNo, CompletedText: completedText, Operator: operator,
|
|
|
|
|
|
OwnershipType: ownershipType, OwnershipNo: ownershipNo, ProductWorkNo: productWorkNo,
|
|
|
|
|
|
}
|
2026-08-28 15:06:01 +08:00
|
|
|
|
var wmsErr error
|
|
|
|
|
|
if action == "INBOUND" {
|
2026-09-30 17:33:11 +08:00
|
|
|
|
wmsErr = s.ctx.Wms.SemiInbound(ctx, payload)
|
2026-08-28 15:06:01 +08:00
|
|
|
|
} else {
|
2026-09-30 17:33:11 +08:00
|
|
|
|
wmsErr = s.ctx.Wms.SemiOutbound(ctx, payload)
|
2026-08-28 15:06:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
if wmsErr != nil {
|
2026-11-09 18:58:47 +08:00
|
|
|
|
endpoint := "/api/internal/semi/inbound"
|
|
|
|
|
|
if action != "INBOUND" {
|
|
|
|
|
|
endpoint = "/api/internal/semi/outbound"
|
|
|
|
|
|
}
|
|
|
|
|
|
s.recordSyncFailure("semi.flow", req.Sn+"|"+action, endpoint, payload, wmsErr)
|
2026-09-01 14:30:26 +08:00
|
|
|
|
s.ctx.EventLog.Write(ctx, "semi.flow.wms_error", req.OrderNo, operator, "semi_flow", req.Sn,
|
|
|
|
|
|
"WMS 同步失败", map[string]any{"action": action, "error": wmsErr.Error()})
|
|
|
|
|
|
return wmsErr
|
2026-08-28 15:06:01 +08:00
|
|
|
|
}
|
2026-11-15 12:17:39 +08:00
|
|
|
|
// WMS 成功后才落 MES 流转记录,保证两边一致
|
|
|
|
|
|
if _, err = s.ctx.EntClient.SemiFlow.Create().
|
|
|
|
|
|
SetSn(req.Sn).SetOrderNo(req.OrderNo).SetCompletedText(completedText).
|
|
|
|
|
|
SetAction(action).SetTargetDock(req.TargetDock).SetOperator(operator).Save(ctx); err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
2026-11-19 08:31:05 +08:00
|
|
|
|
// 2026-11-18 定稿(下线即离场):暂存退库=件离开产线管控——状态置 SEMI_STORED、
|
|
|
|
|
|
// 清物理位置与点位台账占用(半成品不再占产线任何点位标识),写 SEMI_IN 生命周期。
|
|
|
|
|
|
// 出库重上线(OUTBOUND)不在此改状态,由 reonlineWorkpiece 落 ONLINE + 占新点位。
|
|
|
|
|
|
if action == "INBOUND" {
|
|
|
|
|
|
if _, uerr := s.ctx.EntClient.Workpiece.UpdateOneID(wp.ID).
|
|
|
|
|
|
SetStatus("SEMI_STORED").SetAtStationNo(0).Save(ctx); uerr != nil {
|
|
|
|
|
|
return uerr
|
|
|
|
|
|
}
|
|
|
|
|
|
if n, rerr := s.ctx.EntClient.LinePoint.Update().
|
|
|
|
|
|
Where(linepoint.CurrentSnEQ(req.Sn)).
|
|
|
|
|
|
SetCurrentSn("").SetOrderNo("").SetOccupied(false).Save(ctx); rerr == nil && n > 0 {
|
|
|
|
|
|
s.ctx.EventLog.Write(ctx, "line.point.release", req.OrderNo, operator, "line_point", req.Sn,
|
|
|
|
|
|
fmt.Sprintf("暂存退库释放点位台账 %d 处:%s(下线即离场,半成品不占产线)", n, req.Sn), nil)
|
|
|
|
|
|
}
|
|
|
|
|
|
s.writeLifecycle(ctx, req.Sn, wp.OrderNo, wp.Status, "SEMI_STORED", wp.CurrentStationNo, 0, operator, "workstation", "暂存退库(半成品入库)")
|
|
|
|
|
|
}
|
2026-11-15 12:17:39 +08:00
|
|
|
|
s.ctx.EventLog.Write(ctx, "semi.flow", req.OrderNo, operator, "semi_flow", req.Sn, "在制品流转",
|
2026-09-30 17:33:11 +08:00
|
|
|
|
map[string]any{"action": action, "completedText": completedText,
|
|
|
|
|
|
"ownershipType": ownershipType, "ownershipNo": ownershipNo, "productWorkNo": productWorkNo})
|
2026-08-28 15:06:01 +08:00
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-15 12:17:39 +08:00
|
|
|
|
// ListSemiFlows 查询在制品流转记录
|
2026-08-28 15:06:01 +08:00
|
|
|
|
func (s *Service) ListSemiFlows(ctx context.Context, sn, orderNo string) ([]*ent.SemiFlow, error) {
|
|
|
|
|
|
q := s.ctx.EntClient.SemiFlow.Query()
|
|
|
|
|
|
if sn != "" {
|
|
|
|
|
|
q = q.Where(semiflow.Sn(sn))
|
|
|
|
|
|
}
|
|
|
|
|
|
if orderNo != "" {
|
|
|
|
|
|
q = q.Where(semiflow.OrderNo(orderNo))
|
|
|
|
|
|
}
|
|
|
|
|
|
return q.Order(ent.Desc(semiflow.FieldID)).All(ctx)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-15 12:17:39 +08:00
|
|
|
|
// CountSemiIn 在制品入库次数 = workpiece_lifecycle_log.action='SEMI_IN' 记录数(见业务定稿·一.5 入库次数可追溯;替代原 CountSemiStored)。
|
2026-10-17 17:49:47 +08:00
|
|
|
|
// 业务定稿·一.5:上线次数=状态驱动、入库一次解锁一次、入库次数有记录可追溯;
|
2026-10-12 22:32:55 +08:00
|
|
|
|
// 入库次数统一以 lifecycle 状态流水为权威源,不再依赖 semiflow 业务流水。
|
|
|
|
|
|
func (s *Service) CountSemiIn(ctx context.Context, sn string) int {
|
|
|
|
|
|
n, err := s.ctx.EntClient.WorkpieceLifecycleLog.Query().
|
|
|
|
|
|
Where(workpiecelifecyclelog.Sn(sn), workpiecelifecyclelog.Action("SEMI_IN")).Count(ctx)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return 0
|
|
|
|
|
|
}
|
|
|
|
|
|
return n
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-09-10 16:59:15 +08:00
|
|
|
|
// ListEventLogs 操作日志查询(按工单号/操作人;operators 为候选列表,任一命中即算)
|
2026-10-26 16:32:35 +08:00
|
|
|
|
func (s *Service) ListEventLogs(ctx context.Context, orderNo string, operators []string, eventType, entityType string, fromMs, toMs int64, page, pageSize int) ([]*ent.EventLog, int, error) {
|
|
|
|
|
|
return s.ctx.EventLog.Query(ctx, eventType, orderNo, operators, entityType, fromMs, toMs, page, pageSize)
|
2026-09-22 14:42:52 +08:00
|
|
|
|
}
|