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

    <resultMap type="MessageContentUser" id="MessageContentUserResult">
        <result property="id" column="id"/>
        <result property="messageChatContentId" column="message_chat_content_id"/>
        <result property="receiveUserId" column="receive_user_id"/>
        <result property="readStatus" column="read_status"/>
        <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="selectMessageContentUserVo">
        select id,
               message_chat_content_id,
               receive_user_id,
               read_status,
               del_flag,
               create_by,
               create_time,
               update_by,
               update_time
        from message_content_user
    </sql>

    <resultMap id="MessageContentUserDtoResult" type="com.ruoyi.project.common.dto.MessageContentUserDto" extends="MessageContentUserResult">

    </resultMap>

    <select id="markUnReadMessage" parameterType="long" resultMap="MessageContentUserDtoResult" >
        select
            count(1) as un_read_message,
            mcu.receive_user_id,
            message_chat_id
        from message_content_user   mcu
                 INNER JOIN message_chat_content mcc on mcc.id=mcu.message_chat_content_id and mcc.del_flag='0'
                 INNER JOIN message_chat_group mcg on mcc.message_chat_group_id=mcg.id and mcg.del_flag='0'
        WHERE mcu.read_status='0'  and mcu.del_flag='0'
          AND   mcg.message_chat_id in
           <foreach collection="messageChatIds" item="messageChatId" index="index" open="(" close=")" separator=",">
                #{messageChatId}
           </foreach>
          AND mcu.receive_user_id IN
         <foreach collection="receiveUserIds" item="receiveUserId" index="index" open="(" close=")" separator=",">
             #{receiveUserId}
         </foreach>
        GROUP BY message_chat_id,receive_user_id

    </select>


    <select id="getUnReadMessageByUserIdAndChatRoom" parameterType="long" resultMap="MessageContentUserDtoResult">
        SELECT
            mcu.id
        from message_content_user  mcu
                 INNER JOIN message_chat_content  mcc on mcc.id=mcu.message_chat_content_id and mcc.del_flag='0'
                 INNER JOIN message_chat_group mcg on mcg.id=mcc.message_chat_group_id and mcg.del_flag='0'
        WHERE  mcu.read_status='0'
          and mcu.del_flag='0'
          and  mcu.receive_user_id= #{userId}
          AND mcg.message_chat_id = #{chatRoomId}

    </select>


</mapper>