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


    <select id="selectPVCapacity" resultType="com.njcn.device.pms.pojo.vo.PVCapacity">
        SELECT
        sum( P_Capacity ) AS sumCount,
        ( SELECT sum( `Photovoltaic_Capacity` ) FROM pms_power_distributionarea
            <where>
                org_id IN
                <foreach collection="orgIds" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
                AND STATUS = 1
            </where>
        ) AS sumOrgCount,
        ( SELECT sum( `Photovoltaic_Capacity` ) FROM pms_power_distributionarea
            <where>
                org_id IN
                <foreach collection="orgIds" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
                AND Year(Create_Time)=Year(now())
                AND STATUS = 1
            </where>
        ) AS sumOrgTheYear
        FROM
        pms_power_distributionarea

    </select>
    <select id="selectPPVPermeability" resultType="com.njcn.device.pms.pojo.vo.PVCapacity$PVCapacityCount">
        SELECT
        COALESCE( sum( CASE WHEN 0&lt;=num and num&lt;=25 THEN 1 ELSE 0 END ), 0 ) "num_1",
        COALESCE ( sum( CASE WHEN 25&lt;num and num&lt;=50 THEN 1 ELSE 0 END ), 0 ) "num_2",
        COALESCE ( sum( CASE WHEN 50&lt;num and num&lt;=75 THEN 1 ELSE 0 END ), 0 ) "num_3",
        COALESCE ( sum( CASE WHEN 75&lt;num and num&lt;=100 THEN 1 ELSE 0 END ), 0 ) "num_4" ,
        COALESCE ( sum( CASE WHEN 100&lt;num  THEN 1 ELSE 0 END ), 0 ) "num_5"
        FROM
        ( SELECT round( Photovoltaic_Capacity / P_Capacity, 4 )* 100 AS num FROM pms_power_distributionarea
        <where>
            org_id IN
            <foreach collection="orgIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
            AND STATUS = 1
        </where>
        ) x
    </select>
    <select id="selectAbsorptiveCapacity" resultType="com.njcn.device.pms.pojo.vo.PVCapacity$Absorptive">
        SELECT
        Org_Id as org,
        Photovoltaic_Capacity as capacity,
        CASE WHEN 0&lt;=P_Capacity * 0.8 - Photovoltaic_Capacity THEN P_Capacity * 0.8 - Photovoltaic_Capacity ELSE 0 END as Consumption
        FROM
        pms_power_distributionarea
        <where>
            org_id IN
            <foreach collection="orgIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
            AND STATUS = 1
        </where>
    </select>
</mapper>
