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

    <select id="getPage" resultType="com.nantian.cloud.jljc.entity.UseEngergy">
        select t.id, enterprise_name, enterprise_code, industry_code, area old_area, last_year_elv, last_year_ev, cur_year_month_elv,
        cur_year_month_ev, date_section_elv, date_section_ev, last_month_electric_value, sc_year, sc_quarter, sc_month, t.dept_id,
        add_time, t.update_time, t.del_flag, remarks, adminis_flag, auditor_flag, update_user, audit_user, cause, report_user, report_status,
        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_use_engergy  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>
        </trim>
    </select>
    <select id="getArea" resultType="java.lang.String">
        <choose>
            <when test="deptId=='53'">
                select concat(meaning2,'_',id) from dict_region where id like concat(#{deptId},'%') and pid = '530000'
            </when>
            <otherwise>
                select concat(meaning2,'_',id) from dict_region where id like concat(#{deptId},'%') and id not like concat(#{deptId},'00%')
            </otherwise>
        </choose>
    </select>
    <select id="getTotal" resultType="com.nantian.cloud.jljc.entity.UseEngergy">
        select (select meaning2 from dict_region where id like concat(#{deptId},'00%')) regionName,
        count(distinct u.id) num, concat(round(sum(last_year_elv),4),'') last_year_elv, concat(round(sum(last_year_ev),4),'') last_year_ev, concat(round(sum(cur_year_month_elv),4),'') cur_year_month_elv, concat(round(sum(cur_year_month_ev),4),'') cur_year_month_ev,
        concat(round(sum(date_section_elv),4),'') date_section_elv, concat(round(sum(date_section_ev),4),'') date_section_ev, concat(round(sum(last_month_electric_value),4),'') last_month_electric_value, concat(round(sum(cur_year_month_energy),4),'') cur_year_month_energy,
        count(distinct case when JSON_EXTRACT(u.report_status,"$.city") = "1" then u.id else null end) provNum, count(distinct case when JSON_EXTRACT(u.report_status,"$.country") = "1" then u.id else null end) cityNum
        from zstb_use_engergy u left join zstb_produce p on u.id = p.use_engergy_id and p.del_flag = '1'
        where sc_year = #{query.scYear} and sc_month = #{query.scMonth} and dept_id like concat(#{deptId},'%') and u.del_flag = '1'
    </select>
    <select id="getTotalProv" resultType="com.nantian.cloud.jljc.entity.UseEngergy">
        select d.meaning2 regionName, count(distinct u.id) num, concat(round(sum(last_year_elv),4),'') last_year_elv, concat(round(sum(last_year_ev),4),'') last_year_ev, concat(round(sum(cur_year_month_elv),4),'') cur_year_month_elv, concat(round(sum(cur_year_month_ev),4),'') cur_year_month_ev,
        concat(round(sum(date_section_elv),4),'') date_section_elv, concat(round(sum(date_section_ev),4),'') date_section_ev, concat(round(sum(last_month_electric_value),4),'') last_month_electric_value, concat(round(sum(cur_year_month_energy),4),'') cur_year_month_energy,
        count(distinct case when JSON_EXTRACT(u.report_status,"$.city") = "1" then u.id else null end) provNum, count(distinct case when JSON_EXTRACT(u.report_status,"$.country") = "1" then u.id else null end) cityNum
        from dict_region d
        left join zstb_use_engergy u on d.id = concat(left(u.dept_id,4),'00') and sc_year = #{query.scYear} and sc_month = #{query.scMonth} and u.del_flag = '1'
        left join zstb_produce p on u.id = p.use_engergy_id and p.del_flag = '1'
        where d.pid = '530000' group by d.id order by d.id
    </select>
    <select id="getUnit" resultType="java.lang.String">
         select name from measurement_unit_base where name not like '%人%'
    </select>

</mapper>
