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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.nantian.cloud.jljc.entity.TReportUnitenergy">
        <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="child_unit" property="childUnit"/>
        <result column="parent_unit" property="parentUnit"/>
        <result column="ratio" property="ratio"/>
        <result column="code" property="code"/>
        <result column="guideline_val" property="guidelineVal"/>
        <result column="child_val" property="childVal"/>
        <result column="parent_val" property="parentVal"/>
        <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="prevChildVal" property="prevChildVal"/>
        <result column="prevParentVal" property="prevParentVal"/>
        <result column="prevGuidelineVal" property="prevGuidelineVal"/>
    </resultMap>


    <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>


    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, report_id, quota_id, quota_name, quota_unit, child_unit, parent_unit, ratio, code, guideline_val, child_val,
        parent_val, quota_limit, remarks, userid, update_time, del_flag, dept_id
    </sql>

    <select id="getUnitenergyByReportId" resultType="com.nantian.cloud.jljc.entity.TReportUnitenergy">
        select <include refid="Base_Column_List"></include>
        from t_report_unitenergy
        where report_id = #{reportId} and del_flag = "1"
    </select>

    <select id="initialize" resultType="com.nantian.cloud.jljc.entity.TReportUnitenergy">
        select ee.id as quotaId, ee.index_name as quotaName, ee.index_unit as quotaUnit, ee.child_unit, ee.mum_unit as parentUnit,
        ee.change_modulus as ratio, ee.num as code, IFNULL(ee.local_limit,ee.gov_limit) as quotaLimit, p.guideline_val as prevGuidelineVal, p.child_val as prevChildVal,
        p.parent_val as prevParentVal  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 p on p.quota_id = ee.id and p.report_id = #{reportId} and p.del_flag = "1"
    </select>

    <select id="getByIdAndPrevId" resultType="com.nantian.cloud.jljc.entity.TReportUnitenergy">
        select  p.id, p.report_id, p.quota_name, p.quota_unit, p.child_unit, p.parent_unit, p.ratio, p.code, p.guideline_val,
                p.child_val, p.parent_val, p.quota_limit, p.remarks, p.userid, p.update_time, p.del_flag, p.dept_id
                ,pp.guideline_val as prevGuidelineVal, pp.child_val as prevChildVal, pp.parent_val as prevParentVal
        from (
            select <include refid="Base_Column_List"></include>
            from t_report_unitenergy where report_id = #{id}
        ) p
        left join (
            select <include refid="Base_Column_List"></include>
            from t_report_unitenergy where report_id = #{prevId}
        ) pp
        on p.quota_id = pp.quota_id where p.del_flag = "1"
    </select>

    <select id="getQuotaAverage" resultType="java.lang.String">
        select AVG(guideline_val) from t_report_unitenergy
        where report_id in (
            select id from t_report where year = #{year} and type = #{type} and report_status = '3'
        ) and quota_id = #{quotaId} and guideline_val != 0 and guideline_val is not null
    </select>

</mapper>
