<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.nantian.cloud.dbcapp.mapper.record.EnergyMapper">


    <select id="getInfo2019" resultType="java.lang.Float">
        SELECT IFNULL(sum(round(guideline_val/10000,2)),0) FROM t_unitenergy2019 where record_circle BETWEEN #{startTime} and #{endTime} and
        energyTypeCode='0000' and enterprise_id in
        <foreach collection="companyList" item="items" separator="," open="(" close=")">
            #{items}
        </foreach>
        and del_flag=0
    </select>

    <select id="getInfo2020" resultType="java.lang.Float">
        SELECT IFNULL(sum(round(guideline_val/10000,2)),0) from t_unitenergy2020 where enterprise_id in
        <foreach collection="companyList" item="items" separator="," open="(" close=")">
            #{items}
        </foreach>
        and record_circle BETWEEN #{startTime} and #{endTime} and energyTypeCode = '0000' and code='00000000'
        and guideline_val &lt; 1000000 and guideline_val &gt; 0 and isRight = "1"
    </select>

    <!--2019综合能耗-->
    <select id="getInfo2019ByEntList" resultType="java.lang.Float">
        SELECT sum(guideline_val) from t_unitenergy2019
        where energyTypeCode ='0000'
        <if test="companyList!=null">
            and unitid in
            <foreach collection="companyList" item="items" separator="," open="(" close=")">
                #{items}
            </foreach>
        </if>
        AND record_circle BETWEEN #{startTime} and #{endTime}
    </select>

    <!--煤消耗-->
    <select id="foundCoalByMonth" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
        SELECT
        sum(a.StandardValue)/10000 as value,
        a.record_circle as month
        FROM
        t_unitenergy2019 a
        LEFT JOIN history_monitorpoint b ON a.CODE = b.CODE
        WHERE 1=1
        <if test="list !=null">
            and a.unitId in
            <foreach collection="list" item="items" separator="," open="(" close=")">
                #{items}
            </foreach>
        </if>
        AND b.type = 'JZNH'
        AND a.energyName LIKE '%煤%'
        and a.record_circle BETWEEN #{startTime} and #{endTime}
        GROUP BY a.record_circle
    </select>


    <!--工业水消耗-->
    <select id="foundWaterByMonth" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
        SELECT
        sum(a.StandardValue) as value,
        a.record_circle as month
        FROM
        t_unitenergy2019 a
        LEFT JOIN history_monitorpoint b ON a.CODE = b.CODE
        WHERE 1=1

        <if test="list !=null">
            and a.unitId in
            <foreach collection="list" item="items" separator="," open="(" close=")">
                #{items}
            </foreach>
        </if>
        AND b.type = 'JZNH'
        AND a.energyName LIKE '%水'
        and a.record_circle BETWEEN #{startTime} and #{endTime}
        GROUP BY a.record_circle
    </select>

    <!--煤-->
    <select id="getTotalMcoal" resultType="java.lang.Double">
        SELECT sum(guideline_val) FROM t_unitenergy where record_circle BETWEEN #{startTime} and #{endTime} and
        energyTypeCode='0100' and enterprise_id in
        <foreach collection="companyList" item="items" separator="," open="(" close=")">
            #{items}
        </foreach>
        and del_flag=0
    </select>
    <select id="getTotalCoke" resultType="java.lang.Double">
        SELECT sum(guideline_val) FROM t_unitenergy where record_circle BETWEEN #{startTime} and #{endTime} and
        energyTypeCode='0900' and enterprise_id in
        <foreach collection="companyList" item="items" separator="," open="(" close=")">
            #{items}
        </foreach>
        and del_flag=0
    </select>
    <select id="getTotalPower" resultType="java.lang.Double">
        SELECT sum(guideline_val)/100000000 FROM t_unitenergy where record_circle BETWEEN #{startTime} and #{endTime}
        and energyTypeCode='3300' and enterprise_id in
        <foreach collection="companyList" item="items" separator="," open="(" close=")">
            #{items}
        </foreach>
        and del_flag=0
    </select>
    <select id="getTotalNaturalGas" resultType="java.lang.Double">
        SELECT sum(guideline_val) FROM t_unitenergy where record_circle BETWEEN #{startTime} and #{endTime} and
        energyTypeCode='1500' and enterprise_id in
        <foreach collection="companyList" item="items" separator="," open="(" close=")">
            #{items}
        </foreach>
        and del_flag=0
    </select>
    <select id="getTotalPetroleum" resultType="java.lang.Double">
        SELECT sum(guideline_val) FROM t_unitenergy where record_circle BETWEEN #{startTime} and #{endTime} and
        energyTypeCode='2900' and enterprise_id in
        <foreach collection="companyList" item="items" separator="," open="(" close=")">
            #{items}
        </foreach>
        and del_flag=0
    </select>
    <select id="getTotalHeat" resultType="java.lang.Double">
        SELECT sum(guideline_val) FROM t_unitenergy where record_circle BETWEEN #{startTime} and #{endTime} and
        energyTypeCode='3200' and enterprise_id in
        <foreach collection="companyList" item="items" separator="," open="(" close=")">
            #{items}
        </foreach>
        and del_flag=0
    </select>
    <select id="getTotalOther" resultType="java.lang.Double">
        SELECT sum(guideline_val) FROM t_unitenergy where record_circle BETWEEN #{startTime} and #{endTime} and
        energyTypeCode !='0100' and energyTypeCode !='0000'
        and energyTypeCode !='0900'
        and energyTypeCode !='3300' and energyTypeCode !='1500'
        and energyTypeCode !='2900' and energyTypeCode !='3200' and enterprise_id in
        <foreach collection="companyList" item="items" separator="," open="(" close=")">
            #{items}
        </foreach>
        and del_flag=0
    </select>

    <!-- 2020年企业数据-->
    <select id="getAllCompanyList2020" resultType="java.lang.String">
        select enterpriseCode from enterprise_info where regionCode like CONCAT(#{code}, '%') and reportNum like '%已完成%'
        <if test="inde!=null">
            and tid=#{inde}
        </if>
    </select>

    <!-- 2019年企业数据-->
    <select id="getAllCompanyList2019" resultType="java.lang.String">
        select enterprise_code from t_enterprise_old where 1=1
        <if test="code!=null">
            and region_code like CONCAT(#{code}, '%')
        </if>

        <if test="inde1!=null">
            and industryCode= #{inde1}
        </if>

        <if test="cmpName!=null">
            and enterprise_name like CONCAT('%',#{cmpName}, '%')
        </if>
    </select>

    <!-- 查询全部-->
    <select id="getCompanyByArea" resultType="com.nantian.cloud.dbcapp.vo.CompanyVO">
      SELECT count(b.code) as value,b.meaning as name FROM t_enterprise_old a LEFT JOIN dict_region b on a.region_code =b.code  GROUP  BY b.code  ORDER BY COUNT(b.code) desc
   </select>

    <!-- 查询全部州市-->
    <select id="getCompanyByAreaZS" resultType="com.nantian.cloud.dbcapp.vo.CompanyVO">
        SELECT
            count(a.id) AS VALUE,
          b.TNAME as name
        FROM
          dict_region b left  JOIN dict_region c on b.code=c.pcode
            LEFT JOIN t_enterprise_old a ON a.region_code=c.code

            where b.pcode=530000

        GROUP BY
            b.code

        ORDER BY
	COUNT(a.id) desc
   </select>

    <!-- 查询全部州市-->
    <select id="getCompanyByAreaZS2020" resultType="com.nantian.cloud.dbcapp.vo.CompanyVO">
        SELECT
            count(a.id) AS VALUE,
          b.TNAME as name
        FROM
          dict_region b left  JOIN dict_region c on b.code=c.pcode
            LEFT JOIN enterprise_info a ON a.regionCode=c.code and a.reportNum like '%已完成%'
            where b.pcode=530000
        GROUP BY
            b.code
        ORDER BY
	COUNT(a.id) desc
   </select>

    <!-- 查询全部区县-->
    <select id="getCompanyByAreaQX" resultType="com.nantian.cloud.dbcapp.vo.CompanyVO">
       SELECT
            count(a.id ) AS VALUE,
            b.tname AS NAME
        FROM
            dict_region b
            LEFT JOIN t_enterprise_old a  ON b.CODE  =  a.region_code
            where b.pcode=#{code}
        GROUP BY
            b.CODE
        ORDER BY
            COUNT( b.CODE ) DESC
   </select>

    <!-- 查询全部区县-->
    <select id="getCompanyByAreaQX2020" resultType="com.nantian.cloud.dbcapp.vo.CompanyVO">
       SELECT
            count(a.id ) AS VALUE,
            b.tname AS NAME
        FROM
            dict_region b
            LEFT JOIN enterprise_info a  ON b.CODE  =  a.regionCode and a.reportNum like '%已完成%'
            where b.pcode=#{code}
        GROUP BY
            b.CODE
        ORDER BY
            COUNT( b.CODE ) DESC
   </select>

    <!-- 查询全部区县-->
    <select id="getCompanyByAreaAllQX" resultType="com.nantian.cloud.dbcapp.vo.CompanyVO">
        	SELECT
                count(a.id ) AS VALUE,
                b.tname AS NAME
            FROM
                dict_region b
                LEFT JOIN t_enterprise_old a  ON b.CODE  =  a.region_code
                where b.pcode=#{code}
                and a.industryCode=#{inde1}
            GROUP BY
                b.CODE
            ORDER BY
                COUNT( b.CODE ) DESC
   </select>

    <!-- 查询全部区县-->
    <select id="getCompanyByAreaAllQX2020" resultType="com.nantian.cloud.dbcapp.vo.CompanyVO">
        	SELECT
                count(a.id ) AS VALUE,
                b.tname AS NAME
            FROM
                dict_region b
                LEFT JOIN enterprise_info a  ON b.CODE  =  a.regionCode and a.reportNum like '%已完成%'
                where b.pcode=#{code}
                and a.tid=#{inde1}
            GROUP BY
                b.CODE
            ORDER BY
                COUNT( b.CODE ) DESC
   </select>

    <!-- 查询全部-->
    <select id="getCompanyByAreaAll" resultType="com.nantian.cloud.dbcapp.vo.CompanyVO">
        SELECT
            count(a.id) AS VALUE,
          b.TNAME as name
        FROM
          dict_region b left  JOIN dict_region c on b.code=c.pcode
            LEFT JOIN t_enterprise_old a ON a.region_code=c.code

            where b.pcode=530000F
            and a.industryCode=#{industry}
        GROUP BY
            b.code
        ORDER BY
            COUNT(a.id) desc
   </select>

    <!-- 查询全部-->
    <select id="getCompanyByAreaAll2020" resultType="com.nantian.cloud.dbcapp.vo.CompanyVO">
        SELECT
            count(a.id) AS VALUE,
          b.TNAME as name
        FROM
          dict_region b left  JOIN dict_region c on b.code=c.pcode
            LEFT JOIN enterprise_info a ON a.regionCode=c.code and a.reportNum like '%已完成%'
            where b.pcode=530000
            and a.tid=#{industry}
        GROUP BY
            b.code
        ORDER BY
            COUNT(a.id) desc
   </select>

    <!-- 查询所有的行业名称-->
    <select id="findAllIndustry" resultType="java.lang.String">
     select name from temp_dict_name GROUP BY name
   </select>

    <!-- 查询所有的行业名称-->
    <select id="findAllIndustry1" resultType="java.lang.String">
     select tid from dict_industry_code GROUP BY tname
   </select>

    <!-- 查询所有的行业带id-->
    <select id="findAllInd" resultType="com.nantian.cloud.dbcapp.vo.AreaVO">
     select tname as name , tid as code from dict_industry_code GROUP BY tname
   </select>

    <!-- 查询所有的行州市-->
    <select id="findAllArea" resultType="com.nantian.cloud.dbcapp.vo.AreaVO">
     SELECT tname as name ,code as  code   FROM dict_region where pcode=#{code}
   </select>

    <!-- 查询区县-->
    <select id="findAllArea1" resultType="com.nantian.cloud.dbcapp.vo.AreaVO">
     SELECT tname as name ,code as  code   FROM dict_region where code=#{code}
   </select>

    <!-- 通过行业查询综合能耗-->
    <select id="foundEnergyByInd" resultType="java.lang.Float">
     select tname from dict_industry_code GROUP BY tname
   </select>


    <!--<select id="getTotal" resultType="com.nantian.cloud.dbcappp.vo.EnergyVo">
       SELECT entId as id ,enterprise_code as enterpriseCode  FROM t_enterprise_old
    </select>-->


    <select id="selectExceptionInfo" resultType="com.nantian.cloud.dbcapp.vo.ExceptionInfoVo">
        <if test="isForce == null or isForce == ''">
        select tmp.enterpriseCode, tmp.dataCode, convertRation, dataValue, statDate, tmp.collectItemName, enterpriseName, tmp.id,
        tmp.remark, exceptionGov, operateDate, exceptionType exceptionTypeTmp, n.statType, handlerType, handlerReason,
        case substr(tmp.dataCode,9,2) when '01' then d1.qualityUnit when '02' then d1.qualityUnit when '03' then d2.qualityUnit
        when '04' then (select distinct produceUnit from company_materiel_product where enterpriseCode = tmp.enterpriseCode and produceNo = substr(tmp.dataCode,11,4) ) else '' end enterpriseUnit,
        case substr(tmp.dataCode,9,2) when '01' then d1.collectUnit when '02' then d1.collectUnit when '03' then d2.collectUnit else '' end collectUnit,
        (select GROUP_CONCAT(DISTINCT exception_name) from quality_exception_code where tmp.exceptionType like concat('%',exception_code,'%')) exceptionType
        from (
        select DISTINCT a.enterpriseCode, a.dataCode, a.convertRation, a.dataValue, a.exceptionType, a.statDate, a.handlerType, a.handlerReason,
        a.collectItemName, b.cmpName enterpriseName, a.id, a.remark, a.exceptionGov, a.operateDate
        from enterprise_info b
        left join energy_exception_data_info a on a.enterpriseCode = b.enterpriseCode
        where b.regionCode like concat(#{area},'%') and handlerType != 0 and left(a.statDate,7) between #{preTime} and #{endTime}
        <if test="cmpName != null and cmpName != ''">
            and (b.enterpriseCode like concat('%',#{cmpName},'%') or b.cmpName like concat('%',#{cmpName},'%'))
        </if>
        <if test="type != null and type != ''">
            <choose>
                <when test="type == '1'.toString()">
                    and exceptionType like '%0101%'
                </when>
                <otherwise>
                    and exceptionType not like '%0101%'
                </otherwise>
            </choose>
        </if>
        <if test="collectName != null and collectName!=''">
            and (collectItemName like concat('%',#{collectName},'%') or dataCode like concat('%',#{collectName},'%'))
        </if>
        <if test="state != null and state != ''">
            and exceptionGov = #{state}
        </if>
        -- order by a.enterpriseCode, a.dataCode, a.statDate desc
        limit #{page},#{size}
        )tmp left join dict_energy_type_unit_collect d1 on d1.code = substr(tmp.dataCode,11,4)
        left join dict_energy_substance d2 on d2.code = substr(tmp.dataCode,11,4)
        left join configure_data_collect_new n on n.enterpriseCode = tmp.enterpriseCode and n.dataCode = tmp.dataCode
        </if>

        <if test="isForce != null and isForce != ''">
        select tmp.enterpriseCode, tmp.dataCode, convertRation, dataValue, statDate, tmp.collectItemName, enterpriseName, tmp.id,
        tmp.remark, exceptionGov, operateDate, exceptionType exceptionTypeTmp, n.statType, handlerType, handlerReason,
        case substr(tmp.dataCode,9,2) when '01' then d1.qualityUnit when '02' then d1.qualityUnit when '03' then d2.qualityUnit
        when '04' then (select distinct produceUnit from company_materiel_product where enterpriseCode = tmp.enterpriseCode and produceNo = substr(tmp.dataCode,11,4) ) else '' end enterpriseUnit,
        case substr(tmp.dataCode,9,2) when '01' then d1.collectUnit when '02' then d1.collectUnit when '03' then d2.collectUnit else '' end collectUnit,
        (select GROUP_CONCAT(DISTINCT exception_name) from quality_exception_code where tmp.exceptionType like concat('%',exception_code,'%')) exceptionType
        from (
        select DISTINCT a.enterpriseCode, a.dataCode, a.convertRation, a.dataValue, a.exceptionType, a.statDate, a.handlerType, a.handlerReason,
        a.collectItemName, b.cmpName enterpriseName, a.id, a.remark, a.exceptionGov, a.operateDate
        from enterprise_info b
        left join energy_exception_data_info a on a.enterpriseCode = b.enterpriseCode
        where b.regionCode like concat(#{area},'%') and handlerType != 0 and left(a.statDate,7) between #{preTime} and #{endTime}
        <if test="cmpName != null and cmpName != ''">
            and (b.enterpriseCode like concat('%',#{cmpName},'%') or b.cmpName like concat('%',#{cmpName},'%'))
        </if>
        <if test="type != null and type != ''">
            <choose>
                <when test="type == '1'.toString()">
                    and exceptionType like '%0101%'
                </when>
                <otherwise>
                    and exceptionType not like '%0101%'
                </otherwise>
            </choose>
        </if>
        <if test="collectName != null and collectName!=''">
            and collectItemName like concat('%',#{collectName},'%')
        </if>
        <if test="state != null and state != ''">
            and exceptionGov = #{state}
        </if>
        -- order by a.enterpriseCode, a.dataCode, a.statDate desc

        )tmp left join dict_energy_type_unit_collect d1 on d1.code = substr(tmp.dataCode,11,4)
        left join dict_energy_substance d2 on d2.code = substr(tmp.dataCode,11,4)
        left join configure_data_collect_new n on n.enterpriseCode = tmp.enterpriseCode and n.dataCode = tmp.dataCode
            where n.isForce = #{isForce}
            limit #{page},#{size}
        </if>
    </select>



    <select id="selectExceptionInfoCount" resultType="java.lang.Integer">
        select count(1)
        from enterprise_info b
        left join energy_exception_data_info a on a.enterpriseCode = b.enterpriseCode
        where b.regionCode like concat(#{area},'%') and handlerType != 0  and left(a.statDate,7) between #{preTime}
        and #{endTime}
        <if test="cmpName != null and cmpName != ''">
            and (b.enterpriseCode like concat('%',#{cmpName},'%') or b.cmpName like concat('%',#{cmpName},'%'))
        </if>
        <if test="type != null and type != ''">
            <choose>
                <when test="type == '1'.toString()">
                    and exceptionType like '%0101%'
                </when>
                <otherwise>
                    and exceptionType not like '%0101%'
                </otherwise>
            </choose>
        </if>
        <if test="collectName != null and collectName!=''">
            and (collectItemName like concat('%',#{collectName},'%') or dataCode like concat('%',#{collectName},'%'))
        </if>
        <if test="state != null and state != ''">
            and exceptionGov = #{state}
        </if>
    </select>

    <select id="selectExceptionIndex" resultType="com.nantian.cloud.dbcapp.vo.ExceptionInfoVo">

        select enterpriseName, enterpriseCode, dataCode, collectItemName, enterpriseUnit, dataValue, statDate, remark,
        operateDate, exceptionType exceptionTypeTmp, energyLevel energyLevelTmp,
        (select GROUP_CONCAT(DISTINCT exception_name) from quality_exception_code where tmp.exceptionType like
        concat('%',exception_code,'%')) exceptionType,
        case energyConsumeLevel when '1' then '5000吨以下' when '2' then '5000~10000吨' when '3' then '1万-10万吨' when '4'
        then '10万-50万吨' when '5' then '50万-300万吨' when '6' then '300万吨以上' else '' end energyLevel,
        case when exceptionType in ('0207') then concat(datumValue,enterpriseUnit) else '' end powerDatumValue,
        case when exceptionType in ('0210','0211') then concat(datumValue,enterpriseUnit) else '' end limitDatumValue
        from(
        select b.cmpName enterpriseName, b.energyConsumeLevel, a.enterpriseCode, a.dataCode, a.quotaName
        collectItemName, a.quotaUnit enterpriseUnit, a.dataValue, a.exceptionType, a.statDate, a.datumValue,
        a.energyLevel, a.remark, a.operateDate
        from enterprise_info b
        left join energy_exception_result_info a on a.enterpriseCode = b.enterpriseCode
        where b.regionCode like concat(#{area},'%') and left(a.statDate,7) between #{preTime} and #{endTime}
        <if test="cmpName != null and cmpName != ''">
            and (b.enterpriseCode like concat('%',#{cmpName},'%') or b.cmpName like concat('%',#{cmpName},'%'))
        </if>
        <if test="type != null and type != ''">
            <choose>
                <when test="type == '3'.toString()">
                    and exceptionType like '0207'
                </when>
                <when test="type == '4'.toString()">
                    and exceptionType in ('0208','0209')
                </when>
                <otherwise>
                    and exceptionType in ('0210','0211')
                </otherwise>
            </choose>
        </if>
        <if test="collectName != null and collectName!=''">
            and quotaName like concat('%',#{collectName},'%')
        </if>
        order by enterpriseCode, statDate, quotaName desc
        limit #{page},#{size}
        )tmp
    </select>

    <select id="selectExceptionIndexCount" resultType="java.lang.Integer">
        select count(1)
        from enterprise_info b
        left join energy_exception_result_info a on a.enterpriseCode = b.enterpriseCode
        where b.regionCode like concat(#{area},'%') and left(a.statDate,7) between #{preTime} and #{endTime}
        <if test="cmpName != null and cmpName != ''">
            and (b.enterpriseCode like concat('%',#{cmpName},'%') or b.cmpName like concat('%',#{cmpName},'%'))
        </if>
        <if test="type != null and type != ''">
            <choose>
                <when test="type == '3'.toString()">
                    and exceptionType like '0207'
                </when>
                <when test="type == '4'.toString()">
                    and exceptionType in ('0208','0209')
                </when>
                <otherwise>
                    and exceptionType in ('0210','0211')
                </otherwise>
            </choose>
        </if>
        <if test="collectName != null and collectName!=''">
            and quotaName like concat('%',#{collectName},'%')
        </if>
    </select>

    <!--2019年综合能耗-->
    <select id="getTceByArea" resultType="java.lang.Float">
        select sum(guideline_val)/10000 as value from t_unitenergy a LEFT JOIN t_enterprise_old b on a.enterprise_id =b.enterprise_code
        LEFT JOIN dict_region c on b.region_code =c.code
       where a.energyTypeCode='0000' and a.record_circle between #{startTime} and #{endTime} AND b.region_code like concat(#{code},"%")
   </select>

    <!--2020年综合能耗-->
    <select id="getTceByArea1" resultType="java.lang.Float">
        SELECT
        sum( guideline_val )/10000 AS VALUE
        FROM
        t_unitenergy2020 a
        LEFT JOIN enterprise_info b ON a.enterprise_id = b.enterpriseCode
        WHERE
        a.energyTypeCode = '0000' and a.`code`='00000000' and a.guideline_val  &lt; 1000000 and a.guideline_val  >0
        AND a.record_circle between #{startTime} and #{endTime} AND b.regionCode like concat(#{code},"%")
       and isRight = "1"
   </select>

    <!--2020年综合能耗 通过行业-->
    <select id="getTceByIndustry" resultType="java.lang.Float">
        SELECT
        sum( guideline_val )/10000 AS VALUE
        FROM
        t_unitenergy2020 a
        LEFT JOIN enterprise_info b ON a.enterprise_id = b.enterpriseCode
        WHERE
        a.energyTypeCode = '0000' and a.`code`='00000000' and a.guideline_val  &lt; 1000000 and a.guideline_val  >0
        AND a.record_circle between #{startTime} and #{endTime} AND b.industryCode =#{tid}
        and isRight = "1"
   </select>

    <!--2019年综合能耗 通过行业-->
    <select id="getTceByIndustry1" resultType="java.lang.Float">
        SELECT
        sum( guideline_val )/10000 AS VALUE
        FROM
        t_unitenergy a
       LEFT JOIN t_enterprise_old b on a.enterprise_id =b.enterprise_code
        where a.energyTypeCode='0000'
        AND a.record_circle between #{startTime} and #{endTime} AND b.industryCode =#{tid}

   </select>

    <!--通过信用代码的到entID-->
    <select id="getentIdByEnterpriseCode" resultType="java.lang.Integer">
        SELECT entId  FROM t_enterprise_old where 1=1
        <if test="companyList !=null">
            and enterprise_code in
            <foreach collection="companyList" item="items" separator="," open="(" close=")">
                #{items}
            </foreach>
        </if>

    </select>


    <!--查询实物量 -->
    <select id="getTotalMaterialByCode" resultType="com.nantian.cloud.dbcapp.vo.KeyValueVO">
        SELECT
        sum(StandardValue)/10000 as value,
        energyName AS key1
        FROM
        t_unitenergy2019
        WHERE entid=UnitID
        <if test="companyList1 !=null">
            and unitId in
            <foreach collection="companyList1" item="items" separator="," open="(" close=")">
                #{items}
            </foreach>
        </if>
        AND code like 'JZNH%'
        and record_circle BETWEEN #{startTime} and #{endTime}
        GROUP BY energyName
        HAVING VALUE >1
        ORDER BY VALUE desc
    </select>
    <select id="selectCompanyListByAreaAndIndustry" resultType="string">
        select distinct enterpriseCode from enterprise_info
        <where>
            and reportNum like '%已完成%'
            <if test="area!=null">
                and regionCode like #{area}
            </if>
            <if test="industry!=null">
                and tid = #{industry}
            </if>
            <if test="cmpName!=null">
                and (cmpName like concat('%',#{cmpName},'%') or enterpriseCode like concat('%',#{cmpName},'%'))
            </if>
        </where>
    </select>

    <select id="selectCompanyListByAreaAndIndustry2019" resultType="string">
        select enterprise_code from t_enterprise_old
        <where>
            <if test="area!=null">
                region_code like #{area}
            </if>
            <if test="industry!=null">
                and industryCode = #{industry}
            </if>
            <if test="cmpName!=null">
                and (enterprise_name like concat('%',#{cmpName},'%') or enterprise_code like concat('%',#{cmpName},'%'))
            </if>
        </where>
    </select>

    <select id="getTotalMcoal2020" resultType="double">
        select sum(guideline_val)/10000 from t_unitenergy where enterprise_id in
        <foreach collection="list" separator="," item="data" open="(" close=")">
            #{data}
        </foreach>
        and energyTypeCode in ("0102","0103","0104","0105")
        and `code` = "00000000" and record_circle between #{startTime} and #{endTime}
    </select>

    <select id="getTotalOther2020" resultType="double">
        select sum(guideline_val)/10000 from t_unitenergy where enterprise_id in
        <foreach collection="list" separator="," item="data" open="(" close=")">
            #{data}
        </foreach>
        and energyTypeCode not in ("0102","0103","0104","0105","0000","0900","3300","1500","2900","3200")
        and `code` = "00000000" and record_circle between #{startTime} and #{endTime}
    </select>

    <select id="getTotalCoke2020" resultType="double">
        select sum(guideline_val)/10000 from t_unitenergy where enterprise_id in
        <foreach collection="list" separator="," item="data" open="(" close=")">
            #{data}
        </foreach>
        and energyTypeCode = "0900"
        and `code` = "00000000" and record_circle between #{startTime} and #{endTime}
    </select>

    <select id="getTotalPower2020" resultType="double">
        select sum(guideline_val)/10000 from t_unitenergy where enterprise_id in
        <foreach collection="list" separator="," item="data" open="(" close=")">
            #{data}
        </foreach>
        and energyTypeCode = "3300"
        and `code` = "00000000" and record_circle between #{startTime} and #{endTime}
        and guideline_val >0 and guideline_val &lt;100000
    </select>

    <select id="getTotalNaturalGas2020" resultType="double">
        select sum(guideline_val)/10000 from t_unitenergy where enterprise_id in
        <foreach collection="list" separator="," item="data" open="(" close=")">
            #{data}
        </foreach>
        and energyTypeCode = '1500'
        and `code` = "00000000" and record_circle between #{startTime} and #{endTime}
    </select>

    <select id="getTotalPetroleum2020" resultType="double">
        select sum(guideline_val)/10000 from t_unitenergy where enterprise_id in
        <foreach collection="list" separator="," item="data" open="(" close=")">
            #{data}
        </foreach>
        and energyTypeCode = '2900'
        and `code` = "00000000" and record_circle between #{startTime} and #{endTime}
    </select>

    <select id="getTotalHeat2020" resultType="double">
        select sum(guideline_val)/10000 from t_unitenergy where enterprise_id in
        <foreach collection="list" separator="," item="data" open="(" close=")">
            #{data}
        </foreach>
        and energyTypeCode = '3200'
        and `code` = "00000000" and record_circle between #{startTime} and #{endTime}
    </select>

    <select id="foundCoalByMonth2020" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
        select sum(guideline_val)/10000 as `value` ,record_circle as `month` from t_unitenergy2020 where enterprise_id
        in
        <foreach collection="list" separator="," item="data" open="(" close=")">
            #{data}
        </foreach>
        and energyTypeCode in('0102','0103','0104','0105','0100')
        and `code` = "00000000" and record_circle between #{startTime} and #{endTime}
        and isRight = "1" and guideline_val > 0 and energy_type_code in ('01','02')
        group by record_circle
    </select>

    <select id="foundWaterByMonth2020" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
        select sum(StandardValue)/10000 as `value` ,record_circle as `month` from t_unitenergy2020
        where enterprise_id in
        <foreach collection="list" separator="," item="data" open="(" close=")">
            #{data}
        </foreach>
        and energyTypeCode in (select distinct code from dict_energy_substance where code like '02%')
        and energy_type_code = '03' and `code` = "00000000" and isRight = "1" and StandardValue between 0 and 10000000
        and record_circle between #{startTime} and #{endTime}
        group by record_circle
    </select>

    <select id="selectToolByAreaAndIndustry" resultType="int">
        select count(id) from configure_gauge where enterpriseCode in
        (
        select distinct enterpriseCode from enterprise_info a where a.regionCode like #{area}
        <if test="industry!=null">
            and a.tid = #{industry}
        </if>
        )
        and del_flag = "1"  and meter_level in (1,2,3)
    </select>

    <select id="selectToolByAreaAndIndustry2019" resultType="int">
        select count(id) from history_instrument where entid in
        <foreach collection="companyList2019" open="(" separator="," close=")" item="data">
            #{data}
        </foreach>
    </select>


    <select id="selectEnergyKinds" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
        select distinct a.energyTypeCode as `month`, b.meaning as `value`
        from t_unitenergy2020 a join dict_energy_type_unit_collect b on a.energyTypeCode = b.id
        where a.enterprise_id in
        <foreach collection="list" item="data" open="(" separator="," close=")">
            #{data}
        </foreach>
        and a.energy_type_code in ('01','02') and a.energyTypeCode != '0000' and a.code = '00000000'
    </select>

    <select id="selectAllEnergyNumByKinds" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
        select sum(guideline_val)/10000 as `value` ,energyTypeCode as `month` from t_unitenergy2020 where (enterprise_id, record_circle)
        in(
            SELECT distinct enterprise_id, record_circle
            FROM t_unitenergy2020 where energyTypeCode="0000" and `code` = "00000000" and record_circle between #{startTime} and #{endTime}
            and enterprise_id in (
                SELECT enterpriseCode FROM enterprise_info WHERE regionCode LIKE concat(#{area},'%') and reportNum like '%已完成%'
                <if test="industry!=null"> and tid = #{industry}</if>
            ) and guideline_val>0 and guideline_val &lt;1000000
        )
        and `code` = "00000000" and record_circle between #{startTime} and #{endTime} and energy_type_code in ('01','02') and energyTypeCode != '0000'
        -- and guideline_val>0 and guideline_val &lt;1000000 and isRight = "1"
        group by energyTypeCode having value is not null
    </select>

    <!--区县名称-->
    <select id="getCountyName" resultType="com.nantian.cloud.dbcapp.vo.KeyValueVO">
       select tname as key1 ,code as value from dict_region where pcode like concat(#{code},'00%')
    </select>

    <select id="getCompanyListEnergyInfo2020" resultType="com.nantian.cloud.dbcapp.vo.CompanyEnergyInfoVo">
        select b.enterpriseCode,b.cmpName as enterpriseName,round(sum(a.guideline_val),4) as energyValue
        from t_unitenergy2020 as a join enterprise_info as b on
        a.enterprise_id = b.enterpriseCode
        where a.enterprise_ID in
        <foreach collection="list" open="(" separator="," close=")" item="data">
            #{data}
        </foreach>
        and a.record_circle between #{startTime} and #{endTime}
        and a.code = "00000000" and a.energyTypeCode = "0000"
        and isRight = "1"
        and a.guideline_val >=0 and a.guideline_val &lt;1000000
        group by a.enterprise_id order by energyValue desc
    </select>
    <select id="getCompanyListEnergyInfo2019" resultType="com.nantian.cloud.dbcapp.vo.CompanyEnergyInfoVo">
        SELECT
        t1.enterpriseCode as enterpriseCode,
        t1.tce as energyValue,
        t1.name as enterpriseName ,
        t2.total as collectNum
        FROM
        ( SELECT
        sum( a.guideline_val ) as tce,
        unitId as entid,
        b.enterprise_name as name,
        b.enterprise_code as enterpriseCode
        FROM
        t_unitenergy2019 a LEFT JOIN t_enterprise_old b on a.unitId =b.entid
        WHERE
        a.unitId IN
        <foreach collection="companyList2019" open="(" separator="," close=")" item="data">
            #{data}
        </foreach>
        AND a.record_circle BETWEEN #{startTime} and #{endTime}
        AND a.CODE LIKE 'ZHNH%'
        GROUP BY a.unitId ) t1
        INNER JOIN
        (
        SELECT
        count(id) as total,
        entID as entID
        FROM
        history_instrument
        WHERE
        entID IN
        <foreach collection="companyList2019" open="(" separator="," close=")" item="data">
            #{data}
        </foreach>
        GROUP BY entID
        ) t2 ON t1.entid=t2.entId

    </select>

    <select id="selectCollectNum" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
        select count(dataCode) as `value` , enterpriseCode as `month` from configure_data_collect where
        enterpriseCode in
        <foreach collection="list" open="(" separator="," close=")" item="index">
            #{index}
        </foreach>
        group by enterpriseCode
    </select>

    <select id="selectGetEnergyByAreaList" resultType="com.nantian.cloud.dbcapp.vo.AreaAndIndustryVo">
       select sum(a.guideline_val) as energyValue,c.tname as name,count(b.enterpriseCode) from t_unitenergy2020 a left join
       enterprise_info b on a.enterprise_id = b.enterpriseCode left join dict_region c on
       b.region = c.code
       where
          c.pcode = #{area} group by c.pcode
    </select>

    <select id="selectAddressName" resultType="com.nantian.cloud.dbcapp.vo.AreaVO">
          select tname as `name` , code   from dict_region where id like #{s}  and SUBSTR(code,5,2) != '00'  order by  `code` asc
    </select>

    <select id="getCompanyNum00" resultType="com.nantian.cloud.dbcapp.vo.CompanyVO">
        select count(a.enterpriseCode) as `value`,b.tname as `name` from enterprise_info a left join dict_region b on
        a.regionCode = b.code
        <where>
            a.regionCode like #{area} and a.reportNum like '%已完成%'
            <if test="industry!=null">
                and a.tid = #{industry}
            </if>
        </where>
        group by b.tname
    </select>

    <select id="getCompanyList" resultType="com.nantian.cloud.dbcapp.vo.CompanyListVo">
        SELECT DISTINCT t.enterpriseCode AS enterpriseCode, t.companyName AS companyName, t.regionCode as dept_id
        FROM (
        SELECT t.enterprise_code AS enterpriseCode, enterprise_name AS companyName, region_code as regionCode
        FROM t_enterprise_old t
        WHERE 1 = 1
        <if test="cmpName!=null">
            AND (enterprise_name LIKE concat ("%",#{cmpName},"%") or t.enterprise_code LIKE concat ("%",#{cmpName},"%")
            )
        </if>
        UNION ALL
        SELECT d.enterpriseCode AS enterpriseCode, cmpName AS companyName, regionCode
        FROM enterprise_info d WHERE 1 = 1
        <if test="cmpName!=null">
            AND (cmpName LIKE concat ("%",#{cmpName},"%") or d.enterpriseCode LIKE concat ("%",#{cmpName},"%"))
        </if>
        UNION ALL
        SELECT enterprise_code, cmp_name, right(region_code,6) FROM access_enterprise_info
        WHERE enterprise_code in (SELECT DISTINCT enterprise_code FROM access_energys_tructure)
        <if test="cmpName!=null">
            AND (cmp_name LIKE concat ("%",#{cmpName},"%") or enterprise_code LIKE concat ("%",#{cmpName},"%"))
        </if>
        ) t
    </select>


    <select id="selectCompanyListByAreaAndIndustryElectricity" resultType="string">
        select DISTINCT enterpriseCode from (
        SELECT a.enterpriseCode FROM electric_enterprise_info a
        where 1=1
        <if test="areaName !=null">
            and a.regionCode LIKE #{areaName}
        </if>
        <if test="industryName!=null">
            and a.industry_code = #{industryName}
        </if>
        <if test="cmpName!=null">
            and cmpName like #{cmpName}
        </if>

        union all
        SELECT d.enterpriseCode FROM enterprise_info d
        where 1=1
        <if test="areaName !=null">
            and d.regionCode like #{areaName}
        </if>
        <if test="industryName!=null">
            and d.tid =#{industryName}
        </if>
        <if test="cmpName!=null">
            and cmpName like #{cmpName}
        </if>
        ) t

    </select>


    <select id="countAllLogs" resultType="int">
        select count(id) from enterprise_energy_data where enterpriseCode in
        <foreach collection="list" open="(" separator="," close=")" item="item">
            #{item}
        </foreach>
        and statDate between #{startTime} and #{endTime}
    </select>


    <select id="sumAllElectricityValue" resultType="int">
        select count(id) from company_data_detail where enterpriseCode in
        <foreach collection="list" open="(" separator="," close=")" item="item">
            #{item}
        </foreach>
        and time between #{startTime} and #{endTime}
        AND `data` between 0 and 1000000000 and statType = "1" and `time` between #{startTime} and #{endTime}
    </select>

    <select id="selectExceptionNum" resultType="int">
        select count(id) from energy_exception_data_info where enterpriseCode in
        <foreach collection="list" open="(" separator="," close=")" item="item">
            #{item}
        </foreach>
        and statDate between #{startTime} and #{endTime}
    </select>

    <select id="selectUpdateInfoCollect" resultType="com.nantian.cloud.dbcapp.vo.UpdataInfoBo">
        select count(distinct a.id) as logsNum ,count(distinct a.enterpriseCode) as cmpNum from enterprise_backup_data a
        left join enterprise_info b on a.enterpriseCode = b.enterpriseCode
        <where>
            <if test="areaName!=null">
                b.regionCode like #{areaName}
            </if>
            <if test="industryName!=null">
                and b.tid = #{industryName}
            </if>
            <if test="cmpName!=null">
                and b.cmpName like #{cmpName}
            </if>
            <if test="s!=null">
                and a.channelType = #{s}
            </if>
            <if test="o!=null">
                and a.uploadCompleted = #{o}
            </if>
            and a.statDate between #{startTime} and #{endTime}
        </where>
    </select>


    <select id="selectUpdateInfoElectricity" resultType="com.nantian.cloud.dbcapp.vo.UpdataInfoBo">
        select count(distinct a.id) as logsNum ,count(distinct a.enterpriseCode) as cmpNum from enterprise_backup_data a
        left join electric_enterprise_info b on a.enterpriseCode = b.enterpriseCode
        <where>
            <if test="areaName!=null">
                b.regionCode like #{areaName}
            </if>
            <if test="industryName!=null">
                and b.industry_code = #{industryName}
            </if>
            <if test="cmpName!=null">
                and b.cmpName like #{cmpName}
            </if>
            <if test="s!=null">
                and a.channelType = #{s}
            </if>
            <if test="o!=null">
                and a.uploadCompleted = #{o}
            </if>
            and a.statDate between #{startTime} and #{endTime}
        </where>
    </select>

    <select id="selectupDateCountryNum" resultType="int">
        select count(distinct a.enterpriseCode) as cmpNum from enterprise_backup_data a
        left join electric_enterprise_info b on a.enterpriseCode = b.enterpriseCode
        <where>
            <if test="areaName!=null">
                b.regionCode like #{areaName}
            </if>
            <if test="industryName!=null">
                and b.industry_code = #{industryName}
            </if>
            <if test="cmpName!=null">
                and b.cmpName like #{cmpName}
            </if>
            <if test="s!=null">
                and a.channelType = #{s}
            </if>
            <if test="o!=null">
                and a.uploadCompleted = #{o}
            </if>
            and a.statDate = #{statDate}
        </where>
    </select>

    <select id="getAllGxByCode" resultType="com.nantian.cloud.dbcapp.vo.NameValueVO">
        	SELECT processName as name , processCode as value FROM configure_process WHERE enterpriseCode = #{enterpriseCode } order by processCode asc
    </select>

    <select id="getValueByProcedure" resultType="com.nantian.cloud.dbcapp.vo.ProcedureVO">
           select dataCode as configureCode , collectItemName as configureName,isEnable  from configure_data_collect where
           enterpriseCode = #{enterpriseCode} and SUBSTRING(dataCode,1,2) = #{baseCode}
    </select>

    <select id="getValueByNewProcedure" resultType="com.nantian.cloud.dbcapp.vo.ProcedureVO">
        select distinct c.dataCode as configureCode, c.collectItemName as configureName, c.isEnable,
        (select meaning from dict_energy_use where code = substr(c.dataCode,15,2)) energyUseType,
        case c.statType when '1' then '日' when '2' then '月' when '3' then '年' else '' end collectType,
        case substr(c.dataCode,9,2) when '01' then d1.qualityUnit when '02' then d1.qualityUnit when '03' then
        d2.qualityUnit when '04' then (select distinct produceUnit from company_materiel_product where enterpriseCode =
        #{enterpriseCode} and produceNo = substr(c.dataCode,11,4) ) else '' end unit,
        case substr(c.dataCode,9,2) when '01' then d1.collectUnit when '02' then d1.collectUnit when '03' then
        d2.collectUnit else '' end collectUnit,
        case substr(c.dataCode,9,2) when '01' then d1.meaning when '02' then d1.meaning when '03' then d2.meaning else
        (select meaning from dict_collect_data_type where code = substr(c.dataCode,9,2)) end type,
        case left(c.dataCode,2) when '00' then '全厂' else (select processName from configure_process where enterpriseCode
        = #{enterpriseCode} and processCode = left(c.dataCode,2)) end name
        from configure_data_collect c
        left join dict_energy_type_unit_collect d1 on d1.code = substr(c.dataCode,11,4)
        left join dict_energy_substance d2 on d2.code = substr(c.dataCode,11,4)
        where c.enterpriseCode = #{enterpriseCode}
        <if test="baseCode!=null and baseCode!=''">
            and left(c.dataCode,2) = #{baseCode}
        </if>
        order by c.dataCode ASC
    </select>

    <select id="countCollectAndEle" resultType="int">
        select count(*) from enterprise_energy_data where enterpriseCode in
        (SELECT enterpriseCode FROM enterprise_info WHERE regionCode LIKE concat(#{area},'%')
        <if test="industry!=null">
            and tid = #{industry}
        </if>)
        union all
        select count(*) from company_data_detail where enterpriseCode in
        (SELECT enterpriseCode FROM electric_enterprise_info WHERE regionCode LIKE concat(#{area},'%')
        <if test="industry!=null">
            and industry_code = #{industry}
        </if>)
    </select>

    <select id="selectAllInfos" resultType="com.nantian.cloud.dbcapp.vo.UpdataInfoBo">
        select count(distinct a.id) as logsNum ,count(distinct a.enterpriseCode) as cmpNum from enterprise_backup_data a
        left join enterprise_info b on a.enterpriseCode = b.enterpriseCode
        <where>
            <if test="areaName!=null">
                b.regionCode like #{areaName}
            </if>
            <if test="industryName!=null">
                and b.tid = #{industryName}
            </if>
            <if test="cmpName!=null">
                and b.cmpName like #{cmpName}
            </if>
            and a.channelType = "1"
        </where>
        union all
        select count(distinct a.id) as logsNum ,count(distinct a.enterpriseCode) as cmpNum from enterprise_backup_data a
        left join enterprise_info b on a.enterpriseCode = b.enterpriseCode
        <where>
            <if test="areaName!=null">
                b.regionCode like #{areaName}
            </if>
            <if test="industryName!=null">
                and b.tid = #{industryName}
            </if>
            <if test="cmpName!=null">
                and b.cmpName like #{cmpName}
            </if>

            and a.channelType = "1" and a.uploadCompleted = "成功"
        </where>
        union all
        select count(distinct a.id) as logsNum ,count(distinct a.enterpriseCode) as cmpNum from enterprise_backup_data a
        left join electric_enterprise_info b on a.enterpriseCode = b.enterpriseCode
        <where>
            <if test="areaName!=null">
                b.regionCode like #{areaName}
            </if>
            <if test="industryName!=null">
                and b.industry_code = #{industryName}
            </if>
            <if test="cmpName!=null">
                and b.cmpName like #{cmpName}
            </if>
            and a.channelType != "1"
        </where>
        union all
        select count(distinct a.id) as logsNum ,count(distinct a.enterpriseCode) as cmpNum from enterprise_backup_data a
        left join electric_enterprise_info b on a.enterpriseCode = b.enterpriseCode
        <where>
            <if test="areaName!=null">
                b.regionCode like #{areaName}
            </if>
            <if test="industryName!=null">
                and b.industry_code = #{industryName}
            </if>
            <if test="cmpName!=null">
                and b.cmpName like #{cmpName}
            </if>
            and a.channelType != "1" and a.uploadCompleted = "成功"

        </where>
        union all
        select count(distinct a.id) as logsNum ,count(distinct a.enterpriseCode) as cmpNum from enterprise_backup_data a
        left join enterprise_info b on a.enterpriseCode = b.enterpriseCode
        <where>
            <if test="areaName!=null">
                b.regionCode like #{areaName}
            </if>
            <if test="industryName!=null">
                and b.tid = #{industryName}
            </if>
            <if test="cmpName!=null">
                and b.cmpName like #{cmpName}
            </if>
            and a.channelType = "1" and a.uploadCompleted = "成功"
            and a.uploadDate = (select max(uploadDate) from enterprise_backup_data)
        </where>
        union all
        select count(distinct a.id) as logsNum ,count(distinct a.enterpriseCode) as cmpNum from enterprise_backup_data a
        left join electric_enterprise_info b on a.enterpriseCode = b.enterpriseCode
        <where>
            <if test="areaName!=null">
                b.regionCode like #{areaName}
            </if>
            <if test="industryName!=null">
                and b.industry_code = #{industryName}
            </if>
            <if test="cmpName!=null">
                and b.cmpName like #{cmpName}
            </if>
            and a.channelType != "1" and a.uploadCompleted = "成功"
            and a.uploadDate = (select max(uploadDate) from enterprise_backup_data)
        </where>
    </select>

    <sql id="Old_Exception_Data_Info">
        <choose>
            <when test="type=='1'.toString() or type=='2'.toString()">
                select count(1) num, enterpriseCode, left(statDate,7) `month`, case when exceptionType like '%0101%'
                then '1' else '2' end exceptionType
                from energy_exception_data_info where left(statDate,7) between #{preTime} and #{endTime} and handlerType != 0
                group by enterpriseCode, `month`, exceptionType
            </when>
            <when test="type=='3'.toString() or type=='4'.toString() or type=='5'.toString()">
                select count(1) num, enterpriseCode, left(statDate,7) `month`, case when exceptionType like '0207' then
                '3' when exceptionType in ('0208','0209') then '4' else '5' end exceptionType
                from energy_exception_result_info where left(statDate,7) between #{preTime} and #{endTime}
                group by enterpriseCode, `month`, exceptionType
            </when>
            <otherwise>
                select count(1) num, enterpriseCode, left(statDate,7) `month`, case when exceptionType like '%0101%'
                then '1' else '2' end exceptionType
                from energy_exception_data_info where left(statDate,7) between #{preTime} and #{endTime} and handlerType != 0
                group by enterpriseCode, `month`, exceptionType
                union all
                select count(1) num, enterpriseCode, left(statDate,7) `month`, case when exceptionType like '0207' then
                '3' when exceptionType in ('0208','0209') then '4' else '5' end exceptionType
                from energy_exception_result_info where left(statDate,7) between #{preTime} and #{endTime}
                group by enterpriseCode, `month`, exceptionType
            </otherwise>
        </choose>
    </sql>

    <sql id="Exception_Data_Info">
         select sum(`num`) num, epc enterpriseCode, statDate `month`, exceptionType
         from enterprise_data_all where statDate between #{preTime} and #{endTime} and type = '4'
         group by enterpriseCode, `month`, exceptionType
         union all
         select count(1) num, enterpriseCode, left(statDate,7) `month`, case when exceptionType like '0207' then '3' when exceptionType in ('0208','0209') then '4' else '5' end exceptionType
         from energy_exception_result_info where left(statDate,7) between #{preTime} and #{endTime}
         group by enterpriseCode, `month`, exceptionType
    </sql>

    <sql id="Exception_Enterprise_Info">
        select e.regionCode, e.cmpName, e.enterpriseCode, (select distinct popular_industry from data_quality_status t1, quality_industry t2 where t1.quality_industry = t2.code and t1.enterprise_code = e.enterpriseCode limit 0,1) industry from enterprise_info e
    </sql>

    <select id="selectExceptionInfoList" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
        select `month`, sum(a.num) as `value`, sum(case when a.exceptionType = '1' then a.num end) value1, sum(case when a.exceptionType = '2' then a.num end) value2,
        sum(case when a.exceptionType = '3' then a.num end) value3, sum(case when a.exceptionType = '4' then a.num end) value4, sum(case when a.exceptionType = '5' then a.num end) value5
        from (
        <include refid="Exception_Data_Info"/>
        ) a left join (
        <include refid="Exception_Enterprise_Info"/>
        ) b on a.enterpriseCode = b.enterpriseCode
        where b.regionCode like #{area}
        <if test="cmpName != null and cmpName != ''">
            and (b.enterpriseCode like concat('%',#{cmpName},'%') or b.cmpName like concat('%',#{cmpName},'%'))
        </if>
        <if test="industry != null and industry != ''">
            and b.industry like concat('%',industry,'%')
        </if>
        <if test="type != null and type!=''">
            and #{type} like concat('%',a.exceptionType,'%')
        </if>
        group by `month`
    </select>

    <select id="selectExceptionInOrder" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
        select a.exceptionType, sum(a.num) as `value`, (select distinct SUBSTRING_INDEX(sort_name,',',-1) from quality_exception_code where sort_name like concat(a.exceptionType,'%')) `month`
        from (
        <include refid="Exception_Data_Info"/>
        ) a left join (
        <include refid="Exception_Enterprise_Info"/>
        ) b on a.enterpriseCode = b.enterpriseCode
        where b.regionCode like concat('%',#{area},'%')
        <if test="cmpName != null and cmpName != ''">
            and (b.enterpriseCode like concat('%',#{cmpName},'%') or b.cmpName like concat('%',#{cmpName},'%'))
        </if>
        <if test="industry != null and industry != ''">
            and b.industry like concat('%',industry,'%')
        </if>
        <if test="type != null and type!=''">
            and #{type} like concat('%',a.exceptionType,'%')
        </if>
        group by a.exceptionType
    </select>

    <select id="selectExceptionAreaOrder" resultType="com.nantian.cloud.dbcapp.vo.AreaAndIndustryVo">
        select o.tname name, IFNULL(sum(a.num),0) as energyValue, sum(case when a.exceptionType = '1' then a.num end) value1, sum(case when a.exceptionType = '2' then a.num end) value2,
        sum(case when a.exceptionType = '3' then a.num end) value3, sum(case when a.exceptionType = '4' then a.num end) value4, sum(case when a.exceptionType = '5' then a.num end) value5
        from dict_region o left join (
        <include refid="Exception_Enterprise_Info"/>
        ) b on
        <choose>
            <when test="area.length == 2">
                left(b.regionCode,4) = left(o.code,4)
            </when>
            <otherwise>
                b.regionCode = o.code
            </otherwise>
        </choose>
        <if test="cmpName != null and cmpName != ''">
            and (b.enterpriseCode like concat('%',#{cmpName},'%') or b.cmpName like concat('%',#{cmpName},'%'))
        </if>
        <if test="industry != null and industry != ''">
            and b.industry like concat('%',industry,'%')
        </if>
        left join(
            <include refid="Exception_Data_Info"/>
        ) a on a.enterpriseCode = b.enterpriseCode and b.regionCode like concat('%',#{area},'%')
        where 1=1
        <choose>
            <when test="area == '53'.toString()">
                and o.pcode = '530000'
            </when>
            <when test="area.length == 4">
                and o.pcode like concat('%',#{area},'%')
            </when>
            <otherwise>
                and o.code like concat('%',#{area},'%')
            </otherwise>
        </choose>
        <if test="type != null and type!=''">
            and #{type} like concat('%',a.exceptionType,'%')
        </if>
        group by o.code order by energyValue desc
    </select>

    <select id="selectExceptionByIndustryOrder" resultType="com.nantian.cloud.dbcapp.vo.AreaAndIndustryVo">
        select IFNULL(b.industry,'其他') name, IFNULL(sum(a.num),0) as energyValue, sum(case when a.exceptionType = '1' then a.num end) value1, sum(case when a.exceptionType = '2' then a.num end) value2,
        sum(case when a.exceptionType = '3' then a.num end) value3, sum(case when a.exceptionType = '4' then a.num end) value4, sum(case when a.exceptionType = '5' then a.num end) value5
        from (
        <include refid="Exception_Data_Info"/>
        ) a  left join (
        <include refid="Exception_Enterprise_Info"/>
        ) b on a.enterpriseCode = b.enterpriseCode
        where b.regionCode like concat('%',#{area},'%')
        <if test="cmpName != null and cmpName != ''">
            and (b.enterpriseCode like concat('%',#{cmpName},'%') or b.cmpName like concat('%',#{cmpName},'%'))
        </if>
        <if test="industry != null and industry != ''">
            and b.industry like concat('%',industry,'%')
        </if>
        <if test="type != null and type!=''">
            and #{type} like concat('%',a.exceptionType,'%')
        </if>
        group by b.industry order by energyValue desc
    </select>

    <select id="selectExceptionEnterpriseInfo" resultType="com.nantian.cloud.dbcapp.vo.EnterpriseExceptionVo">
        select a.enterpriseCode epcCode, sum(a.num) as `value`, b.cmpName `month`, sum(case when a.exceptionType = '1' then a.num end) value1, sum(case when a.exceptionType = '2' then a.num end) value2,
        sum(case when a.exceptionType = '3' then a.num end) value3, sum(case when a.exceptionType = '4' then a.num end) value4, sum(case when a.exceptionType = '5' then a.num end) value5
        from (
        <include refid="Exception_Data_Info"/>
        ) a left join (
        <include refid="Exception_Enterprise_Info"/>
        ) b on a.enterpriseCode = b.enterpriseCode
        where b.regionCode like concat('%',#{area},'%')
        <if test="cmpName != null and cmpName != ''">
            and (b.enterpriseCode like concat('%',#{cmpName},'%') or b.cmpName like concat('%',#{cmpName},'%'))
        </if>
        <if test="industry != null and industry != ''">
            and b.industry like concat('%',industry,'%')
        </if>
        <if test="type != null and type!=''">
            and #{type} like concat('%',a.exceptionType,'%')
        </if>
        group by a.enterpriseCode order by ${orderFiled} desc
    </select>

    <select id="getExceptionToal" resultType="java.lang.Integer">
        SELECT count(1) from energy_exception_data_info where enterpriseCode in
        <foreach collection="companyList" item="items" separator="," open="(" close=")">
            #{items}
        </foreach>
        AND statDate BETWEEN #{startTime} and #{endTime}
        <if test="type !=null">
            and exceptionType = #{type}
        </if>
    </select>


    <select id="getEpcName" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
        select cmpName as `value`,enterpriseCode as `month` from enterprise_info where
        enterpriseCode in
        <foreach collection="list" item="data" open="(" separator="," close=")">
            #{data}
        </foreach>
    </select>

    <select id="selectMaxSuccess" resultType="int">
        select count(distinct enterpriseCode) from upload_country_enterprise_info
        <where>
            regionCode
            like concat(#{areaName},"%")
            <if test="industryName!=null">
                and tid = #{industryName}
            </if>
            and uploadCompleted = '1'
        </where>
    </select>

    <select id="selectCompanyListByAreaAndIndustryAndCode" resultType="string">
        select distinct enterpriseCode from enterprise_info
        <where>
            <if test="areaName!=null">
                regionCode like #{areaName}
            </if>
            <if test="industryName!=null">
                and tid = #{industryName}
            </if>
            <if test="enterpriseCode!=null">
                and enterpriseCode = #{enterpriseCode}
            </if>
        </where>
    </select>

    <select id="countCollectNUmsByArea" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
        select count(a.id) as `value` from enterprise_energy_data a left join enterprise_info b on
        a.enterpriseCode = b.enterpriseCode
        <where>
            b.regionCode like concat(#{areaCode},'%')
            <if test="industryCode!=null">
                and b.tid = #{industryCode}
            </if>
            <if test="endTime!=null and preTime != null">
                and a.statDate between #{preTime} and #{endTime}
            </if>
        </where>
    </select>

    <select id="selectAllIndustry" resultType="com.nantian.cloud.dbcapp.vo.AreaVO">
        select code ,name from temp_dict_name ;
    </select>

    <select id="countByMonth" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
        select count(*) as `value` , SUBSTR(statDate,1,7) as `month` from enterprise_energy_data a
        <where>
            a.statDate like concat(#{year},'%')
            and a.enterpriseCode in (SELECT DISTINCT enterpriseCode FROM enterprise_info
            WHERE regionCode LIKE concat(#{areaCode},'%')
            <if test="industryCode!=null">and tid = #{industryCode}
            </if>)
        </where>
        group by `month`
    </select>

    <select id="countByDay" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
        select count(*) as `value` , SUBSTR(statDate,1,10) as `month` from enterprise_energy_data a
        <where>
            a.statDate like concat(#{month},'%')
            and a.enterpriseCode in (SELECT DISTINCT enterpriseCode FROM enterprise_info
            WHERE regionCode LIKE concat(#{areaCode},'%')
            <if test="industryCode!=null">and tid = #{industryCode}
            </if>)
        </where>
        group by `month`
    </select>

    <select id="countUpCollectNUmsByArea" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
        select count(a.id) as `value` from enterprise_backup_data a left join upload_country_enterprise_info b on
        a.enterpriseCode = b.enterpriseCode
        <where>
            b.regionCode like concat(#{areaCode},'%')
            <if test="industryCode!=null">
                and b.tid = #{industryCode}
            </if>
            <if test="endTime!=null and preTime != null">
                and a.uploadDate between #{preTime} and #{endTime}
            </if>
        </where>
    </select>

    <select id="UpCountByMonth" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
        select count(a.id) as `value` , SUBSTR(a.uploadDate,1,7) as `month` from enterprise_backup_data a left join
        upload_country_enterprise_info b on
        a.enterpriseCode = b.enterpriseCode
        <where>
            b.regionCode like concat(#{areaCode},'%')
            <if test="industryCode!=null">
                and b.tid = #{industryCode}
            </if>
            <if test="year!=null">
                and a.uploadDate like concat(#{year},'%')
            </if>
        </where>
        group by `month`
    </select>

    <select id="UpCountByMonthNew" resultType="com.nantian.cloud.dbcapp.vo.CoalVoNew">
        select
        sum(case when a.uploadCompleted='成功' then 1 else 0 end) as normalUploadSuccess ,
        sum(case when a.uploadCompleted='失败' then 1 else 0 end) as normalUploadFailed ,
        sum(case when a.uploadCompleted='补传成功' then 1 else 0 end) as repairUploadSuccess ,
        sum(case when a.uploadCompleted='补传失败' then 1 else 0 end) as repairUploadFailed ,
        SUBSTR(a.uploadDate,1,7) as `month` from enterprise_backup_data a left join
        upload_country_enterprise_info b on
        a.enterpriseCode = b.enterpriseCode
        <where>
            b.regionCode like concat(#{areaCode},'%')
            <if test="industryCode!=null">
                and b.tid = #{industryCode}
            </if>
            <if test="year!=null">
                and a.uploadDate like concat(#{year},'%')
            </if>
        </where>
        group by `month`
    </select>

    <select id="UpCountByDay" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
        select count(a.id) as `value` , a.uploadDate as `month` from enterprise_backup_data a left join
        upload_country_enterprise_info b on
        a.enterpriseCode = b.enterpriseCode
        <where>
            b.regionCode like concat(#{areaCode},'%')
            <if test="industryCode!=null">
                and b.tid = #{industryCode}
            </if>
            <if test="month!=null">
                and a.uploadDate like concat(#{month},'%')
            </if>
        </where>
        group by `month`
    </select>

    <select id="UpCountByDayNew" resultType="com.nantian.cloud.dbcapp.vo.CoalVoNew">
        select
        sum(case when a.uploadCompleted='成功' then 1 else 0 end) as normalUploadSuccess ,
        sum(case when a.uploadCompleted='失败' then 1 else 0 end) as normalUploadFailed ,
        sum(case when a.uploadCompleted='补传成功' then 1 else 0 end) as repairUploadSuccess ,
        sum(case when a.uploadCompleted='补传失败' then 1 else 0 end) as repairUploadFailed ,
        a.uploadDate as `month` from enterprise_backup_data a left join
        upload_country_enterprise_info b on
        a.enterpriseCode = b.enterpriseCode
        <where>
            b.regionCode like concat(#{areaCode},'%')
            <if test="industryCode!=null">
                and b.tid = #{industryCode}
            </if>
            <if test="month!=null">
                and a.uploadDate like concat(#{month},'%')
            </if>
        </where>
        group by `month`
    </select>

    <select id="UpCountByDayEpc" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
        select count(distinct a.enterpriseCode) as `value` , a.uploadDate as `month` from enterprise_backup_data a left
        join upload_country_enterprise_info b on
        a.enterpriseCode = b.enterpriseCode
        <where>
            b.regionCode like concat(#{areaCode},'%')
            <if test="industryCode!=null">
                and b.tid = #{industryCode}
            </if>
            <if test="month!=null">
                and a.uploadDate like concat(#{month},'%')
            </if>
            and a.uploadCompleted = '成功'
        </where>
        group by `month`
    </select>



    <select id="UpCountByDayEpcNormalUploadSuccess" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
        select count(distinct a.enterpriseCode) as `value` , a.uploadDate as `month` from enterprise_backup_data a left
        join upload_country_enterprise_info b on
        a.enterpriseCode = b.enterpriseCode
        <where>
            b.regionCode like concat(#{areaCode},'%')
            <if test="industryCode!=null">
                and b.tid = #{industryCode}
            </if>
            <if test="month!=null">
                and a.uploadDate like concat(#{month},'%')
            </if>
            and a.uploadCompleted = '成功'
        </where>
        group by `month`
    </select>

    <select id="UpCountByDayEpcNormalUploadFailed" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
        select count(distinct a.enterpriseCode) as `value` , a.uploadDate as `month` from enterprise_backup_data a left
        join upload_country_enterprise_info b on
        a.enterpriseCode = b.enterpriseCode
        <where>
            b.regionCode like concat(#{areaCode},'%')
            <if test="industryCode!=null">
                and b.tid = #{industryCode}
            </if>
            <if test="month!=null">
                and a.uploadDate like concat(#{month},'%')
            </if>
            and a.uploadCompleted = '失败'
        </where>
        group by `month`
    </select>

    <select id="UpCountByDayEpcRepairUploadSuccess" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
        select count(distinct a.enterpriseCode) as `value` , a.uploadDate as `month` from enterprise_backup_data a left
        join upload_country_enterprise_info b on
        a.enterpriseCode = b.enterpriseCode
        <where>
            b.regionCode like concat(#{areaCode},'%')
            <if test="industryCode!=null">
                and b.tid = #{industryCode}
            </if>
            <if test="month!=null">
                and a.uploadDate like concat(#{month},'%')
            </if>
            and a.uploadCompleted = '补传成功'
        </where>
        group by `month`
    </select>

    <select id="UpCountByDayEpcRepairUploadFailed" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
        select count(distinct a.enterpriseCode) as `value` , a.uploadDate as `month` from enterprise_backup_data a left
        join upload_country_enterprise_info b on
        a.enterpriseCode = b.enterpriseCode
        <where>
            b.regionCode like concat(#{areaCode},'%')
            <if test="industryCode!=null">
                and b.tid = #{industryCode}
            </if>
            <if test="month!=null">
                and a.uploadDate like concat(#{month},'%')
            </if>
            and a.uploadCompleted = '补传失败'
        </where>
        group by `month`
    </select>


    <select id="selectEnergyByAreaAndIndustry" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
        SELECT sum(round(a.guideline_val/10000,2)) as `value` , ${index} as `month` from t_unitenergy2020 a left join enterprise_info b
        on a.enterprise_id = b.enterpriseCode
        <where>
            and b.reportNum like '%已完成%'
            <if test="areaCode!=null">
                and b.regionCode like concat(#{areaCode},'%')
            </if>
            <if test="industryCode!=null">
                and b.tid = #{industryCode}
            </if>
            and record_circle BETWEEN #{preTime} and #{endTime}
            and energyTypeCode = '0000' and code='00000000' and isRight = "1"
            and guideline_val &gt; 0 and guideline_val &lt; 1000000
        </where>
        GROUP BY month ORDER BY value desc
    </select>

    <select id="selectIndustryList" resultType="com.nantian.cloud.dbcapp.vo.AreaVO">
        select name,id as code  from temp_dict_name where id = #{code}
    </select>

    <select id="countAllInfo" resultType="int">
        select count(*) from enterprise_energy_data
        union all
        select count(*) from company_data_detail
    </select>

    <select id="selectCountNumByAreaAndIndustry" resultType="com.nantian.cloud.dbcapp.vo.AreaVO">
        select ${region} as code,count(distinct enterpriseCode) as name from
        (
        SELECT enterpriseCode,regionCode,industry_code FROM electric_enterprise_info
        where 1=1
        <if test="areaName !=null">
            and regionCode LIKE concat(#{areaName},'%')
        </if>
        <if test="industryName!=null">
            and industry_code = #{industryName}
        </if>
        union all
        SELECT enterpriseCode ,regionCode,tid as industry_code FROM enterprise_info
        where 1=1
        <if test="areaName !=null">
            and regionCode like concat(#{areaName},'%')
        </if>
        <if test="industryName!=null">
            and tid =#{industryName}
        </if>
        ) t
        where ${region} like concat(#{areaName},'%')
        <if test="industryName!=null">
            and ${tid} = #{industryName}
        </if>
        group by code
        union all
        select ${tid} as code,count(distinct enterpriseCode) as name from
        (
        SELECT enterpriseCode,regionCode,industry_code FROM electric_enterprise_info
        where 1=1
        <if test="areaName !=null">
            and regionCode LIKE concat(#{areaName},'%')
        </if>
        <if test="industryName!=null">
            and industry_code = #{industryName}
        </if>
        union all
        SELECT enterpriseCode ,regionCode,tid as industry_code FROM enterprise_info
        where 1=1
        <if test="areaName !=null">
            and regionCode like concat(#{areaName},'%')
        </if>
        <if test="industryName!=null">
            and tid =#{industryName}
        </if>
        ) t
        where ${region} like concat(#{areaName},'%')
        <if test="industryName!=null">
            and ${tid} = #{industryName}
        </if>
        group by code
        order by name desc
    </select>

    <select id="selectEcpName" resultType="com.nantian.cloud.dbcapp.vo.EpcVo">
        select ${cmpName},regioncode,tname as regionName,name as industryName from
        (
        SELECT enterpriseCode,regionCode,industry_code,cmpName,tname,name FROM electric_enterprise_info a left join
        dict_region b on a.regionCode = b.code
        left join temp_dict_name c on a.industry_code = c.id
        where 1=1
        <if test="areaName !=null">
            and regionCode LIKE concat(#{areaName},'%')
        </if>
        <if test="industryName!=null">
            and industry_code = #{industryName}
        </if>
        union
        SELECT enterpriseCode ,regionCode,tid as industry_code ,cmpName,tname,name FROM enterprise_info a left join
        dict_region b on a.regionCode = b.code
        left join temp_dict_name c on a.tid = c.id
        where 1=1
        <if test="areaName !=null">
            and regionCode like concat(#{areaName},'%')
        </if>
        <if test="industryName!=null">
            and tid =#{industryName}
        </if>
        ) t
        where ${region} like concat(#{areaName},'%')
        <if test="industryName!=null">
            and ${tid} = #{industryName}
        </if>
        group by t.enterpriseCode ORDER BY regionCode,industryName
    </select>

    <select id="selectCountNumByAreaAndIndustryUpLoad" resultType="com.nantian.cloud.dbcapp.vo.AreaVO">
        select ${region} as code,count(distinct enterpriseCode) as name from
        upload_country_enterprise_info
        where regionCode like concat(#{areaName},'%')
        <if test="industryName!=null">
            and tid = #{industryName}
        </if>
        and uploadCompleted = '1'
        group by code
        union all
        select tid as code,count(distinct enterpriseCode) as name from
        upload_country_enterprise_info
        where regionCode like concat(#{areaName},'%')
        <if test="industryName!=null">
            and tid = #{industryName}
        </if>
        and uploadCompleted = '1'
        group by code
        order by name desc
    </select>

    <select id="selectEcpNameUpLoad" resultType="com.nantian.cloud.dbcapp.vo.EpcVo">
        select cmpName,regioncode,tname as regionName,name as industryName from upload_country_enterprise_info a left
        join
        dict_region b on a.regionCode = b.code
        left join temp_dict_name c on a.tid = c.id
        where a.regionCode like concat(#{areaName},'%')
        <if test="industryName!=null">
            and a.tid = #{industryName}
        </if>
        and uploadCompleted = '1'
        group by enterpriseCode ORDER BY regionCode,industryName
    </select>

    <select id="selectCountNumByAreaAndIndustryAll" resultType="com.nantian.cloud.dbcapp.vo.AreaVO">
        select COUNT( DISTINCT enterpriseCode) as name ,${index} as code from (
        SELECT a.enterpriseCode,a.cmpName,a.regionCode,a.industry_code as tid FROM electric_enterprise_info a
        where 1=1
        <if test="areaName !=null">
            and a.regionCode LIKE concat(#{areaName},"%")
        </if>
        <if test="industryName!=null">
            and a.industry_code = #{industryName}
        </if>

        union all
        SELECT t.enterprise_code as enterpriseCode,t.enterprise_name as cmpName,t.region_code as
        regionCode,t.industryCode as tid FROM t_enterprise_old t
        where 1=1
        and state =0
        <if test="areaName !=null">
            and t.region_code like concat(#{areaName},"%")
        </if>
        <if test="industryName!=null">
            and t.industryCode = #{industryName}
        </if>
        union all
        SELECT d.enterpriseCode,d.cmpName,d.regionCode,d.tid as tid FROM enterprise_info d
        where 1=1
        <if test="areaName!=null">
            and d.regionCode like concat(#{areaName},"%")
        </if>
        <if test="industryName!=null">
            and d.tid = #{industryName}
        </if>
        ) t
        group by code

        union all
        select COUNT( DISTINCT enterpriseCode) as name ,tid as code from (
        SELECT a.enterpriseCode,a.cmpName,a.regionCode,a.industry_code as tid FROM electric_enterprise_info a
        where 1=1
        <if test="areaName !=null">
            and a.regionCode LIKE concat(#{areaName},"%")
        </if>
        <if test="industryName!=null">
            and a.industry_code = #{industryName}
        </if>

        union all
        SELECT t.enterprise_code as enterpriseCode,t.enterprise_name as cmpName,t.region_code as
        regionCode,t.industryCode as tid FROM t_enterprise_old t
        where 1=1
        and state =0
        <if test="areaName !=null">
            and t.region_code like concat(#{areaName},"%")
        </if>
        <if test="industryName!=null">
            and t.industryCode = #{industryName}
        </if>
        union all
        SELECT d.enterpriseCode,d.cmpName,d.regionCode,d.tid as tid FROM enterprise_info d
        where 1=1
        <if test="areaName!=null">
            and d.regionCode like concat(#{areaName},"%")
        </if>
        <if test="industryName!=null">
            and d.tid = #{industryName}
        </if>
        ) t
        group by code
    </select>

    <select id="selectEcpNameAll" resultType="com.nantian.cloud.dbcapp.vo.EpcVo">
        select cmpName,regioncode,tname as regionName,name as industryName from (
        SELECT enterpriseCode,regionCode,industry_code,cmpName,tname,name FROM electric_enterprise_info a left join
        dict_region b on a.regionCode = b.code
        left join temp_dict_name c on a.industry_code = c.id
        where 1=1
        <if test="areaName !=null">
            and regionCode LIKE concat(#{areaName},'%')
        </if>
        <if test="industryName!=null">
            and industry_code = #{industryName}
        </if>
        union
        SELECT enterpriseCode,regionCode,tid,cmpName,tname,name FROM enterprise_info a left join
        dict_region b on a.regionCode = b.code
        left join temp_dict_name c on a.tid = c.id
        where 1=1
        <if test="areaName !=null">
            and regionCode like concat(#{areaName},'%')
        </if>
        <if test="industryName!=null">
            and tid =#{industryName}
        </if>
        union
        SELECT enterprise_code,region_code,industryCode,enterprise_name,tname,name FROM t_enterprise_old a left join
        dict_region b on a.region_code = b.code
        left join temp_dict_name c on a.industryCode = c.id
        where 1=1
        and state =0
        <if test="areaName !=null">
            and a.region_code like concat(#{areaName},"%")
        </if>
        <if test="industryName!=null">
            and a.industryCode = #{industryName}
        </if>

        ) t
        group by t.enterpriseCode ORDER BY regionCode,industryName
    </select>

    <select id="getExceptionToalAll" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
        SELECT COUNT(*) as value ,${region} as `month` from energy_exception_data_info a left join
        enterprise_info b on a.enterpriseCode = b.enterpriseCode where b.regionCode like #{areaName}
        and statDate between #{startTime} and #{endTime}
        and a.handlerType != 0
        <if test="industryName!=null">
            and b.tid = #{industryName}
        </if>
        <if test="cmpName!=null">
            and b.enterpriseCode = #{cmpName}
        </if>
        <if test="type!=null">
            and a.exceptionType = #{type}
        </if>
        group by month
    </select>

    <select id="CountAll" resultType="int">
        select count(*) from
        energy_exception_data_info as a left join enterprise_info as b on a.enterpriseCode = b.enterpriseCode
        where b.regionCode like #{area}
        <if test="cmpName !=null">
            and b.enterpriseCode = #{cmpName}
        </if>
        and a.statDate between #{preTime} and #{endTime}
        <if test="type !=null">
            and a.exceptionType = #{type}
        </if>
        <if test="collectName!=null">
            and collectItemName like #{collectName}
        </if>
        <if test="state!=null">
            and exceptionGov ={state}
        </if>
        union
        select count(*) from
        energy_exception_data_info as a left join enterprise_info as b on a.enterpriseCode = b.enterpriseCode
        where b.regionCode like #{area}
        <if test="cmpName !=null">
            and b.enterpriseCode = #{cmpName}
        </if>
        and a.statDate between #{preTime} and #{endTime}
        <if test="type !=null">
            and a.exceptionType = #{type}
        </if>
        <if test="collectName!=null">
            and collectItemName like #{collectName}
        </if>
        and exceptionGov ='0'

    </select>

    <update id="updateExceptionInfo">
        update energy_exception_data_info set handlerType = 0, remark = #{data.remark},exceptionGov = #{data.exceptionGov},dataValue = #{data.dataValue},convertRation = #{data.convertRation} where id = #{data.id}
    </update>

    <select id="waveExceptionList" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
        select distinct sum(dataValue) as `value` , substring(statDate,1,10) as `month`
        from energy_exception_data_info where enterpriseCode = #{enterpriseCode} and dataCode = #{dataCode}
        and statDate between #{pre} and #{end} group by enterpriseCode,dataCode,substring(statDate,1,10)
    </select>

    <update id="updateInfoByEpcDcSd">
        update enterprise_energy_data set dataValue = #{data.dataValue},convertRation = #{data.convertRation}
        where enterpriseCode =#{data.enterpriseCode} and dataCode = #{data.dataCode} and statDate =#{data.statDate}
    </update>

    <update id="updateExceptionInfoAll">
                update energy_exception_data_info set remark = #{data.remark},exceptionGov = #{data.exceptionGov} where
                enterpriseCode = #{data.enterpriseCode} and dataCode = #{data.dataCode}
    </update>

    <select id="getEnergyCalculateInfo" resultType="com.nantian.cloud.dbcapp.vo.CalculateLogsVo">
        select case when operateType = 1 then '接口结算' when operateType = 2 then '补算结算' else '月结算' end as operateType
        ,operatePerson,operateObject,operateDate,a.statDate, case when a.result = 0 then '成功' else '失败' end AS result
        ,case when energyType = 0 then '综合能耗' else '单位产品综合能耗' end AS energyType,a.reason
        ,case when a.operateObject = 'all' then '全部企业' else cmpName end cmpName from
        t_energy_calculate_log a
        left join enterprise_info b on a.operateObject = b.enterpriseCode
        where 1=1
        <if test="enterpriseCode != null">
            and operateObject = #{enterpriseCode}
        </if>
        <if test="statDate != null">
            and a.statDate like concat('%',#{statDate},'%')
        </if>
        <if test="state != null">
            and a.result = #{state}
        </if>
        order by a.operateDate desc, a.operateObject asc, a.statDate desc
    </select>

    <update id="updateDataCodeState">
        update configure_data_collect set isEnable = #{state} where enterpriseCode = #{enterpriseCode} and dataCode = #{dataCode}
    </update>
    <update id="updateDataCodeStateNew">
                update configure_data_collect_new set isEnable = #{state} where enterpriseCode = #{enterpriseCode} and dataCode = #{dataCode}
    </update>

    <select id="getEnergyTableEpc" resultType="int">
        select count(distinct enterpriseCode) from enterprise_info
        union
        select count(distinct enterpriseCode) from enterprise_info where approvalDate like concat(#{statDate},'%')
        union
        select count(distinct enterpriseCode) from t_effective_enterprise where `time` = #{statDate}
    </select>

    <select id="selectEpcByTime" resultType="string">
        select distinct enterpriseCode from t_effective_enterprise where `time` = #{statDate}
        <if test="areaCode != null">
            and regionCode like concat(#{areaCode},'%')
        </if>
        <if test="industryCode != null">
            and tid = #{industryCode}
        </if>
    </select>
    <select id="selectValueByKinds" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
        SELECT sum(guideline_val)as value , record_circle as `month` from t_unitenergy2020 where enterprise_id in
        <foreach collection="epcLists" item="items" separator="," open="(" close=")">
            #{items}
        </foreach>
        AND record_circle = #{statDate} and energyTypeCode in
        <foreach collection="kindsList" item="items" separator="," open="(" close=")">
            #{items}
        </foreach>
        and code='00000000'
        and guideline_val &lt; 1000000 and guideline_val >0 and isRight = "1"
        and energy_type_code in ('01','02')
        group by record_circle
    </select>

    <select id="selectOutputValue" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
        select ${outputValue} as `value` , `time` as `month` from t_productvalue a left join enterprise_info b
        on a.enterpriseCode = b.enterpriseCode
        where
        b.regionCode like concat(#{areaCode},'%')
        <if test="industryCode!=null">
            and b.tid = #{industryCode}
        </if>
        and `time` = #{statDate}
        group by time
    </select>

    <select id="selectMaxValue" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
        select sum(guideline_val) as `value`,cmpName as `month` from t_unitenergy2020
        a
        left join enterprise_info b on a.enterprise_id = b.enterpriseCode
        where
        enterprise_id in
        <foreach collection="list" item="items" separator="," open="(" close=")">
            #{items}
        </foreach>
        AND record_circle = #{state} and energyTypeCode = '0000'
        and code='00000000'
        and guideline_val &lt; 1000000 and guideline_val >0 and isRight = "1"
        group by ${enterpriseCode} order by `value` desc ;
    </select>

    <select id="getEffectiveAreaOrder" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
        select sum(guideline_val) as `value`,${index} as `month` from t_unitenergy2020 a
        left join enterprise_info b on a.enterprise_id = b.enterpriseCode where
        enterprise_id in
        <foreach collection="list" item="items" separator="," open="(" close=")">
            #{items}
        </foreach>
        AND record_circle = #{state} and energyTypeCode = '0000'
        and code='00000000'
        and guideline_val &lt; 1000000 and guideline_val >0 and isRight = "1"
        group by `month` order by `value` desc ;
    </select>
    <select id="selectMaxOutValue" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
        select energyOutputValue as `value`,cmpName as `month` from t_productvalue a left join
        enterprise_info b on a.enterpriseCode = b.enterpriseCode
        where `time` = #{statDate}
        <if test="areaCode!=null">
            and b.regionCode like concat (#{areaCode},'%')
        </if>
        <if test="industryCode!=null">
            and b.tid = #{industryCode}
        </if>
        order by `value` desc
    </select>

    <select id="getEffectiveAreaOutOrder" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
        select sum(energyOutputValue) as `value`,${index} as `month` from t_productvalue a left join
        enterprise_info b on a.enterpriseCode = b.enterpriseCode
        where `time` = #{statDate}
        <if test="areaCode!=null">
            and b.regionCode like concat (#{areaCode},'%')
        </if>
        <if test="industryCode!=null">
            and b.tid = #{industryCode}
        </if>
        group by `month` order by `value` desc
    </select>
    <insert id="insertExceptionRation">
        insert into t_right_exception_value (enterpriseCode,dataCode,rationValue) values
        (#{index.enterpriseCode},#{index.dataCode},#{index.convertRation})
    </insert>
    <insert id="saveExceptionalLog" parameterType="com.nantian.cloud.dbcapp.vo.TExceptionHandlingLogVo"
            useGeneratedKeys="true" keyProperty="id">
        insert into t_exception_handling_log (`name`,creat_time,epc,collect_item_name,stat_date,state,userid,update_time,
        del_flag,dept_id) values

        (#{data.name},#{data.creatTime},#{data.epc},#{data.collectItemName},#{data.statDate},#{data.state},#{data.userid},
         #{data.updateTime},#{data.delFlag},#{data.deptId})
    </insert>

    <insert id="saveBatchExceptionalLog">
        insert into t_exception_handling_log (`name`,epc,collect_item_name,data_code,stat_date,state,userid,del_flag,dept_id,
        creat_time,update_time,data_value,edit_data_value,convert_ration,edit_convert_ration) values
        <foreach collection="data" item="item" separator=",">
            (#{item.enterpriseCode},#{item.enterpriseCode},#{item.collectItemName},#{item.dataCode},#{item.statDate},#{state},#{user.id},"1",#{user.deptId},
            (select now()),(select now()),#{item.dataValue},#{item.editDataValue},#{item.convertRation},#{item.editConvertRation})
        </foreach>
    </insert>

    <insert id="saveExceptionalLogByApply">
        insert into t_exception_handling_log (`name`,epc,collect_item_name,data_code,stat_date,state,userid,del_flag,dept_id,
        creat_time,update_time,data_value,edit_data_value,convert_ration,edit_convert_ration) values
        (#{data.enterpriseCode},#{data.enterpriseCode},#{data.collectName},#{data.dataCode},#{data.statDate},#{state},#{user.id},"1",#{user.deptId},
        (select now()),(select now()),#{data.dataValue},#{data.editDataValue},#{data.convertRation},#{data.editConvertRation})
    </insert>

    <select id="getEnergyCalculateAddInfo" resultType="com.nantian.cloud.dbcapp.vo.EnergyCalculateAddInfo">
        select a.enterpriseCode,cmpName,a.dataCode,collectItemName,dataValue,convertRation,statDate,addDate
        from enterprise_energy_data_add a left join enterprise_info b on a.enterpriseCode = b.enterpriseCode
        left join configure_data_collect c on a.enterpriseCode = c.enterpriseCode and a.dataCode = c.dataCode
        where 1 = 1
        and a.dataCode like '00000000%'
        <if test="enterpriseCode!=null">
            and a.enterpriseCode = #{enterpriseCode}
        </if>
        <if test="dataCode!=null">
            and a.dataCode = #{dataCode}
        </if>
        <if test="statDate!=null">
            and a.statDate like concat(#{statDate},'%')
        </if>
    </select>

    <update id="upDateEnterpriseRegionCode">
        update enterprise_info set regionCode = #{regionCode} where enterpriseCode = #{enterpriseCode}
    </update>

    <select id="selectCollectEnterpriseInfo" resultType="com.nantian.cloud.dbcapp.vo.EpcVo">
        select enterpriseCode,cmpName,address,regionCode,tname as regionName from enterprise_info a left join
        dict_region b on a.regionCode = b.code where 1=1
        <if test="areaCode!=null">
            and a.regionCode like concat(#{areaCode},'%')
        </if>
        <if test="industryCode!=null">
            and a.tid = #{industryCode}
        </if>
        <if test="cmpName!=null">
            and a.cmpName like concat('%',#{cmpName},'%')
        </if>
    </select>

    <select id="getDictRegionTree" resultType="com.nantian.cloud.dbcapp.vo.TreeVo">
        select id as value, pcode as pvalue, meaning as label
        from dict_region
    </select>

    <select id="getDictRegionTrees" resultType="com.nantian.cloud.dbcapp.vo.TreeVo">
        select id as value, pcode as pvalue, meaning as label
        from dict_region and id=#{id}
        or pcode=#{id}
    </select>
    <select id="getExceptionalLog" resultType="com.nantian.cloud.dbcapp.vo.TExceptionHandlingLogVo">
        select b.cmpName,b.regionCode,a.epc,a.name,a.creat_time,a.collectItem_name,a.stat_date,a.state from t_exception_handling_log a left join
        enterprise_info b on a.epc = b.enterpriseCode where 1=1
        <if test="data!=null">
            and a.epc = #{data}
        </if>
    </select>

    <select id="getEnergyInfoByEpc" resultType="com.nantian.cloud.dbcapp.vo.EnterpriseTce">
        insert into enterprise_energy_tce (
        epc, cmpName, year, regionCode, regionName, trade
        <foreach collection="list" item="date" index="i">
            , month${i+1}
        </foreach>
        )
        select e.enterpriseCode epc, e.cmpName, #{year} year, r.id regionCode, r.tname regionName, e.jnjcTrade trade
        <foreach collection="list" item="date" index="i">
            ,IFNULL(round((
            select sum(round(guideline_val/10000,2)) from t_unitenergy2020
            where energyTypeCode = '0000' and `code`='00000000' and guideline_val > 0 and guideline_val &lt; 1000000
            and isRight = "1" and enterprise_id = e.enterpriseCode
            and record_circle = #{date}
            ),3), 0 + CAST(0 AS CHAR)) as month${i+1}
        </foreach>
        from enterprise_info e
        left join dict_region r on concat(left(e.regionCode,4),'00') = r.id
        where e.reportNum like '%已完成%'; -- 只查接入完成的企业
    </select>
    <delete id="deleteEnergyTce">
        delete from enterprise_energy_tce where year = #{year}
    </delete>
    <insert id="insertEnergyTce">
        insert into enterprise_energy_tce
        (regionCode,regionName,trade,cmpName,epc,year,month1,month2,month3,month4,month5,month6,month7,month8,month9,month10,month11,month12)
        values
        <foreach collection="list" item="item" separator=",">
            (#{item.regionCode},#{item.regionName},#{item.trade},#{item.cmpName},#{item.epc},#{item.year},#{item.month1},#{item.month2},#{item.month3},#{item.month4},#{item.month5},#{item.month6},
            #{item.month7},#{item.month8},#{item.month9},#{item.month10},#{item.month11},#{item.month12})
        </foreach>
    </insert>
    <update id="updateEnergyTce">
        update enterprise_energy_tce
        set sumTce = IFNULL(month1,0)+IFNULL(month2,0)+IFNULL(month3,0)+IFNULL(month4,0)+IFNULL(month5,0)+IFNULL(month6,0)+IFNULL(month7,0)+IFNULL(month8,0)+IFNULL(month9,0)+IFNULL(month10,0)+IFNULL(month11,0)+IFNULL(month12,0),
		tce0Count = (case when month1 = 0 then 1 else 0 end) + (case when month2 = 0 then 1 else 0 end) + (case when month3 = 0 then 1 else 0 end) + (case when month4 = 0 then 1 else 0 end) + (case when month5 = 0 then 1 else 0 end) + (case when month6 = 0 then 1 else 0 end)
        + (case when month7 = 0 then 1 else 0 end) + (case when month8 = 0 then 1 else 0 end) + (case when month9 = 0 then 1 else 0 end) + (case when month10 = 0 then 1 else 0 end) + (case when month11 = 0 then 1 else 0 end) + (case when month12 = 0 then 1 else 0 end)
    </update>
    <select id="getEnergyInfoByRegion" resultType="com.nantian.cloud.dbcapp.vo.EnterpriseTce">
        select regionCode, regionName, count(epc) count, sum(month1) month1, sum(month2) month2, sum(month3) month3, sum(month4) month4, sum(month5) month5, sum(month6) month6,
        sum(month7) month7, sum(month8) month8, sum(month9) month9, sum(month10) month10, sum(month11) month11, sum(month12) month12, sum(sumTce) sumTce
        from enterprise_energy_tce where year = #{year} group by regionCode order by regionCode asc
    </select>
    <select id="getEnergyInfoByTrade" resultType="com.nantian.cloud.dbcapp.vo.EnterpriseTce">
        select trade, num, count(epc) count, sum(month1) month1, sum(month2) month2, sum(month3) month3, sum(month4) month4, sum(month5) month5, sum(month6) month6,
        sum(month7) month7, sum(month8) month8, sum(month9) month9, sum(month10) month10, sum(month11) month11, sum(month12) month12, sum(sumTce) sumTce from (
            select case when trade in (${tradeStr}) then trade else '其他' end trade, case when trade in (${tradeStr}) then 'a' else 'z' end num,
	        epc, month1, month2, month3, month4, month5, month6, month7, month8, month9, month10, month11, month12, sumTce
            from enterprise_energy_tce where year = #{year}
        ) t group by trade order by num asc, sumTce desc
    </select>
    <select id="getNewEnterpriseTce" resultType="java.util.Map">
        select IFNULL(e.enterpriseCode,'') enterpriseCode, IFNULL(e.cmpName,'') cmpName, IFNULL(t.regionName,'') regionName, IFNULL(t.trade,'') trade, IFNULL(e.applyDate,'') applyDate, IFNULL(round(t.sumTce,3),0 + CAST(0 AS CHAR)) sumTce
        from enterprise_info e left join enterprise_energy_tce t on e.enterpriseCode = t.epc
        where t.year = #{year} and e.applyDate between concat(#{statDate},"-01 00:00:00") and concat(#{statDate},"-31 23:59:59") and reportNum like '%已完成%' order by t.sumTce desc
    </select>
    <select id="getExceptionEnterpriseTce" resultType="java.util.Map">
        select IFNULL(e.enterpriseCode,'') enterpriseCode, IFNULL(e.cmpName,'') cmpName, IFNULL(t.regionName,'')
        regionName, IFNULL(t.trade,'') trade, IFNULL(e.applyDate,'') applyDate, IFNULL(round(t.sumTce,3),0 + CAST(0 AS CHAR)) sumTce
        from enterprise_info e left join enterprise_energy_tce t on e.enterpriseCode = t.epc
        where e.cmpName not like '%供电局%' and e.cancelState = '0' and e.applyDate &lt; &apos;${applyDate}&apos;
        and e.enterpriseCode in (select distinct enterpriseCode from energy_exception_data_info where handlerType != '0' and exceptionType = '0101' and statDate like concat('%',left(&apos;${applyDate}&apos;,7),'%'))
        <if test=" list != null and list.length > 0">
            and e.enterpriseCode not in
            <foreach collection="list" item="item" index="i" open="(" separator="," close=")">
                &apos;${item}&apos;
            </foreach>
        </if>
        and t.year = &apos;${year}&apos; -- and t.month${month} = 0
        order by t.sumTce asc
    </select>
    <select id="getExceptionEnterprise0" resultType="java.lang.String">
        select tmp.enterpriseCode from(
        select e.enterpriseCode, e.cmpName, e.applyDate, IFNULL((
        select round(sum( guideline_val),3) from t_unitenergy2020
        where energyTypeCode = '0000' and `code`='00000000' and guideline_val &lt; 1000000
        and enterprise_id = e.enterpriseCode
        and record_circle = #{statDate}
        ),0) tce
        from enterprise_info e
        where e.cmpName not like '%供电局%' and e.cancelState = '0' and e.applyDate &lt; #{applyDate}
        <if test=" list != null and list.length > 0">
            and e.enterpriseCode not in
            <foreach collection="list" item="item" index="i" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        ) tmp where tce = 0
    </select>
    <select id="getTceByEnterprise" resultType="java.util.Map">
        select e.enterpriseCode, e.cmpName, IFNULL(e.jnjcTrade,'') trade, (select tname from dict_region where id = concat(left(e.regionCode,4),'00')) regionName, IFNULL(e.applyDate,'') applyDate,
        IFNULL((
            select round(sum( guideline_val),3) from  t_unitenergy2020
            where energyTypeCode = '0000' and `code`='00000000' and guideline_val  &lt; 1000000
            and enterprise_id = e.enterpriseCode
            and record_circle between #{startDate} and #{endDate}
        ),0) sumTce
        from enterprise_info e where #{exceptionEnterprise} like concat('%',e.enterpriseCode,'%') having sumTce &lt;= 0 order by sumTce asc
    </select>
    <select id="getTradeTotalTce" resultType="java.util.Map">
        select trade, round(sum(sumTce),2) tce
        from enterprise_energy_tce where year = #{year} and trade in (${tradeStr})
        group by trade order by tce desc limit 0,5
    </select>
    <select id="getTradeCurrentTce" resultType="java.util.Map">
        select trade, round(sum(month${month}),2) tce
        from enterprise_energy_tce where year = #{year} and trade in (${tradeStr})
        group by trade order by tce desc limit 0,5
    </select>
    <select id="getExceptionEnterpriseInfos" resultType="com.nantian.cloud.dbcapp.vo.ExceptionEnterpriseInfos">
        select e.id, e.epc, e.name, e.statDate, e.state, e.remark, (select tname from dict_region where id =
        concat(left(info.regionCode,4),'00')) region
        from exception_enterprise_info e
        left join enterprise_info info on info.enterpriseCode = e.epc
        where e.statDate = #{query.statDate}
        <if test="query.epc != null and query.epc != '' ">
            and e.epc like concat('%',#{query.epc},'%')
        </if>
        <if test="query.name != null and query.name != '' ">
            and e.name like concat('%',#{query.name},'%')
        </if>
        <if test="query.state != null and query.state != '' ">
            and e.state = #{query.state}
        </if>
    </select>
    <select id="getExceptionEnterpriseCode" resultType="java.lang.String">
        select epc from exception_enterprise_info where statDate = #{statDate} and state = '1'
    </select>
    <!--select distinct enterpriseCode from energy_exception_data_info where handlerType != '0' and exceptionType = '0101' and statDate like concat('%', #{statDate},'%')-->
    <select id="getExceptionEnterpriseCodeNew" resultType="java.lang.String">
        select distinct epc from enterprise_data_all where exceptionType = '1' and type = '4' and statDate = #{statDate}
        <if test=" list != null and list.length > 0">
            and epc not in
            <foreach collection="list" item="item" index="i" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
    </select>
    <select id="getEnergyEnterpriseCount" resultType="java.lang.Integer">
        select count(*) from enterprise_info where applyDate &lt;= #{statDate} and reportNum like '%已完成%'
    </select>
    <select id="energyMappergetEnergyWarning" resultType="java.util.Map">
        select (select tname from dict_region where id = concat(left(e.regionCode,4),'00')) region,
        (select `name` from temp_dict_name where id = e.tid ) industry,
        case e.energyConsumeLevel when '1' then '5000吨以下' when '2' then '5000~10000吨' when '3' then '1万-10万吨' when '4'
        then '10万-50万吨' when '5' then '50万-300万吨' when '6' then '300万吨以上' else '' end `level`,
        e.enterpriseCode, e.cmpName, record_circle recordCircle, sum(guideline_val) `tce`
        from t_unitenergy2020 tce
        left join enterprise_info e on tce.enterprise_id = e.enterpriseCode
        where record_circle between #{startTime} and #{endTime} and energyTypeCode='0000' and code ='00000000'
        <if test="area!=null and area!=''">
            and e.regionCode like concat(#{area},'%')
        </if>
        <if test="industry!=null and industry!=''">
            and e.tid = #{industry}
        </if>
        group by record_circle, e.enterpriseCode HAVING(0>sum(guideline_val))
        order by left(e.regionCode,4), e.tid, e.enterpriseCode, record_circle asc
    </select>
    <select id="getEpcInfo" resultType="com.nantian.cloud.dbcapp.vo.AreaVO">
        select case when #{area}='53' then left(regionCode,4) else regionCode end code, count(enterpriseCode) name
        from enterprise_info_all
        where regionCode like concat(#{area},'%')
        <if test="industry!=null and industry!=''">
            and tid = #{industry}
        </if>
        <if test="type!=null and type!=''">
            and type like concat('%',#{type},'%')
        </if>
        group by code
        union all
        select tid code, count(enterpriseCode) name
        from enterprise_info_all
        where regionCode like concat(#{area},'%')
        <if test="industry!=null and industry!=''">
            and tid = #{industry}
        </if>
        <if test="type!=null and type!=''">
            and type like concat('%',#{type},'%')
        </if>
        group by code
    </select>
    <select id="getEpcDetailInfo" resultType="com.nantian.cloud.dbcapp.vo.EpcVo">
        select cmpName, regioncode, tname as regionName, name as industryName
        from enterprise_info_all a
        left join dict_region b on a.regionCode = b.code
        left join temp_dict_name c on a.tid = c.id
        where a.regionCode like concat(#{area},'%')
        <if test="industry!=null and industry!=''">
            and a.tid = #{industry}
        </if>
        <if test="type!=null and type!=''">
            and a.type like concat('%',#{type},'%')
        </if>
        order by regionCode,industryName
    </select>
    <select id="getEpcDetailCount" resultType="java.lang.Integer">
        select count(1)
        from enterprise_info_all a
        where a.regionCode like concat(#{area},'%')
        <if test="industry!=null and industry!=''">
            and a.tid = #{industry}
        </if>
        <if test="type!=null and type!=''">
            and a.type like concat('%',#{type},'%')
        </if>
    </select>
    <select id="getEpcDataCount" resultType="java.lang.Integer">
        select sum(num)
        from enterprise_data_all a
        where a.regionCode like concat(#{area},'%')
        <if test="industry!=null and industry!=''">
            and a.tid = #{industry}
        </if>
        <if test="type!=null and type!=''">
            and a.type like concat('%',#{type},'%')
        </if>
    </select>
    <select id="getCollectNum" resultType="java.lang.Integer">
        select count(1) from enterprise_energy_data where enterpriseCode in (
        select enterpriseCode from enterprise_info where regionCode like concat(#{area},'%')
        <if test="industry!=null and industry!=''">
            and tid = #{industry}
        </if>
        )
    </select>
    <select id="getElectricityNum" resultType="java.lang.Integer">
        select count(1) from company_data_detail where region like concat(#{area},'%')
        <if test="industry!=null">
            and industry = #{industry}
        </if>
    </select>
    <select id="getUpdateCountryValue" resultType="java.lang.Integer">
        select count(1) from enterprise_backup_data where enterpriseCode in (
            select enterpriseCode from enterprise_info_all where regionCode like concat(#{area},'%')
            <if test="industry!=null">
                and tid = #{industry}
            </if>
        )
    </select>
    <select id="getExceptionDict" resultType="java.util.Map">
        select SUBSTRING_INDEX(sort_name,',',1) value, SUBSTRING_INDEX(sort_name,',',-1) label, GROUP_CONCAT(exception_code) exceptionCode
		from quality_exception_code where sort_name is not null and sort_name != '' group by SUBSTRING_INDEX(sort_name,',',1) order by value
    </select>
    <select id="getDictRegionCode" resultType="java.lang.String">
        select case when #{area} like '53%' then tname else code end code
        from dict_region where code like concat('%',#{area},'%') or meaning like concat('%',#{area},'%')
    </select>
    <select id="getCollectByArea" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
        select o.tname month, IFNULL(sum(a.num),0) as value
        from dict_region o left join(
        select regionCode, num from enterprise_data_all where type = '1' and left(statDate,7) between #{preTime} and
        #{endTime}
        <if test="area!=null and area!=''">
            and regionCode like concat('%',#{area},'%')
        </if>
        <if test="industry!=null and industry!=''">
            and tid = #{industry}
        </if>
        )a on left(a.regionCode,${length}) = left(o.code,${length})
        <choose>
            <when test="area == '53'.toString()">
                where o.pcode = '530000'
            </when>
            <when test="area.length == 4">
                where o.pcode like concat('%',#{area},'%')
            </when>
            <otherwise>
                where o.code like concat('%',#{area},'%')
            </otherwise>
        </choose>
        group by o.code order by value desc
    </select>
    <select id="getCollectByIndustry" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
        select (select IFNULL(name,'其他') from temp_dict_name where id = a.tid) month, IFNULL(sum(a.num),0) as value
        from enterprise_data_all a
        where 1 = 1 and type = '1' and left(statDate,7) between #{preTime} and #{endTime}
        <if test="area!=null and area!=''">
            and a.regionCode like concat('%',#{area},'%')
        </if>
        <if test="industry!=null and industry!=''">
            and a.tid = #{industry}
        </if>
        group by a.tid order by value desc
    </select>
    <select id="getCollectByMonth" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
        select o.month, IFNULL(sum(a.num),0) as value
        from (
        <foreach collection="list" separator="union" item="item" index="i">
            select #{item.month} month
        </foreach>
        ) o left join(
        select statDate, num from enterprise_data_all where type = '1' and statDate like concat(#{year},'%')
        <if test="area!=null and area!=''">
            and regionCode like concat('%',#{area},'%')
        </if>
        <if test="industry!=null and industry!=''">
            and tid = #{industry}
        </if>
        )a on left(a.statDate,7) = o.month
        group by o.month order by o.month asc
    </select>
    <select id="getCollectByDay" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
        select o.month, IFNULL(sum(a.num),0) as value
        from (
        <foreach collection="list" separator="union" item="item" index="i">
            select #{item.month} month
        </foreach>
        ) o left join(
        select statDate, num from enterprise_data_all where type = '1' and statDate like concat(#{month},'%')
        <if test="area!=null and area!=''">
            and regionCode like concat('%',#{area},'%')
        </if>
        <if test="industry!=null and industry!=''">
            and tid = #{industry}
        </if>
        )a on left(a.statDate,10) = o.month
        group by o.month order by o.month asc
    </select>
    <!--<select id="getPageInfoList" resultType="com.nantian.cloud.dbcapp.vo.QualityCalculateLogVo">
        select t.cmpName as cmpName , t.statDate as statDate , t.enterpriseCode as operate_object, log.type as type, log.operate_end_date as operate_end_date, t.addDate as uploadTime,
        GROUP_CONCAT(
        CONCAT('结算时间：',log.operate_end_date,'，结算结果：', (case log.result when '1' then '成功' else '失败' end),
        (case log.result when '1' then '' else CONCAT('，失败原因：',reason) end)) SEPARATOR '；'
        ) result
        from (
        select distinct left(statDate,7) statDate, enterpriseCode, d.addDate,   (select distinct cmpName from enterprise_info where enterpriseCode = d.enterpriseCode) cmpName from enterprise_energy_data d
        where addDate like CONCAT('%', #{query.uploadTime}, '%') and (select TIMESTAMPDIFF(MONTH,left(statDate,10),left(addDate,10))) > 1
        ) t left join quality_calculate_log as log on t.enterpriseCode = log.operate_object and t.statDate = log.stat_date
        <if test="query.type != '' and query.type != null">
            and log.type = #{query.type}
        </if>
         and log.operate_type = '2'
        WHERE 1 = 1
        <if test="query.OperateObject != null and query.OperateObject != ''">
            and t.enterpriseCode = #{query.OperateObject}
        </if>
        <if test="query.statDate != null and query.statDate != ''">
            and t.statDate like CONCAT('%', #{query.statDate}, '%')
        </if>
        <if test="query.cmpName != '' and query.cmpName != null">
            and t.cmpName like CONCAT('%',#{query.cmpName}, '%')
        </if>
        group by t.statDate, t.enterpriseCode, log.type
    </select>-->

    <select id="getPageInfoList" resultType="com.nantian.cloud.dbcapp.vo.QualityCalculateLogVo">
        SELECT info.cmpName AS cmpName,task.stat_date AS statDate , task.operate_object AS operate_object, log.type AS type,
            log.operate_end_date AS operate_end_date, log.operate_start_date AS operate_start_date, log.reason AS result, task.task_date AS uploadTime
        FROM quality_supply_calculate_task task
        LEFT JOIN (
            SELECT operate_type, operate_object, operate_start_date, operate_end_date, stat_date, result, type, GROUP_CONCAT(
                CONCAT('结算时间：',operate_end_date,'，结算结果：', (case result when '1' then '成功' else '失败' end),
                (case result when '1' then '' else CONCAT('，失败原因：',reason) end)) SEPARATOR '；'
            ) AS reason
            FROM quality_calculate_log
            WHERE (operate_object, stat_date, operate_start_date) IN (
                SELECT operate_object, stat_date, max(operate_start_date) FROM quality_calculate_log WHERE 1 = 1
                <if test="query.type != '' and query.type != null">
                    AND type = #{query.type}
                </if>
                <if test="query.operateStartDate != '' and query.operateStartDate != null">
                    AND left(operate_start_date,10) >= #{query.operateStartDate}
                </if>
                GROUP BY operate_object, stat_date
            ) AND operate_type in ('2','3')
            <if test="query.type != '' and query.type != null">
                AND type = #{query.type}
            </if>
            GROUP BY operate_object, stat_date
        ) log ON task.operate_object = log.operate_object AND task.stat_date = log.stat_date
        LEFT JOIN enterprise_info info ON info.enterpriseCode = task.operate_object
        WHERE 1 = 1
        <if test="query.OperateObject != null and query.OperateObject != ''">
            AND task.operate_object = #{query.OperateObject}
        </if>
        <if test="query.statDate != null and query.statDate != ''">
            AND task.statDate LIKE CONCAT('%', #{query.statDate}, '%')
        </if>
        <if test="query.cmpName != '' and query.cmpName != null">
            AND info.cmpName LIKE CONCAT('%',#{query.cmpName}, '%')
        </if>
        <if test="query.uploadTime != '' and query.uploadTime != null">
            AND task.task_date LIKE CONCAT('%', #{query.uploadTime}, '%')
        </if>
        ORDER BY log.operate_start_date DESC
    </select>

    <select id="getPageLogList" resultType="com.nantian.cloud.dbcapp.vo.QualityCalculateLogVo">
        SELECT
        qc.id, qc.operate_type, qc.operate_object, qc.operate_start_date, qc.operate_end_date,
        qc.formul, qc.data_value, qc.stat_date, qc.result, qc.type, qc.reason,
        ei.cmpName
        FROM
        quality_calculate_log qc
        LEFT JOIN enterprise_info ei ON qc.operate_object = ei.enterpriseCode
        WHERE 1 = 1
        <if test="query.operateObject != '' and query.operateObject!= null">
            and qc.operate_object = #{query.OperateObject}
        </if>
        <if test="query.OperateType != '' and query.OperateType != null">
            and qc.operate_type = #{query.OperateType}
        </if>
        <if test="query.type != '' and query.type != null">
            and qc.type = #{query.type}
        </if>
        <if test="query.cmpName != '' and query.cmpName != null">
            and ei.cmpName like CONCAT('%',#{query.cmpName}, '%')
        </if>
        <if test="query.statDate != '' and query.statDate != null">
            and qc.stat_date like CONCAT ('%',#{query.statDate}, '%')
        </if>
        <if test="query.OperateEndDate != '' and query.OperateEndDate != null">
            and qc.operate_end_date like CONCAT('%',#{query.OperateEndDate}, '%')
        </if>
        ORDER BY qc.operate_start_date DESC
    </select>
    <select id="getExceptionNum" resultType="com.nantian.cloud.dbcapp.vo.ExceptionInfoNumVo">
    SELECT d.id,d.enterpriseCode,d.dataCode,d.collectItemName,d.processCode,d.processUnitCode,d.equipmentCode,d.equipmentUnitCode,d.isForce,d.statType,t1.num,t1.trans,t1.wave
    FROM configure_data_collect_new d LEFT JOIN(
        SELECT e.dataCode, e.enterpriseCode, COUNT(dataCode) num, sum(case when exceptionType like '%0101%' then 1 else 0 end) trans,
               sum(case when exceptionType not like '%0101%' then 1 else 0 end) wave,collectionFrequency
        FROM energy_exception_data_info e WHERE e.enterpriseCode = #{epc} AND handlerType != 0
        <if test="startDate != '' and startDate != null">
            AND left(e.statDate,7) &gt;= #{startDate}
        </if>
        <if test="endDate != '' and endDate != null">
            AND left(e.statDate,7) &lt;= #{endDate}
        </if>
        group by dataCode
	) t1 ON d.dataCode = t1.dataCode and t1.collectionFrequency = d.statType
    WHERE d.enterpriseCode = #{epc}
    <if test="dataCode != '' and dataCode != null">
        AND d.dataCode like concat('%',#{dataCode},'%')
    </if>
    <if test="collectItemName != '' and collectItemName != null">
        AND d.collectItemName like concat('%',#{collectItemName},'%')
    </if>
	ORDER BY d.dataCode
    </select>

    <select id="getDetail" resultType="java.util.Map">
        -- 综合能耗 万tce
        SELECT IFNULL(sum(round(guideline_val/10000,2)),0) tce, IFNULL( count(case when guideline_val is not null then 1 else 0 end),0) epcNum
        from t_unitenergy2020 where enterprise_id in(
            select enterpriseCode from enterprise_info
            <trim prefix="where" prefixOverrides="and">
                and reportNum like '%已完成%'
                <if test="industryCode!=null and industryCode!=''">
                    and tid = #{industryCode}
                </if>
                <if test="areaCode!=null and areaCode!=''">
                    and regionCode like CONCAT(#{areaCode}, '%')
                </if>
            </trim>
        )
        and record_circle between left(#{date},7) and left(#{date},7) and energyTypeCode = '0000' and code='00000000'
        and guideline_val &lt; 1000000 and guideline_val >0
    </select>

    <select id="getDetailOld" resultType="java.util.Map">
        select IFNULL(sum(round(guideline_val/10000,2)),0) tce, IFNULL( count(case when guideline_val is not null then 1 else 0 end),0) epcNum
        from t_unitenergy2019 where record_circle between left(#{date},7) and left(#{date},7) and energyTypeCode='0000' and del_flag = 0 and enterprise_id in (
            select enterprise_code from t_enterprise_old a
            -- left join temp_dict_name b on a.industryCode = b.id
            <trim prefix="where" prefixOverrides="and">
                <if test="industryCode!=null and industryCode!=''">
                    -- and b.name = #{industryCode}
                    and a.industryCode = #{industryCode}
                </if>
                <if test="areaCode!=null and areaCode!=''">
                    and a.region_code like CONCAT(#{areaCode}, '%')
                </if>
            </trim>
        )
    </select>
    <select id="getEpcDetail" resultType="java.util.Map">
        SELECT IFNULL(enterprise_id,'') epc, IFNULL((select cmpName from enterprise_info where enterpriseCode = t.enterprise_id),'') cmpName, IFNULL(round(guideline_val,4),0) tce
        from t_unitenergy2020 t where enterprise_id in(
            select enterpriseCode from enterprise_info
            <trim prefix="where" prefixOverrides="and">
                <if test="industryCode!=null and industryCode!=''">
                    and tid = #{industryCode}
                </if>
                <if test="areaCode!=null and areaCode!=''">
                    and regionCode like CONCAT(#{areaCode}, '%')
                </if>
            </trim>
        )
        and record_circle between left(#{date},7) and left(#{date},7) and energyTypeCode = '0000' and code='00000000'
        and guideline_val is not null and guideline_val &lt; 1000000 and guideline_val >0 order by round(guideline_val,4) desc
    </select>
    <select id="getEpcDetailOld" resultType="java.util.Map">
        select IFNULL(enterprise_id,'') epc, IFNULL((select enterprise_name from t_enterprise_old where enterprise_code = t.enterprise_id),'') cmpName, IFNULL(round(guideline_val,4),0) tce
        from t_unitenergy2019 t where record_circle between left(#{date},7) and left(#{date},7) and energyTypeCode='0000' and del_flag = 0 and enterprise_id in (
            select enterprise_code from t_enterprise_old a
            left join temp_dict_name b on a.industryCode = b.id
            <trim prefix="where" prefixOverrides="and">
                <if test="industryCode!=null and industryCode!=''">
                    and b.name = #{industryCode}
                </if>
                <if test="areaCode!=null and areaCode!=''">
                    and a.region_code like CONCAT(#{areaCode}, '%')
                </if>
            </trim>
        ) and guideline_val is not null order by round(guideline_val,4) desc
    </select>
    <update id="upExceptionEnterpriseInfos">
        update exception_enterprise_info
        set state = #{query.state}, remark = #{query.remark}, userId = #{query.userId}, updateTime = #{query.updateTime}
        where id = #{query.id}
    </update>

    <select id="getComprehensiveExTotal" resultType="java.lang.Integer">
        SELECT COUNT(1) FROM energy_exception_result_info
        WHERE enterpriseCode = #{epc} AND quotaName = "综合能耗" AND LEFT(statDate,7) BETWEEN #{startTime} AND #{endTime};
    </select>
    <select id="getUnitComprehensiveExTotal" resultType="java.lang.Integer">
        SELECT COUNT(1) FROM energy_exception_result_info
        WHERE enterpriseCode = #{epc} AND quotaName != "综合能耗" AND quotaName != "电力"  AND LEFT(statDate,7) BETWEEN #{startTime} AND #{endTime};
    </select>
    <select id="getElectricExTotal" resultType="java.lang.Integer">
        SELECT COUNT(1) FROM energy_exception_result_info
        WHERE enterpriseCode = #{epc} AND quotaName = "电力" AND LEFT(statDate,7) BETWEEN #{startTime} AND #{endTime};
    </select>
    <select id="getDataCollectExTotal" resultType="java.lang.Integer">
        SELECT COUNT(1) FROM energy_exception_data_info
        WHERE enterpriseCode = #{epc} AND exceptionType = "0101" AND handlerType != "0" AND LEFT(statDate,7) BETWEEN #{startTime} AND #{endTime};
    </select>
    <select id="getDataValueExTotal" resultType="java.lang.Integer">
        SELECT COUNT(1) FROM energy_exception_data_info
        WHERE enterpriseCode = #{epc} AND exceptionType NOT LIKE '%0101%' AND handlerType != "0" AND LEFT(statDate,7) BETWEEN #{startTime} AND #{endTime};
    </select>
    <select id="getComprehensiveExDetail" resultType="com.nantian.cloud.dbcapp.vo.ExceptionIndexVo">
        SELECT r.enterpriseCode as enterpriseCode,r.quotaName,r.statDate,r.dataValue,r.quotaUnit,c.exception_name, r.datumValue as reasonableRange, r.remark as exceptionDetail FROM energy_exception_result_info r LEFT JOIN quality_exception_code c ON r.exceptionType = c.exception_code
        WHERE r.enterpriseCode = #{epc} AND r.quotaName = "综合能耗" AND LEFT(r.statDate,7) BETWEEN #{startTime} AND #{endTime} ORDER BY r.statDate ASC
    </select>
    <select id="getUnitComprehensiveExDetail" resultType="com.nantian.cloud.dbcapp.vo.ExceptionIndexVo">
        SELECT r.enterpriseCode as enterpriseCode,r.quotaName,r.statDate,r.dataValue,r.quotaUnit,c.exception_name, r.datumValue as reasonableRange, r.remark as exceptionDetail FROM energy_exception_result_info r LEFT JOIN quality_exception_code c ON r.exceptionType = c.exception_code
        WHERE r.enterpriseCode = #{epc} AND r.quotaName != "综合能耗" AND r.quotaName != "电力" AND LEFT(r.statDate,7) BETWEEN #{startTime} AND #{endTime} ORDER BY r.statDate ASC
    </select>
    <select id="getElectricExDetail" resultType="com.nantian.cloud.dbcapp.vo.ExceptionIndexVo">
        SELECT r.enterpriseCode as enterpriseCode,r.quotaName,r.statDate,r.dataValue,r.quotaUnit,c.exception_name FROM energy_exception_result_info r LEFT JOIN quality_exception_code c ON r.exceptionType = c.exception_code
        WHERE r.enterpriseCode = #{epc} AND r.quotaName = "电力" AND LEFT(r.statDate,7) BETWEEN #{startTime} AND #{endTime} ORDER BY r.statDate ASC
    </select>
    <select id="getDataValueExDetail" resultType="com.nantian.cloud.dbcapp.vo.ExceptionDataVo">
        SELECT d.enterpriseCode as enterpriseCode,d.collectItemName,d.dataCode,
               (CASE d.collectionFrequency WHEN "1" THEN "天" WHEN "2" THEN "月" WHEN "3" THEN "年" ELSE NULL END) as statType,
               d.statDate,d.dataValue,
               (CASE WHEN SUBSTR(d.dataCode,9,2) IN ("01","02") THEN u.qualityUnit WHEN SUBSTR(d.dataCode,9,2) = "03" THEN des.qualityUnit ELSE '-' END) AS dataUnit, d.convertRation,c.exception_name
        FROM energy_exception_data_info d
        LEFT JOIN quality_exception_code c ON d.exceptionType LIKE CONCAT("%",c.exception_code,"%")
        LEFT JOIN dict_energy_type_unit_collect u ON SUBSTR(d.dataCode,11,4) = u.code
        LEFT JOIN dict_energy_substance des ON SUBSTR(d.dataCode,11,4) = des.code
        WHERE d.enterpriseCode = #{epc} AND d.exceptionType != "0101" AND d.handlerType != "0"
        <if test = "dataCode != null and dataCode!=''">
            AND d.dataCode =#{dataCode}
        </if>
        AND LEFT(d.statDate,7) BETWEEN #{startTime} AND #{endTime}
        ORDER BY d.statDate ASC
    </select>
    <select id="getDataCollectExDetail" resultType="com.nantian.cloud.dbcapp.vo.ExceptionDataVo">
        SELECT d.enterpriseCode as enterpriseCode,d.collectItemName,d.dataCode,
               (CASE d.collectionFrequency
                    WHEN "1" THEN
                        "天"
                    WHEN "2" THEN
                        "月"
                    WHEN "3" THEN
                        "年"
                    ELSE
                        NULL
                   END) as statType,
               d.statDate,d.dataValue,u.qualityUnit AS dataUnit,c.exception_name
        FROM energy_exception_data_info d LEFT JOIN quality_exception_code c ON d.exceptionType = c.exception_code
                                          LEFT JOIN dict_energy_type_unit_collect u ON SUBSTR(d.dataCode,11,4) = u.code
        WHERE d.enterpriseCode = #{epc} AND d.exceptionType = "0101" AND d.handlerType != "0" AND LEFT(d.statDate,7) BETWEEN #{startTime} AND #{endTime} ORDER BY d.statDate ASC
    </select>
    <select id="getEpcListByFormul" resultType="com.nantian.cloud.dbcapp.vo.AreaVO">
        select distinct enterprise_code name, formul code from formul where formul_name = #{formul} and formul != '-' and is_use = '1'
        <if test="list != null and list.size > 0">
            <foreach collection="list" item="item" open="and enterprise_code in (" close=")" separator=",">#{item}</foreach>
        </if>
    </select>

    <!--查询各能源消耗量 过滤综合能耗为负或超过1000000的企业和月份-->
    <sql id="base_energy_by_formula">
        select t.enterprise_id, t.dataCode, t.record_circle, t.energy_code, sum(consume_sum*IFNULL(e.collectRatio,t.modulus)) energy_consumption, sum(consume_sum) consume_sum, unit,
        case when energy_code in ("3300", "1100", "1200", "1300", "1400", "1500", "1700", "4200") then 0.0001 else 1 end ratio
        from `t_energy_structure` t
        left join dict_energy_type_unit_collect e on t.energy_code = e.code and e.code in ("1800","1900","2000","2100","2200","2300","2400","2500","2600","2700","2800","2900","3000","3100","3200","3300","3700","3900")
        where t.record_circle between #{startTime} and #{endTime}
        <if test = " list != null and list.size > 0 ">
            and concat(t.enterprise_id,"#",t.dataCode) in
            <foreach collection="list" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        and (t.enterprise_id,t.record_circle) in (
            SELECT distinct a.enterprise_id, a.record_circle
            from t_unitenergy2020 a where a.record_circle  between #{startTime} and #{endTime}
            and a.energyTypeCode = '0000' and a.code = '00000000' and a.isRight = "1"
            and a.guideline_val &gt; 0 and a.guideline_val &lt; 1000000
        ) group by t.enterprise_id, t.dataCode, t.record_circle -- having sum(t.consume_sum)*ratio &gt; 0 AND sum(t.consume_sum)*ratio &lt; 1000000
    </sql>
    <!--企业能源消耗中间表 数据过滤交由具体分析处理 单位tce-->
    <insert id="renewEnergyConsumptionByForuml">
        truncate table t_energy_consumption;
        insert into t_energy_consumption(enterprise_code,record_circle,energy_code,energy_consumption,consume_sum,consume_unit,energy_name,energy_category)
        select enterprise_id, record_circle, energy_code, round(sum(energy_consumption*ratio),4) energy_consumption, round(sum(consume_sum),4) consume_sum, unit,
            (select meaning from dict_energy_type_unit_collect where tmp.energy_code = code) name,
            (select IFNULL(category,meaning) from dict_energy_type_unit_collect where tmp.energy_code = code) category from (
           <include refid="base_energy_by_formula"></include>
        ) tmp group by enterprise_id, record_circle, energy_code;
    </insert>
    <!--查询政府端首页能源消费结构 过滤企业单月单个采集点折标量超出100万tce的数据 单位万tce-->
    <select id="selectAllEnergyNumByForuml" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
        select energy_code month, round(sum(energy_consumption*ratio*0.0001),4) value, (select meaning from dict_energy_type_unit_collect where tmp.energy_code = code) value1 from (
            <include refid="base_energy_by_formula"></include>
            having sum(consume_sum*IFNULL(e.collectRatio,t.modulus))*ratio &gt; 0 AND sum(consume_sum*IFNULL(e.collectRatio,t.modulus))*ratio &lt; 1000000
        ) tmp group by energy_code having value1 is not null order by value desc
    </select>
    <select id="selectAllEnergyNumByConsumption" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
        select GROUP_CONCAT(distinct energy_code) month, round(sum(energy_consumption*0.0001),4) value, energy_category value1
        from t_energy_consumption tmp where energy_consumption &gt; 0 and energy_consumption &lt; 1000000 and record_circle between #{startTime} and #{endTime}
        <if test=" list != null and list.size > 0 ">
            <foreach collection="list" item="item" index="i" open="and enterprise_code in (" separator="," close=")">#{item}</foreach>
        </if>
        group by energy_category having value1 is not null order by value desc
    </select>

    <select id="selectEnergyByFormul" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
        select record_circle month, round(sum(value),4) value from (
            select enterprise_id, dataCode, record_circle, energy_code, sum(consume_sum)*${ratio} value
            from `t_energy_structure` where record_circle between #{startTime} and #{endTime}
            <if test = " list != null and list.size > 0 ">
                and concat(enterprise_id,"#",dataCode) in
                <foreach collection="list" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            group by enterprise_id, dataCode, record_circle having sum(consume_sum)*${ratio} &gt; 0 AND sum(consume_sum)*${ratio} &lt; 1000000
        ) tmp group by record_circle order by record_circle asc
    </select>
    <select id="selectEnergyByConsumption" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
        select record_circle month, round(sum(consume_sum*${ratio}),4) value, energy_category value1
        from t_energy_consumption tmp where energy_consumption &gt; 0 and energy_consumption &lt; 1000000 and record_circle between #{startTime} and #{endTime} and energy_category = #{energyCategory}
        <if test=" list != null and list.size > 0 ">
            <foreach collection="list" item="item" index="i" open="and enterprise_code in (" separator="," close=")">#{item}</foreach>
        </if>
        group by record_circle order by record_circle asc
    </select>

    <select id="industryList" resultType="com.nantian.cloud.dbcapp.vo.AreaVO">
        select id code, name from temp_dict_name
    </select>
    <select id="areaList" resultType="com.nantian.cloud.dbcapp.vo.AreaVO">
        select case when right(code,2) = '00' then left(code,4) else code end code, tname name from dict_region where pcode = #{pcode}
    </select>
    <select id="getExceptionTotal" resultType="java.lang.String">
        SELECT COUNT(1)
        FROM energy_exception_data_info
        WHERE LEFT(statDate,7) between #{preTime} and #{endTime}
        and enterpriseCode = #{epc} and handlerType != 0
        and dataCode in (select dataCode from configure_data_collect_new where enterpriseCode = #{epc})
    </select>

    <select id="getEnterpriseFurnaceGradeValue" resultType="java.lang.Double">
        SELECT <![CDATA[ IF(dataValue <= 1, dataValue * 100, dataValue)]]>
        FROM enterprise_energy_data eed
               LEFT JOIN configure_data_collect_new cdcn ON eed.enterpriseCode = cdcn.enterpriseCode AND eed.dataCode = cdcn.dataCode
        WHERE
            eed.enterpriseCode = #{enterpriseCode} AND LEFT(eed.statDate,7) between #{startTime} and #{endTime} AND cdcn.dataCodeOld = "0000000010020382"
        LIMIT 1;
    </select>

    <select id="getPageLogListA" resultType="com.nantian.cloud.dbcapp.vo.QualityCalculateLogVo">
        SELECT
        ei.cmpName,
        qc.id, qc.operate_type, IFNULL(qc.operate_object,ei.enterpriseCode) operate_object, qc.operate_start_date, qc.operate_end_date,
        qc.formul, qc.data_value, qc.stat_date, qc.result, qc.type, qc.reason
        FROM enterprise_info ei LEFT JOIN quality_calculate_log qc ON qc.operate_object = ei.enterpriseCode and qc.operate_type = '1'
        <if test="query.type != '' and query.type != null">
            and qc.type = #{query.type}
        </if>
        <if test="query.statDate != '' and query.statDate != null">
            and qc.stat_date like CONCAT ('%',#{query.statDate}, '%')
        </if>
        <if test="query.OperateStartDate != '' and query.OperateStartDate != null">
            and qc.operate_start_date like CONCAT('%',#{query.OperateStartDate}, '%')
        </if>
        WHERE 1=1
        <if test="query.operateObject != '' and query.operateObject!= null">
            and qc.operate_object = #{query.OperateObject}
        </if>

        <if test="query.cmpName != '' and query.cmpName != null">
            and ei.cmpName like CONCAT('%',#{query.cmpName}, '%')
        </if>
        <if test="query.result != '' and query.result != null">
            <choose>
                <when test="query.result == '-1'.toString()"> and qc.id is null</when>
                <otherwise> and qc.result = #{query.result}</otherwise>
            </choose>
        </if>
        ORDER BY qc.operate_start_date DESC
    </select>


    <select id="getPageLogListB" resultType="com.nantian.cloud.dbcapp.vo.QualityCalculateLogVo">
        SELECT ei.cmpName,
        qc.id, qc.operate_type, IFNULL(qc.operate_object,qsct.operate_object) operate_object, qc.operate_start_date, qc.operate_end_date,
        qc.formul, qc.data_value, qc.stat_date, qc.result, qc.type, qc.reason
        FROM quality_supply_calculate_task qsct
        LEFT JOIN enterprise_info ei ON qsct.operate_object = ei.enterpriseCode
        LEFT JOIN quality_calculate_log qc ON qsct.operate_object = qc.operate_object and qsct.stat_date = qc.stat_date and qc.operate_type = '2'
        <if test="query.type != '' and query.type != null">
            and qc.type = #{query.type}
        </if>
        WHERE 1=1
        <if test="query.operateObject != '' and query.operateObject!= null">
            and qsct.operate_object = #{query.OperateObject}
        </if>
        <if test="query.cmpName != '' and query.cmpName != null">
            and ei.cmpName like CONCAT('%',#{query.cmpName}, '%')
        </if>
        <if test="query.statDate != '' and query.statDate != null">
            and qsct.stat_date like CONCAT ('%',#{query.statDate}, '%')
        </if>
        <if test="query.OperateStartDate != '' and query.OperateStartDate != null">
            and qsct.task_date like CONCAT('%',#{query.OperateStartDate}, '%')
        </if>
        <if test="query.result != '' and query.result != null">
            <choose>
                <when test="query.result == '-1'.toString()"> and qc.id is null</when>
                <otherwise> and qc.result = #{query.result}</otherwise>
            </choose>
        </if>
        ORDER BY qc.operate_start_date DESC
    </select>


    <select id="getPageLogListC" resultType="com.nantian.cloud.dbcapp.vo.QualityCalculateLogVo">
        SELECT
        ei.cmpName,
        qc.id, qc.operate_type, qc.operate_object, qc.operate_start_date, qc.operate_end_date,
        qc.formul, qc.data_value, qc.stat_date, qc.result, qc.type, qc.reason
        FROM quality_calculate_log qc LEFT JOIN enterprise_info ei ON qc.operate_object = ei.enterpriseCode
        WHERE qc.operate_type = '3'
        <if test="query.operateObject != '' and query.operateObject!= null">
            and qc.operate_object = #{query.OperateObject}
        </if>
        <if test="query.type != '' and query.type != null">
            and qc.type = #{query.type}
        </if>
        <if test="query.cmpName != '' and query.cmpName != null">
            and ei.cmpName like CONCAT('%',#{query.cmpName}, '%')
        </if>
        <if test="query.statDate != '' and query.statDate != null">
            and qc.stat_date like CONCAT ('%',#{query.statDate}, '%')
        </if>
        <if test="query.OperateStartDate != '' and query.OperateStartDate != null">
            and qc.operate_start_date like CONCAT('%',#{query.OperateStartDate}, '%')
        </if>
        <if test="query.result != '' and query.result != null">
            <choose>
                <when test="query.result == '-1'.toString()"> and qc.id is null</when>
                <otherwise> and qc.result = #{query.result}</otherwise>
            </choose>
        </if>
        ORDER BY qc.operate_start_date DESC
    </select>
</mapper>
