<?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.unis.visualization.modular.MonitorAI.mapper.MonitorAIMapper">

    <select id="selectMonitorCount" resultType="java.lang.Integer">
        SELECT COUNT(id) AS totalCameras
        FROM depot_qh.monitor_camera where isremove=1
    </select>
    <select id="selectFaultMonitorCount" resultType="java.lang.Integer">
        SELECT COUNT(id) AS faultyCameras
        FROM depot_qh.monitor_camera
        WHERE isremove=1 and status IN (0, 2)

    </select>
    <select id="selectMonitorProportion" resultType="java.lang.Integer">
        SELECT COUNT(id) AS totalCameras
        FROM depot_qh.monitor_camera where isremove=1 and `status`=1
    </select>
    <select id="selectMonitorInsideCount" resultType="java.util.Map">
        (
            SELECT
                '仓内' AS location,
                COUNT(*) AS totalCount,
                SUM(CASE WHEN status = 0 THEN 1 ELSE 0 END) AS unknownCount,
                SUM(CASE WHEN status = 2 THEN 1 ELSE 0 END) AS offlineCount,
                SUM(CASE WHEN status = 1 THEN 1 ELSE 0 END) AS okCount
            FROM
                depot_qh.monitor_camera
            WHERE
                live = '1' and isremove=1
        ) UNION ALL (
            SELECT
                '仓外' AS location,
                COUNT(*) AS totalCount,
                SUM(CASE WHEN status = 0 THEN 1 ELSE 0 END) AS unknownCount,
                SUM(CASE WHEN status = 2 THEN 1 ELSE 0 END) AS offlineCount,
                SUM(CASE WHEN status = 1 THEN 1 ELSE 0 END) AS okCount
            FROM
                depot_qh.monitor_camera
            WHERE
                live = '2' and isremove=1
        ) ORDER BY location;

    </select>
    <select id="selectCityMonitorCount" resultType="java.util.Map">
        SELECT
            city.enumname,
            COUNT(CASE WHEN mc.status = 1 THEN 1 END) AS onlineCameraCount
        FROM
            depot_qh.monitor_camera mc
                LEFT JOIN depot_qh.org_info oi ON oi.org_id = mc.org_id
                left join depot_qh.basic_enum city on city.enumId=oi.zc_city
        where mc.isremove =1
        GROUP BY
            oi.zc_city;

    </select>
    <select id="selectKqMonitorCount" resultType="java.util.Map">
        SELECT
            COUNT(mc.id) AS camera_count cameraCount,
            oi.longitude,
            oi.latitude
        FROM
            depot_qh.monitor_camera mc
                LEFT JOIN depot_qh.org_info oi ON oi.org_id = mc.org_id
                left join depot_qh.basic_enum city on city.enumId=oi.zc_city
        where mc.isremove =1
        GROUP BY
            mc.org_id;

    </select>
    <select id="selectException" resultType="java.util.Map">
        SELECT
            video_image videoImage
        FROM
            biz_ss_risk_intelligent_recognize
        ORDER BY
            update_time DESC
            LIMIT 20;
    </select>

    <select id="selectMonitorWarningCount" resultType="java.util.Map">
        SELECT
            city.enumname,
            count(ss.id) total
        FROM
            province_all.biz_ss_risk_intelligent_recognize ss
                LEFT JOIN depot_qh.org_info oi ON oi.org_id = ss.org_id
                left join depot_qh.basic_enum city on city.enumId=oi.zc_city
        GROUP BY oi.zc_city
        ORDER BY total desc

    </select>
    <select id="selectMonitorStatistics" resultType="java.util.Map">
        SELECT
            city.enumname,
            count(ss.id) total
        FROM
            depot_qh.monitor_camera ss
                LEFT JOIN depot_qh.org_info oi ON oi.org_id = ss.org_id
                left join depot_qh.basic_enum city on city.enumId=oi.zc_city
        where ss.isremove=1

        GROUP BY oi.zc_city
        ORDER BY total desc
    </select>
    <select id="selectEmergencyBusinessStatistics" resultType="java.util.Map">
        (
            SELECT '应急网点' AS deviceType, COUNT(id)
            FROM  province_all.biz_es_network_entry
        ) UNION ALL (
            SELECT '加工企业', COUNT(id)
            FROM province_all.biz_es_process_enterprise
        ) UNION ALL (
            SELECT '配送企业', COUNT(id)
            FROM province_all.biz_es_delivery_enterprise
        )UNION ALL (
            SELECT '仓储企业', COUNT(id)
            FROM province_all.biz_es_storage_enterprise
        )


    </select>
    <select id="selectCityEmergencyBusinessStatistics" resultType="java.util.Map">
        WITH enterprise_counts AS (
            SELECT 'network' AS enterprise_type, city.DICT_LABEL as dictLabel, COUNT(a.id) AS total
            FROM province_all.biz_es_network_entry a
                     LEFT JOIN province_all.dev_dict city ON city.id = a.management_area_city
            GROUP BY city.DICT_LABEL
            UNION ALL
            SELECT 'process', city.DICT_LABEL, COUNT(a.id)
            FROM province_all.biz_es_process_enterprise a
                     LEFT JOIN province_all.dev_dict city ON city.id = a.management_area_city
            GROUP BY city.DICT_LABEL
            UNION ALL
            SELECT 'delivery', city.DICT_LABEL, COUNT(a.id)
            FROM province_all.biz_es_delivery_enterprise a
                     LEFT JOIN province_all.dev_dict city ON city.id = a.management_area_city
            GROUP BY city.DICT_LABEL
            UNION ALL
            SELECT 'storage', city.DICT_LABEL, COUNT(a.id)
            FROM province_all.biz_es_storage_enterprise a
                     LEFT JOIN province_all.dev_dict city ON city.id = a.management_area_city
            GROUP BY city.DICT_LABEL
        )

        SELECT dictLabel, SUM(total) AS total
        FROM enterprise_counts
        GROUP BY dictLabel
        ORDER BY total desc
    </select>
    <select id="selectDPVSMETStatistics" resultType="java.util.Map">
        SELECT
            CASE
                WHEN grain_variety = 10 THEN '小麦'
                WHEN grain_variety = 11 THEN '稻谷'
                WHEN grain_variety = 12 THEN '油料处理'
                WHEN grain_variety = 13 THEN '油料精炼'
                WHEN grain_variety = 14 THEN '油脂分装'
                WHEN grain_variety = 15 THEN '其它加工'
                ELSE '未知类别'
                END AS processing_type,
            SUM(quantity) AS total_quantity
        FROM
            biz_es_process_enterprise_grain
        WHERE
            grain_variety IN (10, 11, 12, 13, 14, 15) and type=4
        GROUP BY
            grain_variety;


    </select>
    <select id="selectCityDailySupplyCapacity" resultType="java.util.Map">
        SELECT
            city.DICT_LABEL dictLabel,
            SUM(biz_es_network_entry_grain.quantity) AS totalQuantity
        FROM
            biz_es_network_entry
                left JOIN
            biz_es_network_entry_grain
            ON
                    biz_es_network_entry.id = biz_es_network_entry_grain.es_network_entry_id
                LEFT JOIN province_all.dev_dict city ON city.id = biz_es_network_entry.management_area_city
        WHERE
            biz_es_network_entry_grain.type = 3
        GROUP BY
            biz_es_network_entry.actual_address_city;


    </select>
    <select id="selectDailyInventoryCapacity" resultType="java.util.Map">
        SELECT
            CASE
                WHEN grain_variety = 1 THEN '小麦'
                WHEN grain_variety = 2 THEN '稻谷'
                WHEN grain_variety = 6 THEN '小麦粉'
                WHEN grain_variety = 8 THEN '油料'

                ELSE '其他'
                END AS processingType,
            SUM(quantity) AS totalQuantity
        FROM
            biz_es_storage_enterprise_grain
        WHERE
            grain_variety IN (1, 2, 6, 8) and type=1
        GROUP BY
            grain_variety;


    </select>
    <select id="selectDeliveryCapability" resultType="java.util.Map">
        select
            city.DICT_LABEL dictLabel,
            sum(a.own_transport_vehicles) zyyscl,
            sum(a.own_transport_capacity) zyysnl,
            sum(a.daily_delivery_capacity) sjrpsl


        from biz_es_delivery_enterprise a
                 LEFT JOIN province_all.dev_dict city ON city.id = a.management_area_city

        GROUP BY a.management_area_city


    </select>
</mapper>