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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.srs.dormitory.domain.DmsDormitoryUnit">
        <id column="id" property="id" />
        <result column="name" property="name" />
        <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="selectUnitById" resultType="java.util.Map">
        SELECT u.id,u.`name`,u.create_by createBy,u.create_time createTime,
               u.update_by updateBy,u.update_time updateTime,b.`name` building,b.id buildingId,
               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 d ON d.unit_id = u.id
        WHERE u.id = #{id}
        GROUP BY u.id
    </select>
    <select id="getList" resultType="java.util.Map">
        SELECT u.id,u.`name`,u.building_id buildingId,u.remark,u.create_by createBy,
               u.create_time createTime,u.update_by updateBy,u.create_time updateTime,
               b.`name` buildingName
        FROM dms_dormitory_unit u
                 LEFT JOIN dms_dormitory_building b ON u.building_id = b.id
        <where>
           <if test="id != null">and u.id = #{id}</if>
           <if test="buildingId != null">and b.id = #{buildingId}</if>
           <if test="name != null">and u.name = #{name}</if>
        </where>
    </select>

</mapper>
