package logic import ( "context" "strconv" "strings" ) // PointSensorCheck 建档前实物核对:按点位编号查该位置 PLC 传感器实时是否有货。 // 这是只读核对——不触发下流、不写任何表、不改点位台账占用;真正的流转仍按扫码/报工推进。 // 映射规则(点表见 internal/plc/tcp.go): // - C1~C15(主线 STOP/CACHE)→ PLC "C"+n,落在 BitsGroup.Main; // - 纯数字 1~12(工位 STATION)→ PLC "G"+n,落在 BitsGroup.Station; // - END-U → G11、END-D → G12(末端下料检测位对应工位 11/12); // - 其它(含历史 DOCK)无法映射,plcOk=false 降级。 // PLC 为 nil / 模拟 / 读不到 / 模拟数据 / 无法映射 / 位不存在时,统一返回 plcOk=false、err=nil, // 由前端降级为人工确认提示;模拟数据不能当作实物凭证,读不到不阻断建档。 func (s *Service) PointSensorCheck(ctx context.Context, pointNo string) (hasGoods bool, plcOk bool, err error) { p := strings.TrimSpace(pointNo) if p == "" { return false, false, nil } code := PointNoToPlcCode(p) if code == "" { return false, false, nil } // 模拟数据或 PLC 未接入:不作为实物凭证 if s.ctx.PLC == nil || s.ctx.PLC.IsMock() { return false, false, nil } g, e := s.PlcMonitorBits(ctx) if e != nil || g.Mock { // 读不到 / 模拟:降级,不阻断 return false, false, nil } for _, b := range g.Station { if b.Code == code { return b.Has, true, nil } } for _, b := range g.Main { if b.Code == code { return b.Has, true, nil } } return false, false, nil } // PointNoToPlcCode 把点位编号映射成 PLC 位 code(禁用正则,固定格式用 HasPrefix / slice / Atoi 解析)。 // 无法映射返回空串(前端据此降级为人工确认)。 func PointNoToPlcCode(p string) string { // C 开头且后接 1~15 → "C"+n if strings.HasPrefix(p, "C") { n, e := strconv.Atoi(p[1:]) if e != nil || n < 1 || n > 15 { return "" } return "C" + strconv.Itoa(n) } // 末端下料检测位 → 工位 11/12 if p == "END-U" { return "G11" } if p == "END-D" { return "G12" } // 纯数字 1~12(工位 STATION)→ "G"+n n, e := strconv.Atoi(p) if e != nil || n < 1 || n > 12 { return "" } return "G" + strconv.Itoa(n) }