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

    <select id="listPageSpMmsTaskTotal" resultType="cn.roadm.admin.dto.SmsSpSummaryDO">
        select sp.id spId,
        sp.sp_name spName,
        sum(IFNULL(tk.succ,0)+IFNULL(tk.fail,0)) allnumber,
        sum(IFNULL(tk.succ,0)) succ,
        sum(IFNULL(tk.fail,0)) fail,
        sum(IFNULL(tk.arrive_succ,0)) reportSucc,
        sum(IFNULL(tk.arrive_succ,0)+IFNULL(tk.arrive_fail,0)) reportCount,
        sum(IFNULL(tk.succ,0)+IFNULL(tk.fail,0)-IFNULL(tk.arrive_succ,0)-IFNULL(tk.arrive_fail,0)) unRecMsgnum,
        <if test="params.byDay != null and params.byDay==true">
            date_format(tk.stat_time,'%Y-%m-%d') createtime,
        </if>
        CASE
        WHEN sum(IFNULL(tk.succ,0)+IFNULL(tk.fail,0)) = 0 THEN 0
        ELSE sum(IFNULL(tk.succ,0)+IFNULL(tk.fail,0)-IFNULL(tk.arrive_succ,0)-IFNULL(tk.arrive_fail,0)) /sum(IFNULL(tk.succ,0)+IFNULL(tk.fail,0))
        END AS unRecRate,
        CASE
        WHEN sum(ifnull(tk.succ,0)) = 0 THEN 0
        ELSE sum(ifnull(tk.arrive_succ,0)) / sum(ifnull(tk.succ,0))
        END AS channelSuccRate,
        CASE
        WHEN sum(IFNULL(tk.succ,0)+IFNULL(tk.fail,0)) = 0 THEN 0
        ELSE sum(ifnull(tk.arrive_succ,0)) /sum(IFNULL(tk.succ,0)+IFNULL(tk.fail,0))
        END AS succRate,
        asa.total waitAuditCount,
        sum(IFNULL(tk.arrive_fail,0)) reportFail
        from sms_sp_info sp left join  mms_sp_summary tk on tk.spid = sp.id

        left join (select a.sp_id,sum(a.total) total  from sms_send_audit_task a
        where a.status = 1 and a.send_type in('200','20000')
        <if test="params.startTime!=null and params.startTime!=''">
            and a.createtime&gt;=str_to_date(#{params.startTime},'%Y-%m-%d')
        </if>
        <if test="params.endTime!=null and params.endTime!=''">
            and a.createtime&lt;DATE_ADD(str_to_date(#{params.endTime},'%Y-%m-%d'),INTERVAL 1 DAY)
        </if>
        <if test="params.startTime==null or params.startTime==''">
            and a.createtime&gt;=curdate()
        </if>
        group by a.sp_id) asa on tk.spid = asa.sp_id

        where 1=1

        <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 tk.spid=#{params.spId}
        </if>
        <if test="params.spSaler!=null and params.spSaler!=''">
            and sp.sp_saler =#{params.spSaler}
        </if>
        <if test="params.spType!=null and params.spType>0">
            and sp.sp_type =#{params.spType}
        </if>
        <if test="params.startTime!=null and params.startTime!=''">
            and tk.stat_time&gt;=str_to_date(#{params.startTime},'%Y-%m-%d')
        </if>
        <if test="params.endTime!=null and params.endTime!=''">
            and tk.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 tk.stat_time&gt;=curdate()
        </if>
        group by sp.id, sp.sp_name
        <if test="params.byDay != null and params.byDay==true">
            ,tk.stat_time
        </if>

    </select>

    <select id="listSpMmsTaskTotalCount" resultType="cn.roadm.admin.dto.SmsSpSummaryDO">
        select
        sum(IFNULL(tk.succ,0)+IFNULL(tk.fail,0)) allnumber,
        sum(IFNULL(tk.succ,0)) succ,
        sum(IFNULL(tk.fail,0)) fail,
        sum(IFNULL(tk.arrive_succ,0)) reportSucc,
        sum(IFNULL(tk.succ,0)+IFNULL(tk.fail,0)-IFNULL(tk.arrive_succ,0)-IFNULL(tk.arrive_fail,0)) unRecMsgnum,
        sum(IFNULL(tk.arrive_fail,0)) reportFail
        from mms_sp_summary tk, sms_sp_info sp
        where tk.spid = sp.id

        <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 tk.spid=#{params.spId}
        </if>
        <if test="params.spSaler!=null and params.spSaler!=''">
            and sp.sp_saler =#{params.spSaler}
        </if>
        <if test="params.spType!=null and params.spType>0">
            and sp.sp_type =#{params.spType}
        </if>

        <if test="params.startTime!=null and params.startTime!=''">
            and tk.stat_time&gt;=str_to_date(#{params.startTime},'%Y-%m-%d')
        </if>
        <if test="params.endTime!=null and params.endTime!=''">
            and tk.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 tk.stat_time&gt;=curdate()
        </if>

    </select>

    <select id="listPageSysPcoTaskTotal1" resultType="cn.roadm.admin.dto.SmsSpProvinceSummaryDO">
        SELECT
        <if test="(params.bySp!=null and params.bySp==true) or (params.spName!=null and params.spName!='')">
            t.spName,
        </if>
        <if test="(params.bySp!=null and params.bySp==true) or (params.spId!=null and params.spId>0)">
            t.spId,
        </if>
        <if test="params.byChannel!=null and params.byChannel==true">
            t.channelId,
        </if>
        <if test="params.byOtype!=null and params.byOtype==true">
            t.otypeName,
        </if>
        <if test="params.byProvince!=null and params.byProvince==true">
            t.province,
        </if>
        <if test="params.byDay != null and params.byDay==true">
            t.createtime,
        </if>
        t.allnumber,t.succ, t.fail,t.reportCount, t.reportSucc, t.reportFail,t.unRecMsgnum,

        CASE
        WHEN IFNULL(t.allnumber,0) = 0 THEN 0
        ELSE (IFNULL(t.succ,0)+IFNULL(t.fail,0)-IFNULL(t.reportSucc,0)-IFNULL(t.reportFail,0)) /(IFNULL(t.allnumber,0))
        END AS unRecRate,

        CASE
        WHEN (ifnull(t.succ,0)) = 0 THEN 0
        ELSE (ifnull(t.reportSucc,0)) / (ifnull(t.succ,0))
        END AS channelSuccRate,
        CASE
        WHEN (IFNULL(t.allnumber,0)) = 0 THEN 0
        ELSE (ifnull(t.reportSucc,0)) /(IFNULL(t.allnumber,0))
        END AS succRate

        <if test="params.byChannel!=null and params.byChannel==true">
            ,g.CHANNEL_NAME channelName
        </if>
        from (
        SELECT
        <if test="(params.bySp!=null and params.bySp==true) or (params.spName!=null and params.spName!='')">
            sp.sp_name spName,
        </if>
        <if test="(params.bySp!=null and params.bySp==true) or (params.spId!=null and params.spId>0)">
            sp.id spId,
        </if>
        <if test="params.byChannel!=null and params.byChannel==true">
            spp.channelid channelId,
        </if>
        <if test="params.byOtype!=null and params.byOtype==true">
            (case when spp.otype = '1' then "联通"
            when spp.otype = '2' then "移动"
            else "电信" end) otypeName,
        </if>
        <if test="params.byProvince!=null and params.byProvince==true">
            spp.province,
        </if>
        <if test="params.byDay != null and params.byDay==true">
            date_format(spp.stat_time,'%Y-%m-%d') createtime,
        </if>
        sum(IFNULL(spp.submit_succ,0)+IFNULL(spp.submit_fail,0)) allnumber,
        sum( IFNULL(spp.submit_succ, 0 ) ) succ,
        sum( IFNULL( spp.submit_fail, 0 ) ) fail,
        sum(IFNULL(spp.arrive_succ,0)+IFNULL(spp.arrive_fail,0)) reportCount,
        sum(IFNULL(spp.submit_succ,0)+IFNULL(spp.submit_fail,0)-IFNULL(spp.arrive_succ,0)-IFNULL(spp.arrive_fail,0)) unRecMsgnum,
        sum( IFNULL(spp.arrive_succ,0) ) reportSucc,
        sum( IFNULL(spp.arrive_fail,0) ) reportFail
        FROM
        mms_sp_province_summary spp,sms_sp_info sp
        WHERE
        spp.spid=sp.id

        <choose>
            <when test="params.byMonth != null and params.byMonth == &quot;1&quot;">
                <if test="params.month!=null and params.month!=''">
                    and date_format(spp.stat_time,'%Y-%m')=#{params.month}
                </if>
            </when>
            <when test="params.byMonth != null and params.byMonth == &quot;2&quot;">
                <if test="params.year!=null and params.year!=''">
                    and date_format(spp.stat_time,'%Y')=#{params.year}
                </if>
            </when>
            <otherwise>
                <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>
            </otherwise>
        </choose>

        <if test="params.byProvince!=null and params.byProvince==true and params.province!=null and params.province!=''">
            and spp.province=#{params.province}
        </if>
        <if test="params.byOtype!=null and params.byOtype==true and params.otype!=null and params.otype >0">
            and spp.otype=#{params.otype}
        </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 like CONCAT('%', #{params.spSaler}, '%')
        </if>
        <if test="params.spType!=null and params.spType!=''">
            and sp.sp_type = #{params.spType}
        </if>
        <if test="params.salerList!=null and params.salerList.size()>0">
            and sp.sp_saler in
            <foreach collection="params.salerList" index="index"  item="item" open="(" separator="," close=")">
                '${item}'
            </foreach>
        </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>
        <trim prefix="group by" suffixOverrides=",">
            <if test="(params.bySp!=null and params.bySp==true) or (params.spName!=null and params.spName!='')">
                sp.sp_name,
            </if>
            <if test="(params.bySp!=null and params.bySp==true) or (params.spId!=null and params.spId>0)">
                sp.id,
            </if>
            <if test="params.byChannel!=null and params.byChannel==true">
                spp.channelid,
            </if>
            <if test="params.byOtype!=null and params.byOtype==true">
                spp.otype,
            </if>
            <if test="params.byProvince!=null and params.byProvince==true">
                spp.province,
            </if>
            <if test="params.byDay!=null and params.byDay==true">
                spp.stat_time,
            </if>
        </trim>
        ) t

        <if test="(params.byChannel!=null and params.byChannel==true)">
            left join mms_send_channel_info g on t.channelId = g.id
        </if>
        <where>
            <if test="params.succ!=null and params.succ!=''">
                and t.succ &gt; #{params.succ}
            </if>
        </where>
    </select>

    <select id="getSysPcoTaskTotal" resultType="cn.roadm.admin.dto.SmsSpProvinceSummaryDO">
        select
        sum( IFNULL(succ,0) ) succ,
        sum( IFNULL( fail, 0 ) ) fail,
        sum( IFNULL(unRecMsgnum,0) ) unRecMsgnum,
        sum(IFNULL(succ,0)+IFNULL(fail,0)) allnumber,
        sum(IFNULL(reportSucc,0)+IFNULL(reportFail,0)) reportCount,
        sum( IFNULL(reportSucc,0) ) reportSucc,
        sum( IFNULL(reportFail,0) ) reportFail
        from (
        SELECT
        <if test="(params.bySp!=null and params.bySp==true) or (params.spName!=null and params.spName!='')">
            sp.sp_name spName,
        </if>
        <if test="(params.bySp!=null and params.bySp==true) or (params.spId!=null and params.spId>0)">
            sp.id spId,
        </if>
        <if test="params.byChannel!=null and params.byChannel==true">
            spp.channelid channelId,
        </if>
        <if test="params.byOtype!=null and params.byOtype==true">
            spp.otype,
        </if>
        <if test="params.byProvince!=null and params.byProvince==true">
            spp.province,
        </if>
        <if test="params.byDay != null and params.byDay==true">
            date_format(spp.stat_time,'%Y-%m-%d') createtime,
        </if>
        sum( IFNULL(spp.submit_succ, 0 ) ) succ,
        sum( IFNULL( spp.submit_fail, 0 ) ) fail,
        sum( IFNULL(spp.arrive_succ,0) ) reportSucc,
        sum(IFNULL(spp.submit_succ,0)+IFNULL(spp.submit_fail,0)-IFNULL(spp.arrive_succ,0)-IFNULL(spp.arrive_fail,0)) unRecMsgnum,
        sum( IFNULL(spp.arrive_fail,0) ) reportFail
        FROM
        mms_sp_province_summary spp,sms_sp_info sp
        WHERE
        spp.spid=sp.id
        <choose>
            <when test="params.byMonth != null and params.byMonth == &quot;1&quot;">
                <if test="params.month!=null and params.month!=''">
                    and date_format(spp.stat_time,'%Y-%m')=#{params.month}
                </if>
            </when>
            <when test="params.byMonth != null and params.byMonth == &quot;2&quot;">
                <if test="params.year!=null and params.year!=''">
                    and date_format(spp.stat_time,'%Y')=#{params.year}
                </if>
            </when>
            <otherwise>
                <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>
            </otherwise>
        </choose>
        <if test="params.spSaler!=null and params.spSaler!=''">
            and sp.sp_saler like CONCAT('%', #{params.spSaler}, '%')
        </if>
        <if test="params.salerList!=null and params.salerList.size()>0">
            and sp.sp_saler in
            <foreach collection="params.salerList" index="index"  item="item" open="(" separator="," close=")">
                '${item}'
            </foreach>
        </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!=''">
            and sp.sp_type = #{params.spType}
        </if>
        <if test="params.byProvince!=null and params.byProvince==true and params.province!=null and params.province!=''">
            and spp.province=#{params.province}
        </if>
        <if test="params.byOtype!=null and params.byOtype==true and params.otype!=null and params.otype >0">
            and spp.otype=#{params.otype}
        </if>
        <if test="params.byChannel!=null and params.byChannel==true and params.channelId!=null and params.channelId >=0">
            and spp.channelid=#{params.channelId}
        </if>
        <trim prefix="group by" suffixOverrides=",">
            <if test="(params.bySp!=null and params.bySp==true) or (params.spName!=null and params.spName!='')">
                sp.sp_name,
            </if>
            <if test="(params.bySp!=null and params.bySp==true) or (params.spId!=null and params.spId>0)">
                sp.id,
            </if>
            <if test="params.byChannel!=null and params.byChannel==true">
                spp.channelid,
            </if>
            <if test="params.byOtype!=null and params.byOtype==true">
                spp.otype,
            </if>
            <if test="params.byProvince!=null and params.byProvince==true">
                spp.province,
            </if>
            <if test="params.byDay!=null and params.byDay==true">
                spp.stat_time,
            </if>
        </trim>
        ) t
        <where>
            <if test="params.succ!=null and params.succ!=''">
                and t.succ &gt; #{params.succ}
            </if>
        </where>

    </select>

    <select id="getMmsChannelList" resultType="cn.roadm.admin.controller.vo.ChannelInfoQueryVO">
        select channelId,channelName from (select a.id channelId,  concat(a.id,' ',a.channel_name) channelName
        from mms_send_channel_info a
        union all select id channelId,concat(id,' ',channel_name) channelName from sms_send_channel_info  where protocol_type=20) s
        order by s.channelId desc
    </select>

    <select id="listPageMmsSignSummary" resultType="cn.roadm.admin.dto.MmsSpSignSummaryDO">
        select  mpt.sign,
        <if test='params.bySp != null and params.bySp == true'>
            mpt.spid spId, sp.sp_name spName,
        </if>
        <if test='params.byDay != null and params.byDay == true'>
            date_format(mpt.stat_time,'%Y-%m-%d') createtime,
        </if>
        <if test="params.byOtype != null and params.byOtype ==true ">
            (case when mpt.otype = '1' then "联通"
            when mpt.otype = '2' then "移动"
            else "电信" end) otypeName,
        </if>
        <if test="params.byChannel != null and params.byChannel ==true ">
            mpt.channelid channelId,
            ssci.CHANNEL_NAME channelName,
        </if>
        <if test="params.byTemp != null and params.byTemp ==true ">
            mpt.content tempId,
        </if>
        (CASE WHEN sum(IFNULL( mpt.submit_succ, 0) + IFNULL(mpt.submit_fail, 0)) = 0 THEN 0
        ELSE  Round(sum(IFNULL( mpt.submit_succ, 0) + IFNULL(mpt.submit_fail, 0)-IFNULL( mpt.arrive_succ, 0) - IFNULL(mpt.arrive_fail, 0))  /sum(IFNULL( mpt.submit_succ, 0) + IFNULL(mpt.submit_fail, 0) ) * 100, 2 )
        END ) unRecRate,

        (CASE WHEN sum(ifnull(mpt.submit_succ,0)) = 0 THEN 0
        ELSE  Round( sum( IFNULL( mpt.arrive_succ,0) )/ sum( IFNULL(mpt.submit_succ,0) )* 100, 2 )
        END ) channelSuccRate,

        (CASE WHEN  sum(IFNULL( mpt.submit_succ, 0) + IFNULL(mpt.submit_fail, 0)) = 0 THEN 0
        ELSE Round( sum( IFNULL( mpt.arrive_succ,0) )/ sum( IFNULL( mpt.submit_succ, 0) + IFNULL(mpt.submit_fail, 0) )* 100, 2 )
        END ) succRate,
        sum(IFNULL( mpt.submit_succ, 0) + IFNULL(mpt.submit_fail, 0)) allnumber,
        sum( IFNULL( mpt.arrive_succ, 0) + IFNULL(mpt.arrive_fail, 0)) reportCount,
        sum( IFNULL( mpt.submit_succ, 0) + IFNULL(mpt.submit_fail, 0)-IFNULL( mpt.arrive_succ, 0) - IFNULL(mpt.arrive_fail, 0)) unRecMsgnum,
        sum(IFNULL(mpt.submit_succ, 0)) succ,
        sum(IFNULL(mpt.submit_fail, 0)) fail,
        sum(IFNULL(mpt.arrive_succ, 0)) reportSucc,
        sum(IFNULL(mpt.arrive_fail, 0)) reportFail
        from mms_sp_tem_summary mpt left join sms_sp_info sp on mpt.spid = sp.id left join (select id,CHANNEL_NAME from mms_send_channel_info union all select id,CHANNEL_NAME from sms_send_channel_info where protocol_type=20 ) ssci on mpt.channelid=ssci.id
        <where>
            <if test="params.spId != null">
                mpt.spid = #{params.spId}
            </if>
            <if test="params.spName != null and params.spName != ''">
                and sp.sp_name like CONCAT('%',#{params.spName},'%')
            </if>
            <if test="params.sign != null and params.sign != ''">
                and mpt.sign like CONCAT('%',#{params.sign},'%')
            </if>
            <if test="params.startTime != null and params.startTime != ''">
                and mpt.stat_time &gt;= #{params.startTime}
            </if>
            <if test="params.endTime != null and params.endTime != ''">
                and mpt.stat_time &lt;= #{params.endTime}
            </if>
            <if test="params.startTime==null or params.startTime==''">
                and mpt.stat_time&gt;=curdate()
            </if>
            <if test="params.keyword!=null and params.keyword!=''">
                and mpt.content =#{params.keyword}
            </if>
        </where>
        group by
        <if test='params.bySp != null and params.bySp == true'>
            spid, spName,
        </if>
        <if test='params.byDay != null and params.byDay == true'>
            mpt.stat_time,
        </if>
        <if test="params.byOtype != null and params.byOtype ==true">
            mpt.otype,
        </if>
        <if test="params.byChannel != null and params.byChannel ==true">
            mpt.channelid,
            ssci.CHANNEL_NAME,
        </if>
        <if test="params.byTemp != null and params.byTemp ==true ">
            mpt.content,
        </if>
        mpt.sign

    </select>

    <select id="selectSumSign" resultType="cn.roadm.admin.dto.MmsSpSignSummaryDO">
        select
        (case when sum( IFNULL(mpt.submit_succ,0) )=0 then 0 else CONCAT( Round( sum( IFNULL(mpt.arrive_succ,0) )/ sum( IFNULL(mpt.submit_succ,0) )* 100, 2 ), '%' ) end) channelSuccRate,
        (case when sum( IFNULL(mpt.submit_succ,0)+IFNULL(mpt.submit_fail,0) )=0 then 0 else CONCAT( Round( sum( IFNULL(mpt.arrive_succ,0) )/ sum( IFNULL(mpt.submit_succ,0)+IFNULL(mpt.submit_fail,0) )* 100, 2 ), '%' ) end) succRate,
        sum(IFNULL(mpt.submit_succ, 0)) succ,
        sum(IFNULL(mpt.submit_fail, 0)) fail,
        sum(IFNULL(mpt.arrive_succ, 0)) reportSucc,
        sum(IFNULL(mpt.arrive_fail, 0)) reportFail
        from mms_sp_tem_summary mpt left join sms_sp_info sp on mpt.spid = sp.id
        <where>
            <if test="params.spId != null">
                mpt.spid = #{params.spId}
            </if>
            <if test="params.spName != null and params.spName != ''">
                and sp.sp_name like CONCAT('%',#{params.spName},'%')
            </if>
            <if test="params.sign != null and params.sign != ''">
                and mpt.sign like CONCAT('%',#{params.sign},'%')
            </if>
            <if test="params.startTime != null and params.startTime != ''">
                and mpt.stat_time &gt;= #{params.startTime}
            </if>
            <if test="params.endTime != null and params.endTime != ''">
                and mpt.stat_time &lt;= #{params.endTime}
            </if>
            <if test="params.startTime==null or params.startTime==''">
                and mpt.stat_time&gt;=curdate()
            </if>
            <if test="params.keyword!=null and params.keyword!=''">
                and mpt.content =#{params.keyword}
            </if>
        </where>
    </select>



</mapper>