<?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.njcn.harmonic.mapper.StatHarmonicOrgMMapper">

    <select id="listMonthRatio" resultType="StatHarmonicOrgVO">
        SELECT
        r.`org_no` `id`,
        r.over_limit_measurement_ratio_average `ratio`,
        r.over_limit_measurement_average `count`
        FROM
        r_stat_harmonic_org_m r
        where 1=1
        and org_no in
        <foreach collection="list" index="index" item="item" separator="," open="(" close=")">
            #{item.id}
        </foreach>
        <if test="startTime!=null and endTime!=null">
            and DATE_FORMAT(data_date,'%Y-%m-%d') between #{startTime} and #{endTime}
        </if>
    </select>
    <select id="selectInfoList" resultType="com.njcn.harmonic.pojo.vo.RStatHarmonicVO">
         SELECT
            harmonic_type,
            sum( over_limit_measurement_average )/ count(*) AS overLimitMeasurementAverage,
            sum( over_limit_measurement_accrued ) AS overLimitMeasurementAccrued,
            sum( over_Day )/ sum( over_limit_measurement_accrued )/ count(*) AS averageOverDay,
            sum( over_limit_measurement_ratio_average )/ count(*) AS overLimitMeasurementRatioAverage,
            sum( over_limit_measurement_ratio_accrued ) AS overLimitMeasurementRatioAccrued
        FROM
            r_stat_harmonic_org_m
        <where>
            and data_Type=#{dataType}
            <if test="param != null and  param.ids != null and param.ids.size > 0">
                AND org_no IN
                <foreach collection='param.ids' item='item' index="index" open='(' separator=',' close=')'>
                    #{item}
                </foreach>
            </if>
            <if test="ids != null and ids.size > 0">
                AND harmonic_type IN
                <foreach collection='ids' item='item' index="index" open='(' separator=',' close=')'>
                    #{item}
                </foreach>
            </if>
            <if test="param!=null and param.startTime != null and param.startTime !=''">
                AND  data_date >= #{param.startTime}
            </if>
            <if test="param!=null and param.endTime != null and param.endTime != ''">
                AND  data_date &lt;= #{param.endTime}
            </if>
        </where>
        GROUP BY
        harmonic_type
    </select>
    <select id="selectYoY" resultType="com.njcn.harmonic.pojo.vo.OverviewVO">
        SELECT
		concat(ta.yy,'-',ta.mm) dateTime,
		ta.measurement_type_class as monitorSort,
		ta.harmonic_type as harmonicType,
		ta.num as sumNum,
		IFNULL(tb.num,0) as ytbSumNum,
		IFNULL(tc.num,0) as yhbSumNum,
		ifnull(round((ta.num-tb.num)/tb.num*100,2),0) as sameNum,
		ifnull(round((ta.num-tc.num)/tc.num*100,2),0) as ringNum

FROM (
			SELECT
				t.measurement_type_class,
				t.harmonic_type,
				year(t.data_date) as yy,
				month(t.data_date) as mm,
				TRUNCATE(SUM(over_limit_measurement_ratio_accrued)/count(over_limit_measurement_ratio_accrued),2) as num


		FROM  r_stat_harmonic_m t
        <where>
            and data_Type=#{dataType}
            <if test="ids != null and ids.size > 0">
                AND org_no IN
                <foreach collection='ids' item='item' index="index" open='(' separator=',' close=')'>
                    #{item}
                </foreach>
            </if>
            <if test="types != null and types.size > 0">
                AND measurement_type_class IN
                <foreach collection='types' item='item' index="index" open='(' separator=',' close=')'>
                    #{item}
                </foreach>
            </if>
        </where>
		GROUP BY t.measurement_type_class,t.harmonic_type,yy,mm
) ta
-- 同比：上年同月
LEFT JOIN (
	SELECT
				t.measurement_type_class,
				t.harmonic_type,
				year(t.data_date) as yy,
				month(t.data_date) as mm,
				TRUNCATE(SUM(over_limit_measurement_ratio_accrued)/count(over_limit_measurement_ratio_accrued),2) as num


		FROM  r_stat_harmonic_m t
        <where>
            and data_Type=#{dataType}
            <if test="ids != null and ids.size > 0">
                AND org_no IN
                <foreach collection='ids' item='item' index="index" open='(' separator=',' close=')'>
                    #{item}
                </foreach>
            </if>
            <if test="types != null and types.size > 0">
                AND measurement_type_class IN
                <foreach collection='types' item='item' index="index" open='(' separator=',' close=')'>
                    #{item}
                </foreach>
            </if>
        </where>
		GROUP BY t.measurement_type_class,t.harmonic_type,yy,mm
) tb
ON  tb.mm = ta.mm  and tb.yy = ta.yy-1	 and tb.measurement_type_class = ta.measurement_type_class and tb.harmonic_type = ta.harmonic_type

-- 环比：上月
LEFT JOIN (
			SELECT
				t.measurement_type_class,
				t.harmonic_type,
				year(t.data_date) as yy,
				month(t.data_date) as mm,
				TRUNCATE(SUM(over_limit_measurement_ratio_accrued)/count(over_limit_measurement_ratio_accrued),2) as num


		FROM  r_stat_harmonic_m t
        <where>
            and data_Type=#{dataType}
            <if test="ids != null and ids.size > 0">
                AND org_no IN
                <foreach collection='ids' item='item' index="index" open='(' separator=',' close=')'>
                    #{item}
                </foreach>
            </if>
            <if test="types != null and types.size > 0">
                AND measurement_type_class IN
                <foreach collection='types' item='item' index="index" open='(' separator=',' close=')'>
                    #{item}
                </foreach>
            </if>
        </where>
		GROUP BY t.measurement_type_class,t.harmonic_type,yy,mm
) tc
ON  ( (tc.yy = ta.yy and tc.mm = ta.mm - 1 and tc.measurement_type_class = ta.measurement_type_class and tc.harmonic_type = ta.harmonic_type)
				OR
			(tc.yy=ta.yy - 1 AND tc.mm = 12 AND ta.mm = 1 and tc.measurement_type_class = ta.measurement_type_class and tc.harmonic_type = ta.harmonic_type)
			)

        <where>
            <if test="param.year != null and  param.year!=''">
                AND ta.yy = #{param.year}
            </if>
            <if test="param.month != null and  param.month.size > 0">
                AND ta.mm IN
                <foreach collection='param.month' item='item' index="index" open='(' separator=',' close=')'>
                    #{item}
                </foreach>
            </if>
        </where>

    </select>

</mapper>
