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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.nantian.cloud.jljc.entity.TDualcontrolParam">
        <id column="id" property="id" />
        <id column="version_id" property="versionId" />
        <result column="year" property="year" />
        <result column="type" property="type" />
        <result column="item_code" property="itemCode" />
        <result column="item_name" property="itemName" />
        <result column="remark" property="remark" />
        <result column="lower_limit" property="lowerLimit" />
        <result column="upper_limit" property="upperLimit" />
        <result column="add_userid" property="addUserid"/>
        <result column="add_user_name" property="addUserName"/>
        <result column="add_time" property="addTime"/>
        <result column="update_userid" property="updateUserid"/>
        <result column="update_user_name" property="updateUserName"/>
        <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, version_id, year, type, item_code, item_name, remark, lower_limit, upper_limit, add_userid, add_user_name, add_time, update_userid, update_user_name, update_time, del_flag, dept_id
        ,pre_gdp_total, pre_energy_total, pre_energy_unit_gdp, gdp_total_proportion, energy_total_proportion, comprehensive_weight, average_growthrate
    </sql>
    <select id="getGearByComprehensiveWeight" resultType="com.nantian.cloud.jljc.entity.TDualcontrolParam">
        select <include refid="Base_Column_List"></include>
        from `t_dualcontrol_param` where #{weight} between lower_limit and upper_limit and year = #{year} and version_id = #{versionId} and dept_id = #{deptId}
    </select>

    <select id="getPreData" resultType="com.nantian.cloud.jljc.entity.TDualcontrolParam">
        select  id, version_id, year, type, item_code, item_name, '3' type, pre_gdp_total, pre_energy_total, pre_energy_unit_gdp,
                gdp_total_proportion, energy_total_proportion, comprehensive_weight, average_growthrate
        from `t_dualcontrol_param`
        where 1=1 and type = '3' and year = #{year} and version_id = #{versionId}
        <choose>
            <when test=' deptId.contains("0000") '>
                and right(item_code,4) != '0000' and right(item_code,2) = '00'
            </when>
            <otherwise>
                and right(item_code,2) != '00' and left(item_code,4) = left(#{deptId},4)
            </otherwise>
        </choose>
        union
        select  id, version_id, year, type, item_code, item_name, '3' type, pre_gdp_total, pre_energy_total, pre_energy_unit_gdp,
                gdp_total_proportion, energy_total_proportion, comprehensive_weight, average_growthrate
        from `t_dualcontrol_param`
        where 1=1 and type = '3' and year = #{year} and version_id = #{versionId} and item_code = #{deptId}
        order by item_code asc
    </select>

    <select id="getInitializePreData" resultType="com.nantian.cloud.jljc.entity.TDualcontrolParam">
        select t.itemCode, t.itemName, t.preGdpTotal, t.preEnergyTotal, "3" type
        from (
            select d.id itemCode, meaning2 itemName, gdp_total preGdpTotal, energy_total preEnergyTotal
            from dict_region d
            left join t_dualcontrol_progress p on d.id = p.item_code and p.year = #{year} and quarter = 0
            where d.pid = #{deptId} and d.version = 0
            union
            select d.id itemCode, meaning2 itemName, gdp_total preGdpTotal, energy_total preEnergyTotal
            from dict_region d
            left join t_dualcontrol_progress p on d.id = p.item_code and p.year = #{year} and quarter = 0
            where d.id = #{deptId} and d.version = 0
        ) t order by t.itemCode asc
    </select>
</mapper>
