<?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.TrsSensitiveResultsMapper">

    <resultMap type="TrsSensitiveResults" id="TrsSensitiveResultsResult">
        <result property="id"    column="id"    />
        <result property="sitename"    column="sitename"    />
        <result property="sitetag"    column="sitetag"    />
        <result property="title"    column="title"    />
        <result property="page"    column="page"    />
        <result property="wrongWord"    column="wrong_word"    />
        <result property="correctWord"    column="correct_word"    />
        <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="allcontext"    column="allcontext"    />
    </resultMap>

    <sql id="selectTrsSensitiveResultsVo">
        select id, sitename, sitetag, title, page, wrong_word, correct_word, error_type,error_grade, context, time ,scantime, status, datakey from trs_sensitive_results
    </sql>

    <sql id="selectTrsSensitiveResultsVo2">
        select id, sitename, sitetag, title, page, wrong_word, correct_word, error_type,error_grade, context, time ,scantime, status, datakey,allcontext from trs_sensitive_results
    </sql>

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

    <select id="selectTrsSensitiveResultsList" parameterType="TrsSensitiveResults" resultMap="TrsSensitiveResultsResult">
        <include refid="selectTrsSensitiveResultsVo"/>
        <where>
            <if test="sitename != null   and sitename != ''"> and sitename = #{sitename}</if>
            <if test="sitetag != null   and sitetag != ''"> and sitetag = #{sitetag}</if>
            <if test="title != null   and title != ''"> and title = #{title}</if>
            <if test="page != null   and page != ''"> and page like concat('%', #{page}, '%')</if>
            <if test="wrongWord != null  and wrongWord != ''"> and wrong_word like concat('%', #{wrongWord}, '%')</if>
            <if test="correctWord != null  and correctWord != ''"> and correct_word like concat('%', #{correctWord}, '%')</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 concat('%', #{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 like concat('%', #{status}, '%')</if>
            <if test="version != null  and version != ''"> and version = #{version}</if>
            <if test="datakey != null  and datakey != ''"> and datakey = #{datakey}</if>
        </where>
        group by page,wrong_word
        order by time desc
    </select>

    <select id="selectTrsSensitiveResultsListAll" parameterType="TrsSensitiveResults" resultMap="TrsSensitiveResultsResult">
        <include refid="selectTrsSensitiveResultsVo"/>
        <where>
            <if test="sitename != null   and sitename != ''"> and sitename = #{sitename}</if>
            <if test="sitetag != null   and sitetag != ''"> and sitetag = #{sitetag}</if>
            <if test="title != null   and title != ''"> and title = #{title}</if>
            <if test="page != null   and page != ''"> and page = #{page}</if>
            <if test="wrongWord != null  and wrongWord != ''"> and wrong_word = #{wrongWord}</if>
            <if test="correctWord != null  and correctWord != ''"> and correct_word = #{correctWord}</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 concat('%', #{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 like concat('%', #{status}, '%')</if>
            <if test="version != null  and version != ''"> and version = #{version}</if>
            <if test="datakey != null  and datakey != ''"> and datakey = #{datakey}</if>
        </where>
        order by time desc
    </select>

    <select id="selectTrsSensitiveResultsById" parameterType="Long" resultMap="TrsSensitiveResultsResult">
        <include refid="selectTrsSensitiveResultsVo2"/>
        where id = #{id}
    </select>

    <select id="selectTrsSensitiveResultsByIdV2" parameterType="Long" resultMap="TrsSensitiveResultsResult">
        select t.id, t.sitename, t.sitetag, t.title, t.page, t.wrong_word, t.correct_word, t.error_type,t.error_grade, t.context, t.time ,t.scantime, t.status, t.datakey,a.content as allcontext
        from trs_sensitive_results t
        left join trs_article_info a on t.page=a.url
        where t.id = #{id}
    </select>
    <insert id="insertTrsSensitiveResults" parameterType="TrsSensitiveResults" useGeneratedKeys="true" keyProperty="id">
        insert into trs_sensitive_results
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="page != null">page,</if>
            <if test="title != null">title,</if>
            <if test="sitename != null">sitename,</if>
            <if test="sitetag != null">sitetag,</if>
            <if test="wrongWord != null and wrongWord != ''">wrong_word,</if>
            <if test="correctWord != null and correctWord != ''">correct_word,</if>
            <if test="errorType != null and errorType != ''">error_type,</if>
            <if test="errorGrade != null and errorGrade != ''">error_grade,</if>
            <if test="context != null and context != ''">context,</if>
            <if test="time != null">time,</if>
            <if test="scantime != null">scantime,</if>
            <if test="status != null and status != ''">status,</if>
            <if test="version != null">version,</if>
            <if test="datakey != null">datakey,</if>
            <if test="allcontext != null">allcontext,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="page != null">#{page},</if>
            <if test="title != null">#{title},</if>
            <if test="sitename != null">#{sitename},</if>
            <if test="sitetag != null">#{sitetag},</if>
            <if test="wrongWord != null and wrongWord != ''">#{wrongWord},</if>
            <if test="correctWord != null and correctWord != ''">#{correctWord},</if>
            <if test="errorType != null and errorType != ''">#{errorType},</if>
            <if test="errorGrade != null and errorGrade != ''">#{errorGrade},</if>
            <if test="context != null and context != ''">#{context},</if>
            <if test="time != null">#{time},</if>
            <if test="scantime != null">#{scantime},</if>
            <if test="status != null and status != ''">#{status},</if>
            <if test="version != null">#{version},</if>
            <if test="datakey != null">#{datakey},</if>
            <if test="allcontext != null">#{allcontext},</if>
        </trim>
    </insert>

    <update id="updateTrsSensitiveResults" parameterType="TrsSensitiveResults">
        update trs_sensitive_results
        <trim prefix="SET" suffixOverrides=",">
            <if test="page != null">page = #{page},</if>
            <if test="title != null">title = #{title},</if>
            <if test="sitename != null">sitename = #{sitename},</if>
            <if test="sitetag != null">sitetag = #{sitetag},</if>
            <if test="wrongWord != null and wrongWord != ''">wrong_word = #{wrongWord},</if>
            <if test="correctWord != null and correctWord != ''">correct_word = #{correctWord},</if>
            <if test="errorType != null and errorType != ''">error_type = #{errorType},</if>
            <if test="errorGrade != null and errorGrade != ''">error_grade = #{errorGrade},</if>
            <if test="context != null and context != ''">context = #{context},</if>
            <if test="time != null">time = #{time},</if>
            <if test="scantime != null">scantime = #{scantime},</if>
            <if test="status != null and status != ''">status = #{status},</if>
            <if test="version != null  and version != ''">  version = #{version},</if>
            <if test="datakey != null  and datakey != ''">  datakey = #{datakey},</if>
            <if test="allcontext != null  and allcontext != ''">  allcontext = #{allcontext},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteTrsSensitiveResultsById" parameterType="Long">
        delete from trs_sensitive_results where id = #{id}
    </delete>

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

    <select id="getStatistics" parameterType="TrsSensitiveResults" resultType="java.util.Map">
            /*SUM(CASE WHEN error_type = '1' THEN 1 ELSE 0 END) AS errorWordCount,*/
        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_sensitive_results
        <where>
            <if test="sitename != null   and sitename != ''"> and sitename = #{sitename}</if>
            <if test="sitetag != null   and sitetag != ''"> and sitetag = #{sitetag}</if>
            <if test="title != null   and title != ''"> and title = #{title}</if>
            <if test="page != null   and page != ''"> and page = #{page}</if>
            <if test="wrongWord != null  and wrongWord != ''"> and wrong_word = #{wrongWord}</if>
            <if test="correctWord != null  and correctWord != ''"> and correct_word = #{correctWord}</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="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 like concat('%', #{status}, '%')</if>
        </where>
    </select>

</mapper>