<?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.dbcapp.mapper.record.BigScreenMapper">

    <!--获取地区信息-->
    <sql id="base_region">
        select case when code = '530000' then left(code,2) when code like '%00' and code != '530000' then left(code,4) else code end code, tname name, meaning,
	    pcode, IFNULL((select tname from dict_region where code = d.pcode),'云南') pname, IFNULL((select meaning from dict_region where code = d.pcode),'云南省') pmeaning from dict_region d
    </sql>

    <!--获取各企业综合能耗（已接入完成的企业 reportNum like '%已完成%'）-->
    <sql id="base_tce">
        select e.enterpriseCode, e.regionCode, sum(t.guideline_val) value, (select name from temp_dict_name where id = e.tid) industry
        from enterprise_info e left join t_unitenergy2020 t on e.enterpriseCode = t.enterprise_id
        and record_circle between #{startTime} and #{endTime}
        and t.energyTypeCode = '0000' and t.code='00000000' and t.guideline_val &lt; 1000000 and  t.guideline_val &gt; 0 and isRight = "1"
        where e.reportNum like '%已完成%' group by e.enterpriseCode
    </sql>

    <!--根据公式获取各企业能源实物量-->
    <sql id="base_energy_by_formula">
        select tmp.enterprise_id, round(sum(value),4) value, (select regionCode from enterprise_info where enterpriseCode = tmp.enterprise_id) regionCode from (
            select enterprise_id, dataCode, energy_code, sum(consume_sum)*${ratio} value
            from `t_energy_structure` where record_circle = #{month}
            <if test = " list != null and list.size > 0 ">
                and concat(enterprise_id,"#",dataCode) in
                <foreach collection="list" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            group by enterprise_id, dataCode, record_circle having sum(consume_sum)*${ratio} &gt; 0 AND sum(consume_sum)*${ratio} &lt; 1000000
        ) tmp group by enterprise_id
    </sql>

    <!--获取各企业水耗（已接入完成的企业 reportNum like '%已完成%'）-->
    <sql id="base_water">
        select enterprise_id, sum(StandardValue)/10000 as `value`, (select regionCode from enterprise_info where enterpriseCode = t.enterprise_id and reportNum like '%已完成%') regionCode from t_unitenergy2020 t
        where energyTypeCode in (select distinct code from dict_energy_substance where code like '02%')
        and energy_type_code = '03' and `code` = "00000000" and isRight = "1" and StandardValue between 0 and 10000000 and record_circle = #{month}
        group by enterprise_id having regionCode is not null
    </sql>

    <!--根据公式获取各企业能源消费品种 过滤综合能耗为负或超过1000000的企业和月份 过滤企业单月单个采集点折标量超出100万tce的数据-->
    <sql id="base_consumption">
        select enterprise_id, energyName, round(sum(value*ratio*0.0001),4) value, (select regionCode from enterprise_info where enterpriseCode = tmp.enterprise_id) regionCode from (
            select enterprise_id, dataCode, record_circle, energy_code, sum(consume_sum*IFNULL(e.collectRatio,t.modulus)) value,
            case when energy_code in ("3300", "1100", "1200", "1300", "1400", "1500", "1700", "4200") then 0.0001 else 1 end ratio,
            case when '0100,0101,0102,0103,0104,0105,0600,0700,0800' like concat ('%',energy_code,'%') then '煤'
            when '3900,4000,4200,4300,4500,4600,4700' like concat ('%',energy_code,'%') then '其它燃料'
            when '3200,4400,4401,4402,4403,4404' like concat ('%',energy_code,'%') then '热力'
            else (select meaning from dict_energy_type_unit_collect where t.energy_code = code) end energyName
            from `t_energy_structure` t
            left join dict_energy_type_unit_collect e on t.energy_code = e.code and e.code in ("1800","1900","2000","2100","2200","2300","2400","2500","2600","2700","2800","2900","3000","3100","3200","3300","3700","3900")
            where record_circle between #{startTime} and #{endTime}
            <if test = " list != null and list.size > 0 ">
                and concat(t.enterprise_id,"#",t.dataCode) in
                <foreach collection="list" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            and (t.enterprise_id,t.record_circle) in (
                SELECT distinct a.enterprise_id, a.record_circle
                from t_unitenergy2020 a where a.record_circle  between #{startTime} and #{endTime}
                and a.energyTypeCode = '0000' and a.code = '00000000' and a.isRight = "1"
                and a.guideline_val &gt; 0 and a.guideline_val &lt; 1000000
            ) group by enterprise_id, dataCode, record_circle
            having sum(consume_sum*IFNULL(e.collectRatio,t.modulus))*ratio &gt; 0 AND sum(consume_sum*IFNULL(e.collectRatio,t.modulus))*ratio &lt; 1000000
        ) tmp group by enterprise_id, energyName having energyName is not null order by enterprise_id, energyName asc
    </sql>

    <insert id="insertEnterpriseByTmp">
        insert into t_big_screen(value, city, county, type, departmentName, operateDate)
        select IFNULL(count(1),0) num, a.pname, a.name, #{bigScreenType} type, '云南省计量测试技术研究院' departmentName, (select now())
        from (
            <include refid="base_region"></include>
        ) a left join enterprise_info_all b on b.regionCode like concat(a.code,'%')
        <if test="type != null and type != ''">
            and b.type like concat('%',#{type},'%')
        </if>
        group by a.pname, a.name
        union
        select count(1) num, a.pname, a.name, #{bigScreenType} type, concat(case when length(a.code) = 4 then name else pname end, (case when length(a.code) = 4 then (case when meaning like '%市%' then '市' else '州' end) else (case when pmeaning like '%市%' then '市' else '州' end) end),'管理员') departmentName, (select now())
        from (
            <include refid="base_region"></include>
        ) a left join enterprise_info_all b on b.regionCode like concat(a.code,'%')
        <if test="type != null and type != ''">
            and b.type like concat('%',#{type},'%')
        </if>
        where a.pcode is not null group by a.pname, a.name
        order by departmentName asc
    </insert>

    <insert id="insertAllTce">
        insert into t_big_screen(value, year, city, county, type, departmentName, operateDate)
        select IFNULL(sum(b.`value`)/10000,0) num, #{year} year, a.pname, a.name, #{bigScreenType} type, '云南省计量测试技术研究院' departmentName, (select now())
        from (
            <include refid="base_region"></include>
        ) a left join (
            <include refid="base_tce"></include>
        ) b on b.regionCode like concat(a.code,'%') group by a.pname, a.name
        union
        select IFNULL(sum(b.`value`)/10000,0) num, #{year} year, a.pname, a.name, #{bigScreenType} type, concat(case when length(a.code) = 4 then name else pname end,(case when length(a.code) = 4 then (case when meaning like '%市%' then '市' else '州' end) else (case when pmeaning like '%市%' then '市' else '州' end) end),'管理员') departmentName, (select now())
        from (
            <include refid="base_region"></include>
        ) a left join (
            <include refid="base_tce"></include>
        ) b on b.regionCode like concat(a.code,'%') where a.pcode is not null group by a.pname, a.name
        order by departmentName asc
    </insert>

    <insert id="insertRegionTce">
        insert into t_big_screen(value, year, city, county, type, departmentName, operateDate)
        -- 省级用户，选省查看州市数据，选州市看区县数据，选区县看本区县数据
        select IFNULL(sum(b.`value`)/10000,0) num, #{year} year, a.pname, a.name, #{bigScreenType} type, '云南省计量测试技术研究院' departmentName, (select now())
        from (
            <include refid="base_region"></include>
        ) a left join (
            <include refid="base_tce"></include>
        ) b on b.regionCode like concat(a.code,'%') group by a.pname, a.name having a.pname != a.name
        union
        select IFNULL(sum(b.`value`)/10000,0) num, #{year} year, a.name pname, a.name, #{bigScreenType} type, '云南省计量测试技术研究院' departmentName, (select now())
        from (
            <include refid="base_region"></include>
        ) a left join (
            <include refid="base_tce"></include>
        ) b on b.regionCode like concat(a.code,'%') where length(a.code) = 6 group by a.pname, a.name
        -- 州市用户，选州市看区县数据，选区县看本区县数据
        union
        select IFNULL(sum(b.`value`)/10000,0) num, #{year} year, a.pname, a.name, #{bigScreenType} type, concat(case when length(a.code) = 4 then name else pname end,(case when length(a.code) = 4 then (case when meaning like '%市%' then '市' else '州' end) else (case when pmeaning like '%市%' then '市' else '州' end) end),'管理员') departmentName, (select now())
        from (
            <include refid="base_region"></include>
        ) a left join (
            <include refid="base_tce"></include>
        ) b on b.regionCode like concat(a.code,'%') where a.pcode is not null group by a.pname, a.name having a.pname != a.name
        union
        select IFNULL(sum(b.`value`)/10000,0) num, #{year} year, a.name pname, a.name, #{bigScreenType} type, concat(a.pname,(case when pmeaning like '%市%' then '市' else '州' end),'管理员') departmentName, (select now())
        from (
            <include refid="base_region"></include>
        ) a left join (
            <include refid="base_tce"></include>
        ) b on b.regionCode like concat(a.code,'%') where length(a.code) = 6 group by a.pname, a.name
        order by departmentName asc
    </insert>
    
    <insert id="insertIndustryTce">
        insert into t_big_screen(value, year, city, county, type, departmentName, name, operateDate)
        select IFNULL(sum(b.`value`)/10000,0) num, #{year} year, a.pname, a.name, #{bigScreenType} type, '云南省计量测试技术研究院' departmentName, b.industry, (select now())
        from (
            <include refid="base_region"></include>
        ) a left join (
            <include refid="base_tce"></include>
        ) b on b.regionCode like concat(a.code,'%') group by a.pname, a.name, b.industry having b.industry is not null
        union
        select IFNULL(sum(b.`value`)/10000,0) num, #{year} year, a.pname, a.name, #{bigScreenType} type, concat(case when length(a.code) = 4 then name else pname end,(case when length(a.code) = 4 then (case when meaning like '%市%' then '市' else '州' end) else (case when pmeaning like '%市%' then '市' else '州' end) end),'管理员') departmentName, b.industry, (select now())
        from (
            <include refid="base_region"></include>
        ) a left join (
            <include refid="base_tce"></include>
        ) b on b.regionCode like concat(a.code,'%') where a.pcode is not null group by a.pname, a.name, b.industry having b.industry is not null
    </insert>

    <insert id="insertEnergyTceByFormula">
        insert into t_big_screen(value, year, city, county, type, departmentName, name, operateDate)
        select IFNULL(sum(b.`value`),0) num, #{year} year, a.pname, a.name, #{bigScreenType} type, '云南省计量测试技术研究院' departmentName, #{month} month, (select now())
        from (
            <include refid="base_region"></include>
        ) a left join (
            <include refid="base_energy_by_formula"></include>
        ) b on b.regionCode like concat(a.code,'%') group by a.pname, a.name
        union
        select IFNULL(sum(b.`value`),0) num, #{year} year, a.pname, a.name, #{bigScreenType} type, concat(case when length(a.code) = 4 then name else pname end,(case when length(a.code) = 4 then (case when meaning like '%市%' then '市' else '州' end) else (case when pmeaning like '%市%' then '市' else '州' end) end),'管理员') departmentName, #{month} month, (select now())
        from (
            <include refid="base_region"></include>
        ) a left join (
            <include refid="base_energy_by_formula"></include>
        ) b on b.regionCode like concat(a.code,'%') where a.pcode is not null group by a.pname, a.name
        order by departmentName asc
    </insert>

    <insert id="insertWaterValue">
        insert into t_big_screen(value, year, city, county, type, departmentName, name, operateDate)
        select IFNULL(sum(b.`value`),0) num, #{year} year, a.pname, a.name, #{bigScreenType} type, '云南省计量测试技术研究院' departmentName, #{month} month, (select now())
        from (
            <include refid="base_region"></include>
        ) a left join (
            <include refid="base_water"></include>
        ) b on b.regionCode like concat(a.code,'%') group by a.pname, a.name
        union
        select IFNULL(sum(b.`value`),0) num, #{year} year, a.pname, a.name, #{bigScreenType} type, concat(case when length(a.code) = 4 then name else pname end,(case when length(a.code) = 4 then (case when meaning like '%市%' then '市' else '州' end) else (case when pmeaning like '%市%' then '市' else '州' end) end),'管理员') departmentName, #{month} month, (select now())
        from (
            <include refid="base_region"></include>
        ) a left join (
            <include refid="base_water"></include>
        ) b on b.regionCode like concat(a.code,'%') where a.pcode is not null group by a.pname, a.name
        order by departmentName asc
    </insert>

    <insert id="insertConsumptionByFormula">
        insert into t_big_screen(value, year, city, county, type, departmentName, name, operateDate)
        select IFNULL(sum(b.`value`),0) num, #{year} year, a.pname, a.name, #{bigScreenType} type, '云南省计量测试技术研究院' departmentName, b.energyName, (select now())
        from (
            <include refid="base_region"></include>
        ) a left join (
            <include refid="base_consumption"></include>
        ) b on b.regionCode like concat(a.code,'%') group by a.pname, a.name, b.energyName having  b.energyName is not null
        union
        select IFNULL(sum(b.`value`),0) num, #{year} year, a.pname, a.name, #{bigScreenType} type, concat(case when length(a.code) = 4 then name else pname end,(case when length(a.code) = 4 then (case when meaning like '%市%' then '市' else '州' end) else (case when pmeaning like '%市%' then '市' else '州' end) end),'管理员') departmentName, b.energyName, (select now())
        from (
            <include refid="base_region"></include>
        ) a left join (
            <include refid="base_consumption"></include>
        ) b on b.regionCode like concat(a.code,'%') where a.pcode is not null group by a.pname, a.name, b.energyName having b.energyName is not null
        order by departmentName asc
    </insert>


    <select id="getAllArea" resultType="com.nantian.cloud.dbcapp.vo.BigScreenVo">
        SELECT
            b.id as code ,
            a.tname pname,
            b.tname name ,
            b.pcode  as pcode
        FROM
            dict_region a
            LEFT JOIN dict_region b ON a.id = b.pcode
        HAVING
            b.pcode != '530000'


    </select>

    <select id="getTotal" resultType="java.lang.Integer">
        SELECT count(id) FROM enterprise_info d
        where 1=1 and reportNum like '%已完成%'
        <if test="area !=null" >
            and d.regionCode like concat(#{area},"%")
        </if>
        <if test="inde1!=null">
            and d.tid = #{inde1}
        </if>

    </select>

</mapper>
