108 lines
4.5 KiB
Go
108 lines
4.5 KiB
Go
package logic
|
||
|
||
import (
|
||
"context"
|
||
"errors"
|
||
|
||
"bj_power_mes/ent"
|
||
"bj_power_mes/ent/semiflow"
|
||
"bj_power_mes/ent/workpiece"
|
||
"bj_power_mes/ent/workpiecelifecyclelog"
|
||
"bj_power_mes/internal/wmsclient"
|
||
)
|
||
|
||
type SemiReq struct {
|
||
Sn string `json:"sn"`
|
||
OrderNo string `json:"orderNo"`
|
||
Action string `json:"action"` // INBOUND / OUTBOUND
|
||
TargetDock string `json:"targetDock"`
|
||
}
|
||
|
||
// SemiFlow 半成品流转:进度唯一权威源=workpiece.currentStationNo(最后报工工位号),
|
||
// 服务端按 sn 自取进度生成 completedText("已完成至工位N";未报工传空串),
|
||
// 并调用 WMS /api/internal/semi/*(payload={sn,orderNo,completedText,operator,ownershipType,ownershipNo,productWorkNo},
|
||
// 旧 doneProcessCodes 已废除)。
|
||
// 归属自动继承(2026-09-30 定稿):半成品归属=其产品工号的工单归属(合同号/科技项目号 二选一),
|
||
// 从工单自动带出传给 WMS,不做人工手选;工单缺失时归属字段传空,由 WMS 侧按其规则兜底。
|
||
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"
|
||
}
|
||
// 服务端自取进度:工件未上线(无 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)
|
||
}
|
||
// 归属随产品工号自动继承:工件 → 工单 → 归属类型/归属编号/产品工号
|
||
ownershipType, ownershipNo, productWorkNo := "", "", ""
|
||
if wo := s.workpieceWorkOrder(ctx, wp); wo != nil {
|
||
ownershipType = wo.OwnershipType
|
||
ownershipNo = wo.ContractNo
|
||
productWorkNo = wo.WorkOrderNo
|
||
}
|
||
_, 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
|
||
}
|
||
// 调用 WMS 内部接口(本地 SemiFlow 已保存,下面把 WMS 同步结果如实返回,
|
||
// 不再静默吞掉——WMS 不可达或校验失败时必须让调用方感知,才能形成闭环)。
|
||
payload := wmsclient.SemiPayload{
|
||
Sn: req.Sn, OrderNo: req.OrderNo, CompletedText: completedText, Operator: operator,
|
||
OwnershipType: ownershipType, OwnershipNo: ownershipNo, ProductWorkNo: productWorkNo,
|
||
}
|
||
var wmsErr error
|
||
if action == "INBOUND" {
|
||
wmsErr = s.ctx.Wms.SemiInbound(ctx, payload)
|
||
} else {
|
||
wmsErr = s.ctx.Wms.SemiOutbound(ctx, payload)
|
||
}
|
||
if wmsErr != nil {
|
||
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
|
||
}
|
||
s.ctx.EventLog.Write(ctx, "semi.flow", req.OrderNo, operator, "semi_flow", req.Sn, "半成品流转",
|
||
map[string]any{"action": action, "completedText": completedText,
|
||
"ownershipType": ownershipType, "ownershipNo": ownershipNo, "productWorkNo": productWorkNo})
|
||
return nil
|
||
}
|
||
|
||
// ListSemiFlows 查询半成品流转记录
|
||
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)
|
||
}
|
||
|
||
// CountSemiIn 半成品入库次数 = workpiece_lifecycle_log.action='SEMI_IN' 记录数(见业务定稿·一.5 入库次数可追溯;替代原 CountSemiStored)。
|
||
// 业务定稿·一.5:上线次数=状态驱动、入库一次解锁一次、入库次数有记录可追溯;
|
||
// 入库次数统一以 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
|
||
}
|
||
|
||
// ListEventLogs 操作日志查询(按工单号/操作人;operators 为候选列表,任一命中即算)
|
||
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)
|
||
}
|