local LuaMongo = _G.lua_mongo
local DB = require("common.DB")
local ChatRecord = require("chat.ChatRecord")

local QueryChatRecordByUuid = {}

-- 好友私聊消息写入数据库
function addFriendChatRecord(uuid,fUuid,chatItem)
    QueryChatRecordByUuid = {}
    QueryChatRecordByUuid.uuid = uuid
    QueryChatRecordByUuid.fUuid = fUuid

    local recordData = {}
    LuaMongo.find(DB.db_chat_record, QueryChatRecordByUuid)
	if not LuaMongo.next(recordData) then
        local chatRecordTb = {}
        chatRecordTb.uuid = uuid
        chatRecordTb.fUuid = fUuid
        chatRecordTb.chatRecord = {}
        table.insert(chatRecordTb.chatRecord,chatItem)

        LuaMongo.insert(DB.db_chat_record, chatRecordTb)
    else
        table.insert(recordData.chatRecord,chatItem)
        local len = #recordData.chatRecord
        if len > ChatRecord.CHAT_RECORD_CNT then
            table.remove(recordData.chatRecord,1)
        end

        LuaMongo.update(DB.db_chat_record, QueryChatRecordByUuid, recordData)   
	end
end

-- 取出聊天记录
function getFriendChatRecord(uuid,fUuid)
    QueryChatRecordByUuid = {}
    QueryChatRecordByUuid.uuid = uuid
    QueryChatRecordByUuid.fUuid = fUuid

    local recordData = {}
    LuaMongo.find(DB.db_chat_record, QueryChatRecordByUuid)
	if not LuaMongo.next(recordData) then
        return nil
    end
    return recordData
end

-- 删除聊天记录
function delFriendChatRecord(uuid,fUuid)
    QueryChatRecordByUuid = {}
    QueryChatRecordByUuid.uuid = uuid
    QueryChatRecordByUuid.fUuid = fUuid

    LuaMongo.remove(DB.db_chat_record, QueryChatRecordByUuid)
end