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

    <resultMap type="MessageChat" id="MessageChatResult">
        <result property="id" column="id"/>
        <result property="chatType" column="chat_type"/>
        <result property="chatName" column="chat_name"/>
        <result property="quotationId" column="quotation_id"/>
        <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="selectMessageChatVo">
        select id,
               chat_type,
               chat_name,
               quotation_id,
               del_flag,
               create_by,
               create_time,
               update_by,
               update_time
        from message_chat
    </sql>

    <resultMap id="MessageChatDtoResult" extends="MessageChatResult" type="com.ruoyi.project.common.dto.MessageChatDto">
        <result property="active" column="active"/>
        <result property="sort" column="sort"/>
        <result property="connectId" column="connect_id"/>
        <result property="chatUserId" column="user_id"/>
        <result property="messageChatUserId" column="message_chat_user_id"/>
    </resultMap>


    <select id="getChatRoomByUserIds" resultMap="MessageChatDtoResult">
        SELECT
        mc.chat_type,
        mc.chat_name,
        mc.id,
        mcu.id as message_chat_user_id,
        mcu.user_id,
        mc.quotation_id,
        mcu.active,
        mcu.connect_id,
        mcu.sort
        FROM
        message_chat_user mcu
        INNER JOIN message_chat mc ON mcu.message_chat_id = mc.id
        where mc.del_flag='0' and mcu.del_flag='0'
        <if test="chatName !=null and chatName !=''">
            and mc.chat_name like concat('%', #{chatName}, '%')
        </if>
        <if test="quotationId!=null">
            and mc.quotation_id=#{quotationId}
        </if>
        AND mcu.user_id in
        <foreach collection="userIds" item="userId" open="(" separator="," close=")">
            #{userId}
        </foreach>
    </select>

</mapper>