--------------------------------
-- 文件名       ：  MaiDianLogic.lua
-- 文件说明     ：  埋点处理
-- 创建时间     :   2024/12/03
-- 创建人       :   FC
--------------------------------
local MaiDianDefine = require("MaiDian.MaiDianDefine")
local Util = require("common.Util")
local Log = require("common.Log")
local Timer = require("core.Timer")

-- 埋点玩家信息
tMaiDianPlay = 
{
    --[[
        [human.db._id] = 
        {
            [类型] = value      -- 当前类型所在的值
        }
    ]]
}
-- 埋点区间
local RANGES = {
    {1,10},    -- 1-10次
    {11,30},   -- 11-30次
    {31,50},   -- 31-50次
    {51,100},  -- 51-100次
    {101,200}, -- 101-200次
    {201,500}, -- 201-500次
    {501, math.huge} -- 500+次 (math.huge表示无限大)
}

-- 写日志
local function MaiDian_WriteLog(sztext)
    Log.write(Log.LOGID_OSS_MAIDIAN, sztext)
end

-- 排序
local function MainDian_Sort(l, r)
    return l.key < r.key
end

-- 写日志
function MaiDian_DoWrite()
    print("[MaiDian_DoWrite] 写日志开始")
    if not tMaiDianPlay or not _G.next(tMaiDianPlay) then
        print("[MaiDian_DoWrite] 无数据直接返回")
        return
    end

    local tTotalInfo = {}
    -- table.print_lua_table(tMaiDianPlay)
    -- 先汇总
    for _, tTypeData in pairs(tMaiDianPlay) do
        for nType, v in pairs(tTypeData) do
            if not tTotalInfo[nType] then
                tTotalInfo[nType] = {}
            end

            if not tTotalInfo[nType][v] then
                tTotalInfo[nType][v] = {
                    key = v,
                    nValue = 0
                }
            end

            tTotalInfo[nType][v].nValue = tTotalInfo[nType][v].nValue + 1
        end
    end
    print("[MaiDian_DoWrite] 汇总结束")
    table.print_lua_table(tTotalInfo)
    local tSortData ={}
    for nType, tData in pairs(tTotalInfo) do
        if not tSortData[nType] then
            tSortData[nType] = {}
        end

        for key, v in pairs(tData) do
            table.insert(tSortData[nType], v)
        end
        
        table.sort(tSortData[nType], MainDian_Sort)
    end
    table.print_lua_table(tSortData)
     -- 新增区间统计
     local tRangeStats = {
        [MaiDianDefine.MAIDIAN_TYPE_ADV_SUMMON] = {},
        [MaiDianDefine.MAIDIAN_TYPE_CHEST_OPEN] = {}
    }

    -- 初始化区间计数器
    for _, range in ipairs(RANGES) do
        local rangeKey = range[1].."-"..(range[2] == math.huge and "+" or range[2])
        tRangeStats[MaiDianDefine.MAIDIAN_TYPE_ADV_SUMMON][rangeKey] = 0
        tRangeStats[MaiDianDefine.MAIDIAN_TYPE_CHEST_OPEN][rangeKey] = 0
    end

    -- 统计玩家累计值
    local tPlayerTotals = {}
    for playerId, tData in pairs(tMaiDianPlay) do
        tPlayerTotals[playerId] = {
            [MaiDianDefine.MAIDIAN_TYPE_ADV_SUMMON] = 0,
            [MaiDianDefine.MAIDIAN_TYPE_CHEST_OPEN] = 0
        }
        
        for nType, nValue in pairs(tData) do
            if nType == MaiDianDefine.MAIDIAN_TYPE_ADV_SUMMON 
               or nType == MaiDianDefine.MAIDIAN_TYPE_CHEST_OPEN then
                tPlayerTotals[playerId][nType] = (tPlayerTotals[playerId][nType] or 0) + nValue
            end
        end
    end
    print("[MaiDian_DoWrite] 统计玩家累计值结束")
    -- 计算区间分布
    for playerId, totals in pairs(tPlayerTotals) do
        for nType, total in pairs(totals) do
            for _, range in ipairs(RANGES) do
                if total >= range[1] and total <= range[2] then
                    local rangeKey = range[1].."-"..(range[2] == math.huge and "+" or range[2])
                    tRangeStats[nType][rangeKey] = tRangeStats[nType][rangeKey] + 1
                    break
                end
            end
        end
    end
    print("[MaiDian_DoWrite] 计算区间分布结束")
    table.print_lua_table(tSortData)
    for nType, tData in pairs(tSortData) do
        
        local sztext = " nType = "..nType.." "
        for key, v in pairs(tData) do
            sztext = sztext.."{"..v.key..","..v.nValue.."},"
        end
        MaiDian_WriteLog(sztext)
    end

    for nType, ranges in pairs(tRangeStats) do
        local szType = nType == MaiDianDefine.MAIDIAN_TYPE_ADV_SUMMON and "高级召唤" or "宝箱开启"
        local sztext = "[区间统计] "..szType.." "
        for rangeKey, count in pairs(ranges) do
            sztext = sztext..rangeKey.."="..count.." "
        end
        MaiDian_WriteLog(sztext)
    end
    print("[MaiDian_DoWrite] 区间统计结束")
    print("[MaiDian_DoWrite] 写日志结束 ")
    -- 重置
    tMaiDianPlay = {}
end

-- 定时写日志
function MaiDian_OnTime()
    Timer.addLater(10*60, MaiDian_DoWrite)
end

-- 回调
function MaiDian_Begin(human, nType, tData)
    table.print_lua_table(tData)
    if not human or MaiDianDefine.MAIDIAN_TYPE_MIN > nType or MaiDianDefine.MAIDIAN_TYPE_MAX < nType or not tData then
        return
    end

    if "table" ~= type(tData) then
        print("[MaiDian_Begin] 不正确的value")
        return
    end

    local nValue = tData.nValue

    local nHumanID = human.db._id
    if not tMaiDianPlay[nHumanID] then
        tMaiDianPlay[nHumanID] = {}
    end
    if nType == MaiDianDefine.MAIDIAN_TYPE_ADV_SUMMON or nType == MaiDianDefine.MAIDIAN_TYPE_CHEST_OPEN then
       tMaiDianPlay[nHumanID][nType] = (tMaiDianPlay[nHumanID][nType] or 0) + nValue
       return
    end
    tMaiDianPlay[nHumanID][nType] =  nValue
end

function MaiDianLogic_GM()
    MaiDian_OnTime()
end