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

    <resultMap type="DmsManageApplication" id="DmsManageApplicationResult">
        <result property="id"    column="id"    />
        <result property="stuNo"    column="stu_no"    />
        <result property="time"    column="time"    />
        <result property="currentDormId"    column="current_dorm_id"    />
        <result property="targetDormId"    column="target_dorm_id"    />
        <result property="reason"    column="reason"    />
        <result property="type"    column="type"    />
        <result property="status"    column="status"    />
        <result property="createTime"    column="create_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="startTime"    column="start_time"    />
        <result property="endTime"    column="end_time"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="stuName" column="stu_name"/>
        <result property="currRoomNo" column="cRoomNo"/>
        <result property="currBuilding" column="cBuilding"/>
        <result property="tarRoomNo" column="tRoomNo"/>
        <result property="tarBuilding" column="tBuilding"/>
        <result property="tarBuildingId" column="tBuildingId"/>
        <result property="currBuildingId" column="cBuildingId"/>
    </resultMap>

    <resultMap type="DomInfo" id="StudentDormitoryInfoResult">
        <result property="stuNo" column="stu_no"/>
        <result property="stuName" column="stu_name"/>
        <result property="gender" column="gender"/>
        <result property="buildingName" column="building_name"/>
        <result property="roomNo" column="room_no"/>
        <result property="dormitoryId" column="dormitory_id"/>
    </resultMap>

    <sql id="selectDmsManageApplicationVo">
        select id, stu_no, time, current_dorm_id, target_dorm_id, reason, type, status, create_time, create_by, update_time, update_by, start_time, end_time, del_flag from dms_manage_application
    </sql>

    <select id="selectDmsManageApplicationList" parameterType="DmsManageApplication" resultMap="DmsManageApplicationResult">
        SELECT
        a.*,
        s.`name` stu_name,
        p.fdy_no,
        p.fdy_status,
        p.fdy_time,
        p.work_no,
        p.work_status,
        p.work_time,
        p.final_no,
        p.final_status,
        p.final_time
        FROM
        dms_manage_application a
        LEFT JOIN srs_student s ON a.stu_no = s.stu_no
        LEFT JOIN dms_approval_process p ON p.apply_id = a.id
        left join srs_class as c on s.class_id = c.class_id
        left join cph_teacher as d on c.teacher_id = d.teacher_id
        left join srs_majors as e on c.major_id = e.major_id
        <where>
            <if test="stuNo != null  and stuNo != ''"> and a.stu_no = #{stuNo}</if>
            <if test="time != null "> and time = #{time}</if>
            <if test="currentDormId != null "> and current_dorm_id = #{currentDormId}</if>
            <if test="targetDormId != null "> and target_dorm_id = #{targetDormId}</if>
            <if test="reason != null  and reason != ''"> and reason = #{reason}</if>
            <if test="type != null "> and type = #{type}</if>
            <if test="status != null "> and a.status = #{status}</if>
            <if test="startTime != null "> and start_time = #{startTime}</if>
            <if test="endTime != null "> and end_time = #{endTime}</if>
            <if test="fdyNo != null "> and d.employee_id = #{fdyNo}</if>
            <if test="deptId != null "> and e.college_id = #{deptId}</if>
        </where>
        ORDER BY a.create_time desc
    </select>

    <select id="selectDmsManageApplicationById" parameterType="Long" resultMap="DmsManageApplicationResult">
        SELECT
            a.*,
            s.`name` stu_name,
            p.fdy_no,
            p.fdy_status,
            p.fdy_time,
            p.work_no,
            p.work_status,
            p.work_time,
            p.final_no,
            p.final_status,
            p.final_time,
            cdor.room_no cRoomNo,
            cb.`name` cBuilding,
            cb.id cBuildingId,
            tdor.room_no tRoomNo,
            tb.`name` tBuilding,
            tb.id tBuildingId,
            s.gender
        FROM
            dms_manage_application a
                LEFT JOIN srs_student s ON a.stu_no = s.stu_no
                LEFT JOIN dms_approval_process p ON p.apply_id = a.id
                LEFT JOIN dms_dormitory cdor ON cdor.id = a.current_dorm_id
                LEFT JOIN dms_dormitory_floor cf ON cf.id = cdor.floor_id
                LEFT JOIN dms_dormitory_building cb ON cb.id = cf.building_id
                LEFT JOIN dms_dormitory tdor ON tdor.id = a.target_dorm_id
                LEFT JOIN dms_dormitory_floor tf ON tf.id = tdor.floor_id
                LEFT JOIN dms_dormitory_building tb ON tb.id = tf.building_id
        where a.id = #{id}
    </select>

    <select id="selectStudentDormitoryInfoByStuNo" parameterType="String" resultMap="StudentDormitoryInfoResult">
        SELECT 
            s.stu_no,
            s.name AS stu_name,
            s.gender,
            db.name AS building_name,
            d.room_no,
            d.id AS dormitory_id
        FROM srs_student s
        LEFT JOIN srs_dormitory_student ds ON s.stu_no = ds.stu_no
        LEFT JOIN dms_dormitory d ON ds.dormitory_id = d.id
        LEFT JOIN dms_dormitory_floor df ON d.floor_id = df.id
        LEFT JOIN dms_dormitory_building db ON df.building_id = db.id
        WHERE s.stu_no = #{stuNo}
        LIMIT 1
    </select>

    <insert id="insertDmsManageApplication" parameterType="DmsManageApplication"
    useGeneratedKeys="true" keyProperty="id">
        insert into dms_manage_application
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="stuNo != null and stuNo != ''">stu_no,</if>
            <if test="time != null">time,</if>
            <if test="currentDormId != null">current_dorm_id,</if>
            <if test="targetDormId != null">target_dorm_id,</if>
            <if test="reason != null">reason,</if>
            <if test="type != null">type,</if>
            <if test="status != null">status,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="startTime != null">start_time,</if>
            <if test="endTime != null">end_time,</if>
            <if test="delFlag != null">del_flag,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="stuNo != null and stuNo != ''">#{stuNo},</if>
            <if test="time != null">#{time},</if>
            <if test="currentDormId != null">#{currentDormId},</if>
            <if test="targetDormId != null">#{targetDormId},</if>
            <if test="reason != null">#{reason},</if>
            <if test="type != null">#{type},</if>
            <if test="status != null">#{status},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="startTime != null">#{startTime},</if>
            <if test="endTime != null">#{endTime},</if>
            <if test="delFlag != null">#{delFlag},</if>
        </trim>
    </insert>

    <update id="updateDmsManageApplication" parameterType="DmsManageApplication">
        update dms_manage_application
        <trim prefix="SET" suffixOverrides=",">
            <if test="stuNo != null and stuNo != ''">stu_no = #{stuNo},</if>
            <if test="time != null">time = #{time},</if>
            <if test="currentDormId != null">current_dorm_id = #{currentDormId},</if>
            <if test="targetDormId != null">target_dorm_id = #{targetDormId},</if>
            <if test="reason != null">reason = #{reason},</if>
            <if test="type != null">type = #{type},</if>
            <if test="status != null">status = #{status},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="startTime != null">start_time = #{startTime},</if>
            <if test="endTime != null">end_time = #{endTime},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteDmsManageApplicationById" parameterType="Long">
        delete from dms_manage_application where id = #{id}
    </delete>

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


    <select id="listView" parameterType="DmsSearch" resultType="DomInfo">
        select a.id,a.stu_no,a.type,a.`time`,
               concat(c.park_name,' ',c.building_name,' ',c.room_no) as old_dorm,
               concat(d.park_name,' ',d.building_name,' ',d.room_no) as new_dorm,
               e.stu_name,e.dept_name,e.class_name,a.`status`,
               (
                   select count(t1.id) from dms_new_task as t1 where t1.stu_no = a.stu_no and t1.task_status =2
               ) as task_num,
                a.reason
        from dms_manage_application as a
                 left join dms_approval_process as b on a.id = b.apply_id
                 left join view_dorm_info as c on a.current_dorm_id = c.id
                 left join view_dorm_info as d on a.target_dorm_id = d.id
                 left join view_stu_info as e on a.stu_no = e.stu_no

        <where>
            <if test="stuNo != null  and stuNo != ''"> and a.stu_no = #{stuNo}</if>
            <if test="stuName != null  and stuName != ''"> and e.stu_name like concat('%',#{stuName},'%')</if>
            <if test="time != null "> and a.`time` = #{time}</if>
            <if test="type != null "> and a.`type` = #{type}</if>
            <if test="status != null "> and a.`status` = #{status}</if>
            <if test="deptId != null "> and e.dept_id = #{deptId}</if>
        </where>
        order by a.id desc
    </select>
    <select id="selectDmsManageApplicationByStuNO" resultType="com.srs.dormitory.domain.DmsManageApplication">
        SELECT
            a.*,
            s.`name` stu_name,
            p.fdy_no,
            p.fdy_status,
            p.fdy_time,
            p.work_no,
            p.work_status,
            p.work_time,
            p.final_no,
            p.final_status,
            p.final_time,
            cdor.room_no cRoomNo,
            cb.`name` cBuilding,
            cb.id cBuildingId,
            tdor.room_no tRoomNo,
            tb.`name` tBuilding,
            tb.id tBuildingId,
            s.gender
        FROM
            dms_manage_application a
                LEFT JOIN srs_student s ON a.stu_no = s.stu_no
                LEFT JOIN dms_approval_process p ON p.apply_id = a.id
                LEFT JOIN dms_dormitory cdor ON cdor.id = a.current_dorm_id
                LEFT JOIN dms_dormitory_floor cf ON cf.id = cdor.floor_id
                LEFT JOIN dms_dormitory_building cb ON cb.id = cf.building_id
                LEFT JOIN dms_dormitory tdor ON tdor.id = a.target_dorm_id
                LEFT JOIN dms_dormitory_floor tf ON tf.id = tdor.floor_id
                LEFT JOIN dms_dormitory_building tb ON tb.id = tf.building_id
        where s.stu_no = #{stuNo}
    </select>


</mapper>