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

    <select id="listPageSmsIpStrategy" resultType="cn.roadm.bo.SmsIpStrategyBO">
        SELECT s.id,
               s.sp_id AS spId,
               s.template,
               s.channel_id AS channelId,
               s.sign,
               s.factory,
               s.channel_factory_config AS channelFactoryConfig,
               s.create_time AS createTime,
               s.update_time AS updateTime,
               sp.sp_name AS spName,
               CASE 
                   WHEN s.channel_id LIKE 'g%' THEN CONCAT(cg.name, '(组)')
                   ELSE c.channel_name
               END AS channelName
        FROM sms_ip_strategy s
        LEFT JOIN sms_sp_info sp ON s.sp_id = sp.id
        LEFT JOIN sms_send_channel_info c ON s.channel_id = c.id AND s.channel_id NOT LIKE 'g%'
        LEFT JOIN sms_channel_group cg ON CONCAT('g', cg.id) = s.channel_id AND s.channel_id LIKE 'g%'
        <where>
            <if test="spId != null and spId != ''">
                AND s.sp_id like CONCAT('%', #{spId}, '%')
            </if>
            <if test="template != null and template != ''">
                AND s.template like CONCAT('%', #{template}, '%')
            </if>
            <if test="channelId != null and channelId != ''">
                AND s.channel_id = #{channelId}
            </if>
            <if test="sign != null and sign != ''">
                AND s.sign like CONCAT('%', #{sign}, '%')
            </if>
            <if test="factory != null and factory != ''">
                AND s.factory like CONCAT('%', #{factory}, '%')
            </if>
            <if test="createTimeStart != null and createTimeStart != ''">
                AND s.create_time >= #{createTimeStart}
            </if>
            <if test="createTimeEnd != null and createTimeEnd != ''">
                AND s.create_time &lt;= #{createTimeEnd}
            </if>
        </where>
        ORDER BY s.create_time DESC
    </select>

    <select id="querySmsIpStrategyById" resultType="cn.roadm.bo.SmsIpStrategyBO">
        SELECT id,
               sp_id AS spId,
               template,
               channel_id AS channelId,
               sign,
               factory,
               channel_factory_config AS channelFactoryConfig,
               create_time AS createTime,
               update_time AS updateTime
        FROM sms_ip_strategy
        WHERE id = #{id}
    </select>

    <insert id="addSmsIpStrategy" parameterType="cn.roadm.bo.SmsIpStrategyBO" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO sms_ip_strategy (
            sp_id, 
            template, 
            channel_id, 
            sign, 
            factory, 
            channel_factory_config,
            create_time, 
            update_time
        )
        VALUES (
            #{spId,jdbcType=VARCHAR}, 
            #{template,jdbcType=LONGVARCHAR}, 
            #{channelId,jdbcType=INTEGER}, 
            #{sign,jdbcType=LONGVARCHAR}, 
            #{factory,jdbcType=VARCHAR}, 
            #{channelFactoryConfig,jdbcType=LONGVARCHAR},
            #{createTime,jdbcType=TIMESTAMP}, 
            #{updateTime,jdbcType=TIMESTAMP}
        )
    </insert>

    <update id="updateSmsIpStrategy" parameterType="cn.roadm.bo.SmsIpStrategyBO">
        UPDATE sms_ip_strategy
        SET sp_id = #{spId,jdbcType=VARCHAR},
            template = #{template,jdbcType=LONGVARCHAR},
            channel_id = #{channelId,jdbcType=INTEGER},
            sign = #{sign,jdbcType=LONGVARCHAR},
            factory = #{factory,jdbcType=VARCHAR},
            channel_factory_config = #{channelFactoryConfig,jdbcType=LONGVARCHAR},
            update_time = #{updateTime,jdbcType=TIMESTAMP}
        WHERE id = #{id,jdbcType=INTEGER}
    </update>

    <delete id="deleteSmsIpStrategy">
        DELETE FROM sms_ip_strategy
        WHERE id = #{id}
    </delete>

    <delete id="batchDeleteSmsIpStrategy">
        DELETE FROM sms_ip_strategy
        WHERE id IN
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="querySmsIpStrategyList" resultType="cn.roadm.bo.SmsIpStrategyBO">
        SELECT s.id,
               case when s.sp_id = '0' then '全部企业' else CONCAT(s.sp_id, '-', sp.sp_name) end as spId,
               s.template,
               CASE 
                   WHEN s.channel_id LIKE 'g%' THEN CONCAT(s.channel_id, '-', cg.name, '(组)')
                   ELSE CONCAT(s.channel_id, '-', c.channel_name)
               END AS channelName,
               s.sign,
               s.factory,
               s.channel_factory_config AS channelFactoryConfig,
               s.create_time AS createTime,
               s.update_time AS updateTime
        FROM sms_ip_strategy s
        LEFT JOIN sms_sp_info sp ON s.sp_id = sp.id
        LEFT JOIN sms_send_channel_info c ON s.channel_id = c.id AND s.channel_id NOT LIKE 'g%'
        LEFT JOIN sms_channel_group cg ON CONCAT('g', cg.id) = s.channel_id AND s.channel_id LIKE 'g%'
        <where>
            <if test="spId != null and spId > 0 ">
                AND s.sp_id like CONCAT('%', #{spId}, '%')
            </if>
            <if test="template != null and template != ''">
                AND s.template like CONCAT('%', #{template}, '%')
            </if>
            <if test="channelId != null">
                AND s.channel_id = #{channelId}
            </if>
            <if test="sign != null and sign != ''">
                AND s.sign like CONCAT('%', #{sign}, '%')
            </if>
            <if test="factory != null and factory != ''">
                AND s.factory like CONCAT('%', #{factory}, '%')
            </if>
            <if test="createTimeStart != null and createTimeStart != ''">
                AND s.create_time >= #{createTimeStart}
            </if>
            <if test="createTimeEnd != null and createTimeEnd != ''">
                AND s.create_time &lt;= #{createTimeEnd}
            </if>
        </where>
        ORDER BY s.create_time DESC
    </select>

    <select id="selectSmsIpStrategyBySpId" resultType="cn.roadm.bo.SmsIpStrategyBO">
        select sp_id as spId, template, channel_id as channelId, sign, factory, create_time as createTime, channel_factory_config as channelFactoryConfig from sms_ip_strategy where sp_id = #{spid}
    </select>

    <insert id="insertBatchSmsIpStrategy">
        insert into sms_ip_strategy (sp_id, template, channel_id, sign, factory, create_time, channel_factory_config) VALUES
            <foreach collection="list" item="config" separator=",">
                (#{config.spId}, #{config.template}, #{config.channelId}, #{config.sign}, #{config.factory}, #{config.createTime}, #{config.channelFactoryConfig})
            </foreach>
    </insert>
</mapper>