<?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.roadm.mapper.WhiteWordMapper">

    <resultMap type="WhiteWord" id="whiteWordResultMap">
        <id column="id" property="id"/>
        <result column="content" property="content"/>
        <result column="status" property="status"/>
        <result column="type" property="type"/>
        <result column="create_time" property="createtime"/>
        <result column="remark" property="remark"/>
        <association property="whiteWordType" javaType="WhiteWordType">
            <id column="type_id" property="id"/>
            <result column="type_name" property="name"/>
            <result column="type_description" property="description"/>
        </association>
        <association property="userInfo" column="createid"
                     javaType="UserInfo">
            <id column="createid" property="user_id"/>
            <result column="user_name" property="userName"/>
        </association>
    </resultMap>

    <resultMap type="WhiteWord" id="whiteWordMap">
        <result column="content" property="content"/>
    </resultMap>

    <resultMap type="WhiteWord" id="exportWordResultMap">
        <result column="content" property="content"/>
        <result column="create_time" property="createtime"/>
        <result column="type_name" property="typeName"/>
        <result column="remark" property="remark"/>
    </resultMap>

    <resultMap type="WhiteWordType" id="whiteWordTypeResultMap">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="description" property="description"/>
        <result column="create_time" property="createTime"/>
        <result column="status" property="status"/>
    </resultMap>

    <select id="exportWhiteWord" parameterType="WhiteWord"
            resultType="Map">
        select
            wd.content,
            date_format(wd.create_time,'%Y-%m-%d %H:%i:%S') create_time,
            wt.name as type_name,
            case when wd.status = 0 then '生效' else '失效' end as status,
            wd.remark
        from sms_white_word_detail wd
        left join sms_white_word_type wt on wd.type = wt.id
        where 1=1
        <if test="content!=null and content!=''">
            and wd.content like CONCAT('%',#{content},'%')
        </if>
        <if test="type!=null and type!='-1'">
            and wd.type = #{type}
        </if>
        <if test="spId!=null and spId!='-1'">
            and exists (
                select 1 from sms_white_link_sp wls 
                where wls.white_id = wd.type and wls.sp_id = #{spId}
            )
        </if>
        order by wd.id desc
    </select>

    <select id="listAllWhiteWord" fetchSize="5000" parameterType="WhiteWord"
            resultMap="whiteWordMap" useCache="true">
        select wd.content 
        from sms_white_word_detail wd
        where wd.status = 0
        <if test="type!=null and type>-1">
            and wd.type = #{type}
        </if>
    </select>

    <select id="listPageWhiteWord" parameterType="WhiteWord"
            resultMap="whiteWordResultMap">
        select
            wd.id,
            wd.content,
            wd.create_time,
            wd.status,
            wd.type,
            wd.remark,
            wt.id as type_id,
            wt.name as type_name,
            wt.description as type_description
        from sms_white_word_detail wd
        left join sms_white_word_type wt on wd.type = wt.id
        where 1=1
        <if test="content!=null and content!=''">
            and wd.content like CONCAT('%',#{content},'%')
        </if>
        <if test="type!=null and type!='-1'">
            and wd.type = #{type}
        </if>
        <if test="spId!=null and spId!='-1'">
            and exists (
                select 1 from sms_white_link_sp wls 
                where wls.white_id = wd.type and wls.sp_id = #{spId}
            )
        </if>
        order by wd.id desc
    </select>

    <select id="getWhiteWordById" parameterType="int"
            resultMap="whiteWordResultMap">
        select 
            wd.id,
            wd.content,
            wd.create_time,
            wd.status,
            wd.type,
            wd.remark,
            wt.id as type_id,
            wt.name as type_name,
            wt.description as type_description
        from sms_white_word_detail wd
        left join sms_white_word_type wt on wd.type = wt.id
        where wd.id = #{word_id}
    </select>

    <select id="getWordCountByName" parameterType="WhiteWord"
            resultType="int">
        select count(1)
        from sms_white_word_detail wd
        where wd.content = #{content, jdbcType=VARCHAR}
          and wd.type = #{type}
    </select>

    <update id="updateWhiteWordById" parameterType="WhiteWord">
        update sms_white_word_detail
        set content = #{content},
            status = #{status},
            type = #{type},
            remark = #{remark}
        where id = #{id}
    </update>

    <insert id="insertWhiteWord" useGeneratedKeys="true" keyProperty="id"
            parameterType="WhiteWord">
        insert into sms_white_word_detail
        (content, status, create_time, type, remark)
        values (#{content,jdbcType=VARCHAR}, #{status}, sysdate(), #{type}, #{remark})
    </insert>

    <insert id="addWhiteWordBatch" useGeneratedKeys="true" keyProperty="id" parameterType="java.util.List">
        insert into sms_white_word_detail
        (content, status, create_time, type, remark)
        values
        <foreach collection="list" item="whiteword" separator=",">
            (#{whiteword.content,jdbcType=VARCHAR}, #{whiteword.status}, sysdate(), #{whiteword.type}, #{whiteword.remark})
        </foreach>
    </insert>

    <delete id="deleteWhiteWordById" parameterType="int">
        delete from sms_white_word_detail
        where id = #{word_id}
    </delete>

    <select id="listAllWhiteWordType" parameterType="WhiteWordType"
            resultMap="whiteWordTypeResultMap">
        select 
            id, name, description, create_time, status
        from sms_white_word_type
        where 1=1
        <if test="status!=null and status!='-1'">
            and status = #{status}
        </if>
        <if test="id!=null and id!='-1'">
            and id = #{id}
        </if>
        order by id desc
    </select>

    <insert id="insertWhiteWordType" useGeneratedKeys="true" keyProperty="id" parameterType="WhiteWordType">
        insert into sms_white_word_type
        (name, description, create_time, status)
        values (#{name,jdbcType=VARCHAR}, #{description,jdbcType=VARCHAR}, sysdate(), #{status,jdbcType=INTEGER})
    </insert>

    <update id="updateWhiteWordType" parameterType="WhiteWordType">
        update sms_white_word_type
        set name = #{name},
            description = #{description},
            status = #{status}
        where id = #{id}
    </update>

    <select id="listPageWhiteWordType" parameterType="WhiteWordType"
            resultMap="whiteWordTypeResultMap">
        select 
            id, name, description, create_time, status
        from sms_white_word_type
        where 1=1
        <if test="name!=null and name!=''">
            and name like CONCAT('%',#{name},'%')
        </if>
        <if test="status!=null and status!='-1'">
            and status = #{status}
        </if>
        order by id desc
    </select>

    <select id="getWordTypeCountByName" parameterType="WhiteWordType"
            resultType="int">
        select count(1)
        from sms_white_word_type
        where name = #{name}
          and id != #{id}
    </select>

    <delete id="deleteWhiteWordTypeById" parameterType="int">
        delete from sms_white_word_type
        where id = #{id}
    </delete>
    
    <!-- 新增SP与白词库关联的操作 -->
    <insert id="insertWhiteWordLinkSp" parameterType="map">
        insert into sms_white_link_sp
        (sp_id, white_id)
        values (#{spId}, #{whiteId})
    </insert>
    
    <delete id="deleteWhiteWordLinkSp" parameterType="map">
        delete from sms_white_link_sp
        where sp_id = #{spId} and white_id = #{whiteId}
    </delete>
    
    <select id="getSpLinkedWhiteTypes" parameterType="int" resultType="int">
        select white_id
        from sms_white_link_sp
        where sp_id = #{spId}
    </select>
    
    <select id="getWhiteTypeLinkedSps" parameterType="int" resultType="int">
        select sp_id
        from sms_white_link_sp
        where white_id = #{whiteId}
    </select>
    
    <delete id="deleteWhiteWordTypeSpAll" parameterType="int">
        delete from sms_white_link_sp
        where white_id = #{whiteId}
    </delete>
</mapper> 