【全系统 · 接驳台编码终版统一(12-01 用户拍板)】
- 新增 dockcode 包作为唯一真源:工位接驳台 = GW{工位号}{Z|Y}(Z=左/Y=右,工位1~10同一规则,废除左右互换),库房 = JBT21 不变
- 编码即海康 RCS 地图点位码:落库/界面/下发 RCS/读 PLC 传感器全用同一套;旧 DOCK01~20 全量废弃,系统内无第二套编号、无换算层
- dock 表去 side 字段;agv_task.target_dock、line_point、看板点位态与中文名、dockLabel 工具全部改 GW 规则
- 产线布局说明.md 同步终版(图纸名 GxZ/GxY 与 AGV 码 GWxZ/GWxY 一一对应;注明 RCS 图与主线流向图相差 180°,读图一律以 GW 码为准)
【MES · 签字模型二次重构(11-30 实测后定稿)】
- _OPERATOR 签字整体废除:工艺步骤删 needOperatorSign / needAudit / auditRole 字段,签字角色改 CALL(工人报检呼叫)+ INSPECTOR(检验员审核签)
- 呼叫时落步骤录入快照(stepValue/stepText/stepChecked/processId),供检验员查看与指纹比对;「待签任务」只认检验员签名步骤
- 「签字复核」(AuditPending 页面 + produce.audit 菜单 + 路由)整套删除
【MES · 台账续流根因修复(11-30 对账发现)】
- 流转目标「G6」与 line_point.point_no「6」对不上导致到位落账查不到、却先释放源位→工件从调度消失;新增 ledgerPointByTarget 归一
- settleDirect/settleInTransit 改为先占目标位、成功才释放源位;目标被占原样保留,工件进缓存位排队(实测 C2 排队不再丢件)
【WMS · 末级库位强校验与上架移库】
- 新增 LocationCascade 四级联动选择器(区/架/层/号,只从启用 level=4 节点派生);手工入库、半成品/成品入库、隔离库位一律四级必填且命中启用节点
- 新增上架/移库 relocate:只改四级库位与 updated_at,不动质量状态/数量/锁定量;不合格与锁定行同样允许移库;支持 ids 逐行指定或 scope 按物料+原库位整组搬
- MES 新增 WMS 自动回流库位配置(半成品/成品各一组末级库位,默认值 1区/1架/1层/1号,存 sys_config 可改)
【MES · 统一口径与工程整改】
- 新增 workpiece_location:工单详情工件列表/WIP/追溯三处共用位置判定(SCRAPPED→DONE→SEMI_STORED→在 StationNo→在途→待流转)
- 新增 workorder_close_warn:工单完工/取消时汇总各工位实物余料出一条预警,不阻断关单
- 分页下沉:新增 pagination 工具,同步失败台账(两端)改 Count+LIMIT/OFFSET 真分页,列表不再全量切片
- 全网对账/孤儿扫描(问题临时.md 第25/26节):10 类关系边断言实测,给出 8 条可重复巡检项
【文档】
- 产线布局说明.md 按 GW 编码终版重写;问题临时.md 新增 24.7 闭环实测、25 全网对账、26 补料→配送→报工人库闭环
262 lines
13 KiB
Go
262 lines
13 KiB
Go
package logic
|
||
|
||
import (
|
||
"context"
|
||
"encoding/json"
|
||
"errors"
|
||
"fmt"
|
||
"strconv"
|
||
"strings"
|
||
|
||
"bj_power_mes/ent"
|
||
"bj_power_mes/ent/linepoint"
|
||
"bj_power_mes/ent/station"
|
||
"bj_power_mes/ent/workpiece"
|
||
)
|
||
|
||
// ============ 产线点位台账(产线布局说明·主线位置编址版,2026-11-05 权威文档) ============
|
||
// 主线位置统一编址 C1~C15(左→右),**没有独立缓存位**——缓存是主线位置的属性:
|
||
// 停靠位(STOP,不缓存,工件到达即顶升进工位):C1(正对G1/G2)、C4(G3/G4)、C7(G5/G6)、
|
||
// C10(G7/G8)、C13(G9/G10)、C15(G11/G12 进料位·下料检测);
|
||
// 缓存位(CACHE,可缓存):C2/C3、C5/C6、C8/C9、C11/C12、C14。
|
||
// 工件流向:C1 → 1/2 → 3/4 → 5/6 → 7/8 → 9/10 → 末端 11/12(下料检测)。
|
||
// 业务口径(2026-11-06 拍板):缓存位(CACHE)能停工件,但与虚拟工位(负数号)无代码关联——
|
||
// 两者只是编号空间不同,缓存位不参与工单工艺组合/工位资格校验。
|
||
// 上料 = 人工把托盘放到主线任意位置(C1~C15)或任意工位(建档时选当前位置)。
|
||
// 接驳台(工位 GW1Z..GW10Y 20 个 + 库房 JBT21)是 AGV 点位(dock 表),不是主线位置、不进本台账。
|
||
// C1~C15 采用 upsert:存量旧定义(C1~C8 全 CACHE)自动对齐文档编址,并补建 C9~C15。
|
||
|
||
type pointDef struct {
|
||
pointType string
|
||
pointNo string
|
||
label string
|
||
seq int
|
||
stationNo int
|
||
cacheable bool
|
||
}
|
||
|
||
// lineCDefs 主线位置 C1~C15 权威定义(产线布局说明·编址表逐条对应)。
|
||
var lineCDefs = []pointDef{
|
||
{"STOP", "C1", "停靠位C1(工位1/2出料,正对G1/G2)", 45, 0, false},
|
||
{"CACHE", "C2", "缓存位C2(工位1/2与3/4之间)", 50, 0, true},
|
||
{"CACHE", "C3", "缓存位C3(工位1/2与3/4之间)", 55, 0, true},
|
||
{"STOP", "C4", "停靠位C4(工位3/4出料,正对G3/G4)", 75, 0, false},
|
||
{"CACHE", "C5", "缓存位C5(工位3/4与5/6之间)", 80, 0, true},
|
||
{"CACHE", "C6", "缓存位C6(工位3/4与5/6之间)", 85, 0, true},
|
||
{"STOP", "C7", "停靠位C7(工位5/6出料,正对G5/G6)", 105, 0, false},
|
||
{"CACHE", "C8", "缓存位C8(工位5/6与7/8之间)", 110, 0, true},
|
||
{"CACHE", "C9", "缓存位C9(工位5/6与7/8之间)", 115, 0, true},
|
||
{"STOP", "C10", "停靠位C10(工位7/8出料,正对G7/G8)", 135, 0, false},
|
||
{"CACHE", "C11", "缓存位C11(工位7/8与9/10之间)", 140, 0, true},
|
||
{"CACHE", "C12", "缓存位C12(工位7/8与9/10之间)", 145, 0, true},
|
||
{"STOP", "C13", "停靠位C13(工位9/10出料,正对G9/G10)", 165, 0, false},
|
||
{"CACHE", "C14", "缓存位C14(工位9/10与11/12之间)", 170, 0, true},
|
||
{"STOP", "C15", "进料位C15(工位11/12下料检测)", 175, 0, false},
|
||
}
|
||
|
||
// InitLinePoints 初始化点位台账:
|
||
// - C1~C15 按权威编址表 upsert(更新类型/名称/可缓存属性与主线顺序,不动占用字段);
|
||
// - 工位/末端点位幂等只补缺失,不覆盖已占用点位。
|
||
//
|
||
// 废弃的 FEED"上料位"数据清理走独立迁移 SQL(migrations/20261105_purge_feed_line_point.sql,
|
||
// 部署时手工执行——用户裁定:禁止服务启动时更新任何数据库数据结构)。
|
||
func (s *Service) InitLinePoints(ctx context.Context) error {
|
||
// 主线位置 C1~C15:按文档编址 upsert(存量 C1~C8 旧"缓存位"定义自动对齐;C9~C15 补建)
|
||
for _, d := range lineCDefs {
|
||
exist, _ := s.ctx.EntClient.LinePoint.Query().
|
||
Where(linepoint.PointNo(d.pointNo)).First(ctx)
|
||
if exist != nil {
|
||
_, _ = s.ctx.EntClient.LinePoint.UpdateOneID(exist.ID).
|
||
SetPointType(d.pointType).SetLabel(d.label).
|
||
SetSeq(d.seq).SetCacheable(d.cacheable).Save(ctx)
|
||
continue
|
||
}
|
||
_, _ = s.ctx.EntClient.LinePoint.Create().
|
||
SetPointType(d.pointType).SetPointNo(d.pointNo).SetLabel(d.label).
|
||
SetSeq(d.seq).SetStationNo(d.stationNo).SetCacheable(d.cacheable).Save(ctx)
|
||
}
|
||
|
||
defs := []pointDef{}
|
||
seq := 10
|
||
add := func(pt, no, label string, stationNo int) {
|
||
seq += 10
|
||
defs = append(defs, pointDef{pt, no, label, seq, stationNo, false})
|
||
}
|
||
for i := 1; i <= 10; i += 2 {
|
||
add("STATION", fmt.Sprint(i), fmt.Sprintf("工位%d", i), i)
|
||
add("STATION", fmt.Sprint(i+1), fmt.Sprintf("工位%d", i+1), i+1)
|
||
}
|
||
add("END", "END-U", "上排末端下料检测位(工位11)", 11)
|
||
add("END", "END-D", "下排末端下料检测位(工位12)", 12)
|
||
// 注意:接驳台(GW{工位号}{Z|Y} / JBT21)是 AGV 点位(dock 表维护),不进点位台账。
|
||
for _, d := range defs {
|
||
exist, _ := s.ctx.EntClient.LinePoint.Query().
|
||
Where(linepoint.PointType(d.pointType), linepoint.PointNo(d.pointNo)).First(ctx)
|
||
if exist != nil {
|
||
continue
|
||
}
|
||
_, _ = s.ctx.EntClient.LinePoint.Create().
|
||
SetPointType(d.pointType).SetPointNo(d.pointNo).SetLabel(d.label).
|
||
SetSeq(d.seq).SetStationNo(d.stationNo).SetCacheable(d.cacheable).Save(ctx)
|
||
}
|
||
return nil
|
||
}
|
||
|
||
// ListPoints 查询点位台账(按传送带顺序)
|
||
func (s *Service) ListPoints(ctx context.Context) ([]map[string]any, error) {
|
||
pts, err := s.ctx.EntClient.LinePoint.Query().Order(ent.Asc(linepoint.FieldSeq)).All(ctx)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
out := make([]map[string]any, 0, len(pts))
|
||
for _, p := range pts {
|
||
out = append(out, map[string]any{
|
||
"id": p.ID, "pointType": p.PointType, "pointNo": p.PointNo, "label": p.Label,
|
||
"seq": p.Seq, "stationNo": p.StationNo, "currentSn": p.CurrentSn,
|
||
"orderNo": p.OrderNo, "occupied": p.Occupied, "cacheable": p.Cacheable, "updatedAt": p.UpdatedAt,
|
||
})
|
||
}
|
||
return out, nil
|
||
}
|
||
|
||
// CheckPointFree 点位可占用预检(建档/完工落账前调用,避免半量副作用):
|
||
// 点位必须存在且未被其它工件占用(同一工件幂等放行);pointNo 为空=未选点位,直接放行。
|
||
// 2026-11-05 产线布局说明(主线位置编址版):接驳台是 AGV 点位,不作工件落点;
|
||
// FEED"上料位"已废弃删除。
|
||
func (s *Service) CheckPointFree(ctx context.Context, pointNo, sn string) error {
|
||
pointNo = strings.TrimSpace(pointNo)
|
||
if pointNo == "" {
|
||
return nil
|
||
}
|
||
pt, err := s.ctx.EntClient.LinePoint.Query().Where(linepoint.PointNo(pointNo)).First(ctx)
|
||
if err != nil {
|
||
return errors.New("工件位置不存在:" + pointNo)
|
||
}
|
||
if pt.Occupied && pt.CurrentSn != strings.TrimSpace(sn) {
|
||
return errors.New("工件位置 " + pointNo + " 已被工件 " + pt.CurrentSn + " 占用,请改选空闲点位")
|
||
}
|
||
return nil
|
||
}
|
||
|
||
// OccupyPoint 点位人工记账(唯一事实源维护入口)。
|
||
// action=IN :上料/占位,把工件 SN 记到该点位(点位被其它工件占用时报错;同一工件幂等放行)。
|
||
// action=OUT :下料/释放,清空该点位(传入 sn 时校验点位当前就是该工件)。
|
||
// 用途:产线首件上线、末端下料检测位(11/12)下料、未联调 PLC 时的人工记账。
|
||
func (s *Service) OccupyPoint(ctx context.Context, pointNo, sn, orderNo, operator, action string) error {
|
||
pointNo = strings.TrimSpace(pointNo)
|
||
sn = strings.TrimSpace(sn)
|
||
if pointNo == "" {
|
||
return errors.New("点位编号必填")
|
||
}
|
||
pt, err := s.ctx.EntClient.LinePoint.Query().Where(linepoint.PointNo(pointNo)).First(ctx)
|
||
if err != nil {
|
||
// 虚拟工位(OFFLINE,负数工位号)不在产线点位台账(2026-11-25 用户裁决):
|
||
// 实体工位工件由 PLC 流转到位自动显示、不扫码;虚拟工位不连 PLC、无流转,靠扫码收件,
|
||
// 扫码即校验该件下一道工艺是否属于本工位(与在制品重上线同一资格口径:下一工艺候选工位含本工位号)。
|
||
if no, aerr := strconv.Atoi(pointNo); aerr == nil && no < 0 {
|
||
return s.virtualStationTakeIn(ctx, no, sn, orderNo, operator)
|
||
}
|
||
return errors.New("点位不存在:" + pointNo)
|
||
}
|
||
if operator == "" {
|
||
operator = "SYSTEM"
|
||
}
|
||
if action == "OUT" {
|
||
if pt.Occupied && sn != "" && pt.CurrentSn != sn {
|
||
return errors.New("点位 " + pointNo + " 当前不是该工件,无法下料")
|
||
}
|
||
if _, err := s.ctx.EntClient.LinePoint.UpdateOneID(pt.ID).
|
||
SetCurrentSn("").SetOrderNo("").SetOccupied(false).Save(ctx); err != nil {
|
||
return err
|
||
}
|
||
// 2026-11-30 补边:点位释放必须同步清工件物理位置,否则出现“台账空了、at_station 还挂着”
|
||
// 的孤儿——flowStationBusyBy 按 at_station 判占用,下游永远被这个幽灵件占着,缓存件无法续流。
|
||
if sn != "" && pt.StationNo > 0 {
|
||
_, _ = s.ctx.EntClient.Workpiece.Update().
|
||
Where(workpiece.Sn(sn), workpiece.AtStationNo(pt.StationNo)).
|
||
SetAtStationNo(0).Save(ctx)
|
||
}
|
||
s.ctx.EventLog.Write(ctx, "line.point.out", pt.CurrentSn, operator, "line_point", pointNo,
|
||
"点位下料释放 "+pointNo, map[string]any{"sn": pt.CurrentSn, "orderNo": pt.OrderNo})
|
||
s.notifyDashboard()
|
||
s.publishPointChanged(ctx, pt.StationNo, pointNo, "")
|
||
return nil
|
||
}
|
||
if sn == "" {
|
||
return errors.New("上料必须提供工件SN")
|
||
}
|
||
if pt.Occupied && pt.CurrentSn != sn {
|
||
return errors.New("点位 " + pointNo + " 已被工件 " + pt.CurrentSn + " 占用")
|
||
}
|
||
if _, err := s.ctx.EntClient.LinePoint.UpdateOneID(pt.ID).
|
||
SetCurrentSn(sn).SetOrderNo(orderNo).SetOccupied(true).Save(ctx); err != nil {
|
||
return err
|
||
}
|
||
// 2026-11-30 补边(与 OUT 释放清位对称):占工位点位必须同步写工件物理位置,否则台账有件、
|
||
// at_station 没跟上,flowStationBusyBy 判空位、终端在制列表两边不一致(27.5)。
|
||
// 只对实体工位点位(StationNo>0)生效;缓存/停靠位(StationNo=0)不在此写——自动流转 settle
|
||
// 对缓存位按 G 前缀口径自行落账(flow_engine.go settleDirect),实体工位处两次写同值幂等。
|
||
if pt.StationNo > 0 {
|
||
_, _ = s.ctx.EntClient.Workpiece.Update().
|
||
Where(workpiece.Sn(sn)).
|
||
SetAtStationNo(pt.StationNo).Save(ctx)
|
||
}
|
||
s.ctx.EventLog.Write(ctx, "line.point.in", sn, operator, "line_point", pointNo,
|
||
"点位上料占位 "+pointNo, map[string]any{"sn": sn, "orderNo": orderNo})
|
||
s.notifyDashboard()
|
||
s.publishPointChanged(ctx, pt.StationNo, pointNo, sn)
|
||
return nil
|
||
}
|
||
|
||
// publishPointChanged 点位台账变化推送(2026-11-25 去轮询改事件驱动):
|
||
// 占位/释放即广播 station.points,终端收到即刷点位与在制(页面平时零轮询)。
|
||
// 点位台账全产线共享(各工位都要看自己正对的点位),广播语义、由各终端自行过滤展示。
|
||
func (s *Service) publishPointChanged(ctx context.Context, stationNo int, pointNo, sn string) {
|
||
if b, err := json.Marshal(map[string]any{"stationNo": stationNo, "pointNo": pointNo, "sn": sn}); err == nil {
|
||
s.ctx.SSE.Publish("station.points", string(b))
|
||
}
|
||
}
|
||
|
||
// virtualStationTakeIn 虚拟工位(OFFLINE)扫码收件(2026-11-25 用户裁决):
|
||
// 实体工位工件由 PLC 流转到位自动显示、无扫码;虚拟工位不连 PLC、无流转、不占产线点位台账,
|
||
// 靠人工扫码把工件"请"进来——扫码即校验该件下一道工艺是否属于本工位资格
|
||
// (与在制品重上线同一口径:下一工艺候选工位须包含本工位号)。
|
||
// 校验通过只确认可作业、写事件日志,不改工件状态与产线台账(虚拟工位没有点位行)。
|
||
func (s *Service) virtualStationTakeIn(ctx context.Context, stationNo int, sn, orderNo, operator string) error {
|
||
st, err := s.ctx.EntClient.Station.Query().Where(station.StationNo(stationNo)).First(ctx)
|
||
if err != nil || st == nil || st.Status != "ENABLED" {
|
||
return fmt.Errorf("虚拟工位 %d 不存在或已停用,无法接收工件", stationNo)
|
||
}
|
||
wp, err := s.ctx.EntClient.Workpiece.Query().Where(workpiece.Sn(sn)).First(ctx)
|
||
if err != nil || wp == nil {
|
||
return fmt.Errorf("产线无此工件 %s:虚拟工位不参与上线建档,请先在「上线建档」完成建档后再扫码", sn)
|
||
}
|
||
items, _ := RouteStationsFromWO(ctx, s.ctx.EntClient, s.workpieceWorkOrder(ctx, wp))
|
||
if len(items) == 0 {
|
||
return errors.New("工单未配置工艺组合,无法校验作业资格")
|
||
}
|
||
route := groupFlowRoute(items)
|
||
nextSeq, doneAll := s.firstUndoneFlowSeq(ctx, s.ctx.EntClient, wp.Sn, route)
|
||
if doneAll {
|
||
return fmt.Errorf("工件 %s 已报工至最后一道工艺,无需再作业", wp.Sn)
|
||
}
|
||
var nextFlow *FlowRouteStep
|
||
for i := range route {
|
||
if route[i].Seq == nextSeq {
|
||
nextFlow = &route[i]
|
||
break
|
||
}
|
||
}
|
||
if nextFlow == nil {
|
||
return errors.New("工件工艺进度异常:未找到下一道工艺,请在 MES 核查该工单的工艺组合")
|
||
}
|
||
if !intsContains(nextFlow.Stations, stationNo) {
|
||
return fmt.Errorf("工件 %s 下一道工艺是「%s」(候选工位%s),不属于本工位(%s)的作业范围,请到对应工位操作",
|
||
wp.Sn, s.flowNameById(ctx, nextFlow.FlowId), joinIntsLocal(nextFlow.Stations), st.Name)
|
||
}
|
||
s.ctx.EventLog.Write(ctx, "line.virtual.takein", sn, operator, "station", st.Name,
|
||
fmt.Sprintf("虚拟工位扫码收件 %s(下一工艺:%s)", sn, s.flowNameById(ctx, nextFlow.FlowId)),
|
||
map[string]any{"stationNo": stationNo, "nextSeq": nextSeq, "orderNo": orderNo})
|
||
return nil
|
||
}
|