<?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="cn.reghao.tnb.content.app.chat.db.mapper.ChatRecordMapper">
    <insert id="save" useGeneratedKeys="true" keyProperty="id" keyColumn="id">
        insert into chat_chat_record
        (`record_id`,`chat_id`,`chat_type`,`sender_id`,`receiver_id`,`msg_type`,`read`,`revoke`)
        values 
        (#{recordId},#{chatId},#{chatType},#{senderId},#{receiverId},#{msgType},#{read},#{revoke})
    </insert>

    <update id="updateSetRevoke">
        update chat_chat_record
        set `revoke`=1
        where id=#{recordId}
    </update>

    <select id="findChatRecordGetRet" resultType="cn.reghao.tnb.content.app.chat.model.dto.chat.ChatRecordGetRet">
        select chatRecord.record_id, chatRecord.chat_type as talkType,chatRecord.receiver_id,chatRecord.read as isRead,chatRecord.revoke as isRevoke,
        chatRecord.msg_type,date_format(chatRecord.create_time,'%Y-%m-%d %H:%i:%s') as createdAt,
        chatRecord.sender_id as user_id
        from chat_chat_record chatRecord
        <!--and chatRecord.revoke is false-->
        where chatRecord.chat_id=(select chat_id from chat_dialog where receiver_id=#{receiverId} and user_id=#{userId}) and chatRecord.record_id > #{lastRecordId}
        order by chatRecord.create_time desc
        limit #{limit}
    </select>
    <select id="findChatRecordGetRetByMsgType" resultType="cn.reghao.tnb.content.app.chat.model.dto.chat.ChatRecordGetRet">
        select chatRecord.record_id, chatRecord.chat_type as talkType,chatRecord.receiver_id,chatRecord.read as isRead,chatRecord.revoke as isRevoke,
        chatRecord.msg_type,date_format(chatRecord.create_time,'%Y-%m-%d %H:%i:%s') as createdAt,
        chatRecord.sender_id as user_id
        from chat_chat_record chatRecord
        <!--and chatRecord.revoke is false-->
        where chatRecord.msg_type=#{msgType}
        and chatRecord.chat_id=(select chat_id from chat_dialog where receiver_id=#{receiverId} and user_id=#{userId}) and chatRecord.record_id > #{lastRecordId}
        order by chatRecord.create_time desc
        limit #{limit}
    </select>
    <select id="findByRecordId" resultType="cn.reghao.tnb.content.app.chat.model.po.chat.ChatRecord">
        select * from chat_chat_record where record_id=#{recordId}
    </select>
</mapper>
