<?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.ruoyi.project.common.mapper.MessageChatContentMapper">

    <resultMap type="MessageChatContent" id="MessageChatContentResult">
        <result property="id" column="id"/>
        <result property="messageChatGroupId" column="message_chat_group_id"/>
        <result property="sendUserId" column="send_user_id"/>
        <result property="messageType" column="message_type"/>
        <result property="messageContent" column="message_content"/>
        <result property="fileName" column="file_name"/>
        <result property="fileUploadName" column="file_upload_name"/>
        <result property="staticUrl" column="static_url"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="selectMessageChatContentVo">
        select id,
               message_chat_group_id,
               send_user_id,
               message_type,
               message_content,
               file_name,
               file_upload_name,
               static_url,
               del_flag,
               create_by,
               create_time,
               update_by,
               update_time
        from message_chat_content
    </sql>

    <select id="getMessageChatContentList" parameterType="long" resultMap="MessageChatContentDtoResult">
        SELECT *
        FROM (SELECT mcc.id,
                     mcc.send_user_id,
                     mcc.message_type,
                     mcc.message_content,
                     mcc.file_name,
                     mcc.static_url,
                     mcc.image_url,
                     mcc.create_by,
                     mcc.create_time,
                     su.avatar,
                     su.nick_name
              from message_chat_content mcc
                       INNER JOIN message_chat_group mcg on mcg.id = mcc.message_chat_group_id
                       LEFT JOIN sys_user su on su.user_id=mcc.send_user_id
              WHERE mcg.message_chat_id = #{chatRoomId}
                AND mcc.del_flag = '0'
                AND mcg.del_flag = '0'
              ORDER BY create_time DESC LIMIT 10) T
        ORDER BY create_time ASC
    </select>

    <select id="getMessageChatContentListPage" parameterType="long" resultMap="MessageChatContentDtoResult">
        SELECT *
        FROM (SELECT mcc.id,
                     mcc.send_user_id,
                     mcc.message_type,
                     mcc.message_content,
                     mcc.file_name,
                     mcc.static_url,
                     mcc.image_url,
                     mcc.create_by,
                     mcc.create_time,
                     su.avatar,
                     su.nick_name
              from message_chat_content mcc
                       INNER JOIN message_chat_group mcg on mcg.id = mcc.message_chat_group_id
                       LEFT JOIN sys_user su ON su.user_id = mcc.send_user_id
              WHERE mcg.message_chat_id = #{chatRoomId}
                AND mcc.del_flag = '0'
                AND mcg.del_flag = '0'
              ORDER BY create_time ASC ) T
        ORDER BY create_time desc
    </select>


    <resultMap id="MessageChatContentDtoResult" type="com.ruoyi.project.common.dto.MessageChatContentDto">
        <result column="read_status" property="readStatus"/>
        <result column="message_content_user_id" property="messageContentUserId"/>
        <result column="message_chat_id" property="messageChatId"/>
        <result column="receive_user_id" property="receiveUserId"/>
        <result column="avatar" property="avatar"/>
        <result column="nick_name" property="nickName"/>
    </resultMap>
    <select id="getContentByUserIds" parameterType="long" resultMap="MessageChatContentDtoResult">
    SELECT * FROM (
        SELECT
        mcc.create_time,
        mcc.send_user_id,
        mcc.message_type,
        mcc.message_content,
        mcc.file_name,
        mcc.file_upload_name,
        mcc.static_url,
        mcu.read_status,
        mcg.message_chat_id,
        mcu.id as message_content_user_id,
        mcu.receive_user_id,
        su.avatar
        FROM message_chat_content mcc
        INNER JOIN message_chat_group mcg on mcg.id=mcc.message_chat_group_id and mcg.del_flag='0'
        LEFT JOIN message_content_user mcu on mcu.message_chat_content_id=mcc.id and mcu.del_flag='0'
        LEFT JOIN sys_user su on su.user_id= mcc.send_user_id
        WHERE mcc.del_flag='0' and mcu.receive_user_id in
        <foreach collection="userIds" item="userId" open="(" separator="," close=")">
            #{userId}
        </foreach>

        UNION

        SELECT
        mcc.create_time,
        mcc.send_user_id,
        mcc.message_type,
        mcc.message_content,
        mcc.file_name,
        mcc.file_upload_name,
        mcc.static_url,
        mcu.read_status,
        mcg.message_chat_id,
        mcu.id as message_content_user_id,
        mcu.receive_user_id,
        su.avatar
        FROM message_chat_content mcc
        INNER JOIN message_chat_group mcg on mcg.id=mcc.message_chat_group_id and mcg.del_flag='0'
        INNER JOIN sys_user su on su.user_id= mcc.send_user_id
        LEFT JOIN message_content_user mcu on mcu.message_chat_content_id=mcc.id and mcu.del_flag='0' AND
        mcu.receive_user_id in
        <foreach collection="userIds" item="userId" open="(" separator="," close=")">
            #{userId}
        </foreach>
        WHERE mcc.del_flag='0' and mcc.send_user_id in
        <foreach collection="userIds" item="userId" open="(" separator="," close=")">
            #{userId}
        </foreach>
        ) T
     ORDER BY create_time

    </select>


</mapper>