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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.nantian.cloud.jljc.entity.TDualcontrolIndexresolve">
        <id column="id" property="id" />
        <result column="year" property="year" />
        <result column="object" property="object" />
        <result column="version_id" property="versionId" />
        <result column="index_type" property="indexType" />
        <result column="index_status" property="indexStatus" />
        <result column="gear_code" property="gearCode" />
        <result column="gear_upper_limit" property="gearUpperLimit" />
        <result column="gear_lower_limit" property="gearLowerLimit" />
        <result column="item_code" property="itemCode" />
        <result column="item_name" property="itemName" />
        <result column="pre_gdp_total" property="preGdpTotal" />
        <result column="pre_energy_total" property="preEnergyTotal" />
        <result column="pre_energy_unit_gdp" property="preEnergyUnitGdp" />
        <result column="gdp_total_proportion" property="gdpTotalProportion" />
        <result column="energy_total_proportion" property="energyTotalProportion" />
        <result column="comprehensive_weight" property="comprehensiveWeight" />
        <result column="average_growthrate" property="averageGrowthrate" />
        <result column="energy_unit_gdp_reducerate1" property="energyUnitGdpReducerate1" />
        <result column="energy_growthrate1" property="energyGrowthrate1" />
        <result column="gdp_total1" property="gdpTotal1" />
        <result column="energy_total1" property="energyTotal1" />
        <result column="energy_unit_gdp1" property="energyUnitGdp1" />
        <result column="energy_unit_gdp_reducerate2" property="energyUnitGdpReducerate2" />
        <result column="energy_growthrate2" property="energyGrowthrate2" />
        <result column="gdp_total2" property="gdpTotal2" />
        <result column="energy_total2" property="energyTotal2" />
        <result column="energy_unit_gdp2" property="energyUnitGdp2" />
        <result column="energy_unit_gdp_reducerate3" property="energyUnitGdpReducerate3" />
        <result column="energy_growthrate3" property="energyGrowthrate3" />
        <result column="gdp_total3" property="gdpTotal3" />
        <result column="energy_total3" property="energyTotal3" />
        <result column="energy_unit_gdp3" property="energyUnitGdp3" />
        <result column="energy_unit_gdp_reducerate4" property="energyUnitGdpReducerate4" />
        <result column="energy_growthrate4" property="energyGrowthrate4" />
        <result column="gdp_total4" property="gdpTotal4" />
        <result column="energy_total4" property="energyTotal4" />
        <result column="energy_unit_gdp4" property="energyUnitGdp4" />
        <result column="energy_unit_gdp_reducerate5" property="energyUnitGdpReducerate5" />
        <result column="energy_growthrate5" property="energyGrowthrate5" />
        <result column="gdp_total5" property="gdpTotal5" />
        <result column="energy_total5" property="energyTotal5" />
        <result column="energy_unit_gdp5" property="energyUnitGdp5" />
        <result column="amount_gdp_total" property="amountGdpTotal" />
        <result column="amount_energy_total" property="amountEnergyTotal" />
        <result column="amount_energy_unit_gdp" property="amountEnergyUnitGdp" />
        <result column="amount_energy_unit_gdp_reducerate" property="amountEnergyUnitGdpReducerate" />
        <result column="check_calculation" property="checkCalculation" />
        <result column="dept_id" property="deptId" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, year, object, version_id, index_type, index_status, gear_code, item_code, item_name, average_growthrate, energy_unit_gdp_reducerate1, energy_growthrate1, gdp_total1, energy_total1, energy_unit_gdp1, energy_unit_gdp_reducerate2, energy_growthrate2, gdp_total2, energy_total2, energy_unit_gdp2,
        energy_unit_gdp_reducerate3, energy_growthrate3, gdp_total3, energy_total3, energy_unit_gdp3, energy_unit_gdp_reducerate4, energy_growthrate4, gdp_total4, energy_total4, energy_unit_gdp4, energy_unit_gdp_reducerate5, energy_growthrate5, gdp_total5, energy_total5, energy_unit_gdp5,
        amount_gdp_total, amount_energy_total, amount_energy_unit_gdp, amount_energy_unit_gdp_reducerate, check_calculation, dept_id
    </sql>
    <select id="getIndexDataSource" resultType="com.nantian.cloud.jljc.vo.dualControl.DualcontrolIndexDataSourceVo">
        select i.id, i.year, i.version_id versionId, i.index_type indexType, i.item_code itemCode, i.item_name itemName, g. gear_code, g.comprehensive_weight
            , i.energy_unit_gdp_reducerate${num} energyUnitGdpReducerate1, i.energy_growthrate${num} energyGrowthrate1, i.gdp_total${num} gdpTotal1, i.energy_total${num} energyTotal1, i.energy_unit_gdp${num} energyUnitGdp1
            , p.energy_unit_gdp_reducerate energyUnitGdpReducerate2, p.energy_growthrate energyGrowthrate2, p.gdp_total gdpTotal2, p.energy_total energyTotal2, p.energy_unit_gdp energyUnitGdp2
        from t_dualcontrol_indexresolve i
        left join(
            select p1.comprehensive_weight, p1.item_code, case when p1.item_code = #{deptId} then '0' else (
                select p2.id from t_dualcontrol_param p2 where p2.dept_id = p1.dept_id and p2.year = p1.year and p2.version_id = #{tmpVersionId}
                and p2.type = '1' and p1.comprehensive_weight between p2.lower_limit and p2.upper_limit limit 0,1
            ) end gear_code
            from t_dualcontrol_param p1
            where p1.dept_id = #{deptId} and p1.year = #{planYear} and p1.version_id = #{versionId} and p1.type = '3'
        )g on i.item_code = g.item_code
        left join t_dualcontrol_progress p on i.item_code = p.item_code and p.year = #{preYear} and p.quarter = '0'
        where i.dept_id = #{deptId} and i.year = #{planYear} and i.version_id = #{versionId} and i.index_type = #{type} and i.item_code is not null
        order by g.comprehensive_weight desc
    </select>
    <select id="getIndexReducerate" resultType="com.nantian.cloud.jljc.entity.TDualcontrolIndexresolve">
        select i.id, i.year, i.version_id, i.index_type, i.item_code, i.item_name, i.average_growthrate, i.energy_unit_gdp_reducerate1, i.energy_unit_gdp_reducerate2, i.energy_unit_gdp_reducerate3, i.energy_unit_gdp_reducerate4, i.energy_unit_gdp_reducerate5,
            g.gear_code, g.comprehensive_weight, g.pre_gdp_total, g.pre_energy_total, g.pre_energy_unit_gdp, g.gdp_total_proportion, g.energy_total_proportion
        from t_dualcontrol_indexresolve i
        left join(
            select p1.item_code, p1.comprehensive_weight, p1.pre_gdp_total, p1.pre_energy_total, p1.pre_energy_unit_gdp, p1.gdp_total_proportion, p1.energy_total_proportion,
            case when p1.item_code = #{deptId} then '0' else (
                select p2.id from t_dualcontrol_param p2 where p2.dept_id = p1.dept_id and p2.year = p1.year and p2.version_id = #{tmpVersionId}
                and p2.type = '1' and p1.comprehensive_weight between p2.lower_limit and p2.upper_limit limit 0,1
            ) end gear_code
            from t_dualcontrol_param p1
            where p1.dept_id = #{deptId} and p1.year = #{planYear} and p1.version_id = #{versionId} and p1.type = '3'
        )g on i.item_code = g.item_code
        where i.dept_id = #{deptId} and i.year = #{planYear} and i.version_id = #{versionId} and i.index_type = #{type}
        order by g.comprehensive_weight desc
    </select>
    <select id="initializeDataSource" resultType="com.nantian.cloud.jljc.vo.dualControl.DualcontrolIndexDataSourceVo">
        select p1.item_code itemCode, p1.comprehensive_weight comprehensiveWeight, p1.item_name itemName, case when p1.item_code = #{deptId} then '0' else (
            select p2.id from t_dualcontrol_param p2 where p2.dept_id = p1.dept_id and p2.year = p1.year and p2.version_id = #{tmpVersionId}
            and p2.type = '1' and p1.comprehensive_weight between p2.lower_limit and p2.upper_limit limit 0,1
        ) end gearCode, p.energy_unit_gdp_reducerate energyUnitGdpReducerate2, p.energy_growthrate energyGrowthrate2, p.gdp_total gdpTotal2, p.energy_total energyTotal2, p.energy_unit_gdp energyUnitGdp2
        from t_dualcontrol_param p1
        left join t_dualcontrol_progress p on p1.item_code = p.item_code and p.year = #{preYear} and p.quarter = '0'
        where p1.dept_id = #{deptId} and p1.year = #{planYear} and p1.version_id = #{versionId} and p1.type = '3' and p1.item_code is not null
        order by p1.comprehensive_weight desc
    </select>
    <select id="initializeIndexReducerate" resultType="com.nantian.cloud.jljc.entity.TDualcontrolIndexresolve">
        select p1.item_code itemCode, p1.comprehensive_weight comprehensiveWeight, p1.item_name itemName, p1.pre_gdp_total preGdpTotal, p1.pre_energy_total preEnergyTotal, p1.pre_energy_unit_gdp preEnergyUnitGdp, p1.gdp_total_proportion gdpTotalProportion, p1.energy_total_proportion energyTotalProportion, p1.comprehensive_weight comprehensiveWeight,
        case when p1.item_code = #{deptId} then '0' else (
            select p2.id from t_dualcontrol_param p2 where p2.dept_id = p1.dept_id and p2.year = p1.year and p2.version_id = #{tmpVersionId}
            and p2.type = '1' and p1.comprehensive_weight between p2.lower_limit and p2.upper_limit limit 0,1
        ) end gearCode
        from t_dualcontrol_param p1
        where p1.dept_id = #{deptId} and p1.year = #{planYear} and p1.version_id = #{versionId} and p1.type = '3'
        order by p1.comprehensive_weight desc
    </select>
    <select id="getIndexResult" resultType="com.nantian.cloud.jljc.entity.TDualcontrolIndexresolve">
        select t.id, t.year, t.object, t.version_id, t.index_type, t.index_status, t.item_code, t.item_name, t.average_growthrate, t.energy_unit_gdp_reducerate1, t.energy_growthrate1, t.gdp_total1, t.energy_total1, t.energy_unit_gdp1, t.energy_unit_gdp_reducerate2, t.energy_growthrate2, t.gdp_total2, t.energy_total2, t.energy_unit_gdp2,
        energy_unit_gdp_reducerate3, t.energy_growthrate3, t.gdp_total3, t.energy_total3, t.energy_unit_gdp3, t.energy_unit_gdp_reducerate4, t.energy_growthrate4, t.gdp_total4, t.energy_total4, t.energy_unit_gdp4, t.energy_unit_gdp_reducerate5, t.energy_growthrate5, t.gdp_total5, t.energy_total5, t.energy_unit_gdp5,
        amount_gdp_total, t.amount_energy_total, t.amount_energy_unit_gdp, t.amount_energy_unit_gdp_reducerate, t.check_calculation, t.dept_id,
        p1.pre_gdp_total, p1.pre_energy_total, p1.pre_energy_unit_gdp, p1.gdp_total_proportion, p1.energy_total_proportion, p1.comprehensive_weight, p1.average_growthrate,
        case when p1.item_code = #{deptId} then '0' else (
            select p2.id from t_dualcontrol_param p2 where p2.dept_id = p1.dept_id and p2.year = p1.year and p2.version_id = #{tmpVersionId}
            and p2.type = '1' and p1.comprehensive_weight between p2.lower_limit and p2.upper_limit limit 0,1
        ) end gearCode
        from t_dualcontrol_indexresolve t
        left join t_dualcontrol_param p1 on p1.item_code = t.item_code and p1.year = t.year and p1.version_id = t.version_id and p1.dept_id = #{deptId} and p1.type = '3'
        where t.year = #{planYear} and t.version_id = #{versionId} and t.index_type = #{type}
    </select>

</mapper>
