<?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.SmsSummaryMapper">
    <select id="listPageQuerySmsTaskSummary" resultType="cn.roadm.admin.dto.SmsTaskSummaryDO">
        select ts.taskId,
        <if test='queryVO.groupBy != null and queryVO.groupBy.contains("byOtype")'>
            ts.otype,
        </if>
        <if test='queryVO.groupBy != null and queryVO.groupBy.contains("byChannel")'>
            ts.channelId,
            si.CHANNEL_NAME as channelName,
        </if>
        <if test='queryVO.groupBy != null and queryVO.groupBy.contains("byProvince")'>
            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="queryVO.taskId != null and queryVO.taskId != ''">
                and ts.taskId = #{queryVO.taskId}
            </if>
            <if test="queryVO.otype != null and queryVO.otype != ''">
                and ts.otype = #{queryVO.otype}
            </if>
            <if test="queryVO.channelId != null and queryVO.channelId != ''">
                and ts.channelId = #{queryVO.channelId}
            </if>
            <if test="queryVO.province != null and queryVO.province != ''">
                and ts.province = #{queryVO.province}
            </if>
            <if test="queryVO.startTime != null and queryVO.startTime != ''">
                and statTime &gt;= str_to_date(#{queryVO.startTime}, '%Y-%m-%d')
            </if>
            <if test="queryVO.endTime != null and queryVO.endTime != ''">
                and statTime &lt;= str_to_date(#{queryVO.endTime}, '%Y-%m-%d')
            </if>
        </where>
        group by ts.taskId,
        <if test='queryVO.groupBy != null and queryVO.groupBy.contains("byOtype")'>
            ts.otype,
        </if>
        <if test='queryVO.groupBy != null and queryVO.groupBy.contains("byChannel")'>
            ts.channelId, si.CHANNEL_NAME,
        </if>
        <if test='queryVO.groupBy != null and queryVO.groupBy.contains("byProvince")'>
            ts.province,
        </if>
        ts.statTime
        order by statTime desc
    </select>

    <select id="listPageQueryTaskErrorCodeSummary" resultType="cn.roadm.admin.dto.SmsTaskErrorCodeSummaryDO">
        select a.*, sec.reason as reason from
        (select STAT_DESC as errorCode
              , count(1)  as value
        <if test='queryVO.groupBy != null and queryVO.groupBy.contains("byOtype")'>
            , otype
        </if>
        <if test='queryVO.groupBy != null and queryVO.groupBy.contains("byChannel")'>
            , channelId
        </if>
        <if test='queryVO.groupBy != null and queryVO.groupBy.contains("byProvince")'>
            , province
        </if>
        from sms_arrive
        <where>
            TASKID = #{queryVO.taskId}
              and RESULT = 1
            <if test="queryVO.otype != null and queryVO.otype != ''">
                and otype = #{queryVO.otype}
            </if>
            <if test="queryVO.channelId != null and queryVO.channelId != ''">
                and channelId = #{queryVO.channelId}
            </if>
        </where>
        group by STAT_DESC
        <if test='queryVO.groupBy != null and queryVO.groupBy.contains("byOtype")'>
            , otype
        </if>
        <if test='queryVO.groupBy != null and queryVO.groupBy.contains("byChannel")'>
            , channelId
        </if>
        <if test='queryVO.groupBy != null and queryVO.groupBy.contains("byProvince")'>
            , 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
    </select>
</mapper>
