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

    <sql id="Base_Column_List">
        id, name, project_code, enterprise_code, area, industry_code, industry_type, enterprise_name, lg_industry_code, start_date,
        produce_date, check_unit, check_finish_date, check_file_code, investment, output_value, zhnh_dl_value, zhnh_dj_value, result,
        electric_value, coal_value, dept_id, add_time, update_time, del_flag, remarks, sc_year, sc_month,
        adminis_flag, auditor_flag, update_user, audit_user, cause, report_user, report_status, project_nature, construction_content,
        main_product, start_or_not, project_progress, start_and_end_time, energy_consumption, industrial_add_value, design_power_load,
        annual_power_consumption, kilowatt_hour_output_value
    </sql>

    <sql id="New_Base_Column_List">
        t.id, name, project_code, enterprise_code, area old_area, industry_code, industry_type, enterprise_name, lg_industry_code, start_date,
        produce_date, check_unit, check_finish_date, check_file_code, investment, output_value, zhnh_dl_value, zhnh_dj_value, result,
        electric_value, coal_value, t.dept_id, t.add_time, t.update_time, t.del_flag, remarks, sc_year, sc_month,
        adminis_flag, auditor_flag, update_user, audit_user, cause, report_user, report_status, project_nature, construction_content,
        main_product, start_or_not, project_progress, start_and_end_time, energy_consumption, industrial_add_value, design_power_load,
        annual_power_consumption, kilowatt_hour_output_value
    </sql>

    <select id="getPage" resultType="com.nantian.cloud.jljc.entity.ZstbInfo">
        select <include refid="New_Base_Column_List"></include>
        ,case when t.area not like '%53%' then t.area when d1.version like '1' then d1.meaning2 when t.area like '%00%' then concat(d1.meaning2,right(d1.meaning,1)) else (
            select concat(d2.meaning2,right(d2.meaning,1),d1.meaning) from dict_region d2 where d2.id = concat(left(right(t.area,6),4),'00') and d2.del_flag = '1'
        ) end area
        from zstb_info t
        left join dict_region d1 on d1.id = right(t.area,6) and d1.del_flag = '1'
        <trim prefix="where" prefixOverrides="and">
            and t.del_flag = '1'
            <if test="query.scYear != null">
                and sc_year = #{query.scYear}
            </if>
            <if test="query.scMonth != null">
                and sc_month = #{query.scMonth}
            </if>
            <if test="query.enterpriseName != null">
                and enterprise_name like concat("%",#{query.enterpriseName},"%")
            </if>
            <if test="query.enterpriseCode != null">
                and enterprise_code like concat("%",#{query.enterpriseCode},"%")
            </if>
            <if test="query.area != null">
                and area like concat("%",#{query.area},"%")
            </if>
            <if test="query.reportStatus != null">
                and report_status like concat('%', #{query.reportStatus}, '%')
            </if>
            <if test="query.industryType != null">
                and industry_type like concat('%', #{query.industryType}, '%')
            </if>
            <if test="query.industryCode != null">
                and industry_code like concat('%', #{query.industryCode}, '%')
            </if>
            <if test="query.name != null">
                and name like concat('%', #{query.name}, '%')
            </if>
            <if test="query.checkUnit != null">
                and check_unit like concat('%', #{query.checkUnit}, '%')
            </if>
            <if test="query.startOrNot != null">
                and start_or_not like concat('%', #{query.startOrNot}, '%')
            </if>
        </trim>
    </select>
    <select id="getTotal" resultType="com.nantian.cloud.jljc.entity.ZstbInfo">
		select (select meaning2 from dict_region where id like concat(#{deptId},'00%')) regionName, count(id) num, concat(round(sum(zhnh_dl_value),4),'') zhnh_dl_value,
		concat(round(sum(zhnh_dj_value),4),'') zhnh_dj_value, concat(round(sum(result),4),'') result, concat(round(sum(electric_value),4),'') electric_value, concat(round(sum(coal_value),4),'') coal_value,
		concat(round(sum(investment),4),'') investment, concat(round(sum(output_value),4),'') output_value,
        sum(case when JSON_EXTRACT(report_status,"$.city") = "1" then 1 else 0 end) provNum, sum(case when JSON_EXTRACT(report_status,"$.country") = "1" then 1 else 0 end) cityNum
		from zstb_info  where sc_year = #{query.scYear} and sc_month = #{query.scMonth} and dept_id like concat(#{deptId},'%') and del_flag = '1'
    </select>
    <select id="getTotalProv" resultType="com.nantian.cloud.jljc.entity.ZstbInfo">
        select d.meaning2 regionName, count(z.id) num, concat(round(sum(zhnh_dl_value),4),'') zhnh_dl_value, concat(round(sum(zhnh_dj_value),4),'') zhnh_dj_value, concat(round(sum(result),4),'') result,
        concat(round(sum(electric_value),4),'') electric_value, concat(round(sum(coal_value),4),'') coal_value, concat(round(sum(investment),4),'') investment, concat(round(sum(output_value),4),'') output_value,
        sum(case when JSON_EXTRACT(z.report_status,"$.city") = "1" then 1 else 0 end) provNum, sum(case when JSON_EXTRACT(z.report_status,"$.country") = "1" then 1 else 0 end) cityNum
		from dict_region d
		left join zstb_info z on d.id = concat(left(z.dept_id,4),'00') and sc_year = #{query.scYear} and sc_month = #{query.scMonth} and z.del_flag = '1'
		where d.pid = '530000' group by d.id order by d.id
    </select>

</mapper>
