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

    <resultMap type="BlackObject" id="blackObjectResultMap">
        <id column="id" property="id" />
        <id column="mdn" property="mdn" />
        <id column="spid" property="sp_id" />
        <result column="createtime" property="createtime" />
        <result column="descption" property="descption" />
        <result column="blacklist_group_name" property="blacklist_group_name" />
    </resultMap>
    
    <resultMap type="BlackObject" id="exportWordResultMap">
        <id column="id" property="id" />
        <id column="mdn" property="mdn" />
        <id column="spid" property="sp_id" />
        <result column="createtime" property="createtime" />
        <result column="descption" property="descption" />
    </resultMap>
    
    
    <select id="exportWord" parameterType="BlackObject"
        resultType="Map">
        select ot.id,ot.mdn,ot.spid,IFNULL(ot.descption,' ')descption ,date_format(ot.createtime,'%Y-%m-%d %H:%i:%S') createtime,ot.grade  from sms_black_object ot where 1=1
        <if test="mdn!=null and mdn!=''">
            and ot.mdn=#{mdn}
        </if>
         <if test="sp_id!=null and sp_id>0">
            and ot.spid=#{sp_id}
        </if>
         <if test="grade!=null and grade!=-1">
            and ot.grade=#{grade}
         </if>
        <if test="endId != null">
            and ot.id &lt; #{endId}
        </if>
        order by id desc
        <if test="limit != null and limit > 0">
            limit #{limit}
        </if>
    </select>
    <select id="listBlackObjectCount" parameterType="BlackObject" resultType="int">

        select count(0) from  sms_black_object ot where 1=1
 		<if test="mdn!=null and mdn!=''">
            and ot.mdn=#{mdn}
        </if>
       
    </select>

    <select id="listPageBlackObject" parameterType="BlackObject"
        resultMap="blackObjectResultMap">
        select ot.id,ot.mdn,ot.spid,ot.createtime,ot.descption,ot.grade
        from sms_black_object ot
        where  1=1
        <if test="mdn!=null and mdn!=''">
            and ot.mdn=#{mdn} 
        </if>
         <if test="sp_id!=null and sp_id>0">
            and ot.spid=#{sp_id}
        </if>
         <if test="grade!=null and grade!=-1">
            and ot.grade=#{grade}
        </if>
        order by ot.id desc
       <!--  limit #{page.currentResult},#{page.showCount} -->
    </select>

    <select id="listBlackPhone" parameterType="BlackObject" resultMap="blackObjectResultMap">
		    select
			st.blacklist_group_name
	        from sms_black_phone_type st
	        where st.id=#{grade}
	        limit 1
	</select>

    <select id="getBlackPhoneList" resultMap="blackObjectResultMap">
        select st.id,
            st.blacklist_group_name
        from sms_black_phone_type st
    </select>

    <select id="getBlackObjectById" parameterType="int"
        resultMap="blackObjectResultMap">
            select ot.id,ot.mdn,ot.spid,ot.createtime,ot.descption,ot.grade from sms_black_object ot where
         ot.id=#{id}
    </select>
    <select id="getBlackObjectCountByName" parameterType="BlackObject"
        resultType="int">
        select count(1) from sms_black_object sw
        where
        sw.mdn=#{mdn}
        <if test="sp_id!=null and sp_id!=''">
            and sw.spid =#{sp_id}
        </if>
        <if test="grade!=null and grade!= ''">
             and  grade=#{grade}
        </if>
    </select>
    <update id="updateBlackObjectById" parameterType="BlackObject">
        update
        sms_black_object
        <set>
            <if test="sp_ids !=null and sp_ids != '' ">
                spid=#{sp_ids},
            </if>
            <if test="mdn!=null and mdn!= ''">
                mdn=#{mdn},
            </if>
            <if test="descption!=null and descption!= ''">
                descption=#{descption},
            </if>
            <if test="grade!=null and grade!= ''">
                grade=#{grade},
            </if>
            createtime = now()
        </set>
        where
           id=#{id}
    </update>


    <insert id="insertBlackObject"  useGeneratedKeys="true" keyProperty="id"  parameterType="BlackObject">
        insert into sms_black_object
        (MDN,SPID,DESCPTION,CREATETIME,GRADE) values
        (#{mdn,jdbcType=VARCHAR},#{sp_id},#{descption,jdbcType=VARCHAR},sysdate(),#{grade})
    </insert>

 <insert  id="addBlackMdnBatch"  useGeneratedKeys="true" keyProperty="id" parameterType="ArrayList">  
    <!--  <selectKey resultType="int" order="BEFORE" keyProperty="id">
            select sms_black_object_id.nextval from dual
    </selectKey> -->
     insert into sms_black_object (MDN,SPID,DESCPTION,CREATETIME,GRADE)
     VALUES
     <foreach collection="list" item="black"  index="index"  separator="," >
         (#{black.mdn,jdbcType=VARCHAR},#{black.sp_id},#{black.descption},sysdate(),#{black.grade})
     </foreach>
</insert>  


    <delete id="deleteBlackObjectById" parameterType="int">
        delete from sms_black_object where id=#{id}
    </delete>

    <select id="getBlackPhoneTypeList" resultType="cn.roadm.bo.BlackPhoneType">
        select id, blacklist_group_name from sms_black_phone_type where type = 3
    </select>

    <select id="getBlackPhoneSpeedData" resultType="cn.roadm.bo.BlackSpeedEntity">
        select monitor_time as monitorTime, call_speed as speed, table_name as tableId, b.blacklist_group_name as tableName
        from sms_blackphone_speed a left join sms_black_phone_type b on a.table_name = b.ID
        <where>
            <if test="tableId != null and tableId != ''">
                a.table_name = #{tableId}
            </if>
            <if test="startTime != null and startTime != ''">
                and a.create_time &gt;=str_to_date(#{startTime},'%Y-%m-%d %H:%i:%s')
            </if>
            <if test="endTime != null and endTime != ''">
                and a.create_time &lt;=str_to_date(#{endTime},'%Y-%m-%d %H:%i:%s')
            </if>
        </where>
        order by a.create_time
    </select>
</mapper>
