--------------------------------
-- 文件名       ：  ServerCommerceActManger.lua
-- 文件说明     ：  跨服商业-活动模板管理
-- 创建时间     :   2025/03/26
-- 创建人       :   FC
--------------------------------

local Util = require("common.Util")
local Lang = require("common.Lang")
local Broadcast = require("broadcast.Broadcast")
local MailExcel = require("excel.mail")
local Msg = require("core.Msg")
local ObjHuman = require("core.ObjHuman")
local CommonDB = require("common.CommonDB")
local Log = require("common.Log")
local YunYingLogic = require("yunying.YunYingLogic")
local ServerCommerceCof = require("excel.ServerCommerce")
local ServerCommerceActDefine = require("serverCommerce.ServerCommerceActDefine")
local Timer = require("core.Timer")
local BuyConf = require("excel.buy")
local ServerCommerceMiddle = require("serverCommerce.ServerCommerceMiddle")
local ServerCommerceActCharge = require("serverCommerce.ServerCommerceActCharge")
local ServerCommerceActRank = require("serverCommerce.ServerCommerceActRank")
local ServerCommerceTask = require("serverCommerce.ServerCommerceActTask")
local Config = require("Config")
local CombatPosLogic = require("combat.CombatPosLogic")
local CombatDefine = require("combat.CombatDefine")

-- 活动信息
tCommerceActInfo = nil
-- {
--     nStartTime = nil,       -- 开始时间
--     nEendTime = nil,        -- 结束时间
--     isRun = nil,            -- 是否在活动中
--     nOpen = nil,            -- 是否打开
--     nBatchID = nil，        -- 批次
-- }

-- 加载的模块
tCommerceActModuel = {}

-- 当前的操作类型
local nQueryOperate = nil

----------------------------------------- 内部处理开始 -------------------------------------
-- 下发数据
local function CommerceAct_SendData(tMsgData, fd)
    Msg.send(tMsgData, fd)
end

local function CommerceAct_CheckOpenDay()
    local nNowOpenDay = CommonDB.getServerOpenDay()
    return nNowOpenDay >= ServerCommerceActDefine.COMMERCEACT_SHOW_OPENDAY
end

-- 创建玩家DB数据
function CommerceAct_CreateHumanDB(human)
    local nBatchID = 1
    local tCommonDBData = CommonDB.GetCommerceActInfo()
    if tCommonDBData and nil ~= _G.next(tCommonDBData) then
        nBatchID = tCommonDBData.nBatchID
    end
    
    human.db.ServerCommerce = 
    {
        nPoint = 0,         -- 当前玩家个人积分
        Task = {},          -- 任务信息
        Charge = {},        -- 连充豪礼
        Shop = {},          -- 战区钜惠
        nBatchID = nBatchID,       -- 批次
        bSendPlayerMail = false,        -- 是否发送个人邮件
        bSendServerMail = false,        -- 是否发送全服邮件
        battleGround = {
            freeChallengeCnt = ServerCommerceActDefine.COMMERCEACT_DAILY_FREECHALLENGETIMES,
            resetTime = os.time(),
        },
    }

    for nID, module in pairs(tCommerceActModuel) do
        if module and module.CreatDB then
            local bRet = module.CreatDB(human)
            if false == bRet then
                print("[CommerceAct_CreateHumanDB] nID 初始化DB 数据失败 nID = "..nID)
            end
        end
    end
end

-- 下发活动数据
function CommerceAct_SendActInfo(human)
    if not human or not tCommerceActInfo then
        return
    end

    if false == CommerceAct_CheckOpenDay() then
        return
    end

    print("[CommerceAct_SendActInfo] 下发活动数据 开始 ")

    local tMsgData = Msg.gc.GC_SERVEERCOMMERCE_ACT_ALLINFO
    tMsgData.nStartTime = tCommerceActInfo.nStartTime
    tMsgData.nEendTime = tCommerceActInfo.nEendTime

    tMsgData.tActID[0] = #ServerCommerceCof.CommerceAct

    local nIndex = 1
    for id, v in pairs(ServerCommerceCof.CommerceAct) do
        local tActData = tMsgData.tActID[nIndex]
        nIndex = nIndex + 1
        tActData.ID = id
        tActData.name = v.name
        tActData.nSortID = v.sortID
        tActData.nIcon = v.icon
        tActData.nPanelID = v.panelID
        local bRed = false
        if tCommerceActModuel[id] and tCommerceActModuel[id].isRed then
            bRed = tCommerceActModuel[id].isRed(human)
        end
        
        print("[CommerceAct_SendActInfo] name = "..tActData.name)
        tActData.nRed = (bRed == true) and 1 or 0
    end

    YunYingLogic.sendBanner(human)
    CommerceAct_SendData(tMsgData, human.fd)

    print("[CommerceAct_SendActInfo] 下发活动数据 结束 ")
end


-- 各个子活动重置活动数据
local function CommerceAct_ResetData(human)
    if not human then
        return
    end
    
end

-- 各个子活动处理结束数据
local function CommerceAct_HandleEndData(human)
    if not human then
        return
    end


end

-- 所有活动初始化数据开始
local function CommerceAct_BeginAllAct(human)
    if not human or not tCommerceActInfo then
        return
    end

    CommerceAct_CreateHumanDB(human)

    print("[CommerceAct_BeginAllAct] 所有活动初始化数据开始 ")
end

-- 结束所有活动
local function CommerceAct_EndAllAct(human)
    -- 各个子活动处理数据
    CommerceAct_HandleEndData(human)
end

-- 活动开始
local function CommerceAct_Begin()
    if not tCommerceActInfo then
        return
    end

    for nID, module in pairs(tCommerceActModuel) do
        if module and module.ClearCache then
            module.ClearCache()
        end
    end

    -- 遍历在线玩家
    for uuid, human in pairs(ObjHuman.onlineUuid) do
        CommerceAct_BeginAllAct(human)
        CommerceAct_SendActInfo(human)
    end
end

-- 活动结束
function CommerceAct_End()
    -- 排行榜结束
    ServerCommerceMiddle.CommerceMiddle_QueryServerRank(ServerCommerceActDefine.COMMERCEACT_SENDSERVERMAIL)

    -- 处理巅峰战场活动
    local ServerCommerceActBattleGroundNS = require("serverCommerce.ServerCommerceActBattleGroundNS")
    ServerCommerceActBattleGroundNS.Act_End()
end

-- 延迟向中心服请求数据
local function CommerceAct_LaterTimeQuery(nOperate)
    print("[CommerceAct_LaterTimeQuery] 延迟向中心服请求数据开始")
    Timer.addLater(ServerCommerceActDefine.COMMERCEACT_INITSERVERTIME, CommerceAct_GetActOpen, nOperate)
end

-- 延迟10分钟开始
local function CommerceAct_LaterBeginAct()
    if not tCommerceActInfo then
        return 
    end

    tCommerceActInfo.isRun = true
    CommerceAct_Begin()
end

-- 检查活动是否过期
local function CommerceAct_CheckActIsOverTime()
    local tCommerceDBInfo = CommonDB.GetCommerceActInfo()
    if not tCommerceDBInfo or nil == _G.next(tCommerceDBInfo) then
        return true
    end

    local nEndTime = tCommerceDBInfo.nEndTime
    local nNowTime = os.time()

    return nNowTime >= nEndTime
end

-- 检查是否开启下一轮活动
local function CommerceAct_CanOpenNext()
    local tCommerceDBInfo = CommonDB.GetCommerceActInfo()
    if not tCommerceDBInfo or nil == _G.next(tCommerceDBInfo) or not tCommerceDBInfo.nEndTime then
        print("[CommerceAct_CanOpenNext] 不存在对应的DB数据")
        return true
    end

    local nDiffDay = Util.diffDay(tCommerceDBInfo.nEndTime)
    if nDiffDay > ServerCommerceActDefine.COMMERCEACT_NEXTDAY  then
        print("[CommerceAct_CanOpenNext] 间隔天数已经满足条件 nDiffDay = "..nDiffDay)
        return true
    end
    
    return false
end

-- 创建通用DB数据
local function CommerceAct_CreateCommonDB()
    local nNowTime = os.time()
    local tCommerceInfo = CommonDB.GetCommerceActInfo()
    local nEndTime = nNowTime + (ServerCommerceActDefine.COMMERCEACT_LASTDAY - 1)* 86400
    local tEndDate = os.date("*t",nEndTime)
    tEndDate.hour = ServerCommerceActDefine.COMMERCEACT_ENDTIME
    tEndDate.min = 0
    tEndDate.sec = 0
    nEndTime = os.time(tEndDate)

    local tDBData = {
        nBeginTime = nNowTime,
        nEndTime = nEndTime,
        nPoint = 0,
        nSendRankMail = 0,
    }

    if not tCommerceInfo or nil == _G.next(tCommerceInfo) then
        print("[CommerceAct_CreateCommonDB] 不存在DB数据 批次为1")
        tDBData.nBatchID = 1
    else
        if tCommerceInfo.nBatchID then
            tDBData.nBatchID = tCommerceInfo.nBatchID + 1
            print("[CommerceAct_CreateCommonDB] 存在DB数据 批次为加1 nOldBatchID = "..tCommerceInfo.nBatchID.." nNewBatchID = "..tDBData.nBatchID)
        else
            tDBData.nBatchID = 1
        end
    end

    CommonDB.SetCommerceActInfo(tDBData)
end

-- 创建缓存数据
local function CommerceAct_CreateCacheInfo(nOpen, bLater)
    local tCommerceInfo = CommonDB.GetCommerceActInfo()
    if not tCommerceInfo or nil == _G.next(tCommerceInfo)  then
        print("[CommerceAct_CreateCacheInfo] 为什么会不存在数据\n")
        return
    end

    tCommerceActInfo = {}
    tCommerceActInfo.nStartTime = tCommerceInfo.nBeginTime
    tCommerceActInfo.nEendTime = tCommerceInfo.nEndTime
    tCommerceActInfo.isRun = true
    if true == bLater then
        tCommerceActInfo.isRun = false
    end
    
    tCommerceActInfo.nOpen = nOpen
    tCommerceActInfo.nBatchID = tCommerceInfo.nBatchID

    print("[CommerceAct_CreateCacheInfo] 创建缓存数据 nStartTime = "..tCommerceActInfo.nStartTime.." nEendTime = "..tCommerceActInfo.nEendTime
                                    .." nOpen = "..tCommerceActInfo.nOpen.." nBatchID = "
                                    ..tCommerceActInfo.nBatchID.." isRun = "..(tCommerceActInfo.isRun == true and 1 or 0))

    if bLater == true then
        Timer.addLater(ServerCommerceActDefine.COMMERCEACT_BEGINDELATTIME, CommerceAct_LaterBeginAct)
    else
        for uuid, human in pairs(ObjHuman.onlineUuid) do
            if not human.db.ServerCommerce then
                CommerceAct_CreateHumanDB(human)
            end

            ServerCommerceTask.CommerceActTask_HumanSubEvent(human)
            CommerceAct_SendActInfo(human)
        end
    end
end

-- 清理排行榜数据
local function CommerceAct_ClearRankInfo()
    ServerCommerceMiddle.CommerceMiddle_ClearRank()
end

-- 请求中心服活动时间和批次数据
local function CommerceAct_QueryActOpenTime()
    ServerCommerceMiddle.CommerceMiddle_QueryActOpenTime()
end

-- 起服初始化数据
local function CommerceAct_ActCheckOpen_InitServer(nOpen, nOperate, nServerKey)
    if ServerCommerceActDefine.COMMERCEACT_NOOPEN ~= nOpen then
        nQueryOperate = nOperate

        ServerCommerceActRank.CommercerActRank_GetRankServerKey(nServerKey)

        -- 活动开启去请求时间数据
        CommerceAct_QueryActOpenTime()
    end
end

-- 获取玩家当前点数
function CommerceAct_GetHumanPoint(human)
    return human.db.ServerCommerce.nPoint
end

-- 设置玩家当前点数
local function CommerceAct_SetHumanPoint(human, nValue)
    human.db.ServerCommerce.nPoint = nValue
end

-- 获取开始时间
function CommerceAct_GetOpenAndEndTime()
    if not tCommerceActInfo or not tCommerceActInfo.nStartTime or not tCommerceActInfo.nEendTime then
        return 0, 0
    end

    return tCommerceActInfo.nStartTime, tCommerceActInfo.nEendTime
end
----------------------------------------- 外部调用开始 -------------------------------------
function onZeroAll(funcID)
    local nNowTime = os.time()
    local tDate = os.date("*t",nNowTime)

    -- 活动未开启
    if tDate.hour == ServerCommerceActDefine.COMMERCEACT_BEGINTIME then
        -- 获取DB数据
        local tCommerceDBInfo = CommonDB.GetCommerceActInfo()
        if not tCommerceDBInfo or nil == _G.next(tCommerceDBInfo) then
            CommerceAct_LaterTimeQuery(ServerCommerceActDefine.COMMERCEACT_ZERO)
            return
        end

        -- 存在DB数据说明已经满足条件了
        if true == CommerceAct_CheckActIsOverTime() then
            local bOpenNext = CommerceAct_CanOpenNext()
            if false == bOpenNext then
                return
            end

            -- 开启下一轮
            if tCommerceDBInfo.nBatchID < ServerCommerceActDefine.COMMERCEACT_ENDBATCH then
                CommerveManager_WriteLog("[CommerveManager-onZeroAll] 当前条件满足开启下一轮活动 nOldBatchID = "..tCommerceDBInfo.nBatchID.." nNewBatchID = "..tCommerceDBInfo.nBatchID+1)

                -- 清理排行榜数据
                CommerceAct_ClearRankInfo()

                -- 重新请求活动时间数据
                CommerceAct_QueryActOpenTime()

                nQueryOperate = ServerCommerceActDefine.COMMERCEACT_ZERO

                -- CommerceAct_CreateCommonDB()

                -- -- 存在数据说明时间是满足的
                -- CommerceAct_CreateCacheInfo(ServerCommerceActDefine.COMMERCEACT_OPEN, true)

                -- -- 遍历在线玩家
                -- for uuid, human in pairs(ObjHuman.onlineUuid) do
                --     CommerceAct_BeginAllAct(human)
                -- end

                -- -- 重新请求排行榜数据
                -- ServerCommerceActRank.CommercerActRank_InitServerQueryRank()
            end
        end
    end
end

function onHour(hour)
    local nNowTime = os.time()
    local tDate = os.date("*t",nNowTime)
    print("[CommerceAct_onHour] 进入整点时间判断 hour = "..hour.." nNowTime = "..nNowTime)
    if tDate.hour == ServerCommerceActDefine.COMMERCEACT_ENDTIME then
        if not tCommerceActInfo then
            print("[CommerceAct_onHour] 不存在对应的数据 ")
            return
        end

        print("[CommerceAct_onHour] 进入结束时间判断 hour = "..hour.." nNowTime = "..nNowTime)
        local tCommerceDBInfo = CommonDB.GetCommerceActInfo()
        if not tCommerceDBInfo or nil == _G.next(tCommerceDBInfo) then
            print("[CommerceAct_onHour] 存在缓存数据, 但是获取不到DB数据")
            return
        end

        local nEndTime = tCommerceDBInfo.nEndTime
        print("[CommerceAct_onHour] nNowTime = "..nNowTime.." nEndTime = "..nEndTime)
        if nEndTime <= nNowTime then
            print("[CommerceAct_onHour] 活动结束 开始处理活动结束流程 ")
            local nDelayTime = math.random(5, 30)
            Timer.addLater(nDelayTime, CommerceAct_End)
            -- 活动结束处理
            tCommerceActInfo.nSendRankMail = true
            tCommerceActInfo.isRun = false

            -- 关闭活动入口
            for uuid, human in pairs(ObjHuman.onlineUuid) do
                YunYingLogic.sendBanner(human)
            end
        end
    end
end

-- 请求活动打开信息
function CommerceAct_GetActOpen(nOperate)
    print("[CommerceAct_GetActOpen] 开始向中心服请求活动数据")
    ServerCommerceMiddle.CommerceMiddle_IsActOpen(nOperate)
end

-- 中心服回复活动信息
function CommerceAct_ActCheckOpen(nOpen, nOperate, nServerKey)
    print("[CommerceAct_ActCheckOpen] 收到活动信息开始处理 nOpen = "..nOpen.." nOperate ="..nOperate)
    CommerceAct_ActCheckOpen_InitServer(nOpen, nOperate, nServerKey) 
end

-- 起服初始化
function CommerceAct_Init()
    if _G.is_middle == true then
        -- 中心服起服获取信息
        print("[CommerceAct_Init] 中心服起服请求数据开始")
        ServerCommerceMiddle.CommerceMiddle_InitServer()
        return
    end

    local nNowTime = os.time()

    for nID, v in pairs(ServerCommerceCof.CommerceAct) do
        if v.moduleFn then
            local moduleFn = load("return require(\"" .. v.moduleFn .. "\")")()
            if moduleFn then
                tCommerceActModuel[nID] = moduleFn
                if moduleFn.Init then
                    local bRet = moduleFn.Init()
                    if false == bRet then
                        print("[CommerceAct_Init] 初始化模块数据失败 nID ".. nID)
                    end
                end 
                print("[CommerceAct_Init] 加载模块成功 name = "..v.name)
            else
                print("[CommerceAct_Init] 加载模块失败 nID ".. nID)
            end
        else
            print("[CommerceAct_Init] 未配置 name = "..v.name)
        end
    end

    print("[CommerceAct_Init] 延迟进行请求数据操作")
    
    -- 延迟向中心服请求数据
    CommerceAct_LaterTimeQuery(ServerCommerceActDefine.COMMERCEACT_SERVEROPEN)
end

-- 是否还在活动期间
function CommerceAct_IsRun()
    if not tCommerceActInfo then
        return false
    end

    if false == CommerceAct_CheckOpenDay() then
        print("[CommerceAct_IsRun] 开服时间不符合条件 nNowOpenDay = ", CommonDB.getServerOpenDay())
        return false
    end

    return tCommerceActInfo.isRun
end

-- 获取到中心服活动时间数据
function CommerceAct_GetActTime(tData)
    local tOldDBData = CommonDB.GetCommerceActInfo()
    local tNewDBData = nil
    local szText = "[CommerceAct_GetActTime] 获取到对应的中心服时间数据 nBatchID = "..tData.nBatchID.." nBeginTime = "..tData.nBeginTime.." nEndTime = ".. tData.nEndTime
    print(szText)
    if tOldDBData and nil ~= _G.next(tOldDBData) then
            if tOldDBData.nBatchID ~= tData.nBatchID or tOldDBData.nBeginTime ~= tData.nBeginTime
                or tOldDBData.nEndTime ~= tOldDBData.nEndTime then
                    -- szText = szText .."当前服务器数据和获取到的中心服数据不一致进行了重置 旧的DB数据 nOldBatchID = "..tOldDBData.nBatchID
                    --             .." nOldBeginTime = "..tOldDBData.nBeginTime.." nOldEndTime = ".. tOldDBData.nEndTime
                    -- print(szText)
                    tNewDBData = {}
                    tNewDBData.nBatchID = tData.nBatchID
                    tNewDBData.nBeginTime = tData.nBeginTime
                    tNewDBData.nEndTime = tData.nEndTime
                    CommerveManager_WriteLog(szText)

                    -- 存在旧数据，看看需要重置数据
                    if tData.nBeginTime > tOldDBData.nEndTime then
                        tNewDBData.nPoint = 0
                        tNewDBData.nSendRankMail = 0
                        CommerveManager_WriteLog("[CommerceAct_GetActTime] 存在旧的数据,当前批次开启时间大于上一次结束时间, 重置点数和发送邮件数据")
                    else
                        tNewDBData.nPoint = tOldDBData.nPoint
                        tNewDBData.nSendRankMail = tOldDBData.nSendRankMail
                    end
                else 
                    print("[CommerceAct_GetActTime] 当前时间相同")
            end
    else
        tNewDBData = {}
        tNewDBData.nBatchID = tData.nBatchID
        tNewDBData.nBeginTime = tData.nBeginTime
        tNewDBData.nEndTime = tData.nEndTime
        tNewDBData.nPoint = 0
        tNewDBData.nSendRankMail = 0

        szText = szText.." 当前服务器不存在数据直接创建"
        CommerveManager_WriteLog(szText)
    end

    if nil ~= tNewDBData then
        CommonDB.SetCommerceActInfo(tNewDBData)
    end

    local tCommerceInfo = CommonDB.GetCommerceActInfo()
    local nNowTime = os.time()
    if nNowTime < tCommerceInfo.nEndTime then
        local bLater = nQueryOperate == ServerCommerceActDefine.COMMERCEACT_ZERO and true or false
        CommerceAct_CreateCacheInfo(1, bLater)
        ServerCommerceActRank.CommercerActRank_InitServerQueryRank()
    else
        tCommerceActInfo = {}
        tCommerceActInfo.nStartTime = tCommerceInfo.nBeginTime
        tCommerceActInfo.nEendTime = tCommerceInfo.nEndTime
        tCommerceActInfo.isRun = false
        tCommerceActInfo.nOpen = 0
        tCommerceActInfo.nBatchID = tCommerceInfo.nBatchID
        print("[CommerceAct_ActCheckOpen_InitServer] 活动不在开放时间")
    end

end

-- 跨天
function updateDaily(human, funcID)
    if not human then
        return
    end

    if true == CommerceAct_IsRun() then
        for _, module in pairs(tCommerceActModuel) do
            if module and module.updateDaily then
                module.updateDaily(human)
            end
        end
    end
end

-- 玩家登录
function onLogin(human, funcID)
    if not human then
        return
    end

    -- 下发活动基础数据
    if true == CommerceAct_IsRun() then
        print("[CommerceManger_onLogin] 玩家进入进行注册")
        local tCommonDBData = CommonDB.GetCommerceActInfo()
        if not tCommonDBData or nil == _G.next(tCommonDBData) then
            print("[CommerceManger_onLogin] 玩家登录不存在对应的数据通用DB数据!!!!")
            return
        end

        -- 不存在数据直接创建
        if not human.db.ServerCommerce then
            CommerceAct_CreateHumanDB(human)
        else
            -- 用记在玩家身上的批次 和 CommonDB 中的数据比较
            local nOldBatchID = human.db.ServerCommerce.nBatchID
            local nNowBatchID = tCommonDBData.nBatchID
            if nNowBatchID ~= nOldBatchID then
                CommerveManager_WriteLog("[CommerceManger_onLogin] 玩家登录 记录的批次不一致 nOldBatchID = "..nOldBatchID.." nNowBatchID = "..nNowBatchID, human)
                print("[CommerceManger_onLogin] 玩家登录批次不一致, 重置玩家数据 name = "..human.db.name)
                CommerceAct_CreateHumanDB(human)

                -- 重置巅峰战场的阵容数据
                CombatPosLogic.ResetCombatData(human, CombatDefine.COMBAT_TYPE37)
            end
        end
        
        CommerceAct_SendActInfo(human)

        for _, module in pairs(tCommerceActModuel) do
            if module and module.onLogin then
                module.onLogin(human)
            end
        end
    else
        -- 不在活动中
        local tCommonDBData = CommonDB.GetCommerceActInfo()
        if not tCommonDBData or nil == _G.next(tCommonDBData) then
            print("[CommerceManger_onLogin] 玩家登录不存在对应的数据通用DB数据!!!!")
            return
        end

        if false == CommerceAct_CheckActIsOverTime() then
            print("[CommerceManger_onLogin] 当前还在活动时间内，返回")
            return
        end

        -- 不存在对应DB数据
        if not human.db.ServerCommerce then
            return
        end

        -- 未发送个人邮件都是未上榜的
        local bSendPlayerMail = CommerveManager_GetHumanSendPlayerMail(human)
        local nHumanPoint = CommerceAct_GetHumanPoint(human)
        if false == bSendPlayerMail and nHumanPoint >= 1 then
            print("[CommerceManger_onLogin] 发送个人奖励 name = "..human.db.name)
            CommerveManager_SetHumanSendPlayerMail(human, true)
            ServerCommerceActRank.CommercerActRank_SendHumanMailHumanLogin(human)
        else
            --CommerveManager_SetHumanSendPlayerMail(human, true)
            CommerveManager_WriteLog("[CommerceManger_onLogin] 玩家条件不满足发送个人奖励邮件 nSendServer = "..(bSendPlayerMail == true and 1 or 0).." nHumanPoint = "
            ..nHumanPoint, human)
        end

        -- 全服邮件
        local bSendServerMail = CommerveManager_GetHumanSendServerMail(human)
        local nSendServer = true == bSendServerMail and 1 or 0
        if false == bSendServerMail and 1 == tCommonDBData.nSendRankMail and CommerceAct_GetHumanPoint(human) >= 1 then
            print("[CommerceManger_onLogin] 全服邮件奖励开始发送 name = "..human.db.name)
            CommerveManager_SetHumanSendServerMail(human, true)
            ServerCommerceActRank.CommercerActRank_SendServerMailHumanLogin(human)
        else
            CommerveManager_WriteLog("[CommerceManger_onLogin] 玩家条件不满足 nSendServer = "..nSendServer.." nHumanPoint = "
                                        ..nHumanPoint.." tCommonDBData.nSendRankMail = "..tCommonDBData.nSendRankMail, human)
            --CommerveManager_SetHumanSendServerMail(human, true)
            print("[CommerceManger_onLogin] 全服邮件奖励 已经发送过了或积分不满足条件 name = "..human.db.name)
        end
    end

end

-- 是否开启
function isOpen(human, YYInfo, funcConfig)
    local bRet = CommerceAct_IsRun()
    local nRet = bRet and 1 or 0
    print("CommerceAct_isOpen 结束 开启为1, 未开启为0 nRet = "..nRet)
    return bRet
end

-- 
function isActive(human, YYInfo, funcConfig)
    return not isOpen(human, YYInfo, funcConfig)
end

function isRed(human, YYInfo, funcConfig)
    if false == CommerceAct_IsRun() then
        return
    end

    if false == CommerceAct_CheckOpenDay() then
        return
    end

    if not human.db.ServerCommerce then
        CommerceAct_CreateHumanDB(human)
    end

    local bRet
    for _, module in pairs(tCommerceActModuel) do
        if module and module.isRed then
            bRet = module.isRed(human)
            if true == bRet then
                break
            end
        end
    end  

    return bRet
end

function onCharge(human, price, funcID, buyID)
    if not human then
        return
    end

    if false == CommerceAct_IsRun() then
        return
    end
    
    if not human.db.ServerCommerce then
        CommerceAct_CreateHumanDB(human)
    end

    print("[onCharge] 跨服商业活动 玩家充值回调开始 name = "..human.db.name.." price = "..price)

    for _, module in pairs(tCommerceActModuel) do
        if module and module.onCharge then
            module.onCharge(human, price, funcID, buyID)
        end
    end

    -- local tBuyCfg = BuyConf.buy[buyID]
    -- if tBuyCfg then
    --     local region = human.region or "CN"
    --     local nPoint = tBuyCfg[region]
    --     CommerveManager_AddServerPoint(nPoint)
    --     CommerveManager_AddHumanPint(human, nPoint)
    -- end

        CommerveManager_AddServerPoint(price)
        CommerveManager_AddHumanPint(human, price)
end

-- 增加整个服务器的点数
function CommerveManager_AddServerPoint(nAddPoint)
    if false == CommerceAct_IsRun() then
        return
    end

    local tCommonDBData = CommonDB.GetCommerceActInfo()
    local nNewPoint = tCommonDBData.nPoint + nAddPoint
    CommonDB.SetCommerceActInfo_Point(nNewPoint)

    print("[CommerveManager_AddServerPoint] 增加整个服务器的点数 nAddPoint = "..nAddPoint.." nNewPoint = "..nNewPoint)
    for _, module in pairs(tCommerceActModuel) do
        if module and module.onAllPointChange then
            module.onAllPointChange()
        end
    end

    local szText = "[CommerveManager_AddServerPoint] 增加了整个服务器的积分 nAddPoint = "..nAddPoint.." nNewPoint = "..nNewPoint.." 寻宝"..Config.NEW_SVR_INDEX.."区"
    CommerveManager_WriteLog(szText)
end

-- 增加个人积分
function CommerveManager_AddHumanPint(human, nPoint)
    if false == CommerceAct_IsRun() then
        return 
    end

    local nNowPoint = CommerceAct_GetHumanPoint(human)
    local nNewPoint = nNowPoint + nPoint
    CommerceAct_SetHumanPoint(human ,nNewPoint)
    print("[CommerveManager_AddHumanPint] 增加个人积分 nAddPoint = "..nPoint.." nNewPoint = "..nNewPoint.." name = "..human.db.name)
    ServerCommerceActRank.CommercerActRank_HumanPointChange(human, nNewPoint)

    local szText = "[CommerveManager_AddHumanPint] 增加了个人积分 nAddPoint = "..nPoint.." nNewPoint = "..nNewPoint
    CommerveManager_WriteLog(szText, human)
end

-- 写日志
function CommerveManager_WriteLog(szLogText, human)
    if human then
        szLogText = szLogText.." name = "..human.db.name.." id = "..human.db._id
    end

    Log.write(Log.LOGID_OSS_COMMON_ACT, szLogText)
end

-- 设置全服发送邮件奖励
function CommerveManager_SetCommDBSendMail(nValue)
    local tCommonDBData = CommonDB.GetCommerceActInfo()
    if not tCommonDBData then
        print("[CommerveManager_SetCommDBSendMail] 居然不存在对应的数据")
        return
    end

    local tNewDBData =
    {
        nBeginTime = tCommonDBData.nBeginTime,
        nEndTime = tCommonDBData.nEndTime,
        nPoint = tCommonDBData.nPoint,
        nSendRankMail = nValue,
        nBatchID = tCommonDBData.nBatchID
    }
    CommonDB.SetCommerceActInfo(tNewDBData)
    print("[CommerveManager_SetCommDBSendMail] 设置全服邮件奖信息结束")
    -- table.print_lua_table(tNewDBData)
end

-- 获取全服发送邮件标识
function CommerveManager_GetCommDBSendMail()
    return CommonDB.GetCommerceActInfo_SendServerMail()
end

-- 设置玩家已经获取了全服奖励邮件
function CommerveManager_SetHumanSendServerMail(human, nValue)
    human.db.ServerCommerce.bSendServerMail = nValue
end

-- 获取玩家全服奖励邮件状态
function CommerveManager_GetHumanSendServerMail(human)
    return human.db.ServerCommerce.bSendServerMail
end

-- 设置玩家已经获取了个人奖励邮件
function CommerveManager_SetHumanSendPlayerMail(human, nValue)
    human.db.ServerCommerce.bSendPlayerMail = nValue
end

-- 获取玩家全服奖励邮件状态
function CommerveManager_GetHumanSendPlayerMail(human)
    return human.db.ServerCommerce.bSendPlayerMail
end

-- GM 清理数据
function CommerveManager_GMClear(human)
    human.db.ServerCommerce = 
    {
        nPoint = 0,         -- 当前玩家个人积分
        Task = {},          -- 任务信息
        Charge = {},        -- 连充豪礼
        Shop = {},          -- 战区钜惠
        nBatchID = 1,       -- 批次
        bSendPlayerMail = false,        -- 是否发送个人邮件
        bSendServerMail = false,        -- 是否发送全服邮件
        battleGround = {
            freeChallengeCnt = ServerCommerceActDefine.COMMERCEACT_DAILY_FREECHALLENGETIMES,
            resetTime = os.time(),
        },
    }

    for nID, module in pairs(tCommerceActModuel) do
        if module and module.CreatDB then
            local bRet = module.CreatDB(human)
            if false == bRet then
                print("[CommerceAct_CreateHumanDB] nID 初始化DB 数据失败 nID = "..nID)
            end
        end
    end

    -- table.print_lua_table(human.db.ServerCommerce)
    print("[CommerveManager_GMClear] 玩家重置数据完成 name = "..human.db.name)
end

-- GM 增加个人积分
function CommerveManager_GMAddHumanPoint(human, nNum)
    CommerveManager_AddHumanPint(human, nNum)
end

-- GM 增加服务器积分
function CommerveManager_GMAddServerPoint(nNum)
    CommerveManager_AddServerPoint(nNum)
end

-- GM 发送邮件
function CommerveManager_SendMail()
    CommerveManager_SetCommDBSendMail(0)
    CommerceAct_End()
end

function CommerceMiddle_AddTaskPoint(human, nNum)
    ServerCommerceTask.CommerceTask_AddPoint(human, nNum)
end

function CommerveManager_GMRest(human)
    ServerCommerceMiddle.CommerceMiddle_GMClearMiddleMail()
end 

function CommerveManager_GMRestCommonDB()
    local nNowTime = os.time()
    local tCommerceInfo = CommonDB.GetCommerceActInfo()
    local nEndTime = nNowTime + (ServerCommerceActDefine.COMMERCEACT_LASTDAY - 1)* 86400
    local tEndDate = os.date("*t",nEndTime)
    tEndDate.hour = ServerCommerceActDefine.COMMERCEACT_ENDTIME
    tEndDate.min = 0
    tEndDate.sec = 0
    nEndTime = os.time(tEndDate)

    local tDBData = {
        nBeginTime = nNowTime,
        nEndTime = nEndTime,
        nPoint = 0,
        nSendRankMail = 0,
        nBatchID = 1,
    }
    CommonDB.SetCommerceActInfo(tDBData)
    print("[CommerveManager_GMRestCommonDB] 重置通用DB数据完成")
end

function CommerveManager_GMClearMiddleMail()
    ServerCommerceMiddle.CommerceMiddle_GMClearMiddleMail()
end

function CommerveManager_GMSendHumanMail(human)
    ServerCommerceActRank.CommercerActRank_SendHumanMailHumanLogin(human)
end

-- function GetRemainNum(human, nBuyID)
--     for _, module in pairs(tCommerceActModuel) do
--         if module and module.onCharge then
--             if module.IsHaveBuyID and module.IsHaveBuyID(nBuyID) then
--                 return module.GetRemainNum(human, nBuyID)
--             end
--         end
--     end
    
--     return 0
-- end