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

    <select id="listPageReReportTask" resultType="cn.roadm.admin.dto.ReReportTaskDO">
        select st.id,
        st.taskid taskId,
        st.sp_id spId,
        st.mdn,
        st.starttime startTime,
        st.endtime endTime,
        st.stat_desc statDesc,
        st.status,
        st.sign,
        st.source,
        st.allnumber,
        st.createtime,
        st.result
        from sms_rereport_task st
        <where>
            <if test="params.mdn!=null and params.mdn!=''">
                and st.mdn=#{params.mdn}
            </if>
            <if test="params.sign!=null and params.sign!=''">
                and st.sign=#{params.sign}
            </if>
            <if test="params.source!=null and params.source!=''">
                and st.source=#{params.source}
            </if>
            <if test="params.result!=null and params.result>-1">
                and st.result=#{params.result}
            </if>
            <if test="params.spId!=null and params.spId>0">
                and st.sp_id=#{params.spId}
            </if>
            <if test="params.taskId!=null and params.taskId>0">
                and st.taskid=#{params.taskId}
            </if>
            <if test="params.statDesc!=null and params.statDesc!=''">
                and st.stat_desc=#{statDesc}
            </if>
        </where>
        order by st.id desc
    </select>
    <update id="cancelRerortTask" parameterType="int">
        update sms_rereport_task set status=4 where id=#{id}
    </update>
    <insert id="insertReReportTask"  useGeneratedKeys="true" keyProperty="id" parameterType="cn.roadm.admin.controller.vo.ReReportTaskSaveVO">
        insert into sms_rereport_task
        (sp_id,taskid,MDN,starttime,endtime,stat_desc,sign,source,allnumber,createtime,status,result) values
            (#{spId},#{taskId},#{mdn},#{startTime,jdbcType=VARCHAR},#{endTime,jdbcType=VARCHAR}
            ,#{statDesc,jdbcType=VARCHAR},#{sign,jdbcType=VARCHAR},#{source,jdbcType=VARCHAR},#{allnumber},sysdate(),#{status},#{result})
    </insert>
    <update id="updateReReportTask" parameterType="int" >
        update sms_rereport_task
        set status=1 where id=#{id} and status=0
    </update>

    <select id="getReReportTaskNum" parameterType="cn.roadm.admin.controller.vo.ReReportTaskSaveVO"
            resultType="cn.roadm.admin.dto.ReReportTaskDO">
        select
        count(1) allnumber
        from sms_arrive st
        <where>
            <if test="mdn!=null and mdn!=''">
                and st.mdn=#{mdn}
            </if>
            <if test="sign!=null and sign!=''">
                and st.sign=#{sign}
            </if>
            <if test="source!=null and source!=''">
                and st.source=#{source}
            </if>
            <if test="spId!=null and spId>0">
                and st.sp_id=#{spId}
            </if>
            <if test="taskId!=null and taskId>0">
                and st.taskid=#{taskId}
            </if>
            <if test="statDesc!=null and statDesc!=''">
                and st.stat_desc=#{statDesc}
            </if>
            <if test="result!=null and result>-1">
                and st.result=#{result}
            </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>
        </where>
    </select>

    <select id="listPageNoReportTask" resultType="cn.roadm.admin.dto.NoReportTaskDO">
        select id,sp_id spId,DATE_FORMAT(createtime, '%Y-%m-%d %H:%i:%s') createTime,
        DATE_FORMAT(endtime, '%Y-%m-%d %H:%i:%s') endTime,taskid taskId,content,phones,
        num,DATE_FORMAT(starttime, '%Y-%m-%d %H:%i:%s') startTime,status,res_result result,sign,timeout,channelid channelId from noreport_task
        <where>
            <if test="params.spId!=null and params.spId>0">
                and sp_id=#{params.spId}
            </if>
            <if test="params.startTime!=null and params.startTime!=''">
                and createtime &gt;= #{params.startTime}
            </if>
            <if test="params.endTime!=null and params.endTime!=''">
                and createtime &lt;= #{params.endTime}
            </if>
            <if test="params.status!=null and params.status!=''">
                and status=#{params.status}
            </if>
            <if test="params.sign!=null and params.sign!=''">
                and ss.sign=#{params.sign}
            </if>
        </where>
        order by id desc
    </select>

    <insert id="insertNoReportTask" parameterType="cn.roadm.admin.controller.vo.NoReportTaskSaveVO">
        insert into noreport_task(sp_id,createtime,
        <if test="endTime!=null and endTime!=''">
            endtime,
        </if>
        taskid,content,phones,num,
        <if test="startTime!=null and startTime!=''">
            starttime,
        </if>
        <if test="sign!=null and sign!=''">
            sign,
        </if>
        <if test="channelId!=null and channelId!=''">
            channelid,
        </if>
        timeout,status,res_result)
        values(#{spId},now(),
        <if test="endTime!=null and endTime!=''">
            #{endTime},
        </if>
        #{taskId},#{content},#{phones},#{num},
        <if test="startTime!=null and startTime!=''">
            #{startTime},
        </if>
        <if test="sign!=null and sign!=''">
            #{sign},
        </if>
        <if test="channelId!=null and channelId!=''">
            #{channelId},
        </if>
        #{timeout},0,#{result})
    </insert>

    <update id="updatetNoreportTask">
        update noreport_task set status=#{status} where id=#{id}
    </update>

    <select id="getNoReportCount" parameterType="cn.roadm.admin.controller.vo.NoReportTaskSaveVO" resultType="int">
        select count(1) from sms_submit_noreport ssn
        <where>
            <if test="spId!=null and spId>0">
                and ssn.sp_id=#{spId}
            </if>
            <if test="startTime!=null and startTime!=''">
                and ssn.send_time &gt; #{startTime}
                and ssn.create_time &gt; #{startTime}
            </if>
            <if test="endTime!=null and endTime!=''">
                and ssn.send_time &lt; #{endTime}
                and ssn.create_time &lt; #{endTime}
            </if>
            <if test="content!=null and content!=''">
                and ssn.res_content like concat('%',#{content},'%')
            </if>
            <if test="phones!=null and phones!=''">
                and ssn.mdn in
                <foreach collection="phones" index="index"  item="item" open="(" separator="," close=")">
                    '${item}'
                </foreach>
            </if>
            <if test="taskId!=null and taskId!=''">
                and ssn.taskid=#{taskId}
            </if>
            <if test="sign!=null and sign!=''">
                and ssn.sign=#{sign}
            </if>
            <if test="timeout==1">
                and ssn.messageid=1
            </if>
            <if test="channelId!=null and channelId!=''">
                and ssn.channelid=#{channelId}
            </if>

        </where>
    </select>
</mapper>
