<?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.nantian.cloud.dbcapp.carbon.mapper.CarbonEnterpriseMapper">
    <resultMap id="regionEnt" type="com.nantian.cloud.dbcapp.carbon.domain.carbonDetection.RegionEntGroupVO">
        <id property="regionCode" column="REGION_CODE"/>
        <result property="regionName" column="REGION_NAME"/>
        <collection property="childrens"
                    ofType="com.nantian.cloud.dbcapp.carbon.domain.carbonDetection.EnterpriseBrief">
            <id property="entCode" column="ENT_CODE"/>
            <result property="entName" column="ENT_NAME"/>
        </collection>
    </resultMap>

    <select id="entTree" resultMap="regionEnt">
        --         select
--         dicRegion.CODE as REGION_CODE,
--         dicRegion.NAME as REGION_NAME,
--         ent.ENTERPRISE_NAME as ENT_NAME,
--         ent.ENTERPRISE_CODE as ENT_CODE
--         from (select * from DIC_REGION_BASIC where PCODE = '15') as dicRegion
--         left join  ENTERPRISE as ent on dicRegion.CODE = substr(ent.REGION_CODE,1,4)
        SELECT CONCAT(SUBSTR(t.regionCode, 1, 4), '00')                               REGION_CODE,
               CASE d.NAME WHEN '西双版纳傣族自治州' THEN '西双版纳' ELSE SUBSTR(d.NAME, 1, 2) END REGION_NAME,
               t.cmpName                                                              ENT_NAME,
               t.enterpriseCode                                                       ENT_CODE
        FROM energy_record.enterprise_info t
                 LEFT JOIN nt_config.sys_dept d ON d.dept_id = CONCAT(SUBSTR(t.regionCode, 1, 4), '00')
        ORDER BY regionCode
    </select>
    <!--查询企业基本信息接口-->
    <select id="selectEnterpriseVo" parameterType="string"
            resultType="com.nantian.cloud.dbcapp.carbon.domain.carbonDetection.Erprise">
        select *
        from enterprise
        where ENTERPRISE_NAME = #{enterpriseName}
    </select>

    <!--查询机组基本信息-->
    <select id="selectMachineVos" parameterType="string"
            resultType="com.nantian.cloud.dbcapp.carbon.domain.carbonDetection.Machine">
        select *
        from machine
        where ENTERPRISE_NAME = #{enterpriseName}
    </select>

    <!--查询计量器具基本信息-->
    <select id="getInstrumentInfo"
            parameterType="string"
            resultType="com.nantian.cloud.dbcapp.carbon.domain.carbonDetection.InstrumentInfoVo">
        select CODE          as code,
               NAME          as name,
               SPECS         as specs,
               ACU_LEVEL     as acuLevel,
               ASSESS_EXPIRE as assessExpire,
               ASSESS_APPLY  as assessApply,
               UPDATE_DATE   as updateDate,
               ENT_NAME      as entName
        from instrument
        where ENT_NAME = #{enterpriseName}
    </select>

    <!--查询数据统计基本数据信息-->
    <select id="getBaseAccData"
            parameterType="com.nantian.cloud.dbcapp.carbon.domain.carbonDetection.EnterpriseDataRequest"
            resultType="com.nantian.cloud.dbcapp.carbon.domain.carbonDetection.EnterpriseDataVo">
        select
        detail.ENT_NAME,
        detail.MACHE_DATA,
        detail.MONTH,
        round(detail.MONTH_AMOUNT,2) as MONTH_AMOUNT,
        ROUND(total.YEAR_AMOUNT,2) as YEAR_AMOUNT
        from ( select substr(ENTERPRISE_NAME,1,instr(ENTERPRISE_NAME,'-') - 1) as ENT_NAME,
        count(distinct ENTERPRISE_NAME) as MACHE_DATA,
        months.month as MONTH,
        sum(VALUE) as MONTH_AMOUNT
        from (select `day` as `month` from (
        <include refid='days'/>
        ) t where t.`day` &lt;= 12 ) as months
        left join (
        select * from day_consume_detail
        where CODE = 'L'
        and YEAR(DATE) = #{year}
        and instr(ENTERPRISE_NAME,#{entName}) != 0
        ) as detail on MONTH(detail.DATE) = months.month
        group by substr(ENTERPRISE_NAME,1,instr(ENTERPRISE_NAME,'-') - 1),months.month
        ) detail
        left JOIN (
        SELECT substr(ENTERPRISE_NAME,1,instr(ENTERPRISE_NAME,'-') - 1) as ENT_NAME,
        SUM(VALUE) as YEAR_AMOUNT
        FROM day_consume_detail
        WHERE CODE = 'L'
        AND YEAR(DATE) = #{year}
        AND instr(ENTERPRISE_NAME,#{entName}) != 0
        GROUP BY substr(ENTERPRISE_NAME,1,instr(ENTERPRISE_NAME,'-') - 1)
        ) total ON detail.ENT_NAME = total.ENT_NAME
        where detail.`MONTH` = #{month}


        <!--        with detail as (-->
        <!--          select-->
        <!--          substr(ENTERPRISE_NAME,1,instr(ENTERPRISE_NAME,'-') - 1) as ENT_NAME,-->
        <!--          count(distinct ENTERPRISE_NAME) as MACHE_DATA,-->
        <!--          months.month as MONTH,-->
        <!--          sum(VALUE) as MONTH_AMOUNT-->
        <!--          from (select day as month from (<include refid='days'/>) where day &lt;= 12) as months-->
        <!--          left join (-->
        <!--            select * from DAY_CONSUME_DETAIL-->
        <!--            where CODE = 'L'-->
        <!--            and YEAR(DATE) = #{year}-->
        <!--            and instr(ENTERPRISE_NAME,#{entName}) != 0-->
        <!--          ) as detail on MONTH(detail.DATE) = months.month-->
        <!--          group by substr(ENTERPRISE_NAME,1,instr(ENTERPRISE_NAME,'-') - 1),months.month-->
        <!--        )-->
        <!--        select-->
        <!--        detail.ENT_NAME,-->
        <!--        detail.MACHE_DATA,-->
        <!--        detail.MONTH,-->
        <!--        round(detail.MONTH_AMOUNT,2) as MONTH_AMOUNT,-->
        <!--        (select round(sum(MONTH_AMOUNT),2) from detail as innerDetail where innerDetail.MONTH &lt;= detail.MONTH) as YEAR_AMOUNT-->
        <!--        from detail-->
        <!--        where detail.MONTH = #{month}-->
    </select>

    <select id="getMechInfo" resultType="com.nantian.cloud.dbcapp.carbon.domain.carbonDetection.DayConsumeDetail">
        select
            DATE as date,
            ENTERPRISE_NAME as enterpriseName,
            CODE as code,
            INDEX_CLASS as indexClass,
            INDEX_NAME as indexName,
            case when code = 'K' then 'tCO₂/MWh'
            when code = 'L' then 'tCO₂'
            else UNIT
        end as unit,
         case when code = 'A' then round(IFNULL(value, 0), 2)
             when code = 'B' and DATA_SOURCE != 'N/A' then round(IFNULL(value, 0), 4)
             when code = 'B' then 'N/A'
             when code = 'C' then round(IFNULL(value, 0), 5)
             when code = 'D' then round(IFNULL(value, 0), 5)
             when code = 'E' then round(IFNULL(value, 0), 2)
             when code = 'F' then round(IFNULL(value, 0), 3)
             when code = 'G' then round(IFNULL(value, 0), 5)
             when code = 'H' then round(IFNULL(value, 0), 5)
             when code = 'I' then round(IFNULL(value, 0), 2)
             when code = 'J' then round(IFNULL(value, 0), 4)
             when code = 'K' then round(IFNULL(value, 0), 5)
             when code = 'L' then round(IFNULL(value, 0), 2)
        end as value,
         STANDARD_UNSPECIFY as standardUnspecify,
         DATA_SOURCE_TYPE as dataSourceType,
         DATA_SOURCE as dataSource
     from
         day_consume_detail
        where ENTERPRISE_NAME = CONCAT(#{enterpriseName},'-',#{mechNames})
        and date = #{dates}
    </select>

    <sql id="days">
        select 1 as day
        union all
        select 2 as day
        union all
        select 3 as day
        union all
        select 4 as day
        union all
        select 5 as day
        union all
        select 6 as day
        union all
        select 7 as day
        union all
        select 8 as day
        union all
        select 9 as day
        union all
        select 10 as day
        union all
        select 11 as day
        union all
        select 12 as day
        union all
        select 13 as day
        union all
        select 14 as day
        union all
        select 15 as day
        union all
        select 16 as day
        union all
        select 17 as day
        union all
        select 18 as day
        union all
        select 19 as day
        union all
        select 20 as day
        union all
        select 21 as day
        union all
        select 22 as day
        union all
        select 23 as day
        union all
        select 24 as day
        union all
        select 25 as day
        union all
        select 26 as day
        union all
        select 27 as day
        union all
        select 28 as day
        union all
        select 29 as day
        union all
        select 30 as day
        union all
        select 31 as day
    </sql>


    <select id="getMechData" resultType="com.nantian.cloud.dbcapp.carbon.domain.carbonDetection.XY">
        select
        consume.name,
        cems.x,
        consume.y,
        cems.z
        from (
        select
        *
        from (<include refid="days"/>) t
        where day &lt;= DAY(LAST_DAY(CONCAT(#{yearMonth},'-01')))
        ) as timeList
        left join (
        select
        DAY(date) as x,
        round(IFNULL(AMOUNT, 0), 2) as z
        from
        day_consume
        where
        substr(DATE, 1, 7) = #{yearMonth}
        and ENTERPRISE_NAME = #{enterpriseName}
        and MACHINE_NAME = #{mechNames}
        ) as cems on cems.x = timeList.day
        left join (
        select
        #{mechNames} as name,
        DAY(date) as x,
        round(IFNULL(VALUE, 0), 2) as y
        from
        day_consume_detail
        where
        substr(DATE, 1, 7) = #{yearMonth}
        and ENTERPRISE_NAME = concat(#{enterpriseName},'-', #{mechNames})
        and CODE = 'L'
        ) as consume on consume.x = timeList.day
    </select>

    <select id="machines" resultType="string">
        select MACHINE_NAME
        from machine
        where ENTERPRISE_NAME = #{enterpriseName}
    </select>

    <select id="carbonMonthSource" resultType="java.util.Map">
        select concat(cast(substr(date,6,2) as UNSIGNED),'月',cast(substr(date,9,2) as UNSIGNED),'日') as name,
        <foreach collection="mlist" index="i" item="item" separator="," close="," >
            (select round(IFNULL(sum(e.`value`),0),2) from day_consume_detail e WHERE  e.ENTERPRISE_NAME = concat(#{enterpriseName}, '-',#{item})
                and e.date = t.date
                and e.CODE = t.CODE
            ) as v${i}
        </foreach>
        round(IFNULL(sum(t.value),0),2) total
        from (
        <foreach collection="mlist" index="i" item="item" separator="UNION">
            select date,`value`,ENTERPRISE_NAME,#{item} as MACHINE_NAME,CODE from day_consume_detail  WHERE  ENTERPRISE_NAME = concat(#{enterpriseName}, '-',#{item})
            and CODE = 'L'
            AND substr( DATE, 1, 7 ) = #{yearMonth}
        </foreach>
        ) t GROUP BY date
<!--        select-->
<!--        concat(cast(substr(#{yearMonth},6,2) as UNSIGNED),'月',TIME,'日') as name,-->
<!--        <foreach collection="mlist" index="i" item="item" separator="," close=",">-->
<!--            round(v${i},2) as v${i}-->
<!--        </foreach>-->
<!--        <foreach collection="mlist" index="i" item="item" separator="+" close=" as total">-->
<!--            round(IFNULL(v${i},0),2)-->
<!--        </foreach>-->
<!--        from-->
<!--        (-->
<!--        select * from-->
<!--        (-->
<!--        select-->
<!--        machineName.MACHINE_NAME,-->
<!--        dates.TIME,-->
<!--        consume.VALUE-->
<!--        from (-->
<!--        select-->
<!--        MACHINE_NAME-->
<!--        from MACHINE-->
<!--        where ENTERPRISE_NAME = #{enterpriseName}-->
<!--        ) as machineName-->
<!--        left join (-->
<!--        select rownum as TIME from DUAL-->
<!--        connect by rownum &lt;= day(last_day(concat(#{yearMonth},'-01')))-->
<!--        ) as dates on true-->
<!--        left join (-->
<!--        select-->
<!--        cast(substr(DATE,9,2) as UNSIGNED) as TIME,-->
<!--        ENTERPRISE_NAME,-->
<!--        VALUE-->
<!--        from DAY_CONSUME_DETAIL-->
<!--        where ENTERPRISE_NAME like concat(#{enterpriseName},'%')-->
<!--        and substr(DATE,1,7) = #{yearMonth}-->
<!--        and CODE = 'L'-->
<!--        ) as consume on consume.TIME = dates.TIME-->
<!--        and consume.ENTERPRISE_NAME = concat(#{enterpriseName},'-',machineName.MACHINE_NAME)-->
<!--        &#45;&#45; AND dates.TIME &lt;'27'-->
<!--        )-->
<!--        pivot(-->
<!--        max(VALUE) for MACHINE_NAME in-->
<!--        <foreach collection="mlist" index="i" item="item" separator="," open="(" close=")">-->
<!--            #{item} as v${i}-->
<!--        </foreach>-->
<!--        )-->
<!--        order by time-->
<!--        )-->
    </select>

    <select id="carbonYearSource" resultType="java.util.Map">
            select
                concat(cast(substr(date,6,2) as UNSIGNED),'月') as name,
                <foreach collection="mlist" index="i" item="item" separator="," close="," >
                    (select round(IFNULL(sum(e.`value`),0),2) from day_consume_detail e WHERE  e.ENTERPRISE_NAME = concat(#{enterpriseName}, '-',#{item})
                    and substr( e.DATE, 1, 7 )  = t.date
                    and e.CODE = t.CODE
                    ) as v${i}
                </foreach>
                round(IFNULL(sum(t.value),0),2) total
            from (
                <foreach collection="mlist" index="i" item="item" separator="UNION">
                    select substr( DATE, 1, 7 ) date,round(IFNULL(sum(`value`),0),2) value,ENTERPRISE_NAME,#{item} as MACHINE_NAME,CODE from day_consume_detail  WHERE  ENTERPRISE_NAME = concat(#{enterpriseName}, '-',#{item})
                    and CODE = 'L'
                    AND substr( DATE, 1, 4 ) = #{year}
                    GROUP BY substr( DATE, 1, 7 )
                </foreach>
            ) t GROUP BY date

<!--        select-->
<!--        concat(TIME,'月') as name,-->
<!--        <foreach collection="mlist" index="i" item="item" separator="," close=",">-->
<!--            round(v${i},2) as v${i}-->
<!--        </foreach>-->
<!--        <foreach collection="mlist" index="i" item="item" separator="+" close=" as total">-->
<!--            round(nvl(v${i},0),2)-->
<!--        </foreach>-->
<!--        from-->
<!--        (-->
<!--        select * from-->
<!--        (-->
<!--        select-->
<!--        machineName.MACHINE_NAME,-->
<!--        dates.TIME,-->
<!--        consume.VALUE-->
<!--        from (-->
<!--        select-->
<!--        MACHINE_NAME-->
<!--        from MACHINE-->
<!--        where ENTERPRISE_NAME = #{enterpriseName}-->
<!--        ) as machineName-->
<!--        left join (-->
<!--        select rownum as TIME from DUAL-->
<!--        connect by rownum &lt;= decode(YEAR(curdate()), #{year},MONTH(curdate()),12)-->
<!--        ) as dates on true-->
<!--        left join (-->
<!--        select-->
<!--        cast(substr(DATE,6,2) as INTEGER) as TIME,-->
<!--        ENTERPRISE_NAME,-->
<!--        sum(VALUE) as VALUE-->
<!--        from DAY_CONSUME_DETAIL-->
<!--        where ENTERPRISE_NAME like concat(#{enterpriseName},'%')-->
<!--        and substr(DATE,1,4) = #{year}-->
<!--        and CODE = 'L'-->
<!--        group by cast(substr(DATE,6,2) as INTEGER),ENTERPRISE_NAME-->
<!--        ) as consume on consume.TIME = dates.TIME-->
<!--        and consume.ENTERPRISE_NAME = concat(#{enterpriseName},'-',machineName.MACHINE_NAME)-->
<!--        )-->
<!--        pivot(-->
<!--        max(VALUE) for MACHINE_NAME in-->
<!--        <foreach collection="mlist" index="i" item="item" separator="," open="(" close=")">-->
<!--            #{item} as v${i}-->
<!--        </foreach>-->
<!--        )-->
<!--        order by time-->
<!--        )-->
    </select>

    <select id="distribute" resultType="java.util.Map">
        select
        concat(cast(substr(date,6,2) as UNSIGNED),'月') as name,
        <foreach collection="mlist" index="i" item="item" separator="," close="," >
            (select round(IFNULL(sum(e.`value`),0),2) from day_consume_detail e WHERE  e.ENTERPRISE_NAME = concat(#{enterpriseName}, '-',#{item})
            and substr( e.DATE, 1, 7 )  = t.date
            and e.CODE = t.CODE
            ) as v${i}
        </foreach>
        round(IFNULL(sum(t.value),0),2) total
        from (
        <foreach collection="mlist" index="i" item="item" separator="UNION">
            select substr( DATE, 1, 7 ) date,round(IFNULL(sum(`value`),0),2) value,ENTERPRISE_NAME,#{item} as MACHINE_NAME,CODE from day_consume_detail  WHERE  ENTERPRISE_NAME = concat(#{enterpriseName}, '-',#{item})
            and CODE = 'K'
            AND substr( DATE, 1, 4 ) = #{year}
            GROUP BY substr( DATE, 1, 7 )
        </foreach>
        ) t GROUP BY date


<!--        select-->
<!--        concat(TIME,'月') as name,-->
<!--        <foreach collection="mlist" index="i" item="item" separator="," close=",">-->
<!--            round(v${i},2) as v${i}-->
<!--        </foreach>-->
<!--        <foreach collection="mlist" index="i" item="item" separator="+" close=" as total">-->
<!--            round(nvl(v${i},0),2)-->
<!--        </foreach>-->
<!--        from-->
<!--        (-->
<!--        select * from-->
<!--        (-->
<!--        select-->
<!--        machineName.MACHINE_NAME,-->
<!--        dates.TIME,-->
<!--        consume.VALUE-->
<!--        from (-->
<!--        select-->
<!--        MACHINE_NAME-->
<!--        from MACHINE-->
<!--        where ENTERPRISE_NAME = #{enterpriseName}-->
<!--        ) as machineName-->
<!--        left join (-->
<!--        select rownum as TIME from DUAL-->
<!--        connect by rownum &lt;= decode(YEAR(curdate()), #{year},MONTH(curdate()),12)-->
<!--        ) as dates on true-->
<!--        left join (-->
<!--        select-->
<!--        cast(MONTH as INTEGER) as TIME,-->
<!--        concat(ENTERPRISE_NAME,'-',MACHINE_NAME) as ENTERPRISE_NAME,-->
<!--        VALUE-->
<!--        from (-->
<!--        select * from-->
<!--        (-->
<!--        select-->
<!--        *-->
<!--        from DISTRIBUTE_ORIGIN-->
<!--        where YEAR = #{year}-->
<!--        and ENTERPRISE_NAME = #{enterpriseName}-->
<!--        and CODE = 'K'-->
<!--        )-->
<!--        unpivot(-->
<!--        VALUE for MONTH in ("1" ,"2","3","4","5","6","7","8","9","10","11","12")-->
<!--        )-->
<!--        )-->
<!--        ) as consume on consume.TIME = dates.TIME-->
<!--        and consume.ENTERPRISE_NAME = concat(#{enterpriseName},'-',machineName.MACHINE_NAME)-->
<!--        )-->
<!--        pivot(-->
<!--        max(VALUE) for MACHINE_NAME in-->
<!--        <foreach collection="mlist" index="i" item="item" separator="," open="(" close=")">-->
<!--            #{item} as v${i}-->
<!--        </foreach>-->
<!--        )-->
<!--        order by time-->
<!--        )-->
    </select>

    <select id="distributeDetail"
            resultType="com.nantian.cloud.dbcapp.carbon.domain.carbonDetection.DistrbuteOriginMonth">
        select CODE,
               INDEX_NAME,
               UNIT,
               case
                   when code = 'B' OR code = 'H' then round(&quot;${month}&quot;, 3)
                   else round(&quot;${month}&quot;, 2) end as VALUE,
        SOURCE_TYPE,
        SOURCE_NAME
        from distribute_origin
        where YEAR = #{year}
          and MACHINE_NAME = #{machineName}
          and ENTERPRISE_NAME = #{enterpriseName}
    </select>

    <select id="distributeTotal"
            resultType="com.nantian.cloud.dbcapp.carbon.domain.carbonDetection.DistributeMonth">
        select
        distri.`month` as MONTH,
        round(distri.VALUE,2) as TOTAL,
        round(consume.MONTH_AMOUNT,2) as BLOWOFF,
        round(IFNULL(distri.VALUE,0) - IFNULL(consume.MONTH_AMOUNT,0),2) as distribute
        from (
        SELECT CONCAT(YEAR,'-01') AS month, sum(m1) AS value,
        ENTERPRISE_NAME FROM distribute_origin
        WHERE ENTERPRISE_NAME = #{enterpriseName} and CODE = 'K'
        AND substr( YEAR, 1, 4 ) = #{year}
        GROUP BY ENTERPRISE_NAME
        UNION ALL
        SELECT CONCAT(YEAR,'-02') AS month, sum(m2) AS value,
        ENTERPRISE_NAME FROM distribute_origin
        WHERE ENTERPRISE_NAME = #{enterpriseName} and CODE = 'K'
        AND substr( YEAR, 1, 4 ) = #{year}
        GROUP BY ENTERPRISE_NAME
        UNION ALL
        SELECT CONCAT(YEAR,'-03') AS month, sum(m3) AS value,
        ENTERPRISE_NAME FROM distribute_origin
        WHERE ENTERPRISE_NAME = #{enterpriseName} and CODE = 'K'
        AND substr( YEAR, 1, 4 ) = #{year}
        GROUP BY ENTERPRISE_NAME
        UNION ALL
        SELECT CONCAT(YEAR,'-04') AS month, sum(m4) AS value,
        ENTERPRISE_NAME FROM distribute_origin
        WHERE ENTERPRISE_NAME = #{enterpriseName} and CODE = 'K'
        AND substr( YEAR, 1, 4 ) = #{year}
        GROUP BY ENTERPRISE_NAME
        UNION ALL
        SELECT CONCAT(YEAR,'-05') AS month, sum(m5) AS value,
        ENTERPRISE_NAME FROM distribute_origin
        WHERE ENTERPRISE_NAME = #{enterpriseName} and CODE = 'K'
        AND substr( YEAR, 1, 4 ) = #{year}
        GROUP BY ENTERPRISE_NAME
        UNION ALL
        SELECT CONCAT(YEAR,'-06') AS month, sum(m6) AS value,
        ENTERPRISE_NAME FROM distribute_origin
        WHERE ENTERPRISE_NAME = #{enterpriseName} and CODE = 'K'
        AND substr( YEAR, 1, 4 ) = #{year}
        GROUP BY ENTERPRISE_NAME
        UNION ALL
        SELECT CONCAT(YEAR,'-07') AS month, sum(m7) AS value,
        ENTERPRISE_NAME FROM distribute_origin
        WHERE ENTERPRISE_NAME = #{enterpriseName} and CODE = 'K'
        AND substr( YEAR, 1, 4 ) = #{year}
        GROUP BY ENTERPRISE_NAME
        UNION ALL
        SELECT CONCAT(YEAR,'-08') AS month, sum(m8) AS value,
        ENTERPRISE_NAME FROM distribute_origin
        WHERE ENTERPRISE_NAME = #{enterpriseName} and CODE = 'K'
        AND substr( YEAR, 1, 4 ) = #{year}
        GROUP BY ENTERPRISE_NAME
        UNION ALL
        SELECT CONCAT(YEAR,'-09') AS month, sum(m9) AS value,
        ENTERPRISE_NAME FROM distribute_origin
        WHERE ENTERPRISE_NAME = #{enterpriseName} and CODE = 'K'
        AND substr( YEAR, 1, 4 ) = #{year}
        GROUP BY ENTERPRISE_NAME
        UNION ALL
        SELECT CONCAT(YEAR,'-10') AS month, sum(m10) AS value,
        ENTERPRISE_NAME FROM distribute_origin
        WHERE ENTERPRISE_NAME = #{enterpriseName} and CODE = 'K'
        AND substr( YEAR, 1, 4 ) = #{year}
        GROUP BY ENTERPRISE_NAME
        UNION ALL
        SELECT CONCAT(YEAR,'-11') AS month, sum(m11) AS value,
        ENTERPRISE_NAME FROM distribute_origin
        WHERE ENTERPRISE_NAME = #{enterpriseName} and CODE = 'K'
        AND substr( YEAR, 1, 4 ) = #{year}
        GROUP BY ENTERPRISE_NAME
        UNION ALL
        SELECT CONCAT(YEAR,'-12') AS month, sum(m12) AS value,
        ENTERPRISE_NAME FROM distribute_origin
        WHERE ENTERPRISE_NAME = #{enterpriseName} and CODE = 'K'
        AND substr( YEAR, 1, 4 ) = #{year}
        GROUP BY ENTERPRISE_NAME ) distri
        LEFT JOIN ( select substr( DATE, 1, 7 ) time,sum(`value`) MONTH_AMOUNT,
        substr(ENTERPRISE_NAME,1,instr(ENTERPRISE_NAME,'-') - 1) as ENT_NAME from day_consume_detail
        where CODE = 'L' and YEAR(DATE) = #{year}
        and instr(ENTERPRISE_NAME,#{enterpriseName}) != 0
        GROUP BY substr( DATE, 1, 7 ) ) consume
        on consume.time = distri.`month`



        <!--        with detail as (-->
<!--        select-->
<!--        substr(ENTERPRISE_NAME,1,instr(ENTERPRISE_NAME,'-') - 1) as ENT_NAME,-->
<!--        count(distinct ENTERPRISE_NAME) as MACHE_DATA,-->
<!--        months.month as MONTH,-->
<!--        sum(VALUE) as MONTH_AMOUNT-->
<!--        from (select day as month from (<include refid='days'/>) where day &lt;= 12) as months-->
<!--        left join (-->
<!--        select * from DAY_CONSUME_DETAIL-->
<!--        where CODE = 'L'-->
<!--        and YEAR(DATE) = #{year}-->
<!--        and instr(ENTERPRISE_NAME,#{enterpriseName}) != 0-->
<!--        ) as detail on MONTH(detail.DATE) = months.month-->
<!--        group by substr(ENTERPRISE_NAME,1,instr(ENTERPRISE_NAME,'-') - 1),months.month-->
<!--        ),-->
<!--        consume as (-->
<!--        select-->
<!--        detail.*,-->
<!--        (select sum(MONTH_AMOUNT) from detail as innerDetail where innerDetail.MONTH &lt;= detail.MONTH) as YEAR_AMOUNT-->
<!--        from detail-->
<!--        ),-->
<!--        distri as (-->
<!--        select-->
<!--        TIME,-->
<!--        sum(VALUE) as VALUE-->
<!--        from-->
<!--        (-->
<!--        select-->
<!--        machineName.MACHINE_NAME,-->
<!--        dates.TIME,-->
<!--        consume.VALUE-->
<!--        from (-->
<!--        select-->
<!--        MACHINE_NAME-->
<!--        from MACHINE-->
<!--        where ENTERPRISE_NAME = #{enterpriseName}-->
<!--        ) as machineName-->
<!--        left join (-->
<!--        select rownum as TIME from DUAL-->
<!--        connect by rownum &lt;= decode(YEAR(curdate()), #{year},MONTH(curdate()),12)-->
<!--        ) as dates on true-->
<!--        left join (-->
<!--        select-->
<!--        cast(MONTH as INTEGER) as TIME,-->
<!--        concat(ENTERPRISE_NAME,'-',MACHINE_NAME) as ENTERPRISE_NAME,-->
<!--        VALUE-->
<!--        from (-->
<!--        select * from-->
<!--        (-->
<!--        select-->
<!--        *-->
<!--        from DISTRIBUTE_ORIGIN-->
<!--        where YEAR = #{year}-->
<!--        and ENTERPRISE_NAME = #{enterpriseName}-->
<!--        and CODE = 'K'-->
<!--        )-->
<!--        unpivot(-->
<!--        VALUE for MONTH in ("1" ,"2","3","4","5","6","7","8","9","10","11","12")-->
<!--        )-->
<!--        )-->
<!--        ) as consume on consume.TIME = dates.TIME-->
<!--        and consume.ENTERPRISE_NAME = concat(#{enterpriseName},'-',machineName.MACHINE_NAME)-->
<!--        )-->
<!--        group by TIME-->
<!--        order by TIME-->
<!--        )-->
<!--        select-->
<!--        dates.MONTH || '月' as MONTH,-->
<!--        round(distri.VALUE,2) as TOTAL,-->
<!--        round(consume.MONTH_AMOUNT,2) as BLOWOFF,-->
<!--        round(nvl(distri.VALUE,0) - nvl(consume.MONTH_AMOUNT,0),2) as distribute-->
<!--        from (-->
<!--        select rownum as MONTH from DUAL-->
<!--        connect by rownum &lt;= decode(YEAR(curdate()), #{year},MONTH(curdate()),12)-->
<!--        ) as dates-->
<!--        left join consume on consume.MONTH = dates.MONTH-->
<!--        left join distri on distri.TIME = dates.MONTH-->
    </select>

</mapper>
