<?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.comprehensive.mapper.SrsDormitoryMapper">
    
    <resultMap type="SrsDormitory" id="SrsDormitoryResult">
        <result property="id"    column="id"    />
        <result property="floorNo"    column="floor_no"    />
        <result property="roomNo"    column="room_no"    />
        <result property="bedNum"    column="bed_num"    />
        <result property="occupancy"    column="occupancy"    />
        <result property="status"    column="status"    />
        <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="delFlag"    column="del_flag"    />
    </resultMap>

    <sql id="selectSrsDormitoryVo">
        select sd.id, sd.floor_no, sd.room_no, sd.bed_num, sd.occupancy, sd.status, sd.create_by, sd.create_time, sd.update_by, sd.update_time, sd.del_flag,cdb.`name` AS buildingNo
        from srs_dormitory sd
                 LEFT JOIN cph_dormitory_unit cdu ON sd.unit_id = cdu.id
                 LEFT JOIN cph_dormitory_building cdb ON cdb.id = cdu.building_id
    </sql>

    <select id="selectSrsDormitoryList" parameterType="SrsDormitory" resultMap="SrsDormitoryResult">
        <include refid="selectSrsDormitoryVo"/>
        <where>  
            <if test="floorNo != null "> and floor_no = #{floorNo}</if>
            <if test="roomNo != null "> and room_no = #{roomNo}</if>
            <if test="bedNum != null "> and bed_num = #{bedNum}</if>
            <if test="occupancy != null "> and occupancy = #{occupancy}</if>
            <if test="status != null "> and status = #{status}</if>
        </where>
    </select>

    <select id="selectSrsDormitoryBedNum" parameterType="SrsDormitory" resultMap="SrsDormitoryResult" >
        <include refid="selectSrsDormitoryVo"/>
        where building_no=#{buildingNo} and room_no=#{roomNo}
    </select>
    <select id="selectSrsDormitoryById" parameterType="Long" resultMap="SrsDormitoryResult">
        <include refid="selectSrsDormitoryVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertSrsDormitory" parameterType="SrsDormitory" useGeneratedKeys="true" keyProperty="id">
        insert into srs_dormitory
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="buildingNo != null and buildingNo != ''">building_no,</if>
            <if test="floorNo != null">floor_no,</if>
            <if test="roomNo != null">room_no,</if>
            <if test="bedNum != null">bed_num,</if>
            <if test="occupancy != null">occupancy,</if>
            <if test="status != null">status,</if>
            <if test="students != null">students,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="delFlag != null and delFlag != ''">del_flag,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="buildingNo != null and buildingNo != ''">#{buildingNo},</if>
            <if test="floorNo != null">#{floorNo},</if>
            <if test="roomNo != null">#{roomNo},</if>
            <if test="bedNum != null">#{bedNum},</if>
            <if test="occupancy != null">#{occupancy},</if>
            <if test="status != null">#{status},</if>
            <if test="students != null">#{students},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="delFlag != null and delFlag != ''">#{delFlag},</if>
         </trim>
    </insert>

    <update id="updateSrsDormitory" parameterType="SrsDormitory">
        update srs_dormitory
        <trim prefix="SET" suffixOverrides=",">
            <if test="buildingNo != null and buildingNo != ''">building_no = #{buildingNo},</if>
            <if test="floorNo != null">floor_no = #{floorNo},</if>
            <if test="roomNo != null">room_no = #{roomNo},</if>
            <if test="bedNum != null">bed_num = #{bedNum},</if>
            <if test="occupancy != null">occupancy = #{occupancy},</if>
            <if test="status != null">status = #{status},</if>
            <if test="students != null">students = #{students},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="delFlag != null and delFlag != ''">del_flag = #{delFlag},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteSrsDormitoryById" parameterType="Long">
        delete from srs_dormitory where id = #{id}
    </delete>

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