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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.nantian.cloud.jljc.entity.TReportUnitenergyComp">
        <id column="id" property="id" />
        <result column="report_id" property="reportId" />
        <result column="quota_id" property="quotaId" />
        <result column="quota_name" property="quotaName" />
        <result column="quota_unit" property="quotaUnit" />
        <result column="quota_value" property="quotaValue" />
        <result column="quota_limit" property="quotaLimit" />
        <result column="remarks" property="remarks" />
        <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="prevQuotaValue" property="prevQuotaValue" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, report_id, quota_id, quota_name, quota_unit, quota_value, quota_limit, remarks, userid, update_time, del_flag, dept_id
    </sql>

    <sql id="energy_index_sql">
        id, num, index_name, index_unit, child_unit, mum_unit, change_modulus, trade_product_code, trade_product_name, product_info_num, product_info_name,
        local_limit, local_accept, local_good, gov_limit, gov_accept, gov_good, userid, update_time, del_flag, dept_id
    </sql>

    <select id="initialize" resultType="com.nantian.cloud.jljc.entity.TReportUnitenergyComp">
        select ee.id as quotaId,ee.index_name as quotaName, ee.index_unit as quotaUnit, p.quota_value as prevQuotaValue, IFNULL(ee.local_limit,ee.gov_limit) as quotaLimit
        from
        (
            select <include refid="energy_index_sql"></include>
            from energy_index e where e.id in
            <foreach collection="ids" item="id" open="(" close=")" separator=",">
                #{id}
            </foreach>
        )ee
        left join t_report_unitenergy_comp p on p.quota_id = ee.id and p.report_id = #{reportId} and p.del_flag = "1"
    </select>

    <select id="getUnitenergyCompByReportId" resultType="com.nantian.cloud.jljc.entity.TReportUnitenergyComp">
        select  p.id, p.report_id, p.quota_name, p.quota_unit, p.quota_value, p.quota_limit, p.remarks, p.userid, p.update_time,
         p.del_flag, p.dept_id ,pp.quota_value as prevQuotaValue
        from (
            select <include refid="Base_Column_List"></include>
            from t_report_unitenergy_comp where report_id = #{reportId}
        ) p
        left join (
            select <include refid="Base_Column_List"></include>
            from t_report_unitenergy_comp where report_id = #{prevReportId}
        ) pp
        on p.quota_id = pp.quota_id where p.del_flag = "1"
    </select>

</mapper>
