local Msg = require("core.Msg")
local CommonDB = require("common.CommonDB")
local relicModule = require("excel.ServerRelic")  
local Grid = require("bag.Grid")
local BagLogic = require("bag.BagLogic")
local RoleAttr = require("role.RoleAttr")
local ObjHuman = require("core.ObjHuman")
local RoleDefine = require("role.RoleDefine")
local Lang = require("common.Lang")
local Broadcast = require("broadcast.Broadcast")
local HuanJingTowerLogic = require("huanjingTower.HuanjingTowerLogic")
local HeroConfig = require("excel.hero").hero
local RoleSystemDefine = require("roleSystem.RoleSystemDefine")
local HeroLogic = require("hero.HeroLogic")
local RoleSystemLogic = require("roleSystem.RoleSystemLogic")
local Util = require("common.Util")
local TriggerDefine = require("trigger.TriggerDefine")
local TriggerLogic = require("trigger.TriggerLogic")
local TalismanLogic = require("talisman.TalismanLogic")
local ChengjiuLogic = require("chengjiu.ChengjiuLogic")


local GiftLogic

local LOGTYPE = "WinnerRelic"
local relicUpgradeConfig = relicModule.RelicUpgrade  -- 获取升级配置表
local relicDataConfig = relicModule.RelicData       -- 获取遗物数据表
local COND_TOWER_LEVEL = 500  --开启本系统需要通关恶魔之塔的层数
local WINNERRELIC_HEROSTAR = 12     -- 需要穿戴的星级
local WINNERRELIC_2_CHENGJIU_TASKID = 405 -- 圣遗物系统对应的成就任务Id
--[[
    DB =
    {{    	 
        -- 激活状态
        status ={
         --{[1601] = 3,(star级别)
		 --[1602] = 0,
        }
		-- 装备状态（整合到同一命名空间）
		--equipped = {
			 [1601] = {heoidx, heoidx}
		-- }}
]]


-- 获取来自秘宝的属性倍数加成
local function getAttrMulFromTalisman(human)
    local attrMul = TalismanLogic.getTalismanAdd(human, TalismanLogic.OTHER_EFFECT_TBL.WinnerRelic_Attr_Mul)
    attrMul = attrMul / 100
    return attrMul
end


--判断是否开启圣者遗物
local function isOpen(human)
    -- local towerLevel = HuanJingTowerLogic.getTowerLevel(human)
    -- if towerLevel >= COND_TOWER_LEVEL then
    --     return true
    -- end
    -- return false

    local state = ChengjiuLogic.GetTaskState(human, WINNERRELIC_2_CHENGJIU_TASKID)
    if state == 2 then
        return true
    end

    return false
end
-- 获取对应类型的遗物
local function generateCfgByType(type_m)
   local tbl = {}
   for relicId, relicCfg in pairs(relicDataConfig) do
       if relicCfg.nType == type_m then
            tbl[relicId] = relicCfg
       end
   end
   if not next(tbl) then
      return nil
   end
   return tbl
end
-- 红点判断
local function dotJudgment(human, cfgHtbl)
    -- print("当前进入遗物类型红点判断")
    local relicData = human.db.relicListData
    for relicId, relicCfg in pairs(cfgHtbl) do
        local nLevel = 0
        if relicData and relicData.status and relicData.status[relicId] then
            nLevel = relicData.status[relicId]
        end
        -- print("当前遗物等级",nLevel)
        if nLevel == nil or nLevel == 0 then
            if BagLogic.getItemCnt(human, relicCfg.nProp) >= 1 then
                return true
            end
        elseif nLevel +1 <= 10 then
            local num = relicUpgradeConfig and relicUpgradeConfig[nLevel +1] and relicUpgradeConfig[nLevel +1].CostNum or 0
            if BagLogic.getItemCnt(human, relicCfg.nProp) >= num then
                return true
            end
        end
    end
    -- print("当前不包含红点")
    return false
end

local function AddTableAttr(tNode, tConfig, tConfig2,tstar, attrMul)
    attrMul = 1 + attrMul

    tNode[0] = 4
    tNode[1].key = tConfig.HP
    tNode[1].value = tConfig2.nLife* tstar * attrMul
    tNode[2].key = tConfig.ATK
    tNode[2].value= tConfig2.nAttack*tstar * attrMul
    tNode[3].key = tConfig.DEF
    tNode[3].value = tConfig2.nDefense*tstar * attrMul
    tNode[4].key = tConfig.SPEED
    tNode[4].value = tConfig2.nSpeed*tstar * attrMul
end

local function AddTableAttr2(tNode, tConfig, tConfig2)
    tNode[0] = 4
    tNode[1].key = tConfig.HP
    tNode[1].value = tConfig2.nLife
    tNode[2].key = tConfig.ATK
    tNode[2].value= tConfig2.nAttack
    tNode[3].key = tConfig.DEF
    tNode[3].value = tConfig2.nDefense
    tNode[4].key = tConfig.SPEED
    tNode[4].value = tConfig2.nSpeed
end

-- 获取所有圣遗物的总星级
local function WinnerRelic_GetAllStar(human)
    local nAllStar = 0
    if not human.db.relicListData or not human.db.relicListData.status then
        return nAllStar
    end

    local tDBData = human.db.relicListData.status
    for relicId, _ in pairs(relicDataConfig) do
        if tDBData[relicId] then
            nAllStar = nAllStar + tDBData[relicId]
        end
    end

    return nAllStar
end

function Listofrelics(human)
    -- if not isOpen(human) then
    --     return Broadcast.sendErr(human, Lang.COMMON_NOT_OPEN)
    -- end
	local tMsgData = Msg.gc.GC_SACRED_RELIC_QUERY
    tMsgData.nAllStar = WinnerRelic_GetAllStar(human)
    
	local RelicConfigs = tMsgData.SacredRelicConfigs
    local relicData = human.db.relicListData or {}  --数据库
    relicData.status = relicData.status or {}
    relicData.equipped = relicData.equipped or {}
    RelicConfigs[0] = 0
    local len = 0

    local attrMul = getAttrMulFromTalisman(human)

    for relicId, relicCfg in pairs(relicDataConfig) do
            len = len + 1
            RelicConfigs[0] = len
            local playerRelicData = (relicData and relicData.status and relicData.status[relicId]) or 0  -- 默认未激活
            local currentStar = playerRelicData or 0
            local msgEntry = RelicConfigs[len]
            -- 基础属性
            msgEntry.id = relicId 
            msgEntry.name = relicCfg.nName
            msgEntry.activateItem = relicCfg.nProp
            AddTableAttr(msgEntry.attr, RoleDefine, relicCfg, currentStar, attrMul)
            AddTableAttr2(msgEntry.nextattr, RoleDefine, relicCfg)
            msgEntry.effectDesc = relicCfg.nEffectdescription
            local bonusValues = {}
            if relicCfg.nLifeBonus then table.insert(bonusValues, relicCfg.nLifeBonus) end
            if relicCfg.nAttackBonus then table.insert(bonusValues, relicCfg.nAttackBonus) end
            if relicCfg.nDefenseBonus then table.insert(bonusValues, relicCfg.nDefenseBonus) end
            if relicCfg.nSpeedBonus then table.insert(bonusValues, relicCfg.nSpeedBonus) end
            msgEntry.effectData[0] = 4
            msgEntry.effectNextData[0] = 4
            msgEntry.effectMaxData[0] = 4
            for i, bonusArray in ipairs(bonusValues) do
                local replaceValue = currentStar > 0 and bonusArray[currentStar] or 0
                local replaceValueNext = currentStar + 1 > 0 and currentStar + 1 <= 10 and bonusArray[currentStar+1] or 0
                local replaceValueMax =  bonusArray and bonusArray[10] or 0
                -- effectDesc = string.gsub(effectDesc, "X%%", tostring(replaceValue).."%%", 1)
                msgEntry.effectData[i] = replaceValue
                msgEntry.effectNextData[i] = replaceValueNext
                msgEntry.effectMaxData[i] = replaceValueMax
            end
            msgEntry.type = relicCfg.nType
            local nextStar = currentStar + 1
            local costNum = nextStar <= 10 and relicUpgradeConfig and relicUpgradeConfig[nextStar] and relicUpgradeConfig[nextStar].CostNum or 0
            Grid.makeItem(msgEntry.upGradeCost, relicCfg.nProp, costNum)
            msgEntry.upGradeCost.cnt = costNum or 0
            msgEntry.starLevel = currentStar or 0
            if relicUpgradeConfig and relicUpgradeConfig[currentStar] then
                msgEntry.equipLimit = relicUpgradeConfig[currentStar].EquipHeroNum or 0
            else
                msgEntry.equipLimit = 0 
            end
            if relicData and relicData.equipped and relicData.equipped[relicId] then
                -- print("[GC_SACRED_RELIC_QUERY] 回复中的装备的遗物ID relicId = "..relicId)
                -- table.print_lua_table(relicData.equipped)
                msgEntry.heroIdx[0] = #relicData.equipped[relicId]
                for idx, rid in ipairs(relicData.equipped[relicId]) do
                    msgEntry.heroIdx[idx] = rid
                end
            else
                msgEntry.heroIdx = {}  -- 先初始化为空表
                msgEntry.heroIdx[0] = 0
            end
    end
    Msg.send(tMsgData, human.fd)
end

function ActiveandUpgrade(human,relicId)
    -- print("开始激活圣者遗物")
    local relicCfg = relicDataConfig[relicId]
    if not relicCfg then
        return Broadcast.sendErr(human, Lang.COMMON_COMFIG_ERROR)
    end
    human.db.relicListData = human.db.relicListData or {}
    human.db.relicListData.status = human.db.relicListData.status or {}
    local relicData=human.db.relicListData
    local playerRelicData = relicData and relicData.status and relicData.status[relicId] or 0
    local currentStar = playerRelicData or 0
    -- 检查是否已达到最大星级
    if currentStar >=10 then
        return Broadcast.sendErr(human, Lang.COMMON_MAX_LEVEL)
    end
    local nextStar = currentStar +1 
    if nextStar > 10 or not relicUpgradeConfig or not relicUpgradeConfig[nextStar] then
        return Broadcast.sendErr(human, Lang.COMMON_COMFIG_ERROR)
    end
    local costItemId = relicCfg and relicCfg.nProp
    local costNum = relicUpgradeConfig and relicUpgradeConfig[nextStar] and relicUpgradeConfig[nextStar].CostNum or 0
    if not costItemId or BagLogic.getItemCnt(human, costItemId) < costNum then
        return Broadcast.sendErr(human, Lang.COMMON_ITEM_NOT_ENOUGH)
    end
    BagLogic.delItem(human, costItemId, costNum, LOGTYPE)
    --更新数据
    playerRelicData = nextStar or 0
    human.db.relicListData.status = human.db.relicListData.status or {}
    human.db.relicListData.status[relicId] = playerRelicData
    --返回数据
    local attrMul = getAttrMulFromTalisman(human)
    local tMsgData = Msg.gc.GC_SACRED_RELIC_UPGRADE
	local RelicData = tMsgData.data or {}
    RelicData.id = relicId
    RelicData.name = relicCfg.nName
    RelicData.activateItem = relicCfg.nProp
    AddTableAttr(RelicData.attr, RoleDefine, relicCfg, nextStar, attrMul)
    AddTableAttr2(RelicData.nextattr, RoleDefine, relicCfg)
    local bonusValues = {}
    RelicData.effectDesc = relicCfg.nEffectdescription
    if relicCfg.nLifeBonus then table.insert(bonusValues, relicCfg.nLifeBonus) end
    if relicCfg.nAttackBonus then table.insert(bonusValues, relicCfg.nAttackBonus) end
    if relicCfg.nDefenseBonus then table.insert(bonusValues, relicCfg.nDefenseBonus) end
    if relicCfg.nSpeedBonus then table.insert(bonusValues, relicCfg.nSpeedBonus) end
    RelicData.effectData[0] = 4
    RelicData.effectNextData[0] = 4
    RelicData.effectMaxData[0] = 4
    for i, bonusArray in ipairs(bonusValues) do
        local replaceValue = nextStar > 0 and bonusArray[nextStar] or 0
        local replaceValueNext = nextStar + 1 > 0 and nextStar + 1 <= 10 and bonusArray[nextStar+1] or 0
        local replaceValueMax =  bonusArray and bonusArray[10] or 0
        -- effectDesc = string.gsub(effectDesc, "X%%", tostring(replaceValue).."%%", 1)
        RelicData.effectData[i] = replaceValue
        RelicData.effectNextData[i] = replaceValueNext
        RelicData.effectMaxData[i] = replaceValueMax
    end
    RelicData.type = relicCfg.nType or {}
    local costNum = nextStar+1 <= 10 and relicUpgradeConfig and relicUpgradeConfig[nextStar+1] and relicUpgradeConfig[nextStar+1].CostNum or 0
    Grid.makeItem(RelicData.upGradeCost, relicCfg.nProp, costNum)
    RelicData.upGradeCost.cnt = costNum or 0
    RelicData.starLevel = nextStar or 0
    if relicUpgradeConfig and relicUpgradeConfig[nextStar] then
        RelicData.equipLimit = relicUpgradeConfig[nextStar].EquipHeroNum or 0
    else
        RelicData.equipLimit = 0 
    end
    if relicData and relicData.equipped and relicData.equipped[relicId] then
        RelicData.heroIdx[0] = #relicData.equipped[relicId]
        for idx, rid in ipairs(relicData.equipped[relicId]) do
            RelicData.heroIdx[idx] = rid
            local heroID = HeroLogic.getHeroIdByIndex(human, rid)
            local heroGrid = HeroLogic.getHeroGrid(human, heroID, rid)
            if heroGrid and heroGrid.relic and heroGrid.relic.star then
                heroGrid.relic.star = nextStar or 0
            end
        end
    else
        RelicData.heroIdx = {}  -- 先初始化为空表
        RelicData.heroIdx[0] = 0
    end

    if nextStar == 1 then
        -- 激活
        TriggerLogic.PublishEvent(TriggerDefine.YIWU_ACTIVATE, human.db._id, relicId, 1)
    end
    
    -- 总星数
    TriggerLogic.PublishEvent(TriggerDefine.YIWU_ALLSTAR, human.db._id, 1)

    local allStars = WinnerRelic_GetAllStar(human)
    GiftLogic = GiftLogic or require("topup.GiftLogic")
    GiftLogic.trigger(human, GiftLogic.GIFT_WINNERRELIC_UPGRADE_STAR, {currentVal = allStars}, GiftLogic.GIFT_SEC_TYPE3)

    --更新英雄身上穿的遗物的星级

    -- tMsgData = relicData
    Msg.send(tMsgData, human.fd)
    --重算战力
    RoleAttr.cleanHeroAttrCache(human)
    RoleAttr.doCalc(human)
    ObjHuman.sendAttr(human, RoleDefine.ZHANDOULI)
    --刷新红点,
    nextStar = nextStar + 1
    costNum = nextStar <= 10 and relicUpgradeConfig and relicUpgradeConfig[nextStar] and relicUpgradeConfig[nextStar].CostNum or 0
    if nextStar > 10 or BagLogic.getItemCnt(human, costItemId) < costNum then
        local dotID = 0
        if relicCfg.nType  == 1 then
            dotID = RoleSystemDefine.ROLE_SYS_ID_2022
        elseif relicCfg.nType  == 2 then
            dotID = RoleSystemDefine.ROLE_SYS_ID_2023
        elseif relicCfg.nType == 3 then
            dotID = RoleSystemDefine.ROLE_SYS_ID_2024
        end
        RoleSystemLogic.onDot(human, dotID)
        --刷新入口处
        RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_2021)
    end

    Listofrelics(human)
end

function putmsg(human,RelicData,relicCfg,oldid,isexist)
    -- print("当前进入给old赋值")
    local relicId = oldid
    if isexist then
        RelicData.id = oldid
    else
        RelicData.id = 0
    end
    local relicData =human.db.relicListData or {}
    local playerRelicData = human.db.relicListData and human.db.relicListData.status and human.db.relicListData.status[oldid] or 0
    local currentStar = playerRelicData or 0
    RelicData.name = relicCfg.nName
    RelicData.activateItem = relicCfg.nProp

    local attrMul = getAttrMulFromTalisman(human)
    AddTableAttr(RelicData.attr, RoleDefine, relicCfg, currentStar, attrMul)
    AddTableAttr2(RelicData.nextattr, RoleDefine, relicCfg)
    RelicData.effectDesc = relicCfg.nEffectdescription
    local bonusValues = {}
    if relicCfg.nLifeBonus then table.insert(bonusValues, relicCfg.nLifeBonus) end
    if relicCfg.nAttackBonus then table.insert(bonusValues, relicCfg.nAttackBonus) end
    if relicCfg.nDefenseBonus then table.insert(bonusValues, relicCfg.nDefenseBonus) end
    if relicCfg.nSpeedBonus then table.insert(bonusValues, relicCfg.nSpeedBonus) end
    RelicData.effectData[0] = 4
    RelicData.effectNextData[0] = 4
    RelicData.effectMaxData[0] = 4
    for i, bonusArray in ipairs(bonusValues) do
        local replaceValue = currentStar > 0 and bonusArray[currentStar] or 0
        local replaceValueNext = currentStar + 1 > 0 and currentStar + 1 <= 10 and bonusArray[currentStar+1] or 0
        local replaceValueMax =  bonusArray and bonusArray[10] or 0
        -- effectDesc = string.gsub(effectDesc, "X%%", tostring(replaceValue).."%%", 1)
        RelicData.effectData[i] = replaceValue
        RelicData.effectNextData[i] = replaceValueNext
        RelicData.effectMaxData[i] = replaceValueMax
    end
    RelicData.type = relicCfg.nType
    local costNum = currentStar+1 <= 10 and relicUpgradeConfig and relicUpgradeConfig[currentStar+1] and relicUpgradeConfig[currentStar+1].CostNum or 0
    Grid.makeItem(RelicData.upGradeCost, relicCfg.nProp, costNum)
    RelicData.upGradeCost.cnt = costNum or 0
    RelicData.starLevel = currentStar or 0
    if relicUpgradeConfig and relicUpgradeConfig[currentStar] then
        RelicData.equipLimit = relicUpgradeConfig[currentStar].EquipHeroNum or 0
    else
        RelicData.equipLimit = 0 
    end
    -- 填充已装备英雄信息
    if relicData and relicData.equipped and relicData.equipped[relicId] then
        RelicData.heroIdx[0] = #relicData.equipped[relicId]
        for idx, rid in ipairs(relicData.equipped[relicId]) do
            RelicData.heroIdx[idx] = rid
        end
    else
        RelicData.heroIdx = {}  -- 先初始化为空表
        RelicData.heroIdx[0] = 0
    end
    -- print("当前结束old赋值")
end
-- 装备/卸下圣者遗物

function EquipeandRemove(human, heroID, heroIdx, relicId, operate)
    local relicCfg = relicDataConfig and relicDataConfig[relicId]
    local playerRelicData = human.db.relicListData and human.db.relicListData.status and human.db.relicListData.status[relicId] or 0
    local currentStar = playerRelicData or 0
    
    -- 检查系统是否开启
    if not isOpen(human) then
         return
    end
    --检查英雄是否存在
    local heroGrid = HeroLogic.getHeroGrid(human, heroID, heroIdx)
    if not heroGrid then
        return
    end
    --检查英雄是否达到13星
    if heroGrid.star < WINNERRELIC_HEROSTAR then
        return
    end
    -- 初始化遗物数据
    human.db.relicListData = human.db.relicListData or {}
    local relicData =human.db.relicListData or {}
    -- 装备操作

    local tMsgData = Msg.gc.GC_SACRED_RELIC_EQUIP
	local RelicData = tMsgData.olddata
    tMsgData.heroIdx = heroIdx
    local oldcfg = relicCfg
    local oldid = relicId
    local isexist, nIndex = false, -1
    if human.db and human.db.relicListData and human.db.relicListData.equipped then
        for id, heroList in pairs(human.db.relicListData.equipped) do
            for idx, heroid in pairs(heroList) do
                if heroid == heroIdx then
                    oldcfg = relicDataConfig and relicDataConfig[id]
                    oldid = id
                    isexist = true
                    nIndex = idx
                    break
                end
            end
            if isexist then
                break
            end
        end
    end
    putmsg(human,RelicData,oldcfg,oldid,isexist)

    if operate == 1 then
        local LEN = human.db.relicListData and human.db.relicListData.equipped and human.db.relicListData.equipped[relicId] and #human.db.relicListData.equipped[relicId]
        local star = human.db and human.db.relicListData and human.db.relicListData.status and human.db.relicListData.status[relicId] or 0
        local NumLimit = relicUpgradeConfig and relicUpgradeConfig[star] and relicUpgradeConfig[star].EquipHeroNum or 0
        if LEN and LEN >= NumLimit then
          return Broadcast.sendErr(human, Lang.RELIC_EQUIP_LIMIT)
        end
        -- print("当前进入装备操作")
        -- 如果当前英雄已经装备了遗物
        human.db.relicListData.equipped = human.db.relicListData.equipped or {}
        human.db.relicListData.equipped[relicId] = human.db.relicListData.equipped[relicId] or {}
        if heroGrid and heroGrid.relic and  heroGrid.relic.star and heroGrid.relic.star >0 then 
            -- 遍历找到对应的英雄索引并移除
            if isexist and -1 ~= nIndex then
                table.remove(relicData.equipped[oldid],nIndex)
                RelicData.heroIdx[0] = #relicData.equipped[oldid]

                for idx, rid in ipairs(relicData.equipped[oldid]) do
                    RelicData.heroIdx[idx] = rid
                end
            else
                RelicData.heroIdx = {}  -- 先初始化为空表
                RelicData.heroIdx[0] = 0
            end
        end
       heroGrid.relic = {}
       if not human.db.relicListData.equipped[relicId] then
          human.db.relicListData.equipped[relicId] = {}  -- 初始化
       end
       if human.db.relicListData and human.db.relicListData.equipped and human.db.relicListData.equipped[relicId] then
            for _, idx in ipairs(human.db.relicListData.equipped[relicId]) do
                if idx == heroIdx then
                    return
                end
            end
            table.insert(human.db.relicListData.equipped[relicId],heroIdx)
            -- print("成功装备遗物", relicId, "给英雄", heroIdx)
       end
       heroGrid.relic = heroGrid.relic or {}
       heroGrid.relic.relicID = relicId
       heroGrid.relic.star = human.db.relicListData and human.db.relicListData.status and human.db.relicListData.status[relicId] or 0

    --    print("成功装备遗物", relicId, "给英雄", heroIdx,"当前遗物星级为",heroGrid.relic.star)
    else
        if human.db.relicListData and human.db.relicListData.equipped and human.db.relicListData.equipped[relicId] then
            for idx,rid in pairs(human.db.relicListData.equipped[relicId]) do
                if rid == heroIdx then
                    table.remove(human.db.relicListData.equipped[relicId],idx)
                    break
                end
            end
        else
            return Broadcast.sendErr(human, Lang.RELIC_HERO_NO_EQUPPED)
        end
        heroGrid.relic = {}
    end
    --获取更新之后的数据
	local nRelicData = tMsgData.newdata
    if operate == 0 then
        nRelicData.id = 0
    else
        nRelicData.id = relicId
    end
    local attrMul = getAttrMulFromTalisman(human)
    tMsgData.heroIdx = heroIdx
    nRelicData.name = relicCfg.nName
    nRelicData.activateItem = relicCfg.nProp
    AddTableAttr(nRelicData.attr, RoleDefine, relicCfg, currentStar, attrMul)
    AddTableAttr2(nRelicData.nextattr, RoleDefine, relicCfg)
    nRelicData.effectDesc = relicCfg.nEffectdescription
    local nbonusValues = {}
    if relicCfg.nLifeBonus then table.insert(nbonusValues, relicCfg.nLifeBonus) end
    if relicCfg.nAttackBonus then table.insert(nbonusValues, relicCfg.nAttackBonus) end
    if relicCfg.nDefenseBonus then table.insert(nbonusValues, relicCfg.nDefenseBonus) end
    if relicCfg.nSpeedBonus then table.insert(nbonusValues, relicCfg.nSpeedBonus) end
    nRelicData.effectData[0] = 4
    nRelicData.effectNextData[0] = 4
    nRelicData.effectMaxData[0] = 4
    for i, bonusArray in ipairs(nbonusValues) do
        local replaceValue = currentStar > 0 and bonusArray[currentStar] or 0
        local replaceValueNext = currentStar + 1 > 0 and currentStar + 1 <= 10 and bonusArray[currentStar+1] or 0
        local replaceValueMax =  bonusArray and bonusArray[10] or 0
        -- effectDesc = string.gsub(effectDesc, "X%%", tostring(replaceValue).."%%", 1)
        nRelicData.effectData[i] = replaceValue
        nRelicData.effectNextData[i] = replaceValueNext
        nRelicData.effectMaxData[i] = replaceValueMax
    end
    nRelicData.type = relicCfg.nType
    local costNum = currentStar+1 <= 10 and relicUpgradeConfig and relicUpgradeConfig[currentStar+1] and relicUpgradeConfig[currentStar+1].CostNum or 0
    Grid.makeItem(nRelicData.upGradeCost, relicCfg.nProp, costNum)
    nRelicData.upGradeCost.cnt = costNum or 0
    nRelicData.starLevel = currentStar or 0
    if relicUpgradeConfig and relicUpgradeConfig[currentStar] then
        nRelicData.equipLimit = relicUpgradeConfig[currentStar].EquipHeroNum or 0
    else
        nRelicData.equipLimit = 0 
    end
    -- 填充已装备英雄信息
    if relicData and relicData.equipped and relicData.equipped[relicId] then
        nRelicData.heroIdx[0] = #relicData.equipped[relicId]
        for idx, rid in ipairs(relicData.equipped[relicId]) do
            nRelicData.heroIdx[idx] = rid
        end
    else
        nRelicData.heroIdx = {}  -- 先初始化为空表
        nRelicData.heroIdx[0] = 0
    end
    -- table.print_lua_table(tMsgData)
    Msg.send(tMsgData, human.fd)
    --重算战力
    HeroLogic.sendHeroBagUpdate(human,heroIdx)
    -- print("圣者遗物装备完毕")
    ObjHuman.doCalcHero(human, heroIdx)
	
	HeroLogic.sendHeroBagDynamic(human, heroID, heroIdx)
    HeroLogic.refreshDot(human, heroGrid.uuid)
end

-- 遗物对英雄属性加成
function doCalcHero(human,heroGrid,addAttrs)
    local Life,Attack,Defense,Speed = 0, 0, 0, 0
    local reliData = human and human.db and human.db.relicListData and human.db.relicListData.status
    if not reliData then
        return
    end

    local attrMul = getAttrMulFromTalisman(human)
    attrMul = 1 + attrMul

    for relicId, star in pairs(human.db.relicListData.status) do
        local relicData = relicDataConfig and relicDataConfig[relicId]
        Life = relicData and relicData.nLife or 0
        Attack = relicData and relicData.nAttack or 0
        Defense = relicData and relicData.nDefense or 0
        Speed = relicData and relicData.nSpeed or 0
        star = star or 1
        RoleAttr.updateValue(RoleDefine.HP,Life * star * attrMul,addAttrs)
        RoleAttr.updateValue(RoleDefine.ATK,Attack * star *attrMul,addAttrs)
        RoleAttr.updateValue(RoleDefine.DEF,Defense * star * attrMul,addAttrs)
        RoleAttr.updateValue(RoleDefine.SPEED,Speed * star * attrMul,addAttrs)
    end
end
--红点
function isDot(human, dotConfig)
    if not isOpen(human) then
        return false
    end
    --入口处的红点判断
    if dotConfig.id == RoleSystemDefine.ROLE_SYS_ID_2021 then
        return dotJudgment(human, relicDataConfig)
    else
        -- print("当前进红点分页111111 id = "..dotConfig.id) 
        --单个分页的红点
        local RelicType = 0
        if dotConfig.id == RoleSystemDefine.ROLE_SYS_ID_2022 then
            RelicType = 1
        elseif dotConfig.id == RoleSystemDefine.ROLE_SYS_ID_2023 then
            RelicType = 2
        elseif dotConfig.id == RoleSystemDefine.ROLE_SYS_ID_2024 then
            RelicType = 3
        end

        local cfgHtbl = generateCfgByType(RelicType)
        if not cfgHtbl then
            return false
        end

        return dotJudgment(human, cfgHtbl)
    end
end


-- 外部调用, 本系统是否开启
function ModuleisOpen(human)
    local towerLevel = HuanJingTowerLogic.getTowerLevel(human)
    if towerLevel >= COND_TOWER_LEVEL then
        return true
    end
    return false
end



function WinnerRelic_GetSkillID(nRelicID, nStar)
    local tConfig = relicDataConfig[nRelicID]
    if nil == tConfig then
        return 0
    end

    return tConfig.skillID[nStar] or 0
end


--对应遗物加成描述中的第一个x
function WinnerRelic_GetRateLife(nRelicID, nStar)
    local tConfig = relicDataConfig[nRelicID]
    if nil == tConfig then
        return 0
    end
    return tConfig.nLifeBonus[nStar]
end
--对应遗物加成描述中的第二个x
function WinnerRelic_GetRateAttack(nRelicID, nStar)
    local tConfig = relicDataConfig[nRelicID]
    if nil == tConfig then
        return 0
    end
    return tConfig.nAttackBonus[nStar]
end
--对应遗物加成描述中的第三个x
function WinnerRelic_GetRateDef(nRelicID, nStar)
    local tConfig = relicDataConfig[nRelicID]
    if nil == tConfig then
        return 0
    end
    return tConfig.nDefenseBonus[nStar]
end
--对应遗物加成描述中的第四个x
function WinnerRelic_GetRateSpeed(nRelicID, nStar)
    local tConfig = relicDataConfig[nRelicID]
    if nil == tConfig then
        return 0
    end
    return tConfig.nSpeedBonus[nStar]
end

-- 英雄重置或转换取消圣遗物
function WinnerRelic_CancelWinner(human, tHeroGrid, nHeroIndex)
    local tRelicData = human.db.relicListData
    if not tRelicData then
        return
    end

    if not tHeroGrid.relic or nil == _G.next(tHeroGrid.relic) then
        return
    end

    local nRelicID = tHeroGrid.relic.relicID

    if not tRelicData.equipped or not tRelicData.equipped[nRelicID] then
        return
    end

    for i, v in ipairs(tRelicData.equipped[nRelicID]) do
        if v == nHeroIndex then
            table.remove(tRelicData.equipped[nRelicID], i)
            --print("[WinnerRelic_CancelWinner] 移除了对应的英雄index nRelicID = "..nRelicID.." nHeroIndex = "..nHeroIndex.." i = "..i)
            break
        end
    end

    -- table.print_lua_table(tRelicData.equipped[nRelicID])
    -- print("[WinnerRelic_CancelWinner]打印当前遗物装备数据")
end

function onLogin(human)
    local tRelicData = human.db.relicListData
    if not tRelicData then
        return
    end

    if not tRelicData.equipped then
        return
    end

    for nRelicID, tHeroIndex in pairs(tRelicData.equipped) do
        local nLen = #tHeroIndex
        for i = nLen, 1, -1 do
            local nHeroIndex = tHeroIndex[i]
            local nHeroID = HeroLogic.getHeroIdByIndex(human, nHeroIndex)
            local tHeroGrid = HeroLogic.getHeroGrid(human, nHeroID, nHeroIndex)
            -- 不存在对应的 herogrid了
            if not tHeroGrid then
                table.remove(tRelicData.equipped[nRelicID], i)
               -- print("[WinnerRelic_onLogin] 圣遗物登录移除不存在的英雄 nRelicID = "..nRelicID.." i = "..i.." nHeroIndex = "..nHeroIndex.." nHeroID = "..nHeroID)
            else
                local nHeroStar = tHeroGrid.star
                if WINNERRELIC_HEROSTAR > nHeroStar then
                    -- 先取消穿戴
                    if tHeroGrid.relic and nil ~= _G.next(tHeroGrid.relic) then
                        tHeroGrid.relic = {}
                    end
                    
                    -- 移除记录的数据
                    table.remove(tRelicData.equipped[nRelicID], i)

                   -- print("[WinnerRelic_onLogin] 圣遗物登录移除星级不正常的英雄 nRelicID = "..nRelicID.." i = "..i.." nHeroIndex = "..nHeroIndex.." nHeroID = "..nHeroID)
                end

                -- 检查对应英雄当前穿戴
                -- heroGrid.relic.relicID
                -- heroGrid.relic.star
                if not tHeroGrid.relic then
                    table.remove(tRelicData.equipped[nRelicID], i)
                    --print("[WinnerRelic_onLogin] 圣遗物登录,英雄不存在对应的遗物穿戴缓存 nRelicID = "..nRelicID.." i = "..i.." nHeroIndex = "..nHeroIndex.." nHeroID = "..nHeroID)
                else
                    local nEquipRelicID = tHeroGrid.relic.relicID
                    --print("[WinnerRelic_onLogin] 英雄当前穿戴 nEquipRelicID = "..nEquipRelicID.." nHeroID = "..nHeroID)
                   -- local nEquipRelicStar = tHeroGrid.relic.stars
                    if nEquipRelicID ~= nRelicID then
                        table.remove(tRelicData.equipped[nRelicID], i)
                        -- print("[WinnerRelic_onLogin] 圣遗物登录,英雄当前穿戴的遗物 和遗物记录的不一致, 进行移除 nRelicID = "..nRelicID.." nEquipRelicID = "..nEquipRelicID
                        --         .." nHeroIndex = "..nHeroIndex.." nHeroID = "..nHeroID)
                    end
                end
            end

            -- print("[WinnerRelic_onLogin] 检查正常的遗物和穿戴的英雄 nRelicID = "..nRelicID.." i = "..i.." nHeroIndex = "..nHeroIndex.." nHeroID = "..nHeroID)
        end
    end
end