<?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.jljc.mapper.TReportMapper">
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.nantian.cloud.jljc.entity.TReport">
        <id column="id" property="id"/>
        <result column="enterprise_code" property="enterpriseCode"/>
        <result column="year" property="year"/>
        <result column="director" property="director"/>
        <result column="personnel" property="personnel"/>
        <result column="tel" property="tel"/>
        <result column="date" property="date"/>
        <result column="audit_status" property="auditStatus"/>
        <result column="auditor" property="auditor"/>
        <result column="auditor_id" property="auditorId"/>
        <result column="audit_remark" property="auditRemark"/>
        <result column="audit_date" property="auditDate"/>
        <result column="audit_status2" property="auditStatus2"/>
        <result column="auditor2" property="auditor2"/>
        <result column="auditor_id2" property="auditorId2"/>
        <result column="audit_remark2" property="auditRemark2"/>
        <result column="audit_date2" property="auditDate2"/>
        <result column="audit_status3" property="auditStatus3"/>
        <result column="auditor3" property="auditor3"/>
        <result column="auditor_id3" property="auditorId3"/>
        <result column="audit_remark3" property="auditRemark3"/>
        <result column="audit_date3" property="auditDate3"/>
        <result column="report_status" property="reportStatus"/>
        <result column="prov_status" property="provStatus"/>
        <result column="modify_flag" property="modifyFlag"/>
        <result column="modify_comments" property="modifyComments"/>
        <result column="modify_audit_remark" property="modifyAuditRemark"/>
        <result column="modify_audit_status" property="modifyAuditStatus"/>
        <result column="modify_time" property="modifyTime"/>
        <result column="userid" property="userid"/>
        <result column="update_time" property="updateTime"/>
        <result column="del_flag" property="delFlag"/>
        <result column="dept_id" property="deptId"/>
        <result column="type" property="type"/>
        <result column="report_id" property="reportId"/>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id,
        enterprise_code,
        year,
        director,
        personnel,
        tel,
        date,
        audit_status,
        auditor,
        auditor_id,
        audit_remark,
        audit_date,
        audit_status2,
        auditor2,
        auditor_id2,
        audit_remark2,
        audit_date2,
        audit_status3,
        auditor3,
        auditor_id3,
        audit_remark3,
        audit_date3,
        report_status,
        prov_status,
        modify_flag,
        modify_comments,
        modify_audit_remark,
        modify_audit_status,
        modify_time,
        userid,
        update_time,
        del_flag,
        dept_id,
        type,
        report_id,
        enterprise_code_old
    </sql>

    <insert id="addTReport" useGeneratedKeys="true" keyProperty="id">
        insert into t_report
        values (null, #{enterpriseCode}, #{year}, #{director}, #{personnel}, #{tel}, #{date}, #{auditStatus},
                #{auditor}, #{auditorId}, #{auditRemark}, #{auditDate}, #{reportStatus}, #{provStatus},
                #{modifyFlag}, #{modifyComments}, #{modifyTime}, #{modifyAuditStatus}, #{modifyAuditRemark},
                #{userid}, #{updateTime}, #{delFlag}, #{deptId}, #{type}, #{reportId}, null)
    </insert>

    <select id="noProvStatusLog" resultType="java.lang.Integer">
        insert into t_report_audit_log(report_id, report_type, year, enterprise_code, audit_cmp_name, dept_id, update_time, audit_name)
        select GROUP_CONCAT(r.id) rId, GROUP_CONCAT( distinct r.type ORDER BY r.type asc) rType, r.year, r.enterprise_code, e.cmp_name, e.region_code, (select date_format(NOW(), '%Y-%m-%d')),
        case when d.pname is not null and d.pname != '' then concat(d.pname,",",d.name) else d.name end region
        from t_report r
        left join enterprise_info e on r.enterprise_code = e.enterprise_code
        left join (select d.id, d.meaning2 name, (select meaning2 from dict_region where id = d.pid and d.pid != '530000') pname from dict_region d) d on e.region_code = d.id
        where r.prov_status = "2" and r.del_flag = '1' and e.cmp_name is not null and e.cmp_name != '' group by r.enterprise_code, r.year
    </select>

    <update id="upProvStatus">
        update t_report
        set prov_status = "1", update_time = (select date_format(NOW(), '%Y-%m-%d'))
        where del_flag = '1'
    </update>

    <update id="renewReportRegion">
        update t_report set dept_id = #{regionCode} where enterprise_code = #{enterpriseCode};
        update t_report_enterprise set area = #{regionCode}, parea = concat(left(#{regionCode},4),"00"), region_code = #{regionCode}, dept_id = #{regionCode} where enterprise_code = #{enterpriseCode};
        update enterprise_info set region_code = #{regionCode}, dept_id = #{regionCode} where enterprise_code = #{enterpriseCode};
    </update>

    <select id="getModifyRecords" resultType="com.nantian.cloud.jljc.vo.ReportModifyVo">
        select e.cmp_name                        AS name,
               e.enterprise_code                 AS enterpriseCode,
               r.year                            AS time,
               case type
                       when '1' then '报表1-1基本情况表'
                       when '2' then '报表1-2能源消费结构表'
                       when '2-1' then '报表1-2能源消费结构表附表'
                       when '3' then '报表1-3单位产品综合能耗情况表'
                       when '3-1' then '报表1-3单位产品综合能耗情况表附表'
                       when '4' then '报表1-4进度节能量目标完成情况表'
                       else '报表1-5节能改造项目情况表' end AS reportName,
               '年报'                              AS reportType,
               r.modify_time                     AS modifyTime,
               r.modify_comments                 AS modifyComments,
               r.id                              AS id,
               type                              AS reportNum,
               r.modify_audit_remark             AS modifyAuditRemark,
               r.modify_audit_status             AS modifyAuditStatus,
               e.dept_id
        from t_report r, enterprise_info e
        where r.modify_flag = '1' and r.enterprise_code = e.enterprise_code and r.del_flag = "1"
        <if test="cmpName != null and cmpName != ''">
            and e.cmp_name like concat("%", #{cmpName}, "%")
        </if>
        <if test="status != null and status != ''">
            and r.modify_audit_status like concat("%", #{status}, "%")
        </if>
        order by modify_audit_status asc, modifyTime DESC
    </select>

    <select id="getInfoByType" resultType="com.nantian.cloud.jljc.entity.TReport">
        select


        <include refid="Base_Column_List">
        </include>


        from t_report
        where enterprise_code = #{epc}
          and year = #{year}
          and type = #{type}
          and del_flag = "1"
    </select>

    <!--proStatus 报表状态，企业只要有一张报表 prov_status状态为1，省级就可以查看该企业报表-->
    <select id="getListPage" resultType="java.util.HashMap">
        select distinct e.id,
        IFNULL(e.enterprise_code,'') enterprise_code,
        IFNULL(e.cmp_name,'') cmp_name,
        IFNULL(e.address,'') address,
        IFNULL(e.zip_code,'') zip_code,
        IFNULL(e.corporation_name,'') corporation_name,
        IFNULL(e.region_code,'') region_code,
        IFNULL(e.type_code,'') type_code,
        IFNULL(e.industry_code,'') industry_code,
        IFNULL(e.field_code,'') field_code,
        IFNULL(e.energy_consume_level,'') energy_consume_level,
        IFNULL(e.phone,'') phone,
        IFNULL(e.fax,'') fax,
        IFNULL(e.email,'') email,
        IFNULL(e.url,'') url,
        IFNULL(e.state,'') state,
        IFNULL(e.remark,'') remark,
        IFNULL(e.company_type,'') company_type,
        IFNULL(e.update_time,'') update_time,
        IFNULL(e.dept_id,'') dept_id,
        (select IFNULL(meaning,'') from dict_region where id = e.region_code) meaning,
        (select IFNULL(name1998,'') from dict_enterprise_type1998 where id = e.type_code) type_name,
        IFNULL(k.name,'') kind_name,
        IFNULL(re.icsem,'') icsem,
        IFNULL(re.icsem_tel,'') icsem_tel,
        IFNULL(re.fubiao2,0) fubiao2,
        IFNULL(re.fubiao3,0) fubiao3,
        IFNULL(re.processing_link,IFNULL(e.jgzh,2)) jgzh,
        IFNULL(tr.txzt,0) txzt,
        IFNULL(tr.sbzt,0) sbzt,
        IFNULL(tr.proStatus,0) proStatus,
        IFNULL(tr.auditStatus,0) auditStatus,
        IFNULL(tr.auditStatus2,0) auditStatus2,
        IFNULL(tr.auditStatus3,0) auditStatus3,
        IFNULL(tr.returnStatus,0) returnStatus,
        IFNULL(tr.returnStatus2,0) returnStatus2,
        IFNULL(tr.returnStatus3,0) returnStatus3,
        IFNULL(tr.reportDate,'') reportDate,
        IFNULL(tr.auditDate,'') auditDate,
        IFNULL(tr.auditDate2,'') auditDate2,
        IFNULL(tr.auditDate3,'') auditDate3
        from enterprise_info e
        left join trade_kind k on k.code = e.industry_code
        left join(
            select year, enterprise_code, icsem, icsem_tel, processing_link, trade,
            case when processing_link like '1' then 1 else 0 end fubiao2,
            case when trade like 'C301' or trade like 'C302' then 1 else 0 end fubiao3
            from t_report_enterprise where del_flag = '1' and year = #{query.year}
        )re on re.enterprise_code = e.enterprise_code
        left join (
            select enterprise_code,
            max(date) reportDate,
            GROUP_CONCAT(distinct type) type,
            GROUP_CONCAT(distinct prov_status) prov_status,
            sum(case when report_status = '3' then 1 else 0 end) sbzt,
            sum(case when report_status = '2' then 1 else 0 end) txzt,
            sum(case when prov_status = '1' then 1 else 0 end)  proStatus,
            sum(case when audit_status = '2' then 1 else 0 end) auditStatus,
            sum(case when audit_status2 = '2' then 1 else 0 end) auditStatus2,
            sum(case when audit_status3 = '2' then 1 else 0 end) auditStatus3,
            sum(case when audit_status = '4' then 1 else 0 end) returnStatus,
            sum(case when audit_status2 = '4' then 1 else 0 end) returnStatus2,
            sum(case when audit_status3 = '4' then 1 else 0 end) returnStatus3,
            max(audit_date) auditDate,
            max(audit_date2) auditDate2,
            max(audit_date3) auditDate3
            from t_report
            where year = #{query.year} and del_flag = '1'
            group by enterprise_code
        ) tr on tr.enterprise_code = e.enterprise_code
        where e.del_flag = '1'
        <if test="query.code != null and query.code != ''">
            and e.enterprise_code like CONCAT('%', #{query.code}, '%')
        </if>
        <if test="query.name != null and query.name != ''">
            and e.cmp_name like CONCAT('%', #{query.name}, '%')
        </if>
        <if test="query.area != null and query.area != ''">
            <choose>
                <when test='query.area.contains(",")'>
                    and #{query.area} like concat("%",e.region_code,"%")
                </when>
                <otherwise>
                    and e.region_code like concat(#{query.area}, "%")
                </otherwise>
            </choose>
        </if>
        <if test="query.industry != null and query.industry != ''">
            and e.industry_code like concat("%", #{query.industry}, "%")
        </if>
        <if test="list != null and list.length>0">
            and (
            <foreach collection="list" index="index" item="i" separator="or">
                e.notice_year like concat('%', #{i}, '%')
            </foreach>
            )
        </if>
        <if test="query.tradeArr != null and query.tradeArr != ''">
            and k.name like concat("%", #{query.tradeArr}, "%")
        </if>
        <if test="query.item != null and query.item != ''">
            and e.energy_consume_level like #{query.item}
        </if>
        <if test="query.content != null and query.content != ''">
            and tr.prov_status like concat('%',#{query.content},'%')
        </if>
        <if test="query.type != null and query.type != ''">
            <choose>
                <when test="query.type=='0'.toString()">
                    and tr.enterprise_code is not null
                </when>
                <otherwise>
                    and tr.enterprise_code is not null and tr.type like concat('',#{query.type} ,'')
                </otherwise>
            </choose>
        </if>
        <if test="query.state == '1'.toString()">
            and ( tr.sbzt  &lt; 5+re.fubiao2+re.fubiao3 or tr.sbzt is null or tr.sbzt = '' )
        </if>
        <if test="query.state == '0'.toString()">
            and tr.sbzt >= 5+re.fubiao2+re.fubiao3
        </if>
        <if test="query.nbState != null and query.nbState != ''">
            <choose>
                <when test="query.nbState == '1'.toString()">
                    and tr.sbzt >= 5+re.fubiao2+re.fubiao3 and tr.returnStatus = 0 and tr.auditStatus &lt; 5+re.fubiao2+re.fubiao3
                </when>
                <when test="query.nbState == '2'.toString()">
                    and tr.auditStatus >= 5+re.fubiao2+re.fubiao3
                </when>
                <when test="query.nbState == '3'.toString()">
                    and tr.returnStatus > 1
                </when>
                <when test="query.nbState == '4'.toString()">
                    and tr.sbzt >= 5+re.fubiao2+re.fubiao3 and tr.returnStatus2 = 0 and tr.auditStatus2 &lt; 5+re.fubiao2+re.fubiao3
                </when>
                <when test="query.nbState == '5'.toString()">
                    and tr.auditStatus2 >= 5+re.fubiao2+re.fubiao3
                </when>
                <when test="query.nbState == '6'.toString()">
                    and tr.returnStatus2 > 1
                </when>
                <when test="query.nbState == '7'.toString()">
                   and tr.sbzt >= 5+re.fubiao2+re.fubiao3 and tr.returnStatus3 = 0 and tr.auditStatus3 &lt; 5+re.fubiao2+re.fubiao3
                </when>
                <when test="query.nbState == '8'.toString()">
                    and tr.auditStatus3 >= 5+re.fubiao2+re.fubiao3
                </when>
                <when test="query.nbState == '9'.toString()">
                    and tr.returnStatus3 > 1
                </when>
            </choose>
        </if>
        order by region_code asc, cmp_name desc
    </select>
<select id="getNoAuditPage" resultType="java.util.HashMap">
    select t.id, IFNULL(t.enterprise_code,'') enterprise_code,IFNULL(t.cmp_name,'') cmp_name,IFNULL(t.address,'') address,
    IFNULL(t.zip_code,'') zip_code,IFNULL(t.corporation_name,'') corporation_name,IFNULL(t.region_code,'') region_code,
    IFNULL(t.type_code,'') type_code,
    IFNULL(t.industry_code,'') industry_code,
    IFNULL(t.field_code,'') field_code,
    IFNULL(t.energy_consume_level,'') energy_consume_level,
    IFNULL(t.phone,'') phone,
    IFNULL(t.fax,'') fax,
    IFNULL(t.email,'') email,
    IFNULL(t.url,'') url,
    IFNULL(t.state,'') state,
    IFNULL(t.remark,'') remark,
    IFNULL(t.company_type,'') company_type,
    IFNULL(t.update_time,'') update_time,
    IFNULL(t.dept_id,'') dept_id,
    IFNULL(t.meaning,'') meaning,
    IFNULL(t.type_name,'') type_name,
    IFNULL(t.kind_name,'') kind_name,
    IFNULL(t.icsem,'') icsem,
    IFNULL(t.icsem_tel,'') icsem_tel,IFNULL(t.txzt,0) txzt,IFNULL(t.sbzt,0) sbzt,IFNULL(t.proStatus,0) proStatus,IFNULL(t.auditStatus,0) auditStatus,
    IFNULL(t.jgzh,2) jgzh,IFNULL(t.fubiao2,0) fubiao2,IFNULL(t.fubiao3,0) fubiao3 from(select e.id,
    e.enterprise_code,e.cmp_name,e.address,e.zip_code,e.corporation_name,e.region_code,e.type_code,e.industry_code,
    e.field_code,e.energy_consume_level,e.phone,e.fax,e.email,e.url,e.state,e.remark,e.company_type,e.update_time,e.dept_id,
    -- d.meaning,t.name1998 type_name,
    (select meaning from dict_region where id = e.region_code)             meaning,
    (select name1998 from dict_enterprise_type1998 where id = e.type_code) type_name,
    k.name                                                                 kind_name,
    re.icsem,
    re.icsem_tel,
    tr.txzt,
    tr.sbzt,
    tr.proStatus,
    tr.auditStatus,
    IFNULL(re.processing_link,e.jgzh)                                      jgzh,
    re.fubiao2,
    re.fubiao3
    from enterprise_info e
    -- left join dict_region d on d.id=e.region_code
    -- left join dict_enterprise_type1998 t on t.id=e.type_code
    left join trade_kind k on k.code = e.industry_code
    left join(
    select year, enterprise_code, icsem, icsem_tel, processing_link, trade,
    case when processing_link like '1' then 1 else 0 end fubiao2,
    case when trade like 'C301' or trade like 'C302' then 1 else 0 end fubiao3
    from t_report_enterprise where del_flag = '1' and year = #{query.year}
    )re on re.enterprise_code = e.enterprise_code
    left join (
    select enterprise_code,
    IFNULL(sum(case when report_status = '3' then 1 else 0 end),0) sbzt,
    IFNULL(sum(case when report_status = '2' then 1 else 0 end),0) txzt,
    IFNULL(sum(case when prov_status = '1' then 1 else 0 end),0)  proStatus,
    IFNULL(sum(case when audit_status = '2' then 1 else 0 end),0) auditStatus
    from t_report
    where year = #{query.year}
    and del_flag = '1'
    group by enterprise_code
    ) tr on tr.enterprise_code = e.enterprise_code
    where e.del_flag = '1'


    <if test="query.code != null and query.code != ''">
        and e.enterprise_code like CONCAT('%', #{query.code}, '%')
    </if>
    <if test="query.name != null and query.name != ''">
        and e.cmp_name like CONCAT('%', #{query.name}, '%')
    </if>
    <if test="query.area != null and query.area != ''">
        and e.region_code like concat(#{query.area}, "%")
    </if>
    <if test="query.industry != null and query.industry != ''">
        and e.industry_code like concat("%", #{query.industry}, "%")
    </if>
    <if test="query.tradeArr != null and query.tradeArr != ''">
        and k.name like concat("%", #{query.tradeArr}, "%")
    </if>
    <if test="query.item != null and query.item != ''">
        and e.energy_consume_level like #{query.item}
    </if>
    <if test="query.type != null and query.type != ''">
        <choose>
            <when test="query.type=='0'.toString()">
                and e.enterprise_code in (select enterprise_code
                from t_report
                where del_flag = '1'
                and year = #{query.year})
            </when>
            <otherwise>
                and e.enterprise_code in (select enterprise_code
                from t_report
                where del_flag = '1'
                and type = #{query.type}
                and year = #{query.year})
            </otherwise>
        </choose>
    </if>
    <if test="query.state == '1'.toString()">
        <![CDATA[ and ( tr.sbzt < 5 + re.fubiao2 + re.fubiao3 or tr.sbzt is null or tr.sbzt = '' )  ]]>
    </if>
    <if test="query.state == '0'.toString()">
        and tr.sbzt >= 5+re.fubiao2+re.fubiao3
    </if>


    )t
    order by t.region_code asc, t.cmp_name desc
</select>
    <select id="queryCustom" resultType="java.util.HashMap">
        select e.*,
               d.meaning,
               t.name1998                  type_name,
               k.name                      kind_name,
               re.icsem,
               re.icsem_tel,
               (select count(*)
                from t_report
                where year = #{query.year}
                  and del_flag = '1'
                  and enterprise_code = e.enterprise_code
                  and report_status = '2') txzt,
               (select count(*)
                from t_report
                where year = #{query.year}
                  and del_flag = '1'
                  and enterprise_code = e.enterprise_code
                  and report_status = '3') sbzt
        from (
        select * from enterprise_info
        <if test="list != null and list != ''">
            where
            <foreach collection="list" index="index" item="i" separator="or">
                company_type like concat('%', #{i}, '%')
            </foreach>
        </if>
        ) e
                     left join dict_region d on d.id = e.region_code
                     left join dict_enterprise_type1998 t on t.id = e.type_code
                     left join trade_kind k on k.code = e.industry_code
                     left join trade_info b on e.industry_code = b.trade_num
                     left join t_report_enterprise re
                on re.enterprise_code = e.enterprise_code and re.year = #{query.year} and re.del_flag = '1'
                where e.del_flag = '1'

        <if test="query.code != null and query.code != ''">
            and e.enterprise_code = #{query.code}
        </if>
        <if test="query.name != null and query.name != ''">
            and e.cmp_name like CONCAT('%', #{query.name}, '%')
        </if>
        <if test="query.area != null and query.area != ''">
            and #{query.area} like concat('%', left(e.region_code, 4), '%')
        </if>
        <if test="query.industry != null and query.industry != ''">
            and #{query.industry} like concat('%', b.kind_name, '%')
        </if>
        <if test="query.item != null and query.item != ''">
            and e.energy_consume_level like #{query.item}
        </if>
        <if test="query.obj == 11">
            and e.enterprise_code in
                (select enterprise_code from t_report where del_flag = '1' and type = '1' and year = #{query.year})
        </if>
        <if test="query.obj == 12">
            and e.enterprise_code in
                (select enterprise_code from t_report where del_flag = '1' and type = '2' and year = #{query.year})
        </if>
        <if test="query.obj == 13">
            and e.enterprise_code in
                (select enterprise_code from t_report where del_flag = '1' and type = '3' and year = #{query.year})
        </if>
        <if test="query.obj == 14">
            and e.enterprise_code in
                (select enterprise_code from t_report where del_flag = '1' and type = '4' and year = #{query.year})
        </if>
        <if test="query.obj == 15">
            and e.enterprise_code in
                (select enterprise_code from t_report where del_flag = '1' and type = '5' and year = #{query.year})
        </if>
        <if test="query.obj == 21 and query.changeNumStart != null and query.changeNumEnd != null">
            and e.enterprise_code in (
                    select t1.f1
                    from (
                                 select r1.enterprise_code f1, p1.unit_energy_progress c1
                                 from t_report_rateprogress p1
                                              left join
                                              t_report r1 on p1.report_id = r1.id
                                 where r1.del_flag = '1'
                                   and r1.type = '4'
                                   and r1.year = #{query.year}) t1
                                 join
                                 (
                                         select r2.enterprise_code f2, p2.unit_energy_progress c2
                                         from t_report_rateprogress p2
                                                      left join
                                                      t_report r2 on p2.report_id = r2.id
                                         where r2.del_flag = '1'
                                           and r2.type = '4'
                                           and r2.year = #{query.year} - 1) t2 on t1.f1 = t2.f2
                    where abs((t1.c1 - t2.c2) / t1.c1 * 1) >&gt;= #{query.changeNumStart}
                      and abs((t1.c1 - t2.c2) / t1.c1 * 1) &lt;= #{query.changeNumEnd}
                    )
        </if>
        <if test="query.obj == 22 and query.changeNumStart != null and query.changeNumEnd != null">
            and e.enterprise_code in (
                    select t1.f1
                    from (
                                 select r1.enterprise_code f1, p1.energy_progress c1
                                 from t_report_rateprogress p1
                                              left join
                                              t_report r1 on p1.report_id = r1.id
                                 where r1.del_flag = '1'
                                   and r1.type = '4'
                                   and r1.year = #{query.year}) t1
                                 join
                                 (
                                         select r2.enterprise_code f2, p2.energy_progress c2
                                         from t_report_rateprogress p2
                                                      left join
                                                      t_report r2 on p2.report_id = r2.id
                                         where r2.del_flag = '1'
                                           and r2.type = '4'
                                           and r2.year = #{query.year} - 1) t2 on t1.f1 = t2.f2
                    where abs((t1.c1 - t2.c2) / t1.c1 * 1) >&gt;= #{query.changeNumStart}
                      and abs((t1.c1 - t2.c2) / t1.c1 * 1) &lt;= #{query.changeNumEnd}
                    )
        </if>
        <if test="query.obj == 31 and query.changeNumStart != null and query.changeNumEnd != null">
            and e.enterprise_code in (
                    select t1.f1
                    from (
                                 select r1.enterprise_code f1, p1.energy_consumption_cost c1
                                 from t_report_financial p1
                                              left join
                                              t_report r1 on p1.report_id = r1.id
                                 where r1.del_flag = '1'
                                   and r1.type = '1'
                                   and r1.year = #{query.year}) t1
                                 join
                                 (
                                         select r2.enterprise_code f2, p2.energy_consumption_cost c2
                                         from t_report_financial p2
                                                      left join
                                                      t_report r2 on p2.report_id = r2.id
                                         where r2.del_flag = '1'
                                           and r2.type = '1'
                                           and r2.year = #{query.year} - 1) t2 on t1.f1 = t2.f2
                    where abs((t1.c1 - t2.c2) / t1.c1 * 1) >&gt; = #{query.changeNumStart}
                      and abs((t1.c1 - t2.c2) / t1.c1 * 1) &lt;= #{query.changeNumEnd}
                    )
        </if>
        <if test="query.obj == 32 and query.changeNumStart != null and query.changeNumEnd != null">
            and e.enterprise_code in (
                    select t1.f1
                    from (
                                 select r1.enterprise_code f1, p1.production_cost c1
                                 from t_report_financial p1
                                              left join
                                              t_report r1 on p1.report_id = r1.id
                                 where r1.del_flag = '1'
                                   and r1.type = '1'
                                   and r1.year = #{query.year}) t1
                                 join
                                 (
                                         select r2.enterprise_code f2, p2.production_cost c2
                                         from t_report_financial p2
                                                      left join
                                                      t_report r2 on p2.report_id = r2.id
                                         where r2.del_flag = '1'
                                           and r2.type = '1'
                                           and r2.year = #{query.year} - 1) t2 on t1.f1 = t2.f2
                    where abs((t1.c1 - t2.c2) / t1.c1 * 1) >&gt;= #{query.changeNumStart}
                      and abs((t1.c1 - t2.c2) / t1.c1 * 1) &lt;= #{query.changeNumEnd}
                    )
        </if>
        <if test="query.obj == 33 and query.changeNumStart != null and query.changeNumEnd != null">
            and e.enterprise_code in (
                    select t1.f1
                    from (
                                 select r1.enterprise_code f1, p1.sales_revenue c1
                                 from t_report_financial p1
                                              left join
                                              t_report r1 on p1.report_id = r1.id
                                 where r1.del_flag = '1'
                                   and r1.type = '1'
                                   and r1.year = #{query.year}) t1
                                 join
                                 (
                                         select r2.enterprise_code f2, p2.sales_revenue c2
                                         from t_report_financial p2
                                                      left join
                                                      t_report r2 on p2.report_id = r2.id
                                         where r2.del_flag = '1'
                                           and r2.type = '1'
                                           and r2.year = #{query.year} - 1) t2 on t1.f1 = t2.f2
                    where abs((t1.c1 - t2.c2) / t1.c1 * 1) >&gt;= #{query.changeNumStart}
                      and abs((t1.c1 - t2.c2) / t1.c1 * 1) &lt;= #{query.changeNumEnd}
                    )
        </if>
        <if test="query.obj == 34 and query.changeNumStart != null and query.changeNumEnd != null">
            and e.enterprise_code in (
                    select t1.f1
                    from (
                                 select r1.enterprise_code f1, p1.industrial_yield c1
                                 from t_report_financial p1
                                              left join
                                              t_report r1 on p1.report_id = r1.id
                                 where r1.del_flag = '1'
                                   and r1.type = '1'
                                   and r1.year = #{query.year}) t1
                                 join
                                 (
                                         select r2.enterprise_code f2, p2.industrial_yield c2
                                         from t_report_financial p2
                                                      left join
                                                      t_report r2 on p2.report_id = r2.id
                                         where r2.del_flag = '1'
                                           and r2.type = '1'
                                           and r2.year = #{query.year} - 1) t2 on t1.f1 = t2.f2
                    where abs((t1.c1 - t2.c2) / t1.c1 * 1) >&gt;= #{query.changeNumStart}
                      and abs((t1.c1 - t2.c2) / t1.c1 * 1) &lt;= #{query.changeNumEnd}
                    )
        </if>
        <if test="query.obj == 35 and query.changeNumStart != null and query.changeNumEnd != null">
        </if>
        <if test="query.obj == 36 and query.changeNumStart != null and query.changeNumEnd != null">
            and e.enterprise_code in (
                    select t1.f1
                    from (
                                 select r1.enterprise_code f1, p1.taxes_payment c1
                                 from t_report_financial p1
                                              left join
                                              t_report r1 on p1.report_id = r1.id
                                 where r1.del_flag = '1'
                                   and r1.type = '1'
                                   and r1.year = #{query.year}) t1
                                 join
                                 (
                                         select r2.enterprise_code f2, p2.taxes_payment c2
                                         from t_report_financial p2
                                                      left join
                                                      t_report r2 on p2.report_id = r2.id
                                         where r2.del_flag = '1'
                                           and r2.type = '1'
                                           and r2.year = #{query.year} - 1) t2 on t1.f1 = t2.f2
                    where abs((t1.c1 - t2.c2) / t1.c1 * 1) >&gt;= #{query.changeNumStart}
                      and abs((t1.c1 - t2.c2) / t1.c1 * 1) &lt;= #{query.changeNumEnd}
                    )
        </if>
        <if test="query.obj == 37 and query.changeNumStart != null and query.changeNumEnd != null">
            and e.enterprise_code in (
                    select t1.f1
                    from (
                                 select r1.enterprise_code f1, p1.employees_num c1
                                 from t_report_financial p1
                                              left join
                                              t_report r1 on p1.report_id = r1.id
                                 where r1.del_flag = '1'
                                   and r1.type = '1'
                                   and r1.year = #{query.year}) t1
                                 join
                                 (
                                         select r2.enterprise_code f2, p2.employees_num c2
                                         from t_report_financial p2
                                                      left join
                                                      t_report r2 on p2.report_id = r2.id
                                         where r2.del_flag = '1'
                                           and r2.type = '1'
                                           and r2.year = #{query.year} - 1) t2 on t1.f1 = t2.f2
                    where abs((t1.c1 - t2.c2) / t1.c1 * 1) >&gt;= #{query.changeNumStart}
                      and abs((t1.c1 - t2.c2) / t1.c1 * 1) &lt;= #{query.changeNumEnd}
                    )
        </if>
        <if test="query.obj == 38 and query.changeNumStart != null and query.changeNumEnd != null">
            and e.enterprise_code in (
                    select t1.f1
                    from (
                                 select r1.enterprise_code f1, p1.icsem_num c1
                                 from t_report_financial p1
                                              left join
                                              t_report r1 on p1.report_id = r1.id
                                 where r1.del_flag = '1'
                                   and r1.type = '1'
                                   and r1.year = #{query.year}) t1
                                 join
                                 (
                                         select r2.enterprise_code f2, p2.icsem_num c2
                                         from t_report_financial p2
                                                      left join
                                                      t_report r2 on p2.report_id = r2.id
                                         where r2.del_flag = '1'
                                           and r2.type = '1'
                                           and r2.year = #{query.year} - 1) t2 on t1.f1 = t2.f2
                    where abs((t1.c1 - t2.c2) / t1.c1 * 1) >&gt;= #{query.changeNumStart}
                      and abs((t1.c1 - t2.c2) / t1.c1 * 1) &lt;= #{query.changeNumEnd}
                    )
        </if>
        <if test="query.obj == 44 and query.changeNumStart != null and query.changeNumEnd != null">
            and e.enterprise_code in
                (
                        select t1.f1
                        from (
                                     select r1.enterprise_code f1, SUM(p1.consume_industry) c1
                                     from t_report_energys_tructure p1
                                                  left join
                                                  t_report r1 on p1.report_id = r1.id
                                     where r1.del_flag = '1'
                                       and r1.type = '2'
                                       and r1.year = #{query.year}
                                     GROUP BY r1.enterprise_code) t1
                                     join
                                     (
                                             select r2.enterprise_code        f2,
                                                    SUM(p2.consume_industry)  c2,
                                                    sum(energy_saving_effect) e2
                                             from t_report_energys_tructure p2
                                                          left join
                                                          t_report r2 on p2.report_id = r2.id
                                             where r2.del_flag = '1'
                                               and r2.type = '2'
                                               and r2.year = #{query.year} - 1
                                             GROUP BY r2.enterprise_code) t2 on t1.f1 = t2.f2
                        where abs((t1.c1 - t2.c2) / t1.c1 * 1) &gt;= #{query.changeNumStart}
                          and abs((t1.c1 - t2.c2) / t1.c1 * 1) &lt;= #{query.changeNumEnd}
                        )
        </if>
        <if test="query.obj == 45 and query.changeNumStart != null and query.changeNumEnd != null">
            and e.enterprise_code in
                (
                        select t1.f1
                        from (
                                     select r1.enterprise_code f1, SUM(p1.consume_noindustry) c1
                                     from t_report_energys_tructure p1
                                                  left join
                                                  t_report r1 on p1.report_id = r1.id
                                     where r1.del_flag = '1'
                                       and r1.type = '2'
                                       and r1.year = #{query.year}
                                     GROUP BY r1.enterprise_code) t1
                                     join
                                     (
                                             select r2.enterprise_code f2, SUM(p2.consume_noindustry) c2
                                             from t_report_energys_tructure p2
                                                          left join
                                                          t_report r2 on p2.report_id = r2.id
                                             where r2.del_flag = '1'
                                               and r2.type = '2'
                                               and r2.year = #{query.year} - 1
                                             GROUP BY r2.enterprise_code) t2 on t1.f1 = t2.f2
                        where abs((t1.c1 - t2.c2) / t1.c1 * 1) &gt;= #{query.changeNumStart}
                          and abs((t1.c1 - t2.c2) / t1.c1 * 1) &lt;= #{query.changeNumEnd}
                        )
        </if>
        <if test="query.obj == 51 and query.changeNumStart != null and query.changeNumEnd != null">
            and e.enterprise_code in (
                    select t1.f1
                    from (
                                 select r1.enterprise_code f1, p1.unit_energy_consumption c1
                                 from t_report_financial p1
                                              left join
                                              t_report r1 on p1.report_id = r1.id
                                 where r1.del_flag = '1'
                                   and r1.type = '1'
                                   and r1.year = #{query.year}) t1
                                 join
                                 (
                                         select r2.enterprise_code f2, p2.unit_energy_consumption c2
                                         from t_report_financial p2
                                                      left join
                                                      t_report r2 on p2.report_id = r2.id
                                         where r2.del_flag = '1'
                                           and r2.type = '1'
                                           and r2.year = #{query.year} - 1) t2 on t1.f1 = t2.f2
                    where abs((t1.c1 - t2.c2) / t1.c1 * 1) >&gt;= #{query.changeNumStart}
                      and abs((t1.c1 - t2.c2) / t1.c1 * 1) &lt;= #{query.changeNumEnd}
                    )
        </if>
        <if test="query.obj == 61 and query.changeNumStart != null and query.changeNumEnd != null">
            and e.enterprise_code in (
                    select t1.f1
                    from (
                                 select r1.enterprise_code f1, p1.energy_consumption c1
                                 from t_report_financial p1
                                              left join
                                              t_report r1 on p1.report_id = r1.id
                                 where r1.del_flag = '1'
                                   and r1.type = '1'
                                   and r1.year = #{query.year}) t1
                                 join
                                 (
                                         select r2.enterprise_code f2, p2.energy_consumption c2
                                         from t_report_financial p2
                                                      left join
                                                      t_report r2 on p2.report_id = r2.id
                                         where r2.del_flag = '1'
                                           and r2.type = '1'
                                           and r2.year = #{query.year} - 1) t2 on t1.f1 = t2.f2
                    where abs((t1.c1 - t2.c2) / t1.c1 * 1) >&gt;= #{query.changeNumStart}
                      and abs((t1.c1 - t2.c2) / t1.c1 * 1) &lt;= #{query.changeNumEnd}
                    )
        </if>
        <if test="query.obj == 62 and query.changeNumStart != null and query.changeNumEnd != null">
            and e.enterprise_code in (
                    select t1.f1
                    from (
                                 select r1.enterprise_code f1, p1.energy_consumption c1
                                 from t_report_financial p1
                                              left join
                                              t_report r1 on p1.report_id = r1.id
                                 where r1.del_flag = '1'
                                   and r1.type = '1'
                                   and r1.year = #{query.year}) t1
                                 join
                                 (
                                         select r2.enterprise_code f2, p2.energy_consumption c2
                                         from t_report_financial p2
                                                      left join
                                                      t_report r2 on p2.report_id = r2.id
                                         where r2.del_flag = '1'
                                           and r2.type = '1'
                                           and r2.year = #{query.year} - 1) t2 on t1.f1 = t2.f2
                    where abs((t1.c1 - t2.c2) / t1.c1 * 1) >&gt;= #{query.changeNumStart}
                      and abs((t1.c1 - t2.c2) / t1.c1 * 1) &lt;= #{query.changeNumEnd}
                    )
        </if>
        <if test="query.obj == 71 and query.changeNumStart != null and query.changeNumEnd != null">
            and e.enterprise_code in
                (
                        select t1.f1
                        from (
                                     select r1.enterprise_code        f1,
                                            count(*)                  c1,
                                            SUM(money)                m1,
                                            sum(energy_saving_effect) e1
                                     from t_report_energy_project p1
                                                  left join
                                                  t_report r1 on p1.report_id = r1.id
                                     where r1.del_flag = '1'
                                       and r1.type = '5'
                                       and r1.year = #{query.year}
                                     GROUP BY r1.enterprise_code) t1
                                     join
                                     (
                                             select r2.enterprise_code        f2,
                                                    count(*)                  c2,
                                                    SUM(money)                m2,
                                                    sum(energy_saving_effect) e2
                                             from t_report_energy_project p2
                                                          left join
                                                          t_report r2 on p2.report_id = r2.id
                                             where r2.del_flag = '1'
                                               and r2.type = '5'
                                               and r2.year = #{query.year} - 1
                                             GROUP BY r2.enterprise_code) t2 on t1.f1 = t2.f2
                        where abs((t1.c1 - t2.c2) / t1.c1 * 1) &gt;= #{query.changeNumStart}
                          and abs((t1.c1 - t2.c2) / t1.c1 * 1) &lt;= #{query.changeNumEnd}
                        )
        </if>
        <if test="query.obj == 72 and query.changeNumStart != null and query.changeNumEnd != null">
            and e.enterprise_code in
                (
                        select t1.f1
                        from (
                                     select r1.enterprise_code        f1,
                                            count(*)                  c1,
                                            SUM(money)                m1,
                                            sum(energy_saving_effect) e1
                                     from t_report_energy_project p1
                                                  left join
                                                  t_report r1 on p1.report_id = r1.id
                                     where r1.del_flag = '1'
                                       and r1.type = '5'
                                       and r1.year = #{query.year}
                                     GROUP BY r1.enterprise_code) t1
                                     join
                                     (
                                             select r2.enterprise_code        f2,
                                                    count(*)                  c2,
                                                    SUM(money)                m2,
                                                    sum(energy_saving_effect) e2
                                             from t_report_energy_project p2
                                                          left join
                                                          t_report r2 on p2.report_id = r2.id
                                             where r2.del_flag = '1'
                                               and r2.type = '5'
                                               and r2.year = #{query.year} - 1
                                             GROUP BY r2.enterprise_code) t2 on t1.f1 = t2.f2
                        where abs((t1.m1 - t2.m2) / t1.m1 * 1) &gt;= #{query.changeNumStart}
                          and abs((t1.m1 - t2.m2) / t1.m1 * 1) &lt;= #{query.changeNumEnd}
                        )
        </if>
        <if test="query.obj == 73 and query.changeNumStart != null and query.changeNumEnd != null">
            and e.enterprise_code in
                (
                        select t1.f1
                        from (
                                     select r1.enterprise_code        f1,
                                            count(*)                  c1,
                                            SUM(money)                m1,
                                            sum(energy_saving_effect) e1
                                     from t_report_energy_project p1
                                                  left join
                                                  t_report r1 on p1.report_id = r1.id
                                     where r1.del_flag = '1'
                                       and r1.type = '5'
                                       and r1.year = #{query.year}
                                     GROUP BY r1.enterprise_code) t1
                                     join
                                     (
                                             select r2.enterprise_code        f2,
                                                    count(*)                  c2,
                                                    SUM(money)                m2,
                                                    sum(energy_saving_effect) e2
                                             from t_report_energy_project p2
                                                          left join
                                                          t_report r2 on p2.report_id = r2.id
                                             where r2.del_flag = '1'
                                               and r2.type = '5'
                                               and r2.year = #{query.year} - 1
                                             GROUP BY r2.enterprise_code) t2 on t1.f1 = t2.f2
                        where abs((t1.e1 - e2.c2) / e1.c1 * 1) &gt;= #{query.changeNumStart}
                          and abs((t1.e1 - t2.e2) / t1.e1 * 1) &lt;= #{query.changeNumEnd}
                        )
        </if>

        <if test="query.state == '1'.toString()">
            and e.enterprise_code not in (select enterprise_code
                                          from t_report
                                          where del_flag = '1'
                                            and report_status = '3'
                                            and year = #{query.year})
        </if>
        <if test="query.state == '0'.toString()">
            and e.enterprise_code in (select enterprise_code
                                      from t_report
                                      where del_flag = '1'
                                        and report_status = '3'
                                        and year = #{query.year})
        </if>
    </select>


    <select id="getEnergyConsumptionByCom" resultType="java.util.Map">
        select DISTINCT info.dept_id dept_id ,r.enterprise_code as epc,e.enterprise_name as epcName,IFNULL(ROUND(f.energy_consumption,2),0) as
        reportNum,
        IFNUll(ROUND(o.energy_consumption/10000,2),0) as zxjcNum, IFNULL(ROUND(task.tce/10000,2),0) as jnjcNum, e.area
        as area, e.trade as trade
        from t_report r
        left join enterprise_info info on r.enterprise_code = info.enterprise_code
        left join t_report_financial f on r.id=f.report_id
        left join (
        select report_id, area, left(trade,3) trade, enterprise_name from t_report_enterprise
        where area like concat(#{query.area},'%')
        )e on r.id=e.report_id
        left join (
        select a.enterprise_code, sum(a.num) energy_consumption
        from(
        select DISTINCT enterprise_code, num, record_circle from t_temp_online_energy where record_circle like
        concat(#{query.year},"%")
        )a group by a.enterprise_code
        )o on info.epc = o.enterprise_code
        left join t_supervise_task task on r.enterprise_code = task.epc and task.monitor_id like
        concat("%",#{query.year},"%") and task.source like '年度计划'
        where r.report_status='3' and r.del_flag='1' and r.type = 1 and e.enterprise_name is not null


        <if test="query.name != null and query.name != ''">
            and e.enterprise_name like concat("%",#{query.name},"%")
        </if>
        <if test="query.year != null and query.year != ''">
            and r.year = #{query.year}
        </if>
        <if test="query.area != null and query.area != ''">
            and e.area like concat(#{query.area},'%')
        </if>
        <if test="query.industry != null and query.industry != ''">
            and e.trade = #{query.industry}
        </if>


        order by reportNum desc
    </select>

    <select id="getEnergyConsumptionByArea" resultType="java.util.Map">
        select d.id as area,
        <choose>
            <when test="query.area.length == 2">
                CASE SUBSTRING(d.meaning, 1, 2) WHEN '西双' THEN '版纳' ELSE SUBSTRING(d.meaning, 1, 2) END as areaName,
            </when>
            <otherwise>
                d.meaning2 as areaName,
            </otherwise>
        </choose>
        IFNULL(sum(t.reportNum),0) as reportNum,
        IFNULL(sum(t.zxjcNum),0) as zxjcNum,IFNULL(sum(t.jnjcNum),0) as jnjcNum
        from dict_region d
        left join(
        select DISTINCT r.enterprise_code as epc,e.enterprise_name as epcName,IFNULL(ROUND(f.energy_consumption,2),0) as
        reportNum,
        IFNUll(ROUND(o.energy_consumption/10000,2),0) as zxjcNum, IFNULL(ROUND(task.tce/10000,2),0) as jnjcNum, e.area
        as area, e.trade as trade
        from t_report r
        left join enterprise_info info on r.enterprise_code = info.enterprise_code
        left join t_report_financial f on r.id=f.report_id
        left join (
        select report_id, area, left(trade,3) trade, enterprise_name from t_report_enterprise
        where area like concat(#{query.area},'%')
        )e on r.id=e.report_id
        left join (
        select a.enterprise_code, sum(a.num) energy_consumption
        from(
        select DISTINCT enterprise_code, num, record_circle from t_temp_online_energy where record_circle like
        concat(#{query.year},"%")
        )a group by a.enterprise_code
        )o on info.epc = o.enterprise_code
        left join t_supervise_task task on r.enterprise_code = task.epc and task.monitor_id like
        concat("%",#{query.year},"%") and task.source like '年度计划'
        where r.report_status='3' and r.del_flag='1' and r.type = 1 and e.enterprise_name is not null
        -- and r.enterprise_code != '217243660' and r.enterprise_code != '9153030067658902XG' and r.enterprise_code !='915303812172954018'


        <if test="query.name != null and query.name != ''">
            and e.enterprise_name like concat("%",#{query.name},"%")
        </if>
        <if test="query.year != null and query.year != ''">
            and r.year = #{query.year}
        </if>
        <if test="query.area != null and query.area != ''">
            and e.area like concat(#{query.area},'%')
        </if>
        <if test="query.industry != null and query.industry != ''">
            and e.trade = #{query.industry}
        </if>


        )t on d.id = t.area
        <if test="query.area.length == 2">
            where d.pid like concat(#{query.area},'0000')
        </if>
        <if test="query.area.length == 4">
            where d.pid like concat(#{query.area},'00')
        </if>
        group by d.id
        order by reportNum desc
    </select>

    <select id="getEnergyConsumptionByTrade" resultType="java.util.Map">
        select k.code as trade, k.name as trakeName, IFNULL(sum(t.reportNum),0) as reportNum,
        IFNULL(sum(t.zxjcNum),0) as zxjcNum,IFNULL(sum(t.jnjcNum),0) as jnjcNum
        from(
        select tk.id, tk.category, tk.sort, tk.code, tk.name, tk.type
        from(
        select DISTINCT kind.sort from trade_info info left join trade_kind kind
        on info.trade_num = kind.code where kind.sort is not null and kind.sort!="0"
        ) t left join trade_kind tk on t.sort = tk.code
        )k left join (
        select DISTINCT r.enterprise_code as epc,e.enterprise_name as epcName,IFNULL(ROUND(f.energy_consumption,2),0) as
        reportNum,
        IFNUll(ROUND(o.energy_consumption/10000,2),0) as zxjcNum, IFNULL(ROUND(task.tce/10000,2),0) as jnjcNum, e.area
        as area, e.trade as trade
        from t_report r
        left join enterprise_info info on r.enterprise_code = info.enterprise_code
        left join t_report_financial f on r.id=f.report_id
        left join (
        select report_id, area, left(trade,3) trade, enterprise_name from t_report_enterprise
        where area like concat(#{query.area},'%')
        )e on r.id=e.report_id
        left join (
        select a.enterprise_code, sum(a.num) energy_consumption
        from(
        select DISTINCT enterprise_code, num, record_circle from t_temp_online_energy where record_circle like
        concat(#{query.year},"%")
        )a group by a.enterprise_code
        )o on info.epc = o.enterprise_code
        left join t_supervise_task task on r.enterprise_code = task.epc and task.monitor_id like
        concat("%",#{query.year},"%") and task.source like '年度计划'
        where r.report_status='3' and r.del_flag='1' and r.type = 1 and e.enterprise_name is not null
        -- and r.enterprise_code != '217243660' and r.enterprise_code != '9153030067658902XG' and r.enterprise_code !='915303812172954018'


        <if test="query.name != null and query.name != ''">
            and e.enterprise_name like concat("%",#{query.name},"%")
        </if>
        <if test="query.year != null and query.year != ''">
            and r.year = #{query.year}
        </if>
        <if test="query.area != null and query.area != ''">
            and e.area like concat(#{query.area},'%')
        </if>
        <if test="query.industry != null and query.industry != ''">
            and e.trade = #{query.industry}
        </if>


        ) t on k.code = t.trade
        where (t.reportNum>0 or t.zxjcNum>0 or t.jnjcNum>0)
        group by k.code
        order by reportNum desc
    </select>

    <select id="unitConsumptionRanking" resultType="java.util.Map">
        select t.epc, t.epcName, t.year, t.code, t.name, t.unitEnergyConsumption,t.unitEnergyConsumptionUnit
        from (
        select te.enterprise_code as epc, te.enterprise_name as epcName, te.year as year, p.product_num as code, tp.product_name as name,
        case when tp.energy_consumption_unit like '吨标准煤%' then ROUND((tp.energy_consumption*1000)/tp.yield_num,4) else ROUND(tp.energy_consumption/tp.yield_num,4) end unitEnergyConsumption,
        concat("千克标准煤/",tp.yield_unit) as unitEnergyConsumptionUnit
        from t_report_product tp
        inner join t_report_enterprise te on tp.report_id = te.report_id and te.year = #{year} and te.del_flag = "1"
        inner join product_info p on tp.product_name = p.product_name and p.product_num = #{productCode}
        where tp.unit_energy_consumption != 0
        ) t where 1=1 and t.t.unitEnergyConsumption != 0
        <if test="productCode == '310102'">
            <![CDATA[ and t.unitEnergyConsumption  <200 and t.unitEnergyConsumption >50 ]]>
        </if>
        order by t.unitEnergyConsumption
    </select>


    <select id="unitConsumptionTop3" resultType="java.util.Map">
        select t.epc, t.epcName, t.year, t.code, t.name, t.unitEnergyConsumption,t.unitEnergyConsumptionUnit
        from (
        select te.enterprise_code as epc, te.enterprise_name as epcName, te.year as year, p.product_num as code, tp.product_name as name,
        case when tp.energy_consumption_unit like '吨标准煤%' then ROUND((tp.energy_consumption*1000)/tp.yield_num,4) else ROUND(tp.energy_consumption/tp.yield_num,4) end unitEnergyConsumption,
        concat("千克标准煤/",tp.yield_unit) as unitEnergyConsumptionUnit
        from t_report_product tp
        inner join t_report_enterprise te on tp.report_id = te.report_id and te.year = #{year} and te.del_flag = "1"
        inner join product_info p on tp.product_name = p.product_name and p.product_num = #{productCode}
        where tp.unit_energy_consumption != 0
        ) t where 1=1 and t.t.unitEnergyConsumption != 0
        <if test="productCode == '310102'">
            <![CDATA[  and t.unitEnergyConsumption  <200 and t.unitEnergyConsumption >50 ]]>
        </if>
        order by t.unitEnergyConsumption limit 0,3
    </select>

    <select id="queryCustomReport" resultType="java.util.Map">
        select DISTINCT IFNULL(tt.oldEpc,'') oldEpc, IFNULL(tt.epc,'') epc, IFNULL(tt.cmpName,'') cmpName,
        IFNULL(tt.jnjcState,'') jnjcState, IFNULL(tt.reportState,'') reportState, IFNULL(tt.reportProduct,'')
        reportProduct, IFNULL(tt.reportTce,'') reportTce,
        IFNULL(tt.jnjcTce,'') jnjcTce, IFNULL(tt.reportUnitTce,'') reportUnitTce, IFNULL(tt.reportResult,'')
        reportResult, IFNULL(tt.jnjcUnitTce,'') jnjcUnitTce, IFNULL(tt.jnjcResult,'') jnjcResult, IFNULL(tt.level,'')
        level,
        IFNULL(bb.corporation_name,'') corporationName, IFNULL(bb.telephone,'') phone, IFNULL(bb. address,'') address,
        IFNULL(bb.geographical_coordinates,'') geographicalCoordinates, IFNULL(bb.website,'') url,
        IFNULL(bb.zip_code,'') zipCode, IFNULL(bb.email,'') email, IFNULL(bb.fax,'') fax,
        IFNULL(bb.leader,'') leader, IFNULL(bb.leader_tel,'') leaderTel, IFNULL(bb.leader_job,'') leaderJob,
        IFNULL(bb.icsem,'') icsem, IFNULL(bb.icsem_tel,'') icsemTel, IFNULL(bb.staff,'') staff, IFNULL(bb.staff_tel,'')
        staffTel,
        IFNULL(aa.address,'') address2, IFNULL(aa.zip_code,'') zipCode2, IFNULL(aa.corporation_name,'')
        corporationName2, IFNULL(aa.phone,'') phone2, IFNULL(aa.fax,'') fax2,IFNULL(aa.email,'')
        email2,IFNULL(aa.url,'') url2,IFNULL(aa.group_name,'') groupName2,IFNULL(aa.group_address,'') groupAddress2, tt.dept_id dept_id
        from(
        select DISTINCT info.dept_id, info.enterprise_code as oldEpc,info.epc as epc, info.cmp_name as cmpName, a.jnjcState as
        jnjcState, r.reportState as reportState,
        e.productName as reportProduct, b.tce as reportTce, c.tce as jnjcTce, d.level as level,
        f.reportUnitTce as reportUnitTce, f.reportResult as reportResult, g.jnjcUnitTce as jnjcUnitTce, g.jnjcResult as
        jnjcResult
        from enterprise_info info
        left join(
        select e.enterprise_code, case when tt.reportState is null then '1' when tt.reportState like '%3%' then '3' when
        tt.reportState like '%2%' then '2' else '1' end reportState
        from enterprise_info e
        left join(
        select enterprise_code, GROUP_CONCAT(report_status) reportState
        from t_report where year=#{query.endTime} and del_flag='1' GROUP BY enterprise_code
        )tt on e.enterprise_code = tt.enterprise_code
        )r on r.enterprise_code = info.enterprise_code
        left join(
        select tt.monitor_id, tt.epc, tt.type, tt.task_state,
        case when type='2' then n1+n2+n3+n4+n5 else n6 end jnjcState
        from (
        select monitor_id, epc, type, task_state,
        case when insite_supervision is not null or (select count(*) from t_supervision_inspection where monitor_id =
        t.monitor_id)>0 then 0 else 1 end n1,
        case when interview_transcripts is not null or (select count(*) from t_supervision_investigation where
        monitor_id = t.monitor_id)>0 then 0 else 1 end n2,
        case when note is not null or (select count(*) from t_supervision_note where monitor_id = t.monitor_id)>0 then 0
        else 1 end n3,
        case when notification is not null or (select count(*) from t_supervision_notification where monitor_id =
        t.monitor_id)>0 then 0 else 1 end n4,
        case when (select count(*) from t_supervision_comprehensive_energy where monitor_id = t.monitor_id)>0 then 0
        else 1 end n5,
        case when task_state = '3' then 0 else 1 end n6
        from t_supervise_task t
        where source="年度计划" and monitor_id like concat('%',#{query.endTime},'%')
        )tt
        )a on a.epc = info.enterprise_code
        left join(
        select r.enterprise_code, ROUND(f.energy_consumption*10000,3) as tce from t_report r
        left join t_report_financial f on f.report_id = r.id
        where r.year=#{query.endTime} and r.del_flag='1' and r.type ='1'
        )b on b.enterprise_code = info.enterprise_code
        left join(
        select monitor_id, epc, source, ROUND(tce,3) as tce from t_supervise_task
        where type = '2' and source="年度计划" and monitor_id like concat('%',#{query.endTime},'%')
        )c on c.epc = info.enterprise_code
        left join energy_level d on d.id = info.energy_consume_level
        left join(
        select r.enterprise_code, GROUP_CONCAT(p.product_name) as productName
        from t_report r left join t_report_product p on p.report_id = r.id where r.year=#{query.endTime} and
        r.del_flag='1' and r.type ='1' GROUP BY r.enterprise_code
        )e on e.enterprise_code = info.enterprise_code
        left join(
        select tt.enterprise_code, case when tt.guideline_val is null then '' else
        GROUP_CONCAT(concat(tt.quota_name,':',tt.guideline_val,tt.quota_unit)) end reportUnitTce,
        case when tt.result = '' and tt.guideline_val is null then '' else
        GROUP_CONCAT(concat(tt.quota_name,':',tt.result)) end reportResult
        from(
        select r.enterprise_code, u.quota_name, u.quota_unit, u.guideline_val, u.quota_limit,
        case when u.guideline_val is null then '' when u.quota_limit is null then '无限额值'
        when CONVERT(u.guideline_val,DECIMAL(20,4)) > CONVERT(u.quota_limit,DECIMAL(20,4)) then '超限' else '未超限' end
        result
        from t_report r left join t_report_unitenergy u on u.report_id = r.id
        where r.year=#{query.endTime} and r.del_flag='1' and r.type ='3'
        )tt


        <if test="query.num != null and query.num == '1'.toString()">
            where tt.result ='超限'
        </if>


        GROUP BY tt.enterprise_code
        )f on f.enterprise_code = info.enterprise_code
        left join(
        select tt.epc, tt.monitor_id, case when tt.calculation_results is null then '' else
        GROUP_CONCAT(concat(tt.name,':',tt.calculation_results,tt.unit)) end jnjcUnitTce,
        case when tt.result = '' and tt.calculation_results is null then '' else
        GROUP_CONCAT(concat(tt.name,':',tt.result)) end jnjcResult
        from(
        select t.epc, t.monitor_id, u.calculation_results, u.unit_product_limit_standard, u.unit,
        case when u.product_name like '%1.%' or u.product_name like '%2.%' then substring(u.product_name,5) else
        u.product_name end name,
        case when u.calculation_results is null then '' when u.unit_product_limit_standard is null then '无限额值'
        when CONVERT(u.calculation_results,DECIMAL(20,4)) > CONVERT(u.unit_product_limit_standard,DECIMAL(20,4)) then
        '超限' else '未超限' end result
        from t_supervision_unit_energy u
        left join t_supervise_task t on u.monitor_id = t.monitor_id
        where u.tce='1' and t.monitor_id like concat('%',#{query.endTime},'%')
        )tt


        <if test="query.num != null and query.num == '2'.toString()">
            where tt.result ='超限'
        </if>


        GROUP BY tt.monitor_id
        )g on g.monitor_id = c.monitor_id and g.epc = info.enterprise_code


        <trim prefix="where" prefixOverrides="and">
            <if test="query.item != null and query.item != ''">
                and #{query.item} like concat("%",info.energy_consume_level,"%")
            </if>
            <if test="query.type != null and query.type != ''">
                and info.company_type like concat("%",#{query.type},"%")
            </if>
            <if test="query.name != null and query.name != ''">
                and info.cmp_name like concat("%",#{query.name},"%")
            </if>
            <if test="query.industry != null and query.industry != ''">
                and info.industry_code like concat(#{query.industry},"%")
            </if>
            <if test="query.pArea != null and query.pArea != ''">
                and info.region_code like concat(#{query.pArea},"%")
            </if>
            <if test="query.product != null and query.product != ''">
                and e.productName like concat("%",#{query.product},"%")
            </if>
            <if test="query.nbState != null and query.nbState != '' and query.nbState != ''">
                and r.reportState like concat("%",#{query.nbState},"%")
            </if>
            <if test="query.jnState == '0'.toString()">
                and a.jnjcState = 0
            </if>
            <if test="query.jnState == '1'.toString()">
                and a.jnjcState > 0
            </if>
            <if test="query.num != null and query.num == '1'.toString()">
                and f.reportResult like '%:超限%'
            </if>
            <if test="query.num != null and query.num == '2'.toString()">
                and g.jnjcResult like '%:超限%'
            </if>
        </trim>


        )tt left join enterprise_info aa on tt.oldEpc = aa.enterprise_code
        left join t_report_enterprise bb on tt.oldEpc = bb.enterprise_code and year=#{query.endTime}


        <if test="query.source != null and query.source != '' and query.state == '1'.toString()">
            <![CDATA[






             where CONVERT(tt.reportTce,DECIMAL(20,4)) < CONVERT(#{query.source},DECIMAL(20,4)) or CONVERT(tt.jnjcTce,DECIMAL(20,4)) < CONVERT(#{query.source},DECIMAL(20,4))






            ]]>
        </if>
        <if test="query.source != null and query.source != '' and query.state == '2'.toString()">
            <![CDATA[






             WHERE CONVERT(tt.reportTce,DECIMAL(20,4)) <= CONVERT(#{query.source},DECIMAL(20,4)) or CONVERT(tt.jnjcTce,DECIMAL(20,4)) <= CONVERT(#{query.source},DECIMAL(20,4))






            ]]>
        </if>
        <if test="query.source != null and query.source != '' and query.state == '3'.toString()">
            <![CDATA[






             where CONVERT(tt.reportTce,DECIMAL(20,4)) > CONVERT(#{query.source},DECIMAL(20,4)) or CONVERT(tt.jnjcTce,DECIMAL(20,4)) > CONVERT(#{query.source},DECIMAL(20,4))






            ]]>
        </if>
        <if test="query.source != null and query.source != '' and query.state == '4'.toString()">
            <![CDATA[






             where CONVERT(tt.reportTce,DECIMAL(20,4)) >= CONVERT(#{query.source},DECIMAL(20,4)) or CONVERT(tt.jnjcTce,DECIMAL(20,4)) >= CONVERT(#{query.source},DECIMAL(20,4))






            ]]>
        </if>


        order by tt.reportTce desc
    </select>

    <select id="queryCustomReportSummary" resultType="com.nantian.cloud.jljc.vo.QueryCustomReportSummaryVo">
        select DISTINCT #{query.year} as year, #{query.area} as area, (select meaning2 from dict_region where id =
        #{query.area} and del_flag = "1") areaName,
        #{query.industry} as trade, (select name from trade_kind where code = #{query.industry} and del_flag = "1") as
        tradeName,
        IFNULL(sum(CONVERT(tt.reportState,DECIMAL(20,0))),0) reportNum,
        IFNULL(sum(CONVERT(tt.jnjcState,DECIMAL(20,0))),0) jnjcNum,IFNULL(sum(CONVERT(tt.reportTce,DECIMAL(20,3))),0)
        reportTce,
        IFNULL(sum(CONVERT(tt.jnjcTce,DECIMAL(20,3))),0) jnjcTce, IFNULL(count(*),0) epcSum,
        IFNULL(sum(CONVERT(tt.reportResult,DECIMAL(20,0))),0) reportResult,
        IFNULL(sum(CONVERT(tt.jnjcResult,DECIMAL(20,0))),0) jnjcResult
        from(
        select DISTINCT info.enterprise_code as epc, info.cmp_name as cmpName, e.productName as reportProduct,
        case when r.reportState like '%3%' then '1' else '0' end reportState, case when a.jnjcState = 0 then '1' else
        '0' end jnjcState,
        b.tce as reportTce, c.tce as jnjcTce,
        case when f.reportResult like '%:超限%' then '1' else '0' end reportResult,
        case when g.jnjcResult like '%:超限%' then '1' else '0' end jnjcResult
        from enterprise_info info
        left join(
        select e.enterprise_code, case when tt.reportState is null then '1' when tt.reportState like '%3%' then '3' when
        tt.reportState like '%2%' then '2' else '1' end reportState
        from enterprise_info e
        left join(
        select enterprise_code, GROUP_CONCAT(report_status) reportState
        from t_report where year= #{query.year} and del_flag='1' GROUP BY enterprise_code
        )tt on e.enterprise_code = tt.enterprise_code
        )r on r.enterprise_code = info.enterprise_code
        left join(
        select tt.monitor_id, tt.year, tt.epc, tt.type, tt.task_state,
        case when type='2' then n1+n2+n3+n4+n5 else n6 end jnjcState
        from (
        select monitor_id, substring(monitor_id,6,4) as year, epc, type, task_state,
        case when insite_supervision is not null or (select count(*) from t_supervision_inspection where monitor_id =
        t.monitor_id)>0 then 0 else 1 end n1,
        case when interview_transcripts is not null or (select count(*) from t_supervision_investigation where
        monitor_id = t.monitor_id)>0 then 0 else 1 end n2,
        case when note is not null or (select count(*) from t_supervision_note where monitor_id = t.monitor_id)>0 then 0
        else 1 end n3,
        case when notification is not null or (select count(*) from t_supervision_notification where monitor_id =
        t.monitor_id)>0 then 0 else 1 end n4,
        case when (select count(*) from t_supervision_comprehensive_energy where monitor_id = t.monitor_id)>0 then 0
        else 1 end n5,
        case when task_state = '3' then 0 else 1 end n6
        from t_supervise_task t
        where source="年度计划" and monitor_id like concat('%',#{query.year},'%')
        )tt
        )a on a.epc = info.enterprise_code
        left join(
        select r.enterprise_code, r.year, ROUND(f.energy_consumption*10000,3) as tce from t_report r
        left join t_report_financial f on f.report_id = r.id
        where r.del_flag='1' and r.type ='1' and report_status ='3' and r.year= #{query.year}
        )b on b.enterprise_code = info.enterprise_code
        left join(
        select monitor_id, substring(monitor_id,6,4) as year, epc, source, ROUND(tce,3) as tce from t_supervise_task
        where type = '2' and source="年度计划" and monitor_id like concat('%',#{query.year},'%')
        )c on c.epc = info.enterprise_code
        left join energy_level d on d.id = info.energy_consume_level
        left join(
        select r.enterprise_code, GROUP_CONCAT(p.product_name) as productName
        from t_report r left join t_report_product p on p.report_id = r.id where r.year=#{query.year} and r.del_flag='1'
        and r.type ='1' GROUP BY r.enterprise_code
        )e on e.enterprise_code = info.enterprise_code
        left join(
        select tt.enterprise_code, case when tt.guideline_val is null then '' else
        GROUP_CONCAT(concat(tt.quota_name,':',tt.guideline_val,tt.quota_unit)) end reportUnitTce,
        case when tt.result = '' and tt.guideline_val is null then '' else
        GROUP_CONCAT(concat(tt.quota_name,':',tt.result)) end reportResult
        from(
        select r.enterprise_code, u.quota_name, u.quota_unit, u.guideline_val, u.quota_limit,
        case when u.guideline_val is null then '' when u.quota_limit is null then '无限额值'
        when CONVERT(u.guideline_val,DECIMAL(20,4)) > CONVERT(u.quota_limit,DECIMAL(20,4)) then '超限' else '未超限' end
        result
        from t_report r left join t_report_unitenergy u on u.report_id = r.id
        where r.year=#{query.year} and r.del_flag='1' and r.type ='3'
        )tt where tt.result like '超限'
        GROUP BY tt.enterprise_code
        )f on f.enterprise_code = info.enterprise_code
        left join(
        select tt.epc, tt.monitor_id, case when tt.calculation_results is null then '' else
        GROUP_CONCAT(concat(tt.name,':',tt.calculation_results,tt.unit)) end jnjcUnitTce,
        case when tt.result = '' and tt.calculation_results is null then '' else
        GROUP_CONCAT(concat(tt.name,':',tt.result)) end jnjcResult
        from(
        select t.epc, t.monitor_id, u.calculation_results, u.unit_product_limit_standard, u.unit,
        case when u.product_name like '%1.%' or u.product_name like '%2.%' then substring(u.product_name,5) else
        u.product_name end name,
        case when u.calculation_results is null then '' when u.unit_product_limit_standard is null then '无限额值'
        when CONVERT(u.calculation_results,DECIMAL(20,4)) > CONVERT(u.unit_product_limit_standard,DECIMAL(20,4)) then
        '超限' else '未超限' end result
        from t_supervision_unit_energy u
        left join t_supervise_task t on u.monitor_id = t.monitor_id
        where u.tce='1' and t.monitor_id like concat('%',#{query.year},'%')
        )tt where tt.result like '超限'
        GROUP BY tt.monitor_id
        )g on g.monitor_id = c.monitor_id and g.epc = info.enterprise_code


        <trim prefix="where" prefixOverrides="and">
            <if test="query.item != null and query.item != ''">
                and info.energy_consume_level = #{query.item}
            </if>
            <if test="query.type != null and query.type != ''">
                and info.company_type like concat("%",#{query.type},"%")
            </if>
            <if test="query.name != null and query.name != ''">
                and info.cmp_name like concat("%",#{query.name},"%")
            </if>
            <if test="query.industry != null and query.industry != ''">
                and info.industry_code like concat(#{query.industry},"%")
            </if>
            <if test="query.pArea != null and query.pArea != ''">
                and info.region_code like concat(#{query.pArea},"%")
            </if>
            <if test="query.product != null and query.product != ''">
                and e.productName like concat("%",#{query.product},"%")
            </if>
        </trim>


        )tt


        <if test="query.source != null and query.source != '' and query.state == '1'.toString()">
            <![CDATA[


             where CONVERT(tt.reportTce,DECIMAL(20,4)) < CONVERT(#{query.source},DECIMAL(20,4)) or CONVERT(tt.jnjcTce,DECIMAL(20,4)) < CONVERT(#{query.source},DECIMAL(20,4))


            ]]>
        </if>
        <if test="query.source != null and query.source != '' and query.state == '2'.toString()">
            <![CDATA[


             where CONVERT(tt.reportTce,DECIMAL(20,4)) <= CONVERT(#{query.source},DECIMAL(20,4)) or CONVERT(tt.jnjcTce,DECIMAL(20,4)) <= CONVERT(#{query.source},DECIMAL(20,4))


            ]]>
        </if>
        <if test="query.source != null and query.source != '' and query.state == '3'.toString()">
            <![CDATA[


             where CONVERT(tt.reportTce,DECIMAL(20,4)) > CONVERT(#{query.source},DECIMAL(20,4)) or CONVERT(tt.jnjcTce,DECIMAL(20,4)) > CONVERT(#{query.source},DECIMAL(20,4))


            ]]>
        </if>
        <if test="query.source != null and query.source != '' and query.state == '4'.toString()">
            <![CDATA[


             where CONVERT(tt.reportTce,DECIMAL(20,4)) >= CONVERT(#{query.source},DECIMAL(20,4)) or CONVERT(tt.jnjcTce,DECIMAL(20,4)) >= CONVERT(#{query.source},DECIMAL(20,4))


            ]]>
        </if>
    </select>

    <select id="queryCustomReportSummaryByAllArea" resultType="com.nantian.cloud.jljc.vo.QueryCustomReportSummaryVo">
        select DISTINCT #{query.year} as year, tt.infoArea as area, d.meaning2 areaName,
        #{query.industry} as trade, (select name from trade_kind where code = #{query.industry} and del_flag = "1") as
        tradeName,
        IFNULL(sum(CONVERT(tt.reportState,DECIMAL(20,0))),0) reportNum,
        IFNULL(sum(CONVERT(tt.jnjcState,DECIMAL(20,0))),0) jnjcNum,IFNULL(sum(CONVERT(tt.reportTce,DECIMAL(20,3))),0)
        reportTce,
        IFNULL(sum(CONVERT(tt.jnjcTce,DECIMAL(20,3))),0) jnjcTce, IFNULL(count(*),0) epcSum,
        IFNULL(sum(CONVERT(tt.reportResult,DECIMAL(20,0))),0) reportResult,
        IFNULL(sum(CONVERT(tt.jnjcResult,DECIMAL(20,0))),0) jnjcResult
        from(
        select DISTINCT concat(left(info.region_code,4),'00') infoArea, info.enterprise_code as epc, info.cmp_name as
        cmpName, e.productName as reportProduct,
        case when r.reportState like '%3%' then '1' else '0' end reportState, case when a.jnjcState = 0 then '1' else
        '0' end jnjcState,
        b.tce as reportTce, c.tce as jnjcTce,
        case when f.reportResult like '%:超限%' then '1' else '0' end reportResult,
        case when g.jnjcResult like '%:超限%' then '1' else '0' end jnjcResult
        from enterprise_info info
        left join(
        select e.enterprise_code, case when tt.reportState is null then '1' when tt.reportState like '%3%' then '3' when
        tt.reportState like '%2%' then '2' else '1' end reportState
        from enterprise_info e
        left join(
        select enterprise_code, GROUP_CONCAT(report_status) reportState
        from t_report where year= #{query.year} and del_flag='1' GROUP BY enterprise_code
        )tt on e.enterprise_code = tt.enterprise_code
        )r on r.enterprise_code = info.enterprise_code
        left join(
        select tt.monitor_id, tt.year, tt.epc, tt.type, tt.task_state,
        case when type='2' then n1+n2+n3+n4+n5 else n6 end jnjcState
        from (
        select monitor_id, substring(monitor_id,6,4) as year, epc, type, task_state,
        case when insite_supervision is not null or (select count(*) from t_supervision_inspection where monitor_id =
        t.monitor_id)>0 then 0 else 1 end n1,
        case when interview_transcripts is not null or (select count(*) from t_supervision_investigation where
        monitor_id = t.monitor_id)>0 then 0 else 1 end n2,
        case when note is not null or (select count(*) from t_supervision_note where monitor_id = t.monitor_id)>0 then 0
        else 1 end n3,
        case when notification is not null or (select count(*) from t_supervision_notification where monitor_id =
        t.monitor_id)>0 then 0 else 1 end n4,
        case when (select count(*) from t_supervision_comprehensive_energy where monitor_id = t.monitor_id)>0 then 0
        else 1 end n5,
        case when task_state = '3' then 0 else 1 end n6
        from t_supervise_task t
        where source="年度计划" and monitor_id like concat('%',#{query.year},'%')
        )tt
        )a on a.epc = info.enterprise_code
        left join(
        select r.enterprise_code, r.year, ROUND(f.energy_consumption*10000,3) as tce from t_report r
        left join t_report_financial f on f.report_id = r.id
        where r.del_flag='1' and r.type ='1' and report_status ='3' and r.year= #{query.year}
        )b on b.enterprise_code = info.enterprise_code
        left join(
        select monitor_id, substring(monitor_id,6,4) as year, epc, source, ROUND(tce,3) as tce from t_supervise_task
        where type = '2' and source="年度计划" and monitor_id like concat('%',#{query.year},'%')
        )c on c.epc = info.enterprise_code
        left join energy_level d on d.id = info.energy_consume_level
        left join(
        select r.enterprise_code, GROUP_CONCAT(p.product_name) as productName
        from t_report r left join t_report_product p on p.report_id = r.id where r.year=#{query.year} and r.del_flag='1'
        and r.type ='1' GROUP BY r.enterprise_code
        )e on e.enterprise_code = info.enterprise_code
        left join(
        select tt.enterprise_code, case when tt.guideline_val is null then '' else
        GROUP_CONCAT(concat(tt.quota_name,':',tt.guideline_val,tt.quota_unit)) end reportUnitTce,
        case when tt.result = '' and tt.guideline_val is null then '' else
        GROUP_CONCAT(concat(tt.quota_name,':',tt.result)) end reportResult
        from(
        select r.enterprise_code, u.quota_name, u.quota_unit, u.guideline_val, u.quota_limit,
        case when u.guideline_val is null then '' when u.quota_limit is null then '无限额值'
        when CONVERT(u.guideline_val,DECIMAL(20,4)) > CONVERT(u.quota_limit,DECIMAL(20,4)) then '超限' else '未超限' end
        result
        from t_report r left join t_report_unitenergy u on u.report_id = r.id
        where r.year=#{query.year} and r.del_flag='1' and r.type ='3'
        )tt where tt.result like '超限'
        GROUP BY tt.enterprise_code
        )f on f.enterprise_code = info.enterprise_code
        left join(
        select tt.epc, tt.monitor_id, case when tt.calculation_results is null then '' else
        GROUP_CONCAT(concat(tt.name,':',tt.calculation_results,tt.unit)) end jnjcUnitTce,
        case when tt.result = '' and tt.calculation_results is null then '' else
        GROUP_CONCAT(concat(tt.name,':',tt.result)) end jnjcResult
        from(
        select t.epc, t.monitor_id, u.calculation_results, u.unit_product_limit_standard, u.unit,
        case when u.product_name like '%1.%' or u.product_name like '%2.%' then substring(u.product_name,5) else
        u.product_name end name,
        case when u.calculation_results is null then '' when u.unit_product_limit_standard is null then '无限额值'
        when CONVERT(u.calculation_results,DECIMAL(20,4)) > CONVERT(u.unit_product_limit_standard,DECIMAL(20,4)) then
        '超限' else '未超限' end result
        from t_supervision_unit_energy u
        left join t_supervise_task t on u.monitor_id = t.monitor_id
        where u.tce='1' and t.monitor_id like concat('%',#{query.year},'%')
        )tt where tt.result like '超限'
        GROUP BY tt.monitor_id
        )g on g.monitor_id = c.monitor_id and g.epc = info.enterprise_code


        <trim prefix="where" prefixOverrides="and">
            <if test="query.item != null and query.item != ''">
                and info.energy_consume_level = #{query.item}
            </if>
            <if test="query.type != null and query.type != ''">
                and info.company_type like concat("%",#{query.type},"%")
            </if>
            <if test="query.name != null and query.name != ''">
                and info.cmp_name like concat("%",#{query.name},"%")
            </if>
            <if test="query.industry != null and query.industry != ''">
                and info.industry_code like concat(#{query.industry},"%")
            </if>
            <if test="query.product != null and query.product != ''">
                and e.productName like concat("%",#{query.product},"%")
            </if>
        </trim>


        )tt
        left join dict_region d on d.id = tt.infoArea
        where d.pid = '530000'


        <if test="query.source != null and query.source != '' and query.state == '1'.toString()">
            <![CDATA[

             and CONVERT(tt.reportTce,DECIMAL(20,4)) < CONVERT(#{query.source},DECIMAL(20,4)) or CONVERT(tt.jnjcTce,DECIMAL(20,4)) < CONVERT(#{query.source},DECIMAL(20,4))

            ]]>
        </if>
        <if test="query.source != null and query.source != '' and query.state == '2'.toString()">
            <![CDATA[

             and CONVERT(tt.reportTce,DECIMAL(20,4)) <= CONVERT(#{query.source},DECIMAL(20,4)) or CONVERT(tt.jnjcTce,DECIMAL(20,4)) <= CONVERT(#{query.source},DECIMAL(20,4))

            ]]>
        </if>
        <if test="query.source != null and query.source != '' and query.state == '3'.toString()">
            <![CDATA[

             and CONVERT(tt.reportTce,DECIMAL(20,4)) > CONVERT(#{query.source},DECIMAL(20,4)) or CONVERT(tt.jnjcTce,DECIMAL(20,4)) > CONVERT(#{query.source},DECIMAL(20,4))

            ]]>
        </if>
        <if test="query.source != null and query.source != '' and query.state == '4'.toString()">
            <![CDATA[

             and CONVERT(tt.reportTce,DECIMAL(20,4)) >= CONVERT(#{query.source},DECIMAL(20,4)) or CONVERT(tt.jnjcTce,DECIMAL(20,4)) >= CONVERT(#{query.source},DECIMAL(20,4))

            ]]>
        </if>


        GROUP BY tt.infoArea
    </select>

    <select id="energyConsumptionByAreas" resultType="java.util.Map">
        select #{year} year, IFNULL(t.id,'') regionCode, IFNULL(t.meaning2,'') regionName,
        IFNULL(sum(CONVERT(t.tce,DECIMAL(20,3))),0) tce, '万tce' unit
        from(
        select DISTINCT d.id, d.meaning2, info.enterprise_code, f.tce
        from
        <if test="state == '1'.toString()">
            ( SELECT id, meaning2 FROM dict_region where pid like concat(#{area},"%") )d
            left join enterprise_info info on concat(left(info.region_code,4),"00") = d.id
        </if>
        <if test="state == '2'.toString()">
            ( SELECT id, meaning2 FROM dict_region where pid like concat(#{area},"%") )d
            left join enterprise_info info on info.region_code = d.id
        </if>
        <if test="state == '3'.toString()">
            ( SELECT id, meaning2 FROM dict_region where id like #{area} )d
            left join enterprise_info info on info.region_code = d.id
        </if>
        left join(
        select r.enterprise_code, r.year, f.energy_consumption as tce from t_report r
        left join t_report_financial f on f.report_id = r.id
        where r.del_flag='1' and r.type ='1' and r.report_status ='3' and r.year= #{year}
        )f on f.enterprise_code = info.enterprise_code
        -- where info.enterprise_code != '217243660' and info.enterprise_code != '9153030067658902XG' and info.enterprise_code != '915303812172954018'
        )t GROUP BY t.id
    </select>


    <select id="totalByReport" resultType="java.util.Map">
        select #{year} year, count(e.enterprise_code) epcNum, count(t.enterprise_code) reportNum, count(e.enterprise_code)-count(t.enterprise_code) noReportNum
        from enterprise_info e
        left join(
            select year, enterprise_code, processing_link, trade,
            case when processing_link like '1' then 1 else 0 end fubiao2,
            case when trade like 'C301' or trade like 'C302' then 1 else 0 end fubiao3
            from t_report_enterprise where del_flag = '1' and year = #{year}
        )re on re.enterprise_code = e.enterprise_code
        left join (
            select enterprise_code, year, sum(case when report_status = '3' then 1 else 0 end) sbzt
            from t_report where year = #{year} and del_flag = '1'
            group by enterprise_code
        ) t on t.enterprise_code = re.enterprise_code and t.sbzt >= 5+re.fubiao2+re.fubiao3
        where e.del_flag = "1"  and e.region_code like concat(#{area},'%')
    </select>

    <select id="energyConsumptionByEnergy" resultType="com.nantian.cloud.jljc.entity.TReportEnergysTructure">
        select tt.report_id, tt.energy_code, tt.energy_code_name, tt.unit, tt.consume_industry, tt.modulus,
        tt.ref_modulus
        from(
        select r.id rid, r.enterprise_code epc1, r.year, e.enterprise_code epc2, e.region_code, t.id tid, t.report_id,
        t.energy_code, t.energy_code_name, t.unit, t.start_inventory, t.object, t.money, t.consume_sum,
        t.consume_industry, t.consume_material, t.consume_noindustry, t.consume_transport, t.end_inventory,
        t.production_sum, t.modulus, t.ref_modulus
        from enterprise_info e
        left join t_report r on r.enterprise_code = e.enterprise_code and r.type = '2' and r.del_flag = "1" and
        r.report_status='3' and r.year = #{query.year}
        left join t_report_energys_tructure t on t.report_id = r.id and t.del_flag = "1" and t.energy_code =
        #{query.code}
        where e.region_code like concat(#{query.pArea},"%") and t.report_id is not null
        -- and e.enterprise_code != '217243660' and e.enterprise_code != '9153030067658902XG' and e.enterprise_code!='915303812172954018'
        ) tt
    </select>

    <select id="comAnalysisList" resultType="java.util.Map">
        select IFNULL(e.enterprise_code,'') enterpriseCode, IFNULL(e.cmp_name,'') cmpName, IFNULL(d.meaning, '') area,
        IFNULL(k.name,'') kindName, IFNULL(re.icsem,'') icsem,
        IFNULL(re.icsem_tel,'') icsemTel,IFNULL(ROUND(f.energy_consumption,2),'') energyConsumption, '万tce'
        energyConsumptionUnit, IFNULL(ROUND(f.industrial_yield,2),'') industrialYield,
        '万元' industrialYieldUnit, IFNULL(Round(f.unit_energy_consumption ,2),'') unitEnergyConsumption, 'tce/万元'
        unitEnergyConsumptionUnit, p.productName
        from enterprise_info e
        left join dict_region d on d.id=e.region_code
        left join trade_kind k on k.code=e.industry_code
        left join t_report_enterprise re on re.enterprise_code=e.enterprise_code and re.del_flag='1' and re.year=
        #{query.year}
        left join (
        select r.enterprise_code, r.year, GROUP_CONCAT(p.product_num) productNum, GROUP_CONCAT(p.product_name)
        productName
        from t_report r left join t_report_product p on r.id = p.report_id
        where r.type = '1' and r.del_flag = '1' and r.year = #{query.year}
        group by r.enterprise_code
        ) p on p.enterprise_code = e.enterprise_code
        left join (
        select year, enterprise_code, GROUP_CONCAT(report_status) state
        from t_report where year = #{query.year} group by enterprise_code
        ) r on r.enterprise_code = e.enterprise_code
        left join (
        select t.enterprise_code, t.year, n.energy_consumption, n.industrial_yield, n.unit_energy_consumption
        from t_report t left join t_report_financial n on t.id = n.report_id
        where t.del_flag = "1" and t.type = "1" and t.year = #{query.year}
        ) f on f.enterprise_code = e.enterprise_code
        where e.del_flag='1' and r.state like '%3%'
        <if test="query.item != null and query.item != ''">
            and p.productName like CONCAT('%',#{query.item},'%')
        </if>
        <if test="query.name != null and query.name != ''">
            and e.cmp_name like CONCAT('%',#{query.name},'%')
        </if>
        <if test="query.area != null and query.area != ''">
            and e.region_code like concat(#{query.area},"%")
        </if>
        <if test="query.industry != null and query.industry != ''">
            and e.industry_code like concat("%",#{query.industry},"%")
        </if>
        order by f.unit_energy_consumption desc
    </select>

    <select id="energyBenchmarkingPage" resultType="java.util.Map">
        select IFNULL(e.enterprise_code,'') epc, IFNULL(e.enterprise_name,'') cmpName, IFNULL(info.index_name,'')
        indexName, IFNULL(Round(u.guideline_val,3),'') value, IFNULL(u.quota_unit,'') unit
        , IFNULL(d.meaning,'') area, IFNULL(k.name,'') trade, e.dept_id
        from t_report r
        left join t_report_unitenergy u on u.report_id = r.id
        left join t_report_enterprise e on e.enterprise_code = r.enterprise_code
        left join energy_index info on u.quota_id = info.id
        left join trade_kind k on k.code = e.trade
        left join dict_region d on d.id = e.area
        where r.del_flag = "1" and r.type = "3" and u.guideline_val is not null and u.guideline_val != '' and u.guideline_val != 0
        and r.year = #{year} and e.year = #{year} and u.quota_id = #{energyId}
        order by u.guideline_val asc
    </select>


    <select id="energyBenchmarkingGov" resultType="java.util.Map">
        select sum(tt.lq_gov_good) lqGovGood, sum(tt.g_gov_good) gGovGood, sum(tt.lq_gov_limit) lqGovLimit,
        sum(tt.g_gov_limit) gGovLimit
        from(
        select IFNULL(e.enterprise_code,'') epc, IFNULL(e.enterprise_name,'') cmpName, IFNULL(info.index_name,'')
        indexName, IFNULL(Round(u.guideline_val,3),'') value, IFNULL(u.quota_unit,'') unit,
        info.gov_good govGood, info.gov_limit govLimit, info.local_good localGood, info.local_limit localLimit,
        case when info.gov_good = '-' then 0 when CONVERT(info.gov_good,DECIMAL(10,3)) >=
        CONVERT(u.guideline_val,DECIMAL(10,3)) then 1 else 0 end lq_gov_good,
        case when info.gov_limit = '-' and info.gov_good != '-' and CONVERT(u.guideline_val,DECIMAL(10,3)) >
        CONVERT(info.gov_good,DECIMAL(10,3)) then 1 else 0 end g_gov_good,
        case when info.gov_limit = '-' then 0 when info.gov_good != '-' and CONVERT(info.gov_limit,DECIMAL(10,3)) >=
        CONVERT(u.guideline_val,DECIMAL(10,3)) and CONVERT(u.guideline_val,DECIMAL(10,3)) >
        CONVERT(info.gov_good,DECIMAL(10,3)) then 1
        when info.gov_good = '-' and CONVERT(info.gov_limit,DECIMAL(10,3)) >= CONVERT(u.guideline_val,DECIMAL(10,3))
        then 1 else 0 end lq_gov_limit,
        case when info.gov_limit = '-' then 0 when CONVERT(u.guideline_val,DECIMAL(10,3)) >
        CONVERT(info.gov_limit,DECIMAL(10,3)) then 1 else 0 end g_gov_limit
        from t_report r
        left join t_report_unitenergy u on u.report_id = r.id
        left join t_report_enterprise e on e.enterprise_code = r.enterprise_code
        left join (
        select id, index_name, case when gov_good = '—' or gov_good = '-' or gov_good = '' or gov_good is null then '-'
        else gov_good end gov_good,
        case when gov_limit = '—' or gov_limit = '-' or gov_limit = '' or gov_limit is null then '-' else gov_limit end
        gov_limit,
        case when gov_accept = '—' or gov_accept = '-' or gov_accept = '' or gov_accept is null then '-' else gov_accept
        end gov_accept,
        case when local_good = '—' or local_good = '-' or local_good = '' or local_good is null then '-' else local_good
        end local_good,
        case when local_limit = '—' or local_limit = '-' or local_limit = '' or local_limit is null then '-' else
        local_limit end local_limit,
        case when local_accept = '—' or local_accept = '-' or local_accept = '' or local_accept is null then '-' else
        local_accept end local_accept
        from energy_index where id = #{energyId}
        ) info on u.quota_id = info.id
        where r.report_status='3' and r.del_flag = "1" and r.type = "3" and u.guideline_val is not null and u.guideline_val != '' and u.guideline_val != 0
        and r.year = #{year} and e.year = #{year} and u.quota_id = #{energyId}
        and e.dept_id like concat(#{area},'%')
        order by u.guideline_val asc
        )tt
    </select>


    <select id="energyBenchmarkingLocal" resultType="java.util.Map">
        select sum(tt.lq_local_good) lqLocalGood, sum(tt.g_local_good) gLocalGood, sum(tt.lq_local_limit) lqLocalLimit,
        sum(tt.g_local_limit) gLocalLimit
        from(
        select IFNULL(e.enterprise_code,'') epc, IFNULL(e.enterprise_name,'') cmpName, IFNULL(info.index_name,'')
        indexName, IFNULL(Round(u.guideline_val,3),'') value, IFNULL(u.quota_unit,'') unit,
        info.gov_good govGood, info.gov_limit govLimit, info.local_good localGood, info.local_limit localLimit,
        case when info.local_good = '-' then 0 when CONVERT(info.local_good,DECIMAL(10,3)) >=
        CONVERT(u.guideline_val,DECIMAL(10,3)) then 1 else 0 end lq_local_good,
        case when info.local_limit = '-' and info.local_good != '-' and CONVERT(u.guideline_val,DECIMAL(10,3)) >
        CONVERT(info.local_good,DECIMAL(10,3)) then 1 else 0 end g_local_good,
        case when info.local_limit = '-' then 0 when info.local_good != '-' and CONVERT(info.local_limit,DECIMAL(10,3))
        >= CONVERT(u.guideline_val,DECIMAL(10,3)) and CONVERT(u.guideline_val,DECIMAL(10,3)) >
        CONVERT(info.local_good,DECIMAL(10,3)) then 1
        when info.local_good = '-' and CONVERT(info.local_limit,DECIMAL(10,3)) >= CONVERT(u.guideline_val,DECIMAL(10,3))
        then 1 else 0 end lq_local_limit,
        case when info.local_limit = '-' then 0 when CONVERT(u.guideline_val,DECIMAL(10,3)) >
        CONVERT(info.local_limit,DECIMAL(10,3)) then 1 else 0 end g_local_limit
        from t_report r
        left join t_report_unitenergy u on u.report_id = r.id
        left join t_report_enterprise e on e.enterprise_code = r.enterprise_code
        left join (
        select id, index_name, case when gov_good = '—' or gov_good = '-' or gov_good = '' or gov_good is null then '-'
        else gov_good end gov_good,
        case when gov_limit = '—' or gov_limit = '-' or gov_limit = '' or gov_limit is null then '-' else gov_limit end
        gov_limit,
        case when gov_accept = '—' or gov_accept = '-' or gov_accept = '' or gov_accept is null then '-' else gov_accept
        end gov_accept,
        case when local_good = '—' or local_good = '-' or local_good = '' or local_good is null then '-' else local_good
        end local_good,
        case when local_limit = '—' or local_limit = '-' or local_limit = '' or local_limit is null then '-' else
        local_limit end local_limit,
        case when local_accept = '—' or local_accept = '-' or local_accept = '' or local_accept is null then '-' else
        local_accept end local_accept
        from energy_index where id = #{energyId}
        ) info on u.quota_id = info.id
        where r.report_status='3' and r.del_flag = "1" and r.type = "3" and u.guideline_val is not null and u.guideline_val != '' and u.guideline_val != 0
        and r.year = #{year} and e.year = #{year} and u.quota_id = #{energyId}
        and e.dept_id like concat(#{area},'%')
        order by u.guideline_val asc
        )tt
    </select>

    <select id="noticeListByReport" resultType="java.util.Map">
        select #{query.year} yaer, tt.epc, tt.cmpName, tt.area, tt.trade, tt.icsem, tt.icsemTel, tt.noAudit,
        tt.proStatus, tt.dept_id
        from(
        select e.enterprise_code epc, re.area dept_id, re.enterprise_name cmpName, d.meaning area, k.name trade,
        re.icsem, re.icsem_tel icsemTel,
        case when rt.status like '%2%' or rt.status like '%4%' then 0 else 1 end noAudit,
        (select count(*) from t_report where year=#{query.year} and del_flag='1' and enterprise_code=e.enterprise_code
        and prov_status='1' and report_status='3') proStatus
        from enterprise_info e
        left join t_report_enterprise re on re.enterprise_code=e.enterprise_code and re.year=#{query.year} and
        re.del_flag='1'
        left join(
        select enterprise_code, GROUP_CONCAT(audit_status) status
        from t_report where year=#{query.year} and del_flag='1' and report_status='3' group by enterprise_code
        )rt on rt.enterprise_code=e.enterprise_code
        left join dict_region d on d.id=e.region_code
        left join trade_kind k on k.code=e.industry_code
        where e.del_flag='1' and re.enterprise_name is not null and re.enterprise_name != ''
        <if test="query.area != null and query.area != ''">
            and re.area like concat(#{query.area},'%')
        </if>
        <if test="query.industry != null and query.industry != ''">
            and e.industry_code like concat(#{query.industry},"%")
        </if>
        )tt where tt.noAudit = '1'
        order by tt.cmpName asc
    </select>

    <select id="exceptionInfoByReport1" resultType="java.util.Map">
        select a.year, a.epc, a.epc_name, a.energy_region, a.total_energy, a.trade_num2, a.area2, e.region_code dept_id
        from epc_energy_accuracy_analysis a
        left join enterprise_info e on e.enterprise_code = a.epc
        where a.del_flag ="1" and a.check_result = '1' and a.year = #{query.year}
        <if test="query.area != null and query.area != ''">
            and e.region_code like concat(#{query.area},"%")
        </if>
        <if test="query.industry != null and query.industry != ''">
            and e.industry_code like concat(#{query.industry},"%")
        </if>
        order by a.total_energy desc
    </select>

    <select id="exceptionInfoByReport2" resultType="java.util.Map">
        select e.region_code dept_id, e.enterprise_code epc, e.cmp_name cmpName, d.meaning area, k.name trade,
        f.energy_code_name, f.modulus, f.rid
        from enterprise_info e
        left join dict_region d on d.id = e.region_code
        left join trade_kind k on k.code = e.industry_code
        left join(
        select r.enterprise_code, r.year, r.id rid, t.energy_code_name, t.modulus
        from t_report r
        left join t_report_energys_tructure t on t.report_id = r.id
        where r.report_status='3' and r.del_flag='1' and r.type ='2' and r.report_status ='3' and
        CONVERT(SUBSTRING_INDEX(t.modulus,'-',-1),DECIMAL(10,4)) > 20 and r.year = #{query.year}
        ) f on f.enterprise_code = e.enterprise_code
        where f.modulus != '' and f.modulus is not null
        <if test="query.area != null and query.area != ''">
            and e.region_code like concat(#{query.area},"%")
        </if>
        <if test="query.industry != null and query.industry != ''">
            and e.industry_code like concat(#{query.industry},"%")
        </if>
        order by e.cmp_name asc
    </select>

    <select id="reportSummaryAndAudit" resultType="java.util.Map">
        select count(tt.enterprise_code) epcSum, sum(tt.repoprtState) reportSum,
        count(tt.enterprise_code)-sum(tt.repoprtState) noReport,
        sum(tt.provState) prov, sum(tt.noAudit) noAudit, sum(tt.provNoAudit) provNoAudit
        from(
        select e.enterprise_code, case when r.reportState like '%3%' then 1 else 0 end repoprtState,
        case when r.reportState like '%3%' and r.provState like '%1%' then 1 else 0 end provState,
        case when r.reportState like '%3%' and r.auditState = '9' then 1 else 0 end noAudit,
        case when r.reportState like '%3%' and r.provState like '%1%' and r.auditState = '9' then 1 else 0 end
        provNoAudit
        from enterprise_info e
        left join(
        select enterprise_code, GROUP_CONCAT(report_status) reportState, GROUP_CONCAT(prov_status) provState,
        IFNULL(GROUP_CONCAT(audit_status),9) auditState
        from t_report where year = #{query.year} and del_flag = '1' and report_status='3' GROUP BY enterprise_code
        )r on e.enterprise_code = r.enterprise_code
        where e.del_flag = '1'
        <if test="query.area != null and query.area != ''">
            and e.region_code like concat(#{query.area},"%")
        </if>
        <if test="query.industry != null and query.industry != ''">
            and e.industry_code like concat(#{query.industry},"%")
        </if>
        )tt
    </select>


    <!--##################################能报自定义查询##################################-->
    <!-- 1.企业列表查询结果 -->
    <sql id="Cmp_Column">
        id,
        enterprise_code,
        isorg,
        org_type,
        org_code,
        password,
        cmp_name,
        address,
        zip_code,
        corporation_name,
        region_code,
        license,
        organization,
        register_address,
        register_date,
        register_principal,
        type_code,
        industry_code,
        field_code,
        energy_consume_level,
        center,
        jgzh,
        latitude,
        longitude,
        phone,
        fax,
        email,
        url,
        production_line,
        leading_product,
        group_name,
        group_address,
        apply_date,
        approval_date,
        last_login_date,
        state,
        remark,
        company_type,
        userid,
        update_time,
        del_flag,
        dept_id
    </sql>
    <!-- 2.企业信息表 -->
    <sql id="cmp_table">
        select a.enterprise_code, a.cmp_name, concat(left(a.region_code,4),"00") area, a.industry_code, a.company_type,
        b.kind_name, c.staff, c.staff_tel
        from (
        select
        <include refid="Cmp_Column">
        </include>
        from enterprise_info
        <if test="list != null and list != ''">
            where
            <foreach collection="list" index="index" item="i" separator="or">
                company_type like concat("%",#{i},"%")
            </foreach>
        </if>
        ) a left join trade_info b on a.industry_code = b.trade_num
        left join t_report_enterprise c on a.enterprise_code = c.enterprise_code and year = #{query.year}
        where a.del_flag = "1"
        <if test="query.name != null and query.name != ''">
            and a.cmp_name like concat("%",#{query.name},"%")
        </if>
        <if test="query.area != null and query.area != ''">
            and #{query.area} like concat("%",left(a.region_code,4),"%")
        </if>
        <if test="query.industry != null and query.industry != ''">
            and #{query.industry} like concat("%",b.kind_name,"%")
        </if>
        <if test="query.code != null and query.code != ''">
            and a.region_code like concat("%",#{query.code},"%")
        </if>
    </sql>
    <!-- 3.变化率范围low1,high1、数值范围low2,high2 -->
    <sql id="change_rate">
        <trim prefix="where" prefixOverrides="and">
            <if test="query.low1 != null and query.low1 != ''">
                <![CDATA[ and CONVERT(tt.rate,DECIMAL(20,4)) >= CONVERT(#{query.low1},DECIMAL(20,4)) ]]>
            </if>
            <if test="query.high1 != null and query.high1 != ''">
                <![CDATA[ and CONVERT(tt.rate,DECIMAL(20,4)) <= CONVERT(#{query.high1},DECIMAL(20,4)) ]]>
            </if>
            <if test="query.low2 != null and query.low2 != ''">
                <![CDATA[ and CONVERT(tt.benqi,DECIMAL(20,4)) >= CONVERT(#{query.low2},DECIMAL(20,4)) ]]>
            </if>
            <if test="query.high2 != null and query.high2 != ''">
                <![CDATA[ and CONVERT(tt.benqi,DECIMAL(20,4)) <= CONVERT(#{query.high2},DECIMAL(20,4)) ]]>
            </if>
        </trim>
    </sql>

    <!--61 工业总产值综合能耗 (单位产值综合能耗)-->
    <sql id="queryCustomRank61_cmp">
        select distinct e.area, e.kind_name, IFNULL(e.enterprise_code,'') epc, IFNULL(e.cmp_name,'') name,
        IFNULL(e.staff,'') staff, IFNULL(e.staff_tel,'') staffTel, f2.industrial_yield y, f2.energy_consumption e,
        case when f2.industrial_yield != '' and f2.industrial_yield is not null then f1.industrial_yield else null end
        prevy,
        case when f2.industrial_yield != '' and f2.industrial_yield is not null then f1.energy_consumption else null end
        preve
        from (
        <include refid="cmp_table">
        </include>
        ) e
        left join (select id, enterprise_code, year from t_report where del_flag = "1" and type="1" and
        report_status='3' and year = #{query.prevYear}) r1 on e.enterprise_code = r1.enterprise_code
        left join (select id, enterprise_code, year from t_report where del_flag = "1" and type="1" and
        report_status='3' and year = #{query.year}) r2 on e.enterprise_code = r2.enterprise_code
        left join t_report_financial f1 on f1.report_id = r1.id
        left join t_report_financial f2 on f2.report_id = r2.id
        where f2.industrial_yield is not null and f2.industrial_yield != ''
    </sql>
    <select id="queryCustomRank61_1" resultType="java.util.Map">
        select IFNULL(tt.area,'') area, IFNULL(tt.benqi,'') benqi, IFNULL(tt.tongqi,'') tongqi, IFNULL(tt.rate,'') rate,
        IFNULL(tt.epcNum,0) epcNum
        from(
        select t.meaning2 area, concat(ROUND(sum(t.e*10000)/sum(t.y),3),'') benqi,
        concat(ROUND(sum(t.preve*10000)/sum(t.prevy),3),'') tongqi,
        ROUND( ( ROUND(sum(t.e*10000)/sum(t.y),3)/ROUND(sum(t.preve*10000)/sum(t.prevy),3) -1 ) *100,2) rate, count(*)
        epcNum
        from(
        select d.id, d.meaning2, e.epc, e.y, e.e, e.prevy, e.preve
        from (select id, meaning2 from dict_region where pid = "530000" and del_flag = "1") d
        left join (
        <include refid="queryCustomRank61_cmp">
        </include>
        ) e on e.area = d.id
        )t group by t.id
        )tt
        <include refid="change_rate">
        </include>
    </select>
    <select id="queryCustomRank61_2" resultType="java.util.Map">
        select IFNULL(tt.trade,'') trade, IFNULL(tt.benqi,'') benqi, IFNULL(tt.tongqi,'') tongqi, IFNULL(tt.rate,'')
        rate, IFNULL(tt.epcNum,0) epcNum
        from(
        select t.name trade, concat(ROUND(sum(t.e*10000)/sum(t.y),3),'') benqi,
        concat(ROUND(sum(t.preve*10000)/sum(t.prevy),3),'') tongqi,
        ROUND( ( ROUND(sum(t.e*10000)/sum(t.y),3)/ROUND(sum(t.preve*10000)/sum(t.prevy),3) -1 ) *100,2) rate, count(*)
        epcNum
        from(
        select d.id, d.name, e.kind_name, e.epc, e.y, e.e, e.prevy, e.preve
        from (select id, name from trade_category where del_flag = "1") d
        left join (
        <include refid="queryCustomRank61_cmp">
        </include>
        ) e on e.kind_name = d.name
        )t group by t.id
        )tt
        <include refid="change_rate">
        </include>
    </select>
    <select id="queryCustomRank61_3" resultType="java.util.Map">
        select tt.area, tt.kind_name, tt.epc, tt.name, tt.staff, tt.staffTel, tt.benqi, tt.tongqi, tt.rate
        from(
        select t.area, t.kind_name, t.epc, t.name, t.staff, t.staffTel,
        IFNULL(ROUND(t.e*10000/t.y,3),'') benqi, IFNULL(ROUND(t.preve*10000/t.prevy,3),'') tongqi, IFNULL(ROUND( (
        ROUND(t.e*10000/t.y,3)/ROUND(t.preve*10000/t.prevy,3) -1 ) *100,2),'') rate
        from(
        <include refid="queryCustomRank61_cmp">
        </include>
        )t
        )tt
        <include refid="change_rate">
        </include>
    </select>
    <select id="queryCustomRank61_4" resultType="java.util.Map">
        select '平均值' area, '平均值' trade, IFNULL(ROUND(sum(t.e*10000)/sum(t.y),3),'') benqi,
        IFNULL(ROUND(sum(t.preve*10000)/sum(t.prevy),3),'') tongqi,
        IFNULL(ROUND( ( ROUND(sum(t.e*10000)/sum(t.y),3)/ROUND(sum(t.preve*10000)/sum(t.prevy),3) -1 ) *100,2),'') rate,
        count(*) epcNum
        from(
        <include refid="queryCustomRank61_cmp">
        </include>
        )t
    </select>

    <!--企业经营指标 31能源消费成本,33企业销售收入,34工业总产值,36上缴利锐,37从业人员,38能源管理师-->
    <sql id="queryCustomRank31_cmp">
        select distinct e.area, e.kind_name, IFNULL(e.enterprise_code,'') epc, IFNULL(e.cmp_name,'') name,
        IFNULL(e.staff,'') staff, IFNULL(e.staff_tel,'') staffTel
        <if test="query.id == '31'.toString()">
            , f2.energy_consumption_cost num, case when f2.energy_consumption_cost != '' and f2.energy_consumption_cost
            is not null then f1.energy_consumption_cost else null end prevNum
        </if>
        <if test="query.id == '33'.toString()">
            , f2.sales_revenue num, case when f2.sales_revenue != '' and f2.sales_revenue is not null then
            f1.sales_revenue else null end prevNum
        </if>
        <if test="query.id == '34'.toString()">
            , f2.industrial_yield num, case when f2.industrial_yield != '' and f2.industrial_yield is not null then
            f1.industrial_yield else null end prevNum
        </if>
        <if test="query.id == '36'.toString()">
            , f2.taxes_payment num, case when f2.taxes_payment != '' and f2.taxes_payment is not null then
            f1.taxes_payment else null end prevNum
        </if>
        <if test="query.id == '37'.toString()">
            , f2.employees_num num, case when f2.employees_num != '' and f2.employees_num is not null then
            f1.employees_num else null end prevNum
        </if>
        <if test="query.id == '38'.toString()">
            , f2.icsem_num num, case when f2.icsem_num != '' and f2.icsem_num is not null then f1.icsem_num else null
            end prevNum
        </if>
        from (
        <include refid="cmp_table">
        </include>
        ) e
        left join (select id, enterprise_code, year from t_report where del_flag = "1" and type="1" and
        report_status='3' and year = #{query.prevYear}) r1 on e.enterprise_code = r1.enterprise_code
        left join (select id, enterprise_code, year from t_report where del_flag = "1" and type="1" and
        report_status='3' and year = #{query.year}) r2 on e.enterprise_code = r2.enterprise_code
        left join t_report_financial f1 on f1.report_id = r1.id
        left join t_report_financial f2 on f2.report_id = r2.id
        <if test="query.id == '31'.toString()">
            where f2.energy_consumption_cost is not null and f2.energy_consumption_cost != ''
        </if>
        <if test="query.id == '33'.toString()">
            where f2.sales_revenue is not null and f2.sales_revenue != ''
        </if>
        <if test="query.id == '34'.toString()">
            where f2.industrial_yield is not null and f2.industrial_yield != ''
        </if>
        <if test="query.id == '36'.toString()">
            where f2.taxes_payment is not null and f2.taxes_payment != ''
        </if>
        <if test="query.id == '37'.toString()">
            where f2.employees_num is not null and f2.employees_num != ''
        </if>
        <if test="query.id == '38'.toString()">
            where f2.icsem_num is not null and f2.icsem_num != ''
        </if>
    </sql>
    <select id="queryCustomRank31_1" resultType="java.util.Map">
        select IFNULL(tt.area,'') area, IFNULL(tt.benqi,'') benqi, IFNULL(tt.tongqi,'') tongqi, IFNULL(tt.rate,'') rate,
        IFNULL(tt.epcNum,0) epcNum
        from(
        select t.meaning2 area,
        <choose>
            <when test="query.id == '37'.toString() or query.id == '38'.toString()">
                concat(ROUND(sum(t.num),0),'') benqi, concat(ROUND(sum(t.prevNum),0),'') tongqi,
            </when>
            <otherwise>
                concat(ROUND(sum(t.num),3),'') benqi, concat(ROUND(sum(t.prevNum),3),'') tongqi,
            </otherwise>
        </choose>
        ROUND((sum(t.num)/sum(t.prevNum)-1 )*100,2) rate, count(*) epcNum
        from(
        select d.id, d.meaning2, e.epc, e.num, e.prevNum
        from (select id, meaning2 from dict_region where pid = "530000" and del_flag = "1") d
        left join (
        <include refid="queryCustomRank31_cmp">
        </include>
        ) e on e.area = d.id
        )t group by t.id
        )tt
        <include refid="change_rate">
        </include>
    </select>
    <select id="queryCustomRank31_2" resultType="java.util.Map">
        select IFNULL(tt.trade,'') trade, IFNULL(tt.benqi,'') benqi, IFNULL(tt.tongqi,'') tongqi, IFNULL(tt.rate,'')
        rate, IFNULL(tt.epcNum,0) epcNum
        from(
        select t.name trade,
        <choose>
            <when test="query.id == '37'.toString() or query.id == '38'.toString()">
                concat(ROUND(sum(t.num),0),'') benqi, concat(ROUND(sum(t.prevNum),0),'') tongqi,
            </when>
            <otherwise>
                concat(ROUND(sum(t.num),3),'') benqi, concat(ROUND(sum(t.prevNum),3),'') tongqi,
            </otherwise>
        </choose>
        ROUND((sum(t.num)/sum(t.prevNum)-1 )*100,2) rate, count(*) epcNum
        from(
        select d.id, d.name, e.kind_name, e.epc, e.num, e.prevNum
        from (select id, name from trade_category where del_flag = "1") d
        left join (
        <include refid="queryCustomRank31_cmp">
        </include>
        ) e on e.kind_name = d.name
        )t group by t.id
        )tt
        <include refid="change_rate">
        </include>
    </select>
    <select id="queryCustomRank31_3" resultType="java.util.Map">
        select tt.area, tt.kind_name, tt.epc, tt.name, tt.staff, tt.staffTel, tt.benqi, tt.tongqi, tt.rate
        from(
        select t.area, t.kind_name, t.epc, t.name, t.staff, t.staffTel,
        <choose>
            <when test="query.id == '37'.toString() or query.id == '38'.toString()">
                IFNULL(ROUND(t.num,0),'') benqi, IFNULL(ROUND(t.prevNum,0),'') tongqi,
            </when>
            <otherwise>
                IFNULL(ROUND(t.num,3),'') benqi, IFNULL(ROUND(t.prevNum,3),'') tongqi,
            </otherwise>
        </choose>
        IFNULL(ROUND((t.num/t.prevNum-1 )*100,2),'') rate
        from(
        <include refid="queryCustomRank31_cmp">
        </include>
        )t
        )tt
        <include refid="change_rate">
        </include>
    </select>
    <select id="queryCustomRank31_4" resultType="java.util.Map">
        select '合计' area, '合计' trade,
        <choose>
            <when test="query.id == '37'.toString() or query.id == '38'.toString()">
                IFNULL(concat(ROUND(sum(t.num),0),''),'') benqi, IFNULL(concat(ROUND(sum(t.prevNum),0),''),'') tongqi,
            </when>
            <otherwise>
                IFNULL(concat(ROUND(sum(t.num),3),''),'') benqi, IFNULL(concat(ROUND(sum(t.prevNum),3),''),'') tongqi,
            </otherwise>
        </choose>
        IFNULL(ROUND( (sum(t.num)/sum(t.prevNum) -1 ) *100,2) ,'') rate, count(*) epcNum
        from(
        <include refid="queryCustomRank31_cmp">
        </include>
        )t
    </select>

    <!--企业经营指标  32企业生产成本,35产品产量-->
    <!-- 生产总成本 benqi1,tongqi1,rate1   能源成本 benqi2,tongqi2,rate2   能源成本占生产成本比例 benqi3,tongqi3,rate3-->
    <sql id="queryCustomRank32_cmp">
        select distinct e.area, e.kind_name, IFNULL(e.enterprise_code,'') epc, IFNULL(e.cmp_name,'') name,
        IFNULL(e.staff,'') staff, IFNULL(e.staff_tel,'') staffTel,
        f2.production_cost num1, case when f2.production_cost != '' and f2.production_cost is not null then
        f1.production_cost else null end prevNum1,
        f2.energy_consumption_cost num2, case when f2.energy_consumption_cost != '' and f2.energy_consumption_cost is
        not null then f1.energy_consumption_cost else null end prevNum2
        from (
        <include refid="cmp_table">
        </include>
        ) e
        left join (select id, enterprise_code, year from t_report where del_flag = "1" and type="1" and
        report_status='3' and year = #{query.prevYear}) r1 on e.enterprise_code = r1.enterprise_code
        left join (select id, enterprise_code, year from t_report where del_flag = "1" and type="1" and
        report_status='3' and year = #{query.year}) r2 on e.enterprise_code = r2.enterprise_code
        left join t_report_financial f1 on f1.report_id = r1.id
        left join t_report_financial f2 on f2.report_id = r2.id
        where f2.production_cost is not null and f2.production_cost != ''
    </sql>
    <!-- 产品产能 benqi1,tongqi1,rate1   产品产量 benqi2,tongqi2,rate2   产量占产能比例 benqi3,tongqi3,rate3-->
    <sql id="queryCustomRank35_cmp">
        select distinct e.area, e.kind_name, IFNULL(e.enterprise_code,'') epc, IFNULL(e.cmp_name,'') name,
        IFNULL(e.staff,'') staff, IFNULL(e.staff_tel,'') staffTel,
        f2.capacity_num num1, case when f2.capacity_num != '' and f2.capacity_num is not null then f1.capacity_num else
        null end prevNum1,
        f2.yield_num num2, case when f2.yield_num != '' and f2.yield_num is not null then f1.yield_num else null end
        prevNum2
        from (
        <include refid="cmp_table">
        </include>
        ) e
        left join (select id, enterprise_code, year from t_report where del_flag = "1" and type="1" and
        report_status='3' and year = #{query.prevYear}) r1 on e.enterprise_code = r1.enterprise_code
        left join (select id, enterprise_code, year from t_report where del_flag = "1" and type="1" and
        report_status='3' and year = #{query.year}) r2 on e.enterprise_code = r2.enterprise_code
        left join t_report_product f1 on f1.report_id = r1.id and f1.product_num = #{query.source}
        left join t_report_product f2 on f2.report_id = r2.id and f1.product_num = #{query.source}
        where f2.capacity_num is not null and f2.capacity_num != ''
    </sql>
    <!-- 加工转换投入合计 benqi1,tongqi1,rate1   能源加工转换产出折标量（吨标准煤） benqi2,tongqi2,rate2   能源加工转换效率 benqi3,tongqi3,rate3-->
    <sql id="queryCustomRank48_cmp">
        SELECT DISTINCT
        e.area,
        e.kind_name,
        IFNULL( e.enterprise_code, '' ) epc,
        IFNULL( e.cmp_name, '' ) name,
        IFNULL( e.staff, '' ) staff,
        IFNULL( e.staff_tel, '' ) staffTel,
        f2.pro_con_total pct,
        f2.conversion_output_ref cor,
        CASE
        WHEN f2.pro_con_total != ''
        AND f2.pro_con_total IS NOT NULL THEN
        f1.pro_con_total ELSE NULL
        END prevPct,
        CASE
        WHEN f2.pro_con_total != ''
        AND f2.pro_con_total IS NOT NULL THEN
        f1.conversion_output_ref ELSE NULL
        END prevCor
        from (
        <include refid="cmp_table">
        </include>
        ) e
        left join (select id, enterprise_code, year from t_report where del_flag = "1" and type="2-1" and
        report_status='3' and year = #{query.prevYear}) r1 on e.enterprise_code = r1.enterprise_code
        left join (select id, enterprise_code, year from t_report where del_flag = "1" and type="2-1" and
        report_status='3' and year = #{query.year}) r2 on e.enterprise_code = r2.enterprise_code
        left join t_report_energy_out f1 on f1.report_id = r1.id
        left join t_report_energy_out f2 on f2.report_id = r2.id
        where f2.pro_con_total is not null and f2.pro_con_total != ''
    </sql>
    <select id="queryCustomRank48_1" resultType="java.util.Map">
        SELECT
        IFNULL( tt.area, '' ) area,
        IFNULL( tt.benqi, '' ) benqi,
        IFNULL( tt.tongqi, '' ) tongqi,
        IFNULL( tt.rate, '' ) rate,
        IFNULL( tt.epcNum, 0 ) epcNum
        FROM
        (
        SELECT
        t.meaning2 area,
        concat( ROUND( (sum( t.cor )/ sum( t.pct ))*100, 2 ), '' ) benqi,
        concat( ROUND( (sum( t.prevCor)/ sum( t.prevPct ))*100, 2 ), '' ) tongqi,
        ROUND(((ROUND( (sum( t.cor )/ sum( t.pct ))*100, 2 )-ROUND( (sum( t.prevCor)/ sum( t.prevPct ))*100, 2 ))/ROUND(
        (sum( t.prevCor)/ sum( t.prevPct ))*100, 2 ))*100,2) rate,
        count(*) epcNum
        FROM
        (
        SELECT
        d.id,
        d.meaning2,
        e.epc,
        e.pct,
        e.cor,
        e.prevPct,
        e.prevCor
        FROM
        ( SELECT id, meaning2 FROM dict_region WHERE pid = "530000" AND del_flag = "1" ) d
        LEFT JOIN (
        <include refid="queryCustomRank48_cmp">
        </include>
        )e on e.area = d.id
        )t group by t.id
        )tt
        <include refid="change_rate">
        </include>
    </select>
    <select id="queryCustomRank48_2" resultType="java.util.Map">
        SELECT
        IFNULL( tt.trade, '' ) trade,
        IFNULL( tt.benqi, '' ) benqi,
        IFNULL( tt.tongqi, '' ) tongqi,
        IFNULL( tt.rate, '' ) rate,
        IFNULL( tt.epcNum, 0 ) epcNum
        FROM
        (
        SELECT
        t.NAME trade,
        concat( ROUND( (sum( t.cor )/ sum( t.pct ))*100, 2 ), '' ) benqi,
        concat( ROUND( (sum( t.prevCor)/ sum( t.prevPct ))*100, 2 ), '' ) tongqi,
        ROUND(((ROUND( (sum( t.cor )/ sum( t.pct ))*100, 2 )-ROUND( (sum( t.prevCor)/ sum( t.prevPct ))*100, 2 ))/ROUND(
        (sum( t.prevCor)/ sum( t.prevPct ))*100, 2 ))*100,2) rate,
        count(*) epcNum
        FROM
        (
        SELECT
        d.id,
        d.NAME,
        e.kind_name,
        e.epc,
        e.pct,
        e.cor,
        e.prevPct,
        e.prevCor
        FROM
        ( SELECT id, NAME FROM trade_category WHERE del_flag = "1" ) d
        LEFT JOIN (
        <include refid="queryCustomRank48_cmp">
        </include>
        )e ON e.kind_name = d.NAME
        ) t
        GROUP BY
        t.id
        ) tt
    </select>
    <select id="queryCustomRank48_3" resultType="java.util.Map">
        SELECT
        tt.area,
        tt.kind_name,
        tt.epc,
        tt.name,
        tt.staff,
        tt.staffTel,
        tt.benqi,
        tt.tongqi,
        tt.rate
        FROM
        (
        SELECT
        t.area,
        t.kind_name,
        t.epc,
        t.name,
        t.staff,
        t.staffTel,
        concat( ROUND( (sum( t.cor )/ sum( t.pct ))*100, 2 ), '' ) benqi,
        concat( ROUND( (sum( t.prevCor)/ sum( t.prevPct ))*100, 2 ), '' ) tongqi,
        ROUND(((ROUND( (sum( t.cor )/ sum( t.pct ))*100, 2 )-ROUND( (sum( t.prevCor)/ sum( t.prevPct ))*100, 2 ))/ROUND(
        (sum( t.prevCor)/ sum( t.prevPct ))*100, 2 ))*100,2) rate
        FROM(
        <include refid="queryCustomRank48_cmp">
        </include>
        )t
        GROUP BY t.name
        )tt
        <include refid="change_rate">
        </include>
    </select>
    <select id="queryCustomRank48_4" resultType="java.util.Map">
        SELECT
        '平均值' trade,
        '平均值' area,
        concat(ROUND(sum(cor)/sum(pct)*100,2),'') benqi,
        concat(ROUND(sum(prevCor)/sum(prevPct)*100,2),'') tongqi,
        concat(ROUND(((ROUND(sum(cor)/sum(pct)*100,2)-ROUND(sum(prevCor)/sum(prevPct)*100,2))/ROUND(sum(prevCor)/sum(prevPct)*100,2))*100,2),'')
        rate
        FROM(
        SELECT
        t.NAME trade,
        sum( t.cor ) cor,
        sum( t.pct ) pct,
        sum( t.prevCor) prevCor,
        sum( t.prevPct ) prevPct,
        count(*) epcNum
        FROM
        (
        SELECT
        d.id,
        d.NAME,
        e.kind_name,
        e.epc,
        e.pct,
        e.cor,
        e.prevPct,
        e.prevCor
        FROM
        ( SELECT id, NAME FROM trade_category WHERE del_flag = "1" ) d
        LEFT JOIN (
        <include refid="queryCustomRank48_cmp">
        </include>)e ON e.kind_name = d.NAME
        ) t
        GROUP BY
        t.id )tt
    </select>
    <select id="queryCustomRank32_1" resultType="java.util.Map">
        select IFNULL(tt.area,'') area, IFNULL(tt.epcNum,0) epcNum, IFNULL(tt.benqi1,'') benqi1, IFNULL(tt.tongqi1,'')
        tongqi1, IFNULL(tt.rate1,'') rate1, IFNULL(tt.benqi2,'') benqi2, IFNULL(tt.tongqi2,'') tongqi2,
        IFNULL(tt.rate2,'') rate2, IFNULL(tt.benqi3,'') benqi3, IFNULL(tt.tongqi3,'') tongqi3, IFNULL(tt.rate3,'') rate3
        from(
        select t.meaning2 area, count(*) epcNum,
        concat(ROUND(sum(t.num1),3),'') benqi1, concat(ROUND(sum(t.prevNum1),3),'') tongqi1,
        ROUND((sum(t.num1)/sum(t.prevNum1)-1)*100,2) rate1,
        concat(ROUND(sum(t.num2),3),'') benqi2, concat(ROUND(sum(t.prevNum2),3),'') tongqi2,
        ROUND((sum(t.num2)/sum(t.prevNum2)-1)*100,2) rate2,
        ROUND(sum(t.num2)*100/sum(t.num1),2) benqi3, ROUND(sum(t.prevNum2)*100/sum(t.prevNum1),2) tongqi3, ROUND((
        (sum(t.num2)/sum(t.num1))/(sum(t.prevNum2)/sum(t.prevNum1)) -1 )*100,2) rate3
        from(
        select d.id, d.meaning2, e.epc, e.num1, e.prevNum1, e.num2, e.prevNum2
        from (select id, meaning2 from dict_region where pid = "530000" and del_flag = "1") d
        left join (
        <if test="query.id == '32'.toString()">
            <include refid="queryCustomRank32_cmp">
            </include>
        </if>
        <if test="query.id == '35'.toString()">
            <include refid="queryCustomRank35_cmp">
            </include>
        </if>
        <if test="query.id == '48'.toString()">
            <include refid="queryCustomRank48_cmp">
            </include>
        </if>
        ) e on e.area = d.id
        )t group by t.id
        )tt
        <include refid="change_rate">
        </include>
    </select>
    <select id="queryCustomRank32_2" resultType="java.util.Map">
        select IFNULL(tt.trade,'') trade, IFNULL(tt.epcNum,0) epcNum, IFNULL(tt.benqi1,'') benqi1, IFNULL(tt.tongqi1,'')
        tongqi1, IFNULL(tt.rate1,'') rate1, IFNULL(tt.benqi2,'') benqi2, IFNULL(tt.tongqi2,'') tongqi2,
        IFNULL(tt.rate2,'') rate2, IFNULL(tt.benqi3,'') benqi3, IFNULL(tt.tongqi3,'') tongqi3, IFNULL(tt.rate3,'') rate3
        from(
        select t.name trade, count(*) epcNum,
        concat(ROUND(sum(t.num1),3),'') benqi1, concat(ROUND(sum(t.prevNum1),3),'') tongqi1,
        ROUND((sum(t.num1)/sum(t.prevNum1)-1)*100,2) rate1,
        concat(ROUND(sum(t.num2),3),'') benqi2, concat(ROUND(sum(t.prevNum2),3),'') tongqi2,
        ROUND((sum(t.num2)/sum(t.prevNum2)-1)*100,2) rate2,
        ROUND(sum(t.num2)*100/sum(t.num1),2) benqi3, ROUND(sum(t.prevNum2)*100/sum(t.prevNum1),2) tongqi3, ROUND((
        (sum(t.num2)/sum(t.num1))/(sum(t.prevNum2)/sum(t.prevNum1)) -1 )*100,2) rate3
        from(
        select d.id, d.name, e.kind_name, e.epc, e.num1, e.prevNum1, e.num2, e.prevNum2
        from (select id, name from trade_category where del_flag = "1") d
        left join (
        <if test="query.id == '32'.toString()">
            <include refid="queryCustomRank32_cmp">
            </include>
        </if>
        <if test="query.id == '35'.toString()">
            <include refid="queryCustomRank35_cmp">
            </include>
        </if>
        <if test="query.id == '48'.toString()">
            <include refid="queryCustomRank48_cmp">
            </include>
        </if>
        ) e on e.kind_name = d.name
        )t group by t.id
        )tt
        <include refid="change_rate">
        </include>
    </select>
    <select id="queryCustomRank32_3" resultType="java.util.Map">
        select tt.area, tt.kind_name, tt.epc, tt.name, tt.staff, tt.staffTel, tt.benqi1, tt.tongqi1, tt.rate1,
        tt.benqi2, tt.tongqi2, tt.rate2, tt.benqi3, tt.tongqi3, tt.rate3
        from(
        select t.area, t.kind_name, t.epc, t.name, t.staff, t.staffTel,
        IFNULL(ROUND(t.num1,3),'') benqi1, IFNULL(ROUND(t.prevNum1,3),'') tongqi1, IFNULL(ROUND((t.num1/t.prevNum1-1
        )*100,2),'') rate1,
        IFNULL(ROUND(t.num2,3),'') benqi2, IFNULL(ROUND(t.prevNum2,3),'') tongqi2, IFNULL(ROUND((t.num2/t.prevNum2-1
        )*100,2),'') rate2,
        IFNULL(ROUND(t.num2*100/t.num1,2),'') benqi3, IFNULL(ROUND(t.prevNum2*100/t.prevNum1,2),'') tongqi3,
        IFNULL(ROUND(( (t.num2/t.num1)/(t.prevNum2/t.prevNum1) -1 )*100,2),'') rate3
        from(
        <if test="query.id == '32'.toString()">
            <include refid="queryCustomRank32_cmp">
            </include>
        </if>
        <if test="query.id == '35'.toString()">
            <include refid="queryCustomRank35_cmp">
            </include>
        </if>
        <if test="query.id == '48'.toString()">
            <include refid="queryCustomRank48_cmp">
            </include>
        </if>
        )t
        )tt
        <include refid="change_rate">
        </include>
    </select>
    <select id="queryCustomRank32_4" resultType="java.util.Map">
        select '合计' area, '合计' trade, count(*) epcNum,
        IFNULL(concat(ROUND(sum(t.num1),3),''),'') benqi1, IFNULL(concat(ROUND(sum(t.prevNum1),3),''),'') tongqi1,
        IFNULL(ROUND((sum(t.num1)/sum(t.prevNum1)-1)*100,2),'') rate1,
        IFNULL(concat(ROUND(sum(t.num2),3),''),'') benqi2, IFNULL(concat(ROUND(sum(t.prevNum2),3),''),'') tongqi2,
        IFNULL(ROUND((sum(t.num2)/sum(t.prevNum2)-1)*100,2),'') rate2,
        IFNULL(ROUND(sum(t.num2)*100/sum(t.num1),2),'') benqi3, IFNULL(ROUND(sum(t.prevNum2)*100/sum(t.prevNum1),2),'')
        tongqi3, IFNULL(ROUND(( (sum(t.num2)/sum(t.num1))/(sum(t.prevNum2)/sum(t.prevNum1)) -1 )*100,2),'') rate3
        from(
        <if test="query.id == '32'.toString()">
            <include refid="queryCustomRank32_cmp">
            </include>
        </if>
        <if test="query.id == '35'.toString()">
            <include refid="queryCustomRank35_cmp">
            </include>
        </if>
        )t
    </select>

    <!--能源消费、产出、回收 42综合能源消费量-->
    <sql id="queryCustomRank42_cmp">
        select distinct e.area,dr.meaning, e.kind_name, IFNULL(e.enterprise_code,'') epc, IFNULL(e.cmp_name,'') name,
        IFNULL(e.staff,'') staff, IFNULL(e.staff_tel,'') staffTel
        <if test="query.id == '42'.toString()">
            , f2.energy_consumption num, case when f2.energy_consumption != '' and f2.energy_consumption is not null
            then f1.energy_consumption else null end prevNum
        </if>
        from (
        <include refid="cmp_table">
        </include>
        ) e
        LEFT JOIN dict_region dr ON dr.id=e.area
        left join (select id, enterprise_code, year from t_report where del_flag = "1" and type="1" and
        report_status='3' and year = #{query.prevYear}) r1 on e.enterprise_code = r1.enterprise_code
        left join (select id, enterprise_code, year from t_report where del_flag = "1" and type="1" and
        report_status='3' and year = #{query.year}) r2 on e.enterprise_code = r2.enterprise_code
        left join t_report_financial f1 on f1.report_id = r1.id
        left join t_report_financial f2 on f2.report_id = r2.id
        <if test="query.id == '42'.toString()">
            where f2.energy_consumption is not null and f2.energy_consumption != ''
        </if>
    </sql>
    <select id="queryCustomRank42_1" resultType="java.util.Map">
        select IFNULL(tt.area,'') area, IFNULL(tt.benqi,'') benqi, IFNULL(tt.tongqi,'') tongqi, IFNULL(tt.rate,'') rate,
        IFNULL(tt.epcNum,0) epcNum
        from(
        select t.meaning2 area,
        concat(ROUND(sum(t.num),3),'') benqi, concat(ROUND(sum(t.prevNum),3),'') tongqi,
        ROUND((sum(t.num)/sum(t.prevNum)-1 )*100,2) rate, count(*) epcNum
        from(
        select d.id, d.meaning2, e.epc, e.num, e.prevNum
        from (select id, meaning2 from dict_region where pid = "530000" and del_flag = "1") d
        left join (
        <include refid="queryCustomRank42_cmp">
        </include>
        ) e on e.area = d.id
        )t group by t.id
        )tt
        <include refid="change_rate">
        </include>
    </select>
    <select id="queryCustomRank42_2" resultType="java.util.Map">
        select IFNULL(tt.trade,'') trade, IFNULL(tt.benqi,'') benqi, IFNULL(tt.tongqi,'') tongqi, IFNULL(tt.rate,'')
        rate, IFNULL(tt.epcNum,0) epcNum
        from(
        select t.name trade,
        concat(ROUND(sum(t.num),3),'') benqi, concat(ROUND(sum(t.prevNum),3),'') tongqi,
        ROUND((sum(t.num)/sum(t.prevNum)-1 )*100,2) rate, count(*) epcNum
        from(
        select d.id, d.name, e.kind_name, e.epc, e.num, e.prevNum
        from (select id, name from trade_category where del_flag = "1") d
        left join (
        <include refid="queryCustomRank42_cmp">
        </include>
        ) e on e.kind_name = d.name
        )t group by t.id
        )tt
        <include refid="change_rate">
        </include>
    </select>
    <select id="queryCustomRank42_3" resultType="java.util.Map">
        select tt.area,tt.meaning, tt.kind_name, tt.epc, tt.name, tt.staff, tt.staffTel, tt.benqi, tt.tongqi, tt.rate
        from(
        select t.area,t.meaning, t.kind_name, t.epc, t.name, t.staff, t.staffTel,
        IFNULL(ROUND(t.num,3),'') benqi, IFNULL(ROUND(t.prevNum,3),'') tongqi,
        IFNULL(ROUND((t.num/t.prevNum-1 )*100,2),'') rate
        from(
        <include refid="queryCustomRank42_cmp">
        </include>
        )t
        )tt
        <include refid="change_rate">
        </include>
    </select>
    <select id="queryCustomRank42_4" resultType="java.util.Map">
        select '合计' area, '合计' trade,
        IFNULL(concat(ROUND(sum(t.num),3),''),'') benqi, IFNULL(concat(ROUND(sum(t.prevNum),3),''),'') tongqi,
        IFNULL(ROUND( (sum(t.num)/sum(t.prevNum) -1 ) *100,2) ,'') rate, count(*) epcNum
        from(
        <include refid="queryCustomRank42_cmp">
        </include>
        )t
    </select>

    <!--能源消费、产出、回收 44工业生产消费量  41能源消费量合计  45非工业生产消费量-->
    <sql id="queryCustomRank44_cmp">
        select distinct e.area, e.kind_name, IFNULL(e.enterprise_code,'') epc, IFNULL(e.cmp_name,'') name,
        IFNULL(e.staff,'') staff, IFNULL(e.staff_tel,'') staffTel,f1.energy_code_name energyName,f1.unit unit
        <if test="query.id == '44'.toString()">
            , f2.consume_industry num, case when f2.consume_industry != '' and f2.consume_industry is not null then
            f1.consume_industry else null end prevNum
        </if>
        <if test="query.id == '41'.toString()">
            , f2.consume_sum num, case when f2.consume_sum != '' and f2.consume_sum is not null then f1.consume_sum else
            null end prevNum
        </if>
        <if test="query.id == '45'.toString()">
            , f2.consume_noindustry num, case when f2.consume_noindustry != '' and f2.consume_noindustry is not null
            then f1.consume_noindustry else null end prevNum
        </if>
        from (
        <include refid="cmp_table">
        </include>
        ) e
        left join (select id, enterprise_code, year from t_report where del_flag = "1" and type="2" and
        report_status='3' and year = #{query.prevYear}) r1 on e.enterprise_code = r1.enterprise_code
        left join (select id, enterprise_code, year from t_report where del_flag = "1" and type="2" and
        report_status='3' and year = #{query.year}) r2 on e.enterprise_code = r2.enterprise_code
        left join t_report_energys_tructure f1 on f1.report_id = r1.id and f1.energy_code= #{code}
        left join t_report_energys_tructure f2 on f2.report_id = r2.id and f2.energy_code= #{code}
        <if test="query.id == '44'.toString()">
            where f2.consume_industry is not null and f2.consume_industry != ''
        </if>
        <if test="query.id == '41'.toString()">
            where f2.consume_sum is not null and f2.consume_sum != ''
        </if>
        <if test="query.id == '45'.toString()">
            where f2.consume_noindustry is not null and f2.consume_noindustry != ''
        </if>
    </sql>
    <sql id="queryCustomRank44-4_cmp">
        select distinct e.area, e.kind_name, IFNULL(e.enterprise_code,'') epc, IFNULL(e.cmp_name,'') name,
        IFNULL(e.staff,'') staff, IFNULL(e.staff_tel,'') staffTel,f1.energy_code_name energyName,f1.unit unit
        <if test="query.id == '44'.toString()">
            , f2.consume_industry num, case when f2.consume_industry != '' and f2.consume_industry is not null then
            f1.consume_industry else null end prevNum
        </if>
        <if test="query.id == '41'.toString()">
            , f2.consume_sum num, case when f2.consume_sum != '' and f2.consume_sum is not null then f1.consume_sum else
            null end prevNum
        </if>
        <if test="query.id == '45'.toString()">
            , f2.consume_noindustry num, case when f2.consume_noindustry != '' and f2.consume_noindustry is not null
            then f1.consume_noindustry else null end prevNum
        </if>
        from (
        <include refid="cmp_table">
        </include>
        ) e
        left join (select id, enterprise_code, year from t_report where del_flag = "1" and type="2" and
        report_status='3' and year = #{query.prevYear}) r1 on e.enterprise_code = r1.enterprise_code
        left join (select id, enterprise_code, year from t_report where del_flag = "1" and type="2" and
        report_status='3' and year = #{query.year}) r2 on e.enterprise_code = r2.enterprise_code
        left join t_report_energys_tructure f1 on f1.report_id = r1.id and f1.energy_code= #{query.source}
        left join t_report_energys_tructure f2 on f2.report_id = r2.id and f2.energy_code= #{query.source}
        <if test="query.id == '44'.toString()">
            where f2.consume_industry is not null and f2.consume_industry != ''
        </if>
        <if test="query.id == '41'.toString()">
            where f2.consume_sum is not null and f2.consume_sum != ''
        </if>
        <if test="query.id == '45'.toString()">
            where f2.consume_noindustry is not null and f2.consume_noindustry != ''
        </if>
    </sql>
    <select id="queryCustomRank44_1" resultType="java.util.Map">
        select IFNULL(tt.area,'') area,IFNULL( tt.energyName,'') energyName, IFNULL(tt.benqi,'') benqi,
        IFNULL(tt.tongqi,'') tongqi, IFNULL(tt.rate,'') rate, IFNULL(tt.epcNum,0) epcNum
        from(
        select t.meaning2 area,
        concat(ROUND(sum(t.num),3),'') benqi, concat(ROUND(sum(t.prevNum),3),'') tongqi,
        ROUND((sum(t.num)/sum(t.prevNum)-1 )*100,2) rate, count(*) epcNum,concat(t.energyName,'(',t.unit,')') energyName
        from(
        select d.id, d.meaning2, e.epc, e.num, e.prevNum,e.energyName,e.unit
        from (select id, meaning2 from dict_region where pid = "530000" and del_flag = "1") d
        left join (
        <include refid="queryCustomRank44_cmp">
        </include>
        ) e on e.area = d.id
        )t group by t.id
        )tt
        <include refid="change_rate">
        </include>
    </select>
    <select id="queryCustomRank44_2" resultType="java.util.Map">
        select IFNULL(tt.trade,'') trade, IFNULL(tt.benqi,'') benqi, IFNULL(tt.tongqi,'') tongqi, IFNULL(tt.rate,'')
        rate, IFNULL(tt.epcNum,0) epcNum,IFNULL( tt.energyName,'') energyName
        from(
        select t.name trade,
        concat(ROUND(sum(t.num),3),'') benqi, concat(ROUND(sum(t.prevNum),3),'') tongqi,
        ROUND((sum(t.num)/sum(t.prevNum)-1 )*100,2) rate, count(*) epcNum,concat(t.energyName,'(',t.unit,')') energyName
        from(
        select d.id, d.name, e.kind_name, e.epc, e.num, e.prevNum,e.energyName,e.unit
        from (select id, name from trade_category where del_flag = "1") d
        left join (
        <include refid="queryCustomRank44_cmp">
        </include>
        ) e on e.kind_name = d.name
        )t group by t.id
        )tt
        <include refid="change_rate">
        </include>
    </select>
    <select id="queryCustomRank44_3" resultType="java.util.Map">
        select tt.area, tt.kind_name, tt.epc, tt.name, tt.staff, tt.staffTel, tt.benqi, tt.tongqi, tt.rate
        from(
        select t.area, t.kind_name, t.epc, t.name, t.staff, t.staffTel,
        IFNULL(ROUND(t.num,3),'') benqi, IFNULL(ROUND(t.prevNum,3),'') tongqi,
        IFNULL(ROUND((t.num/t.prevNum-1 )*100,2),'') rate
        from(
        <include refid="queryCustomRank44-4_cmp">
        </include>
        )t
        )tt
        <include refid="change_rate">
        </include>
    </select>
    <select id="queryCustomRank44_4" resultType="java.util.Map">
        select '合计' area, '合计' trade,
        IFNULL(concat(ROUND(sum(t.num),3),''),'') benqi, IFNULL(concat(ROUND(sum(t.prevNum),3),''),'') tongqi,
        IFNULL(ROUND( (sum(t.num)/sum(t.prevNum) -1 ) *100,2) ,'') rate, count(*) epcNum,concat(t.energyName,'(',t.unit,')') energyName
        from(
        <include refid="queryCustomRank44_cmp">
        </include>
        )t
    </select>

    <!--能源消费、产出、回收 43能源回收利用量 46加工转换产出能源量 47加工转换投入能源量-->
    <sql id="queryCustomRank43_cmp">
        select distinct e.area, e.kind_name, IFNULL(e.enterprise_code,'') epc, IFNULL(e.cmp_name,'') name,
        IFNULL(e.staff,'') staff, IFNULL(e.staff_tel,'') staffTel,f1.energy_code_name energyName
        <if test="query.id == '43'.toString()">
            , f2.recycling num, case when f2.recycling != '' and f2.recycling is not null then f1.recycling else null
            end prevNum
        </if>
        <if test="query.id == '46'.toString()">
            , f2.conversion_output_ref num, case when f2.conversion_output_ref != '' and f2.conversion_output_ref is not
            null then f1.conversion_output_ref else null end prevNum
        </if>
        <if test="query.id == '47'.toString()">
            , f2.pro_con_total num, case when f2.pro_con_total != '' and f2.pro_con_total is not null then
            f1.pro_con_total else null end prevNum
        </if>
        from (
        <include refid="cmp_table">
        </include>
        ) e
        left join (select id, enterprise_code, year from t_report where del_flag = "1" and type="2-1" and
        report_status='3' and year = #{query.prevYear}) r1 on e.enterprise_code = r1.enterprise_code
        left join (select id, enterprise_code, year from t_report where del_flag = "1" and type="2-1" and
        report_status='3' and year = #{query.year}) r2 on e.enterprise_code = r2.enterprise_code
        left join t_report_energy_out f1 on f1.report_id = r1.id and f1.energy_code= #{code}
        left join t_report_energy_out f2 on f2.report_id = r2.id and f2.energy_code= #{code}
        <if test="query.id == '43'.toString()">
            where f2.recycling is not null and f2.recycling != ''
        </if>
        <if test="query.id == '46'.toString()">
            where f2.conversion_output_ref is not null and f2.conversion_output_ref != ''
        </if>
        <if test="query.id == '47'.toString()">
            where f2.pro_con_total is not null and f2.pro_con_total != ''
        </if>
    </sql>
    <sql id="queryCustomRank43-3_cmp">
        select distinct e.area, e.kind_name, IFNULL(e.enterprise_code,'') epc, IFNULL(e.cmp_name,'') name,
        IFNULL(e.staff,'') staff, IFNULL(e.staff_tel,'') staffTel,f1.energy_code_name energyName
        <if test="query.id == '43'.toString()">
            , f2.recycling num, case when f2.recycling != '' and f2.recycling is not null then f1.recycling else null
            end prevNum
        </if>
        <if test="query.id == '46'.toString()">
            , f2.conversion_output_ref num, case when f2.conversion_output_ref != '' and f2.conversion_output_ref is not
            null then f1.conversion_output_ref else null end prevNum
        </if>
        <if test="query.id == '47'.toString()">
            , f2.pro_con_total num, case when f2.pro_con_total != '' and f2.pro_con_total is not null then
            f1.pro_con_total else null end prevNum
        </if>
        from (
        <include refid="cmp_table">
        </include>
        ) e
        left join (select id, enterprise_code, year from t_report where del_flag = "1" and type="2-1" and
        report_status='3' and year = #{query.prevYear}) r1 on e.enterprise_code = r1.enterprise_code
        left join (select id, enterprise_code, year from t_report where del_flag = "1" and type="2-1" and
        report_status='3' and year = #{query.year}) r2 on e.enterprise_code = r2.enterprise_code
        left join t_report_energy_out f1 on f1.report_id = r1.id and f1.energy_code= #{query.source}
        left join t_report_energy_out f2 on f2.report_id = r2.id and f2.energy_code= #{query.source}
        <if test="query.id == '43'.toString()">
            where f2.recycling is not null and f2.recycling != ''
        </if>
        <if test="query.id == '46'.toString()">
            where f2.conversion_output_ref is not null and f2.conversion_output_ref != ''
        </if>
        <if test="query.id == '47'.toString()">
            where f2.pro_con_total is not null and f2.pro_con_total != ''
        </if>
    </sql>
    <select id="queryCustomRank43_1" resultType="java.util.Map">
        select IFNULL(tt.area,'') area,IFNULL( tt.energyName,'') energyName, IFNULL(tt.benqi,'') benqi,
        IFNULL(tt.tongqi,'') tongqi, IFNULL(tt.rate,'') rate, IFNULL(tt.epcNum,0) epcNum
        from(
        select t.meaning2 area,
        concat(ROUND(sum(t.num),3),'') benqi, concat(ROUND(sum(t.prevNum),3),'') tongqi,
        ROUND((sum(t.num)/sum(t.prevNum)-1 )*100,2) rate, count(*) epcNum,t.energyName energyName
        from(
        select d.id, d.meaning2, e.epc, e.num, e.prevNum,e.energyName
        from (select id, meaning2 from dict_region where pid = "530000" and del_flag = "1") d
        left join (
        <include refid="queryCustomRank43_cmp">
        </include>
        ) e on e.area = d.id
        )t group by t.id
        )tt
        <include refid="change_rate">
        </include>
    </select>
    <select id="queryCustomRank43_2" resultType="java.util.Map">
        select IFNULL(tt.trade,'') trade, IFNULL(tt.benqi,'') benqi, IFNULL(tt.tongqi,'') tongqi, IFNULL(tt.rate,'')
        rate, IFNULL(tt.epcNum,0) epcNum,IFNULL( tt.energyName,'') energyName
        from(
        select t.name trade,
        concat(ROUND(sum(t.num),3),'') benqi, concat(ROUND(sum(t.prevNum),3),'') tongqi,
        ROUND((sum(t.num)/sum(t.prevNum)-1 )*100,2) rate, count(*) epcNum,t.energyName energyName
        from(
        select d.id, d.name, e.kind_name, e.epc, e.num, e.prevNum,e.energyName
        from (select id, name from trade_category where del_flag = "1") d
        left join (
        <include refid="queryCustomRank43_cmp">
        </include>
        ) e on e.kind_name = d.name
        )t group by t.id
        )tt
        <include refid="change_rate">
        </include>
    </select>
    <select id="queryCustomRank43_3" resultType="java.util.Map">
        select tt.area, tt.kind_name, tt.epc, tt.name, tt.staff, tt.staffTel, tt.benqi, tt.tongqi, tt.rate
        from(
        select t.area, t.kind_name, t.epc, t.name, t.staff, t.staffTel,
        IFNULL(ROUND(t.num,3),'') benqi, IFNULL(ROUND(t.prevNum,3),'') tongqi,
        IFNULL(ROUND((t.num/t.prevNum-1 )*100,2),'') rate
        from(
        <include refid="queryCustomRank43-3_cmp">
        </include>
        )t
        )tt
        <include refid="change_rate">
        </include>
    </select>
    <select id="queryCustomRank43_4" resultType="java.util.Map">
        select '合计' area, '合计' trade,
        IFNULL(concat(ROUND(sum(t.num),3),''),'') benqi, IFNULL(concat(ROUND(sum(t.prevNum),3),''),'') tongqi,
        IFNULL(ROUND( (sum(t.num)/sum(t.prevNum) -1 ) *100,2) ,'') rate, count(*) epcNum,t.energyName energyName
        from(
        <include refid="queryCustomRank43_cmp">
        </include>
        )t
    </select>
    <!--单位产品综合能耗 51单位产品综合能耗  52可比单位产品综合能耗-->

    <select id="queryCustomRank51_1" resultType="com.nantian.cloud.jljc.vo.TReportUnitenergy">
        SELECT
        quota_name,
        GROUP_CONCAT( benqi ) as benqi,
        GROUP_CONCAT( tongqi ) as tongqi,
        GROUP_CONCAT( tongqiNum ) as tongqi_num,
        GROUP_CONCAT( benqiNum ) as benqi_num,
        GROUP_CONCAT( areacode ) as area_code,
        GROUP_CONCAT( area ) as area,
        GROUP_CONCAT( rate ) as rate
        FROM
        (
        SELECT
        ROUND( SUM( DISTINCT( CASE b.`year` WHEN #{query.year} THEN a.guideline_val ELSE 0 END )), 2 ) AS benqi,
        ROUND( SUM( DISTINCT( CASE b.`year` WHEN #{query.prevYear} THEN a.guideline_val ELSE 0 END )), 2 ) AS tongqi,
        ROUND((SUM( DISTINCT( CASE b.`year` WHEN #{query.year} THEN a.guideline_val ELSE 0 END )) / SUM( DISTINCT( CASE b.`year` WHEN #{query.prevYear} THEN a.guideline_val ELSE 0 END )) - 1 ) * 100,2 ) rate,
        concat(quota_name,'(',quota_unit,')') quota_name,
        count( DISTINCT ( CASE b.`year` WHEN #{query.prevYear} THEN b.enterprise_code ELSE NULL END ) ) AS tongqiNum,
        count( DISTINCT ( CASE b.`year` WHEN #{query.year} THEN b.enterprise_code ELSE NULL END ) ) AS benqiNum,
        concat( LEFT ( c.region_code, 4 ), "00" ) AS areacode,
        d.meaning2 AS area
        FROM
        t_report b
        LEFT JOIN(
        select * from t_report_enterprise
        <if test="split != null and split != ''">
            where
            <foreach collection="split" index="index" item="i" separator="or">
                epc_type like concat("%",#{i},"%")
            </foreach>
        </if>
        )c ON b.enterprise_code = c.enterprise_code
        AND b.YEAR = c.
        YEAR LEFT JOIN t_report_unitenergy a ON b.id = a.report_id
        LEFT JOIN (SELECT DISTINCT
        concat( LEFT ( dd.area, 4 ), "00" ) area,
        dd.meaning2
        FROM
        ( SELECT LEFT ( d.id, 4 ) area, meaning2 FROM dict_region d WHERE pid = "530000" AND del_flag = "1" ) dd
        GROUP BY
        dd.area ) d ON concat( LEFT ( c.region_code, 4 ), "00" )= d.area
        LEFT JOIN trade_info e ON c.trade = e.trade_num
        WHERE
        1 = 1
        AND b.YEAR IN ( #{query.prevYear}, #{query.year} )
        <if test="query.area != null and query.area != ''">
            AND #{query.area} LIKE concat( "%", LEFT ( c.region_code, 4 ), "%" )
        </if>
        <if test="query.industry != null and query.industry != ''">
            and #{query.industry} like concat("%",e.kind_name,"%")
        </if>
        AND b.del_flag = "1"
        AND b.type = "3"
        AND b.report_status = '3'
        AND a.guideline_val !=''
        AND a.guideline_val is not NULL
        GROUP BY
        a.quota_name,
        SUBSTR(
        c.region_code,
        1,
        4
        )
<!--        变化率-->
        <if test="query.low1 != null and query.low1 != '' and query.high1 != null and query.high1 != ''">
            having rate between #{query.low1} and #{query.high1}
        </if>
<!--        数值-->
        <if test="query.low2 != null and query.low2 != '' and query.high2 != null and query.high2 != ''">
            <choose>
                <when test="query.low1 != null and query.low1 != '' and query.high1 != null and query.high1 != ''">
                    and benqi between #{query.low2} and #{query.high2}
                </when>
                <otherwise>
                    HAVING benqi between #{query.low2} and #{query.high2}
                </otherwise>
            </choose>
        </if>
        ) t
        where t.quota_name is not null and t.quota_name!=''
        and t.benqi is not null and t.benqi!=''
        and t.tongqi is not null and t.tongqi!=''
        AND t.area IS NOT NULL AND t.area !=''
        GROUP BY
        quota_name
    </select>
    <select id="queryCustomRank51_2" resultType="com.nantian.cloud.jljc.vo.TReportUnitenergy">
        SELECT
        quota_name,
        GROUP_CONCAT( benqi ) as benqi,
        GROUP_CONCAT( tongqi ) as tongqi,
        GROUP_CONCAT( tongqiNum ) as tongqi_num,
        GROUP_CONCAT( benqiNum ) as benqi_num,
        GROUP_CONCAT( industry ) as trade,
        GROUP_CONCAT( rate ) as rate
        FROM
        (
        SELECT
        ROUND( SUM( DISTINCT( CASE b.`year` WHEN #{query.year} THEN a.guideline_val ELSE 0 END )), 2 ) AS benqi,
        ROUND( SUM( DISTINCT( CASE b.`year` WHEN #{query.prevYear} THEN a.guideline_val ELSE 0 END )), 2 ) AS tongqi,
        ROUND((SUM( DISTINCT( CASE b.`year` WHEN #{query.year} THEN a.guideline_val ELSE 0 END )) / SUM( DISTINCT( CASE b.`year` WHEN #{query.prevYear} THEN a.guideline_val ELSE 0 END )) - 1 ) * 100,2 ) rate,
        concat(quota_name,'(',quota_unit,')') quota_name,
        count( DISTINCT ( CASE b.`year` WHEN #{query.prevYear} THEN b.enterprise_code ELSE NULL END ) ) AS tongqiNum,
        count( DISTINCT ( CASE b.`year` WHEN #{query.year} THEN b.enterprise_code ELSE NULL END ) ) AS benqiNum,
        d.kind_name AS industry
        FROM
        t_report b
        LEFT JOIN(
        select * from t_report_enterprise
        <if test="split != null and split != ''">
            where
            <foreach collection="split" index="index" item="i" separator="or">
                epc_type like concat("%",#{i},"%")
            </foreach>
        </if>
        )c ON b.enterprise_code = c.enterprise_code
        AND b.YEAR = c.YEAR
        LEFT JOIN t_report_unitenergy a ON b.id = a.report_id
        LEFT JOIN trade_info d ON c.trade = d.trade_num
        LEFT JOIN (SELECT DISTINCT
        concat( LEFT ( dd.area, 4 ), "00" ) area,
        dd.meaning2
        FROM
        ( SELECT LEFT ( d.id, 4 ) area, meaning2 FROM dict_region d WHERE pid = "530000" AND del_flag = "1" ) dd
        GROUP BY
        dd.area ) f ON concat( LEFT ( c.region_code, 4 ), "00" )= f.area
        WHERE
        1 = 1
        <if test="query.industry != null and query.industry != ''">
            and #{query.industry} like concat("%",d.kind_name,"%")
        </if>
        <if test="query.area != null and query.area != ''">
            AND #{query.area} LIKE concat( "%", LEFT ( c.region_code, 4 ), "%" )
        </if>
        AND b.YEAR IN ( #{query.prevYear}, #{query.year} )
        AND b.del_flag = "1"
        AND b.type = "3"
        AND b.report_status = '3'
        AND a.guideline_val !=''
        AND a.guideline_val is not NULL
        GROUP BY
        a.quota_name,
        d.kind_name
        <!--        变化率-->
        <if test="query.low1 != null and query.low1 != '' and query.high1 != null and query.high1 != ''">
            having rate between #{query.low1} and #{query.high1}
        </if>
<!--        数值-->
        <if test="query.low2 != null and query.low2 != '' and query.high2 != null and query.high2 != ''">
            <choose>
                <when test="query.low1 != null and query.low1 != '' and query.high1 != null and query.high1 != ''">
                    and benqi between #{query.low2} and #{query.high2}
                </when>
                <otherwise>
                    having benqi between #{query.low2} and #{query.high2}
                </otherwise>
            </choose>
        </if>
        ) t
        where t.quota_name is not null and t.quota_name!=''
        and t.benqi is not null and t.benqi!=''
        and t.tongqi is not null and t.tongqi!=''
        and t.industry is not null and t.industry!=''
        GROUP BY
        quota_name
    </select>
    <select id="queryCustomRank51_3" resultType="java.util.Map">
        SELECT *
	    FROM(
	    SELECT
	    concat(quota_name,'(',quota_unit,')') quota_name,
	    ROUND(SUM(DISTINCT(a.guideline_val)), 2) AS benqi,
	    count(DISTINCT(b.enterprise_code)) AS benqiNum,
	    a.quota_limit,
	    ROUND((1-SUM(DISTINCT(a.guideline_val))/a.quota_limit)*100,2) limitRate,
	    c.staff,
	    c.staff_tel as staffTel,
	    c.enterprise_name ,
	    d.meaning2 area,
	    e.kind_name trade
        FROM
        t_report b
        LEFT JOIN(
        select * from t_report_enterprise
        <if test="split != null and split != ''">
            where
            <foreach collection="split" index="index" item="i" separator="or">
                epc_type like concat("%",#{i},"%")
            </foreach>
        </if>
        )c ON b.enterprise_code = c.enterprise_code
        AND b.YEAR = c.YEAR LEFT JOIN t_report_unitenergy a ON b.id = a.report_id
        LEFT JOIN (SELECT DISTINCT
        concat( LEFT ( dd.area, 4 ), "00" ) area,
        dd.meaning2
        FROM
        ( SELECT LEFT ( d.id, 4 ) area, meaning2 FROM dict_region d WHERE pid = "530000" AND del_flag = "1" ) dd
        GROUP BY
        dd.area ) d ON concat( LEFT ( c.region_code, 4 ), "00" )= d.area
        LEFT JOIN trade_info e ON c.trade = e.trade_num
        WHERE
        1 = 1
        AND b.YEAR =#{query.year}
        <if test="query.industry != null and query.industry != ''">
            and #{query.industry} like concat("%",e.kind_name,"%")
        </if>
        <if test="query.area != null and query.area != ''">
            and #{query.area} LIKE concat( "%", LEFT ( c.region_code, 4 ), "%" )
        </if>
        <if test="query.name != null and query.name != ''">
            and c.enterprise_name like concat("%",#{query.name},"%")
        </if>
        <if test="query.source != null and query.source != ''">
            and a.quota_name =#{query.source}
        </if>
        AND b.del_flag = "1"
        AND b.type = "3"
        AND b.report_status = '3'
        AND a.guideline_val !=''
        AND a.guideline_val is not NULL
        GROUP BY
        c.enterprise_name
        <!--        变化率-->
        <if test="query.low1 != null and query.low1 != '' and query.high1 != null and query.high1 != ''">
            having limitRate between #{query.low1} and #{query.high1}
        </if>
<!--        数值-->
        <if test="query.low2 != null and query.low2 != '' and query.high2 != null and query.high2 != ''">
            <choose>
                <when test="query.low1 != null and query.low1 != '' and query.high1 != null and query.high1 != ''">
                    and benqi between #{query.low2} and #{query.high2}
                </when>
                <otherwise>
                    having benqi between #{query.low2} and #{query.high2}
                </otherwise>
            </choose>
        </if>
        ) t
	WHERE t.benqi !=0
    </select>
    <select id="queryCustomRank52_1" resultType="com.nantian.cloud.jljc.vo.TReportUnitenergy">
        SELECT
        quota_name,
        GROUP_CONCAT( benqi ) as benqi,
        GROUP_CONCAT( tongqi ) as tongqi,
        GROUP_CONCAT( tongqiNum ) as tongqi_num,
        GROUP_CONCAT( benqiNum ) as benqi_num,
        GROUP_CONCAT( areacode ) as area_code,
        GROUP_CONCAT( area ) as area,
        GROUP_CONCAT( rate ) as rate
        FROM
        (
        SELECT
        ROUND( SUM( DISTINCT( CASE b.`year` WHEN #{query.year} THEN a.quota_value ELSE 0 END )), 2 ) AS benqi,
        ROUND( SUM( DISTINCT( CASE b.`year` WHEN #{query.prevYear} THEN a.quota_value ELSE 0 END )), 2 ) AS tongqi,
        ROUND((SUM( DISTINCT( CASE b.`year` WHEN #{query.year} THEN a.quota_value ELSE 0 END )) / SUM( DISTINCT( CASE b.`year` WHEN #{query.prevYear} THEN a.quota_value ELSE 0 END )) - 1 ) * 100,2 ) rate,
        concat(quota_name,'(',quota_unit,')') quota_name,
        count( DISTINCT ( CASE b.`year` WHEN #{query.prevYear} THEN b.enterprise_code ELSE NULL END ) ) AS tongqiNum,
        count( DISTINCT ( CASE b.`year` WHEN #{query.year} THEN b.enterprise_code ELSE NULL END ) ) AS benqiNum,
        concat( LEFT ( c.region_code, 4 ), "00" ) AS areacode,
        d.meaning2 AS area
        FROM
        t_report b
        LEFT JOIN(
        select * from t_report_enterprise
        <if test="split != null and split != ''">
            where
            <foreach collection="split" index="index" item="i" separator="or">
                epc_type like concat("%",#{i},"%")
            </foreach>
        </if>
        )c ON b.enterprise_code = c.enterprise_code
        AND b.YEAR = c.
        YEAR LEFT JOIN t_report_unitenergy_comp a ON b.id = a.report_id
        LEFT JOIN (SELECT DISTINCT
        concat( LEFT ( dd.area, 4 ), "00" ) area,
        dd.meaning2
        FROM
        ( SELECT LEFT ( d.id, 4 ) area, meaning2 FROM dict_region d WHERE pid = "530000" AND del_flag = "1" ) dd
        GROUP BY
        dd.area ) d ON concat( LEFT ( c.region_code, 4 ), "00" )= d.area
        LEFT JOIN trade_info e ON c.trade = e.trade_num
        WHERE
        1 = 1
        AND b.YEAR IN ( #{query.prevYear}, #{query.year} )
        <if test="query.area != null and query.area != ''">
            AND #{query.area} LIKE concat( "%", LEFT ( c.region_code, 4 ), "%" )
        </if>
        <if test="query.industry != null and query.industry != ''">
            and #{query.industry} like concat("%",e.kind_name,"%")
        </if>
        AND b.del_flag = "1"
        AND b.type = "3-1"
        AND b.report_status = '3'
        AND a.quota_value !=''
        AND a.quota_value is not NULL
        GROUP BY
        a.quota_name,
        SUBSTR(
        c.region_code,
        1,
        4
        )
        <!--        变化率-->
        <if test="query.low1 != null and query.low1 != '' and query.high1 != null and query.high1 != ''">
            having rate between #{query.low1} and #{query.high1}
        </if>
<!--        数值-->
        <if test="query.low2 != null and query.low2 != '' and query.high2 != null and query.high2 != ''">
            <choose>
                <when test="query.low1 != null and query.low1 != '' and query.high1 != null and query.high1 != ''">
                    and benqi between #{query.low2} and #{query.high2}
                </when>
                <otherwise>
                    having benqi between #{query.low2} and #{query.high2}
                </otherwise>
            </choose>
        </if>
        ) t
        where t.quota_name is not null and t.quota_name!=''
        and t.benqi is not null and t.benqi!=''
        and t.tongqi is not null and t.tongqi!=''
        AND t.area IS NOT NULL AND t.area !=''
        GROUP BY
        quota_name
    </select>
    <select id="queryCustomRank52_2" resultType="com.nantian.cloud.jljc.vo.TReportUnitenergy">
        SELECT
        quota_name,
        GROUP_CONCAT( benqi ) as benqi,
        GROUP_CONCAT( tongqi ) as tongqi,
        GROUP_CONCAT( tongqiNum ) as tongqi_num,
        GROUP_CONCAT( benqiNum ) as benqi_num,
        GROUP_CONCAT( industry ) as trade,
        GROUP_CONCAT( rate ) as rate
        FROM
        (
        SELECT
        ROUND( SUM( DISTINCT( CASE b.`year` WHEN #{query.year} THEN a.quota_value ELSE 0 END )), 2 ) AS benqi,
        ROUND( SUM( DISTINCT( CASE b.`year` WHEN #{query.prevYear} THEN a.quota_value ELSE 0 END )), 2 ) AS tongqi,
        ROUND((SUM( DISTINCT( CASE b.`year` WHEN #{query.year} THEN a.quota_value ELSE 0 END )) / SUM( DISTINCT( CASE b.`year` WHEN #{query.prevYear} THEN a.quota_value ELSE 0 END )) - 1 ) * 100,2 ) rate,
        concat(quota_name,'(',quota_unit,')') quota_name,
        count( DISTINCT ( CASE b.`year` WHEN #{query.prevYear} THEN b.enterprise_code ELSE NULL END ) ) AS tongqiNum,
        count( DISTINCT ( CASE b.`year` WHEN #{query.year} THEN b.enterprise_code ELSE NULL END ) ) AS benqiNum,
        d.kind_name AS industry
        FROM
        t_report b
        LEFT JOIN(
        select * from t_report_enterprise
        <if test="split != null and split != ''">
            where
            <foreach collection="split" index="index" item="i" separator="or">
                epc_type like concat("%",#{i},"%")
            </foreach>
        </if>
        )c ON b.enterprise_code = c.enterprise_code
        AND b.YEAR = c.YEAR
        LEFT JOIN t_report_unitenergy_comp a ON b.id = a.report_id
        LEFT JOIN trade_info d ON c.trade = d.trade_num
        LEFT JOIN (SELECT DISTINCT
        concat( LEFT ( dd.area, 4 ), "00" ) area,
        dd.meaning2
        FROM
        ( SELECT LEFT ( d.id, 4 ) area, meaning2 FROM dict_region d WHERE pid = "530000" AND del_flag = "1" ) dd
        GROUP BY
        dd.area ) f ON concat( LEFT ( c.region_code, 4 ), "00" )= f.area
        WHERE
        1 = 1
        <if test="query.industry != null and query.industry != ''">
            and #{query.industry} like concat("%",d.kind_name,"%")
        </if>
        <if test="query.area != null and query.area != ''">
            AND #{query.area} LIKE concat( "%", LEFT ( c.region_code, 4 ), "%" )
        </if>
        AND b.YEAR IN ( #{query.prevYear}, #{query.year} )
        AND b.del_flag = "1"
        AND b.type = "3-1"
        AND b.report_status = '3'
        AND a.quota_value !=''
        AND a.quota_value is not NULL
        GROUP BY
        a.quota_name,
        d.kind_name
        <!--        变化率-->
        <if test="query.low1 != null and query.low1 != '' and query.high1 != null and query.high1 != ''">
            having rate between #{query.low1} and #{query.high1}
        </if>
<!--        数值-->
        <if test="query.low2 != null and query.low2 != '' and query.high2 != null and query.high2 != ''">
            <choose>
                <when test="query.low1 != null and query.low1 != '' and query.high1 != null and query.high1 != ''">
                    and benqi between #{query.low2} and #{query.high2}
                </when>
                <otherwise>
                    having benqi between #{query.low2} and #{query.high2}
                </otherwise>
            </choose>
        </if>
        ) t
        where t.quota_name is not null and t.quota_name!=''
        and t.benqi is not null and t.benqi!=''
        and t.tongqi is not null and t.tongqi!=''
        and t.industry is not null and t.industry!=''
        GROUP BY
        quota_name
    </select>
    <select id="queryCustomRank52_3" resultType="java.util.Map">
        SELECT *
	    FROM(
	    SELECT
	    concat(quota_name,'(',quota_unit,')') quota_name,
	    ROUND(SUM(DISTINCT(a.quota_value)), 2) AS benqi,
	    count(DISTINCT(b.enterprise_code)) AS benqiNum,
	    a.quota_limit,
	    ROUND((1-SUM(DISTINCT(a.quota_value))/a.quota_limit)*100,2) limitRate,
	    c.staff,
	    c.staff_tel as staffTel,
	    c.enterprise_name ,
	    d.meaning2 area,
	    e.kind_name trade
        FROM
        t_report b
        LEFT JOIN(
        select * from t_report_enterprise
        <if test="split != null and split != ''">
            where
            <foreach collection="split" index="index" item="i" separator="or">
                epc_type like concat("%",#{i},"%")
            </foreach>
        </if>
        )c ON b.enterprise_code = c.enterprise_code
        AND b.YEAR = c.YEAR LEFT JOIN t_report_unitenergy_comp a ON b.id = a.report_id
        LEFT JOIN (SELECT DISTINCT
        concat( LEFT ( dd.area, 4 ), "00" ) area,
        dd.meaning2
        FROM
        ( SELECT LEFT ( d.id, 4 ) area, meaning2 FROM dict_region d WHERE pid = "530000" AND del_flag = "1" ) dd
        GROUP BY
        dd.area ) d ON concat( LEFT ( c.region_code, 4 ), "00" )= d.area
        LEFT JOIN trade_info e ON c.trade = e.trade_num
        WHERE
        1 = 1
        AND b.YEAR =#{query.year}
        <if test="query.industry != null and query.industry != ''">
            and #{query.industry} like concat("%",e.kind_name,"%")
        </if>
        <if test="query.area != null and query.area != ''">
            and #{query.area} LIKE concat( "%", LEFT ( c.region_code, 4 ), "%" )
        </if>
        <if test="query.name != null and query.name != ''">
            and c.enterprise_name like concat("%",#{query.name},"%")
        </if>
        <if test="query.source != null and query.source != ''">
            and a.quota_name =#{query.source}
        </if>
        AND b.del_flag = "1"
        AND b.type = "3-1"
        AND b.report_status = '3'
        AND a.quota_value !=''
        AND a.quota_value is not NULL
        GROUP BY
        c.enterprise_name
        <!--        变化率-->
        <if test="query.low1 != null and query.low1 != '' and query.high1 != null and query.high1 != ''">
            having limitRate between #{query.low1} and #{query.high1}
        </if>
<!--        数值-->
        <if test="query.low2 != null and query.low2 != '' and query.high2 != null and query.high2 != ''">
            <choose>
                <when test="query.low1 != null and query.low1 != '' and query.high1 != null and query.high1 != ''">
                    and benqi between #{query.low2} and #{query.high2}
                </when>
                <otherwise>
                    having benqi between #{query.low2} and #{query.high2}
                </otherwise>
            </choose>
        </if>
        ) t
	WHERE t.benqi !=0
    </select>


    <!--企业节能技改项目 71年度完成节能项目数量、投资金额-->
    <sql id="queryCustomRank71_cmp">
        select distinct e.area, e.kind_name, IFNULL(e.enterprise_code,'') epc, IFNULL(e.cmp_name,'') name,
        IFNULL(e.staff,'') staff, IFNULL(e.staff_tel,'') staffTel,f1.project_type Type1,f2.project_type Type2
        <if test="query.id == '71'.toString()">
            , f2.money num, case when f2.money != '' and f2.money is not null then
            f1.money else null end prevNum
        </if>
        <if test="query.id == '72'.toString()">
            , f2.money num, case when f2.money != '' and f2.money is not null then f1.money else
            null end prevNum
        </if>
        <if test="query.id == '73'.toString()">
            , f2.energy_saving_effect num, case when f2.energy_saving_effect != '' and f2.energy_saving_effect is not
            null
            then f1.energy_saving_effect else null end prevNum
        </if>
        from (
        <include refid="cmp_table">
        </include>
        ) e
        left join (select id, enterprise_code, year from t_report where del_flag = "1" and type="5" and
        report_status='3' and year = #{query.prevYear}) r1 on e.enterprise_code = r1.enterprise_code
        left join (select id, enterprise_code, year from t_report where del_flag = "1" and type="5" and
        report_status='3' and year = #{query.year}) r2 on e.enterprise_code = r2.enterprise_code
        left join t_report_energy_project f1 on f1.report_id = r1.id and f1.project_type= #{projectType} and
        f1.poject_classification="完成项目"
        left join t_report_energy_project f2 on f2.report_id = r2.id and f2.project_type= #{projectType} and
        f2.poject_classification="完成项目"
        <if test="query.id == '71'.toString()">
            where f2.money is not null and f2.money != ''
        </if>
        <if test="query.id == '72'.toString()">
            where f2.money is not null and f2.money != ''
        </if>
        <if test="query.id == '73'.toString()">
            where f2.energy_saving_effect is not null and f2.energy_saving_effect != ''
        </if>
    </sql>
    <sql id="queryCustomRank71-3_cmp">
        SELECT
        e.area,
        dr.meaning,
        e.kind_name,
        IFNULL( e.enterprise_code, '' ) epc,
        IFNULL( e.cmp_name, '' ) name,
        IFNULL( e.staff, '' ) staff,
        IFNULL( e.staff_tel, '' ) staffTel,
        f2.project_type Type2,
        f1.project_type Type1,
        f2.money moneyNum,
        f2.energy_saving_effect savingNum,
        CASE
        WHEN f2.money != ''
        AND f2.money IS NOT NULL THEN
        f1.money ELSE NULL
        END prevMoneyNum,
        CASE
        WHEN f2.money != ''
        AND f2.money IS NOT NULL THEN
        f1.energy_saving_effect ELSE NULL
        END prevSavingNum
        from (
        <include refid="cmp_table">
        </include>
        ) e
        LEFT JOIN dict_region dr ON dr.id = e.area
        LEFT JOIN ( SELECT id, enterprise_code, YEAR FROM t_report WHERE del_flag = "1" AND type = "5" AND report_status
        = '3' AND YEAR = #{query.prevYear} ) r1 ON e.enterprise_code = r1.enterprise_code
        LEFT JOIN ( SELECT id, enterprise_code, YEAR FROM t_report WHERE del_flag = "1" AND type = "5" AND report_status
        = '3' AND YEAR = #{query.year} ) r2 ON e.enterprise_code = r2.enterprise_code
        LEFT JOIN t_report_energy_project f1 ON f1.report_id = r1.id
        AND f1.project_type = #{query.source}
        AND f1.poject_classification = "完成项目"
        LEFT JOIN t_report_energy_project f2 ON f2.report_id = r2.id
        AND f2.project_type = #{query.source}
        AND f2.poject_classification = "完成项目"
        WHERE
        f2.money IS NOT NULL
        AND f2.money != ''
    </sql>
    <select id="queryCustomRank71_1" resultType="java.util.Map">
        select IFNULL(tt.area,'') area,IFNULL( tt.Type2,'') type, IFNULL(tt.benqi,'') benqi,IFNULL( tt.epcNum2, '' )
        benqiepcNum,
        IFNULL(tt.tongqi,'') tongqi,IFNULL( tt.epcNum1, '' ) tongqiepcNum, IFNULL(tt.rate,'')
        rate,IFNULL(tt.rateNum,'') rateNum
        from(
        select t.meaning2 area,
        concat(ROUND(sum(t.num),3),'') benqi, concat(ROUND(sum(t.prevNum),3),'') tongqi,
        ROUND((sum(t.num)/sum(t.prevNum)-1 )*100,2) rate,ROUND((count(Type2)/count(Type1)-1 )*100,2) rateNum,
        count(Type1) epcNum1,
        count(Type2) epcNum2,
        t.Type2
        from(
        select d.id, d.meaning2, e.epc, e.num, e.prevNum,e.Type1,e.Type2
        from (select id, meaning2 from dict_region where pid = "530000" and del_flag = "1") d
        left join (
        <include refid="queryCustomRank71_cmp">
        </include>
        ) e on e.area = d.id
        )t group by t.id
        )tt
        <include refid="change_rate">
        </include>
    </select>
    <select id="queryCustomRank71_2" resultType="java.util.Map">
        select IFNULL(tt.trade,'') trade, IFNULL(tt.benqi,'') benqi,IFNULL( tt.epcNum2, '' ) benqiepcNum,
        IFNULL(tt.tongqi,'') tongqi,IFNULL( tt.epcNum1, '' ) tongqiepcNum, IFNULL(tt.rate,'')
        rate,IFNULL(tt.rateNum,'') rateNum,IFNULL( tt.Type2,'') type
        from(
        select t.name trade,
        concat(ROUND(sum(t.num),3),'') benqi, concat(ROUND(sum(t.prevNum),3),'') tongqi,
        ROUND((sum(t.num)/sum(t.prevNum)-1 )*100,2) rate, ROUND((count(Type2)/count(Type1)-1 )*100,2)
        rateNum,count(Type1) epcNum1,
        count(Type2) epcNum2,
        t.Type2
        from(
        select d.id, d.name, e.kind_name, e.epc, e.num, e.prevNum,e.Type1,e.Type2
        from (select id, name from trade_category where del_flag = "1") d
        left join (
        <include refid="queryCustomRank71_cmp">
        </include>
        ) e on e.kind_name = d.name
        )t group by t.id
        )tt
        <include refid="change_rate">
        </include>
    </select>
    <select id="queryCustomRank71_3" resultType="java.util.Map">
        SELECT
        tt.area,
        tt.meaning,
        tt.kind_name,
        tt.epc,
        tt.name,
        tt.staff,
        tt.staffTel,
        IFNULL( ROUND( sum( tt.moneyNum ), 3 ), '' ) benqiMoney,
        IFNULL( ROUND( sum( tt.prevMoneyNum ), 3 ), '' ) tongqiMoney,
        IFNULL( ROUND(( sum( tt.moneyNum ) / sum( tt.prevMoneyNum ) - 1 )* 100, 2 ), '' ) rateMoney,
        IFNULL( ROUND( sum( tt.savingNum ), 3 ), '' ) benqiSaving,
        IFNULL( ROUND( sum( tt.prevSavingNum ), 3 ), '' ) tongqiSaving,
        IFNULL( ROUND(( sum( tt.savingNum ) / sum( tt.prevSavingNum ) - 1 )* 100, 2 ), '' ) rateSaving,
        count(tt.Type1) tongqiepcNum,
        count(tt.Type2) benqiepcNum,
        IFNULL(ROUND((count( Type2 )/count( Type1 )-1)*100,2 ), '' ) rateNum,
        tt.Type2
        from (
        SELECT
        t.area,
        t.meaning,
        t.kind_name,
        t.epc,
        t.name,
        t.staff,
        t.staffTel,
        t.Type1,
        t.Type2,
        IFNULL( ROUND( t.moneyNum, 3 ), '' ) moneyNum,
        IFNULL( ROUND( t.prevMoneyNum, 3 ), '' ) prevMoneyNum,
        IFNULL( ROUND( t.savingNum, 3 ), '' ) savingNum,
        IFNULL( ROUND( t.prevSavingNum, 3 ), '' ) prevSavingNum
        from(
        <include refid="queryCustomRank71-3_cmp">
        </include>
        )t
        )tt
        GROUP BY tt.name
        <if test="query.low1 != null and query.low1 != '' and query.high1 != null and query.high1 != ''">
            <if test="query.id == 71">
                having rateNum between #{query.low1} and #{query.high1}
            </if>
            <if test="query.id == 72">
                HAVING rateMoney between #{query.low1} and #{query.high1}
            </if>
            <if test="query.id == 73">
                having rateSaving between #{query.low1} and #{query.high1}
            </if>
        </if>
    </select>
    <select id="queryCustomRank71_4" resultType="java.util.Map">
        select
        '合计' area,
        '合计' trade,
        concat( ROUND( sum( t.num ), 3 ), '' ) benqi,
        concat( ROUND( sum( t.prevNum ), 3 ), '' ) tongqi,
        ROUND(( sum( t.num )/ sum( t.prevNum )- 1 )* 100, 2 ) rateMoney,
        ROUND(( count( Type2 )/ count( Type1 )- 1 )* 100, 2 ) rateNum,
        count( Type1 ) tongqiepcNum,
        count( Type2 ) benqiepcNum,
        t.Type2 type
        from(
        <include refid="queryCustomRank71_cmp">
        </include>
        )t
    </select>
    <select id="queryCustomRank21_1" resultType="java.util.Map">
        SELECT
        sum(c.target) as target,
        sum(c.unit_energy_progress_equal) as infactComplete,
        ROUND(sum(c.unit_energy_progress_equal)/sum(c.target)*100,2) as rate,
        ROUND(sum(c.unit_energy_progress_equal)-sum(c.target),2) as overComplete,
        ROUND(sum(c.target)-sum(c.unit_energy_progress_equal),2) as unComplete,
        substring(d.meaning2, 1,4) as area,
        substring(a.region_code, 1,4) as areacode
        FROM(
        select * from t_report_enterprise
        <if test="split != null and split != ''">
            where
            <foreach collection="split" index="index" item="i" separator="or">
                epc_type like concat("%",#{i},"%")
            </foreach>
        </if>
        ) a
        LEFT JOIN t_report b ON a.enterprise_code = b.enterprise_code and a.year= b.year
        LEFT JOIN t_report_rateprogress c on b.id =c.report_id
        LEFT JOIN dict_region d ON substring(a.region_code, 1,4)=substring(d.id, 1,4)
        LEFT JOIN trade_info e on a.trade=e.trade_num
        WHERE 1=1

        <if test="query.area != null and query.area != ''">
            AND #{query.area} LIKE concat( "%", LEFT ( a.region_code, 4 ), "%" )
        </if>
        <if test="query.industry != null and query.industry != ''">
            and #{query.industry} like concat("%",e.kind_name,"%")
        </if>
        and b.del_flag = "1" AND b.type = "4" AND b.report_status = '3'
        <if test="years != null and years != ''">
            and b.year in
            <foreach collection="years" item="items" separator="," open="(" close=")">
                #{items}
            </foreach>
        </if>
        GROUP BY substring(a.region_code, 1,4)
        <if test="query.low1 != null and query.low1 != '' and query.high1 != null and query.high1 != ''">
            having rate between #{query.low1} and #{query.high1}
        </if>
    </select>
    <select id="queryCustomRank21_2" resultType="java.util.Map">
        SELECT
        sum(c.target) as target,
        sum(c.unit_energy_progress_equal) as infactComplete,
        ROUND(sum(c.unit_energy_progress_equal)/sum(c.target)*100,2) as rate,
        ROUND(sum(c.unit_energy_progress_equal)-sum(c.target),2) as overComplete,
        ROUND(sum(c.target)-sum(c.unit_energy_progress_equal),2) as unComplete,
        d.kind_name as trade
        FROM(
        select * from t_report_enterprise
        <if test="split != null and split != ''">
            where
            <foreach collection="split" index="index" item="i" separator="or">
                epc_type like concat("%",#{i},"%")
            </foreach>
        </if>
        ) a
        LEFT JOIN t_report b ON a.enterprise_code = b.enterprise_code and a.year= b.year
        LEFT JOIN t_report_rateprogress c on b.id =c.report_id
        LEFT JOIN trade_info d on a.trade=d.trade_num
        LEFT JOIN dict_region e ON substring(a.region_code, 1,4)=substring(e.id, 1,4)
        WHERE 1=1
        <if test="query.industry != null and query.industry != ''">
            and #{query.industry} like concat("%",d.kind_name,"%")
        </if>
        <if test="query.area != null and query.area != ''">
            AND #{query.area} LIKE concat( "%", LEFT ( a.region_code, 4 ), "%" )
        </if>
        and b.del_flag = "1" AND b.type = "4" AND b.report_status = '3'
        <if test="years != null and years != ''">
            and b.year in
            <foreach collection="years" item="items" separator="," open="(" close=")">
                #{items}
            </foreach>
        </if>
        GROUP BY d.kind_name
        <if test="query.low1 != null and query.low1 != '' and query.high1 != null and query.high1 != ''">
            having rate between #{query.low1} and #{query.high1}
        </if>
    </select>
    <select id="queryCustomRank21_3" resultType="java.util.Map">
        SELECT
        sum(c.target) as target,
        sum(c.unit_energy_progress_equal) as infactComplete,
        ROUND(sum(c.unit_energy_progress_equal)/sum(c.target)*100,2) as rate,
        ROUND(sum(c.unit_energy_progress_equal)-sum(c.target),2) as overComplete,
        ROUND(sum(c.target)-sum(c.unit_energy_progress_equal),2) as unComplete,
        a.enterprise_name,
        a.staff,
        a.staff_tel as staffTel,
        e.kind_name
        FROM(
        select * from t_report_enterprise
        <if test="split != null and split != ''">
            where
            <foreach collection="split" index="index" item="i" separator="or">
                epc_type like concat("%",#{i},"%")
            </foreach>
        </if>
        ) a
        LEFT JOIN t_report b ON a.enterprise_code = b.enterprise_code and a.year= b.year
        LEFT JOIN t_report_rateprogress c on b.id =c.report_id
        LEFT JOIN trade_info e ON a.trade=e.trade_num
        WHERE 1=1
        <if test="query.industry != null and query.industry != ''">
            and #{query.industry} like concat("%",e.kind_name,"%")
        </if>
        <if test="query.area != null and query.area != ''">
            and #{query.area} LIKE concat( "%", LEFT ( a.region_code, 4 ), "%" )
        </if>
        <if test="query.name != null and query.name != ''">
            and a.enterprise_name like concat("%",#{query.name},"%")
        </if>
        and b.del_flag = "1" AND b.type = "4" AND b.report_status = '3'
        <if test="years != null and years != ''">
            and b.year in
            <foreach collection="years" item="items" separator="," open="(" close=")">
                #{items}
            </foreach>
        </if>
        GROUP BY a.enterprise_name
        <if test="query.low1 != null and query.low1 != '' and query.high1 != null and query.high1 != ''">
            having rate between #{query.low1} and #{query.high1}
        </if>
    </select>
    <select id="queryCustomRank22_1" resultType="java.util.Map">
        SELECT
        sum(c.target) as target,
        sum(c.energy_progress_equal) as infactComplete,
        ROUND(sum(c.energy_progress_equal)/sum(c.target)*100,2) as rate,
        ROUND(sum(c.energy_progress_equal)-sum(c.target),2) as overComplete,
        ROUND(sum(c.target)-sum(c.energy_progress_equal),2) as unComplete,
        substring(d.meaning2, 1,4) as area,
        substring(a.region_code, 1,4) as areacode
        FROM(
        select * from t_report_enterprise
        <if test="split != null and split != ''">
            where
            <foreach collection="split" index="index" item="i" separator="or">
                epc_type like concat("%",#{i},"%")
            </foreach>
        </if>
        ) a
        LEFT JOIN t_report b ON a.enterprise_code = b.enterprise_code and a.year= b.year
        LEFT JOIN t_report_rateprogress c on b.id =c.report_id
        LEFT JOIN dict_region d ON substring(a.region_code, 1,4)=substring(d.id, 1,4)
        LEFT JOIN trade_info e on a.trade=e.trade_num
        WHERE 1=1
        <if test="query.area != null and query.area != ''">
            AND #{query.area} LIKE concat( "%", LEFT ( a.region_code, 4 ), "%" )
        </if>
        <if test="query.industry != null and query.industry != ''">
            and #{query.industry} like concat("%",e.kind_name,"%")
        </if>
        and b.del_flag = "1" AND b.type = "4" AND b.report_status = '3'
        <if test="years != null and years != ''">
            and b.year in
            <foreach collection="years" item="items" separator="," open="(" close=")">
                #{items}
            </foreach>
        </if>
        GROUP BY substring(a.region_code, 1,4)
        <if test="query.low1 != null and query.low1 != '' and query.high1 != null and query.high1 != ''">
            having rate between #{query.low1} and #{query.high1}
        </if>
    </select>
    <select id="queryCustomRank22_2" resultType="java.util.Map">
        SELECT
        sum(c.target) as 'target',
        sum(c.energy_progress_equal) as 'infactComplete',
        ROUND(sum(c.energy_progress_equal)/sum(c.target)*100,2) as rate,
        ROUND(sum(c.energy_progress_equal)-sum(c.target),2) as overComplete,
        ROUND(sum(c.target)-sum(c.energy_progress_equal),2) as unComplete,
        d.kind_name as trade
        FROM(
        select * from t_report_enterprise
        <if test="split != null and split != ''">
            where
            <foreach collection="split" index="index" item="i" separator="or">
                epc_type like concat("%",#{i},"%")
            </foreach>
        </if>
        ) a
        LEFT JOIN t_report b ON a.enterprise_code = b.enterprise_code and a.year= b.year
        LEFT JOIN t_report_rateprogress c on b.id =c.report_id
        LEFT JOIN trade_info d on a.trade=d.trade_num
        LEFT JOIN dict_region e ON substring(a.region_code, 1,4)=substring(e.id, 1,4)
        WHERE 1=1
        <if test="query.industry != null and query.industry != ''">
            and #{query.industry} like concat("%",d.kind_name,"%")
        </if>
        <if test="query.area != null and query.area != ''">
            AND #{query.area} LIKE concat( "%", LEFT ( a.region_code, 4 ), "%" )
        </if>
        and b.del_flag = "1" AND b.type = "4" AND b.report_status = '3'
        <if test="years != null and years != ''">
            and b.year in
            <foreach collection="years" item="items" separator="," open="(" close=")">
                #{items}
            </foreach>
        </if>
        GROUP BY d.kind_name
        <if test="query.low1 != null and query.low1 != '' and query.high1 != null and query.high1 != ''">
            having rate between #{query.low1} and #{query.high1}
        </if>
    </select>
    <select id="queryCustomRank22_3" resultType="java.util.Map">
        SELECT
        sum(c.target) as target,
        sum(c.energy_progress_equal) as infactComplete,
        ROUND(sum(c.energy_progress_equal)/sum(c.target)*100,2) as rate,
        ROUND(sum(c.energy_progress_equal)-sum(c.target),2) as overComplete,
        ROUND(sum(c.target)-sum(c.energy_progress_equal),2) as unComplete,
        a.enterprise_name,
        a.staff,
        a.staff_tel as staffTel
        FROM(
        select * from t_report_enterprise
        <if test="split != null and split != ''">
            where
            <foreach collection="split" index="index" item="i" separator="or">
                epc_type like concat("%",#{i},"%")
            </foreach>
        </if>
        ) a
        LEFT JOIN t_report b ON a.enterprise_code = b.enterprise_code and a.year= b.year
        LEFT JOIN t_report_rateprogress c on b.id =c.report_id
        LEFT JOIN trade_info e ON a.trade=e.trade_num
        WHERE 1=1
        <if test="query.industry != null and query.industry != ''">
            and #{query.industry} like concat("%",e.kind_name,"%")
        </if>
        <if test="query.area != null and query.area != ''">
            and #{query.area} LIKE concat( "%", LEFT ( a.region_code, 4 ), "%" )
        </if>
        <if test="query.name != null and query.name != ''">
            and a.enterprise_name like concat("%",#{query.name},"%")
        </if>
        and b.del_flag = "1" AND b.type = "4" AND b.report_status = '3'
        <if test="years != null and years != ''">
            and b.year in
            <foreach collection="years" item="items" separator="," open="(" close=")">
                #{items}
            </foreach>
        </if>
        GROUP BY a.enterprise_name
        <if test="query.low1 != null and query.low1 != '' and query.high1 != null and query.high1 != ''">
            having rate between #{query.low1} and #{query.high1}
        </if>
    </select>
    <select id="listByAuditorJudge" resultType="com.nantian.cloud.jljc.entity.TReport">
        select <include refid="Base_Column_List"></include>
        from t_report
        where enterprise_code = #{query.enterpriseCode}
        and year = #{query.year}
        and del_flag = "1"
        and (audit_status = '4' or audit_status2 = '4' or audit_status3 = '4')
    </select>
    <select id="getEnergyData" resultType="com.nantian.cloud.jljc.vo.comAnalysis.EnergyInfoVo">
         select distinct e.id, e.cmp_name cmpName, e.enterprise_code enterpriseCode, e.trade_code trade, e.name, e.area,
         -- 综合能耗当量值 万tce
         case when t2_1.id is not null then (select round((production_consumption-conversion_output_ref-recycling)/10000,2) from t_report_energy_out where del_flag = '1' and  report_id = t2_1.id and energy_code_name = '合计当量值') else (select round(consume_industry/10000,4) from t_report_energys_tructure where del_flag = '1' and  report_id = t2.id and energy_code_name = '合计当量值') end tcdDl,
         -- 综合能耗等价值 万tce
         case when t2_1.id is not null then (select round((production_consumption-conversion_output_ref-recycling)/10000,2) from t_report_energy_out where del_flag = '1' and  report_id = t2_1.id and energy_code_name = '合计等价值') else (select round(consume_industry/10000,4) from t_report_energys_tructure where del_flag = '1' and  report_id = t2.id and energy_code_name = '合计等价值') end tcdDj,
         -- 电力 万千瓦时
         (select consume_industry from t_report_energys_tructure where del_flag = '1' and report_id = t2.id and energy_code_name = '电力') ele,
         -- 煤耗 吨
         (select sum(consume_industry) from t_report_energys_tructure where del_flag = '1' and report_id = t2.id and energy_code_name in ("一般烟煤","褐煤","洗精煤","无烟煤","炼焦烟煤","其它洗煤","煤油","煤制品","煤矸石用于燃料")) coal,
         -- 工业总产值（当年价） 亿元
         (select distinct round(production_sum/10000,2) from t_report_energys_tructure where del_flag = '1' and report_id = t2.id and energy_code_name = '合计等价值') yieldDn,
         -- 工业总产值（基期可比价） 亿元
         (select distinct round(industrial_yield/10000,2) from t_report_financial where del_flag = '1' and report_id = t1.id ) yieldJq,
         -- concat('产品',p.num,'：',p.product_name) productName,
         null yieldIn, p.product_name productName,
         concat(round(p.capacity_num,2),p.capacity_unit) capacity, concat(round(p.yield_num,2),p.yield_unit) yield, case when p.energy_consumption!=0 then round(p.energy_consumption,2) else '' end energy, concat(round(p.unit_energy_consumption,2),p.unit_energy_consumption_unit) unitEnergy
         from tmp_info e
         left join t_report t1 on e.enterprise_code = t1.enterprise_code and t1.del_flag = '1' and t1.year = '2021' and t1.type = '1'
         left join t_report t2 on e.enterprise_code = t2.enterprise_code and t2.del_flag = '1' and t2.year = '2021' and t2.type = '2'
         left join t_report t2_1 on e.enterprise_code = t2_1.enterprise_code and t2_1.del_flag = '1' and t2_1.year = '2021' and t2_1.type = '2-1'
         -- left join t_report_product p on p.report_id = t1.id and p.del_flag = '1'
         left join (
            select distinct t.id, p1.report_id, p1.id pid, count(*) num, p1.product_name, p1.capacity_num, p1.capacity_unit, p1.yield_num,p1.yield_unit,p1.energy_consumption,p1.unit_energy_consumption,p1.unit_energy_consumption_unit
            from t_report t
            left join t_report_product p1 on p1.report_id = t.id
            left join t_report_product p2 on p2.report_id = p1.report_id and p2.id >= p1.id
            where t.del_flag = '1' and t.year = '2021' and t.type = '1' and p1.del_flag = '1' group by p1.report_id, p1.id
         ) p on p.report_id = t1.id
         order by e.id, p.num asc
    </select>


    <select id="getReturnDetail" resultType="java.util.Map">
        select IFNULL(reportName,'') reportName, IFNULL(remark,'') remark, IFNULL(dept,'') dept , IFNULL(auditDate,'') auditDate from(
            select case type when '1' then '报表1-1基本情况表' when '2' then '报表1-2能源消费结构表' when '2-1' then '报表1-2能源消费结构表附表' when '3' then '报表1-3单位产品综合能耗情况表'
            when '3-1' then '报表1-3单位产品综合能耗情况表附表' when '4' then '报表1-4进度节能量目标完成情况表' else '报表1-5节能改造项目情况表' end reportName,
            case when audit_status = '4' then audit_remark  when audit_status2 = '4' then audit_remark2  else audit_remark3 end remark,
            case when audit_status = '4' then "区县"  when audit_status2 = '4' then "州市" else "省级" end dept,
            case when audit_status = '4' then audit_date  when audit_status2 = '4' then audit_date2 else audit_date3 end auditDate
            from t_report
            where enterprise_code = #{epc} and year = #{year}
            and del_flag = '1' and report_status = '2' and (audit_status = '4' or audit_status2 = '4' or audit_status3 = '4')
        )t
    </select>
</mapper>
