2026-09-10 16:59:15 +08:00
|
|
|
|
package logic
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
2026-11-26 17:18:58 +08:00
|
|
|
|
"encoding/json"
|
2026-09-10 16:59:15 +08:00
|
|
|
|
"errors"
|
|
|
|
|
|
"fmt"
|
2026-11-26 17:18:58 +08:00
|
|
|
|
"strconv"
|
2026-09-10 16:59:15 +08:00
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
|
|
"bj_power_mes/ent"
|
|
|
|
|
|
"bj_power_mes/ent/linepoint"
|
2026-11-26 17:18:58 +08:00
|
|
|
|
"bj_power_mes/ent/station"
|
|
|
|
|
|
"bj_power_mes/ent/workpiece"
|
2026-09-10 16:59:15 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2026-11-07 15:06:00 +08:00
|
|
|
|
// ============ 产线点位台账(产线布局说明·主线位置编址版,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)或任意工位(建档时选当前位置)。
|
|
|
|
|
|
// 接驳台(工位 GxL/GxR 20 个 + 库房 JBT21)是 AGV 点位(dock 表),不是主线位置、不进本台账。
|
|
|
|
|
|
// C1~C15 采用 upsert:存量旧定义(C1~C8 全 CACHE)自动对齐文档编址,并补建 C9~C15。
|
2026-09-10 16:59:15 +08:00
|
|
|
|
|
|
|
|
|
|
type pointDef struct {
|
|
|
|
|
|
pointType string
|
|
|
|
|
|
pointNo string
|
|
|
|
|
|
label string
|
|
|
|
|
|
seq int
|
|
|
|
|
|
stationNo int
|
2026-11-07 15:06:00 +08:00
|
|
|
|
cacheable bool
|
2026-09-10 16:59:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-07 15:06:00 +08:00
|
|
|
|
// 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,
|
|
|
|
|
|
// 部署时手工执行——用户裁定:禁止服务启动时更新任何数据库数据结构)。
|
2026-09-10 16:59:15 +08:00
|
|
|
|
func (s *Service) InitLinePoints(ctx context.Context) error {
|
2026-11-07 15:06:00 +08:00
|
|
|
|
// 主线位置 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)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-09-10 16:59:15 +08:00
|
|
|
|
defs := []pointDef{}
|
|
|
|
|
|
seq := 10
|
|
|
|
|
|
add := func(pt, no, label string, stationNo int) {
|
|
|
|
|
|
seq += 10
|
2026-11-07 15:06:00 +08:00
|
|
|
|
defs = append(defs, pointDef{pt, no, label, seq, stationNo, false})
|
2026-09-10 16:59:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
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)
|
2026-11-07 15:06:00 +08:00
|
|
|
|
// 注意:接驳台(GxL/GxR/JBT21)是 AGV 点位(dock 表维护),不再进点位台账;
|
|
|
|
|
|
// 历史库遗留的 DOCK 类型行不删除、不参与主线落点(前端建档落点下拉已排除)。
|
2026-09-10 16:59:15 +08:00
|
|
|
|
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).
|
2026-11-07 15:06:00 +08:00
|
|
|
|
SetSeq(d.seq).SetStationNo(d.stationNo).SetCacheable(d.cacheable).Save(ctx)
|
2026-09-10 16:59:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
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,
|
2026-11-07 15:06:00 +08:00
|
|
|
|
"orderNo": p.OrderNo, "occupied": p.Occupied, "cacheable": p.Cacheable, "updatedAt": p.UpdatedAt,
|
2026-09-10 16:59:15 +08:00
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
return out, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-17 17:49:47 +08:00
|
|
|
|
// CheckPointFree 点位可占用预检(建档/完工落账前调用,避免半量副作用):
|
|
|
|
|
|
// 点位必须存在且未被其它工件占用(同一工件幂等放行);pointNo 为空=未选点位,直接放行。
|
2026-11-07 15:06:00 +08:00
|
|
|
|
// 2026-11-05 产线布局说明(主线位置编址版):接驳台(DOCK=AGV 点位)不作工件落点,
|
|
|
|
|
|
// 历史遗留行(如 1-R1)在类型层拒绝,不依赖前端下拉;FEED"上料位"已废弃删除。
|
2026-10-17 17:49:47 +08:00
|
|
|
|
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)
|
|
|
|
|
|
}
|
2026-11-07 15:06:00 +08:00
|
|
|
|
if pt.PointType == "DOCK" {
|
|
|
|
|
|
return errors.New("接驳台是 AGV 点位,不能作为工件位置:" + pointNo + "(请选主线位置 C1~C15 或工位号)")
|
|
|
|
|
|
}
|
2026-10-17 17:49:47 +08:00
|
|
|
|
if pt.Occupied && pt.CurrentSn != strings.TrimSpace(sn) {
|
|
|
|
|
|
return errors.New("工件位置 " + pointNo + " 已被工件 " + pt.CurrentSn + " 占用,请改选空闲点位")
|
|
|
|
|
|
}
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-09-10 16:59:15 +08:00
|
|
|
|
// OccupyPoint 点位人工记账(唯一事实源维护入口)。
|
|
|
|
|
|
// action=IN :上料/占位,把工件 SN 记到该点位(点位被其它工件占用时报错;同一工件幂等放行)。
|
|
|
|
|
|
// action=OUT :下料/释放,清空该点位(传入 sn 时校验点位当前就是该工件)。
|
2026-11-07 15:06:00 +08:00
|
|
|
|
// 用途:产线首件上线、末端下料检测位(11/12)下料、未联调 PLC 时的人工记账。
|
2026-09-10 16:59:15 +08:00
|
|
|
|
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 {
|
2026-11-26 17:18:58 +08:00
|
|
|
|
// 虚拟工位(OFFLINE,负数工位号)不在产线点位台账(2026-11-25 用户裁决):
|
|
|
|
|
|
// 实体工位工件由 PLC 流转到位自动显示、不扫码;虚拟工位不连 PLC、无流转,靠扫码收件,
|
|
|
|
|
|
// 扫码即校验该件下一道工艺是否属于本工位(与在制品重上线同一资格口径:下一工艺候选工位含本工位号)。
|
|
|
|
|
|
if no, aerr := strconv.Atoi(pointNo); aerr == nil && no < 0 {
|
|
|
|
|
|
return s.virtualStationTakeIn(ctx, no, sn, orderNo, operator)
|
|
|
|
|
|
}
|
2026-09-10 16:59:15 +08:00
|
|
|
|
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
|
|
|
|
|
|
}
|
|
|
|
|
|
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()
|
2026-11-26 17:18:58 +08:00
|
|
|
|
s.publishPointChanged(ctx, pt.StationNo, pointNo, "")
|
2026-09-10 16:59:15 +08:00
|
|
|
|
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
|
|
|
|
|
|
}
|
|
|
|
|
|
s.ctx.EventLog.Write(ctx, "line.point.in", sn, operator, "line_point", pointNo,
|
|
|
|
|
|
"点位上料占位 "+pointNo, map[string]any{"sn": sn, "orderNo": orderNo})
|
|
|
|
|
|
s.notifyDashboard()
|
2026-11-26 17:18:58 +08:00
|
|
|
|
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})
|
2026-09-10 16:59:15 +08:00
|
|
|
|
return nil
|
|
|
|
|
|
}
|