<?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.RouteNumberMapper">
    <resultMap type="RoutingNumber" id="routeNumberResultMap">
        <id column="id" property="id" />
        <result column="mdn" property="mdn" />
        <result column="operator" property="operator" />
        <result column="beginSendTime" property="beginSendTime" />
        <result column="otype" property="otype" />
        <result column="Sp_saler" property="Sp_saler" />
        <result column="Id" property="Id" />
        <result column="new_otype" property="new_otype" />
        <result column="type" property="type" />
    </resultMap>

    <!--fuction:携号转网路由库管理 and 携号转网路由库管理 导出    developer : 李相  BEGIN -->
    <select id="searchPageRoutingNumber" parameterType="RoutingNumber" resultMap="routeNumberResultMap">
        select
        srnm.id,
        srnm.mdn ,
       	srnm.otype,
       	srnm.new_otype,
        date_format(srnm.createtime, '%Y-%m-%d') beginSendTime,
        srnm.type
        from sms_routing_number_management srnm
        <where>
            <if test="mdn!=null and mdn!=''">
                and srnm.mdn like CONCAT('%',#{mdn},'%')
            </if>
            <if test="otype!=null and otype!=''">
                and srnm.otype=#{otype}
            </if>
            <if test="createTime!=null and createTime!=''">
                and srnm.createtime&gt;=str_to_date(#{createTime},'%Y-%m-%d')
            </if>
            <if test="createTime1!=null and createTime1!=''">
                and
                srnm.createtime&lt;DATE_ADD(str_to_date(#{createTime1},'%Y-%m-%d'),INTERVAL 1 DAY)
            </if>
        </where>
        group by srnm.mdn, srnm.otype, date_format(srnm.createtime, '%Y-%m-%d'),srnm.id
        order by srnm.createtime desc
    </select>

    <select id="getRouteNumberCountByName" parameterType="RoutingNumber"
            resultType="int">
        select count(1) from sms_routing_number_management srnm
        where
        srnm.mdn=#{mdn}
        <if test="id!=null and id!=''">
            and srnm.id =#{id}
        </if>
    </select>

    <update id="updateRouteNumberById" parameterType="RoutingNumber">
        update
        sms_routing_number_management
        <set>
            <if test="id!=null and id!= '' ">
                id=#{id},
            </if>
            <if test="mdn!=null and mdn!= ''">
                mdn=#{mdn},
            </if>
            <if test="otype!=null and otype!= ''">
                otype=#{otype},
            </if>
            <if test="new_otype!=null and new_otype!= ''">
                new_otype=#{new_otype},
            </if>
            createtime = now()
        </set>
        where
        id=#{id}
    </update>

    <insert id="insertRouteNumber"  useGeneratedKeys="true" keyProperty="id"  parameterType="RoutingNumber">
        insert into sms_routing_number_management
            (MDN,id,otype,CREATETIME,new_otype) values
        (#{mdn,jdbcType=VARCHAR},#{id},#{otype},sysdate(),#{new_otype})
    </insert>

    <select id="exportWord" parameterType="RoutingNumber"
            resultType="Map">
        select
        srnm.id,
        srnm.mdn ,
        (case when srnm.otype='1' then '联通'
        when srnm.otype='2' then '移动'
        when srnm.otype='3' then '电信' end) s_otype,
        (case when srnm.new_otype='1' then '联通'
        when srnm.new_otype='2' then '移动'
        when srnm.new_otype='3' then '电信' end)
        s_new_otype,
        srnm.type,
        date_format(srnm.createtime, '%Y-%m-%d') beginSendTime
        from sms_routing_number_management srnm
        <where>
            <if test="mdn!=null and mdn!=''">
                and srnm.mdn like CONCAT('%',#{mdn},'%')
            </if>
            <if test="createTime!=null and createTime!=''">
                and srnm.createtime&gt;=str_to_date(#{createTime},'%Y-%m-%d')
            </if>
            <if test="createTime1!=null and createTime1!=''">
                and srnm.createtime&lt;DATE_ADD(str_to_date(#{createTime1},'%Y-%m-%d'),INTERVAL 1 DAY)
            </if>
            <if test="type!=null and type!=''">
                and srnm.type=#{type}
            </if>
            <if test="endId != null">
                and srnm.id &lt;  #{endId}
            </if>
        </where>
        group by srnm.mdn, srnm.otype, date_format(srnm.createtime, '%Y-%m-%d'),srnm.id
        order by srnm.id desc
        <if test="page.showCount != null ">
            limit #{page.showCount}
        </if>
    </select>

    <select id="getCount" parameterType="RoutingNumber" resultType="long">
        select count(ss.id) from (
        select
        srnm.id
        from sms_routing_number_management srnm
        <where>
            <if test="mdn!=null and mdn!=''">
                and srnm.mdn like CONCAT('%',#{mdn},'%')
            </if>
            <if test="createTime!=null and createTime!=''">
                and srnm.createtime&gt;=str_to_date(#{createTime},'%Y-%m-%d')
            </if>
            <if test="createTime1!=null and createTime1!=''">
                and
                srnm.createtime&lt;DATE_ADD(str_to_date(#{createTime1},'%Y-%m-%d'),INTERVAL 1 DAY)
            </if>
        </where>
        group by srnm.mdn, srnm.otype, date_format(srnm.createtime, '%Y-%m-%d'),srnm.id
            ) ss
    </select>

    <delete id="deleteRouteNumberById" parameterType="int">
        delete from sms_routing_number_management where id=#{id}
    </delete>

    <insert  id="addRouteNumberBatch"  useGeneratedKeys="true" keyProperty="id" parameterType="ArrayList">
        replace into sms_routing_number_management (mdn,otype,new_otype,createtime)
        select A.* from(
        <foreach collection="list" item="white"  index="index"  separator="UNION"  >
            SELECT #{white.mdn,jdbcType=VARCHAR},#{white.otype,jdbcType=INTEGER},#{white.new_otype},sysdate() FROM DUAL
        </foreach>
        ) A
    </insert>
    <!--fuction:携号转网路由库管理 and 携号转网路由库管理 导出    developer : 李相  END -->

    <select id="getRoutingNumberCount" resultType="java.lang.Long">
        select count(1) from sms_routing_number_management srnm
        <where>
            <if test="mdn!=null and mdn!=''">
                and srnm.mdn like CONCAT('%',#{mdn},'%')
            </if>
            <if test="otype!=null and otype!=''">
                and srnm.otype=#{otype}
            </if>
            <if test="createTime!=null and createTime!=''">
                and srnm.createtime&gt;=str_to_date(#{createTime},'%Y-%m-%d')
            </if>
            <if test="createTime1!=null and createTime1!=''">
                and
                srnm.createtime&lt;DATE_ADD(str_to_date(#{createTime1},'%Y-%m-%d'),INTERVAL 1 DAY)
            </if>
        </where>
    </select>
</mapper>