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

    <resultMap id="taskResult" type="cn.roadm.bo.TaskDetailResultDO">
        <id column="id" property="id"/>
        <result column="taskid" property="taskId"/>
        <result column="sp_id" property="spId"/>
        <result column="sp_name" property="spName"/>
        <result column="channelid" property="channelId"/>
        <result column="callmdn" property="callMdn"/>
        <result column="send_time" property="sendTime"/>
        <result column="res_desc" property="resDesc"/>
        <result column="stat_desc" property="statDesc"/>
        <result column="arrive_time" property="arriveTime"/>
        <result column="res_content" property="resContent"/>
    </resultMap>

<select id="queryTableName" resultType="java.lang.String">
    select table_name from information_schema.tables where table_schema=#{dbName} and table_name like 'sms_submit_2%' ORDER BY `table_name`
</select>

<select id="listPageHistoryTaskDetailInfo" resultMap="taskResult">
    select st.id,
           st.taskid,
           st.sp_id,
           st.mdn,
           st.callmdn,
           st.send_time,
           st.res_desc,
           st.num,
           st.status,
           st.arrive_time,
           st.res_content
    from ${queryMonth} st
    <where>
        <if test="mdn != null and mdn != ''">
            and st.mdn = #{mdn}
        </if>

        <if test="spId != null and spId > 0">
            and st.sp_id = #{spId}
        </if>
        <if test="taskId != null and taskId  != ''">
            and st.taskid = #{taskId}
        </if>
        <if test="statDesc != null and statDesc != ''">
            and st.stat_desc = #{statDesc}
        </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')
        </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="queryHistoryTaskArrive" resultType="cn.roadm.bo.ArriveDetail">
        select
        st.result result,
        st.stat_desc statDesc,
        st.callmdn callMdn,
        st.channelid channelId,
        st.arrive_time arriveTime
        from ${query.tableName} st
        where taskid = #{taskDetail.taskId}
        and mdn = #{taskDetail.mdn} and num = #{taskDetail.num}

        <if test="query.startTime != null and query.startTime != ''">
            and st.create_time&gt;=str_to_date(#{query.startTime},'%Y-%m-%d %H:%i:%s')
        </if>

        <if test="query.endTime != null and query.endTime != ''">
            and st.create_time&lt;DATE_ADD(str_to_date(#{query.endTime},'%Y-%m-%d %H:%i:%s'),INTERVAL 3 DAY)
        </if>
        limit 1
    </select>

    <select id="queryHistoryTaskArriveSearch" resultType="cn.roadm.bo.ArriveDetail">
        select
        st.result result,
        st.stat_desc statDesc,
        st.callmdn callMdn,
        st.channelid channelId,
        st.arrive_time arriveTime
        from ${query.tableName} st
        where taskid = #{taskDetail.taskId}
        and mdn = #{taskDetail.mdn} and num = #{taskDetail.num}

        <if test="query.startTime != null and query.startTime != ''">
            and st.create_time&gt;=str_to_date(#{query.startTime},'%Y-%m-%d %H:%i:%s')
        </if>

        <if test="query.endTime != null and query.endTime != ''">
            and st.create_time&lt;DATE_ADD(str_to_date(#{query.endTime},'%Y-%m-%d %H:%i:%s'),INTERVAL 3 DAY)
        </if>
        limit 1
    </select>

    <select id="searchPageHistoryTaskDetailInfo" resultMap="taskResult">
        select st.id,
               st.taskid,
               st.sp_id,
               st.mdn,
               st.callmdn,
               st.send_time,
               st.res_desc,
               st.num,
               st.status,
               st.arrive_time,
               st.res_content
        from ${queryMonth} 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="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 != ''">
                and st.taskid = #{taskId}
            </if>
            <if test="statDesc != null and statDesc != ''">
                and st.stat_desc = #{statDesc}
            </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')
            </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>
            <if test="province != null and province != ''">
                and st.province = #{province}
            </if>
        </where>
        order by st.id desc
    </select>

    <select id="queryArriveTableName" resultType="java.lang.String">
        select table_name from information_schema.tables where table_schema=#{dbName} and table_name like 'sms_arrive_2%' ORDER BY `table_name`
    </select>

    <select id="searchPageHistoryTaskArriveList" resultMap="taskResult">
        select st.id,
               st.channelid,
               st.taskid,
               st.sp_id,
               st.mdn,
               st.callmdn,
               st.result,
               st.stat_desc,
               st.num,
               st.status,
               st.arrive_time
        from ${queryMonth} 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="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 != ''">
                and st.taskid = #{taskId}
            </if>
            <if test="statDesc != null and statDesc != ''">
                and st.stat_desc = #{statDesc}
            </if>
            <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 1 DAY)
            </if>
            <if test="province != null and province != ''">
                and st.province = #{province}
            </if>
        </where>
        order by st.id desc
    </select>

    <select id="queryHistoryTaskSubmitDetailInfo" resultType="cn.roadm.bo.SubmitDetail">
        select tb.res_content, tb.res_result, tb.res_desc, tb.send_time
        from ${query.tableName} tb
        <where>
            tb.taskid = #{taskDetail.taskId}
              and tb.mdn = #{taskDetail.mdn}
              and tb.num = #{taskDetail.num}

            <if test="query.startTime != null and query.startTime != ''">
                and st.create_time &gt;= str_to_date(#{query.startTime}, '%Y-%m-%d %H:%i:%s')
            </if>

            <if test="query.endTime != null and query.endTime != ''">
                and st.create_time &lt; DATE_ADD(str_to_date(#{query.endTime}, '%Y-%m-%d %H:%i:%s'), INTERVAL 1 DAY)
            </if>
            limit 1
        </where>
    </select>
</mapper>
