<?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.majornetwork.RStatOrgBusbarVoltageMMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.njcn.harmonic.pojo.po.RStatOrgBusbarVoltageM">
        <id column="org_no" property="orgNo" />
        <result column="data_date" property="dataDate" />
        <result column="busbar_id" property="busbarId" />
        <result column="measurement_count" property="measurementCount" />
        <result column="v_average" property="vAverage" />
        <result column="v_accrued" property="vAccrued" />
        <result column="unbalance_average" property="unbalanceAverage" />
        <result column="unbalance_accrued" property="unbalanceAccrued" />
        <result column="flicker_average" property="flickerAverage" />
        <result column="flicker_accrued" property="flickerAccrued" />
    </resultMap>
    <select id="getMstatOrgBusbarVoltageVO" resultType="com.njcn.harmonic.pojo.vo.RStatOrgBusbarVoltageVO">
        SELECT aa.org_no,
               aa.station_name powerStationName,
               aa.scale busbarVoltageLevel,
               ifnull(aa.average,0) average,
               ifnull(aa.accrued,0) accrued,
               ifnull(aa.unbalanceAverage,0) unbalanceAverage,
               ifnull(aa.unbalanceAccrued,0) unbalanceAccrued,
               ifnull(aa.flickerAverage,0) flickerAverage,
               ifnull(aa.flickerAccrued,0) flickerAccrued,
               ifnull(bb.averageSame,0) averageSame,
               ifnull(bb.accruedSame,0) accruedSame,
               ifnull(bb.unbalanceAverageSame,0) unbalanceAverageSame,
               ifnull(bb.unbalanceAccruedSame,0) unbalanceAccruedSame,
               ifnull(bb.flickerAverageSame,0) flickerAverageSame,
               ifnull(bb.flickerAccruedSame,0) flickerAccruedSame
                FROM
                (
                SELECT b.org_no                 org_no,
                       a.Station_Name           station_name,
                       a.scale                  scale,
                       avg(b.v_average) AS      average,
                       sum(b.v_accrued) AS      accrued,
                       avg(b.unbalance_average) unbalanceAverage,
                       sum(b.unbalance_accrued) unbalanceAccrued,
                       avg(b.flicker_average)   flickerAverage,
                       sum(b.flicker_average)   flickerAccrued
                FROM r_stat_org_busbar_voltage_m b,
                     pms_generatrix_wire a
                WHERE
                a.id = b.busbar_id
        <if test="param != null and param.ids != null and param.ids.size > 0">
            AND b.org_no IN
            <foreach collection='param.ids' item='item' index="index" open='(' separator=',' close=')'>
                #{item}
            </foreach>
        </if>
        <if test="param != null and param.startTime != null and param.startTime != ''">
            AND  b.data_date >= #{param.startTime}
        </if>
        <if test="param != null and param.endTime != null and param.endTime != ''">
            AND  b.data_date &lt;= #{param.endTime}
        </if>
        GROUP BY org_no,
                 station_name,
                 scale
                ) aa
                LEFT JOIN (
                SELECT b.org_no                 org_no,
                       a.Station_Name           station_name,
                       a.scale                  scale,
                       avg(b.v_average) AS      averageSame,
                       sum(b.v_accrued) AS      accruedSame,
                       avg(b.unbalance_average) unbalanceAverageSame,
                       sum(b.unbalance_accrued) unbalanceAccruedSame,
                       avg(b.flicker_average)   flickerAverageSame,
                       sum(b.flicker_average)   flickerAccruedSame
                FROM r_stat_org_busbar_voltage_m b,
                     pms_generatrix_wire a
                WHERE
                a.id = b.busbar_id
        <if test="param != null and param.ids != null and param.ids.size > 0">
            AND b.org_no IN
            <foreach collection='param.ids' item='item' index="index" open='(' separator=',' close=')'>
                #{item}
            </foreach>
        </if>
        <if test="param != null and param.startTime != null and param.startTime != ''">
            AND (b.data_date >= date(
                    DATE_ADD(#{param.startTime}, INTERVAL - 1 YEAR)))
        </if>
        <if test="param != null and param.endTime != null and param.endTime != ''">
            AND (b.data_date &lt;= date(
                    DATE_ADD(#{param.endTime}, INTERVAL - 1 YEAR)))
        </if>
        GROUP BY b.org_no,
                 a.Station_Name,
                 a.scale
                ) bb ON aa.org_no = bb.org_no
                AND aa.station_name = bb.station_name
                AND aa.scale = bb.scale
    </select>

</mapper>
