<?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.KeyWordGroupMapper">
    <select id="listPageQueryKeyWordGroup" resultType="cn.roadm.bo.KeyWordGroupBO">
        SELECT *
        FROM sms_keyword_group
        <where>
            <if test="groupName != null and groupName != ''">
                groupName like CONCAT('%', #{groupName}, '%')
            </if>
            <if test="mobile_channel != null and mobile_channel != '' and mobile_channel != '-1'">
                and mobile_channel = #{mobile_channel}
            </if>
            <if test="unicom_channel != null and mobile_channel != '' and unicom_channel != '-1'">
                and unicom_channel = #{unicom_channel}
            </if>
            <if test="telecom_channel != null and mobile_channel != '' and telecom_channel != '-1'">
                and telecom_channel = #{telecom_channel}
            </if>
            and state = 0
        </where>
        order by createTime desc
    </select>

    <select id="getSignGroupAll" resultType="cn.roadm.bo.KeyWordGroupBO">
        SELECT *
        FROM sms_keyword_group
        <where>
<!--            获取全部签名策略组-->
            and state = 0 and type = 2
        </where>
        order by createTime desc
    </select>

    <select id="listPageQueryKeyWordGroupItems" resultType="cn.roadm.bo.KeyWordGroupItemBO">
        Select *
        from sms_keyword_item
        <where>
            groupId = #{groupId}
            <if test="keyword != null and keyword != ''">
                and keyword like CONCAT('%', #{keyword}, '%')
            </if>
            <if test="startTime != null and startTime != ''">
                and createTime &gt;= str_to_date(#{startTime}, '%Y-%m-%d')
            </if>
            <if test="endTime != null and endTime != ''">
                and createTime &lt;= DATE_ADD(str_to_date(#{endTime}, '%Y-%m-%d'), INTERVAL 1 DAY)
            </if>
        </where>
    </select>

    <select id="queryKeyWordGroup" resultType="cn.roadm.bo.KeyWordGroupBO">
        select *
        from sms_keyword_group
        where id = #{groupId}
          and state = 0
    </select>

    <insert id="addKeyWordGroup" parameterType="cn.roadm.bo.KeyWordGroupBO" useGeneratedKeys="true" keyProperty="id">
        insert into sms_keyword_group (groupName, createTime, unicom_channel, mobile_channel, telecom_channel,
                                       mobile_suffix, unicom_suffix, telecom_suffix, suffix_type, province_group_id,
                                       state, type, length)
        values (#{groupName}, #{createTime}, #{unicom_channel}, #{mobile_channel}, #{telecom_channel}, #{mobile_suffix},
                #{unicom_suffix}, #{telecom_suffix}, #{suffix_type}, #{province_group_id}, #{state}, #{type}, #{length})
    </insert>

    <insert id="addKeyWordItemList" parameterType="list">
        insert into sms_keyword_item (keyword, createTime, groupId)
                VALUES
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.keyword}, #{item.createTime}, #{item.groupId})
        </foreach>
    </insert>

    <update id="updateKeyWordGroup" parameterType="cn.roadm.bo.KeyWordGroupBO">
        update sms_keyword_group
        set groupName         = #{groupName},
            unicom_channel    = #{unicom_channel},
            mobile_channel    = #{mobile_channel},
            telecom_channel   = #{telecom_channel},
            mobile_suffix     = #{mobile_suffix},
            unicom_suffix     = #{unicom_suffix},
            telecom_suffix    = #{telecom_suffix},
            suffix_type       = #{suffix_type},
            province_group_id = #{province_group_id},
            type = #{type},
            length = #{length}
        where id = #{id}
    </update>

    <insert id="bitchInsertKeyWord">
    </insert>

    <update id="deleteGroup">
        update sms_keyword_group
        set state = 1
        where id = #{groupId}
    </update>

    <delete id="deleteItem">
        delete from sms_keyword_item
        where groupId = #{groupId}
          and id in
        <foreach collection="list" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="queryKeyWordGroupList" resultType="cn.roadm.bo.KeyWordGroupBO">
        SELECT * FROM sms_keyword_group where state = 0
    </select>
</mapper>