<?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.ics.admin.mapper.BuildingMapper">

    <resultMap type="com.ics.admin.domain.Building" id="BuildingResult">
        <result property="id"    column="id"    />
        <result property="buildingName"    column="building_name"    />
        <result property="buildingPic"    column="building_pic"    />
        <result property="remark"    column="remark"    />
        <result property="version"    column="version"    />
        <result property="deleteFlag"    column="delete_flag"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="tenantId"    column="tenant_id"    />
        <result property="parkId"   column="park_id"/>
        <association property="park" javaType="com.ics.admin.domain.Park" resultMap="ParkResult" />
    </resultMap>

    <resultMap id="ParkResult" type="com.ics.admin.domain.Park">
        <result property="name"    column="pName"    />
    </resultMap>

    <sql id="selectBuildingVo">
        SELECT
            ib.id,
            ib.building_name,
            ib.building_pic,
            ib.remark,
            ib.version,
            ib.delete_flag,
            ib.create_by,
            ib.create_time,
            ib.update_by,
            ib.update_time,
            ib.tenant_id,
            ib.park_id,
            ip.name AS pName
        FROM
            ics_building AS ib
                LEFT JOIN   ics_park AS ip ON ip.id = ib.`park_id`
    </sql>

    <select id="selectBuildingList" parameterType="com.ics.admin.domain.Building" resultMap="BuildingResult">
        <include refid="selectBuildingVo"/>
        <where>
            <if test="buildingName != null  and buildingName != ''"> AND ib.building_name LIKE CONCAT('%', #{buildingName}, '%')</if>
            <if test="deleteFlag != null"> and ib.delete_flag = #{deleteFlag} </if>
            <if test="parkId != null and parkId != ''"> and ib.park_id = #{parkId} </if>
        </where>
        ORDER BY ib.id DESC
    </select>

</mapper>