
local Lang = require("common.Lang")
local Util = require("common.Util")
local Msg = require("core.Msg")
local ObjHuman = require("core.ObjHuman")
local DailyTaskExcel = require("excel.HonorJourney")
local BagLogic = require("bag.BagLogic")
local Grid = require("bag.Grid")
local ChatPaoMaLogic = require("chat.ChatPaoMaLogic")
local RoleSystemLogic = require("roleSystem.RoleSystemLogic")
local RoleSystemDefine = require("roleSystem.RoleSystemDefine")
local ItemDefine = require("bag.ItemDefine")
local ChengjiuLogic = require("chengjiu.ChengjiuLogic")
local ChengjiuDefine = require("chengjiu.ChengjiuDefine")
local YunYingLogic = require("yunying.YunYingLogic")
local HonorJourneyExcel = require("excel.HonorJourney")
local CommonDefine = require("common.CommonDefine")
local TriggerLogic = require("trigger.TriggerLogic")
local TriggerDefine = require("trigger.TriggerDefine")
local YjTreasureDBLogic = require("yjTreasure.YjTreasureDB")
local HeroLogic = require("hero.HeroLogic")

-- HonorJourney ={
--  nPoint = 0,             -- 荣耀积分
--   TaskPrize = {                -- 任务数据
     --[id] = {finishNum}
     --}
--},             
--   TaskPointPrize = {},      -- 任务积分奖励
-- }

local HONORJOURNEY_SHOWTASK = 1         -- 显示任务
local HONORJOURNEY_NOWSHOWTASK = 0      -- 非显示任务(需要完成前置任务)

local HONORJOURNEY_LEN = nil            -- 荣耀任务长度

-- 初始化DB数据
local function HonorJourneyTask_CreateDB(human)
    human.db.HonorJourney = {
        nPoint = 0,     -- 荣耀积分
        TaskPointPrize = {},      -- 任务积分奖励
        TaskPrize = {
            --[[
                [nID] =  {
                    nFinishNum = xx,
                    nState = 0
                }
            ]]
        }
    }

    local tTaskPrize = human.db.HonorJourney.TaskPrize
    local tConf = HonorJourneyExcel.HonorJourney
    for nID, v in pairs(tConf) do
        if not tTaskPrize[nID] then
            tTaskPrize[nID] = {
                nFinishNum = 0,
                nState = CommonDefine.COMMON_PRIZE_STATE_NOGET
            }
        else
            print("[HonorJourneyTask_CreateDB] 配置了重复的奖励ID nID = "..nID)
        end
    end

    tConf = HonorJourneyExcel.Reward
    local tTaskPointPrize = human.db.HonorJourney.TaskPointPrize
    for nID, v in ipairs(tConf) do
        if not tTaskPointPrize[nID] then
            tTaskPointPrize[nID] = CommonDefine.COMMON_PRIZE_STATE_NOGET
        else
            print("[HonorJourneyTask_CreateDB] 配置了重复的奖励ID nID = "..nID)
        end
    end
end

-- 检测是否初始化DB数据
local function HonorJourneyTask_CheckCreateDB(human)
    if human.db.HonorJourney and nil ~= _G.next(human.db.HonorJourney) then
        return
    end

    HonorJourneyTask_CreateDB(human)
end

-- 增加任务完成次数
local function HonorJourneyTask_AddTaskFinishNum(human, nID, nValue)
    local tDBTaskPrize = human.db.HonorJourney.TaskPrize

    tDBTaskPrize[nID].nFinishNum = tDBTaskPrize[nID].nFinishNum + nValue
end

-- 设置任务完成次数
local function HonorJourneyTask_SetTaskFinishNum(human, nID, nValue)
    local tDBTaskPrize = human.db.HonorJourney.TaskPrize

    tDBTaskPrize[nID].nFinishNum = nValue
end

function HonorJourney_GetTaskCof()
    return HonorJourneyExcel
end

local function HonorJourneyTask_GetTaskDB(human)
    if not human.db.HonorJourney then
        HonorJourneyTask_CreateDB(human)
    end

    local tTaskPointPrize = human.db.HonorJourney and human.db.HonorJourney.TaskPointPrize

    if tTaskPointPrize and #tTaskPointPrize < #HonorJourneyExcel.Reward then
        -- local nNowPoint = HonorJourney_GetTaskPoint(human)
        local nNowPoint = human.db.HonorJourney and human.db.HonorJourney.nPoint or 0

        for i=#tTaskPointPrize+1, #HonorJourneyExcel.Reward do
            if not tTaskPointPrize[i] then
                tTaskPointPrize[i] = CommonDefine.COMMON_PRIZE_STATE_NOGET
                local boxCfg = HonorJourneyExcel.Reward[i]
                if boxCfg and nNowPoint >= boxCfg.needcnt then
                    tTaskPointPrize[i] = CommonDefine.COMMON_PRIZE_STATE_CANGET
                end
            else
                print("[HonorJourneyTask_CreateDB] 配置了重复的奖励ID nID = "..i)
            end
        end
    end


    return human.db.HonorJourney
end

-- 获取任务完成次数
local function HonorJourney_GetTaskFinishNum(human, nID)
    local tDBData = HonorJourneyTask_GetTaskDB(human)
    if not tDBData.TaskPrize[nID] then
        print("[HonorJourney_GetTaskFinishNum] 不存在对应的任务ID nID = "..nID)
        return 0
    end

    return tDBData.TaskPrize[nID].nFinishNum
end

-- 获取任务状态
local function HonorJourney_GetTaskStatus(human, nID)
    local tDBData = HonorJourneyTask_GetTaskDB(human)
    if not tDBData.TaskPrize or not tDBData.TaskPrize[nID] or not tDBData.TaskPrize[nID].nState then
        return CommonDefine.COMMON_PRIZE_STATE_NOGET
    end

    return tDBData.TaskPrize[nID].nState
end

-- 设置任务状态
local function HonorJourney_SetTaskStatus(human, nID, nValue)
    local tDBData = HonorJourneyTask_GetTaskDB(human)
    tDBData.TaskPrize[nID].nState = nValue
end

-- 获取任务累积奖励状态
local function HonorJourney_GetTaskPointPrize(human, nID)
    local tDBData = HonorJourneyTask_GetTaskDB(human)
    if not tDBData.TaskPointPrize or not tDBData.TaskPointPrize[nID] then
        return CommonDefine.COMMON_PRIZE_STATE_NOGET
    end

    return tDBData.TaskPointPrize[nID]
end

-- 设置任务累积奖励状态
local function HonorJourney_SetTaskPointPrize(human, nID, nValue)
    local tDBData = HonorJourneyTask_GetTaskDB(human)
    if not tDBData.TaskPointPrize or not tDBData.TaskPointPrize[nID] then
        return
    end

    tDBData.TaskPointPrize[nID] = nValue
end

-- 获取积分
local function HonorJourney_GetTaskPoint(human)
    return human.db.HonorJourney.nPoint
end

-- 增加积分
local function HonorJourney_AddTaskPoint(human, nPoint)
    human.db.HonorJourney.nPoint = human.db.HonorJourney.nPoint + nPoint
end


local function HonorJourney_GetDayLoginTime(human)
    local tDBData = HonorJourneyTask_GetTaskDB(human)
    return tDBData.dayLoginTime
end

local function HonorJourney_UpdateDayLoginTime(human)
    local tDBData = HonorJourneyTask_GetTaskDB(human)
    tDBData.dayLoginTime = os.time()
end


-- 检测任务状态
function HonorJourney_CheckTaskStatus(human,nID)
    local nNowFinishNum = HonorJourney_GetTaskFinishNum(human, nID)
    local tConf = HonorJourneyExcel.HonorJourney[nID]
    -- print("[HonorJourney_CheckTaskStatus] nID = "..nID.." nNowFinishNum = "..nNowFinishNum.." taskProcess = "..tConf.taskProcess)
    if nNowFinishNum >= tConf.taskProcess then
        print("[HonorJourney_CheckTaskStatus] 满足条件 nID = "..nID)
        local nState = HonorJourney_GetTaskStatus(human,  nID)
        if CommonDefine.COMMON_PRIZE_STATE_NOGET == nState then
            print("[HonorJourney_CheckTaskStatus] 进行设置 nID = "..nID)
            HonorJourney_SetTaskStatus(human, nID, CommonDefine.COMMON_PRIZE_STATE_CANGET)

            -- local msgRet = Msg.gc.GC_WEEKSHARE_SET
            -- msgRet.type = 5
            -- msgRet.desc = tConf.taskDesc
            -- Msg.send(msgRet,human.fd)
        end
    end
end

-- 检测任务积分奖励状态
local function HonorJourney_CheckTaskPointStatus(human)
    local nNowPoint = HonorJourney_GetTaskPoint(human)
    for nID, v in ipairs(HonorJourneyExcel.Reward) do
        if v.needcnt > nNowPoint then
            break
        end

        local nState = HonorJourney_GetTaskPointPrize(human, nID)
        if nState == CommonDefine.COMMON_PRIZE_STATE_NOGET and nNowPoint >= v.needcnt then
            HonorJourney_SetTaskPointPrize(human, nID, CommonDefine.COMMON_PRIZE_STATE_CANGET)
        end
    end
end

local function getIDbyEventAndArgumentType(nEventType, nArgumentType)
    local tTaskID = {}
    for id,cfg in pairs(HonorJourneyExcel.HonorJourney) do
        if cfg.triggerType == nEventType then
            if nArgumentType then
                local argumentNum = tonumber(string.sub(tostring(cfg.taskArgument), -1))
                if nArgumentType == argumentNum then
                    table.insert(tTaskID, id)
                end
            else
                table.insert(tTaskID, id)
            end
        end
    end

    return tTaskID
end

-- 获取前置任务
local function HonorJourney_Gettaskpre(id)
    if not id or not HonorJourneyExcel.HonorJourney or not HonorJourneyExcel.HonorJourney[id] then
        return nil
    end
    
    local taskPre = HonorJourneyExcel.HonorJourney[id].taskPre
    if type(taskPre) ~= "number" then
        return nil
    end
    
    return taskPre
end

local function HonorJourneyTask_CheckFinshTask(human)
    -- 检查新加的任务
    local tTaskPrize = human.db.HonorJourney.TaskPrize
    for nID, v in pairs(HonorJourneyExcel.HonorJourney) do
        if not tTaskPrize[nID] then
            tTaskPrize[nID] = {
                nFinishNum = 0,
                nState = CommonDefine.COMMON_PRIZE_STATE_NOGET
            }
        end
    end

    -- 检测凤凰等级
    if human.db.moshou and human.db.moshou.lv then
        local nNowFengHuangLv = human.db.moshou.lv
        -- print("[HonorJourneyTask_CheckFinshTask] 当前凤凰等级 nNowFengHuangLv = "..nNowFengHuangLv)
        for nID, v in pairs(HonorJourneyExcel.HonorJourney) do
            if v.triggerType == TriggerDefine.FENGHUANGLV then
                local nState = HonorJourney_GetTaskStatus(human,  nID)
                if nNowFengHuangLv >= v.taskProcess and nState == CommonDefine.COMMON_PRIZE_STATE_NOGET then
                    print("[HonorJourneyTask_CheckFinshTask] 当前凤凰等级大于任务配置的等级 nNowFengHuangLv = "..nNowFengHuangLv.." taskProcess = "..v.taskProcess)

                    HonorJourneyTask_SetTaskFinishNum(human, nID, nNowFengHuangLv)
                    HonorJourney_CheckTaskStatus(human, nID)
                end
            end
        end
    else
        print("[HonorJourneyTask_CheckFinshTask] 不存在对应的凤凰数据 name = "..human.db.name)
    end

    -- 检测遗迹通关数
    local layerData = YjTreasureDBLogic.getLayerDataByUuid(human.db._id)
    if layerData and layerData.layer then
        local nowLayer = layerData.layer
        for nID, v in pairs(HonorJourneyExcel.HonorJourney) do
            if v.triggerType == TriggerDefine.YJTREASURE_PASS_LEVEL then
                local nState = HonorJourney_GetTaskStatus(human,  nID)
                if nowLayer >= v.taskProcess and nState == CommonDefine.COMMON_PRIZE_STATE_NOGET then
                    -- print("[HonorJourneyTask_CheckFinshTask] 当前遗迹通关数大于任务配置的等级 nowLayer = "..nowLayer.." taskProcess = "..v.taskProcess)

                    HonorJourneyTask_SetTaskFinishNum(human, nID, nowLayer)
                    HonorJourney_CheckTaskStatus(human, nID)
                elseif nowLayer < v.taskProcess and nState == CommonDefine.COMMON_PRIZE_STATE_NOGET then
                    local nNowNum = HonorJourney_GetTaskFinishNum(human, nID)
                    if nNowNum < nowLayer then
                        HonorJourneyTask_SetTaskFinishNum(human, nID, nowLayer)
                    end
                end
            end
        end
    end

    -- 检查是否获得指定英雄
    for nID, v in pairs(HonorJourneyExcel.HonorJourney) do
        if v.triggerType == TriggerDefine.EVENT_TYPE_GETPOINTHERO then
            local nState = HonorJourney_GetTaskStatus(human,  nID)
            if nState == CommonDefine.COMMON_PRIZE_STATE_NOGET then
                local tTaskArgument = Util.split(v.taskArgument , "|")
                if 2 == #tTaskArgument then
                    local nHeroID, nHeroStar = tonumber(tTaskArgument[1]), tonumber(tTaskArgument[2])
                    local tListHeroIndex = HeroLogic.getHeroListById(human, nHeroID)
                    if nil ~= _G.next(tListHeroIndex) then
                        for _, nIndex in ipairs(tListHeroIndex) do
                            local tHeroGrid = HeroLogic.getHeroGrid(human, nHeroID, nIndex)
                            if tHeroGrid.star and tHeroGrid.id then
                                -- print("[HonorJourneyTask_CheckFinshTask] 检测获得指定英雄 nHeroID = "..nHeroID.." star = "..tHeroGrid.star)
                                if tHeroGrid.star >= nHeroStar and tHeroGrid.id == nHeroID then
                                    HonorJourneyTask_AddTaskFinishNum(human, nID, 1)
                                    HonorJourney_CheckTaskStatus(human, nID)
                                end
                            end
                        end
                    end
                    
                end
            end
        end
    end

end

function HonorJourneyTaskQuery(human, isOnLogin)
    -- print("当前进入荣耀历程查询")
    local sendErr = isOnLogin == false and true or false
	if RoleSystemLogic.isOpen(human, RoleSystemDefine.ROLE_SYS_ID_803, sendErr) ~= true then
		return
	end

    -- 先组装活跃数据
    local nNowPoint = HonorJourney_GetTaskPoint(human)
    local msgRet = Msg.gc.GC_HONORJOURNEY_QUERY
	msgRet.nowHuoYue = nNowPoint
	msgRet.huoYueBox[0] = 1
    local ret = 1
    -- 初始化box数据结构
    for nID, v in ipairs(HonorJourneyExcel.Reward) do
        local nState = HonorJourney_GetTaskPointPrize(human, nID)
        -- print("[HonorJourneyTaskQuery] nID = "..nID .." nState = "..nState)
        if nState ~= CommonDefine.COMMON_PRIZE_STATE_GET then
            msgRet.huoYueBox[ret].boxID = nID
            msgRet.huoYueBox[ret].needHuoYue = v.needcnt
            msgRet.huoYueBox[ret].items[0] = #v.reward
            for nIndex, tGoods in ipairs(v.reward) do
                local itemID = tGoods[1]
                local itemCnt = tGoods[2]
                
                Grid.makeItem(msgRet.huoYueBox[ret].items[nIndex], itemID, itemCnt)
            end
            
            msgRet.huoYueBox[ret].isGet = 0
            break
        end
    end


    -- 组装荣耀数据
	local HonorJourneyConfig = HonorJourneyExcel.HonorJourney
	if not HonorJourneyConfig then return end

    if not HONORJOURNEY_LEN then
        HONORJOURNEY_LEN = 0
        for nID, _ in pairs(HonorJourneyConfig) do 
            HONORJOURNEY_LEN = HONORJOURNEY_LEN + 1
        end
    end
	
    local nFirst, cnt, nSendLen = 1, 0, 0
    for nID, value in pairs(HonorJourneyConfig) do 
        local nPreTaskID = HonorJourney_Gettaskpre(nID)
        local nState = HonorJourney_GetTaskStatus(human, nID)
        nSendLen = nSendLen + 1
        --print("[HonorJourneyTaskQuery] nID = "..nID.." nState = "..nState.."\n")
        if CommonDefine.COMMON_PRIZE_STATE_GET ~= nState then
            local bSow = value.isVisiable == HONORJOURNEY_SHOWTASK and true or false

            -- 获取前置任务状态
            if nPreTaskID then
                local nPreTaskState = HonorJourney_GetTaskStatus(human, nPreTaskID)
                if CommonDefine.COMMON_PRIZE_STATE_GET == nPreTaskState then
                    bSow = true
                else
                    bSow = false
                end
            else
                if CommonDefine.COMMON_PRIZE_STATE_CANGET == nState then
                    bSow = true
                end
            end

            --print("[HonorJourneyTaskQuery] nID = "..nID.." bSow = "..(bSow == true and 1 or 0))
            -- 能领取了都要显示出来
            -- if CommonDefine.COMMON_PRIZE_STATE_CANGET == nState then
            --     bSow = true
            -- end

            -- 显示才下发
            if true == bSow then
                cnt = cnt + 1
                msgRet.list[cnt].taskID = nID
                msgRet.list[cnt].desc = value.taskDesc
                msgRet.list[cnt].maxCnt = value.taskProcess
    
                local len = 0
                for i = 1, #value.reward do 
                    len = len + 1
                    local itemId = value.reward[i][1]
                    local itemCnt = value.reward[i][2]
                    Grid.makeItem(msgRet.list[cnt].itemData[len], itemId, itemCnt)
                end
                msgRet.list[cnt].itemData[0] = len
                msgRet.list[cnt].jump = value.panelID or 0

                msgRet.list[cnt].finishCnt = HonorJourney_GetTaskFinishNum(human, nID)
                msgRet.list[cnt].isGet = (nState == CommonDefine.COMMON_PRIZE_STATE_GET) and 1 or 0

                if cnt >= 30 then
                    msgRet.list[0] = cnt
                    msgRet.nFirst = nFirst
                    msgRet.nEnd = nSendLen == HONORJOURNEY_LEN and 1 or 0
                    -- print("[HonorJourneyTaskQuery] 发送数据 cnt = "..cnt.." nFirst = "..nFirst.." nSendLen = "..nSendLen)
                    Msg.send(msgRet, human.fd)
                    nFirst = 0
                    cnt = 0
                end
            end
        end
    end

    -- print("[HonorJourneyTaskQuery] 循环遍历结束 cnt = "..cnt.." nFirst = "..nFirst.." nSendLen = "..nSendLen)

    msgRet.list[0] = cnt
    msgRet.nFirst = nFirst
    msgRet.nEnd = 1
    -- print("[HonorJourneyTaskQuery] 最后一次发送数据 cnt = "..cnt.." nFirst = "..nFirst.." nSendLen = "..nSendLen)
    Msg.send(msgRet, human.fd)
end

function onHonorJourneyTaskHuoYue(human,sum)
	if not human.db.HonorJourney then return end

	human.db.HonorJourney.nowHuoYue = (human.db.HonorJourney.nowHuoYue or 0) + sum

    YunYingLogic.onCallBack(human, "onDailyTask",sum)   
end

--统计奖励
local function calcReward(human, awardCfg)
	for i = 1, #awardCfg do
		local itemId = awardCfg[i][1]
		local itemCnt = awardCfg[i][2]
        if itemId == ItemDefine.ITME_HUOYUE_ID then
            onHonorJourneyTaskHuoYue(human,itemCnt)
        else
			local newCnt = math.ceil(itemCnt)
			--BagLogic.updateMomentItem(2, itemId, itemCnt)
			BagLogic.updateMomentItem(2, itemId, newCnt)
        end
	end
end

function HonorJourneyTaskGetReward(human)
    if RoleSystemLogic.isOpen(human, RoleSystemDefine.ROLE_SYS_ID_803) ~= true then
        print("[HonorJourneyTaskGetReward] 未开启直接返回")
		return
	end

    -- print("[HonorJourneyTaskGetReward] 1111111")
    local tAddItem, nAddPoint = {}, 0
	for nID, v in pairs(HonorJourneyExcel.HonorJourney) do
        local nState = HonorJourney_GetTaskStatus(human, nID)
        -- print("[HonorJourneyTaskGetReward] nID = "..nID.." nState = "..nState)
        if CommonDefine.COMMON_PRIZE_STATE_CANGET == nState then
            HonorJourney_SetTaskStatus(human, nID, CommonDefine.COMMON_PRIZE_STATE_GET)
            for _, tGoods in ipairs(v.reward) do
                local nGoodsID = tGoods[1]
                local nGoodsNum = tGoods[2]
                if not tAddItem[nGoodsID] and nGoodsID ~= ItemDefine.ITEM_HONOR_ID then
                    tAddItem[nGoodsID] = 0
                end
                
                if nGoodsID ~= ItemDefine.ITEM_HONOR_ID then
                    tAddItem[nGoodsID] = tAddItem[nGoodsID] + nGoodsNum
                else 
                    nAddPoint = nAddPoint + nGoodsNum
                end
            end
            -- nAddPoint = nAddPoint + v.taskCnt
        end
    end

	if nil ~= _G.next(tAddItem) then
		local tAddGoods = {}
        for nGoodsID, nGoodsNum in pairs(tAddItem) do
            table.insert(tAddGoods, {nGoodsID, nGoodsNum})
        end

        BagLogic.addItemList(human, tAddGoods, "honorjourney")

        -- 增加积分，并更新状态
        HonorJourney_AddTaskPoint(human, nAddPoint)
        HonorJourney_CheckTaskPointStatus(human)

		HonorJourneyTaskQuery(human)
		RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_803)
	end
end


local function huoYueBoxCanGet(human,boxID)
	local HonorJourneyDB = human.db.HonorJourney
	if not HonorJourneyDB then return end

	local isGet = HonorJourneyDB.huoYueBox and HonorJourneyDB.huoYueBox[boxID] or 0
	if isGet ~= 0 then return end

	local nowHuoYue = HonorJourneyDB.nowHuoYue or 0
	local huoYueBoxConfig = HonorJourneyExcel.Reward[boxID]
	if nowHuoYue <= 0 or nowHuoYue < huoYueBoxConfig.needHuoYue then return end

	return true
end

-- 领取活跃宝箱
function CG_HONORJOURNEYTASK_GET_HUOYUEBOX(human, nBoxID)
    local tConfig = HonorJourneyExcel.Reward[nBoxID]
    if not tConfig then
        return
    end

    local nNowPoint = HonorJourney_GetTaskPoint(human)
    if nNowPoint < tConfig.needcnt then
        return
    end

    local nState = HonorJourney_GetTaskPointPrize(human, nBoxID)
    if nState ~= CommonDefine.COMMON_PRIZE_STATE_CANGET then
        return
    end

    HonorJourney_SetTaskPointPrize(human, nBoxID, CommonDefine.COMMON_PRIZE_STATE_GET)

    BagLogic.addItemList(human, tConfig.reward, "honorjourney")
    HonorJourneyTaskQuery(human)
    RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_803)
end


function isDot(human)
	-- 主界面的任务包含2个标签 每日任务/成就
	if RoleSystemLogic.isOpen(human, RoleSystemDefine.ROLE_SYS_ID_803) ~= true then
        print("[HonorJourney_isDot] 当前系统未开放直接返回false")
		return false
	end

    HonorJourneyTask_CheckCreateDB(human)

    for nID, v in pairs(HonorJourneyExcel.HonorJourney) do
        local nState = HonorJourney_GetTaskStatus(human, nID)
        if nState == CommonDefine.COMMON_PRIZE_STATE_CANGET then
            return true
        end
    end

	for nID, v in ipairs(HonorJourneyExcel.Reward) do
        local nState = HonorJourney_GetTaskPointPrize(human, nID)
        if nState == CommonDefine.COMMON_PRIZE_STATE_CANGET then
            return true
        end
	end
	
	return false
end

-- 事件处理
local tEventHandler = nil
-- 起服处理
local function HonorJourneyTask_EvnentCommon(nEventType, uuid, nValue, nArgumentType)
    -- print("当前进入荣耀历程增加次数..............................",nEventType, nArgumentType)
    local human = ObjHuman.onlineUuid[uuid]
    if not human then
        print("[HonorJourneyTask_EvnentCommon] 玩家不在线了？ nEventType = "..nEventType)
        return
    end

    local tTaskID = getIDbyEventAndArgumentType(nEventType, nArgumentType)
    if nil ~= _G.next(tTaskID) then
        for _, nID in ipairs(tTaskID) do
            local nStatus = HonorJourney_GetTaskStatus(human, nID)
            if nStatus == CommonDefine.COMMON_PRIZE_STATE_NOGET then
                HonorJourneyTask_AddTaskFinishNum(human,nID, nValue)
                HonorJourney_CheckTaskStatus(human, nID)
            end
        end
        RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_803)
    end
end

local function HonorJourney_EvnentGetHero(nEventType, uuid, nValue1, nValue2)
    local human = ObjHuman.onlineUuid[uuid]
    if not human then
        print("[HonorJourney_EvnentGetHero] 不存在对应的人物直接返回 nEventType = "..nEventType.." uuid = "..uuid)
        return
    end

    local nNum = nValue1
    local nStar = nValue2
    print("[HonorJourney_EvnentGetHero] nEventType = "..nEventType.." nNum = "..nNum.." nStar = "..nStar.." name = "..human.db.name)

    local bCheck, nTaskID = false, 0
    for nID, v in pairs(HonorJourneyExcel.HonorJourney) do
        if v.triggerType == nEventType then
            if not v.taskArgument or v.taskArgument == "" then
                print("[HonorJourney_EvnentGetHero] 获得英雄但是没有获取到对应的配置参数要求")
            else
                local tTaskArgument = Util.split(v.taskArgument , "|")
                if 2 ~= #tTaskArgument then
                    print("[HonorJourneyTask_EvnentGetHero] 获取的任务 参数数量不正确")
                else
                    if nNum >= tonumber(tTaskArgument[1]) and nStar == tonumber(tTaskArgument[2]) then
                        local nStatus = HonorJourney_GetTaskStatus(human, nID)
                        if nStatus == CommonDefine.COMMON_PRIZE_STATE_NOGET then
                            HonorJourneyTask_AddTaskFinishNum(human, nID, 1)
                            bCheck = true
                            nTaskID = nID
                        end
                        break
                    end
                end
            end
        end
    end

    --检查任务是否完成
    if true == bCheck then
        HonorJourney_CheckTaskStatus(human, nTaskID)
        RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_803)
    end
end

-- 战斗力改变
local function HonorJourney_PowerChange(nEventType, uuid, nValue1, nValue2)
    local human = ObjHuman.onlineUuid[uuid]
    if not human then
        return
    end

    local tTaksID = {}
    for nID, v in pairs(HonorJourneyExcel.HonorJourney) do
        if v.triggerType == nEventType then
            local nState = HonorJourney_GetTaskStatus(human, nID)
            if nValue1 >= v.taskProcess and nState == CommonDefine.COMMON_PRIZE_STATE_NOGET then
                table.insert(tTaksID, nID)
            end
        end
    end

    if tTaksID and nil ~= _G.next(tTaksID) then
        for _, nID in pairs(tTaksID) do
            HonorJourneyTask_SetTaskFinishNum(human, nID, nValue1)
            HonorJourney_CheckTaskStatus(human, nID)

            -- HonorJourney_SetTaskStatus(human, nID, CommonDefine.COMMON_PRIZE_STATE_CANGET)
        end

        RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_803)
    end
end

-- 凤凰等级
local function HonorJourney_FengHuangLv(nEventType, uuid, nValue1, nValue2)
    local human = ObjHuman.onlineUuid[uuid]
    if not human then
        return
    end

    local tTaksID = {}
    for nID, v in pairs(HonorJourneyExcel.HonorJourney) do
        if v.triggerType == nEventType then
            local nState = HonorJourney_GetTaskStatus(human, nID)
            if nValue1 >= v.taskProcess and nState == CommonDefine.COMMON_PRIZE_STATE_NOGET then
                table.insert(tTaksID, nID)
            end
        end
    end

    if tTaksID and nil ~= _G.next(tTaksID) then
        for _, nID in pairs(tTaksID) do
            HonorJourneyTask_SetTaskFinishNum(human, nID, nValue1)
            -- HonorJourney_SetTaskStatus(human, nID, CommonDefine.COMMON_PRIZE_STATE_CANGET)
            HonorJourney_CheckTaskStatus(human, nID)
        end

        RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_803)
    end
end

-- 打开宝箱
local function HonorJourney_OpenBox(nEventType, uuid, nValue1, nBoxType)
    -- print("[HonorJourney_OpenBox] 进入打开宝箱回调 nValue1 = "..nValue1.." nBoxType = "..nBoxType)
    local human = ObjHuman.onlineUuid[uuid]
    if not human then
        return
    end

    local tTaksID = {}
    for nID, v in pairs(HonorJourneyExcel.HonorJourney) do
        if v.triggerType == nEventType then
            local nNeedBoxType = v.taskArgument
            local nState = HonorJourney_GetTaskStatus(human, nID)
            if nBoxType == nNeedBoxType and nState == CommonDefine.COMMON_PRIZE_STATE_NOGET then
                table.insert(tTaksID, nID)
            end
        end
    end

    if tTaksID and nil ~= _G.next(tTaksID) then
        for _, nID in pairs(tTaksID) do
            HonorJourneyTask_AddTaskFinishNum(human, nID, nValue1)
            HonorJourney_CheckTaskStatus(human, nID)
        end

        RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_803)
    else
        print("[HonorJourney_OpenBox] 不存在满足条件的任务")
    end
end

-- 获得装备或者符文
local function HonorJourneyTask_GetEquiporFuWen(nEventType, uuid, nQuality, nNum)
    local human = ObjHuman.onlineUuid[uuid]
    if not human then
        return
    end

    -- print("[HonorJourneyTask_GetEquiporFuWen] nEventType = "..nEventType.." nQuality = "..nQuality.." nNum = "..nNum)
    local tTaksID = {}
    for nID, v in pairs(HonorJourneyExcel.HonorJourney) do
        if v.triggerType == nEventType then
            local nState = HonorJourney_GetTaskStatus(human, nID)
            if nQuality == v.taskArgument and nState == CommonDefine.COMMON_PRIZE_STATE_NOGET then
                table.insert(tTaksID, nID)
            end
        end
    end

    if tTaksID and nil ~= _G.next(tTaksID) then
        for _, nID in pairs(tTaksID) do
            print("[HonorJourneyTask_GetEquiporFuWen] 进行判断的任务ID = "..nID)
            HonorJourneyTask_AddTaskFinishNum(human, nID, nNum)
            HonorJourney_CheckTaskStatus(human, nID)
        end

        RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_803)
    end
end

-- 秘宝或者遗物激活
local function HonorJourneyTask_ItemActivate(nEventType, uuid, nItemID, nItemNum)
    local human = ObjHuman.onlineUuid[uuid]
    if not human then
        return
    end

    -- print("[HonorJourneyTask_ItemActivate] nEventType = "..nEventType.." nItemID = "..nItemID.." nItemNum = "..nItemNum)

    local tTaksID = {}
    for nID, v in pairs(HonorJourneyExcel.HonorJourney) do
        if v.triggerType == nEventType then
            local nState = HonorJourney_GetTaskStatus(human, nID)
            if nItemID == v.taskArgument and nState == CommonDefine.COMMON_PRIZE_STATE_NOGET then
                table.insert(tTaksID, nID)
            end
        end
    end

    if tTaksID and nil ~= _G.next(tTaksID) then
        for _, nID in pairs(tTaksID) do
            -- print("[HonorJourneyTask_ItemActivate] 进行判断的任务ID = "..nID)
            HonorJourneyTask_AddTaskFinishNum(human, nID, nItemNum)
            HonorJourney_CheckTaskStatus(human, nID)
        end

        RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_803)
    end
end

-- 等级改变
local function HonorJourneyTask_LvChange(nEventType, uuid, nValue1, nValue2)
    local human = ObjHuman.onlineUuid[uuid]
    if not human then
        return
    end

    -- print("[HonorJourneyTask_LvChange] nEventType = "..nEventType.." nValue1 = "..nValue1.." nValue2 = "..nValue2)

    local tTaksID = {}
    for nID, v in pairs(HonorJourneyExcel.HonorJourney) do
        if v.triggerType == nEventType then
            local nState = HonorJourney_GetTaskStatus(human, nID)
            if nValue2 >= v.taskProcess and nState == CommonDefine.COMMON_PRIZE_STATE_NOGET then
                table.insert(tTaksID, nID)
            end
        end
    end

    if tTaksID and nil ~= _G.next(tTaksID) then
        for _, nID in pairs(tTaksID) do
            --  print("[HonorJourneyTask_ItemActivate] 进行判断的任务ID = "..nID)
            HonorJourneyTask_SetTaskFinishNum(human, nID, nValue2)
            HonorJourney_CheckTaskStatus(human, nID)
        end

        RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_803)
    end
end

-- 境界提升
local function HonorJourneyTask_JingjieLvChange(nEventType, uuid, nValue1, nValue2)
    local human = ObjHuman.onlineUuid[uuid]
    if not human then
        return
    end

    -- print("[HonorJourneyTask_JingjieLvChange] nEventType = "..nEventType.." nValue1 = "..nValue1.." nValue2 = "..nValue2)

    local tTaksID = {}
    for nID, v in pairs(HonorJourneyExcel.HonorJourney) do
        if v.triggerType == nEventType then
            local nState = HonorJourney_GetTaskStatus(human, nID)
            if nValue2 >= v.taskArgument and nState == CommonDefine.COMMON_PRIZE_STATE_NOGET then
                table.insert(tTaksID, nID)
            end
        end
    end

    if tTaksID and nil ~= _G.next(tTaksID) then
        for _, nID in pairs(tTaksID) do
            -- print("[HonorJourneyTask_JingjieLvChange] 进行判断的任务ID = "..nID)
            HonorJourneyTask_SetTaskFinishNum(human, nID, 1)
            HonorJourney_CheckTaskStatus(human, nID)
        end

        RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_803)
    end
end

-- 获得指定英雄
local function HonorJourneyTask_EvnentGetPointHero(nEventType, uuid, nValue1, nValue2, nValue3)
    local human = ObjHuman.onlineUuid[uuid]
    if not human then
        return
    end

    if not nValue1 or not nValue2 or not nValue3 then
        return
    end

    local nNum, nStar, nHeroID = nValue1, nValue2, nValue3
    local bCheck, nTaskID = false, 0

    print("[HonorJourneyTask_EvnentGetPointHero] nEventType = "..nEventType.." nNum = "..nNum.." nStar = "..nStar.." nHeroID = "..nHeroID.." name = "..human.db.name)

    for nID, v in pairs(HonorJourneyExcel.HonorJourney) do
        if v.triggerType == nEventType then
            if not v.taskArgument or v.taskArgument == "" then
                print("[HonorJourneyTask_EvnentGetPointHero] 获得英雄但是没有获取到对应的配置参数要求")
            else
                local tTaskArgument = Util.split(v.taskArgument , "|")
                local nHeroCofID, nHeroCofStar = tonumber(tTaskArgument[1]), tonumber(tTaskArgument[2])
                
                if nHeroID == nHeroCofID then
                    print("[HonorJourneyTask_EvnentGetPointHero] 配置的英雄ID = "..nHeroCofID.." 星级 = "..nHeroCofStar)
                    local nStatus = HonorJourney_GetTaskStatus(human, nID)
                    if nStar >= nHeroCofStar and CommonDefine.COMMON_PRIZE_STATE_NOGET == nStatus then
                        HonorJourneyTask_AddTaskFinishNum(human, nID, 1)
                        bCheck = true
                        nTaskID = nID
                        break
                    end
                end
            end
        end
    end

    if true == bCheck then
        HonorJourney_CheckTaskStatus(human, nTaskID)
        RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_803)
    end
end

local function HonorJourneyTask_EvnentGetJingLing(nEventType, uuid, nValue1, nValue2)
     local human = ObjHuman.onlineUuid[uuid]
    if not human then
        return
    end

    if not nValue1 or not nValue2 then
        return
    end

    local nNum = nValue1
    local nJingLingID = nValue2
    local bCheck, nTaskID = false, 0
    
    for nID, v in pairs(HonorJourneyExcel.HonorJourney) do
        if v.triggerType == nEventType then
            if v.taskArgument and v.taskArgument == nJingLingID then
                local nStatus = HonorJourney_GetTaskStatus(human, nID)
                if CommonDefine.COMMON_PRIZE_STATE_NOGET == nStatus then
                    HonorJourneyTask_AddTaskFinishNum(human, nID, 1)
                    bCheck = true
                    nTaskID = nID
                    break
                end
            end
        end
    end

     if true == bCheck then
        HonorJourney_CheckTaskStatus(human, nTaskID)
        RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_803)
    end
end

-- 每日登录
local function HonorJourneyTask_EvnentDailyLogin(nEventType, uuid, nValue1)
    local human = ObjHuman.onlineUuid[uuid]
    if not human then
        return
    end

    if not nValue1 then
        return
    end
    
    local tTaksID = {}
    for nID, v in pairs(HonorJourneyExcel.HonorJourney) do
        if v.triggerType == nEventType then
            local nState = HonorJourney_GetTaskStatus(human, nID)
            if nState == CommonDefine.COMMON_PRIZE_STATE_NOGET then
                table.insert(tTaksID, nID)
            end
        end
    end

    if tTaksID and nil ~= _G.next(tTaksID) then
        for _, nID in ipairs(tTaksID) do
            HonorJourneyTask_AddTaskFinishNum(human, nID, nValue1)
            HonorJourney_CheckTaskStatus(human, nID)
        end

        HonorJourney_UpdateDayLoginTime(human)
        RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_803)
    end
end


function Init()
    tEventHandler = {
        [TriggerDefine.CHAT_TIMES] = HonorJourneyTask_EvnentCommon,
        [TriggerDefine.FRIEND_COMBAT] = HonorJourneyTask_EvnentCommon,
        [TriggerDefine.EVENT_TYPE_FATESUMMON] = HonorJourneyTask_EvnentCommon,
        [TriggerDefine.EVENT_TYPE_OPENBOX] = HonorJourney_OpenBox,
        [TriggerDefine.EVENT_TYPE_GETHERO] = HonorJourney_EvnentGetHero,
        [TriggerDefine.EVENT_TYPE_SHOPBUY] = HonorJourneyTask_EvnentCommon,
        [TriggerDefine.EVENT_TYPE_ELITESUMMON] = HonorJourneyTask_EvnentCommon,
        [TriggerDefine.FUWEN_HECHENG] = HonorJourneyTask_EvnentCommon,
        [TriggerDefine.GOLD_POINTS_NUM] = HonorJourneyTask_EvnentCommon,
        [TriggerDefine.VOUCHER_BUY_NUM] = HonorJourneyTask_EvnentCommon,
        [TriggerDefine.DRAGON_PEARL] = HonorJourneyTask_EvnentCommon,
        [TriggerDefine.ANCIENTG_CHARGING] = HonorJourneyTask_EvnentCommon,
        [TriggerDefine.SECRET_REALM] = HonorJourneyTask_EvnentCommon,
        [TriggerDefine.ZHANDOULI] = HonorJourney_PowerChange,
        [TriggerDefine.FENGHUANGLV] = HonorJourney_FengHuangLv,
        [TriggerDefine.MOZHU_CHALLENGE] = HonorJourneyTask_EvnentCommon,
        [TriggerDefine.ZHANBU_SYNTHESIS] = HonorJourneyTask_EvnentCommon,
        [TriggerDefine.UNION_DONATE] = HonorJourneyTask_EvnentCommon,
        [TriggerDefine.EVENT_TYPE_SWEEP] = HonorJourneyTask_EvnentCommon,
        [TriggerDefine.FUWEN_GETQUALITY] = HonorJourneyTask_GetEquiporFuWen,
        [TriggerDefine.EQUIP_GETQUALITY] = HonorJourneyTask_GetEquiporFuWen,
        [TriggerDefine.FUWEN_RESET] = HonorJourneyTask_EvnentCommon,
        [TriggerDefine.EQUIP_REFINEMENT] = HonorJourneyTask_EvnentCommon,
        [TriggerDefine.HUMAN_LV_UP] = HonorJourneyTask_LvChange,
        [TriggerDefine.REALM_UPGRADE] = HonorJourneyTask_JingjieLvChange,
        [TriggerDefine.YJTREASURE_PASS_LEVEL] = HonorJourneyTask_EvnentCommon,
        [TriggerDefine.MIBAO_ACTIVATE] = HonorJourneyTask_ItemActivate,
        [TriggerDefine.YIWU_ACTIVATE] = HonorJourneyTask_ItemActivate,
        [TriggerDefine.ZHONGZU_LEVEL] = HonorJourneyTask_ItemActivate,
        [TriggerDefine.MIBAO_ALLSTAR] = HonorJourneyTask_EvnentCommon,
        [TriggerDefine.YIWU_ALLSTAR] = HonorJourneyTask_EvnentCommon,
        [TriggerDefine.EVENT_TYPE_GETPOINTHERO] = HonorJourneyTask_EvnentGetPointHero,
        [TriggerDefine.EVENT_TYPE_JINGLING_GET] = HonorJourneyTask_EvnentGetJingLing,
        [TriggerDefine.EVENT_TYPE_JINGLING_STAR] = HonorJourneyTask_EvnentCommon,
        [TriggerDefine.EVENT_TYPE_DAILY_LOGIN] = HonorJourneyTask_EvnentDailyLogin,
    }
end

--订阅事件

local function HonorJourney_SubEvent(uuid, nEventType)
    -- print("当前进入荣耀历程订阅事件 注册 nEventType = "..nEventType)
    TriggerLogic.SubscribeEvent(nEventType, uuid, HonorJourneyTask_EventCallBack)
end

local function HonorJourney_OnLoginSubEvent(human)
    if not human then
        print("[HonorJourney_OnLoginSubEvent] 不存在对应的人物, 订阅事件失败")
        return
    end

    local uuid = human.db._id
    HonorJourney_SubEvent(uuid, TriggerDefine.EVENT_TYPE_OPENBOX)
    HonorJourney_SubEvent(uuid, TriggerDefine.EVENT_TYPE_GETHERO)
    HonorJourney_SubEvent(uuid, TriggerDefine.FUWEN_HECHENG)
    HonorJourney_SubEvent(uuid, TriggerDefine.MOZHU_CHALLENGE)
    HonorJourney_SubEvent(uuid, TriggerDefine.ZHANBU_SYNTHESIS)
    HonorJourney_SubEvent(uuid, TriggerDefine.EVENT_TYPE_SHOPBUY)
    HonorJourney_SubEvent(uuid, TriggerDefine.GOLD_POINTS_NUM)
    HonorJourney_SubEvent(uuid, TriggerDefine.UNION_DONATE)
    HonorJourney_SubEvent(uuid, TriggerDefine.VOUCHER_BUY_NUM)
    HonorJourney_SubEvent(uuid, TriggerDefine.EVENT_TYPE_FATESUMMON)
    HonorJourney_SubEvent(uuid, TriggerDefine.EVENT_TYPE_ELITESUMMON)
    HonorJourney_SubEvent(uuid, TriggerDefine.FRIEND_COMBAT)
    HonorJourney_SubEvent(uuid, TriggerDefine.ZHANDOULI)
    HonorJourney_SubEvent(uuid, TriggerDefine.FENGHUANGLV)
    HonorJourney_SubEvent(uuid, TriggerDefine.CHAT_TIMES)
    HonorJourney_SubEvent(uuid, TriggerDefine.DRAGON_PEARL)
    HonorJourney_SubEvent(uuid, TriggerDefine.ANCIENTG_CHARGING)
    HonorJourney_SubEvent(uuid, TriggerDefine.FUWEN_GETQUALITY)
    HonorJourney_SubEvent(uuid, TriggerDefine.EQUIP_GETQUALITY)
    HonorJourney_SubEvent(uuid, TriggerDefine.EVENT_TYPE_SWEEP)
    -- 符文重铸次数
    HonorJourney_SubEvent(uuid, TriggerDefine.FUWEN_RESET)
    -- 装备洗练
    HonorJourney_SubEvent(uuid, TriggerDefine.EQUIP_REFINEMENT)
    -- 境界提升
    HonorJourney_SubEvent(uuid, TriggerDefine.REALM_UPGRADE)
    -- 遗迹探险层数
    HonorJourney_SubEvent(uuid, TriggerDefine.YJTREASURE_PASS_LEVEL)
    -- 秘宝激活
    HonorJourney_SubEvent(uuid, TriggerDefine.MIBAO_ACTIVATE)
    -- 遗物激活
    HonorJourney_SubEvent(uuid, TriggerDefine.YIWU_ACTIVATE)
    -- 种族试炼
    HonorJourney_SubEvent(uuid, TriggerDefine.ZHONGZU_LEVEL)
    -- 等级提升
    HonorJourney_SubEvent(uuid, TriggerDefine.HUMAN_LV_UP)
    HonorJourney_SubEvent(uuid, TriggerDefine.MIBAO_ALLSTAR)
    HonorJourney_SubEvent(uuid, TriggerDefine.YIWU_ALLSTAR)

    -- 获取指定英雄
    HonorJourney_SubEvent(uuid, TriggerDefine.EVENT_TYPE_GETPOINTHERO)
    HonorJourney_SubEvent(uuid, TriggerDefine.EVENT_TYPE_JINGLING_GET)

    HonorJourney_SubEvent(uuid, TriggerDefine.EVENT_TYPE_JINGLING_STAR)

    -- 每日登录
    HonorJourney_SubEvent(uuid, TriggerDefine.EVENT_TYPE_DAILY_LOGIN)
end

-- 回调，增加次数
function HonorJourneyTask_EventCallBack(nEventType, uuid, nValue1, nValue2, nValue3)
    if not tEventHandler then
        Init()
    end

    if not tEventHandler or not tEventHandler[nEventType] then
        print("当前已经返回",nEventType)
        return
    end

    -- print("[HonorJourneyTask_EventCallBack] 触发事件回调 nEventType = "..nEventType.." nValue1 = "..nValue1)
    pcall(tEventHandler[nEventType], nEventType, uuid, nValue1, nValue2, nValue3)
    -- print("[HonorJourneyTask_EventCallBack] 触发事件回调 处理结束 ")
end


local function dailyLoginCheck(human)
    local dayLoginTime = HonorJourney_GetDayLoginTime(human)
    local now = os.time()

    if not dayLoginTime or not Util.isSameDayByTimes(dayLoginTime,now) then
        HonorJourneyTask_EvnentDailyLogin(TriggerDefine.EVENT_TYPE_DAILY_LOGIN, human.db._id, 1)
    end
end


function onLogin(human)	
    -- print("当前进入荣耀历程登录")

    HonorJourneyTask_CheckCreateDB(human)

    HonorJourneyTask_CheckFinshTask(human)

    HonorJourney_OnLoginSubEvent(human)

	-- HonorJourneyTaskQuery(human, true)

    dailyLoginCheck(human)
end

function HonorJourney_GMClear(human)
    HonorJourneyTask_CreateDB(human)
    -- print("[HonorJourney_GMClear] 重置数据成功")
end

function HonorJourney_GMAddPoint(human, nAddPoint)
    HonorJourney_AddTaskPoint(human, nAddPoint)
    HonorJourney_CheckTaskPointStatus(human)
end