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

	<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" />
        <result column="difference" property="difference" />

    </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="res_desc" property="res_desc" />
        <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" />
 		<result column="num" property="num" />
    </resultMap>
    
     <resultMap type="TaskInfo" id="taskInfoMap">
        <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="telecomnumber" property="telecomnumber" />
        <result column="task_type" property="task_type" />
        <result column="endtime" property="endTime" />
        <result column="send_type" property="send_type" />
        <result column="sp_id" property="sp_id" />
        <result column="sp_audit_type" property="sp_audit_type" />
    </resultMap>

    <resultMap type="cn.roadm.bo.ReplyDetailInfo" id="ReplyDetailInfoResultMap">
        <id column="id" property="id" />
        <result column="sp_id" property="sp_id" />
        <result column="mobile" property="mobile" />
        <result column="callmdn" property="callmdn" />
        <result column="content" property="content" />
        <result column="reply_time" property="replyTime" />
        <result column="status" property="flag" />
        <result column="createtime" property="createtime" />
        <result column="subport" property="subport" />

    </resultMap>

    <select id="listPageSpTaskTotal" parameterType="Map"
        resultMap="taskResultMap">
        select sp.id sp_id,
        sp.sp_name,
        sum(tk.allnumber) allnumber,
        sum(tk.succ) succ,
        sum(IFNULL(tk.fail,0)) fail,
        sum(tk.arrive_succ) arrive_succ,
        <!-- sum(tk.allnumber-asa.total-tk.arrive_succ-tk.arrive_fail) unRecMsgnum, -->
        sum(tk.arrive_fail) arrive_fail
        from sms_sp_info sp left join  sms_sp_summary tk on tk.spid = sp.id 
        
        where 1=1 and sp.id and sp.sa_id=#{saId}
     
        <if test="sp_name!=null and sp_name!=''">
            and sp.sp_name like CONCAT('%',#{sp_name},'%') 
        </if>
        <if test="sp_id!=null and sp_id>0">
            and tk.spid=#{sp_id}
        </if>
        <if test="sp_saler!=null and sp_saler!=''">
            and sp.sp_saler =#{sp_saler}
        </if>
        <if test="sp_type!=null and sp_type>0">
            and sp.sp_type =#{sp_type}
        </if>
        <if test="createTime!=null and createTime!=''">
            and tk.stat_time&gt;=str_to_date(#{createTime},'%Y-%m-%d')
        </if>
        <if test="createTime1!=null and createTime1!=''">
            and
            tk.stat_time&lt;DATE_ADD(str_to_date(#{createTime1},'%Y-%m-%d'),INTERVAL 1 DAY)
        </if>
        group by sp.id, sp.sp_name
        order by sum(tk.allnumber) desc

    </select>
    
    <select id="waitAuditCount" parameterType="TaskInfo"
        resultType="Integer">
        
        select sum(a.allnumber) wait_audit  from sms_send_audit_task a 
        where a.status = 1 
        and a.send_type in('1000','10000')
        and a.sp_id=#{sp_id}
        <if test="createTime!=null and createTime!=''">
            and a.createtime&gt;=str_to_date(#{createTime},'%Y-%m-%d')
        </if>
        <if test="createTime1!=null and createTime1!=''">
            and
            a.createtime&lt;DATE_ADD(str_to_date(#{createTime1},'%Y-%m-%d'),INTERVAL 1 DAY)
        </if>
        
    </select>
    <select id="waitApiAuditCount" parameterType="TaskInfo"
        resultType="Integer">
        
       select sum(api.allnumber) wait_audit  from sms_api_audit_task api 
        where api.status = 1 
        and api.send_type in('1000','10000')
        and api.sp_id=#{sp_id}
        <if test="createTime!=null and createTime!=''">
            and api.createtime&gt;=str_to_date(#{createTime},'%Y-%m-%d')
        </if>
        <if test="createTime1!=null and createTime1!=''">
            and
            api.createtime&lt;DATE_ADD(str_to_date(#{createTime1},'%Y-%m-%d'),INTERVAL 1 DAY)
        </if>
     </select>
     
     <select id="crontabCount" parameterType="TaskInfo"
        resultType="Integer">
        
       select sum(st.allnumber) crontab  from sms_crontab_send b 
       left join sms_send_task st on st.id = b.taskid
        where b.status =0 and b.sp_id=#{sp_id} and st.status in (0,1)
        <if test="createTime!=null and createTime!=''">
            and b.createtime&gt;=str_to_date(#{createTime},'%Y-%m-%d')
        </if>
        <if test="createTime1!=null and createTime1!=''">
            and
            b.createtime&lt;DATE_ADD(str_to_date(#{createTime1},'%Y-%m-%d'),INTERVAL 1 DAY)
        </if>
        
     </select>
     
     <select id="listSpTaskTotalCount" parameterType="TaskInfo"
        resultMap="taskResultMap">
        select 
        sum(IFNULL(tk.allnumber,0)) allnumber,
        sum(IFNULL(tk.succ,0)) succ,
        sum(IFNULL(tk.fail,0)) fail,
        sum(IFNULL(tk.arrive_succ,0)) arrive_succ,
        <!-- sum(IFNULL(tk.allnumber-tk.arrive_succ-tk.arrive_fail,0)) unRecMsgnum, -->
        sum(IFNULL(tk.allnumber,0))-sum(IFNULL(tk.arrive_succ,0))-sum(IFNULL(tk.arrive_fail,0)) unRecMsgnum,
        sum(IFNULL(tk.arrive_fail,0)) arrive_fail
        from sms_sp_summary tk, sms_sp_info sp
        where tk.spid = sp.id and sp.sa_id=#{saId}
  
         <if test="sp_name!=null and sp_name!=''">
            and sp.sp_name like CONCAT('%',#{sp_name},'%') 
        </if>
        <if test="sp_id!=null and sp_id>0">
            and tk.spid=#{sp_id}
        </if>
        <if test="sp_saler!=null and sp_saler!=''">
            and sp.sp_saler =#{sp_saler}
        </if>
        <if test="sp_type!=null and sp_type>0">
            and sp.sp_type =#{sp_type}
        </if>   
        
        <if test="createTime!=null and createTime!=''">
            and tk.stat_time&gt;=str_to_date(#{createTime},'%Y-%m-%d')
        </if>
        <if test="createTime1!=null and createTime1!=''">
            and
            tk.stat_time&lt;DATE_ADD(str_to_date(#{createTime1},'%Y-%m-%d'),INTERVAL 1 DAY)
        </if>
        
    </select>  
    
    <select id="listPageSpProvinceTaskTotal"
		parameterType="TaskInfo" resultMap="taskResultMap">
		select sp.id sp_id,
		sp.sp_name,
		tk.province,
		vspp.sp_all_submit,
		sum(tk.submit_succ) succ,
		sum(IFNULL(tk.submit_fail,0)) fail,
		sum(tk.arrive_succ) arrive_succ,
		sum(IFNULL(tk.submit_succ,0))+sum(IFNULL(tk.submit_fail,0))-sum(IFNULL(tk.arrive_succ,0))-sum(IFNULL(tk.arrive_fail,0)) unRecMsgnum,
<!-- sum(IFNULL(tk.submit_succ+tk.submit_fail-tk.arrive_succ-tk.arrive_fail,0)) unRecMsgnum, -->
		sum(tk.arrive_fail) arrive_fail
		from sms_sp_province_summary tk, sms_sp_info sp,
		(
		select sum(spp.submit_succ) sp_all_submit,spp.spid from
		sms_sp_province_summary spp, sms_sp_info spinfo
		where spp.spid = spinfo.id and spinfo.sa_id=#{saId}
		<if test="sp_name!=null and sp_name!=''">
			and spinfo.sp_name like CONCAT('%',#{sp_name},'%')
		</if>
		<if test="sp_id!=null and sp_id>0">
			and spp.spid=#{sp_id}
		</if>
		<if test="createTime!=null and createTime!=''">
			and spp.stat_time&gt;=str_to_date(#{createTime},'%Y-%m-%d')
		</if>
		<if test="createTime1!=null and createTime1!=''">
			and
			spp.stat_time&lt;DATE_ADD(str_to_date(#{createTime1},'%Y-%m-%d'),INTERVAL 1 DAY)
		</if>
		 group by spp.spid
		 ) vspp
		where tk.spid = sp.id and tk.spid = vspp.spid and sp.sa_id=#{saId}


		<if test="sp_name!=null and sp_name!=''">
			and sp.sp_name like CONCAT('%',#{sp_name},'%')
		</if>
		<if test="sp_id!=null and sp_id>0">
			and tk.spid=#{sp_id}
		</if>
 		<if test="sp_saler!=null and sp_saler!=''">
            and sp.sp_saler =#{sp_saler}
        </if>
		<if test="province!=null and province!=''">
			and tk.province =#{province}
		</if>
		<if test="createTime!=null and createTime!=''">
			and tk.stat_time&gt;=str_to_date(#{createTime},'%Y-%m-%d')
		</if>
		<if test="createTime1!=null and createTime1!=''">
			and
			tk.stat_time&lt;DATE_ADD(str_to_date(#{createTime1},'%Y-%m-%d'),INTERVAL
			1 DAY)
		</if>
		group by sp.id, sp.sp_name,tk.province
		order by succ desc

	</select>
	
	<!--fuction:企业发送统计(运营商) and 企业发送统计(运营商) 导出    developer : 李相  BEGIN -->
    <select id="listPageCarrierTotalList" parameterType="TaskInfo" resultMap="taskResultMap">
        select sp.id sp_id,
        sp.sp_name,
        tk.otype,
        vspp.sp_all_submit,
        sum(tk.submit_succ) succ,
        sum(IFNULL(tk.submit_fail,0)) fail,
        sum(tk.arrive_succ) arrive_succ,
      <!--   sum(IFNULL(tk.submit_succ+tk.submit_fail-tk.arrive_succ-tk.arrive_fail,0)) unRecMsgnum, -->
        sum(IFNULL(tk.submit_succ,0))+sum(IFNULL(tk.submit_fail,0))-sum(IFNULL(tk.arrive_succ,0))-sum(IFNULL(tk.arrive_fail,0)) unRecMsgnum,
        sum(tk.arrive_fail) arrive_fail
        from sms_sp_province_summary tk, sms_sp_info sp,
        
        (
		select sum(spp.submit_succ) sp_all_submit,spp.spid from
		sms_sp_province_summary spp, sms_sp_info spinfo
		where spp.spid = spinfo.id and spinfo.sa_id=#{saId}
		<if test="sp_name!=null and sp_name!=''">
			and spinfo.sp_name like CONCAT('%',#{sp_name},'%')
		</if>
		<if test="sp_saler!=null and sp_saler!=''">
            and spinfo.sp_saler =#{sp_saler}
        </if>
		<if test="sp_id!=null and sp_id>0">
			and spp.spid=#{sp_id}
		</if>
		<if test="createTime!=null and createTime!=''">
			and spp.stat_time&gt;=str_to_date(#{createTime},'%Y-%m-%d')
		</if>
		<if test="createTime1!=null and createTime1!=''">
			and
			spp.stat_time&lt;DATE_ADD(str_to_date(#{createTime1},'%Y-%m-%d'),INTERVAL 1 DAY)
		</if>
		 group by spp.spid
		
		) vspp
        
        where tk.spid = sp.id
			and tk.spid = vspp.spid and sp.sa_id=#{saId}
        <if test="sp_name!=null and sp_name!=''">
            and sp.sp_name like CONCAT('%',#{sp_name},'%')
        </if>
        <if test="sp_id!=null and sp_id>0">
            and tk.spid=#{sp_id}
        </if>
        <if test="sp_saler!=null and sp_saler!=''">
            and sp.sp_saler =#{sp_saler}
        </if>
        <if test="otype!=null and otype>0 ">
            and tk.otype =#{otype}
        </if>
        <if test="createTime!=null and createTime!=''">
            and tk.stat_time&gt;=str_to_date(#{createTime},'%Y-%m-%d')
        </if>
        <if test="createTime1!=null and createTime1!=''">
            and
            tk.stat_time&lt;DATE_ADD(str_to_date(#{createTime1},'%Y-%m-%d'),INTERVAL 1 DAY)
        </if>
        group by sp.id, sp.sp_name, tk.otype
        order by succ desc
    </select>
    
    
    <select id="listSpPcoTaskTotal"
		parameterType="TaskInfo" resultMap="taskResultMap">
		select 
		sum(IFNULL(tk.submit_succ,0)) succ,
		sum(IFNULL(tk.submit_fail,0)) fail,
		sum(IFNULL(tk.arrive_succ,0)) arrive_succ,
		sum(IFNULL(tk.arrive_fail,0)) arrive_fail,
		sum(IFNULL(tk.submit_succ,0))+sum(IFNULL(tk.submit_fail,0))-sum(IFNULL(tk.arrive_succ,0))-sum(IFNULL(tk.arrive_fail,0)) unRecMsgnum
		<!-- sum(IFNULL(tk.submit_succ+tk.submit_fail-tk.arrive_succ-tk.arrive_fail,0)) unRecMsgnum -->
		from sms_sp_province_summary tk, sms_sp_info sp,sms_send_channel_info ssci
		
		where tk.spid = sp.id  and tk.channelid = ssci.id and sp.sa_id=#{saId}
		<if test="sp_name!=null and sp_name!=''">
			and sp.sp_name like CONCAT('%',#{sp_name},'%')
		</if>
		<if test="sp_id!=null and sp_id>0">
			and tk.spid=#{sp_id}
		</if>
 		<if test="sp_saler!=null and sp_saler!=''">
            and sp.sp_saler =#{sp_saler}
        </if>
		<if test="queryType == 'channel' and channelid !=null and channelid >= 0">
			and ssci.id =#{channelid} 
		</if>
		<if test="queryType == 'operator' and otype!=null and otype>0 ">
            and tk.otype =#{otype}
        </if>
        <if test="queryType == 'province' and province!=null and province!=''">
			and tk.province =#{province}
		</if>
		<if test="createTime!=null and createTime!=''">
			and tk.stat_time&gt;=str_to_date(#{createTime},'%Y-%m-%d')
		</if>
		<if test="createTime1!=null and createTime1!=''">
			and
			tk.stat_time&lt;DATE_ADD(str_to_date(#{createTime1},'%Y-%m-%d'),INTERVAL
			1 DAY)
		</if>

	</select>
	<select id="exportSpProvinceTotalList" parameterType="TaskInfo"
		resultType="Map">
		select sp.id sp_id,
		tk.province,
		CONCAT(Round(sum(tk.submit_succ)/vspp.sp_all_submit*100,2),'%') sp_province_percent,
		CONCAT(Round(sum(tk.arrive_succ)/sum(tk.submit_succ)*100,2),'%') sp_succ_percent,
		sp.sp_name,
		sum(IFNULL(tk.submit_succ,0))+sum(IFNULL(tk.submit_fail,0))-sum(IFNULL(tk.arrive_succ,0))-sum(IFNULL(tk.arrive_fail,0)) unRecMsgnum,
		sum(tk.submit_succ) succ,
		sum(IFNULL(tk.submit_fail,0)) fail,
		sum(tk.arrive_succ) arrive_succ,
		sum(tk.arrive_fail) arrive_fail
		from sms_sp_province_summary tk, sms_sp_info sp,
		(
		select sum(spp.submit_succ) sp_all_submit,spp.spid from
		sms_sp_province_summary spp, sms_sp_info spinfo
		where spp.spid = spinfo.id and spinfo.sa_id=#{saId}
		<if test="sp_name!=null and sp_name!=''">
			and spinfo.sp_name like CONCAT('%',#{sp_name},'%')
		</if>
		<if test="sp_id!=null and sp_id>0">
			and spp.spid=#{sp_id}
		</if>
		<if test="createTime!=null and createTime!=''">
			and spp.stat_time&gt;=str_to_date(#{createTime},'%Y-%m-%d')
		</if>
		<if test="createTime1!=null and createTime1!=''">
			and
			spp.stat_time&lt;DATE_ADD(str_to_date(#{createTime1},'%Y-%m-%d'),INTERVAL 1 DAY)
		</if>
		 group by spp.spid
		) vspp
		where tk.spid = sp.id and sp.sa_id=#{saId}
			and tk.spid = vspp.spid
		<if test="sp_name!=null and sp_name!=''">
			and sp.sp_name like CONCAT('%',#{sp_name},'%')
		</if>
		<if test="sp_id!=null and sp_id>0">
			and tk.spid=#{sp_id}
		</if>
		<if test="sp_saler!=null and sp_saler!=''">
			and sp.sp_saler =#{sp_saler}
		</if>
		<if test="province!=null and province!='' ">
			and tk.province =#{province}
		</if>
		<if test="createTime!=null and createTime!=''">
			and tk.stat_time&gt;=str_to_date(#{createTime},'%Y-%m-%d')
		</if>
		<if test="createTime1!=null and createTime1!=''">
			and
			tk.stat_time&lt;DATE_ADD(str_to_date(#{createTime1},'%Y-%m-%d'),INTERVAL
			1 DAY)
		</if>
		group by sp.id, sp.sp_name,tk.province
		order by sp.id,succ desc
	</select>
	
	 
    <select id="exportCarrierTotalList" parameterType="TaskInfo" resultType="Map">
        select sp.id sp_id,
        sp.sp_name,
        tk.otype,
        CONCAT(Round(sum(tk.submit_succ)/vspp.sp_all_submit*100,2),'%') sp_otype_percent,
		CONCAT(Round(sum(tk.arrive_succ)/sum(tk.submit_succ)*100,2),'%') sp_succ_percent,
        sum(tk.submit_succ) succ,
        sum(IFNULL(tk.submit_succ,0))+sum(IFNULL(tk.submit_fail,0))-sum(IFNULL(tk.arrive_succ,0))-sum(IFNULL(tk.arrive_fail,0)) unRecMsgnum,
        sum(IFNULL(tk.submit_fail,0)) fail,
        sum(tk.arrive_succ) arrive_succ,
        sum(tk.arrive_fail) arrive_fail
        from sms_sp_province_summary tk, sms_sp_info sp,
        
         (
		
		select sum(spp.submit_succ) sp_all_submit,spp.spid from
		sms_sp_province_summary spp, sms_sp_info spinfo
		where spp.spid = spinfo.id and spinfo.sa_id=#{saId}
		<if test="sp_name!=null and sp_name!=''">
			and spinfo.sp_name like CONCAT('%',#{sp_name},'%')
		</if>
		<if test="sp_id!=null and sp_id>0">
			and spp.spid=#{sp_id}
		</if>
		<if test="createTime!=null and createTime!=''">
			and spp.stat_time&gt;=str_to_date(#{createTime},'%Y-%m-%d')
		</if>
		<if test="createTime1!=null and createTime1!=''">
			and
			spp.stat_time&lt;DATE_ADD(str_to_date(#{createTime1},'%Y-%m-%d'),INTERVAL 1 DAY)
		</if>
		 group by spp.spid
		
		) vspp
        
        where tk.spid = sp.id and sp.sa_id=#{saId}
			and tk.spid = vspp.spid
        <if test="sp_name!=null and sp_name!=''">
            and sp.sp_name like CONCAT('%',#{sp_name},'%')
        </if>
        <if test="sp_id!=null and sp_id>0">
            and tk.spid=#{sp_id}
        </if>
		<if test="sp_saler!=null and sp_saler!=''">
            and sp.sp_saler =#{sp_saler}
        </if>
        <if test=" otype != null and otype>0">
            and tk.otype=#{otype}
        </if>

        <if test="createTime!=null and createTime!=''">
            and tk.stat_time&gt;=str_to_date(#{createTime},'%Y-%m-%d')
        </if>
        <if test="createTime1!=null and createTime1!=''">
            and
            tk.stat_time&lt;DATE_ADD(str_to_date(#{createTime1},'%Y-%m-%d'),INTERVAL 1 DAY)
        </if>
        group by sp.id, sp.sp_name, tk.otype
        order by sp.id,succ desc
    </select>
    
    
    <select id="exportSpTotalList" parameterType="TaskInfo"
        resultType="Map">
        select sp.id sp_id,
        sp.sp_name,
        sum(tk.allnumber) allnumber,
        sum(tk.succ) succ,
        sum(IFNULL(tk.fail,0)) fail,
        sum(tk.arrive_succ) arrive_succ,
        
        sum(IFNULL(tk.succ,0))+sum(IFNULL(tk.fail,0))-sum(IFNULL(tk.arrive_succ,0))-sum(IFNULL(tk.arrive_fail,0)) unRecMsgnum,
        <!-- asa.wait_audit+scs.crontab total, -->
        sum(tk.arrive_fail) arrive_fail
        from sms_sp_info sp left join sms_sp_summary tk on tk.spid = sp.id
        where 1=1 and sp.sa_id=#{saId}
        <if test="sp_name!=null and sp_name!=''">
            and sp.sp_name like CONCAT('%',#{sp_name},'%') 
        </if>
        <if test="sp_id!=null and sp_id>0">
            and tk.spid=#{sp_id}
        </if>
        <if test="sp_saler!=null and sp_saler!=''">
            and sp.sp_saler =#{sp_saler}
        </if>
        <if test="sp_type!=null and sp_type>0">
            and sp.sp_type =#{sp_type}
        </if>
        <if test="createTime!=null and createTime!=''">
            and tk.stat_time&gt;=str_to_date(#{createTime},'%Y-%m-%d')
        </if>
        <if test="createTime1!=null and createTime1!=''">
            and
            tk.stat_time&lt;DATE_ADD(str_to_date(#{createTime1},'%Y-%m-%d'),INTERVAL 1 DAY)
        </if>
        group by sp.id, sp.sp_name
        order by sp.id
    </select>
    
    <select id="listPageSpSubmitFailDetailInfo" 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.res_desc,
        st.num,
        <!-- st.REPORT_RESULT result, -->
        <!-- IFNULL(r.receipt_desc,st.stat_desc) stat_desc, -->
        <!-- st.stat_desc, -->
        st.status
        <!-- st.arrive_time -->
        from sms_submit 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
       <!--  LEFT JOIN receipt_code r
        ON st.stat_desc = r.stat_desc -->
        <where>
        	st.res_result!=0
            <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')
            </if>

            <if test="endtime!=null and endtime!=''">
                and st.create_time&lt;DATE_ADD(str_to_date(#{endtime},'%Y-%m-%d'),INTERVAL 1 DAY)
            </if>
        </where>
        order by st.id desc
    </select>
    
    <select id="getTaskDetailFromArrive" parameterType="TaskDetailInfo"
        resultMap="TaskDetailInfoResultMap">
 		select 
        st.result result,
		st.stat_desc stat_desc,
		st.channelid,
		st.callmdn,
		<!-- DATE_FORMAT(str_to_date(st.arrive_time,'%Y%m%d%H%i%s'),'%Y-%m-%d %H:%i:%s') arrive_time -->
        st.arrive_time
        from sms_arrive st
        where taskid=#{taskid} 
        and mdn=#{mdn} and num=#{num} 
        
        <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 3 DAY)
            </if>
        limit 1
    </select>
    
    
    <select id="listPageSpReportFailDetailInfo" 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.num,
        st.RESULT result,
        <!-- IFNULL(r.receipt_desc,st.stat_desc) stat_desc --> st.stat_desc,
        st.status,
        <!-- st.arrive_time -->
        DATE_FORMAT(str_to_date(st.arrive_time,'%Y%m%d%H%i%s'),'%Y-%m-%d %H:%i:%s') arrive_time
        from sms_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
       <!--  LEFT JOIN receipt_code r
        ON st.stat_desc = r.stat_desc -->
        <where>
        	st.result!=0
            <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="stat_desc!=null and stat_desc!=''">
                and st.stat_desc=#{stat_desc}
            </if>
            <if test="createtime!=null and createtime!=''">
                and st.create_time&gt;=str_to_date(#{createtime},'%Y-%m-%d')
                and st.taskid&gt;CONCAT(date_format(str_to_date(#{createtime}, '%Y-%m-%d' ) , '%y%m%d'),'000000000000')
            </if>

            <if test="endtime!=null and endtime!=''">
                and st.create_time&lt;DATE_ADD(str_to_date(#{endtime},'%Y-%m-%d'),INTERVAL 1 DAY)
                and st.taskid&lt;CONCAT(date_format(DATE_ADD(str_to_date(#{endtime},'%Y-%m-%d'),INTERVAL 1 DAY), '%y%m%d'),'000000000000')
            </if>
        </where>
        order by st.id desc
    </select>
    
     <select id="getTaskDetailFromSubmit" parameterType="TaskDetailInfo"
        resultMap="TaskDetailInfoResultMap">
 		select 
 		st.id,
		st.res_desc,
        st.send_time,
        st.res_content
        from sms_submit st
        where taskid=#{taskid} and mdn=#{mdn} and num=#{num} 
        <if test="createtime!=null and createtime!=''">
                and st.create_time&gt;=DATE_ADD(str_to_date(#{createtime},'%Y-%m-%d %H:%i:%s'),INTERVAL -3 DAY)
            </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>
        limit 1
    </select>
    
    <select id="listPageTaskInfoApi" parameterType="TaskInfo"
        resultMap="taskResultMap">
        select tk.id,
        tk.sp_id,
        sp.sp_name,
        tk.content,
        tk.createtime,
        tk.total,
        tk.allnumber,
        tk.unicomnumber,
        tk.mobilenumber,
        tk.telecomnumber,
        tk.INTERNATIONALNUMBER
       
       
        from sms_send_task_api tk, sms_sp_info sp 
        where sp.id = tk.sp_id  and sp.sa_id=#{saId}
        <if test="sp_name!=null and sp_name!=''">
            and sp.sp_name like CONCAT('%',#{sp_name},'%') 
        </if>
        <if test="content!=null and content!=''">
            and tk.content like CONCAT('%',#{content},'%') 
        </if>
        <if test="sp_id!=null and sp_id>0">
            and tk.sp_id=#{sp_id}  
        </if>
        <if test="sp_saler!=null and sp_saler!=''">
            and sp.sp_saler =#{sp_saler}
        </if>
        <if test="task_id!=null and task_id>0">
            and tk.id=#{task_id}  
        </if>
              
        <if test="sign!=null and sign!=''">
            and tk.sign=#{sign}     
        </if>
        <if test="createTime!=null and createTime!=''">
            and tk.createtime&gt;=str_to_date(#{createTime},'%Y-%m-%d')        
        </if>

        <if test="createTime1!=null and createTime1!=''">
            and   
            tk.createtime&lt;=DATE_ADD(str_to_date(#{createTime1},'%Y-%m-%d'),INTERVAL 1 DAY)
        </if>
        order by tk.id desc
    </select>
    
    
    <select id="listPageTaskInfo" parameterType="TaskInfo"
        resultMap="taskResultMap">
        select tk.id,
        tk.sp_id,
        sp.sp_name,
        tk.content,
        tk.createtime,
        tk.total,
        tk.allnumber,
        tk.unicomnumber,
        tk.mobilenumber,
        tk.telecomnumber,
        tk.succ,
        tk.fail,
        tk.arrive_succ,
        tk.arrive_fail,
        tk.task_type,
        tk.status,
        tk.endtime,
        (tk.allnumber-tk.arrive_succ-tk.arrive_fail) unRecMsgnum,
        tk.send_type
        from sms_send_task tk, sms_sp_info sp 
        where sp.id = tk.sp_id  and sp.sa_id=#{saId}
        <if test="sp_name!=null and sp_name!=''">
            and sp.sp_name like CONCAT('%',#{sp_name},'%') 
        </if>
        <if test="content!=null and content!=''">
            and tk.content like CONCAT('%',#{content},'%') 
        </if>
        <if test="sp_id!=null and sp_id>0">
            and tk.sp_id=#{sp_id}  
        </if>
        <if test="sp_saler!=null and sp_saler!=''">
            and sp.sp_saler =#{sp_saler}
        </if>
        <if test="task_id!=null and task_id>0">
            and tk.id=#{task_id}  
        </if>
         <if test="send_type!=null and send_type!=''">
            and tk.send_type=#{send_type}     
        </if>
         <if test="task_type!=null and task_type!=''">
            and tk.task_type=#{task_type}     
        </if>       
        <if test="sign!=null and sign!=''">
            and tk.sign=#{sign}     
        </if>
        <if test="createTime!=null and createTime!=''">
            and tk.createtime&gt;=str_to_date(#{createTime},'%Y-%m-%d')        
        </if>

        <if test="createTime1!=null and createTime1!=''">
            and   
            tk.createtime&lt;=DATE_ADD(str_to_date(#{createTime1},'%Y-%m-%d'),INTERVAL 1 DAY)
        </if>
        order by tk.id desc
    </select>
    
    <select id="getTaskSumFromSubmit" parameterType="TaskDetailInfo"
        resultMap="TaskDetailInfoResultMap">
 		select 
 		taskid,res_result,count(*) num
        from sms_submit st
        where taskid=#{taskid}
         <if test="createtime!=null and createtime!=''">
                and st.create_time&gt;=str_to_date(#{createtime},'%Y-%m-%d')
            </if>

            <if test="endtime!=null and endtime!=''">
                and st.create_time&lt;DATE_ADD(str_to_date(#{endtime},'%Y-%m-%d'),INTERVAL 3 DAY)
            </if>
            
		group by taskid,res_result
    </select>
    
    <select id="getTaskSumFromArrive" parameterType="TaskDetailInfo"
        resultMap="TaskDetailInfoResultMap">
 		select 
 		taskid,result res_result,count(*) num
        from sms_arrive st
        where taskid=#{taskid}
       <if test="createtime!=null and createtime!=''">
                and st.create_time&gt;=str_to_date(#{createtime},'%Y-%m-%d')
            </if>

            <if test="endtime!=null and endtime!=''">
                and st.create_time&lt;DATE_ADD(str_to_date(#{endtime},'%Y-%m-%d'),INTERVAL 3 DAY)
            </if>
            
		group by taskid,result
    </select>
    
     <select id="getCrontabTaskById" parameterType="String"
        resultMap="taskResultMap">
 		select 
 			taskid,book_time,book_type
        from sms_crontab_send st
        where taskid=#{taskid}
		limit 1
    </select>
    
    <select id="listPageTaskDetailInfoForStatDesc" 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.num,
        st.RESULT result,
       <!--  IFNULL(r.receipt_desc,st.stat_desc) stat_desc, -->
        st.stat_desc,
        st.status,
        <!-- st.arrive_time -->
        st.channelid,
        DATE_FORMAT(str_to_date(st.arrive_time,'%Y%m%d%H%i%s'),'%Y-%m-%d %H:%i:%s') arrive_time
        from sms_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
        <!-- LEFT JOIN receipt_code r
        ON st.stat_desc = r.stat_desc -->
        <where>
        	sp.sa_id=#{saId}
            <if test="mdn!=null and mdn!=''">
                and st.mdn=#{mdn}
            </if>
            
			<if test="sign !=null and sign !=''">
                and st.sign=#{sign}
            </if>
            <if test="sp_id!=null and sp_id>0">
                and st.sp_id=#{sp_id}
            </if>
            <if test="taskid!=null and taskid>0">
                <choose>
	                <when test="taskid.toString().length() == 6">
	                     and left(st.taskid,6)=#{taskid}
	                </when>
	                <otherwise>
	                   and st.taskid=#{taskid}
	                </otherwise>
            	</choose>
            </if>
            <if test="channelid!=null and channelid >=0">
                and st.channelid=#{channelid}
            </if>
            <if test="stat_desc!=null and stat_desc!=''">
                and st.stat_desc=#{stat_desc}
            </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='res_result=="2"'>
                and st.result= 0
            </if>
            <if test='res_result=="3"'>
                and st.result != 0
            </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="listPageTaskNoReportDetailInfo" parameterType="TaskDetailInfo"
        resultMap="TaskDetailInfoResultMap">
        select
        a.id,a.taskid,a.sp_id,a.pa_dec,a.sp_name,a.mdn,a.callmdn,a.send_time,a.CHANNELID,a.num
        from (select st.sid 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.num
        from sms_submit_noreport st, sms_sp_info sp, sms_send_channel_info sc
        where st.sp_id = sp.id
        and st.CHANNELID = sc.id and sp.sa_id=#{saId}

		<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">
           <choose>
	                <when test="taskid.toString().length() == 6">
	                     and left(st.taskid,6)=#{taskid}
	                </when>
	                <otherwise>
	                   and st.taskid=#{taskid}
	                </otherwise>
            	</choose>
        </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="channelid!=null and channelid >=0">
                and st.channelid=#{channelid}
        </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>
        order by st.sid desc
        ) a
        
    </select>
    
    <select id="listPageTaskDetailInfo" 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.res_desc,
        st.num,
        <!-- st.REPORT_RESULT result, -->
        <!-- IFNULL(r.receipt_desc,st.stat_desc) stat_desc, -->
        <!-- st.stat_desc, -->
        st.channelid,
        st.status,
        st.res_content
       <!--  st.arrive_time -->
        from sms_submit 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
       <!--  LEFT JOIN receipt_code r
        ON st.stat_desc = r.stat_desc -->
        <where>
        	sp.sa_id=#{saId}
            <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">
               <choose>
	                <when test="taskid.toString().length() == 6">
	                     and left(st.taskid,6)=#{taskid}
	                </when>
	                <otherwise>
	                   and st.taskid=#{taskid}
	                </otherwise>
            	</choose>
            </if>
            <if test="channelid !=null and channelid>=0">
                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='res_result=="0"'>
                and st.res_result= 0
            </if>
            <if test='res_result=="1"'>
                and st.res_result != 0
            </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="getTaskInfoById" parameterType="TaskInfo" resultMap="taskInfoMap">
        select tk.id,
        tk.sp_id,
        tk.content,
        tk.createid,
        tk.createtime,
        tk.total,
        tk.allnumber,
        tk.unicomnumber,
        tk.mobilenumber,
        tk.telecomnumber,
        tk.task_type,
        tk.status,
        tk.endtime,
        sp.sp_audit_type,
        tk.send_type
        from sms_send_task tk, sms_api_setting sp
        where sp.sp_id = tk.sp_id
        <if test="sp_id!=null and sp_id>0">
            and tk.sp_id=#{sp_id}
        </if>
        <if test="task_id!=null and task_id>0">
            and tk.id=#{task_id}
        </if>
    </select>
    
    <select id="getTaskDetailById" parameterType="int"
        resultMap="TaskDetailInfoResultMap">
         select st.id,
        st.taskid,
        st.sp_id,
        sc.CHANNEL_NAME as pa_dec,
        sp.sp_name,
        st.mdn,
        st.res_content,
        st.callmdn,
        st.send_time,
        st.num,
        st.otype,
        st.province mdnProvice,
        
        st.res_desc
        from sms_submit 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 
         st.id=#{id}
    </select>
    
    <select id="getMmsTaskDetailById" parameterType="int"
        resultMap="TaskDetailInfoResultMap">
         select st.id,
        st.taskid,
        st.sp_id,
        sc.CHANNEL_NAME as pa_dec,
        sp.sp_name,
        st.mdn,
        st.res_content,
        st.callmdn,
        st.send_time,
        st.num,
        st.otype,
        st.res_desc
        from sms_submit st 
        left join  sms_sp_info sp on st.sp_id = sp.id 
        left join  mms_send_channel_info sc on st.CHANNELID = sc.id
        where 
         st.id=#{id}
    </select>

    <select id="listPageReplyDetailInfo" parameterType="cn.roadm.bo.ReplyDetailInfo"
            resultMap="ReplyDetailInfoResultMap">
        select rd.id,
        rd.sp_id,
        rd.callmdn,
        rd.mobile,
        rd.content,
        str_to_date(rd.reply_time,'%Y%m%d%H%i%S') reply_time,
        rd.createtime,
        rd.channelid,
        sc.channel_name,
        rd.status,
        rd.subport,
        rd.TASK_ID as taskId,
        rd.reason
        from sms_reply_record rd
        left join sms_send_channel_info sc on sc.id = rd.channelid
        left join sms_sp_info sp on rd.sp_id = sp.id
        <where>
            rd.IS_DEL = 0
            <if test="callmdn!=null and callmdn!=''">
                and rd.callmdn=#{callmdn}
            </if>
            <if test="taskId != null and taskId != ''">
                and rd.TASK_ID = #{taskId}
            </if>
            <if test="sp_id!=null and sp_id>=0">
                and rd.sp_id=#{sp_id}
            </if>

            <if test="sp_saler!=null and sp_saler!=''">
                and sp.sp_saler=#{sp_saler}
            </if>
            <if test="spIdList!=null and spIdList.size()>0">
                and rd.sp_id in
                <foreach collection="spIdList" index="index" item="item" open="(" separator="," close=")">
                    ${item}
                </foreach>
            </if>
            <if test="status!=null ">
                and rd.status=#{status}
            </if>
            <if test="channelid !=null and channelid != '' and channelid != '-1'">
                and rd.channelid=#{channelid}
            </if>
            <if test="mobile!=null and mobile!=''">
                and rd.mobile=#{mobile}
            </if>

            <if test="content!=null and content!=''">
                and rd.content like CONCAT('%',#{content},'%')
            </if>

            <if test="createtime!=null and createtime!=''">
                and rd.createtime&gt;=str_to_date(#{createtime},'%Y-%m-%d')
            </if>

            <if test="endtime!=null and endtime!=''">
                and rd.createtime&lt;=DATE_ADD(str_to_date(#{endtime},'%Y-%m-%d'),INTERVAL 1 DAY)
            </if>
        </where>
        order by rd.createtime desc

    </select>
    
</mapper>     
