package logic import ( "context" "errors" "fmt" "strings" "bj_power_mes/ent" "bj_power_mes/ent/linepoint" ) // ============ 产线点位台账(产线布局说明·主线位置编址版,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。 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) // 注意:接驳台(GxL/GxR/JBT21)是 AGV 点位(dock 表维护),不再进点位台账; // 历史库遗留的 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 产线布局说明(主线位置编址版):接驳台(DOCK=AGV 点位)不作工件落点, // 历史遗留行(如 1-R1)在类型层拒绝,不依赖前端下拉;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.PointType == "DOCK" { return errors.New("接驳台是 AGV 点位,不能作为工件位置:" + pointNo + "(请选主线位置 C1~C15 或工位号)") } 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 { 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() 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() return nil }