<?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.staff.mapper.StaffOneStopRoomReservationMapper">

    <resultMap type="StaffOneStopRoomReservation" id="StaffOneStopRoomReservationResult">
        <result property="rtId" column="rt_id"/>
        <result property="rtNo" column="rt_no"/>
        <result property="roomNo" column="room_no"/>
        <result property="roomName" column="room_name"/>
        <result property="rtTheme" column="rt_theme"/>
        <result property="rtRole" column="rt_role"/>
        <result property="rtPeople" column="rt_people"/>
        <result property="rtPurpose" column="rt_purpose"/>
        <result property="rtTime" column="rt_time"/>
        <result property="rtTimePeriod" column="rt_time_period"/>
        <result property="rtState" column="rt_state"/>
        <result property="rtImgs" column="rt_imgs"/>
        <result property="rtCreatTime" column="rt_creat_time"/>
        <result property="rtUpTime" column="rt_up_time"/>
        <result property="rtCreatRole" column="rt_creat_role"/>
        <result property="rtDepar" column="rt_depar"/>
        <result property="auditStatus" column="audit_status"/>
        <result property="editOpinion" column="edit_opinion"/>
        <result property="feedback" column="feedback"/>
        <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="remark"    column="remark"    />
        <!--查询功能房的开放时间-->
        <collection property="staffOneStopRoomOpeningHoursList" column="{rtNo = rt_no}"
                    javaType="java.util.ArrayList" select="selectStaffOneStopRoomOpeningHoursByRoomNo">
        </collection>
        <!--查询功能房-->
        <collection property="staffOneStopRoom" column="{roomNo = room_no}"
                    javaType="java.util.ArrayList" select="selectStaffOneStopRoomByRoomNo">
        </collection>
    </resultMap>


    <!--功能房开放时间-->
    <resultMap type="StaffOneStopRoomOpeningHours" id="StaffOneStopRoomOpeningHoursResult">
        <result property="ohId" column="oh_id"/>
        <result property="roomNo" column="room_no"/>
        <result property="ohWeekday" column="oh_weekday"/>
        <result property="openingHours" column="opening_hours"/>
        <result property="isOccupy" column="is_occupy"/>
        <result property="isDel" column="is_del"/>
        <result property="rtNo" column="rt_no"/>
    </resultMap>

    <!--功能房开放时间字段-->
    <sql id="selectStaffOneStopRoomOpeningHoursVo">
        select oh_id, room_no, oh_weekday, opening_hours, is_occupy, is_del, rt_no
        from staff_one_stop_room_opening_hours
    </sql>

    <!--功能房开放时间子表的数据-->
    <select id="selectStaffOneStopRoomOpeningHoursByRoomNo" resultMap="StaffOneStopRoomOpeningHoursResult">
        <include refid="selectStaffOneStopRoomOpeningHoursVo"/>
        <where>
            <if test="rtNo != null and rtNo != ''">
                and rt_no = #{rtNo}
            </if>
        </where>
    </select>

    <!--功能房-->
    <resultMap type="StaffOneStopRoom" id="StaffOneStopRoomResult">
        <result property="roomId" column="room_id"/>
        <result property="roomNo" column="room_no"/>
        <result property="roomName" column="room_name"/>
        <result property="roomMatter" column="room_matter"/>
        <result property="roomRule" column="room_rule"/>
        <result property="roomInfo" column="room_info"/>
        <result property="roomPurp" column="room_purp"/>
        <result property="roomStatus" column="room_status"/>
        <result property="roomCapacity" column="room_capacity"/>
        <result property="roomCreatetime" column="room_createTime"/>
        <result property="roomUptime" column="room_upTime"/>
        <result property="roomUpuser" column="room_upuser"/>
        <result property="roomUser" column="room_user"/>
        <result property="roomDepar" column="room_depar"/>
        <result property="roomImgs" column="room_imgs"/>
    </resultMap>
    <!--功能房-->
    <sql id="selectStaffOneStopRoomVo">
        select room_id,
               room_no,
               room_name,
               room_matter,
               room_rule,
               room_info,
               room_purp,
               room_status,
               room_capacity,
               room_createTime,
               room_upTime,
               room_upuser,
               room_user,
               room_depar,
               room_imgs
        from staff_one_stop_room
    </sql>

    <select id="selectStaffOneStopRoomByRoomNo" resultMap="StaffOneStopRoomResult">
        <include refid="selectStaffOneStopRoomVo"/>
        <where>
            <if test="roomNo != null and roomNo != ''">
                and room_no = #{roomNo}
            </if>
        </where>
    </select>


    <sql id="selectStaffOneStopRoomReservationVo">
        select rt_id,
               rt_no,
               room_no,
               room_name,
               rt_theme,
               rt_role,
               rt_people,
               rt_purpose,
               rt_time,
               rt_time_period,
               rt_state,
               rt_imgs,
               rt_creat_time,
               rt_up_time,
               rt_creat_role,
               rt_depar,
               audit_status,
               edit_opinion,
               feedback
        from staff_one_stop_room_reservation
    </sql>

    <select id="selectStaffOneStopRoomReservationList" parameterType="StaffOneStopRoomReservation"
            resultMap="StaffOneStopRoomReservationResult">
        <include refid="selectStaffOneStopRoomReservationVo"/>
        <where>
            <!-- 原有条件 -->
            <if test="rtNo != null  and rtNo != ''">and rt_no = #{rtNo}</if>
            <if test="roomNo != null  and roomNo != ''">and room_no = #{roomNo}</if>
            <if test="rtTheme != null  and rtTheme != ''">and rt_theme = #{rtTheme}</if>
            <if test="rtPeople != null  and rtPeople != ''">and rt_people = #{rtPeople}</if>
            <if test="rtPurpose != null  and rtPurpose != ''">and rt_purpose = #{rtPurpose}</if>
            <if test="rtImgs != null  and rtImgs != ''">and rt_imgs = #{rtImgs}</if>
            <if test="rtCreatTime != null ">and rt_creat_time = #{rtCreatTime}</if>
            <if test="rtUpTime != null ">and rt_up_time = #{rtUpTime}</if>
            <if test="auditStatus != null ">and audit_status = #{auditStatus}</if>
            <if test="editOpinion != null  and editOpinion != ''">and edit_opinion = #{editOpinion}</if>
            <if test="feedback != null  and feedback != ''">and feedback = #{feedback}</if>
            <if test="createBy != null and createBy != ''">and create_by = #{createBy}</if>
            <if test="updateBy != null and updateBy != ''">and update_by = #{updateBy}</if>
            <!-- 新增查询条件 -->
            <!-- 根据功能房名模糊查询 -->
            <if test="roomName != null and roomName != ''">
                and room_name like concat('%', #{roomName}, '%')
            </if>

            <!-- 根据部门模糊查询 -->
            <if test="rtDepar != null and rtDepar != ''">
                and rt_depar like concat('%', #{rtDepar}, '%')
            </if>

            <!-- 根据预约时间段查询 -->
            <if test="rtTimeStart != null and rtTimeStart != ''">
                <![CDATA[ and rt_time >= #{rtTimeStart} ]]>
            </if>
            <if test="rtTimeEnd != null and rtTimeEnd != ''">
                <![CDATA[ and rt_time <= #{rtTimeEnd} ]]>
            </if>

            <!-- 根据参与人员类型查询 -->
            <if test="rtRole != null and rtRole != ''">
                and rt_role = #{rtRole}
            </if>

            <!-- 根据申请人模糊查询 -->
            <if test="rtCreatRole != null and rtCreatRole != ''">
                and rt_creat_role like concat('%', #{rtCreatRole}, '%')
            </if>

            <!-- 根据预约状态查询 -->
            <if test="rtState != null">
                and rt_state = #{rtState}
            </if>

        </where>
        order by
        CASE audit_status
        WHEN 0 THEN 0
        WHEN 1 THEN 1
        WHEN 2 THEN 2
        ELSE 99
        END,
        rt_creat_time DESC
    </select>

    <select id="selectStaffOneStopRoomReservationByRtTime"
            resultMap="StaffOneStopRoomReservationResult">
        <include refid="selectStaffOneStopRoomReservationVo"/>
        <where>
            <if test="roomNo != null and roomNo != ''">
                and room_no = #{roomNo}
            </if>
            <if test="rtTime != null and rtTime != ''">
                and rt_time = #{rtTime}
            </if>
        </where>
    </select>

    <select id="selectStaffOneStopRoomReservationByRtId" parameterType="Long"
            resultMap="StaffOneStopRoomReservationResult">
        <include refid="selectStaffOneStopRoomReservationVo"/>
        where rt_id = #{rtId}
    </select>

    <insert id="insertStaffOneStopRoomReservation" parameterType="StaffOneStopRoomReservation" useGeneratedKeys="true"
            keyProperty="rtId">
        insert into staff_one_stop_room_reservation
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="rtNo != null">rt_no,</if>
            <if test="roomNo != null">room_no,</if>
            <if test="roomName != null">room_name,</if>
            <if test="rtTheme != null">rt_theme,</if>
            <if test="rtRole != null">rt_role,</if>
            <if test="rtPeople != null">rt_people,</if>
            <if test="rtPurpose != null">rt_purpose,</if>
            <if test="rtTime != null">rt_time,</if>
            <if test="rtTimePeriod != null">rt_time_period,</if>
            <if test="rtState != null">rt_state,</if>
            <if test="rtImgs != null">rt_imgs,</if>
            <if test="rtCreatTime != null">rt_creat_time,</if>
            <if test="rtUpTime != null">rt_up_time,</if>
            <if test="rtCreatRole != null">rt_creat_role,</if>
            <if test="rtDepar != null">rt_depar,</if>
            <if test="auditStatus != null">audit_status,</if>
            <if test="editOpinion != null">edit_opinion,</if>
            <if test="feedback != null">feedback,</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="remark != null">remark,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="rtNo != null">#{rtNo},</if>
            <if test="roomNo != null">#{roomNo},</if>
            <if test="roomName != null">#{roomName},</if>
            <if test="rtTheme != null">#{rtTheme},</if>
            <if test="rtRole != null">#{rtRole},</if>
            <if test="rtPeople != null">#{rtPeople},</if>
            <if test="rtPurpose != null">#{rtPurpose},</if>
            <if test="rtTime != null">#{rtTime},</if>
            <if test="rtTimePeriod != null">#{rtTimePeriod},</if>
            <if test="rtState != null">#{rtState},</if>
            <if test="rtImgs != null">#{rtImgs},</if>
            <if test="rtCreatTime != null">#{rtCreatTime},</if>
            <if test="rtUpTime != null">#{rtUpTime},</if>
            <if test="rtCreatRole != null">#{rtCreatRole},</if>
            <if test="rtDepar != null">#{rtDepar},</if>
            <if test="auditStatus != null">#{auditStatus},</if>
            <if test="editOpinion != null">#{editOpinion},</if>
            <if test="feedback != null">#{feedback},</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="remark != null">#{remark},</if>
        </trim>
    </insert>

    <update id="updateStaffOneStopRoomReservation" parameterType="StaffOneStopRoomReservation">
        update staff_one_stop_room_reservation
        <trim prefix="SET" suffixOverrides=",">
            <if test="rtNo != null">rt_no = #{rtNo},</if>
            <if test="roomNo != null">room_no = #{roomNo},</if>
            <if test="roomName != null">room_name = #{roomName},</if>
            <if test="rtTheme != null">rt_theme = #{rtTheme},</if>
            <if test="rtRole != null">rt_role = #{rtRole},</if>
            <if test="rtPeople != null">rt_people = #{rtPeople},</if>
            <if test="rtPurpose != null">rt_purpose = #{rtPurpose},</if>
            <if test="rtTime != null">rt_time = #{rtTime},</if>
            <if test="rtTimePeriod != null">rt_time_period = #{rtTimePeriod},</if>
            <if test="rtState != null">rt_state = #{rtState},</if>
            <if test="rtImgs != null">rt_imgs = #{rtImgs},</if>
            <if test="rtCreatTime != null">rt_creat_time = #{rtCreatTime},</if>
            <if test="rtUpTime != null">rt_up_time = #{rtUpTime},</if>
            <if test="rtCreatRole != null">rt_creat_role = #{rtCreatRole},</if>
            <if test="rtDepar != null">rt_depar = #{rtDepar},</if>
            <if test="auditStatus != null">audit_status = #{auditStatus},</if>
            <if test="editOpinion != null">edit_opinion = #{editOpinion},</if>
            <if test="feedback != null">feedback = #{feedback},</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="remark != null">remark = #{remark},</if>
        </trim>
        where rt_id = #{rtId}
    </update>

    <delete id="deleteStaffOneStopRoomReservationByRtId" parameterType="Long">
        delete
        from staff_one_stop_room_reservation
        where rt_id = #{rtId}
    </delete>

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