<?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.device.pms.mapper.majornetwork.OverviewMapper">

    <select id="selectYoY" resultType="com.njcn.device.pms.pojo.vo.OverviewVO$MonitorObject">

        SELECT
            ta.Monitor_Sort as monitorSort,
            SUM(ta.num) as sumNum,
            IFNULL(SUM(tb.num),0) as ytbSumNum,
            IFNULL(SUM(tc.num),0) as yhbSumNum,
            ifnull(round((SUM(ta.num)-SUM(tb.num))/SUM(tb.num)*100,2),0) as sameNum,
            ifnull(round((SUM(ta.num)-SUM(tc.num))/SUM(tc.num)*100,2),0) as ringNum
        FROM (
             SELECT
                 t.Monitor_Sort,
                 year(t.Created_Date) as yy,
                 month(t.Created_Date) as mm,
                 COUNT(*) as num

             FROM
                (
                    SELECT
                        b.Monitor_Sort,
                        b.Created_Date
                    FROM
                        pms_distribution_monitor b
                    <where>
                        org_id IN
                        <foreach collection="orgIds" item="item" open="(" close=")" separator=",">
                            #{item}
                        </foreach>
                        AND STATUS = 1
                    </where>
                )t
                GROUP BY t.Monitor_Sort,yy,mm
             ) ta
-- 同比：上年同月
                 LEFT JOIN (
                    SELECT
                        t.Monitor_Sort,
                        year(t.Created_Date) as yy,
                        month(t.Created_Date) as mm,
                        COUNT(*) as num
                        FROM
                        (
                            SELECT
                                b.Monitor_Sort,
                                b.Created_Date
                            FROM
                                pms_distribution_monitor b
                            <where>
                                org_id IN
                                <foreach collection="orgIds" item="item" open="(" close=")" separator=",">
                                    #{item}
                                </foreach>
                                AND STATUS = 1
                            </where>
                        )t
                        GROUP BY t.Monitor_Sort,yy,mm
                    ) tb
                           ON  tb.mm = ta.mm  and tb.yy = ta.yy-1	 and tb.Monitor_Sort = ta.Monitor_Sort

-- 环比：上月
                 LEFT JOIN (
            SELECT
                t.Monitor_Sort,
                year(t.Created_Date) as yy,
                month(t.Created_Date) as mm,
                COUNT(*) as num
            FROM  (
                    SELECT
                    b.Monitor_Sort,
                    b.Created_Date
                    FROM
                    pms_distribution_monitor b
                    <where>
                        org_id IN
                        <foreach collection="orgIds" item="item" open="(" close=")" separator=",">
                            #{item}
                        </foreach>
                        AND STATUS = 1
                    </where>
                )t
                GROUP BY t.Monitor_Sort,yy,mm
            ) tc
                   ON  ( (tc.yy = ta.yy and tc.mm = ta.mm - 1 and tc.Monitor_Sort = ta.Monitor_Sort)
                       OR
                         (tc.yy=ta.yy - 1 AND tc.mm = 12 AND ta.mm = 1 and tc.Monitor_Sort = ta.Monitor_Sort)
                       )

        <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>

        GROUP BY  ta.Monitor_Sort;

    </select>
    <select id="selectListAll" resultType="com.njcn.device.pms.pojo.vo.DistributionMonitorVO">
        SELECT
            b.org_id as orgNo,
            pss.Longitude as longitude,
            pss.Latitude as latitude,
            b.Status ,
            b.*
        FROM
            pms_distribution_monitor b
        LEFT JOIN pms_statation_stat  pss on b.Power_Station_Id=pss.Power_Id
        <where>
            b.org_id IN
            <foreach collection="orgIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
            AND  b.STATUS = 1
        </where>
    </select>
    <select id="selectPhotovoltaicUser" resultType="com.njcn.device.pms.pojo.vo.OverviewVO$PhotoVoltaicVoltage">
        (select
            1 AS type,
            count(*) AS num,
            IFNULL( sum( installed_capacity ), 0 ) AS Capacity
        from
            f_cons_power
        <where>
            section IN
            <foreach collection="orgIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>

            and  voltage_level IN
            <foreach collection="levelOne" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </where>
        )
        UNION ALL
        (
        select
        2 AS type,
        count(*) AS num,
        IFNULL( sum( installed_capacity ), 0 ) AS Capacity
        from
        f_cons_power
        <where>
            section IN
            <foreach
                    collection="orgIds" item="item" open="(" close=")" separator=",">
                    #{item}
            </foreach>

            and   voltage_level IN
            <foreach collection="levelTwo" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>

        </where>
        )

    </select>
</mapper>