-- 英雄装备相关逻辑
local Lang = require("common.Lang")
local HeroExcel = require("excel.hero")
local EquipExcel = require("excel.equip")
local Msg = require("core.Msg")
local ObjHuman = require("core.ObjHuman")
local HeroDefine = require("hero.HeroDefine")
local HeroLogic = require("hero.HeroLogic")
local ItemDefine = require("bag.ItemDefine")
local Grid = require("bag.Grid")
local BagLogic = require("bag.BagLogic")
local Broadcast = require("broadcast.Broadcast")
local RoleAttr = require("role.RoleAttr")
local Util = require("common.Util")
local ItemExcel = require("excel.item").item
local FuwenLogic = require("fuwen.FuwenLogic")
local EquipLogic = require("equip.EquipLogic")
local HeroGem = require("hero.HeroGem")
local EquipLogicGrid = require("equip.EquipLogicGrid")

function query(human, heroID, heroIndex)
	local heroGrid = HeroLogic.getHeroGrid(human, heroID, heroIndex)
	if heroGrid == nil then
		return
	end

	local msgRet = Msg.gc.GC_HERO_EQUIP_QUERY
	msgRet.id = heroID
	msgRet.index = heroIndex

	--宝石套装加成
	local suitBonusTbl = HeroGem.suitAttrBonus(heroGrid)
	
	local cnt = 0
	for i = 1, ItemDefine.EQUIP_MAX_CNT do
        local equipGrid = heroGrid.equip and heroGrid.equip[i]
		if equipGrid then
			cnt = cnt + 1
			if i == ItemDefine.EQUIP_SUBTYPE_SHUIJIN then -- 水晶比较特殊
				Grid.makeItem(msgRet.equips[cnt], equipGrid.id, 1, heroGrid.shuijingAttrID, nil, nil, nil, nil, nil, nil, human)
			else
				Grid.makeItem(msgRet.equips[cnt], equipGrid.id, 1, nil, equipGrid, -1, Grid.getOpflagAtBag(heroGrid.equip[i].id), nil, suitBonusTbl, nil, human)
			end
		end

        local dot = isEquipDotByPos(human, heroGrid, i)
        msgRet.equipDot[i] = dot or 0
	end
    msgRet.equipDot[0] = ItemDefine.EQUIP_MAX_CNT
	msgRet.equips[0] = cnt

    cnt = 0
    for i = 1, 2 do
        -- 尝试解锁符文
        FuwenLogic.fuwenGridUnlock(human, heroID, heroIndex, i)

		if heroGrid.fuwen and 
           heroGrid.fuwen[i] and 
           heroGrid.fuwen[i].id ~= nil then
			cnt = cnt + 1
            msgRet.fuwens[cnt].pos = i
			Grid.makeItem(msgRet.fuwens[cnt].fuwen, heroGrid.fuwen[i].id, 1, nil, heroGrid.fuwen[i], -1, Grid.getOpflagAtBag(heroGrid.fuwen[i].id), nil, nil, nil, human)
		end

		msgRet.fuwenGrid[i] = 1
		if heroGrid.fuwen == nil or 
           heroGrid.fuwen[i] == nil then
			msgRet.fuwenGrid[i] = 0
		end

        local dot = FuwenLogic.isFuwenDotByPos(human, heroGrid, i) == true and 1 or 0
        msgRet.fuwenDot[i] = dot
	end
	msgRet.fuwenGrid[0] = 2
    msgRet.fuwenDot[0] = 2
    msgRet.fuwens[0] = cnt
	Msg.send(msgRet, human.fd)
end

function putOn(human, heroID, heroIndex, bagIndex, noCalc)
    local heroGrid = HeroLogic.getHeroGrid(human, heroID, heroIndex)
    if heroGrid == nil then return end

    if bagIndex == 0 then return end

    local equipGrid = human.db.equipBag[bagIndex]
    if not equipGrid or type(equipGrid) ~= "table" then return end

    local equipID = equipGrid.id
    local equipConfig = EquipExcel.equip[equipID]
    if equipConfig == nil then return end

    if heroGrid.star < equipConfig.star then
       local errDesc = Util.format(Lang.EQUIP_PUT_ON_STAR_ERR, equipConfig.star)
       return Broadcast.sendErr(human, errDesc)
    end
    
    local pos = equipConfig.subType
    if pos == ItemDefine.EQUIP_SUBTYPE_SHUIJIN then return end

    local msgRet = Msg.gc.GC_HERO_EQUIP_PUTON
    msgRet.heroID = heroID
    msgRet.heroIndex = heroIndex

	--获取宝石加成
	local suitBonusTbl = HeroGem.suitAttrBonus(heroGrid)

    Grid.makeItem(msgRet.equip, equipID, 1, nil, equipGrid, bagIndex, Grid.getOpflagAtBag(equipID), nil, suitBonusTbl, nil, human)

    if heroGrid.equip == nil or
        heroGrid.equip[pos] == nil then
        heroGrid.equip = heroGrid.equip or { }
        heroGrid.equip[pos] = equipGrid
        -- 穿戴者
        equipGrid.putUuid = heroGrid.uuid
        -- 新穿装备
        EquipLogic.delEquip(human, bagIndex, "equip_puton")
    else
        -- 同一件装备
        if heroGrid.equip[pos].uuid == equipGrid.uuid then
            return Msg.send(msgRet, human.fd)
        end

        -- 替换装备
        EquipLogic.delEquip(human, bagIndex, "equip_puton")

        local unEquipGrid = heroGrid.equip[pos]
		unEquipGrid.putUuid = nil
        EquipLogic.addByEquipGrid(human, unEquipGrid, "equip_puton", true)

        heroGrid.equip[pos] = equipGrid
        equipGrid.putUuid = heroGrid.uuid
    end

    if noCalc ~= true then
        -- 计算属性
        ObjHuman.doCalcHero(human, heroIndex)
        HeroLogic.sendHeroBagDynamic(human, heroID, heroIndex)
        HeroLogic.refreshDot(human, heroGrid.uuid)
    end
    -- 通知客户端
    Msg.send(msgRet, human.fd)
end

--
function putOnHero(human, heroID, heroIndex, putHeroID, putHeroIndex, putHeroPos)
    local heroGrid = HeroLogic.getHeroGrid(human, heroID, heroIndex)
    if heroGrid == nil then return end

    local putHeroGrid = HeroLogic.getHeroGrid(human, putHeroID, putHeroIndex)
    if putHeroGrid == nil then return end
    -- 同一个英雄
    if heroGrid.uuid == putHeroGrid.uuid then return end
    -- 部位有没有装备
    if putHeroGrid.equip == nil then return end

    local putEquipGrid = putHeroGrid.equip[putHeroPos]
    if not putEquipGrid then return end

    local equipID = putEquipGrid.id
    local equipConfig = EquipExcel.equip[equipID]
    if equipConfig == nil then return end

    if heroGrid.star < equipConfig.star then
        local errDesc = Util.format(Lang.EQUIP_PUT_ON_STAR_ERR, equipConfig.star)
        return Broadcast.sendErr(human, errDesc)
    end

    local pos = equipConfig.subType
    if pos == ItemDefine.EQUIP_SUBTYPE_SHUIJIN then return end

    if heroGrid.equip == nil or
        heroGrid.equip[pos] == nil then
        heroGrid.equip = heroGrid.equip or { }
        -- 穿戴者
        heroGrid.equip[pos] = putEquipGrid
        putEquipGrid.putUuid = heroGrid.uuid
    else
        -- 同一件装备
        if heroGrid.equip[pos].uuid == putHeroGrid.uuid then
            return Msg.send(msgRet, human.fd)
        end

        --放入到背包
        local unEquipGrid = heroGrid.equip[pos]
		unEquipGrid.putUuid = nil
        EquipLogic.addByEquipGrid(human, unEquipGrid, "equip_puton", true)
		
        -- 设置新装备
        heroGrid.equip[pos] = putEquipGrid
        putEquipGrid.putUuid = heroGrid.uuid
    end

    -- 老英雄脱去此装备
    putHeroGrid.equip[putHeroPos] = nil

    -- 计算属性
    ObjHuman.doCalcHero(human, heroIndex)
    HeroLogic.sendHeroBagDynamic(human, heroID, heroIndex)
    HeroLogic.refreshDot(human, heroGrid.uuid)

    ObjHuman.doCalcHero(human, putHeroIndex)
    HeroLogic.sendHeroBagDynamic(human, putHeroID, putHeroIndex)
    HeroLogic.refreshDot(human, putHeroGrid.uuid)

    -- 通知客户端
    local msgRet = Msg.gc.GC_HERO_EQUIP_PUTON
    msgRet.heroID = heroID
    msgRet.heroIndex = heroIndex
	
	--获取宝石加成
	local suitBonusTbl = HeroGem.suitAttrBonus(heroGrid)

    Grid.makeItem(msgRet.equip, equipID, 1, nil, equipGrid, bagIndex, Grid.getOpflagAtBag(equipID), nil, suitBonusTbl, nil, human)
	
    --Grid.makeItem(msgRet.equip, equipID, 1, nil, equipGrid, 0, Grid.getOpflagAtBag(equipID))
    Msg.send(msgRet, human.fd)
end

function putOff(human, heroID, heroIndex, pos, noCalc, noSend)
	local heroGrid = HeroLogic.getHeroGrid(human, heroID, heroIndex)
	if heroGrid == nil then
		return
	end
	
	if pos == ItemDefine.EQUIP_SUBTYPE_SHUIJIN then return end	
	if heroGrid.equip == nil or heroGrid.equip[pos] == nil then
		return
	end

    -- 判断背包空间
    if not EquipLogic.checkEmptyCnt(human, 1) then
       return
    end

	-- 改db
	local equipGrid = heroGrid.equip[pos]
	heroGrid.equip[pos] = nil
    equipGrid.putUuid = nil
	EquipLogic.addByEquipGrid(human, equipGrid, "equip_putoff", true)

	if noCalc ~= true then
		-- 计算属性
		ObjHuman.doCalcHero(human,heroIndex)
		HeroLogic.sendHeroBagDynamic(human, heroID, heroIndex)
		HeroLogic.refreshDot(human, heroGrid.uuid)
	end
	
    if not noSend  then
        -- 通知客户端
        local msgRet = Msg.gc.GC_HERO_EQUIP_PUTOFF
        msgRet.heroID = heroID
        msgRet.heroIndex = heroIndex
        msgRet.pos = pos
        Msg.send(msgRet, human.fd)
    end
end

-- 一键穿装
function putOnQuick(human, heroID, heroIndex, equipID1, equipID2, equipID3, equipID4, equipID5, equipID6)
	local heroGrid = HeroLogic.getHeroGrid(human, heroID, heroIndex)
	if heroGrid == nil then
		return
	end

	putOn(human, heroID, heroIndex, equipID1, true)
	putOn(human, heroID, heroIndex, equipID2, true)
	putOn(human, heroID, heroIndex, equipID3, true)
	putOn(human, heroID, heroIndex, equipID4, true)
	putOn(human, heroID, heroIndex, equipID5, true)
	putOn(human, heroID, heroIndex, equipID6, true)
	ObjHuman.doCalcHero(human,heroIndex)
	HeroLogic.sendHeroBagDynamic(human, heroID, heroIndex)
	HeroLogic.refreshDot(human, heroGrid.uuid)
end

-- 一键脱装
function putOffQuick(human, heroID, heroIndex, noSendDy)
	local heroGrid = HeroLogic.getHeroGrid(human, heroID, heroIndex)
	if heroGrid == nil then
		return
	end

	putOff(human, heroID, heroIndex, ItemDefine.EQUIP_SUBTYPE_WEAPON, true)
	putOff(human, heroID, heroIndex, ItemDefine.EQUIP_SUBTYPE_CLOTH, true)
	putOff(human, heroID, heroIndex, ItemDefine.EQUIP_SUBTYPE_SHOES, true)
	putOff(human, heroID, heroIndex, ItemDefine.EQUIP_SUBTYPE_SHIPIN, true)
	-- putOff(human, heroID, heroIndex, ItemDefine.EQUIP_SUBTYPE_SHENQI, true)	

	putOff(human, heroID, heroIndex, ItemDefine.EQUIP_SUBTYPE_RING, true)
	putOff(human, heroID, heroIndex, ItemDefine.EQUIP_SUBTYPE_AMULET, true)
	
    if not noSendDy then
       ObjHuman.doCalcHero(human,heroIndex)
	   HeroLogic.sendHeroBagDynamic(human, heroID, heroIndex)
    end
end

function doCalcHero(human, obj, attrs)
	local equip = obj.equip
	if not equip then return end
	

	local suitBonusTbl = HeroGem.suitAttrBonus(obj)

    local mathFloor = math.floor
	local equipSuitCntTable = nil
	for i = 1,ItemDefine.EQUIP_MAX_CNT do
		local equipGrid = equip[i]
		if equipGrid then
            local equipID = equipGrid.id	
			local equipConfig = EquipExcel.equip[equipID]
            local baseRate = EquipLogic.getEquipBaseRate(equipGrid.quality)
			local succinctRate = baseRate
            -- 水晶
			if equipConfig.subType == ItemDefine.EQUIP_SUBTYPE_SHUIJIN then
				for _,v in ipairs(EquipExcel.shuijingAttr[obj.shuijingAttrID].attr) do
					RoleAttr.updateValue(v[1],v[2],attrs)
				end
			else

				local baseAttrs = equipConfig.base


				-- 戒指、护符
				if equipConfig.subType == ItemDefine.EQUIP_SUBTYPE_RING or equipConfig.subType == ItemDefine.EQUIP_SUBTYPE_AMULET then
					baseRate = 1
					baseAttrs = equipGrid.baseAttr

					-- 特效
					for effectId in pairs(equipGrid.effectList) do
						local effectAttrs = EquipExcel.texiao[effectId].attrs
						RoleAttr.updateValue(effectAttrs[1][1], effectAttrs[1][2], attrs)
					end

					-- 英雄专属
					if equipGrid.exclusiveHeroId and obj.id == equipGrid.exclusiveHeroId then
						baseRate = 2
					end
				end


				--装备基础属性
				for _,v in ipairs(baseAttrs) do
					if equipConfig.subType == ItemDefine.EQUIP_SUBTYPE_RING or equipConfig.subType == ItemDefine.EQUIP_SUBTYPE_AMULET then
						local val = mathFloor(v[2] * baseRate * (1 + (suitBonusTbl and suitBonusTbl.sp_base or 0)) * (equipGrid.baseRandVal or 1) )
						RoleAttr.updateValue(v[1], val ,attrs)
					else
						local val = mathFloor(v[2] * baseRate * (1 + (suitBonusTbl and suitBonusTbl.base or 0)) * (equipGrid.baseRandVal or 1) )
						RoleAttr.updateValue(v[1], val, attrs)
					end
				end

                -- 洗练属性
                -- for key, value in pairs(equipGrid.attr) do
				-- 	RoleAttr.updateValue(key, mathFloor(value * baseRate) ,attrs)
				-- end

				for _, attrData in ipairs(equipGrid.attr) do
					RoleAttr.updateValue(attrData[1], mathFloor(attrData[2] * succinctRate) ,attrs)
				end
				
				local heroID = obj.id
				local heroConfig = HeroExcel.hero[heroID]
				if equipConfig.camp == heroConfig.camp then
					-- 阵营专属属性
					for _,v in ipairs(equipConfig.campAttr) do
						RoleAttr.updateValue(v[1],v[2],attrs)
					end	
				end
				
				if equipConfig.job == heroConfig.job then
					-- 职业专属属性
					for _,v in ipairs(equipConfig.jobAttr) do
						RoleAttr.updateValue(v[1],v[2],attrs)
					end	
				end
				
				if equipConfig.suit ~= 0 then
					equipSuitCntTable = equipSuitCntTable or {}
					equipSuitCntTable[equipConfig.suit] = equipSuitCntTable[equipConfig.suit] or {cnt = 0}
					equipSuitCntTable[equipConfig.suit].equipID = equipID
					equipSuitCntTable[equipConfig.suit].cnt = equipSuitCntTable[equipConfig.suit].cnt + 1

					-- 是否是戒指, 护符
					equipSuitCntTable[equipConfig.suit].isSpEquip = 0
					if equipConfig.subType == ItemDefine.EQUIP_SUBTYPE_RING or equipConfig.subType == ItemDefine.EQUIP_SUBTYPE_AMULET then
						equipSuitCntTable[equipConfig.suit].isSpEquip = 1
					end

					--等级 >= 15级的装备
					if equipConfig.jobSuitIm and next(equipConfig.jobSuitIm) then
						local jobId = EquipLogicGrid.GetEquipOwnerJob(human, equipGrid)
						equipSuitCntTable[equipConfig.suit].jobId = jobId
					end
				end
			end
		end
	end
	
	-- 套装属性
	if equipSuitCntTable then
		for k, v in pairs(equipSuitCntTable) do
			if v.cnt > 1 then
				local equipIDTemp = v.equipID
				local equipConfigTemp = EquipExcel.equip[equipIDTemp]
				local suitImCfg = equipConfigTemp.suitIm
				if v.jobId then
					suitImCfg = Util.copyTable(equipConfigTemp.suitIm)
					local attrInfo = equipConfigTemp.jobSuitIm[v.jobId]
					EquipLogicGrid.MergeEquipSuitIm(suitImCfg, attrInfo)
				end

				for i = 1, #suitImCfg do
					local keyTemp = suitImCfg[i][1]
					local valTemp = suitImCfg[i][2]
					if v.cnt > i then
						-- 普通套装
						if v.isSpEquip ~= 1 and (suitBonusTbl and suitBonusTbl[i+1]) then
							--这里i+1的原因是，装备套装属性配置suitIm中, idx为1的value是2件套加成, idx为2的是3件套加成，以此类推
							valTemp = valTemp + (suitBonusTbl[i+1] * 10000) --宝石对套装的加成
						end

						-- 戒指，护符套装
						if v.isSpEquip == 1 and (suitBonusTbl and suitBonusTbl.sp_suit) then
							valTemp = valTemp + (suitBonusTbl.sp_suit * 10000)
						end

						RoleAttr.updateValue(keyTemp,valTemp,attrs)
					end
				end
			end
		end
	end
end

-- 激活水晶
function jihuoShuijing(human, heroID, heroIndex)
	local heroGrid = HeroLogic.getHeroGrid(human, heroID, heroIndex)
	if heroGrid == nil then
		return
	end
	
	if heroGrid.lv < HeroDefine.JIHUO_SHUIJING_NEEDLV then
		return
    end
	
	local pos = ItemDefine.EQUIP_SUBTYPE_SHUIJIN
	if heroGrid.equip ~= nil and heroGrid.equip[pos] then 
		return 
	end
	
	--存db
	local equipID = HeroDefine.DEFAULT_SHUIJING_GET -- todo 20011
	local equipConfig = EquipExcel.equip[equipID]
	if not equipConfig then return end
	
	heroGrid.equip = heroGrid.equip or {}
	heroGrid.equip[pos] = equipID
	
	local randIdCnt = #equipConfig.shuijingAttrID
	local indexTemp = math.random(1,randIdCnt)
	local attrID = equipConfig.shuijingAttrID[indexTemp]
	heroGrid.shuijingAttrID = attrID
	
	-- 计算属性
	ObjHuman.doCalcHero(human,heroIndex)
	HeroLogic.sendHeroBagDynamic(human, heroID, heroIndex)
	
	-- 通知客户端
	local msgRet = Msg.gc.GC_HERO_EQUIP_PUTON
	msgRet.heroID = heroID
	msgRet.heroIndex = heroIndex
	Grid.makeItem(msgRet.equip, equipID, 1, heroGrid.shuijingAttrID, nil, nil, nil, nil, nil, nil, human)
	Msg.send(msgRet, human.fd)
end

-- 水晶转换的信息查询
function shuijingTransQuery(human, heroID, heroIndex)
	local heroGrid = HeroLogic.getHeroGrid(human, heroID, heroIndex)
	if heroGrid == nil then
		return
	end
	
	local pos = ItemDefine.EQUIP_SUBTYPE_SHUIJIN
	if heroGrid.equip == nil or heroGrid.equip[pos] == nil then 
		return 
	end
	
	local equipID = heroGrid.equip[pos]
	local shuijingTransConfig = EquipExcel.shuijingTransNeed[equipID]
	if not shuijingTransConfig then return end
	
	local msgRet = Msg.gc.GC_SHUIJING_TRANS_QUERY
	msgRet.heroID = heroID
	msgRet.heroIndex = heroIndex
	msgRet.needJinbi = shuijingTransConfig.money
	msgRet.needItemCnt = shuijingTransConfig.jinghua
	Msg.send(msgRet, human.fd)
end
	
-- 水晶转换
function shuijingTransDo(human, heroID, heroIndex)
	local heroGrid = HeroLogic.getHeroGrid(human, heroID, heroIndex)
	if heroGrid == nil then
		return
	end
	
	local pos = ItemDefine.EQUIP_SUBTYPE_SHUIJIN
	if heroGrid.equip == nil or heroGrid.equip[pos] == nil then 
		return 
	end
	
	local equipID = heroGrid.equip[pos]
	local shuijingTransConfig = EquipExcel.shuijingTransNeed[equipID]
	if not shuijingTransConfig then return end
	
	-- 材料判断
	local needJinbi = shuijingTransConfig.money
	if human.db.jinbi < needJinbi then 
		return Broadcast.sendErr(human, Lang.COMMON_NO_JINBI)
	end
	
	local needItemCnt = shuijingTransConfig.jinghua
	local nowItemCnt = BagLogic.getItemCnt(human, ItemDefine.ITEM_SHUIJING_UPLEVEL_ID)
	if  nowItemCnt< needItemCnt then
		local strName = ItemDefine.getValue(ItemDefine.ITEM_SHUIJING_UPLEVEL_ID, "name")
		local strRet = Util.format(Lang.COMMON_NO_ITEM, strName)
		return Broadcast.sendErr(human, strRet)
	end
	
	-- 扣道具
	BagLogic.delItem(human, ItemDefine.ITEM_SHUIJING_UPLEVEL_ID, needItemCnt, "shuijing_change")
	ObjHuman.updateJinbi(human, -needJinbi, "shuijing_change")
	
	local msgRet = Msg.gc.GC_SHUIJING_TRANS_DO
	msgRet.heroID = heroID
	msgRet.heroIndex = heroIndex
	
	-- 存db之前，先随机获得下一个水晶的属性
	local equipConfig = EquipExcel.equip[equipID]
	if not equipConfig then return end
	local randIdCnt = #equipConfig.shuijingAttrID
	local indexTemp = math.random(1,randIdCnt)

	-- todo 单单这样子会有漏洞。。。玩家可以把水晶A洗练出的属性保存到水晶B那里，需要注意！！太忙了，之后修改
	human.tempAttrID = equipConfig.shuijingAttrID[indexTemp]

	-- 通知客户端
	Grid.makeItem(msgRet.equip, equipID, 1, human.tempAttrID, nil, nil, nil, nil, nil, nil, human)
	Msg.send(msgRet, human.fd)
end

-- 水晶转换保存
function shuijingTransSave(human, heroID, heroIndex)
	local heroGrid = HeroLogic.getHeroGrid(human, heroID, heroIndex)
	if heroGrid == nil then
		return
	end
	
	local pos = ItemDefine.EQUIP_SUBTYPE_SHUIJIN
	if heroGrid.equip == nil or heroGrid.equip[pos] == nil then 
		return 
	end
	
	if human.tempAttrID == nil then
		return
	end
	
	local equipID = heroGrid.equip[pos]
	
	-- 改db
	heroGrid.shuijingAttrID = human.tempAttrID
	human.tempAttrID = nil
	
	-- 计算属性并发送
	ObjHuman.doCalcHero(human,heroIndex)
	HeroLogic.sendHeroBagDynamic(human, heroID, heroIndex)

	-- 通知客户端
	local msgRet = Msg.gc.GC_HERO_EQUIP_PUTON
	msgRet.heroID = heroID
	msgRet.heroIndex = heroIndex
	Grid.makeItem(msgRet.equip, equipID, 1, heroGrid.shuijingAttrID, nil, nil, nil, nil, nil, nil, human)
	Msg.send(msgRet, human.fd)
	
	local msgRet = Msg.gc.GC_SHUIJING_TRANS_SAVE
	Msg.send(msgRet, human.fd)
end

-- 水晶升级查询
function shuijingUpLvQuery(human, mainType, heroID, heroIndex)
	local heroGrid = HeroLogic.getHeroGrid(human, heroID, heroIndex)
	if heroGrid == nil then
		return
	end
	
	local pos = ItemDefine.EQUIP_SUBTYPE_SHUIJIN
	if heroGrid.equip == nil or heroGrid.equip[pos] == nil then 
		return 
	end
	
	local equipID = heroGrid.equip[pos]
	local shuijingUpNeedConfig = EquipExcel.shuijingUpNeed[equipID]
	if not shuijingUpNeedConfig then return end
	
	-- 上限判断
	local nextID = shuijingUpNeedConfig.nextID
	if nextID == 0 then return end
	
	local msgRet = Msg.gc.GC_SHUIJING_UPLV_QUERY
	msgRet.heroID = heroID
	msgRet.heroIndex = heroIndex
	msgRet.needJinbi = 0
	msgRet.needItemCnt = 0
    msgRet.needZuanshi = 0
	local nextID = shuijingUpNeedConfig.nextID
	if nextID ~= 0 then 
		local tempConfig = EquipExcel.shuijingUpNeed[nextID]
        msgRet.needJinbi = tempConfig.money
		msgRet.needItemCnt = tempConfig.jinghua
        msgRet.needZuanshi = tempConfig.zuanshi
	end
	
	msgRet.equip[0] = 1
	local attrID = heroGrid.shuijingAttrID
	local tempAttrID = EquipExcel.shuijingAttr[attrID].lockNextID
	Grid.makeItem(msgRet.equip[1], nextID, 1, tempAttrID, nil, nil, nil, nil, nil, nil, human)
	Msg.send(msgRet, human.fd)
end

-- 水晶升级
function shuijingUpLvDo(human, mainType, heroID, heroIndex)
	local heroGrid = HeroLogic.getHeroGrid(human, heroID, heroIndex)
	if heroGrid == nil then
		return
	end
	
	local pos = ItemDefine.EQUIP_SUBTYPE_SHUIJIN
	if heroGrid.equip == nil or heroGrid.equip[pos] == nil then 
		return 
	end
	
	
	local equipID = heroGrid.equip[pos]
	local shuijingUpNeedConfig = EquipExcel.shuijingUpNeed[equipID]
	if not shuijingUpNeedConfig then return end
	
	-- 上限判断
	local nextID = shuijingUpNeedConfig.nextID
	if nextID == 0 then return end

    local nextConfig = EquipExcel.shuijingUpNeed[nextID]
    if nextConfig == nil then return end

	-- 材料判断
	local needJinbi = nextConfig.money
	if human.db.jinbi < needJinbi then 
		return Broadcast.sendErr(human, Lang.COMMON_NO_JINBI)
	end
	
	local needItemCnt = nextConfig.jinghua
	local nowItemCnt = BagLogic.getItemCnt(human, ItemDefine.ITEM_SHUIJING_UPLEVEL_ID)
	if  nowItemCnt< needItemCnt then
		local strName = ItemDefine.getValue(ItemDefine.ITEM_SHUIJING_UPLEVEL_ID, "name")
		local strRet = Util.format(Lang.COMMON_NO_ITEM, strName)
		return Broadcast.sendErr(human, strRet)
	end
	
	if mainType == HeroDefine.SHUIJING_UP_LV_LOCK then 
		local needZuanshi = nextConfig.zuanshi
		if not ObjHuman.checkRMB(human, needZuanshi) then
			return
		end
	end

	-- 扣道具
	BagLogic.delItem(human, ItemDefine.ITEM_SHUIJING_UPLEVEL_ID, needItemCnt, "shuijing_change")
	ObjHuman.updateJinbi(human, -needJinbi, "shuijing_change")
	if mainType == HeroDefine.SHUIJING_UP_LV_LOCK then 
		local needZuanshi = shuijingUpNeedConfig.zuanshi
		ObjHuman.decZuanshi(human, -needZuanshi, "shuijing_change")
	end

	--改db
	heroGrid.equip[pos] = nextID
	if mainType == HeroDefine.SHUIJING_UP_LV_LOCK then 
		local attrID = heroGrid.shuijingAttrID
		heroGrid.shuijingAttrID = EquipExcel.shuijingAttr[attrID].lockNextID
	else
		local equipConfig = EquipExcel.equip[nextID]
		if not equipConfig then return end
		local randIdCnt = #equipConfig.shuijingAttrID
		local indexTemp = math.random(1,randIdCnt)
		heroGrid.shuijingAttrID = equipConfig.shuijingAttrID[indexTemp]
	end
	
	-- 计算属性并发送
	ObjHuman.doCalcHero(human,heroIndex)
	HeroLogic.sendHeroBagDynamic(human, heroID, heroIndex)

	-- 通知客户端
	local msgRet = Msg.gc.GC_HERO_EQUIP_PUTON
	msgRet.heroID = heroID
	msgRet.heroIndex = heroIndex
	Grid.makeItem(msgRet.equip, nextID, 1, heroGrid.shuijingAttrID, nil, nil, nil, nil, nil, nil, human)
	Msg.send(msgRet, human.fd)
	
	local msgRet = Msg.gc.GC_SHUIJING_UPLV_DO
	msgRet.heroID = heroID
	msgRet.heroIndex = heroIndex
	Grid.makeItem(msgRet.equip, nextID, 1, heroGrid.shuijingAttrID, nil, nil, nil, nil, nil, nil, human)
	Msg.send(msgRet, human.fd)
end

-- 神器升级查询
function shenqiUpLvQuery(human, heroID, heroIndex)
	local heroGrid = HeroLogic.getHeroGrid(human, heroID, heroIndex)
	if heroGrid == nil then
		return
	end
	
	local pos = ItemDefine.EQUIP_SUBTYPE_SHENQI
	if heroGrid.equip == nil or heroGrid.equip[pos] == nil then 
		return 
	end
	
	local equipID = heroGrid.equip[pos]
	local shenqiUpNeedConfig = EquipExcel.shenqiUpNeed[equipID]
	if not shenqiUpNeedConfig then return end
	
	-- 上限判断
	local nextID = shenqiUpNeedConfig.nextID
	if nextID == 0 then return end
	
	-- 下发信息
	local msgRet = Msg.gc.GC_SHENQI_UPLV_QUERY
	msgRet.heroID = heroID
	msgRet.heroIndex = heroIndex
	msgRet.needExp = shenqiUpNeedConfig.needExp
	Grid.makeItem(msgRet.equip, nextID, 1, nil, nil, nil, nil, nil, nil, nil, human)
	Msg.send(msgRet, human.fd)
end


-- 神器升级
function shenqiUpLvDo(human, heroID, heroIndex, equipIDList, equipCntList)
	local heroGrid = HeroLogic.getHeroGrid(human, heroID, heroIndex)
	if heroGrid == nil then
		return
	end
	
	local pos = ItemDefine.EQUIP_SUBTYPE_SHENQI
	if heroGrid.equip == nil or heroGrid.equip[pos] == nil then 
		return 
	end
	
	local equipID = heroGrid.equip[pos]
	local shenqiUpNeedConfig = EquipExcel.shenqiUpNeed[equipID]
	if not shenqiUpNeedConfig then return end
	
	-- 上限判断
	local nextID = shenqiUpNeedConfig.nextID
	if nextID == 0 then return end
	
	-- 判断消耗
	if equipIDList[0] ~= equipCntList[0] then
		return
	end
	
	for i=1, equipIDList[0] do 
		local needEquipCnt = equipCntList[i]
		local nowEquipCnt = BagLogic.getItemCnt(human, equipIDList[i])
		if  nowEquipCnt < needEquipCnt then
			return
		end
	end
	
	local totalExp = 0
	for i=1, equipIDList[0] do
		local id = equipIDList[i]
		local equipConfig = EquipExcel.equip[id]
		if equipConfig then  
			local nowExp = equipConfig.shenqiExp
			local nowCnt = equipCntList[i]
			totalExp = totalExp + math.floor(nowExp * nowCnt)
        else
			local itemConfig = ItemExcel[id]
			if not itemConfig or (id ~= ItemDefine.ITEM_SHENQI_EXP_ID1 and 
				id ~= ItemDefine.ITEM_SHENQI_EXP_ID2) then
				return
			end
			local nowExp = itemConfig.effect
			local nowCnt = equipCntList[i]
			totalExp = totalExp + math.floor(nowExp * nowCnt)
		end
	end
	
	local needExp = shenqiUpNeedConfig.needExp
	if totalExp < needExp then 
		return Broadcast.sendErr(human, Lang.EQUIP_SHENQI_UP_ERR_EXP)
	end
	
	-- 扣道具
	for i=1, equipIDList[0] do 
		local needEquipCnt = equipCntList[i]
		BagLogic.delItem(human, equipIDList[i], needEquipCnt, "shenqi_upLv")
	end
	
	-- 改db
	heroGrid.equip[pos] = nextID
	local backExp = totalExp - needExp 
	local backCnt = math.floor(backExp/10)
	
	-- 返还物
	if backCnt > 0 then 
		BagLogic.addItem(human, HeroDefine.DEFAULT_SHENQI_UPLV_BACK, backCnt, "shenqi_upLv_back", noSend)
	end
	
	-- 计算属性
	ObjHuman.doCalcHero(human,heroIndex)
	HeroLogic.sendHeroBagDynamic(human, heroID, heroIndex)
	
	-- 通知客户端
	local msgRet = Msg.gc.GC_HERO_EQUIP_PUTON
	msgRet.heroID = heroID
	msgRet.heroIndex = heroIndex
	Grid.makeItem(msgRet.equip, nextID, 1, nil, nil, nil, nil, nil, nil, nil, human)
	Msg.send(msgRet, human.fd)
	
	local msgRet = Msg.gc.GC_SHENQI_UPLV_DO
	msgRet.heroID = heroID
	msgRet.heroIndex = heroIndex
	if backCnt > 0 then 
		msgRet.equip[0] = 1
		Grid.makeItem(msgRet.equip[1], HeroDefine.DEFAULT_SHENQI_UPLV_BACK, backCnt, nil, nil, nil, nil, nil, nil, nil, human)
	else
		msgRet.equip[0] = 0
	end
	Msg.send(msgRet, human.fd)
end

-- 比较2件装备信息，返回较好的装备id
function cmpEquip(tNowequipGrid, tBagEquipGrid, heroConfig)
	if not tNowequipGrid or not tBagEquipGrid then 
		return nil 
	end

	--print("[cmpEquip] 装备ID ID1 = "..tNowequipGrid.id.." ID2 = "..tBagEquipGrid.id)

	--print("[cmpEquip] 11111")
	local tConfig1 = EquipExcel.equip[tNowequipGrid.id]
	local tConfig2 = EquipExcel.equip[tBagEquipGrid.id]
	if not tConfig1 or not tConfig2 then
		return tNowequipGrid
	end

	local nBaseRate1 = EquipLogic.getEquipBaseRate(tNowequipGrid.quality)
	local nBaseRate2 = EquipLogic.getEquipBaseRate(tBagEquipGrid.quality)
	local nScore1 = math.floor(tConfig1.score * nBaseRate1)
	local nScore2 = math.floor(tConfig2.score * nBaseRate2)

	--print("[cmpEquip] nBaseRate1 = "..nBaseRate1.." nScore1 = "..nScore1.." nBaseRate2 = "..nBaseRate2.." nScore2 = "..nScore2)

	-- 先比较评分
	if nScore1 ~= nScore2 then
		-- print("[cmpEquip] score 不同 score1 = "..nScore1.." score2 = "..nScore2)
		return (nScore1 >= nScore2) and tNowequipGrid or tBagEquipGrid
	end

	--print("[cmpEquip] 333333")

	if tConfig1.level and tConfig2.level and tConfig1.level ~= tConfig2.level then
		-- print("[cmpEquip] level 不同 level1 = "..tConfig1.level.." level2 = "..tConfig2.level)
		return (tConfig1.level >= tConfig2.level) and tNowequipGrid or tBagEquipGrid
	end

	--print("[cmpEquip] 4444")
	if not tNowequipGrid.quality then
		-- print("当前穿戴的装备没有品级 ID1 = "..tNowequipGrid.id)
	end

	if not tBagEquipGrid.quality then
		-- print("当前比较的装备没有品级 ID1 = "..tBagEquipGrid.id)
	end

	if tNowequipGrid.quality and tBagEquipGrid.quality then
		if tNowequipGrid.quality ~= tBagEquipGrid.quality then
			print("[cmpEquip] quality 不同 quality1 = "..tNowequipGrid.quality.." quality2 = "..tBagEquipGrid.quality)
			return (tNowequipGrid.quality >= tBagEquipGrid.quality) and tNowequipGrid or tBagEquipGrid
		else
			-- print("cmpEquip 当前装备品质相同")
		end
	end

	--print("[cmpEquip] 5555")

	return tNowequipGrid
	
	-- if heroConfig then -- 专属装备判断 阵营 职业
	-- 	-- if config1.camp == heroConfig.camp then return equipID1 end
	-- 	-- if config2.camp == heroConfig.camp then return equipID2 end
	-- 	-- if config1.job == heroConfig.job then return equipID1 end
	-- 	-- if config2.job == heroConfig.job then return equipID2 end
	-- end
	--print("cmpEquip 默认返回装备1 equipID1 = "..equipID1)
end

-- 根据部位和伙伴信息获取背包最好的装备id
function getBagBestEquipID(human, pos, tNowequipGrid, nHeroStar, heroConfig)
	-- 当前最好的装备
	local tChoseEquipGrid = tNowequipGrid

	for k, equipGrid in pairs(human.db.equipBag) do
		local config = EquipExcel.equip[equipGrid.id]
		-- 装备位置相同，星级<=英雄星级，并且装备未穿戴
		if equipGrid and config and config.subType == pos
				and config.star <= nHeroStar and equipGrid.putUuid == nil then
			tChoseEquipGrid = cmpEquip(tChoseEquipGrid, equipGrid, heroConfig)
			if nil == tChoseEquipGrid then
				print("[getBagBestEquipID] 返回的值不正确")
				tChoseEquipGrid = tNowequipGrid
			end
		end
	end

	local nBestEquipID = tChoseEquipGrid.id
	if tNowequipGrid.id == tChoseEquipGrid.id 
		and tNowequipGrid.quality == tChoseEquipGrid.quality 
		and tNowequipGrid.score == tChoseEquipGrid.score and tNowequipGrid.level == tChoseEquipGrid.level then
			nBestEquipID = nil
	end

	return nBestEquipID
end

-- 是否有装备红点
function isEquipDot(human, heroGrid)
    if human.db.lv < 9 then
       return false
    end

	if not heroGrid then return false end
	local heroConfig = HeroExcel.hero[heroGrid.id]
	if not heroConfig then return false end

	for pos = 1, ItemDefine.EQUIP_MAX_CNT do
		if pos ~= ItemDefine.EQUIP_SUBTYPE_SHUIJIN then
			local equipGrid = heroGrid.equip and heroGrid.equip[pos]
			if equipGrid == nil and EquipLogic.getEquipByPos(human, heroGrid.star, pos) then
				return true
			else
				if equipGrid ~= nil then
					local nEquipID = equipGrid.id
					local nBestEquipID = getBagBestEquipID(human, pos, equipGrid, heroGrid.star)
					if nil ~= nBestEquipID and nBestEquipID ~= nEquipID then
						-- print("[isEquipDot] heroID = "..heroGrid.id.." nEquipID = "..nEquipID.." nBestEquipID = "..nBestEquipID)
						return true
					end
				end
			end
		end
	end

	--print("[isEquipDot] 不存在更好的装备 ")
	return false
end

-- 是否有装备红点
function isEquipDotByPos(human, heroGrid, pos)
    if human.db.lv < 9 then
       return false
    end

	if not heroGrid then return end
	local heroConfig = HeroExcel.hero[heroGrid.id]
	if not heroConfig then return end
	if pos ~= ItemDefine.EQUIP_SUBTYPE_SHUIJIN then
		local equipGrid = heroGrid.equip and heroGrid.equip[pos]
		if equipGrid == nil and EquipLogic.getEquipByPos(human, heroGrid.star, pos) then
			return 1
		end
	end
end

function getEquipAndFuwenByHeroIndex(human,heroID,heroIndex)
    local heroGrid = HeroLogic.getHeroGrid(human, heroID, heroIndex)

    if not heroGrid then return end
	local heroConfig = HeroExcel.hero[heroGrid.id]
	if not heroConfig then return end

    local equipTb = {}
	for pos = 1, ItemDefine.EQUIP_MAX_CNT do
		if pos ~= ItemDefine.EQUIP_SUBTYPE_SHUIJIN then
			local equipID = heroGrid.equip and heroGrid.equip[pos] and heroGrid.equip[pos].id
            equipTb[pos] = equipID
		end
	end

    local fuwenTb = {}
    for pos = 1,2 do
        local fuwenIndex = heroGrid.fuwen and heroGrid.fuwen[pos]
        fuwenTb[pos] = fuwenIndex
    end
    return equipTb,fuwenTb
end