<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.iocoder.yudao.module.server.dal.mysql.policycommunications.PolicyCommunicationsMapper">

    <!--
        一般情况下，尽可能使用 Mapper 进行 CRUD 增删改查即可。
        无法满足的场景，例如说多表关联查询，才使用 XML 编写 SQL。
        代码生成器暂时只生成 Mapper XML 文件本身，更多推荐 MybatisX 快速开发插件来生成查询。
        文档可见：https://www.iocoder.cn/MyBatis/x-plugins/
     -->

    <!-- 统计查询相关SQL -->

    <!-- 查询总数量 -->
    <select id="selectCount" resultType="java.lang.Long">
        SELECT COUNT(*) FROM trs_policy_communications
        <include refid="analysisWhereSql"/>
    </select>

    <!-- 查询已回复数量 -->
    <select id="selectRepliedCount" resultType="java.lang.Long">
        SELECT COUNT(*) FROM trs_policy_communications
        <include refid="analysisWhereSql"/>
        AND deal_status IN (1, 2)
    </select>

    <!-- 查询平均处理时长 -->
    <select id="selectAvgProcessTime" resultType="java.lang.Double">
        SELECT
        AVG(
        CASE
        WHEN replay_time IS NOT NULL AND submit_time IS NOT NULL
        THEN TIMESTAMPDIFF(DAY, submit_time, replay_time)
        ELSE 0
        END
        ) as avgProcessDays
        FROM trs_policy_communications
        <include refid="analysisWhereSql"/>
        AND deal_status IN (1, 2)
        AND replay_time IS NOT NULL
    </select>

    <!-- 查询政策排行 -->
    <select id="selectPolicyRank" resultType="cn.iocoder.yudao.module.server.controller.admin.policycommunications.vo.analysis.PolicyCommunicationsRankRespVO$PolicyRankItem">
        SELECT
        policy_id as policyId,
        policy_title as policyTitle,
        COUNT(*) as count,
        SUM(CASE WHEN deal_status IN (1, 2) THEN 1 ELSE 0 END) as replyCount,
        CASE
        WHEN COUNT(*) > 0
        THEN ROUND(SUM(CASE WHEN deal_status IN (1, 2) THEN 1 ELSE 0 END) * 100.0 / COUNT(*), 2)
        ELSE 0
        END as replyRate
        FROM trs_policy_communications
        <include refid="analysisWhereSql"/>
        AND policy_id IS NOT NULL
        AND policy_title IS NOT NULL
        GROUP BY policy_id, policy_title
        ORDER BY
        <choose>
            <when test="order == 'asc'">
                count ASC
            </when>
            <otherwise>
                count DESC
            </otherwise>
        </choose>
        <if test="limit != null">
            LIMIT #{limit}
        </if>
    </select>

    <!-- 查询部门排行 -->
    <select id="selectDeptRank" resultType="cn.iocoder.yudao.module.server.controller.admin.policycommunications.vo.analysis.PolicyCommunicationsRankRespVO$DeptRankItem">
        SELECT
        reply_deptid as deptId,
        reply_dept as deptName,
        COUNT(*) as handleCount,
        AVG(
        CASE
        WHEN replay_time IS NOT NULL AND submit_time IS NOT NULL
        THEN TIMESTAMPDIFF(DAY, submit_time, replay_time)
        ELSE NULL
        END
        ) as avgProcessDays,
        CASE
        WHEN COUNT(*) > 0
        THEN ROUND(SUM(CASE WHEN deal_status IN (1, 2) THEN 1 ELSE 0 END) * 100.0 / COUNT(*), 2)
        ELSE 0
        END as replyRate
        FROM trs_policy_communications
        <include refid="analysisWhereSql"/>
        AND reply_deptid IS NOT NULL
        AND reply_dept IS NOT NULL
        GROUP BY reply_deptid, reply_dept
        ORDER BY handleCount DESC
    </select>

    <!-- 按日趋势分析 -->
    <select id="selectDayTrend" resultType="cn.iocoder.yudao.module.server.controller.admin.policycommunications.vo.analysis.PolicyCommunicationsTrendAnalysisRespVO$TrendItem">
        SELECT
        DATE(submit_time) as date,
        COUNT(*) as count,
        SUM(CASE WHEN deal_status IN (1, 2) THEN 1 ELSE 0 END) as replyCount
        FROM trs_policy_communications
        <include refid="analysisWhereSql"/>
        GROUP BY DATE(submit_time)
        ORDER BY date ASC
    </select>

    <!-- 按周趋势分析 -->
    <select id="selectWeekTrend" resultType="cn.iocoder.yudao.module.server.controller.admin.policycommunications.vo.analysis.PolicyCommunicationsTrendAnalysisRespVO$TrendItem">
        SELECT
        CONCAT(YEAR(submit_time), '-W', WEEK(submit_time, 1)) as week,
        COUNT(*) as count,
        SUM(CASE WHEN deal_status IN (1, 2) THEN 1 ELSE 0 END) as replyCount
        FROM trs_policy_communications
        <include refid="analysisWhereSql"/>
        GROUP BY YEAR(submit_time), WEEK(submit_time, 1)
        ORDER BY YEAR(submit_time) ASC, WEEK(submit_time, 1) ASC
    </select>

    <!-- 按月趋势分析 -->
    <select id="selectMonthTrend" resultType="cn.iocoder.yudao.module.server.controller.admin.policycommunications.vo.analysis.PolicyCommunicationsTrendAnalysisRespVO$TrendItem">
        SELECT
        DATE_FORMAT(submit_time, '%Y-%m') as month,
        COUNT(*) as count,
        SUM(CASE WHEN deal_status IN (1, 2) THEN 1 ELSE 0 END) as replyCount
        FROM trs_policy_communications
        <include refid="analysisWhereSql"/>
        GROUP BY DATE_FORMAT(submit_time, '%Y-%m')
        ORDER BY month ASC
    </select>

    <!-- 部门详细分析 -->
    <select id="selectDepartmentAnalysis" resultType="cn.iocoder.yudao.module.server.controller.admin.policycommunications.vo.analysis.PolicyCommunicationsDepartmentAnalysisRespVO$DepartmentAnalysisItem">
        SELECT
        reply_deptid as deptId,
        reply_dept as deptName,
        COUNT(*) as totalCount,
        SUM(CASE WHEN deal_status IN (1, 2) THEN 1 ELSE 0 END) as repliedCount,
        SUM(CASE WHEN deal_status = 0 THEN 1 ELSE 0 END) as pendingCount,
        SUM(CASE WHEN deal_status = 2 THEN 1 ELSE 0 END) as publishedCount,
        AVG(
        CASE
        WHEN replay_time IS NOT NULL AND submit_time IS NOT NULL
        THEN TIMESTAMPDIFF(DAY, submit_time, replay_time)
        ELSE NULL
        END
        ) as avgProcessDays,
        CASE
        WHEN COUNT(*) > 0
        THEN ROUND(SUM(CASE WHEN deal_status IN (1, 2) THEN 1 ELSE 0 END) * 100.0 / COUNT(*), 2)
        ELSE 0
        END as replyRate,
        SUM(
        CASE
        WHEN deal_status = 0 AND submit_time IS NOT NULL
        AND TIMESTAMPDIFF(DAY, submit_time, NOW()) > 30
        THEN 1
        ELSE 0
        END
        ) as overdueCount
        FROM trs_policy_communications
        <include refid="analysisWhereSql"/>
        AND reply_deptid IS NOT NULL
        GROUP BY reply_deptid, reply_dept
        ORDER BY totalCount DESC
    </select>

    <!-- 工作量分布 -->
    <select id="selectWorkloadDistribution" resultType="cn.iocoder.yudao.module.server.controller.admin.policycommunications.vo.analysis.PolicyCommunicationsDepartmentAnalysisRespVO$WorkloadItem">
        SELECT
        reply_dept as deptName,
        COUNT(*) as workload,
        ROUND(COUNT(*) * 100.0 / total.total, 2) as percentage
        FROM trs_policy_communications tpc
        CROSS JOIN (
        SELECT COUNT(*) as total
        FROM trs_policy_communications
        <include refid="analysisWhereSql"/>
        ) total
        <include refid="analysisWhereSql"/>
        AND reply_dept IS NOT NULL
        GROUP BY reply_dept, total.total
        ORDER BY workload DESC
    </select>

    <!-- 效率排行 -->
    <select id="selectEfficiencyRanking" resultType="cn.iocoder.yudao.module.server.controller.admin.policycommunications.vo.analysis.PolicyCommunicationsDepartmentAnalysisRespVO$EfficiencyItem">
        SELECT
        t.deptName,
        t.avgProcessDays,
        @rownum := @rownum + 1 as rank
        FROM (
        SELECT
        reply_dept as deptName,
        AVG(
        CASE
        WHEN replay_time IS NOT NULL AND submit_time IS NOT NULL
        THEN TIMESTAMPDIFF(DAY, submit_time, replay_time)
        ELSE NULL
        END
        ) as avgProcessDays
        FROM trs_policy_communications
        <include refid="analysisWhereSql"/>
        AND reply_dept IS NOT NULL
        AND deal_status IN (1, 2)
        AND replay_time IS NOT NULL
        GROUP BY reply_dept
        HAVING avgProcessDays IS NOT NULL
        ORDER BY avgProcessDays ASC
        ) t
        CROSS JOIN (SELECT @rownum := 0) r
    </select>

    <!-- 超时未回复清单 -->
    <select id="selectOverdueList" resultType="cn.iocoder.yudao.module.server.controller.admin.policycommunications.vo.analysis.PolicyCommunicationsOverdueListRespVO$OverdueItem">
        SELECT
        id,
        policy_title as policyTitle,
        title,
        submit_time as submitTime,
        reply_dept as replyDept,
        submit_type as submitType,
        user_name as userName,
        mobile,
        TIMESTAMPDIFF(DAY, submit_time, NOW()) as overdueDays
        FROM trs_policy_communications
        WHERE deleted = 0
        AND deal_status = 0
        AND submit_time IS NOT NULL
        AND submit_time &lt;= #{deadline}
        <if test="pageReqVO.policyTitle != null and pageReqVO.policyTitle != ''">
            AND policy_title LIKE CONCAT('%', #{pageReqVO.policyTitle}, '%')
        </if>
        <if test="pageReqVO.replyDept != null and pageReqVO.replyDept != ''">
            AND reply_dept LIKE CONCAT('%', #{pageReqVO.replyDept}, '%')
        </if>
        <if test="pageReqVO.submitType != null and pageReqVO.submitType != ''">
            AND submit_type = #{pageReqVO.submitType}
        </if>
        <if test="pageReqVO.submitTime != null and pageReqVO.submitTime.length == 2">
            AND submit_time BETWEEN #{pageReqVO.submitTime[0]} AND #{pageReqVO.submitTime[1]}
        </if>
        ORDER BY overdueDays DESC, submit_time ASC
    </select>

    <!-- 类型分布 -->
    <select id="selectTypeDistribution" resultType="cn.iocoder.yudao.module.server.controller.admin.policycommunications.vo.analysis.PolicyCommunicationsContentAnalysisRespVO$TypeDistributionItem">
        SELECT
        submit_type as typeCode,
        COUNT(*) as count,
        ROUND(COUNT(*) * 100.0 / total.total, 2) as percentage
        FROM trs_policy_communications tpc
        CROSS JOIN (
        SELECT COUNT(*) as total
        FROM trs_policy_communications
        <include refid="analysisWhereSql"/>
        ) total
        <include refid="analysisWhereSql"/>
        AND submit_type IS NOT NULL
        GROUP BY submit_type, total.total
        ORDER BY count DESC
    </select>

    <!-- 高频词云数据（简化版本，实际需要分词处理） -->
    <select id="selectWordCloudData" resultType="cn.iocoder.yudao.module.server.controller.admin.policycommunications.vo.analysis.PolicyCommunicationsContentAnalysisRespVO$WordCloudItem">
        SELECT
        word as name,
        COUNT(*) as value
        FROM (
        -- 提取标题中的前3个关键词
        SELECT
        CASE
        WHEN LOCATE('政策', title) > 0 THEN '政策'
        WHEN LOCATE('咨询', title) > 0 THEN '咨询'
        WHEN LOCATE('建议', title) > 0 THEN '建议'
        WHEN LOCATE('投诉', title) > 0 THEN '投诉'
        WHEN LOCATE('问题', title) > 0 THEN '问题'
        WHEN LOCATE('实施', title) > 0 THEN '实施'
        WHEN LOCATE('改革', title) > 0 THEN '改革'
        WHEN LOCATE('发展', title) > 0 THEN '发展'
        WHEN LOCATE('建设', title) > 0 THEN '建设'
        WHEN LOCATE('服务', title) > 0 THEN '服务'
        WHEN LOCATE('审批', title) > 0 THEN '审批'
        WHEN LOCATE('申报', title) > 0 THEN '申报'
        WHEN LOCATE('办理', title) > 0 THEN '办理'
        WHEN LOCATE('申请', title) > 0 THEN '申请'
        WHEN LOCATE('资金', title) > 0 THEN '资金'
        WHEN LOCATE('补贴', title) > 0 THEN '补贴'
        WHEN LOCATE('补助', title) > 0 THEN '补助'
        WHEN LOCATE('奖励', title) > 0 THEN '奖励'
        WHEN LOCATE('贷款', title) > 0 THEN '贷款'
        WHEN LOCATE('税收', title) > 0 THEN '税收'
        WHEN LOCATE('税收优惠', title) > 0 THEN '税收'
        WHEN LOCATE('减免', title) > 0 THEN '减免'
        WHEN LOCATE('优惠', title) > 0 THEN '优惠'
        WHEN LOCATE('扶持', title) > 0 THEN '扶持'
        WHEN LOCATE('扶持政策', title) > 0 THEN '扶持'
        WHEN LOCATE('支持', title) > 0 THEN '支持'
        WHEN LOCATE('监管', title) > 0 THEN '监管'
        WHEN LOCATE('检查', title) > 0 THEN '检查'
        WHEN LOCATE('执法', title) > 0 THEN '执法'
        WHEN LOCATE('处罚', title) > 0 THEN '处罚'
        WHEN LOCATE('违规', title) > 0 THEN '违规'
        WHEN LOCATE('违法', title) > 0 THEN '违法'
        WHEN LOCATE('合规', title) > 0 THEN '合规'
        WHEN LOCATE('标准', title) > 0 THEN '标准'
        WHEN LOCATE('规范', title) > 0 THEN '规范'
        WHEN LOCATE('规定', title) > 0 THEN '规定'
        WHEN LOCATE('制度', title) > 0 THEN '制度'
        WHEN LOCATE('办法', title) > 0 THEN '办法'
        WHEN LOCATE('条例', title) > 0 THEN '条例'
        WHEN LOCATE('法律', title) > 0 THEN '法律'
        WHEN LOCATE('法规', title) > 0 THEN '法规'
        WHEN LOCATE('解读', title) > 0 THEN '解读'
        WHEN LOCATE('解释', title) > 0 THEN '解释'
        WHEN LOCATE('说明', title) > 0 THEN '说明'
        WHEN LOCATE('反馈', title) > 0 THEN '反馈'
        WHEN LOCATE('回复', title) > 0 THEN '回复'
        WHEN LOCATE('答复', title) > 0 THEN '答复'
        WHEN LOCATE('处理', title) > 0 THEN '处理'
        WHEN LOCATE('解决', title) > 0 THEN '解决'
        WHEN LOCATE('协调', title) > 0 THEN '协调'
        WHEN LOCATE('沟通', title) > 0 THEN '沟通'
        WHEN LOCATE('交流', title) > 0 THEN '交流'
        WHEN LOCATE('讨论', title) > 0 THEN '讨论'
        WHEN LOCATE('研究', title) > 0 THEN '研究'
        WHEN LOCATE('调研', title) > 0 THEN '调研'
        WHEN LOCATE('评估', title) > 0 THEN '评估'
        WHEN LOCATE('评价', title) > 0 THEN '评价'
        WHEN LOCATE('考核', title) > 0 THEN '考核'
        WHEN LOCATE('评比', title) > 0 THEN '评比'
        WHEN LOCATE('认证', title) > 0 THEN '认证'
        WHEN LOCATE('资质', title) > 0 THEN '资质'
        WHEN LOCATE('许可', title) > 0 THEN '许可'
        WHEN LOCATE('许可证', title) > 0 THEN '许可'
        WHEN LOCATE('执照', title) > 0 THEN '执照'
        WHEN LOCATE('证书', title) > 0 THEN '证书'
        WHEN LOCATE('备案', title) > 0 THEN '备案'
        WHEN LOCATE('登记', title) > 0 THEN '登记'
        WHEN LOCATE('注册', title) > 0 THEN '注册'
        WHEN LOCATE('公示', title) > 0 THEN '公示'
        WHEN LOCATE('公开', title) > 0 THEN '公开'
        WHEN LOCATE('信息', title) > 0 THEN '信息'
        WHEN LOCATE('数据', title) > 0 THEN '数据'
        WHEN LOCATE('统计', title) > 0 THEN '统计'
        WHEN LOCATE('分析', title) > 0 THEN '分析'
        WHEN LOCATE('报告', title) > 0 THEN '报告'
        WHEN LOCATE('总结', title) > 0 THEN '总结'
        WHEN LOCATE('计划', title) > 0 THEN '计划'
        WHEN LOCATE('规划', title) > 0 THEN '规划'
        WHEN LOCATE('方案', title) > 0 THEN '方案'
        WHEN LOCATE('措施', title) > 0 THEN '措施'
        WHEN LOCATE('对策', title) > 0 THEN '对策'
        WHEN LOCATE('建议', title) > 0 THEN '建议'
        WHEN LOCATE('意见', title) > 0 THEN '意见'
        WHEN LOCATE('提案', title) > 0 THEN '提案'
        WHEN LOCATE('议案', title) > 0 THEN '议案'
        WHEN LOCATE('议程', title) > 0 THEN '议程'
        WHEN LOCATE('会议', title) > 0 THEN '会议'
        WHEN LOCATE('座谈', title) > 0 THEN '座谈'
        WHEN LOCATE('研讨会', title) > 0 THEN '研讨'
        WHEN LOCATE('培训', title) > 0 THEN '培训'
        WHEN LOCATE('教育', title) > 0 THEN '教育'
        WHEN LOCATE('宣传', title) > 0 THEN '宣传'
        WHEN LOCATE('推广', title) > 0 THEN '推广'
        WHEN LOCATE('示范', title) > 0 THEN '示范'
        WHEN LOCATE('试点', title) > 0 THEN '试点'
        WHEN LOCATE('试验', title) > 0 THEN '试验'
        WHEN LOCATE('创新', title) > 0 THEN '创新'
        WHEN LOCATE('创业', title) > 0 THEN '创业'
        WHEN LOCATE('就业', title) > 0 THEN '就业'
        WHEN LOCATE('招聘', title) > 0 THEN '招聘'
        WHEN LOCATE('人才', title) > 0 THEN '人才'
        WHEN LOCATE('专家', title) > 0 THEN '专家'
        WHEN LOCATE('技术', title) > 0 THEN '技术'
        WHEN LOCATE('科技', title) > 0 THEN '科技'
        WHEN LOCATE('科研', title) > 0 THEN '科研'
        WHEN LOCATE('项目', title) > 0 THEN '项目'
        WHEN LOCATE('工程', title) > 0 THEN '工程'
        WHEN LOCATE('基础设施', title) > 0 THEN '基建'
        WHEN LOCATE('环保', title) > 0 THEN '环保'
        WHEN LOCATE('环境', title) > 0 THEN '环境'
        WHEN LOCATE('生态', title) > 0 THEN '生态'
        WHEN LOCATE('节能', title) > 0 THEN '节能'
        WHEN LOCATE('减排', title) > 0 THEN '减排'
        WHEN LOCATE('安全', title) > 0 THEN '安全'
        WHEN LOCATE('安全生产', title) > 0 THEN '安全'
        WHEN LOCATE('质量', title) > 0 THEN '质量'
        WHEN LOCATE('卫生', title) > 0 THEN '卫生'
        WHEN LOCATE('健康', title) > 0 THEN '健康'
        WHEN LOCATE('医疗', title) > 0 THEN '医疗'
        WHEN LOCATE('医保', title) > 0 THEN '医保'
        WHEN LOCATE('社保', title) > 0 THEN '社保'
        WHEN LOCATE('养老', title) > 0 THEN '养老'
        WHEN LOCATE('住房', title) > 0 THEN '住房'
        WHEN LOCATE('房地产', title) > 0 THEN '房产'
        WHEN LOCATE('土地', title) > 0 THEN '土地'
        WHEN LOCATE('农业', title) > 0 THEN '农业'
        WHEN LOCATE('农村', title) > 0 THEN '农村'
        WHEN LOCATE('农民', title) > 0 THEN '农民'
        WHEN LOCATE('乡村振兴', title) > 0 THEN '乡村振兴'
        WHEN LOCATE('工业', title) > 0 THEN '工业'
        WHEN LOCATE('制造业', title) > 0 THEN '制造'
        WHEN LOCATE('商业', title) > 0 THEN '商业'
        WHEN LOCATE('贸易', title) > 0 THEN '贸易'
        WHEN LOCATE('进出口', title) > 0 THEN '贸易'
        WHEN LOCATE('外资', title) > 0 THEN '外资'
        WHEN LOCATE('投资', title) > 0 THEN '投资'
        WHEN LOCATE('融资', title) > 0 THEN '融资'
        WHEN LOCATE('金融', title) > 0 THEN '金融'
        WHEN LOCATE('银行', title) > 0 THEN '金融'
        WHEN LOCATE('证券', title) > 0 THEN '金融'
        WHEN LOCATE('保险', title) > 0 THEN '保险'
        WHEN LOCATE('旅游', title) > 0 THEN '旅游'
        WHEN LOCATE('文化', title) > 0 THEN '文化'
        WHEN LOCATE('体育', title) > 0 THEN '体育'
        WHEN LOCATE('教育', title) > 0 THEN '教育'
        WHEN LOCATE('学校', title) > 0 THEN '教育'
        WHEN LOCATE('学生', title) > 0 THEN '教育'
        WHEN LOCATE('教师', title) > 0 THEN '教育'
        WHEN LOCATE('网络', title) > 0 THEN '网络'
        WHEN LOCATE('互联网', title) > 0 THEN '网络'
        WHEN LOCATE('数字化', title) > 0 THEN '数字'
        WHEN LOCATE('数字', title) > 0 THEN '数字'
        WHEN LOCATE('智能', title) > 0 THEN '智能'
        WHEN LOCATE('智慧', title) > 0 THEN '智慧'
        WHEN LOCATE('信息化', title) > 0 THEN '信息'
        WHEN LOCATE('效率', title) > 0 THEN '效率'
        WHEN LOCATE('效能', title) > 0 THEN '效能'
        WHEN LOCATE('绩效', title) > 0 THEN '绩效'
        WHEN LOCATE('成本', title) > 0 THEN '成本'
        WHEN LOCATE('效益', title) > 0 THEN '效益'
        WHEN LOCATE('效果', title) > 0 THEN '效果'
        WHEN LOCATE('成果', title) > 0 THEN '成果'
        WHEN LOCATE('成就', title) > 0 THEN '成就'
        WHEN LOCATE('荣誉', title) > 0 THEN '荣誉'
        WHEN LOCATE('表彰', title) > 0 THEN '表彰'
        WHEN LOCATE('先进', title) > 0 THEN '先进'
        WHEN LOCATE('优秀', title) > 0 THEN '优秀'
        ELSE '其他'
        END as word
        FROM trs_policy_communications
        WHERE deleted = 0
        <if test="pageReqVO != null">
            <if test="pageReqVO.policyTitle != null and pageReqVO.policyTitle != ''">
                AND policy_title LIKE CONCAT('%', #{pageReqVO.policyTitle}, '%')
            </if>
            <if test="pageReqVO.submitType != null and pageReqVO.submitType != ''">
                AND submit_type = #{pageReqVO.submitType}
            </if>
            <if test="pageReqVO.dealStatus != null">
                AND deal_status = #{pageReqVO.dealStatus}
            </if>
            <if test="pageReqVO.submitTime != null and pageReqVO.submitTime.length == 2">
                AND submit_time BETWEEN #{pageReqVO.submitTime[0]} AND #{pageReqVO.submitTime[1]}
            </if>
        </if>
        ) t
        GROUP BY word
        ORDER BY value DESC
    </select>

    <!-- 热词统计 -->
    <select id="selectHotWords" resultType="cn.iocoder.yudao.module.server.controller.admin.policycommunications.vo.analysis.PolicyCommunicationsContentAnalysisRespVO$HotWordItem">
        SELECT word, COUNT(*) AS count, 0 AS growthRate
        FROM (
            SELECT
            tpc.id,  -- ✅ 添加id字段
            SUBSTRING_INDEX(SUBSTRING_INDEX(tpc.title, ' ', n.n), ' ', -1) AS word
            FROM trs_policy_communications tpc
            CROSS JOIN (SELECT 1 n UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5) n
            WHERE
            tpc.deleted = 0
            AND tpc.title IS NOT NULL
            AND LENGTH(tpc.title) - LENGTH(REPLACE(tpc.title, ' ', '')) >= n.n - 1

            UNION ALL

            SELECT
            tpc.id,  -- ✅ 添加id字段
            SUBSTRING_INDEX(SUBSTRING_INDEX(tpc.content, ' ', n.n), ' ', -1) AS word
            FROM trs_policy_communications tpc
            CROSS JOIN (SELECT 1 n UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5) n
            WHERE
            tpc.deleted = 0
            AND tpc.content IS NOT NULL
            AND LENGTH(tpc.content) - LENGTH(REPLACE(tpc.content, ' ', '')) >= n.n - 1
            ) words
        WHERE LENGTH(word) > 1
          AND word NOT IN ('的', '了', '在', '是', '和', '与', '及', '等', '为', '对', '关于')
        GROUP BY word
        HAVING COUNT(*) >= 3
        ORDER BY count DESC
            LIMIT 20
    </select>

    <!-- 通用分析查询条件 -->
    <sql id="analysisWhereSql">
        WHERE deleted = 0
        <if test="pageReqVO.policyTitle != null and pageReqVO.policyTitle != ''">
            AND policy_title LIKE CONCAT('%', #{pageReqVO.policyTitle}, '%')
        </if>
        <if test="pageReqVO.submitType != null and pageReqVO.submitType != ''">
            AND submit_type = #{pageReqVO.submitType}
        </if>
        <if test="pageReqVO.dealStatus != null">
            AND deal_status = #{pageReqVO.dealStatus}
        </if>
        <if test="pageReqVO.replyDeptid != null">
            AND reply_deptid = #{pageReqVO.replyDeptid}
        </if>
        <if test="pageReqVO.submitTime != null and pageReqVO.submitTime.length == 2">
            AND submit_time BETWEEN #{pageReqVO.submitTime[0]} AND #{pageReqVO.submitTime[1]}
        </if>
        <if test="pageReqVO.policyId != null and pageReqVO.policyId != ''">
            -- 如果数据库中有policy_code字段
            AND policy_id = #{pageReqVO.policyId}
        </if>
        <if test="pageReqVO.departmentId != null and pageReqVO.departmentId != ''">
            -- 如果数据库中有policy_code字段
            AND department_id_t = #{pageReqVO.departmentId}
        </if>
    </sql>

    <!-- 子查询专用条件 -->
    <sql id="analysisWhereSqlForSubquery">
        AND EXISTS (
        SELECT 1 FROM trs_policy_communications tpc2
        WHERE tpc2.id = words.id
        AND tpc2.deleted = 0
        <if test="pageReqVO.policyTitle != null and pageReqVO.policyTitle != ''">
            AND tpc2.policy_title LIKE CONCAT('%', #{pageReqVO.policyTitle}, '%')
        </if>
        <if test="pageReqVO.submitType != null and pageReqVO.submitType != ''">
            AND tpc2.submit_type = #{pageReqVO.submitType}
        </if>
        <if test="pageReqVO.dealStatus != null">
            AND tpc2.deal_status = #{pageReqVO.dealStatus}
        </if>
        <if test="pageReqVO.submitTime != null and pageReqVO.submitTime.length == 2">
            AND tpc2.submit_time BETWEEN #{pageReqVO.submitTime[0]} AND #{pageReqVO.submitTime[1]}
        </if>
        )
    </sql>


</mapper>