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

    <select id="listPageComplaintTotal"  resultType="cn.roadm.admin.dto.ComplaintDO">
        select * from (
        SELECT

        <if test="(params.bySp!=null and params.bySp==true) or (params.spName!=null and params.spName!='') or (params.spId!=null and params.spId>0)">
            sp.sp_name spName,sp.id spId,
        </if>
        <if test="params.byChannel!=null and params.byChannel==true">
            spp.channelid channelId,
        </if>
        <if test="params.byDay != null and params.byDay==true">
            date_format(spp.stat_time, '%Y-%m-%d') createtime,
        </if>

        sum(ifnull(spp.arrive_succ, 0)) reportSucc,
        sum(ifnull(complaintNumber,0)) complaintNumber,
        sum(ifnull(complaintNumber12321,0)) complaintNumber12321,
        sum(ifnull(complaintNumberOp,0)) complaintNumberOp,
        (case when sum(ifnull(spp.arrive_succ,0))=0 then 0 else Round( sum(ifnull(complaintNumber,0))*1000000/sum(spp.arrive_succ),2) end) complaintRate,
        (case when sum(ifnull(spp.arrive_succ,0))=0 then 0 else Round(sum(ifnull(complaintNumber12321,0))*1000000/sum(spp.arrive_succ),2) end) complaint12321Rate,

        <if test="(params.bySp!=null and params.bySp==true) or (params.spName!=null and params.spName!='') or (params.spId!=null and params.spId>0)">
        (case when sp.noProfit=1 then 0 else Round((sum(spp.income)-sum(spp.cost))/(sum(complaintNumber)*10000),2) end ) profit,
        </if>
        (case when sum(ifnull(spp.arrive_succ,0))=0 then 0 else Round(sum(ifnull(complaintNumberOp,0))*1000000/sum(spp.arrive_succ),2) end) complaintOpRate

        FROM
        (
        select spid, channelid, stat_time, sum(ifnull(arrive_succ,0)) arrive_succ,sum(ifnull(arrive_succ,0)*ifnull(sp_price,0)) income,
        sum(ifnull(arrive_succ,0)*ifnull(channel_price,0)) cost
        from sms_sp_province_summary
        <where>
            <if test="params.startTime!=null and params.startTime!=''">
                and stat_time&gt;=str_to_date(#{params.startTime},'%Y-%m-%d')
            </if>
            <if test="params.endTime!=null and params.endTime!=''">
                and stat_time&lt;DATE_ADD(str_to_date(#{params.endTime},'%Y-%m-%d'),INTERVAL
                1 DAY)
            </if>
            <if test="params.startTime==null or params.startTime==''">
                and stat_time&gt;=curdate()
            </if>
            <if test="params.byChannel!=null and params.byChannel==true and params.channelId!=null and params.channelId >=0">
                and channelid=#{channelid}
            </if>
            <if test="params.spId!=null and params.spId>0">
                and spid=#{params.spId}
            </if>
        </where>
        group by spid, channelid, stat_time
        ) spp
        LEFT JOIN sms_sp_info sp ON spp.spid = sp.id
        LEFT JOIN

        (
        select channelid,sp_id,to_days(submit_time) submit_time,count(id) complaintNumber,count(case when type=0 then 1 end) complaintNumber12321,count(case when type>0 then 1 end) complaintNumberOp  from
        (select id,sp_id,channelid,case when submit_time is null then send_time else submit_time end submit_time,
        send_time,create_time,complaintTime,type
        from sms_complaint) tmp
        where 1=1

        <choose>
            <when test="params.queryTimeType!=null and params.queryTimeType ==1">
                <if test="params.startTime!=null and params.startTime!=''">
                    and send_time&gt;=#{params.startTime}
                </if>
                <if test="params.endTime!=null and params.endTime!=''">
                    and send_time&lt;DATE_ADD(#{params.endTime},INTERVAL 1 DAY)
                </if>
                <if test="params.startTime==null or params.startTime==''">
                    and send_time&gt;=curdate()
                </if>
            </when>
            <when test="params.queryTimeType!=null and params.queryTimeType ==2">
                <if test="params.startTime!=null and params.startTime!=''">
                    and create_time&gt;=#{params.startTime}
                </if>
                <if test="params.endTime!=null and params.endTime!=''">
                    and create_time&lt;DATE_ADD(#{params.endTime},INTERVAL 1 DAY)
                </if>
                <if test="params.startTime==null or params.startTime==''">
                    and create_time&gt;=curdate()
                </if>
            </when>
            <when test="params.queryTimeType!=null and params.queryTimeType ==3">
                <if test="params.startTime!=null and params.startTime!=''">
                    and complaintTime&gt;=#{params.startTime}
                </if>
                <if test="params.endTime!=null and params.endTime!=''">
                    and complaintTime&lt;DATE_ADD(#{params.endTime},INTERVAL 1 DAY)
                </if>
                <if test="params.startTime==null or params.startTime==''">
                    and complaintTime&gt;=curdate()
                </if>
            </when>
            <when test="params.queryTimeType!=null and params.queryTimeType ==4">
                <if test="params.startTime!=null and params.startTime!=''">
                    and submit_time&gt;=#{params.startTime}
                </if>
                <if test="params.endTime!=null and params.endTime!=''">
                    and submit_time&lt;DATE_ADD(#{params.endTime},INTERVAL 1 DAY)
                </if>
                <if test="params.startTime==null or params.startTime==''">
                    and submit_time&gt;=curdate()
                </if>
            </when>
        </choose>

        <if test="params.spId!=null and params.spId>0">
            and sp_id=#{params.spId}
        </if>
        group by channelid,sp_id,to_days(submit_time )
        )
        c ON spp.spid = c.sp_id
        AND spp.channelid = c.channelid
        AND to_days( spp.stat_time ) = c.submit_time
        <where>
            <if test="params.startTime!=null and params.startTime!=''">
                and spp.stat_time&gt;=str_to_date(#{params.startTime},'%Y-%m-%d')
            </if>
            <if test="params.endTime!=null and params.endTime!=''">
                and
                spp.stat_time&lt;DATE_ADD(str_to_date(#{params.endTime},'%Y-%m-%d'),INTERVAL
                1 DAY)
            </if>
            <if test="params.startTime==null or params.startTime==''">
                and spp.stat_time&gt;=curdate()
            </if>
            <if test="params.byChannel!=null and params.byChannel==true and params.channelId!=null and params.channelId >=0">
                and spp.channelid=#{params.channelId}
            </if>
            <if test="params.spSaler!=null and params.spSaler!=''">
                and sp.sp_saler =#{params.spSaler}
            </if>
            <if test="params.spName!=null and params.spName!=''">
                and sp.sp_name like CONCAT('%',#{params.spName},'%')
            </if>
            <if test="params.spId!=null and params.spId>0">
                and spp.spid=#{params.spId}
            </if>
            <if test="params.spType!=null and params.spType>0">
                and sp.sp_type =#{params.spType}
            </if>
        </where>
        <trim prefix="group by" suffixOverrides=",">
            <if test="(params.bySp!=null and params.bySp==true) or (params.spName!=null and params.spName!='') or (params.spId!=null and params.spId>0)">
                sp.sp_name, sp.id,
            </if>
            <if test="params.byChannel!=null and params.byChannel==true">
                spp.channelid,
            </if>
            <if test="params.byDay!=null and params.byDay==true">
                spp.stat_time,
            </if>
        </trim>
        ) sm where complaintNumber > 0


    </select>

    <select id="getComplaintTotal"  resultType="cn.roadm.admin.dto.ComplaintDO">
        select
        sum(ifnull(arrive_succ, 0)) reportSucc,
        sum(ifnull(complaintNumber,0)) complaintNumber,
        sum(ifnull(complaintNumber12321,0)) complaintNumber12321,
        sum(ifnull(complaintNumberOp,0)) complaintNumberOp,
        (case when sum(ifnull(arrive_succ,0))=0 then 0 else Round( sum(ifnull(complaintNumber,0))*1000000/sum(arrive_succ),2) end) complaintRate,
        (case when sum(ifnull(arrive_succ,0))=0 then 0 else Round(sum(ifnull(complaintNumber12321,0))*1000000/sum(arrive_succ),2) end) complaint12321Rate,

        <if test="(params.bySp!=null and params.bySp==true) or (params.spName!=null and params.spName!='') or (params.spId!=null and params.spId>0)">
        Round(sum(profit)/sum(complaintNumber),2) profit,
        </if>
        (case when sum(ifnull(arrive_succ,0))=0 then 0 else Round(sum(ifnull(complaintNumberOp,0))*1000000/sum(arrive_succ),2) end) complaintOpRate
        from (
        SELECT

        <if test="(params.bySp!=null and params.bySp==true) or (params.spId!=null and params.spId>0) or (params.spName!=null and params.spName!='')">
            sp.id spId, sp.sp_name spName,
        </if>
        <if test="params.byChannel!=null and params.byChannel==true">
            spp.channelid channelId,
        </if>
        <if test="params.byDay != null and params.byDay==true">
            spp.stat_time createtime,
        </if>

        sum(ifnull(spp.arrive_succ, 0)) arrive_succ,
        sum(ifnull(complaintNumber,0)) complaintNumber,
        sum(ifnull(complaintNumber12321,0)) complaintNumber12321,

        <if test="(params.bySp!=null and params.bySp==true) or (params.spName!=null and params.spName!='') or (params.spId!=null and params.spId>0)">
            (case when sp.noProfit=1 then 0 else (sum(spp.income)-sum(spp.cost))/10000 end ) profit,
        </if>
        sum(ifnull(complaintNumberOp,0)) complaintNumberOp
        FROM
        (
        select spid, channelid, stat_time, sum(arrive_succ) arrive_succ,sum(ifnull(arrive_succ,0)*ifnull(sp_price,0)) income,
        sum(ifnull(arrive_succ,0)*ifnull(channel_price,0)) cost
        from sms_sp_province_summary
        <where>
            <if test="params.startTime!=null and params.startTime!=''">
                and stat_time&gt;=str_to_date(#{params.startTime},'%Y-%m-%d')
            </if>
            <if test="params.endTime!=null and params.endTime!=''">
                and stat_time&lt;DATE_ADD(str_to_date(#{params.endTime},'%Y-%m-%d'),INTERVAL
                1 DAY)
            </if>
            <if test="params.startTime==null or params.startTime==''">
                and stat_time&gt;=curdate()
            </if>
            <if test="params.byChannel!=null and params.byChannel==true and params.channelId!=null and params.channelId >=0">
                and channelid=#{channelid}
            </if>
            <if test="params.spId!=null and params.spId>0">
                and spid=#{params.spId}
            </if>
        </where>
        group by spid, channelid, stat_time
        )spp
        LEFT JOIN sms_sp_info sp ON spp.spid = sp.id
        LEFT JOIN

        (
        select channelid,sp_id,to_days(submit_time) submit_time,count(id) complaintNumber,count(case when type=0 then 1 end) complaintNumber12321,count(case when type>0 then 1 end) complaintNumberOp  from
        (select id,sp_id,channelid,case when submit_time is null then send_time else submit_time end submit_time,
        send_time,create_time,complaintTime,type
        from sms_complaint) tmp
        where 1=1

        <choose>
            <when test="params.queryTimeType!=null and params.queryTimeType ==1">
                <if test="params.startTime!=null and params.startTime!=''">
                    and send_time&gt;=#{params.startTime}
                </if>
                <if test="params.endTime!=null and params.endTime!=''">
                    and send_time&lt;DATE_ADD(#{params.endTime},INTERVAL 1 DAY)
                </if>
                <if test="params.startTime==null or params.startTime==''">
                    and send_time&gt;=curdate()
                </if>
            </when>
            <when test="params.queryTimeType!=null and params.queryTimeType ==2">
                <if test="params.startTime!=null and params.startTime!=''">
                    and create_time&gt;=#{params.startTime}
                </if>
                <if test="params.endTime!=null and params.endTime!=''">
                    and create_time&lt;DATE_ADD(#{params.endTime},INTERVAL 1 DAY)
                </if>
                <if test="params.startTime==null or params.startTime==''">
                    and create_time&gt;=curdate()
                </if>
            </when>
            <when test="params.queryTimeType!=null and params.queryTimeType ==3">
                <if test="params.startTime!=null and params.startTime!=''">
                    and complaintTime&gt;=#{params.startTime}
                </if>
                <if test="params.endTime!=null and params.endTime!=''">
                    and complaintTime&lt;DATE_ADD(#{params.endTime},INTERVAL 1 DAY)
                </if>
                <if test="params.startTime==null or params.startTime==''">
                    and complaintTime&gt;=curdate()
                </if>
            </when>
            <when test="params.queryTimeType!=null and params.queryTimeType ==4">
                <if test="params.startTime!=null and params.startTime!=''">
                    and submit_time&gt;=#{params.startTime}
                </if>
                <if test="params.endTime!=null and params.endTime!=''">
                    and submit_time&lt;DATE_ADD(#{params.endTime},INTERVAL 1 DAY)
                </if>
                <if test="params.startTime==null or params.startTime==''">
                    and submit_time&gt;=curdate()
                </if>
            </when>
        </choose>

        <if test="params.spId!=null and params.spId>0">
            and sp_id=#{params.spId}
        </if>
        group by channelid,sp_id,to_days(submit_time )
        )
        c ON spp.spid = c.sp_id
        AND spp.channelid = c.channelid
        AND to_days( spp.stat_time ) = c.submit_time
        <where>
            <if test="params.startTime!=null and params.startTime!=''">
                and spp.stat_time&gt;=str_to_date(#{params.startTime},'%Y-%m-%d')
            </if>
            <if test="params.endTime!=null and params.endTime!=''">
                and
                spp.stat_time&lt;DATE_ADD(str_to_date(#{params.endTime},'%Y-%m-%d'),INTERVAL
                1 DAY)
            </if>
            <if test="params.startTime==null or params.startTime==''">
                and spp.stat_time&gt;=curdate()
            </if>
            <if test="params.byChannel!=null and params.byChannel==true and params.channelId!=null and params.channelId >=0">
                and spp.channelid=#{params.channelId}
            </if>
            <if test="params.spSaler!=null and params.spSaler!=''">
                and sp.sp_saler =#{params.spSaler}
            </if>
            <if test="params.spName!=null and params.spName!=''">
                and sp.sp_name like CONCAT('%',#{params.spName},'%')
            </if>
            <if test="params.spId!=null and params.spId>0">
                and spp.spid=#{params.spId}
            </if>
            <if test="params.spType!=null and params.spType>0">
                and sp.sp_type =#{params.spType}
            </if>
        </where>
        <trim prefix="group by" suffixOverrides=",">
            <if test="(params.bySp!=null and params.bySp==true) or (params.spName!=null and params.spName!='') or (params.spId!=null and params.spId>0)">
                sp.sp_name,sp.id,
            </if>
            <if test="params.byChannel!=null and params.byChannel==true">
                spp.channelid,
            </if>
            <if test="params.byDay!=null and params.byDay==true">
                spp.stat_time,
            </if>
        </trim>
        ) sm where complaintNumber > 0

    </select>

    <select id="getComplaintRecordCount" resultType="java.lang.Integer">
        select count(c.id)
        from sms_complaint c
        <where>
            <if test="channelId != null and channelId != ''">
                and c.channelid = #{channelId}
            </if>

            <if test="mdn != null and mdn != ''">
                and c.mdn = #{mdn}
            </if>
            <if test="callMdn != null and callMdn != ''">
                and c.callmdn = #{callMdn}
            </if>
        </where>
    </select>

    <insert id="addComplaintRecord">
        insert into sms_complaint
        (taskid, mdn, callmdn, res_content, sp_id, send_time, create_time, channelid, sign, otype, url, type,
         remark, mdnProvice, complaintTime, submit_time)
        values ( #{taskId}, #{mdn}, #{callMdn}, #{content}, #{spId}, #{submitTime}
               , now(), #{channelId}, #{sign}, #{otype}, #{sourceLink}, #{complaintSource}, #{remark}, #{mdnProvince}, #{complaintTime}
               , #{submitTime})
    </insert>
</mapper>
