<?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.jljc.mapper.TReportRateprogressMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.nantian.cloud.jljc.entity.TReportRateprogress">
        <id column="id" property="id"/>
        <result column="report_id" property="reportId"/>
        <result column="remarks" property="remarks"/>
        <result column="target" property="target"/>
        <result column="unit_energy_progress" property="unitEnergyProgress"/>
        <result column="energy_progress" property="energyProgress"/>
        <result column="unit_energy_completion" property="unitEnergyCompletion"/>
        <result column="energy_completion" property="energyCompletion"/>
        <result column="unit_energy_progress_equal" property="unitEnergyProgressEqual"/>
        <result column="energy_progress_equal" property="energyProgressEqual"/>
        <result column="unit_energy_completion_equal" property="unitEnergyCompletionEqual"/>
        <result column="energy_completion_equal" property="energyCompletionEqual"/>
        <result column="userid" property="userid"/>
        <result column="update_time" property="updateTime"/>
        <result column="del_flag" property="delFlag"/>
        <result column="dept_id" property="deptId"/>
        <result column="f1" property="f1" />
        <result column="f2" property="f2" />
        <result column="f3" property="f3" />
        <result column="d1" property="d1" />
        <result column="d2" property="d2" />
        <result column="d3" property="d3" />
        <result column="t1" property="t1" />
        <result column="t2" property="t2" />
        <result column="t3" property="t3" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, report_id, remarks, target, unit_energy_progress, energy_progress, unit_energy_completion, energy_completion,
        unit_energy_progress_equal, energy_progress_equal, unit_energy_completion_equal, energy_completion_equal, userid,
        update_time, del_flag, dept_id, f1, f2, f3, d1, d2, d3, t1, t2, t3
    </sql>
    <insert id="saveOrUpdates" useGeneratedKeys="true" keyProperty="id">
        insert into t_report_rateprogress (report_id, remarks, target, unit_energy_progress, energy_progress, unit_energy_completion, energy_completion,
        unit_energy_progress_equal, energy_progress_equal, unit_energy_completion_equal, energy_completion_equal, userid,
        update_time, del_flag, dept_id) values (#{reportId},#{remarks},#{target},#{unitEnergyProgress},#{energyProgress},#{unitEnergyCompletion},#{energyCompletion},
        #{unitEnergyProgressEqual},#{energyProgressEqual},#{unitEnergyCompletionEqual},#{energyCompletionEqual},#{userid},#{updateTime},#{delFlag},#{deptId})
    </insert>
    <select id="getEnergyProgress" resultType="java.lang.Double">
        select case when ptce is null or ptce = 0 then 0 else IFNULL(round((ptce-tce)*yield,2),0) end energy
        from (
            select e.enterprise_code, (select IFNULL(industrial_yield,0) from t_report_financial where del_flag = '1' and report_id = t1.id ) yield,
            (select IFNULL(unit_energy_consumption,0) from t_report_financial where del_flag = '1' and report_id = t1.id ) tce,
            (select IFNULL(unit_energy_consumption,0) from t_report_financial where del_flag = '1' and report_id = t2.id ) ptce
            from enterprise_info e
            left join t_report t1 on e.enterprise_code = t1.enterprise_code and t1.del_flag = '1' and t1.year = #{year} and t1.type = '1'
            left join t_report t2 on e.enterprise_code = t2.enterprise_code and t2.del_flag = '1' and t2.year = #{pYear} and t2.type = '1'
            where e.enterprise_code = #{enterpriseCode}
        ) t
    </select>
    <select id="getUnitEnergyProgress" resultType="java.lang.Double">
        select case when pguide is null or pguide = 0 then 0 else round(IFNULL(SUM(product*coefficient*((pguide-guide)/ratio)),0),2) end energy
        from (
             select distinct e.enterprise_code, u1.quota_id, u1.quota_name, IFNULL(u1.guideline_val,0) guide, IFNULL(u1.parent_val,0) product, IFNULL(u1.ratio,1) ratio, IFNULL(u2.guideline_val,0) pguide,
             case when u1.child_unit = '千克标准煤' then 0.001 else 1 end coefficient
             from enterprise_info e
             left join t_report t1 on e.enterprise_code = t1.enterprise_code and t1.del_flag = '1' and t1.year = #{year} and t1.type = '3'
             left join t_report t2 on e.enterprise_code = t2.enterprise_code and t2.del_flag = '1' and t2.year = #{pYear} and t2.type = '3'
             left join t_report_unitenergy u1 on u1.del_flag = '1' and u1.report_id = t1.id
             left join t_report_unitenergy u2 on u2.del_flag = '1' and u2.report_id = t2.id and u2.quota_id = u1.quota_id
             where (
                 select case when nameStr like '%吨水泥综合能耗%' then '吨水泥综合能耗' when nameStr like '%吨水泥熟料综合能耗%' then '吨水泥熟料综合能耗' else nameStr end nameStr
                 from (
                     select t1.enterprise_code, GROUP_CONCAT(u1.quota_name) nameStr from t_report t1 left join t_report_unitenergy u1 on u1.del_flag = '1' and u1.report_id = t1.id
                     where t1.del_flag = '1' and t1.year = #{year} and t1.type = '3' and t1.enterprise_code = #{enterpriseCode} group by t1.enterprise_code
                 ) tmp
            ) like concat('%',u1.quota_name,'%') and e.enterprise_code = #{enterpriseCode}
        ) t
    </select>
</mapper>
