<?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.admin.mapper.SendTaskMapper">
    <resultMap id="crontabTaskList" type="cn.roadm.admin.dto.CrontabSendTaskDO">
        <id column="id" property="id"/>
        <result column="sp_id" property="spId"/>
        <result column="sp_name" property="spName"/>
        <result column="content" property="content"/>
        <result column="createtime" property="createTime"/>
        <result column="total" property="total"/>
        <result column="allnumber" property="allNumber"/>
        <result column="unicomnumber" property="unicomNumber"/>
        <result column="mobilenumber" property="mobileNumber"/>
        <result column="telecomnumber" property="telecomNumber"/>
        <result column="succ" property="success"/>
        <result column="fail" property="fail"/>
        <result column="arrive_succ" property="arriveSuccess"/>
        <result column="arrive_fail" property="arriveFail"/>
        <result column="task_type" property="taskType"/>
        <result column="status" property="status"/>
        <result column="endtime" property="endTime"/>
        <result column="send_type" property="sendType"/>
        <result column="description" property="description"/>
        <result column="unRecMsgnum" property="unRecMsgNum"/>
        <result column="task_name" property="taskName"/>
        <result column="return_fee_type" property="returnFeeType"/>
        <result column="createid" property="createId"/>

    </resultMap>

    <select id="listPageQueryCrontabSendTask" resultMap="crontabTaskList">
        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.DESCPTION as description,
        (tk.allnumber - tk.arrive_succ - tk.arrive_fail) unRecMsgnum,
        tk.send_type,
        tk.task_name
        from sms_send_task tk,
        sms_sp_info sp
        where sp.id = tk.sp_id
        and TASK_TYPE = 2
        <if test="crontabSendTask.spName != null and crontabSendTask.spName != ''">
            and sp.sp_name like CONCAT('%', #{crontabSendTask.spName}, '%')
        </if>
        <if test="crontabSendTask.content != null and crontabSendTask.content != ''">
            and tk.content like CONCAT('%', #{crontabSendTask.content}, '%')
        </if>
        <if test="crontabSendTask.spId != null and crontabSendTask.spId > 0">
            and tk.sp_id = #{crontabSendTask.spId}
        </if>
        <if test="crontabSendTask.taskId != null and crontabSendTask.taskId > 0">
            and tk.id = #{crontabSendTask.taskId}
        </if>
        <if test="crontabSendTask.sendType != null and crontabSendTask.sendType != ''">
            and tk.send_type = #{crontabSendTask.sendType}
        </if>
        <if test="crontabSendTask.startTime != null and crontabSendTask.startTime != ''">
            and tk.createtime &gt;= str_to_date(#{crontabSendTask.startTime}, '%Y-%m-%d')
        </if>
        <if test="crontabSendTask.endTime != null and crontabSendTask.endTime != ''">
            and tk.createtime &lt;= DATE_ADD(str_to_date(#{crontabSendTask.endTime}, '%Y-%m-%d'), INTERVAL 1 DAY)
        </if>
        order by tk.CREATETIME desc
    </select>

    <select id="getTaskSumFromSubmit" resultType="cn.roadm.admin.controller.vo.CrontabApiSendDetailVO">
        select taskid as taskId, res_result as resResult, count(1) as num
        from sms_submit st
        where taskid = #{taskId}
        <if test="startTime != null and startTime != ''">
            and st.create_time &gt;= str_to_date(#{startTime}, '%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" resultType="cn.roadm.admin.controller.vo.CrontabApiSendDetailVO">
        select taskid, result as res_result, count(1) as num
        from sms_arrive st
        where taskid = #{taskId}
        <if test="startTime != null and startTime != ''">
            and st.create_time &gt;= str_to_date(#{startTime}, '%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="getCrontabTaskBookTimeById" resultType="java.lang.String">
        select DATE_FORMAT(tk.BOOK_TIME, '%Y-%m-%d %H:%i:%s') as bookTime
        from sms_crontab_send tk
        where tk.TASKID = #{taskId} limit 1
    </select>

    <select id="fetchTaskDetailRecordListByContent" resultType="cn.roadm.admin.dto.SmsSendRecordDetailDO">
        SELECT a1.id,
        a1.taskId,
        a1.sp_id,
        sc.channel_name,
        sp.sp_name,
        a1.mdn,
        a1.callmdn,
        a1.send_time,
        a1.res_desc,
        a1.num,
        a1.channelid,
        a1.`status`,
        a1.res_content,
        a1.otype,
        b1.result,
        b1.stat_desc,
        b1.arrive_time,
        a1.messageId,
        a1.sign
        FROM
        (
        SELECT a.id,
        a.res_content,
        a.sign,
        a.taskid,
        a.mdn,
        a.num,
        a.send_time,
        a.sp_id,
        a.callmdn,
        a.res_desc,
        a.channelid,
        a.`status`,
        a.otype,
        a.sizes,
        a.MESSAGEID,
        a.sign
        FROM sms_submit a
        <where>
            <if test="content != null and content != ''">
                AND a.RES_CONTENT LIKE concat('%', #{content}, '%')
            </if>
            <if test="mdn != null and mdn != ''">
                <choose>
                    <when test="mdn.toString().length() &lt; 11">
                        and a.mdn like CONCAT('%', #{mdn}, '%')
                    </when>
                    <otherwise>
                        and a.mdn = #{mdn}
                    </otherwise>
                </choose>
            </if>

            <if test="sign != null and sign != ''">
                and a.sign = #{sign}
            </if>
            <if test="spId != null and spId > 0">
                and a.sp_id = #{spId}
            </if>
            <if test="taskId != null and taskId > 0">
                <choose>
                    <when test="taskId.toString().length() &lt; 18">
                        and a.taskid like CONCAT(#{taskId}, '%')
                    </when>
                    <otherwise>
                        and a.taskid = #{taskId}
                    </otherwise>
                </choose>
            </if>
            <if test="channelId != null and channelId >= 0">
                and a.channelid = #{channelId}
            </if>
            <if test="startTime != null and startTime != ''">
                and a.create_time &gt;= str_to_date(#{startTime}, '%Y-%m-%d %H:%i:%s')
                and a.taskid &gt; CONCAT(DATE_FORMAT(#{startTime}, '%y%m%d'), '000000000000')
            </if>
            <if test="endTime != null and endTime != ''">
                and a.create_time &lt; DATE_ADD(str_to_date(#{endTime}, '%Y-%m-%d %H:%i:%s'), INTERVAL 5 SECOND)
            </if>
            <if test="otype != null and otype != ''">
                and a.otype = #{otype}
            </if>
            <if test="lsms != null and lsms == 1">
                and a.sizes = 1
            </if>
            <if test="lsms != null and lsms == 2">
                and a.sizes > 1
            </if>
            <if test="province != null and province != ''">
                and a.province = #{province}
            </if>
        </where>
        ) a1,
        (
        SELECT b.SP_ID,
        b.taskid,
        b.mdn,
        b.num,
        b.STAT_DESC,
        b.CHANNELID,
        b.RESULT result,
        b.arrive_time,
        b.SIZES
        FROM sms_arrive b
        <where>
            <if test="statDesc != null and statDesc != ''">
                and b.stat_desc = #{statDesc}
            </if>
            <if test="mdn != null and mdn != ''">
                <choose>
                    <when test="mdn.toString().length() &lt; 11">
                        and b.mdn like CONCAT('%', #{mdn}, '%')
                    </when>
                    <otherwise>
                        and b.mdn = #{mdn}
                    </otherwise>
                </choose>
            </if>

            <if test="sign != null and sign != ''">
                and b.sign = #{sign}
            </if>
            <if test="spId != null and spId > 0">
                and b.sp_id = #{spId}
            </if>
            <if test="taskId != null and taskId > 0">
                <choose>
                    <when test="taskId.toString().length() &lt; 18">
                        and b.taskid like CONCAT(#{taskId}, '%')
                    </when>
                    <otherwise>
                        and b.taskid = #{taskId}
                    </otherwise>
                </choose>
            </if>
            <if test="channelId != null and channelId >= 0">
                and b.channelid = #{channelId}
            </if>
            <if test='resResult == "2"'>
                and b.result = 0
            </if>
            <if test='resResult == "3"'>
                and b.result != 0
            </if>
            <if test="startTime != null and startTime != ''">
                and b.create_time &gt;= str_to_date(#{startTime}, '%Y-%m-%d %H:%i:%s')
                and b.taskid &gt; CONCAT(DATE_FORMAT(#{startTime}, '%y%m%d'), '000000000000')
            </if>

            <if test="endTime != null and endTime != ''">
                and b.create_time &lt; DATE_ADD(str_to_date(#{endTime}, '%Y-%m-%d %H:%i:%s'), INTERVAL 5 SECOND)
            </if>
            <if test="otype != null and otype != ''">
                and b.otype = #{otype}
            </if>
            <if test="lsms != null and lsms == 1">
                and b.sizes = 1
            </if>
            <if test="lsms != null and lsms == 2">
                and b.sizes > 1
            </if>
        </where>
        ) b1,
        sms_sp_info sp,
        sms_send_channel_info sc
        WHERE a1.TASKID = b1.TASKID
        AND a1.mdn = b1.mdn
        AND a1.num = b1.num
        and a1.SP_ID = sp.id
        and a1.SP_ID = b1.SP_ID
        and a1.channelid = b1.channelid
        and a1.channelid = sc.id
        <where>
            <if test="spName != null and spName != ''">
                and sp.sp_name like CONCAT('%',#{spName}
                , '%')
            </if>
        </where>
        limit ${offset},${pageSize}
    </select>

    <select id="fetchTaskDetailRecordListForStatDesc" resultType="cn.roadm.admin.dto.SmsSendRecordDetailDO">
        select st.id,
        st.taskid,
        st.sp_id,
        sc.channel_name,
        sp.sp_name,
        st.mdn,
        st.callmdn,
        st.num,
        st.result,
        st.stat_desc,
        st.status,
        st.channelid,
        st.sizes,
        st.otype,
        st.messageId,
        st.sign,
        DATE_FORMAT(str_to_date(st.arrive_time, '%Y%m%d%H%i%s'), '%Y-%m-%d %H:%i:%s') as 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
        <where>
            <if test="mdn != null and mdn != ''">
                <choose>
                    <when test='mdn.toString().contains("_")'>
                        and st.mdn like #{mdn}
                    </when>
                    <otherwise>
                        and st.mdn = #{mdn}
                    </otherwise>
                </choose>
            </if>

            <if test="sign != null and sign != ''">
                and st.sign = #{sign}
            </if>

            <if test="spId != null and spId > 0">
                and st.sp_id = #{spId}
            </if>
            <if test="taskId != null and taskId > 0">
                <choose>
                    <when test="taskId.toString().length() &lt; 18">
                        and st.taskid like CONCAT(#{taskId}, '%')
                    </when>
                    <otherwise>
                        and st.taskid = #{taskId}
                    </otherwise>
                </choose>
            </if>
            <if test="channelId != null and channelId >= 0">
                and st.channelid = #{channelId}
            </if>
            <if test="statDesc != null and statDesc != ''">
                and st.stat_desc = #{statDesc}
            </if>
            <if test="spName != null and spName != ''">
                and sp.sp_name like CONCAT('%', #{spName}, '%')
            </if>
            <if test='resResult == "2"'>
                and st.result = 0
            </if>
            <if test='resResult == "3"'>
                and st.result != 0
            </if>
            <if test="startTime != null and startTime != ''">
                and st.create_time &gt;= str_to_date(#{startTime}, '%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="otype != null and otype != ''">
                and st.otype = #{otype}
            </if>
            <if test="lsms != null and lsms == 1">
                and st.sizes = 1
            </if>
            <if test="lsms != null and lsms == 2">
                and st.sizes > 1
            </if>
            <if test="province != null and province != ''">
                and st.province = #{province}
            </if>
        </where>
        order by st.id desc
        limit ${offset},${pageSize}
    </select>

    <select id="getTaskDetailForSubmit" resultType="cn.roadm.admin.dto.SmsSendSubmitRecordDO">
        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="otype != null and otype != ''">
            and st.otype = #{otype}
        </if>
        limit 1
    </select>

    <select id="fetchTaskDetailRecordListNoReport" resultType="cn.roadm.admin.dto.SmsSendRecordDetailDO">
        SELECT st.sid as id,
        st.taskid,
        st.sp_id,
        sp.sp_name,
        st.mdn,
        st.callmdn,
        st.send_time,
        st.channelId,
        st.num,
        st.sizes,
        st.otype,
        st.messageId,
        st.res_content,
        st.sign
        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
            <if test="province != null and province != ''">
                and st.province = #{province}
            </if>
            <if test="mdn != null and mdn != ''">
                <choose>
                    <when test='mdn.toString().contains("_")'>
                        and st.mdn like #{mdn}
                    </when>
                    <otherwise>
                        and st.mdn = #{mdn}
                    </otherwise>
                </choose>
            </if>

            <if test="spId != null and spId > 0">
                and st.sp_id = #{spId}
            </if>
            <if test="taskId != null and taskId > 0">
                <choose>
                    <when test="taskId.toString().length() &lt; 18">
                        and st.taskid like CONCAT(#{taskId}, '%')
                    </when>
                    <otherwise>
                        and st.taskid = #{taskId}
                    </otherwise>
                </choose>
            </if>
            <if test="spName != null and spName != ''">
                and sp.sp_name like CONCAT('%', #{spName}, '%')
            </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="startTime != null and startTime != ''">
                and st.create_time &gt;= str_to_date(#{startTime}, '%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="otype != null and otype != ''">
                and st.otype = #{otype}
            </if>
            <if test="content != null and content != ''">
                and st.res_content like concat('%', #{content}, '%')
            </if>
            <if test="lsms != null and lsms == 1">
                and st.sizes = 1
            </if>
            <if test="lsms != null and lsms == 2">
                and st.sizes > 1
            </if>
        </where>
        order by st.TASKID desc
        limit ${offset},${pageSize}
    </select>

    <select id="fetchTaskDetailRecordList" resultType="cn.roadm.admin.dto.SmsSendRecordDetailDO">
        select st.id,
        st.taskid,
        st.sp_id,
        sc.channel_name,
        sp.sp_name,
        st.mdn,
        st.callmdn,
        st.send_time,
        st.res_desc,
        st.num,
        st.SIZES,
        st.channelid,
        st.status,
        st.otype,
        st.messageId,
        st.res_content,
        st.sign
        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>
            <if test="mdn != null and mdn != ''">
                <choose>
                    <when test='mdn.toString().contains("_")'>
                        and st.mdn like #{mdn}
                    </when>
                    <otherwise>
                        and st.mdn = #{mdn}
                    </otherwise>
                </choose>
            </if>


            <if test="spId != null and spId > 0">
                and st.sp_id = #{spId}
            </if>
            <if test="taskId != null and taskId > 0">
                <choose>
                    <when test="taskId.toString().length() &lt; 18">
                        and st.taskid like CONCAT(#{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="spName != null and spName != ''">
                and sp.sp_name like CONCAT('%', #{spName}, '%')
            </if>
            <if test='resResult == "0"'>
                and st.res_result = 0
            </if>
            <if test='resResult == "1"'>
                and st.res_result != 0
            </if>

            <if test="startTime != null and startTime != ''">
                and st.create_time &gt;= str_to_date(#{startTime}, '%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="otype != null and otype != ''">
                and st.otype = #{otype}
            </if>
            <if test="content != null and content != ''">
                and st.res_content like concat('%', #{content}, '%')
            </if>
            <if test="lsms != null and lsms == 1">
                and st.sizes = 1
            </if>
            <if test="lsms != null and lsms == 2">
                and st.sizes > 1
            </if>
            <if test="province != null and province != ''">
                and st.province = #{province}
            </if>
        </where>
        order by st.id desc
        limit ${offset},${pageSize}
    </select>

    <select id="getTaskDetailForArrive" resultType="cn.roadm.admin.dto.SmsSendSubmitRecordDO">
        select st.result,
               st.stat_desc,
               st.channelid,
               st.callmdn,
               st.arrive_time
        from sms_arrive st
        where taskid = #{taskId}
          and mdn = #{mdn}
          and num = #{num} limit 1
    </select>

    <select id="getSendTypeByTaskId" resultType="java.lang.String">
        select SEND_TYPE
        from sms_send_task
        where id = #{taskId}
    </select>

    <select id="resendRecordQueryList" resultType="cn.roadm.admin.controller.vo.ResendQueryVO">
        select concat('', taskId) as taskId, mdn, channelId, resend_channelId, callmdn, stat_desc, arrive_time
        from sms_arrive_resend
        <where>
            <foreach collection="list" item="item" index="index" separator="or">
                taskId = #{item.taskId} and mdn = #{item.mdn}
            </foreach>
        </where>
        order by taskId desc, CREATE_TIME desc
    </select>

    <select id="taskDetailByContentCount" resultType="java.lang.Long">
        SELECT count(a1.id) FROM
        (
        SELECT a.id,
        a.RES_CONTENT,
        a.SIGN,
        a.TASKID,
        a.MDN,
        a.NUM,
        a.SEND_TIME,
        a.SP_ID,
        a.CALLMDN,
        a.RES_DESC,
        a.CHANNELID,
        a.`STATUS`,
        a.otype,
        a.SIZES
        FROM sms_submit a
        <where>
            <if test="content != null and content != ''">
                AND a.RES_CONTENT LIKE concat('%', #{content}, '%')
            </if>
            <if test="mdn != null and mdn != ''">
                <choose>
                    <when test="mdn.toString().length() &lt; 11">
                        and a.mdn like CONCAT('%', #{mdn}, '%')
                    </when>
                    <otherwise>
                        and a.mdn = #{mdn}
                    </otherwise>
                </choose>
            </if>

            <if test="sign != null and sign != ''">
                and a.sign = #{sign}
            </if>
            <if test="spId != null and spId > 0">
                and a.sp_id = #{spId}
            </if>
            <if test="taskId != null and taskId > 0">
                <choose>
                    <when test="taskId.toString().length() &lt; 18">
                        and a.taskid like CONCAT(#{taskId}, '%')
                    </when>
                    <otherwise>
                        and a.taskid = #{taskId}
                    </otherwise>
                </choose>
            </if>
            <if test="channelId != null and channelId >= 0">
                and a.channelid = #{channelId}
            </if>
            <if test="startTime != null and startTime != ''">
                and a.create_time &gt;= str_to_date(#{startTime}, '%Y-%m-%d %H:%i:%s')
                and a.taskid &gt; CONCAT(DATE_FORMAT(#{startTime}, '%y%m%d'), '000000000000')
            </if>
            <if test="endTime != null and endTime != ''">
                and a.create_time &lt; DATE_ADD(str_to_date(#{endTime}, '%Y-%m-%d %H:%i:%s'), INTERVAL 5 SECOND)
            </if>
            <if test="otype != null and otype != ''">
                and a.otype = #{otype}
            </if>
            <if test="lsms != null and lsms == 1">
                and a.sizes = 1
            </if>
            <if test="lsms != null and lsms == 2">
                and a.sizes > 1
            </if>
            <if test="province != null and province != ''">
                and a.province = #{province}
            </if>
        </where>
        ) a1,
        (
        SELECT b.SP_ID,
        b.taskid,
        b.mdn,
        b.num,
        b.STAT_DESC,
        b.CHANNELID,
        b.RESULT result,
        b.arrive_time,
        b.SIZES
        FROM sms_arrive b
        <where>
            <if test="statDesc != null and statDesc != ''">
                and b.stat_desc = #{statDesc}
            </if>
            <if test="mdn != null and mdn != ''">
                <choose>
                    <when test="mdn.toString().length() &lt; 11">
                        and b.mdn like CONCAT('%', #{mdn}, '%')
                    </when>
                    <otherwise>
                        and b.mdn = #{mdn}
                    </otherwise>
                </choose>
            </if>

            <if test="sign != null and sign != ''">
                and b.sign = #{sign}
            </if>
            <if test="spId != null and spId > 0">
                and b.sp_id = #{spId}
            </if>
            <if test="taskId != null and taskId > 0">
                <choose>
                    <when test="taskId.toString().length() &lt; 18">
                        and b.taskid like CONCAT(#{taskId}, '%')
                    </when>
                    <otherwise>
                        and b.taskid = #{taskId}
                    </otherwise>
                </choose>
            </if>
            <if test="channelId != null and channelId >= 0">
                and b.channelid = #{channelId}
            </if>
            <if test='resResult == "2"'>
                and b.result = 0
            </if>
            <if test='resResult == "3"'>
                and b.result != 0
            </if>
            <if test="startTime != null and startTime != ''">
                and b.create_time &gt;= str_to_date(#{startTime}, '%Y-%m-%d %H:%i:%s')
                and b.taskid &gt; CONCAT(DATE_FORMAT(#{startTime}, '%y%m%d'), '000000000000')
            </if>

            <if test="endTime != null and endTime != ''">
                and b.create_time &lt; DATE_ADD(str_to_date(#{endTime}, '%Y-%m-%d %H:%i:%s'), INTERVAL 5 SECOND)
            </if>
            <if test="otype != null and otype != ''">
                and b.otype = #{otype}
            </if>
            <if test="lsms != null and lsms == 1">
                and b.sizes = 1
            </if>
            <if test="lsms != null and lsms == 2">
                and b.sizes > 1
            </if>
        </where>
        ) b1,
        sms_sp_info sp,
        sms_send_channel_info sc
        WHERE a1.TASKID = b1.TASKID
        AND a1.mdn = b1.mdn
        AND a1.num = b1.num
        and a1.SP_ID = sp.id
        and a1.SP_ID = b1.SP_ID
        and a1.channelid = b1.channelid
        and a1.channelid = sc.id
        <where>
            <if test="spName != null and spName != ''">
                and sp.sp_name like CONCAT('%',#{spName}
                , '%')
            </if>
        </where>
    </select>

    <select id="taskDetailInfoForStatDescCount" resultType="java.lang.Long">
        select count(st.id)
        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
        <where>
            <if test="mdn != null and mdn != ''">
                <choose>
                    <when test='mdn.toString().contains("_")'>
                        and st.mdn like #{mdn}
                    </when>
                    <otherwise>
                        and st.mdn = #{mdn}
                    </otherwise>
                </choose>
            </if>

            <if test="sign != null and sign != ''">
                and st.sign = #{sign}
            </if>

            <if test="spId != null and spId > 0">
                and st.sp_id = #{spId}
            </if>
            <if test="taskId != null and taskId > 0">
                <choose>
                    <when test="taskId.toString().length() &lt; 18">
                        and st.taskid like CONCAT(#{taskId}, '%')
                    </when>
                    <otherwise>
                        and st.taskid = #{taskId}
                    </otherwise>
                </choose>
            </if>
            <if test="channelId != null and channelId >= 0">
                and st.channelid = #{channelId}
            </if>
            <if test="statDesc != null and statDesc != ''">
                and st.stat_desc = #{statDesc}
            </if>
            <if test="spName != null and spName != ''">
                and sp.sp_name like CONCAT('%', #{spName}, '%')
            </if>
            <if test='resResult == "2"'>
                and st.result = 0
            </if>
            <if test='resResult == "3"'>
                and st.result != 0
            </if>
            <if test="startTime != null and startTime != ''">
                and st.create_time &gt;= str_to_date(#{startTime}, '%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="otype != null and otype != ''">
                and st.otype = #{otype}
            </if>
            <if test="lsms != null and lsms == 1">
                and st.sizes = 1
            </if>
            <if test="lsms != null and lsms == 2">
                and st.sizes > 1
            </if>
            <if test="province != null and province != ''">
                and st.province = #{province}
            </if>
        </where>
    </select>

    <select id="taskNoReportDetailInfoCount" resultType="java.lang.Long">
        SELECT count(st.sid)
        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
            <if test="mdn != null and mdn != ''">
                <choose>
                    <when test='mdn.toString().contains("_")'>
                        and st.mdn like #{mdn}
                    </when>
                    <otherwise>
                        and st.mdn = #{mdn}
                    </otherwise>
                </choose>
            </if>
            <if test="spId != null and spId > 0">
                and st.sp_id = #{spId}
            </if>
            <if test="taskId != null and taskId > 0">
                <choose>
                    <when test="taskId.toString().length() &lt; 18">
                        and st.taskid like CONCAT(#{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="spName != null and spName != ''">
                and sp.sp_name like CONCAT('%', #{spName}, '%')
            </if>
            <if test='resResult == "0"'>
                and st.res_result = 0
            </if>
            <if test='resResult == "1"'>
                and st.res_result != 0
            </if>

            <if test="startTime != null and startTime != ''">
                and st.create_time &gt;= str_to_date(#{startTime}, '%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="otype != null and otype != ''">
                and st.otype = #{otype}
            </if>
            <if test="content != null and content != ''">
                and st.res_content like concat('%', #{content}, '%')
            </if>
            <if test="lsms != null and lsms == 1">
                and st.sizes = 1
            </if>
            <if test="lsms != null and lsms == 2">
                and st.sizes > 1
            </if>
            <if test="province != null and province != ''">
                and st.province = #{province}
            </if>
        </where>
    </select>

    <select id="taskDetailInfoCount" resultType="java.lang.Long">
        select count(st.id)
        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>
            <if test="mdn != null and mdn != ''">
                <choose>
                    <when test='mdn.toString().contains("_")'>
                        and st.mdn like #{mdn}
                    </when>
                    <otherwise>
                        and st.mdn = #{mdn}
                    </otherwise>
                </choose>
            </if>
            <if test="spId != null and spId > 0">
                and st.sp_id = #{spId}
            </if>
            <if test="taskId != null and taskId > 0">
                <choose>
                    <when test="taskId.toString().length() &lt; 18">
                        and st.taskid like CONCAT(#{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="spName != null and spName != ''">
                and sp.sp_name like CONCAT('%', #{spName}, '%')
            </if>
            <if test='resResult == "0"'>
                and st.res_result = 0
            </if>
            <if test='resResult == "1"'>
                and st.res_result != 0
            </if>

            <if test="startTime != null and startTime != ''">
                and st.create_time &gt;= str_to_date(#{startTime}, '%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="otype != null and otype != ''">
                and st.otype = #{otype}
            </if>
            <if test="content != null and content != ''">
                and st.res_content like concat('%', #{content}, '%')
            </if>
            <if test="lsms != null and lsms == 1">
                and st.sizes = 1
            </if>
            <if test="lsms != null and lsms == 2">
                and st.sizes > 1
            </if>
            <if test="province != null and province != ''">
                and st.province = #{province}
            </if>
        </where>
    </select>

    <select id="exportTaskDetailByStatDescList" resultType="cn.roadm.admin.controller.vo.ExportSendRecordVO">
        select a.*, sp.sp_name from
        (select st.id,
        st.taskid,
        st.sp_id,
        st.mdn,
        st.callmdn,
        st.num,
        st.result result,
        st.stat_desc,
        st.status,
        st.arrive_time
        from sms_arrive st
        <where>
            <if test="mdn != null and mdn != ''">
                <choose>
                    <when test='mdn.toString().contains("_")'>
                        and st.mdn like #{mdn}
                    </when>
                    <otherwise>
                        and st.mdn = #{mdn}
                    </otherwise>
                </choose>
            </if>
            <if test="otype != null and otype != ''">
                and st.otype = #{otype}
            </if>
            <if test='resResult == "2"'>
                and st.result = 0
            </if>
            <if test='resResult == "3"'>
                and st.result != 0
            </if>
            <if test="spId != null and spId > 0">
                and st.sp_id = #{spId}
            </if>
            <if test="taskId != null and taskId > 0">
                <choose>
                    <when test="taskId.toString().length() &lt; 18">
                        and st.taskid like CONCAT(#{taskId}, '%')
                    </when>
                    <otherwise>
                        and st.taskid = #{taskId}
                    </otherwise>
                </choose>
            </if>
            <if test="channelId != null and channelId >= 0">
                and st.channelid = #{channelId}
            </if>
            <if test="statDesc != null and statDesc != ''">
                and st.stat_desc = #{statDesc}
            </if>
            <if test="province != null and province != ''">
                and st.province = #{province}
            </if>

            <if test="sign != null and sign != ''">
                and st.sign = #{sign}
            </if>
            <if test="startTime != null and startTime != ''">
                and st.create_time &gt;= str_to_date(#{startTime}, '%Y-%m-%d %H:%i:%s')
                and st.res_content &gt;= str_to_date(#{startTime}, '%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)
                and st.res_content &lt; str_to_date(#{endTime}, '%Y-%m-%d %H:%i:%s')
            </if>
            <if test="endId != null">
                and st.id &lt; #{endId}
            </if>
        </where>
        ORDER BY st.id DESC
        <if test="exportSize != null">
            limit #{exportSize}
        </if>
        ) a,
        sms_sp_info sp
        where sp.id = a.SP_ID
        <if test="spName != null and spName != ''">
            and sp.sp_name like CONCAT('%', #{spName}, '%')
        </if>
        ORDER BY a.id DESC
    </select>

    <select id="exportTaskNoReportDetailList" resultType="cn.roadm.admin.controller.vo.ExportSendRecordVO">
        select
        a.id,a.taskid,a.sp_id,
        a.pa_dec,a.sp_name,a.mdn,
        a.callmdn,a.send_time,
        a.res_desc,a.CHANNELID,
        a.num,a.res_content
        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,
        (case when st.RES_RESULT=0 then '提交成功' else '提交失败' end) res_desc,
        st.CHANNELID,
        st.num,
        st.res_content

        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

        <if test="sp_id!=null and sp_id>0">
            and st.sp_id=#{sp_id}
        </if>
        <if test="mdn != null and mdn != ''">
            <choose>
                <when test='mdn.toString().contains("_")'>
                    and st.mdn like #{mdn}
                </when>
                <otherwise>
                    and st.mdn=#{mdn}
                </otherwise>
            </choose>
        </if>
        <if test="otype!=null and otype!=''">
            and st.otype=#{otype}
        </if>
        <if test="taskid!=null and taskid>0">
            <choose>
                <when test="taskid.toString().length()&lt;18">
                    and st.taskid like CONCAT(#{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="content!=null and content!=''">
            AND st.RES_CONTENT LIKE concat('%',#{content},'%')
        </if>
        <if test="createtime!=null and createtime!=''">
            and st.send_time&gt;= str_to_date(#{createtime}, '%Y-%m-%d %H:%i:%s')
            and st.create_time&gt;=str_to_date(#{createtime},'%Y-%m-%d %H:%i:%s')
        </if>

        <if test="endtime!=null and endtime!=''">
            and st.send_time &lt;DATE_ADD( str_to_date(#{endtime}, '%Y-%m-%d %H:%i:%s'), INTERVAL 1 SECOND )
            and st.create_time&lt;=DATE_ADD(str_to_date(#{endtime},'%Y-%m-%d %H:%i:%s'),INTERVAL 3600 SECOND)
        </if>
        <if test="endId != null">
            and st.sid &lt; #{endId}
        </if>
        order by st.sid desc
        ) a

        <if test="exportExcelSize != null ">
            limit #{exportExcelSize}
        </if>
    </select>

    <select id="exportTaskDetailByAllList" resultType="cn.roadm.admin.controller.vo.ExportSendRecordVO">
        select st.id, st.taskid,
        st.sp_id,
        sp.sp_name,
        st.mdn,
        st.callmdn,
        st.send_time,
        st.res_desc,
        st.num,
        st.status,
        st.res_content
        from sms_submit st,
        sms_sp_info sp
        where st.sp_id = sp.id

        <if test="mdn != null and mdn != ''">
            <choose>
                <when test='mdn.toString().contains("_")'>
                    and st.mdn like #{mdn}
                </when>
                <otherwise>
                    and st.mdn = #{mdn}
                </otherwise>
            </choose>
        </if>
        <if test="otype != null and otype != ''">
            and st.otype = #{otype}
        </if>
        <if test="spName != null and spName != ''">
            and sp.sp_name like CONCAT('%', #{spName}, '%')
        </if>
        <if test='resResult == "0"'>
            and st.res_result = 0
        </if>
        <if test='resResult == "1"'>
            and st.res_result != 0
        </if>
        <if test="spId != null and spId > 0">
            and st.sp_id = #{spId}
        </if>
        <if test="taskId != null and taskId > 0">
            <choose>
                <when test="taskId.toString().length() &lt; 18">
                    and st.taskid like CONCAT(#{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="sign != null and sign != ''">
            and st.sign = #{sign}
        </if>
        <if test="content != null and content != ''">
            AND st.RES_CONTENT LIKE concat('%', #{content}, '%')
        </if>
        <if test="startTime != null and startTime != ''">
            and st.create_time &gt;= str_to_date(#{startTime}, '%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>
        ORDER BY st.id DESC
        <if test="exportSize != null">
            limit #{exportSize}
        </if>
    </select>
    <update id="updateTaskBookTime">
        update sms_crontab_send
        set BOOK_TIME = #{bookTime}
        where TASKID = #{id}
    </update>
    <update id="updateTaskFail" parameterType="string">
        update sms_send_task
        set status=2,
            descption='取消任务'
        where id = #{id}
          and status!=2
    </update>

    <update id="updateBookSend" parameterType="string">
        update
            SMS_CRONTAB_SEND
        set status=2
        where taskid = #{id}
          and status!=2
    </update>

    <delete id="deleteAuditTask" parameterType="string">
        delete
        from sms_send_audit_task
        where id = #{id}
          and status = 1
    </delete>

    <select id="getTaskInfoAllnumberById" parameterType="string" resultMap="crontabTaskList">
        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,
               tk.send_type,
               sp.return_fee_type,
               tk.fail
        from sms_send_task tk,
             sms_sp_info sp
        where sp.id = tk.sp_id
          and tk.id = #{id} limit 1
    </select>

    <select id="getCrontabTaskStatusById" parameterType="String" resultType="String">
        select status
        from sms_crontab_send st
        where taskid = #{id} limit 1
    </select>

    <select id="getCrontabTaskLimit1ById" resultType="cn.roadm.admin.table.SmsCrontabSendTable">
        select id,
               sp_id,
               taskid,
               status,
               book_time,
               book_type,
               createtime,
               imessage_id,
               send_type,
               total,
               otype,
               `describe`
        from sms_crontab_send st
        where taskid = #{taskId} limit 1
    </select>

    <select id="getPlatformSendTaskDetailById" resultType="cn.roadm.admin.dto.PlatformTaskRecordDO">
        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,
               tk.send_type,
               tk.FAIL
        from sms_send_task tk,
             sms_sp_info sp
        where sp.id = tk.sp_id
          and tk.id = #{taskId} limit 1
    </select>

    <select id="resumeBookTask" resultType="java.lang.Integer">
        update sms_crontab_send
        set STATUS = 0
        where TASKID = #{taskId}
    </select>

    <update id="resumeSendTask">
        update sms_send_task
        set status    = 0,
            DESCPTION = null
        where ID = #{taskId}
    </update>
</mapper>
