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

    <sql id="Base_Column_List">
        `id`,`network`,`name`,`platform`,`biz`,`strategy`,`channels`,`weight`,`overstocks`,`modifyTime`
    </sql>
    <select id="listPageInterChannelGroup" parameterType="cn.roadm.bo.SpChannelGroupInfo" resultType="InterChannelGroup">
        select <include refid="Base_Column_List"/> from inter_channel_group g
        <where>
            g.status = '1'
            <if test="network != null and network != ''">
                and g.network = #{network}
            </if>
            <if test="name != null and name!=''">
                and g.name like CONCAT('%', #{name}, '%')
            </if>
            <if test="channels!=null and channels!=''">
                and CONCAT(',',channels, ',') like CONCAT('%,',#{channels}, ',%')
            </if>
        </where>
        order by g.id desc
    </select>

    <select id="getInterChannelGroupAll" parameterType="cn.roadm.bo.SpChannelGroupInfo" resultType="InterChannelGroup">
        select <include refid="Base_Column_List"/> from inter_channel_group g
        <where>
            g.status = '1'
            <if test="network != null and network != ''">
                and g.network = #{network}
            </if>
            <if test="name != null and name!=''">
                and g.name like CONCAT('%', #{name}, '%')
            </if>
            <if test="channels!=null and channels!=''">
                and CONCAT(',',channels, ',') like CONCAT('%,',#{channels}, ',%')
            </if>
        </where>

    </select>

    <insert id="insertInterChannelGroup" useGeneratedKeys="true" keyProperty="id" parameterType="InterChannelGroup">
        insert into inter_channel_group
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test = "id != null">
                id,
            </if>
            <if test = "network != '' and network!=null">
                network,
            </if>
            <if test = "name != null and name!= ''">
                `name`,
            </if>
            <if test = "platform != null and platform != ''">
                platform,
            </if>
            <if test = "biz != null and biz != ''">
                biz,
            </if>
            <if test = "strategy != null and strategy != ''">
                strategy,
            </if>
            <if test = "channels != null and channels != ''">
                channels,
            </if>
            <if test = "weight != null and weight != ''">
                weight,
            </if>
            <if test = "overstocks != null and overstocks != ''">
                overstocks,
            </if>
            modifyTime,
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test = "id != null">
                #{id, jdbcType=INTEGER},
            </if>
            <if test = "network != null">
                #{network, network=VARCHAR},
            </if>
            <if test = "name != null">
                #{name, jdbcType=VARCHAR},
            </if>
            <if test = "platform != null">
                #{platform, jdbcType=VARCHAR},
            </if>
            <if test = "biz != null">
                #{biz, jdbcType=VARCHAR},
            </if>
            <if test = "strategy != null">
                #{strategy, jdbcType=VARCHAR},
            </if>
            <if test = "channels != null">
                #{channels, jdbcType=VARCHAR},
            </if>
            <if test = "weight != null">
                #{weight, jdbcType=VARCHAR},
            </if>
            <if test = "overstocks != null and overstocks != ''">
                #{overstocks, jdbcType=VARCHAR},
            </if>
            sysdate(),
        </trim>
    </insert>

    <update id="updateInterChannelGroup" parameterType="InterChannelGroup">
        update inter_channel_group
        <set>
            <if test = "name != null and name != ''">
                `name` = #{name, jdbcType=VARCHAR},
            </if>
            <if test = "network != null and network != ''">
                network = #{network, jdbcType=VARCHAR},
            </if>
            <if test = "platform != null and platform != ''">
                platform = #{platform, jdbcType=VARCHAR},
            </if>
            <if test = "biz != null and biz != ''">
                biz = #{biz, jdbcType=VARCHAR},
            </if>
            <if test = "strategy != null and strategy != ''">
                strategy = #{strategy, jdbcType=VARCHAR},
            </if>
            <if test = "status != null and status != ''">
                status = #{status, jdbcType=INTEGER},
            </if>
            <if test = "channels != null and status != ''">
                channels = #{channels, jdbcType=VARCHAR},
            </if>
            <if test = "weight != null and weight != ''">
                weight = #{weight, jdbcType=VARCHAR},
            </if>
            <if test = "overstocks != null and overstocks != ''">
                overstocks = #{overstocks, jdbcType=VARCHAR},
            </if>
            modifyTime = sysdate(),
        </set>
        where id = #{id, jdbcType=INTEGER}
    </update>

    <update id="delInterGroup" parameterType="int">
        update inter_channel_group set status=0 where id=#{id}
    </update>

    <select id="seleteById" resultType="InterChannelGroup" parameterType="int">
        select <include refid="Base_Column_List"/> from inter_channel_group g
        where
        g.status = '1' and g.id = #{id}
    </select>

    <select id="listPageCountryGroup" resultType="cn.roadm.bo.SmsCountryGroup">
        select id,name,DATE_FORMAT(createtime, '%Y-%m-%d %H:%i:%s') createtime,`status`,`desc` from sms_country_group
        <where>
            status=1
            <if test="name!=null and name!=''">
                and name like CONCAT('%',#{name},'%')
            </if>
        </where>
    </select>

    <select id="getCountryGroupConfig" resultType="cn.roadm.bo.SmsCountryGroup">
        select sc.id,sc.group_id,sc.interchannel,sc.country,sg.name from sms_country_group_config sc left join sms_country_group sg on sc.group_id = sg.id  where group_id=#{group_id}
    </select>

    <select id="getCountryGroupById" resultType="cn.roadm.bo.SmsCountryGroup">
        select id,name,DATE_FORMAT(createtime, '%Y-%m-%d %H:%i:%s') createtime,`status` from sms_country_group
        <where>
            status=1 and id=#{id}
        </where>
    </select>

    <select id="getCountryGroupAll" resultType="cn.roadm.bo.SmsCountryGroup">
        select id,name,DATE_FORMAT(createtime, '%Y-%m-%d %H:%i:%s') createtime,`status` from sms_country_group where status=1
    </select>

    <update id="delCountryGroup" parameterType="cn.roadm.bo.SmsCountryGroup">
        update sms_country_group set status=0 where id=#{id}
    </update>

    <insert id="insertChannelCountryGroup" useGeneratedKeys="true" keyProperty="groupId" parameterType="cn.roadm.bo.SpChannelCountryGroupData">
        insert into sms_country_group(name, createtime, status) values (#{groupName}, now(), 1)
    </insert>

    <delete id="deleteChannelCountryGroup" parameterType="cn.roadm.bo.SpChannelCountryGroupData">
        delete from sms_country_group_config where group_id = #{groupId}
    </delete>

    <insert id="insertChannelCountryGroupData" useGeneratedKeys="true" keyProperty="id" parameterType="cn.roadm.bo.SpChannelCountryConfig">
        insert into sms_country_group_config (group_id,interchannel,country) values
        <foreach collection="list" item="item" separator=",">
            (#{item.groupId}, #{item.interchannel}, #{item.country})
        </foreach>
    </insert>

    <update id="updateSpChannelCountryGroupName" parameterType="cn.roadm.bo.SpChannelCountryGroupData">
        update sms_country_group set name=#{groupName} where id=#{groupId}
    </update>
</mapper>
