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


    <select id="getTceTotal2019" resultType="java.lang.Float">
        SELECT IFNULL(sum(guideline_val)/10000,0)  FROM t_unitenergy2019  where  record_circle BETWEEN #{startTime} and #{endTime} and energyTypeCode='0000' and enterprise_id =#{enterpriseCode} and guideline_val > 0
    </select>

    <select id="getTceTotal2020" resultType="java.lang.Float">
        SELECT IFNULL(sum(guideline_val)/10000,0)  FROM t_unitenergy2020 where  record_circle BETWEEN #{startTime} and #{endTime}
        and energyTypeCode='0000'
        and code ='00000000'
        and enterprise_id =#{enterpriseCode}
        and guideline_val > 0
    </select>

    <select id="getElectricity" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
        SELECT
        a.record_circle as month,
        sum(
        CASE a.quota_unit
        WHEN '万千瓦·时' THEN  a.guideline_val*10000
        WHEN '千瓦·时' THEN  a.guideline_val
        ELSE a.guideline_val END
        )/10000 as value
        FROM
        t_unitenergy2019 a LEFT JOIN t_enterprise_old b on a.entId = b.entId
        WHERE
        a.entid = a.unitId
        and a.enterprise_id =#{enterpriseCode}
        AND a.record_circle BETWEEN #{startTime} and #{endTime}
        AND a.CODE LIKE 'JZNH%'
        AND a.energyName IN ( "电", "电力" )
        AND a.guideline_val > 0
        AND a.guideline_val &lt; 100000000
        GROUP BY
        a.record_circle
        </select>


    <select id="getEntId" resultType="java.lang.Integer">
        SELECT entId FROM t_enterprise_old where  enterprise_code =#{enterpriseCode}
    </select>

    <select id="getCollection" resultType="java.lang.Integer">
        SELECT COUNT(ID)  FROM  collectpoint where EntID=#{entId}
    </select>

    <select id="getInstrument" resultType="java.lang.Integer">
        SELECT COUNT(ID)  FROM  history_instrument where EntID=#{entId}
    </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 =#{enterpriseCode}
        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 =#{enterpriseCode}
    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 =#{enterpriseCode}

        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 =#{enterpriseCode}

        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 =#{enterpriseCode}

        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 =#{enterpriseCode}

    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 =#{enterpriseCode}

        and del_flag=0
    </select>

    <select id="getProductName" resultType="com.nantian.cloud.dbcapp.vo.ProductNameAndIdVO">
        select DISTINCT id as id ,product_name as name  from history_product_quota where industry_code =(SELECT DISTINCT industry_code from history_enterprise_product  where  enterprise_code =#{enterpriseCode} ) and product_id  is null
    </select>

    <select id="getProductNameByEpc" resultType="java.lang.String">
        SELECT DISTINCT f.product_name
        FROM formul f
        LEFT JOIN t_standard ts ON f.formul_name = ts.quota_name
        WHERE f.type = "2" AND f.enterprise_code = #{enterpriseCode} AND f.is_use != 0 AND length(f.product_data_code)>=16
        ORDER BY CASE WHEN ts.id IS NULL THEN 1 ELSE 0 END ASC,
        CASE WHEN ts.order IS NULL OR ts.order = '' THEN 1 ELSE 0 END ASC,
        ts.order ASC
    </select>

    <!--只查询指标数据不为空和0的产品-->
    <select id="getOldProductNameByEpc" resultType="java.lang.String">
        select distinct concat(t1.product_name,'(历史)')
        from (
         select t1.enterprise_code, t2.id, t2.product_name
         from history_enterprise_product t1
         left join history_product_quota t2 on t2.industry_code = t1.industry_code
         where t2.product_id is null and t2.id is not null
         group by t1.enterprise_code, t2.id
        ) t1 left join history_product_quota t2 on t2.product_id = t1.id
        left join (
            select distinct enterprise_id, quota_name, GROUP_CONCAT(distinct record_circle) record_circle, sum(StandardValue) StandardValue from t_unitenergy2019 where code like '%ZB%' and StandardValue != 0
            group by enterprise_id, quota_name having record_circle is not null
        ) t3 on t2.quota_name = t3.quota_name and t1.enterprise_code = t3.enterprise_id
        where t2.product_id is not null and t3.record_circle is not null and t1.enterprise_code = #{enterpriseCode}
    </select>

    <select id="getProductQuotaByEpc" resultType="com.nantian.cloud.dbcapp.vo.ProductNameAndIdVO">
        SELECT f.id as id, f.formul_name as name, s.value_unit as unitName,f.use_date_start FROM formul f
        LEFT JOIN data_quality_status q ON q.enterprise_code = f.enterprise_code
        LEFT JOIN t_standard s ON f.formul_name = s.quota_name AND s.code = q.quality_industry
        WHERE f.formul_name != "综合能耗" AND f.enterprise_code = #{enterpriseCode} AND f.product_name = #{productName} AND
        not(f.`use_date_start`>#{endDate} or (#{startDate}>f.`use_date_end`)) AND f.is_use!=0
         ORDER BY f.formul_name
    </select>

    <select id="getOldProductQuotaByEpc" resultType="com.nantian.cloud.dbcapp.vo.ProductNameAndIdVO">
        select distinct t3.id, concat(t3.quota_name,'(历史)') name, t3.unitName
        from history_enterprise_product t1
        left join history_product_quota t2 on t2.industry_code = t1.industry_code
        left join history_product_quota t3 on t3.product_id = t2.id
        where t1.enterprise_code = #{enterpriseCode} and concat(t2.product_name,'(历史)') = #{productName}
    </select>

    <select id="getProductQuota" resultType="com.nantian.cloud.dbcapp.vo.ProductNameAndIdVO">
    select id as id ,quota_name as name,unitName from history_product_quota where product_id =#{id}
    </select>


    <select id="getUnitConsumption" resultType="com.nantian.cloud.dbcapp.vo.KeyValueVO">
    select quota_unit as key1 ,guideline_val as value, child_val as childVal, parent_val as parentVal from t_unitvalue where enterprise_id=#{enterpriseCode} and record_circle BETWEEN #{startTime} and #{endTime} and quota_name =#{quotaName}
    and guideline_val != 0
    </select>

    <select id="getUnitConsumptionWeightedMean" resultType="java.lang.Float">
        select case when child_val is null or parent_val is null or child_val = 0 or parent_val = 0 then
        IFNULL(sum(guideline_val)/count(guideline_val),0)
        else IFNULL(sum(child_val)/sum(parent_val),0) end guideline_val
        from t_unitvalue where enterprise_id=#{enterpriseCode} and record_circle BETWEEN #{startTime} and #{endTime} and
        quota_name = #{quotaName} and guideline_val != 0 and guideline_val is not null
        <if test="list!=null and list.length>0">
            and record_circle not in
            <foreach collection="list" item="time" open="(" separator="," close=")">
                #{time}
            </foreach>
        </if>
    </select>

    <select id="getUnitConsumption2019" resultType="com.nantian.cloud.dbcapp.vo.KeyValueVO">
        SELECT record_circle, quota_unit AS key1, SUM(StandardValue) AS VALUE FROM t_unitenergy2019
        WHERE concat(quota_name,'(历史)') = #{quotaName} AND record_circle BETWEEN #{startTime} and #{endTime} and code like '%ZB%' and enterprise_id = #{enterpriseCode}
        GROUP BY record_circle
    </select>


    <select id="getCodeByProductId" resultType="java.lang.String">
        select product_code from history_enterprise_product where enterprise_code=#{enterpriseCode} and product_id =#{productId}
    </select>

    <select id="getValueByProductCode" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">

        SELECT sum(guideline_val)/10000 as value ,record_circle as month from t_unitenergy WHERE record_circle BETWEEN
        #{startTime} and #{endTime} and code in
        <foreach collection="list1" item="items" separator="," open="(" close=")">
            #{items}
        </foreach>
        and del_flag=0
        and enterprise_id=#{enterpriseCode}
        GROUP BY record_circle
    </select>
    <!--按月统计综合能耗-->
    <select id="getZhnhByMonth" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
        SELECT record_circle as month ,guideline_val as value FROM `t_unitenergy` where enterprise_id = #{enterpriseCode} and record_circle BETWEEN #{startTime} and #{endTime} and energyTypeCode ='0000'
        GROUP BY record_circle
    </select>

    <!--按月统计综合能耗2019-->
    <select id="getZhnhByMonth2019" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
       SELECT
		t.record_circle AS MONTH,
		t.guideline_val AS VALUE
	FROM
		 history_monitorpoint b  LEFT JOIN  t_unitenergy2019 t on t.code=b.code
	WHERE
		t.unitid = #{baseCode} and record_circle BETWEEN #{startTime} and #{endTime}
		and  b.type ='ZHNH'
	GROUP BY
		record_circle
    </select>

    <!--按月统计综合能耗 2020-->
    <select id="getZhnhByMonth1" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
        SELECT record_circle as month ,guideline_val as value FROM `t_unitenergy2020` where enterprise_id = #{enterpriseCode}
        and record_circle BETWEEN #{startTime} and #{endTime} and energyTypeCode ='0000' and code='00000000'
        GROUP BY record_circle
    </select>

    <!--按月统计综合能耗列表-->
    <select id="getZhnhListByMonth" resultType="com.nantian.cloud.dbcapp.vo.ZhnhListVo">
        SELECT t1.tce, t2.yieldValue, t1.month, t2.yieldName, t2.unitName
        FROM(
            SELECT distinct a.guideline_val as  tce,  a.record_circle as month, a.quota_name  as yieldName
            FROM `t_unitenergy2019` a LEFT JOIN history_monitorpoint b ON a.CODE = b.CODE
            WHERE a.unitId = #{baseCode} and a.enterprise_id = #{enterpriseCode}
            AND b.type LIKE '%ZHNH%' AND a.record_circle BETWEEN #{startTime} and #{endTime}
        ) t1 left  JOIN (
            SELECT distinct a.guideline_val as yieldValue, a.record_circle as month, a.quota_name  as yieldName, a.quota_unit as unitName
            FROM `t_unitenergy2019` a
            LEFT JOIN history_monitorpoint b ON a.CODE = b.CODE
            WHERE a.unitId = #{baseCode}  and a.enterprise_id = #{enterpriseCode}
            AND b.type LIKE '%CP%' AND a.record_circle BETWEEN #{startTime} and #{endTime}
        ) t2 on t1.month = t2.month group by t1.month
    </select>

    <select id="getZbByenterpriseCode" resultType="com.nantian.cloud.dbcapp.vo.ZbVo">
        select a.quota_name as zb , a.product_id as productId,b.product_name as productName,a.unitName as unit from
        history_product_quota a
        LEFT JOIN history_product_quota b on a.product_id =b.id
        where a.product_id in (select product_id from history_enterprise_product where enterprise_code=#{enterpriseCode}
        ) order by zb
        <!-- select quota_name as zb , product_id as productId from history_product_quota where product_id in
		(select product_id from history_enterprise_product where enterprise_code=#{enterpriseCode}
		) -->
    </select>

    <select id="getZbByenterpriseCode2019" resultType="com.nantian.cloud.dbcapp.vo.ZbVo">
        <!-- SELECT
            a.unitId as productId,
            b.quota_name as zb
        FROM
            history_enterprise_product a
            LEFT JOIN history_product_quota b ON a.product_id = b.product_id
        WHERE
	 a.enterprise_code =#{enterpriseCode} -->
        SELECT a.unitId as productId, b.quota_name as zb, c.product_name as productName
        FROM history_enterprise_product a
        LEFT JOIN history_product_quota b ON a.product_id = b.product_id
        LEFT JOIN history_product_quota c on b.product_id =c.id
        WHERE a.enterprise_code =#{enterpriseCode}
        HAVING zb is not null and productId is not null
        order by zb
    </select>

    <select id="getDhByName" resultType="com.nantian.cloud.dbcapp.vo.DHVo">
        <!--SELECT
            sum(guideline_val )/count(id) AS VALUE,
            quota_name AS zb,
            quota_unit AS unit
        FROM
            t_unitvalue
        WHERE
            enterprise_id = #{enterpriseCode}
            AND quota_name = #{productId}
            AND record_circle BETWEEN #{startTime} and #{endTime} and guideline_val!=0
        GROUP BY
            quota_name -->
        SELECT
        sum(a.guideline_val ) / count( a.id ) AS VALUE,
        a.quota_name AS zb,
        b.unitName AS unit
        FROM
        t_unitvalue a left JOIN history_product_quota b on a.quota_name =b.quota_name
        WHERE
        a.enterprise_id = #{enterpriseCode}
        AND a.quota_name = #{productId}
        AND a.record_circle BETWEEN #{startTime} and #{endTime} and guideline_val!=0
        GROUP BY
        a.quota_name
    </select>

    <select id="getDhByName2019" resultType="com.nantian.cloud.dbcapp.vo.DHVo">
       	SELECT
	        b.unitName AS unit,
	        a.quota_name as zb,
	        a.quota_unit as unitName,
           CASE quota_unit

 WHEN '吨标准煤/吨' THEN
  sum(a.StandardValue )/count( a.enterprise_id )*1000
 ELSE
  sum(a.StandardValue )/count( a.enterprise_id )
END as value
        FROM
            t_unitenergy2019 a left join history_product_quota b on a.quota_name = b.quota_name
        WHERE
            a.uNitId =#{productId}
            AND a.quota_name=#{zb}
            AND a.record_circle BETWEEN  #{startTime} and #{endTime}
            AND a.CODE LIKE '%ZB%'
            and a.StandardValue !=0
        GROUP BY
            a.uNitId
    </select>

    <select id="getCpByproductId" resultType="com.nantian.cloud.dbcapp.vo.DHVo">
       SELECT
            sum( consume_sum ) / COUNT( id ) AS VALUE
        FROM
            t_energy_structure
        WHERE
            enterprise_id = #{enterpriseCode}
            AND dataCode IN ( SELECT data_code FROM history_enterprise_product WHERE product_id =#{productId} and enterprise_code =#{enterpriseCode} )
            AND record_circle BETWEEN #{startTime} and #{endTime}
            AND consume_sum != 0
            GROUP BY enterprise_id
    </select>

    <select id="getCpByproductId2019" resultType="com.nantian.cloud.dbcapp.vo.DHVo">
        SELECT
            a.quota_name AS zb,
            a.quota_unit AS unit,
            SUM(a.StandardValue) AS VALUE
        FROM
            t_unitenergy2019 a left join history_monitorpoint b on a.code =b.code
        WHERE
            a.uNitId= #{productId}
            AND a.record_circle BETWEEN #{startTime} and #{endTime}
            AND b.CODE LIKE '%CP%'
        GROUP BY
            a.uNitId
    </select>

    <!-- 得到企业实物量下拉框 水换算为千吨 电换算为万千瓦时 根据公式退出各项能源对应的能源编码-->
    <select id="getMaterial" resultType="com.nantian.cloud.dbcapp.vo.MaterialVo">
        select a.EnterEnergyName as name, GROUP_CONCAT(b.code) as code, d.Formula, c.cname as oldUnit,
        case when c.cname like '%万%' then c.cname
             when c.cname like '千%' and c.cname not like '千瓦%' then c.cname
             when a.EnterEnergyName like '%水%' then '千吨' else concat('万',c.cname) end unit
        from history_entenergy a
        left join history_monitorpoint b on a.id = b.EnergyID
        left join history_measureunit c on b.MunitID = c.id
        left join (select GROUP_CONCAT(Formula) Formula, UnitID from history_monitorpoint where UnitID = #{baseCode} and Type ='JZNH') d on b.UnitID = d.UnitID
        WHERE b.entId=#{entID} and b.UnitID =#{baseCode} and b.Type ='NY' and d.Formula like concat('%',b.code,'%')
        GROUP BY b.EnergyID
    </select>


    <!-- 得到企业实物量2019 水换算为千吨 电换算为万千瓦时-->
    <select id="getMaterialValue" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
        SELECT sum(
            case when a.quota_unit like '%万%' then a.guideline_val
                 when a.quota_unit like '千%' and a.quota_unit not like '千瓦%' then a.guideline_val
                 when a.quota_name like '%水%' then a.guideline_val/1000 else a.guideline_val/10000 end
        ) AS value, a.record_circle as  month  from t_unitenergy2019 a
        LEFT join history_monitorpoint b on a.UnitID=b.UnitID and a.code =b.code
        where a.UnitID =#{baseCode}
        and #{code} like concat('%',a.code,'%')
        and a.record_circle BETWEEN #{startTime} and #{endTime}
        GROUP BY a.record_circle
    </select>
    <!-- 跨年查询-->
    <select id="getMaterialValue1" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
        SELECT  sum(a.guideline_val)/10000 AS value, a.record_circle as  month  from t_unitenergy2019 a
        LEFT join history_monitorpoint b on a.UnitID=b.UnitID and a.code =b.code
        where a.UnitID =#{baseCode}
        and #{code} like concat('%',b.energyTypeCode,'%')
        and b.type='JZNH'
        and a.record_circle BETWEEN #{startTime} and #{endTime}
        GROUP BY a.record_circle
    </select>


    <!-- 得到行业code-->
    <select id="getHyCodeById" resultType="java.lang.String">
        select quality_industry from data_quality_status where enterprise_code=#{enterpriseCode}
    </select>

    <!--查询每个能源的消耗量  2020年-->
    <select id="getListValue" resultType="com.nantian.cloud.dbcapp.vo.ListValueVO">
        SELECT
            b.meaning as name,
            t.unit as  unit,
            t.energy_code as code,
            t.start_inventory as openingInventory,
            t.object  as purchaseVolume,
            sum(c.guideline_val) as total,
            t.consume_industry as productionConsumption,
            t.consume_material as forMaterials,
            t.consume_noindustry as noProductionConsumption,
            t.consume_transport   as transportTotal,
            t.end_inventory as endingInventory,
            t.modulus as ratio,
            t.ref_modulus as consultRatio
        FROM
            t_energy_structure t
            LEFT JOIN dict_energy_type_unit_collect b ON t.energy_code = b.CODE
            LEFT JOIN t_unitenergy c ON t.enterprise_id = c.enterprise_id  and c.energyTypeCode=t.energy_code
        WHERE
            t.enterprise_id = #{enterpriseCode }
						and c.code ='00000000'
            AND b.meaning IS NOT NULL
            and  t.record_circle #{startTime} and #{endTime}
        GROUP BY
            energy_code
    </select>

    <!--查询每个能源的消耗量  2019年-->
    <select id="getListValue1" resultType="com.nantian.cloud.dbcapp.vo.ListValueVO">
        SELECT
			b.name as name,
            t.quota_unit as  unit,
            f.code as code,
            t.guideline_val as total,
            sum(standardValue) as standardValue
        FROM
            history_monitorpoint b  LEFT JOIN  t_unitenergy2019 t on t.code=b.code AND b.entid =t.entid
            LEFT JOIN dict_energy_type_unit_collect f on f.meaning = t.energyName

        where 1=1 and
            b.type ='JZNH'
			and b.EntID=#{entID}
			and t.UnitID=#{baseCode}
            and  t.record_circle = #{time}
        GROUP BY b.code

    </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>

    <!--查询2020工序情况 -->
    <select id="getValueByProcedure" resultType="com.nantian.cloud.dbcapp.vo.ProcedureVO">
        SELECT distinct
            c.dataCode as configureCode,
            a.consume_sum  as value,
            b.meaning AS type,
            a.record_circle_type AS rate ,
            a.modulus as radio,
            a.unit,
            a.convertRation_unit as collectUnit,
            c.collectItemName as configureName,
            c.statType as collectType
        FROM
            configure_data_collect c
            LEFT join t_energy_structure a ON a.dataCode = c.dataCode and a.enterprise_id=c.enterpriseCode and record_circle BETWEEN  #{startTime} and #{endTime}
            LEFT join dict_energy_type_unit_collect b ON a.energy_code = b.CODE
        WHERE
            c.enterpriseCode = #{enterpriseCode} and c.dataCode like CONCAT(#{code}, '%') order by c.dataCode ASC
    </select>
    <!-- isForce 是否必传（0非必传 1必传）； isEnable 是否停用（0启用 1停用）-->
    <select id="getValueByNewProcedure" resultType="com.nantian.cloud.dbcapp.vo.ProcedureVO">
        select id, enterpriseCode, configureCode, configureName, radio, value, energyUseType, collectType, unit, collectUnit, type, name, isForce, isEnable, thresholdInterval
        from(
            select distinct c.id, c.enterpriseCode, c.dataCode as configureCode, c.collectItemName as configureName, t.modulus radio, t.consume_sum value,
            (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,
            (select distinct case when isForce = '0' then '0' else '1' end isForce from configure_data_collect_new where c.enterpriseCode = enterpriseCode and c.dataCode = dataCode) isForce,
            (select distinct thresholdInterval from configure_data_collect_new where c.enterpriseCode = enterpriseCode and c.dataCode = dataCode) thresholdInterval,
            (select case when GROUP_CONCAT(id) is null then '0' else '1' end isEnable from configure_data_collect_enable where c.enterpriseCode = enterpriseCode and c.dataCode = dataCode
                and delFlag = '1' and left(#{startTime},7) between left(startTime,7) and left(IFNULL(NULLIF(endTime, ''), NOW()),7) ) isEnable
            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)
            left join t_energy_structure t on t.dataCode = c.dataCode and enterprise_id = #{enterpriseCode} and
            record_circle = #{startTime}
            where c.enterpriseCode = #{enterpriseCode} order by c.dataCode ASC
        ) tmp where 1 = 1
        <if test="energyType != null and energyType != ''">
            and type like concat('%',#{energyType},'%')
        </if>
        <if test="energyUseType != null and energyUseType != ''">
            and energyUseType like concat('%',#{energyUseType},'%')
        </if>
        <if test="configureName != null and configureName != ''">
            and configureName like concat('%',#{configureName},'%')
        </if>
        <if test="configureCode != null and configureCode != ''">
            and configureCode like concat('%',#{configureCode},'%')
        </if>
    </select>

    <!--查询2019工序情况 -->
    <select id="getValueByProcedure1" resultType="com.nantian.cloud.dbcapp.vo.ProcedureVO">
        select
            c.unitId  as unitId,
            b.name as name,
            a.name as configureName,
            sum(c.guideline_val) as value,
            c.record_circle_type AS rate,
            c.quota_unit as unit,
			f.code as type,
			c.pointId as configureCode
            from  history_monitorpoint a LEFT JOIN history_energy_accountunit b on a.unitId =b.id
            LEFT JOIN t_unitenergy2019 c on a.code =c.code and a.entid =c.entid
            LEFT JOIN history_entenergy d on a.EnergyID =d.id
			left join dict_energy_type_unit_collect f on f.meaning = d.EnterEnergyName
            WHERE  a.entId=#{entID}
            AND c.record_circle =#{time}
            GROUP BY a.code ,a.unitID
            ORDER BY b.name asc

    </select>

    <!--查询2020采集点 -->
    <select id="getCollectionValue" resultType="com.nantian.cloud.dbcapp.vo.CollectValueVO">
    	select
    	    a.id as id,
            a.meterName as unitName,
            a.precisionLevel as level,
            a.location as address,
            a.dataCode as code,
            a.manageNo as manageNo,
            c.collectItemName as name,
            a.ranges as ranges,
            a.model as model
            from configure_gauge a
            INNER join  configure_data_collect c ON a.enterpriseCode = c.enterpriseCode and a.dataCode = c.dataCode
            where  a.enterpriseCode =#{enterpriseCode }
            and a.dataCode=#{code }
    </select>

    <!--查询2019采集点 -->
    <select id="getCollectionValue1" resultType="com.nantian.cloud.dbcapp.vo.CollectValueVO">
        	select
        	    id as id,
        	    Specification as model,
                name as name,
                ManageDepartment as address,
                instrumentLevel as level,
                MaxRange as ranges,
                ManufacturerNumber as manageNo
                from history_instrument where
                EntID =#{entID}
                and unitid=#{code}
    </select>

    <select id="getUnitEnergyConsumption" resultType="com.nantian.cloud.dbcapp.vo.OutputValueVo">
        select IFNULL(record_circle,#{year}) as recordCircle, output_value as value, output_value_unit as unit
        from t_output_value where enterprise_code = #{enterpriseCode} and record_circle = #{year}
    </select>

    <select id="getExceptionInfo" resultType="com.nantian.cloud.dbcapp.vo.ExceptionInfoVo">
        select enterpriseCode, dataCode, collectItemName, convertRation, dataValue, statDate,
        case when #{exceptionType} like '%0101%' then '数据传输异常' else '波动异常' end exceptionType
        from energy_exception_data_info where enterpriseCode = #{enterpriseCode} and handlerType != 0
        <choose>
            <when test="exceptionType!=null and exceptionType=='1'.toString()">
                and exceptionType like '%0101%'
            </when>
            <otherwise>
                and exceptionType not like '%0101%'
            </otherwise>
        </choose>
        <if test="startTime!=null">
            and statDate between #{startTime} and #{endTime}
        </if>
        limit 200
    </select>


    <select id="selectInfoBycode" resultType="com.nantian.cloud.dbcapp.vo.EnterpriseInfoVO">
        SELECT cmpName AS NAME,
        enterpriseCode AS CODE,
        address AS address,
        regionCode as regionCode,
isorg,orgType,orgCode,corporationName,license,organization,registerDate,registerPrincipal,typeCode,industryCode,
fieldCode,center,jgzh,latitude,longitude,phone,fax,email,url,productionLine,leadingProduct,groupName,groupAddress,
uploadTime,remark,updateSay,dianliFlag,
        (select tname from dict_region where code = concat(left(e.regionCode,4),'00')) as addName,
        energyConsumeLevel as energyConsumeLevel
        FROM enterprise_info e
        WHERE enterpriseCode =  #{enterpriseCode}
    </select>

    <!--老数据-->
    <select id="selectInfoBycode1" resultType="com.nantian.cloud.dbcapp.vo.EnterpriseInfoVO">
        SELECT a.enterprise_code as code,
        a.enterprise_name as name,
        a.region_code as regionCode,
        (select tname from dict_region where code = concat(left(a.region_code,4),'00')) as addName,
        b.UnitAdress as address
        from t_enterprise_old a
        LEFT JOIN energycconsumptionunit b on a.entID=b.id
        where a.enterprise_code =#{enterpriseCode}
    </select>

    <select id="selectInfoByAccess" resultType="com.nantian.cloud.dbcapp.vo.EnterpriseInfoVO">
        SELECT a.enterprise_code as code,
        a.cmp_name as name,
        right(a.region_code,6) as regionCode,
        (select tname from dict_region where code = concat(left(right(a.region_code,6),4),'00')) as addName
        from access_enterprise_info a  where a.enterprise_code = #{enterpriseCode}
    </select>

    <!--查询指标值排序-->
    <select id="getOrderByQuota" resultType="com.nantian.cloud.dbcapp.vo.QuotaVO">
        select
        t.cmpName as enterpriseName, t.enterprise_id as code, sum(t.guideline_val*IFNULL(t.parent_val,1))/sum(IFNULL(t.parent_val,1)) as value
        from (select r1.enterprise_id,r1.child_val, r1.parent_val, r1.record_circle, r1.quota_name, r1.cmpName, r1.guideline_val,r2.enterpriseCode,eh.epc
        from (select a.enterprise_id, a.child_val, a.quota_name, a.parent_val, a.record_circle, b.cmpName, a.guideline_val
        from t_unitvalue a left join enterprise_info b on a.enterprise_id =b.enterpriseCode
        where a.record_circle between #{startTime} and #{endTime} and a.quota_name = #{quotaName}
        and b.regionCode like concat(#{addressCode},'%') and a.parent_val != 0 and a.guideline_val != 0 and a.guideline_val is not null
        <if test="shieldList!=null and shieldList.size>0">
            and (a.enterprise_id,a.record_circle) not in
            <foreach collection="shieldList" index="i" item="shield" open="(" separator="union" close=")">
                select #{shield.enterpriseCode}, #{shield.shieldStr}
            </foreach>
        </if>
        ) r1
        left join (select enterpriseCode, statDate, quotaName, dataValue from energy_exception_result_info where quotaName = #{quotaName} and statDate between #{startTime} and #{endTime}) r2
        on r1.enterprise_id = r2.enterpriseCode and r1.quota_name = r2.quotaName and r1.record_circle = r2.statDate
        left join jly.enterprise_halt eh
        on eh.epc = r1.enterprise_id and left(eh.start_time, 7) &lt;= r1.record_circle and (left(eh.regain_time,7) >= r1.record_circle or eh.regain_time is null) and eh.state in ("3","4")
        having r2.enterpriseCode is null and eh.epc is null
        ) t GROUP BY t.enterprise_id HAVING value != 0 ORDER BY value asc
    </select>

    <select id="getOrderByQuota1" resultType="com.nantian.cloud.dbcapp.vo.QuotaVO">
          SELECT
 a.enterprise_id AS CODE,

 CASE quota_unit

 WHEN '吨标准煤/吨' THEN
  sum(a.StandardValue )/count( a.enterprise_id )*1000
 ELSE
  sum(a.StandardValue )/count( a.enterprise_id )
END as value ,

 b.enterprise_name AS enterpriseName
FROM
 t_unitenergy2019 a
 LEFT JOIN t_enterprise_old b ON a.enterprise_id = b.enterprise_code
WHERE
 a.quota_name =#{quotaName}
 AND a.record_circle BETWEEN #{startTime}
 AND #{endTime}
 AND b.region_code LIKE CONCAT( #{addressCode}, '%' )
 AND a.StandardValue != 0
GROUP BY
 enterprise_id
HAVING

VALUE
 != 0
ORDER BY
VALUE
ASC
    </select>

    <select id="getIndexName2020" resultType="com.nantian.cloud.dbcapp.vo.EnterpriseProcessVo">
         select processName,processCode from configure_process where enterpriseCode = #{enterpriseCode} order by processCode asc
     </select>

    <select id="getIndexName2019" resultType="com.nantian.cloud.dbcapp.vo.EnterpriseProcessVo">
        select id as processCode,name as processName from history_energy_accountunit where UnitID=#{entID}
    </select>

    <select id="getCollection2020" resultType="int">
        select count(dataCode) from configure_data_collect where enterpriseCode = #{enterpriseCode}
    </select>

    <select id="getInstrument2020" resultType="int">
        select count(id) from configure_gauge where enterpriseCode = #{enterpriseCode}
    </select>

    <select id="getZhnhListByMonth2020" resultType="com.nantian.cloud.dbcapp.vo.ZhnhListVo">
        select a.guideline_val as tce  ,a.record_circle as `month`  from t_unitenergy2020 a
        where a.enterprise_id = #{param1} and a.record_circle between #{param2} and #{param3} and a.code = #{param4} and a.energyTypeCode = "0000"
        order by record_circle
    </select>

    <select id="getListValue2020" resultType="com.nantian.cloud.dbcapp.vo.ListValueVO">
        select distinct a.StandardValue as total, a.convertRation as ratio ,a.energyTypeCode as code, b.collectUnit as collectUnit,
        case when a.StandardValue = 0 then 0 else 1 end state, a.guideline_val standardValue
        from t_unitenergy2020 a left join dict_energy_type_unit_collect b on a.energyTypeCode = b.code
        where a.enterprise_id = #{param1} and a.record_circle =#{param2} and a.code = #{param3} and a.energy_type_code in ('01','02') -- and a.energyTypeCode != '0000'
        order by code,state,total asc
    </select>

    <select id="getForMaterial" resultType="com.nantian.cloud.dbcapp.vo.ListValueVO">
		select substr(t1.dataCode,11,4) code, IFNULL(sum( dataValue*rate),0) forMaterials, IFNULL(sum(dataValue*convertRation)/sum(dataValue),1) ratio
		from (select enterpriseCode, dataCode from configure_data_collect_new where enterpriseCode =  #{enterpriseCode} and dataCodeOld like concat(#{baseCode},'%') and isMaterial = 1) t1
		left join enterprise_energy_data t2 on t1.enterpriseCode = t2.enterpriseCode and t1.dataCode = t2.dataCode and left(statDate,7) = #{time}
		group by substr(t1.dataCode,11,4)
    </select>

    <select id="getElectricity2020" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
           SELECT sum(StandardValue)/10000 as value ,record_circle as month FROM t_unitenergy2020 where  record_circle BETWEEN #{startTime} and #{endTime} and energyTypeCode='3300' and enterprise_id =#{enterpriseCode}
            and del_flag= 1 and code = "00000000"
            GROUP BY record_circle
    </select>

    <select id="getMaterial2020" resultType="com.nantian.cloud.dbcapp.vo.MaterialVo">
        select distinct a.energyTypeCode as `code`, b.meaning as `name`, SUBSTRING_INDEX(b.collectUnit,"/",-1) as unit
        from t_unitenergy2020 a join dict_energy_type_unit_collect b on a.energyTypeCode = b.id
        where energy_type_code in ('01','02') and a.energyTypeCode != '0000' and a.code like concat( #{baseCode},'%') and a.enterprise_id = #{enterpriseCode}
        and record_circle between #{startTime} and #{endTime}
        -- and energyTypeCode not in ("0000","4000","4200","4300","4500","4600","4700","4400","4401","4402","4403","4404")
    </select>

    <select id="getMaterialValue2020" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
        select sum(StandardValue) as `value`,record_circle as `month` from t_unitenergy2020 where enterprise_id = #{enterpriseCode}
        and #{energyTypeCode} like concat('%',energyTypeCode,'%') and `code` = #{baseCode} and record_circle between
        #{startTime} and #{endTime} group by record_circle
    </select>

    <select id="getCodeByProductId2020" resultType="string">
        select data_code from history_enterprise_product where enterprise_code=#{enterpriseCode} and product_id =#{productId}
    </select>

    <select id="getValueByProductCode2020" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
        SELECT sum(consume_sum)/10000 as value ,record_circle as month from t_energy_structure WHERE record_circle
        BETWEEN #{startTime} and #{endTime} and dataCode in
        <foreach collection="list1" item="items" separator="," open="(" close=")">
            #{items}
        </foreach>
        and enterprise_id=#{enterpriseCode}
        GROUP BY record_circle
    </select>


    <select id="getTotalMaterial" resultType="java.lang.Double">
         SELECT
        sum(a.StandardValue)/10000
        FROM
        t_unitenergy2019 a
        LEFT JOIN history_monitorpoint b ON a.CODE = b.CODE
        WHERE 1=1
        and a.unitId =#{entId}
        AND b.type = 'JZNH'
        AND a.energyName LIKE concat ('%',#{code},'%')
        and a.record_circle BETWEEN #{startTime} and #{endTime}
    </select>


    <select id="getunitIdByCode" resultType="java.lang.Integer">
        SELECT a.unit_id
        FROM history_enterprise_product a
        LEFT JOIN history_product_quota b ON a.product_id = b.product_id
        WHERE concat(b.quota_name,'(历史)') = #{quotaName} AND a.enterprise_code = #{enterpriseCode}
      </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.energy_type_code in ('01','02') and a.energyTypeCode != '0000' and a.code like concat(#{process},'%') and a.enterprise_id = #{enterpriseCode}
    </select>

    <select id="selectEnergyValues" resultType="double">
        select round(IFNUll(sum(guideline_val)/10000,0),4)  from t_unitenergy2020 where enterprise_id =#{enterpriseCode}
        and energyTypeCode = #{kinds}
        and `code` = "00000000" and record_circle between #{startTime} and #{endTime} and energy_type_code in ('01','02')
        and  record_circle in (
            SELECT record_circle FROM t_unitenergy2020
            WHERE record_circle BETWEEN #{startTime} AND #{endTime} AND guideline_val > 0 AND energyTypeCode = '0000' AND code = '00000000' AND enterprise_id = #{enterpriseCode}
        )
    </select>

    <!--能源消费结构 只查询综合能耗为正数的月份 unitId只查询全厂的能耗(第一个工序就是全厂)-->
    <select id="selectEnergyValues2019" resultType="com.nantian.cloud.dbcapp.vo.KeyValueVO">
        SELECT round(IFNULL(sum(a.StandardValue)/10000,0),4) as value,  a.energyName AS key1
        FROM t_unitenergy2019 a
        LEFT JOIN history_monitorpoint b ON a.CODE = b.CODE and a.unitId = b.UnitID
        WHERE 1=1 and a.entid = #{entId} AND b.type = 'JZNH' and a.record_circle BETWEEN #{startTime} and #{endTime}
        and a.record_circle in (
             SELECT record_circle FROM t_unitenergy2019  where record_circle BETWEEN #{startTime} and #{endTime} and energyTypeCode='0000' and entid = #{entId} and guideline_val > 0
        ) and a.unitId = (
            select IFNULL(id,0) from history_energy_accountunit where unitId = #{entId} limit 0,1
        )
        GROUP BY a.energyName HAVING value != 0
        ORDER BY VALUE  desc
    </select>

    <select id="getInstrumentById2020" resultType="com.nantian.cloud.dbcapp.vo.InstrumentVO">
        select * from configure_gauge where id =#{id}
    </select>

    <select id="getInstrumentById2019" resultType="com.nantian.cloud.dbcapp.vo.InstrumentVO">
        select a.Specification as model,
        a.Manufacturer as installer,
        a.ManufacturerNumber as serNo,
        a.instrumentLevel as meterLevel,
        a.ManageDepartment as location,
        a.addTime as installTime,
        a.Precision as precisionLevel,
        a.Range as ranges
         from history_instrument a where id = #{id}
    </select>

    <select id="getTotalZhnh2019" resultType="java.lang.Float">
        SELECT
            sum(a.guideline_val ) as  total,
            a.record_circle as month,
            a.quota_name  as yieldName
        FROM
            `t_unitenergy2019` a
            LEFT JOIN history_monitorpoint b ON a.CODE = b.CODE
        WHERE
            a.unitId = #{baseCode}
            AND b.type = 'ZHNH'
            AND a.record_circle =  #{time}
        GROUP BY
            a.record_circle

    </select>

    <select id="selectProductById" resultType="string">
        select distinct data_code from history_enterprise_product  where enterprise_code = #{param1} and base_code = #{param2}
    </select>

    <select id="selectProductByIds" resultType="java.lang.String">
        select distinct data_code from history_enterprise_product  where enterprise_code = #{param1}
    </select>
    <select id="selectProductInfos" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
        select distinct a.product_name as `month` ,a.unitName as `value` from history_product_quota a left join history_enterprise_product b
        on a.id = b.product_id where b.enterprise_code = #{param1}
    </select>
    <select id="selectProductNum" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
        select sum(consume_sum) AS `value`,record_circle AS `month` from t_energy_structure where enterprise_id = #{param1}
        and dataCode = #{param2} and record_circle between #{param3} and #{param4} group by record_circle
        order by record_circle asc
    </select>

    <select id="selectProductNums" resultType="com.nantian.cloud.dbcapp.vo.StructureVo">
        select sum(consume_sum) AS `value`,left(record_circle,7) AS `month`,unit from t_energy_structure where enterprise_id = #{param1}
        and #{param2} like concat("%",dataCode,"%") and record_circle between #{param3} and #{param4} group by left(record_circle,7)
        order by left(record_circle,7) asc
    </select>

    <select id="selectProductName" resultType="java.lang.String">
        select product_name
            from history_product_quota q
            left join history_enterprise_product p on q.id = p.product_id
            where p.enterprise_code = #{enterpriseCode} and data_code = #{dataCode}
    </select>

    <select id="selectProductInfo" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
        select distinct a.product_name as `month` ,a.unitName as `value` from history_product_quota a left join history_enterprise_product b
        on a.id = b.product_id where b.enterprise_code = #{param1} and b.base_code = #{param2}
    </select>

    <!-- 得到对应企业的截止时间 -->
    <select id="getEndTime" resultType="java.lang.String">
        select endTime from history_config_endtime where enterpriseCode= #{enterpriseCode}
    </select>

    <select id="getValueByProductCodeByYear" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
        SELECT sum(guideline_val)/10000 as value ,substring(record_circle,1,4) as month from t_unitenergy WHERE
        record_circle like #{startTime} and code in
        <foreach collection="list" item="items" separator="," open="(" close=")">
            #{items}
        </foreach>
        and del_flag=0
        and enterprise_id=#{enterpriseCode}
        GROUP BY substring(record_circle,1,4)
    </select>


    <select id="getValueByProductCodeByYear2020" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
        SELECT sum(consume_sum)/10000 as value ,substring(record_circle,1,4) as month from t_energy_structure WHERE
        record_circle like #{startTime} and dataCode in
        <foreach collection="list" item="items" separator="," open="(" close=")">
            #{items}
        </foreach>
        and enterprise_id=#{enterpriseCode}
        GROUP BY substring(record_circle,1,4)
    </select>

    <select id="getUnitIdByBaseCode" resultType="java.lang.Integer">
     select UnitID from configure_process where enterpriseCode =#{enterpriseCode}  and processCode =#{baseCode}
    </select>

    <select id="getStandardVO" resultType="com.nantian.cloud.dbcapp.vo.StandardVO">
       SELECT
        limit_value as limitValue,
        advanced_value as advancedValue,
            CASE when   type=0  then  "国家标准"
             ELSE  "省级标准"  end as type
        FROM
            t_standard
        WHERE
            CODE =#{code}
            AND quota_name =#{quotaName}
    </select>

    <select id="getStandardByType" resultType="com.nantian.cloud.dbcapp.vo.StandardVO">
        select limit_value as limitValue,admit_value as admitValue, advanced_value as advancedValue, benchmarking_level as benchmarkingLevel,
               datum_plane as datumPlane, db_limit_value as dbLimitValue, db_admit_value as dbAdmitValue, db_advanced_value as dbAdvancedValue,
               implement_data as implementData,furnace_grade as furnaceGrade,adjusted_value as adjustedValue,
               case SUBSTR(reference_standards,1,2) when 'GB' then '国标' else '行标' end standardTypeName
        from t_standard where  code =#{code} and quota_name =#{quotaName}
    </select>
    <select id="getEnergyData" resultType="java.lang.Float">
    select AVG(dataValue) as AdataValue from enterprise_energy_data
        where enterpriseCode = #{enterpriseCode} and dataCode = #{quotaCode} and
         substring(statDate,1,7) between #{startTime} and #{endTime}
    </select>

    <select id="selectDetailByDataCodeName" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
        select (dataValue * rate) as 'value', left(statDate,10) as `month`,
        case isAdditional when '1' then '补传' when '2' THEN '修正' END isAdditional
        from enterprise_energy_data
        where enterpriseCode = #{enterpriseCode} and dataCode = #{dataCode}
        and left(statDate,7) = #{time}
        GROUP BY  substring(statDate,1,10)
        order by substring(statDate,1,10)


    </select>


    <select id="selectUnit" resultType="string">
        select qualityUnit from dict_energy_type_unit_collect
        where  code = #{dataCode} limit 1
    </select>

    <select id="selectDetailMonthByDataCodeName" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
        select SUM(dataValue) as 'value',LEFT(statDate,7) as `month` from enterprise_energy_data
        where enterpriseCode = #{enterpriseCode} and dataCode = #{dataCode} and statDate like concat(#{time},'%') GROUP BY `month` order by LEFT(statDate,7)
    </select>

    <!--selectDetailByDataCodeName1-->
    <select id="selectDetailByDataCodeName1" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
        <foreach collection="list" separator="union" item="item">
            select IFNULL(sum(dataValue * rate),'0') as 'value', #{item} as `month`,
            case isAdditional when '1' then '补传' when '2' THEN '修正' END isAdditional
            from enterprise_energy_data where enterpriseCode = #{enterpriseCode} and dataCode = #{dataCode} and statDate
            like concat(#{item},'%')
        </foreach>
        order by `month` asc
    </select>

    <!--selectDetailByDataCodeName2    -->
    <select id="selectDetailByDataCodeName2" resultType="java.lang.String">
        select substring(statDate,1,4) as `year` from enterprise_energy_data
        where enterpriseCode = #{enterpriseCode} and dataCode = #{dataCode} and dataValue is not null order by substring(statDate,1,4)
    </select>

    <select id="selectDetailByDataCodeName3" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
        <foreach collection="list" separator="union" item="item">
            select IFNULL(sum(dataValue * rate),'0') as 'value', #{item.month} as `month`,
            case isAdditional when '1' then '补传' when '2' THEN '修正' END isAdditional
            from enterprise_energy_data
            where enterpriseCode = #{enterpriseCode}
            and dataCode = #{dataCode}
            and DATE_FORMAT(statDate,'%Y-%m-%d') BETWEEN #{item.startDate} and #{item.endDate}
        </foreach>
        order by `month` asc
    </select>

    <select id="selectDetailByDataCodeNameall" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
        select dataValue as 'value', substring(statDate,1,10) as `month` from enterprise_energy_data
        where enterpriseCode = #{enterpriseCode} and dataCode = #{dataCode} order by substring(statDate,1,10)
    </select>
    <select id="selectUnitByProductName" resultType="string">
        select distinct unitName from history_product_quota where product_name = #{productName}
    </select>

    <select id="selectUnitByProductId" resultType="string">
        select unitName from history_product_quota where id = #{productName}
    </select>

    <select id="selectSubName" resultType="string">
        select meaning from dict_energy_substance where code =#{energyType}
    </select>

    <select id="isSuper" resultType="string">
        select is_son from t_super_enterprise_info where epc_code = #{enterpriseCode}
    </select>

    <select id="getSonEpcList" resultType="string">
        select distinct epc_code from t_super_enterprise_info where super_tree_code = #{enterpriseCode}
        and is_delete = '0'
    </select>

    <select id="superTotal" resultType="float">
        select sum(guideline_val)/10000 from t_unitenergy2020 where enterprise_id in
        <foreach collection="list" item="data" open="(" separator="," close=")">
            #{data}
        </foreach>
        and record_circle between #{beginTime} and #{endTime}
        and energyTypeCode='0000'
        and code ='00000000'
    </select>

    <select id="superEnergyKinds" resultType="com.nantian.cloud.dbcapp.vo.StringAndFolat">
        select sum(guideline_val)/10000 as `value` , b.meaning as `month` from t_unitenergy2020 a left join
        dict_energy_type_unit_collect b on a.energyTypeCode = b.code where enterprise_id in
        <foreach collection="list" item="data" open="(" separator="," close=")">
            #{data}
        </foreach>
        and record_circle between #{beginTime} and #{endTime}
        and a.code ='00000000' and energy_type_code in ('01','02') and energyTypeCode != '0000'
        group by energyTypeCode
    </select>

    <select id="superElectricConsume" resultType="com.nantian.cloud.dbcapp.vo.StringAndFolat">
        select sum(StandardValue) as `value`, record_circle as month from t_unitenergy2020 where enterprise_id in
        <foreach collection="list" item="data" open="(" separator="," close=")">
            #{data}
        </foreach>
        and record_circle between #{beginTime} and #{endTime}
        and energyTypeCode='3300'
        and code ='00000000'
        group by record_circle
    </select>

    <select id="superElectricConsumeCompany" resultType="com.nantian.cloud.dbcapp.vo.StringAndFolat">
        select sum(dataValue)/10000 as `value`, statDate as month from t_electricity_data_value where enterpriseCode in
        <foreach collection="list" item="data" open="(" separator="," close=")">
            #{data}
        </foreach>
        and statDate between #{beginTime} and #{endTime}
        group by statDate
    </select>

    <select id="getCollectNum" resultType="int">
        select count(*) from configure_data_collect where enterpriseCode in
        <foreach collection="list" item="data" open="(" separator="," close=")">
            #{data}
        </foreach>
    </select>

    <select id="getToolNum" resultType="int">
        select count(*) from configure_gauge where enterpriseCode in
        <foreach collection="list" item="data" open="(" separator="," close=")">
            #{data}
        </foreach>
    </select>

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

    <select id="selectSuperProductInfo" resultType="com.nantian.cloud.dbcapp.vo.SuperProductInfoBo">
        select enterprise_code as enterpriseCode ,a.product_id as productId,data_code as dataCode,product_name
        as productName,cmpName,unitName as unit  from history_enterprise_product a left join history_product_quota b on a.product_id = b.id
        left join enterprise_info c on a.enterprise_code = c.enterpriseCode
        where a.enterprise_code = #{epc}
    </select>

    <select id="selectProductValue" resultType="float">
        select sum(consume_sum) from t_energy_structure where enterprise_id = #{epc}  and
        dataCode = #{dataCode} and record_circle between #{beginTime} and #{endTime}
    </select>

    <select id="superProductValueOut" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
        select sum(output_value) as value , record_circle as month from t_output_value where enterprise_code in
        <foreach collection="list" item="data" open="(" separator="," close=")">
            #{data}
        </foreach>
        and record_circle = #{year}
    </select>

    <select id="selectTreeEpc" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
                select epc_name as month ,epc_code as value  from t_super_enterprise_info where super_tree_code = #{enterpriseCode} AND is_son != '1'
    </select>
    <select id="getSuperExceptionList" resultType="com.nantian.cloud.dbcapp.vo.ExceptionInfoVo">
        select DISTINCT (a.enterpriseCode),a.dataCode
        ,a.convertRation,a.dataValue,a.exceptionType,a.statDate,a.collectItemName,b.cmpName as enterpriseName
        ,c.qualityUnit as enterpriseUnit ,c.collectUnit ,
        a.id,a.remark,a.exceptionGov from
        energy_exception_data_info as a left join enterprise_info as b on a.enterpriseCode = b.enterpriseCode left join
        dict_energy_type_unit_collect c
        on substring(a.dataCode,11,4) = c.code
        where a.enterpriseCode in
        <foreach collection="list" item="data" open="(" separator="," close=")">
            #{data}
        </foreach>
        and statDate between #{beginTime} and #{endTime}
        <if test="type !=null">
            and a.exceptionType = #{type}
        </if>
        <if test="collectName!=null">
            and a.collectItemName like concat("%",#{collectName},"%")
        </if>
        <if test="state!=null">
            and a.exceptionGov = #{state}
        </if>
    </select>

    <select id="selectUpdateTag" resultType="float">
        select object from t_energy_structure where enterprise_id = #{enterpriseCode}
        and dataCode = #{dataCode}
        and record_circle = #{time}
    </select>

    <select id="selectMonthUpdateTag" resultType="int">
        select count(*)  from t_energy_structure where enterprise_id = #{enterpriseCode}
        and dataCode = #{dataCode}
        and record_circle like concat(#{time},'%')
    </select>

    <!--selectUpdateTag1-->
    <select id="selectUpdateTag1" resultType="float">
        select object from t_energy_structure where enterprise_id = #{enterpriseCode}
                                                and dataCode = #{dataCode}
                                                and record_circle = #{time}
    </select>
    <select id="selectUpdateTagall" resultType="float">
        select object from t_energy_structure where enterprise_id = #{enterpriseCode}
                                                and dataCode = #{dataCode}
    </select>
    <!--selectUpdateTag2 -->
    <select id="selectUpdateTag2" resultType="float">
        select object from t_energy_structure where enterprise_id = #{enterpriseCode}
                                                and dataCode = #{dataCode}
    </select>
    <select id="selectElectricityDetail" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
        select `data` *10000 as `value` ,`time` as `month` from company_data_detail where
        enterpriseCode = #{enterpriseCode} and `time` like concat(#{time},'%')
        order by `time` asc
    </select>

    <select id="selectMonthElectricityDetail" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
        select SUM(`data` *10000) as `value` , left(`time`,7) as `month` from company_data_detail where
        enterpriseCode = #{enterpriseCode} and `time` like concat(#{time},'%')
        GROUP BY `month` order by `month` asc
    </select>

    <!--    selectElectricityDetail1-->
    <select id="selectElectricityDetail1" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
        <foreach collection="list" separator="union" item="item">
            select case when `data` is not null then `data`*10000 else 0 end `value`, #{item} as `month`
            from company_data_detail where enterpriseCode = #{enterpriseCode} and `time` like concat(#{item},'%')
        </foreach>
        order by `month` asc
    </select>
    <!--    selectElectricityDetail2-->
    <select id="selectElectricityDetail2" resultType="java.lang.String">
        select `time` as `year` from company_data_detail where
            enterpriseCode = #{enterpriseCode} and `data` is not null
        order by `time` asc
    </select>
    <select id="selectElectricityDetailall" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
        select `data` *10000 as `value` ,`time` as `time` from company_data_detail where
            enterpriseCode = #{enterpriseCode}
        order by `time` asc
    </select>
    <select id="getQuotaShield" resultType="java.lang.String">
        select IFNULL(quotaShield,'') quotaShield from enterprise_info where enterpriseCode = #{enterpriseCode}
    </select>
    <select id="getQuotaShieldList" resultType="com.nantian.cloud.dbcapp.vo.QuotaShieldVo">
        select enterpriseCode,quotaShield shieldStr from enterprise_info where quotaShield is not null and quotaShield != '' and regionCode like concat(#{addressCode},'%')
    </select>
    <select id="getMaterialElectricOld" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
    select record_circle month,  round(consume_sum/10000,4) value, t.dataCode from `t_energy_structure` t, (
        select dataCode, case when SUBSTRING(dataCode,15,2) = '11' then 1 when SUBSTRING(dataCode,15,2) = '20' then 2 when SUBSTRING(dataCode,15,2) = '21' then 3 when SUBSTRING(dataCode,15,2) = '10' then 4 else 5 end num
        from configure_data_collect
        where enterpriseCode = #{enterpriseCode} and SUBSTRING(dataCode,1,8)  = #{baseCode}
        and SUBSTRING(dataCode,11,4) = #{energyTypeCode} and SUBSTRING(dataCode,15,2) in ("11","20","21","10") order by num asc limit 0,1
    ) c where enterprise_id =  #{enterpriseCode} and record_circle between left(#{startTime},7) and left(#{endTime},7) and t.dataCode = c.dataCode
    </select>
    <select id="getstatType" resultType="java.lang.String">
        select statType from configure_data_collect WHERE enterpriseCode =#{enterpriseCode} and dataCode=#{dataCode}
    </select>
    <!--企业全厂 产品信息 04非能源类产品 01能源类产品-->
    <select id="selectMaterielProduct" resultType="com.nantian.cloud.dbcapp.vo.MaterialVo">
		select distinct produceName name, produceNo code, produceUnit unit, '04' type, case when t2.produceType in ('P','M') then'产品' when t2.produceType = 'S' then '物料' else '' end category
        from configure_data_collect_new t1
        left join company_materiel_product t2 on t2.enterpriseCode = t1.enterpriseCode and t2.produceNo = substr(t1.dataCodeOld,11,4)
        where left(t1.dataCodeOld,8) = '00000000' and substr(t1.dataCodeOld,9,2) = '04' and t1.enterpriseCode = #{enterpriseCode}
        union all
        select distinct t2.meaning name, substr(t1.dataCodeOld,11,4) code, t2.qualityUnit unit, '01' type, '产品' category
        from configure_data_collect_new t1
        left join dict_energy_type_unit_collect t2 on t2.code = substr(t1.dataCodeOld,11,4)
        where left(t1.dataCodeOld,8) = '00000000' and substr(t1.dataCodeOld,9,2) in ('01','02') and right(t1.dataCodeOld,2) = '33' and t1.enterpriseCode = #{enterpriseCode}
        having name is not null order by category
    </select>
    <select id="getProductOutPutCode" resultType="java.lang.String">
        select dataCodeOld from (
            select enterpriseCode, dataCode, dataCodeOld, collectItemName, right(dataCodeOld,2) useType
            from configure_data_collect_new where left(dataCodeOld,8) = '00000000' and substr(dataCodeOld,9,2) = '04'
            and right(dataCodeOld,2) in ('30','33','60') and substr(dataCodeOld,11,4) = #{code} and enterpriseCode = #{enterpriseCode} order by useType asc
        ) t limit 0,1
    </select>
    <select id="getProductConsumptionCode" resultType="java.lang.String">
        select dataCodeOld from (
            select enterpriseCode, dataCode, dataCodeOld, collectItemName, right(dataCodeOld,2) useType, case when right(dataCodeOld,2) in ('11','31') then 1 else 2 end orderNum
            from configure_data_collect_new where left(dataCodeOld,8) = '00000000' and substr(dataCodeOld,9,2) = '04'
            and right(dataCodeOld,2) in ('11','31','20','21','23') and substr(dataCodeOld,11,4) = #{code} and enterpriseCode = #{enterpriseCode}
        ) t order by orderNum,useType asc limit 0,1
    </select>
    <select id="selectMaterielProductValue" resultType="com.nantian.cloud.dbcapp.vo.StructureVo">
        <if test="list!=null and list.size>0">
            <foreach collection="list" item="date" separator="union" index="i">
                select sum(consume_sum) AS `value`, #{date} AS `month`, #{unit} unit,
                (select sum(consume_sum) from t_energy_structure where enterprise_id = #{enterpriseCode} and dataCodeOld
                = #{consumption} and record_circle = #{date}) consumption
                from t_energy_structure where enterprise_id = #{enterpriseCode} and dataCodeOld = #{outPut} and
                record_circle = #{date}
            </foreach>
        </if>
    </select>
    <select id="selectEnergyProductValue" resultType="com.nantian.cloud.dbcapp.vo.StructureVo">
        <if test="list!=null and list.size>0">
            <foreach collection="list" item="date" separator="union" index="i">
                select sum(consume_sum) AS `value`, #{date} AS `month`, #{unit} unit from t_energy_structure
                where enterprise_id = #{enterpriseCode} and left(dataCodeOld,8) = '00000000' and substr(dataCodeOld,9,2)
                in('01','02') and right(dataCodeOld,2) = '33' and substr(dataCodeOld,11,4) = #{code} and record_circle =
                #{date}
            </foreach>
        </if>
    </select>
    <select id="getMaterialElectric" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
        select left(t.statDate,7) month,  round(sum(dataValue)/10000,4) value, t.dataCode from enterprise_energy_data t, (
            select dataCodeOld, GROUP_CONCAT(dataCode) dataCode, case when SUBSTRING(dataCodeOld,15,2) = '11' then 1 when SUBSTRING(dataCodeOld,15,2) = '20' then 2 when SUBSTRING(dataCodeOld,15,2) = '21' then 3 when SUBSTRING(dataCodeOld,15,2) = '10' then 4 else 5 end num
            from configure_data_collect_new
            where enterpriseCode = #{enterpriseCode} and SUBSTRING(dataCodeOld,1,8)  = '00000000'
            and SUBSTRING(dataCodeOld,11,4) = '3300' and SUBSTRING(dataCodeOld,15,2) in ("11","20","21","10")
            group by dataCodeOld order by num asc limit 0,1
        ) c where t.enterpriseCode = #{enterpriseCode} and left(t.statDate,7) between left(#{startTime},7) and left(#{endTime},7) and c.dataCode like concat('%',t.dataCode,'%')
		group by left(t.statDate,7) order by left(t.statDate,7)
    </select>
    <select id="getAccessEnergysTructure" resultType="com.nantian.cloud.dbcapp.vo.AccessTimeRangeVo">
        select min(record_date) start_circle, max(record_date) last_circle, GROUP_CONCAT(distinct record_date) concat_circle from access_energys_tructure where enterprise_code = #{enterpriseCode}
    </select>
    <select id="getOld" resultType="com.nantian.cloud.dbcapp.vo.AccessTimeRangeVo">
        select min(record_circle) start_circle, max(record_circle) last_circle, GROUP_CONCAT(distinct record_circle) concat_circle from t_unitenergy2019 where enterprise_id = #{enterpriseCode}
    </select>
    <select id="getEnergyDateByCollect" resultType="com.nantian.cloud.dbcapp.vo.AccessTimeRangeVo">
        select min(left(statDate,7)) start_circle, max(left(statDate,7)) last_circle, GROUP_CONCAT(distinct left(statDate,7)) concat_circle  from enterprise_energy_data where enterpriseCode = #{enterpriseCode} and remark not like '%延期企业%'
    </select>
    <select id="getEnergyDateByMobile" resultType="com.nantian.cloud.dbcapp.vo.AccessTimeRangeVo">
        select min(left(energy_stat_datetime,7)) start_circle, max(left(energy_stat_datetime,7)) last_circle, GROUP_CONCAT(distinct left(energy_stat_datetime,7)) concat_circle
	    from old_enterprise t1 left join old_collect_config t2 on t1.id = t2.enterprise_id left join old_energy_data t3 on t2.id = t3.collect_config_id and energy_stat_datetime not like '0001-01%'
	    where t1.enterprise_code = #{enterpriseCode}
    </select>
    <select id="getAllEpc" resultType="com.nantian.cloud.dbcapp.vo.SuperEpcVo">
        select epc_name,epc_code,super_tree_code,epc_name as title, is_empty, is_son from t_super_enterprise_info where
        epc_code IN
        <foreach collection="enterpriseCode" item="code" index="index" open="(" close=")" separator=",">
            #{code}
        </foreach>
    </select>
    <!--   where epc_code=#{enterpriseCode} or super_tree_code=#{enterpriseCode}
    -->
    <select id="getSuperEnterpriseList" resultType="java.lang.String">
        SELECT DISTINCT super_tree_code FROM t_super_enterprise_info WHERE super_tree_code is not null;
    </select>
    <select id="getOldDataByEpc" resultType="com.nantian.cloud.dbcapp.vo.OldEnergyDataVo">
SELECT e.enterprise_name,c.collect_config_name,d.energy_data_value,d.energy_stat_datetime,d.energy_data_img_url
        FROM old_enterprise e
        LEFT JOIN old_collect_config c ON e.id=c.enterprise_id
        LEFT JOIN (SELECT * FROM old_energy_data WHERE energy_stat_datetime like concat(left(#{time},7),'%')) d ON c.id=d.collect_config_id
        WHERE e.enterprise_code=#{enterpriseCode}
    </select>
    <select id="getFillingMethodByOld" resultType="com.nantian.cloud.dbcapp.vo.AccessTimeRangeVo">
        select filling_method fillingMethod, data_description dataDescription from t_enterprise_old where enterprise_code = #{enterpriseCode}
    </select>
    <select id="getFillingMethod" resultType="com.nantian.cloud.dbcapp.vo.AccessTimeRangeVo">
        select case when e2.enterpriseCode is not null then '通过端系统填报数据'
		            when e3.enterprise_code is not null then '通过延期企业填报功能填报数据'
                    else '无' end fillingMethod, '' dataDescription
        from ( select #{enterpriseCode} enterpriseCode ) e1
        left join enterprise_info e2 on e1.enterpriseCode = e2.enterpriseCode and applyDate is not null
        left join access_enterprise_info e3 on e1.enterpriseCode = e3.enterprise_code and e3.state = '2'
        -- left join (
        -- 	select enterpriseCode, min(left(statDate,7)) start_circle, max(left(statDate,7)) last_circle, GROUP_CONCAT(distinct left(statDate,7)) concat_circle  from enterprise_energy_data  group by enterpriseCode
        -- ) d2 on e1.enterpriseCode = d2.enterpriseCode
        -- left join (
        -- 	select enterprise_code, min(record_date) start_circle, max(record_date) last_circle, GROUP_CONCAT(distinct record_date) concat_circle from access_energys_tructure group by enterprise_code
        -- ) d3 on e1.enterpriseCode = d3.enterprise_code
        -- left join (
        --     select t1.enterprise_code, min(left(energy_stat_datetime,7)) start_circle, max(left(energy_stat_datetime,7)) last_circle, GROUP_CONCAT(distinct left(energy_stat_datetime,7)) concat_circle
        --     from old_enterprise t1 left join old_collect_config t2 on t1.id = t2.enterprise_id left join old_energy_data t3 on t2.id = t3.collect_config_id and energy_stat_datetime not like '0001-01%' group by t1.enterprise_code
        -- ) e4 on e1.enterpriseCode = e4.enterprise_code
    </select>
    <select id="getEpcById" resultType="String">
        SELECT
	t3.epc_code
FROM (
	SELECT
		t1.epc_code, t1.super_tree_code,
		t2.*,
		IF(FIND_IN_SET(t1.super_tree_code, @pids) > 0, @pids := CONCAT(@pids, ',', t1.epc_code), 0) AS isChild
	FROM (
		SELECT epc_code, super_tree_code FROM t_super_enterprise_info ORDER BY super_tree_code, epc_code
	) AS t1,
	( SELECT @pids := #{enterpriseCode} ) AS t2

) t3
WHERE t3.isChild != 0
    </select>
    <select id="getTceStatusChange" resultType="java.util.Map">
        select #{enterpriseCode} enterpriseCode, concat(IFNULL(max(guideline_val),0),"") maxDataValue, concat(IFNULL(min(guideline_val),0),"") minDataValue, concat(IFNULL(round(max(guideline_val)/min(guideline_val),2),0),"") ratio
        from t_unitenergy2020 where energyTypeCode = '0000' and code = '00000000' and guideline_val != 0 and guideline_val is not null
        and enterprise_id = #{enterpriseCode} and left(record_circle,7) > left(#{haltStartTime},7)
    </select>
    <select id="getEleStatusChange" resultType="java.util.Map">
        select '电网' type, #{enterpriseCode} enterpriseCode, concat(IFNULL(max(data),0),"") maxDataValue, concat(IFNULL(min(data),0),"") minDataValue, concat(IFNULL(round(max(data)/min(data),2),0),"") ratio from (
	        select companyId, left(time,7) time, sum(data) data from company_data_detail group by companyId, left(time,7)
        ) t where data > 1 and companyId = #{enterpriseCode} and left(time,7) > left(#{haltStartTime},7)
        union all
        select '在线' type, #{enterpriseCode} enterpriseCode, concat(IFNULL(max(dataValue),0),"") maxDataValue, concat(IFNULL(min(dataValue),0),"") minDataValue, concat(IFNULL(round(max(dataValue)/min(dataValue),2),0),"") ratio from (
            select d.enterpriseCode, GROUP_CONCAT(d.dataCode) dataCode, sum(d.dataValue/10000) dataValue, left(d.statDate,7) statDate from (
                select enterpriseCode, case when GROUP_CONCAT(dataCode) like '%0000000002330011%' then '0000000002330011' else '0000000002330010' end dataCode
                from configure_data_collect where dataCode like '00000000023300%' and enterpriseCode = #{enterpriseCode} group by enterpriseCode
	        ) c left join enterprise_energy_data d on d.enterpriseCode = c.enterpriseCode and d.dataCode = c.dataCode and left(d.statDate,7) > left(#{haltStartTime},7)
	        group by d.enterpriseCode, left(d.statDate,7)
	    ) t order by ratio desc
    </select>
    <select id="getProductStatusChange" resultType="java.util.Map">
        select #{enterpriseCode} enterpriseCode, dataCode, concat(IFNULL(max(dataValue),0),"") maxDataValue, concat(IFNULL(min(dataValue),0),"") minDataValue, concat(IFNULL(round(max(dataValue)/min(dataValue),2),0),"") ratio
        from enterprise_energy_data where SUBSTR(dataCode,9,2) = '04' and dataValue > 0 and enterpriseCode = #{enterpriseCode} and left(statDate,10) > left(#{haltStartTime},10)
        group by dataCode order by ratio desc
    </select>
    <select id="getTceStatusChange2" resultType="java.util.Map">
        select distinct t1.enterprise_id epc, concat(IFNULL(t1.guideline_val,0),"") startDataValue, concat(IFNULL(t2.guideline_val,0),"")endDataValue, concat(IFNULL(round(t1.guideline_val/t2.guideline_val,2),0),"") ratio
        from t_unitenergy2020 t1 left join t_unitenergy2020 t2 on t2.enterprise_id = t1.enterprise_id
        and t2.energyTypeCode = '0000' and t2.code = '00000000' and t2.guideline_val != 0 and t2.guideline_val is not null and left(t2.record_circle,7) = #{endDate}
        where t1.energyTypeCode = '0000' and t1.code = '00000000' and t1.guideline_val != 0 and t1.guideline_val is not null and left(t1.record_circle,7) = #{startDate} and t1.enterprise_id = #{enterpriseCode}
    </select>
    <select id="getEleStatusChange2" resultType="java.util.Map">
        select '电网' type, t1.companyId, concat(IFNULL(sum(t1.data),0),"") startDataValue, concat(IFNULL(sum(t2.data),0),"") endDataValue, concat(IFNULL(round(sum(t1.data)/sum(t2.data),2),0),"") ratio
        from company_data_detail t1
        left join company_data_detail t2 on t1.companyId = t2.companyId and left(t2.time,7) = #{endDate}
        where left(t1.time,7) = #{startDate} and t1.companyId = #{enterpriseCode}
        union all
        select '在线' type, enterpriseCode, concat(IFNULL(dataValue1,0),"") startDataValue, concat(IFNULL(dataValue2,0),"") endDataValue, concat(IFNULL(round(dataValue1/dataValue2,2),0),"") ratio
        from (
             select c.enterpriseCode, GROUP_CONCAT(c.dataCode) dataCode, sum(d1.dataValue/10000) dataValue1, sum(d2.dataValue/10000) dataValue2 from (
                 select enterpriseCode, case when GROUP_CONCAT(dataCode) like '%0000000002330011%' then '0000000002330011' else '0000000002330010' end dataCode
                 from configure_data_collect where dataCode like '00000000023300%' and enterpriseCode = #{enterpriseCode} group by enterpriseCode
             ) c
             left join enterprise_energy_data d1 on d1.enterpriseCode = c.enterpriseCode and d1.dataCode = c.dataCode and left(d1.statDate,7) = #{startDate}
             left join enterprise_energy_data d2 on d2.enterpriseCode = c.enterpriseCode and d2.dataCode = c.dataCode and left(d2.statDate,7) = #{endDate}
        ) t order by ratio desc;
    </select>
    <select id="getProductStatusChange2" resultType="java.util.Map">
        select #{enterpriseCode} enterpriseCode, d1.dataCode, concat(IFNULL(d1.dataValue,0),"") startDataValue, concat(IFNULL(d2.dataValue,0),"") endDataValue, concat(IFNULL(round(d1.dataValue/d2.dataValue,2),0),"") ratio
        from enterprise_energy_data d1 left join enterprise_energy_data d2 on d1.enterpriseCode = d2.enterpriseCode and d1.dataCode = d2.dataCode
        and SUBSTR(d2.dataCode,9,2) = '04' and d2.dataValue > 0 and left(d2.statDate,10) = #{endDate}
        where SUBSTR(d1.dataCode,9,2) = '04' and d1.dataValue > 0 and left(d1.statDate,10) = #{startDate} and d1.enterpriseCode = #{enterpriseCode}
        group by d1.dataCode order by ratio desc;
    </select>


    <select id="getEnergyTypeByProcedure" resultType="java.util.Map">
        select distinct type id,type label from (
            select * from (select  distinct c.id,
                                            c.collectItemName as configureName,
                                            c.dataCode as configureCode,
                                            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,
                                            (select meaning from dict_energy_use where code = substr(c.dataCode,15,2)) energyUseType
                           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)
                                    left join t_energy_structure t on t.dataCode = c.dataCode
                               and enterprise_id = #{enterpriseCode}
                               and record_circle = #{startTime}
                           where c.enterpriseCode = #{enterpriseCode})
                            tmp where 1 = 1
                            <if test="energyUseType != null and energyUseType != ''">
                                and energyUseType like concat('%',#{energyUseType},'%')
                            </if>
                            <if test="configureName != null and configureName != ''">
                                and configureName like concat('%',#{configureName},'%')
                            </if>
                            <if test="configureCode != null and configureCode != ''">
                                and configureCode like concat('%',#{configureCode},'%')
                            </if>
        ) t
    </select>

    <select id="getEnergyTypeByProcedure1" resultType="java.util.Map">
        select DISTINCT f.meaning as id,
                        f.meaning as label
        from  history_monitorpoint a LEFT JOIN history_energy_accountunit b on a.unitId =b.id
                                     LEFT JOIN t_unitenergy2019 c on a.code =c.code and a.entid =c.entid
                                     LEFT JOIN history_entenergy d on a.EnergyID =d.id
                                     left join dict_energy_type_unit_collect f on f.meaning = d.EnterEnergyName
        WHERE  a.entId= #{entID}
          AND c.record_circle = #{time}
    </select>

    <update id="updateDianliFlag" parameterType="String">
        update enterprise_info set dianliFlag = #{dianliFlag} where enterpriseCode = #{enterpriseCode}
    </update>


    <select id="selectDetailByDataCodeNameAndSettlementDataConfig" resultType="com.nantian.cloud.dbcapp.vo.CoalVo">
        select (dataValue * rate) as 'value', left(statDate,10) as `month` from enterprise_energy_data
        where enterpriseCode = #{enterpriseCode} and dataCode = #{dataCode}
        and left(statDate,11) between #{startTime} and #{entTime}
        GROUP BY substring(statDate,1,10)
        order by substring(statDate,1,10)
    </select>

</mapper>
