<?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.esstorageenterprise.mapper.BizEsStorageEnterpriseMapper">

<select id="getAllList" resultType="java.util.Map">
    SELECT
        a.enterprise_name qymc,
        a.id id,
        a.longitude jd,
        a.latitude wd,
        '1' type
    FROM
        biz_es_network_entry a -- 应急网点
    UNION
    SELECT
        b.enterprise_name qymc,
        b.id id,
        b.longitude jd,
        b.latitude wd,
        '2' type
    FROM
        biz_es_process_enterprise b -- 应急加工企业
    UNION
    SELECT
        c.enterprise_name qymc,
        c.id id,
        c.longitude jd,
        c.latitude wd,
        '3' type
    FROM
        biz_es_delivery_enterprise c  -- 应急配送企业
    UNION
    SELECT
        d.enterprise_name qymc,
        d.id id,
        d.longitude jd,
        d.latitude wd,
        '4' type
    FROM
        biz_es_storage_enterprise d    -- 应急仓储企业

</select>

    <select id="getListByType" resultType="java.util.Map" parameterType="java.lang.String">
        <if test="type==1">
            SELECT
            a.enterprise_name qymc,
            a.id id,
            a.longitude jd,
            a.latitude wd,
            '1' type
            FROM
            biz_es_network_entry a -- 应急网点
        </if>
        <if test="type==2">
            SELECT
            b.enterprise_name qymc,
            b.id id,
            b.longitude jd,
            b.latitude wd,
            '2' type
            FROM
            biz_es_process_enterprise b -- 应急加工企业
        </if>
        <if test="type==3">
            SELECT
            c.enterprise_name qymc,
            c.id id,
            c.longitude jd,
            c.latitude wd,
            '3' type
            FROM
            biz_es_delivery_enterprise c  -- 应急配送企业
        </if>
        <if test="type==4">
            SELECT
            d.enterprise_name qymc,
            d.id id,
            d.longitude jd,
            d.latitude wd,
            '4' type
            FROM
            biz_es_storage_enterprise d    -- 应急仓储企业
        </if>
    </select>
    <select id="getDataByProvince" resultType="java.util.Map">
       -- grain_variety = 1：小麦原粮  5：其他原粮 7：大米成品粮 8：食用油成品粮
       -- type = 1 日库存
        /*select
            management_area_province province,
            management_area_province dq,
            '' city,
            '' county,
            sum(case grain_variety when '1' then quantity else 0 end) as 'xmylrkc',
                sum(case grain_variety when '5' then quantity else 0 end) as 'qtylrkc',
                sum(case grain_variety when '7' then quantity else 0 end) as 'dmcplrkc',
                sum(case grain_variety when '8' then quantity else 0 end) as 'syycplrkc'
        from (
                 SELECT
                     a.management_area_province,
                     a.management_area_city,
                     a.management_area_county,
                     b.grain_variety,
                     b.quantity
                 FROM
                     biz_es_storage_enterprise a
                         LEFT JOIN biz_es_storage_enterprise_grain b on a.id = b.es_storage_enterprise_id
                 where b.type = 1 and b.grain_variety in (1,5,7,8)
             )c GROUP BY management_area_province*/
       select
           management_area_province province,
           -- '' city,
           -- '' county,
           dqmc dqmc,
           sum(case grain_variety when '1' then quantity else 0 end) as 'xmylrkc',
               sum(case grain_variety when '5' then quantity else 0 end) as 'qtylrkc',
               sum(case grain_variety when '7' then quantity else 0 end) as 'dmcplrkc',
               sum(case grain_variety when '8' then quantity else 0 end) as 'syycplrkc'
       from (
                SELECT
                    a.management_area_province,
                    a.actual_address_city city,
                    a.actual_address_county county,
                    c.DICT_LABEL dqmc,
                    b.grain_variety,
                    b.quantity
                FROM
                    biz_es_storage_enterprise a
                        LEFT JOIN biz_es_storage_enterprise_grain b on a.id = b.es_storage_enterprise_id
                        LEFT JOIN dev_dict c on a.management_area_province = c.id
                -- where b.type = 1 and b.grain_variety in (1,5,7,8)
            )d GROUP BY management_area_province

    </select>
    <select id="getSlByProvince" resultType="java.util.Map">
        select COUNT(id) wdsl,management_area_province province from biz_es_storage_enterprise GROUP BY management_area_province
    </select>

    <select id="getDataByCity" resultType="java.util.Map">
        -- grain_variety = 1：小麦原粮  5：其他原粮 7：大米成品粮 8：食用油成品粮
        -- type = 1 日库存
        /*select
            management_area_province province,
            management_area_city city,
            '' county,
            sum(case grain_variety when '1' then quantity else 0 end) as 'xmylrkc',
                sum(case grain_variety when '5' then quantity else 0 end) as 'qtylrkc',
                sum(case grain_variety when '7' then quantity else 0 end) as 'dmcplrkc',
                sum(case grain_variety when '8' then quantity else 0 end) as 'syycplrkc'
        from (
                 SELECT
                     a.management_area_province,
                     a.management_area_city,
                     a.management_area_county,
                     b.grain_variety,
                     b.quantity
                 FROM
                     biz_es_storage_enterprise a
                         LEFT JOIN biz_es_storage_enterprise_grain b on a.id = b.es_storage_enterprise_id
                 where b.type = 1 and b.grain_variety in (1,5,7,8)
             )c GROUP BY management_area_city*/
        select
            management_area_province province,
            management_area_city city,
            dqmc dqmc,
            sum(case grain_variety when '1' then quantity else 0 end) as 'xmylrkc',
                sum(case grain_variety when '5' then quantity else 0 end) as 'qtylrkc',
                sum(case grain_variety when '7' then quantity else 0 end) as 'dmcplrkc',
                sum(case grain_variety when '8' then quantity else 0 end) as 'syycplrkc'
        from (
                 SELECT
                     a.management_area_province,
                     a.management_area_city,
                     a.management_area_county,
                     c.DICT_LABEL dqmc,
                     b.grain_variety,
                     b.quantity
                 FROM
                     biz_es_storage_enterprise a
                         LEFT JOIN biz_es_storage_enterprise_grain b on a.id = b.es_storage_enterprise_id
                         LEFT JOIN dev_dict c on a.management_area_city = c.id
                 -- where b.type = 1 and b.grain_variety in (1,5,7,8)
             )c  GROUP BY management_area_city
    </select>
    <select id="getSlByCity" resultType="java.util.Map">
        select COUNT(id) wdsl,management_area_city city from biz_es_storage_enterprise GROUP BY management_area_city
    </select>

    <select id="getDataByCounty" resultType="java.util.Map" parameterType="java.lang.String">
        -- grain_variety = 1：小麦原粮  5：其他原粮 7：大米成品粮 8：食用油成品粮
        -- type = 1 日库存
        /*select
            management_area_province province,
            management_area_city city,
            management_area_county county,
            sum(case grain_variety when '1' then quantity else 0 end) as 'xmylrkc',
                sum(case grain_variety when '5' then quantity else 0 end) as 'qtylrkc',
                sum(case grain_variety when '7' then quantity else 0 end) as 'dmcplrkc',
                sum(case grain_variety when '8' then quantity else 0 end) as 'syycplrkc'
        from (
                 SELECT
                     a.management_area_province,
                     a.management_area_city,
                     a.management_area_county,
                     b.grain_variety,
                     b.quantity
                 FROM
                     biz_es_storage_enterprise a
                         LEFT JOIN biz_es_storage_enterprise_grain b on a.id = b.es_storage_enterprise_id
                 where b.type = 1 and b.grain_variety in (1,5,7,8)
             )c GROUP BY management_area_county*/
        select
            management_area_province province,
            management_area_city city,
            management_area_county county,
            dqmc dqmc,
            sum(case grain_variety when '1' then quantity else 0 end) as 'xmylrkc',
                sum(case grain_variety when '5' then quantity else 0 end) as 'qtylrkc',
                sum(case grain_variety when '7' then quantity else 0 end) as 'dmcplrkc',
                sum(case grain_variety when '8' then quantity else 0 end) as 'syycplrkc'
        from (
                 SELECT
                     a.management_area_province,
                     a.management_area_city,
                     a.management_area_county,
                     c.DICT_LABEL dqmc,
                     b.grain_variety,
                     b.quantity
                 FROM
                     biz_es_storage_enterprise a
                         LEFT JOIN biz_es_storage_enterprise_grain b on a.id = b.es_storage_enterprise_id
                         LEFT JOIN dev_dict c on a.management_area_county = c.id
                 -- where b.type = 1 and b.grain_variety in (1,5,7,8)
                 where 1=1
                   <if test="city != null">
                       and a.management_area_city = #{city}
                   </if>

             )c GROUP BY management_area_county
    </select>
    <select id="getSlByCounty" resultType="java.util.Map">
        select COUNT(id) wdsl,management_area_county county,management_area_city from biz_es_storage_enterprise
            <if test="city != null">
                where management_area_city = #{city}
            </if>
        GROUP BY management_area_county
    </select>

    <select id="getQyDataByCounty" resultType="java.util.Map" parameterType="java.lang.String">
        select
            management_area_province province,
            management_area_city city,
            management_area_county county,
            dqmc dqmc,
            qymc qymc,
            sum(case grain_variety when '1' then quantity else 0 end) as 'xmylrkc',
            sum(case grain_variety when '5' then quantity else 0 end) as 'qtylrkc',
            sum(case grain_variety when '7' then quantity else 0 end) as 'dmcplrkc',
            sum(case grain_variety when '8' then quantity else 0 end) as 'syycplrkc'
        from (
                 SELECT
                     a.management_area_province,
                     a.management_area_city,
                     a.management_area_county,
                     c.DICT_LABEL dqmc,
                     b.grain_variety,
                     b.quantity,
                     a.enterprise_name qymc
                 FROM
                     biz_es_storage_enterprise a
                         LEFT JOIN biz_es_storage_enterprise_grain b on a.id = b.es_storage_enterprise_id
                         LEFT JOIN dev_dict c on a.management_area_county = c.id
                 where 1=1
                   and a.management_area_county = #{county}
             )c GROUP BY qymc
    </select>
</mapper>