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

    <resultMap type="WhiteCity" id="whiteObjectResultMap">
        <id column="id" property="id" />
        <id column="city" property="city" />
        <id column="spid" property="sp_id" />
        <result column="createtime" property="createtime" />
        <result column="descption" property="descption" />
    </resultMap>
    
    <resultMap type="WhiteCity" id="exportWordResultMap">
        <id column="id" property="id" />
        <id column="city" property="city" />
        <id column="spid" property="sp_id" />
        <result column="createtime" property="createtime" />
        <result column="descption" property="descption" />
    </resultMap>
    
   <resultMap type="TaskInfo" id="taskResultMap">
        <id column="id" property="task_id" />
        <result column="content" property="content" />
        <result column="createtime" property="createTime" />
        <result column="total" property="total" />
        <result column="allnumber" property="allnumber" />
        <result column="mobilenumber" property="mobilenumber" />
        <result column="unicomnumber" property="unicomnumber" />
        <result column="telecomnumer" property="telecomnumber" />
        <result column="succ" property="succ" />
        <result column="fail" property="fail" />
        <result column="arrive_succ" property="arrive_succ" />
        <result column="arrive_fail" property="arrive_fail" />
        <result column="task_type" property="task_type" />
        <result column="status" property="status" />
        <result column="endtime" property="endTime" />
        <result column="send_type" property="send_type" />
        <result column="sp_id" property="sp_id" />
        <result column="unRecMsgnum" property="unRecMsgnum" />
        <result column="sp_name" property="sp_name" />
        <result column="time" property="time" />

    </resultMap>
 
     <resultMap type="TaskDetailInfo" id="TaskDetailInfoResultMap">
        <id column="id" property="id" />
        <result column="taskid" property="taskid" />
        <result column="res_result" property="res_result" />
        <result column="callmdn" property="callmdn" />
        <result column="res_content" property="res_content" />
        <result column="sp_id" property="sp_id" />
        <result column="send_time" property="send_time" />
        <result column="create_time" property="createtime" />
        <result column="status" property="status" />
        <result column="result" property="result" />
        <result column="stat_desc" property="stat_desc" />
        <result column="arrive_time" property="arrive_time" />
        <result column="sp_name" property="sp_name" />
        <result column="pa_dec" property="pa_dsc" />
    </resultMap>  
        
    <select id="exportDeductTask" parameterType="TaskDetailInfo"
        resultType="Map">
         select st.id,
        st.taskid,
        st.sp_id,
        sc.CHANNEL_NAME as pa_dec,
        sp.sp_name,
        st.mdn,
        st.callmdn,
        st.send_time,
        st.channelid,
        st.status,
        st.res_content,
        st.create_time
        from sms_deduct_arrive st
        LEFT JOIN sms_sp_info sp
        ON st.sp_id = sp.id
        LEFT JOIN sms_send_channel_info sc
        ON st.CHANNELID = sc.id
      
        <where>
            <if test="mdn!=null and mdn!=''">
                and st.mdn=#{mdn}
            </if>
		
            <if test="sp_id!=null and sp_id>0">
                and st.sp_id=#{sp_id}
            </if>
            <if test="taskid!=null and taskid>0">
                and st.taskid=#{taskid}
            </if>
            <if test="channelid!=null and channelid!=''">
                and st.channelid=#{channelid}
            </if>
             <if test="sign!=null and sign!=''">
                and st.sign=#{sign}
            </if>
		<if test="sp_name!=null and sp_name!=''">
			and sp.sp_name like CONCAT('%',#{sp_name},'%')
		</if> 
		 <if test="sp_saler!=null and sp_saler!=''">
            and sp.sp_saler =#{sp_saler}
        </if>           
            
            <if test="createtime!=null and createtime!=''">
                and st.create_time&gt;=str_to_date(#{createtime},'%Y-%m-%d %H:%i:%s')
            </if>

            <if test="endtime!=null and endtime!=''">
                and st.create_time&lt;DATE_ADD(str_to_date(#{endtime},'%Y-%m-%d %H:%i:%s'),INTERVAL 5 SECOND)
            </if>
            
            <if test="endId != null">
                and st.id &lt;  #{endId}
        </if>
        </where>
        
        
        order by st.id desc
     <if test="exportExcelSize != null ">
            limit #{exportExcelSize}
        </if>
    </select>
  
  	<select id="ExportDeductTaskCount" parameterType="TaskDetailInfo"
		resultType="Long">
		 select
        count(1)
        from sms_deduct_arrive st
        LEFT JOIN sms_sp_info sp
        ON st.sp_id = sp.id
        LEFT JOIN sms_send_channel_info sc
        ON st.CHANNELID = sc.id
      
        <where>
            <if test="mdn!=null and mdn!=''">
                and st.mdn=#{mdn}
            </if>
		
            <if test="sp_id!=null and sp_id>0">
                and st.sp_id=#{sp_id}
            </if>
            <if test="taskid!=null and taskid>0">
                and st.taskid=#{taskid}
            </if>
            <if test="channelid!=null and channelid!=''">
                and st.channelid=#{channelid}
            </if>
             <if test="sign!=null and sign!=''">
                and st.sign=#{sign}
            </if>
		<if test="sp_name!=null and sp_name!=''">
			and sp.sp_name like CONCAT('%',#{sp_name},'%')
		</if> 
		 <if test="sp_saler!=null and sp_saler!=''">
            and sp.sp_saler =#{sp_saler}
        </if>           
            
            <if test="createtime!=null and createtime!=''">
                and st.create_time&gt;=str_to_date(#{createtime},'%Y-%m-%d %H:%i:%s')
            </if>

            <if test="endtime!=null and endtime!=''">
                and st.create_time&lt;DATE_ADD(str_to_date(#{endtime},'%Y-%m-%d %H:%i:%s'),INTERVAL 5 SECOND)
            </if>
        </where>
        order by st.id desc
	</select>
	
	  
    <select id="listPageDeductTask" parameterType="TaskDetailInfo"
        resultMap="TaskDetailInfoResultMap">
        
        
        select st.id,
        st.taskid,
        st.sp_id,
        sc.CHANNEL_NAME as pa_dec,
        sp.sp_name,
        st.mdn,
        st.callmdn,
        st.send_time,
        st.channelid,
        st.status,
        st.res_content,
        st.create_time,
        st.STAT_DESC,
        st.RESULT,
        st.RES_RESULT,
        st.SOURCE,
        st.RES_DESC
        from sms_deduct_arrive st
        LEFT JOIN sms_sp_info sp
        ON st.sp_id = sp.id
        LEFT JOIN sms_send_channel_info sc
        ON st.CHANNELID = sc.id
      
        <where>
            <if test="mdn!=null and mdn!=''">
                and st.mdn=#{mdn}
            </if>
		
            <if test="sp_id!=null and sp_id>0">
                and st.sp_id=#{sp_id}
            </if>
            <if test="taskid!=null and taskid>0">
                and st.taskid=#{taskid}
            </if>
            <if test="channelid!=null and channelid!=''">
                and st.channelid=#{channelid}
            </if>
             <if test="sign!=null and sign!=''">
                and st.sign=#{sign}
            </if>
            <if test="status !=null and status >=0 ">
                and st.status=#{status}
            </if>
            
		<if test="sp_name!=null and sp_name!=''">
			and sp.sp_name like CONCAT('%',#{sp_name},'%')
		</if> 
		 <if test="sp_saler!=null and sp_saler!=''">
            and sp.sp_saler =#{sp_saler}
        </if>           
            
            <if test="createtime!=null and createtime!=''">
                and st.create_time&gt;=str_to_date(#{createtime},'%Y-%m-%d %H:%i:%s')
            </if>

            <if test="endtime!=null and endtime!=''">
                and st.create_time&lt;DATE_ADD(str_to_date(#{endtime},'%Y-%m-%d %H:%i:%s'),INTERVAL 5 SECOND)
            </if>
        </where>
        order by st.id desc
    </select>
    
    <update id="updateDeductTaskByTaskid" parameterType="Long">
        update
        sms_deduct_arrive
        set status =1
        where id=#{id} and IFNULL(status,0) in (0,5,6)
    </update>
    
    <update id="updateDeductTaskBySearch" parameterType="TaskDetailInfo">
        update
        sms_deduct_arrive st
        set status =1
        
          <where>
            <if test="mdn!=null and mdn!=''">
                and st.mdn=#{mdn}
            </if>
		
            <if test="sp_id!=null and sp_id>0">
                and st.sp_id=#{sp_id}
            </if>
            <if test="taskid!=null and taskid>0">
                and st.taskid=#{taskid}
            </if>
            <if test="channelid!=null and channelid!=''">
                and st.channelid=#{channelid}
            </if>
             <if test="sign!=null and sign!=''">
                and st.sign=#{sign}
            </if>
	         
            
            <if test="createtime!=null and createtime!=''">
                and st.create_time&gt;=str_to_date(#{createtime},'%Y-%m-%d %H:%i:%s')
            </if>

            <if test="endtime!=null and endtime!=''">
                and st.create_time&lt;DATE_ADD(str_to_date(#{endtime},'%Y-%m-%d %H:%i:%s'),INTERVAL 5 SECOND)
            </if>
        </where>
        and IFNULL(status,0) in (0,5,6)
    </update>
      
    
    <select id="getWhiteObjectById" parameterType="int"
        resultMap="whiteObjectResultMap">
            select ot.id,ot.city,ot.spid,ot.createtime,ot.descption  from sms_deduct_white_city ot where 
         ot.id=#{id}
    </select>
    <select id="getWhiteObjectCountByName" parameterType="WhiteCity"
        resultType="int">
        select count(1) from sms_deduct_white_city sw
        where
        sw.city=#{city}
        <if test="sp_id!=null and sp_id!=''">
            and sw.spid =#{sp_id}
        </if>
    </select>
    <update id="updateWhiteObjectById" parameterType="WhiteCity">
        update
        sms_deduct_white_city
        <set>
            <if test="sp_ids !=null and sp_ids != '' ">
                spid=#{sp_ids},
            </if>
            <if test="city!=null and city!= ''">
                city=#{city},
            </if>
            <if test="descption!=null and descption!= ''">
                descption=#{descption},
            </if>
            createtime = now()
        </set>
        where
           id=#{id}
    </update>


    <insert id="insertWhiteObject"  useGeneratedKeys="true" keyProperty="id"  parameterType="WhiteCity">
        insert into sms_deduct_white_city
        (CITY,SPID,DESCPTION,CREATETIME) values
        (#{city,jdbcType=VARCHAR},#{sp_id},#{descption,jdbcType=VARCHAR},sysdate())
    </insert>

 <insert  id="addWhiteMdnBatch"  useGeneratedKeys="true" keyProperty="id" parameterType="ArrayList">  
    <!--  <selectKey resultType="int" order="BEFORE" keyProperty="id">
            select sms_white_object_id.nextval from dual
    </selectKey> -->
    insert into sms_deduct_white_city (CITY,SPID,DESCPTION,CREATETIME)
     select A.* from(
    <foreach collection="list" item="white"  index="index"  separator="UNION"  >  
        SELECT #{white.city},#{white.sp_id},#{white.descption,jdbcType=VARCHAR},sysdate() FROM DUAL
    </foreach>  
      ) A
</insert>  


    <delete id="deleteWhiteObjectById" parameterType="int">
        delete from sms_deduct_white_city where id=#{id}
    </delete>
    
<update id="updateDeductTaskBySearchFail" parameterType="TaskDetailInfo">
        update
        sms_deduct_arrive st
        set status =3
        
          <where>
            <if test="mdn!=null and mdn!=''">
                and st.mdn=#{mdn}
            </if>
		
            <if test="sp_id!=null and sp_id>0">
                and st.sp_id=#{sp_id}
            </if>
            <if test="taskid!=null and taskid>0">
                and st.taskid=#{taskid}
            </if>
            <if test="channelid!=null and channelid!=''">
                and st.channelid=#{channelid}
            </if>
             <if test="sign!=null and sign!=''">
                and st.sign=#{sign}
            </if>
	         
            
            <if test="createtime!=null and createtime!=''">
                and st.create_time&gt;=str_to_date(#{createtime},'%Y-%m-%d %H:%i:%s')
            </if>

            <if test="endtime!=null and endtime!=''">
                and st.create_time&lt;DATE_ADD(str_to_date(#{endtime},'%Y-%m-%d %H:%i:%s'),INTERVAL 5 SECOND)
            </if>
        </where>
        and IFNULL(status,0)=0
        <if test="limit != null">
            limit #{limit}
        </if>
</update>
    
    
    <update id="updateDeductTaskFailByTaskid" parameterType="Long">
        update
        sms_deduct_arrive
        set status =3
        where id=#{id} and IFNULL(status,0)  = 0 
    </update>

    <update id="updateDeductTaskBySearchSuccess" parameterType="TaskDetailInfo">
        update
        sms_deduct_arrive st
        set status = 6
        <where>
            <if test="mdn!=null and mdn!=''">
                and st.mdn=#{mdn}
            </if>
            <if test="sp_id!=null and sp_id>0">
                and st.sp_id=#{sp_id}
            </if>
            <if test="taskid!=null and taskid>0">
                and st.taskid=#{taskid}
            </if>
            <if test="channelid!=null and channelid!=''">
                and st.channelid=#{channelid}
            </if>
            <if test="sign!=null and sign!=''">
                and st.sign=#{sign}
            </if>
            <if test="createtime!=null and createtime!=''">
                and st.create_time&gt;=str_to_date(#{createtime},'%Y-%m-%d %H:%i:%s')
            </if>
            <if test="endtime!=null and endtime!=''">
                and st.create_time&lt;DATE_ADD(str_to_date(#{endtime},'%Y-%m-%d %H:%i:%s'),INTERVAL 5 SECOND)
            </if>
        </where>
        and IFNULL(status,0) = 0
        <if test="limit != null">
            limit #{limit}
        </if>
    </update>
</mapper>