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

    <resultMap type="SrsStaffOneStopRegistrationRecord" id="SrsStaffOneStopRegistrationRecordResult">
        <result property="recordId" column="record_id"/>
        <result property="activityId" column="activity_id"/>
        <result property="activityTheme" column="activity_theme"/>
        <result property="initiator" column="initiator"/>
        <result property="applicant" column="applicant"/>
        <result property="registrationTime" column="registration_time"/>
        <result property="auditStatus" column="audit_status"/>
        <result property="jobNumber" column="job_number"/>
        <collection property="communityActivitiesList" column="{activityId = activity_id}"
                    javaType="java.util.ArrayList" select="selectActivitiesByActivityId">
        </collection>
    </resultMap>


    <resultMap type="SrsStaffOneStopCommunityActivities" id="SrsStaffOneStopCommunityActivitiesResult">
        <result property="activityId" column="activity_id"/>
        <result property="activityTheme" column="activity_theme"/>
        <result property="galleryful" column="galleryful"/>
        <result property="activityPlace" column="activity_place"/>
        <result property="publishStatus" column="publish_status"/>
        <result property="activityStatus" column="activity_status"/>
        <result property="specificRequirements" column="specific_requirements"/>
        <result property="signUpPrompt" column="sign_up_prompt"/>
        <result property="feedback" column="feedback"/>
        <result property="eventPromoter" column="event_promoter"/>
        <result property="feedbackImgs"    column="feedback_imgs"    />
    </resultMap>

    <!--报名活动父表的数据-->
    <select id="selectActivitiesByActivityId" resultMap="SrsStaffOneStopCommunityActivitiesResult">
        select *
        from srs_staff_one_stop_community_activities
        <where>
            <if test="activityId != null">
                and activity_id = #{activityId}
            </if>
        </where>
    </select>

    <sql id="selectSrsStaffOneStopRegistrationRecordVo">
        select record_id, activity_id, activity_theme,initiator, applicant, registration_time, audit_status
        from srs_staff_one_stop_registration_record
    </sql>

    <select id="selectRecordByActivityIdAndApplicant" parameterType="String" resultMap="SrsStaffOneStopRegistrationRecordResult">
        <include refid="selectSrsStaffOneStopRegistrationRecordVo"/>
        <where>
            <if test="activityId != null">
                and activity_id = #{activityId}
            </if>
            <if test="jobNumber != null and jobNumber != ''">
                and job_number = #{jobNumber}
            </if>
        </where>
    </select>

    <select id="selectSrsStaffOneStopRegistrationRecordList" parameterType="SrsStaffOneStopRegistrationRecord"
            resultMap="SrsStaffOneStopRegistrationRecordResult">
        <include refid="selectSrsStaffOneStopRegistrationRecordVo"/>
        <where>
            <if test="activityId != null "> and activity_id = #{activityId}</if>
            <if test="activityTheme != null  and activityTheme != ''"> and activity_theme like concat('%', #{activityTheme}, '%')</if>
            <if test="initiator != null  and initiator != ''"> and initiator = #{initiator}</if>
            <if test="applicant != null  and applicant != ''"> and applicant = #{applicant}</if>
            <if test="registrationTime != null "> and registration_time = #{registrationTime}</if>
            <if test="auditStatus != null "> and audit_status = #{auditStatus}</if>
            <if test="jobNumber != null  and jobNumber != ''"> and job_number = #{jobNumber}</if>
        </where>
    </select>

    <select id="selectSrsStaffOneStopRegistrationRecordByRecordId" parameterType="Long"
            resultMap="SrsStaffOneStopRegistrationRecordResult">
        <include refid="selectSrsStaffOneStopRegistrationRecordVo"/>
        where record_id = #{recordId}
    </select>

    <select id="selectSrsStaffOneStopRegistrationRecordByActivityId" parameterType="Long"
            resultMap="SrsStaffOneStopRegistrationRecordResult">
        <include refid="selectSrsStaffOneStopRegistrationRecordVo"/>
        where activity_id = #{activityId}
    </select>
    <select id="selectSrsStaffOneStopRegistrationRecordListexport"
            resultType="com.srs.staff.domain.vo.SrsStaffOneStopRegistrationRecordExportVo">
        SELECT
        activity_theme activityTheme,
        initiator,
        applicant,
        registration_time registrationTime,
        CASE
        WHEN audit_status = 0 THEN '待审核'
        WHEN audit_status = 1 THEN '同意'
        WHEN audit_status = 2 THEN '不同意'
        ELSE '未知状态'
        END AS auditStatusValue,
        job_number jobNumber
        FROM
        srs_staff_one_stop_registration_record
        <where>
            <if test="activityId != null "> and activity_id = #{activityId}</if>
            <if test="activityTheme != null  and activityTheme != ''"> and activity_theme like concat('%', #{activityTheme}, '%')</if>
            <if test="initiator != null  and initiator != ''"> and initiator = #{initiator}</if>
            <if test="applicant != null  and applicant != ''"> and applicant = #{applicant}</if>
            <if test="registrationTime != null "> and registration_time = #{registrationTime}</if>
            <if test="auditStatus != null "> and audit_status = #{auditStatus}</if>
            <if test="jobNumber != null  and jobNumber != ''"> and job_number = #{jobNumber}</if>
            <if test="recordIds != null and recordIds != ''">
                and record_id in
                <foreach collection="recordIds" item="recordId" open="(" separator="," close=")">
                    #{recordId}
                </foreach>
            </if>
        </where>
    </select>

    <!-- 校属各部门功能房预约的统计 (饼图) -->
    <select id="getDepartmentUsage" resultType="map">
        SELECT
        rt_depar AS department,
        COUNT(*) AS reservation_count,
        SUM(CAST(rt_people AS SIGNED)) AS total_visitors
        FROM
        staff_one_stop_room_reservation
        WHERE
        1 = 1
<!--        <if test="roomName != null and roomName != ''">-->
<!--            AND room_name = #{roomName}-->
<!--        </if>-->
        <if test="startTime != null and startTime != ''">
            AND rt_time &gt;= #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
            AND rt_time &lt;= #{endTime}
        </if>
        <if test="role != null and role != ''">
            AND rt_role = #{role}
        </if>
        <if test="borrower != null and borrower != ''">
            AND rt_creat_role LIKE CONCAT('%', #{borrower}, '%')
        </if>
        <if test="auditStatus != null">
            AND audit_status = #{auditStatus}
        </if>
        <if test="rtDepar != null and rtDepar.size() > 0">
            AND rt_depar IN
            <foreach collection="rtDepar" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>

        GROUP BY
        rt_depar
        ORDER BY
        reservation_count DESC
    </select>

    <!-- 校属各功能房预约的统计 (饼图) -->
    <select id="getRoomReservationCount" resultType="map">
        SELECT
        room_name AS roomName,
        COUNT(*) AS reservation_count,
        SUM(CAST(rt_people AS SIGNED)) AS total_visitors
        FROM
        staff_one_stop_room_reservation
        WHERE
        1 = 1
        <if test="startTime != null and startTime != ''">
            AND rt_time &gt;= #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
            AND rt_time &lt;= #{endTime}
        </if>
        <if test="role != null and role != ''">
            AND rt_role = #{role}
        </if>
        <if test="borrower != null and borrower != ''">
            AND rt_creat_role LIKE CONCAT('%', #{borrower}, '%')
        </if>
        <if test="auditStatus != null">
            AND audit_status = #{auditStatus}
        </if>
        <if test="rtDepar != null and rtDepar.size() > 0">
            AND rt_depar IN
            <foreach collection="rtDepar" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        GROUP BY
        room_name
        ORDER BY
        reservation_count DESC
    </select>

    <!-- 校属各部门功能房使用情况 (表格) -->
    <select id="getDepartmentUsageStatistics" resultType="map">
        SELECT
        rt_depar AS department,
        COUNT(*) AS reservation_count,
        SUM(CAST(rt_people AS SIGNED)) AS total_visitors
        FROM
        staff_one_stop_room_reservation
        WHERE
        1 = 1
        <if test="startTime != null and startTime != ''">
            AND rt_time &gt;= #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
            AND rt_time &lt;= #{endTime}
        </if>
        <if test="role != null and role != ''">
            AND rt_role = #{role}
        </if>
        <if test="borrower != null and borrower != ''">
            AND rt_creat_role LIKE CONCAT('%', #{borrower}, '%')
        </if>
        <if test="auditStatus != null">
            AND audit_status = #{auditStatus}
        </if>
        <if test="rtDepar != null and rtDepar.size() > 0">
            AND rt_depar IN
            <foreach collection="rtDepar" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        GROUP BY
        rt_depar
        ORDER BY
        reservation_count DESC
    </select>

    <!-- 校属各功能房使用情况 (表格) -->
    <select id="getRoomUsageStatistics" resultType="map">
        SELECT
        room_name AS roomName,
        COUNT(*) AS reservation_count,
        SUM(CAST(rt_people AS SIGNED)) AS total_visitors
        FROM
        staff_one_stop_room_reservation
        WHERE
        1 = 1
        <if test="startTime != null and startTime != ''">
            AND rt_time &gt;= #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
            AND rt_time &lt;= #{endTime}
        </if>
        <if test="role != null and role != ''">
            AND rt_role = #{role}
        </if>
        <if test="borrower != null and borrower != ''">
            AND rt_creat_role LIKE CONCAT('%', #{borrower}, '%')
        </if>
        <if test="auditStatus != null">
            AND audit_status = #{auditStatus}
        </if>
        <if test="rtDepar != null and rtDepar.size() > 0">
            AND rt_depar IN
            <foreach collection="rtDepar" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        GROUP BY
        room_name
        ORDER BY
        reservation_count DESC
    </select>
    <insert id="insertSrsStaffOneStopRegistrationRecord" parameterType="SrsStaffOneStopRegistrationRecord"
            useGeneratedKeys="true" keyProperty="recordId">
        insert into srs_staff_one_stop_registration_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="activityId != null">activity_id,</if>
            <if test="activityTheme != null">activity_theme,</if>
            <if test="initiator != null">initiator,</if>
            <if test="applicant != null">applicant,</if>
            <if test="registrationTime != null">registration_time,</if>
            <if test="auditStatus != null">audit_status,</if>
            <if test="jobNumber != null">job_number,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="activityId != null">#{activityId},</if>
            <if test="activityTheme != null">#{activityTheme},</if>
            <if test="initiator != null">#{initiator},</if>
            <if test="applicant != null">#{applicant},</if>
            <if test="registrationTime != null">#{registrationTime},</if>
            <if test="auditStatus != null">#{auditStatus},</if>
            <if test="jobNumber != null">#{jobNumber},</if>
        </trim>
    </insert>

    <update id="updateSrsStaffOneStopRegistrationRecord" parameterType="SrsStaffOneStopRegistrationRecord">
        update srs_staff_one_stop_registration_record
        <trim prefix="SET" suffixOverrides=",">
            <if test="activityId != null">activity_id = #{activityId},</if>
            <if test="activityTheme != null">activity_theme = #{activityTheme},</if>
            <if test="initiator != null">initiator = #{initiator},</if>
            <if test="applicant != null">applicant = #{applicant},</if>
            <if test="registrationTime != null">registration_time = #{registrationTime},</if>
            <if test="auditStatus != null">audit_status = #{auditStatus},</if>
            <if test="jobNumber != null">job_number = #{jobNumber},</if>
        </trim>
        where record_id = #{recordId}
    </update>

    <!--批量修改-->
    <update id="batchUpdateSysRegistrationRecord" parameterType="SrsStaffOneStopRegistrationRecord">
        <foreach collection="list" item="item" index="index" open="" close="" separator=";">
            update srs_staff_one_stop_registration_record
            <trim prefix="SET" suffixOverrides=",">
                <if test="item.activityId != null">activity_id = #{item.activityId},</if>
                <if test="item.activityTheme != null">activity_theme = #{item.activityTheme},</if>
                <if test="item.initiator != null">initiator = #{item.initiator},</if>
                <if test="item.applicant != null">applicant = #{item.applicant},</if>
                <if test="item.registrationTime != null">registration_time = #{item.registrationTime},</if>
                <if test="item.auditStatus != null">audit_status = #{item.auditStatus},</if>
                <if test="item.jobNumber != null">job_number = #{item.jobNumber},</if>
            </trim>
            where record_id = #{item.recordId}
        </foreach>
    </update>

    <update id="updateAuditStatusToPending" parameterType="int">
        UPDATE staff_one_stop_room_reservation
        SET audit_status = 0,rt_state = 0
        WHERE rt_id = #{id}
    </update>


    <delete id="deleteSrsStaffOneStopRegistrationRecordByRecordId" parameterType="Long">
        delete
        from srs_staff_one_stop_registration_record
        where record_id = #{recordId}
    </delete>

    <delete id="deleteSrsStaffOneStopRegistrationRecordByRecordIds" parameterType="String">
        delete from srs_staff_one_stop_registration_record where record_id in
        <foreach item="recordId" collection="array" open="(" separator="," close=")">
            #{recordId}
        </foreach>
    </delete>
</mapper>