<?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.SpecialAnalysisMapper">

    <select id="selectList" resultType="com.njcn.device.pms.pojo.vo.SpecialAnalysisMonitorVO$SpecialAnalysisVO">
        SELECT
            pm.Id,
            pm.`Name`,
            pm.Org_Name,
            pm.Org_Id,
            pm.Powerr_Name,
            pm.Powerr_Id,
            pm.Terminal_Id,
            pm.Voltage_Level,
            IFNULL(psm.capacity,0) as capacity,
            ps.Longitude,
            ps.Latitude
        FROM
            pms_monitor pm
        LEFT JOIN pms_statation_stat ps ON pm.Powerr_Id = ps.Power_Id
        LEFT JOIN pms_special_monitor psm ON  pm.Id = psm.id
        <where>
            pm.`Status` = 1
            AND ps.`Status` = 1
            <if test="deptIdList != null and  deptIdList.size > 0">
                AND pm.Org_Id IN
                <foreach collection='deptIdList' item='item' index="index" open='(' separator=',' close=')'>
                    #{item}
                </foreach>
            </if>
            <if test="monitorTags != null and  monitorTags!=''">
                AND pm.Monitor_Tag = #{monitorTags}
            </if>
        </where>
    </select>
    <select id="selectYoYList" resultType="com.njcn.device.pms.pojo.vo.SpecialAnalysisMonitorVO$CompareDetailVO">
        SELECT
            SUM(ta.sumNum) as sumNum,
            IFNULL(SUM(tb.sumNum),0) as ytbSumNum,
            IFNULL(SUM(tc.sumNum),0) as yhbSumNum,
            ifnull(round((SUM(ta.sumNum)-SUM(tb.sumNum))/SUM(tb.sumNum)*100,2),0) as sameNum,
            ifnull(round((SUM(ta.sumNum)-SUM(tc.sumNum))/SUM(tc.sumNum)*100,2),0) as ringNum,

            SUM(ta.sumNum2) as sumNum2,
            IFNULL(SUM(tb.sumNum2),0) as ytbSumNum2,
            IFNULL(SUM(tc.sumNum2),0) as yhbSumNum2,
            ifnull(round((SUM(ta.sumNum2)-SUM(tb.sumNum2))/SUM(tb.sumNum2)*100,2),0) as sameNum2,
            ifnull(round((SUM(ta.sumNum2)-SUM(tc.sumNum2))/SUM(tc.sumNum2)*100,2),0) as ringNum2,

            SUM(ta.sumNum3) as sumNum3,
            IFNULL(SUM(tb.sumNum3),0) as ytbSumNum3,
            IFNULL(SUM(tc.sumNum3),0) as yhbSumNum3,
            ifnull(round((SUM(ta.sumNum3)-SUM(tb.sumNum3))/SUM(tb.sumNum3)*100,2),0) as sameNum3,
            ifnull(round((SUM(ta.sumNum3)-SUM(tc.sumNum3))/SUM(tc.sumNum3)*100,2),0) as ringNum3,

            SUM(ta.sumNum4) as sumNum4,
            IFNULL(SUM(tb.sumNum4),0) as ytbSumNum4,
            IFNULL(SUM(tc.sumNum4),0) as yhbSumNum4,
            ifnull(round((SUM(ta.sumNum4)-SUM(tb.sumNum4))/SUM(tb.sumNum4)*100,2),0) as sameNum4,
            ifnull(round((SUM(ta.sumNum4)-SUM(tc.sumNum4))/SUM(tc.sumNum4)*100,2),0) as ringNum4,

            SUM(ta.capacity) as capacity,
            IFNULL(SUM(tb.capacity),0) as ytbCapacity,
            IFNULL(SUM(tc.capacity),0) as yhbCapacity,
            ifnull(round((SUM(ta.capacity)-SUM(tb.capacity))/SUM(tb.capacity)*100,2),0) as sameCapacity,
            ifnull(round((SUM(ta.capacity)-SUM(tc.capacity))/SUM(tc.capacity)*100,2),0) as ringCapacity,

            SUM(ta.capacity2) as capacity2,
            IFNULL(SUM(tb.capacity2),0) as ytbCapacity2,
            IFNULL(SUM(tc.capacity2),0) as yhbCapacity2,
            ifnull(round((SUM(ta.capacity2)-SUM(tb.capacity2))/SUM(tb.capacity2)*100,2),0) as sameCapacity2,
            ifnull(round((SUM(ta.capacity2)-SUM(tc.capacity2))/SUM(tc.capacity2)*100,2),0) as ringCapacity2,

            SUM(ta.capacity3) as capacity3,
            IFNULL(SUM(tb.capacity3),0) as ytbCapacity3,
            IFNULL(SUM(tc.capacity3),0) as yhbCapacity3,
            ifnull(round((SUM(ta.capacity3)-SUM(tb.capacity3))/SUM(tb.capacity3)*100,2),0) as sameCapacity3,
            ifnull(round((SUM(ta.capacity3)-SUM(tc.capacity3))/SUM(tc.capacity3)*100,2),0) as ringCapacity3,

            SUM(ta.capacity3) as capacity4,
            IFNULL(SUM(tb.capacity4),0) as ytbCapacity4,
            IFNULL(SUM(tc.capacity4),0) as yhbCapacity4,
            ifnull(round((SUM(ta.capacity4)-SUM(tb.capacity4))/SUM(tb.capacity4)*100,2),0) as sameCapacity4,
            ifnull(round((SUM(ta.capacity4)-SUM(tc.capacity4))/SUM(tc.capacity4)*100,2),0) as ringCapacity4
        FROM (
             SELECT
                 t.Org_Id,
                 year(t.Data_Date) as yy,
                 month(t.Data_Date) as mm,
                 sum(t.Wind_Power_Num) as sumNum,
                 sum(t.Pv_Num) as sumNum2,
                 sum(t.Electric_Track_Num) as sumNum3,
                 sum(t.Smelting_Load_Num) as sumNum4,

                 sum(t.Wind_Power_Capacity) as capacity,
                 sum(t.Pv_Capacity) as capacity2,
                 sum(t.Electric_Track_Capacity) as capacity3,
                 sum(t.Smelting_Load_Capacity) as capacity4
             FROM  pms_statistics_special_monitor t
            <where>
                AND t.`Status` = 1
                <if test="deptIdList != null and  deptIdList.size > 0">
                    AND t.Org_Id IN
                    <foreach collection='deptIdList' item='item' index="index" open='(' separator=',' close=')'>
                        #{item}
                    </foreach>
                </if>
            </where>
             GROUP BY t.Org_Id,yy,mm
             ) ta
-- 同比：上年同月
         LEFT JOIN (
            SELECT
                t.Org_Id,
                year(t.Data_Date) as yy,
                month(t.Data_Date) as mm,
                sum(t.Wind_Power_Num) as sumNum,
                sum(t.Pv_Num) as sumNum2,
                sum(t.Electric_Track_Num) as sumNum3,
                sum(t.Smelting_Load_Num) as sumNum4,

                sum(t.Wind_Power_Capacity) as capacity,
                sum(t.Pv_Capacity) as capacity2,
                sum(t.Electric_Track_Capacity) as capacity3,
                sum(t.Smelting_Load_Capacity) as capacity4
            FROM  pms_statistics_special_monitor t
            <where>
                AND t.`Status` = 1
                <if test="deptIdList != null and  deptIdList.size > 0">
                    AND t.Org_Id IN
                    <foreach collection='deptIdList' item='item' index="index" open='(' separator=',' close=')'>
                        #{item}
                    </foreach>
                </if>
            </where>
        GROUP BY t.Org_Id,yy,mm
        ) tb
            ON  tb.mm = ta.mm  and tb.yy = ta.yy-1	 and tb.Org_Id = ta.Org_Id
-- 环比：上月
         LEFT JOIN (
            SELECT
                t.Org_Id,
                year(t.Data_Date) as yy,
                month(t.Data_Date) as mm,
                sum(t.Wind_Power_Num) as sumNum,
                sum(t.Pv_Num) as sumNum2,
                sum(t.Electric_Track_Num) as sumNum3,
                sum(t.Smelting_Load_Num) as sumNum4,

                sum(t.Wind_Power_Capacity) as capacity,
                sum(t.Pv_Capacity) as capacity2,
                sum(t.Electric_Track_Capacity) as capacity3,
                sum(t.Smelting_Load_Capacity) as capacity4
            FROM  pms_statistics_special_monitor t
            <where>
                AND t.`Status` = 1
                <if test="deptIdList != null and  deptIdList.size > 0">
                    AND t.Org_Id IN
                    <foreach collection='deptIdList' item='item' index="index" open='(' separator=',' close=')'>
                        #{item}
                    </foreach>
                </if>
            </where>
        GROUP BY t.Org_Id,yy,mm
        ) tc
       ON  (
           (tc.yy = ta.yy and tc.mm = ta.mm - 1 and tc.Org_Id = ta.Org_Id)
           OR
           (tc.yy=ta.yy - 1 AND tc.mm = 12 AND ta.mm = 1 and tc.Org_Id = ta.Org_Id)
           )
        <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>
    <select id="selectRailwayList"
            resultType="com.njcn.device.pms.pojo.vo.SpecialAnalysisMonitorVO$SpecialAnalysisRailwayVO">
        SELECT
            DISTINCT pts.Id,
            pts.`Name`,
            pts.Org_Name,
            pts.Railway_Line_Name,
            pts.Org_Id,
            pts.Power_Name as powerrName,
            pts.Power_Id as powerrId,
            pts.Voltage_Level,
            IFNULL(ptse.capacity,0) as capacity,
            ps.Longitude,
            ps.Latitude
        FROM
            pms_traction_station pts
        LEFT JOIN pms_statation_stat ps ON pts.Power_Id = ps.Power_Id
        LEFT JOIN pms_traction_station_expand ptse ON ptse.traction_Station_Id = pts.Id
        <where>
            pts.`Status` = 1
            AND ps.`Status` = 1
            <if test="deptIdList != null and  deptIdList.size > 0">
                AND pts.Org_Id IN
                <foreach collection='deptIdList' item='item' index="index" open='(' separator=',' close=')'>
                    #{item}
                </foreach>
            </if>
        </where>
    </select>
    <select id="selectMapDisplayRailway"
            resultType="com.njcn.device.pms.pojo.vo.SpecialAnalysisMonitorVO$CompareVO">
        SELECT
            CASE
                ta.railway_type
                WHEN 1 THEN
                    "高铁" ELSE "普铁"
                END as name,
            SUM( ta.sumNum2 ) AS tractionStation,
            SUM( ta.sumNum ) AS num,
            IFNULL( SUM( tb.sumNum ), 0 ) AS ytbSumNum,
            IFNULL( SUM( tc.sumNum ), 0 ) AS yhbSumNum,
            concat(
                    ifnull( round(( SUM( ta.sumNum )- SUM( tb.sumNum ))/ SUM( tb.sumNum )* 100, 2 ), 0 ),
                    '%'
                ) AS sameNum,
            concat(
                    ifnull( round(( SUM( ta.sumNum )- SUM( tc.sumNum ))/ SUM( tc.sumNum )* 100, 2 ), 0 ),
                    '%'
                ) AS ringNum
        FROM
            (
                SELECT
                    t.railway_type,
                    YEAR ( t.Created_Date ) AS yy,
                    MONTH ( t.Created_Date ) AS mm,
                    sum( t.traction_Station_num ) AS sumNum2,
                    sum( t.monitor_num ) AS sumNum
                FROM
                    pms_traction_station_expand t
                <where>
                    <if test="param != null and  param.powerVoltageLevel.size > 0">
                        AND t.voltage_level IN
                        <foreach collection='param.powerVoltageLevel' item='item' index="index" open='(' separator=',' close=')'>
                            #{item}
                        </foreach>
                    </if>
                    <if test="deptIdList != null and  deptIdList.size > 0">
                        AND t.Org_Id IN
                        <foreach collection='deptIdList' item='item' index="index" open='(' separator=',' close=')'>
                            #{item}
                        </foreach>
                    </if>
                </where>
                GROUP BY
                    t.railway_type,
                    yy,
                    mm
            ) ta -- 同比：上年同月
                LEFT JOIN (
                SELECT
                    t.railway_type,
                    YEAR ( t.Created_Date ) AS yy,
                    MONTH ( t.Created_Date ) AS mm,
                    sum( t.traction_Station_num ) AS sumNum2,
                    sum( t.monitor_num ) AS sumNum
                FROM
                    pms_traction_station_expand t
                <where>
                    <if test="param != null and  param.powerVoltageLevel.size > 0">
                        AND t.voltage_level IN
                        <foreach collection='param.powerVoltageLevel' item='item' index="index" open='(' separator=',' close=')'>
                            #{item}
                        </foreach>
                    </if>
                    <if test="deptIdList != null and  deptIdList.size > 0">
                        AND t.Org_Id IN
                        <foreach collection='deptIdList' item='item' index="index" open='(' separator=',' close=')'>
                            #{item}
                        </foreach>
                    </if>
                </where>
                GROUP BY
                    t.railway_type,
                    yy,
                    mm
            ) tb ON tb.mm = ta.mm
                AND tb.yy = ta.yy - 1
                AND tb.railway_type = ta.railway_type -- 环比：上月
                LEFT JOIN (
                SELECT
                    t.railway_type,
                    YEAR ( t.Created_Date ) AS yy,
                    MONTH ( t.Created_Date ) AS mm,
                    sum( t.traction_Station_num ) AS sumNum2,
                    sum( t.monitor_num ) AS sumNum
                FROM
                    pms_traction_station_expand t
                <where>
                    <if test="param != null and  param.powerVoltageLevel.size > 0">
                        AND t.voltage_level IN
                        <foreach collection='param.powerVoltageLevel' item='item' index="index" open='(' separator=',' close=')'>
                            #{item}
                        </foreach>
                    </if>
                    <if test="deptIdList != null and  deptIdList.size > 0">
                        AND t.Org_Id IN
                        <foreach collection='deptIdList' item='item' index="index" open='(' separator=',' close=')'>
                            #{item}
                        </foreach>
                    </if>
                </where>
                GROUP BY
                    t.railway_type,
                    yy,
                    mm
            ) tc ON (
                    ( tc.yy = ta.yy AND tc.mm = ta.mm - 1 AND tc.railway_type = ta.railway_type )
                    OR (
                        tc.yy = ta.yy - 1
                        AND tc.mm = 12
                        AND ta.mm = 1
                        AND tc.railway_type = ta.railway_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>
        GROUP BY
            ta.railway_type

    </select>
    <select id="selectWiringTypeRailway"
            resultType="com.njcn.device.pms.pojo.vo.SpecialAnalysisMonitorVO$WiringTypeDetail">
        SELECT
            connet_group_way,
            sum( traction_Station_num ) as num,
            sum( load_rate_avg ) as rateAvg,
            sum( load_rate_95 ) as rate95,
            sum( load_rate_99 ) as rate99
        FROM
            pms_traction_station_expand
        <where>
            <if test="param!=null and param.year != null ">
              and  YEAR(Created_Date)=#{param.year}
            </if>
            <if test="param != null and param.month!=null and param.month.size > 0">
                AND MONTH(Created_Date) IN
                <foreach collection='param.month' item='item' index="index" open='(' separator=',' close=')'>
                    #{item}
                </foreach>
            </if>
            <if test="deptIdList != null and  deptIdList.size > 0">
                AND Org_Id IN
                <foreach collection='deptIdList' item='item' index="index" open='(' separator=',' close=')'>
                    #{item}
                </foreach>
            </if>
        </where>
        GROUP BY
            connet_group_way
    </select>
</mapper>