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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.nantian.cloud.jljc.entity.EnergyIndex">
        <id column="id" property="id"/>
        <result column="num" property="num"/>
        <result column="index_name" property="indexName"/>
        <result column="index_unit" property="indexUnit"/>
        <result column="child_unit" property="childUnit"/>
        <result column="mum_unit" property="mumUnit"/>
        <result column="change_modulus" property="changeModulus"/>
        <result column="trade_product_code" property="tradeProductCode"/>
        <result column="trade_product_name" property="tradeProductName"/>
        <result column="product_info_num" property="productInfoNum"/>
        <result column="product_info_name" property="productInfoName"/>
        <result column="local_limit" property="localLimit"/>
        <result column="local_accept" property="localAccept"/>
        <result column="local_good" property="localGood"/>
        <result column="gov_limit" property="govLimit"/>
        <result column="gov_accept" property="govAccept"/>
        <result column="gov_good" property="govGood"/>
        <result column="t_name" property="tName"/>
        <result column="userid" property="userid"/>
        <result column="update_time" property="updateTime"/>
        <result column="del_flag" property="delFlag"/>
        <result column="dept_id" property="deptId"/>
        <association property="tradeProducts" javaType="java.util.List" resultMap="tradeProductsing">
            <id column="id" property="id"/>
            <result column="code" property="code"/>
            <result column="name" property="name"/>
        </association>
    </resultMap>

    <resultMap id="tradeProductsing" type="com.nantian.cloud.jljc.entity.TradeProduct">
        <id column="id" property="id"/>
        <result column="code" property="code"/>
        <result column="name" property="name"/>
    </resultMap>

    <resultMap id="EnergyIndexVo" type="com.nantian.cloud.jljc.vo.EnergyIndexVo">
        <id column="num" property="num"/>
        <result column="index_name" property="indexName"/>
        <result column="index_unit" property="indexUnit"/>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        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="getInfoByStr" resultType="com.nantian.cloud.jljc.entity.EnergyIndex">
        select
        <include refid="Base_Column_List"></include>
        from energy_index
        where id in
        <foreach collection="list" index="index" item="i" separator="," open="(" close=")">
            #{i}
        </foreach>
    </select>

    <select id="getByProductNum" resultType="com.nantian.cloud.jljc.vo.EnergyIndexVo">
        select num,index_name, index_unit
        from energy_index
        where product_info_num = #{productNum}
    </select>

    <select id="selectall" resultType="com.nantian.cloud.jljc.entity.EnergyIndex">
        select a.id, a.num, a.index_name, a.index_unit, a.child_unit, a.mum_unit, a.change_modulus,
        a.trade_product_code, a.product_info_num, a.local_limit, a.local_accept, a.local_good, a.gov_limit,
        a.gov_accept, a.gov_good,b.name as t_name
        from energy_index a,trade_product b
        <trim prefix="where" prefixOverrides="and">
            <if test="param2.tName !=null and param2.tName !=''">
                and b.`name` like concat ('%',#{param2.tName},'%')
            </if>
            <if test="param2.indexName !=null and param2.indexName !=''">
                and a.index_name like concat ('%',#{param2.indexName},'%')
            </if>
            <if test="param2.tradeProductCode !=null and param2.tradeProductCode !=''">
                and a.trade_product_code like concat ('%',#{param2.tradeProductCode},'%')
            </if>
            <if test="param2.num !=null and param2.num !=''">
                and a.num like concat ('%',#{param2.num},'%')
            </if>
            and a.trade_product_code =b.`code` and a.del_flag = "1"
        </trim>
    </select>

    <select id="getPageScopes" resultType="com.nantian.cloud.jljc.entity.EnergyIndex">
        select e.id, e.num, e.index_name, e.index_unit, e.child_unit, e.mum_unit, e.change_modulus,
        e.trade_product_code, k.name trade_product_name, e.product_info_num, p.product_name product_info_name,
        e.local_limit, e.local_accept, e.local_good, e.gov_limit, e.gov_accept, e.gov_good, e.userid, e.update_time, e.del_flag, e.dept_id
        from energy_index e
        left join trade_kind k on e.trade_product_code = k.code
        left join product_info p on e.product_info_num = p.product_num
        <trim prefix="where" prefixOverrides="and">
            <if test="query.indexName != null  and query.indexName != ''">
                and e.index_name like concat("%",#{query.indexName},"%")
            </if>
            <if test="query.tradeProductCode != null  and query.tradeProductCode != ''">
                and e.trade_product_code like concat("%",#{query.tradeProductCode},"%")
            </if>
            <if test="query.tradeProductName != null and query.tradeProductName != ''">
                and k.name like concat("%",#{query.tradeProductName},"%")
            </if>
            <if test="query.productInfoNum != null  and query.productInfoNum != ''">
                and e.product_info_num like concat("%",#{query.productInfoNum},"%")
            </if>
            <if test="query.productInfoName != null  and query.productInfoName != ''">
                and p.product_name like concat("%",#{query.productInfoName},"%")
            </if>
            <if test="query.delFlag != null  and query.delFlag != ''">
                and e.del_flag = #{query.delFlag}
            </if>
        </trim>
        order by e.local_limit desc
    </select>

    <select id="getPageScope" resultType="com.nantian.cloud.jljc.entity.EnergyIndex">
        select  COUNT(*) as zbnum,e.product_info_num,k.name trade_product_name, p.product_name product_info_name,e.trade_product_code
        from energy_index e
        left join trade_kind k on e.trade_product_code = k.code
        left join product_info p on e.product_info_num = p.product_num
        <trim prefix="where" prefixOverrides="and">
            <if test="query.indexName != null  and query.indexName != ''">
                and e.index_name like concat("%",#{query.indexName},"%")
            </if>
            <if test="query.tradeProductCode != null  and query.tradeProductCode != ''">
                and e.trade_product_code like concat("%",#{query.tradeProductCode},"%")
            </if>
            <if test="query.tradeProductName != null and query.tradeProductName != ''">
                and k.name like concat("%",#{query.tradeProductName},"%")
            </if>
            <if test="query.productInfoNum != null  and query.productInfoNum != ''">
                and e.product_info_num like concat("%",#{query.productInfoNum},"%")
            </if>
            <if test="query.productInfoName != null  and query.productInfoName != ''">
                and p.product_name like concat("%",#{query.productInfoName},"%")
            </if>
            <if test="query.delFlag != null  and query.delFlag != ''">
                and e.del_flag = #{query.delFlag}
            </if>
        </trim>
        GROUP BY e.product_info_num
    </select>

    <select id="getPageScopeCode" resultType="com.nantian.cloud.jljc.entity.EnergyIndex">
        select e.id, e.num, e.index_name, e.index_unit, e.child_unit, e.mum_unit, e.change_modulus,
        e.trade_product_code, k.name trade_product_name, e.product_info_num, p.product_name product_info_name,
        e.local_limit, e.local_accept, e.local_good, e.gov_limit, e.gov_accept, e.gov_good, e.userid, e.update_time, e.del_flag, e.dept_id
        from energy_index e
        left join trade_kind k on e.trade_product_code = k.code
        left join product_info p on e.product_info_num = p.product_num
        <trim prefix="where" prefixOverrides="and">
            <if test="query.indexName != null  and query.indexName != ''">
                and e.index_name like concat("%",#{query.indexName},"%")
            </if>
            <if test="query.tradeProductCode != null  and query.tradeProductCode != ''">
                and e.trade_product_code like concat("%",#{query.tradeProductCode},"%")
            </if>
            <if test="query.tradeProductName != null and query.tradeProductName != ''">
                and k.name like concat("%",#{query.tradeProductName},"%")
            </if>
            <if test="query.productInfoNum != null  and query.productInfoNum != ''">
                and e.product_info_num like concat("%",#{query.productInfoNum},"%")
            </if>
            <if test="query.productInfoName != null  and query.productInfoName != ''">
                and p.product_name like concat("%",#{query.productInfoName},"%")
            </if>
            <if test="query.delFlag != null  and query.delFlag != ''">
                and e.del_flag = #{query.delFlag}
            </if>
        </trim>
        order by e.local_limit desc
    </select>

    <select id="getTrade" resultType="java.lang.String">
        select distinct k.name trade_product_name
        from energy_index e
        left join trade_kind k on e.trade_product_code = k.code
    </select>

    <select id="getInfoByEnergyId" resultType="com.nantian.cloud.jljc.entity.EnergyIndex">
		select id, index_name, index_unit, case when gov_good = '—' or gov_good = '-' or gov_good = '' or gov_good is null  then '-' else gov_good end gov_good,
		case when gov_limit = '—' or gov_limit = '-' or gov_limit = '' or gov_limit is null  then '-' else gov_limit end gov_limit,
		case when gov_accept = '—' or gov_accept = '-' or gov_accept = '' or gov_accept is null  then '-' else gov_accept end gov_accept,
		case when local_good = '—' or local_good = '-' or local_good = '' or local_good is null  then '-' else local_good end local_good,
		case when local_limit = '—' or local_limit = '-' or local_limit = '' or local_limit is null  then '-' else local_limit end local_limit,
		case when local_accept = '—' or local_accept = '-' or local_accept = '' or local_accept is null  then '-' else local_accept end local_accept
		from energy_index where id = #{energyId}
    </select>
    <select id="getEnergyIndexList" resultType="com.nantian.cloud.jljc.entity.EnergyIndex">
        SELECT DISTINCT id  ,index_name FROM energy_index WHERE del_flag="1"
    </select>
</mapper>
