<?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.EsProcessEnterpriseMapper">

    <select id="selectProvinceCount"
            resultType="com.unis.emergencySupport.modular.es.entity.vo.EsProcessEnterpriseCountVo">
        SELECT
            management_area_province area,
            COUNT(*) AS  enterpriseCount
        FROM
            biz_es_process_enterprise
        GROUP BY
            management_area_province


    </select>
    <select id="selectCityCount"
            resultType="com.unis.emergencySupport.modular.es.entity.vo.EsProcessEnterpriseCountVo">
        SELECT
            management_area_city area,
            COUNT(*) AS enterpriseCount
        FROM
            biz_es_process_enterprise
        where 1=1
        <if test="param.managementAreaCity!=null and param.managementAreaCity!=''">
            and
            management_area_city =#{param.managementAreaCity}
        </if>
        GROUP BY
            management_area_city

    </select>
    <select id="selectCountyCount"
            resultType="com.unis.emergencySupport.modular.es.entity.vo.EsProcessEnterpriseCountVo">
        SELECT
        management_area_county area,
        COUNT(*) AS enterpriseCount
        FROM
        biz_es_process_enterprise
        where 1=1
        <if test="param.managementAreaCity!=null and param.managementAreaCity!=''">
            and
            management_area_city =#{param.managementAreaCity}
        </if>
        <if test="param.managementAreaCounty!=null and param.managementAreaCounty!=''">
            and
            management_area_county =#{param.managementAreaCounty}
        </if>

        GROUP BY
        management_area_county



    </select>
    <select id="selecTprCacount"
            resultType="com.unis.emergencySupport.modular.es.entity.vo.EsProcessEnterprisePrCaCountVo">
        SELECT
            e.enterprise_name enterpriseName,
            SUM(CASE WHEN g.grain_variety = 10 THEN g.quantity ELSE 0 END) AS "wheatQuantity",
            SUM(CASE WHEN g.grain_variety = 11 THEN g.quantity ELSE 0 END) AS "riceQuantity",
            SUM(CASE WHEN g.grain_variety IN (12) THEN g.quantity ELSE 0 END) AS "oilQuantity"
        FROM
            biz_es_process_enterprise e
             left   JOIN
            biz_es_process_enterprise_grain g ON e.id = g.es_process_enterprise_id
        WHERE
            e.management_area_county =#{param.managementAreaCounty}
        <if test="param.enterpriseName!=null and param.enterpriseName!=''">
            and
            e.enterprise_name LIKE CONCAT('%', #{param.enterpriseName}, '%')
        </if>
          AND g.type = 5
          AND g.grain_variety IN (10, 11, 12)
        GROUP BY
            e.enterprise_name;
    </select>
</mapper>