<?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.srs.dormitory.mapper.DmsDormitoryFloorMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.srs.dormitory.domain.DmsDormitoryFloor">
        <id column="id" property="id" />
        <result column="floor" property="floor" />
        <result column="building_id" property="buildingId" />
        <result column="remark" property="remark" />
        <result column="create_by" property="createBy" />
        <result column="create_time" property="createTime" />
        <result column="update_by" property="updateBy" />
        <result column="update_time" property="updateTime" />
        <result column="del_flag" property="delFlag" />
    </resultMap>
    <select id="selectTotalOccupancy" resultType="java.lang.Integer">
        SELECT IFNULL(SUM(d.occupancy),0)
        FROM dms_dormitory_floor f
                 LEFT JOIN dms_dormitory d ON f.id=d.floor_id
        WHERE f.id = #{id}
    </select>
    <select id="selectUnitTotalOccupancy" resultType="java.util.Map">
        SELECT u.id,u.`name`,IFNULL(SUM(d.occupancy),0) occupancy
        FROM dms_dormitory_unit u
                 LEFT JOIN dms_dormitory_building b ON u.building_id = b.id
                 LEFT JOIN dms_dormitory_floor f ON f.building_id = b.id
                 LEFT JOIN dms_dormitory d ON d.unit_id = u.id
        WHERE f.id = #{id}
        GROUP BY u.id
    </select>
    <select id="selectFloorById" resultType="java.util.Map">
        SELECT f.id,f.floor,f.remark,f.create_by createBy,f.create_time createTime,f.update_by updareBy,b.id buildingId,
               f.update_time updateTime,b.`name` building,IFNULL(SUM(d.occupancy),0) occupancy,
               IFNULL(COUNT(d.id),0) dormitoryCount
        FROM dms_dormitory_floor f
                 LEFT JOIN dms_dormitory_building b ON f.building_id = b.id
                 LEFT JOIN dms_dormitory d ON d.floor_id = f.id
        WHERE f.id = #{id}
        GROUP BY f.id
    </select>
    <select id="getDormitoryFloorList" resultType="java.util.Map">
        SELECT f.id,f.floor,b.`name` buildingName,f.remark,f.create_by createBy,
               f.create_time createTime,f.update_by updateBy,f.update_time updateTime,
               c.`name` as parkName
        FROM dms_dormitory_floor f
                 LEFT JOIN dms_dormitory_building b ON f.building_id = b.id
                left join dms_dormitory_park as c on b.park_id = c.id
        <where>

            <if test="buildingId != null">and b.id = #{buildingId}</if>
        </where>
    </select>

    <select id="viewFloorInfo" resultType="DomInfo">
        select *
        from view_floor_info
    </select>

</mapper>
