<?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.TaskSummaryMapper">
    <select id="listPageQueryTaskSummary" resultType="cn.roadm.bo.TaskSummaryDO">
        select ts.taskId,
        <if test='byOtype != null and byOtype == "1"'>
            ts.otype,
        </if>
        <if test='byChannelId != null and byChannelId == "1"'>
            ts.channelId,
            si.CHANNEL_NAME as channelName,
        </if>
        <if test='byProvince != null and byProvince == "1"'>
            ts.province,
        </if>
        ts.statTime,
        sum(ts.succ)       as succ,
        sum(ts.fail)       as fail,
        sum(ts.arriveSucc) as arriveSucc,
        sum(ts.arriveFail) as arriveFail
        from sms_task_summary ts
                 left join sms_send_channel_info si on ts.channelId = si.ID
        <where>
            <if test="taskId != null and taskId != ''">
                and ts.taskId = #{taskId}
            </if>
            <if test="otype != null and otype != ''">
                and ts.otype = #{otype}
            </if>
            <if test="channelId != null and channelId != ''">
                and ts.channelId = #{channelId}
            </if>
            <if test="province != null and province != ''">
                and ts.province = #{province}
            </if>
            <if test="startTime != null and startTime != ''">
                and statTime &gt;= str_to_date(#{startTime}, '%Y-%m-%d')
            </if>
            <if test="endTime != null and endTime != ''">
                and statTime &lt;= str_to_date(#{endTime}, '%Y-%m-%d')
            </if>
        </where>
        group by ts.taskId,
        <if test='byOtype != null and byOtype == "1"'>
            ts.otype,
        </if>
        <if test='byChannelId != null and byChannelId == "1"'>
            ts.channelId, si.CHANNEL_NAME,
        </if>
        <if test='byProvince != null and byProvince == "1"'>
            ts.province,
        </if>
        ts.statTime
        order by statTime desc
    </select>

    <select id="listPageQueryErrorCodeSummary" resultType="cn.roadm.bo.ErrorCodeSummaryVO">
        select a.*,sec.reason from
        (select STAT_DESC as errorCode, count(1) as value
        <if test='byOtype != null and byOtype == 1'>
            ,otype
        </if>
        <if test='byChannel != null and byChannel == 1'>
            ,channelId
        </if>
        <if test='byProvince != null and byProvince == 1'>
            ,province
        </if>
        from sms_arrive
        <where>
            TASKID = #{taskId}
            and RESULT = 1
            <if test="otype != null and otype != ''">
                and otype = #{otype}
            </if>
            <if test="channelId != null and channelId != ''">
                and channelId = #{channelId}
            </if>
        </where>
        group by STAT_DESC
        <if test='byOtype != null and byOtype == 1'>
            , otype
        </if>
        <if test='byChannel != null and byChannel == 1'>
            , channelId
        </if>
        <if test='byProvince != null and byProvince == 1'>
            , province
        </if>
        ) a left join
        (SELECT errcode , GROUP_CONCAT(reason SEPARATOR';') reason FROM sms_err_code GROUP BY errcode) sec
        on a.errorCode = sec.errcode

        order by value desc
    </select>

    <select id="listPageQueryKeywordSummary" resultType="cn.roadm.bo.SmsKeywordInterceptSummaryDO">
        select any_value(sp_id) as spId, any_value(sign) as sign, keyword, sum(count) as count,
        <choose>
            <when test="byTime == 1">
                DATE_FORMAT(stat_time, '%Y-%m-%d %H:%i:%s') as statTime
            </when>
            <otherwise>
                DATE_FORMAT(MAX(stat_time), '%Y-%m-%d %H:%i:%s') as statTime
            </otherwise>
        </choose>
        from sms_keyword_intercept_summary
        <where>
            <if test="spId != null and spId != ''">
                and sp_id = #{spId}
            </if>
            <if test="sign != null and sign != ''">
                and sign = #{sign}
            </if>
            <if test="keyword != null and keyword != ''">
                and keyword like CONCAT('%', #{keyword}, '%')
            </if>
            <if test="startTime != null and startTime != ''">
                and stat_time &gt;= str_to_date(#{startTime}, '%Y-%m-%d')
            </if>
            <if test="endTime != null and endTime != ''">
                and stat_time &lt;= DATE_ADD(str_to_date(#{endTime}, '%Y-%m-%d'), INTERVAL 1 DAY)
            </if>
        </where>
        group by keyword
        <if test="bySpId == 1">
            , sp_id
        </if>
        <if test="bySign == 1">
            , sign
        </if>
        <if test="byTime == 1">
            , statTime
        </if>
        <if test="col != null and col != '' and col=='time'">
            order by  statTime ${orderType}
        </if>
        <if test="col != null and col != '' and col=='count'">
            order by  count ${orderType}
        </if>
    </select>
</mapper>