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

	<select id="selectEnableDataCode" resultType="com.nantian.cloud.dbcapp.bean.EnterpriseDataInfo">
		select distinct enterpriseCode, dataCode code, dataCodeOld oldCode, left(startTime,10) startTime,
			case when endTime is null or endTime = '' then (select date_format(NOW(),'%Y-%m-%d')) else left(endTime,10) end endTime
		from configure_data_collect_enable
		where delFlag = "1"
		<if test=" enterpriseCode != null and enterpriseCode != '' ">
			and enterpriseCode = #{enterpriseCode}
		</if>
		having left(#{startTime},10) between startTime and endTime
	</select>

	<select id="selectEnableDataCodeByMonth" resultType="com.nantian.cloud.dbcapp.bean.EnterpriseDataInfo">
		select distinct enterpriseCode, dataCode code, dataCodeOld oldCode, left(startTime,7) startTime,
			case when endTime is null or endTime = '' then (select date_format(NOW(),'%Y-%m')) else left(endTime,7) end endTime
		from configure_data_collect_enable
		where delFlag = "1"
		<if test=" enterpriseCode != null and enterpriseCode != '' ">
			and enterpriseCode = #{enterpriseCode}
		</if>
		having left(#{startTime},7) between startTime and endTime
	</select>

	<select id="selectDataCode" resultType="com.nantian.cloud.dbcapp.bean.EnterpriseDataInfo">
	  select enterpriseCode, collectItemName as name , dataCode as code ,dataCodeOld as oldCode, statType from configure_data_collect_new
	  where enterpriseCode = #{enterpriseCode}
	  -- and isEnable = 0
	  <if test=" list != null and list.size > 0 ">
		  and dataCode not in
		  <foreach collection="list" item="enable" open="(" separator="," close=")">
			  #{enable.code}
		  </foreach>
	  </if>
	  order by dataCode asc
	</select>
  <select id="selectDataValue" resultType="com.nantian.cloud.dbcapp.bean.Values">
	SELECT d.datacode, SUM(d.dataValue) dataValue, d.convertRation, d.statType
	FROM enterprise_energy_data_copy1 d
	LEFT JOIN configure_data_collect_new c ON c.enterpriseCode = d.enterpriseCode AND c.dataCode = d.dataCode
	AND d.dataValue BETWEEN CONVERT( IFNULL(c.minNum,0),DECIMAL(20,3)) AND CONVERT(IFNULL(c.maxNum,100000000),DECIMAL(20,3))
	WHERE d.enterpriseCode = #{param1} AND d.statDate BETWEEN #{param2} AND #{param3}
	GROUP BY d.datacode ORDER BY d.datacode ASC
  </select>

	<insert id="insertData" >
		insert into t_energy_structure_add(enterprise_id,record_circle,record_circle_type,consume_sum,modulus,dataCode,energy_code,unit,convertRation_unit,energy_type_code,dataCodeOld)
		values
		<foreach collection="list" item="item"  separator=",">
			(#{item.enterpriseCode},#{item.time},#{item.timeType},#{item.dataValue},#{item.convertRation},#{item.code},#{item.energyCode},#{item.unit},#{item.convertRationUnit},#{item.energyTypeCode},#{item.oldCode})
		</foreach>
	</insert>

	<select id="selectEnergyValue" resultType="com.nantian.cloud.dbcapp.bean.CalculateBo">
		select dataCodeOld as dataCode ,sum(consume_sum) as consumeSum ,modulus from t_energy_structure where enterprise_id = #{enterpriseCode}
		and energy_code = #{energyCode} and record_circle = #{startTime} and energy_type_code = #{energyTypeCode} and dataCodeOld like concat(#{base},'%')
		group by energy_code,dataCodeOld,record_circle
	</select>

	<select id="selectEnterpriseInfo" resultType="string">
		select enterpriseCode from enterprise_info
		<where>
			<if test="cmpName != null">
				cmpName like #{cmpName}
			</if>
	  </where>
	  order by enterpriseCode limit 65,500
	</select>

	<insert id="insertDistinctEnergyData">
		insert into ${tableName}(cmpName,enterpriseCode,collectItemName,dataCode,statType,dDataCode,dataValue,convertRation
		,dStatType,statDate,uploadDate,addDate,dId,state)
		values
		<foreach collection="list" item="data" separator=",">
			(#{data.cmpName},#{data.enterpriseCode},#{data.collectItemName},#{data.dataCode},#{data.statType},#{data.dDataCode},#{data.dataValue},#{data.convertRation}
			,#{data.dStatType},#{data.statDate},#{data.uploadDate},#{data.addDate},#{data.dId},#{data.state})
		</foreach>
	</insert>

	<select id="selectUnitEnergyInfo" resultType="float">
		select distinct sum(guideline_val) from t_unitenergy2020 where enterprise_id = #{enterpriseCode}
		and code = #{base} and energyTypeCode = #{dataCode} and record_circle = #{time} group by enterprise_id,code,energyTypeCode,record_circle
	</select>

	<select id="selectUnitProduct" resultType="float">
		select IFNULL(SUM(consume_sum),0) from t_energy_structure where enterprise_id = #{enterpriseCode} and #{productCode} like concat("%",dataCode,"%")
		and record_circle=#{time}
	</select>

	<select id="selectElectricPower" resultType="java.lang.Float">
		select IFNULL(SUM(consume_sum),0) from t_energy_structure where enterprise_id = #{enterpriseCode} and dataCode like #{productCode} and record_circle=#{time}
	</select>

	<select id="selectUnitEnergyName" resultType="com.nantian.cloud.dbcapp.bean.UnitInfo">
		select enterpriseCode ,indexName,energykindscode,productCode,base,correctionValues,baseState,calculate from t_energy_index_info
		where enterpriseCode = #{enterpriseCode}
	</select>

	<insert id="insertIndexData" >
		insert into t_unitvalue_add(enterprise_id,quota_name,record_circle,guideline_val,record_circle_type,child_val,parent_val) VALUES
		<foreach collection="list" item="data" separator=",">
			(#{data.enterpriseCode},#{data.indexName},#{data.statisticalTime},#{data.statisticalValues},"月",#{data.childVal},#{data.parentVal})
		</foreach>
	</insert>

	<select id="selectEnterpriseConfigureInfo" resultType="com.nantian.cloud.dbcapp.bean.EnterpriseConfigureInfo">
		select enterpriseCode , dataCodeOld as dataCode from configure_data_collect_new where isEnable = 0
		<if test="enterpriseCode !=null">
			and enterpriseCode = #{enterpriseCode}
		</if>
		order by enterpriseCode,dataCode asc
	</select>

	<insert id="insertIntoCongigure" >
		insert into t_configure_energykinds(enterprise_id,energy_type,baseCode,energyTypeCode) values
		<foreach collection="list" item="data" separator=",">
			(#{name},#{data.energyCode},#{data.baseCode},#{data.energyTypeCode})
		</foreach>
	</insert>

	<select id="selectEnergyKindsInfo" resultType="com.nantian.cloud.dbcapp.bean.EnergyKindsInfo">
		select energy_type as energyCode, energyTypeCode,convertRation from t_configure_energykinds where enterprise_id = #{enterpriseCode} and baseCode = #{baseCode}
	</select>

	<select id="selectDataInfoByDataCode" resultType="float">
		select consume_sum from t_energy_structure_add where enterprise_id = #{param2} and record_circle = #{param3}
		and dataCode = #{param1}
	</select>

	<select id="selectCoefficientbyDataCode" resultType="float">
		select dataValue from enterprise_energy_data where enterpriseCode = #{param1}
		and dataCode = #{param2}
		and statDate like concat(#{param3},"%")
	</select>

	<select id="selectProductAssist" resultType="com.nantian.cloud.dbcapp.bean.ProductAssist">
		select meaning,dataCode from t_unit_assist where enterpriseCode = #{enterpriseCode}
	</select>

	<select id="selectUnitAssist" resultType="com.nantian.cloud.dbcapp.bean.ProductAssist">
		select tmp.meaning, IFNULL(tmp.dataCode00,tmp.dataCode) dataCode
		from (
			select enterpriseCode, meaning
			, ( select GROUP_CONCAT(distinct dataCode) from t_unit_assist where enterpriseCode = ua.enterpriseCode and meaning = ua.meaning and dataCode like '00000000%') dataCode00
			, ( select GROUP_CONCAT(distinct dataCode) from t_unit_assist where enterpriseCode = ua.enterpriseCode and meaning = ua.meaning and dataCode not like '00000000%') dataCode
			from t_unit_assist ua where ua.enterpriseCode = #{enterpriseCode}
			group by enterpriseCode, meaning
		) tmp
	</select>

	<select id="selectRecyleElectricityFromStructury" resultType="float">
		select consume_sum from t_energy_structure where enterprise_ID = #{enterPriseCode} AND dataCode = #{dataCode}
		and record_circle = #{time}
	</select>


	<select id="selectUnitByCode" resultType="com.nantian.cloud.dbcapp.bean.ValueUnit">
		select  distinct  code as energyCode , qualityUnit as unit, collectUnit as convertRationUnit from dict_energy_type_unit_collect
		where code = #{substring}
	</select>

	<select id="selectUnitByCode1" resultType="com.nantian.cloud.dbcapp.bean.ValueUnit">
		select code as energyCode , qualityUnit as unit, collectUnit as convertRationUnit from dict_energy_substance
		where code = #{substring}
	</select>


	<select id="selectAllUnitList" resultType="com.nantian.cloud.dbcapp.bean.UnitList">
    select a.id,a.enterprise_id as enterpriseCode,a.record_circle as recordCircle ,a.`code` as baseCode ,
    a.guideline_val as dataValue,a.quota_unit as dataValueUnit , a.convertRation ,a.parent_unit as convertRationUnit,
    a.energyTypeCode as energyCode , a.isRight,b.energyConsumeLevel as enterpriseLevel,a.energy_type_code as energyTypeCode from t_unitenergy_add a left join enterprise_info b on a.enterprise_id = b.enterpriseCode
	where  a.enterprise_id in
	<foreach collection="list" open="(" close=")" separator="," item="item">
		#{item}
	</foreach>
	and a.record_circle between #{preTime} and #{finishTime}
	</select>

	<select id="selectUnitInfo01" resultType="com.nantian.cloud.dbcapp.bean.DataValueUnit">
      select `code` as energyCode,qualityUnit as dataValueUnit,collectUnit as convertRationUnit,
      collectRatio as convertRation from dict_energy_type_unit_collect
	</select>

	<select id="selectUnitInfo03" resultType="com.nantian.cloud.dbcapp.bean.DataValueUnit">
     select `code` as energyCode,qualityUnit as dataValueUnit,collectUnit as convertRationUnit from dict_energy_substance
	</select>

	<update id="upsetUnit">
		<foreach collection="list" item="data"  separator=";" >
		 update  t_unitenergy_add set quota_unit =#{data.dataValueUnit} ,parent_unit =#{data.convertRationUnit} ,
		 quota_limit = #{data.enterpriseLevel},
		 isRight = #{data.isRight},
		 convertRation = #{data.convertRation} where id = #{data.id}
	  </foreach>
	</update>


	 <select id="selectEnterpriseInfoAll" resultType="com.nantian.cloud.dbcapp.bean.ExceptionInfoBo">
		 select a.enterpriseCode,a.dataCode,a.dataValue,a.convertRation,a.statDate,b.collectItemName from enterprise_energy_data_exception a
		 left join configure_data_collect_new b on a.enterpriseCode = b.enterpriseCode and a.dataCode = b.dataCode where a.dataCode like '00000000%' and  b.isEnable = '0'
		 and a.addDate like concat(#{time},'%')
	 </select>

	 <insert id="insertExceptionInfo" >
		insert into energy_exception_data_info(enterpriseCode,dataCode,dataValue,convertRation,statDate,collectItemName,exceptionType,operateDate,collectionFrequency)
		values
		<foreach collection="list" item="item"  separator="," >
			(#{item.enterpriseCode},#{item.dataCode},#{item.dataValue},#{item.convertRation},#{item.statDate},#{item.collectItemName},#{item.exceptionType},#{item.operateDate},#{statType})
		</foreach>
	 </insert>

	<select id="selectWaveInfo" resultType="com.nantian.cloud.dbcapp.bean.CollectWaveBo">
		select * from configure_data_collect_tag where dataCode like '00000000%'
	</select>

	<select id="selectConsume" resultType="com.nantian.cloud.dbcapp.bean.ConsumeBo">
		SELECT DISTINCT
	enterprise_id as enterpriseCode,
	dataCode,
	sum( consume_sum )/ count( consume_sum ) as consume
FROM
	t_energy_structure
WHERE
	consume_sum IS NOT NULL
	and consume_sum between 0
	and 100000000
	AND dataCode LIKE '00000000%'
	and record_circle BETWEEN '2020-01' and '2021-02'
GROUP BY
	enterprise_id,
	dataCode
	ORDER BY sum( consume_sum )/ count( consume_sum )  desc
	</select>

	<select id="selectTransException" resultType="com.nantian.cloud.dbcapp.bean.ExceptionInfoBo">
		 select distinct enterpriseCode,dataCode,collectItemName from  configure_data_collect_new where statType = #{type} and isEnable = '0' and isForce = '1'
		<if test=" haltArr != null and haltArr.length > 0">
			and enterpriseCode not in
			<foreach collection="haltArr" item="halt"  index="i" open="(" separator="," close=")">
				#{halt}
			</foreach>
		</if>
		<if test="list != null and list.size > 0">
			and (enterpriseCode,dataCode) not in
			<foreach collection="list" index="i" item="enable" open="(" separator="union" close=")">
				select  #{enable.enterpriseCode} as enterpriseCode, #{enable.code} as dataCode
			</foreach>
		</if>
	</select>

	<select id="selectEnterpriseInfoAllByDay" resultType="com.nantian.cloud.dbcapp.bean.ExceptionInfoBo">
		 select a.enterpriseCode,a.dataCode,a.dataValue,a.convertRation,a.statDate,b.collectItemName from enterprise_energy_data_exception a
		 left join configure_data_collect_new b on a.enterpriseCode = b.enterpriseCode and a.dataCode = b.dataCode where a.statType = '1' and b.isEnable = '0' and b.isForce = '2'
		<if test=" haltArr != null and haltArr.length > 0">
			and a.enterpriseCode not in
			<foreach collection="haltArr" item="halt"  index="i" open="(" separator="," close=")">
				#{halt}
			</foreach>
		</if>
	</select>

	<delete id="delectCollectInfo">
		truncate table enterprise_energy_data_exception
	</delete>

	<insert id="insertInfoByDay" >
		insert into enterprise_energy_data_exception(id, enterpriseCode, dataCode, dataValue, convertRation, valid, scope, inputType, statType, statDate, uploadDate, addDate, digest, storeDate, analysedTimes, uploadedCountry, remark, deleteFlag, exceptionCode, exceptionName)
		select id, enterpriseCode, dataCode, dataValue, convertRation, valid, scope, inputType, statType, statDate, uploadDate, addDate, digest, storeDate, analysedTimes, uploadedCountry, remark, deleteFlag, exceptionCode, exceptionName from enterprise_energy_data where statDate between #{preTime} and #{endTime}
	</insert>



	<select id="isHasOpenId" resultType="int">
		select count(id) from nt_config.sys_user_sub where openid=#{openId}
	</select>

	<insert id="savaOpenId" >
		insert into nt_config.sys_user_sub(user_id,openid) values (#{userId},#{openId})
	</insert>

	<select id="getUserInfo" resultType="com.nantian.cloud.dbcapp.bean.SysUserSub">
		select user_id,dept_id,user_dept from nt_config.sys_user where user_id = (select user_id from nt_config.sys_user_sub where openid=#{openId})
	</select>

	<delete id="deleteYearAndMonthRigth">
		delete from t_energy_structure where record_circle between #{preTime} and #{finishTime}
		<if test="enterpriseCode!=null">
			and enterprise_id = #{enterpriseCode}
		</if>
	</delete>

	<delete id="delectExceptionByDay">
		delete from energy_exception_data_info where statDate like concat(#{format},'%')
	</delete>

	<insert id="merageExceptionByDay">
		insert into energy_exception_data_info select * from energy_exception_data_info_new where statDate like concat(#{format},'%')
	</insert>

	<delete id="deleteExceptionNew">
		delete from energy_exception_data_info_new where statDate like concat(#{format},'%')
	</delete>

	<update id="judge">
		UPDATE energy_exception_data_info set exceptionGov = 0 where exceptionGov is null
	</update>

	<select id="selectEnterpriseInfoAllByMonth" resultType="com.nantian.cloud.dbcapp.bean.ExceptionInfoBo">
     	 select a.enterpriseCode,a.dataCode,a.dataValue,a.convertRation,a.statDate,b.collectItemName from enterprise_energy_data a
		 left join configure_data_collect b on a.enterpriseCode = b.enterpriseCode and a.dataCode = b.dataCode where   a.statDate between #{pre} and #{end} and a.statType = #{type}
		 and a.dataCode like '00000000%'
		<if test=" haltArr != null and haltArr.length > 0">
			and a.enterpriseCode not in
			<foreach collection="haltArr" item="halt"  index="i" open="(" separator="," close=")">
				#{halt}
			</foreach>
		</if>
	</select>

	<select id="selectEnterpriseDataByMonth" resultType="com.nantian.cloud.dbcapp.bean.ExceptionInfoBo">
		select a.enterpriseCode,a.dataCode,a.dataValue,a.convertRation,a.statDate,b.collectItemName from enterprise_energy_data a
		left join configure_data_collect_new b on a.enterpriseCode = b.enterpriseCode and a.dataCode = b.dataCode where  left(a.statDate,7) = #{pre} and a.statType = #{type} and b.isForce = '1'
		<if test=" haltArr != null and haltArr.length > 0">
			and a.enterpriseCode not in
			<foreach collection="haltArr" item="halt"  index="i" open="(" separator="," close=")">
				#{halt}
			</foreach>
		</if>
	</select>

	<select id="analyseEnterpriseCode" resultType="com.nantian.cloud.dbcapp.bean.EnterpriseBo">
 		-- select id, enterpriseCode,statDate as 'statDateTime' ,addDate as 'addTime'from enterprise_energy_data_exception ORDER BY enterpriseCode
		select distinct enterpriseCode, left(statDate,10) as 'statDateTime' , left(addDate,10) as 'addTime'from enterprise_energy_data_exception ORDER BY enterpriseCode
	</select>

    <insert id="insertExceptionInfoMonth">
        insert  into  energy_exception_data_info(enterpriseCode,dataCode,dataValue,convertRation,statDate,collectItemName,exceptionType,collectionFrequency)
        values
        <foreach collection="list" item="item"  separator="," >
            (#{item.enterpriseCode},#{item.dataCode},#{item.dataValue},#{item.convertRation},#{item.statDate},#{item.collectItemName},#{item.exceptionType},#{statType})
        </foreach>
    </insert>

	<update id="updateDataCodeState">
		        update configure_data_collect set isEnable = #{state} where enterpriseCode = #{enterpriseCode} and dataCode = #{dataCode}
	</update>
	<update id="updateDataCodeStateNew">
				        update configure_data_collect_new set isEnable = #{state} where enterpriseCode = #{enterpriseCode} and dataCode = #{dataCode}
	</update>
	<select id="selectEnterpriseCode" resultType="string">
		select distinct  enterpriseCode from enterprise_info where 1 = 1
		<if test="enterpriseCode != null">
			and enterpriseCode = #{enterpriseCode}
		</if>
	</select>
	<delete id="deleteEnergyKinds">
		delete  from t_configure_energykinds where enterprise_id = #{enterpriseCode}
	</delete>
	<delete id="deleteExceptionByCollect">
		delete from energy_exception_data_info
		where enterpriseCode = #{epc} and dataCode not in
		(select distinct dataCode from configure_data_collect where enterpriseCode = #{epc} and dataCode like '00000000%')
	</delete>
	<delete id="truncateDistinctEnergyData">
		truncate table ${tableName}
	</delete>
	<delete id="truncateDistinctEnergyDataTemp">
		truncate table `distinct_energy_data_temp`
	</delete>

	<delete id="deleteNewCollect">
		delete from configure_data_collect_new
 		where (enterpriseCode,dataCode) not in (select distinct enterpriseCode, dataCode from configure_data_collect)
	</delete>


	<select id="getEffectiveEnterprise" resultType="string">
		select distinct enterprise_id as enterpriseCode  from t_energy_structure
		where dataCode like '00000000%' and record_circle = #{time}
	</select>

	<select id="selectEnergyKindsValue" resultType="float">
		select guideline_val from t_unitenergy2020 where enterprise_id= #{enterpriseCode}
		and record_circle = #{time} and code =#{baseCode} and energyTypeCode = #{energyCode} and
		energy_type_code = #{energyTypeCode}
	</select>

	<insert id="insetEpcList">
		insert into t_effective_enterprise (enterpriseCode,`time`,regionCode,tid) values
		<foreach collection="list" item="index" separator="," >
			(#{index.enterpriseCode},#{time},#{index.regionCode},#{index.tid})
		</foreach>
	</insert>

	<update id="updateEpcAreaAndIndustry" >
		UPDATE t_effective_enterprise as  a left  join  enterprise_info as  b on  a.enterpriseCode = b.enterpriseCode  set a.regionCode = b.regionCode ,a.tid = b.tid
			</update>

	<select id="selectOutputValue" resultType="com.nantian.cloud.dbcapp.bean.XYBo">
		SELECT enterpriseCode as x,dataCode as y from t_output_datacode
	</select>
	<select id="selectZhnh" resultType="float">
		SELECT guideline_val from t_unitenergy2020 where enterprise_id = #{enterpriseCode}
		AND record_circle = #{time} and energyTypeCode = '0000' and code='00000000'
		and guideline_val &lt; 1000000 and guideline_val >0 and isRight = "1"
	</select>

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

	<select id="insetOutPutValue">
		insert into t_productvalue (enterpriseCode,dataCode,outputValue,energyOutputValue,`time`) values
		<foreach collection="list" item="index" separator="," >
			(#{index.enterpriseCode},#{index.dataCode},#{index.outputValue},#{index.energyOutputValue},#{time})
		</foreach>
	</select>

	<select id="selectRightExceptionRation" resultType="com.nantian.cloud.dbcapp.vo.ExceptionInfoVo">
		select enterpriseCode,dataCode,rationValue as convertRation from t_right_exception_value
	</select>

    <insert id="insetAddEnterpriseDataInfo">
        insert into enterprise_energy_data_add
        select null,id,enterpriseCode,dataCode,dataValue,convertRation,valid,scope,inputType,statType,statDate,uploadDate,addDate,digest,storeDate,analysedTimes,uploadedCountry,remark,deleteFlag
        from enterprise_energy_data a where a.id in
        <foreach collection="list" open="(" close=")" separator="," item="data">
        	#{data}
		</foreach>
    </insert>
	<insert id="insertDistinctEnergyDataTemp">
		insert into distinct_energy_data_temp
		select * from enterprise_energy_data where enterpriseCode = #{epc} and statDate like concat(#{year},"%") and dataCode = #{collect}
	</insert>
	<insert id="insertDistinctEnergyDataTempByAddDate">
		insert into distinct_energy_data_temp
		select * from enterprise_energy_data where (enterpriseCode, dataCode, left(statDate,${num})) in
		(
			select enterpriseCode, dataCode, left(statDate,${num}) from enterprise_energy_data
			where enterpriseCode = #{epc} and addDate like concat(#{addDate},"%") and dataCode = #{collect}
		)
	</insert>
	<insert id="insertEnterpriseTmp">
		-- 更新企业信息中间表
		truncate table enterprise_info_all;
		insert into enterprise_info_all
		select distinct t.enterpriseCode,
		case when e1.cmpName is not null then e1.cmpName when e2.cmpName is not null then e2.cmpName when e3.cmpName is not null then e3.cmpName when e4.enterprise_name is not null then e4.enterprise_name end cmpName,
		concat(case when e1.id is not null and e1.reportNum like '%已完成%' then '1,' else '' end, case when e5.enterpriseCode is not null or e2.id is not null then '2,' else '' end, case when e3.id is not null then '3,' else '' end, case when e4.id is not null then '4,' else '' end) type,
		case when e1.regionCode is not null then e1.regionCode when e2.regionCode is not null then e2.regionCode when e3.regionCode is not null then e3.regionCode when e4.region_code is not null then e4.region_code end regionCode,
		case when e1.tid is not null then e1.tid when e2.industry_code is not null then e2.industry_code when e3.tid is not null then e3.tid when e4.industryCode is not null then e4.industryCode end tid
		from (
		select distinct enterpriseCode from (
			select enterpriseCode from electric_enterprise_info where 1=1
			union all
			select enterprise_code from t_enterprise_old where 1=1 and state =0
			union all
			select enterpriseCode from enterprise_info where 1=1
			union all
			select enterpriseCode from upload_country_enterprise_info where uploadCompleted = '1'
		)a ) t left join enterprise_info e1 on e1.enterpriseCode = t.enterpriseCode
		left join electric_enterprise_info e2 on e2.enterpriseCode = t.enterpriseCode
		left join upload_country_enterprise_info e3 on e3.enterpriseCode = t.enterpriseCode and e3.uploadCompleted = '1'
		left join t_enterprise_old e4 on e4.enterprise_code = t.enterpriseCode
		left join (
			select distinct enterpriseCode from configure_data_collect where substr(dataCode,11,4) = '3300'
			and enterpriseCode in ( select enterpriseCode from enterprise_info)
		) e5 on e5.enterpriseCode = t.enterpriseCode;
		-- 更新更新487、299企业名单 正在对接或已完成对接状态
		update access_enterprise_info t1, (
			select enterpriseCode, case when JSON_EXTRACT(reportNum,"$.accomplish") = "已完成" then '4' else '3' end state from enterprise_info
		) t2 set t1.state = t2.state
		where t1.state != '2' and t1.enterprise_code = t2.enterpriseCode;
	</insert>
    <insert id="insertEnterpriseJly">
		-- 将energy_record库的enterprise_info表信息 同步至 jly库的enterprise_info表
		update jly.enterprise_info je, energy_record.enterprise_info ee
		set je.cmp_name = ee.cmpName,
		je.isorg = ee.isorg,
		je.org_type = ee.orgType,
		je.org_code = ee.orgCode,
		je.password = ee.password,
		je.address = ee.address,
		je.zip_code = ee.zipCode,
		je.corporation_name = ee.corporationName,
		je.region_code = ee.regionCode,
		je.license = ee.license,
		je.organization = ee.organization,
		je.register_address = ee.registerAddress,
		je.register_date = ee.registerDate,
		je.register_principal = ee.registerPrincipal,
		je.type_code = ee.typeCode,
		je.industry_code = ee.industryCode,
		je.field_code = ee.fieldCode,
		je.energy_consume_level = ee.energyConsumeLevel,
		je.center = ee.center,
		je.jgzh = ee.jgzh,
		je.latitude = ee.latitude,
		je.longitude = ee.longitude,
		je.phone = ee.phone,
		je.fax = ee.fax,
		je.email = ee.email,
		je.url = ee.url,
		je.production_line = ee.productionLine,
		je.leading_product = ee.leadingProduct,
		je.group_name = ee.groupName,
		je.group_address = ee.groupAddress,
		je.apply_date = ee.applyDate,
		je.approval_date = ee.approvalDate,
		je.last_login_date = ee.lastLoginDate,
		je.state = ee.state,
		je.upload_time = ee.uploadTime,
		je.remark = ee.remark,
		je.check_upload_time = ee.checkUploadTime,
		je.can_upload_country = ee.canUploadCountry,
		je.uploaded_country = ee.uploadedCountry,
		je.t_id = ee.tid,
		je.dept_id = ee.regionCode
		where je.enterprise_code = ee.enterpriseCode;
        -- 将energy_record库的enterprise_info表新增企业 导入至 jly库的enterprise_info表
		insert into jly.enterprise_info(id,enterprise_code,isorg,org_type,org_code,password,cmp_name,address,zip_code,corporation_name,region_code,license,organization,register_address,register_date,register_principal,type_code,industry_code,field_code,energy_consume_level,center,jgzh,latitude,longitude,phone,fax,email,url,production_line,leading_product,group_name,group_address,apply_date,approval_date,last_login_date,state,upload_time,remark,check_upload_time,can_upload_country,uploaded_country,t_id,dept_id)
		select id,enterpriseCode,isorg,orgType,orgCode,password,cmpName,address,zipCode,corporationName,regionCode,license,organization,registerAddress,registerDate,registerPrincipal,typeCode,industryCode,fieldCode,energyConsumeLevel,center,jgzh,latitude,longitude,phone,fax,email,url,productionLine,leadingProduct,groupName,groupAddress,applyDate,approvalDate,lastLoginDate,state,uploadTime,remark,checkUploadTime,canUploadCountry,uploadedCountry,tid,regionCode
		from energy_record.enterprise_info where enterpriseCode not in (select distinct enterprise_code from jly.enterprise_info);
	</insert>

	<insert id="insertConfigureGauge">
	    insert into configure_gauge(enterpriseCode, nextAlignDate, alignOrg, unAlignReason, location, installer, installTime, linkSys, currentState, stateChangeTime, serNo, barCode, meterName, meterType, meterLevel, params, dataCode, reportArithmetic, reportRatio, manufacturer, model, precisionLevel, ranges, manageNo, alignState, alignCycle, LastAlignDate, state, remark, paramsType, uploadedCountry, digest, quoted, del_flag)
	    <foreach collection="list" item="item" separator="union">
			select #{item.enterpriseCode} as enterpriseCode, #{item.nextAlignDate} as nextAlignDate, #{item.alignOrg} as alignOrg, #{item.unAlignReason} as unAlignReason, #{item.location} as location, #{item.installer} as installer, #{item.installTime} as installTime, #{item.linkSys} as linkSys, #{item.currentState} as currentState, #{item.stateChangeTime} as stateChangeTime,
			#{item.serNo} as serNo, #{item.barCode} as barCode, #{item.meterName} as meterName, #{item.meterType} as meterType, #{item.meterLevel} as meterLevel, #{item.params} as params, #{item.dataCode} as dataCode, #{item.reportArithmetic} as reportArithmetic, #{item.reportRatio} as reportRatio, #{item.manufacturer} as manufacturer, #{item.model} as model, #{item.precisionLevel} as precisionLevel,
			#{item.ranges} as ranges, #{item.manageNo} as manageNo, #{item.alignState} as alignState, #{item.alignCycle} as alignCycle, #{item.LastAlignDate} as LastAlignDate, #{item.state} as state, #{item.remark} as remark, #{item.paramsType} as paramsType, #{item.uploadedCountry} as uploadedCountry, #{item.digest} as digest, #{item.quoted} as quoted, '1' as del_flag
		</foreach>
	</insert>

	<insert id="etlGauges">
	    delete from configure_gauge where enterpriseCode = #{epc};
	    insert into configure_gauge(enterpriseCode,meterName,model,meterLevel,ranges,manufacturer,serNo,meterType,params,manageNo,location,alignCycle,alignState,del_flag)
	    <foreach collection="list" item="item" separator="union">
			select #{item.enterpriseCode} as enterpriseCode,#{item.meterName} as meterName,#{item.model} as model,#{item.meterLevel} as meterLevel,#{item.ranges} as ranges,#{item.manufacturer} as manufacturer,#{item.serNo} as serNo,#{item.meterType} as meterType,#{item.params} as params,#{item.manageNo} as manageNo,#{item.location} as location,#{item.alignCycle} as alignCycle,#{item.alignState} as alignState,"1" as del_flag
		</foreach>
    </insert>

	<insert id="insertEnterpriseDataTmp">
		-- 清空表数据
		truncate table enterprise_data_all;
		-- 在线采集数据
		insert into enterprise_data_all(epc,statDate,num,type)
		select enterpriseCode epc, left(statDate,10) statDate, count(1) num, "1" type
		from enterprise_energy_data  group by left(statDate,10), enterpriseCode;
		-- 电网数据
		insert into enterprise_data_all(regionCode,tid,statDate,num,type)
		select region, industry, time, count(id) num, "2" type
		from (
			select id, region, IFNULL(industry,(select distinct tid from enterprise_info_all where t.enterpriseCode = enterpriseCode)) industry, time from company_data_detail t
		) tmp group by region, industry, time;
		-- 上送国家数据
		insert into enterprise_data_all(epc,statDate,num,type)
		select enterpriseCode epc, left(statDate,10) statDate, count(1) num, "3" type
		from enterprise_backup_data group by left(statDate,10), enterpriseCode;
		-- 修改在线采集、上送国家数据所在行业和地区
		update enterprise_data_all t1, enterprise_info_all t2
		set t1.regionCode = t2.regionCode, t1.tid = t2.tid
		where t1.type in ('1','3') and t1.epc = t2.enterpriseCode;
		-- 传输异常数据、波动异常数据
		insert into enterprise_data_all(epc,statDate,num,exceptionType,type)
		select enterpriseCode, left(statDate,7), count(1) num, case when exceptionType like '%0101%' then '1' else '2' end exceptionType, "4" type
    	from energy_exception_data_info where handlerType != 0
    	group by enterpriseCode, left(statDate,7), exceptionType;
	</insert>

	<insert id="insertUnitEnergyConsumption">
		truncate table energy_record.t_output_value;
		insert into energy_record.t_output_value( id, enterprise_code, output_value, output_value_unit, record_circle, record_circle_type)
		select null as id, case when tt.epc is null or tt.epc='' then tt.enterprise_code else tt.epc end enterprise_code, tt.unit_energy_consumption as output_value, unit as output_value_unit, tt.year as record_circle, "年" as record_circle_type
		from(
			select f.report_id, f.unit_energy_consumption, '吨标准煤/万元' as unit, r.id, r.year, r.type, r.enterprise_code, e.enterprise_code as oldEpc, e.epc
			from jnjc.t_report_financial f
			left join jnjc.t_report r on f.report_id = r.id
			left join jnjc.enterprise_info e on r.enterprise_code = e.enterprise_code
		)tt where tt.unit_energy_consumption is not null and tt.unit_energy_consumption != 0 ORDER BY tt.year asc;
	</insert>

	<select id="selectEnterpriseInfoByEpc" resultType="string">
		select enterpriseCode from enterprise_info
		<where>
			<if test="epc != null">
				enterpriseCode	 = #{epc}
			</if>
		</where>
		order by enterpriseCode
	</select>


	<select id="selectElectricityDataInfo" resultType="com.nantian.cloud.dbcapp.bean.XYBo">
     select enterpriseCode as x ,dataValue as y from t_electricity_data_value where
     enterpriseCode in <foreach collection="list" open="(" close=")" separator="," item="item">
		#{item}
	</foreach>
     and statDate = #{time}
	</select>

	<select id="selectCollectDataInfo" resultType="float">
     select consume_sum from t_energy_structure where enterprise_id = #{epc}
     and dataCode = #{dataCode}
     and record_circle = #{time}
	</select>

	<update id="updateCollectDataInfoByElectricity">
      update t_energy_structure set consume_sum = #{y} ,object = 0 where enterprise_id =#{x}
      and dataCode = #{dataCode} and record_circle = #{time}
	</update>

	<update id="updateNewCollect">
		 update configure_data_collect_new n, (select distinct enterpriseCode, dataCode, statType from configure_data_collect) c
		 set n.statType = c.statType
		 where n.enterpriseCode = c.enterpriseCode and n.dataCode = c.dataCode
	</update>

	<update id="updateOldEnterpriseName">
		update t_enterprise_old e1, enterprise_info e2
		set e1.enterprise_name = e2.cmpName
		where e1.enterprise_code = e2.enterpriseCode
	</update>

	<update id="updateEnterpriseTrade">
		-- 设置在线企业通俗行业编码
		update enterprise_info e1, (
			select a1.enterprise_code, a1.enterprise_name, a2.name, a2.popular_industry from data_quality_status a1 left join quality_industry a2 on a1.quality_industry = a2.id
		) t1, temp_dict_name t2 set e1.tid = t2.id where e1.enterpriseCode = t1.enterprise_code and t1.popular_industry = t2.name and e1.tid is null;
		-- 设置在线企业国民经济行业（1）
		update enterprise_info e1, enterprise_info e2
		set e1.jnjcTrade = e2.jnjcTrade
		where e1.jnjcTrade is null and e1.industryCode = e2.industryCode;
		-- 设置在线企业国民经济行业（2）
		update enterprise_info e1, (select d.meaning, e.enterpriseCode from enterprise_info e left join dict_industry_code d on e.industryCode = d.id where e.jnjcTrade is null) e2
		set e1.jnjcTrade = e2.meaning
		where  e1.jnjcTrade is null and e1.enterpriseCode = e2.enterpriseCode;
	</update>

    <update id="configEndTime">
		-- 更新配置表信息(截止时间及之后走新表，截止时间之前走老表)
		update history_config_endtime e1, ( select enterprise_id, min(record_circle) tmpTime from t_unitenergy2020 where guideline_val != 0 and energyTypeCode = '0000' and `code`='00000000'  group by enterprise_id) e2
		set e1.endTime = e2.tmpTime
		where e1.endTime > e2.tmpTime and e1.enterpriseCode = e2.enterprise_id;
		-- 新增配置表信息
		insert into history_config_endtime(enterpriseCode,endTime)
		select distinct e.enterpriseCode, (select min(record_circle) tmpTime from t_unitenergy2020 where guideline_val != 0 and energyTypeCode = '0000' and `code`='00000000' and enterprise_id = e.enterpriseCode) tmpTime
		from (select enterpriseCode from enterprise_info where enterpriseCode not in (select distinct enterpriseCode from history_config_endtime)) e HAVING tmpTime is not null;
	</update>

	<update id="updateDataException">
		update enterprise_energy_data set isIgnore = 1 where id in
		<foreach collection="list" item="item" open="(" separator="," close=")">
			#{item}
		</foreach>
	</update>

	<select id="selectEpcDictIndustryCode" resultType="com.nantian.cloud.dbcapp.vo.EpcDictIndustryCode">
		select enterpriseCode,regionCode,tid from enterprise_info where
		enterpriseCode in <foreach collection="list" item="item" open="(" separator="," close=")">
		#{item}
	</foreach>
	</select>

	<select id="selectExceptionTrans" resultType="com.nantian.cloud.dbcapp.vo.JudegTransExceptionBo">
		select id,enterpriseCode,dataCode,statDate from energy_exception_data_info where exceptionType like concat('%',#{exceptionType},'%')  and handlerType != 0 and statDate like concat(#{date},'%')
		order by enterpriseCode
	</select>

	<select id="selectTransExceptionValue" resultType="com.nantian.cloud.dbcapp.vo.JudegTransExceptionBo">
		select id,enterpriseCode,dataCode,statDate,dataValue from enterprise_energy_data where  statDate like concat(#{date},'%')
		<if test="list != null and list.size > 0 ">
			and (enterpriseCode,dataCode) in
			<foreach collection="list" item="item" open="(" separator="union" close=")">
				select #{item.enterpriseCode} as enterpriseCode, #{item.dataCode} as dataCode
			</foreach>
		</if>
		order by enterpriseCode
	</select>

	<select id="updateTransException" resultType="int">
		update energy_exception_data_info set handlerType = 0, handlerDate = (select now()), remark = "系统定时处理异常" where id in
		<foreach collection="list" item="item" open="(" separator="," close=")">
			#{item}
		</foreach>
	</select>
	<select id="selectExceptionTransMonth" resultType="com.nantian.cloud.dbcapp.vo.JudegTransExceptionBo">
		select a.id,a.enterpriseCode,a.dataCode,a.statDate from energy_exception_data_info a  left join configure_data_collect b on a.enterpriseCode = b.enterpriseCode and a.dataCode = b.dataCode
		where exceptionType like concat('%',#{exceptionType},'%') and statDate like concat(#{date},"%")
		and statType = '2' and a.handlerType != 0 order by enterpriseCode
	</select>
	<select id="selectTransExceptionValueMonth" resultType="com.nantian.cloud.dbcapp.vo.JudegTransExceptionBo">
		select distinct id,enterpriseCode,dataCode,statDate,dataValue from enterprise_energy_data where  statDate between  #{pre} and #{end}
		<if test="list != null and list.size > 0 ">
			and (enterpriseCode,dataCode) in
			<foreach collection="list" item="item" open="(" separator="union"  close=")">
				select #{item.enterpriseCode} as enterpriseCode, #{item.dataCode} as dataCode
			</foreach>
		</if>
		order by enterpriseCode
	</select>
	<select id="getEnergyConvertRation" resultType="java.lang.Float">
		select IFNULL(collectRatio,0) collectRatio from dict_energy_type_unit_collect where id = #{energyCode}
	</select>
	<select id="getExceptionDate" resultType="java.lang.String">
		select distinct left(statDate,10) from energy_exception_data_info where handlerType != 0
		<choose>
			<when test="exceptionType == '2'.toString()">
				and exceptionType not like '%0101%'  -- 波动异常
			</when>
			<otherwise>
				and exceptionType like concat('%',#{exceptionType},'%') -- 传输异常
			</otherwise>
		</choose>
	</select>
	<select id="selectWaveException" resultType="com.nantian.cloud.dbcapp.vo.JudegTransExceptionBo">
		select id,enterpriseCode,dataCode,statDate,dataValue from energy_exception_data_info
		where exceptionType not like '%0101%' and handlerType != 0 and statDate like concat(#{date},'%') order by enterpriseCode
	</select>
	<select id="selectWaveExceptionValue" resultType="com.nantian.cloud.dbcapp.vo.JudegTransExceptionBo">
		select id,enterpriseCode,dataCode,statDate,dataValue from enterprise_energy_data where statDate like concat(#{date},'%')
		<if test="list != null and list.size > 0 ">
			and (enterpriseCode,dataCode) in
			<foreach collection="list" item="item" open="(" separator="union" close=")">
				select #{item.enterpriseCode} as enterpriseCode, #{item.dataCode} as dataCode
			</foreach>
		</if>
		order by enterpriseCode
	</select>
	<select id="selectUnitEnergyByStructure" resultType="java.lang.Float">
		select sum(consume_sum) from t_energy_structure where enterprise_id = #{enterpriseCode} and record_circle = #{time}
		and substring(dataCode,1,8) = #{base} and substring(dataCode,11,4) = #{dataCode}
	</select>
	<select id="getProductCode" resultType="java.lang.String">
		select distinct product_code from history_enterprise_product where enterprise_code = #{enterpriseCode}
	</select>
    <select id="getCorrectEnergyCode" resultType="java.lang.String">
		select distinct energyCode from t_energy_tce_correct where enterpriseCode = #{enterpriseCode} and calculateTime = #{calculateTime}
	</select>
	<select id="getCorrectConsume" resultType="com.nantian.cloud.dbcapp.vo.EffectiveBo">
		select tmp.energyCode,
		case when count(tmp.dataCode) > 1 then sum(tmp.consumeSum*tmp.modulus*tmp.calculate) else sum(tmp.consumeSum*tmp.calculate) end consume,
		case when count(tmp.dataCode) > 1 then 1 else tmp.modulus end modulus
		from (
			select c.energyCode, c.dataCode,
			case when SUBSTR(c.dataCode,11,4) in ("3300", "1100", "1200", "1300", "1400", "1500", "1600", "1700", "4200") then IFNULL(sum(t.consume_sum)/10000,0)
			     when SUBSTR(c.dataCode,11,4) = "4000" then IFNULL(sum(t.consume_sum)/1000,0) else IFNULL(sum(t.consume_sum),0) end consumeSum,
			case when c.ratioFlag = 1 then IFNULL(c.ratio,t.modulus) else 1 end modulus,
			case when c.calculate = 1 then 1 else -1 end calculate
			from t_energy_tce_correct c
			left join t_energy_structure t on t.enterprise_id = c.enterpriseCode and t.dataCode = c.dataCode and t.record_circle = #{time}
			where c.enterpriseCode = #{enterpriseCode} and c.calculateTime = #{calculateTime} and c.energyCode =#{energyCode} group by c.dataCode
		) tmp group by tmp.energyCode
	</select>
	<select id="getCorrectConsumeByQuotaName" resultType="com.nantian.cloud.dbcapp.vo.EffectiveBo">
		select tmp.quotaName, sum(tmp.consumeSum*tmp.modulus*tmp.calculate) consume
		from (
			select c.quotaName, c.dataCode,
			case when SUBSTR(c.dataCode,11,4) in ("3300", "1100", "1200", "1300", "1400", "1500", "1600", "1700", "4200") then IFNULL(sum(t.consume_sum)/10000,0)
			     when SUBSTR(c.dataCode,11,4) = "4000" then IFNULL(sum(t.consume_sum)/1000,0) else IFNULL(sum(t.consume_sum),0) end consumeSum,
			case when c.ratioFlag = 1 then IFNULL(c.ratio,t.modulus) else 1 end modulus,
			case when c.calculate = 1 then 1 else -1 end calculate
			from t_energy_tce_correct c
			left join t_energy_structure t on t.enterprise_id = c.enterpriseCode and t.dataCode = c.dataCode and t.record_circle = #{time}
			where c.enterpriseCode = #{enterpriseCode} and c.calculateTime = #{calculateTime} and c.quotaName =#{quotaName} group by c.dataCode
		) tmp group by tmp.quotaName
	</select>
	<select id="selectCollect" resultType="java.lang.String">
		select dataCode from configure_data_collect where enterpriseCode = #{epc} and statType = #{statType} order by dataCode asc
	</select>
	<select id="selectEnergyData" resultType="com.nantian.cloud.dbcapp.vo.JudegTransExceptionBo">
		select 0+cast(dataValue as char) dataValue, count(*) statDate
		from distinct_energy_data_temp where enterpriseCode = #{epc} and dataCode = #{collect} and statDate like concat(#{date},"%")
		group by 0+cast(dataValue as char) order by dataValue asc
	</select>
	<select id="selectEnergyDataInfo" resultType="com.nantian.cloud.dbcapp.vo.EnergyDataVo">
		select (select distinct cmpName from enterprise_info where enterpriseCode = d.enterpriseCode) cmpName, d.enterpriseCode,
		(select distinct collectItemName from configure_data_collect where enterpriseCode = d.enterpriseCode and dataCode = d.dataCode ) collectItemName, d.dataCode,
		(select distinct statType from configure_data_collect where enterpriseCode = d.enterpriseCode and dataCode = d.dataCode ) statType,
		 d.dataCode dDataCode, d.dataValue, d.convertRation, d.statType dStatType, d.statDate, d.uploadDate, d.addDate, d.id dId,
		case when #{state} is null or #{state} = "" then '4' else #{state} end state
		from distinct_energy_data_temp d
		where d.enterpriseCode = #{epc} and d.dataCode = #{collect} and d.statDate like concat(#{date},"%")
		order by d.dataValue desc, d.statDate asc
	</select>
	<select id="selectEnergyDataInfo2" resultType="com.nantian.cloud.dbcapp.vo.EnergyDataVo">
		select (select distinct cmpName from enterprise_info where enterpriseCode = d.enterpriseCode) cmpName, d.enterpriseCode,
		(select distinct collectItemName from configure_data_collect where enterpriseCode = d.enterpriseCode and dataCode = d.dataCode ) collectItemName, d.dataCode,
		(select distinct statType from configure_data_collect where enterpriseCode = d.enterpriseCode and dataCode = d.dataCode ) statType,
		 d.dataCode dDataCode, d.dataValue, d.convertRation, d.statType dStatType, d.statDate, d.uploadDate, d.addDate, d.id dId,
		case when #{state} is null or #{state} = "" then '4' else #{state} end state
		from distinct_energy_data_temp d
		where d.enterpriseCode = #{epc} and d.dataCode = #{collect} and d.statDate like concat(#{date},"%")
		order by d.uploadDate asc
	</select>
	<select id="distinctEnergyDataTemp" resultType="java.lang.String">
		select left(statDate,${num}) from distinct_energy_data_temp
		where enterpriseCode = #{epc} and dataCode = #{collect} group by left(statDate,${num}) having(count(*)) > 1
	</select>
	<select id="selectPreUnitProduct" resultType="java.lang.Float">
		select dataValue from enterprise_energy_data
		where enterpriseCode = #{enterpriseCode} and dataCode = #{productCode} and #{time} > left(statDate,7)
		and dataValue is not null and dataValue != 0 order by statDate desc limit 0,1
	</select>
	<select id="getGuidelineVal" resultType="com.nantian.cloud.dbcapp.vo.JudegTransExceptionBo">
		select IFNULL(guideline_val,0) dataValue, record_circle statDate from t_unitvalue
		where enterprise_id = #{enterpriseCode} and quota_name = #{name} and #{time} > record_circle
		and guideline_val is not null and guideline_val != 0 order by record_circle desc limit 0,1
	</select>
    <select id="selectEnterpriseData" resultType="com.nantian.cloud.dbcapp.vo.JudegTransExceptionBo">
        select id,enterpriseCode,dataCode,statDate,dataValue from enterprise_energy_data
        where enterpriseCode = #{enterpriseCode} and dataCode = #{dataCode} and statDate like concat(#{date},'%')
    </select>
	<select id="getProductName" resultType="com.nantian.cloud.dbcapp.bean.ValueUnit">
		select distinct produceNo as energyCode, produceUnit as unit
		from company_materiel_product where enterpriseCode = #{enterpriseCode} and produceNo = #{energyCode}
	</select>

	<select id="getConfigureGaugeEpc" resultType="java.lang.String">
        select distinct enterpriseCode from configure_gauge
	</select>
    <insert id="noProvStatusLog">
        insert into jnjc.t_report_audit_log(report_id, report_type, year, enterprise_code, audit_cmp_name, dept_id, update_time, audit_name)
        select GROUP_CONCAT(r.id) rId, GROUP_CONCAT( distinct r.type ORDER BY r.type asc) rType, r.year, r.enterprise_code, e.cmp_name, e.region_code, (select date_format(NOW(), '%Y-%m-%d')),
        case when d.pname is not null and d.pname != '' then concat(d.pname,",",d.name) else d.name end region
        from jnjc.t_report r
        left join jnjc.enterprise_info e on r.enterprise_code = e.enterprise_code
        left join (select d.id, d.meaning2 name, (select meaning2 from dict_region where id = d.pid and d.pid != '530000') pname from jnjc.dict_region d) d on e.region_code = d.id
        where r.prov_status = "2" and r.del_flag = '1' and  DATEDIFF(#{date},r.date) > ${step}
		group by r.enterprise_code, r.year
	</insert>
    <insert id="renewMaterielProduct">
		INSERT INTO company_materiel_product(id, enterpriseCode, produceName, produceNo, produceType, produceUnit, remark, state, digest, quoted)
		VALUES (#{item.id}, #{item.enterpriseCode}, #{item.produceName}, #{item.produceNo}, #{item.produceType}, #{item.produceUnit}, #{item.remark}, #{item.state}, #{item.digest}, #{item.quoted})
		ON DUPLICATE KEY UPDATE
			id = #{item.id}, enterpriseCode = #{item.enterpriseCode}, produceName = #{item.produceName}, produceNo = #{item.produceNo},
		    produceType = #{item.produceType}, produceUnit = #{item.produceUnit}, remark = #{item.remark}, state = #{item.state},
		    digest = #{item.digest}, quoted = #{item.quoted}
	</insert>
    <update id="upProvStatus">
	update jnjc.t_report t1, (
		select id, enterprise_code, year, type, date, DATEDIFF(#{date},date) diffDate
		from jnjc.t_report where del_flag = '1' and prov_status = '2'
		having diffDate > ${step} order by date asc
	) t2 set t1.prov_status = '1' where t1.id = t2.id
	</update>

	<delete id="insertNewCollect">
		INSERT into configure_data_collect_new (id,enterpriseCode,dataCode,collectItemName,processCode,processUnitCode,equipmentCode,
												equipmentUnitCode,energyClassCode,energyTypeCode,dataUsageCode,inputType,dataValueMax,
												dataValueMin,statType,collectSystemName,scope,state,remark,uploadedCountry,
												digest,quoted,quotedByCollect,updateSay,deleteSay,isEnable
		) select id,enterpriseCode,dataCode,collectItemName,processCode,processUnitCode,equipmentCode,
				 equipmentUnitCode,energyClassCode,energyTypeCode,dataUsageCode,inputType,dataValueMax,
				 dataValueMin,statType,collectSystemName,scope,state,remark,uploadedCountry,
				 digest,quoted,quotedByCollect,updateSay,deleteSay,isEnable
		from configure_data_collect where (enterpriseCode,dataCode) not in (select distinct enterpriseCode, dataCode from configure_data_collect_new)
	</delete>

</mapper>
