--渠道返利逻辑
local Config = require("Config")
local InnerMsg = require("core.InnerMsg")
local ObjHuman = require("core.ObjHuman")
local RebateCfg = require("excel.rebate")
local CommonDB = require("common.CommonDB")
local Util = require("common.Util")
local MiddleManager = require("middle.MiddleManager")
local RoleDBLogic = require("role.RoleDBLogic")
local MailManager = require("mail.MailManager")
local MailExcel = require("excel.mail")
local Log = require("common.Log")

-- 返利邮件ID
local  REBATE_MAIL_ID = 7016

-- 获取玩家总充值金额
local function getRoleTotalRecharge(account)
    account = tostring(account)
    for _, v in ipairs(RebateCfg.rebate) do
        if v.account == account then
            return v.totalRecharge1
        end
    end
end

-- 检查玩家是否符合返利条件
local function check(channelId, account)
    local mainCfg = RebateCfg.main[1]
    if channelId ~= mainCfg.channelId then
        return false
    end

    if getRoleTotalRecharge(account) then
        return true
    end

    return false
end



----------------------------------------------------普通服-----------------------------------------------------
--计算返利道具数量
local function calcRebateItemCnt(account)
    local roleTotalRecharge = getRoleTotalRecharge(account)

    local rebateDays = RebateCfg.main[1].rebateDays

    return math.ceil(roleTotalRecharge / rebateDays)
end


--玩家登录时
function NS_RebateQuery(channelId, serverId, account)
    if not check(channelId, account) then
        return
    end


    -- 向跨服查询是否返利
    local tMsgData = InnerMsg.lw.LW_REBATE_QUERY
    tMsgData.nSrcServerID = Config.SVR_INDEX
    tMsgData.channelId = channelId
    tMsgData.serverId = serverId
    tMsgData.account = account

    InnerMsg.sendMsg(0, tMsgData)
end


--收到跨服通知给玩家返利
function NS_Rebate_OK(channelId, serverId, account)
    local uTag = RoleDBLogic.Generateuuid(channelId, account, serverId)

    local human = ObjHuman.onlineNewUniqueTag[uTag]

    if not human or not human.db or not human.db._id then
        local logInfo = string.format("channelId: %s, serverId: %s, account: %s, result: %s, time: %s",
            channelId, serverId, account, "defeat", os.time())

        Log.write(Log.LOGID_OSS_REBATE, logInfo)

        return
    end

    -- 发邮件
    local mailCfg = MailExcel.mail[REBATE_MAIL_ID]

    local itemId = RebateCfg.main[1].rebateItemId
    local itemCnt = calcRebateItemCnt(account)
    local itemList = { {itemId, itemCnt } }

    MailManager.add(MailManager.SYSTEM, human.db._id, mailCfg.title, mailCfg.content, itemList, mailCfg.senderName or "GM")
end


----------------------------------------------------------------------------------------------------------------






----------------------------------------------------跨服-----------------------------------------------------

local function getRebateData()
    return CommonDB.getValueByKey(CommonDB.KEY_REBATE_DATA)
end

local function updateRebateData(newData)
    CommonDB.updateValue(CommonDB.KEY_REBATE_DATA, newData)
end


-- 收到普通服查询是否给玩家返利
function CS_RebateQuery(msg)
    do return end -- 屏蔽返利功能
    local channelId = msg.channelId
    local serverId = msg.serverId
    local account = msg.account

    if not check(channelId, account) then
        return
    end

    local now = os.time()
    local rebateData = getRebateData()
    local mainCfg = RebateCfg.main[1]

    if rebateData and rebateData[account] then

        local roleRebateData = rebateData[account]

        --同账号值返一个区
        if roleRebateData.serverId ~= serverId then
            return
        end

        if roleRebateData.rebateDays >= mainCfg.rebateDays then
            return
        end

        -- 一天返一次
        if Util.isSameDayByTimes(roleRebateData.lastRebateTime, now) then
            return
        end
    end

    rebateData = rebateData or {}
    rebateData[account] = rebateData[account] or {}
    local roleRebateData = rebateData[account]

    roleRebateData.serverId = serverId
    roleRebateData.rebateDays = (roleRebateData.rebateDays or 0) + 1
    roleRebateData.lastRebateTime = now

    -- 更新数据库
    updateRebateData(rebateData)


    -- 通知游戏服当天可以返利
    local tMsgData = InnerMsg.wl.WL_REBATE_OK
    tMsgData.channelId = channelId
    tMsgData.serverId = serverId
    tMsgData.account = account
    local fd = MiddleManager.getFDBySvrIndex(msg.nSrcServerID)
    InnerMsg.sendMsg(fd, tMsgData)
end


----------------------------------------------------------------------------------------------------------------