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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.srs.dormitory.domain.DmsHolidayStayApplication">
        <id column="id" property="id" />
        <result column="stu_no" property="stuNo" />
        <result column="start_date" property="startDate" />
        <result column="end_date" property="endDate" />
        <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="getHolidayStayApplication" resultType="java.util.Map">
        SELECT apply.id,apply.stu_no stuNo,d.room_no roomNo,b.`name` buildingName,
               apply.reason,apply.create_time createTime,s.`name` stuName,apply.start_date startDate,apply.end_date endDate,
               app.fdy_status fdyStatus,app.fdy_comment fdyComment,app.work_status workStatus,
               app.work_comment workComment,app.final_status finalStatus,app.final_comment statusComment
        FROM dms_holiday_stay_application apply
                 LEFT JOIN dms_dormitory d ON d.id = apply.dorm_id
                 LEFT JOIN dms_dormitory_floor f ON f.id = d.floor_id
                 LEFT JOIN dms_dormitory_building b ON b.id = f.building_id
                 LEFT JOIN srs_student s ON s.stu_no = apply.stu_no
                 LEFT JOIN dms_approval_process app ON app.apply_type = 'STAY' AND app.apply_id = apply.id
        WHERE  apply.id = #{id}
    </select>
    <select id="getList" resultType="java.util.Map">
        SELECT a.id,s.stu_no stuNo,s.`name` stuName,a.reason,a.start_date startTime,a.end_date endTime,a.create_time createTime,a.create_by createBy,
        a.update_time updateTime,a.status,'STAY' AS applyType,
        a.update_by updateBy,bed.bed_code bedCode,
        d.room_no roomNo,building.`name` buildingName
        FROM `dms_holiday_stay_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.dorm_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="studentNo != null">and s.stu_no = #{studentNo}</if>
        </where>
        order by a.id desc
    </select>
    <select id="getMyApply" resultType="java.util.Map">
        SELECT apply.id,apply.stu_no stuNo,apply.start_date startDate,'STAY' AS applyType,
        apply.end_date endDate,apply.reason,apply.create_time createTime,
        apply.`status`,s.`name` stuName,d.room_no cRoomNo,b.`name` cBuilding
        FROM dms_holiday_stay_application apply
        LEFT JOIN srs_student s ON apply.stu_no = s.stu_no
        LEFT JOIN dms_dormitory d ON d.id = apply.dorm_id
        LEFT JOIN dms_dormitory_floor f ON f.id = d.floor_id
        LEFT JOIN dms_dormitory_building b ON b.id = f.building_id
        <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>
    <select id="workApply" resultType="java.util.Map">
        SELECT apply.id,apply.stu_no stuNo,apply.start_date startDate,'STAY' AS applyType,
        apply.end_date endDate,apply.reason,apply.create_time createTime,
        apply.`status`,s.`name` stuName,d.room_no cRoomNo,b.`name` cBuilding
        FROM `dms_holiday_stay_application` apply
        LEFT JOIN dms_approval_process app ON app.apply_type = 'STAY' AND app.apply_id = apply.id
        LEFT JOIN srs_student s ON apply.stu_no = s.stu_no
        LEFT JOIN dms_dormitory d ON d.id = apply.dorm_id
        LEFT JOIN dms_dormitory_floor f ON f.id = d.floor_id
        LEFT JOIN dms_dormitory_building b ON b.id = f.building_id
        <where>
            <if test="status == 0">and app.work_status = #{status}</if>
            <if test="status == 1">and app.work_status != 0</if>
            and apply.status != 0
        </where>
        ORDER BY apply.id DESC
    </select>
    <select id="finalApply" resultType="java.util.Map">
        SELECT apply.id,apply.stu_no stuNo,apply.start_date startDate,'STAY' AS applyType,
        apply.end_date endDate,apply.reason,apply.create_time createTime,
        apply.`status`,s.`name` stuName,d.room_no cRoomNo,b.`name` cBuilding
        FROM `dms_holiday_stay_application` apply
        LEFT JOIN dms_approval_process app ON app.apply_type = 'STAY' AND app.apply_id = apply.id
        LEFT JOIN srs_student s ON apply.stu_no = s.stu_no
        LEFT JOIN dms_dormitory d ON d.id = apply.dorm_id
        LEFT JOIN dms_dormitory_floor f ON f.id = d.floor_id
        LEFT JOIN dms_dormitory_building b ON b.id = f.building_id
        <where>
            <if test="status == 0">and app.work_status = #{status}</if>
            <if test="status == 1">and app.work_status != 0</if>
            and apply.status != 0
            and apply.status != 1
        </where>
        ORDER BY apply.id DESC
    </select>

</mapper>
