<?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="org.xyzh.workcase.mapper.TbMeetingParticipantMapper">

    <resultMap id="BaseResultMap" type="org.xyzh.api.workcase.dto.TbMeetingParticipantDTO">
        <id column="participant_id" property="participantId" jdbcType="VARCHAR"/>
        <result column="optsn" property="optsn" jdbcType="VARCHAR"/>
        <result column="meeting_id" property="meetingId" jdbcType="VARCHAR"/>
        <result column="user_id" property="userId" jdbcType="VARCHAR"/>
        <result column="user_type" property="userType" jdbcType="VARCHAR"/>
        <result column="user_name" property="userName" jdbcType="VARCHAR"/>
        <result column="join_time" property="joinTime" jdbcType="TIMESTAMP"/>
        <result column="leave_time" property="leaveTime" jdbcType="TIMESTAMP"/>
        <result column="duration_seconds" property="durationSeconds" jdbcType="INTEGER"/>
        <result column="is_moderator" property="isModerator" jdbcType="BOOLEAN"/>
        <result column="join_method" property="joinMethod" jdbcType="VARCHAR"/>
        <result column="device_info" property="deviceInfo" jdbcType="VARCHAR"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
    </resultMap>

    <resultMap id="VOResultMap" type="org.xyzh.api.workcase.vo.MeetingParticipantVO">
        <id column="participant_id" property="participantId" jdbcType="VARCHAR"/>
        <result column="optsn" property="optsn" jdbcType="VARCHAR"/>
        <result column="meeting_id" property="meetingId" jdbcType="VARCHAR"/>
        <result column="user_id" property="userId" jdbcType="VARCHAR"/>
        <result column="user_type" property="userType" jdbcType="VARCHAR"/>
        <result column="user_name" property="userName" jdbcType="VARCHAR"/>
        <result column="join_time" property="joinTime" jdbcType="TIMESTAMP"/>
        <result column="leave_time" property="leaveTime" jdbcType="TIMESTAMP"/>
        <result column="duration_seconds" property="durationSeconds" jdbcType="INTEGER"/>
        <result column="is_moderator" property="isModerator" jdbcType="BOOLEAN"/>
        <result column="join_method" property="joinMethod" jdbcType="VARCHAR"/>
        <result column="device_info" property="deviceInfo" jdbcType="VARCHAR"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
    </resultMap>

    <sql id="Base_Column_List">
        participant_id, optsn, meeting_id, user_id, user_type, user_name, join_time, leave_time,
        duration_seconds, is_moderator, join_method, device_info, create_time, update_time
    </sql>

    <insert id="insertMeetingParticipant" parameterType="org.xyzh.api.workcase.dto.TbMeetingParticipantDTO">
        INSERT INTO workcase.tb_meeting_participant (
            optsn, participant_id, meeting_id, user_id, user_type, user_name
            <if test="isModerator != null">, is_moderator</if>
            <if test="joinMethod != null">, join_method</if>
            <if test="deviceInfo != null">, device_info</if>
        ) VALUES (
            #{optsn}, #{participantId}, #{meetingId}, #{userId}, #{userType}, #{userName}
            <if test="isModerator != null">, #{isModerator}</if>
            <if test="joinMethod != null">, #{joinMethod}</if>
            <if test="deviceInfo != null">, #{deviceInfo}</if>
        )
    </insert>

    <update id="updateMeetingParticipant" parameterType="org.xyzh.api.workcase.dto.TbMeetingParticipantDTO">
        UPDATE workcase.tb_meeting_participant
        <set>
            <if test="leaveTime != null">leave_time = #{leaveTime},</if>
            <if test="durationSeconds != null">duration_seconds = #{durationSeconds},</if>
            <if test="isModerator != null">is_moderator = #{isModerator},</if>
            <if test="deviceInfo != null">device_info = #{deviceInfo},</if>
            update_time = now()
        </set>
        WHERE participant_id = #{participantId}
    </update>

    <delete id="deleteMeetingParticipant" parameterType="org.xyzh.api.workcase.dto.TbMeetingParticipantDTO">
        DELETE FROM workcase.tb_meeting_participant
        WHERE participant_id = #{participantId}
    </delete>

    <select id="selectMeetingParticipantById" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM workcase.tb_meeting_participant
        WHERE participant_id = #{participantId}
    </select>

    <select id="selectMeetingParticipantList" resultMap="VOResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM workcase.tb_meeting_participant
        <where>
            <if test="filter.participantId != null and filter.participantId != ''">AND participant_id = #{filter.participantId}</if>
            <if test="filter.meetingId != null and filter.meetingId != ''">AND meeting_id = #{filter.meetingId}</if>
            <if test="filter.userId != null and filter.userId != ''">AND user_id = #{filter.userId}</if>
            <if test="filter.userType != null and filter.userType != ''">AND user_type = #{filter.userType}</if>
            <if test="filter.userName != null and filter.userName != ''">AND user_name LIKE CONCAT('%', #{filter.userName}, '%')</if>
            <if test="filter.isModerator != null">AND is_moderator = #{filter.isModerator}</if>
            <if test="filter.joinMethod != null and filter.joinMethod != ''">AND join_method = #{filter.joinMethod}</if>
        </where>
        ORDER BY join_time DESC
    </select>

    <select id="selectMeetingParticipantPage" resultMap="VOResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM workcase.tb_meeting_participant
        <where>
            <if test="filter.participantId != null and filter.participantId != ''">AND participant_id = #{filter.participantId}</if>
            <if test="filter.meetingId != null and filter.meetingId != ''">AND meeting_id = #{filter.meetingId}</if>
            <if test="filter.userId != null and filter.userId != ''">AND user_id = #{filter.userId}</if>
            <if test="filter.userType != null and filter.userType != ''">AND user_type = #{filter.userType}</if>
            <if test="filter.userName != null and filter.userName != ''">AND user_name LIKE CONCAT('%', #{filter.userName}, '%')</if>
            <if test="filter.isModerator != null">AND is_moderator = #{filter.isModerator}</if>
            <if test="filter.joinMethod != null and filter.joinMethod != ''">AND join_method = #{filter.joinMethod}</if>
        </where>
        ORDER BY join_time DESC
        LIMIT #{pageParam.pageSize} OFFSET #{pageParam.offset}
    </select>

    <select id="countMeetingParticipants" resultType="long">
        SELECT COUNT(*)
        FROM workcase.tb_meeting_participant
        <where>
            <if test="filter.participantId != null and filter.participantId != ''">AND participant_id = #{filter.participantId}</if>
            <if test="filter.meetingId != null and filter.meetingId != ''">AND meeting_id = #{filter.meetingId}</if>
            <if test="filter.userId != null and filter.userId != ''">AND user_id = #{filter.userId}</if>
            <if test="filter.userType != null and filter.userType != ''">AND user_type = #{filter.userType}</if>
            <if test="filter.userName != null and filter.userName != ''">AND user_name LIKE CONCAT('%', #{filter.userName}, '%')</if>
            <if test="filter.isModerator != null">AND is_moderator = #{filter.isModerator}</if>
            <if test="filter.joinMethod != null and filter.joinMethod != ''">AND join_method = #{filter.joinMethod}</if>
        </where>
    </select>

</mapper>
