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


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

		SELECT
		a.sequence_nbr sequenceNbr,
		a.person_charge_name personChargeName,
		a.longitude,
		a.name,
		a.latitude,
		a.person_charge_telephone personChargeTelephone,
		(SELECT count(i.instance_id)
		from
		cb_dynamic_form_instance i
		where
		i.field_value = a.sequence_nbr ) As userNum,
		(SELECT COUNT(*)FROM jc_firestation_jacket WHERE firefighters_id =
		a.sequence_nbr and is_delete =0) AS eqNum,
		Round(st_distance(point(a.longitude,a.latitude),point(#{par.longitude},#{par.latitude}))*111195,1)
		AS distance
		FROM cb_fire_station a
		where a.is_delete=0 and a.longitude is not null and a.latitude is not null
		<if test='par.distance!=null'>
			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="getStationListCount" resultType="Integer">
		SELECT
		COUNT(a.sequence_nbr) num
		FROM cb_fire_station a
		where a.is_delete=0 and a.longitude is not null and a.latitude is not null
		<if test='par.distance!=null'>
			Round(st_distance(point(a.longitude,a.latitude),point(#{par.longitude},#{par.latitude}))*111195,1)
			&lt;= #{par.distance}
		</if>
	</select>


	<select id="getFireStationDetails"
		resultType="com.yeejoin.amos.boot.module.common.api.dto.FireStationDto">
		SELECT
		a.*,
		(SELECT
		count(i.instance_id)
		from
		cb_dynamic_form_instance i
		where
		i.field_value = #{id}) As userNum
		FROM cb_fire_station a
		where
		a.sequence_nbr = #{id}
	</select>

	<select id="getfireStatioCount" resultType="map">
		SELECT
		parent_building_id,
		count(1) as count
		FROM
		cb_fire_station
		WHERE is_delete= false 
		and parent_building_id is not null
		<if test="bizOrgCode != null and bizOrgCode != ''">
		AND	biz_org_code like concat(#{bizOrgCode}, '%')
		</if>
		GROUP BY
		where_building_id
	</select>
</mapper>
