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


    <select id="selectListPageChannelCount"  resultType="cn.roadm.admin.dto.PaDayCountDO">
        select
        g.id channelId,
        g.CHANNEL_NAME channelName,
        sum(ifnull(pt.submitresp_succ,0))+sum(ifnull(pt.submitresp_fail,0)) submitCount,
        sum(ifnull(pt.submitresp_succ,0)) submitrespSucc,
        sum(ifnull(pt.submitresp_fail,0)) submitrespFail,
        sum(pt.timeout_count) timeout_count,
        sum(ifnull(pt.report_succ,0))+sum(ifnull(pt.report_fail,0)) reportCount,
        sum(ifnull(pt.report_succ,0)) reportSucc,
        sum(ifnull(pt.report_fail,0)) reportFail,
        CASE
        WHEN sum(ifnull(pt.submitresp_succ,0))+sum(ifnull(pt.submitresp_fail,0))-sum(ifnull(pt.report_succ,0))-sum(ifnull(pt.report_fail,0)) &lt;= 0 then 0
        else sum(ifnull(pt.submitresp_succ,0))+sum(ifnull(pt.submitresp_fail,0))-sum(ifnull(pt.report_succ,0))-sum(ifnull(pt.report_fail,0))
        END AS unRecMsgnum,

        CASE
        WHEN sum(ifnull(pt.submitresp_succ,0))+sum(ifnull(pt.submitresp_fail,0))-sum(ifnull(pt.report_succ,0))-sum(ifnull(pt.report_fail,0)) &lt;= 0 THEN 0
        ELSE (sum(ifnull(pt.submitresp_succ,0))+sum(ifnull(pt.submitresp_fail,0))-sum(ifnull(pt.report_succ,0))-sum(ifnull(pt.report_fail,0)) )/ (sum(ifnull(pt.submitresp_succ,0))+sum(ifnull(pt.submitresp_fail,0)))
        END AS unRecRate,

        CASE
        WHEN sum(ifnull(pt.submitresp_succ,0)) = 0 THEN 0
        ELSE sum(ifnull(pt.report_succ,0)) / sum(ifnull(pt.submitresp_succ,0))
        END AS channelSuccRate,

        CASE
        WHEN sum(ifnull(pt.submitresp_succ,0))+sum(ifnull(pt.submitresp_fail,0)) = 0 THEN 0
        ELSE sum(ifnull(pt.report_succ,0)) /(sum(ifnull(pt.submitresp_succ,0))+sum(ifnull(pt.submitresp_fail,0)))
        END AS succRate,

        <choose>
            <when test="params.byMonth != null and params.byMonth == &quot;1&quot;">
                date_format(pt.total_time,'%Y-%m') createtime,
            </when>
            <otherwise>
                date_format(pt.total_time,'%Y-%m-%d') createtime,
            </otherwise>
        </choose>

        <if test="params.bySpId != null and params.bySpId == true">
            pt.sp_id spId, sp.sp_name spName,
        </if>
        g.business_manager
        from pa_day_count pt, sms_send_channel_info g, sms_sp_info sp
        where pt.channelid = g.id and pt.sp_id = sp.id

        <if test="params.channelName!=null and params.channelName!=''">
            and g.CHANNEL_NAME like CONCAT('%',#{params.channelName},'%')
        </if>

        <if test="params.channelId!=null and params.channelId>=0">
            and g.id =#{params.channelId}
        </if>
        <if test="params.businessManager != null and params.businessManager != '' and params.businessManager != 'undefined'">
            and g.business_manager = #{params.businessManager}
        </if>
        <if test="params.managerId != null and params.managerId != ''">
            and g.business_manager in (select business from business_manager where managerId = #{params.managerId})
        </if>

        <choose>
                <when test="params.byMonth != null and params.byMonth == &quot;1&quot;">
                    <if test="params.month!=null and params.month!=''">
                        and date_format(pt.total_time,'%Y-%m')=#{params.month}
                    </if>
                </when>
            <otherwise>
                <if test="params.startTime!=null and params.startTime!=''">
                    and pt.total_time &gt;=str_to_date(#{params.startTime},'%Y-%m-%d')
                </if>
                <if test="params.endTime!=null and params.endTime!=''">
                    and pt.total_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 pt.total_time &gt;=curdate()
                </if>
            </otherwise>
        </choose>

        <if test="params.spId != null and params.spId != ''">
            and pt.SP_ID = #{params.spId}
        </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 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>
        group by
        <choose>
        <when test="params.byMonth != null and params.byMonth == &quot;1&quot;">
            date_format(pt.total_time, '%Y-%m'),
        </when>
            <otherwise>
                date_format(pt.total_time, '%Y-%m-%d'),
            </otherwise>
        </choose>

            g.id,g.CHANNEL_NAME
        <if test="params.bySpId != null and params.bySpId ==true">
            , pt.sp_id, sp.sp_name
        </if>

    </select>

    <select id="getlistPaInfCount" resultType="cn.roadm.admin.dto.PaDayCountDO">
        select
        sum(pt.submitresp_succ) submitresp_succ,
        sum(pt.submitresp_fail) submitresp_fail,
        sum(pt.report_succ) report_succ,
        sum(pt.submit_count)-sum(pt.report_count) unRecMsgnum,
        sum(pt.report_fail) report_fail
        from pa_day_count pt, sms_send_channel_info g, sms_sp_info sp
        where pt.channelid = g.id and pt.SP_ID = sp.id
        <if test="params.channelId!=null and params.channelId>=0">
            and g.id =#{params.channelId}
        </if>
        <if test="params.channelName!=null and params.channelName!=''">
            and g.CHANNEL_NAME like CONCAT('%',#{params.channelName},'%')
        </if>
        <if test="params.managerId != null and params.managerId != ''">
            and g.business_manager in (select ifnull(business,'') from business_manager where managerId = #{params.managerId}) and g.business_manager != ''
        </if>
        <if test="params.businessManager != null and params.businessManager != '' and params.businessManager != 'undefined'">
            and g.business_manager = #{params.businessManager}
        </if>

        <choose>
            <when test="params.byMonth != null and params.byMonth == &quot;1&quot;">
                <if test="params.month!=null and params.month!=''">
                    and date_format(pt.total_time,'%Y-%m')=#{params.month}
                </if>
            </when>
            <otherwise>
                <if test="params.startTime!=null and params.startTime!=''">
                    and pt.total_time &gt;=str_to_date(#{params.startTime},'%Y-%m-%d')
                </if>
                <if test="params.endTime!=null and params.endTime!=''">
                    and pt.total_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 pt.total_time &gt;=curdate()
                </if>
            </otherwise>
        </choose>

        <if test="params.spId != null and params.spId != ''">
            and pt.SP_ID = #{params.spId}
        </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 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>

    </select>

    <select id="getPaConfigByBusiness" resultType="cn.roadm.admin.controller.vo.ChannelInfoQueryVO">
        select a.id channelId,  concat(a.id,' ',a.channel_name) channelName
        from sms_send_channel_info a left JOIN sms_carrier_channel_info p ON a.id = p.id
        <where>
             1=1
            <if test="managerId != null and managerId != ''">
                and a.business_manager in (select ifnull(business,0) from business_manager where managerId =  #{managerId}) and a.business_manager != ''
            </if>
            <if test="businessManager != null and businessManager != ''">
                and a.business_manager = #{businessManager}
            </if>
            <if test="queryChannelNetworkType != null and queryChannelNetworkType == 0">
                and a.protocol_type != '4'
            </if>
            <if test="queryChannelNetworkType != null and queryChannelNetworkType == 1">
                and a.protocol_type = '4'
            </if>
            <if test="id != null ">
                and a.id = #{id}
            </if>
        </where>
        order by a.id desc
    </select>


    <select id="listPageGetBlackNum" resultType="cn.roadm.admin.dto.BlackSummaryDO">
        select bps.spId ,bps.black_num submitCount,DATE_FORMAT(bps.stat_time, '%Y-%m-%d') createtime,bps.black_type_id channelId,bpt.blacklist_group_name channelName from black_phone_summary bps,sms_black_phone_type bpt
        <where>
            bps.black_type_id=bpt.id
            <if test="params.spId!=null and params.spId!=''">
                and spid=#{params.spId}
            </if>
            <if test="params.startTime!=null and params.startTime!=''">
                and stat_time &gt;= #{params.startTime}
            </if>
            <if test="params.endTime!=null and params.endTime!=''">
                and stat_time &lt; date_add(#{params.endTime},INTERVAL 1 DAY)
            </if>
            <if test="params.channelId!=null and params.channelId!=''">
                and black_type_id=#{params.channelId}
            </if>
        </where>
    </select>

    <select id="getBlackNumCount" resultType="int">
        select ifnull(sum(bps.black_num),0) from black_phone_summary bps,sms_black_phone_type bpt
        <where>
            bps.black_type_id=bpt.id
            <if test="params.spId!=null and params.spId!=''">
                and spid=#{params.spId}
            </if>
            <if test="params.startTime!=null and params.startTime!=''">
                and stat_time &gt;= #{params.startTime}
            </if>
            <if test="params.endTime!=null and params.endTime!=''">
                and stat_time &lt; date_add(#{params.endTime},INTERVAL 1 DAY)
            </if>
            <if test="params.channelId!=null and params.channelId!=''">
                and black_type_id=#{params.channelId}
            </if>
        </where>
    </select>

</mapper>
