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

    <resultMap id="BaseResultMap" type="org.xyzh.api.workcase.dto.TbChatRoomMemberDTO">
        <id column="member_id" property="memberId" jdbcType="VARCHAR"/>
        <result column="optsn" property="optsn" jdbcType="VARCHAR"/>
        <result column="room_id" property="roomId" 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="status" property="status" jdbcType="VARCHAR"/>
        <result column="unread_count" property="unreadCount" jdbcType="INTEGER"/>
        <result column="last_read_time" property="lastReadTime" jdbcType="TIMESTAMP"/>
        <result column="last_read_msg_id" property="lastReadMsgId" jdbcType="VARCHAR"/>
        <result column="join_time" property="joinTime" jdbcType="TIMESTAMP"/>
        <result column="leave_time" property="leaveTime" jdbcType="TIMESTAMP"/>
        <result column="creator" property="creator" 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.ChatMemberVO">
        <id column="member_id" property="memberId" jdbcType="VARCHAR"/>
        <result column="optsn" property="optsn" jdbcType="VARCHAR"/>
        <result column="room_id" property="roomId" 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="status" property="status" jdbcType="VARCHAR"/>
        <result column="unread_count" property="unreadCount" jdbcType="INTEGER"/>
        <result column="last_read_time" property="lastReadTime" jdbcType="TIMESTAMP"/>
        <result column="last_read_msg_id" property="lastReadMsgId" jdbcType="VARCHAR"/>
        <result column="join_time" property="joinTime" jdbcType="TIMESTAMP"/>
        <result column="leave_time" property="leaveTime" jdbcType="TIMESTAMP"/>
        <result column="creator" property="creator" jdbcType="VARCHAR"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
    </resultMap>

    <sql id="Base_Column_List">
        member_id, optsn, room_id, user_id, user_type, user_name, status,
        unread_count, last_read_time, last_read_msg_id, join_time, leave_time,
        creator, create_time, update_time
    </sql>

    <insert id="insertChatRoomMember" parameterType="org.xyzh.api.workcase.dto.TbChatRoomMemberDTO">
        INSERT INTO workcase.tb_chat_room_member (
            optsn, member_id, room_id, user_id, user_type, user_name, creator
            <if test="status != null">, status</if>
            <if test="unreadCount != null">, unread_count</if>
        ) VALUES (
            #{optsn}, #{memberId}, #{roomId}, #{userId}, #{userType}, #{userName}, #{creator}
            <if test="status != null">, #{status}</if>
            <if test="unreadCount != null">, #{unreadCount}</if>
        )
    </insert>

    <update id="updateChatRoomMember" parameterType="org.xyzh.api.workcase.dto.TbChatRoomMemberDTO">
        UPDATE workcase.tb_chat_room_member
        <set>
            <if test="status != null and status != ''">status = #{status},</if>
            <if test="unreadCount != null">unread_count = #{unreadCount},</if>
            <if test="lastReadTime != null">last_read_time = #{lastReadTime},</if>
            <if test="lastReadMsgId != null">last_read_msg_id = #{lastReadMsgId},</if>
            <if test="leaveTime != null">leave_time = #{leaveTime},</if>
            update_time = now()
        </set>
        WHERE member_id = #{memberId}
    </update>

    <delete id="deleteChatRoomMember" parameterType="org.xyzh.api.workcase.dto.TbChatRoomMemberDTO">
        DELETE FROM workcase.tb_chat_room_member
        WHERE member_id = #{memberId}
    </delete>

    <select id="selectChatRoomMemberById" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM workcase.tb_chat_room_member
        WHERE member_id = #{memberId}
    </select>

    <select id="selectChatRoomMemberList" resultMap="VOResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM workcase.tb_chat_room_member
        <where>
            <if test="filter.memberId != null and filter.memberId != ''">AND member_id = #{filter.memberId}</if>
            <if test="filter.roomId != null and filter.roomId != ''">AND room_id = #{filter.roomId}</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.status != null and filter.status != ''">AND status = #{filter.status}</if>
        </where>
        ORDER BY join_time DESC
    </select>

    <select id="selectChatRoomMemberPage" resultMap="VOResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM workcase.tb_chat_room_member
        <where>
            <if test="filter.memberId != null and filter.memberId != ''">AND member_id = #{filter.memberId}</if>
            <if test="filter.roomId != null and filter.roomId != ''">AND room_id = #{filter.roomId}</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.status != null and filter.status != ''">AND status = #{filter.status}</if>
        </where>
        ORDER BY join_time DESC
        LIMIT #{pageParam.pageSize} OFFSET #{pageParam.offset}
    </select>

    <select id="countChatRoomMembers" resultType="long">
        SELECT COUNT(*)
        FROM workcase.tb_chat_room_member
        <where>
            <if test="filter.memberId != null and filter.memberId != ''">AND member_id = #{filter.memberId}</if>
            <if test="filter.roomId != null and filter.roomId != ''">AND room_id = #{filter.roomId}</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.status != null and filter.status != ''">AND status = #{filter.status}</if>
        </where>
    </select>

</mapper>
