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

    <select id="selectElectricityDataValue" resultType="com.nantian.cloud.dbcapp.bean.TElectricityDataValue">
        select  enterpriseCode, dataValue
        from t_electricity_data_value
        where statDate = #{statDate}
    </select>

    <select id="selectEnergyStructure" resultType="com.nantian.cloud.dbcapp.bean.TEnergyStructure">
        select consume_sum as consumeSum
        from t_energy_structure
        where record_circle = #{recordCircle} and enterprise_id = #{enterpriseId} and dataCode = #{dataCode}
    </select>

    <insert id="insertEnergyExceptionResultInfo" >
        insert into
            energy_exception_result_info(enterpriseCode,dataCode,quotaName,dataValue,quotaUnit,exceptionType,statDate,datumValue,operateDate)
        values
            (#{enterpriseCode},#{dataCode},#{quotaName},#{dataValue},#{quotaUnit},#{exceptionType},#{statDate},#{datumValue},#{operateDate})
    </insert>

    <select id="selectUnitenergy" resultType="com.nantian.cloud.dbcapp.bean.TUnitenergy2020">
        select distinct enterprise_id as enterpriseId,guideline_val as guidelineVal
        from  t_unitenergy2020 where record_circle = #{recordCircle} and  code = #{code} and energyTypeCode = #{energyTypeCode}
        <if test=" haltList != null and haltList.size > 0">
            and enterprise_id not in
            <foreach collection="haltList" item="halt"  index="i" open="(" separator="," close=")">
                #{halt}
            </foreach>
        </if>
    </select>

    <select id="selectNergyConsumeLevel" resultType="string">
        select energyConsumeLevel as energyConsumeLevel
        from enterprise_info
        where enterpriseCode = #{enterpriseId}
    </select>

    <insert id="insertComprehensiveEnergyExceptionResult" >
        insert into
            energy_exception_result_info(enterpriseCode,quotaName,dataValue,quotaUnit,exceptionType,statDate,energyLevel,operateDate)
        values
            (#{enterpriseCode},#{quotaName},#{dataValue},#{quotaUnit},#{exceptionType},#{recordCircle},#{energyConsumeLevel},#{operateDate})
    </insert>

    <delete id="deleteComprehensiveEnergyExceptionResult">
        delete from energy_exception_result_info
        where quotaName = #{quotaName} and statDate = #{recordCircle}
    </delete>

    <delete id="deleteUnitComprehensiveEnergyExceptionResult">
        delete from energy_exception_result_info where statDate >= #{statDate} and quotaName != "综合能耗" AND quotaName != "电力"
    </delete>

    <select id="insertUnitComprehensiveOvertop">
        INSERT INTO energy_exception_result_info ( enterpriseCode, quotaName, dataValue, quotaUnit, exceptionType, statDate, datumValue, operateDate )
            (SELECT
                 t.enterprise_id as enterpriseCode,
                 t.quota_name as quotaName,
                 t.guideline_val as dataValue,
                 s.value_unit as quotaUnit,
                 "0211" as exceptionType,
                 t.record_circle as statDate,
                 ( s.limit_value * 1.2 ) as datumValue,
                 now() as operateDate
             FROM
                 t_unitvalue t
                     LEFT JOIN data_quality_status d ON t.enterprise_id = d.enterprise_code
                     LEFT JOIN t_standard s ON d.quality_industry = s.code
                     AND t.quota_name = s.quota_name
             WHERE
                <![CDATA[  t.record_circle >= #{statDate} ]]>
                <![CDATA[  AND t.guideline_val > (s.limit_value * 1.2) ]]>
            )
    </select>

    <select id="insertUnitComprehensiveUnder">
        INSERT INTO energy_exception_result_info ( enterpriseCode, quotaName, dataValue, quotaUnit, exceptionType, statDate, datumValue, operateDate )
            (SELECT
                 t.enterprise_id as enterpriseCode,
                 t.quota_name as quotaName,
                 t.guideline_val as dataValue,
                 s.value_unit as quotaUnit,
                 "0210" as exceptionType,
                 t.record_circle as statDate,
                 ( s.advanced_value * 0.8 ) as datumValue,
                 now() as operateDate
             FROM
                 t_unitvalue t
                     LEFT JOIN data_quality_status d ON t.enterprise_id = d.enterprise_code
                     LEFT JOIN t_standard s ON d.quality_industry = s.code
                     AND t.quota_name = s.quota_name
             WHERE
                <![CDATA[  t.record_circle >= #{statDate} ]]>
                <![CDATA[  AND t.guideline_val < (s.advanced_value * 0.8) ]]>
            )
    </select>

</mapper>