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

	<select id="getPageList"
		resultType="com.yeejoin.amos.boot.module.common.api.dto.KeySiteDto">
		SELECT
		c.`name` as name,
		c.belong_id as belongId,
		c.biz_org_name as
		belongName,
		c.building_id as buildingId,
		c.building_name as
		buildingName,
		c.address_desc as addressDesc,
		c.building_area as
		buildingArea,
		c.building_height as buildingHeight,
		c.fire_endurance_rate as fireEnduranceRate,
		c.use_nature as useNature,
		c.charge_person AS chargePerson,
		c.sequence_nbr AS sequenceNbr,
		c.charge_person_id AS chargePersonId,
		c.key_prevention_reason AS
		keyPreventionReason,
		c.fire_facilities_info AS fireFacilitiesInfo,
		c.fire_prevention_flag AS firePreventionFlag,
		c.hazard AS hazard,
		c.safety_management_measures AS safetyManagementMeasures,
		c.preventive_measures AS preventiveMeasures,
		c.remark AS remark,
		c.is_delete AS isDelete,
		c.rec_user_id AS recUserId,
		c.rec_user_name AS
		recUserName,
		c.rec_date AS recDate,
		c.fire_endurance_rate_name as
		fireEnduranceRateName,
		c.use_nature_name as useNatureName
		FROM
		cb_key_site c
		where c.is_delete=FALSE
		<if test="name != null and name != ''">
			AND c.`name` like concat('%',#{name}, '%')
		</if>
		<if test="buildingIds != null">
			AND c.`building_id` in  
			  <foreach collection="buildingIds" item="buildingId" open="(" close=")" separator=",">
                #{buildingId}
            </foreach>
         </if>
		<if test="fireEnduranceRate != null and fireEnduranceRate != ''">
			AND c.`fire_endurance_rate`= #{fireEnduranceRate}
		</if>
		<if test="useNature != null and useNature != ''">
			AND c.`use_nature`= #{useNature}
		</if>
		<if test="fireFacilitiesInfo != null and fireFacilitiesInfo != ''">
			AND c.`fire_facilities_info`= #{fireFacilitiesInfo}
		</if>
		<if test="bizOrgCode != null and bizOrgCode !=''">
			AND c.`biz_org_code` like concat(#{bizOrgCode},'%')
		</if>
		order by c.rec_date desc
	</select>

	<select id="getSequenceNbr"
		resultType="com.yeejoin.amos.boot.module.common.api.dto.KeySiteDto">
		SELECT c.`name` as name,
		c.belong_id as belongId,
		cou.biz_org_name as belongName,
		c.building_id as buildingId,
		c.building_name as buildingName,
		c.address_desc as addressDesc,
		c.building_area as buildingArea,
		c.building_height as buildingHeight,
		c.fire_endurance_rate as fireEnduranceRate,
		c.use_nature as useNature,
		c.charge_person AS chargePerson,
		c.sequence_nbr AS sequenceNbr,
		c.charge_person_id AS chargePersonId,
		c.key_prevention_reason AS keyPreventionReason,
		c.fire_facilities_info AS fireFacilitiesInfo,
		c.fire_prevention_flag AS firePreventionFlag,
		c.hazard AS hazard,
		c.safety_management_measures AS safetyManagementMeasures,
		c.preventive_measures AS preventiveMeasures,
		c.remark AS remark,
		c.is_delete AS isDelete,
		c.rec_user_id AS recUserId,
		c.rec_user_name AS recUserName,
		c.longitude,
		c.latitude,
		c.fire_endurance_rate_name as fireEnduranceRateName,
		c.use_nature_name as useNatureName,
		c.charge_person_code chargePersonCode,
		c.biz_org_name as bizOrgName,
		c.biz_org_code as bizOrgCode
		FROM cb_key_site c
		left join cb_org_usr cou on c.belong_id = cou.sequence_nbr
		where
		c.sequence_nbr = #{sequenceNbr}
		and c.is_delete = FALSE;
	</select>

	<select id="getKeySiteList"
		resultType="com.yeejoin.amos.boot.module.common.api.dto.KeySiteDto">
		SELECT c.`name` as name,
		c.belong_id as belongId,
		c.biz_org_name as belongName,
		c.building_id as buildingId,
		c.building_name as buildingName,
		c.address_desc as addressDesc,
		c.building_area as buildingArea,
		c.building_height as buildingHeight,
		c.fire_endurance_rate as fireEnduranceRate,
		c.use_nature as useNature,
		c.charge_person AS chargePerson,
		c.sequence_nbr AS sequenceNbr,
		c.charge_person_id AS chargePersonId,
		c.key_prevention_reason AS keyPreventionReason,
		c.fire_facilities_info AS fireFacilitiesInfo,
		c.fire_prevention_flag AS firePreventionFlag,
		c.hazard AS hazard,
		c.safety_management_measures AS safetyManagementMeasures,
		c.preventive_measures AS preventiveMeasures,
		c.remark AS remark,
		c.is_delete AS isDelete,
		c.rec_user_id AS recUserId,
		c.rec_user_name AS recUserName,
		c.rec_date AS recDate,
		c.fire_endurance_rate_name as fireEnduranceRateName,
		c.use_nature_name as useNatureName
		FROM cb_key_site c
		where c.is_delete = FALSE
		<if test="name != null and name != '' and name != 'null'">
			AND c.`name` like concat('%',#{name}, '%')
		</if>
		<if test="buildingId != null and buildingId != -1">
			AND c.`building_id`= #{buildingId}
		</if>
		<if test="fireEnduranceRate != null and fireEnduranceRate != '' and fireEnduranceRate != 'null'">
			AND c.`fire_endurance_rate`= #{fireEnduranceRate}
		</if>
		<if test="useNature != null and useNature != '' and useNature != 'null'">
			AND c.`use_nature`= #{useNature}
		</if>
		<if test="fireFacilitiesInfo != null and fireFacilitiesInfo != '' and fireFacilitiesInfo != 'null' ">
			AND c.`fire_facilities_info`= #{fireFacilitiesInfo}
		</if>
		<if test="bizOrgCode != null and bizOrgCode!='' and bizOrgCode!='null' ">
			AND c.`biz_org_code` like concat(#{bizOrgCode}, '%')
		</if>
	</select>

	<select id="getKeySiteDate"
		resultType="com.yeejoin.amos.boot.module.common.api.dto.KeySiteDateDto">
		SELECT
		c.sequence_nbr AS `key`,
		c.sequence_nbr AS `value`,
		c.`name` as
		label,
		c.address_desc as fireLocation,
		c.longitude as floorLongitude,
		c.latitude AS floorLatitude,
		cou.biz_org_name as belongName,
		c.belong_id as belongId
		FROM cb_key_site c LEFT JOIN
		cb_org_usr cou on cou.sequence_nbr = c.belong_id
		where
		c.is_delete=FALSE
		<if test="id != null and id != -1">
			AND c.`belong_id`= #{id}
		</if>
	</select>

	<select id="getAddress" resultType="string">
		SELECT
		cb_key_site.address_desc
		FROM
		cb_key_site
		WHERE
		cb_key_site.is_delete = FALSE AND
		cb_key_site.address_desc IS NOT NULL AND
		cb_key_site.address_desc != ''
		GROUP BY
		cb_key_site.address_desc
	</select>

	<select id="getKeySiteCount" resultType="map">
		SELECT
		parent_building_id,
		count(1) as count
		FROM
		cb_key_site
		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
		building_id
	</select>
</mapper>
