<?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
                            (
                                ( SELECT id, Operation_Id, Power_Station_Id, Line_Id FROM pms_power_client
                                <where>

                                    org_id IN
                                    <foreach collection="orgIds" item="item" open="(" close=")" separator=",">
                                        #{item}
                                    </foreach>
                                      AND STATUS = 1
                                </where>
                                ) UNION ALL
                                ( SELECT id, Operation_Id, Power_Station_Id, Line_Id FROM pms_power_generation_user
                                    <where>

                                        org_id IN
                                        <foreach collection="orgIds" item="item" open="(" close=")" separator=",">
                                            #{item}
                                        </foreach>
                                          AND STATUS = 1
                                    </where>
                                ) UNION ALL
                                ( SELECT id, Operation_Id, Power_Station_Id, Line_Id FROM pms_power_distributionarea
                                    <where>

                                    org_id IN
                                    <foreach collection="orgIds" item="item" open="(" close=")" separator=",">
                                        #{item}
                                    </foreach>
                                          AND STATUS = 1
                                </where>

                                ) UNION ALL
                                ( SELECT id, Operation_Id, Powerr_Id, Line_Id FROM pms_monitor
                                <where>

                                    org_id IN
                                    <foreach collection="orgIds" item="item" open="(" close=")" separator=",">
                                        #{item}
                                    </foreach>
                                      AND STATUS = 1
                                </where>

                                )
                            ) t
                                INNER JOIN pms_distribution_monitor b ON t.id = b.Monitor_Id

                    )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
                        (
                        ( SELECT id, Operation_Id, Power_Station_Id, Line_Id FROM pms_power_client
                        <where>

                            org_id IN
                            <foreach collection="orgIds" item="item" open="(" close=")" separator=",">
                                #{item}
                            </foreach>
                              AND STATUS = 1
                        </where>
                        ) UNION ALL
                        ( SELECT id, Operation_Id, Power_Station_Id, Line_Id FROM pms_power_generation_user
                        <where>

                            org_id IN
                            <foreach collection="orgIds" item="item" open="(" close=")" separator=",">
                                #{item}
                            </foreach>
                              AND STATUS = 1
                        </where>
                        ) UNION ALL
                        ( SELECT id, Operation_Id, Power_Station_Id, Line_Id FROM pms_power_distributionarea
                        <where>

                            org_id IN
                            <foreach collection="orgIds" item="item" open="(" close=")" separator=",">
                                #{item}
                            </foreach>
                              AND STATUS = 1
                        </where>

                        ) UNION ALL
                        ( SELECT id, Operation_Id, Powerr_Id, Line_Id FROM pms_monitor
                        <where>

                            org_id IN
                            <foreach collection="orgIds" item="item" open="(" close=")" separator=",">
                                #{item}
                            </foreach>
                              AND STATUS = 1
                        </where>

                        )
                        ) t
                        INNER JOIN pms_distribution_monitor b ON t.id = b.Monitor_Id

                        )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
        (
        ( SELECT id, Operation_Id, Power_Station_Id, Line_Id FROM pms_power_client
        <where>

            org_id IN
            <foreach collection="orgIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
              AND STATUS = 1
        </where>
        ) UNION ALL
        ( SELECT id, Operation_Id, Power_Station_Id, Line_Id FROM pms_power_generation_user
        <where>

            org_id IN
            <foreach collection="orgIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
              AND STATUS = 1
        </where>
        ) UNION ALL
        ( SELECT id, Operation_Id, Power_Station_Id, Line_Id FROM pms_power_distributionarea
        <where>

            org_id IN
            <foreach collection="orgIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
              AND STATUS = 1
        </where>

        ) UNION ALL
        ( SELECT id, Operation_Id, Powerr_Id, Line_Id FROM pms_monitor
        <where>

            org_id IN
            <foreach collection="orgIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
              AND STATUS = 1
        </where>

        )
        ) t
        INNER JOIN pms_distribution_monitor b ON t.id = b.Monitor_Id

        )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
            t.org_id as orgNo,
            pss.Longitude as longitude,
            pss.Latitude as latitude,
            b.*
        FROM
        (
        ( SELECT id,org_id, Operation_Id, Power_Station_Id, Line_Id FROM pms_power_client
        <where>
            org_id IN
            <foreach collection="orgIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
              AND STATUS = 1
        </where>
        ) UNION ALL
        ( SELECT id,org_id, Operation_Id, Power_Station_Id, Line_Id FROM pms_power_generation_user
        <where>

            org_id IN
            <foreach collection="orgIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
              AND STATUS = 1
        </where>
        ) UNION ALL
        ( SELECT id,org_id, Operation_Id, Power_Station_Id, Line_Id FROM pms_power_distributionarea
        <where>

            org_id IN
            <foreach collection="orgIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
              AND STATUS = 1
        </where>

        ) UNION ALL
        ( SELECT id,org_id, Operation_Id, Powerr_Id, Line_Id FROM pms_monitor
        <where>

            org_id IN
            <foreach collection="orgIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
              AND STATUS = 1
        </where>

        )
        ) t
        INNER JOIN pms_distribution_monitor b ON t.id = b.Monitor_Id and b.Status=1
        LEFT JOIN pms_statation_stat  pss on t.Power_Station_Id=pss.Power_Id
    </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>