<?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.wecloud.im.thousandchat.mapper.ImMsgReadLastestMapper">

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, create_time, update_time, read_time, receive_time, fk_appid, fk_client_id, fk_conversation_id, fk_read_msg_id, fk_receive_msg_id
    </sql>
    <update id="upsertImMsgReceive">
        insert into im_msg_read_lastest(id, create_time, read_time, receive_time, fk_appid, fk_client_id, fk_conversation_id, fk_read_msg_id, fk_receive_msg_id)
        values(#{id}, NOW(), NOW(), NOW(), #{fkAppid}, #{fkClientId}, #{fkConversationId}, #{fkReadMsgId}, #{fkReceiveMsgId})
            ON DUPLICATE KEY UPDATE
                <if test="fkReadMsgId">
                 read_time = if(fk_read_msg_id is null or fk_read_msg_id &lt; #{fkReadMsgId}, NOW(), read_time),
                 fk_read_msg_id = if(fk_read_msg_id is null or fk_read_msg_id &lt; #{fkReadMsgId}, #{fkReadMsgId}, fk_read_msg_id),
                </if>
                 receive_time = if(fk_receive_msg_id is null or fk_receive_msg_id &lt; #{fkReceiveMsgId}, NOW(), receive_time),
                 fk_receive_msg_id = if(fk_receive_msg_id is null or fk_receive_msg_id &lt; #{fkReceiveMsgId}, #{fkReceiveMsgId}, fk_receive_msg_id)
    </update>

    <update id="upsertImMsgRead">
        insert into im_msg_read_lastest(id, create_time, read_time, fk_appid, fk_client_id, fk_conversation_id, fk_read_msg_id)
        values(#{id}, NOW(), NOW(), #{fkAppid}, #{fkClientId}, #{fkConversationId}, #{fkReadMsgId})
            ON DUPLICATE KEY UPDATE
                 read_time = if(fk_read_msg_id is null or fk_read_msg_id &lt; #{fkReadMsgId}, NOW(), read_time),
                 fk_read_msg_id = if(fk_read_msg_id is null or fk_read_msg_id &lt; #{fkReadMsgId}, #{fkReadMsgId}, fk_read_msg_id)
    </update>

    <select id="findLastestReadMsgs" resultType="com.wecloud.im.thousandchat.param.ConversationLastestReadMsg">
        select fk_conversation_id as conversationId, fk_read_msg_id as lastestReadMsgId, fk_receive_msg_id as lastestReceiveMsgId
        from  im_msg_read_lastest
        where fk_client_id = #{clientId}
    </select>

    <select id="findOfflineMsgs" resultType="com.wecloud.im.thousandchat.param.ThousandOffineMsgDto">
        <foreach collection="lastestMsgs" item="lastestMsg" separator=" union all ">
        select msg.id              AS msgId,
               msg.create_time,
               msg.withdraw_time,
               msg.update_date,
               client.client_id    AS sender,
               msg.content,
               msg.withdraw,
               msg.`event`,
               msg.system_flag,
               msg.`at`,
               msg.send_status,
               msg.fk_conversation_id as conversationId,
               (SELECT COUNT(a.id) FROM im_message a WHERE a.fk_conversation_id = msg.fk_conversation_id and a.id > #{lastestMsg.lastestReadMsgId})
                                                                                                AS not_read_count,
               (SELECT COUNT(b.id) FROM im_message b WHERE b.fk_conversation_id = msg.fk_conversation_id and b.id >= msg.id)
                                                                                                AS not_receiver_count
        from  im_message msg INNER JOIN `im_client` client ON client.id = msg.sender
        where
            (msg.fk_conversation_id = #{lastestMsg.conversationId} and msg.id > #{lastestMsg.lastestReceiveMsgId} )
        </foreach>


    </select>

</mapper>
