<?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.DischargeEnergyStockMapper">
    <sql id="tableName">
        t_discharge_energy_stock
    </sql>

    <sql id="baseColumn">
        id,industry_type_name,industry_type_code,industry_subclass_name,industry_subclass_code,item_name,item_location,enterprise_name,enterprise_code,production_value,energy_consumption,energy_consumption_unit,energy_standard,remark,userid,update_time,del_flag,dept_id,state
    </sql>
    <sql id="baseColumn1">
        industry_type_name,industry_type_code,industry_subclass_name,industry_subclass_code,item_name,item_location,
        enterprise_name,enterprise_code,energy_consumption,energy_consumption_unit,energy_standard,remark,userid,del_flag,dept_id,state,
        year,quarter,replacement,item_description,item_invest,energy_design_dl,energy_design_dj,
        energy_coal,energy_current,revenue,production_new,start_time,production_time,industry_class_name,enterprise_state,energy_raw,energy_raw_value
    </sql>

    <insert id="addStock" parameterType="com.nantian.cloud.jljc.entity.DischargeEnergyStock" useGeneratedKeys="true" keyProperty="id">
        insert into t_discharge_energy_stock(<include refid="baseColumn1"/>)
        values(#{query.industryTypeName},#{query.industryTypeCode},#{query.industrySubclassName},#{query.industrySubclassCode},#{query.itemName},#{query.itemLocation},
        #{query.enterpriseName},#{query.enterpriseCode},#{query.energyConsumption},#{query.energyConsumptionUnit},#{query.energyStandard},#{query.remark},#{query.userid},#{query.delFlag},#{query.deptId},#{query.state},
        #{query.year},#{query.quarter},#{query.replacement},#{query.item_description},#{query.item_invest},#{query.energy_design_dl},#{query.energy_design_dj},
        #{query.energy_coal},#{query.energy_current},#{query.revenue},#{query.production_new},#{query.start_time},#{query.production_time},#{query.industry_class_name},
        #{query.enterprise_state},#{query.energy_raw},#{query.energy_raw_value})
    </insert>

    <select id="getPageData" resultType="com.nantian.cloud.jljc.vo.dischargeEnergy.DischargeEnergyStockVo">
        select *
        from t_discharge_energy_stock
        <trim prefix="where" prefixOverrides="and">
            <if test="query.industryTypeCode != null">
                and industry_type_code = #{query.industryTypeCode}
            </if>
            <if test="query.industrySubclassCode != null">
                and industry_subclass_code = #{query.industrySubclassCode}
            </if>
            <if test="query.itemName != null">
                and item_name = #{query.itemName}
            </if>
            <if test="query.delFlag != null">
                and del_flag = #{query.delFlag}
            </if>
            <if test="query.itemLocation != null">
                and item_location = #{query.itemLocation}
            </if>
            <if test="query.enterpriseName != null">
                and enterprise_name = #{query.enterpriseName}
            </if>
            <if test="query.enterpriseCode != null">
                and enterprise_code = #{query.enterpriseCode}
            </if>
            <if test="query.year != null">
                and year = #{query.year}
            </if>
            <if test="query.quarter != null">
                and quarter = #{query.quarter}
            </if>
            <if test="query.id != null">
                and id = #{query.id}
            </if>
            <if test="query.state != null">
                and state = #{query.state}
            </if>
        </trim>
    </select>
    <select id="getCityData" resultType="com.nantian.cloud.jljc.vo.dischargeEnergy.DischargeEnergyStockVo">
        -- select d.meaning2, left(t.item_location,4), sum(t.energy_raw_value)
        -- from t_discharge_energy_stock t
        -- left join dict_region d on concat(left(t.item_location,4),'00') = d.id
        -- group by left(t.item_location,4)
        select d.meaning2, t2.rawSum,t2.tceDl,t2.tceDj,t2.tcetowDl,t2.tcetowDj,
        t1.id,t1.industry_class_name, t1.industry_type_code,t1.industry_subclass_code,t1.item_location,t1.enterprise_name,t1.enterprise_code,t1.remark,t1.dept_id,t1.state,t1.year,t1.quarter,t1.item_invest,t1.enterprise_state,t1.energy_raw,t1.energy_raw_value,t1.energy_consumption_last_dl,t1.energy_consumption_last_dj,t1.energy_consumption_last_two_dl,t1.energy_consumption_last_two_dj,
        c.product_id,c.index_name,c.index_unit,c.product_energy,
        b.id pid,b.product_name,b.product_num,b.product_output1,b.product_output2,b.product_output3,b.product_output4,b.product_output5,b.energy_office,b.energy_number,b.energy_time,b.production_value,b.capacity_design
        from t_discharge_energy_stock t1
        left join dict_region d on concat(left(t1.item_location,4),'00') = d.id
        LEFT JOIN t_discharge_energy_product b ON t1.id=b.item_id and b.item_type='1'
        LEFT JOIN t_discharge_energy_index c ON b.id=c.product_id
        left join (
        select left(t.item_location,4) loation,
        sum(t.energy_raw_value) rawSum,
        sum(t.energy_consumption_last_dl) tceDl,
        sum(t.energy_consumption_last_dj) tceDj,
        sum(t.energy_consumption_last_two_dl) tcetowDl,
        sum(t.energy_consumption_last_two_dj) tcetowDj,
        count(1) num
        from t_discharge_energy_stock t
        <trim prefix="where" prefixOverrides="and">
            <if test="query.industryTypeCode != null and query.industryTypeCode != 'null'">
                and t.industry_type_code = #{query.industryTypeCode}
            </if>
            <if test="query.industrySubclassCode != null and query.industrySubclassCode != 'null'">
                and t.industry_subclass_code = #{query.industrySubclassCode}
            </if>
            <if test="query.itemLocation != null and query.itemLocation != 'null'">
                and t.item_location like concat("%",#{query.itemLocation},"%")
            </if>
            <if test="query.state != null and query.state != 'null'">
                and t.state = #{query.state}
            </if>
        </trim>
        group by left(t.item_location,4)
        ) t2 on left(t1.item_location,4) = t2.loation
        <trim prefix="where" prefixOverrides="and">
            <if test="query.industryTypeCode != null and query.industryTypeCode != 'null'">
                and t1.industry_type_code = #{query.industryTypeCode}
            </if>
            <if test="query.industrySubclassCode != null and query.industrySubclassCode != 'null'">
                and t1.industry_subclass_code = #{query.industrySubclassCode}
            </if>
            <if test="query.itemLocation != null and query.itemLocation != 'null'">
                and t1.item_location like concat("%",#{query.itemLocation},"%")
            </if>
            <if test="query.state != null and query.state != 'null'">
                and t1.state = #{query.state}
            </if>
        </trim>

    </select>
    <select id="getNumAndTceData"
            resultType="java.util.Map">
        select d.meaning2, left(t.item_location,4) as city, sum(t.energy_consumption_last_dl) as tce,count(1) as num,t.dept_id
        from t_discharge_energy_stock t
        left join dict_region d on concat(left(t.item_location,4),'00') = d.id
        <trim prefix="where" prefixOverrides="and">
            <if test="query.industryTypeCode != null">
                and industry_type_code = #{query.industryTypeCode}
            </if>
            <if test="query.itemLocation != null">
                and item_location like concat("%",#{query.itemLocation},"%")
            </if>
            <if test="query.year != null">
                and year = #{query.year}
            </if>
            <if test="query.quarter != null">
                and quarter = #{query.quarter}
            </if>
        </trim>
        group by left(t.item_location,4) ORDER BY num DESC
    </select>
    <select id="getindustryNumAndTce" resultType="java.util.Map">
        select t.industry_type_code as code,count(1) as num,sum(t.energy_consumption_last_dl) as tce,t.dept_id
        from t_discharge_energy_stock t
        <trim prefix="where" prefixOverrides="and">
            <if test="query.industryTypeCode != null">
                and industry_type_code = #{query.industryTypeCode}
            </if>
            <if test="query.itemLocation != null">
                and item_location like concat("%",#{query.itemLocation},"%")
            </if>
            <if test="query.year != null">
                and year = #{query.year}
            </if>
            <if test="query.quarter != null">
                and quarter = #{query.quarter}
            </if>
        </trim>
        group by t.industry_type_code ORDER BY num DESC
    </select>
    <select id="getStockProductIndex"
            resultType="com.nantian.cloud.jljc.vo.dischargeEnergy.DischargeEnergyStockVo">
        select a.id,a.industry_class_name, a.industry_type_code,a.industry_subclass_code,a.item_location,a.enterprise_name,a.enterprise_code,a.remark,a.dept_id,a.state,a.year,a.quarter,a.item_invest,a.enterprise_state,a.energy_raw,a.energy_raw_value,a.energy_consumption_last_dl,a.energy_consumption_last_dj,a.energy_consumption_last_two_dl,a.energy_consumption_last_two_dj,
        c.product_id,c.index_name,c.index_unit,c.product_energy,
        b.id pid,b.product_name,b.product_num,b.product_output1,b.product_output2,b.product_output3,b.product_output4,b.product_output5,b.energy_office,b.energy_number,b.energy_time,b.production_value,b.capacity_design
        from t_discharge_energy_stock a
        LEFT JOIN t_discharge_energy_product b ON a.id=b.item_id and b.item_type='1'
        LEFT JOIN t_discharge_energy_index c ON b.id=c.product_id
        <trim prefix="where" prefixOverrides="and">
            <if test="query.industryTypeCode != null">
                and a.industry_type_code = #{query.industryTypeCode}
            </if>
            <if test="query.industrySubclassCode != null">
                and a.industry_subclass_code = #{query.industrySubclassCode}
            </if>
            <if test="query.itemName != null">
                and a.item_name = #{query.itemName}
            </if>
            <if test="query.delFlag != null">
                and a.del_flag = #{query.delFlag}
            </if>
            <if test="query.itemLocation != null">
                and a.item_location = #{query.itemLocation}
            </if>
            <if test="query.enterpriseName != null">
                and a.enterprise_name = #{query.enterpriseName}
            </if>
            <if test="query.enterpriseCode != null">
                and a.enterprise_code = #{query.enterpriseCode}
            </if>
            <if test="query.year != null">
                and a.year = #{query.year}
            </if>
            <if test="query.quarter != null">
                and a.quarter = #{query.quarter}
            </if>
            <if test="query.id != null">
                and a.id = #{query.id}
            </if>
            <if test="query.state != null">
                and a.state = #{query.state}
            </if>
        </trim>
        ORDER BY a.id DESC

    </select>

</mapper>