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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.srs.dormitory.domain.DmsTransferApplication">
        <id column="id" property="id" />
        <result column="stu_no" property="stuNo" />
        <result column="current_dorm_id" property="currentDormId" />
        <result column="current_bed_id" property="currentBedId" />
        <result column="target_dorm_id" property="targetDormId" />
        <result column="target_bed_id" property="targetBedId" />
        <result column="reason" property="reason" />
        <result column="create_time" property="createTime" />
        <result column="status" property="status" />
        <result column="del_flag" property="delFlag" />
    </resultMap>
    <select id="selectTransferApplicationList" resultType="java.util.Map">
        SELECT a.id,a.reason,a.create_time createTime,a.create_by createBy,
               a.update_time updateTime,a.update_by updateBy,a.`status`,s.`name` stuName,s.stu_no stuNo,
               cbed.bed_code currentBed,cd.room_no currentDor,cb.`name` currentBuilding,
               tbed.bed_code targetBed,td.room_no targetDor,tb.`name` targetBuilding
        FROM `dms_transfer_application` a
                 LEFT JOIN srs_student s ON s.stu_no = a.stu_no
                 LEFT JOIN dms_dormitory_bed cbed ON a.current_bed_id = cbed.id
                 LEFT JOIN dms_dormitory cd ON cd.id = a.current_dorm_id
                 LEFT JOIN dms_dormitory_floor cf ON cf.id = cd.floor_id
                 LEFT JOIN dms_dormitory_building cb ON cb.id = cf.building_id
                 LEFT JOIN dms_dormitory_bed tbed ON a.target_bed_id = tbed.id
                 LEFT JOIN dms_dormitory td ON td.id = a.target_dorm_id
                 LEFT JOIN dms_dormitory_floor tf ON tf.id = td.floor_id
                 LEFT JOIN dms_dormitory_building tb ON tb.id = tf.building_id
        <where>
            <if test="buildingId != null">and tb.id = #{buildingId}</if>
            <if test="floorId != null">and tf.id = #{floorId}</if>
            <if test="dormitoryId != null">and td.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,cd.room_no cRoomNo,cb.`name` cBuilding,
        td.room_no tRoomNo,tb.`name` tBuilding,apply.reason,apply.`status`,'TSSQB' AS applyType,
        s.`name` stuName,apply.create_time createTime
        FROM `dms_transfer_application` apply
        LEFT JOIN srs_student s ON apply.stu_no = s.stu_no
        LEFT JOIN dms_dormitory cd ON apply.current_dorm_id = cd.id
        LEFT JOIN dms_dormitory_floor cf ON cf.id = cd.floor_id
        LEFT JOIN dms_dormitory_building cb ON cb.id = cf.building_id
        LEFT JOIN dms_dormitory td ON td.id = apply.target_dorm_id
        LEFT JOIN dms_dormitory_floor tf ON tf.id = td.floor_id
        LEFT JOIN dms_dormitory_building tb ON tb.id = tf.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,cd.room_no cRoomNo,cb.`name` cBuilding,
               td.room_no tRoomNo,tb.`name` tBuilding,apply.reason,apply.`status`,
               s.`name` stuName,apply.create_time createTime,'TSSQB' AS applyType
        FROM `dms_transfer_application` apply
                 LEFT JOIN dms_approval_process app ON app.apply_type = 'TSSQB' AND app.apply_id = apply.id
                 LEFT JOIN srs_student s ON apply.stu_no = s.stu_no
                 LEFT JOIN dms_dormitory cd ON apply.current_dorm_id = cd.id
                 LEFT JOIN dms_dormitory_floor cf ON cf.id = cd.floor_id
                 LEFT JOIN dms_dormitory_building cb ON cb.id = cf.building_id
                 LEFT JOIN dms_dormitory td ON td.id = apply.target_dorm_id
                 LEFT JOIN dms_dormitory_floor tf ON tf.id = td.floor_id
                 LEFT JOIN dms_dormitory_building tb ON tb.id = tf.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="selectApplyInfo" resultType="java.util.Map">
        SELECT apply.id,apply.stu_no stuNo,cd.room_no cRoomNo,apply.create_time createTime,
               cb.`name` cBuilding,td.room_no tRoomNo,tb.`name` tBuilding,s.name stuName,apply.reason,
               app.fdy_status fdyStatus,app.fdy_comment fdyComment,app.wordk_status workStatus,
               app.work_comment workComment,app.final_status finalStatus,app.final_comment statusComment,apply.time
        FROM dms_transfer_application apply
                 LEFT JOIN srs_student s ON s.stu_no = apply.stu_no
                 LEFT JOIN dms_dormitory cd ON cd.id = apply.current_dorm_id
                 LEFT JOIN dms_dormitory_floor cf ON cf.id = cd.floor_id
                 LEFT JOIN dms_dormitory_building cb ON cb.id = cf.building_id
                 LEFT JOIN dms_dormitory td ON td.id = apply.target_dorm_id
                 LEFT JOIN dms_dormitory_floor tf ON tf.id = td.floor_id
                 LEFT JOIN dms_dormitory_building tb ON tb.id = tf.building_id
                 LEFT JOIN dms_approval_process app ON app.apply_type = 'TSSQB' AND app.apply_id = apply.id
        where apply.id = #{id}
    </select>
    <select id="finalApply" resultType="java.util.Map">
        SELECT apply.id,apply.stu_no stuNo,cd.room_no cRoomNo,cb.`name` cBuilding,
        td.room_no tRoomNo,tb.`name` tBuilding,apply.reason,apply.`status`,
        s.`name` stuName,apply.create_time createTime,'TSSQB' AS applyType
        FROM `dms_transfer_application` apply
        LEFT JOIN dms_approval_process app ON app.apply_type = 'TSSQB' AND app.apply_id = apply.id
        LEFT JOIN srs_student s ON apply.stu_no = s.stu_no
        LEFT JOIN dms_dormitory cd ON apply.current_dorm_id = cd.id
        LEFT JOIN dms_dormitory_floor cf ON cf.id = cd.floor_id
        LEFT JOIN dms_dormitory_building cb ON cb.id = cf.building_id
        LEFT JOIN dms_dormitory td ON td.id = apply.target_dorm_id
        LEFT JOIN dms_dormitory_floor tf ON tf.id = td.floor_id
        LEFT JOIN dms_dormitory_building tb ON tb.id = tf.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>
