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

	<delete id="deleteRealTime">
		delete from enterprise_energy_data_real_time
		<if test="epc != null and epc != ''">
			where enterpriseCode = #{epc}
		</if>
	</delete>
	<insert id="insertAllRealTime">
		insert into enterprise_energy_data_real_time(id,enterpriseCode,dataCode,dataValue,convertRation,valid,scope,inputType,statType,statDate,uploadDate,addDate,digest,storeDate,analysedTimes,uploadedCountry,remark,deleteFlag)
		values
		<foreach collection="list" item="item"  separator=",">
			(#{item.id},#{item.enterpriseCode},#{item.dataCode},#{item.dataValue},#{item.convertRation},#{item.valid},#{item.scope},#{item.inputType},#{item.statType},#{item.statDate},#{item.uploadDate},#{item.addDate},#{item.digest}
			,#{item.storeDate},#{item.analysedTimes},#{item.uploadedCountry},#{item.remark},#{item.deleteFlag})
		</foreach>
	</insert>
	<!--<select id="noEnergyDataExcel" resultType="com.nantian.cloud.dbcapp.pojo.EnterpriseEnergyDataInfoPOJO">
		select tmp.id, tmp.tname, tmp.cmpName, tmp.enterpriseCode, tmp.applyDate, tmp.productTrade, tmp.collect_r collectR, tmp.collect_r*${days} collectRLL, tmp.data_r_sj dataRSJ,
		tmp.collect_y collectY, tmp.collect_y*1 collectYLL, tmp.data_y_sj dataYSJ, case when tmp.collect_y > tmp.data_y_sj then 1 else 0 end state, tmp.remark
		from(
			select r.id, r.tname, e.cmpName, e.enterpriseCode, left(e.applyDate,10) applyDate, e.productTrade, halt.epc ,halt.tmpTime ,halt.remark,
			count(distinct cr.enterpriseCode, cr.dataCode) collect_r, count(distinct dr.enterpriseCode, dr.dataCode, dr.statType, dr.statDate) data_r_sj,
			count(distinct cy.enterpriseCode, cy.dataCode) collect_y, count(distinct dy.enterpriseCode, dy.dataCode, dy.statType, dy.statDate) data_y_sj
			from enterprise_info e
			left join dict_region r on concat(left(e.regionCode,4),'00') = r.id
			left join (
				select distinct enterpriseCode, dataCode, statType, collectItemName
				from configure_data_collect_new where statType = '1'
				<if test="type == '1'.toString()">
					and isForce != '0'
				</if>
				<if test="list != null and list.size > 0">
					and concat(enterpriseCode,'#',dataCode) not in
					<foreach collection="list" index="i" item="enable" open="(" separator="," close=")">
						concat(#{enable.enterpriseCode},'#',#{enable.code})
					</foreach>
				</if>
			) cr on cr.enterpriseCode = e.enterpriseCode
			left join (
				select distinct enterpriseCode, dataCode, statType, left(statDate,10) statDate, addDate
				from enterprise_energy_data where left(statDate,7) = #{statDate} and #{sendDate} >= addDate
				union
				select distinct enterpriseCode, dataCode, statType, left(statDate,10) statDate, addDate
				from enterprise_energy_data_real_time where left(statDate,7) = #{statDate} and #{sendDate} >= addDate
			) dr on dr.enterpriseCode = cr.enterpriseCode and dr.enterpriseCode = e.enterpriseCode and dr.dataCode = cr.dataCode
			left join (
				select distinct enterpriseCode, dataCode, statType, collectItemName
				from configure_data_collect_new where statType = '2'
				<if test="type == '1'.toString()">
					and isForce != '0'
				</if>
				<if test="list != null and list.size > 0">
					and concat(enterpriseCode,'#',dataCode) not in
					<foreach collection="list" index="i" item="enable" open="(" separator="," close=")">
						concat(#{enable.enterpriseCode},'#',#{enable.code})
					</foreach>
				</if>
			) cy on cy.enterpriseCode = e.enterpriseCode
			left join (
				select distinct enterpriseCode, dataCode, statType, left(statDate,7) statDate, addDate
				from enterprise_energy_data where left(statDate,7) = #{statDate} and #{sendDate} >= addDate
				union
				select distinct enterpriseCode, dataCode, statType, left(statDate,7) statDate, addDate
				from enterprise_energy_data_real_time where left(statDate,7) = #{statDate} and #{sendDate} >= addDate
			) dy on dy.enterpriseCode = cy.enterpriseCode and dy.enterpriseCode = e.enterpriseCode and dy.dataCode = cy.dataCode
			left join
			<foreach collection="haltCompany" index="i" item="halt" open="(" separator="union" close=")">
				select  #{halt.epc} as epc, #{halt.tmpTime} as tmpTime, #{halt.remark} as remark
			</foreach>
			as halt on halt.epc = e.enterpriseCode
			<trim prefix="where" prefixOverrides="and">
				and e.cancelState = '0'
				<if test="epcStr != null and epcStr != ''">
					and #{epcStr} like concat("%",e.enterpriseCode,"%")
				</if>
				<if test="type == '1'.toString() or type == '2'.toString()">
					<if test="haltCompany != null and haltCompany.size > 0">
						and e.enterpriseCode not in
						<foreach collection="haltCompany" index="i" item="halt" open="(" separator="," close=")">
							#{halt.epc}
						</foreach>
					</if>
				</if>
			</trim>
			group by e.enterpriseCode
		) tmp
		<if test="type == '1'.toString()">
			where tmp.collect_y > tmp.data_y_sj
		</if>
		order by tmp.id, tmp.enterpriseCode asc
	</select>-->
	<select id="energyDataByEpc" resultType="com.nantian.cloud.dbcapp.vo.EnterpriseEnergyDataVo">
		select distinct e.dataCode collectCode, e.collectItemName collectName, e.statType collectStatType, c.*
		from configure_data_collect_new e left join (
			select enterpriseCode, dataCode, statDate, dataValue from enterprise_energy_data_real_time
			where statDate like concat(#{statDate},"%") and statType = '2' and enterpriseCode = #{epc}
			-- and (dataCode like '0000000001%'  or dataCode like '0000000002%' or dataCode like '0000000003%' or dataCode like '0000000004%' )
			<if test="sendDate != null and sendDate != ''">
				and #{sendDate} >= addDate
			</if>
			union
			select enterpriseCode, dataCode, statDate, dataValue from enterprise_energy_data
			where statDate like concat(#{statDate},"%") and statType = '2' and enterpriseCode = #{epc}
			-- and (dataCode like '0000000001%'  or dataCode like '0000000002%' or dataCode like '0000000003%' or dataCode like '0000000004%' )
			<if test="sendDate != null and sendDate != ''">
				and #{sendDate} >= addDate
			</if>
		) c on c.dataCode = e.dataCode and c.enterpriseCode = e.enterpriseCode
		where e.statType = '2' and e.enterpriseCode = #{epc}
		-- and (e.dataCode like '0000000001%'  or e.dataCode like '0000000002%' or e.dataCode like '0000000003%' or e.dataCode like '0000000004%')
		<if test="type == '1'.toString()">
			-- and collectItemName not like '%综合能耗%' and collectItemName not like '%单耗%' and collectItemName not like '%指标%'
			-- and collectItemName not like '%期初%' and collectItemName not like '%期末%'
			and e.isForce != '0'
		</if>
		order by e.collectItemName desc
	</select>

    <select id="energyPage" resultType="com.nantian.cloud.dbcapp.vo.EnterpriseEnergyDataVo">
		select e.statType collectStatType, e.id, e.enterpriseCode, e.dataCode, e.dataValue, e.convertRation, e.valid, e.scope, e.inputType, e.statType, e.statDate, e.uploadDate,
		e.addDate, e.digest, e.storeDate, e.analysedTimes, e.uploadedCountry, e.remark, e.deleteFlag, e.statType statTypeName, e.scope scopeName, e.inputType inputTypeName,
		c.collectItemName collectName
		<choose>
			<when test="type == '1'.toString()">
				,isAdditional
				from enterprise_energy_data e
				INNER JOIN configure_data_collect c
				on c.enterpriseCode = e.enterpriseCode and c.dataCode = e.dataCode
			</when>
			<otherwise>
				from enterprise_energy_data_real_time e
				INNER JOIN configure_data_collect c
				on c.enterpriseCode = e.enterpriseCode and c.dataCode = e.dataCode
			</otherwise>
		</choose>
		where e.enterpriseCode = #{query.enterpriseCode}
		<if test="query.inputType != null and query.inputType != ''">
			and e.inputType = #{query.inputType}
		</if>
		<if test="query.statType != null and query.statType != ''">
			and e.statType = #{query.statType}
		</if>
		<if test="query.scope != null and query.scope != ''">
			and e.scope = #{query.scope}
		</if>
		<if test="query.dataCode != null and query.dataCode != ''">
			and e.dataCode like concat("%",#{query.dataCode},"%")
		</if>
		<if test="start != null and start != ''">
			and e.statDate >= #{start}
		</if>
		<if test="end != null and end != ''">
			and #{end} >= e.statDate
		</if>
		<if test="query.collectName != null and query.collectName != ''">
			and (
				select GROUP_CONCAT(dataCode) from configure_data_collect where enterpriseCode = #{query.enterpriseCode} and collectItemName like concat("%",#{query.collectName},"%")
			) like concat('%',e.dataCode,'%')
		</if>
		order by e.statDate desc, e.dataCode asc
	</select>
	<select id="noDataCollectByException" resultType="com.nantian.cloud.dbcapp.vo.ConfigureCollectVo">
		SELECT d.enterpriseCode, d.dataCode, d.collectItemName, d.processCode, d.processUnitCode, d.equipmentCode, d.equipmentUnitCode, d.isForce, t.trans lackNum,
		case d.statType when '0' then '实时上传'  when '1' then '按天上传'  when '2' then '按月上传'  when '3' then '按年上传' else '' end statType,
		case when d.statType = '1' then 1*${num} when d.statType = '2' then 1 else 0 end theoryNum,
		case when d.statType = '1' then 1*${num}-t.trans when d.statType = '2' then 1-t.trans else 0 end actualNum
    	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
			FROM energy_exception_data_info e WHERE e.enterpriseCode = #{epc} AND e.statDate LIKE CONCAT('%',#{statDate}, '%') AND handlerType != 0 group by dataCode
		) t ON d.dataCode = t.dataCode WHERE d.enterpriseCode = #{epc} and trans >= 1 ORDER BY dataCode
	</select>
	<select id="noDataCollectByEpc" resultType="com.nantian.cloud.dbcapp.vo.ConfigureCollectVo">
		select enterpriseCode, collectItemName, dataCode, processCode, processUnitCode, statType, yc theoryNum, (dr+d) actualNum, (yc-dr-d) lackNum
		from (
			select distinct e.enterpriseCode, e.collectItemName, e.dataCode, e.processCode, e.processUnitCode,
			case e.statType when '0' then '实时上传'  when '1' then '按天上传'  when '2' then '按月上传'  when '3' then '按年上传' else '' end statType,
			case when e.statType = '1' then 1*${num} when e.statType = '2' then 1 else 0 end yc,
			count(distinct dr.statDate) dr, count(distinct d.statDate) d
			from configure_data_collect_new e
			left join (
				select enterpriseCode, dataCode, statType, case when statType=1 then left(statDate,10) when statType=2 then left(statDate,7) when statType=3 then left(statDate,4) else statDate end statDate
				from enterprise_energy_data_real_time where statDate like concat(#{statDate},'%') and enterpriseCode = #{epc}
			) dr on dr.datacode = e.dataCode
			left join (
				select enterpriseCode, dataCode, statType, case when statType=1 then left(statDate,10) when statType=2 then left(statDate,7) when statType=3 then left(statDate,4) else statDate end statDate
				from enterprise_energy_data where statDate like concat(#{statDate},'%') and enterpriseCode = #{epc}
			) d on d.datacode = e.dataCode
			where e.enterpriseCode = #{epc} and e.statType in ('1','2') and isForce != '0'
			-- and (e.dataCode like '0000000001%'  or e.dataCode like '0000000002%' or e.dataCode like '0000000003%' or e.dataCode like '0000000004%')
			-- and collectItemName not like '%综合能耗%' and collectItemName not like '%单耗%' and collectItemName not like '%指标%'
			-- and collectItemName not like '%期初%' and collectItemName not like '%期末%'
			<if test="list != null and list.size > 0 ">
				and e.dataCode not in
				<foreach collection="list" item="enable" open="(" separator="," close=")">
					#{enable.code}
				</foreach>
			</if>
			group by e.dataCode
		) tmp where yc > dr+d order by statType desc
	</select>
	<select id="getCancelCompanyCode" resultType="java.lang.String">
		select enterpriseCode from enterprise_info where cancelState = '1'
	</select>
	<select id="getCompanyCancelState" resultType="java.lang.String">
		select cancelState from enterprise_info where enterpriseCode = #{epc}
	</select>
	<delete id="deleteEnergyTce">
		delete from enterprise_energy_tce where year = #{year}
	</delete>
	<delete id="deleteDataBack">
        delete from enterprise_energy_data_back where id = #{dataId}
    </delete>
	<delete id="deleteDataInfo">
        delete from enterprise_energy_data where id = #{applyRecord.dataId}
    </delete>
	<delete id="deleteDataBackByDistinct">
		delete b from enterprise_energy_data_back b, distinct_energy_data d
		where b.id = d.dId and d.state = 4
	</delete>
	<delete id="deleteDataInfoByDistinct">
		delete b from enterprise_energy_data b, distinct_energy_data d
		where b.id = d.dId and d.state = 4
	</delete>
	<delete id="deleteExceptionList">
		delete from energy_exception_data_info where id in
		<foreach collection="list" index="i" item="item" open="(" separator=","  close=")"> #{item.id} </foreach>
	</delete>
	<delete id="deleteException">
		delete from energy_exception_data_info
		where enterpriseCode = #{applyRecord.enterpriseCode} and dataCode = #{applyRecord.dataCode} and statDate like concat( left(#{applyRecord.statDate},10),'%')
	</delete>
	<insert id="insertEnergyTce">
		insert into enterprise_energy_tce (cmpName,epc,year,month1,month2,month3,month4,month5,month6,month7,month8,month9,month10,month11,month12)
		select e.cmpName, e.enterpriseCode epc, #{year} year
		<foreach collection="list" item="date" index="i">
			,IFNULL(round((
			select sum( guideline_val ) from  t_unitenergy2020
			where energyTypeCode = '0000' and `code`= '00000000'
			and enterprise_id = e.enterpriseCode
			and record_circle = #{date}
			),3), 0 + CAST(0 AS CHAR)) as month${i+1}
		</foreach>
		from enterprise_info e
		<if test=" epcStr != null and epcStr != ''">
			where #{epcStr} like concat('%',e.enterpriseCode,'%')
		</if>
	</insert>
	<insert id="insertDataBack">
        insert into enterprise_energy_data_back(oldId,enterpriseCode,dataCode,dataValue,convertRation,valid,scope,inputType,statType,statDate,uploadDate,addDate,digest,storeDate,
        analysedTimes,uploadedCountry,remark,deleteFlag,operaType,operaTime,userId)
        select id,enterpriseCode,dataCode,dataValue,convertRation,valid,scope,inputType,statType,statDate,uploadDate,addDate,digest,storeDate,
        analysedTimes,uploadedCountry,#{applyRecord.remark},deleteFlag, #{applyRecord.operaType}, (select now()), #{applyRecord.auditUser}
        from enterprise_energy_data where id = #{applyRecord.dataId}
    </insert>
	<insert id="insertDataBackByDistinct">
		insert into enterprise_energy_data_back(oldId,enterpriseCode,dataCode,dataValue,convertRation,valid,scope,inputType,statType,statDate,uploadDate,addDate,digest,storeDate,
        analysedTimes,uploadedCountry,remark,deleteFlag,operaType,operaTime,userId)
        select b.id,b.enterpriseCode,b.dataCode,b.dataValue,b.convertRation,b.valid,b.scope,b.inputType,b.statType,b.statDate,b.uploadDate,b.addDate,
        b.digest,b.storeDate,b.analysedTimes,b.uploadedCountry,#{remark},b.deleteFlag,#{type},(select now()),#{user}
        from enterprise_energy_data b where id in (select dId from distinct_energy_data where state = 4)
	</insert>
	<insert id="inestApplyRecord">
		<foreach collection="list" index="i" item="item" separator=";">
			insert into t_apply_record (enterprise_code, collect_name, data_code, apply_time, opera_type, data_value,
			stat_type, stat_date, convert_ration, edit_reason, edit_data_value, edit_convert_ration) values
			(#{item.enterpriseCode}, #{item.collectItemName}, #{item.dataCode}, (select now()), "3", #{item.dataValue},
			(case #{item.statType} when 1 then '按日上传' when 2 then '按月上传' when 3 then '按年上传' else '' end),
			#{item.statDate}, #{item.convertRation}, #{item.remark}, #{item.editDataValue}, #{item.editConvertRation})
		</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)
	</update>
	<update id="updateInfoByEpc">
        update enterprise_energy_data
		<trim prefix="set" suffixOverrides=",">
			addDate = (select now()),
			isIgnore = 1,
			<if test="data.dataValue != null and data.dataValue != ''">dataValue = #{data.dataValue},</if>
			<if test="data.convertRation != null and data.convertRation != ''">convertRation = #{data.convertRation},</if>
		</trim>
        where enterpriseCode =#{data.enterpriseCode} and dataCode = #{data.dataCode} and statDate like concat(#{data.statDate},'%')
    </update>
	<update id="updateDataInfo">
        update enterprise_energy_data
		<trim prefix="set" suffixOverrides=",">
			addDate = (select now()),
			isAdditional = '2',
			<if test="applyRecord.editDataValue != null and applyRecord.editDataValue != ''">dataValue = #{applyRecord.editDataValue},</if>
			<if test="applyRecord.editConvertRation != null and applyRecord.editConvertRation != ''">convertRation = #{applyRecord.editConvertRation},</if>
		</trim>
        where id = #{applyRecord.dataId}
    </update>
	<select id="getDataId" resultType="java.lang.String">
		select IFNULL(MAX(id),'') from enterprise_energy_data where enterpriseCode = #{applyRecord.enterpriseCode} and dataCode = #{applyRecord.dataCode} and statDate like concat(left(#{applyRecord.statDate},10),'%')
	</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>
    <update id="updateElectricDetailTable">
		update epc_ele_info set m${num} = #{date} where id = 1
	</update>
	<update id="updateElectricDetailData">
		update (
			select e.epc, case when ele.dataValue != '' and CONVERT(ele.dataValue,DECIMAL(10,4)) != 0  and ele.dataValue is not null then ele.dataValue else d.dataValue end dataValue
			from (select epc from epc_ele_info) e
			left join (
				select enterpriseCode, sum(data) as dataValue from company_data_detail where `time` like concat(#{date},"%") group by enterpriseCode
			) ele on e.epc = ele.enterpriseCode
			left join (
				select d.enterpriseCode, GROUP_CONCAT(d.dataCode), sum(d.dataValue/10000) dataValue
				from (
					select enterpriseCode, case when GROUP_CONCAT(dataCode) like '%0000000002330011%' then '0000000002330011' else '0000000002330010' end dataCode
					from configure_data_collect  where dataCode like '00000000023300%' group by enterpriseCode
				) c left join enterprise_energy_data d on d.enterpriseCode = c.enterpriseCode and d.dataCode = c.dataCode
				where d.statDate like concat(#{date},"%") group by d.enterpriseCode
			) d on e.epc = d.enterpriseCode group by e.epc
		) t1, epc_ele_info t2
		set t2.m${num} = round(t1.dataValue,4)
		where t2.epc = t1.epc
	</update>
	<update id="updateExceptionList">
		update energy_exception_data_info set handlerType = 0, handlerDate = (select now())
		where id in
		<foreach collection="list" index="i" item="item" open="(" separator=","  close=")">
			#{item.id}
		</foreach>
	</update>
	<update id="updateExceptionState">
		update energy_exception_data_info set handlerType = 0, handlerDate = (select now())
		<if test="applyRecord.remark != null and applyRecord.remark != ''">, remark = #{applyRecord.remark}</if>
		<if test="applyRecord.editDataValue != null and applyRecord.editDataValue != ''">, dataValue = #{applyRecord.editDataValue}</if>
		<if test="applyRecord.editConvertRation != null and applyRecord.editConvertRation != ''">, convertRation = #{applyRecord.editConvertRation}</if>
		where enterpriseCode = #{applyRecord.enterpriseCode} and dataCode = #{applyRecord.dataCode} and statDate like concat( left(#{applyRecord.statDate},10),'%')
	</update>
	<update id="updateExceptionHandlerType">
		update energy_exception_data_info set handlerType = #{type}
		where id in
		<foreach collection="list" index="i" item="item" open="(" separator=","  close=")">
			#{item.id}
		</foreach>
	</update>
	<update id="updateExceptionHandlerReson">
		update energy_exception_data_info set handlerType = #{handlerType}, handlerReason = #{applyRecord.auditText}
		where enterpriseCode = #{applyRecord.enterpriseCode} and dataCode = #{applyRecord.dataCode} and statDate like concat( left(#{applyRecord.statDate},10),'%')
	</update>
	<update id="updateApplyRecord">
		update t_apply_record
		set audit_user = #{applyRecord.auditUser}, audit_type = #{applyRecord.auditType}, audit_text = #{applyRecord.auditText}, audit_time = (select now())
		where id = #{applyRecord.id}
	</update>
	<select id="enterpriseTceExcel" resultType="com.nantian.cloud.dbcapp.pojo.EnterpriseTcePOJO">
		select r.tname region, dict.name industry, e.jnjcTrade, e.cmpName, e.enterpriseCode, e.applyDate, e.energyConsumeLevel energyConsumeLevelNum, dl.levle energyConsumeLevel, dl.lowerLevel, dl.hightLevel,
		<foreach collection="list" item="date" index="i">
			round(CONVERT(t.month${i+1},DECIMAL(10,3)),3) month${i+1},
			case when dl.lowerLevel is not null and CONVERT(dl.lowerLevel/12,DECIMAL(10,3)) > CONVERT(t.month${i+1},DECIMAL(10,3)) then '是'
			when dl.hightLevel is not null and CONVERT(t.month${i+1},DECIMAL(10,3)) > CONVERT(dl.hightLevel/12,DECIMAL(10,3)) then '是' else '' end month${i+1}OverstepLevel,
			round( IFNULL( (select sum(dataValue) from enterprise_energy_data where enterpriseCode = tc.enterpriseCode and dataCode = tc.dataCode and statDate like concat(#{date},'%')), 0), 3) month${i+1}Zs,
		</foreach>
		round(CONVERT(t.sumTce,DECIMAL(20,3)),3) sumTce,
		case when dl.lowerLevel is not null and CONVERT(dl.lowerLevel,DECIMAL(10,3)) > CONVERT(t.sumTce,DECIMAL(10,3)) then '是'
		when dl.hightLevel is not null and CONVERT(t.sumTce,DECIMAL(10,3)) > CONVERT(dl.hightLevel,DECIMAL(10,3)) then '是' else '' end sumOverstepLevel,
		round( IFNULL( (select sum(dataValue) from enterprise_energy_data where enterpriseCode = tc.enterpriseCode and dataCode = tc.dataCode and statDate like concat(#{year},'%')), 0), 3) sumTceZs
		from enterprise_info e
		left join dict_region r on concat(left(e.regionCode,4),'00') = r.id
		left join temp_dict_name dict on e.tid = dict.id
		left join enterprise_energy_tce t on e.enterpriseCode = t.epc and t.year = #{year}
		left join (
			 select '1' num, '5000吨以下'   levle, null    lowerLevel, 5000      hightLevel union
			 select '2' num, '5000~10000吨' levle, 5000    lowerLevel, 10000     hightLevel union
			 select '3' num, '1万-10万吨'   levle, 10000   lowerLevel, 100000    hightLevel union
			 select '4' num, '10万-50万吨'  levle, 100000  lowerLevel, 500000    hightLevel union
			 select '5' num, '50万-300万吨' levle, 500000  lowerLevel, 3000000   hightLevel union
			 select '6' num, '300万吨以上'  levle, 3000000 lowerLevel, null		 hightLevel
		) dl on e.energyConsumeLevel = dl.num
		left join(
			select enterpriseCode, case when length(dataCode) = 16 then dataCode when dataCode like '%0000000008000071%' then '0000000008000071'
			when dataCode like '%0000000008000072%' then '0000000008000072' when dataCode like '%0000000008000020%' then '0000000008000020' else left(dataCode,16) end dataCode
			from (
				select enterpriseCode, GROUP_CONCAT(distinct dataCode) dataCode from configure_data_collect
				where collectItemName like '%综合能耗%' and  substring(dataCode,1,8) = '00000000' and substring(dataCode,11,4) = '0000' group by enterpriseCode
			) t
		)tc on e.enterpriseCode = tc.enterpriseCode
		where 1=1
		<if test=" epcStr != null and epcStr != ''">
			and #{epcStr} like concat('%',e.enterpriseCode,'%')
		</if>
	</select>
	<select id="enterpriseQuotaDetailExcel" resultType="com.nantian.cloud.dbcapp.pojo.EnterpriseQuotaDetailPOJO">
		select distinct r.tname region, dict.name industry, e.jnjcTrade, e.cmpName, e.enterpriseCode, tu.quota_name quotaName,
		(select distinct limit_value from t_standard where type = '0' and quota_name = tu.quota_name and code = tu.Industry_code) quotaLimit,
		(select distinct unitName from history_product_quota where quota_name = tu.quota_name and Industry_code = tu.Industry_code) quotaUnit,
		<foreach collection="list" item="date" index="i" separator=",">
			(select distinct guideline_val from t_unitvalue where enterprise_id = tu.enterprise_id and quota_name = tu.quota_name and record_circle = #{date}) month${i+1}
		</foreach>
		from (
			select distinct u.enterprise_id, u.quota_name, t.Industry_code from `t_unitvalue` u left join history_enterprise_product t on u.enterprise_id = t.enterprise_code
		) tu
		left join enterprise_info e on e.enterpriseCode = tu.enterprise_id
		left join dict_region r on concat(left(e.regionCode,4),'00') = r.id
		left join temp_dict_name dict on e.tid = dict.id
		where e.cmpName is not null
		<if test=" epcStr != null and epcStr != ''">
			and #{epcStr} like concat('%',e.enterpriseCode,'%')
		</if>
		order by e.enterpriseCode, tu.quota_name asc
	</select>
    <select id="getEnterpriseElectricExcel" resultType="com.nantian.cloud.dbcapp.pojo.EnterpriseElectricPOJO">
		select distinct e.cmpName, e.enterpriseCode,
		<foreach collection="list" item="date" index="i" separator=",">
			round(IFNULL((select sum(dataValue)/10000 from t_electricity_data_value where enterpriseCode = e.enterpriseCode and statDate = #{date}),0),4) month${i+1},
			round(IFNULL((select sum(StandardValue) from  t_unitenergy2020 where energyTypeCode = '3300' and `code`='00000000' and enterprise_id = e.enterpriseCode and record_circle = #{date}),0),4) month${i+1}Zxjc,
			round(IFNULL((select sum(consume_sum)/10000 as dataValue from t_energy_structure where enterprise_id = e.enterpriseCode and dataCode = '0000000002330060' and record_circle = #{date}),0),4) month${i+1}ZxjcOut
		</foreach>
		from enterprise_info e
		<if test=" epcStr != null and epcStr != ''">
			where #{epcStr} like concat('%',e.enterpriseCode,'%')
		</if>
		order by e.enterpriseCode asc
	</select>
	<select id="getEnergyName" resultType="java.lang.String">
		select meaning from dict_energy_type_unit_collect where id = #{energyCode}
	</select>
	<select id="getEnterpriseEnergyExcel" resultType="com.nantian.cloud.dbcapp.pojo.EnterpriseEnergyPOJO">
		select distinct e.cmpName, e.enterpriseCode, cl.theoryNum collect, cl.dataCode collectCode,
		<foreach collection="list" item="date" index="i" separator=",">
		(
			select concat(count( distinct c.dataCode),";",GROUP_CONCAT(distinct c.dataCode))
			from configure_data_collect c
			left join enterprise_energy_data d on d.enterpriseCode = c.enterpriseCode and d.dataCode = c.dataCode and left(d.statDate,7) = #{date}
			where c.enterpriseCode = cl.enterpriseCode and cl.dataCode like concat('%',c.dataCode,'%') and d.dataValue is null
		) month${i+1}
		</foreach>
		from enterprise_info e
		left join(
			select distinct enterpriseCode, GROUP_CONCAT(distinct dataCode) dataCode, count(distinct dataCode) theoryNum
			from configure_data_collect
			where substring(dataCode,1,8) = '00000000' and substring(dataCode,9,2) in ('01','02') and substring(dataCode,11,4) in
			<foreach collection="energyList" item="energy" index="i" open="(" separator="," close=")">
				#{energy}
			</foreach>
			and collectItemName not like '%期初%' and collectItemName not like '%期末%' and collectItemName not like '%指标%'
			and collectItemName not like '%能耗%' and collectItemName not like '%单耗%'
			group by enterpriseCode
		)cl on cl.enterpriseCode = e.enterpriseCode
		<if test=" epcStr != null and epcStr != ''">
			where #{epcStr} like concat('%',e.enterpriseCode,'%')
		</if>
		order by e.enterpriseCode asc
	</select>
	<select id="getEnergyCollectInfo" resultType="com.nantian.cloud.dbcapp.vo.EnterpriseEnergyDataVo">
		select distinct enterpriseCode, dataCode, #{data.dataValue} dataValue, IFNULL(#{data.convertRation},0) convertRation, scope, inputType, statType, #{data.statDate} statDate, NOW() uploadDate, NOW() addDate
		from configure_data_collect
		where enterpriseCode =#{data.enterpriseCode} and dataCode = #{data.dataCode}
	</select>
	<select id="getDataBySataDate" resultType="java.lang.String">
		select id from enterprise_energy_data where enterpriseCode =#{data.enterpriseCode} and dataCode = #{data.dataCode} and statDate like concat(#{data.statDate},'%')
	</select>

	<sql id="Base_Data_Info">
		select distinct r.id, r.tname, info.cmpName, e.enterpriseCode, e.collectItemName, e.dataCode, e.processCode, e.processUnitCode, e.statType, info.productTrade,
		<choose>
			<when test="haltCompany != null and haltCompany.size > 0">
				hh.haltRemark,
			</when>
			<otherwise>
				'' haltRemark,
			</otherwise>
		</choose>
		case when e.statType = '1' then 1*${num} when e.statType = '2' then 1 else 0 end yc,
		count(distinct dr.statDate) dr, count(distinct d.statDate) d
		from enterprise_info info
		left join dict_region r on concat(left(info.regionCode,4),'00') = r.id
		<if test=" haltCompany != null and haltCompany.size > 0">
			left join
			<foreach collection="haltCompany" index="i" item="halt" open="(" separator="union" close=")">
				select  #{halt.epc} as epc, #{halt.tmpTime} as tmpTime, #{halt.remark} as haltRemark
			</foreach>
			as hh on hh.epc = info.enterpriseCode
		</if>
		left join (
		select distinct enterpriseCode, collectItemName, dataCode, processCode, processUnitCode, statType, isForce
		from configure_data_collect_new where 1 = 1
		<if test="list != null and list.size > 0">
			and concat(enterpriseCode,'#',dataCode) not in
			<foreach collection="list" index="i" item="enable" open="(" separator="," close=")">
				concat(#{enable.enterpriseCode},'#',#{enable.code})
			</foreach>
		</if>
		) e on e.enterpriseCode = info.enterpriseCode
		left join (
			select enterpriseCode, dataCode, statType, case when statType=1 then left(statDate,10) when statType=2 then left(statDate,7) when statType=3 then left(statDate,4) else statDate end statDate
			from enterprise_energy_data_real_time where statDate like concat(#{statDate},'%')
		) dr on dr.datacode = e.dataCode and dr.enterpriseCode = info.enterpriseCode
		left join (
			select enterpriseCode, dataCode, statType, case when statType=1 then left(statDate,10) when statType=2 then left(statDate,7) when statType=3 then left(statDate,4) else statDate end statDate
			from enterprise_energy_data where statDate like concat(#{statDate},'%')
		) d on d.datacode = e.dataCode and d.enterpriseCode = info.enterpriseCode
		where info.cancelState = '0' and e.statType in ('1','2')
		<if test="type == '1'.toString() or type == '3'.toString()">
			and e.isForce != '0'
		</if>
		group by e.enterpriseCode, e.dataCode
	</sql>

    <select id="noEnergyDataInfoExcel" resultType="com.nantian.cloud.dbcapp.pojo.EnterpriseEnergyDetailPOJO">
		select id, tname, cmpName, enterpriseCode, haltRemark, collectItemName, dataCode, productTrade,
		case statType when '0' then '实时上传'  when '1' then '按天上传'  when '2' then '按月上传'  when '3' then '按年上传' else '' end statType, yc theoryNum, (dr+d) actualNum, (yc-dr-d) lackNum,
		case when haltRemark != '' and haltRemark is not null then '/' when yc > dr+d then '缺数据' else '' end remark
		from (
			<include refid="Base_Data_Info"/>
		) tmp
		<if test="type == '3'.toString()">
			where yc > dr+d and (haltRemark is null or haltRemark = '')
		</if>
		order by id,enterpriseCode,statType,dataCode asc
	</select>

	<select id="noEnergyDataExcel" resultType="com.nantian.cloud.dbcapp.pojo.EnterpriseEnergyDetailPOJO">
		select id, tname, cmpName, enterpriseCode, haltRemark, productTrade, sum(yc) theoryNum, sum(dr)+sum(d) actualNum, sum(yc)-sum(dr)-sum(d) lackNum,
		case when haltRemark != '' and haltRemark is not null then '/' when sum(yc) > sum(dr)+sum(d) then '缺数据' else '' end remark
		from (
			<include refid="Base_Data_Info"/>
		) tmp
		<if test="type == '3'.toString()">
			where sum(yc) > sum(dr)+sum(d) and (haltRemark is null or haltRemark = '')
		</if>
		group by enterpriseCode
		order by id,enterpriseCode,statType,dataCode asc
	</select>

	<select id="getEnterpriseDetail" resultType="java.util.Map">
		select count(1) total, '接入企业家数' title from (
		select distinct enterpriseCode, SUBSTRING_INDEX(SUBSTRING_INDEX(reportNum,'"updateTime":"',-1),'"',1) updateTime, SUBSTRING_INDEX(SUBSTRING_INDEX(reportNum,'"finishTime":"',-1),'"',1) finishTime, SUBSTRING_INDEX(SUBSTRING_INDEX(reportNum,'"accomplish":"',-1),'"',1) accomplish
		from enterprise_info  where reportNum like '%已完成%'
		) t where 1 = 1
		<if test="startDate != null and startDate != '' ">
			and left(updateTime,10) &gt;= #{startDate}
		</if>
		<if test="endDate != null and endDate != '' ">
			and left(updateTime,10) &lt;= #{endDate}
		</if>
		union
		<choose>
			<when test="type=='1'.toString">
				select count(distinct enterprise_code) total, '公式配置家数' title from formul
				where formul != "-" and left(create_time,10) &lt;= #{endDate}
			</when>
			<otherwise>
				select count(distinct enterprise_code) num, '公式配置家数' title from formul
				where formul != '-' and left(create_time,10) &gt;= #{startDate} and left(create_time,10) &lt;= #{endDate}
				and enterprise_code not in (
					select distinct enterprise_code from formul where left(create_time,10) &lt; #{startDate}
				)
			</otherwise>
		</choose>
	</select>

	<select id="getEnterpriseDataDetail" resultType="java.util.Map">
		select '在线采集与公式配置数据' title,count(1) formulTotal, count(case when formul_name = '综合能耗' then 1 end) tceTotal, count(case when formul_name != '综合能耗' and length(product_data_code)>=16 then 1 end) unitEnergyTotal,
		(
			select IFNULL(sum(num),0) from enterprise_data_all where type in ('1','2')
			<if test="startDate != null and startDate != '' ">
				and left(statDate,10) &gt;= #{startDate}
			</if>
			<if test="endDate != null and endDate != '' ">
				and left(statDate,10) &lt;= #{endDate}
			</if>
		) collectTotal
		from formul  where formul != '-'
		<if test="startDate != null and startDate != '' ">
			and left(create_time,10) &gt;= #{startDate}
		</if>
		<if test="endDate != null and endDate != '' ">
			and left(create_time,10) &lt;= #{endDate}
		</if>
	</select>

	<select id="getEnterpriseByException" resultType="java.util.Map">
		select '数据不合格企业' title, count(1) total, IFNULL(GROUP_CONCAT(cmpName),"") cmpName from (
			select distinct enterpriseCode, (select cmpName from enterprise_info where enterpriseCode = e.enterpriseCode) cmpName
			from energy_exception_data_info e where handlerType != '0'
			and (enterpriseCode,dataCode) in (select enterpriseCode,dataCode from configure_data_collect_new where isForce = '2')
			<if test="startDate != null and startDate != '' ">
				and left(statDate,10) &gt;= #{startDate}
			</if>
			<if test="endDate != null and endDate != '' ">
				and left(statDate,10) &lt;= #{endDate}
			</if>
		) t
	</select>

	<select id="getExceptionDetailGroupByType" resultType="java.util.Map">
		select '各异常情况' title, sum(case when exceptionType = '1' then num end) transportException, sum(case when exceptionType = '2' then num end) waveException,
		sum(case when exceptionType = '3' then num end) electricException, sum(case when exceptionType = '4' then num end) tceException, sum(case when exceptionType = '5' then num end) unitEnergyException from (
			select sum(`num`) num, exceptionType
			from enterprise_data_all where type = '4' and epc in (select enterpriseCode from enterprise_info)
			<if test="startDate != null and startDate != '' ">
				and left(statDate,7) &gt;= left(#{startDate},7)
			</if>
			<if test="endDate != null and endDate != '' ">
				and left(statDate,7) &lt;= left(#{endDate},7)
			</if>
			group by exceptionType
			union all
			select count(1) num, case when exceptionType like '0207' then '3' when exceptionType in ('0208','0209') then '4' else '5' end exceptionType
			from energy_exception_result_info where 1 = 1 and enterpriseCode in (select enterpriseCode from enterprise_info)
			<if test="startDate != null and startDate != '' ">
				and left(statDate,7) &gt;= left(#{startDate},7)
			</if>
			<if test="endDate != null and endDate != '' ">
				and left(statDate,7) &lt;= left(#{endDate},7)
			</if>
			group by exceptionType
		) t
	</select>
    <select id="getRegisterEnterprise" resultType="java.util.Map">
		select enterpriseCode, cmpName, regionCode
		from enterprise_info where 1 = 1
		<foreach collection="list" item="item" separator="," open=" and enterpriseCode not in (" close=")">
			#{item}
		</foreach>
	</select>
</mapper>
