feat: 同步失败台账双向可重试 + 拧紧枪按工位配置 + 虚拟工位体系 + 去Redis与文档口径对齐
- 同步失败台账:MES/WMS 双端新增台账与页面,跨系统通知失败落台账、人工重试或标记已处理,不自动重传;重试前先判对端终态;叫料、配送、出库回写失败提示指向台账;补幂等建表脚本与菜单权限。 - 拧紧枪:新增按工位配置页,展示控制器地址与三把枪通道号(仅供查看,不参与采集校验);「允许手填」由全局开关改为每工位开关,关闭时校验该工位已配控制器;终端启动/登录上报配置并按本工位拉取,扭矩手填框按本工位置灰;补全局值迁移到各工位的脚本。 - 工位体系:终端未配置工位号时登录页可选(含虚拟工位),固定工位模式保留;虚拟/线下位不叫料、不退料、不作转产目标,未采集绑定放行并说明原因;叫料期望接驳台改为选填。 - AGV与出库:补「出库单已送达」回写;空车回充、接驳台传感器失联、配送状态更新失败均落预警或留痕并提示人工核对;人工出库不引入送达状态;出库单支持目标工位/接驳台并扩展导出列。 - 基础设施:看板缓存由Redis改为MES进程内实现,文档与启动脚本同步去Redis;扫码监听支持多地址、端口被占自动降级;PLC对账与工位报警在MES不可达时降级放行并留日志。 - 文档与帮助:案例与部署手册按实测订正(工位绑定、菜单名称、补料由班组长代提、菜单覆盖对照、虚拟工位);迁移脚本改为本地即时执行并登记7个已执行脚本;三端页面帮助改为「业务/操作/影响范围/后果」四要素。 - 测试:新增独立Go模块,按三个业务场景做真实HTTP与真实写库的流水测试,含运行报告。
This commit is contained in:
@@ -657,6 +657,12 @@ func (s *Service) notifyDeliveredOnArrival(ctx context.Context, t *ent.AgvTask)
|
||||
}
|
||||
if dErr := s.ctx.Wms.DeliveredOutbound(ctx, nos); dErr != nil {
|
||||
fmt.Println("出库单回写已送达失败(可忽略):", dErr)
|
||||
// WMS 侧本地回写失败返回 5xx 后,MES 调用方落 event_log 留痕;
|
||||
// 同时落台账供人工重试(best-effort,不阻断到位链路,不自动重试)
|
||||
s.ctx.EventLog.Write(ctx, "wms.delivered_outbound_failed", "", "system", "agv_task", t.TaskNo,
|
||||
"出库单已送达回写 WMS 失败:"+dErr.Error(), map[string]any{"outboundNos": nos})
|
||||
s.recordSyncFailure("outbound.delivered", t.TaskNo, "/api/internal/outbound/delivered",
|
||||
map[string]any{"outboundNos": nos}, dErr)
|
||||
return
|
||||
}
|
||||
s.ctx.EventLog.Write(ctx, "outbound.delivered", "", "AGV", "agv_task", t.TaskNo,
|
||||
@@ -860,6 +866,8 @@ func (s *Service) ConfirmUnloadRelease(ctx context.Context, taskNo, operator str
|
||||
if s.ctx.Wms != nil && t.RelatedRequestNo != "" {
|
||||
if derr := s.ctx.Wms.DeliveryDelivered(ctx, t.BizNo); derr != nil {
|
||||
fmt.Println("配送单送达回写失败(可人工改状态):", derr)
|
||||
s.recordSyncFailure("delivery.delivered", t.BizNo, "/api/internal/delivery/delivered",
|
||||
map[string]any{"deliveryNo": t.BizNo}, derr)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -869,6 +877,13 @@ func (s *Service) ConfirmUnloadRelease(ctx context.Context, taskNo, operator str
|
||||
Source: t.TargetDock, Target: agvChargePoint, Operator: operator, AutoEnd: true,
|
||||
}); derr != nil {
|
||||
fmt.Println("空车回充段下发失败(可人工调度):", derr)
|
||||
// 本地动作失败(非跨系统通知):落预警中心 + 操作日志,不进同步失败台账
|
||||
if aerr := s.CreateAlert(ctx, "agv_empty_return_fail", "警告", t.TaskNo,
|
||||
"AGV 任务 "+t.TaskNo+" 空车回充段下发失败:"+derr.Error()+"(请人工调度)"); aerr != nil {
|
||||
fmt.Println("空车回充失败落预警异常:", aerr)
|
||||
}
|
||||
s.ctx.EventLog.Write(ctx, "agv.empty_return_failed", "", "system", "agv_task", t.TaskNo,
|
||||
"空车回充段下发失败:"+derr.Error()+"(请人工调度)", nil)
|
||||
}
|
||||
s.ctx.EventLog.Write(ctx, "agv.unload_confirm", operator, "", "agv_task", taskNo,
|
||||
"卸货完成并放行,托盘落位 "+t.TargetDock+",空车自动回充", nil)
|
||||
|
||||
@@ -58,8 +58,23 @@ func (s *Service) preDispatchPointCheck(ctx context.Context, target, confirmForc
|
||||
return errors.New("目标点位 " + target + " 存在强提风险标记(PLC 离线期间强行提交过叫料),请确认现场安全后勾选人工确认重试")
|
||||
}
|
||||
// ③ 传感器↔锁比对(不一致落预警不拦车)
|
||||
// B8(2026-11-08):设备失联统一降级为「提示 + 人工确认 + 留痕」(案例.md:450/550)。
|
||||
// 旧实现把 PLC 读取错误 err!=nil 静默吞掉(fail-open 无任何留痕),现改为:
|
||||
// 先落预警中心 + 操作日志留痕,再要求人工确认;确认后放行,未确认则拦截并提示。
|
||||
// 系统不做任何自动补偿,单据与台账照常落库,现场状态由人工目视兜底。
|
||||
if !pt.Locked && pt.IsDock && s.ctx.PLC != nil && !s.ctx.PLC.IsMock() {
|
||||
if has, err := s.ctx.PLC.Get().QueryDockPallet(ctx, target); err == nil && has {
|
||||
has, derr := s.ctx.PLC.Get().QueryDockPallet(ctx, target)
|
||||
if derr != nil {
|
||||
s.ctx.EventLog.Write(ctx, "agv.plc_unavailable", "system", "", "agv_point", target,
|
||||
"派车前读取接驳台传感器失败(设备失联):"+derr.Error()+",已降级为人工确认", nil)
|
||||
_ = s.CreateAlert(ctx, "agv_plc_unavailable", "警告", target,
|
||||
"派车前无法读取接驳台 "+target+" 的传感器(PLC 不可达),需人工核对现场托盘状态后确认派车")
|
||||
if confirmForcePick != "1" {
|
||||
return fmt.Errorf("设备失联:无法读取接驳台 %s 的传感器(%v)。请人工确认现场安全后勾选人工确认重试派车;系统不做自动补偿", target, derr)
|
||||
}
|
||||
return nil // 已人工确认:放行并留痕
|
||||
}
|
||||
if has {
|
||||
s.ctx.EventLog.Write(ctx, "agv.point_sensor_mismatch", "system", "", "agv_point", target,
|
||||
"派车前比对:点位未锁定但传感器检测到托盘(无在途任务占用),请现场核实", nil)
|
||||
}
|
||||
|
||||
@@ -184,6 +184,30 @@ type StationLoginResp struct {
|
||||
StationNo int `json:"stationNo"`
|
||||
}
|
||||
|
||||
// StationOption 登录页工位选项(2026-11-09 拍板:未配置工位的终端登录页可选实体+虚拟工位)。
|
||||
type StationOption struct {
|
||||
StationNo int `json:"stationNo"`
|
||||
Name string `json:"name"`
|
||||
StationType string `json:"stationType"` // LINE 实体工位 / OFFLINE 虚拟工位(0 上线位、13 下线位)
|
||||
}
|
||||
|
||||
// StationOptions 登录页可选工位清单:全部启用工位(实体+虚拟),按工位号升序。
|
||||
// 未配置固定工位的终端在登录前调用(免用户态,X-API-TOKEN 保护)。
|
||||
func (s *Service) StationOptions(ctx context.Context) ([]StationOption, error) {
|
||||
sts, err := s.ctx.EntClient.Station.Query().
|
||||
Where(station.Status("ENABLED")).Order(station.ByStationNo()).All(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := make([]StationOption, 0, len(sts))
|
||||
for _, st := range sts {
|
||||
out = append(out, StationOption{
|
||||
StationNo: st.StationNo, Name: st.Name, StationType: st.StationType,
|
||||
})
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// StationLogin 工位终端登录:校验 用户名 + 工位终端密码 + 工位权限
|
||||
func (s *Service) StationLogin(ctx context.Context, req StationLoginReq) (StationLoginResp, error) {
|
||||
var resp StationLoginResp
|
||||
|
||||
@@ -7,10 +7,10 @@ import (
|
||||
"golang.org/x/sync/singleflight"
|
||||
)
|
||||
|
||||
// memCache 进程内 TTL 缓存(2026-11-05 由 Redis 看板缓存等价迁移):
|
||||
// memCache 进程内 TTL 缓存:
|
||||
// MES 为单机单实例部署,看板缓存无需跨进程共享,内存缓存等价替代且少一个外部依赖。
|
||||
// 防击穿:singleflight 保证同一 key 并发重建只放行一个,其余调用方共享同一份结果
|
||||
// (等价于原 Redis SETNX 锁 + 轮询等待,且无需手写重试循环)。
|
||||
// (无需手写重试循环,也不依赖任何外部服务)。
|
||||
type memCache struct {
|
||||
mu sync.Mutex
|
||||
items map[string]*memItem
|
||||
@@ -26,7 +26,7 @@ type memItem struct {
|
||||
var dashboardCache = &memCache{items: make(map[string]*memItem)}
|
||||
|
||||
// getOrBuild 命中未过期直接返回缓存值;否则经 singleflight 重建并写回。
|
||||
// build 返回错误时不写缓存,下次请求重试(与原 Redis 版行为一致)。
|
||||
// build 返回错误时不写缓存,下次请求重试。
|
||||
func (c *memCache) getOrBuild(key string, ttl time.Duration, build func() (any, error)) (any, error) {
|
||||
c.mu.Lock()
|
||||
if it, ok := c.items[key]; ok && time.Now().Before(it.expire) {
|
||||
|
||||
@@ -468,6 +468,11 @@ func (s *Service) CreateCallOrders(ctx context.Context, stationNo int, items []C
|
||||
if st.Status == "DISABLED" {
|
||||
return 0, errors.New("工位已停用,不能叫料")
|
||||
}
|
||||
// 2026-11-09 拍板:虚拟/无接驳台工位不支持叫料——工位无启用 line 接驳台(=虚拟/线下位,判据同
|
||||
// 2026-11-08 B2 报工绑定拦截 stationHasEnabledLineDock)时拒绝,防 API 直调产生无台可送的叫料单。
|
||||
if !s.stationHasEnabledLineDock(ctx, s.ctx.EntClient, stationNo) {
|
||||
return 0, errors.New("该工位无接驳台(虚拟/线下工位),不支持叫料")
|
||||
}
|
||||
// 同工序候选工位(同 FlowId、启用、非本工位):本工位不足时向其借用剩余额度
|
||||
sibNos := []int{}
|
||||
if st.FlowId > 0 {
|
||||
@@ -854,6 +859,11 @@ func (s *Service) RequestReturn(ctx context.Context, requestNo string, stationNo
|
||||
"叫料单退库", operator, mr.StationNo, int(mr.ReqQty), mr.Unit, requestNo, mr.TargetDock, "", "agv"); werr != nil {
|
||||
return fmt.Errorf("通知 WMS 建退库单失败:%w", werr)
|
||||
}
|
||||
} else {
|
||||
// 配置缺失(2026-11-08):不拒绝本地退库,但必须落预警中心,避免形成
|
||||
// 无对应 WMS 单据的孤儿 RETURNING 状态而不被业务人员察觉。
|
||||
_ = s.CreateAlert(ctx, "wms_not_configured", "警告", requestNo,
|
||||
"叫料单 "+requestNo+" 申请退库时 WMS 未配置,已跳过创建 WMS 退库单(本地仍置 RETURNING),请人工跟进")
|
||||
}
|
||||
if _, uerr := s.ctx.EntClient.MaterialRequest.UpdateOneID(mr.ID).SetStatus("RETURNING").Save(ctx); uerr != nil {
|
||||
return uerr
|
||||
|
||||
@@ -179,6 +179,7 @@ func (s *Service) CreateInspection(ctx context.Context, req InspectionReq, opera
|
||||
}
|
||||
if err := s.ctx.Wms.DisposeInventory(ctx, wreq); err != nil {
|
||||
msg = "检验记录已保存;库存联动失败: " + err.Error()
|
||||
s.recordSyncFailure("inspection.dispose", req.Sn, "/api/internal/inventory/dispose", wreq, err)
|
||||
s.ctx.EventLog.Write(ctx, "inspection.wms_dispose_failed", req.OrderNo, req.Operator,
|
||||
"inspection_record", req.Sn, "库存联动失败 "+err.Error(),
|
||||
map[string]any{"materialCode": req.MaterialCode, "batchSn": req.BatchSn,
|
||||
@@ -380,6 +381,15 @@ func (s *Service) finishedInspectionInbound(ctx context.Context, rec *ent.Inspec
|
||||
"完工检合格,但WMS提示成品SN已入库(防重拦截),请勿重复入库", map[string]any{"productCode": wo.ProductCode, "productStatus": productStatus})
|
||||
return "检验记录已保存;该成品已入WMS库,请勿重复入库"
|
||||
}
|
||||
s.recordSyncFailure("finished.inbound", wp.Sn, "/api/internal/finished/inbound",
|
||||
map[string]any{
|
||||
"productCode": wo.ProductCode, "productName": wo.ProductName, "sn": wp.Sn,
|
||||
"orderNo": wp.OrderNo, "snSource": "AUTO", "operator": rec.Operator,
|
||||
"productStatus": productStatus, "qualityStatus": qualityStatusForWMS,
|
||||
"relatedStandard": rec.RelatedStandard, "testRecordNo": rec.TestRecordNo,
|
||||
"ownershipType": wo.OwnershipType, "ownershipNo": wo.ContractNo,
|
||||
"productWorkNo": wo.WorkOrderNo,
|
||||
}, wmsErr)
|
||||
s.ctx.EventLog.Write(ctx, "wms.finished.inbound_failed", wp.OrderNo, rec.Operator, "workpiece", wp.Sn,
|
||||
"完工检合格但成品回流WMS入库失败(库存未入,请人工补录)",
|
||||
map[string]any{"error": wmsErr.Error(), "productCode": wo.ProductCode, "productStatus": productStatus})
|
||||
|
||||
@@ -25,9 +25,9 @@ func (s *Service) notifyDashboard() {
|
||||
}
|
||||
}
|
||||
|
||||
// cachedTyped 看板进程内缓存(2026-11-05 由 Redis 等价迁移):
|
||||
// cachedTyped 看板进程内缓存:
|
||||
// TTL + singleflight 防击穿,返回强类型,调用方不必再做类型断言(看板快照等结构化数据用这个)。
|
||||
// 原 Redis 版的零调用死代码 cached(any) 已一并删除。
|
||||
// 零调用的旧 cached(any) 死代码已一并删除。
|
||||
func cachedTyped[T any](ctx context.Context, s *Service, key string, ttl int, build func() (T, error)) (T, error) {
|
||||
v, err := dashboardCache.getOrBuild(key, time.Duration(ttl)*time.Second, func() (any, error) {
|
||||
return build()
|
||||
|
||||
@@ -3,7 +3,9 @@ package logic
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"bj_power_mes/ent"
|
||||
@@ -204,10 +206,19 @@ func (s *Service) PullMaterialsFromWMSAsync() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
||||
defer cancel()
|
||||
total := 0
|
||||
failed := 0
|
||||
var failedCodes []string
|
||||
for page := 1; page <= 100; page++ {
|
||||
_, rows, err := s.ctx.Wms.MaterialPage(ctx, "", "", "", 0, 0, page, 200)
|
||||
if err != nil {
|
||||
log.Printf("[material-sync] 拉取 WMS 物料失败(第 %d 页): %v", page, err)
|
||||
// 启动期同步不是单据级回调:落预警中心 + 操作日志,不进同步失败台账(无可重放端点)
|
||||
if aerr := s.CreateAlert(ctx, "material_sync_pull_failed", "警告", "物料档案",
|
||||
"冷启动从 WMS 全量拉取物料失败:"+err.Error()+"(请人工确认两库物料一致)"); aerr != nil {
|
||||
log.Printf("[material-sync] 落预警中心失败: %v", aerr)
|
||||
}
|
||||
s.ctx.EventLog.Write(ctx, "material.sync.pull_failed", "", "system", "material", "",
|
||||
"冷启动从 WMS 全量拉取物料失败:"+err.Error(), nil)
|
||||
return
|
||||
}
|
||||
for _, r := range rows {
|
||||
@@ -218,6 +229,10 @@ func (s *Service) PullMaterialsFromWMSAsync() {
|
||||
SafetyStock: r.SafetyStock, IsActive: &active,
|
||||
}); err != nil {
|
||||
log.Printf("[material-sync] 物料 %s 落库失败: %v", r.Code, err)
|
||||
failed++
|
||||
if len(failedCodes) < 20 {
|
||||
failedCodes = append(failedCodes, r.Code)
|
||||
}
|
||||
continue
|
||||
}
|
||||
total++
|
||||
@@ -227,6 +242,16 @@ func (s *Service) PullMaterialsFromWMSAsync() {
|
||||
}
|
||||
}
|
||||
log.Printf("[material-sync] 自 WMS 全量同步物料 %d 行", total)
|
||||
// 2026-11-09 缺陷修复:单行落库失败此前只打日志无 DB 留痕,冷启动后两库不一致无人察觉。
|
||||
// 汇总一条操作日志 + 预警中心(启动期同步非单据级回调,无可重放端点,不进同步失败台账)。
|
||||
if failed > 0 {
|
||||
detail := fmt.Sprintf("冷启动从 WMS 拉取物料:共 %d 行落库失败(%s%s),请人工核对两库物料一致",
|
||||
failed, strings.Join(failedCodes, ","), map[bool]string{true: " 等", false: ""}[len(failedCodes) == 20])
|
||||
if aerr := s.CreateAlert(ctx, "material_sync_pull_failed", "警告", "物料档案", detail); aerr != nil {
|
||||
log.Printf("[material-sync] 落预警中心失败: %v", aerr)
|
||||
}
|
||||
s.ctx.EventLog.Write(ctx, "material.sync.row_failed", "", "system", "material", "", detail, nil)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
|
||||
@@ -148,6 +148,8 @@ func (s *Service) AutoDispatchReturnAGV(ctx context.Context) (int, error) {
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
if merr = s.ctx.Wms.MarkReturnDispatched(ctx, ro.ReturnNo); merr != nil {
|
||||
logx.Errorf("退料单 %s 派车标记回写失败(重试后仍失败): %v", ro.ReturnNo, merr)
|
||||
s.recordSyncFailure("return.dispatch", ro.ReturnNo, "/api/internal/return-order/mark-dispatched",
|
||||
map[string]any{"returnNo": ro.ReturnNo}, merr)
|
||||
}
|
||||
}
|
||||
s.ctx.EventLog.Write(ctx, "agv.return_auto", "", "传感器自动派车", "agv_task", ro.ReturnDock,
|
||||
|
||||
@@ -118,9 +118,13 @@ func (s *Service) Seed(ctx context.Context) error {
|
||||
{"sys.role", "角色管理", "MENU", "/role"},
|
||||
// 基础设置:WMS 地址(热切换)/需求窗口天数/外推参数/事件日志保留天数 + 本机信息展示
|
||||
{"sys.settings", "基础设置", "MENU", "/settings"},
|
||||
// 拧紧枪配置(2026-11-09):控制器地址 + 枪1/2/3 通道号仅展示,不参与拧紧数据收集
|
||||
{"sys.torqueconfig", "拧紧枪配置", "MENU", "/torque-gun-config"},
|
||||
// 预警中心 / 附件中心此前只有前端菜单码(MainLayout menuGroups)、权限表未登记,
|
||||
// 导致这两个菜单无法授权给任何角色(只有 SUPER_ADMIN 靠 roleCode 旁路可见),2026-09-19 补齐。
|
||||
{"sys.alert", "预警中心", "MENU", "/alert-center"},
|
||||
// 同步失败台账(2026-11-08):跨系统通知失败人工重试,不自动重传
|
||||
{"sys.syncfailure", "同步失败台账", "MENU", "/sync-failure"},
|
||||
{"sys.attachment", "附件中心", "MENU", "/attachment"},
|
||||
// 数据归档(备份到 U 盘 + 按时间窗物理清理):仅管理员可用,OPERATOR/INSPECTOR 不授予
|
||||
{"sys.backup", "数据归档", "MENU", "/data-archive"},
|
||||
@@ -215,6 +219,8 @@ func (s *Service) Seed(ctx context.Context) error {
|
||||
// 附件中心(对齐 WMS attachment:manage 口径):删除 + 归档清理
|
||||
{"sys.attachment:manage", "附件-删除/归档清理", "BUTTON", "", "sys.attachment"},
|
||||
{"sys.settings:edit", "基础设置-修改", "BUTTON", "", "sys.settings"},
|
||||
// 同步失败台账(2026-11-08):人工重试/标记已处理,与 perm.go 的 POST 路径映射同码
|
||||
{"sys.syncfailure:retry", "同步失败台账-重试/标记已处理", "BUTTON", "", "sys.syncfailure"},
|
||||
// PLC↔MES 对账结果「忽略确认」(#8 T7,管理员级):仅超级管理员(*)持有,操作员不给;
|
||||
// 前端入口在预警中心(sys.alert),parent 挂 sys.alert。
|
||||
{"produce.plc.mismatch:override", "PLC对账-忽略确认", "BUTTON", "", "sys.alert"},
|
||||
|
||||
@@ -67,6 +67,11 @@ func (s *Service) SemiFlow(ctx context.Context, req SemiReq, operator string) er
|
||||
wmsErr = s.ctx.Wms.SemiOutbound(ctx, payload)
|
||||
}
|
||||
if wmsErr != nil {
|
||||
endpoint := "/api/internal/semi/inbound"
|
||||
if action != "INBOUND" {
|
||||
endpoint = "/api/internal/semi/outbound"
|
||||
}
|
||||
s.recordSyncFailure("semi.flow", req.Sn+"|"+action, endpoint, payload, wmsErr)
|
||||
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
|
||||
|
||||
@@ -49,8 +49,7 @@ const (
|
||||
CfgAgvUnloadTimeoutMinutes = "agv.unload.timeout.minutes"
|
||||
// CfgMultiOrderCachePolicy 多工单缓存策略:physical=物理优先(目标点位实际空闲即可放);strict=严格隔离(多工单物料禁共位);默认 physical
|
||||
CfgMultiOrderCachePolicy = "cache.multi.order.policy"
|
||||
// CfgTorqueManualInput 拧紧枪数据输入:forbid=禁止手填(必须真实采集,默认);allow=允许手填(离线/无枪时手工录扭矩)
|
||||
CfgTorqueManualInput = "torque.manual.input"
|
||||
// 2026-11-09:全局 torque.manual.input 开关已废弃;「允许手填」改由拧紧枪配置页按工位维护,见 torque_gun_config.go。
|
||||
)
|
||||
|
||||
// startedAt 服务启动时刻(本机信息展示用)
|
||||
@@ -74,7 +73,6 @@ type SettingsVO struct {
|
||||
DashboardVoice bool `json:"dashboardVoice"` // 门口看板语音播报开关
|
||||
AgvUnloadTimeoutMinutes int `json:"agvUnloadTimeoutMinutes"` // AGV 卸货超时报警(分钟,默认 5)
|
||||
MultiOrderCachePolicy string `json:"multiOrderCachePolicy"` // 多工单缓存策略:physical/strict
|
||||
TorqueManualInput string `json:"torqueManualInput"` // 拧紧枪数据输入:forbid/allow
|
||||
|
||||
// 本机基础信息(只读)
|
||||
LocalIPs []string `json:"localIPs"` // 本机内网 IPv4 列表
|
||||
@@ -102,7 +100,6 @@ type SettingsReq struct {
|
||||
DashboardVoice *bool `json:"dashboardVoice"` // 门口看板语音播报开关(nil=不改)
|
||||
AgvUnloadTimeoutMinutes *int `json:"agvUnloadTimeoutMinutes"` // AGV 卸货超时报警(分钟,nil=不改)
|
||||
MultiOrderCachePolicy *string `json:"multiOrderCachePolicy"` // 多工单缓存策略(nil=不改)
|
||||
TorqueManualInput *string `json:"torqueManualInput"` // 拧紧枪数据输入(nil=不改)
|
||||
}
|
||||
|
||||
// defaults 建议默认值(未配置时生效)
|
||||
@@ -145,7 +142,6 @@ func (s *Service) GetSettings(ctx context.Context) (*SettingsVO, error) {
|
||||
DashboardVoice: s.CfgInt(ctx, CfgDashboardVoice, 1) == 1,
|
||||
AgvUnloadTimeoutMinutes: cfgIntFrom(kv, CfgAgvUnloadTimeoutMinutes),
|
||||
MultiOrderCachePolicy: cfgStrFrom(kv, CfgMultiOrderCachePolicy, "physical"),
|
||||
TorqueManualInput: cfgStrFrom(kv, CfgTorqueManualInput, "forbid"),
|
||||
LocalIPs: localIPv4s(),
|
||||
ListenAddr: fmt.Sprintf("%s:%d", s.ctx.Config.Host, s.ctx.Config.Port),
|
||||
DBInfo: fmt.Sprintf("%s:%d/%s", s.ctx.Config.Database.Host, s.ctx.Config.Database.Port, s.ctx.Config.Database.Dbname),
|
||||
@@ -239,15 +235,8 @@ func (s *Service) SaveSettings(ctx context.Context, req SettingsReq, operator st
|
||||
return err
|
||||
}
|
||||
}
|
||||
if req.TorqueManualInput != nil {
|
||||
t := strings.TrimSpace(*req.TorqueManualInput)
|
||||
if t != "allow" && t != "forbid" {
|
||||
return errors.New("拧紧枪数据输入仅支持 forbid(禁止手填)或 allow(允许手填)")
|
||||
}
|
||||
if err := s.upsertConfig(ctx, CfgTorqueManualInput, t, "拧紧枪数据输入(forbid=禁止手填,必须真实采集;allow=允许手填,离线/无枪时手工录扭矩)"); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
// 2026-11-09:「允许手填」改由「拧紧枪配置」页按工位维护(关闭时校验本工位控制器地址),
|
||||
// 基础设置页不再保存该全局开关。
|
||||
payload := map[string]any{"req": req}
|
||||
s.ctx.EventLog.Write(ctx, "sys.settings.save", "", operator, "sys_config", "", "保存基础设置", payload)
|
||||
return nil
|
||||
|
||||
@@ -158,6 +158,11 @@ func (s *Service) StationReturnMaterial(ctx context.Context, orderNo string, sta
|
||||
if stationNo <= 0 || materialCode == "" || qty <= 0 {
|
||||
return errors.New("工位号、物料编码、退库数量必填")
|
||||
}
|
||||
// 2026-11-09 拍板:虚拟/无接驳台工位不支持退料(判据同 B2 stationHasEnabledLineDock)——
|
||||
// 无接驳台即无托盘可放置,前端物料页签已隐藏,此处防 API 直调。
|
||||
if !s.stationHasEnabledLineDock(ctx, s.ctx.EntClient, stationNo) {
|
||||
return errors.New("该工位无接驳台(虚拟/线下工位),不支持退料")
|
||||
}
|
||||
if returnDock == "" {
|
||||
return errors.New("退料必须指定接驳台(左/右)")
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package logic
|
||||
|
||||
import "bj_power_mes/internal/syncfailure"
|
||||
|
||||
// recordSyncFailure 跨系统通知失败落台账(MES→WMS 方向)。
|
||||
// 口径:① 本地事务已提交才记;② best-effort,表未建/写失败只打日志,绝不阻断业务;
|
||||
// ③ 不自动重试,由「同步失败台账」页人工点击重试;④ 重试只重放 endpoint+payload,不重跑本地业务。
|
||||
func (s *Service) recordSyncFailure(bizType, bizKey, endpoint string, payload any, err error) {
|
||||
msg := ""
|
||||
if err != nil {
|
||||
msg = err.Error()
|
||||
}
|
||||
syncfailure.Record(s.ctx.SQLDB, "MES→WMS", bizType, bizKey, endpoint, payload, msg)
|
||||
}
|
||||
@@ -0,0 +1,259 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"bj_power_mes/ent"
|
||||
"bj_power_mes/ent/station"
|
||||
"bj_power_mes/ent/sysconfig"
|
||||
)
|
||||
|
||||
// 拧紧枪配置(阿特拉斯 Open Protocol)—— MES 侧「按工位」展示与配置记录。
|
||||
//
|
||||
// 设计边界(2026-11-09 用户拍板,勿扩大):
|
||||
// - 三把枪通道号仅供配置与查看,不参与任何拧紧数据的区分 / 校验 / 拦截;
|
||||
// 即便采集到的数据不在本配置内也不拦截,torque_record / step_data 不加列、采集链路一行不动。
|
||||
// - 「允许手填」按工位配置(2026-11-09 改):虚拟工位不连控制器必须能手填,实体工位连控制器可禁止;
|
||||
// 与控制器地址、通道号存在同一条工位记录里。已废弃全局 torque.manual.input,不再作为判据。
|
||||
// - 页面编辑只写 MES 侧展示记录,不下发工位终端、不影响真实控制器连接。
|
||||
// - 工位终端启动时上报本终端 yaml 的 Tightening 段作为初始值(best-effort,不覆盖人工编辑)。
|
||||
// - 存储复用 sys_config(每工位一个 key,避免 value 512 字节上限),不新建表、不改 ent schema。
|
||||
const CfgTorqueGunPrefix = "torque.gun.config."
|
||||
|
||||
const (
|
||||
ManualInputAllow = "allow" // 允许手填(默认;拧紧枪离线/无枪时开放人工录入)
|
||||
ManualInputForbid = "forbid" // 禁止手填(扭矩必须真实采集,须本工位已配控制器)
|
||||
)
|
||||
|
||||
// TorqueGunConfig 单个工位的拧紧枪展示配置。
|
||||
type TorqueGunConfig struct {
|
||||
StationNo int `json:"stationNo"`
|
||||
StationName string `json:"stationName"`
|
||||
StationType string `json:"stationType"`
|
||||
|
||||
// 页面可编辑(仅 MES 侧展示记录)
|
||||
ControllerAddr string `json:"controllerAddr"`
|
||||
Gun1Channel string `json:"gun1Channel"`
|
||||
Gun2Channel string `json:"gun2Channel"`
|
||||
Gun3Channel string `json:"gun3Channel"`
|
||||
ManualInput string `json:"manualInput"` // 本工位允许手填:allow / forbid
|
||||
|
||||
// 终端上报的 yaml 配置快照(只读展示,页面不可编辑)
|
||||
Enable bool `json:"enable"`
|
||||
CellID string `json:"cellId"`
|
||||
ChannelID string `json:"channelId"`
|
||||
StationID string `json:"stationId"`
|
||||
OperatorID string `json:"operatorId"`
|
||||
HeartbeatSec int64 `json:"heartbeatSec"`
|
||||
ReportedAt string `json:"reportedAt"`
|
||||
}
|
||||
|
||||
// TorqueGunConfigVO 列表视图:一工位一行,允许手填状态随行返回(不再有全局字段)。
|
||||
type TorqueGunConfigVO struct {
|
||||
Items []TorqueGunConfig `json:"items"`
|
||||
}
|
||||
|
||||
// TorqueGunReportReq 工位终端启动时上报的 yaml 配置快照。
|
||||
type TorqueGunReportReq struct {
|
||||
StationNo int `json:"stationNo"`
|
||||
Enable bool `json:"enable"`
|
||||
ControllerAddr string `json:"controllerAddr"`
|
||||
CellID string `json:"cellId"`
|
||||
ChannelID string `json:"channelId"`
|
||||
StationID string `json:"stationId"`
|
||||
OperatorID string `json:"operatorId"`
|
||||
HeartbeatSec int64 `json:"heartbeatSec"`
|
||||
}
|
||||
|
||||
// gunConfigStored 实际落盘 sys_config.value 的字段(不含工位名等派生字段)。
|
||||
type gunConfigStored struct {
|
||||
ControllerAddr string `json:"controllerAddr"`
|
||||
Gun1Channel string `json:"gun1Channel"`
|
||||
Gun2Channel string `json:"gun2Channel"`
|
||||
Gun3Channel string `json:"gun3Channel"`
|
||||
ManualInput string `json:"manualInput"`
|
||||
Enable bool `json:"enable"`
|
||||
CellID string `json:"cellId"`
|
||||
ChannelID string `json:"channelId"`
|
||||
StationID string `json:"stationId"`
|
||||
OperatorID string `json:"operatorId"`
|
||||
HeartbeatSec int64 `json:"heartbeatSec"`
|
||||
ReportedAt string `json:"reportedAt"`
|
||||
}
|
||||
|
||||
func torqueGunKey(stationNo int) string {
|
||||
return CfgTorqueGunPrefix + strconv.Itoa(stationNo)
|
||||
}
|
||||
|
||||
// normalizeManualInput 空值 / 非法值一律回落 allow(无控制器工位必须能手填)。
|
||||
func normalizeManualInput(v string) string {
|
||||
if strings.TrimSpace(v) == ManualInputForbid {
|
||||
return ManualInputForbid
|
||||
}
|
||||
return ManualInputAllow
|
||||
}
|
||||
|
||||
// gunConfigMap 一次读出全部工位的展示配置(key 前缀过滤,不用正则)。
|
||||
func (s *Service) gunConfigMap(ctx context.Context) map[int]gunConfigStored {
|
||||
out := map[int]gunConfigStored{}
|
||||
rows, err := s.ctx.EntClient.SysConfig.Query().All(ctx)
|
||||
if err != nil {
|
||||
return out
|
||||
}
|
||||
for _, r := range rows {
|
||||
if !strings.HasPrefix(r.Key, CfgTorqueGunPrefix) {
|
||||
continue
|
||||
}
|
||||
n, err := strconv.Atoi(strings.TrimPrefix(r.Key, CfgTorqueGunPrefix))
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
var v gunConfigStored
|
||||
if json.Unmarshal([]byte(r.Value), &v) == nil {
|
||||
out[n] = v
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// GetTorqueGunConfig 按工位读取展示配置;工位真源 = station 表(不硬编码 1~12)。
|
||||
func (s *Service) GetTorqueGunConfig(ctx context.Context) (*TorqueGunConfigVO, error) {
|
||||
sts, err := s.ctx.EntClient.Station.Query().Order(ent.Asc(station.FieldStationNo)).All(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cfg := s.gunConfigMap(ctx)
|
||||
items := make([]TorqueGunConfig, 0, len(sts))
|
||||
for _, st := range sts {
|
||||
it := TorqueGunConfig{
|
||||
StationNo: st.StationNo,
|
||||
StationName: st.Name,
|
||||
StationType: st.StationType,
|
||||
ManualInput: ManualInputAllow, // 无记录(含全部虚拟工位)默认允许手填
|
||||
}
|
||||
if v, ok := cfg[st.StationNo]; ok {
|
||||
it.ControllerAddr = v.ControllerAddr
|
||||
it.Gun1Channel = v.Gun1Channel
|
||||
it.Gun2Channel = v.Gun2Channel
|
||||
it.Gun3Channel = v.Gun3Channel
|
||||
it.ManualInput = normalizeManualInput(v.ManualInput)
|
||||
it.Enable = v.Enable
|
||||
it.CellID = v.CellID
|
||||
it.ChannelID = v.ChannelID
|
||||
it.StationID = v.StationID
|
||||
it.OperatorID = v.OperatorID
|
||||
it.HeartbeatSec = v.HeartbeatSec
|
||||
it.ReportedAt = v.ReportedAt
|
||||
}
|
||||
items = append(items, it)
|
||||
}
|
||||
return &TorqueGunConfigVO{Items: items}, nil
|
||||
}
|
||||
|
||||
// SaveTorqueGunConfig 只写 MES 侧展示记录:不校验通道号取值、不下发终端、不触碰拧紧数据链路。
|
||||
// 「允许手填」按工位:关闭某工位手填时须该工位已填控制器地址,否则拒绝(一次收全所有工位问题)。
|
||||
func (s *Service) SaveTorqueGunConfig(ctx context.Context, items []TorqueGunConfig, operator string) error {
|
||||
if len(items) == 0 {
|
||||
return errors.New("没有可保存的工位配置")
|
||||
}
|
||||
old := s.gunConfigMap(ctx)
|
||||
type pending struct {
|
||||
key string
|
||||
value gunConfigStored
|
||||
}
|
||||
list := make([]pending, 0, len(items))
|
||||
errs := make([]string, 0)
|
||||
for _, it := range items {
|
||||
if it.StationNo <= 0 {
|
||||
continue
|
||||
}
|
||||
v := old[it.StationNo] // 保留终端上报的快照字段
|
||||
v.ControllerAddr = strings.TrimSpace(it.ControllerAddr)
|
||||
v.Gun1Channel = strings.TrimSpace(it.Gun1Channel)
|
||||
v.Gun2Channel = strings.TrimSpace(it.Gun2Channel)
|
||||
v.Gun3Channel = strings.TrimSpace(it.Gun3Channel)
|
||||
v.ManualInput = normalizeManualInput(it.ManualInput)
|
||||
if v.ManualInput == ManualInputForbid && v.ControllerAddr == "" {
|
||||
errs = append(errs, fmt.Sprintf("工位%d:该工位未配置控制器,不能关闭允许手填", it.StationNo))
|
||||
continue
|
||||
}
|
||||
b, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// 仅做存储层长度保护(sys_config.value 上限 512 字节,schema/sys_config.go:24),
|
||||
// 不是业务校验:通道号内容、格式、是否与实际采集一致一律不检查。
|
||||
if len(b) > 480 {
|
||||
errs = append(errs, fmt.Sprintf("工位%d 配置内容过长,请缩短控制器地址或通道号", it.StationNo))
|
||||
continue
|
||||
}
|
||||
list = append(list, pending{key: torqueGunKey(it.StationNo), value: v})
|
||||
}
|
||||
if len(errs) > 0 {
|
||||
return fmt.Errorf("保存失败,共 %d 项需要处理:%s", len(errs), strings.Join(errs, ";"))
|
||||
}
|
||||
if len(list) == 0 {
|
||||
return errors.New("没有可保存的工位配置")
|
||||
}
|
||||
for _, p := range list {
|
||||
b, _ := json.Marshal(p.value)
|
||||
if err := s.upsertConfig(ctx, p.key, string(b),
|
||||
"拧紧枪展示配置(控制器地址 + 枪1/2/3 通道号 + 本工位允许手填);仅供查看,不参与拧紧数据收集"); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
s.ctx.EventLog.Write(ctx, "sys.torqueconfig.save", "", operator, "sys_config", "",
|
||||
"保存拧紧枪配置(按工位允许手填,仅展示记录)", map[string]any{"stations": len(list)})
|
||||
return nil
|
||||
}
|
||||
|
||||
// StationTorqueManualInput 供工位终端按自己工位号拉取「允许手填」(替代已废弃的全局开关)。
|
||||
// 无记录 / 无控制器的工位(含虚拟工位)一律 allow,避免终端既不能手填又没有控制器可采集。
|
||||
func (s *Service) StationTorqueManualInput(ctx context.Context, stationNo int) string {
|
||||
if stationNo <= 0 {
|
||||
return ManualInputAllow
|
||||
}
|
||||
v, ok := s.gunConfigMap(ctx)[stationNo]
|
||||
if !ok {
|
||||
return ManualInputAllow
|
||||
}
|
||||
mi := normalizeManualInput(v.ManualInput)
|
||||
if mi == ManualInputForbid && strings.TrimSpace(v.ControllerAddr) == "" {
|
||||
return ManualInputAllow
|
||||
}
|
||||
return mi
|
||||
}
|
||||
|
||||
// ReportTorqueGunConfig 工位终端启动时上报 yaml 的 Tightening 段,作为 MES 展示记录的初始值。
|
||||
// 人工编辑优先:该工位已有展示记录时直接跳过,不覆盖页面配置。终端侧 best-effort,失败不阻断启动。
|
||||
func (s *Service) ReportTorqueGunConfig(ctx context.Context, req TorqueGunReportReq) error {
|
||||
if req.StationNo <= 0 {
|
||||
return errors.New("工位号非法")
|
||||
}
|
||||
exist, err := s.ctx.EntClient.SysConfig.Query().
|
||||
Where(sysconfig.Key(torqueGunKey(req.StationNo))).Only(ctx)
|
||||
if err == nil && exist != nil {
|
||||
return nil
|
||||
}
|
||||
v := gunConfigStored{
|
||||
ControllerAddr: strings.TrimSpace(req.ControllerAddr),
|
||||
Gun1Channel: strings.TrimSpace(req.ChannelID),
|
||||
ManualInput: ManualInputAllow,
|
||||
Enable: req.Enable,
|
||||
CellID: req.CellID,
|
||||
ChannelID: req.ChannelID,
|
||||
StationID: req.StationID,
|
||||
OperatorID: req.OperatorID,
|
||||
HeartbeatSec: req.HeartbeatSec,
|
||||
ReportedAt: time.Now().Format("2006-01-02 15:04:05"),
|
||||
}
|
||||
b, _ := json.Marshal(v)
|
||||
return s.upsertConfig(ctx, torqueGunKey(req.StationNo), string(b),
|
||||
"工位终端上报的拧紧枪配置快照(仅展示,不下发终端)")
|
||||
}
|
||||
@@ -14,6 +14,7 @@ import (
|
||||
"bj_power_mes/ent/dock"
|
||||
"bj_power_mes/ent/flowmaterial"
|
||||
"bj_power_mes/ent/materialrequest"
|
||||
"bj_power_mes/ent/station"
|
||||
"bj_power_mes/ent/workorder"
|
||||
"bj_power_mes/ent/workpiece"
|
||||
)
|
||||
@@ -298,10 +299,51 @@ func (s *Service) CreateTransferReturns(ctx context.Context, stationNo int, oper
|
||||
return nil
|
||||
}
|
||||
|
||||
// guardTransferTargetPhysical 转产改线目标工位「实体化」守卫(2026-11-08 拍板):
|
||||
// 本次变更后的新目标工位必须是 LINE 启用工位,且存在启用的 line 类型接驳台;
|
||||
// 虚拟工位/线下位(OFFLINE)无接驳台、AGV 无法送达、无工位终端,不能接手产线在制工件。
|
||||
// 只校验「本次真正变更」的目标工位,未变工位(如末端下料检测位 11/12 本就无接驳台)
|
||||
// 属存量组合、不是转产目标,不重复校验以免误伤既有工单。
|
||||
func (s *Service) guardTransferTargetPhysical(ctx context.Context, oldCombo, newCombo []map[string]int) error {
|
||||
const msg = "转产目标工位必须是实体工位(有接驳台、启用状态);虚拟工位/线下位不能作为转产目标"
|
||||
oldStOf := map[int]int{}
|
||||
for _, it := range flowMapsToItems(oldCombo) {
|
||||
oldStOf[it.FlowId] = it.StationNo
|
||||
}
|
||||
checked := map[int]bool{}
|
||||
for _, it := range flowMapsToItems(newCombo) {
|
||||
if old, ok := oldStOf[it.FlowId]; ok && old == it.StationNo {
|
||||
continue // 该工序工位未变,不是本次转产目标
|
||||
}
|
||||
if checked[it.StationNo] {
|
||||
continue
|
||||
}
|
||||
checked[it.StationNo] = true
|
||||
st, err := s.ctx.EntClient.Station.Query().Where(station.StationNo(it.StationNo)).Only(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s(工位%d 不存在)", msg, it.StationNo)
|
||||
}
|
||||
if st.StationType != "LINE" || st.Status != "ENABLED" {
|
||||
return fmt.Errorf("%s(工位%d 为 %s/%s)", msg, it.StationNo, st.StationType, st.Status)
|
||||
}
|
||||
n, derr := s.ctx.EntClient.Dock.Query().Where(
|
||||
dock.StationNo(it.StationNo), dock.DockTypeEQ("line"), dock.StatusEQ("ENABLED")).Count(ctx)
|
||||
if derr != nil || n == 0 {
|
||||
return fmt.Errorf("%s(工位%d 无启用的产线接驳台)", msg, it.StationNo)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GuardTransferReroute 转产改线前置闸门(2026-11-03 定论:先人工盘库退料、库房确认收货后,才允许改线,
|
||||
// 严禁一键全自动连锁)。校验:① 该工单不存在「待收货(RETURNING)」的转产退库单;
|
||||
// 严禁一键全自动连锁)。校验:⓪ 新目标工位必须是实体工位(有启用接驳台);
|
||||
// ① 该工单不存在「待收货(RETURNING)」的转产退库单;
|
||||
// ② 旧组合中将腾退的工位,其接驳台台账均无托盘(料已退走、AGV 已拉走)。
|
||||
func (s *Service) GuardTransferReroute(ctx context.Context, wo *ent.WorkOrder, newCombo []map[string]int) error {
|
||||
// 实体工位互转守卫:虚拟/线下位不能作为转产目标(先于腾退判定,避免 vacated 为空时静默放行)
|
||||
if err := s.guardTransferTargetPhysical(ctx, wo.FlowCombination, newCombo); err != nil {
|
||||
return err
|
||||
}
|
||||
// 键名修复(2026-11-04):{flowId,stationNo},旧键 {flow,station} 使腾退判定恒空=闸门静默放行
|
||||
newStOf := map[int]int{}
|
||||
for _, it := range flowMapsToItems(newCombo) {
|
||||
|
||||
@@ -527,7 +527,8 @@ func (s *Service) ReportProcess(ctx context.Context, req ReportProcessReq, opera
|
||||
if _, err := s.applyBinds(ctx, client, req.Sn, wp.OrderNo, flowId, stationNo, operator, req.Binds); err != nil {
|
||||
return err
|
||||
}
|
||||
if miss, err := s.validateStationBinds(ctx, client, req.Sn, flowId); err != nil {
|
||||
if miss, err := s.validateStationBinds(ctx, client, req.Sn, flowId,
|
||||
s.stationHasEnabledLineDock(ctx, client, req.StationNo)); err != nil {
|
||||
return err
|
||||
} else if miss != "" {
|
||||
return errors.New(miss)
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"time"
|
||||
|
||||
"bj_power_mes/ent"
|
||||
"bj_power_mes/ent/dock"
|
||||
"bj_power_mes/ent/flowmaterial"
|
||||
"bj_power_mes/ent/workpiece"
|
||||
"bj_power_mes/ent/workpiecebind"
|
||||
@@ -193,13 +194,64 @@ func (s *Service) applyBinds(ctx context.Context, client *ent.Client, sn, orderN
|
||||
return bound, nil
|
||||
}
|
||||
|
||||
// stationHasEnabledLineDock 判断工位是否为「实体工位」:存在启用的 line 类型接驳台。
|
||||
// 虚拟/线下位(如末端下料检测位 11/12)无接驳台、不承载 AGV 物料物流,
|
||||
// 因此不采集装配物料绑定,报工/完工/追溯/终端面板一律放行(2026-11-08 B2 拍板)。
|
||||
func (s *Service) stationHasEnabledLineDock(ctx context.Context, client *ent.Client, stationNo int) bool {
|
||||
if stationNo <= 0 {
|
||||
return false
|
||||
}
|
||||
n, err := client.Dock.Query().Where(
|
||||
dock.StationNo(stationNo), dock.DockTypeEQ("line"), dock.StatusEQ("ENABLED")).Count(ctx)
|
||||
return err == nil && n > 0
|
||||
}
|
||||
|
||||
const bindSkipReason = "该工序未采集绑定(工位无接驳台)"
|
||||
|
||||
// BindNotCollected 追溯页:因候选工位无接驳台而未采集装配绑定的工序。
|
||||
type BindNotCollected struct {
|
||||
FlowId int `json:"flowId"`
|
||||
FlowName string `json:"flowName"`
|
||||
StationNo int `json:"stationNo"`
|
||||
Reason string `json:"reason"`
|
||||
}
|
||||
|
||||
// flowBindGate 按工单工艺组合判定每道工序是否需采集装配绑定:
|
||||
// 多工位同工艺=并行候选工位,任一候选工位有启用 line 接驳台即需采集;
|
||||
// 该工序全部候选均无接驳台(虚拟/线下位)时不采集,并返回追溯用未采集清单。
|
||||
func (s *Service) flowBindGate(ctx context.Context, client *ent.Client, wo *ent.WorkOrder) (map[int]bool, []BindNotCollected) {
|
||||
items, _ := RouteStationsFromWO(ctx, client, wo)
|
||||
required := map[int]bool{}
|
||||
seen := map[int]bool{}
|
||||
skipped := []BindNotCollected{}
|
||||
for _, it := range items {
|
||||
if s.stationHasEnabledLineDock(ctx, client, it.StationNo) {
|
||||
required[it.FlowId] = true
|
||||
}
|
||||
}
|
||||
for _, it := range items {
|
||||
if seen[it.FlowId] || required[it.FlowId] {
|
||||
continue
|
||||
}
|
||||
seen[it.FlowId] = true
|
||||
skipped = append(skipped, BindNotCollected{
|
||||
FlowId: it.FlowId, FlowName: s.flowNameById(ctx, it.FlowId),
|
||||
StationNo: it.StationNo, Reason: bindSkipReason,
|
||||
})
|
||||
}
|
||||
return required, skipped
|
||||
}
|
||||
|
||||
// validateStationBinds 校验某工件在某工艺的装配物料是否绑齐(齐套强校验核心):
|
||||
// 返回缺料明细(空串=齐套)。判定:电气件(SN) 绑定的不同SN数 ≥ 单台用量;结构件(批次) 至少绑定1个批次号。
|
||||
// 该工艺未配置物料清单时视为不启用绑定校验,放行。
|
||||
func (s *Service) validateStationBinds(ctx context.Context, client *ent.Client, sn string, flowId int) (string, error) {
|
||||
// 该工艺未配置物料清单,或报工工位无启用产线接驳台(不采集绑定)时放行。
|
||||
func (s *Service) validateStationBinds(ctx context.Context, client *ent.Client, sn string, flowId int, requireBinds bool) (string, error) {
|
||||
if _, _, err := s.workpieceProduct(ctx, client, sn); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if !requireBinds {
|
||||
return "", nil // 工位无启用产线接驳台(虚拟/线下位)→ 不采集绑定,放行
|
||||
}
|
||||
targets, err := s.stationBindTargets(ctx, client, flowId)
|
||||
if err != nil {
|
||||
return "", err
|
||||
@@ -242,12 +294,14 @@ func (s *Service) validateStationBinds(ctx context.Context, client *ent.Client,
|
||||
}
|
||||
|
||||
// validateAllFlows 完工兜底:按工单工艺组合(唯一路线源头)逐工艺校验装配物料齐套。
|
||||
// 同一工艺绑多工位时只校验一次(按 FlowId 去重)。
|
||||
// 同一工艺绑多工位时只校验一次(按 FlowId 去重);该工序候选工位全无启用 line 接驳台
|
||||
// (虚拟/线下位,未采集绑定)时跳过,不再把「没绑」误判成缺料。
|
||||
func (s *Service) validateAllFlows(ctx context.Context, client *ent.Client, sn string, wo *ent.WorkOrder) (string, error) {
|
||||
if _, _, err := s.workpieceProduct(ctx, client, sn); err != nil {
|
||||
return "", err
|
||||
}
|
||||
items, _ := RouteStationsFromWO(ctx, client, wo)
|
||||
required, _ := s.flowBindGate(ctx, client, wo)
|
||||
missing := []string{}
|
||||
seen := map[int]bool{}
|
||||
for _, it := range items {
|
||||
@@ -255,7 +309,10 @@ func (s *Service) validateAllFlows(ctx context.Context, client *ent.Client, sn s
|
||||
continue
|
||||
}
|
||||
seen[it.FlowId] = true
|
||||
msg, err := s.validateStationBinds(ctx, client, sn, it.FlowId)
|
||||
if !required[it.FlowId] {
|
||||
continue // 该工序全部候选工位无接驳台 → 未采集绑定,不参与齐套
|
||||
}
|
||||
msg, err := s.validateStationBinds(ctx, client, sn, it.FlowId, true)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -327,10 +384,11 @@ func (s *Service) ListWorkpieceBinds(ctx context.Context, sn string) ([]*ent.Wor
|
||||
|
||||
// BindTraceVO 追溯页:装机绑定明细 + 每工艺齐套状态 + 装机物料明细
|
||||
type BindTraceVO struct {
|
||||
Binds []*ent.WorkpieceBind `json:"binds"`
|
||||
Complete bool `json:"complete"`
|
||||
Missing string `json:"missing"`
|
||||
BomTree []BindNode `json:"bomTree"` // 装机物料明细(按工单工艺组合×工艺物料清单平铺一层,无多级树)
|
||||
Binds []*ent.WorkpieceBind `json:"binds"`
|
||||
Complete bool `json:"complete"`
|
||||
Missing string `json:"missing"`
|
||||
BomTree []BindNode `json:"bomTree"` // 装机物料明细(按工单工艺组合×工艺物料清单平铺一层,无多级树)
|
||||
NotCollected []BindNotCollected `json:"notCollected"` // 无接驳台工序:未采集绑定原因
|
||||
}
|
||||
|
||||
// BindNode 追溯用的装机物料明细节点(平铺一层;多级 BOM 已于 2026-09-23 退役,无下级展开)
|
||||
@@ -357,6 +415,8 @@ func (s *Service) BuildBindTrace(ctx context.Context, sn string) (*BindTraceVO,
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// 无接驳台工序未采集绑定:追溯页显式给出原因,避免与「已绑齐」的空列表混淆
|
||||
_, skipped := s.flowBindGate(ctx, s.ctx.EntClient, wo)
|
||||
// 物料组成:按工单工艺组合×工艺物料清单平铺一层(物料清单已删除,无多级树)
|
||||
var tree []BindNode
|
||||
for _, it := range s.WorkOrderUnitUsage(ctx, wo) {
|
||||
@@ -366,7 +426,8 @@ func (s *Service) BuildBindTrace(ctx context.Context, sn string) (*BindTraceVO,
|
||||
Qty: it.UnitQty,
|
||||
})
|
||||
}
|
||||
return &BindTraceVO{Binds: binds, Complete: missing == "", Missing: missing, BomTree: tree}, nil
|
||||
return &BindTraceVO{Binds: binds, Complete: missing == "", Missing: missing,
|
||||
BomTree: tree, NotCollected: skipped}, nil
|
||||
}
|
||||
|
||||
// BindRequiredItem 应绑清单条目(工位终端展示引导 + 前端齐套计算)
|
||||
@@ -393,6 +454,8 @@ type StationBindPanel struct {
|
||||
FlowId int `json:"flowId"`
|
||||
LastBinds []*ent.WorkpieceBind `json:"lastBinds"` // 最近10条绑定记录
|
||||
StationNames map[int]string `json:"-"`
|
||||
HasLineDock bool `json:"hasLineDock"` // 候选工位是否有启用 line 接驳台
|
||||
SkipReason string `json:"skipReason"` // 无需采集绑定的原因
|
||||
}
|
||||
|
||||
// StationBindPanelData 供工位终端在报工前拉取:本工件本工艺该装什么、已装什么
|
||||
@@ -417,7 +480,16 @@ func (s *Service) StationBindPanelData(ctx context.Context, sn string, flowId in
|
||||
}
|
||||
}
|
||||
}
|
||||
panel := &StationBindPanel{Enabled: len(targets) > 0, FlowId: flowId, StationNo: stationNo}
|
||||
// B2(2026-11-08):候选工位无启用 line 接驳台 → 该工序不采集装配绑定,
|
||||
// 面板不展示「需绑批次/需绑SN」,齐套恒为 true,前端不会拦截报工。
|
||||
hasDock := s.stationHasEnabledLineDock(ctx, s.ctx.EntClient, stationNo)
|
||||
panel := &StationBindPanel{
|
||||
Enabled: len(targets) > 0 && hasDock, FlowId: flowId, StationNo: stationNo,
|
||||
HasLineDock: hasDock, Required: []BindRequiredItem{},
|
||||
}
|
||||
if !hasDock {
|
||||
panel.SkipReason = bindSkipReason
|
||||
}
|
||||
// 全量绑定(齐套判定必须按全量统计,不能用 limit 截断,否则多料/多SN会误判缺料)
|
||||
allBinds, _ := s.ctx.EntClient.WorkpieceBind.Query().
|
||||
Where(workpiecebind.Sn(sn), workpiecebind.FlowId(flowId)).All(ctx)
|
||||
@@ -429,6 +501,9 @@ func (s *Service) StationBindPanelData(ctx context.Context, sn string, flowId in
|
||||
boundMap[b.MaterialCode][b.BindValue] = true
|
||||
}
|
||||
for _, t := range targets {
|
||||
if !hasDock {
|
||||
break // 无接驳台工位不展示「需绑批次 / 需绑SN」应绑清单
|
||||
}
|
||||
need := int(math.Ceil(t.UnitQty))
|
||||
if need < 1 {
|
||||
need = 1
|
||||
@@ -458,8 +533,8 @@ func (s *Service) StationBindPanelData(ctx context.Context, sn string, flowId in
|
||||
}
|
||||
}
|
||||
panel.Complete = allComplete
|
||||
if !allComplete {
|
||||
panel.Missing, _ = s.validateStationBinds(ctx, s.ctx.EntClient, sn, flowId)
|
||||
if !allComplete && hasDock {
|
||||
panel.Missing, _ = s.validateStationBinds(ctx, s.ctx.EntClient, sn, flowId, true)
|
||||
}
|
||||
// 最近绑定(仅展示用)
|
||||
sort.Slice(allBinds, func(i, j int) bool { return allBinds[i].ID > allBinds[j].ID })
|
||||
|
||||
Reference in New Issue
Block a user