<?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.yeejoin.amos.boot.module.common.api.mapper.WaterResourceMapper">

    <select id="exportToExcel" resultType="com.yeejoin.amos.boot.module.common.api.dto.WaterResourceDto">
        select r.name,
               r.address,
               r.resource_type_name,
               r.belong_building,
               r.belong_fighting_system,
               r.management_unit,
               r.maintenance_unit,
               r.build_date,
               r.enable_date,
               r.orientation_img,
               r.reality_img,
               r.contact_user,
               r.contact_phone,
               (case r.resource_type when 'crane' then rc.height when 'natural' then rn.height end) height,
               (case r.resource_type
                    when 'crane' then rc.status
                    when 'pool' then rp.status
                    when 'natural' then rn.status end)                                              status,
               (case r.resource_type when 'crane' then rc.section when 'pool' then rp.section end) section,
               (case r.resource_type when 'crane' then rc.pipe_network when 'hydarant' then rh.pipe_network end) pipe_network,
               (case r.resource_type when 'crane' then rc.pipe_type_name when 'hydarant' then rh.pipe_type_name when 'pool' then rp.pipe_type_name end) pipe_type_name,
               (case r.resource_type when 'crane' then rc.pipe_diameter when 'hydarant' then rh.pipe_diameter end) pipe_diameter,
               rc.inlet_pipe_diameter,
               rc.outlet_pipe_diameter,
               rc.waterfilling_lane_num,
               (case r.resource_type when 'crane' then rc.water_supply_name when 'hydarant' then rh.water_supply_name when 'pool' then rp.water_supply_name end) water_supply_name,
               (case when 'hydrant' then rh.type when 'natural' then rn.type end) type,
               rh.system_type,
               rh.facilities_category,
               rh.road_junction_info,
               rh.place_form,
               rh.hose_connection,
               (case r.resource_type when 'hydrant' then rh.intake_form when 'pool' then rp.intake_form end) intake_form,
               (case r.resource_type when 'natural' then rn.volume when 'pool' then rp.volume end) volume,
               rn.area,
               rn.quality_situation_info,
               rn.season_change_info,
               rn.has_dry_season,
               rn.dry_period_span,
               rp.intake_height,
               rp.elevation_difference,
               rp.parking_position,
               rp.parking_num,
               ri.iot_water_storage,
               ri.iot_flow_rate,
               ri.iot_status,
               ri.iot_pipe_pressure
        from cb_water_resource r
            left join cb_water_resource_pool rp
        on r.sequence_nbr = rp.resource_id
            left join cb_water_resource_crane rc on r.sequence_nbr = rc.resource_id
            left join cb_water_resource_hydrant rh on r.sequence_nbr = rh.resource_id
            left join cb_water_resource_natural rn on r.sequence_nbr = rn.resource_id
            left join cb_water_resource_iot ri on r.sequence_nbr = ri.resource_id
        where r.is_delete = #{isDelete}
    </select>


    <select id="getWaterResourceList" resultType="com.yeejoin.amos.boot.module.common.api.dto.WaterResourceZhDto">

        SELECT
        a.sequence_nbr sequenceNbr,
        a.name,
        a.longitude,
        a.latitude,
        a.address,
        a.resource_type resourceType,
        a.resource_type_name resourceTypeName,
        a.belong_building belongBuilding,
        a.belong_fighting_system belongFightingSystem,
        a.management_unit managementUnit,
        a.maintenance_unit maintenanceUnit,
        Round(st_distance(point(a.longitude,a.latitude),point(#{par.longitude},#{par.latitude}))*111195,1) AS distance
        FROM cb_water_resource a
        where a.is_delete=1 and a.longitude is not null and a.latitude is not null
        <if test='par.resourceType!=null and par.resourceType!=""'>
            and a.resource_type= #{par.resourceType}
        </if>
        <if test='par.distance!=null'>
           <!--  and Round(st_distance(point(a.longitude,a.latitude),point(#{par.longitude},#{par.latitude}))*111195,1) &lt;=
            #{par.distance} -->
        </if>
        ORDER BY distance limit #{pageNum},#{pageSize}
    </select>

    <select id="getWaterResourceListCount" resultType="Integer">
        SELECT
        COUNT(a.sequence_nbr) num
        FROM cb_water_resource a
        where a.is_delete=1 and a.longitude is not null and a.latitude is not null
        <if test='par.resourceType!=null and par.resourceType!=""'>
            and a.resource_type= #{par.resourceType}
        </if>
        <if test='par.distance!=null'>
          <!--   and Round(st_distance(point(a.longitude,a.latitude),point(#{par.longitude},#{par.latitude}))*111195,1) &lt;=
            #{par.distance} -->
        </if>
    </select>
    <select id="getWaterResourceTypeList" resultType="com.yeejoin.amos.boot.module.common.api.dto.WaterResourceTypeDto">
        select distinct (case
                             when ISNULL(belong_fighting_system_id) = 0 and LENGTH(trim(belong_fighting_system_id)) > 0
                                 then belong_fighting_system_id
                             else 0 end)   belong_fighting_system_id,
                        (case
                             when ISNULL(belong_fighting_system) = 0 and LENGTH(trim(belong_fighting_system)) > 0
                                 then belong_fighting_system
                             else '其他' end) belong_fighting_system,
                        resource_type,
                        resource_type_name
        from cb_water_resource
        where is_delete = #{isDelete}
    </select>
    <select id="getWaterResourcePageByParams"
            resultType="com.yeejoin.amos.boot.module.common.api.dto.WaterResourceDto">
        select * from cb_water_resource where is_delete = 1
        <if test="sequenceNbr != null and sequenceNbr != ''">and sequence_nbr = #{sequenceNbr}</if>
        <if test="resourceType != null and resourceType != ''">and resource_type = #{resourceType}</if>
        <if test="name != null and name != ''">and name like concat('%', #{name}, '%')</if>
        <if test="belongFightingSystemId != null">
            <if test="belongFightingSystemId > -1">
                and belong_fighting_system_id = #{belongFightingSystemId}
            </if>
            <if test="belongFightingSystemId == -1">
                and belong_fighting_system_id is null
            </if>
        </if>
        <if test="belongBuildingId != null and belongBuildingId.size() > 0">
            and find_in_set(belong_building_id, #{belongBuildingId}) > 0
        </if>
    </select>


</mapper>
