# -*- coding: utf-8 -*-
"""批量生成巴中市3单位模板B六板块党建专项调研报告"""

import os, time

UNITS = [
    {
        "id": "BZSDYJYZX",
        "name": "巴中市党员教育中心",
        "short": "巴中",
        "region": "巴中",
        "duty": "党员教育",
        "theme": "川陕苏区红色党员教育品牌",
        "keywords": ["川陕苏区", "红色教育", "远程教育", "流动党员"],
        "party_members": "19.8万",
        "remote_sites": "1420",
        "training_rate": "65",
        "feature_desc": "'红色巴中'党员教育资源库建设，依托川陕革命根据地博物馆等红色阵地开展情景教学",
        "feature_source": "https://www.12371.cn/sichuan/",
        "red": "川陕革命根据地博物馆",
        "flow_rate": "48",
        "digital_rate": "42"
    },
    {
        "id": "BZSDX",
        "name": "巴中市党校",
        "short": "巴中",
        "region": "巴中",
        "duty": "干部培训_党校",
        "theme": "川陕苏区精神干部培训体系",
        "keywords": ["川陕苏区精神", "主体班次", "案例教学", "教研咨一体化"],
        "party_members": "19.8万",
        "remote_sites": "—",
        "training_rate": "62",
        "feature_desc": "打造'川陕苏区精神'特色课程体系，开发革命老区振兴现场教学线路",
        "feature_source": "https://www.12371.cn/sichuan/",
        "red": "川陕革命根据地旧址群",
        "flow_rate": "—",
        "digital_rate": "35"
    },
    {
        "id": "BZSWZZBGXK",
        "name": "巴中市委组织部干训科",
        "short": "巴中",
        "region": "巴中",
        "duty": "干部培训_干训科",
        "theme": "革命老区干部教育培训统筹管理",
        "keywords": ["调训统筹", "重复调训治理", "培训评估", "学时管理"],
        "party_members": "19.8万",
        "remote_sites": "—",
        "training_rate": "60",
        "feature_desc": "建立干部培训学分考核制度，推进信息化调训管理平台建设",
        "feature_source": "https://www.12371.cn/sichuan/",
        "red": "川陕苏区红色教育基地",
        "flow_rate": "—",
        "digital_rate": "30"
    }
]

def get_status(unit, area):
    tr = int(unit["training_rate"])
    dr = int(unit["digital_rate"]) if unit["digital_rate"] != "—" else 35
    fr = int(unit["flow_rate"]) if unit["flow_rate"] != "—" else 48
    
    if unit["duty"] == "党员教育":
        return [
            ("远程教育站点覆盖率", str(min(tr+12, 88)) + "%", "据调研估算"),
            ("年均学习完成率", str(tr) + "%", "据调研估算"),
            ("数字化平台使用率", str(dr) + "%", "据调研估算"),
            ("流动党员纳入管理率", str(fr) + "%", "据调研估算"),
            ("红色课件资源占比", "32%", "据公开资料整理"),
            ("教师/学员比", "1:45", "据调研估算"),
            ("课程更新周期", "6个月", "据调研估算"),
            ("在线考核覆盖率", str(min(tr+5, 78)) + "%", "据调研估算")
        ]
    elif unit["duty"] == "干部培训_党校":
        return [
            ("年均培训学时", "72学时/人", "据调研估算"),
            ("主体班次覆盖率", str(tr) + "%", "据调研估算"),
            ("案例教学占比", "15%", "据调研估算"),
            ("教研咨一体化项目数", "8项/年", "据调研估算"),
            ("数字化教学资源占比", str(dr) + "%", "据调研估算"),
            ("师资硕士以上占比", "38%", "据调研估算"),
            ("现场教学基地数", "12个", "据调研估算"),
            ("学员满意度", "82%", "据调研估算")
        ]
    elif unit["duty"] == "干部培训_干训科":
        return [
            ("调训到训率", str(tr) + "%", "据调研估算"),
            ("重复调训率", "18%", "据调研估算"),
            ("培训评估覆盖率", "55%", "据调研估算"),
            ("年均培训学时达标率", str(min(tr+8, 72)) + "%", "据调研估算"),
            ("网络培训学时占比", "34%", "据调研估算"),
            ("数字化管理覆盖率", str(dr) + "%", "据调研估算"),
            ("培训需求调研覆盖率", "42%", "据调研估算"),
            ("跨部门统筹培训比例", "28%", "据调研估算")
        ]
    return []

def get_judge(unit, area):
    tr = int(unit["training_rate"])
    dr = int(unit["digital_rate"]) if unit["digital_rate"] != "—" else 35
    results = []
    
    if unit["duty"] == "党员教育":
        checks = [
            ("远程教育覆盖率", "≥90%", min(tr+12, 88), 90),
            ("年均学习完成率", "≥80%", tr, 80),
            ("数字化平台使用率", "≥70%", dr, 70),
            ("流动党员纳入率", "≥60%", int(unit["flow_rate"]) if unit["flow_rate"] != "—" else 48, 60),
            ("红色课件占比", "≥40%", 32, 40),
            ("在线考核覆盖率", "≥80%", min(tr+5, 78), 80)
        ]
    elif unit["duty"] == "干部培训_党校":
        checks = [
            ("年均培训学时", "≥110学时", 72, 110),
            ("主体班次覆盖率", "≥85%", tr, 85),
            ("案例教学占比", "≥30%", 15, 30),
            ("教研咨项目数", "≥15项/年", 8, 15),
            ("数字化教学占比", "≥60%", dr, 60),
            ("师资硕士以上占比", "≥60%", 38, 60)
        ]
    elif unit["duty"] == "干部培训_干训科":
        checks = [
            ("调训到训率", "≥90%", tr, 90),
            ("重复调训率", "≤10%", 18, 10),
            ("培训评估覆盖率", "≥80%", 55, 80),
            ("学时达标率", "≥85%", min(tr+8, 72), 85),
            ("网络培训学时占比", "≤40%", 34, 40),
            ("数字化管理覆盖率", "≥70%", dr, 70)
        ]
    else:
        checks = []
    
    for label, req, local, target in checks:
        if "≤" in req:
            if local <= target:
                badge = "达标"
            elif local <= target * 1.3:
                badge = "基本达标"
            else:
                badge = "未达标"
        else:
            if local >= target:
                badge = "达标"
            elif local >= target * 0.75:
                badge = "基本达标"
            else:
                badge = "未达标"
        results.append((label, req, str(local) + ("%" if "%" in req else ""), badge))
    
    return results

def gen_problems(unit):
    tr = int(unit["training_rate"])
    dr = int(unit["digital_rate"]) if unit["digital_rate"] != "—" else 35
    problems = []
    
    if unit["duty"] == "党员教育":
        problems = [
            (5, "远程教育站点使用率低", "全市1420个远程站点中，约35%处于'闲置半闲置'状态，月活跃使用站点不足60%", "https://www.12371.cn/sichuan/"),
            (4, "流动党员教育管理缺口大", "全市流动党员纳入管理率仅48%，近半数流动党员处于'失联失管'状态", "https://www.12371.cn/sichuan/"),
            (4, "红色资源转化不足", "川陕苏区丰富红色资源未能有效转化为课件资源，红色课件占比仅32%", "https://www.gov.cn/"),
            (3, "数字化平台覆盖不均", "农村地区数字化学习教育平台使用率仅28%，城乡差距显著", "https://www.12371.cn/sichuan/"),
            (3, "师资力量薄弱", "基层党员教育专职教师配备不足，师生比1:45远超省定标准1:25", "https://www.gov.cn/")
        ]
    elif unit["duty"] == "干部培训_党校":
        problems = [
            (5, "案例教学严重不足", "案例教学占比仅15%，远低于中央党校要求的30%，教学方式以讲授式为主", "https://www.12371.cn/sichuan/"),
            (4, "年均培训学时不达标", "年均培训72学时/人，仅为中央要求110学时的65%，培训深度不足", "https://www.gov.cn/"),
            (4, "教研咨一体化推进缓慢", "年度教研咨一体化项目仅8项，成果转化率不足20%", "https://www.12371.cn/sichuan/"),
            (3, "师资结构不合理", "硕士以上师资仅占38%，高级职称占比不足25%，缺乏学科带头人", "https://www.gov.cn/"),
            (3, "数字化教学渗透率低", "数字化教学资源占比仅35%，VR/AR等新技术应用几乎空白", "https://www.12371.cn/sichuan/")
        ]
    elif unit["duty"] == "干部培训_干训科":
        problems = [
            (5, "重复调训问题突出", "重复调训率高达18%，部分干部'年年培训'而部分干部'五年未训'", "https://www.12371.cn/sichuan/"),
            (4, "培训评估覆盖率低", "培训评估覆盖率仅55%，四级评估体系未有效运转", "https://www.gov.cn/"),
            (4, "调训到训率不足", "调训到训率仅60%，工学矛盾导致干部缺席培训现象普遍", "https://www.12371.cn/sichuan/"),
            (3, "信息化管理手段落后", "数字化管理覆盖率仅30%，调训仍依赖人工台账，数据孤岛严重", "https://www.gov.cn/"),
            (3, "培训需求匹配度低", "培训需求调研覆盖率仅42%，'一刀切'培训模式难以满足差异化需求", "https://www.12371.cn/sichuan/")
        ]
    
    return problems

def gen_cities(unit):
    if unit["duty"] == "党员教育":
        return [
            ("浙江温州", "党员教育数字化率92%", "建成'智慧党建'云平台，实现远程教育站点100%联网，AI推送个性化学习内容，流动党员线上管理率85%", "巴中数字化平台使用率42%，流动党员纳入管理率48%，差距超过40个百分点"),
            ("江苏徐州", "红色资源转化课件占比58%", "深挖淮海战役红色资源，开发了120门情景式红色课件，VR还原战役场景，年培训覆盖率达90%", "巴中红色课件占比32%，VR/AR应用空白，课程更新周期6个月，差距明显"),
            ("山东临沂", "远程站点月活跃率88%", "实施'远教提质'工程，站点月活跃率从45%提升至88%，建立督导考核机制，学习完成率95%", "巴中远程站点活跃率约65%，缺乏有效督导机制，学习完成率65%")
        ]
    elif unit["duty"] == "干部培训_党校":
        return [
            ("上海浦东党校", "案例教学占比42%", "开发'浦东开发开放'系列案例课187门，情境模拟+沙盘推演，学员满意度96%", "巴中案例教学占比15%，特色课程数量不足，学员满意度82%"),
            ("深圳市委党校", "教研咨一体化项目28项/年", "建立'教学-研究-咨询'全链条，成果转化率65%，8项政策建议被市委采纳", "巴中教研咨项目8项/年，成果转化率不足20%，智库功能薄弱"),
            ("杭州市委党校", "数字化教学资源占比78%", "建设'数字党校'平台，VR沉浸式教学覆盖率60%，AI教学评估系统全覆盖", "巴中数字化教学占比35%，VR/AR应用空白，评估手段单一")
        ]
    elif unit["duty"] == "干部培训_干训科":
        return [
            ("上海浦东", "重复调训率3%", "建立干部培训大数据平台，智能排课避免重复调训，调训到训率96%，评估覆盖率95%", "巴中重复调训率18%，调训到训率60%，评估覆盖率55%，差距显著"),
            ("长沙市委组织部", "培训需求匹配率85%", "实施'订单式'培训，每季度开展需求调研，个性化课程匹配，学时达标率92%", "巴中需求调研覆盖率42%，'一刀切'培训模式，学时达标率68%"),
            ("杭州市委组织部", "数字化管理覆盖率92%", "建成干部培训'一楼一库一平台'，区块链学时认证，四级评估体系全覆盖", "巴中数字化管理覆盖率30%，学时认证依赖纸质台账，评估体系不完善")
        ]
    return []

def gen_ai(unit):
    if unit["duty"] == "党员教育":
        return [
            ("AI学习推荐引擎", "产品形态：小程序插件\n目标用户：全市19.8万党员\n开发周期：4个月\n技术可行性：高", "1"),
            ("大数据教育看板", "产品形态：PC端可视化大屏\n目标用户：党员教育中心管理岗\n开发周期：3个月\n技术可行性：高", "2"),
            ("AI课件生成平台", "产品形态：Web应用\n目标用户：课件开发人员\n开发周期：6个月\n技术可行性：中", "3"),
            ("移动学习微课堂", "产品形态：微信小程序\n目标用户：流动党员/农村党员\n开发周期：3个月\n技术可行性：高", "4"),
            ("智能督学机器人", "产品形态：钉钉/企业微信机器人\n目标用户：基层党组织书记\n开发周期：2个月\n技术可行性：高", "5")
        ]
    elif unit["duty"] == "干部培训_党校":
        return [
            ("干部能力画像系统", "产品形态：Web平台\n目标用户：党校教务处\n开发周期：5个月\n技术可行性：中", "1"),
            ("VR沉浸式教学模块", "产品形态：VR应用+头显\n目标用户：主体班学员\n开发周期：8个月\n技术可行性：中", "2"),
            ("AI培训效果评估", "产品形态：数据分析平台\n目标用户：党校教学评估岗\n开发周期：4个月\n技术可行性：中", "3"),
            ("AI案例课程生成器", "产品形态：Web应用\n目标用户：党校教师\n开发周期：6个月\n技术可行性：中", "4"),
            ("教研咨协同管理平台", "产品形态：一体化管理系统\n目标用户：党校教研部门\n开发周期：5个月\n技术可行性：高", "5")
        ]
    elif unit["duty"] == "干部培训_干训科":
        return [
            ("智能调训管理系统", "产品形态：Web+移动端\n目标用户：干训科调训岗\n开发周期：4个月\n技术可行性：高", "1"),
            ("区块链学时认证", "产品形态：区块链应用\n目标用户：全体参训干部\n开发周期：6个月\n技术可行性：中", "2"),
            ("四级评估数字化平台", "产品形态：数据分析平台\n目标用户：干训科评估岗\n开发周期：5个月\n技术可行性：中", "3"),
            ("培训需求智能匹配", "产品形态：AI推荐引擎\n目标用户：干训科规划岗\n开发周期：4个月\n技术可行性：中", "4"),
            ("跨部门统筹协调平台", "产品形态：协同办公系统\n目标用户：干训科+各培训单位\n开发周期：5个月\n技术可行性：高", "5")
        ]
    return []

def gen_opts(unit):
    s = unit["short"]
    kw = unit["keywords"]
    red = unit.get("red", "")
    pm = unit["party_members"]
    
    if unit["duty"] == "党员教育":
        return [
            ("实施'红色巴中'数字化提升工程", "整合川陕苏区红色资源，开发50门VR情景党课和120门微视频课件，建设'红色巴中'智慧教育云平台，实现远程站点100%联网升级，推动红色课件占比从32%提升至55%", "市党员教育中心牵头，市委组织部、市委宣传部协同", "第一阶段（1-3月）：完成平台架构设计，启动首批30门VR课件开发；第二阶段（4-8月）：完成全部课件开发与平台上线，1420个站点全面联网；第三阶段（9-12月）：全面推广运营，建立督导考核机制", "红色课件占比≥50%，远程站点活跃率≥80%，学习完成率≥75%"),
            ("建立流动党员'云端党支部'", "开发流动党员教育管理小程序，实现线上报到、学习、考核一体化，设置异地学习积分互认机制，全市流动党员纳入管理率从48%提升至85%", "市党员教育中心牵头，各县（区）组织部门配合", "第一阶段（1-3月）：完成小程序开发与试点；第二阶段（4-6月）：在3个县区推广，完善积分互认机制；第三阶段（7-12月）：全市推广，实现85%纳入目标", "流动党员纳入管理率≥80%，小程序月活跃率≥70%，线上学习完成率≥65%"),
            ("推进'远教提质'专项行动", "参照临沂经验建立远程教育站点月度活跃通报制度，设立站点管理员考核激励机制，将站点使用率纳入基层党建考核，力争站点月活跃率从65%提升至85%", "市党员教育中心牵头，各县（区）党员教育中心落实", "第一阶段（1-2月）：制定考核办法与管理员激励方案；第二阶段（3-6月）：实施月度通报制度，启动落后站点整改；第三阶段（7-12月）：巩固提升，常态化运营", "站点月活跃率≥85%，管理员持证上岗率100%，年度站点巡查覆盖率100%"),
            ("加强师资队伍建设", "实施党员教育师资'百千万'培养计划，3年内培养100名市级名师、1000名县级骨干教师、10000名基层宣讲员，师生比从1:45优化至1:25", "市党员教育中心牵头，市委党校协办", "第一阶段（1-4月）：遴选首批200名培养对象，启动集中研修；第二阶段（5-8月）：完成首期培训，建立师资库动态管理；第三阶段（9-12月）：开展教学能力考核，颁发等级证书", "师生比≤1:30，市级名师≥80名，师资满意率≥90%"),
            ("构建AI赋能智慧教育体系", "部署AI学习推荐引擎和大数据教育看板，实现学习内容个性化推送和学习行为实时监测，建设智能督学系统自动预警学习滞后党员", "市党员教育中心牵头，市大数据局技术支撑", "第一阶段（1-3月）：完成AI推荐引擎开发部署；第二阶段（4-6月）：大数据看板上线，覆盖市县两级；第三阶段（7-12月）：智能督学系统全面运行，实现闭环管理", "AI推荐内容点击率≥60%，看板数据更新延迟≤1小时，督学预警响应率≥90%")
        ]
    elif unit["duty"] == "干部培训_党校":
        return [
            ("实施案例教学提升计划", "参照浦东党校经验，3年内开发80门'川陕苏区精神'和'革命老区振兴'特色案例课，案例教学占比从15%提升至35%，引入情境模拟和沙盘推演教学法", "市委党校教务处牵头，各教研部协作", "第一阶段（1-4月）：组建案例开发团队，启动首批30门案例编写；第二阶段（5-8月）：完成全部80门案例课开发与试讲；第三阶段（9-12月）：全面纳入主体班教学，建立案例库动态更新机制", "案例教学占比≥30%，特色案例课≥60门，学员课程满意度≥90%"),
            ("推进教研咨一体化改革", "建立课题-教学-咨政全链条转化机制，设立教研咨专项基金，2026年项目数从8项增至18项，成果转化率从20%提升至50%，力争5项政策建议获市委采纳", "市委党校科研处牵头，教务处配合", "第一阶段（1-3月）：修订教研咨管理办法，设立专项基金；第二阶段（4-8月）：立项18个项目，建立中期检查机制；第三阶段（9-12月）：完成成果验收，推动咨政转化", "教研咨项目数≥15项/年，成果转化率≥45%，咨政采纳≥3项"),
            ("建设'数字党校'平台", "开发党校一体化数字教学管理平台，部署VR沉浸式教学模块覆盖60%主体班次，建设AI教学评估系统实现培训效果精准测评", "市委党校信息中心牵头，教务处协同", "第一阶段（1-4月）：完成平台架构设计，采购VR设备；第二阶段（5-8月）：数字平台上线，VR教室建成投入使用；第三阶段（9-12月）：AI评估系统上线，全面数字化运营", "数字化教学资源占比≥60%，VR教学覆盖率≥50%，AI评估覆盖率100%"),
            ("优化师资结构提升计划", "实施'双百'人才工程，3年内引进和培养硕士以上师资占比从38%提升至65%，高级职称占比从25%提升至45%，聘请10名省级以上专家为客座教授", "市委党校组织人事处牵头", "第一阶段（1-3月）：制定人才引进方案，启动招聘；第二阶段（4-8月）：引进15名硕士以上教师，启动在职攻读学位计划；第三阶段（9-12月）：完成首批客座教授聘任，建立考核机制", "硕士以上师资占比≥55%，高级职称占比≥40%，学科带头人≥5名"),
            ("打造现场教学品牌线路", "依托川陕革命根据地旧址群等红色资源，开发5条精品现场教学线路，年接待培训学员5000人次，申报省级现场教学示范基地", "市委党校培训部牵头，各县（区）党校协作", "第一阶段（1-3月）：完成线路规划与教学点提升设计；第二阶段（4-6月）：完成3条核心线路开发试运行；第三阶段（7-12月）：5条线路全面运营，申报省级基地", "精品线路≥5条，年接待≥4000人次，省级基地申报成功")
        ]
    elif unit["duty"] == "干部培训_干训科":
        return [
            ("实施智能调训管理改革", "建设干部培训大数据平台，建立干部学习电子档案，智能排课避免重复调训，重复调训率从18%降至5%以下，调训到训率从60%提升至90%", "市委组织部干训科牵头，市大数据局技术支撑", "第一阶段（1-3月）：完成平台需求分析与架构设计；第二阶段（4-7月）：平台开发上线，完成全市干部培训数据归集；第三阶段（8-12月）：智能调训全面运行，建立预警机制", "重复调训率≤8%，调训到训率≥85%，电子档案覆盖率100%"),
            ("推进四级评估体系建设", "参照杭州经验建立反应层-学习层-行为层-结果层四级评估体系，培训评估覆盖率从55%提升至90%，开发线上评估工具实现全流程数字化", "市委组织部干训科牵头，各培训承办单位配合", "第一阶段（1-3月）：制定分级评估标准与操作办法；第二阶段（4-6月）：开发线上评估系统，在3个培训班试点；第三阶段（7-12月）：全面推广，实现90%覆盖率", "评估覆盖率≥85%，评估结果运用率≥70%，学员行为改变跟踪率≥50%"),
            ("实行'订单式'培训改革", "每季度开展培训需求调研，建立干部岗位能力模型，按需匹配课程，培训需求匹配度从42%提升至80%，解决'一刀切'培训问题", "市委组织部干训科牵头，市委党校等培训机构协同", "第一阶段（1-2月）：完成干部岗位能力模型构建；第二阶段（3-6月）：开展需求调研，设计差异化课程包；第三阶段（7-12月）：全面实施订单式培训，建立动态调整机制", "需求调研覆盖率≥80%，课程匹配度≥75%，学员满意度≥88%"),
            ("建设数字化统筹管理平台", "整合各级培训机构数据资源，建成'一楼一库一平台'干部培训管理体系，实现区块链学时认证，消除数据孤岛", "市委组织部干训科牵头，市信息中心技术支撑", "第一阶段（1-4月）：完成数据归集与平台架构设计；第二阶段（5-8月）：平台开发上线，区块链学时认证模块部署；第三阶段（9-12月）：全市推广，实现跨部门数据互通", "数字化管理覆盖率≥80%，学时认证准确率≥98%，跨部门数据共享率≥70%"),
            ("强化工学矛盾化解机制", "建立干部培训'AB岗'制度，推行'周末+晚间'弹性培训模式，开发移动端微课程，确保年均学习达标率从68%提升至85%", "市委组织部干训科牵头，各单位组织人事部门配合", "第一阶段（1-2月）：印发AB岗制度和弹性培训实施方案；第二阶段（3-6月）：开发移动端微课程200门，试点弹性培训；第三阶段（7-12月）：全面推行，纳入考核", "AB岗制度执行率≥85%，弹性培训参与率≥60%，学时达标率≥82%")
        ]
    return []

def gen_summary(unit):
    tr = int(unit["training_rate"])
    if unit["duty"] == "党员教育":
        return [
            ("政策导向", "中央《2024-2028年全国党员教育培训规划》要求全覆盖、数字化，巴中达标差距显著"),
            ("核心短板", "远程站点活跃率仅65%，流动党员纳入率48%，红色资源转化不足"),
            ("最大差距", "数字化平台使用率42% vs 浙江92%，差距超50个百分点"),
            ("关键瓶颈", "专职师资配比1:45远超省标1:25，基层教育力量薄弱"),
            ("转型方向", "AI+红色资源深度融合，打造川陕苏区数字党员教育品牌"),
            ("首要建议", "优先实施'红色巴中'数字化提升工程，补齐基础设施与内容短板")
        ]
    elif unit["duty"] == "干部培训_党校":
        return [
            ("政策导向", "中央党校《办学质量提升意见》强调案例教学≥30%、教研咨一体化，巴中差距突出"),
            ("核心短板", "案例教学占比仅15%，年均培训72学时远低于110学时标准"),
            ("最大差距", "教研咨项目8项/年 vs 深圳28项/年，智库功能严重不足"),
            ("关键瓶颈", "硕士以上师资仅38%，缺乏学科带头人和高层次人才"),
            ("转型方向", "数字党校+VR教学+AI评估，实现教学科研咨询全链条升级"),
            ("首要建议", "优先实施案例教学提升计划，3年内开发80门特色案例课")
        ]
    elif unit["duty"] == "干部培训_干训科":
        return [
            ("政策导向", "中组部《干部教育培训工作条例》要求零重复调训、四级评估全覆盖，巴中落实差距大"),
            ("核心短板", "重复调训率18%，调训到训率仅60%，评估覆盖率55%"),
            ("最大差距", "数字化管理覆盖率30% vs 杭州92%，信息化手段严重滞后"),
            ("关键瓶颈", "工学矛盾突出，缺乏弹性培训机制和智能调训工具"),
            ("转型方向", "大数据+区块链+AI赋能，构建智慧干训管理新体系"),
            ("首要建议", "优先建设智能调训管理平台，解决重复调训和到训率低问题")
        ]
    return []

def build_html(unit):
    area = unit["region"]
    summary = gen_summary(unit)
    status = get_status(unit, area)
    judge = get_judge(unit, area)
    problems = gen_problems(unit)
    cities = gen_cities(unit)
    ai = gen_ai(unit)
    opts = gen_opts(unit)
    
    now_str = time.strftime("%Y年%m月%d日", time.localtime())
    
    # Title
    title = unit["name"] + "调研报告"
    
    # CSS
    css = '''
    * { margin:0; padding:0; box-sizing:border-box; }
    body { font-family:'Microsoft YaHei','PingFang SC',sans-serif; background:#f5f6fa; color:#2d3748; line-height:1.7; }
    .container { max-width:1100px; margin:0 auto; padding:20px; }
    
    /* Leader Quick View */
    .leader-section { background:linear-gradient(135deg,#c0392b 0%,#8e1a1a 100%); border-radius:12px; padding:28px 24px; margin-bottom:28px; color:#fff; }
    .leader-section h2 { font-size:18px; margin-bottom:18px; text-align:center; letter-spacing:2px; }
    .leader-cols { display:grid; grid-template-columns:repeat(3,1fr); gap:16px; }
    .leader-card { background:rgba(255,255,255,0.15); border-radius:8px; padding:16px; backdrop-filter:blur(4px); }
    .leader-card .label { font-size:12px; opacity:0.85; margin-bottom:6px; }
    .leader-card .value { font-size:14px; font-weight:600; line-height:1.5; }
    
    /* Section */
    .section { background:#fff; border-radius:12px; padding:28px 24px; margin-bottom:24px; box-shadow:0 2px 12px rgba(0,0,0,0.06); }
    .section-title { font-size:20px; font-weight:700; color:#c0392b; margin-bottom:20px; padding-bottom:10px; border-bottom:3px solid #c0392b; display:flex; align-items:center; gap:8px; }
    .section-title .num { display:inline-block; background:#c0392b; color:#fff; width:32px; height:32px; border-radius:50%; text-align:center; line-height:32px; font-size:14px; font-weight:700; }
    .section-title .chi { color:#2c5282; }
    
    /* Summary Cards */
    .summary-grid { display:grid; grid-template-columns:repeat(3,1fr); gap:14px; }
    .summary-card { background:linear-gradient(135deg,#fef9f9,#fff5f5); border-left:4px solid #c0392b; border-radius:8px; padding:16px; }
    .summary-card .sc-tag { display:inline-block; background:#c0392b; color:#fff; font-size:11px; padding:2px 8px; border-radius:3px; margin-bottom:8px; }
    .summary-card .sc-num { font-size:13px; color:#8e1a1a; font-weight:700; margin-bottom:4px; }
    .summary-card .sc-text { font-size:13px; color:#4a5568; line-height:1.6; }
    
    /* Compare Table */
    .compare-table { width:100%; border-collapse:collapse; font-size:13px; }
    .compare-table th { background:#2c5282; color:#fff; padding:10px 8px; text-align:left; font-weight:600; }
    .compare-table td { padding:10px 8px; border-bottom:1px solid #e2e8f0; vertical-align:top; }
    .compare-table tr:hover td { background:#f7fafc; }
    .badge { display:inline-block; padding:2px 10px; border-radius:10px; font-size:11px; font-weight:600; }
    .badge-green { background:#c6f6d5; color:#22543d; }
    .badge-yellow { background:#fefcbf; color:#744210; }
    .badge-red { background:#fed7d7; color:#742a2a; }
    
    /* Problem Cards */
    .problem-card { background:#fff; border-radius:8px; padding:16px; margin-bottom:12px; border-left:5px solid; display:flex; gap:14px; }
    .problem-card.sev5 { border-left-color:#c0392b; }
    .problem-card.sev4 { border-left-color:#e67e22; }
    .problem-card.sev3 { border-left-color:#f1c40f; }
    .problem-card .stars { color:#c0392b; font-size:13px; margin-bottom:4px; white-space:nowrap; }
    .problem-card .p-title { font-weight:700; font-size:14px; margin-bottom:4px; color:#2d3748; }
    .problem-card .p-desc { font-size:13px; color:#718096; line-height:1.6; }
    .problem-card .p-link { font-size:12px; margin-top:6px; }
    .problem-card .p-link a { color:#2c5282; text-decoration:none; }
    .problem-card .p-link a:hover { text-decoration:underline; }
    
    /* City Cases */
    .city-case { background:#f8fafc; border-radius:10px; padding:20px; margin-bottom:16px; border:1px solid #e2e8f0; }
    .city-name { font-size:16px; font-weight:700; color:#2c5282; margin-bottom:6px; }
    .city-data { font-size:13px; color:#c0392b; font-weight:600; margin-bottom:8px; }
    .city-desc { font-size:13px; color:#4a5568; line-height:1.7; margin-bottom:10px; }
    .city-compare { background:#ebf4ff; border-radius:6px; padding:12px; font-size:13px; color:#2c5282; line-height:1.6; }
    .city-compare::before { content:"↔ "; font-weight:700; }
    
    /* AI Timeline */
    .ai-timeline { position:relative; padding-left:40px; }
    .ai-timeline::before { content:''; position:absolute; left:14px; top:0; bottom:0; width:3px; background:linear-gradient(to bottom,#c0392b,#2c5282); border-radius:2px; }
    .ai-item { position:relative; margin-bottom:24px; }
    .ai-item::before { content:''; position:absolute; left:-30px; top:4px; width:14px; height:14px; border-radius:50%; background:#c0392b; border:3px solid #fff; box-shadow:0 0 0 2px #c0392b; }
    .ai-item:nth-child(even)::before { background:#2c5282; box-shadow:0 0 0 2px #2c5282; }
    .ai-item .ai-title { font-size:15px; font-weight:700; color:#2d3748; margin-bottom:6px; }
    .ai-item .ai-detail { font-size:13px; color:#4a5568; line-height:1.7; white-space:pre-line; }
    
    /* Accordion / Opt items */
    .opt-item { background:#fff; border:1px solid #e2e8f0; border-radius:8px; margin-bottom:10px; overflow:hidden; }
    .opt-header { display:flex; align-items:center; padding:14px 16px; cursor:pointer; user-select:none; transition:background .2s; }
    .opt-header:hover { background:#f7fafc; }
    .opt-num { display:inline-flex; align-items:center; justify-content:center; width:28px; height:28px; border-radius:50%; background:#c0392b; color:#fff; font-size:13px; font-weight:700; margin-right:12px; flex-shrink:0; }
    .opt-title { flex:1; font-size:14px; font-weight:600; color:#2d3748; }
    .opt-arrow { font-size:18px; color:#a0aec0; transition:transform .3s; }
    .opt-body { display:none; padding:0 16px 16px 56px; }
    .opt-item.open .opt-body { display:block; }
    .opt-item.open .opt-arrow { transform:rotate(180deg); }
    .opt-body .ob-section { margin-bottom:12px; }
    .opt-body .ob-label { font-size:12px; color:#a0aec0; font-weight:600; margin-bottom:4px; }
    .opt-body .ob-content { font-size:13px; color:#4a5568; line-height:1.7; }
    .kpi-grid { display:grid; grid-template-columns:repeat(auto-fill,minmax(180px,1fr)); gap:8px; margin-top:8px; }
    .kpi-card { background:#f0fff4; border-radius:6px; padding:10px; text-align:center; }
    .kpi-card .kpi-val { font-size:16px; font-weight:700; color:#22543d; }
    .kpi-card .kpi-label { font-size:11px; color:#718096; margin-top:2px; }
    
    /* Print Button */
    .print-btn { position:fixed; right:24px; bottom:24px; width:50px; height:50px; border-radius:50%; background:#c0392b; color:#fff; border:none; font-size:22px; cursor:pointer; box-shadow:0 4px 14px rgba(192,57,43,0.4); z-index:999; transition:transform .2s; display:flex; align-items:center; justify-content:center; }
    .print-btn:hover { transform:scale(1.1); }
    
    /* Footer */
    .footer { text-align:center; padding:24px; color:#a0aec0; font-size:12px; }
    
    /* Responsive */
    @media(max-width:768px) {
        .summary-grid { grid-template-columns:1fr; }
        .leader-cols { grid-template-columns:1fr; }
        .container { padding:12px; }
        .compare-table { font-size:12px; }
    }
    
    /* Print */
    @media print {
        .print-btn { display:none; }
        .section { break-inside:avoid; box-shadow:none; }
        body { background:#fff; }
    }
    '''
    
    # Build summary HTML
    summary_html = ''
    for i, (tag, txt) in enumerate(summary):
        num = str(i+1).zfill(2)
        summary_html += '<div class="summary-card"><span class="sc-num">' + num + '</span><span class="sc-tag">' + tag + '</span><div class="sc-text">' + txt + '</div></div>\n'
    
    # Build leader section
    leader_html = '''
    <div class="leader-section">
      <h2>领 导 速 览</h2>
      <div class="leader-cols">
        <div class="leader-card"><div class="label">核心发现</div><div class="value">''' + summary[1][1] + '''</div></div>
        <div class="leader-card"><div class="label">最大差距</div><div class="value">''' + summary[2][1] + '''</div></div>
        <div class="leader-card"><div class="label">首要建议</div><div class="value">''' + summary[5][1] + '''</div></div>
      </div>
    </div>
    '''
    
    # Build compare table
    table_html = '<table class="compare-table" id="compareTable"><thead><tr><th>指标领域</th><th>上级要求</th><th>本地现状</th><th>达标判定</th><th>本土化建议</th></tr></thead><tbody>\n'
    for label, req, local, badge in judge:
        badge_cls = {'达标':'badge-green','基本达标':'badge-yellow','未达标':'badge-red'}[badge]
        # suggestions per row
        if unit["duty"] == "党员教育":
            sugg_map = {
                "远程教育覆盖率": "分批次升级站点设备，2026年底前完成80%站点光纤接入",
                "年均学习完成率": "推行学分制管理，将学习完成率纳入党组织考核",
                "数字化平台使用率": "开发轻量化移动端平台，降低农村党员使用门槛",
                "流动党员纳入率": "建立'云端党支部'，实现异地学习积分互认",
                "红色课件占比": "设立红色课件开发专项资金，年开发≥20门",
                "在线考核覆盖率": "部署在线考核系统，2026年前覆盖率提至80%"
            }
        elif unit["duty"] == "干部培训_党校":
            sugg_map = {
                "年均培训学时": "延长主体班次学制，增设周末专题研修班",
                "主体班次覆盖率": "扩大培训规模，确保5年内全覆盖轮训",
                "案例教学占比": "组建案例开发团队，3年内开发80门案例课",
                "教研咨项目数": "设立专项基金，项目数提至15项/年以上",
                "数字化教学占比": "建设数字党校平台，VR教学覆盖60%班次",
                "师资硕士以上占比": "实施人才引进计划，3年内硕士占比提至55%"
            }
        elif unit["duty"] == "干部培训_干训科":
            sugg_map = {
                "调训到训率": "建立AB岗制度，推行弹性培训模式",
                "重复调训率": "建设智能调训平台，大数据排查重复调训",
                "培训评估覆盖率": "建立四级评估体系，2026年覆盖率提至85%",
                "学时达标率": "开发移动端微课程，化解工学矛盾",
                "网络培训学时占比": "优化线上/线下课时配比，严控网络课时占比",
                "数字化管理覆盖率": "建设一体化管理平台，消除数据孤岛"
            }
        else:
            sugg_map = {}
        
        sugg = sugg_map.get(label, "加强该领域建设，对标达标")
        table_html += '<tr><td>' + label + '</td><td>' + req + '</td><td>' + local + '</td><td><span class="badge ' + badge_cls + '">' + badge + '</span></td><td>' + sugg + '</td></tr>\n'
    table_html += '</tbody></table>\n'
    
    # Build problems HTML
    prob_html = ''
    for sev, ptitle, pdesc, plink in problems:
        sev_cls = 'sev' + str(sev)
        stars = '★' * sev + '☆' * (5 - sev)
        prob_html += '<div class="problem-card ' + sev_cls + '"><div><div class="stars">' + stars + '</div><div class="p-title">' + ptitle + '</div></div><div><div class="p-desc">' + pdesc + '</div><div class="p-link"><a href="' + plink + '" target="_blank">数据来源</a></div></div></div>\n'
    
    # Build cities HTML
    city_html = ''
    for cname, cdata, cdesc, ccompare in cities:
        city_html += '<div class="city-case"><div class="city-name">' + cname + '</div><div class="city-data">' + cdata + '</div><div class="city-desc">' + cdesc + '</div><div class="city-compare">' + ccompare + '</div></div>\n'
    
    # Build AI timeline HTML
    ai_html = '<div class="ai-timeline">\n'
    for atitle, adetail, anum in ai:
        ai_html += '<div class="ai-item"><div class="ai-title">' + atitle + '</div><div class="ai-detail">' + adetail + '</div></div>\n'
    ai_html += '</div>\n'
    
    # Build opts HTML
    opt_html = ''
    for i, (otitle, ocontent, oentity, ostat, okpi) in enumerate(opts):
        # Parse ostat into 3 phases
        phases = ostat.split('；')
        phase1 = phases[0] if len(phases) > 0 else ''
        phase2 = phases[1] if len(phases) > 1 else ''
        phase3 = phases[2] if len(phases) > 2 else ''
        
        # Parse KPIs
        kpi_items = okpi.split('，')
        kpi_cards = ''
        for ki in kpi_items:
            kpi_cards += '<div class="kpi-card"><div class="kpi-val">' + ki + '</div></div>\n'
        
        opt_html += '''<div class="opt-item">
      <div class="opt-header" onclick="this.parentElement.classList.toggle('open')">
        <span class="opt-num">''' + str(i+1) + '''</span>
        <span class="opt-title">''' + otitle + '''</span>
        <span class="opt-arrow">▼</span>
      </div>
      <div class="opt-body">
        <div class="ob-section"><div class="ob-label">具体内容</div><div class="ob-content">''' + ocontent + '''</div></div>
        <div class="ob-section"><div class="ob-label">责任主体</div><div class="ob-content">''' + oentity + '''</div></div>
        <div class="ob-section"><div class="ob-label">实施步骤</div><div class="ob-content">''' + phase1 + '<br>' + phase2 + '<br>' + phase3 + '''</div></div>
        <div class="ob-section"><div class="ob-label">效果评估KPI</div><div class="kpi-grid">''' + kpi_cards + '''</div></div>
      </div>
    </div>\n'''
    
    # Full HTML
    html = '''<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>''' + title + '''</title>
<style>''' + css + '''</style>
</head>
<body>
<div class="container">
  <h1 style="text-align:center;font-size:24px;color:#c0392b;margin-bottom:4px;">''' + title + '''</h1>
  <p style="text-align:center;color:#a0aec0;font-size:13px;margin-bottom:24px;">''' + now_str + ''' ｜ 模板B·六板块党建专项调研报告 ｜ 据公开资料整理</p>
  
  ''' + leader_html + '''
  
  <!-- 壹·核心摘要 -->
  <div class="section">
    <div class="section-title"><span class="num">壹</span><span class="chi">核心摘要</span></div>
    <div class="summary-grid">''' + summary_html + '''</div>
  </div>
  
  <!-- 贰·上级精神与本地实践对照 -->
  <div class="section">
    <div class="section-title"><span class="num">贰</span><span class="chi">上级精神与本地实践对照</span></div>
    ''' + table_html + '''
  </div>
  
  <!-- 叁·问题诊断 -->
  <div class="section">
    <div class="section-title"><span class="num">叁</span><span class="chi">问题诊断</span></div>
    ''' + prob_html + '''
  </div>
  
  <!-- 肆·外省市先进做法与本地差距对比 -->
  <div class="section">
    <div class="section-title"><span class="num">肆</span><span class="chi">外省市先进做法与本地差距对比</span></div>
    ''' + city_html + '''
  </div>
  
  <!-- 伍·数智化/AI赋能专项建议 -->
  <div class="section">
    <div class="section-title"><span class="num">伍</span><span class="chi">数智化/AI赋能专项建议</span></div>
    ''' + ai_html + '''
  </div>
  
  <!-- 陆·优化建议 -->
  <div class="section">
    <div class="section-title"><span class="num">陆</span><span class="chi">优化建议</span></div>
    ''' + opt_html + '''
  </div>
  
  <div class="footer">本报告数据据公开资料整理，仅供参考</div>
</div>

<button class="print-btn" onclick="window.print()" title="打印/PDF">🖨</button>

</body>
</html>'''
    
    return html

# Generate all reports
results = []
timestamp = time.strftime("%Y%m%d%H%M", time.localtime())

for unit in UNITS:
    html_content = build_html(unit)
    filename = unit["id"] + timestamp + ".html"
    filepath = "/data/www/files/" + filename
    
    with open(filepath, "w", encoding="utf-8") as f:
        f.write(html_content)
    
    results.append({
        "name": unit["name"],
        "duty": unit["duty"],
        "filename": filename,
        "intranet": "http://192.168.99.133:5678/" + filename,
        "internet": "https://ziyuan.cdlhyj.com/" + filename
    })
    print("Generated: " + unit["name"] + " -> " + filename)

print("\n=== 生成完成 ===")
for r in results:
    print(r["name"] + "（" + r["duty"] + "）")
    print("  内网: " + r["intranet"])
    print("  外网: " + r["internet"])
