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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.srs.dormitory.domain.DmsDelayApplication">
        <id column="id" property="id" />
        <result column="stu_no" property="stuNo" />
        <result column="dormitory_id" property="dormitoryId" />
        <result column="bed_id" property="bedId" />
        <result column="delay_time" property="delayTime" />
        <result column="reason" property="reason" />
        <result column="create_time" property="createTime" />
        <result column="create_by" property="createBy" />
        <result column="update_time" property="updateTime" />
        <result column="update_by" property="updateBy" />
        <result column="status" property="status" />
        <result column="del_flag" property="delFlag" />
    </resultMap>
    <select id="getDelayApplicationList" resultType="java.util.Map">
        SELECT a.id,s.stu_no stuNo,s.`name` stuName,a.reason,a.delay_time delayTime,a.create_time createTime,a.create_by createBy,
               a.update_time updateTime,
               a.update_by updateBy,bed.bed_code bedCode,
               d.room_no roomNo,building.`name` buildingName
        FROM `dms_delay_application` a
                 LEFT JOIN srs_student s ON a.stu_no = s.stu_no
                 LEFT JOIN dms_dormitory_bed bed ON a.bed_id = bed.id
                 LEFT JOIN dms_dormitory d ON a.dormitory_id = d.id
                 LEFT JOIN dms_dormitory_floor f ON d.floor_id = f.id
                 LEFT JOIN dms_dormitory_building building ON building.id = f.building_id
        <where>
            <if test="buildingId != null">and building.id = #{buildingId}</if>
            <if test="floorId != null">and f.id = #{floorId}</if>
            <if test="dormitoryId != null">and d.id = #{dormitoryId}</if>
            <if test="studentId != null">and s.stu_id = #{studentId}</if>
        </where>
    </select>
    <select id="getDelayApplicationInfo" resultType="java.util.Map">
        SELECT a.id,s.`name` stuName,a.reason,a.delay_time delayTime,a.create_time createTime,a.create_by createBy,
               a.update_time updateTime,
               a.update_by updateBy,bed.bed_code bedCode,
               d.room_no roomNo,building.`name` buildingName
        FROM `dms_delay_application` a
                 LEFT JOIN srs_student s ON a.stu_no = s.stu_no
                 LEFT JOIN dms_dormitory_bed bed ON a.bed_id = bed.id
                 LEFT JOIN dms_dormitory d ON a.dormitory_id = d.id
                 LEFT JOIN dms_dormitory_floor f ON f.id = d.floor_id
                 LEFT JOIN dms_dormitory_building building ON building.id = f.building_id
        where a.id = #{id}
    </select>
    <select id="getMyApply" resultType="java.util.Map">
        SELECT apply.id,apply.stu_no stuNo,apply.create_time createTime,
               s.`name` stuName,apply.`status`
        FROM dms_delay_application apply
        LEFT JOIN srs_student s ON apply.stu_no = s.stu_no
        <where>
            <if test="status == 0">and apply.status = #{status}</if>
            <if test="status == 1">and apply.status != 0</if>
        </where>
        ORDER BY apply.id DESC
    </select>
</mapper>
