<?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.mapper.SmsTextLengthSummaryMapper">

    <select id="listPageQuerySummaryList" resultType="cn.roadm.bo.SmsTextLengthSummaryModal">
        select a.sp_id as spId, b.sp_name as spName, 
        a.sign, a.otype
        <if test="byChannel != null and byChannel == 1">
            , a.channel_id as channelId, c.channel_name as channelName
        </if>
        , sum(IFNULL(a.send_count,0)) as sendCount
        <if test="byDay != null and byDay == 1">
            , DATE_FORMAT(a.summary_date, '%Y-%m-%d') as summaryDate
        </if>
        <if test="byLength != null and byLength == 1">
            , a.length
        </if>
        from sms_text_length_summary a 
        left join sms_sp_info b on a.sp_id = b.id
        left join sms_send_channel_info c on a.channel_id = c.id
        <where>
            <if test="spId != null and spId != ''">
                and a.sp_id = #{spId}
            </if>
            <if test="sign != null and sign != ''">
                and a.sign like concat('%', #{sign}, '%')
            </if>
            <if test="otype != null">
                and a.otype = #{otype}
            </if>
            <if test="channelId != null and channelId != '' and channelId != '-1'">
                and a.channel_id = #{channelId}
            </if>
            <if test="channelName != null and channelName != ''">
                and c.channel_name like concat('%', #{channelName}, '%')
            </if>
            <if test="length != null">
                and a.length = #{length}
            </if>
            <if test="startTime != null and startTime != ''">
                and a.summary_date &gt;= STR_TO_DATE(#{startTime}, '%Y-%m-%d')
            </if>
            <if test="endTime != null and endTime != ''">
                and a.summary_date &lt; DATE_ADD(STR_TO_DATE(#{endTime}, '%Y-%m-%d'), INTERVAL 1 DAY)
            </if>
        </where>
        group by a.sp_id, b.sp_name, a.sign, a.otype
        <if test="byChannel != null and byChannel == 1">
            , a.channel_id, c.channel_name
        </if>
        <if test="byDay != null and byDay == 1">
            , DATE_FORMAT(a.summary_date, '%Y-%m-%d')
        </if>
        <if test="byLength != null and byLength == 1">
            , a.length
        </if>
        order by sendCount desc
    </select>

    <select id="exportSmsTextLengthSummaryList" parameterType="cn.roadm.bo.SmsTextLengthSummaryModal" resultType="Map">
        select 
            a.sp_id as SP_ID,
            b.sp_name as SP_NAME,
            a.sign as SIGN,
            (case when a.otype = '1' then "联通"
                  when a.otype = '2' then "移动"
                  else "电信" end) as OTYPE_NAME
        <if test="byChannel != null and byChannel == 1">
            , a.channel_id as CHANNEL_ID,
            c.channel_name as CHANNEL_NAME
        </if>
            , sum(IFNULL(a.send_count,0)) as SEND_COUNT
        <if test="byDay != null and byDay == 1">
            , DATE_FORMAT(a.summary_date, '%Y-%m-%d') as SUMMARY_DATE
        </if>
        <if test="byLength != null and byLength == 1">
            , (case when a.length = 0 then "单条短信"
                    when a.length = 1 then "长短信"
                    else "未知" end) as LENGTH_TYPE
        </if>
        from sms_text_length_summary a 
        left join sms_sp_info b on a.sp_id = b.id
        left join sms_send_channel_info c on a.channel_id = c.id
        <where>
            <if test="spId != null and spId != ''">
                and a.sp_id = #{spId}
            </if>
            <if test="sign != null and sign != ''">
                and a.sign like concat('%', #{sign}, '%')
            </if>
            <if test="otype != null">
                and a.otype = #{otype}
            </if>
            <if test="channelId != null and channelId != '' and channelId != '-1'">
                and a.channel_id = #{channelId}
            </if>
            <if test="channelName != null and channelName != ''">
                and c.channel_name like concat('%', #{channelName}, '%')
            </if>
            <if test="length != null">
                and a.length = #{length}
            </if>
            <if test="startTime != null and startTime != ''">
                and a.summary_date &gt;= STR_TO_DATE(#{startTime}, '%Y-%m-%d')
            </if>
            <if test="endTime != null and endTime != ''">
                and a.summary_date &lt; DATE_ADD(STR_TO_DATE(#{endTime}, '%Y-%m-%d'), INTERVAL 1 DAY)
            </if>
        </where>
        group by 
            a.sp_id, 
            b.sp_name, 
            a.sign, 
            a.otype
        <if test="byChannel != null and byChannel == 1">
            , a.channel_id, 
            c.channel_name
        </if>
        <if test="byDay != null and byDay == 1">
            , DATE_FORMAT(a.summary_date, '%Y-%m-%d')
        </if>
        <if test="byLength != null and byLength == 1">
            , a.length
        </if>
        order by SEND_COUNT desc
    </select>

    <select id="getSmsTextLengthSummaryTotal" parameterType="cn.roadm.bo.SmsTextLengthSummaryModal" resultType="Map">
        select 
            sum(IFNULL(a.send_count,0)) as TOTAL_SEND_COUNT
        from sms_text_length_summary a 
        left join sms_sp_info b on a.sp_id = b.id
        left join sms_send_channel_info c on a.channel_id = c.id
        <where>
            <if test="spId != null and spId != ''">
                and a.sp_id = #{spId}
            </if>
            <if test="sign != null and sign != ''">
                and a.sign like concat('%', #{sign}, '%')
            </if>
            <if test="otype != null">
                and a.otype = #{otype}
            </if>
            <if test="channelId != null and channelId != '' and channelId != '-1'">
                and a.channel_id = #{channelId}
            </if>
            <if test="channelName != null and channelName != ''">
                and c.channel_name like concat('%', #{channelName}, '%')
            </if>
            <if test="length != null">
                and a.length = #{length}
            </if>
            <if test="startTime != null and startTime != ''">
                and a.summary_date &gt;= STR_TO_DATE(#{startTime}, '%Y-%m-%d')
            </if>
            <if test="endTime != null and endTime != ''">
                and a.summary_date &lt; DATE_ADD(STR_TO_DATE(#{endTime}, '%Y-%m-%d'), INTERVAL 1 DAY)
            </if>
        </where>
    </select>

</mapper>
