- MES 新增损耗台账 loss_ledger,差异确认损耗接口 ConfirmReceiveLoss(按工单归集) - 备料清单 preparation:MES 按 日计划×工单×工位×物料 实时计算,WMS 只读展示 PreparationList - 退料闭环:N5 工位托盘守卫(未完成禁止退料)、退料 AGV 自动派车(JBT21 空位判定 + 托盘回收链路)、退料参考剩余量接口(应剩余=实备-已耗-已退-已损) - 新增 AGV 车辆状态表 agv_vehicle_state 及查询 - 整合接驳台/叫料池/库存锁既有闭环,清理 sql 目录注释生成脚本与 catalog,更新文档
265 lines
11 KiB
Go
265 lines
11 KiB
Go
package logic
|
||
|
||
import (
|
||
"context"
|
||
"errors"
|
||
"fmt"
|
||
"time"
|
||
|
||
"bj_power_mes/ent"
|
||
"bj_power_mes/ent/inspectionrecord"
|
||
"bj_power_mes/ent/workorder"
|
||
)
|
||
|
||
// ============ 工位终端业务操作(P0-3 / 数量不符 / 退库 / 工序间检验记录) ============
|
||
|
||
// EmergencyCall 应急呼叫:落预警消息(type=emergency_call),预警中心实时展示 + pad 顶部提示。
|
||
// 纯软件方案(客户 L614 已定:发消息给班组长,不依赖安灯硬件)。
|
||
func (s *Service) EmergencyCall(ctx context.Context, stationNo int, orderNo, remark, operator string) error {
|
||
if stationNo <= 0 {
|
||
return errors.New("缺少工位号")
|
||
}
|
||
content := fmt.Sprintf("工位 %d 发起应急呼叫", stationNo)
|
||
if orderNo != "" {
|
||
content += "(工单 " + orderNo + ")"
|
||
}
|
||
if remark != "" {
|
||
content += ":" + remark
|
||
}
|
||
_, err := s.ctx.EntClient.Alert.Create().
|
||
SetRuleId(0).
|
||
SetType("emergency_call").
|
||
SetTitle("应急呼叫").
|
||
SetContent(content).
|
||
SetRefType("station").
|
||
SetRefId(fmt.Sprintf("%d", stationNo)).
|
||
SetReceiver("").
|
||
SetStatus("UNREAD").
|
||
Save(ctx)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
s.ctx.EventLog.Write(ctx, "station.emergency", orderNo, operator, "alert",
|
||
fmt.Sprintf("%d", stationNo), "工位应急呼叫", map[string]any{"stationNo": stationNo, "remark": remark})
|
||
s.notifyDashboard()
|
||
return nil
|
||
}
|
||
|
||
// QtyReportReq 数量不符上报请求(工位收到料数量与下发数量不一致)
|
||
type QtyReportReq struct {
|
||
StationNo int `json:"stationNo"`
|
||
OrderNo string `json:"orderNo"`
|
||
Sn string `json:"sn"`
|
||
MaterialCode string `json:"materialCode"`
|
||
MaterialName string `json:"materialName"`
|
||
PlanQty int `json:"planQty"`
|
||
ActualQty int `json:"actualQty"`
|
||
Cause string `json:"cause"`
|
||
Operator string `json:"operator"`
|
||
}
|
||
|
||
// CreateQtyReport 数量不符上报:落 material_qty_report + 预警中心消息(type=qty_diff)。
|
||
//
|
||
// 归属(2026-09-19 定稿):MES 是唯一真源与处理侧 —— 上报、明细、处理(补发/退库/调整)
|
||
// 全在 MES(见 logic/qty_report.go:HandleQtyReport),WMS 只经内部接口被动建单。
|
||
// 预警 refId 存差异单业务号 QR{id},处理完成后可精确定位并闭环该预警。
|
||
func (s *Service) CreateQtyReport(ctx context.Context, req QtyReportReq) error {
|
||
if req.StationNo <= 0 || req.MaterialCode == "" {
|
||
return errors.New("工位号与物料编码必填")
|
||
}
|
||
diff := req.PlanQty - req.ActualQty
|
||
rec, err := s.ctx.EntClient.MaterialQtyReport.Create().
|
||
SetStationNo(req.StationNo).
|
||
SetOrderNo(req.OrderNo).
|
||
SetSn(req.Sn).
|
||
SetMaterialCode(req.MaterialCode).
|
||
SetMaterialName(req.MaterialName).
|
||
SetPlanQty(req.PlanQty).
|
||
SetActualQty(req.ActualQty).
|
||
SetDiffQty(diff).
|
||
SetCause(req.Cause).
|
||
SetStatus("PENDING").
|
||
SetOperator(req.Operator).
|
||
Save(ctx)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
// 融合成单段文字描述,直接写入预警「内容」(即预警原因)。
|
||
cause := req.Cause
|
||
if cause == "" {
|
||
cause = "未填写"
|
||
}
|
||
content := fmt.Sprintf("差异单 %s|工位%d 数量不符:物料 %s", qtyReportNo(rec.ID), req.StationNo, req.MaterialCode)
|
||
if req.MaterialName != "" {
|
||
content += "(" + req.MaterialName + ")"
|
||
}
|
||
content += fmt.Sprintf(";应发 %d,实收 %d,差异 %d", req.PlanQty, req.ActualQty, diff)
|
||
if req.OrderNo != "" {
|
||
content += ";工单 " + req.OrderNo
|
||
}
|
||
if req.Sn != "" {
|
||
content += ";序列号 " + req.Sn
|
||
}
|
||
content += ";上报原因:" + cause
|
||
_, _ = s.ctx.EntClient.Alert.Create().
|
||
SetRuleId(0).
|
||
SetType("qty_diff").
|
||
SetTitle("数量不符预警").
|
||
SetContent(content).
|
||
SetRefType("material_qty_report").
|
||
SetRefId(qtyReportNo(rec.ID)).
|
||
SetReceiver("").
|
||
SetStatus("UNREAD").
|
||
Save(ctx)
|
||
s.ctx.EventLog.Write(ctx, "station.qty_report", req.OrderNo, req.Operator, "material_qty_report",
|
||
req.MaterialCode, "数量不符上报", map[string]any{"stationNo": req.StationNo, "diff": diff, "reportNo": qtyReportNo(rec.ID)})
|
||
s.notifyDashboard()
|
||
return nil
|
||
}
|
||
|
||
// ErrReturnForceConfirm 哨兵错误(拍板2 2026-11-04):PLC 离线时退料拦截只作提示和警戒,
|
||
// 人工可强行确认退料。handler 据此(errors.Is)返回业务码 2409,前端弹风险提示二次确认。
|
||
// 2026-11-04 撤除包:撤「未使用」归类——退料数量允许手填/可改,以库管实际录入为准。
|
||
var ErrReturnForceConfirm = errors.New("工位传感器不可读(PLC 离线),无法确认工件已完工;如现场确认物料已离开工位,可强行退料")
|
||
|
||
// CheckReturnStationClear 退料前置检查(N5 + 拍板2 2026-11-04):
|
||
// - PLC 可读:无托盘 → (false, nil) 正常放行(按实际消耗算,不标「未使用」);
|
||
// 有托盘 → 硬拒"工件未完工,不允许退料"(工件在工位消耗未定,强行不适用——拍板2 仅放开 PLC 离线场景);
|
||
// - PLC 不可读(未接入/离线)→ (true, nil):不再一律拒绝,调用方未确认强行前以
|
||
// ErrReturnForceConfirm 拦截(业务码 2409),前端弹风险提示,人工确认后带 forceConfirm 重提放行。
|
||
func (s *Service) CheckReturnStationClear(ctx context.Context, stationNo int) (needForce bool, err error) {
|
||
if stationNo <= 0 {
|
||
return false, errors.New("缺少工位号,无法校验工件完工状态")
|
||
}
|
||
if s.ctx.PLC == nil || s.ctx.PLC.Get() == nil {
|
||
return true, nil
|
||
}
|
||
has, rerr := s.ctx.PLC.QueryStationPallet(ctx, stationNo)
|
||
if rerr != nil {
|
||
return true, nil // PLC 离线:拦截降级为提示+警戒,人工可强行确认
|
||
}
|
||
if has {
|
||
return false, errors.New("工件未完工,不允许退料")
|
||
}
|
||
return false, nil
|
||
}
|
||
|
||
// StationReturnMaterial 工位退料回库房:落事件日志 + 调 WMS 预建待确认退库单。
|
||
// returnDock(2026-11-03 拍板)= 退料接驳台(必填,退料托盘放置位置)。
|
||
// lossReason(2026-11-04 拍板)= 损耗原因(巡检退料一律必填,随退库单落 WMS loss_reason 可查)。
|
||
// transportMode(2026-11-04 定稿)= 运输方式 agv/manual(必填):agv=传感器检测到接驳台有托盘且
|
||
// 库房接驳台有空位时自动派车拉运(库管不点派车);manual=无 AGV,人工搬运,退料单照常等库管收货。
|
||
// 退库单在 WMS 侧由库管确认收货后库存加回(库管确认=物料进出库动作,不覆盖派车)。
|
||
// N5(2026-11-04 拍板)+ 拍板2:提交前校验工位传感器——无托盘正常退料;
|
||
// PLC 离线时拦截降级为提示,forceConfirm=true(前端弹风险提示后人工确认)方可放行;
|
||
// 传感器有托盘(工件在工位)始终硬拒,强行确认不适用。
|
||
// 2026-11-04 撤除包:撤「未使用」标记——退料数量允许手填/可改,以库管实际录入为准。
|
||
func (s *Service) StationReturnMaterial(ctx context.Context, orderNo string, stationNo int, sn, materialCode, materialName, spec, reason, operator, unit string, qty int, returnDock, lossReason, transportMode string, forceConfirm bool) error {
|
||
if stationNo <= 0 || materialCode == "" || qty <= 0 {
|
||
return errors.New("工位号、物料编码、退库数量必填")
|
||
}
|
||
if returnDock == "" {
|
||
return errors.New("退料必须指定接驳台(左/右)")
|
||
}
|
||
if transportMode != "agv" && transportMode != "manual" {
|
||
return errors.New("运输方式必选(AGV / 人工)")
|
||
}
|
||
needForce, gerr := s.CheckReturnStationClear(ctx, stationNo)
|
||
if gerr != nil {
|
||
return gerr
|
||
}
|
||
if needForce && !forceConfirm {
|
||
return ErrReturnForceConfirm
|
||
}
|
||
s.ctx.EventLog.Write(ctx, "station.return_material", orderNo, operator, "return_order",
|
||
materialCode, "工位退料回库房", map[string]any{"stationNo": stationNo, "qty": qty, "reason": reason, "returnDock": returnDock, "lossReason": lossReason, "transportMode": transportMode})
|
||
// 调 WMS 内部 API 预建退库单(失败降级:记录日志但不阻断工位操作)
|
||
if s.ctx.Wms != nil {
|
||
if err := s.ctx.Wms.CreateReturnOrder(ctx, orderNo, sn, materialCode, materialName, spec, reason, operator, stationNo, qty, unit, "", returnDock, lossReason, transportMode); err != nil {
|
||
return fmt.Errorf("通知 WMS 建退库单失败:%w", err)
|
||
}
|
||
}
|
||
return nil
|
||
}
|
||
|
||
// ListStationReturnOrders 工位终端「我的退料单」列表(WMS 数据转发;PENDING 可取消)。
|
||
func (s *Service) ListStationReturnOrders(ctx context.Context, stationNo int) ([]map[string]any, error) {
|
||
if s.ctx.Wms == nil {
|
||
return nil, errors.New("WMS 未连接")
|
||
}
|
||
return s.ctx.Wms.StationReturnList(ctx, stationNo)
|
||
}
|
||
|
||
// CancelStationReturn 工位取消自己发起的 PENDING 退料单(转发 WMS 校验 stationNo 归属后取消)。
|
||
func (s *Service) CancelStationReturn(ctx context.Context, returnNo string, stationNo int, operator, reason string) error {
|
||
if s.ctx.Wms == nil {
|
||
return errors.New("WMS 未连接")
|
||
}
|
||
return s.ctx.Wms.CancelReturnOrder(ctx, returnNo, stationNo, operator, reason)
|
||
}
|
||
|
||
// ============ 一键生成工序间检验记录(P0-4) ============
|
||
|
||
// InterProcessInspection 工序间检验记录汇总项
|
||
type InterProcessInspection struct {
|
||
OrderNo string `json:"orderNo"`
|
||
StationNo int `json:"stationNo"`
|
||
StepId int `json:"stepId"`
|
||
StepName string `json:"stepName"`
|
||
Sn string `json:"sn"`
|
||
Result string `json:"result"`
|
||
Measured string `json:"measuredValue"`
|
||
Photo string `json:"photo"`
|
||
Operator string `json:"operator"`
|
||
CreatedAt time.Time `json:"createdAt"`
|
||
// 装配物料绑定明细(按 SN 跨工艺汇总;客户需求:检验单附装配明细)
|
||
Binds []InspectionBindItem `json:"binds,omitempty"`
|
||
}
|
||
|
||
// GenerateInterProcessInspection 汇总某工单 PROCESS 类巡检记录(按步骤维度)生成工序间检验记录。
|
||
// 返回结构化列表,前端/H5 据此渲染打印(PDF/Excel);每条记录附该 SN 的装配物料绑定明细。
|
||
func (s *Service) GenerateInterProcessInspection(ctx context.Context, orderNo string) ([]*InterProcessInspection, error) {
|
||
if orderNo == "" {
|
||
return nil, errors.New("工单号必填")
|
||
}
|
||
if _, err := s.ctx.EntClient.WorkOrder.Query().Where(workorder.WorkOrderNo(orderNo)).First(ctx); err != nil {
|
||
return nil, errors.New("工单不存在:" + orderNo)
|
||
}
|
||
rows, err := s.ctx.EntClient.InspectionRecord.Query().
|
||
Where(
|
||
inspectionrecord.Category("PROCESS"),
|
||
inspectionrecord.OrderNo(orderNo),
|
||
).
|
||
Order(ent.Desc(inspectionrecord.FieldCreatedAt), ent.Desc(inspectionrecord.FieldID)).
|
||
All(ctx)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
// 同 SN 的绑定明细只查一次(多条巡检记录常指向同一工件)
|
||
bindCache := map[string][]InspectionBindItem{}
|
||
out := make([]*InterProcessInspection, 0, len(rows))
|
||
for _, r := range rows {
|
||
var binds []InspectionBindItem
|
||
if r.Sn != "" {
|
||
if _, ok := bindCache[r.Sn]; !ok {
|
||
bindCache[r.Sn] = s.inspectionBindItems(ctx, r.Sn)
|
||
}
|
||
binds = bindCache[r.Sn]
|
||
}
|
||
out = append(out, &InterProcessInspection{
|
||
OrderNo: r.OrderNo,
|
||
StationNo: atoiSafe(r.StationNo),
|
||
StepId: r.StepId,
|
||
StepName: r.StepName,
|
||
Sn: r.Sn,
|
||
Result: r.Result,
|
||
Measured: r.MeasuredValue,
|
||
Photo: r.Photo,
|
||
Operator: r.Operator,
|
||
CreatedAt: r.CreatedAt,
|
||
Binds: binds,
|
||
})
|
||
}
|
||
return out, nil
|
||
}
|