-- 英雄装备相关逻辑
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")

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 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)
			else
				Grid.makeItem(msgRet.equips[cnt], equipGrid.id, 1, nil, equipGrid, -1, Grid.getOpflagAtBag(heroGrid.equip[i].id))
			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))
		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
    Grid.makeItem(msgRet.equip, equipID, 1, nil, equipGrid, bagIndex, Grid.getOpflagAtBag(equipID))

    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
    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)
	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
	
	-- 通知客户端
	local msgRet = Msg.gc.GC_HERO_EQUIP_PUTOFF
	msgRet.heroID = heroID
	msgRet.heroIndex = heroIndex
	msgRet.pos = pos
	Msg.send(msgRet, human.fd)
end

-- 一键穿装
function putOnQuick(human, heroID, heroIndex, equipID1, equipID2, equipID3, equipID4, 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, 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)	
	
    if not noSendDy then
       ObjHuman.doCalcHero(human,heroIndex)
	   HeroLogic.sendHeroBagDynamic(human, heroID, heroIndex)
    end
end

function doCalcHero(obj,attrs)
	local equip = obj.equip
	if not equip then return end
	
    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)
            -- 水晶
			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
				for _,v in ipairs(equipConfig.base) do
					RoleAttr.updateValue(v[1],mathFloor(v[2] * baseRate),attrs)
				end

                -- 随机属性
                for key, value in pairs(equipGrid.attr) do
					RoleAttr.updateValue(key, mathFloor(value * baseRate) ,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
				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]
				for i = 1, #equipConfigTemp.suitIm do
					local keyTemp = equipConfigTemp.suitIm[i][1]
					local valTemp = equipConfigTemp.suitIm[i][2]
					if v.cnt > i then
						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)
	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)
	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)
	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)
	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)
	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)
	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)
	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)
	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)
	else
		msgRet.equip[0] = 0
	end
	Msg.send(msgRet, human.fd)
end

-- 比较2件装备信息，返回较好的装备id
function cmpEquip(equipID1, equipID2, heroConfig)
	if not equipID1 then return equipID2 end
	if not equipID2 then return equipID1 end
	if equipID1 == equipID2 then
		return equipID1
	end

	local config1 = EquipExcel.equip[equipID1]
	if not config1 then return equipID2 end
	local config2 = EquipExcel.equip[equipID2]
	if not config2 then return equipID1 end
	if config1.rare ~= config2.rare then
		return (config1.rare > config2.rare) and equipID1 or equipID2
	end
	if config1.level ~= config2.level then
		return (config1.level > config2.level) and equipID1 or equipID2
	end
	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
	return equipID1
end

-- 根据部位和伙伴信息获取背包最好的装备id
function getBagBestEquipID(human, pos, heroConfig)
	local bestEquipID = nil 
	for k, equipGrid in pairs(human.db.equipBag) do
		local config = EquipExcel.equip[equipGrid.id]
		if config and config.subType == pos then
			bestEquipID = cmpEquip(bestEquipID, equipGrid.id, heroConfig)
		end
	end
	return bestEquipID
end

-- 是否有装备红点
function isEquipDot(human, heroGrid)
    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

	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
			end
		end
	end
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