<?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.ruoyi.ems.mapper.AreaBuildingMapper">
    
    <resultMap type="com.ruoyi.ems.domain.AreaBuilding" id="AreaBuildingResult">
        <result property="id"    column="id"    />
        <result property="bldgCode"    column="bldg_code"    />
        <result property="bldgName"    column="bldg_name"    />
        <result property="address"    column="address"    />
        <result property="upBldgFloor"    column="up_bldg_floor"    />
        <result property="downBldgFloor"    column="down_bldg_floor"    />
        <result property="bldgHeight"    column="bldg_height"    />
        <result property="floorArea"    column="floor_area"    />
        <result property="bldgUsage"    column="bldg_usage"    />
        <result property="areaCode"    column="area_code"    />
        <result property="areaName"    column="area_name"    />
        <result property="areaShortName"  column="area_short_name"    />
    </resultMap>

    <sql id="selectAreaBuildingVo">
        select b.`id`, b.`bldg_code`, b.`bldg_name`, b.`address`, b.`up_bldg_floor`, b.`down_bldg_floor`, b.`bldg_height`, b.`floor_area`, b.`bldg_usage`, b.`area_code`, a.`area_name`, a.`short_name` as area_short_name from adm_area_building b LEFT JOIN adm_service_area a ON b.`area_code` = a.`area_code`
    </sql>

    <select id="selectAreaBuildingList" parameterType="com.ruoyi.ems.domain.AreaBuilding" resultMap="AreaBuildingResult">
        <include refid="selectAreaBuildingVo"/>
        <where>
            <if test="areaCode != null  and areaCode != ''"> and a.`area_code` = #{areaCode}</if>
            <if test="bldgCode != null  and bldgCode != ''"> and b.`bldg_code` = #{bldgCode}</if>
            <if test="bldgName != null  and bldgName != ''"> and b.`bldg_name` like concat('%', #{bldgName}, '%')</if>
            <if test="address != null  and address != ''"> and b.`address` = #{address}</if>
            <if test="upBldgFloor != null "> and b.`up_bldg_floor` = #{upBldgFloor}</if>
            <if test="downBldgFloor != null "> and b.`down_bldg_floor` = #{downBldgFloor}</if>
            <if test="bldgHeight != null "> and b.`bldg_height` = #{bldgHeight}</if>
            <if test="floorArea != null "> and b.`floor_area` = #{floorArea}</if>
            <if test="bldgUsage != null  and bldgUsage != ''"> and b.`bldg_usage` = #{bldgUsage}</if>
        </where>
    </select>
    
    <select id="selectAreaBuildingById" parameterType="Long" resultMap="AreaBuildingResult">
        <include refid="selectAreaBuildingVo"/>
        where b.`id` = #{id}
    </select>

    <select id="selectAreaBuildingByCode" parameterType="String" resultMap="AreaBuildingResult">
        <include refid="selectAreaBuildingVo"/>
        where b.`bldg_code` = #{code}
    </select>

    <insert id="insertAreaBuilding" parameterType="com.ruoyi.ems.domain.AreaBuilding" useGeneratedKeys="true" keyProperty="id">
        insert into adm_area_building
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="bldgCode != null and bldgCode != ''">bldg_code,</if>
            <if test="bldgName != null and bldgName != ''">bldg_name,</if>
            <if test="address != null">address,</if>
            <if test="upBldgFloor != null">up_bldg_floor,</if>
            <if test="downBldgFloor != null">down_bldg_floor,</if>
            <if test="bldgHeight != null">bldg_height,</if>
            <if test="floorArea != null">floor_area,</if>
            <if test="bldgUsage != null">bldg_usage,</if>
            <if test="areaCode != null">area_code,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="bldgCode != null and bldgCode != ''">#{bldgCode},</if>
            <if test="bldgName != null and bldgName != ''">#{bldgName},</if>
            <if test="address != null">#{address},</if>
            <if test="upBldgFloor != null">#{upBldgFloor},</if>
            <if test="downBldgFloor != null">#{downBldgFloor},</if>
            <if test="bldgHeight != null">#{bldgHeight},</if>
            <if test="floorArea != null">#{floorArea},</if>
            <if test="bldgUsage != null">#{bldgUsage},</if>
            <if test="areaCode != null">#{areaCode},</if>
         </trim>
    </insert>

    <update id="updateAreaBuilding" parameterType="com.ruoyi.ems.domain.AreaBuilding">
        update adm_area_building
        <trim prefix="SET" suffixOverrides=",">
            <if test="bldgCode != null and bldgCode != ''">bldg_code = #{bldgCode},</if>
            <if test="bldgName != null and bldgName != ''">bldg_name = #{bldgName},</if>
            <if test="address != null">address = #{address},</if>
            <if test="upBldgFloor != null">up_bldg_floor = #{upBldgFloor},</if>
            <if test="downBldgFloor != null">down_bldg_floor = #{downBldgFloor},</if>
            <if test="bldgHeight != null">bldg_height = #{bldgHeight},</if>
            <if test="floorArea != null">floor_area = #{floorArea},</if>
            <if test="bldgUsage != null">bldg_usage = #{bldgUsage},</if>
            <if test="areaCode != null">area_code = #{areaCode},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteAreaBuildingById" parameterType="Long">
        delete from adm_area_building where id = #{id}
    </delete>

    <delete id="deleteAreaBuildingByIds" parameterType="String">
        delete from adm_area_building where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>