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

    <select id="getComplaintList" resultType="cn.roadm.admin.dto.SmsComplaintDTO">
        select c.id, c.taskid, c.mdn, c.callmdn, c.res_content, c.sp_id, c.send_time,c.mdnProvice,c.complaintTime
        , c.create_time, c.url ,c.type,c.otype, c.remark,c.channelid,c.submit_time, sc.channel_name, sp.sp_name
        from (select id, taskid, mdn, callmdn,res_content, sp_id, send_time,mdnProvice,complaintTime,create_time, url ,
        type,otype, remark,channelid,case when submit_time is null then send_time else submit_time end submit_time
        from sms_complaint) c, sms_send_channel_info sc, sms_sp_info sp
        where c.channelid = sc.id and c.sp_id = sp.id

        <if test="query.spId != null and query.spId != ''">
            and c.sp_id = #{query.spId}
        </if>

        <if test="query.mdnProvice != null and query.mdnProvice != ''">
            and c.mdnProvice = #{query.mdnProvice}
        </if>

        <if test="query.channelId != null and query.channelId != ''">
            and c.channelid = #{query.channelId}
        </if>

        <if test="query.mdn != null and query.mdn != ''">
            and c.mdn = #{query.mdn}
        </if>

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

        <choose>
            <when test="query.queryTimeType!=null and query.queryTimeType ==1">
                <if test="query.beginTime!=null and query.beginTime!=''">
                    and c.send_time&gt;=#{query.beginTime}
                </if>
                <if test="query.endTime!=null and query.endTime!=''">
                    and c.send_time&lt;DATE_ADD(#{query.endTime},INTERVAL 1 DAY)
                </if>
            </when>
            <when test="query.queryTimeType!=null and query.queryTimeType ==2">
                <if test="query.beginTime!=null and query.beginTime!=''">
                    and c.create_time&gt;=#{query.beginTime}
                </if>
                <if test="query.endTime!=null and query.endTime!=''">
                    and c.create_time&lt;DATE_ADD(#{query.endTime},INTERVAL 1 DAY)
                </if>
            </when>
            <when test="query.queryTimeType!=null and query.queryTimeType ==3">
                <if test="query.beginTime!=null and query.beginTime!=''">
                    and c.complaintTime&gt;=#{query.beginTime}
                </if>
                <if test="query.endTime!=null and query.endTime!=''">
                    and c.complaintTime&lt;DATE_ADD(#{query.endTime},INTERVAL 1 DAY)
                </if>
            </when>
            <when test="query.queryTimeType!=null and query.queryTimeType ==4">
                <if test="query.beginTime!=null and query.beginTime!=''">
                    and c.submit_time&gt;=#{query.beginTime}
                </if>
                <if test="query.endTime!=null and query.endTime!=''">
                    and c.submit_time&lt;DATE_ADD(#{query.endTime},INTERVAL 1 DAY)
                </if>
            </when>
        </choose>
        order by c.create_time desc
    </select>

    <select id="exportComplaint" resultType="cn.roadm.admin.dto.SmsComplaintDTO">
        select c.id, c.taskid, c.mdn, c.callmdn, c.res_content, c.sp_id, c.send_time,c.mdnProvice,c.complaintTime
        , c.create_time, c.url ,c.type,c.otype, c.remark,c.channelid,c.submit_time, sc.channel_name, sp.sp_name
        from (select id, taskid, mdn, callmdn,res_content, sp_id, send_time,mdnProvice,complaintTime,create_time, url ,
        type,otype, remark,channelid,case when submit_time is null then send_time else submit_time end submit_time
        from sms_complaint) c, sms_send_channel_info sc, sms_sp_info sp
        where c.channelid = sc.id and c.sp_id = sp.id

        <if test="query.spId != null and query.spId != ''">
            and c.sp_id = #{spId}
        </if>

        <if test="query.mdn != null and query.mdn != ''">
            and c.mdn = #{query.mdn}
        </if>

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

        <if test="query.channelId != null and query.channelId != ''">
            and c.channelid = #{query.channelId}
        </if>
        <choose>
            <when test="query.queryTimeType!=null and query.queryTimeType ==1">
                <if test="query.beginTime!=null and query.beginTime!=''">
                    and c.send_time&gt;=#{query.beginTime}
                </if>
                <if test="query.endTime!=null and query.endTime!=''">
                    and c.send_time&lt;DATE_ADD(#{query.endTime},INTERVAL 1 DAY)
                </if>
            </when>
            <when test="query.queryTimeType!=null and query.queryTimeType ==2">
                <if test="query.beginTime!=null and query.beginTime!=''">
                    and c.create_time&gt;=#{query.beginTime}
                </if>
                <if test="query.endTime!=null and query.endTime!=''">
                    and c.create_time&lt;DATE_ADD(#{query.endTime},INTERVAL 1 DAY)
                </if>
            </when>
            <when test="query.queryTimeType!=null and query.queryTimeType ==3">
                <if test="query.beginTime!=null and query.beginTime!=''">
                    and c.complaintTime&gt;=#{query.beginTime}
                </if>
                <if test="query.endTime!=null and query.endTime!=''">
                    and c.complaintTime&lt;DATE_ADD(#{query.endTime},INTERVAL 1 DAY)
                </if>
            </when>
            <when test="query.queryTimeType!=null and query.queryTimeType ==4">
                <if test="query.beginTime!=null and query.beginTime!=''">
                    and c.submit_time&gt;=#{query.beginTime}
                </if>
                <if test="query.endTime!=null and query.endTime!=''">
                    and c.submit_time&lt;DATE_ADD(#{query.endTime},INTERVAL 1 DAY)
                </if>
            </when>
        </choose>
        order by c.create_time desc
    </select>

</mapper> 