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

    <resultMap id="BaseResultMap" type="cn.roadm.bo.SmsPhonepoolGroup">
        <result column="id" property="id"/>
        <result column="city" property="city"/>
        <result column="data_type" property="dataType"/>
        <result column="name" property="name"/>
        <result column="taskid" property="taskid"/>
        <result column="file_name" property="fileName"/>
        <result column="isused" property="isused"/>
        <result column="operator_type" property="operatorType"/>
        <result column="createtime" property="createtime"/>
        <result column="channel_id" property="channelId"/>
    </resultMap>

    <resultMap id="CountResultMap" type="cn.roadm.bo.SmsPhonepoolGroup">
        <result column="count" property="count"/>
        <result column="city" property="city"/>
    </resultMap>

    <sql id="Base_Column_List">
        `id`,`name`,`city`,`data_type`,`taskid`,`isused`, `operator_type`,`createtime`
    </sql>

    <select id="listPageSmsPhonepoolGroup" resultMap="BaseResultMap" parameterType="cn.roadm.bo.SmsPhonepoolGroupInfo">
      select g.id,g.name,g.city,g.data_type,g.operator_type,t.name as file_name,g.isused,g.createtime,g.channel_id from sms_phonepool_group g left join sms_phonepool_task t
          on g.taskid = t.id
        <where>
            <if test="dataType != null">
                AND g.data_type = #{dataType}
            </if>
            <if test="@cn.roadm.util.StringUtil@isNotEmpty(name)">
                AND g.name = #{name}
            </if>
            <if test="isused != null">
                AND g.isused = #{isused}
            </if>
            <if test="@cn.roadm.util.StringUtil@isNotEmpty(city)">
                AND g.city = #{city}
            </if>
            <if test="channelId != null and channelId != ''">
                AND g.channel_id = #{channelId}
            </if>
        </where>
        order by createtime desc
    </select>

    <select id="getSmsPhonepoolGroupByCity" resultMap="CountResultMap" parameterType="string">
        select g.city ,count(1) as `count` from sms_phonepool_group g,sms_phonepool p
        where  g.isused = 1 and g.id = p.gid
        <if test="@cn.roadm.util.StringUtil@isNotEmpty(city)">
            AND g.city = #{city}
        </if>
        group by g.city
    </select>

    <update id="batchUpdateSmsPhonepoolGroupByStatus" >
        update sms_phonepool_group
        <set>
            <if test="isused != null">
                `isused` = #{isused},
            </if>
            <if test="statusCode != null">
                `data_type` = #{statusCode}
            </if>
        </set>
        where id in
        <foreach collection="groupIds" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </update>

    <update id="updateSmsPhonepoolGroupIsusedById">
        update sms_phonepool_group
        <set>
            <if test="isused != null">
                `isused` = #{isused},
            </if>
            <if test="operatorType != null">
                `operator_type` = #{operatorType}
            </if>
        </set>
        where `id` = #{groupId}
    </update>

    <select id="selectSmsPhonepoolGroup" parameterType="SmsPhonepoolGroup" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/> from sms_phonepool_group
        <where>
            <if test="dataType != null">
                AND `data_type` = #{dataType}
            </if>
            <if test="@cn.roadm.util.StringUtil@isNotEmpty(name)">
                AND `name` = #{name}
            </if>
            <if test="isused != null">
                AND `isused` = #{isused}
            </if>
            <if test="@cn.roadm.util.StringUtil@isNotEmpty(city)">
                AND `city` = #{city}
            </if>
            <if test="id != null">
                AND `id` = #{id}
            </if>
        </where>
    </select>

     <select id="getAllSmsPhonepoolGroup" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/> from sms_phonepool_group
    </select>

    <insert id="insert" parameterType="SmsPhonepoolGroup" useGeneratedKeys="true" keyProperty="id">
        insert into sms_phonepool_group
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test = "id != null">
                id,
            </if>
            <if test = "@cn.roadm.util.StringUtil@isNotEmpty(name)">
                name,
            </if>
            <if test = "@cn.roadm.util.StringUtil@isNotEmpty(city)">
                city,
            </if>
            <if test = "dataType != null">
                data_type,
            </if>
            <if test = "@cn.roadm.util.StringUtil@isNotEmpty(taskid)">
                taskid,
            </if>
            <if test = "isused != null">
                isused,
            </if>
            <if test = "createtime != null">
                createtime
            </if>
            <if test = "operatorType != null">
                operator_type
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test = "id != null">
                #{id, jdbcType=INTEGER},
            </if>
            <if test = "@cn.roadm.util.StringUtil@isNotEmpty(name)">
                #{name, jdbcType=VARCHAR},
            </if>
            <if test = "@cn.roadm.util.StringUtil@isNotEmpty(city)">
                #{city, jdbcType=VARCHAR},
            </if>
            <if test = "dataType != null">
                #{dataType, jdbcType=INTEGER},
            </if>
            <if test = "@cn.roadm.util.StringUtil@isNotEmpty(taskid)">
                #{taskid, jdbcType=VARCHAR},
            </if>
            <if test = "isused != null">
                #{isused, jdbcType=INTEGER},
            </if>
            <if test = "createtime != null">
                #{createtime, jdbcType=TIMESTAMP}
            </if>
            <if test = "operatorType != null">
                #{operatorType, jdbcType=INTEGER},
            </if>
        </trim>
    </insert>

    <select id="batchSelectSmsPhonepoolGroupById" resultMap="BaseResultMap" parameterType="list">
        select <include refid="Base_Column_List"/> from sms_phonepool_group
        where isused = 1 and id in
        <foreach collection="groupIds" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

    <delete id="deleteById" parameterType="integer">
        delete from sms_phonepool_group where id = #{id}
    </delete>
</mapper>