<?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.admin.mapper.ChannelSensitiveWordMapper">

    <select id="listPageChannelSensitiveWord" resultType="cn.roadm.admin.dto.ChannelSensitiveWordDTO">
        select scw.id,
               scw.content,
               scw.createid as createId,
               date_format(scw.create_time, '%Y-%m-%d %H:%i:%s') as createTime,
               scw.channelid as channelId,
               us.user_name as userName
        from sms_channel_word scw
                 left join sms_user_info us on scw.createid = us.id
        <where>
            <if test="reqVO.content != null and reqVO.content != ''">
                and scw.content like concat('%', #{reqVO.content}, '%')
            </if>
            <if test="reqVO.channelId != null">
                and scw.channelid = #{reqVO.channelId}
            </if>
        </where>
        order by scw.create_time desc
    </select>

    <select id="getChannelSensitiveWordById" resultType="cn.roadm.admin.dto.ChannelSensitiveWordDTO">
        select scw.id,
               scw.content,
               scw.createid as createId,
               date_format(scw.create_time, '%Y-%m-%d %H:%i:%s') as createTime,
               scw.channelid as channelId,
               us.user_name as userName
        from sms_channel_word scw
                 left join sms_user_info us on scw.createid = us.id
        where scw.id = #{id}
        limit 1
    </select>

    <select id="countByContentAndChannel" resultType="int">
        select count(1)
        from sms_channel_word
        where content = #{content}
          and channelid = #{channelId}
        <if test="id != null">
            and id != #{id}
        </if>
    </select>

    <insert id="insertChannelSensitiveWord">
        insert into sms_channel_word(content, createid, create_time, channelid)
        values (#{reqVO.content}, #{createId}, now(), #{reqVO.channelId})
    </insert>

    <update id="updateChannelSensitiveWord">
        update sms_channel_word
        set content   = #{content},
            channelid = #{channelId}
        where id = #{id}
    </update>

    <delete id="deleteChannelSensitiveWord">
        delete
        from sms_channel_word
        where id = #{id}
    </delete>

    <delete id="batchDeleteChannelSensitiveWord">
        delete
        from sms_channel_word
        where id in
        <foreach collection="ids" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </delete>

    <select id="listChannelSensitiveWordForExport" resultType="cn.roadm.admin.dto.ChannelSensitiveWordDTO">
        select scw.id,
               scw.content,
               scw.createid as createId,
               date_format(scw.create_time, '%Y-%m-%d %H:%i:%s') as createTime,
               scw.channelid as channelId,
               us.user_name as userName
        from sms_channel_word scw
                 left join sms_user_info us on scw.createid = us.id
        <where>
            <if test="reqVO.content != null and reqVO.content != ''">
                and scw.content like concat('%', #{reqVO.content}, '%')
            </if>
            <if test="reqVO.channelId != null">
                and scw.channelid = #{reqVO.channelId}
            </if>
        </where>
        order by scw.create_time desc
    </select>

    <select id="listAllChannelSensitiveWordContent" resultType="string">
        select content
        from sms_channel_word
    </select>

</mapper>
