<?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="com.sunyard.ssp.modules.monitor.transaction.mapper.StatisticalMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.sunyard.ssp.modules.monitor.transaction.entity.StatisticalResult">
        <id column="successCount" property="successCount"/>
        <result column="failCount" property="failCount"/>
        <result column="statisticalTime" property="statisticalTime"/>
    </resultMap>


    <select id="countPieChart" resultMap="BaseResultMap" databaseId="mysql">
        select SUM (SUCCESS_COUNT) as successCount,SUM (FAIL_COUNT) as failCount
        from SC_BUSINESS_STATISTICS
        <where>
            <if test="statisticalParam.server != null and statisticalParam.server != ''">
                and SERVER = #{statisticalParam.server}
            </if>
            <if test="statisticalParam.channelId != null">
                and CHANNEL_ID = #{statisticalParam.channelId}
            </if>
            <if test="statisticalParam.solutionId != null">
                and SOLUTION__ID = #{statisticalParam.solutionId}
            </if>
            <if test="statisticalParam.startTime != null and statisticalParam.endTime != null">
                and `CURRENT_TIME` between str_to_date(#{statisticalParam.startTime},'%Y-%m-%d %H:%i:%s') and to_date(#{statisticalParam.endTime},'%Y-%m-%d %H:%i:%s')
            </if>
        </where>
    </select>
    <select id="countPieChart" resultMap="BaseResultMap" databaseId="oracle">
        select SUM (SUCCESS_COUNT) as successCount,SUM (FAIL_COUNT) as failCount
        from SC_BUSINESS_STATISTICS
        <where>
            <if test="statisticalParam.server != null and statisticalParam.server != ''">
                and SERVER = #{statisticalParam.server}
            </if>
            <if test="statisticalParam.channelId != null">
                and CHANNEL_ID = #{statisticalParam.channelId}
            </if>
            <if test="statisticalParam.solutionId != null">
                and SOLUTION__ID = #{statisticalParam.solutionId}
            </if>
            <if test="statisticalParam.startTime != null and statisticalParam.endTime != null">
                and "CURRENT_TIME" between to_date(#{statisticalParam.startTime},'YYYY-MM-DD HH24:MI:SS') and to_date(#{statisticalParam.endTime},'YYYY-MM-DD HH24:MI:SS')
            </if>
        </where>
    </select>

    <select id="countPieChart" resultMap="BaseResultMap" databaseId="dm">
        select SUM (SUCCESS_COUNT) as successCount,SUM (FAIL_COUNT) as failCount
        from SC_BUSINESS_STATISTICS
        <where>
            <if test="statisticalParam.server != null and statisticalParam.server != ''">
                and SERVER = #{statisticalParam.server}
            </if>
            <if test="statisticalParam.channelId != null">
                and CHANNEL_ID = #{statisticalParam.channelId}
            </if>
            <if test="statisticalParam.solutionId != null">
                and SOLUTION__ID = #{statisticalParam.solutionId}
            </if>
            <if test="statisticalParam.startTime != null and statisticalParam.endTime != null">
                and "CURRENT_TIME" between to_date(#{statisticalParam.startTime},'YYYY-MM-DD HH24:MI:SS') and to_date(#{statisticalParam.endTime},'YYYY-MM-DD HH24:MI:SS')
            </if>
        </where>
    </select>

    <select id="countLineChartByTimeInterval" resultMap="BaseResultMap" databaseId="mysql">
        select
        <if test="statisticalParam.timeInterval != null and statisticalParam.timeInterval == 'month'">
            CONCAT(DATE_FORMAT(`CURRENT_TIME`, '%Y-%m-'),(DAY(`CURRENT_TIME`)+WEEKDAY(`CURRENT_TIME`-INTERVAL DAY(`CURRENT_TIME`) DAY)) DIV 7 + 1) as statisticalTime ,
        </if>
        <if test="statisticalParam.timeInterval != null and statisticalParam.timeInterval == 'week'">
            DATE_FORMAT(`CURRENT_TIME`,'%Y-%m-%d') as statisticalTime,
        </if>
        <if test="statisticalParam.timeInterval != null and statisticalParam.timeInterval == 'day'">
            DATE_FORMAT(`CURRENT_TIME`,'%Y-%m-%d %H') as statisticalTime,
        </if>
        SUM(SUCCESS_COUNT) as successCount,
        SUM(FAIL_COUNT) as failCount
        from SC_BUSINESS_STATISTICS
        <where>
            <if test="statisticalParam.server != null and statisticalParam.server != ''">
                and SERVER = #{statisticalParam.server}
            </if>
            <if test="statisticalParam.channelId != null">
                and CHANNEL_ID = #{statisticalParam.channelId}
            </if>
            <if test="statisticalParam.solutionNumber != null">
                and SOLUTION_NUMBER = #{statisticalParam.solutionNumber}
            </if>
            <if test="statisticalParam.timeInterval != null and statisticalParam.timeInterval == 'month'">
                and DATE_FORMAT(`CURRENT_TIME`,'%Y-%m') =  date_format(#{statisticalParam.sqlStatisticalTime}, '%Y-%m')
            </if>
            <if test="statisticalParam.timeInterval != null and statisticalParam.timeInterval == 'week'">
                and DATE_FORMAT(`CURRENT_TIME`,'%x-%v') = date_format(#{statisticalParam.sqlStatisticalTime}, '%x-%v')
            </if>
            <if test="statisticalParam.timeInterval != null and statisticalParam.timeInterval == 'day'">
                and DATE_FORMAT(`CURRENT_TIME`,'%Y-%m-%d') = date_format(#{statisticalParam.sqlStatisticalTime}, '%Y-%m-%d')
            </if>
        </where>
        group by statisticalTime
        order by statisticalTime
    </select>
    <select id="countLineChartByTimeInterval" resultMap="BaseResultMap" databaseId="oracle">
        select to_char("CURRENT_TIME",'${statisticalParam.timeIntervalFormat}') as statisticalTime,
        SUM(SUCCESS_COUNT) as successCount,
        SUM(FAIL_COUNT) as failCount
        from SC_BUSINESS_STATISTICS
        <where>
            <if test="statisticalParam.server != null and statisticalParam.server != ''">
                and SERVER = #{statisticalParam.server}
            </if>
            <if test="statisticalParam.channelId != null">
                and CHANNEL_ID = #{statisticalParam.channelId}
            </if>
            <if test="statisticalParam.solutionNumber != null">
                and SOLUTION_NUMBER = #{statisticalParam.solutionNumber}
            </if>
            <if test="statisticalParam.sqlStatisticalTime != null and statisticalParam.sqlStatisticalTimeFormat !=null">
                and to_char(TRUNC("CURRENT_TIME"),'${statisticalParam.sqlStatisticalTimeFormat}') =  (SELECT TO_CHAR (#{statisticalParam.sqlStatisticalTime}, '${statisticalParam.sqlStatisticalTimeFormat}') FROM DUAL)
            </if>
        </where>
        group by to_char("CURRENT_TIME",'${statisticalParam.timeIntervalFormat}')
        order by to_char("CURRENT_TIME",'${statisticalParam.timeIntervalFormat}')
    </select>

    <select id="countLineChartByTimeInterval" resultMap="BaseResultMap" databaseId="dm">
        select to_char("CURRENT_TIME",'${statisticalParam.timeIntervalFormat}') as statisticalTime,
        SUM(SUCCESS_COUNT) as successCount,
        SUM(FAIL_COUNT) as failCount
        from SC_BUSINESS_STATISTICS
        <where>
            <if test="statisticalParam.server != null and statisticalParam.server != ''">
                and SERVER = #{statisticalParam.server}
            </if>
            <if test="statisticalParam.channelId != null">
                and CHANNEL_ID = #{statisticalParam.channelId}
            </if>
            <if test="statisticalParam.solutionNumber != null">
                and SOLUTION_NUMBER = #{statisticalParam.solutionNumber}
            </if>
            <if test="statisticalParam.sqlStatisticalTime != null and statisticalParam.sqlStatisticalTimeFormat !=null">
                and to_char(TRUNC("CURRENT_TIME"),'${statisticalParam.sqlStatisticalTimeFormat}') =  (SELECT TO_CHAR (#{statisticalParam.sqlStatisticalTime}, '${statisticalParam.sqlStatisticalTimeFormat}') FROM DUAL)
            </if>
        </where>
        group by to_char("CURRENT_TIME",'${statisticalParam.timeIntervalFormat}')
        order by to_char("CURRENT_TIME",'${statisticalParam.timeIntervalFormat}')
    </select>

    <select id="countLineChartByKeyGroup" resultMap="BaseResultMap" databaseId="mysql">
        select
        <if test="statisticalParam.timeInterval != null and statisticalParam.timeInterval == 'month'">
            CONCAT(DATE_FORMAT(`CURRENT_TIME`, '%Y-%m-'),(DAY(`CURRENT_TIME`)+WEEKDAY(`CURRENT_TIME`-INTERVAL DAY(`CURRENT_TIME`) DAY)) DIV 7 + 1) as statisticalTime ,
        </if>
        <if test="statisticalParam.timeInterval != null and statisticalParam.timeInterval == 'week'">
            DATE_FORMAT(`CURRENT_TIME`,'%Y-%m-%d') as statisticalTime,
        </if>
        <if test="statisticalParam.timeInterval != null and statisticalParam.timeInterval == 'day'">
            DATE_FORMAT(`CURRENT_TIME`,'%Y-%m-%d %H') as statisticalTime,
        </if>
        SUM(SUCCESS_COUNT) as successCount,
        SUM(FAIL_COUNT) as failCount
        from SC_BUSINESS_STATISTICS
        <where>
            <if test="statisticalParam.group != null and statisticalParam.group != ''">
                and "GROUP" = #{statisticalParam.group}
            </if>
            <if test="statisticalParam.timeInterval != null and statisticalParam.timeInterval == 'month'">
                and DATE_FORMAT(`CURRENT_TIME`,'%Y-%m') =  date_format(#{statisticalParam.sqlStatisticalTime}, '%Y-%m')
            </if>
            <if test="statisticalParam.timeInterval != null and statisticalParam.timeInterval == 'week'">
                and DATE_FORMAT(`CURRENT_TIME`,'%x-%v') = date_format(#{statisticalParam.sqlStatisticalTime}, '%x-%v')
            </if>
            <if test="statisticalParam.timeInterval != null and statisticalParam.timeInterval == 'day'">
                and DATE_FORMAT(`CURRENT_TIME`,'%Y-%m-%d') = date_format(#{statisticalParam.sqlStatisticalTime}, '%Y-%m-%d')
            </if>
        </where>
        group by statisticalTime
        order by statisticalTime
    </select>

    <select id="countLineChartByKeyGroup" resultMap="BaseResultMap" databaseId="oracle">
        select to_char("CURRENT_TIME",'${statisticalParam.timeIntervalFormat}') as statisticalTime ,
        SUM(SUCCESS_COUNT) as successCount,
        SUM(FAIL_COUNT) as failCount
        from SC_BUSINESS_STATISTICS
        <where>
            <if test="statisticalParam.group != null and statisticalParam.group != ''">
                and "GROUP" = #{statisticalParam.group}
            </if>
            <if test="statisticalParam.sqlStatisticalTime != null and statisticalParam.sqlStatisticalTimeFormat !=null">
                and to_char(TRUNC("CURRENT_TIME"),'${statisticalParam.sqlStatisticalTimeFormat}') =  (SELECT TO_CHAR (#{statisticalParam.sqlStatisticalTime}, '${statisticalParam.sqlStatisticalTimeFormat}') FROM DUAL)
            </if>
        </where>
        group by to_char("CURRENT_TIME",'${statisticalParam.timeIntervalFormat}')
        order by to_char("CURRENT_TIME",'${statisticalParam.timeIntervalFormat}')
    </select>

    <select id="countLineChartByKeyGroup" resultMap="BaseResultMap" databaseId="dm">
        select to_char("CURRENT_TIME",'${statisticalParam.timeIntervalFormat}') as statisticalTime ,
        SUM(SUCCESS_COUNT) as successCount,
        SUM(FAIL_COUNT) as failCount
        from SC_BUSINESS_STATISTICS
        <where>
            <if test="statisticalParam.group != null and statisticalParam.group != ''">
                and "GROUP" = #{statisticalParam.group}
            </if>
            <if test="statisticalParam.sqlStatisticalTime != null and statisticalParam.sqlStatisticalTimeFormat !=null">
                and to_char(TRUNC("CURRENT_TIME"),'${statisticalParam.sqlStatisticalTimeFormat}') =  (SELECT TO_CHAR (#{statisticalParam.sqlStatisticalTime}, '${statisticalParam.sqlStatisticalTimeFormat}') FROM DUAL)
            </if>
        </where>
        group by to_char("CURRENT_TIME",'${statisticalParam.timeIntervalFormat}')
        order by to_char("CURRENT_TIME",'${statisticalParam.timeIntervalFormat}')
    </select>
</mapper>
