<?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.ggjg.mapper.QuotaMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.nantian.cloud.ggjg.entity.Quota">
        <id column="id" property="id" />
        <result column="area_code" property="areaCode" />
        <result column="type" property="type" />
        <result column="comprehensive_energy" property="comprehensiveEnergy" />
        <result column="building_energy_total" property="buildingEnergyTotal" />
        <result column="building_energy" property="buildingEnergy" />
        <result column="water_energy" property="waterEnergy" />
        <result column="electricity_energy" property="electricityEnergy" />
        <result column="comprehensive_energy_constraints" property="comprehensiveEnergyConstraints" />
        <result column="comprehensive_energy_baseline" property="comprehensiveEnergyBaseline" />
        <result column="comprehensive_energy_boot" property="comprehensiveEnergyBoot" />
        <result column="update_time" property="update_time" />
    </resultMap>

    <select id="queryTjb" parameterType="java.util.HashMap" resultType="java.util.LinkedHashMap">
select * from (
select v.team_title,v.team_type,v.area_code,v.code,d.meaning,v.Business_Time,IFNULL(round((v.f120*0.0001229+v.f150*0.001074122+v.f160*0.001253106+v.f130*0.7143+v.f140*0.00133+v.f170*0.03412+v.f180)*1000/v.f102,4),0) c1,
	IFNULL(round(v.f120*0.0001229+v.f130*0.7143+v.f140*0.00133+v.f170*0.03412+v.f180,4),0) c2,
	IFNULL(round((v.f120*0.0001229+v.f130*0.7143+v.f140*0.00133+v.f170*0.03412+v.f180)*1000/v.f101,4),0) c3,
	IFNULL(round(v.f120/v.f102,4),0) c4,
	IFNULL(round(v.f110/v.f102,4),0) c5,
	IFNULL(round((v.f120*0.0001229+v.f150*0.001074122+v.f160*0.001253106+v.f130*0.7143+v.f140*0.00133+v.f170*0.03412+v.f180)/10000,4),0) c6
	,IFNULL(s.comprehensive_energy,0)	comprehensive_energy
	,IFNULL(s.building_energy_total,0)	building_energy_total
	,IFNULL(s.building_energy,0)	building_energy
	,IFNULL(s.water_energy,0)	water_energy
	,IFNULL(s.electricity_energy,0)	electricity_energy
	,IFNULL(s.comprehensive_energy_constraints,0)	comprehensive_energy_constraints
	,IFNULL(s.comprehensive_energy_baseline,0)	comprehensive_energy_baseline
	,IFNULL(s.comprehensive_energy_boot,0)	comprehensive_energy_boot
	from t_z1view_data v
	left join t_quota s on v.area_code =s.area_code
	left join dict_region d on rpad(v.area_code,6,'0') =d.id
	where   v.Business_Time=2019 and v.level=0 and v.team_title!='合计'
        <if test="map.areaCode != null and map.areaCode !=''" >
            and v.area_code LIKE CONCAT(TRIM(TRAILING '0' FROM #{map.areaCode}), '%')
        </if>
        <if test="map.type != null and map.type !=''" >
            and v.team_type = #{map.type}
        </if>
	order by v.Business_Time) t where t.c1 != 0

    </select>

    <select id="queryList" parameterType="java.util.HashMap" resultType="java.util.LinkedHashMap">

select s.*,d.meaning,'国家机关' type_name	from  t_quota s
	left join dict_region d on rpad(s.area_code,6,'0') =d.id
	where   1=1
        <if test="map.areaCode != null and map.areaCode !=''" >
            and d.id LIKE CONCAT(TRIM(TRAILING '0' FROM #{map.areaCode}), '%')
        </if>
    </select>


    <select id="getOverstretch" resultType="java.util.Map">
        select a.id areaCode, a.meaning area, #{q.type} value, count(b.team_title) label
        from (
            <choose>
                <when test="q.areaCode != null and q.areaCode.length == 2">
                    select '53' as id, '省属' as meaning from dict_region
                    union
                    select left(id,4), meaning from dict_region where pid = '530000'
                </when>
                <otherwise>
                    select left(id,4) as id, '市属' as meaning from dict_region where id = concat(#{q.areaCode},'00')
                    union
                    select id, meaning from dict_region where pid = concat(#{q.areaCode},'00')
                </otherwise>
            </choose>
        )a left join(
            select * from (
                select * from (
                    select v.team_title,v.team_type,v.code,v.Business_Time,
                    <choose>
                        <when test="q.areaCode != null and q.areaCode.length == 2">
                            left(v.area_code,4) area_code,
                            case when v.area_code = '53' then '省属' else d.meaning end meaning,
                        </when>
                        <otherwise>
                            v.area_code, case when v.area_code = #{q.areaCode} then '市属' else d.meaning end meaning,
                        </otherwise>
                    </choose>
                    IFNULL(round((v.f120*0.0001229+v.f150*0.001074122+v.f160*0.001253106+v.f130*0.7143+v.f140*0.00133+v.f170*0.03412+v.f180)*1000/v.f102,4),0) c1,
                    IFNULL(round(v.f120*0.0001229+v.f130*0.7143+v.f140*0.00133+v.f170*0.03412+v.f180,4),0) c2,
                    IFNULL(round((v.f120*0.0001229+v.f130*0.7143+v.f140*0.00133+v.f170*0.03412+v.f180)*1000/v.f101,4),0) c3,
                    IFNULL(round(v.f120/v.f102,4),0) c4,
                    IFNULL(round(v.f110/v.f102,4),0) c5,
                    IFNULL(round((v.f120*0.0001229+v.f150*0.001074122+v.f160*0.001253106+v.f130*0.7143+v.f140*0.00133+v.f170*0.03412+v.f180)/10000,4),0) c6
                    ,IFNULL(s.comprehensive_energy,0)	comprehensive_energy
                    ,IFNULL(s.building_energy_total,0)	building_energy_total
                    ,IFNULL(s.building_energy,0)	building_energy
                    ,IFNULL(s.water_energy,0)	water_energy
                    ,IFNULL(s.electricity_energy,0)	electricity_energy
                    ,IFNULL(s.comprehensive_energy_constraints,0)	comprehensive_energy_constraints
                    ,IFNULL(s.comprehensive_energy_baseline,0)	comprehensive_energy_baseline
                    ,IFNULL(s.comprehensive_energy_boot,0)	comprehensive_energy_boot
                    from t_z1view_data v
                    left join t_quota s on v.area_code =s.area_code
                    left join dict_region d on rpad(v.area_code,6,'0') =d.id
                    where v.Business_Time=2019 and v.level=0 and v.team_title!='合计'
                    <if test="q.type != null and q.type != ''">
                        and v.team_type = #{q.type}
                    </if>
                    order by v.Business_Time
                ) t where t.c1 != 0
            ) temp  where c1 > comprehensive_energy or c2 > building_energy_total or c3 > building_energy  or c4 > water_energy or c5 > electricity_energy
        )b on a.id = b.area_code
        group by a.id
    </select>


</mapper>
