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


    <select id="listPageSpTaskTotal" resultType="cn.roadm.admin.dto.SmsSpSummaryDO">
        select sp.id spId,
        sp.sp_name spName,
        sum(IFNULL(tk.allnumber,0)) allnumber,
        sum(IFNULL(tk.succ,0)) succ,
        sum(IFNULL(tk.fail,0)) fail,
        sum(IFNULL(tk.arrive_succ,0)+IFNULL(tk.arrive_fail,0)) reportCount,
        sum(IFNULL(tk.arrive_succ,0)) reportSucc,
        sum(IFNULL(tk.arrive_fail,0)) reportFail,
        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.allnumber,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.allnumber,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.allnumber,0)) = 0 THEN 0
        ELSE sum(ifnull(tk.arrive_succ,0)) /sum(IFNULL(tk.allnumber,0))
        END AS succRate
        from sms_sp_info sp left join  sms_sp_summary tk on tk.spid = sp.id
        where 1=1
        <if test="params.spName!=null and params.spName!=''">
            and sp.sp_name like CONCAT('%',#{params.spName},'%')
        </if>

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

        <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.managerId != null and params.managerId != ''">
            and sp.sp_saler in (select ifnull(seller,'') from sale_manager sm where managerId=#{params.managerId}) and sp.sp_saler != ''
        </if>

        group by sp.id, sp.sp_name
        <if test="params.byDay != null and params.byDay==true">
            ,tk.stat_time
        </if>

    </select>


    <select id="listSpTaskTotalCount" resultType="cn.roadm.admin.dto.SmsSpSummaryDO">
        select
        sum(IFNULL(tk.allnumber,0)) allnumber,

        sum(IFNULL(tk.succ,0)) succ,
        sum(IFNULL(tk.fail,0)) fail,
        sum(IFNULL(tk.arrive_succ,0)+IFNULL(tk.arrive_fail,0)) reportCount,
        sum(IFNULL(tk.arrive_succ,0)) reportSucc,
        sum(IFNULL(tk.arrive_fail,0)) reportFail,
        sum(IFNULL(tk.succ,0)+IFNULL(tk.fail,0)-IFNULL(tk.arrive_succ,0)-IFNULL(tk.arrive_fail,0)) unRecMsgnum
        from sms_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.managerId != null and params.managerId != ''">
            and sp.sp_saler in (select ifnull(seller,'') from sale_manager sm where managerId=#{params.managerId}) and sp.sp_saler != ''
        </if>

        <choose>
            <when test="params.byMonth != null and params.byMonth == &quot;1&quot;">
                <if test="params.month!=null and params.month!=''">
                    and date_format(tk.stat_time,'%Y-%m')=#{params.month}
                </if>
            </when>
            <otherwise>
                <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>
            </otherwise>
        </choose>
    </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.bySp!=null and params.bySp==true) or (params.spId!=null and params.spId>0)">
            ,s.clientid
        </if>
        <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
        sms_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.bySp!=null and params.bySp==true) or (params.spId!=null and params.spId>0)">
            left join sms_api_socket_sp s on t.spId = s.spid
        </if>
        <if test="(params.byChannel!=null and params.byChannel==true)">
            left join sms_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(succ+fail) allnumber,
        sum(reportSucc+reportFail) 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
        sms_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="waitAuditCount" resultType="Integer">

        select sum(a.allnumber) wait_audit  from sms_send_audit_task a
        where a.status = 1
        and a.send_type in('1000','10000')
        and a.sp_id=#{params.spId}
        <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>

    </select>

    <select id="waitApiAuditCount" resultType="Integer">

        select sum(api.allnumber) wait_audit  from sms_api_audit_task api
        where api.status = 1
        and api.send_type in('1000','10000')
        and api.sp_id=#{params.spId}
        <if test="params.startTime!=null and params.startTime!=''">
            and api.createtime&gt;=str_to_date(#{params.startTime},'%Y-%m-%d')
        </if>
        <if test="params.endTime!=null and params.endTime!=''">
            and
            api.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 api.createtime&gt;=curdate()
        </if>
    </select>

    <select id="crontabCount" resultType="Integer">

        select sum(st.allnumber) crontab  from sms_crontab_send b
        left join sms_send_task st on st.id = b.taskid
        where b.status =0 and b.sp_id=#{params.spId} and st.status in (0,1)
        <if test="params.startTime!=null and params.startTime!=''">
            and b.createtime&gt;=str_to_date(#{params.startTime},'%Y-%m-%d')
            and st.createtime &gt;= str_to_date( #{params.startTime}, '%Y-%m-%d' )
        </if>
        <if test="params.endTime!=null and params.endTime!=''">
            and
            b.createtime&lt;DATE_ADD(str_to_date(#{params.endTime},'%Y-%m-%d'),INTERVAL 1 DAY)
            and st.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 b.createtime&gt;=curdate()
            and st.createtime &gt;=curdate()
        </if>
    </select>

    <select id="crontabApiCount" resultType="Integer">

        select sum(b.allnumber) crontab  from sms_api_crontab_task b
        where b.sp_id=#{params.spId} and b.status = 2
        <if test="params.startTime!=null and params.startTime!=''">
            and b.createtime&gt;=str_to_date(#{params.startTime},'%Y-%m-%d')
        </if>
        <if test="params.endTime!=null and params.endTime!=''">
            and
            b.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 b.createtime&gt;=curdate()
        </if>
    </select>

    <select id="listPageSignTaskTotal"  resultType="cn.roadm.admin.dto.SmsSpSignSummaryDO">
        select * 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,
            ssci.CHANNEL_NAME channelName,
        </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.byDay != null and params.byDay==true">
            date_format(spp.stat_time,'%Y-%m-%d') createtime,
        </if>
        (CASE WHEN sum(IFNULL( spp.submit_succ, 0) + IFNULL(spp.submit_fail, 0)) = 0 THEN 0
        ELSE  Round(sum(IFNULL( spp.submit_succ, 0) + IFNULL(spp.submit_fail, 0)-IFNULL( spp.arrive_succ, 0) - IFNULL(spp.arrive_fail, 0))  /sum(IFNULL( spp.submit_succ, 0) + IFNULL(spp.submit_fail, 0) ) * 100, 2 )
        END ) unRecRate,

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

        (CASE WHEN  sum(IFNULL( spp.submit_succ, 0) + IFNULL(spp.submit_fail, 0)) = 0 THEN 0
        ELSE  Round( sum( IFNULL( spp.arrive_succ,0) )/ sum( IFNULL( spp.submit_succ, 0) + IFNULL(spp.submit_fail, 0) )* 100, 2 )
        END ) succRate,

        sum( IFNULL(spp.submit_succ,0) ) succ,
        sum( IFNULL( spp.submit_fail, 0 ) ) fail,
        sum(IFNULL( spp.submit_succ, 0) + IFNULL(spp.submit_fail, 0)) allnumber,
        sum( IFNULL(spp.arrive_succ,0) ) reportSucc,
        sum( IFNULL(spp.arrive_fail,0) ) reportFail,
        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,
        spp.sign sign
        FROM
        sms_sp_sign_summary spp,sms_sp_info sp,sms_send_channel_info ssci
        WHERE
        spp.spid=sp.id and spp.channelid = ssci.id
        <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.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>
        <if test="params.managerId != null and params.managerId != ''">
            and sp.sp_saler in (select ifnull(seller,'') from sale_manager sm where managerId=#{params.managerId}) and sp.sp_saler != ''
        </if>

        <if test="params.business!=null and params.business!=''">
            and ssci.business_manager =#{params.business}
        </if>
        <if test="params.businessManagerId != null and params.businessManagerId != ''">
            and ssci.business_manager in
            (select ifnull(business, '') from business_manager bm where managerId=#{params.businessManagerId})
            and ssci.business_manager != ''
        </if>
        <if test="params.sign!=null and params.sign!=''">
            and spp.sign=#{params.sign}
        </if>

        <trim prefix="group by" suffixOverrides=",">
            spp.sign,

            <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,
                ssci.CHANNEL_NAME,
            </if>
            <if test="params.byOtype!=null and params.byOtype==true">
                spp.otype,
            </if>
            <if test="params.byDay!=null and params.byDay==true">
                spp.stat_time,
            </if>

        </trim>
        ) as spp
        <where>
            <if test="params.keyword != null and params.keyword != ''">
                and
                <foreach item="word" collection="params.keyword.split(',')" separator="OR">
                    spp.sign LIKE CONCAT('%', '${word}', '%')
                </foreach>
            </if>
        </where>

    </select>

    <select id="getSignTaskTotal" resultType="cn.roadm.admin.dto.SmsSpSignSummaryDO">
        SELECT
        CONCAT( Round( sum( spp.arrive_succ )/ sum( spp.succ )* 100, 2 ), '%' ) channelSuccRate,
        CONCAT( Round( sum( spp.arrive_succ )/ sum( spp.succ+spp.fail )* 100, 2 ), '%' ) succRate,
        sum( IFNULL(spp.succ,0) ) as succ,
        sum( IFNULL( spp.fail, 0 ) ) as fail,
        sum( IFNULL(spp.arrive_succ,0) ) as reportSucc,
        sum( IFNULL(spp.arrive_fail,0) ) as reportFail
        FROM (
        SELECT
        sum( ifnull(spp.submit_succ,0) ) succ,
        sum( IFNULL( spp.submit_fail, 0 ) ) fail,
        sum( ifnull(spp.arrive_succ,0) ) arrive_succ,
        sum( ifnull(spp.arrive_fail,0) ) arrive_fail,
        sign
        FROM
        sms_sp_sign_summary spp,sms_sp_info sp,sms_send_channel_info ssci
        WHERE
        spp.spid=sp.id and spp.channelid = ssci.id
        <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.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>
        <if test="params.managerId != null and params.managerId != ''">
            and sp.sp_saler in (select ifnull(seller,'') from sale_manager sm where managerId=#{params.managerId}) and sp.sp_saler != ''
        </if>

        <if test="params.business!=null and params.business!=''">
            and ssci.business_manager =#{params.business}
        </if>
        <if test="params.businessManagerId != null and params.businessManagerId != ''">
            and ssci.business_manager in
            (select ifnull(business, '') from business_manager bm where managerId=#{params.businessManagerId})
            and ssci.business_manager != ''
        </if>
        <if test="params.sign!=null and params.sign!=''">
            and spp.sign=#{params.sign}
        </if>
        GROUP BY spp.sign
        ) as spp
        <where>
            <if test="params.keyword != null and params.keyword != ''">
                <foreach item="word" collection="params.keyword.split(',')" separator="OR">
                    spp.sign LIKE CONCAT('%', '${word}', '%')
                </foreach>
            </if>
        </where>
    </select>


    <!-- 错误码统计 -->
    <select id="listPageSpErrCodeTotal" resultType="cn.roadm.admin.dto.SmsSpErrCodeSummaryDO">
        select a.*,sec.reason from
        (select sum(tk.allnumber) allnumber,
        <if test="params.bySp != null and params.bySp ==true">
            sp.id spId, sp.sp_name spName,
        </if>
        <if test="params.byChannel != null and params.byChannel ==true">
            tk.channelid channelId,
            ssci.CHANNEL_NAME channelName,
        </if>
        <if test="params.bySign != null and params.bySign ==true">
            tk.sign,
        </if>
        <if test="params.byProvince != null and params.byProvince ==true">
            tk.province,
        </if>
        <if test="params.byDay != null and params.byDay==true">
            date_format(tk.stat_time,'%Y-%m-%d') createtime,
        </if>
        tk.errcode errCode
        from sms_sp_info sp, sms_errcode_summary tk,sms_send_channel_info ssci
        <where>
             tk.spid = sp.id
            and tk.channelid=ssci.id
            <if test="params.spName != null and params.spName != ''">
                and sp.sp_name like CONCAT('%',#{params.spName},'%')
            </if>
            <if test="params.salerList != null and params.salerList.size() > 0">
                and sp.sp_saler in
                <foreach collection="params.salerList" item="item" open="(" separator="," close=")">
                    '${item}'
                </foreach>
            </if>
            <if test="params.spSaler!=null and params.spSaler!=''">
                and sp.sp_saler =#{params.spSaler}
            </if>
            <if test="params.spId!=null and params.spId>0">
                and tk.spid=#{params.spId}
            </if>
            <if test="params.channelId !=null and params.channelId >=0">
                and tk.channelid=#{params.channelId}
            </if>
            <if test="params.errCode !=null and params.errCode !=''">
                and tk.errcode in
                <foreach collection="params.errCode.split(',')" item="err" open="(" separator="," close=")">
                    '${err}'
                </foreach>
            </if>
            <if test="params.sign !=null and params.sign !=''">
                and tk.sign like CONCAT('%', #{params.sign}, '%')
            </if>
            <if test="params.province !=null and params.province !=''">
                and tk.province =#{params.province}
            </if>
            <if test="params.code !=null  and params.code ==1">
                and  tk.errcode = 'DELIVRD'
            </if>
            <if test="params.code !=null and params.code ==2">
                and  tk.errcode != 'DELIVRD'
            </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>

        </where>
        group by tk.errcode
        <if test="params.bySp != null and params.bySp ==true">
            ,sp.id
        </if>
        <if test="params.byChannel != null and params.byChannel ==true">
            ,tk.channelid
            ,ssci.CHANNEL_NAME
        </if>
        <if test="params.bySign != null and params.bySign ==true">
            , tk.sign
        </if>
        <if test="params.byProvince != null and params.byProvince ==true">
            , tk.province
        </if>
        <if test="params.byDay != null and params.byDay==true">
            , tk.stat_time
        </if>

        ) a left join
        (SELECT errcode , GROUP_CONCAT(reason SEPARATOR';') reason FROM sms_err_code GROUP BY errcode) sec
        on a.errcode = sec.errcode
        order by a.allnumber desc

    </select>

    <select id="listSpTaskErrCodeCount" resultType="cn.roadm.admin.dto.SmsSpErrCodeSummaryDO">
        select
        sum(IFNULL(tk.allnumber,0)) allnumber
        from sms_errcode_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.code !=null  and params.code ==1">
            and  tk.errcode = 'DELIVRD'
        </if>
        <if test="params.code !=null and params.code ==2">
            and  tk.errcode != 'DELIVRD'
        </if>
        <if test="params.channelId !=null and params.channelId >=0">
            and tk.channelid=#{params.channelId}
        </if>
        <if test="params.errCode !=null and params.errCode !=''">
            and tk.errcode in
            <foreach collection="params.errCode.split(',')" item="err" open="(" separator="," close=")">
                '${err}'
            </foreach>
        </if>
        <if test="params.sign !=null and params.sign !=''">
            and tk.sign like CONCAT('%',#{params.sign},'%')
        </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.province !=null and params.province !=''">
            and tk.province =#{params.province}
        </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="listPageResendErrCodeTotal" resultType="cn.roadm.admin.dto.SmsSpErrCodeSummaryDO">
        select a.*,sec.reason from
        (select sum(tk.allnumber) allnumber,
        <if test="params.bySp != null and params.bySp ==true">
            sp.id spId, sp.sp_name spName,
        </if>
        <if test="params.byChannel != null and params.byChannel ==true">
            tk.channelid channelId,
            ssci.CHANNEL_NAME channelName,
        </if>
        <if test="params.bySign != null and params.bySign ==true">
            tk.sign,
        </if>
        <if test="params.byDay != null and params.byDay==true">
            date_format(tk.stat_time,'%Y-%m-%d') createtime,
        </if>
        tk.errcode errCode
        from sms_sp_info sp, sms_resend_errcode_summary tk,sms_send_channel_info ssci
        <where>
            tk.spid = sp.id
            and tk.channelid=ssci.id
            <if test="params.spName != null and params.spName != ''">
                and sp.sp_name like CONCAT('%',#{params.spName},'%')
            </if>
            <if test="params.salerList != null and params.salerList.size() > 0">
                and sp.sp_saler in
                <foreach collection="params.salerList" item="item" open="(" separator="," close=")">
                    '${item}'
                </foreach>
            </if>
            <if test="params.spSaler!=null and params.spSaler!=''">
                and sp.sp_saler =#{params.spSaler}
            </if>
            <if test="params.spId!=null and params.spId>0">
                and tk.spid=#{params.spId}
            </if>
            <if test="params.channelId !=null and params.channelId >=0">
                and tk.channelid=#{params.channelId}
            </if>
            <if test="params.errCode !=null and params.errCode !=''">
                and tk.errcode in
                <foreach collection="params.errCode.split(',')" item="err" open="(" separator="," close=")">
                    '${err}'
                </foreach>
            </if>
            <if test="params.sign !=null and params.sign !=''">
                and tk.sign like CONCAT('%', #{params.sign}, '%')
            </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>

        </where>
        group by tk.errcode
        <if test="params.bySp != null and params.bySp ==true">
            ,sp.id
        </if>
        <if test="params.byChannel != null and params.byChannel ==true">
            ,tk.channelid
            ,ssci.CHANNEL_NAME
        </if>
        <if test="params.bySign != null and params.bySign ==true">
            , tk.sign
        </if>
        <if test="params.byDay != null and params.byDay==true">
            , tk.stat_time
        </if>

        ) a left join
        (SELECT errcode , GROUP_CONCAT(reason SEPARATOR';') reason FROM sms_err_code GROUP BY errcode) sec
        on a.errcode = sec.errcode
        order by a.allnumber desc

    </select>

    <select id="listResendErrCodeCount" resultType="cn.roadm.admin.dto.SmsSpErrCodeSummaryDO">
        select
        sum(IFNULL(tk.allnumber,0)) allnumber
        from sms_resend_errcode_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.channelId !=null and params.channelId >=0">
            and tk.channelid=#{params.channelId}
        </if>
        <if test="params.errCode !=null and params.errCode !=''">
            and tk.errcode in
            <foreach collection="params.errCode.split(',')" item="err" open="(" separator="," close=")">
                '${err}'
            </foreach>
        </if>
        <if test="params.sign !=null and params.sign !=''">
            and tk.sign like CONCAT('%',#{params.sign},'%')
        </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="listPageSpDelayMinRatio" resultType="cn.roadm.admin.dto.SmsSpDelaySummaryDO">
        select
        <if test="params.groupBy != null and params.groupBy =='bySp'">
            sp_id spId,
            sp_name spName,
        </if>
        <if test="params.groupBy != null and params.groupBy =='byChannel'">
            channel_name channelName,
            channelid channelId,
        </if>
        <if test="params.groupBy != null and params.groupBy =='bySpChannel'">
            sp_id spId,
            sp_name spName,
            channel_name channelName,
            channelid channelId,
        </if>
        all_number allnumber,
        submit_succ submitSucc,
        submit_fail submitFail,
        arrive_succ reportSucc,
        arrive_fail reportFail,
        arrive_succ+arrive_fail reportCount,
        case when unRecMsgnum &lt;0 then 0 else unRecMsgnum end as unRecMsgnum,
        case when all_number=0 then 0 when unRecMsgnum &lt;0 then 0 else Round(unRecMsgnum/all_number* 100, 2 ) end as unRecRate,
        sucess_rate succRate,
        five_less fiveLess,
        ten_less tenLess,
        ten_to_twenty tenToTwenty,
        twenty_to_thirty twentyToThirty,
        thirty_to_sisty thirtyToSisty,
        sisty_more sistyMore,
        createtime
        from (
        select
        <if test="params.groupBy != null and params.groupBy =='bySp'">
            sp.id sp_id,
            sp.sp_name,
        </if>
        <if test="params.groupBy != null and params.groupBy =='byChannel'">
            si.channel_name,
            sr.channelid,
        </if>
        <if test="params.groupBy != null and params.groupBy =='bySpChannel'">
            sp.id sp_id,
            sp.sp_name,
            si.channel_name,
            sr.channelid,
        </if>
        sum(IFNULL(sr.all_number,0)) submit_succ,
        sum(IFNULL(sr.all_number, 0))+sum(IFNULL(sr.submit_fail, 0))  all_number,
        sum(IFNULL(sr.submit_fail,0)) submit_fail,
        sum(IFNULL(sr.arrive_succ,0)) arrive_succ,
        sum(IFNULL(sr.arrive_fail,0)) arrive_fail,

        sum(IFNULL(sr.all_number, 0)+IFNULL(sr.submit_fail, 0)-IFNULL(sr.arrive_succ,0)-IFNULL(sr.arrive_fail,0)) unRecMsgnum,
        Round( sum( IFNULL( sr.arrive_succ, 0 ) )/ sum( IFNULL( sr.all_number, 0 ) + IFNULL( sr.submit_fail, 0 ))* 100, 2 ) sucess_rate,
        CONCAT(CONCAT(sum(sr.five_less), '(' ),(CONCAT(Round(sum(sr.five_less)/sum(IFNULL( sr.arrive_succ, 0 ) + IFNULL( sr.arrive_fail, 0 ))*100,2), '%)' ))) five_less,
        CONCAT(CONCAT(sum(sr.ten_less), '(' ),(CONCAT(Round(sum(sr.ten_less)/sum(IFNULL( sr.arrive_succ, 0 ) + IFNULL( sr.arrive_fail, 0 ))*100,2), '%)' ))) ten_less,
        CONCAT(CONCAT(sum(sr.ten_to_twenty), '(' ),(CONCAT(Round(sum(sr.ten_to_twenty)/sum(IFNULL( sr.arrive_succ, 0 ) + IFNULL( sr.arrive_fail, 0 ))*100,2), '%)' ))) ten_to_twenty,
        CONCAT(CONCAT(sum(sr.twenty_to_thirty), '(' ),(CONCAT(Round(sum(sr.twenty_to_thirty)/sum(IFNULL( sr.arrive_succ, 0 ) + IFNULL( sr.arrive_fail, 0 ))*100,2), '%)' ))) twenty_to_thirty,
        CONCAT(CONCAT(sum(sr.thirty_to_sisty), '(' ),(CONCAT(Round(sum(sr.thirty_to_sisty)/sum(IFNULL( sr.arrive_succ, 0 ) + IFNULL( sr.arrive_fail, 0 ))*100,2), '%)' ))) thirty_to_sisty,
        CONCAT(CONCAT(sum(sr.sisty_more), '(' ),(CONCAT(Round(sum(sr.sisty_more)/sum(IFNULL( sr.arrive_succ, 0 ) + IFNULL( sr.arrive_fail, 0 ))*100,2), '%)' ))) sisty_more,
        date_format(sr.createtime,'%Y-%m-%d %H:%i') createtime
        from sms_sp_delay_ratio sr, sms_sp_info sp,sms_send_channel_info si
        where sr.spid = sp.id and si.id=sr.channelid
        <if test="params.spName!=null and params.spName!=''">
            and sp.sp_name like CONCAT('%',#{params.spName},'%')
        </if>
        <if test="params.salerList != null and params.salerList.size() > 0">
            and sp.sp_saler in
            <foreach collection="salerList" item="item" open="(" separator="," close=")">
                '${item}'
            </foreach>
        </if>
        <if test="params.spSaler!=null and params.spSaler!=''">
            and sp.sp_saler =#{params.spSaler}
        </if>
        <if test="params.channelId!=null and params.channelId>=0">
            and si.id = #{params.channelId}
        </if>
        <if test="params.spId!=null and params.spId>0">
            and sr.spid=#{params.spId}
        </if>
        <if test="params.startTime!=null and params.startTime!=''">
            and sr.createtime&gt;=str_to_date(#{params.startTime},'%Y-%m-%d %H:%i')
        </if>
        <if test="params.startTime==null or params.startTime==''">
            and sr.createtime&gt;=DATE_SUB(now(),INTERVAL 1 HOUR)
        </if>
        <if test="params.endTime!=null and params.endTime!=''">
            and
            sr.createtime&lt;str_to_date(#{params.endTime},'%Y-%m-%d %H:%i')
        </if>
        group by
        <if test="params.groupBy != null and params.groupBy =='bySp'">
            sp.id,
            sp.sp_name,
        </if>
        <if test="params.groupBy != null and params.groupBy =='byChannel'">
            si.channel_name,
            sr.channelid,
        </if>
        <if test="params.groupBy != null and params.groupBy =='bySpChannel'">
            sp.id,
            sp.sp_name,
            si.channel_name,
            sr.channelid,
        </if>
        date_format( sr.createtime, '%Y-%m-%d %H:%i' )
        ) m
        <where>
            1=1
            <if test="params.largeRate!= null and params.largeRate >= 0">
                and m.sucess_rate &gt;= #{params.largeRate}
            </if>
            <if test="params.smallRate!= null and params.smallRate >= 0">
                and m.sucess_rate &lt;= #{params.smallRate}
            </if>
        </where>
    </select>

    <select id="getSpDelayMinRatioSum" resultType="cn.roadm.admin.dto.SmsSpDelaySummaryDO">
        select
        sum(all_number) allnumber,
        sum(submit_succ) submitSucc,
        sum(submit_fail) submitFail,
        sum(arrive_succ) reportSucc,
        sum(arrive_fail) reportFail,
        sum(unRecMsgnum) unRecMsgnum,
        CONCAT(CONCAT(sum(five_less), '(' ),(CONCAT(Round(sum(five_less)/sum(IFNULL( arrive_succ, 0 ) + IFNULL( arrive_fail, 0 ))*100,2), '%)' ))) fiveLess,
        CONCAT(CONCAT(sum(ten_less), '(' ),(CONCAT(Round(sum(ten_less)/sum(IFNULL( arrive_succ, 0 ) + IFNULL( arrive_fail, 0 ))*100,2), '%)' ))) tenLess,
        CONCAT(CONCAT(sum(ten_to_twenty), '(' ),(CONCAT(Round(sum(ten_to_twenty)/sum(IFNULL( arrive_succ, 0 ) + IFNULL( arrive_fail, 0 ))*100,2), '%)' ))) tenToTwenty,
        CONCAT(CONCAT(sum(twenty_to_thirty), '(' ),(CONCAT(Round(sum(twenty_to_thirty)/sum(IFNULL( arrive_succ, 0 ) + IFNULL( arrive_fail, 0 ))*100,2), '%)' ))) twentyToThirty,
        CONCAT(CONCAT(sum(thirty_to_sisty), '(' ),(CONCAT(Round(sum(thirty_to_sisty)/sum(IFNULL(arrive_succ, 0 ) + IFNULL( arrive_fail, 0 ))*100,2), '%)' ))) thirtyToSisty,
        CONCAT(CONCAT(sum(sisty_more), '(' ),(CONCAT(Round(sum(sisty_more)/sum(IFNULL( arrive_succ, 0 ) + IFNULL( arrive_fail, 0 ))*100,2), '%)' ))) sistyMore
        from (
        select sp.id sp_id,
        sp.sp_name,
        si.channel_name,
        sr.channelid,
        sum(IFNULL(sr.all_number,0)) submit_succ,
        sum(IFNULL(sr.all_number, 0))+sum(IFNULL(sr.submit_fail, 0))  all_number,
        sum(IFNULL(sr.submit_fail,0)) submit_fail,
        sum(IFNULL(sr.arrive_succ,0)) arrive_succ,
        sum(IFNULL(sr.arrive_fail,0)) arrive_fail,
        sum(IFNULL(sr.all_number, 0)+IFNULL(sr.submit_fail, 0)-IFNULL(sr.arrive_succ,0)-IFNULL(sr.arrive_fail,0)) unRecMsgnum,
        Round( sum( IFNULL( sr.arrive_succ, 0 ) )/ sum( IFNULL( sr.all_number, 0 ) + IFNULL( sr.submit_fail, 0 ))* 100, 2 ) sucess_rate,
        sum(sr.five_less) five_less,
        sum(sr.ten_less) ten_less,
        sum(sr.ten_to_twenty) ten_to_twenty,
        sum(sr.twenty_to_thirty) twenty_to_thirty,
        sum(sr.thirty_to_sisty) thirty_to_sisty,
        sum(sr.sisty_more) sisty_more
        from sms_sp_delay_ratio sr, sms_sp_info sp,sms_send_channel_info si
        where sr.spid = sp.id and si.id=sr.channelid
        <if test="params.spName!=null and params.spName!=''">
            and sp.sp_name like CONCAT('%',#{params.spName},'%')
        </if>
        <if test="params.salerList != null and params.salerList.size() > 0">
            and sp.sp_saler in
            <foreach collection="salerList" item="item" open="(" separator="," close=")">
                '${item}'
            </foreach>
        </if>
        <if test="params.spSaler!=null and params.spSaler!=''">
            and sp.sp_saler =#{params.spSaler}
        </if>
        <if test="params.channelId!=null and params.channelId>=0">
            and si.id = #{params.channelId}
        </if>
        <if test="params.spId!=null and params.spId>0">
            and sr.spid=#{params.spId}
        </if>
        <if test="params.startTime!=null and params.startTime!=''">
            and sr.createtime&gt;=str_to_date(#{params.startTime},'%Y-%m-%d %H:%i')
        </if>
        <if test="params.startTime==null or params.startTime==''">
            and sr.createtime&gt;=DATE_SUB(now(),INTERVAL 1 HOUR)
        </if>
        <if test="params.endTime!=null and params.endTime!=''">
            and
            sr.createtime&lt;str_to_date(#{params.endTime},'%Y-%m-%d %H:%i')
        </if>
        group by sp.id, sp.sp_name,si.channel_name,sr.channelid,date_format( sr.createtime, '%Y-%m-%d %H:%i' )
        ) m
        <where>
            1=1
            <if test="params.largeRate!= null and params.largeRate >= 0">
                and m.sucess_rate &gt;= #{params.largeRate}
            </if>
            <if test="params.smallRate!= null and params.smallRate >= 0">
                and m.sucess_rate &lt;= #{params.smallRate}
            </if>
        </where>
    </select>

    <select id="listSpDelayMinRatio" resultType="cn.roadm.admin.dto.SmsSpDelaySummaryDO">
        select
        ${params.sumField} `time`,
        sum(IFNULL( sr.all_number, 0 )) + sum(IFNULL( sr.submit_fail, 0 )) allnumber,
        sum(IFNULL(sr.all_number,0)) submitSucc,
        sum(IFNULL(sr.submit_fail,0)) submitFail,
        sum(IFNULL(sr.arrive_succ,0)) reportSucc,
        sum(IFNULL(sr.arrive_fail,0)) reportFail,
        Round( sum( IFNULL( sr.arrive_succ, 0 ) )/ sum( IFNULL( sr.all_number, 0 ) + IFNULL( sr.submit_fail, 0 ))* 100, 2 ) sucessRate,
        Round( sum( IFNULL( sr.arrive_succ, 0 ) + IFNULL( sr.arrive_fail, 0 )) / sum( IFNULL( sr.all_number, 0 ) + IFNULL( sr.submit_fail, 0 ))* 100,2) reportRate
        from sms_sp_delay_ratio sr, sms_sp_info sp,sms_send_channel_info si
        where sr.spid = sp.id and si.id=sr.channelid
        <if test="params.spName!=null and params.spName!=''">
            and sp.sp_name like CONCAT('%',#{params.spName},'%')
        </if>
        <if test="params.channelId!=null and params.channelId>=0">
            and si.id = #{params.channelId}
        </if>
        <if test="params.spId!=null and params.spId>0">
            and sr.spid=#{params.spId}
        </if>
        <if test="params.startTime!=null and params.startTime!=''">
            and sr.createtime&gt;=str_to_date(#{params.startTime},'%Y-%m-%d %H:%i')
        </if>
        <if test="params.startTime==null or params.startTime==''">
            and sr.createtime&gt;=DATE_SUB(now(),INTERVAL 1 HOUR)
        </if>
        <if test="params.endTime!=null and params.endTime!=''">
            and sr.createtime&lt;str_to_date(#{params.endTime},'%Y-%m-%d %H:%i')
        </if>

        group by `time`
        order by `time` desc

    </select>

    <select id="listPageSmsFinance" resultType="cn.roadm.admin.dto.SmsFinanceSummaryDO">

        select *,(case when a.income=0 then 0 else Round(a.profit/a.income*100,2) end) profitRate from (
        select
        prs.spid spId,
        sp.sp_name spName,
        sp.noProfit,
        sp.sp_type spType,
        sp.is_api_billing payType,
        (case when sp.return_fee_type=2 then sum(ifnull(prs.arrive_succ,0)) else sum(ifnull(prs.submit_succ,0)) end )
        num,
        (case when sp.noProfit=1 then 0 else
        (case when sp.return_fee_type=2 then Round(sum(ifnull(prs.arrive_succ,0)*ifnull(sp_price,0))/10000,2) else
        Round(sum(ifnull(prs.submit_succ,0)*ifnull(sp_price,0))/10000,2) end ) end) income,

        (case when sp.noProfit=1 then 0 else
        (case when sp.return_fee_type=2 then Round(sum(ifnull(prs.arrive_succ,0)*ifnull(channel_price,0))/10000,2) else
        Round(sum(ifnull(prs.submit_succ,0)*ifnull(channel_price,0))/10000,2) end ) end) cost,

        (case when sp.noProfit=1 then 0 else
        (case when sp.return_fee_type=2 then Round(sum(ifnull(prs.arrive_succ,0)*ifnull(sp_price,0))/10000,2) else
        Round(sum(ifnull(prs.submit_succ,0)*ifnull(sp_price,0))/10000,2) end ) - (case when sp.return_fee_type=2 then
        Round(sum(ifnull(prs.arrive_succ,0)*ifnull(channel_price,0))/10000,2) else
        Round(sum(ifnull(prs.submit_succ,0)*ifnull(channel_price,0))/10000,2) end ) end) profit,
        (case when count(distinct sp_price)=1 then 0 else 1 end) adjustPrice
        from(
        select
        sps.spid,
        sps.arrive_succ,
        sps.submit_succ,
        sps.sp_price,
        sps.channel_price
        FROM
        sms_sp_province_summary sps
        <where>
            <if test="params.startTime!=null and params.startTime!=''">
                and sps.stat_time &gt;= #{params.startTime}
            </if>
            <if test="params.startTime==null or params.startTime==''">
                and sps.stat_time &gt;=curdate()
            </if>
            <if test="params.endTime!=null and params.endTime!=''">
                and sps.stat_time &lt; DATE_ADD(str_to_date(#{params.endTime},'%Y-%m-%d'),INTERVAL 1 DAY)
            </if>
            <if test="params.spId!=null and params.spId!=''">
                and sps.spid=#{params.spId}
            </if>
        </where>
        ) as prs inner join sms_sp_info sp ON prs.spid = sp.id

        <where>
            <if test="params.spType!=null and params.spType>0">
                AND sp.sp_type =#{params.spType}
            </if>
            <if test="params.payType != null">
                and sp.is_api_billing = #{params.payType}
            </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!=''">
                and sp.id=#{params.spId}
            </if>
            <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>
        </where>
        GROUP BY
        sp.id
        ) a
    </select>

    <select id="getSmsFinanceTotal" resultType="cn.roadm.admin.dto.SmsFinanceSummaryDO">
        select
        sum(num) num,
        sum(income) income,
        sum(cost) cost,
        sum(profit) profit,
        (case when sum(income)=0 then 0 else Round(sum(profit)/sum(income)*100,2) end) profitRate
        from (
        select
        prs.spid spId,
        sp.sp_name spName,
        sp.noProfit,
        sp.sp_type spType,
        (case when sp.return_fee_type=2 then sum(ifnull(prs.arrive_succ,0)) else sum(ifnull(prs.submit_succ,0)) end )
        num,
        (case when sp.noProfit=1 then 0 else
        (case when sp.return_fee_type=2 then Round(sum(ifnull(prs.arrive_succ,0)*ifnull(sp_price,0))/10000,2) else
        Round(sum(ifnull(prs.submit_succ,0)*ifnull(sp_price,0))/10000,2) end ) end) income,

        (case when sp.noProfit=1 then 0 else
        (case when sp.return_fee_type=2 then Round(sum(ifnull(prs.arrive_succ,0)*ifnull(channel_price,0))/10000,2) else
        Round(sum(ifnull(prs.submit_succ,0)*ifnull(channel_price,0))/10000,2) end ) end) cost,
        (case when sp.noProfit=1 then 0 else
        (case when sp.return_fee_type=2 then Round(sum(ifnull(prs.arrive_succ,0)*ifnull(sp_price,0))/10000,2) else
        Round(sum(ifnull(prs.submit_succ,0)*ifnull(sp_price,0))/10000,2) end ) - (case when sp.return_fee_type=2 then
        Round(sum(ifnull(prs.arrive_succ,0)*ifnull(channel_price,0))/10000,2) else
        Round(sum(ifnull(prs.submit_succ,0)*ifnull(channel_price,0))/10000,2) end ) end) profit
        from(
        select
        sps.spid,
        sps.arrive_succ,
        sps.submit_succ,
        sps.sp_price,
        sps.channel_price
        FROM
        sms_sp_province_summary sps
        <where>
            <if test="params.startTime!=null and params.startTime!=''">
                and sps.stat_time &gt;= #{params.startTime}
            </if>
            <if test="params.startTime==null or params.startTime==''">
                and sps.stat_time &gt;=curdate()
            </if>
            <if test="params.endTime!=null and params.endTime!=''">
                and sps.stat_time &lt; DATE_ADD(str_to_date(#{params.endTime},'%Y-%m-%d'),INTERVAL 1 DAY)
            </if>
            <if test="params.spId!=null and params.spId!=''">
                and sps.spid=#{params.spId}
            </if>
        </where>
        ) as prs inner join sms_sp_info sp ON prs.spid = sp.id

        <where>
            <if test="params.spType!=null and params.spType>0">
                AND sp.sp_type =#{params.spType}
            </if>
            <if test="params.payType != null">
                and sp.is_api_billing = #{params.payType}
            </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!=''">
                and sp.id=#{params.spId}
            </if>
            <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>
        </where>
        GROUP BY
        sp.id
        ) a
    </select>

    <select id="getBlackNumCost" resultType="BigDecimal">
        SELECT ifnull(sum(black_count),0)/10000 from (select ifnull(sum(bps.black_num*bpt.price),0) black_count from black_phone_summary bps,sms_black_phone_type bpt,sms_sp_info sp
        <where>
            bps.black_type_id=bpt.id and bps.spid=sp.id and bpt.type=3
            <if test="params.spId!=null and params.spId!=''">
                and bps.spid=#{params.spId}
            </if>
            <if test="params.startTime!=null and params.startTime!=''">
                and bps.stat_time &gt;= #{params.startTime}
            </if>
            <if test="params.startTime==null or params.startTime==''">
                and bps.stat_time &gt;=curdate()
            </if>
            <if test="params.endTime!=null and params.endTime!=''">
                and bps.stat_time &lt;date_add(#{params.endTime},INTERVAL 1 DAY)
            </if>
            <if test="params.spName!=null and params.spName!=''">
                and sp.sp_name like concat('%',#{params.spName},'%')
            </if>
            <if test="params.spSaler!=null and params.spSaler!=''">
                and sp.sp_saler = #{params.spSaler}
            </if>
            <if test="params.payType != null">
                and sp.is_api_billing = #{params.payType}
            </if>
            <if test="params.spType!=null and params.spType>0">
                AND sp.sp_type =#{params.spType}
            </if>
            group by bps.spid) a
        </where>
    </select>


    <select id="getSmsFinanceDetail" resultType="cn.roadm.admin.dto.SmsFinanceDetailSummaryDO">
        select
        sum(num) num,
        sum(fail) fail,
        sum(unknown) unknown,
        sum(income) income,
        sum(un_income) unIncome,
        sum(cost) cost,
        sum(un_cost) unCost,
        sum(profit) profit,
        sum(commissionIncome) commissionIncome,

        <if test="params.queryType=='spchannel'">
            ss.clientid clientId,
            ss.full_name,
            ss.spId,
            ss.spName,
            ss.noProfit,
            ss.payType,
            ss.spType,
            Round(ss.sp_price/10000,4) sp_price,
            Round(ss.channel_price/10000,4) channel_price,
            ss.spSaler,
            Round(ss.commission_price/10000,4) commission_price,
            ss.channelid channelId,
            chan.channel_name channelName,
            c.loginname,
            chan.supplier_name,
            chan.business_manager as businessManager,
        </if>
        <if test="params.queryType=='channel'">
            ss.channelid channelId,
            Round(ss.channel_price/10000,4) channel_price,
            chan.channel_name channelName,
        </if>
        <if test="params.queryType=='sp'">
            ss.spId,
            ss.clientid clientId,
            ss.full_name,
            ss.spName,
            ss.payType,
            ss.spType,
            Round(ss.sp_price/10000,4) sp_price,
            ss.noProfit,
            Round(ss.commission_price/10000,4) commission_price,
            ss.spSaler,
        </if>
        <choose>
        <when test="params.byMonth != null and params.byMonth == true">
            DATE_FORMAT(ss.stat_time,'%Y-%m') as createtime,
        </when>
        <otherwise>
            DATE_FORMAT(ss.stat_time,'%Y-%m-%d') as createtime,
        </otherwise>
        </choose>
        ss.province_otype
        from (
        select sas.clientid,ent.full_name,a.* from (
        select
        prs.spid spId,
        sp.sp_name spName,
        sp.noProfit,
        sp.eid,
        sp.commission_price,
        sp.sp_type spType,
        sp.sp_saler spSaler,
        sp.is_api_billing payType,
        prs.channelid,
        prs.stat_time,
        prs.sp_price,
        prs.channel_price,
        prs.province_otype,
        (case when sp.return_fee_type=2 then sum(ifnull(prs.arrive_succ,0)) else sum(ifnull(prs.submit_succ,0)) end ) num,

        sum(ifnull(prs.fail,0)) fail,
        sum(ifnull(prs.unknown,0)) unknown,

        (case when sp.noProfit=1 then 0 else
        (case when sp.return_fee_type=2 then Round(sum(ifnull(prs.arrive_succ,0)*ifnull(commission_price,0))/10000,4) else
        Round(sum(ifnull(prs.submit_succ,0)*ifnull(commission_price,0))/10000,4) end ) end) commissionIncome,

        (case when sp.noProfit=1 then 0 else
        (case when sp.return_fee_type=2 then Round(sum(ifnull(prs.arrive_succ,0)*ifnull(sp_price,0))/10000,4) else
        Round(sum(ifnull(prs.submit_succ,0)*ifnull(sp_price,0))/10000,4) end ) end) income,

        (case when sp.noProfit=1 then 0 else
        Round(sum(ifnull(prs.unknown,0)*ifnull(sp_price,0))/10000,4) end )  un_income,

        (case when sp.noProfit=1 then 0 else
        (case when sp.return_fee_type=2 then Round(sum(ifnull(prs.arrive_succ,0)*ifnull(channel_price,0))/10000,4) else
        Round(sum(ifnull(prs.submit_succ,0)*ifnull(channel_price,0))/10000,4) end ) end) cost,

        (case when sp.noProfit=1 then 0 else
        Round(sum(ifnull(prs.unknown,0)*ifnull(channel_price,0))/10000,4) end )  un_cost,

        (case when sp.noProfit=1 then 0 else
        (case when sp.return_fee_type=2 then Round(sum(ifnull(prs.arrive_succ,0)*ifnull(sp_price,0))/10000,4) else
        Round(sum(ifnull(prs.submit_succ,0)*ifnull(sp_price,0))/10000,4) end ) - (case when sp.return_fee_type=2 then
        Round(sum(ifnull(prs.arrive_succ,0)*ifnull(channel_price,0))/10000,4) else
        Round(sum(ifnull(prs.submit_succ,0)*ifnull(channel_price,0))/10000,4) end ) end) profit

        from(
        select
        sps.spid,
        sps.channelid,
        sps.stat_time,
        (case when sps.nettype=0 then (case  when sps.otype=1 then '联通' when sps.otype=2 then '移动' when sps.otype=3 then '电信' else '全国' end)  else sps.province end) province_otype,
        sum(ifnull(sps.arrive_succ,0)) arrive_succ,
        sum(ifnull(sps.submit_succ,0)) submit_succ,
        sum(ifnull( sps.arrive_fail, 0 )) fail,
        sum(
        ifnull( sps.submit_succ, 0 )) + sum(
        ifnull( sps.submit_fail, 0 )) - sum(
        ifnull( sps.arrive_fail, 0 )) - sum(
        ifnull( sps.arrive_succ, 0 )) unknown,
        sps.sp_price,
        sps.channel_price
        FROM
        sms_sp_province_summary sps
        <where>
            <if test="params.startTime!=null and params.startTime!=''">
                and sps.stat_time &gt;= #{params.startTime}
            </if>
            <if test="params.startTime==null or params.startTime==''">
                and sps.stat_time &gt;=curdate()
            </if>
            <if test="params.endTime!=null and params.endTime!=''">
                and sps.stat_time &lt; DATE_ADD(str_to_date(#{params.endTime},'%Y-%m-%d'),INTERVAL 1 DAY)
            </if>
            <if test="params.spId!=null and params.spId!=''">
                and sps.spid=#{params.spId}
            </if>
            <if test="params.channelId!=null and params.channelId!=''">
                and sps.channelid=#{params.channelId}
            </if>
            <if test="params.otype!=null and params.otype>0">
                and sps.otype=#{params.otype}
            </if>
            <if test="params.nettype!=null and params.nettype!=''">
                and sps.nettype=#{params.nettype}
            </if>

        </where>
        GROUP BY
        sps.spid,
        sps.channelid,
        sps.stat_time,
        sps.sp_price,
        sps.channel_price,
        (case when sps.nettype=0 then (case  when sps.otype=1 then '联通' when sps.otype=2 then '移动' when sps.otype=3 then '电信' else '全国' end)  else sps.province end)
        ) as prs inner join sms_sp_info sp ON prs.spid = sp.id
        <where>
            <if test="params.spType!=null and params.spType>0">
                AND sp.sp_type =#{params.spType}
            </if>
            <if test="params.payType != null">
                and sp.is_api_billing = #{params.payType}
            </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!=''">
                and sp.id=#{params.spId}
            </if>
            <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>
        </where>
        GROUP BY
        sp.id,
        prs.channelid,
        prs.stat_time,
        prs.sp_price,
        prs.channel_price,
        prs.province_otype
        ) a left join sms_api_socket_sp sas on sas.spid = a.spid
        left join sms_enterprise ent on ent.id = a.eid
        ) ss
        <if test="params.queryType=='spchannel' ">
            left join (SELECT channelid,loginname FROM
            (SELECT id AS channelid,loginname FROM sms_carrier_channel_info
            UNION ALL
            SELECT channelid,
            CASE
            WHEN channeltemplatevar->'$.appId' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.appId')
            WHEN channeltemplatevar->'$.appid' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.appid')
            WHEN channeltemplatevar->'$.LoginName' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.LoginName')
            WHEN channeltemplatevar->'$.uname' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.uname')
            WHEN channeltemplatevar->'$.accessKeyId' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.accessKeyId')
            WHEN channeltemplatevar->'$.accessKey' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.accessKey')
            WHEN channeltemplatevar->'$.username' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.username')
            WHEN channeltemplatevar->'$.AccountId' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.AccountId')
            WHEN channeltemplatevar->'$.Account' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.Account')
            WHEN channeltemplatevar->'$.account' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.account')
            WHEN channeltemplatevar->'$.AppKey' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.AppKey')
            WHEN channeltemplatevar->'$.api_key' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.api_key')
            WHEN channeltemplatevar->'$.apiKey' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.apiKey')
            WHEN channeltemplatevar->'$.X_APP_ID' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.X_APP_ID')
            END  AS loginname FROM http_channel_template_config) b
            ) c ON c.channelid = ss.channelid left join (select a.id,a.channel_name,a.business_manager,b.supplier_name from sms_send_channel_info a left join channel_supplier b on a.supplier_id = b.id) chan on chan.id = ss.channelid
        group by
            ss.clientid,
            ss.full_name,
            ss.spId,
            ss.spName,
            ss.payType,
            ss.sp_price,
            ss.channel_price,
            ss.spSaler,
            ss.channelid,
            chan.channel_name,
            c.loginname,
            chan.supplier_name,
            chan.business_manager,
            <choose>
                <when test="params.byMonth != null and params.byMonth == true">
                    DATE_FORMAT(ss.stat_time,'%Y-%m'),
                </when>
                <otherwise>
                    DATE_FORMAT(ss.stat_time,'%Y-%m-%d'),
                </otherwise>
            </choose>
            ss.province_otype
        </if>
        <if test="params.queryType=='sp' ">
            group by
            ss.spId,
            ss.clientid,
            ss.spName,
            ss.payType,
            ss.sp_price,
            <choose>
                <when test="params.byMonth != null and params.byMonth == true">
                    DATE_FORMAT(ss.stat_time,'%Y-%m'),
                </when>
                <otherwise>
                    DATE_FORMAT(ss.stat_time,'%Y-%m-%d'),
                </otherwise>
            </choose>
            ss.province_otype
        </if>
        <if test="params.queryType=='channel' ">
            left join sms_send_channel_info chan on chan.id = ss.channelid
            group by
            ss.channelid ,
            ss.channel_price,
            chan.channel_name,
            <choose>
                <when test="params.byMonth != null and params.byMonth == true">
                    DATE_FORMAT(ss.stat_time,'%Y-%m'),
                </when>
                <otherwise>
                    DATE_FORMAT(ss.stat_time,'%Y-%m-%d'),
                </otherwise>
            </choose>
            ss.province_otype
        </if>
    </select>

    <select id="getSmsFinanceDetailTotal" resultType="cn.roadm.admin.dto.SmsFinanceDetailSummaryDO">
        select
        sum(num) num,
        sum(fail) fail,
        sum(unknown) unknown,
        sum(income) income,
        sum(commissionIncome) commissionIncome,
        sum(un_income) unIncome,
        sum(cost) cost,
        sum(un_cost) unCost,
        sum(yx_un_income) yxUnIncome,
        sum(yx_un_cost) yxUnCost,
        sum(profit) profit

        from (
        select a.* from (
        select
        prs.spid spId,
        prs.channelid,
        prs.stat_time,
        prs.sp_price,
        prs.channel_price,
        prs.province_otype,
        (case when sp.return_fee_type=2 then sum(ifnull(prs.arrive_succ,0)) else sum(ifnull(prs.submit_succ,0)) end )
        num,

        sum(ifnull(prs.fail,0)) fail,
        sum(ifnull(prs.unknown,0)) unknown,

        (case when sp.noProfit=1 then 0 else
        (case when sp.return_fee_type=2 then Round(sum(ifnull(prs.arrive_succ,0)*ifnull(sp_price,0))/10000,4) else
        Round(sum(ifnull(prs.submit_succ,0)*ifnull(sp_price,0))/10000,4) end ) end) income,

        (case when sp.noProfit=1 then 0 else
        (case when sp.return_fee_type=2 then Round(sum(ifnull(prs.arrive_succ,0)*ifnull(commission_price,0))/10000,4) else
        Round(sum(ifnull(prs.submit_succ,0)*ifnull(commission_price,0))/10000,4) end ) end) commissionIncome,

        (case when sp.noProfit=1 then 0 else
        (case when sp.return_fee_type=2 then Round(sum(ifnull(prs.arrive_succ,0)*ifnull(channel_price,0))/10000,4) else
        Round(sum(ifnull(prs.submit_succ,0)*ifnull(channel_price,0))/10000,4) end ) end) cost,

        (case when sp.noProfit=1 then 0 else
        (case when sp.sp_type=1 then
        Round(sum(ifnull(prs.unknown,0)*ifnull(sp_price,0))/10000,4) else 0 end) end ) un_income,

        (case when sp.noProfit=1 then 0 else
        (case when sp.sp_type=1 then
        Round(sum(ifnull(prs.unknown,0)*ifnull(channel_price,0))/10000,4) else 0 end) end ) un_cost,

        (case when sp.noProfit=1 then 0 else
        (case when sp.sp_type=2 then
        Round(sum(ifnull(prs.unknown,0)*ifnull(sp_price,0))/10000,4) else 0 end) end ) yx_un_income,

        (case when sp.noProfit=1 then 0 else
        (case when sp.sp_type=2 then
        Round(sum(ifnull(prs.unknown,0)*ifnull(channel_price,0))/10000,4) else 0 end) end ) yx_un_cost,

        (case when sp.noProfit=1 then 0 else
        (case when sp.return_fee_type=2 then Round(sum(ifnull(prs.arrive_succ,0)*ifnull(sp_price,0))/10000,4) else
        Round(sum(ifnull(prs.submit_succ,0)*ifnull(sp_price,0))/10000,4) end ) - (case when sp.return_fee_type=2 then
        Round(sum(ifnull(prs.arrive_succ,0)*ifnull(channel_price,0))/10000,4) else
        Round(sum(ifnull(prs.submit_succ,0)*ifnull(channel_price,0))/10000,4) end ) end) profit

        from(
        select
        sps.spid,
        sps.channelid,
        sps.stat_time,
        (case when sps.nettype=0 then (case when sps.otype=1 then '联通' when sps.otype=2 then '移动' when sps.otype=3
        then '电信' else '全国' end) else sps.province end) province_otype,
        sum(ifnull(sps.arrive_succ,0)) arrive_succ,
        sum(ifnull(sps.submit_succ,0)) submit_succ,
        sum(ifnull( sps.arrive_fail, 0 )) fail,
        sum(
        ifnull( sps.submit_succ, 0 )) + sum(
        ifnull( sps.submit_fail, 0 )) - sum(
        ifnull( sps.arrive_fail, 0 )) - sum(
        ifnull( sps.arrive_succ, 0 )) unknown,
        sps.sp_price,
        sps.channel_price
        FROM
        sms_sp_province_summary sps
        <where>
            <if test="params.startTime!=null and params.startTime!=''">
                and sps.stat_time &gt;= #{params.startTime}
            </if>
            <if test="params.startTime==null or params.startTime==''">
                and sps.stat_time &gt;=curdate()
            </if>
            <if test="params.endTime!=null and params.endTime!=''">
                and sps.stat_time &lt; DATE_ADD(str_to_date(#{params.endTime},'%Y-%m-%d'),INTERVAL 1 DAY)
            </if>
            <if test="params.spId!=null and params.spId!=''">
                and sps.spid=#{params.spId}
            </if>
            <if test="params.channelId!=null and params.channelId!=''">
                and sps.channelid=#{params.channelId}
            </if>
            <if test="params.otype!=null and params.otype>0">
                and sps.otype=#{params.otype}
            </if>
            <if test="params.nettype!=null and params.nettype!=''">
                and sps.nettype=#{params.nettype}
            </if>

        </where>
        GROUP BY
        sps.spid,
        sps.channelid,
        sps.stat_time,
        sps.sp_price,
        sps.channel_price,
        (case when sps.nettype=0 then (case when sps.otype=1 then '联通' when sps.otype=2 then '移动' when sps.otype=3
        then '电信' else '全国' end) else sps.province end)
        ) as prs inner join sms_sp_info sp ON prs.spid = sp.id
        <where>
            <if test="params.spType!=null and params.spType>0">
                AND sp.sp_type =#{params.spType}
            </if>
            <if test="params.payType != null">
                and sp.is_api_billing = #{params.payType}
            </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!=''">
                and sp.id=#{params.spId}
            </if>
            <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>
        </where>
        GROUP BY
        sp.id,
        prs.channelid,
        prs.stat_time,
        prs.sp_price,
        prs.channel_price,
        prs.province_otype
        ) a
        ) ss

    </select>

</mapper>
