<?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.emergencySupport.modular.es.mapper.EsNetworkEntryMapper">

    <select id="selectesNetworkEntryDetail"
            resultType="com.unis.emergencySupport.modular.es.entity.vo.EsNetworkEntryVO">
        SELECT
            ene.*,
            ar.type,
            ar.step,
            ar.audit_time auditTime,
            ar.attachment,
            ar.audit_advice auditAdvice,
            ar.request_id requestId,
            ar.audit_status auditStatus,
            ar.audit_user_id auditUserId
        FROM
            biz_es_network_entry ene
                LEFT JOIN biz_audit_request ar ON ar.request_id = ene.id
                where ene.id
                =#{esNetworkEntryIdParam.id}
    </select>
    <select id="selectProvinceCount" resultType="com.unis.emergencySupport.modular.es.entity.vo.EsNetworkEntryCountVO">
        SELECT
            ma_prov.management_area_province AS area,
            COUNT(biz_es_network_entry.id) AS enterpriseCount,
            SUM(CASE WHEN beng.grain_variety = 1 THEN beng.quantity ELSE 0 END) AS wheatQuantity,
            SUM(CASE WHEN beng.grain_variety = 2 THEN beng.quantity ELSE 0 END) AS riceQuantity,
            SUM(CASE WHEN beng.grain_variety = 3 THEN beng.quantity ELSE 0 END) AS oilQuantity,
            SUM(CASE WHEN beng.grain_variety = 4 THEN beng.quantity ELSE 0 END) AS otherGrainQuantity
        FROM
            biz_es_network_entry
                INNER JOIN
            biz_es_network_entry_grain beng ON biz_es_network_entry.id = beng.es_network_entry_id
                LEFT JOIN
                (SELECT DISTINCT management_area_province FROM biz_es_network_entry) ma_prov ON biz_es_network_entry.management_area_province = ma_prov.management_area_province
            where beng.type =1
        <if test="esNetworkEntryCountParam.managementAreaProvince!=null and esNetworkEntryCountParam.managementAreaProvince!=''">
            and biz_es_network_entry.management_area_province =#{esNetworkEntryCountParam.managementAreaProvince}
        </if>
        GROUP BY
            ma_prov.management_area_province;

    </select>
    <select id="selectCityCount" resultType="com.unis.emergencySupport.modular.es.entity.vo.EsNetworkEntryCountVO">
        SELECT
            ma_city.management_area_city AS area,
            COUNT(biz_es_network_entry.id) AS enterpriseCount,
            SUM(CASE WHEN beng.grain_variety = 1 THEN beng.quantity ELSE 0 END) AS wheatQuantity,
            SUM(CASE WHEN beng.grain_variety = 2 THEN beng.quantity ELSE 0 END) AS riceQuantity,
            SUM(CASE WHEN beng.grain_variety = 3 THEN beng.quantity ELSE 0 END) AS oilQuantity,
            SUM(CASE WHEN beng.grain_variety = 4 THEN beng.quantity ELSE 0 END) AS otherGrainQuantity
        FROM
            biz_es_network_entry
                INNER JOIN
            biz_es_network_entry_grain beng ON biz_es_network_entry.id = beng.es_network_entry_id
                LEFT JOIN
                (SELECT DISTINCT management_area_city FROM biz_es_network_entry) ma_city ON biz_es_network_entry.management_area_city = ma_city.management_area_city

        where beng.type=1
        <if test="esNetworkEntryCountParam.managementAreaCity!=null and esNetworkEntryCountParam.managementAreaCity!=''">
            and biz_es_network_entry.management_area_city =#{esNetworkEntryCountParam.managementAreaCity}
        </if>
        <if test="esNetworkEntryCountParam.managementAreaProvince!=null and esNetworkEntryCountParam.managementAreaProvince!=''">
            and biz_es_network_entry.management_area_province =#{esNetworkEntryCountParam.managementAreaCity}
        </if>
        GROUP BY
            ma_city.management_area_city;

    </select>
    <select id="selectCountyCount"
            resultType="com.unis.emergencySupport.modular.es.entity.vo.EsNetworkEntryCountVO">
        SELECT
            biz_es_network_entry.management_area_county AS area,
            COUNT(DISTINCT biz_es_network_entry.id) AS enterpriseCount,
            SUM(CASE WHEN biz_es_network_entry_grain.grain_variety = 1 THEN biz_es_network_entry_grain.quantity ELSE 0 END) AS wheatQuantity,
            SUM(CASE WHEN biz_es_network_entry_grain.grain_variety = 2 THEN biz_es_network_entry_grain.quantity ELSE 0 END) AS riceQuantity,
            SUM(CASE WHEN biz_es_network_entry_grain.grain_variety = 3 THEN biz_es_network_entry_grain.quantity ELSE 0 END) AS oilQuantity,
            SUM(CASE WHEN biz_es_network_entry_grain.grain_variety = 4 THEN biz_es_network_entry_grain.quantity ELSE 0 END) AS otherGrainQuantity
        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
        where 1=1
        <if test="esNetworkEntryCountParam.managementAreaCity!=null and esNetworkEntryCountParam.managementAreaCity!=''">
            and
            biz_es_network_entry.management_area_city =#{esNetworkEntryCountParam.managementAreaCity}
        </if>
        <if test="esNetworkEntryCountParam.managementAreaCounty!=null and esNetworkEntryCountParam.managementAreaCounty!=''">
            and
            biz_es_network_entry.management_area_county =#{esNetworkEntryCountParam.managementAreaCounty}
        </if>
        GROUP BY
            biz_es_network_entry.management_area_county;

    </select>
    <select id="selectEnterpriseCount"
            resultType="com.unis.emergencySupport.modular.es.entity.vo.EsNetworkEntryCountVO">
        SELECT
        biz_es_network_entry.enterprise_name AS area,
        SUM(CASE WHEN biz_es_network_entry_grain.grain_variety = 1 THEN biz_es_network_entry_grain.quantity ELSE 0 END) AS wheatQuantity,
        SUM(CASE WHEN biz_es_network_entry_grain.grain_variety = 2 THEN biz_es_network_entry_grain.quantity ELSE 0 END) AS riceQuantity,
        SUM(CASE WHEN biz_es_network_entry_grain.grain_variety = 3 THEN biz_es_network_entry_grain.quantity ELSE 0 END) AS oilQuantity,
        SUM(CASE WHEN biz_es_network_entry_grain.grain_variety = 4 THEN biz_es_network_entry_grain.quantity ELSE 0 END) AS otherGrainQuantity
        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
        <if test="esNetworkEntryCountParam.managementAreaCounty!=null and esNetworkEntryCountParam.managementAreaCounty!=''">
            and
            biz_es_network_entry.management_area_county =#{esNetworkEntryCountParam.managementAreaCounty}
        </if>
        GROUP BY
        biz_es_network_entry.enterprise_name;

    </select>
</mapper>