<?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="cn.cecep.talroad.mapper.LeaderCockpitMapper">


    <select id="selectStationTypeList" resultType="cn.cecep.talroad.domain.leader.vo.StationTypeDictVO">

        select type_name,
               type_code,
               station_type
        from s_station_type_dict where status = 0
                                 order by sort
    </select>

    <select id="selectMapUpCount" resultType="cn.cecep.talroad.domain.leader.vo.MapUpVo">
        select * from
                     (select count(1) as problemCount from m_am_problem_fi where feedback_date is null) t1,
                     (select count(1) as taskCount from b_main_exception_task_check where deal_status = '未办理')t2,
                     (select count(1) as factoryCount from pc_factory pf  where pf.country_code is not null)t3
    </select>

    <select id="getFactoryManTypeCount" resultType="cn.cecep.talroad.domain.leader.vo.ManTypeVO">

        select count(*) as totalCount,
                count(*) filter (where license_man_type like 'FOCUS')  as focusCount,
                count(*) filter (where license_man_type = 'SIMPLIFY') as simplifyCount,
                count(*) filter (where license_man_type = 'REGISTER') as registerCount
        from pc_factory where country_code is not null
    </select>

<!--        之前的数据接入情况查询sql-->
<!--    select '在线监测' as adsl_name , count(*) as adslCount-->
<!--    from (select factory_id from pc_outlet where pollutant_type = '4' group by factory_id) t1-->
<!--    union ALL-->
<!--    select '无组织排放' as adsl_name , count(*) as adslCount-->
<!--    from (select factory_id from pc_outlet where pollutant_type in('1','2') and pollutant_outlet_type = '2' group by factory_id) t1-->
<!--    union ALL-->
<!--    select '分表计电' as adsl_name , count(*) as adslCount-->
<!--    from (select factory_id from bas_device_site group by factory_id) t1-->
<!--    union ALL-->
<!--    select '工况监控' as adsl_name , count(*) as adslCount-->
<!--    from (select factory_id from bas_device where is_status = '1' group by factory_id ) t1-->
<!--    union ALL-->
<!--    select '视频监控' as adsl_name , count(*) as adslCount-->
<!--    from (select factory_id from m_am_video_dev_fi where factory_id is not null group by factory_id) t1-->
<!--    union ALL-->
<!--    select '动态管控' as adsl_name , count(*) as adslCount-->
<!--    from (select factory_id from pc_station_building where factory_id is not null group by factory_id) t1-->
<!--    union ALL-->
<!--    select '门禁监控' as adsl_name , count(*) as adslCount-->
<!--    from (select factory_id from pc_factory_access_control where  factory_id is not null group by factory_id) t1-->

    <select id="getFactoryDataTypeCount" resultType="cn.cecep.talroad.domain.leader.vo.DataTypeVO">
        SELECT
            adsl.adsl_name ,
            count(*) as adslCount
        from pc_adsl_factory factory left join pc_adsl adsl on factory.adsl_code = adsl.adsl_code
        group by adsl.adsl_name
        union ALL
        select '固危废' as adsl_name , count(*) as adslCount
        from (select factory_id from b_main_hazardous_waste_output where  factory_id is not null group by factory_id) t1

    </select>


    <select id="selectDeviceCount" resultType="cn.cecep.talroad.domain.leader.vo.DeviceCountVO">
        select  count(*) as totalCount,
                count(*) filter (where bd.device_type = '0' ) as productTotalCount,
                count(*) filter (where bd.device_type = '0' and bd.industry ='XK' ) as productPermitCount,
                count(*) filter (where bd.device_type = '0' and bd.industry ='EL' ) as productElecCount,
                count(*) filter (where bd.device_type is null ) as productDcsCount,
                count(*) filter (where bd.device_type = '1' ) as governTotalCount,
                count(*) filter (where bd.device_type = '1' and bd.industry ='XK' ) as governPermitCount,
                count(*) filter (where bd.device_type = '1' and bd.industry ='EL' ) as governtElecCount
        from bas_device bd
    </select>

    <select id="selectOutletCount" resultType="cn.cecep.talroad.domain.leader.vo.OutletCountVO">
        select count(*) filter (where pc.pollutant_type != '4')totalCount,
                count(*) filter (where  pc.pollutant_type = '1' and pc.pollutant_outlet_type = '1') gasCount,
                count(*) filter (where  pc.pollutant_type = '4' and pc.pollutant_outlet_type = '1' ) gasMonitorCount,
                (select count(*) filter ( where pc.pollutant_type = '1') from pc_outlet pc inner join (SELECT pc.parent_outlet_id FROM pc_outlet pc inner JOIN s_main_dynamic_control smdc ON pc.ID = smdc.outlet_id where pc.parent_outlet_id is not null and pc.parent_outlet_id != '') t1 on t1.parent_outlet_id = pc.id) as gasDynamicCount,
               count(*) filter (where  pc.pollutant_type = '2' ) waterCount,
                count(*) filter (where  pc.pollutant_type = '4' and pc.pollutant_outlet_type = '2' ) waterMonitorCount,
                (select count(*) filter ( where pc.pollutant_type = '2') from pc_outlet pc inner join (SELECT pc.parent_outlet_id FROM pc_outlet pc inner JOIN s_main_dynamic_control smdc ON pc.ID = smdc.outlet_id where pc.parent_outlet_id is not null and pc.parent_outlet_id != '') t1 on t1.parent_outlet_id = pc.id) as waterDynamicCount,
               count(*) filter (where  pc.pollutant_type = '1' and pc.pollutant_outlet_type = '2') gasNoGroupCount,
                count(*) filter (where  pc.pollutant_type = '3' ) gasNoGroupMonitorCount
        from pc_outlet pc where pc.is_status = '1'
    </select>

    <select id="getDatabaseDataCount" resultType="java.lang.Long">
        select sum(reltuples) from (
                                       SELECT
                                           relname,
                                           reltuples
                                       FROM
                                           pg_class
                                               CLS LEFT JOIN pg_namespace N ON ( N.oid = CLS.relnamespace )
                                       WHERE
                                           nspname NOT IN ( 'pg_catalog', 'information_schema' )
                                         AND relkind = 'r' ) t1
    </select>

    <select id="selectProblemTypeCount" resultType="cn.cecep.talroad.domain.leader.vo.ProblemTypeCountVO">
        select count(*) filter (where prpt.parent_id is null) as levelOneCount ,
                (select count(*) from
                    ((select id from pc_ra_problem_type prpt where "char_length"(prpt.parent_id) = 3 and is_status = '1' order by id )
                     except
                     (select parent_id as id  from pc_ra_problem_type  where "char_length"(parent_id) = 6 and is_status = '1' group by parent_id )
                     union all
                     (select id  from pc_ra_problem_type  where "char_length"(parent_id) = 6 and is_status = '1' )) t1 ) as levelTwoCount,
               19 as modelCount
        from pc_ra_problem_type prpt
    </select>

    <select id="selectProblemDoingCount" resultType="cn.cecep.talroad.domain.leader.vo.ProblemDoingCountVO">
        select
            count(*) as totalCount,
            count(*) filter(where mapf.feedback_date is null) as undoneCount,
                count(*) filter(where mapf.feedback_date is not null) as doneCount
        from m_am_problem_fi mapf
    </select>

    <select id="selectProblemOneLevelGroupCount" resultType="cn.cecep.talroad.domain.leader.vo.ProblemGroupCountVO">
        select
            prpt.name,
            prpt."id" as problemId,
            count(*) filter (where mapf.id is not null) as totalCount
        from pc_ra_problem_type prpt
                 left join  m_am_problem_fi mapf on substring(mapf.problem_type,1,3)  = prpt.id
        where prpt.parent_id is null
        group by prpt."name",prpt."id" order by totalCount desc

    </select>

    <select id="selectProblemTwoLevelGroupCount" resultType="cn.cecep.talroad.domain.leader.vo.ProblemGroupCountVO">
        select
            prpt.name,
            count(*) filter (where mapf.id is not null) as totalCount
        from pc_ra_problem_type prpt
                 left join  m_am_problem_fi mapf on mapf.problem_type  = prpt.id
        <where>
           <if test="problemId != null and problemId !=''">
               and substring(prpt.id,1,3) = #{problemId}
           </if>
        </where>
        group by prpt."name" order by totalCount desc
    </select>

    <select id="getCityAirQualityData" resultType="cn.cecep.talroad.domain.leader.vo.AirQualityDataVO">
        SELECT x.* FROM public.pc_national_city_ranking_hour x
        WHERE "cityName" = '唐山市'
        ORDER BY x.date_time DESC
        limit 1
    </select>

</mapper>
