local Util = require("common.Util")
local WarZoneConf = require("excel.WarZone")
local MiddleConnect = require("middle.MiddleConnect")
local MiddleManager = require("middle.MiddleManager")
local InnerMsg = require("core.InnerMsg")
local Config = require("Config")
local CommonDB = require("common.CommonDB")
local Timer = require("core.Timer")
local CommonDefine = require("common.CommonDefine")
local ServerCommerceManger = require("serverCommerce.ServerCommerceManager")
local ServerCommerceActDefine = require("serverCommerce.ServerCommerceActDefine")
local ServerCommerceRank = require("serverCommerce.ServerCommerceActRank")
local Log = require("common.Log")
local ServerCommerceActBattleGroundCS = require("serverCommerce.ServerCommerceActBattleGroundCS")


-- 缓存开服天数
local tServerOpenDay = nil
local SERVERCOMMERCEOPENDAY = 8     -- 活动开启要求天数

-- 缓存已经回复的开服天数的服务器
local tServerOKDay = nil

local function CommerceMiddle_WriteLog(szLogText)
    Log.write(Log.LOGID_OSS_COMMON_ACT, szLogText)
end

-- 检查是否进入下一轮
local function CommerceMiddle_CheckGoNextBatch(nServerKey)
    if _G.is_middle ~= true then 
        return false
    end

    local tDBData = CommonDB.GetCommerceMiddleAct_ByKey(nServerKey)
    if not tDBData then
        return true
    end

    -- 已经是第三轮了
    if tDBData.nBatchID >= 3 then
        return false
    end

    local nNowTime = os.time()
    if nNowTime < tDBData.nEndTime then
        return false
    end

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

local function CommerceMiddle_CreateActTime()
    local nNowTime = os.time()
    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)

    return nNowTime, nEndTime
end

-- 更新下一轮DB数据
local function CommerceMiddle_CreateNextBatchDB(nServerKey)
    local tOldDBData = CommonDB.GetCommerceMiddleAct_ByKey(nServerKey)
    if not tOldDBData then
        CommerceMiddle_WriteLog("[CommerceMiddle_CreateNextBatchDB] 更新下一轮中心服活动时间数据, 但是不存在久的数据 nServerKey = "..nServerKey)
    end

    local nBeginTime, nEndTime = CommerceMiddle_CreateActTime()
    local nBatchID = 1
    if tOldDBData and tOldDBData.nBatchID then
        nBatchID = tOldDBData.nBatchID + 1
    end

    local tDBData = 
    {
        nBatchID = nBatchID,
        nBeginTime = nBeginTime,
        nEndTime = nEndTime
    }

    local szText = "[CommerceMiddle_CreateNewDB] 更新下一轮的DB数据 nServerKey = "..nServerKey.." nBatchID = "..tDBData.nBatchID
    .." nBeginTime = "..tDBData.nBeginTime.." nEndTime = "..tDBData.nEndTime
    CommerceMiddle_WriteLog(szText)

    CommonDB.SetCommerceMiddleAct_ByKey(nServerKey, tDBData)
end

-- 创建新的DB数据
local function CommerceMiddle_CreateNewDB(nServerKey)
    local tOldData = CommonDB.GetCommerceMiddleAct_ByKey(nServerKey)
    if tOldData then
        local bRet = CommerceMiddle_CheckGoNextBatch(nServerKey)
        if false == bRet then
            return
        else
            CommerceMiddle_WriteLog("[CommerceMiddle_CreateNewDB] 该服务器满足生成下一轮条件, 生成中心服时间数据 nServerKey = "..nServerKey)

            CommerceMiddle_CreateNextBatchDB(nServerKey)

            return
        end
    end

    local nBeginTime, nEndTime = CommerceMiddle_CreateActTime()
    local tDBData = 
    {
        nBatchID = 1,
        nBeginTime = nBeginTime,
        nEndTime = nEndTime
    }

    local szText = "[CommerceMiddle_CreateNewDB] 生成新的DB数据 nServerKey = "..nServerKey.." nBatchID = "..tDBData.nBatchID
                    .." nBeginTime = "..tDBData.nBeginTime.." nEndTime = "..tDBData.nEndTime
    CommerceMiddle_WriteLog(szText)

    CommonDB.SetCommerceMiddleAct_ByKey(nServerKey, tDBData)
end

local function CommerceMiddle_Refresh(nServerKey)
    if _G.is_middle ~= true or not tServerOpenDay then
        return
    end

    local nOpenDay = tServerOpenDay[nServerKey]
    if nOpenDay < 8 then
        print("[CommerceMiddle_Refresh] 当前开服时间不足直接返回")
        return
    elseif nOpenDay == 8 then
        CommerceMiddle_WriteLog("[CommerceMiddle_Refresh] 该服务器时间到达8天, 生成中心服时间数据 nServerKey = "..nServerKey)
        CommerceMiddle_CreateNewDB(nServerKey)
    else
        if true == CommerceMiddle_CheckGoNextBatch(nServerKey) then
            CommerceMiddle_WriteLog("[CommerceMiddle_Refresh] 该服务器满足生成下一轮条件, 生成中心服时间数据 nServerKey = "..nServerKey)

            CommerceMiddle_CreateNextBatchDB(nServerKey)
        else 
            print("[CommerceMiddle_Refresh] 当前不满足条件不生成下一轮")
        end
    end
end

-- 发送活动开始时间
local function CommerceMiddle_SendOpenTime(nServerID)
    local fd = MiddleManager.getFDBySvrIndex(nServerID)
    if not fd then
        print("[CommerceMiddle_SendOpenTime] 获取不到对应的fd nServerID = "..nServerID)
        return
    end

    local nServerConfID = MiddleConnect.MiddleConnect_TrueServerID2ConfServerID(nServerID)
    local nServerKey = MiddleConnect.MiddleConnect_GetWarZoneServer(nServerConfID)

    local tDBData = CommonDB.GetCommerceMiddleAct_ByKey(nServerKey)
    if not tDBData then
        CommerceMiddle_WriteLog("[CommerceMiddle_SendOpenTime] 不存在对应的中心服DB数据 nServerKey = "..nServerKey)
        return
    end
 
    local tMsgData = InnerMsg.wl.WL_COMMERCE_ACT_GET_OPENTIME
    tMsgData.nBatchID = tDBData.nBatchID
    tMsgData.nBeginTime = tDBData.nBeginTime
    tMsgData.nEndTime = tDBData.nEndTime

    InnerMsg.sendMsg(fd, tMsgData)
end

-- 起服获取信息
function CommerceMiddle_InitServer()
    if _G.is_middle ~= true then 
        return
    end

    Timer.addLater(ServerCommerceActDefine.COMMERCEACT_INITOPENDAY, CommerceMiddle_GetOpen)
    print("[CommerceMiddle_InitServer] 起服定时去获取数据 nTime = "..ServerCommerceActDefine.COMMERCEACT_INITOPENDAY)
end

-- 定时获取普通服开服信息
function CommerceMiddle_OnZero()
    if _G.is_middle ~= true then 
        return
    end

    ServerCommerceActBattleGroundCS.onZero()

    Timer.addLater(15, CommerceMiddle_GetOpen)
    print("[CommerceMiddle_OnZero] 整点定时去获取数据")
end

-- 获取(中心服主动去获取)
function CommerceMiddle_GetOpen()
    if _G.is_middle ~= true then 
        return
    end
    tServerOKDay = {}

    print("[CommerceMiddle_GetOpen] 开始去获取服务器开启信息")
    local tWarZoneConf = WarZoneConf.group
    for _, v in ipairs(tWarZoneConf) do
        local nFirstServerID = MiddleConnect.MiddleConnect_ConfServerID2TrueServerID(v.nMinServerID)
        print("[CommerceMiddle_GetOpen] nMinServerID = "..v.nMinServerID.." nFirstServerID = "..nFirstServerID)
        local fd = MiddleManager.getFDBySvrIndex(nFirstServerID)
        if fd then
            tServerOKDay[nFirstServerID] = 0
            print("[CommerceMiddle_GetOpen] 中心服主动去获取数据 nFirstServerID = "..nFirstServerID)
            local tMsgData = InnerMsg.wl.WL_COMMERCE_QUERYOPENDAY
            InnerMsg.sendMsg(fd, tMsgData)
            print("[CommerceMiddle_GetOpen] 发送结束")
        else
            print("[CommerceMiddle_GetOpen] 中心服主动去获取数据, 但是获取不到fd nFirstServerID = "..nFirstServerID)
        end
    end
end

-- 回复开服天数
function CommerceMiddle_SendOpenDay(fd, msg)
    local nOpenDay = msg.nOpenDay

    local nServerConfID = MiddleConnect.MiddleConnect_TrueServerID2ConfServerID(msg.nSrcServerID)
    if not tServerOpenDay then
        tServerOpenDay = {}
    end

    if not tServerOKDay then
        print("[CommerceMiddle_SendOpenDay] 没有正确的对 tServerOKDay 进行初始化")
        tServerOKDay = {}
    end

    tServerOpenDay[nServerConfID] = nOpenDay
    print("[CommerceMiddle_SendOpenDay] 回复开服天数 nServerConfID = "..nServerConfID.." nOpenDay = "..nOpenDay)
    
    tServerOKDay[msg.nSrcServerID] = 1

    local bOK = true
    for _, v in pairs(tServerOKDay) do
        if 0 == v then
            bOK = false
        end
    end

    if true == bOK then
        CommerMiddle_CheckMiddleInfo()
    end
end

-- 请求开服天数
function CommerceMiddle_QueryOpenDay(fd, msg)
    local tMsgData = InnerMsg.lw.LW_COMMERCE_SENDOPENDAY
    tMsgData.nOpenDay = CommonDB.getServerOpenDay()
    tMsgData.nSrcServerID = Config.SVR_INDEX

    InnerMsg.sendMsg(0, tMsgData)
    print("[CommerceMiddle_QueryOpenDay] 请求开服天数")
end

function CommerceMiddle_QueryOpenAct(fd, msg)
    local nConfServerID = MiddleConnect.MiddleConnect_TrueServerID2ConfServerID(msg.nSrcServerID)
    local nFirstServerID
    for _, v in ipairs(WarZoneConf.group) do
        if v.nMinServerID <= nConfServerID and v.nMaxServerID >= nConfServerID then
            nFirstServerID = v.nMinServerID
            break
        end
    end
    
    if nil == nFirstServerID or not tServerOpenDay or not tServerOpenDay[nFirstServerID] then
        print("[QueryOpenAct] 获取不到战区的第一个服务器\n")
        return
    end

    local nSrcFD = MiddleManager.getFDBySvrIndex(msg.nSrcServerID)
    if not nSrcFD then
        return
    end

    local tMsgData = InnerMsg.wl.WL_COMMERCE_ACTOPEN
    tMsgData.nOpen = tServerOpenDay[nFirstServerID] >= SERVERCOMMERCEOPENDAY and 1 or 0
    tMsgData.nOperate = msg.nOperate
    tMsgData.nServerKey = nFirstServerID
    InnerMsg.sendMsg(nSrcFD, tMsgData)

    print("[CommerceMiddle_QueryOpenAct] 请求中心服活动是否开启 nOpen = "..tMsgData.nOpen)
end


function CommerceMiddle_SendOpenAct(fd, msg)
    print("[CommerceMiddle_SendOpenAct] 获取到中心服发送的是否开服数据 nOpen = "..msg.nOpen)
    ServerCommerceManger.CommerceAct_ActCheckOpen(msg.nOpen, msg.nOperate, msg.nServerKey)
end

-- 发送玩家排行榜信息
function CommerveMiddle_HumanPointChange(human, nPoint)
    local tMsgData = InnerMsg.lw.LW_COMMERCE_RANK_POINT_CHANGE
    tMsgData.uuid = human.db._id
    tMsgData.name = human.db.name
    tMsgData.head = human.db.head
    tMsgData.headFrame = human.db.headFrame
    tMsgData.nSrcServerID = Config.SVR_INDEX
    tMsgData.nValue = nPoint
    tMsgData.nRankType = CommonDefine.COMMONRANK_TYPE_SERVERCOMMERCE
    tMsgData.nRankSubType = CommonDefine.COMMONRANK_SUB_TYPE_SERVERCOMMERCE_HUMAN
    tMsgData.nOperate = CommonDefine.COMMONRANK_VALUE_REPLACE
    tMsgData.servername = Config.NEW_SVR_INDEX.."区"

    InnerMsg.sendMsg(0, tMsgData)
    print("[CommerveMiddle_HumanPointChange] 发送玩家排行榜信息结束 name = "..tMsgData.name.." nValue = "..tMsgData.nValue)
end

-- 服务器点数改变发送排行榜信息
function CommerveMiddle_ServerPointChange(nPoint)
    local tMsgData = InnerMsg.lw.LW_COMMERCE_RANK_POINT_CHANGE
    tMsgData.uuid = Config.NEW_SVR_INDEX
    tMsgData.name = Config.NEW_SVR_INDEX.."区"
    tMsgData.head = -1
    tMsgData.headFrame = -1
    tMsgData.nSrcServerID = Config.SVR_INDEX
    tMsgData.nValue = nPoint
    tMsgData.nRankType = CommonDefine.COMMONRANK_TYPE_SERVERCOMMERCE
    tMsgData.nRankSubType = CommonDefine.COMMONRANK_SUB_TYPE_SERVERCOMMERCE_SERVER
    tMsgData.nOperate = CommonDefine.COMMONRANK_VALUE_REPLACE
    tMsgData.servername = Config.NEW_SVR_INDEX.."区"

    InnerMsg.sendMsg(0, tMsgData)

    print("[CommerveMiddle_HumanPointChange] 发送服务器排行榜信息结束 name = "..tMsgData.name.." nValue = "..tMsgData.nValue)
end

-- 请求排行榜数据
function CommerveMiddle_QueryRankInfo(nRankType, nRankSubType)
    local tMsgData = InnerMsg.lw.LW_COMMERCE_QUERY_RANK_INFO
    tMsgData.nRankType = nRankType
    tMsgData.nRankSubType = nRankSubType
    tMsgData.nSrcServerID = Config.SVR_INDEX
    
    InnerMsg.sendMsg(0, tMsgData)
    print("[CommerveMiddle_QueryRankInfo] 向中心服请求排行榜数据")
end

-- 获取到排行榜数据
function CommerveMiddle_GetRankInfo(tData)
    ServerCommerceRank.CommercerActRank_GetRankInfo(tData)
end

-- 检测是否存在中心服数据
function CommerMiddle_CheckMiddleInfo()
    if _G.is_middle ~= true then
        return
    end
    
    for nServerID, v in pairs(tServerOKDay) do
        local nServerConfID = MiddleConnect.MiddleConnect_TrueServerID2ConfServerID(nServerID)
        local nServerKey = MiddleConnect.MiddleConnect_GetWarZoneServer(nServerConfID)
        print("[CommerMiddle_CheckMiddleInfo] 进行判断中心服是否存在数据 nServerKey = "..nServerKey.." nServerID = "..nServerID)
        local tDBData = CommonDB.GetCommerceMiddleAct_ByKey(nServerKey)
        local nOpenDay = tServerOpenDay[nServerConfID]
        -- 不存在去拿数据
        if nil == tDBData then
            --if nOpenDay >= 8 then
                local fd = MiddleManager.getFDBySvrIndex(nServerID)
                if fd then
                    local tMsgData = InnerMsg.wl.WL_COMMERCE_GET_ACT_INFO
                    tMsgData.nReqServerID = 0
                    InnerMsg.sendMsg(fd, tMsgData)
                    print("[CommerMiddle_CheckMiddleInfo] 发送数据完成")
                else
                    print("[CommerMiddle_CheckMiddleInfo] 获取不到FD nServerID = "..nServerID)
                end
            --end
        else
            print("[CommerMiddle_CheckMiddleInfo] 存在数据, 判断是否刷新 nServerID = "..nServerID.." nServerKey = "..nServerKey
                    .." nBatchID = "..tDBData.nBatchID.." nBeginTime = "..tDBData.nBeginTime.." nEndTime = "..tDBData.nEndTime)
            CommerceMiddle_Refresh(nServerKey)
        end
    end
end

-- 中心服请求活动时间相关数据
function CommerceMiddle_GetActInfo_WL(msg)
    print("[CommerceMiddle_GetActInfo] 收到中心服请求")

    local tDBData = CommonDB.GetCommerceActInfo()
    local tMsgData = InnerMsg.lw.LW_COMMERCE_GET_ACT_INFO
    -- table.print_lua_table(tDBData)

    tMsgData.nReqServerID = msg.nReqServerID
    tMsgData.nSrcServerID = Config.SVR_INDEX
    tMsgData.nOperate = 0
    tMsgData.nBatchID = 0
    tMsgData.nBeginTime = 0
    tMsgData.nEndTime = 0

    if not tDBData or nil == _G.next(tDBData) then
        tMsgData.nOperate = 2
    else
        tMsgData.nOperate = 1
        tMsgData.nBatchID = tDBData.nBatchID
        tMsgData.nBeginTime = tDBData.nBeginTime
        tMsgData.nEndTime = tDBData.nEndTime
    end

    InnerMsg.sendMsg(0, tMsgData)
end

-- 中心服收到活动相关数据
function CommerceMiddle_GetActInfo_LW(msg)
    if msg.nOperate == 0 then
        print("[CommerceMiddle_GetActInfo_LW] 不存在对应的数据")
    end
    
    local nReqServerID = msg.nReqServerID
    local nServerConfID = MiddleConnect.MiddleConnect_TrueServerID2ConfServerID(msg.nSrcServerID)
    local nServerKey = MiddleConnect.MiddleConnect_GetWarZoneServer(nServerConfID)
    print("[CommerceMiddle_GetActInfo_LW] 中心服收到回包 nReqServerID = "..nReqServerID.." nServerConfID = "..nServerConfID)
    -- table.print_lua_table(msg)

    local tDBData = {
        nBatchID = 0,
        nBeginTime = 0,
        nEndTime = 0
    }

    local tOldDBData = CommonDB.GetCommerceMiddleAct_ByKey(nServerKey)
    if tOldDBData then
        print("[CommerceMiddle_GetActInfo_LW] 存在db数据")
        -- table.print_lua_table(tOldDBData)
    end

    -- 不存在对应的数据
    if msg.nOperate == 2 then
        print("[CommerceMiddle_GetActInfo_LW] 当前上报的服务器没有对应的数据, 活动未开始")
        CommerceMiddle_Refresh(nServerKey)
    elseif msg.nOperate == 1 then
        if nil == tOldDBData then
            tDBData.nBatchID = msg.nBatchID
            tDBData.nBeginTime = msg.nBeginTime
            tDBData.nEndTime = msg.nEndTime
            print("[CommerceMiddle_GetActInfo_LW] 当前不存在数据, 进行了设置 nServerKey = "..nServerKey)
            CommonDB.SetCommerceMiddleAct_ByKey(nServerKey, tDBData)
        end

        tOldDBData = CommonDB.GetCommerceMiddleAct_ByKey(nServerKey)
        if tOldDBData then
            print("[CommerceMiddle_GetActInfo_LW] 设置后的数据打印")
            -- table.print_lua_table(tOldDBData)
        end

        CommerceMiddle_Refresh(nServerKey)
    end

    print("[CommerceMiddle_GetActInfo_LW] 1111111")
    -- table.print_lua_table(tDBData)

    if 0 ~= nReqServerID then
        print("[CommerceMiddle_GetActInfo_LW] 当前有服务器请求数据 nReqServerID = "..nReqServerID)
        CommerceMiddle_SendOpenTime(nReqServerID)
    end
end

-- 普通服请求活动时间
function CommerceMiddle_GetActOpenTime_LW(msg)
    if _G.is_middle ~= true then return end

    local nServerConfID = MiddleConnect.MiddleConnect_TrueServerID2ConfServerID(msg.nSrcServerID)
    local nServerKey = MiddleConnect.MiddleConnect_GetWarZoneServer(nServerConfID)

    local tDBData = CommonDB.GetCommerceMiddleAct_ByKey(nServerKey)
    if nil == tDBData then
        local nFirstServerID = MiddleConnect.MiddleConnect_ConfServerID2TrueServerID(nServerKey)
        local fd = MiddleManager.getFDBySvrIndex(nFirstServerID)
        if fd then
            local tMsgData = InnerMsg.wl.WL_COMMERCE_GET_ACT_INFO
            tMsgData.nReqServerID = msg.nSrcServerID
            InnerMsg.sendMsg(fd, tMsgData)
        else
            print("[CommerceMiddle_GetActOpenTime_LW] 不存在对应的fd 数据 nFirstServerID = "..nFirstServerID)
        end
    else
        CommerceMiddle_Refresh(nServerKey)
        CommerceMiddle_SendOpenTime(msg.nSrcServerID)
    end
end

function CommerceMiddle_GetActOpenTime_WL(msg)
    ServerCommerceManger.CommerceAct_GetActTime(msg)
end

------------------ 活动管理器操作 ------------------------
-- 请求活动是否开启
function CommerceMiddle_IsActOpen(nOperate)
    local tMsgData = InnerMsg.lw.LW_COMMERCE_ACTOPEN
    tMsgData.nSrcServerID = Config.SVR_INDEX
    tMsgData.nOperate = nOperate

    InnerMsg.sendMsg(0, tMsgData)
    print("[CommerceMiddle_IsActOpen] 普通服开始请求活动信息") 
end

-- 请求当前服务器排行最新的排名
function CommerceMiddle_QueryServerRank(nOperate)
    local tMsgData = InnerMsg.lw.LW_COMMERCE_QUERY_SERVER_RANK
    tMsgData.nSrcServerID = Config.SVR_INDEX
    tMsgData.nServerUuid = tostring(Config.NEW_SVR_INDEX)
    tMsgData.nOperate = nOperate
    InnerMsg.sendMsg(0, tMsgData)
end

-- 获取到 当前服务器排行最新的排名
function CommerceMiddle_GetServerRank(msg)
    ServerCommerceRank.CommercerActRank_GetServerRank(msg)
end

-- 通知中心服当前服务器在线全服玩家邮件结束
function CommerceMiddle_TellServerMailOk()
    local tMsgData = InnerMsg.lw.LW_COMMERCE_TELL_SERVER_MAILOK
    tMsgData.nSrcServerID = Config.SVR_INDEX
    InnerMsg.sendMsg(0, tMsgData)
    print("[CommerceMiddle_TellServerMailOk] 通知中心服当前服务器在线全服玩家邮件结束")
end

function CommerceMiddle_GetHumanRank(msg)
    ServerCommerceRank.CommercerActRank_SendHumanRankPrize(msg)
end

function CommerceMiddle_ClearRank()
    local tMsgData = InnerMsg.lw.LW_COMMERCE_CLEAR_RANK
    tMsgData.nRankType = CommonDefine.COMMONRANK_TYPE_SERVERCOMMERCE
    tMsgData.nRankSubType = CommonDefine.COMMONRANK_SUB_TYPE_SERVERCOMMERCE_SERVER
    tMsgData.nSrcServerID = Config.SVR_INDEX
    InnerMsg.sendMsg(0, tMsgData)

    tMsgData.nRankSubType = CommonDefine.COMMONRANK_SUB_TYPE_SERVERCOMMERCE_HUMAN
    InnerMsg.sendMsg(0, tMsgData)
end

function CommerceMiddle_GMClearMiddleMail()
    local tMsgData = InnerMsg.lw.LW_COMMERCE_GM_CLEAR_MAIL
    tMsgData.nSrcServerID = Config.SVR_INDEX
    InnerMsg.sendMsg(0, tMsgData)
end

function CommerceMiddle_QueryActOpenTime()
    local tMsgData = InnerMsg.lw.LW_COMMERCE_ACT_GET_OPENTIME
    tMsgData.nSrcServerID = Config.SVR_INDEX
    InnerMsg.sendMsg(0, tMsgData)
    print("[CommerceMiddle_QueryActOpenTime] 请求中心服活动开启时间")
end

function CommerceMiddle_GMClearDB(nServerID)
    local nServerConfID = MiddleConnect.MiddleConnect_TrueServerID2ConfServerID(nServerID)
    local nServerKey = MiddleConnect.MiddleConnect_GetWarZoneServer(nServerConfID)

    local nBeginTime, nEndTime = CommerceMiddle_CreateActTime()
    local tDBData = 
    {
        nBatchID = 1,
        nBeginTime = nBeginTime,
        nEndTime = nEndTime
    }

    local szText = "[CommerceMiddle_GMClearDB] 生成新的DB数据 nServerKey = "..nServerKey.." nBatchID = "..tDBData.nBatchID
                    .." nBeginTime = "..tDBData.nBeginTime.." nEndTime = "..tDBData.nEndTime
    CommerceMiddle_WriteLog(szText)

    CommonDB.SetCommerceMiddleAct_ByKey(nServerKey, tDBData)

    CommerceMiddle_SendOpenTime(nServerID)
end