<?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.TrsPolicyWordsMapper">
    
    <resultMap type="TrsPolicyWords" id="TrsPolicyWordsResult">
        <result property="id"    column="id"    />
        <result property="wrongWord"    column="wrong_word"    />
        <result property="errorRule"    column="error_rule"    />
        <result property="errorType"    column="error_type"    />
        <result property="errorGrade"    column="error_grade"    />
        <result property="dictType"    column="dict_type"    />
        <result property="flag"    column="flag"    />
        <result property="status"    column="status"    />
        <result property="time"    column="time"    />
        <result property="accord"    column="accord"    />
        <result property="source"    column="source"    />
        <result property="linkurl"    column="linkurl"    />
        <result property="effectstart"    column="effectstart"    />
        <result property="effectend"    column="effectend"    />
    </resultMap>

    <sql id="selectTrsPolicyWordsVo">
        select t.id, t.wrong_word, t.error_rule, t.error_type,t.error_grade, t.dict_type, t.flag, t.status,t.time,t.accord, t.source, t.linkurl, t.effectstart, t.effectend from trs_policy_words t
    </sql>

    <select id="selectTrsWordsListByErrorWord" parameterType="String" resultMap="TrsPolicyWordsResult">
        <include refid="selectTrsPolicyWordsVo"/>
        where wrong_word = #{errorWord}
    </select>

    <select id="selectTrsPolicyWordsList" parameterType="TrsPolicyWords" resultMap="TrsPolicyWordsResult">
        <include refid="selectTrsPolicyWordsVo"/>
        <if test="params.dataScope != null and params.dataScope != ''">
            <!-- 添加部门表关联 -->
            LEFT JOIN sys_dept d ON t.dept_id = d.dept_id
            LEFT JOIN sys_user u ON t.user_id = u.user_id

        </if>
        <where>  
            <if test="wrongWord != null  and wrongWord != ''"> and wrong_word like concat('%', #{wrongWord}, '%')</if>
            <if test="errorRule != null  and errorRule != ''"> and error_rule = #{errorRule}</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="status != null  and status != ''"> and t.status = #{status}</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>
            <choose>
                <when test="deptId != null and deptId == 100">
                    <!-- 教对部门可以看到所有数据 -->
                    and (t.dept_id = 100 or t.dept_id = 0)
                </when>
                <otherwise>
                    <!-- 数据范围过滤移动到where标签内 -->
                    <if test="params.dataScope != null and params.dataScope != ''">
                        ${params.dataScope}
                    </if>
                </otherwise>
            </choose>
        </where>
        order by t.time desc
    </select>

    <select id="selectTrsPolicyWordsList_jiaodui" parameterType="TrsPolicyWords" resultMap="TrsPolicyWordsResult">
        <include refid="selectTrsPolicyWordsVo"/>
        <where>
            <if test="wrongWord != null  and wrongWord != ''"> and wrong_word like concat('%', #{wrongWord}, '%')</if>
            <if test="errorRule != null  and errorRule != ''"> and error_rule = #{errorRule}</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="status != null  and status != ''"> and t.status = #{status}</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>
            and dept_id = 0
        </where>
        order by t.time desc
    </select>
    
    <select id="selectTrsPolicyWordsById" parameterType="Long" resultMap="TrsPolicyWordsResult">
        <include refid="selectTrsPolicyWordsVo"/>
        where id = #{id}
    </select>

    <insert id="insertTrsPolicyWords" parameterType="TrsPolicyWords" useGeneratedKeys="true" keyProperty="id">
        insert into trs_policy_words
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="wrongWord != null and wrongWord != ''">wrong_word,</if>
            <if test="errorRule != null">error_rule,</if>
            <if test="errorType != null">error_type,</if>
            <if test="errorGrade != null">error_grade,</if>
            <if test="dictType != null">dict_type,</if>
            <if test="flag != null">flag,</if>
            <if test="status != null">status,</if>
            <if test="effectstart != null">effectstart,</if>
            <if test="effectend != null">effectend,</if>
            <if test="accord != null">accord,</if>
            <if test="source != null">source,</if>
            <if test="linkurl != null">linkurl,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="time != null">time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="wrongWord != null and wrongWord != ''">#{wrongWord},</if>
            <if test="errorRule != null">#{errorRule},</if>
            <if test="errorType != null">#{errorType},</if>
            <if test="errorGrade != null">#{errorGrade},</if>
            <if test="dictType != null">#{dictType},</if>
            <if test="flag != null">#{flag},</if>
            <if test="status != null">#{status},</if>
            <if test="effectstart != null">#{effectstart},</if>
            <if test="effectend != null">#{effectend},</if>
            <if test="accord != null">#{accord},</if>
            <if test="source != null">#{source},</if>
            <if test="linkurl != null">#{linkurl},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="time != null">#{time},</if>
        </trim>
    </insert>

    <update id="updateTrsPolicyWords" parameterType="TrsPolicyWords">
        update trs_policy_words
        <trim prefix="SET" suffixOverrides=",">
            <if test="wrongWord != null and wrongWord != ''">wrong_word = #{wrongWord},</if>
            <if test="errorRule != null">error_rule = #{errorRule},</if>
            <if test="errorType != null">error_type = #{errorType},</if>
            <if test="errorGrade != null">error_grade = #{errorGrade},</if>
            <if test="dictType != null">dict_type = #{dictType},</if>
            <if test="flag != null">flag = #{flag},</if>
            <if test="status != null">status = #{status},</if>
            <if test="effectstart != null">effectstart = #{effectstart},</if>
            <if test="effectend != null">effectend = #{effectend},</if>
            <if test="accord != null">accord = #{accord},</if>
            <if test="source != null">source = #{source},</if>
            <if test="linkurl != null">linkurl = #{linkurl},</if>
            <if test="deptId != null">deptid = #{deptId},</if>
            <if test="time != null">time = #{time},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteTrsPolicyWordsById" parameterType="Long">
        delete from trs_policy_words where id = #{id}
    </delete>

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

</mapper>