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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.nantian.cloud.jljc.entity.TDualcontrolIndex">
        <id column="id" property="id" />
        <id column="year" property="year" />
        <result column="index_name" property="indexName" />
        <result column="index_type" property="indexType" />
        <result column="item_code" property="itemCode" />
        <result column="item_name" property="itemName" />
        <result column="energy_total_basic" property="energyTotalBasic" />
        <result column="energy_total_inspire" property="energyTotalInspire" />
        <result column="energy_unit_gdp_basic" property="energyUnitGdpBasic" />
        <result column="energy_unit_gdp_inspire" property="energyUnitGdpInspire" />
        <result column="energy_unit_gdp_reducerate_basic" property="energyUnitGdpReducerateBasic" />
        <result column="energy_unit_gdp_reducerate_inspire" property="energyUnitGdpReducerateInspire" />
        <result column="userid" property="userid" />
        <result column="update_time" property="updateTime" />
        <result column="del_flag" property="delFlag" />
        <result column="dept_id" property="deptId" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, year, index_name, index_type, item_code, item_name, energy_total_basic, energy_total_inspire, energy_unit_gdp_basic, energy_unit_gdp_inspire, energy_unit_gdp_reducerate_basic, energy_unit_gdp_reducerate_inspire, userid, update_time, del_flag, dept_id
    </sql>
    <select id="getIndexTree" resultType="com.nantian.cloud.jljc.vo.dualControl.DualcontrolIndexTreeVo">
        select distinct d.value, d.pvalue, d.label, i.year, i.item_code itemCode, i.item_name itemName, i.energy_total_basic energyTotalBasic, i.energy_total_inspire energyTotalInspire, i.energy_unit_gdp_basic energyUnitGdpBasic, i.energy_unit_gdp_inspire energyUnitGdpInspire, i.energy_unit_gdp_reducerate_basic energyUnitGdpReducerateBasic, i.energy_unit_gdp_reducerate_inspire energyUnitGdpReducerateInspire
        from (
            select id as value, case when #{deptId} = id then '' else  pid end pvalue, meaning as label
            from dict_region  where del_flag = "1" and version = 0
        ) d left join t_dualcontrol_index i on d.value = i.item_code and i.index_type = 2 and i.`year` = #{indexYear}
        where d.`value` like concat(#{regionId},"%")
    </select>
    <select id="getIndexByVersion" resultType="com.nantian.cloud.jljc.entity.TDualcontrolIndex">
        select #{year} year, "2" index_type, p.item_code, p.item_name,
        <choose>
            <when test=" num== '0'.toString() ">
                i1.amount_energy_total energy_total_basic, i1.amount_energy_unit_gdp energy_unit_gdp_basic, i1.check_calculation energy_unit_gdp_reducerate_basic,
                i2.amount_energy_total energy_total_inspire, i2.amount_energy_unit_gdp energy_unit_gdp_inspire, i2.check_calculation energy_unit_gdp_reducerate_inspire
            </when>
            <otherwise>
                i1.energy_total${num} energy_total_basic, i1.energy_unit_gdp${num} energy_unit_gdp_basic, i1.energy_unit_gdp_reducerate${num} energy_unit_gdp_reducerate_basic,
                i2.energy_total${num} energy_total_inspire, i2.energy_unit_gdp${num} energy_unit_gdp_inspire, i2.energy_unit_gdp_reducerate${num} energy_unit_gdp_reducerate_inspire
            </otherwise>
        </choose>
        from t_dualcontrol_param p
        left join t_dualcontrol_indexresolve i1 on i1.version_id = p.version_id and i1.item_code = p.item_code and i1.index_type = '1'
        left join t_dualcontrol_indexresolve i2 on i2.version_id = p.version_id and i2.item_code = p.item_code and i2.index_type = '2'
        where 1=1 and p.version_id = #{versionId} and p.type = '3'
        <choose>
            <when test=' deptId.contains("0000") '>
                and p.dept_id = #{deptId} and p.item_code like '%00'
            </when>
            <otherwise>
                and p.dept_id = #{deptId} and p.item_code != #{deptId}
            </otherwise>
        </choose>
    </select>
    <select id="getIndex" resultType="com.nantian.cloud.jljc.entity.TDualcontrolIndex">
        select <include refid="Base_Column_List"></include>
        from t_dualcontrol_index
        where year = #{year} and item_code = #{itemCode} and index_type = '2' and del_flag = '1'
    </select>

</mapper>
