Files
bj_power/bj_power_mes/internal/logic/workpiece_location.go
T
SunYF f930a1325f feat(mes/wms): 接驳台编码统一为GW点位码 + 签字模型改报检呼叫 + 台账续流修复 + 末级库位强校验(2026-12-01 终版整改)
【全系统 · 接驳台编码终版统一(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 补料→配送→报工人库闭环
2026-12-01 22:15:28 +08:00

221 lines
7.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package logic
import (
"context"
"fmt"
"bj_power_mes/ent"
"bj_power_mes/ent/processflow"
"bj_power_mes/ent/station"
"bj_power_mes/ent/workpieceprocess"
)
// WorkpieceLocation 统一工件位置展示(2026-11-30):工单详情工件列表 / WIP / 追溯三处共用。
// 判定顺序固定:SCRAPPED → DONE → SEMI_STORED → atStationNo>0 → flowTransitTarget → 待流转。
// type 为机器码,name 为中文展示,progress 为进度摘要,source 为判定依据字段。
type WorkpieceLocation struct {
Type string `json:"type"` // SCRAPPED/DONE/SEMI_STORED/STATION/VIRTUAL/TRANSIT/PENDING
Name string `json:"name"` // 已报废/已完工/半成品库/工位名/虚拟工位/滚筒线在途/待流转
Progress string `json:"progress"` // 进度摘要(如 "已完成至工位3" / "3/5"),无则空
Source string `json:"source"` // status / atStationNo / flowTransitTarget / default
}
// ResolveLocation 按统一顺序推导单个工件的位置。
// client 可为事务 client;stationCache 可选(批量场景复用)。
func (s *Service) ResolveLocation(ctx context.Context, client *ent.Client, wp *ent.Workpiece, stationCache map[int]*ent.Station) WorkpieceLocation {
if wp == nil {
return WorkpieceLocation{Type: "PENDING", Name: "待流转", Source: "default"}
}
// 1) 已报废
if wp.Status == "SCRAPPED" {
return WorkpieceLocation{Type: "SCRAPPED", Name: "已报废", Source: "status"}
}
// 2) 已完工(有成品库存行则带库位;WMS 不可达/未入库只展示「已完工」)
if wp.Status == "DONE" {
loc := WorkpieceLocation{Type: "DONE", Name: "已完工", Source: "status"}
if s != nil && s.ctx != nil && s.ctx.Wms != nil {
if found, _, e := s.ctx.Wms.FinishedSnExists(ctx, wp.Sn); e == nil && found {
fl := s.FinishedWmsLocation(ctx)
loc.Name = fmt.Sprintf("已完工(成品库 %s/%s/%s/%s)", fl.ZoneCode, fl.ShelfNo, fl.LayerNo, fl.PositionNo)
}
}
return loc
}
// 3) 半成品库
if wp.Status == "SEMI_STORED" {
return WorkpieceLocation{Type: "SEMI_STORED", Name: "半成品库", Source: "status"}
}
// 4) 物理落点工位(atStationNo>0;按 station_type 区分实体/虚拟)
if wp.AtStationNo > 0 {
name, typ := s.stationNameAndType(ctx, client, wp.AtStationNo, stationCache)
loc := WorkpieceLocation{Type: "STATION", Name: name, Source: "atStationNo"}
if typ == "OFFLINE" {
loc.Type = "VIRTUAL"
loc.Name = "虚拟工位·" + name
}
return loc
}
// 5) 滚筒线在途
if wp.FlowTransitTarget != "" {
return WorkpieceLocation{Type: "TRANSIT", Name: "滚筒线在途 → " + wp.FlowTransitTarget, Source: "flowTransitTarget"}
}
// 6) 待流转
return WorkpieceLocation{Type: "PENDING", Name: "待流转", Source: "default"}
}
// stationNameAndType 工位显示名+类型(查不到按「工位N/LINE」兜底;虚拟站 OFFLINE 或 >12/<=0)。
func (s *Service) stationNameAndType(ctx context.Context, client *ent.Client, no int, cache map[int]*ent.Station) (string, string) {
if client == nil {
client = s.ctx.EntClient
}
if cache != nil {
if st, ok := cache[no]; ok && st != nil {
return stationDisplayName(st.Name, no), st.StationType
}
}
st, err := client.Station.Query().Where(station.StationNo(no)).Only(ctx)
if err != nil || st == nil {
if no <= 0 || no > 12 {
return "工位" + itoa(no), "OFFLINE"
}
return "工位" + itoa(no), "LINE"
}
if cache != nil {
cache[no] = st
}
typ := st.StationType
if no <= 0 || no > 12 {
typ = "OFFLINE"
}
return stationDisplayName(st.Name, no), typ
}
// LookupRouteItem 上线建档完整工艺路线(/api/internal/workpiece/lookup.route)
type LookupRouteItem struct {
Seq int `json:"seq"`
FlowId int `json:"flowId"`
FlowName string `json:"flowName"`
Stations []LookupRouteStation `json:"stations"`
Done bool `json:"done"`
Current bool `json:"current"`
}
// LookupRouteStation 路线中的工位
type LookupRouteStation struct {
StationNo int `json:"stationNo"`
Name string `json:"name"`
StationType string `json:"stationType"`
}
// BuildLookupRoute 由工单工艺组合构建路线数组(seq/flowName 取 GroupFlowRoute + process_flow.name;
// stationType 取 station.station_type,虚拟站 OFFLINE 或 <=0/>12;done=该 flowId 有 DONE 报工;
// current=firstUndoneFlowSeq 命中)。
func (s *Service) BuildLookupRoute(ctx context.Context, sn string, wo *ent.WorkOrder) []LookupRouteItem {
if wo == nil {
return []LookupRouteItem{}
}
client := s.ctx.EntClient
route := GroupFlowRoute(FlowItemsOfWO(wo))
if len(route) == 0 {
return []LookupRouteItem{}
}
flowNames := map[int]string{}
ids := make([]int, 0, len(route))
for _, r := range route {
ids = append(ids, r.FlowId)
}
if rows, e := client.ProcessFlow.Query().Where(processflow.IDIn(ids...)).All(ctx); e == nil {
for _, pf := range rows {
flowNames[pf.ID] = pf.Name
}
}
stationNos := map[int]bool{}
for _, r := range route {
for _, no := range r.Stations {
stationNos[no] = true
}
}
stCache := map[int]*ent.Station{}
if rows, e := client.Station.Query().All(ctx); e == nil {
for _, st := range rows {
stCache[st.StationNo] = st
}
}
firstUndone, _ := s.firstUndoneFlowSeq(ctx, client, sn, route)
out := make([]LookupRouteItem, 0, len(route))
for _, r := range route {
item := LookupRouteItem{
Seq: r.Seq, FlowId: r.FlowId, FlowName: flowNames[r.FlowId],
Stations: make([]LookupRouteStation, 0, len(r.Stations)),
Current: firstUndone > 0 && r.Seq == firstUndone,
}
for _, no := range r.Stations {
name, typ := s.stationNameAndType(ctx, client, no, stCache)
item.Stations = append(item.Stations, LookupRouteStation{StationNo: no, Name: name, StationType: typ})
}
done, e := client.WorkpieceProcess.Query().
Where(workpieceprocess.Sn(sn), workpieceprocess.FlowId(r.FlowId), workpieceprocess.Status("DONE")).Exist(ctx)
item.Done = e == nil && done
out = append(out, item)
}
return out
}
// FlowItemsOfWO 从工单 flow_combination 取扁平条目(供路线构建)
func FlowItemsOfWO(wo *ent.WorkOrder) []FlowItem {
return flowMapsToItems(wo.FlowCombination)
}
// LocationOfWorkpiece 面向 handler 的统一位置入口(按 status/atStationNo/flowTransitTarget 判定)
func LocationOfWorkpiece(ctx context.Context, s *Service, wp *ent.Workpiece) WorkpieceLocation {
return s.ResolveLocation(ctx, s.ctx.EntClient, wp, nil)
}
// LocationsOfWorkpieces 批量为一 avail set 工件补位置(缓存工位查询)
func (s *Service) LocationsOfWorkpieces(ctx context.Context, rows []*ent.Workpiece) map[int]WorkpieceLocation {
out := map[int]WorkpieceLocation{}
cache := map[int]*ent.Station{}
if rows == nil || len(rows) == 0 || s == nil || s.ctx == nil || s.ctx.EntClient == nil {
return out
}
client := s.ctx.EntClient
// 批量预取所有用到的工位
nos := map[int]bool{}
for _, wp := range rows {
if wp.AtStationNo > 0 {
nos[wp.AtStationNo] = true
}
}
if len(nos) > 0 {
keys := make([]int, 0, len(nos))
for n := range nos {
keys = append(keys, n)
}
if sts, e := client.Station.Query().Where(station.StationNoIn(keys...)).All(ctx); e == nil {
for _, st := range sts {
cache[st.StationNo] = st
}
}
}
for _, wp := range rows {
out[wp.ID] = s.ResolveLocation(ctx, client, wp, cache)
}
return out
}
// workpieceProgressText 进度摘要(已完成至工位N / 已报工 X/Y 道)
func workpieceProgressText(ctx context.Context, client *ent.Client, wp *ent.Workpiece, route []FlowRouteStep) string {
done, _ := client.WorkpieceProcess.Query().
Where(workpieceprocess.Sn(wp.Sn), workpieceprocess.Status("DONE")).Count(ctx)
if done <= 0 {
return ""
}
if len(route) > 0 {
return fmt.Sprintf("%d/%d 道工艺", done, len(route))
}
if wp.CurrentStationNo > 0 {
return "已完成至工位" + itoa(wp.CurrentStationNo)
}
return ""
}