# -*- coding: utf-8 -*-
"""纸表识别：离线 OCR + 固定模板结构解析。

设计原则：
1. 印刷坐标决定班级/时段/房号，OCR 只处理问题格内容；
2. 结果只是候选，低置信度、人数计分、高分值必须人工确认；
3. 识别过程不直接写飞书，提交由 app.py 的 /api/paper/confirm 完成。
"""
from __future__ import annotations

import io
import math
import os
import re
import threading
import time
from collections import Counter

TEMPLATES = {
    'after_class': {
        'id': 'after_class',
        'name': '课后班级检查汇总表',
        'short': '课后勾选表',
        'dept': '纪检部',
        'priority': '首选试点',
        'summary': '班级行 × A–G 项目列，勾选后按印刷规则算分',
        'needs_grade': True,
        'needs_building': False,
        'items': [
            {'code': 'A', 'item': '灯未关（含黑板灯、走廊灯）', 'score': 5, 'unit': '项', 'qty_mode': 'fixed', 'rule': '5分/班'},
            {'code': 'B', 'item': '电脑/一体机未关', 'score': 5, 'unit': '项', 'qty_mode': 'fixed', 'rule': '5分/班'},
            {'code': 'C', 'item': '风扇/空调未关', 'score': 5, 'unit': '项', 'qty_mode': 'fixed', 'rule': '5分/班'},
            {'code': 'D', 'item': '门窗/窗帘未关', 'score': 5, 'unit': '项', 'qty_mode': 'fixed', 'rule': '5分/班'},
            {'code': 'E', 'item': '桌椅/卫生角/垃圾桶不规范', 'score': 2, 'unit': '处', 'qty_mode': 'place', 'rule': '2分/处'},
            {'code': 'F', 'item': '男女交往异常（已核实）', 'score': 50, 'unit': '人次', 'qty_mode': 'person', 'rule': '50分/人次', 'risk': 'high'},
            {'code': 'G', 'item': '安全隐患未及时报告', 'score': 2, 'unit': '项', 'qty_mode': 'fixed', 'rule': '2分/班'},
        ],
    },
    'daily_matrix': {
        'id': 'daily_matrix',
        'name': '年级日常量化表',
        'short': '年级手写矩阵',
        'dept': '纪检部',
        'priority': '第二期',
        'summary': '班级行 × 早/午/晚读列，一格可拆多项',
        'needs_grade': True,
        'needs_building': False,
        'items': [],
    },
    'dorm_log': {
        'id': 'dorm_log',
        'name': '宿舍检查评价表',
        'short': '宿舍检查评价表',
        'dept': '宿管部',
        'priority': '第三期',
        'summary': '行式明细：宿舍号×班级×扣分说明，宿舍安排映射班级，页边手写分值',
        'needs_grade': False,
        'needs_building': True,
        'items': [],
    },
}

ISSUE_WORDS = (
    '迟到', '早退', '讲话', '说话', '睡觉', '手机', '校服', '校牌', '早餐', '仪容',
    '打闹', '追逐', '吸烟', '打架', '顶撞', '卫生', '垃圾', '桌椅', '地面', '床铺',
    '被褥', '物品', '摆放', '未关', '灯', '电脑', '风扇', '空调', '门窗', '窗帘',
    '安全隐患', '交往', '熄灯', '讲话', '串寝', '晚归', '内务', '纪律',
)
STOPWORDS = {
    '检查人', '复核人', '年级', '班级', '日期', '星期', '早读', '午读', '晚读', '委员签到',
    '序号', '备注', '小计', '扣分', '加分', '楼栋', '楼层', '房间', '内务卫生', '纪律',
    '班主任', '值日负责人', '所在班级', '得分', '评分', '合计',
}
PERIODS = ('早读', '午读', '晚读')
CN_FLOOR = {'一': 1, '二': 2, '三': 3, '四': 4, '五': 5, '六': 6, '七': 7, '八': 8}

_OCR_LOCK = threading.Lock()
_ENGINE_PATH_READY = [False]   # engines/ 已注入 sys.path 的标记
_OCR_ENGINE = None
_OCR_ENGINE_NAME = ''
_OCR_ERROR = None

# ---------- 外挂引擎目录（桌面版"下载本地识别引擎"解压至此；优先于内置包） ----------
import sys as _sys

def engine_dir() -> str:
    """exe/脚本同目录下的 engines/ 目录。"""
    return os.path.join(os.path.dirname(os.path.abspath(__file__)), 'engines')

def ensure_engine_path():
    """外挂引擎目录存在时注入 sys.path 最前，使 rapidocr/cv2/onnxruntime 可被加载。"""
    p = engine_dir()
    if os.path.isdir(p) and p not in _sys.path:
        _sys.path.insert(0, p)

def engine_installed() -> bool:
    return os.path.isdir(os.path.join(engine_dir(), 'rapidocr'))

def reload_engine() -> bool:
    """下载解压后热重载：清缓存 + 卸载已导入的引擎模块 + 重探测。"""
    global _OCR_ENGINE, _OCR_ENGINE_NAME, _OCR_ERROR
    _OCR_ENGINE = None
    _OCR_ENGINE_NAME = ''
    _OCR_ERROR = None
    for m in list(_sys.modules):
        if m.split('.')[0] in ('rapidocr', 'rapidocr_onnxruntime', 'cv2', 'onnxruntime',
                               'pyclipper', 'shapely', 'omegaconf', 'colorlog'):
            del _sys.modules[m]
    info = engine_info()
    ok = bool(info.get('ready'))
    if ok:
        try:
            _get_engine()
        except Exception:
            ok = False
    return ok


def ensure_engine_path_if_needed():
    """环境/包内已有 rapidocr（venv、_internal）则不注入外挂目录（避免同源 DLL 冲突）；
    没有才注入 engines/——必须在任何 import cv2 之前调用（image_quality 等）。"""
    if _ENGINE_PATH_READY[0]:
        return
    try:
        import importlib.util as _ilu2
        if _ilu2.find_spec('rapidocr') is not None:
            _ENGINE_PATH_READY[0] = True   # 环境已装，无需外挂
            return
    except Exception:
        pass
    ensure_engine_path()
    _ENGINE_PATH_READY[0] = True


class PaperError(RuntimeError):
    pass


def public_templates():
    out = []
    for tid in ('after_class', 'daily_matrix', 'dorm_log'):
        t = TEMPLATES[tid]
        out.append({k: t[k] for k in ('id', 'name', 'short', 'dept', 'priority', 'summary', 'needs_grade', 'needs_building', 'items')})
    return out


def image_from_bytes(data: bytes):
    """返回 (BGR numpy, RGB PIL)；处理 EXIF 方向与 RGBA 白底。"""
    from PIL import Image, ImageOps
    import numpy as np
    if not data:
        raise PaperError('没有收到图片数据')
    try:
        img = Image.open(io.BytesIO(data))
        img = ImageOps.exif_transpose(img)
        if img.mode in ('RGBA', 'LA'):
            bg = Image.new('RGB', img.size, 'white')
            bg.paste(img.convert('RGB'), mask=img.getchannel('A'))
            img = bg
        else:
            img = img.convert('RGB')
    except Exception as exc:
        raise PaperError('无法读取这张图片，请换 JPG/PNG 重试') from exc
    max_side = 2400
    if max(img.size) > max_side:
        scale = max_side / max(img.size)
        img = img.resize((max(1, int(img.width * scale)), max(1, int(img.height * scale))), Image.LANCZOS)
    rgb = np.array(img)
    bgr = rgb[:, :, ::-1].copy()
    return bgr, img


def image_quality(bgr):
    import cv2
    gray = cv2.cvtColor(bgr, cv2.COLOR_BGR2GRAY)
    h, w = gray.shape
    blur = float(cv2.Laplacian(gray, cv2.CV_64F).var())
    brightness = float(gray.mean())
    issues = []
    if w < 900 or h < 650:
        issues.append('图片分辨率偏低，建议使用 1080P 以上照片')
    if blur < 45:
        issues.append('画面较模糊，请保持手机/摄像头稳定后重拍')
    if brightness < 55:
        issues.append('画面偏暗，请补充光线')
    elif brightness > 215:
        issues.append('画面过亮，可能丢失手写细节')
    return {'width': w, 'height': h, 'blur': round(blur, 1), 'brightness': round(brightness, 1), 'issues': issues}


def _get_engine():
    global _OCR_ENGINE, _OCR_ENGINE_NAME, _OCR_ERROR
    if _OCR_ENGINE is not None:
        return _OCR_ENGINE
    errors = []

    def _try3():
        from rapidocr import RapidOCR as RapidOCR3
        return RapidOCR3(params={'Global.log_level': 'error'})

    def _try1():
        from rapidocr_onnxruntime import RapidOCR
        return RapidOCR()

    def _name3():
        try:
            from importlib.metadata import version
            return 'RapidOCR ' + version('rapidocr') + ' / PP-OCRv6'
        except Exception:
            return 'RapidOCR 3 / PP-OCRv6'

    # 第一轮：环境/包内已安装的引擎（venv、_internal），不注入外挂目录，
    # 避免同源包的 DLL 冲突（engines/ 是同一套文件的解压副本）
    for label, fn in (('RapidOCR3', _try3, ), ('RapidOCR1', _try1)):
        try:
            _OCR_ENGINE = fn()
            _OCR_ENGINE_NAME = _name3() if label == 'RapidOCR3' else 'RapidOCR / PP-OCRv3'
            _OCR_ERROR = None
            return _OCR_ENGINE
        except Exception as exc:
            errors.append(label + ': ' + str(exc))
    # 第二轮：注入外挂引擎目录（engines/）重试（桌面版"下载安装引擎"后的首次加载）
    ensure_engine_path_if_needed()
    for label, fn, nm in (('RapidOCR3(外挂)', _try3, 'RapidOCR 3 / PP-OCRv6'),
                          ('RapidOCR1(外挂)', _try1, 'RapidOCR / PP-OCRv3')):
        try:
            _OCR_ENGINE = fn()
            _OCR_ENGINE_NAME = nm
            _OCR_ERROR = None
            return _OCR_ENGINE
        except Exception as exc:
            errors.append(label + ': ' + str(exc))
    _OCR_ERROR = ' | '.join(errors)
    raise PaperError('本地 OCR 引擎未就绪：' + _OCR_ERROR[:200])


def engine_info():
    """不加载模型，只报告安装了哪一套本地引擎。优先外挂目录，其次内置/环境安装。
    注意：这里不做 sys.path 注入（注入属于真实加载阶段 _get_engine 的职责）。"""
    if engine_installed():
        v = ''
        try:
            from importlib.metadata import version
            v = version('rapidocr')
        except Exception:
            # 外挂目录的元数据不在 importlib 检索路径，读 dist-info 目录名兜底
            try:
                import glob as _glob
                m = _glob.glob(os.path.join(engine_dir(), 'rapidocr-*.dist-info'))
                v = os.path.basename(m[0]).split('-')[1] if m else ''
            except Exception:
                pass
        return {'ready': True, 'name': 'RapidOCR ' + (v or '3') + ' / PP-OCRv6', 'engine': 'rapidocr3'}
    try:
        from importlib.metadata import version
        v = version('rapidocr')
        return {'ready': True, 'name': 'RapidOCR ' + v + ' / PP-OCRv6', 'engine': 'rapidocr3'}
    except Exception:
        pass
    try:
        from importlib.metadata import version
        v = version('rapidocr_onnxruntime')
        return {'ready': True, 'name': 'RapidOCR ' + v + ' / PP-OCRv3', 'engine': 'rapidocr1'}
    except Exception:
        return {'ready': False, 'name': '未安装本地 OCR', 'engine': ''}


def ocr_available():
    """真实加载探测：返回 (ok, name, err)。目录/包元数据存在不代表能加载（DLL/ABI 可能损坏）。"""
    global _OCR_ERROR
    try:
        _get_engine()
        return True, _OCR_ENGINE_NAME or '', ''
    except Exception as exc:
        return False, '', str(exc)[:200]


def _token_from_box(box, text, score):
    pts = [(float(p[0]), float(p[1])) for p in box]
    xs, ys = [p[0] for p in pts], [p[1] for p in pts]
    return {
        'text': str(text or '').strip(), 'confidence': round(float(score) * 100, 0),
        'x': min(xs), 'y': min(ys), 'right': max(xs), 'bottom': max(ys),
        'cx': sum(xs) / len(xs), 'cy': sum(ys) / len(ys),
        'w': max(xs) - min(xs), 'h': max(ys) - min(ys),
    }


def _run_ocr(bgr):
    with _OCR_LOCK:
        engine = _get_engine()
        t0 = time.time()
        try:
            raw = engine(bgr)
        except Exception as exc:
            raise PaperError('OCR 执行失败：' + str(exc)[:160]) from exc
        elapsed = time.time() - t0
    tokens = []
    if hasattr(raw, 'txts') and hasattr(raw, 'boxes'):
        # rapidocr 3.x: RapidOCROutput（boxes 是 ndarray，不能用 or [] 判断）
        boxes = raw.boxes if raw.boxes is not None else []
        txts = raw.txts if raw.txts is not None else []
        scores = raw.scores if raw.scores is not None else []
        for box, text, score in zip(boxes, txts, scores):
            try:
                t = _token_from_box(box, text, score)
                if t['text']:
                    tokens.append(t)
            except Exception:
                continue
    else:
        # rapidocr 1.x: (result, elapse)
        result = raw[0] if isinstance(raw, tuple) else raw
        for row in result or []:
            try:
                t = _token_from_box(row[0], row[1], row[2])
                if t['text']:
                    tokens.append(t)
            except Exception:
                continue
    tokens.sort(key=lambda t: (t['y'], t['x']))
    return tokens, elapsed


def _norm_text(s):
    s = (s or '').replace('（', '(').replace('）', ')').replace('：', ':').replace('，', ',').replace('。', ' ').replace('、', ',')
    s = re.sub(r'\s+', ' ', s).strip()
    return s


def _class_no(text):
    s = _norm_text(text)
    m = re.search(r'(?<!\d)([1-9]|[12]\d|30)\s*班', s)
    return int(m.group(1)) if m else None


def _class_name(grade, no, classes):
    """把纸表上的班号映射到当前年级的班级字典。
    字典缺失或该班号不存在时返回空串——绝不编造“高一（5）班”这种假班级，
    由上层标成必须人工复核。"""
    pool = list((classes or {}).get(grade) or [])
    for c in pool:
        if re.search(r'[（(]%d[）)]班' % no, c) or re.search(r'(?<!\d)%d班' % no, c):
            return c
    return ''


def _grade_from_cls(cls):
    for g in ('初一', '初二', '初三', '高一', '高二', '高三'):
        if (cls or '').startswith(g):
            return g
    return ''


_OCR_FIXES = (
    ('退到', '迟到'), ('送到', '迟到'), ('买到', '迟到'), ('达到', '迟到'), ('迟刭', '迟到'), ('迟列', '迟到'),
    ('荣早', '带早餐'), ('早参', '带早餐'), ('带早', '带早餐'), ('带早餐餐', '带早餐'),
    ('末戴', '未戴'), ('未载', '未戴'), ('戴校卑', '未戴校牌'), ('戴枚牌', '未戴校牌'),
    ('藏枝牌', '未戴校牌'), ('戴枝牌', '未戴校牌'), ('藏板牌', '未戴校牌'),
    ('未戴校牌', '未佩戴校牌'), ('校牌', '校牌'),
    ('校服不合', '校服不合格'), ('浪服', '校服'), ('讲活', '讲话'), ('睡党', '睡觉'),
    ('卫坐', '卫生'), ('垃极', '垃圾'),
)

def _fix_ocr_text(text):
    s = _norm_text(text)
    # 去掉勾选符号、单元格边缘漏进来的班级序号
    s = re.sub(r'^[Vv√✓✔×xX\s]+', '', s)
    s = re.sub(r'^(\d{1,2})(?!\s*(?:人|名|处|次|间))(?=[\u4e00-\u9fa5])', '', s)
    for a, b in _OCR_FIXES:
        s = s.replace(a, b)
    s = s.replace('未未佩戴', '未佩戴').replace('未未戴', '未戴')
    return s

_RULE_ALIAS = (
    ('板牌', '校牌'), ('校卑', '校牌'), ('未带校牌', '未佩戴校牌'), ('没戴校牌', '未佩戴校牌'),
    ('头发长', '头发过长'), ('长头发', '头发过长'), ('睡堂', '课堂睡觉'),
)


def _rule_core(item):
    """细则名去掉场景前缀后的“核心词”，用于短手写词的匹配。
    例：仪容仪表检查·头发过长 → 头发过长；早读巡查·迟到 → 迟到。"""
    s = re.sub(r'[（(].*?[）)]', '', item or '')
    parts = [x for x in re.split(r'[·•\-—_/]', s) if x.strip()]
    s = parts[-1] if parts else s
    s = re.sub(r'^(仪容仪表|早读|午读|晚读|课堂|宿舍|卫生|两操|课间|自习|就餐|升旗|集会)(检查|巡查|纪律|状态|管理|秩序)', '', s)
    return re.sub(r'[\s()（）:：,，。;；\-_/]', '', s)


def _rule_scores(text, rules, dept='', grade=''):
    """给每条细则打分（0-1），按分数降序返回 [(score, rule)]。不裁剪阈值，供上层决定。"""
    import difflib
    s = _fix_ocr_text(text)
    clean = re.sub(r'[\s\d:.,，。;；、()（）\-_/]', '', s)
    clean = re.sub(r'(人|名|人次|处|次|间|项)$', '', clean)
    if not clean:
        return []
    for a, b in _RULE_ALIAS:
        if a in clean:
            clean = clean.replace(a, b)
    out = []
    for r in rules or []:
        if (r.get('type') or '扣分') != '扣分':
            continue
        if dept and r.get('dept') and r.get('dept') != dept:
            continue
        rg = r.get('grade') or ''
        if grade and rg not in ('', '通用', grade):
            continue
        item = re.sub(r'[\s()（）·:：,，。;；\-_/]', '', r.get('item') or '')
        if not item:
            continue
        score = 0.0
        if clean == item:
            score = 1.0
        elif len(clean) >= 3 and (clean in item or item in clean) and abs(len(clean) - len(item)) <= 6:
            score = 0.92
        elif len(clean) == 2 and len(item) <= 6 and (clean in item or item in clean):
            score = 0.86
        elif len(clean) >= 4 and len(item) >= 4:
            ratio = difflib.SequenceMatcher(None, clean, item).ratio()
            if ratio > 0.55:
                score = ratio * 0.8
        # 核心词匹配：手写常只写事项本身，细则带“XX检查·”前缀
        if score < 0.9:
            core = _rule_core(r.get('item') or '')
            if core and len(clean) >= 2:
                if clean == core:
                    score = max(score, 0.95)
                elif clean in core:
                    extra = len(core) - len(clean)
                    if extra <= 2:
                        score = max(score, 0.88)
                    elif extra <= 6:
                        score = max(score, 0.80)
                elif core in clean and len(core) >= 3:
                    score = max(score, 0.82)
        if score > 0:
            out.append((score, r))
    out.sort(key=lambda x: (-x[0], len(x[1].get('item') or '')))
    return out


def _rule_match(text, rules, dept='', grade=''):
    """在现有细则里找最接近的扣分项。短词只在“近似等长”时匹配，避免把“迟到”匹配到长条款。"""
    for score, r in _rule_scores(text, rules, dept, grade):
        return r if score >= 0.72 else None
    return None


def rule_candidates(text, rules, dept='', grade='', topn=3, min_sim=0.55, prefer_dept=''):
    """本地 Top-N 细则候选（不调用任何模型）。返回 [{item, dept, score, sim}]。
    同部门候选给 0.03 加权，只影响排序，不影响是否入选。"""
    scored = _rule_scores(text, rules, dept, grade)
    if prefer_dept:
        scored = [(min(1.0, sc + (0.03 if (r.get('dept') or '') == prefer_dept else 0)), r)
                  for sc, r in scored]
        scored.sort(key=lambda x: -x[0])
    out, seen = [], set()
    for sim, r in scored:
        if sim < min_sim:
            break
        item = r.get('item') or ''
        if not item or item in seen:
            continue
        seen.add(item)
        try:
            sc = abs(float(r.get('score') or 0))
        except (TypeError, ValueError):
            sc = 0
        out.append({'item': item, 'dept': r.get('dept') or '', 'score': sc, 'sim': round(sim, 2)})
        if len(out) >= topn:
            break
    return out


def _extract_qty(text):
    s = _norm_text(text)
    m = re.search(r'(\d{1,3})\s*(?:人|名|人次|处|次|间)', s)
    if m:
        return max(1, int(m.group(1)))
    # “学号20、21”这类没有“人”字，但多个分隔数字表示多名学生
    nums = re.findall(r'(?<!\d)(\d{1,3})(?!\d)', s)
    if len(nums) >= 2 and re.search(r'[、,，/]', s):
        return len(nums)
    # 单独一个数字不等同于人数（可能是日期、总分或被识别出来的页眉）
    return 1


def _meaningful_text(text):
    t = re.sub(r'[\s\d:.,，。;；、()（）/\\\-_]+', '', _norm_text(text))
    if len(t) < 2 or t in STOPWORDS:
        return False
    return any(w in _norm_text(text) for w in ISSUE_WORDS)


def _group_lines(tokens, y_tol=0.65):
    lines = []
    for t in sorted(tokens, key=lambda x: (x['cy'], x['cx'])):
        placed = False
        for line in lines:
            ref = line[0]
            h = max(8.0, min(ref['h'], t['h']))
            if abs(ref['cy'] - t['cy']) <= max(8.0, h * y_tol):
                line.append(t)
                placed = True
                break
        if not placed:
            lines.append([t])
    for line in lines:
        line.sort(key=lambda x: x['x'])
    lines.sort(key=lambda line: min(t['y'] for t in line))
    return lines


def _cluster_positions(values, tol):
    vals = sorted(values)
    groups = []
    for v in vals:
        if not groups or abs(v - sum(groups[-1]) / len(groups[-1])) > tol:
            groups.append([v])
        else:
            groups[-1].append(v)
    return [sum(g) / len(g) for g in groups]


def _detect_checkmarks(bgr):
    """检测小方框内是否有勾/叉。返回坐标和内部墨迹比例，不判断业务。"""
    try:
        import cv2
        import numpy as np
    except Exception:
        return []
    gray = cv2.cvtColor(bgr, cv2.COLOR_BGR2GRAY)
    bw = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, 21, 8)
    contours, _ = cv2.findContours(bw, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
    boxes = []
    for cnt in contours:
        x, y, w, h = cv2.boundingRect(cnt)
        if not (9 <= w <= 36 and 9 <= h <= 36):
            continue
        if not (0.68 <= w / max(1, h) <= 1.45):
            continue
        # 方框外轮廓本身应有一定的线条感
        peri = cv2.arcLength(cnt, True)
        if peri < 28 or peri > 180:
            continue
        m = 4
        if w <= 2 * m + 4 or h <= 2 * m + 4:
            continue
        inner = gray[y + m:y + h - m, x + m:x + w - m]
        if inner.size == 0:
            continue
        ratio = float((inner < 145).mean())
        boxes.append({'x': x, 'y': y, 'w': w, 'h': h, 'cx': x + w / 2, 'cy': y + h / 2, 'ink': ratio})
    # 同一位置可能得到内外两个轮廓，按中心去重
    out = []
    for b in sorted(boxes, key=lambda z: z['ink'], reverse=True):
        if any(abs(b['cx'] - a['cx']) < 6 and abs(b['cy'] - a['cy']) < 6 for a in out):
            continue
        out.append(b)
    return out


def _base_candidate(idx, template_id, item, qty, score, unit, formula, desc, conf, status='ok', issue='', unit_score=None, qty_mode='fixed', **extra):
    """候选统一结构。score/total 是合计；unit_score 是单价（服务端确认时会重新解析，不信任客户端）。
    qty_mode: fixed=数量固定 1；place=按处数；person=按人次。"""
    qty = max(1, int(qty or 1))
    total = float(score or 0)
    try:
        unit_score = float(unit_score) if unit_score is not None else round(total / qty, 2)
    except (TypeError, ValueError):
        unit_score = 0.0
    c = {
        'id': 'p%d' % idx,
        'template': template_id,
        'item': item[:80],
        'qty': qty,
        'qty_mode': qty_mode,
        'unit': unit or '项',
        'unit_score': unit_score,
        'score': total,
        'total': total,
        'formula': formula or '',
        'desc': desc[:500],
        'confidence': int(round(max(0, min(100, conf or 0)))),
        'status': status,
        'issue': issue,
        'selected': True,
    }
    c.update(extra)
    return c


def _parse_after(tokens, boxes, bgr, ctx, classes):
    """印刷坐标 + 小方框勾选。只输出被勾选的项目。"""
    import numpy as np
    h, w = bgr.shape[:2]
    items = {x['code']: x for x in TEMPLATES['after_class']['items']}
    headers = {}
    for code in items:
        cand = []
        for t in tokens:
            if t['y'] > h * 0.48:
                continue
            tx = _norm_text(t['text']).upper()
            if len(tx) <= 8 and (code in tx and any(k in tx for k in ('灯', '电脑', '风扇', '门', '桌椅', '交往', '安全')) or tx == code):
                cand.append(t)
        if cand:
            headers[code] = min(cand, key=lambda z: abs(z['y'] - min(q[ 'y'] for q in cand)))['cx']
    # 班级行：左列印刷 1班、2班……；同一行右侧重复列时取最左
    class_tokens = []
    for t in tokens:
        no = _class_no(t['text'])
        if no and no <= 40 and t['x'] < w * 0.48:
            class_tokens.append((no, t))
    rows = []
    for no, t in sorted(class_tokens, key=lambda x: x[1]['cy']):
        if rows and abs(rows[-1]['cy'] - t['cy']) < max(9, t['h'] * 0.7):
            if t['x'] < rows[-1]['x']:
                rows[-1].update({'no': no, 'cy': t['cy'], 'x': t['x']})
        else:
            rows.append({'no': no, 'cy': t['cy'], 'x': t['x']})
    if not rows:
        return [], ['未识别到左侧班级行，无法把勾选映射到班级。请拍全左侧班级列。']
    if not headers:
        return [], ['未识别到 A–G 项目表头，无法判断勾选属于哪一项。请拍全表头。']

    # 小方框：只保留表格范围内的，使用中位墨迹作为空框基线
    min_y = min(r['cy'] for r in rows) - 35
    max_y = max(r['cy'] for r in rows) + 35
    min_x = headers.get('A', min(headers.values())) - 85
    max_x = headers.get('G', max(headers.values())) + 85
    valid_boxes = [b for b in boxes if min_x <= b['cx'] <= max_x and min_y <= b['cy'] <= max_y]
    inks = [b['ink'] for b in valid_boxes]
    base = float(np.median(inks)) if inks else 0.0
    threshold = max(0.055, base + 0.035)

    marks = [b for b in valid_boxes if b['ink'] >= threshold]
    # OCR 有时会直接识别出勾/叉，补充进候选位置
    for t in tokens:
        if _norm_text(t['text']).lower() in ('v', '✓', '√', '✔', 'x', '×') and min_x <= t['cx'] <= max_x:
            marks.append({'cx': t['cx'], 'cy': t['cy'], 'ink': 0.2})

    row_gap = 45.0
    if len(rows) > 1:
        diffs = [rows[i + 1]['cy'] - rows[i]['cy'] for i in range(len(rows) - 1)]
        diffs = [d for d in diffs if d > 5]
        if diffs:
            row_gap = float(np.median(diffs))
    seen, out = set(), []
    for b in sorted(marks, key=lambda z: (z['cy'], z['cx'])):
        row = min(rows, key=lambda r: abs(r['cy'] - b['cy']))
        if abs(row['cy'] - b['cy']) > max(12, row_gap * 0.62):
            continue
        code = min(headers, key=lambda k: abs(headers[k] - b['cx']))
        if abs(headers[code] - b['cx']) > 95:
            continue
        if (row['no'], code) in seen:
            continue
        seen.add((row['no'], code))
        meta = items[code]
        cls = _class_name(ctx.get('grade'), row['no'], classes)
        risk_high = meta.get('risk') == 'high'
        status = 'error' if risk_high else 'ok'
        issue = 'F类高分值且须核实，识别后仍需人工确认' if risk_high else ''
        conf = min(99, 88 + (b.get('ink', 0.1) * 100))
        known = ctx.get('_known_classes') or []
        if not cls:
            status = 'error'
            issue = '第%d班不在当前年级班级字典中，请核对年级或班级序号' % row['no']
        elif known and cls not in known:
            status = 'error'
            issue = '班级未匹配到当前年级班级字典'
        out.append(_base_candidate(
            len(out) + 1, 'after_class', meta['item'], 1, meta['score'], meta['unit'],
            meta['rule'], '来源：%d班 × %s项；复选框检测墨迹 %.3f；模板规则自动算分' % (row['no'], code, b.get('ink', 0)),
            conf, status, issue,             target=cls, grade=_grade_from_cls(cls), cls=cls, dept='纪检部',
            unit_score=meta['score'], qty_mode=meta.get('qty_mode', 'fixed'), paper_code=code,
            roi=[int(max(0, b.get('x', b['cx'] - 35))), int(max(0, b.get('y', b['cy'] - 35))),
                 int(min(w, b.get('right', b['cx'] + 35))), int(min(h, b.get('bottom', b['cy'] + 35)))],
            cell='%d班 × %s' % (row['no'], code), risk=meta.get('risk', ''),
        ))
    warns = []
    if not out:
        warns.append('未检测到明确勾选项。请确认纸表是“勾选表示存在问题”，或改用手动补录。')
    if len(valid_boxes) < 10:
        warns.append('检测到的方框数量偏少，照片可能未拍全或表格线不清晰。')
    if any(r['no'] > len(((classes or {}).get(ctx.get('grade')) or [])) for r in rows):
        warns.append('部分班级行号超出当前年级班级字典，请核对年级选择。')
    return out, warns


def _extract_cell_qty_detail(text):
    """返回 (确认数量, 疑似数量)。只有“数字+单位”或“多个人名/编号用分隔符列出”才算确认数量；
    冒号后的单数字、末尾粘连的单数字只作为疑似数量，交给人工确认，避免把“校牌:5”当 5 人。"""
    s = _norm_text(text)
    m = re.search(r'(\d{1,3})\s*(?:人|名|人次|处|次|间)', s)
    if m:
        return max(1, int(m.group(1))), 0
    nums = re.findall(r'(?<!\d)(\d{1,3})(?!\d)', s)
    if len(nums) >= 2 and re.search(r'[、,，/]', s):
        return len(nums), 0
    if len(nums) == 1:
        n = int(nums[0])
        if 1 <= n <= 20 and (re.search(r'[:：]', s) or re.search(r'[\u4e00-\u9fa5]\s*\d{1,3}\s*$', s)):
            return 1, n
    return 1, 0

def _extract_cell_qty(text):
    return _extract_cell_qty_detail(text)[0]

def _extract_cell_qty_suspect(text):
    return _extract_cell_qty_detail(text)[1]

def _ocr_cell_text(bgr, x0, x1, y0, y1):
    """把疑似问题格放大后单独 OCR 一次；返回 (文本, 平均置信度)。"""
    try:
        import cv2
    except Exception:
        return '', 0
    h, w = bgr.shape[:2]
    x0, x1 = max(0, int(x0)), min(w, int(x1))
    y0, y1 = max(0, int(y0)), min(h, int(y1))
    if x1 - x0 < 12 or y1 - y0 < 12:
        return '', 0
    crop = bgr[y0:y1, x0:x1]
    scale = max(2.0, min(4.0, 120.0 / max(1, crop.shape[0])))
    crop = cv2.resize(crop, None, fx=scale, fy=scale, interpolation=cv2.INTER_CUBIC)
    try:
        toks, _ = _run_ocr(crop)
    except Exception:
        return '', 0
    parts, scores = [], []
    for t in toks:
        s = _norm_text(t['text'])
        if not s:
            continue
        if re.fullmatch(r'[Vv√✓✔xX×\-—_.,，。:：;；]+', s):
            continue
        if not re.search(r'[\u4e00-\u9fa5]', s) and not re.search(r'\d', s):
            continue
        parts.append(s)
        scores.append(t['confidence'])
    return ''.join(parts), (sum(scores) / len(scores) if scores else 0)

def _cell_components(bgr, x0, x1, y0, y1):
    """统计单元格内小墨迹连通域，作为“这一格有手写”的补充证据。"""
    try:
        import cv2
        import numpy as np
    except Exception:
        return 0
    x0, x1 = max(0, int(x0)), min(bgr.shape[1], int(x1))
    y0, y1 = max(0, int(y0)), min(bgr.shape[0], int(y1))
    if x1 - x0 < 8 or y1 - y0 < 8:
        return 0
    gray = cv2.cvtColor(bgr[y0:y1, x0:x1], cv2.COLOR_BGR2GRAY)
    bw = (gray < 108).astype(np.uint8)
    n, lab, stats, cent = cv2.connectedComponentsWithStats(bw, 8)
    cnt = 0
    for j in range(1, n):
        area = int(stats[j, cv2.CC_STAT_AREA])
        ww = int(stats[j, cv2.CC_STAT_WIDTH])
        hh = int(stats[j, cv2.CC_STAT_HEIGHT])
        if 5 <= area <= 700 and ww >= 2 and hh >= 2 and ww <= (x1 - x0) * 0.9:
            cnt += 1
    # 窄格写得少也算；归一到约 300px 宽，减少列宽差异
    return cnt * (300.0 / max(1, x1 - x0))

def _parse_daily(tokens, bgr, ctx, classes, rules, refine=False):
    h, w = bgr.shape[:2]
    period_headers = {}
    for p in PERIODS:
        cand = [t for t in tokens if p in _norm_text(t['text']) and t['y'] < h * 0.55]
        if cand:
            period_headers[p] = min(cand, key=lambda z: z['y'])['cx']
    period_cols = sorted(period_headers.items(), key=lambda x: x[1])
    if not period_cols:
        return [], ['未识别到早读/午读/晚读表头，时段需人工确认。请拍全表头后重试。']
    # 班级行：取左半页印刷“N班”，同一行右侧重复列只保留最左
    rows = []
    for t in tokens:
        no = _class_no(t['text'])
        if no and no <= 40 and t['x'] < w * 0.5:
            if rows and abs(rows[-1]['cy'] - t['cy']) < max(9, t['h'] * 0.7):
                if t['x'] < rows[-1]['x']:
                    rows[-1].update({'no': no, 'cy': t['cy'], 'x': t['x'], 'right': t['right']})
            else:
                rows.append({'no': no, 'cy': t['cy'], 'x': t['x'], 'right': t['right']})
    if not rows:
        return [], ['未识别到左侧班级行，无法把问题格映射到班级。请拍全左侧班级列。']
    xs = [v for _, v in period_cols]
    bounds = [xs[0] - (xs[1] - xs[0]) * 0.55 if len(xs) > 1 else xs[0] - 180]
    for i in range(1, len(xs)):
        bounds.append((xs[i - 1] + xs[i]) / 2)
    bounds.append(min(w, xs[-1] + ((xs[-1] - xs[-2]) if len(xs) > 1 else 260) * 0.8))
    gaps = [rows[i + 1]['cy'] - rows[i]['cy'] for i in range(len(rows) - 1) if rows[i + 1]['cy'] > rows[i]['cy']]
    if not gaps:
        return [], ['班级行间距异常，无法定位问题格。']
    gaps.sort()
    gap = gaps[len(gaps) // 2]
    out, warnings = [], []
    for ri, row in enumerate(rows):
        cls = _class_name(ctx.get('grade'), row['no'], classes)
        y0 = row['cy'] - gap * 0.46
        y1 = row['cy'] + gap * 0.46
        for pi, (period, _) in enumerate(period_cols):
            bx0, bx1 = bounds[pi], bounds[pi + 1]
            cell_tokens = []
            for t in tokens:
                if not (bx0 + 4 <= t['cx'] <= bx1 - 4 and y0 <= t['cy'] <= y1):
                    continue
                s = _norm_text(t['text'])
                if not s or s in STOPWORDS or _class_no(s) or s in ('✓', '√', 'V', 'v'):
                    continue
                if any(p in s and len(s) <= 6 for p in PERIODS):
                    continue
                cell_tokens.append(t)
            cell_tokens.sort(key=lambda z: z['x'])
            raw_whole = _fix_ocr_text(' / '.join(t['text'] for t in cell_tokens))
            ink = _cell_components(bgr, bx0 + 10, bx1 - 10, y0 + gap * 0.08, y1 - gap * 0.08)
            rule0 = _rule_match(raw_whole, rules, '') if raw_whole else None
            meaningful = bool(rule0) or any(x in raw_whole for x in ISSUE_WORDS)
            if ink < 5.0 and not meaningful:
                continue
            if not meaningful and ink < 6.5:
                continue
            # 整页 OCR 已被表线干扰；对疑似问题格放大后二次识别，手写短句提升最明显
            raw = raw_whole
            conf_cell = 0
            refined = False
            if refine and ink >= 2.0:
                cell_text, cell_conf = _ocr_cell_text(bgr, bx0 + 12, bx1 - 12, y0 + gap * 0.1, y1 - gap * 0.1)
                if cell_text:
                    raw = _fix_ocr_text(cell_text)
                    conf_cell = cell_conf
                    refined = True
            # 没有中文也没有命中细则的碎片（如边缘“5K”）不生成候选
            if raw and not re.search(r'[\u4e00-\u9fa5]', raw) and not _rule_match(raw, rules, ''):
                continue
            conf = int(round(conf_cell)) if conf_cell else (int(round(sum(t['confidence'] for t in cell_tokens) / len(cell_tokens))) if cell_tokens else 0)
            qty_whole = max(_extract_cell_qty(raw_whole) if raw_whole else 1, _extract_cell_qty(raw) if raw else 1)
            clauses = [x.strip() for x in re.split(r'[、,，;；/]', raw) if x.strip()] if raw else ['']
            # 去重且只保留有意义的短句，最多拆 3 项，避免一行手写拆得过碎
            tmp = []
            for x in clauses:
                if x not in tmp and (len(x) >= 2 or re.search(r'\d', x)):
                    tmp.append(x)
            clauses = tmp[:3] or ['']
            for ci, clause in enumerate(clauses):
                rule = _rule_match(clause, rules, '') if clause else None
                dept = (rule or {}).get('dept') or ctx.get('dept') or ''
                qty, qty_suspect = _extract_cell_qty_detail(clause) if clause else (1, 0)
                if len(clauses) == 1:
                    qty = max(qty, qty_whole)
                if rule:
                    item = rule.get('item') or clause[:40]
                    base_score = abs(float(rule.get('score') or 0))
                    score = base_score * qty
                    formula = '%s；按 %s分 × %d%s（口径待确认）' % (item, rule.get('score'), qty, '人' if '人' in clause else '处')
                else:
                    item = clause[:40] if clause else ''
                    base_score = 0
                    score = 0
                    formula = '检测到问题格有手写，OCR 未可靠匹配细则'
                status, issue = 'ok', ''
                if not cls:
                    status, issue = 'error', '第%d班不在当前年级班级字典中，请核对年级或班级序号' % row['no']
                elif not clause or not rule or score <= 0:
                    status, issue = 'error', '手写内容未可靠识别，请对照原图补全事项和分值'
                elif qty_suspect > 1 and qty <= 1:
                    status, issue = 'error', '疑似数量 %d（冒号/末尾数字），请确认后填写数量' % qty_suspect
                elif qty > 1:
                    status, issue = 'error', '识别到数量 %d，请确认按人还是按班计分' % qty
                elif conf < 78:
                    status, issue = 'error', '手写识别置信度偏低，请对照原图'
                desc_base = ('逐格识别：' if refined else '识别原文：') + clause
                desc = desc_base if clause else '检测到该问题格有手写内容，OCR 未识别出可靠文字'
                cell_label = '%d班 × %s' % (row['no'], period)
                if len(clauses) > 1:
                    cell_label += '·%d' % (ci + 1)
                out.append(_base_candidate(
                    len(out) + 1, 'daily_matrix', item, qty, score, '人' if ('人' in clause or '名' in clause) else ('处' if '处' in clause else '项'),
                    formula, desc + ('；检测墨迹 %.1f' % ink), conf, status, issue,
                    unit_score=base_score, qty_mode='count', suspect_qty=qty_suspect,
                    target=cls, grade=_grade_from_cls(cls), cls=cls, dept=dept,
                    roi=[int(bx0), int(y0), int(bx1), int(y1)],
                    cell=cell_label,
                ))
    if not out:
        warnings.append('未从早读/午读/晚读问题格中识别出扣分事项。请确认照片方向、清晰度和表头完整性。')
    if len(out) >= 60:
        warnings.append('候选较多，已截取前 60 个问题格，请核对是否拍入了额外纸张。')
    return out[:60], warnings


def _dorm_is_printed_noise(text):
    s = _norm_text(text)
    if not s:
        return True
    if re.fullmatch(r'[一二三四五六七八]\s*楼', s):
        return True
    if any(k in s for k in ('扣分', '加分', '说明', '打分', '分说', '分设', '分税', '分谨', '内务', '纪律', '卫生', '房号', '检查人', '评分', '标准', '时段', '星期', '宿舍', '项目')):
        # 带明确扣分数值的仍可能是手写备注，保留
        if not re.search(r'[-−]\s*\d', s):
            return True
    return False

def _parse_dorm(tokens, bgr, ctx, rules, dorm_rows):
    h, w = bgr.shape[:2]
    floor_tokens = []
    for t in tokens:
        m = re.search(r'([一二三四五六七八])\s*楼', _norm_text(t['text']))
        if m:
            floor_tokens.append((CN_FLOOR[m.group(1)], t))
    if not floor_tokens:
        return [], ['未识别到左侧楼层（一楼、二楼……），无法组合房号。请拍全左侧楼层列。']
    first_floor_y = min(t['cy'] for _, t in floor_tokens)
    room_hits = {}
    for t in tokens:
        s = _norm_text(t['text'])
        m = re.match(r'^0?(\d{1,2})(?:\s|$)', s)
        if m and t['y'] < first_floor_y - 15:
            n = int(m.group(1))
            if 1 <= n <= 30 and t['cx'] > w * 0.12:
                room_hits.setdefault(n, []).append(t['cx'])
    room_x = {}
    for n, arr in room_hits.items():
        if arr:
            arr.sort()
            room_x[n] = arr[len(arr) // 2]
    if not room_x:
        return [], ['未识别到房号表头（01、02……），无法定位房间。请拍全表格上方的房号列。']
    # 补全 OCR 漏掉的房号列（按已识别房号间距线性推断）
    nums = sorted(room_x)
    if len(nums) >= 3:
        dx = [room_x[b] - room_x[a] for a, b in zip(nums, nums[1:]) if room_x[b] > room_x[a]]
        dx.sort()
        step = dx[len(dx) // 2] if dx else 60
        for n in range(1, min(30, max(nums)) + 1):
            if n not in room_x:
                near = min(nums, key=lambda k: abs(k - n))
                room_x[n] = room_x[near] + (n - near) * step
        room_x = {n: x for n, x in room_x.items() if w * 0.12 <= x <= w * 0.95}
    building = (ctx.get('building') or '').strip()
    if not building:
        for t in tokens[:30]:
            m = re.search(r'([\u4e00-\u9fa5]{1,6}(?:栋|楼))', _norm_text(t['text']))
            if m and '教学' not in m.group(1):
                building = m.group(1)
                break
    room_map = {}
    for r in dorm_rows or []:
        if building and r.get('building') and r['building'] != building:
            continue
        room = str(r.get('room') or '')
        if room:
            room_map.setdefault(room, [])
            if r.get('cls') and r['cls'] not in room_map[room]:
                room_map[room].append(r['cls'])

    floors = sorted(floor_tokens, key=lambda x: x[1]['cy'])
    fys = [t['cy'] for _, t in floors]
    bands = []
    for i, (fl, t) in enumerate(floors):
        top = (fys[i - 1] + fys[i]) / 2 if i else fys[i] - 75
        bottom = (fys[i] + fys[i + 1]) / 2 if i + 1 < len(fys) else fys[i] + 155
        bands.append((fl, top, bottom))
    step_x = 60.0
    if len(room_x) > 1:
        diffs = [abs(room_x[b] - room_x[a]) for a, b in zip(sorted(room_x), sorted(room_x)[1:]) if room_x[b] != room_x[a]]
        if diffs:
            diffs.sort()
            step_x = diffs[len(diffs) // 2]
    out, warnings, seen = [], [], set()
    for t in tokens:
        text = _norm_text(t['text'])
        if not text or len(text) < 2 or len(text) > 40:
            continue
        if t['cy'] < first_floor_y - 8 or _dorm_is_printed_noise(text) or _class_no(text):
            continue
        if not re.search(r'[\u4e00-\u9fa5]', text) and not re.search(r'\d', text):
            continue
        if t['cx'] < min(room_x.values()) - 18 or t['cx'] > max(room_x.values()) + step_x * 0.65:
            continue
        band = next((b for b in bands if b[1] <= t['cy'] < b[2]), None)
        if not band:
            continue
        floor = band[0]
        room_no = min(room_x, key=lambda n: abs(room_x[n] - t['cx']))
        if abs(room_x[room_no] - t['cx']) > max(42, step_x * 0.72):
            continue
        room = '%d%02d' % (floor, room_no)
        key = (room, re.sub(r'\d', '#', text))
        if key in seen:
            continue
        seen.add(key)
        rule = _rule_match(text, rules, ctx.get('dept'))
        item = (rule or {}).get('item') or text[:40]
        mscore = re.search(r'[-−]\s*(\d{1,2}(?:\.\d)?)', text) or re.search(r'扣\s*(\d{1,2}(?:\.\d)?)', text)
        score = float(mscore.group(1)) if mscore else abs(float((rule or {}).get('score') or 0))
        classes = room_map.get(room, [])
        status, issue = 'ok', ''
        if not mscore:
            status, issue = 'error', '未从手写中确认扣分值'
        if not classes:
            status, issue = 'error', '宿舍安排中未找到该房间对应班级'
        if not rule:
            status, issue = 'error', (issue + '；' if issue else '') + '未匹配现有宿舍细则'
        if t['confidence'] < 82:
            status, issue = 'error', '手写识别置信度偏低，请对照原图'
        out.append(_base_candidate(
            len(out) + 1, 'dorm_log', item, 1, score, '间', '宿舍纸表手写分值',
            '识别原文：%s；房间：%s' % (text, room), t['confidence'], status, issue,
            target=('%s宿舍 → %s' % (room, '、'.join(classes))) if classes else ('%s宿舍 → 待映射班级' % room),
            building=building, room=room, classes=classes, dept='宿管部',
            cell='%s × %02d' % (('一二三四五六七八'[floor - 1] + '楼'), room_no),
        ))
    if not building:
        warnings.append('未识别到楼栋名称，请在识别前选择楼栋。')
    if not out:
        warnings.append('未从宿舍问题格识别出可靠文字。宿舍手写识别难度较高，建议对照原图补录，或改用现有宿舍扣分页。')
    else:
        warnings.append('宿舍日志为手写密集表，只提取可定位的文字，所有候选均建议对照原图确认。')
    return out[:60], warnings


def _norm_dorm_cls_list(cls_raw):
    """手写班级规范化：'高三4班'→['高三（4）班']；'高三17-19班'→['高三（17）班','高三（19）班']。"""
    s = _norm_text(cls_raw)
    m = re.search(r'高([一二三])\s*(\d{1,2})\s*[-~—至]\s*(\d{1,2})\s*班', s)
    if m:
        g = {'一': '高一', '二': '高二', '三': '高三'}[m.group(1)]
        a, b = int(m.group(2)), int(m.group(3))
        if 1 <= a <= b <= 40:
            return [g + '（%d）班' % n for n in range(a, b + 1)]
    m = re.search(r'高([一二三])\s*(\d{1,2})\s*班', s)
    if m:
        return [{'一': '高一', '二': '高二', '三': '高三'}[m.group(1)] + '（%d）班' % int(m.group(2))]
    return []


def _parse_dorm_rowtable(tokens, bgr, ctx, rules, dorm_rows):
    """宿舍检查评价表（行式明细）：宿舍号 | 班级 | 座号 | 扣分说明，页边手写分值。
    每行 = 一条候选；班级优先由宿舍安排（房间→班级）映射，手写班级做校验；分值优先取页边手写。"""
    h, w = bgr.shape[:2]
    # 1) 表头行（印刷体，定位列边界）
    heads = {}
    for t in tokens:
        s = _norm_text(t['text'])
        if s in ('宿舍号', '班级', '座号', '扣分说明') and s not in heads:
            heads[s] = t['cx']
    # 2) 行锚点：三位数宿舍号 token 的 x 聚类主簇（不假设列在画面左侧——倾斜/偏置拍摄自适应）
    cands = []
    for t in tokens:
        s = _norm_text(t['text'])
        m = re.match(r'^0?(\d{3})\D{0,2}$', s)
        if not m or not (100 <= int(m.group(1)) <= 999):
            continue
        cands.append((m.group(1), t['cx'], t['cy'], t['confidence']))
    if not cands:
        return [], ['未识别到宿舍号（三位数房号，如 313）。请正对纸表、拍清宿舍号列后重试。']
    xs = sorted(c[1] for c in cands)
    med = xs[len(xs) // 2]
    cands = [c for c in cands if abs(c[1] - med) <= 120]   # 主簇 = 宿舍号列，剔除座号/说明里的偶发三位数
    if len(cands) < 4:
        return [], ['仅识别到 %d 个宿舍号，无法按行解析。请靠近拍摄，保证宿舍号列清晰完整。' % len(cands)]
    anchors, seen_room = [], set()
    for room, cx, cy, conf in sorted(cands, key=lambda c: c[2]):
        if room in seen_room:
            continue
        seen_room.add(room)
        anchors.append({'room': room, 'cy': cy, 'cx': cx, 'conf': conf})
    if not anchors:
        return [], ['未识别到左侧宿舍号列（三位数房号，如 313）。请拍全表格，并确认选择的是"宿舍检查评价表"模板。']
    anchors.sort(key=lambda a: a['cy'])
    bands = []
    for i, a in enumerate(anchors):
        top = (anchors[i - 1]['cy'] + a['cy']) / 2 if i else a['cy'] - 40
        bottom = (a['cy'] + anchors[i + 1]['cy']) / 2 if i + 1 < len(anchors) else a['cy'] + 80
        bands.append([a, top, bottom])
    # 3) 列边界：表头定位优先；兜底用宿舍号列簇的 x 中位数
    x_room_med = sorted(a['cx'] for a in anchors)[len(anchors) // 2]
    if {'宿舍号', '班级', '座号', '扣分说明'} <= set(heads):
        x_cls_l = (heads['宿舍号'] + heads['班级']) / 2
        x_cls_r = heads['座号'] - 8      # 座号列窄，右界直接贴座号列
        x_desc_l = heads['座号'] + 25    # 说明列从座号列右侧开始
    else:
        x_cls_l = x_room_med + w * 0.075
        x_cls_r = x_room_med + w * 0.24
        x_desc_l = x_room_med + w * 0.26
    x_right = w * 0.90   # 表格右边界（页边分值区：手写分值常骑在表格线上）
    header_y = min(t['cy'] for t in tokens if _norm_text(t['text']) in ('宿舍号', '班级', '座号', '扣分说明')) if heads else anchors[0]['cy'] - 40
    # 4) 楼栋：前端选择优先，OCR 表头兜底
    building = (ctx.get('building') or '').strip()
    if not building:
        for t in tokens[:40]:
            m = re.search(r'([\u4e00-\u9fa5]{1,4}(?:栋|楼))', _norm_text(t['text']))
            if m and t['cy'] < anchors[0]['cy'] and '教学' not in m.group(1):
                building = m.group(1)
                break
    # 5) 宿舍安排：房间→班级
    room_map, classes_all = {}, []
    for r in dorm_rows or []:
        if r.get('cls') and r['cls'] not in classes_all:
            classes_all.append(r['cls'])
        if building and r.get('building') and r['building'] != building:
            continue
        room = str(r.get('room') or '')
        if not room:
            continue
        room_map.setdefault(room, [])
        if r.get('cls') and r['cls'] not in room_map[room]:
            room_map[room].append(r['cls'])
    # 6) 页边分值归行（取 cy 最近的行锚点）
    for t in tokens:
        text = _norm_text(t['text'])
        if t['cx'] > x_right and t['cy'] > header_y - 10 and re.search(r'[-−—]\s*\d{1,2}', text):
            band = min(bands, key=lambda b: abs(b[0]['cy'] - t['cy']))
            band[0].setdefault('score_tokens', []).append(t)
    # 7) 先给每个正文 token 分配唯一行锚（cy 最近者，超出容差丢弃），再逐行组装——避免相邻行串行/重复分配
    row_gap = sorted((b[2] - b[1]) for b in bands)[len(bands) // 2] if bands else 60
    max_cy_dist = max(30.0, row_gap * 0.75)
    body = []
    for t in tokens:
        if t['cx'] > x_right:
            continue
        text = _norm_text(t['text'])
        if not text or len(text) > 40 or text in ('宿舍号', '班级', '座号', '扣分说明'):
            continue
        if re.match(r'^0?\d{3}\D{0,2}$', text) and t['cx'] <= x_cls_l + 10:
            continue   # 宿舍号本体
        if t['cx'] < x_cls_l:
            continue
        if not bands:
            break
        b = min(bands, key=lambda bb: abs(bb[0]['cy'] - t['cy']))
        if abs(b[0]['cy'] - t['cy']) > max_cy_dist:
            continue   # 离任何行都远（页边污点/背景误识别）
        body.append((b[0], t))
    out, warnings, seen = [], [], set()
    for a, top, bottom in bands:
        room = a['room']
        if room in seen:
            continue
        seen.add(room)
        row_ts = [t for aa, t in body if aa is a]
        cls_tokens = sorted([t for t in row_ts if t['cx'] < x_cls_r], key=lambda t: t['cx'])
        desc_tokens = sorted([t for t in row_ts if t['cx'] >= x_cls_r], key=lambda t: t['cx'])
        cls_tokens.sort(key=lambda t: t['cx'])
        desc_tokens.sort(key=lambda t: t['cx'])
        cls_raw = ''.join(t['text'] for t in cls_tokens).strip()
        desc = ' '.join(t['text'] for t in desc_tokens).strip()
        if not desc and not cls_raw:
            continue   # 空白行
        score = None
        st = a.get('score_tokens') or []
        if st:
            st.sort(key=lambda t: abs(t['cy'] - a['cy']))
            m = re.findall(r'[-−—]\s*(\d{1,2}(?:\.\d)?)', _norm_text(st[0]['text']))
            if m:
                score = float(m[-1])
        # 页边分值常骑在表格线上：落进说明列时表现为“说明末尾的负号数字”，直接提取并从说明中剥离
        if score is None:
            m2 = re.search(r'[-−—]\s*(\d{1,2}(?:\.\d)?)\s*$', desc)
            if m2:
                score = float(m2.group(1))
                desc = desc[:m2.start()].strip()
        # 页边放大二次 OCR 兜底：整图 OCR 对骑线/页缘小字经常漏读，按行裁切放大专读
        if score is None and bands:
            score = _margin_score_ocr(bgr, max(0, top - 6), min(h, bottom + 6), max(0, x_right - 10), w)
        desc_all = (cls_raw + ' ' + desc).strip()   # 座号可能写在班级列右侧（如 8号），并入匹配原文
        rule = _rule_match(desc_all, rules, ctx.get('dept') or '宿管部')
        item = (rule or {}).get('item') or desc[:40]
        conf = int(a.get('conf') or 0)
        if score is None:
            score = abs(float((rule or {}).get('score') or 0))
        classes = room_map.get(room, [])
        cls_check = _norm_dorm_cls_list(cls_raw)
        if not classes and cls_check:
            classes = cls_check
        status, issue = 'ok', ''
        if not desc:
            status, issue = 'error', '未识别到扣分说明，请对照原图补录'
        if score is None or score <= 0:
            status, issue = 'error', (issue + '；' if issue else '') + '未从页边确认扣分值'
        if not classes:
            status, issue = 'error', (issue + '；' if issue else '') + '宿舍安排中未找到该宿舍对应班级（请核对楼栋/宿舍号）'
        elif cls_check and not (set(cls_check) & set(classes)):
            status, issue = 'error', (issue + '；' if issue else '') + '手写班级(%s)与宿舍安排(%s)不一致，请人工核对' % ('、'.join(cls_check), '、'.join(classes))
        if conf < 75:
            status, issue = 'error', (issue + '；' if issue else '') + '宿舍号识别置信度偏低，请对照原图'
        out.append(_base_candidate(
            len(out) + 1, 'dorm_log', item, 1, score, '间', '宿舍纸表手写分值',
            ('识别原文：%s %s %s' % (room, cls_raw, desc))[:200], conf, status, issue,
            target=('%s %s → %s' % (building or '', room, '、'.join(classes))) if classes else ('%s %s → 待映射班级' % (building or '', room)),
            building=building, room=room, classes=classes, dept='宿管部',
            unit_score=(score or 0), qty_mode='fixed',
            cell=room,
            roi=[int(max(0, x_desc_l - 30)), int(max(0, top - row_gap)), int(w), int(min(h, bottom + row_gap))],
        ))
    if not building:
        warnings.append('未识别到楼栋名称，请在识别前选择楼栋（用于映射宿舍安排）。')
    if not out:
        warnings.append('未从表格识别出数据行。请拍全表格（含宿舍号列与页边分值），光线充足、正对拍摄。')
    else:
        warnings.append('已按行识别 %d 条宿舍检查记录；班级由宿舍安排映射，扣分值取页边手写，请逐行确认。' % len(out))
    return out[:80], warnings


def _margin_score_ocr(bgr, y0, y1, x0, x1):
    """页边手写分值兜底：裁切该行右侧页边区域放大后单独 OCR，只认 [负号]数字 的极窄模式。
    返回 (score or None)。整图 OCR 对骑线小字经常漏读，放大专用识别能救回大半。"""
    try:
        import cv2
    except Exception:
        return None
    h, w = bgr.shape[:2]
    x0, x1 = max(0, int(x0)), min(w, int(x1))
    y0, y1 = max(0, int(y0)), min(h, int(y1))
    if x1 - x0 < 14 or y1 - y0 < 14:
        return None
    crop = bgr[y0:y1, x0:x1]
    scale = max(3.0, min(5.0, 150.0 / max(1, crop.shape[0])))
    crop = cv2.resize(crop, None, fx=scale, fy=scale, interpolation=cv2.INTER_CUBIC)
    try:
        toks, _ = _run_ocr(crop)
    except Exception:
        return None
    best = None
    for t in toks:
        for m in re.finditer(r'[-−—]\s*(\d{1,2}(?:\.\d)?)', _norm_text(t['text'])):
            v = float(m.group(1))
            if v <= 0 or v > 50:
                continue
            c = t['confidence']
            if best is None or c > best[1]:
                best = (v, c)
    if best and best[1] >= 40:
        return best[0]
    return None


def _detect_header_meta(tokens, ctx=None):
    """纸表上可能叠着上一张表；优先选择“同年级标题正上方”的日期。"""
    import datetime
    grade = (ctx or {}).get('grade') or ''
    found = []
    for i, t in enumerate(tokens[:140]):
        s = _norm_text(t['text'])
        m = re.search(r'(20\d{2})\s*[年./-]\s*(\d{1,2})\s*[月./-]\s*(\d{1,2})\s*日?', s)
        if not m:
            continue
        try:
            d = datetime.date(int(m.group(1)), int(m.group(2)), int(m.group(3))).isoformat()
        except Exception:
            continue
        score = 0.0
        # 日期一般位于本表标题右下方 0~150px
        for q in tokens[max(0, i - 35):i + 2]:
            if q is t:
                continue
            qt = _norm_text(q['text'])
            dy = t['cy'] - q['cy']
            if not (-15 <= dy <= 150):
                continue
            if grade and grade in qt and ('量化' in qt or '年级' in qt or '检查' in qt):
                score = max(score, 20.0 - abs(dy - 35) / 10.0)
            elif ('日常量化' in qt or '检查汇总' in qt or '检查评价' in qt):
                score = max(score, 8.0 - abs(dy - 35) / 20.0)
        # 日期下方很快出现表头，说明它属于当前大表
        for q in tokens[i + 1:i + 28]:
            if _norm_text(q['text']) in ('委员签到', '早读', '午读', '晚读', '班级', '房号'):
                if 0 < q['cy'] - t['cy'] < 130:
                    score += 2.0
                break
        found.append((score, t['cy'], d))
    best = max(found) if found else None
    date_s = best[2] if best and best[0] >= 1.0 else ''
    date_score = round(float(best[0]), 1) if best else 0.0
    text = ' '.join(_norm_text(t['text']) for t in tokens[:140])
    session = ''
    if '下午放学' in text or '课后' in text:
        session = '下午放学'
    else:
        counts = {p: text.count(p) for p in PERIODS}
        if counts and max(counts.values()) > 0:
            session = max(counts, key=counts.get)
    return date_s, session, date_score

def analyze_paper(data: bytes, template_id: str, ctx: dict, rules=None, classes=None, dorm_rows=None):
    if template_id not in TEMPLATES:
        raise PaperError('未知纸表模板')
    ensure_engine_path_if_needed()   # cv2 等依赖在 image_quality 里就会用到，必须先于一切 import
    bgr, pil = image_from_bytes(data)
    quality = image_quality(bgr)
    tokens, elapsed = _run_ocr(bgr)
    boxes = _detect_checkmarks(bgr) if template_id == 'after_class' else []
    detected_date, detected_session, date_score = _detect_header_meta(tokens, ctx)
    warnings = list(quality['issues'])
    if len(tokens) < 4:
        warnings.append('OCR 只识别到极少文字，请检查照片方向、清晰度与完整度。')
    if template_id == 'after_class':
        candidates, more = _parse_after(tokens, boxes, bgr, ctx, classes or {})
    elif template_id == 'daily_matrix':
        candidates, more = _parse_daily(tokens, bgr, ctx, classes or {}, rules or [], refine=True)
    else:
        candidates, more = _parse_dorm_rowtable(tokens, bgr, ctx, rules or [], dorm_rows or [])
    warnings.extend(more)
    if detected_date:
        warnings.append('纸表日期识别为 %s（把握 %.0f%%）；提交时默认仍用操作人选择的日期，需二选一确认。' % (detected_date, min(99, max(0, date_score * 5))))
    if detected_date and ctx.get('date') and detected_date != ctx.get('date'):
        warnings.append('当前选择日期 %s 与纸表日期 %s 不一致，提交前必须在两者中选择。' % (ctx.get('date'), detected_date))
    # 统一给候选编号，避免解析器内部编号重复
    for i, c in enumerate(candidates, 1):
        c['id'] = 'p%d' % i
        c.setdefault('grade', _grade_from_cls(c.get('cls', '')))
    return {
        'template': template_id,
        'template_name': TEMPLATES[template_id]['name'],
        'width': quality['width'], 'height': quality['height'],
        'blur': quality['blur'], 'brightness': quality['brightness'],
        'quality_issues': quality['issues'], 'warnings': warnings[:14],
        'detected_date': detected_date, 'detected_session': detected_session,
        'date_score': date_score,
        'candidates': candidates,
        'raw_texts': [_norm_text(t['text']) for t in tokens if _norm_text(t['text'])][:200],
        'ocr_count': len(tokens),
        'ocr_seconds': round(elapsed, 2),
        'checkboxes_seen': len(boxes),
    }
