<?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="com.ruoyi.project.mapper.TrsPolicyResultsMapper">

    <resultMap type="TrsPolicyResults" id="TrsPolicyResultsResult">
        <result property="id"    column="id"    />
        <result property="sitename"    column="sitename"    />
        <result property="sitetag"    column="sitetag"    />
        <result property="page"    column="page"    />
        <result property="wrongWord"    column="wrong_word"    />
        <result property="wrongRule"    column="wrong_rule"    />
        <result property="errorType"    column="error_type"    />
        <result property="errorGrade"    column="error_grade"    />
        <result property="context"    column="context"    />
        <result property="time"    column="time"    />
        <result property="scantime"    column="scantime"    />
        <result property="status"    column="status"    />
        <result property="version"    column="version"    />
        <result property="datakey"    column="datakey"    />
        <result property="title"    column="title"    />
        <result property="allcontext"    column="allcontext"    />
        <result property="tenantId"    column="tenant_id"    />
    </resultMap>

    <sql id="selectTrsPolicyResultsVo">
        select id, sitename,sitetag, page, wrong_word, wrong_rule, error_type,error_grade, context, time,scantime, status,title,tenant_id from trs_policy_results
    </sql>

    <sql id="selectTrsPolicyResultsVo2">
        select id, sitename, page, wrong_word, wrong_rule, error_type,error_grade, context, time,scantime, status,title,allcontext,tenant_id from trs_policy_results
    </sql>

    <select id="selectId" parameterType="String" resultType="Long">
        select id from trs_policy_results where datakey = #{datakey}
    </select>

    <!-- 修复 selectTrsPolicyResultsList 中的 concat 问题 -->
    <select id="selectTrsPolicyResultsList" parameterType="TrsPolicyResults" resultMap="TrsPolicyResultsResult">
        <include refid="selectTrsPolicyResultsVo"/>
        <where>
            <if test="sitename != null and sitename != ''"> and sitename = #{sitename}</if>
            <if test="sitetag != null and sitetag != ''"> and sitetag = #{sitetag}</if>
            <if test="page != null and page != ''"> and page like '%' || #{page} || '%'</if>
            <if test="wrongWord != null and wrongWord != ''"> and wrong_word like '%' || #{wrongWord} || '%'</if>
            <if test="wrongRule != null and wrongRule != ''"> and wrong_rule like '%' || #{wrongRule} || '%'</if>
            <if test="errorType != null and errorType != ''"> and error_type = #{errorType}</if>
            <if test="errorGrade != null and errorGrade != ''"> and error_grade = #{errorGrade}</if>
            <if test="context != null and context != ''"> and context like '%' || #{context} || '%'</if>
            <if test="params.beginTime != null and params.beginTime != '' and params.endTime != null and params.endTime != ''">
                and time between #{params.beginTime} and #{params.endTime}
            </if>
            <if test="status != null and status != ''"> and status = #{status}</if>
            <if test="version != null and version != ''"> and version = #{version}</if>
            <if test="datakey != null and datakey != ''"> and datakey = #{datakey}</if>
            <if test="title != null and title != ''"> and title = #{title}</if>
            <if test="tenantId != null and tenantId != ''"> and tenant_id = #{tenantId}</if>
        </where>
        order by time desc
    </select>

    <select id="selectTrsPolicyResultsById" parameterType="Long" resultMap="TrsPolicyResultsResult">
        <include refid="selectTrsPolicyResultsVo2"/>
        where id = #{id}
    </select>

    <!-- 修复 insert 语句，添加 ID 生成 -->
    <insert id="insertTrsPolicyResults" parameterType="TrsPolicyResults">
        <selectKey keyProperty="id" resultType="java.lang.Long" order="BEFORE">
            SELECT nextval('seq_trs_policy_results_id')
        </selectKey>

        insert into trs_policy_results
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="sitename != null">sitename,</if>
            <if test="sitetag != null">sitetag,</if>
            <if test="page != null">page,</if>
            <if test="wrongWord != null">wrong_word,</if>
            <if test="wrongRule != null">wrong_rule,</if>
            <if test="errorType != null">error_type,</if>
            <if test="errorGrade != null">error_grade,</if>
            <if test="context != null">context,</if>
            <if test="time != null">time,</if>
            <if test="scantime != null">scantime,</if>
            <if test="status != null">status,</if>
            <if test="version != null">version,</if>
            <if test="datakey != null">datakey,</if>
            <if test="title != null">title,</if>
            <if test="allcontext != null">allcontext,</if>
            <if test="tenantId != null">tenant_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="sitename != null">#{sitename},</if>
            <if test="sitetag != null">#{sitetag},</if>
            <if test="page != null">#{page},</if>
            <if test="wrongWord != null">#{wrongWord},</if>
            <if test="wrongRule != null">#{wrongRule},</if>
            <if test="errorType != null">#{errorType},</if>
            <if test="errorGrade != null">#{errorGrade},</if>
            <if test="context != null">#{context},</if>
            <if test="time != null">#{time},</if>
            <if test="scantime != null">#{scantime},</if>
            <if test="status != null">#{status},</if>
            <if test="version != null">#{version},</if>
            <if test="datakey != null">#{datakey},</if>
            <if test="title != null">#{title},</if>
            <if test="allcontext != null">#{allcontext},</if>
            <if test="tenantId != null">#{tenantId},</if>
        </trim>
    </insert>

    <update id="updateTrsPolicyResults" parameterType="TrsPolicyResults">
        update trs_policy_results
        <trim prefix="SET" suffixOverrides=",">
            <if test="sitename != null">sitename = #{sitename},</if>
            <if test="sitetag != null">sitetag = #{sitetag},</if>
            <if test="page != null">page = #{page},</if>
            <if test="wrongWord != null">wrong_word = #{wrongWord},</if>
            <if test="wrongRule != null">wrong_rule = #{wrongRule},</if>
            <if test="errorType != null">error_type = #{errorType},</if>
            <if test="errorGrade != null">error_grade = #{errorGrade},</if>
            <if test="context != null">context = #{context},</if>
            <if test="time != null">time = #{time},</if>
            <if test="scantime != null">scantime = #{scantime},</if>
            <if test="status != null">status = #{status},</if>
            <if test="version != null">version = #{version},</if>
            <if test="datakey != null">datakey = #{datakey},</if>
            <if test="title != null">title = #{title},</if>
            <if test="allcontext != null">allcontext = #{allcontext},</if>
            <if test="tenantId != null and tenantId != ''">tenant_id = #{tenantId},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteTrsPolicyResultsById" parameterType="Long">
        delete from trs_policy_results where id = #{id}
    </delete>

    <delete id="deleteTrsPolicyResultsByIds" parameterType="String">
        delete from trs_policy_results where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <!-- 修复 getStatistics 中的 concat 问题 -->
    <select id="getStatistics" parameterType="TrsPolicyResults" resultType="java.util.Map">
        SELECT
        /* 第一组：总体统计 */
        COUNT(DISTINCT datakey) AS errorWordCount,
        SUM(CASE WHEN status = '已处理' THEN 1 ELSE 0 END) AS processedCount,
        SUM(CASE WHEN status = '不必处理' THEN 1 ELSE 0 END) AS noNeedProcessCount,

        /* 第二组：严重性错误(errorGrade=1) */
        SUM(CASE WHEN error_grade = '严重性错误' THEN 1 ELSE 0 END) AS errorWordCount2,
        SUM(CASE WHEN error_grade = '严重性错误' AND status = '已处理' THEN 1 ELSE 0 END) AS processedCount2,
        SUM(CASE WHEN error_grade = '严重性错误' AND status = '不必处理' THEN 1 ELSE 0 END) AS noNeedProcessCount2,

        /* 第三组：一般性错误(errorGrade=2) */
        SUM(CASE WHEN error_grade = '一般性错误' THEN 1 ELSE 0 END) AS errorWordCount3,
        SUM(CASE WHEN error_grade = '一般性错误' AND status = '已处理' THEN 1 ELSE 0 END) AS processedCount3,
        SUM(CASE WHEN error_grade = '一般性错误' AND status = '不必处理' THEN 1 ELSE 0 END) AS noNeedProcessCount3
        FROM trs_policy_results
        <where>
            <if test="sitename != null and sitename != ''"> and sitename = #{sitename}</if>
            <if test="sitetag != null and sitetag != ''"> and sitetag = #{sitetag}</if>
            <if test="page != null and page != ''"> and page = #{page}</if>
            <if test="wrongWord != null and wrongWord != ''"> and wrong_word = #{wrongWord}</if>
            <if test="wrongRule != null and wrongRule != ''"> and wrong_rule = #{wrongRule}</if>
            <if test="errorType != null and errorType != ''"> and error_type = #{errorType}</if>
            <if test="errorGrade != null and errorGrade != ''"> and error_grade = #{errorGrade}</if>
            <if test="context != null and context != ''"> and context like '%' || #{context} || '%'</if>
            <if test="params.beginTime != null and params.beginTime != '' and params.endTime != null and params.endTime != ''">
                and time between #{params.beginTime} and #{params.endTime}
            </if>
            <if test="status != null and status != ''"> and status = #{status}</if>
            <if test="version != null and version != ''"> and version = #{version}</if>
            <if test="datakey != null and datakey != ''"> and datakey = #{datakey}</if>
        </where>
    </select>

</mapper>