-- 魔兽 --
Json = Json or require("common.Json")
local Log = require("common.Log")
local MoShouExcel = require("excel.moshou")
local Msg = require("core.Msg")
local Grid = require("bag.Grid")
local CommonDB = require("common.CommonDB")
local Broadcast = require("broadcast.Broadcast")
local Lang = require("common.Lang")
local ItemDefine = require("bag.ItemDefine")
local RoleDefine = require("role.RoleDefine")
local BagLogic = require("bag.BagLogic")
local RoleAttr = require("role.RoleAttr")
local SkillExcel = require("excel.skill")
local HeroExcel = require("excel.hero")
local ObjHuman = require("core.ObjHuman")
local SkillExcel = require("excel.skill")
local Util = require("common.Util")
local CombatPosLogic = require("combat.CombatPosLogic")
local CombatDefine = require("combat.CombatDefine")
local CombatImpl = require("combat.CombatImpl")
local ChengjiuLogic = require("chengjiu.ChengjiuLogic")
local ChengjiuDefine = require("chengjiu.ChengjiuDefine")
local RoleSystemLogic = require("roleSystem.RoleSystemLogic")
local RoleSystemDefine = require("roleSystem.RoleSystemDefine")
local BuyLogic = require("topup.BuyLogic")
local YunYingLogic = require("yunying.YunYingLogic")
local TriggerDefine = require("trigger.TriggerDefine")
local TriggerLogic = require("trigger.TriggerLogic")
local TalismanLogic = require("talisman.TalismanLogic")

-- human.db.moshou = {
-- 	lv = nil --等级
-- 	lvExp = nil --魔兽等级经验
-- 	jingLian = nil --精炼
-- 	zhuHun = nil --铸魂
-- 	skillLV = nil --魔兽技能等级
-- 	putOnMoshou = nil --已上阵魔兽
--  xilian = nil  -- 洗练
-- }

-- human.db.moshou[moshouID] = {
-- 	taskGet = nil --魔兽激活任务领取
-- }

-- human.db.moshou[moshouID].taskGet[taskID] = nil --任务领取奖励即记录
-- human.db.moshou.jingLian = jingLianLV --精炼等级
-- human.db.moshou.zhuHun = zhuHunCnt --铸魂石已使用个数
-- human.db.moshou.task[taskType] = cnt --任务类型->已完成数

MOSHOU_SKILLLV_ARGE1 = 1 --魔兽技能等级限制 魔兽等级
MOSHOU_SKILLLV_ARGE2 = 2 --魔兽技能等级限制 君主等级

MOSHOU_JINGLIANLV_ARGE1 = 1 --魔兽精炼等级限制 魔兽等级
MOSHOU_JINGLIANLV_ARGE2 = 2 --魔兽精炼等级限制 聚义厅等级


MOSHOU_ID1 = 1 -- 魔兽ID
MOSHOU_MIN_LEVEL = 4 -- 魔兽技能使用最低要求
MOSHOU_SKILL = MOSHOU_SKILL or {}

MOSHOU_XILIAN_OP_1 =  1   -- 洗练
MOSHOU_XILIAN_OP_2 =  2   -- 保存洗练



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


local function initMoShouSkill(moshouID)
    local moshouSkillExcel = MoShouExcel.moshouSkill
    if not MOSHOU_SKILL[moshouID] then
       for k, v in ipairs(moshouSkillExcel) do
          if v.moshouId == moshouID then
             MOSHOU_SKILL[moshouID] = MOSHOU_SKILL[moshouID] or {}
             MOSHOU_SKILL[moshouID][k] = v.moshouSkill
          end
       end
    end
end

function initXilian()

    for _, config in ipairs(MoShouExcel.moshouxlRandom) do
        config.rareSum1 = 0
        config.rareSum2 = 0
        for i = config.random[1],  config.random[2] do
            local templeConfig = MoShouExcel.moshouXiLian[i]
            config.rareSum1 = config.rareSum1 + templeConfig.rare1
            config.rareSum2 = config.rareSum2 + templeConfig.rare2
        end        
    end

    for _, config in ipairs(MoShouExcel.moshouXiLian) do   
        local rare = 0
        for k, v in ipairs(config.attrs1) do
            rare = rare + v[3] 
        end
        config.attrRareMax = rare
    end
    
end

function initAfterHot()

   initMoShouSkill(MOSHOU_ID1)

   initXilian()
end

local function makeMoshouLvAttrs(human, net, moshouID)
    local lvAttrMul = getLvAttrMulFromTalisman(human)
    lvAttrMul = 1 + lvAttrMul

    net[0] = 0
    local nowLV = getMoshouLv(human)
    local config = MoShouExcel.moshouLv[nowLV]
    if config then
        net[0] = #config.lvAttrs
        for i = 1, #config.lvAttrs do
            net[i].key = config.lvAttrs[i][1]
            net[i].value = config.lvAttrs[i][2] * lvAttrMul
        end
    end

    local lvAttrsAdd = human.db.moshou and human.db.moshou.lvAttrsAdd or nil
    if lvAttrsAdd then
		for k,v in pairs(lvAttrsAdd) do
			for i=1,#config.lvAttrs do
				if net[i].key == k then
					net[i].value = net[i].value + v * lvAttrMul
				end
			end
		end
	end
end


local function makeMoshouLvUp(human, net)
    net[0] = 0
    local nowLV = getMoshouLv(human)
    if nowLV >= #MoShouExcel.moshouLv then return end

    net[0] = 1
    net[1].nowJinDu = human.db.moshou and human.db.moshou.lvExp or 0

    local nextConfig = MoShouExcel.moshouLv[nowLV + 1]
    net[1].needJinDu = nextConfig.needJinDu

    net[1].needItem[0] = #nextConfig.lvUpNeed
    for i = 1, #nextConfig.lvUpNeed do
        local itemID = nextConfig.lvUpNeed[i][1]
        local itemCnt = nextConfig.lvUpNeed[i][2]
        Grid.makeItem(net[1].needItem[i], itemID, itemCnt)
    end
end

local function makeMoshouLvUpNet(net, human, moshouID)
    local config = MoShouExcel.moshouActive[MOSHOU_ID1]

    net.moshouID = moshouID
    net.name = config.moshouName
    net.body = config.body
    net.moshouLv = getMoshouLv(human)
    makeMoshouLvAttrs(human, net.lvAttrs, moshouID)
    makeMoshouLvUp(human, net.moshouLvUp)
end

local function makeMoshouReds(net, human)
    net.lvUpRed = moshouLVupRed(human) and 1 or 0
    net.jinglianRed = moshouJinglianRed(human) and 1 or 0
    net.zhuHunRed = moshouZhunHunRed(human) and 1 or 0
    net.skillRed = moshouSkillUpRed(human) and 1 or 0
    net.xilianRed = moshouXilianUpRed(human) and 1 or 0
end

local function makeJingLianLvData(human, net, attr, targrtLv)
    net.lv = targrtLv
    net.attr.key = attr[1]
    net.attr.value = attr[2]
end

function moshouLvUpQuery(human, moshouID)
    local msgRet = Msg.gc.GC_MOSHOU_LVUP_QUERY
    local config = MoShouExcel.moshouActive[MOSHOU_ID1]
    if not config then return end

    makeMoshouLvUpNet(msgRet.moshouLv, human, MOSHOU_ID1)
    makeMoshouReds(msgRet.moshouReds, human)

    msgRet.jingLianLvData[0] = 0
    local nowLV = getMoShouJiLian(human)
    local nowConfig = MoShouExcel.moshouJingLian[nowLV]
    if nowConfig then
        msgRet.jingLianLvData[0] = 1
        makeJingLianLvData(human, msgRet.jingLianLvData[1], nowConfig.lvAttrs, nowLV)
    end

    msgRet.xilianOpen = xilianIsOpen(human) 
    Msg.send(msgRet, human.fd)
end

local function makeMoshouSkill(human, net, moshouID, posSkillID)
    local len = 0
    local skillLV = getMoShouSkillLv(human)
    for k, v in ipairs(MOSHOU_SKILL[moshouID]) do
       local skillID = v[skillLV]
       if skillID then
          local skillConfig = SkillExcel.skill[skillID]
          len = len + 1
          if len > #net then
             break
          end

          net[len].id = skillID
          net[len].icon = skillConfig.icon
          net[len].name = skillConfig.name
          net[len].desc = skillConfig.desc
          net[len].lv = skillLV
          net[len].index = k

          net[len].state = 1
          if k == posSkillID then
             net[len].state = 2
          end
       end
    end
    net[0] = len
end

function makeXilianQuery(human, msgRet)
    msgRet.list[0] = 0
    if human.db.moshou and human.db.moshou.xilian then
        for k, db in ipairs(human.db.moshou.xilian) do
            local net = msgRet.list[k]
            local config = MoShouExcel.moshouXiLian[db.id]
            net.id = db.id
            net.index = k
            net.rare = config.pinzhiLv
            net.name = config.name
            net.lv = config.pinzhi
       --     print("   net.rare ",  net.rare , net.lv, db.type)
            net.attrs[1].key = db.key
            net.attrs[1].value  = db.value
            net.attrs[0] = 1
            net.needZuanshi = getXilianNeed(config, MOSHOU_XILIAN_OP_2, k)
            Grid.makeItem(net.item,  ItemDefine.ITEM_LONGXUE_ID , getXilianNeed(config, MOSHOU_XILIAN_OP_1, k))
            net.nextRate = 0
            net.nextAttr[0] = 0
            net.nextName = ""
            local lastID = db.lastId
            if lastID and db.lastKey then
                local lastConfig =  MoShouExcel.moshouXiLian[lastID]
                net.nextRate = lastConfig.pinzhiLv
                net.nextAttr[1].key    = db.lastKey
                net.nextAttr[1].value  = db.lastValue
                net.nextAttr[0] = 1
                net.nextName = lastConfig.name
            end           
        end
        msgRet.list[0] = #human.db.moshou.xilian 
    end
end

-- 刚打开魔兽界面查询 moshouID默认1
-- 魔兽已全部激活 返回升级界面协议
-- 魔兽未全部激活 返回魔兽激活任务协议（已激活的魔兽+正在完成任务的魔兽）
function CG_MOSHOU_QUERY(human, moshouID)
    local cjPrivilege = ChengjiuLogic.checkPrivilege(human, ChengjiuDefine.PRIVILEGE_TYPE_8)
    if not cjPrivilege then return end

    -- 升级界面协议
    moshouLvUpQuery(human, moshouID)
end


function CG_MOSHOU_LVUP_UP(human, upType)
    local cjPrivilege = ChengjiuLogic.checkPrivilege(human, ChengjiuDefine.PRIVILEGE_TYPE_8)
    if not cjPrivilege then return end

    local nowLV = getMoshouLv(human)
	local maxLV = #MoShouExcel.moshouLv
	if nowLV >= maxLV then
		Broadcast.sendErr(human,Lang.MOSHOU_LV_UP_ERR)
        return
	end

	local config = MoShouExcel.moshouLv[nowLV+1]

	local lvUpNeed = config.lvUpNeed
	for i=1,#lvUpNeed do
		local itemID = lvUpNeed[i][1]
		local itemCnt = lvUpNeed[i][2]
		local now = BagLogic.getItemCnt(human, itemID)
		if now < itemCnt then
			Broadcast.sendErr(human,Lang.MOSHOU_LV_UP_NOT)
            return
		end
	end

	for i=1,#lvUpNeed do
		local itemID = lvUpNeed[i][1]
		local itemCnt = lvUpNeed[i][2]
		BagLogic.delItem(human, itemID, itemCnt, "moshou")
	end
	
	local nowConf = MoShouExcel.moshouLv[nowLV]
	local upGetJinDuCnf = nowConf.upGetJinDu
	local totalWeight = nil
	for i=1,#upGetJinDuCnf do
		totalWeight = (totalWeight or 0) + upGetJinDuCnf[i][2]
	end
	if not totalWeight then return end

	local weight = nil
	local upGetJinDuIndex = nil
	local randWeight = math.random(1,totalWeight)
	for i=1,#upGetJinDuCnf do
		weight = (weight or 0) + upGetJinDuCnf[i][2]
		if randWeight <= weight then
			upGetJinDuIndex = i
			break
		end
	end
	if not upGetJinDuIndex then return end

	local upGetJinDu = upGetJinDuCnf[upGetJinDuIndex][1]
	local lvAttrsAddCnf = nowConf.lvAttrsAdd[upGetJinDuIndex]
    local lvUpState = false
	human.db.moshou = human.db.moshou or {}
	human.db.moshou.lvExp = (human.db.moshou.lvExp or 0) + upGetJinDu
	if human.db.moshou.lvExp >= config.needJinDu * config.upNeedRate then
        human.db.moshou.lv = getMoshouLv(human)
		human.db.moshou.lv = human.db.moshou.lv + 1
		human.db.moshou.lvExp = 0
		human.db.moshou.lvAttrsAdd = nil
        lvUpState = true
        -- 守护之龙的等级记录
        --Log.write(Log.LOGID_OSS_DRAGON_LEVEL, human.db._id, human.db.account, human.db.name, human.db.moshou.lv, getMoShouSkillLv(human))
        Log.write(Log.LOGID_OSS_DRAGON_LEVEL, human.db._id, human.db.newUniqueTag, human.db.name, human.db.moshou.lv, getMoShouSkillLv(human))
        TriggerLogic.PublishEvent(TriggerDefine.FENGHUANGLV, human.db._id, human.db.moshou.lv)
	else
		human.db.moshou.lvAttrsAdd = human.db.moshou.lvAttrsAdd or {}
		for i=1,#lvAttrsAddCnf do
			local key = lvAttrsAddCnf[i][1]
			local value = lvAttrsAddCnf[i][2]
			human.db.moshou.lvAttrsAdd[key] = (human.db.moshou.lvAttrsAdd[key] or 0) + value
		end
	end

	local msgRet = Msg.gc.GC_MOSHOU_LVUP_UP
	makeMoshouLvAttrs(human,msgRet.lvAttrs)
	makeMoshouLvUp(human,msgRet.moshouLvUp)
	msgRet.moshouLv = getMoshouLv(human)
	Msg.send(msgRet,human.fd)

    moshouLvUpQuery(human)

    RoleAttr.cleanHeroAttrCache(human)
    RoleAttr.doCalc(human)
    ObjHuman.sendAttr(human, RoleDefine.ZHANDOULI)
    RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_2002)
    if lvUpState then
        moshouLvUpToXilianBreak(human)
    end
end


local function getLangByCanUpType(human,canUpType)
	if canUpType == MOSHOU_SKILLLV_ARGE1 then
		return Lang.MOSHOU_UP_DESC_MOSHOULV
	elseif canUpType == MOSHOU_SKILLLV_ARGE2 then
		return Lang.MOSHOU_UP_DESC_JUNZHU
	elseif canUpType == MOSHOU_JINGLIANLV_ARGE1 then
		return Lang.MOSHOU_UP_DESC_MOSHOULV
	elseif canUpType == MOSHOU_JINGLIANLV_ARGE2 then
		return Lang.MOSHOU_UP_DESC_JUYI
	end
end

local function getSkillCanUpNeed(human)
	local skillLV = getMoShouSkillLv(human)
	if skillLV >= #MoShouExcel.moshouSkillLv then
		return 0,0
	end
	local nextConf = MoShouExcel.moshouSkillLv[skillLV+1]
	return nextConf.canUpNeed[1] or 0,nextConf.canUpNeed[2] or 0
end


local function getSkillMaxLV(human)
	local moshouLV = getMoshouLv(human)
	local conf = MoShouExcel.moshouSkillLv
	local skillLV = getMoShouSkillLv(human)
	if skillLV >= #conf then
		return skillLV
	end
	for i=skillLV+1,#conf do
		local arge = conf[i].canUpNeed[1]
		local argeNeed = conf[i].canUpNeed[2]
		if arge == MOSHOU_SKILLLV_ARGE1 then
			if moshouLV < argeNeed then
				return i - 1
			end
		elseif arge == MOSHOU_SKILLLV_ARGE2 then
			return 0
		end
	end

	return #conf
end

function CG_MOSHOU_SKILL_QUERY(human, moshouID)
    local cjPrivilege = ChengjiuLogic.checkPrivilege(human, ChengjiuDefine.PRIVILEGE_TYPE_8)
    if not cjPrivilege then return end

    if not MoShouExcel.moshouActive[MOSHOU_ID1] then return end

    local msgRet = Msg.gc.GC_MOSHOU_SKILL_QUERY
    msgRet.moshouID = MOSHOU_ID1
    makeMoshouSkill(human, msgRet.moshouSkill, MOSHOU_ID1)

    local moshouLV = getMoshouLv(human)
    local skillMaxLv = getSkillMaxLV(human)
    msgRet.skillMaxLv = skillMaxLv

    msgRet.needItem[0] = 0
    local skillLV = getMoShouSkillLv(human)
    local config = MoShouExcel.moshouSkillLv[skillLV + 1]
    if config and skillLV < skillMaxLv then
        msgRet.needItem[0] = #config.upCost
        for i = 1, #config.upCost do
            local itemID = config.upCost[i][1]
            local itemCnt = config.upCost[i][2]
            Grid.makeItem(msgRet.needItem[i], itemID, itemCnt)
        end
    end

    msgRet.skillCanUp = ""
	if skillLV >= #MoShouExcel.moshouSkillLv then
		msgRet.skillCanUp = Lang.MOSHOU_SKILL_LV_MAX
	else
		if skillLV >= skillMaxLv then
			local arge,argeNeed = getSkillCanUpNeed(human)
			local content = getLangByCanUpType(human,arge)
			if content then
				content = Util.format(content,argeNeed)
				msgRet.skillCanUp = content
			end
		end
	end
    Msg.send(msgRet, human.fd)
end

function CG_MOSHOU_SKILL_UP(human, moshouID)
    local cjPrivilege = ChengjiuLogic.checkPrivilege(human, ChengjiuDefine.PRIVILEGE_TYPE_8)
    if not cjPrivilege then return end

    if not MoShouExcel.moshouActive[MOSHOU_ID1] then return end
    if not MOSHOU_SKILL[moshouID] then return end

    local moshouLV = getMoshouLv(human)
    local skillLV = getMoShouSkillLv(human)
    local config = MoShouExcel.moshouSkillLv
    if skillLV >= #config then return end

    local nextSkillLv = skillLV + 1

    if not MOSHOU_SKILL[moshouID][1][nextSkillLv] then return end

 
	local nowArgeCnt = nil
	local canUpNeedType = config[nextSkillLv].canUpNeed[1] or 0
	local canUpNeedValue = config[nextSkillLv].canUpNeed[2] or 0

	if canUpNeedType == MOSHOU_SKILLLV_ARGE1 then
	    nowArgeCnt = moshouLV
	else
        return
    end
	
	if not nowArgeCnt then return end

	if nowArgeCnt < canUpNeedValue then return end

    local upCost = config[nextSkillLv].upCost
    for i = 1, #upCost do
        local itemID = upCost[i][1]
        local itemCnt = upCost[i][2]
        local nowCnt = BagLogic.getItemCnt(human, itemID)
        if nowCnt < itemCnt then return Broadcast.sendErr(human, Lang.MOSHOU_LV_UP_NOT) end
    end

    for i = 1, #upCost do
        local itemID = upCost[i][1]
        local itemCnt = upCost[i][2]
        BagLogic.delItem(human, itemID, itemCnt, "moshou")
    end

    human.db.moshou = human.db.moshou or {}
    human.db.moshou.skillLV = nextSkillLv
    --Log.write(Log.LOGID_OSS_DRAGON_LEVEL, human.db._id, human.db.account, human.db.name, human.db.moshou.lv, getMoShouSkillLv(human))
    Log.write(Log.LOGID_OSS_DRAGON_LEVEL, human.db._id, human.db.newUniqueTag, human.db.name, human.db.moshou.lv, getMoShouSkillLv(human))

    Broadcast.sendErr(human, Lang.MOSHOU_SKILL_LV_UP)

    moshouLvUpQuery(human)
    CG_MOSHOU_SKILL_QUERY(human, moshouID)
    RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_2002)
end

function CG_MOSHOU_JINGLIAN_QUERY(human)
    local cjPrivilege = ChengjiuLogic.checkPrivilege(human, ChengjiuDefine.PRIVILEGE_TYPE_8)
    if not cjPrivilege then return end

    local msgRet = Msg.gc.GC_MOSHOU_JINGLIAN_QUERY
    local jinglianLV = getMoShouJiLian(human)
    local moshouLV = getMoshouLv(human)
    local jingLianConfig = MoShouExcel.moshouJingLian
    local jingLianCanLV = #jingLianConfig
    msgRet.jingLianCanLV = jingLianCanLV

    msgRet.nowLvData[0] = 0
    msgRet.nextLvData[0] = 0
    msgRet.needItem[0] = 0
    local nowLV = getMoShouJiLian(human)
    local nextConfig = MoShouExcel.moshouJingLian[nowLV + 1]
    if nextConfig then
        local showLeft = nextConfig.showLeft
        local showRight = nextConfig.showRight
        msgRet.nowLvData[0] = 1
        makeJingLianLvData(human, msgRet.nowLvData[1], showLeft, nowLV)

        msgRet.nextLvData[0] = 1
        makeJingLianLvData(human, msgRet.nextLvData[1], showRight, nowLV + 1)

        msgRet.needItem[0] = #nextConfig.lvUpNeed
        for i = 1, #nextConfig.lvUpNeed do
            local itemID = nextConfig.lvUpNeed[i][1]
            local itemCnt = nextConfig.lvUpNeed[i][2]
            Grid.makeItem(msgRet.needItem[i], itemID, itemCnt)
        end
    else
        local nowConfig = MoShouExcel.moshouJingLian[nowLV]
        msgRet.nowLvData[0] = 1
        makeJingLianLvData(human, msgRet.nowLvData[1], nowConfig.lvAttrs, nowLV)
    end

    msgRet.attrs[0] = 0
    local attrs = nil
    if jingLianConfig[jinglianLV] then
        attrs = { }
        for i = 1, jinglianLV do
            local key = jingLianConfig[i].lvAttrs[1]
            local value = jingLianConfig[i].lvAttrs[2]
            attrs[key] =(attrs[key] or 0) + value
        end
    end

    if attrs then
        for key, value in pairs(attrs) do
            msgRet.attrs[0] = msgRet.attrs[0] + 1
            local index = msgRet.attrs[0]
            msgRet.attrs[index].key = key
            msgRet.attrs[index].value = value
        end
    end
    -- Msg.trace(msgRet)
    Msg.send(msgRet, human.fd)
end

function CG_MOSHOU_JINGLIAN_DO(human)
    local cjPrivilege = ChengjiuLogic.checkPrivilege(human, ChengjiuDefine.PRIVILEGE_TYPE_8)
    if not cjPrivilege then return end

    local jingLianLV = getMoShouJiLian(human)
    local moshouLV = getMoshouLv(human)

    local jingLianMaxLV = #MoShouExcel.moshouJingLian
    if jingLianLV >= jingLianMaxLV then return end

    local nextConfig = MoShouExcel.moshouJingLian[jingLianLV + 1]
    local lvUpNeed = nextConfig.lvUpNeed
    for i = 1, #lvUpNeed do
        local itemID = lvUpNeed[i][1]
        local itemCnt = lvUpNeed[i][2]
        local nowCnt = BagLogic.getItemCnt(human, itemID)
        if nowCnt < itemCnt then return Broadcast.sendErr(human, Lang.MOSHOU_LV_UP_NOT) end
    end

    for i = 1, #lvUpNeed do
        local itemID = lvUpNeed[i][1]
        local itemCnt = lvUpNeed[i][2]
        BagLogic.delItem(human, itemID, itemCnt, "moshou")
    end

    human.db.moshou = human.db.moshou or { }
    human.db.moshou.jingLian = jingLianLV + 1

    Broadcast.sendErr(human, Lang.MOSHOU_JINGLIAN_LV_UP)

    CG_MOSHOU_JINGLIAN_QUERY(human)

    RoleAttr.cleanHeroAttrCache(human)

    RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_2002)
end

function CG_MOSHOU_ZHUHUN_QUERY(human)
    local cjPrivilege = ChengjiuLogic.checkPrivilege(human, ChengjiuDefine.PRIVILEGE_TYPE_8)
    if not cjPrivilege then return end

    local msgRet = Msg.gc.GC_MOSHOU_ZHUHUN_QUERY
    local config = MoShouExcel.moshouZhuHun[1]
    Grid.makeItem(msgRet.needItem, config.costID, 1)

    msgRet.nowAttr[0] = 0
    local nowUseCnt = human.db.moshou and human.db.moshou.zhuHun or 0
    if nowUseCnt > 0 then
        msgRet.nowAttr[0] = #config.zhuHunAttrs
        for i = 1, #config.zhuHunAttrs do
            msgRet.nowAttr[i].key = config.zhuHunAttrs[i][1]
            msgRet.nowAttr[i].value = config.zhuHunAttrs[i][2] + config.addZhuHun * nowUseCnt
        end
    end

    msgRet.nowUseCnt = nowUseCnt

    local moshouLV = getMoshouLv(human)
    local zhuHunCntMax = MoShouExcel.moshouLv[moshouLV].zhuHunCntMax
    msgRet.maxUseCan = zhuHunCntMax
    Msg.send(msgRet, human.fd)
end

function CG_MOSHOU_ZHUHUN_DO(human, useCnt)
    local cjPrivilege = ChengjiuLogic.checkPrivilege(human, ChengjiuDefine.PRIVILEGE_TYPE_8)
    if not cjPrivilege then return end

    local moshouLV = getMoshouLv(human)
    local zhuHunCntMax = MoShouExcel.moshouLv[moshouLV].zhuHunCntMax
    local nowUseCnt = human.db.moshou and human.db.moshou.zhuHun or 0
    if nowUseCnt >= zhuHunCntMax or(nowUseCnt + useCnt) > zhuHunCntMax then return Broadcast.sendErr(human, Lang.MOSHOU_ZHUHUN_MAX) end

    local config = MoShouExcel.moshouZhuHun[1]
    local nowCnt = BagLogic.getItemCnt(human, config.costID)

    if useCnt > nowCnt then return Broadcast.sendErr(human, Lang.MOSHOU_LV_UP_NOT) end

    BagLogic.delItem(human, config.costID, useCnt, "moshou")

    human.db.moshou = human.db.moshou or { }
    human.db.moshou.zhuHun = nowUseCnt + useCnt

    Broadcast.sendErr(human, Lang.MOSHOU_ZHUHUN_LV_UP)

    CG_MOSHOU_ZHUHUN_QUERY(human)

    RoleAttr.cleanHeroAttrCache(human)

    RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_2002)
end

local function makeMoshouPutOn(human, net, moshouID)
    local cjPrivilege = ChengjiuLogic.checkPrivilege(human, ChengjiuDefine.PRIVILEGE_TYPE_8)
    if not cjPrivilege then return end

    net.moshouID = MOSHOU_ID1

    local config = MoShouExcel.moshouActive[MOSHOU_ID1]
    net.moshouName = config.moshouName
    net.moshouIcon = config.icon
    net.moshouLv = getMoshouLv(human)

    makeMoshouSkill(human, net.moshouSkill, MOSHOU_ID1)
end

-- 上阵界面 查询魔兽信息
function CG_MOSHOU_PUT_QUERY(human)
    local cjPrivilege = ChengjiuLogic.checkPrivilege(human, ChengjiuDefine.PRIVILEGE_TYPE_8)
    if not cjPrivilege then return end

    local config = MoShouExcel.moshouActive
    local msgRet = Msg.gc.GC_MOSHOU_PUT_QUERY
    msgRet.moshouPutOn[0] = #config
    for i = 1, #config do
        makeMoshouPutOn(human, msgRet.moshouPutOn[i], i)
    end
    Msg.send(msgRet, human.fd)
end

function doCalcHero(human, heroGrid, attrs)
    local cjPrivilege = ChengjiuLogic.checkPrivilege(human, ChengjiuDefine.PRIVILEGE_TYPE_8)
    if not cjPrivilege then return end

    -- 魔兽等级属性加成
    local nowLV = getMoshouLv(human)
    local moshouLvConfig = MoShouExcel.moshouLv[nowLV]
	if moshouLvConfig and #moshouLvConfig.lvAttrs > 0 then
		local lvAttrs = nil
		for i=1,#moshouLvConfig.lvAttrs do
			local key = moshouLvConfig.lvAttrs[i][1]
			local value = moshouLvConfig.lvAttrs[i][2]
			lvAttrs = lvAttrs or {}
			lvAttrs[key] = (lvAttrs[key] or 0) + value
		end

		if human.db.moshou and human.db.moshou.lvAttrsAdd then
			local lvAttrsAdd = human.db.moshou.lvAttrsAdd
			for k,v in pairs(lvAttrsAdd) do
				lvAttrs = lvAttrs or {}
				lvAttrs[k] = (lvAttrs[k] or 0) + v
			end
		end

        local lvAttrMul = getLvAttrMulFromTalisman(human)
        lvAttrMul = 1 + lvAttrMul

		if lvAttrs then
			for k,v in pairs(lvAttrs) do
				RoleAttr.updateValue(k,v * lvAttrMul,attrs)
			end
		end
	end

	--魔兽精练等级属性加成
	local jingLianLV = human.db.moshou and human.db.moshou.jingLian or 0
	local jingLianConfig = MoShouExcel.moshouJingLian
    if jingLianConfig[jingLianLV] then
        for i=1,jingLianLV do
            local key = jingLianConfig[i].lvAttrs[1]
            local value = jingLianConfig[i].lvAttrs[2]
            RoleAttr.updateValue(key,value,attrs)
        end
    end

	--魔兽铸魂属性加成
	local zhuHun = human.db.moshou and human.db.moshou.zhuHun or 0
	local zhuHunConfig = MoShouExcel.moshouZhuHun[1]
	if zhuHun > 0 and #zhuHunConfig.zhuHunAttrs > 0 then
		for i=1,#zhuHunConfig.zhuHunAttrs do
			RoleAttr.updateValue(zhuHunConfig.zhuHunAttrs[i][1],zhuHunConfig.zhuHunAttrs[i][2] * zhuHun,attrs)
		end
	end

    -- 魔兽洗练的加成
    if human.db.moshou and human.db.moshou.xilian then
        for k, db in ipairs(human.db.moshou.xilian) do
            if db and db.key and db.value ~= 0 then
                RoleAttr.updateValue(db.key ,db.value ,attrs)
            end            
        end

        local bestCnt = getXilianBestCnt(human)
        if bestCnt > 0 then
            local config = MoShouExcel.moshouxlHx[bestCnt] 
            for k, v in ipairs(config.attrs) do
                RoleAttr.updateValue(v[1] ,v[2] ,attrs)
            end
        end
    end

end

function CG_MOSHOU_PREVIEW(human)
    local cjPrivilege = ChengjiuLogic.checkPrivilege(human, ChengjiuDefine.PRIVILEGE_TYPE_8)
    if not cjPrivilege then return end

    local config = MoShouExcel.moshouActive
    local msgRet = Msg.gc.GC_MOSHOU_PREVIEW
    msgRet.preViews[0] = #config
    for i = 1, #config do
        msgRet.preViews[i].moshouID = i
        msgRet.preViews[i].name = config[i].moshouName
        msgRet.preViews[i].body = config[i].body
    end
    Msg.send(msgRet, human.fd)
end

-- 布阵界面查询魔兽
function CG_MOSHOU_COMBAT_SKILL_QUERY(human, combatType)
    local cjPrivilege = ChengjiuLogic.checkPrivilege(human, ChengjiuDefine.PRIVILEGE_TYPE_8)
    if not cjPrivilege then return end

    local posSkillID = CombatPosLogic.getCombatMoshou(human, combatType)
    local msgRet = Msg.gc.GC_MOSHOU_COMBAT_SKILL_QUERY
    local config = MoShouExcel.moshouSkill
    makeMoshouSkill(human, msgRet.skill, MOSHOU_ID1, posSkillID)

    Msg.send(msgRet,human.fd)
end

function xilianIsOpen(human)
    if not RoleSystemLogic.isOpen(human, RoleSystemDefine.ROLE_SYS_ID_2003) then
        return 0
    end

    if not human.db.moshou then
        return 0
    end
    
    if human.db.moshou.xilian and human.db.moshou.xilian.init then
        return 1
    end
    
    return 0
     
end

function xilianZDLUpdate(human)
    RoleAttr.cleanHeroAttrCache(human)
    RoleAttr.doCalc(human)
    ObjHuman.sendAttr(human, RoleDefine.ZHANDOULI)
end

function onLogin(human)
    initXilianDB(human)
end

-- 魔兽  洗练
function initXilianDB(human)
    -- if not human.db.moshou then
    --     return
    -- end

    human.db.moshou = human.db.moshou or {}

    if not xilianIsOpen(human) then
        return
    end  

    if human.db.moshou.xilian and human.db.moshou.xilian.init then
        return
    end

    -- local weekDay = Util.getWeekDay()
    -- if weekDay ~= 4 and weekDay ~= 7 then
    --     return
    -- end

    local lv = human.db.moshou.lv or 0
    local isChange = false
    for index, config in ipairs(MoShouExcel.moshouxlCao) do
        if lv >= config.breakLv then
            breakXiLianCao(human, index)
            isChange = true
        end
    end
    human.db.moshou.xilian.init = 1
    
    if isChange then
        xilianZDLUpdate(human)
        if not human.db.moshou.xilian.initTime then
            human.db.moshou.xilian.initTime = os.time()
        end
    end
end

function moshouLvUpToXilianBreak(human)
    if not xilianIsOpen(human) then
        return
    end

    local lv = human.db.moshou.lv or 0
    initXilianDB(human)
    local isChange = false
    for index, config in ipairs(MoShouExcel.moshouxlCao) do
        if lv >= config.breakLv then
            breakXiLianCao(human, index)
            isChange = true
        end
    end

    if isChange then
        xilianZDLUpdate(human)

        if not human.db.moshou.xilian.initTime then
            human.db.moshou.xilian.initTime = os.time()
        end
    end
end

function breakXiLianCao(human, index)   
    if not human.db.moshou then return end
    human.db.moshou.xilian = human.db.moshou.xilian or {}
    if not human.db.moshou.xilian.cnt then
        human.db.moshou.xilian.cnt = 0
    end
    if human.db.moshou.xilian[index] then return end


    local newKey 
    local newValue
    local haveList = {}

    if human.db.moshou and human.db.moshou.xilian then
        for _, db in ipairs(human.db.moshou.xilian) do
            haveList[db.key] = 1
        end
    end

    human.db.moshou.xilian[index] = {}
    local xilian = human.db.moshou.xilian[index]
    xilian.id = math.random(1, 5)
    local newConfig = MoShouExcel.moshouXiLian[xilian.id] 
    for k , v in pairs(newConfig.attrs1) do
        if v and not haveList[v[1]] then
            newKey = v[1]
            newValue = v[2]
        end
    end
    
    xilian.key = newKey
    xilian.value = newValue
end

function getXilianNeed(config, type, index)
    local need = config["need"..type]
    local templateConfig = MoShouExcel.moshouxlCao[index]
    return math.floor(need * templateConfig.add)
end

function CG_MOSHOU_XILIAN_QUERY(human, index)
    initXilianDB(human)
    if index == 0 then
        index = human.db.moshou and human.db.moshou.xilian and #human.db.moshou.xilian or 0
    end
    local msgRet = Msg.gc.GC_MOSHOU_XILIAN_QUERY
    makeXilianQuery(human, msgRet)
    msgRet.index = index
    msgRet.fjAttrs[0] = 0

    for k, config in ipairs(MoShouExcel.moshouxlCao) do
        msgRet.openLv[k] = config.breakLv
    end
    msgRet.openLv[0] = #MoShouExcel.moshouxlCao

    local bestCnt = getXilianBestCnt(human)
    if bestCnt > 0 then
        local config = MoShouExcel.moshouxlHx[bestCnt] 
        for k, v in ipairs(config.attrs) do
            msgRet.fjAttrs[k].key = v[1]
            msgRet.fjAttrs[k].value = v[2]
        end
        msgRet.fjAttrs[0] = #config.attrs
    end
--    Util.printTable(human.db.moshou.xilian)
   -- Msg.trace(msgRet)
    Msg.send(msgRet,human.fd)
end

function xilianRandom(type, cnt)
    local min = 0
    local max = 0
    local random = 0
    for k, v in ipairs(MoShouExcel.moshouxlRandom) do
        if cnt >= v.cnt then
            min = v.random[1]
            max = v.random[2]
            random = v["rareSum"..type]
        end
    end   

    local random = math.random(1,  random)
    for i = min, max do
        local config = MoShouExcel.moshouXiLian[i] 
        if random <= config["rare"..type] then
            return i
        else
            random = random - config["rare"..type]
        end
    end
end

function CG_MOSHOU_XILIAN_OP(human, op, type, index)

    if op == MOSHOU_XILIAN_OP_1 then
        xilianDo(human, index, type)
    elseif op == MOSHOU_XILIAN_OP_2 then
        saveXilian(human, index)
    end
end

function randomXilian(id)
    local config = MoShouExcel.moshouXiLian[id]
    local random = math.random(1, config.attrRareMax)
    
    for k ,v  in ipairs(config.attrs1) do
        if v and v[3] >= random then 
            return v[1], v[2]
        else
            random = random - v[3]
        end
    end
end

function xilianDo(human, index, type)
    if not human.db.moshou or not human.db.moshou.xilian then
        return
    end
    local xilian = human.db.moshou.xilian[index]
    if not xilian then return end
    if type ~= MOSHOU_XILIAN_OP_1 then -- and type ~= MOSHOU_XILIAN_OP_2 取消钻石洗练 
        return
    end

  --  print("  xilianDo    xilianDo  ", index , type)
    local oldConfig = MoShouExcel.moshouXiLian[xilian.id]
    -- 洗练方式  验证材料是否够
    local itemID 
    if type == MOSHOU_XILIAN_OP_1 then
        itemID = ItemDefine.ITEM_LONGXUE_ID
    else
        itemID = ItemDefine.ITEM_ZUANSHI_ID
    end

    local itemCnt = BagLogic.getItemCnt(human, itemID)
    local needCnt = oldConfig["need"..type]
    if itemCnt < needCnt then
        return
    end

    local newId = xilianRandom(type, human.db.moshou.xilian.cnt or 0)
    if not newId then return end
   -- print("  new ID   ", newId)
   local newConfig = MoShouExcel.moshouXiLian[newId] 
--   local random = math.random(1, #newConfig["attrs"..type])
--   local key = newConfig["attrs"..type][random][1]
--   local value = newConfig["attrs"..type][random][2]
    local key, value = randomXilian(newId)
    -- 除所需材料
    BagLogic.delItem(human, itemID, needCnt,  "moshou")

    xilian.lastId = newId
    xilian.lastKey = key
    xilian.lastValue = value
    human.db.moshou.xilian.cnt = human.db.moshou.xilian.cnt + 1

    CG_MOSHOU_XILIAN_QUERY(human, index)


end


function saveXilian(human, index)
    if not human.db.moshou or not human.db.moshou.xilian then
        return
    end
    local xilian = human.db.moshou.xilian[index]
    if not xilian then return end

    if not xilian.lastId then return end

    local haveIndex
    if human.db.moshou and human.db.moshou.xilian then
        for k, v in ipairs(human.db.moshou.xilian) do
            if v.key == xilian.lastKey  then
                haveIndex = k
            end
        end
    end

    if haveIndex then
        local haveDb = human.db.moshou.xilian[haveIndex]
        haveDb.id = xilian.lastId
        haveDb.key =  xilian.lastKey 
        haveDb.value =  xilian.lastValue

        xilian.lastId = nil
        xilian.lastKey = nil
        xilian.lastValue = nil    
    else
        xilian.id = xilian.lastId
        xilian.key =  xilian.lastKey 
        xilian.value =  xilian.lastValue
        xilian.lastId = nil
        xilian.lastKey = nil
        xilian.lastValue = nil    
    end
   
    CG_MOSHOU_XILIAN_QUERY(human, index)

    xilianZDLUpdate(human)
end

function CG_MOSHOU_XILIAN_GIFT_QUERY(human)
    local msgRet = Msg.gc.GC_MOSHOU_XILIAN_GIFT_QUERY
    local absAct = YunYingLogic.onCallBack(human, "onQueryLongXue", nil, true)
    local len = 0
    for k, config in pairs(MoShouExcel.xiLianGift) do 
        len = len + 1
        for j = 1, #config.reward do
            Grid.makeItem(msgRet.list[len].item[j], config.reward[j][1], config.reward[j][2])
        end

        local absCnt = 0
        if absAct and absAct ~= 0 and absAct[config.buyID] then
            absCnt = absAct[config.buyID].cnt or 0
        end
        local intCnt = getXilianInitDoubleCnt(human, config.buyID)
        local isInitDouble = isXilianInitDouble(human)
        local absMax = absAct and absAct[config.buyID] and absAct[config.buyID].max or 0
        local initMax = isInitDouble and config.frequency or 0
        msgRet.list[len].item[0] = #config.reward
        msgRet.list[len].cnt = absCnt + intCnt
        msgRet.list[len].id = k
        msgRet.list[len].maxCnt = absMax + initMax
        msgRet.list[len].icon = config.icon or 0
        msgRet.list[len].name = config.name or ""

        local buyID = config.buyID
        msgRet.list[len].buyMsg[0] = 0
        msgRet.list[len].needItem[0] = 0
        BuyLogic.fontBuyItem(human, msgRet.list[len].buyMsg[1], buyID)
        msgRet.list[len].buyMsg[0] = 1
    end
    msgRet.list[0] = len
    Msg.send(msgRet, human.fd)
end

function isXilianInitDouble(human)
    if human.db.moshou.xilian.initTime then
        local now = os.time()
        local endTime = human.db.moshou.xilian.initTime + 24 * 60 * 60 * 3
        if now <= endTime then
            return true
        end
    end
end

function getXilianInitDoubleCnt(human, buyID)
    if human.db.moshou.xilian.initTime then
        local max = MoShouExcel.xiLianGift[buyID].frequency
        local now = os.time()
        local endTime = human.db.moshou.xilian.initTime + 24 * 60 * 60 * 3
        if now <= endTime then
            human.db.moshou.xilian.giftBuy = human.db.moshou.xilian.giftBuy or {}
            local oldCnt = human.db.moshou.xilian.giftBuy[buyID] or 0
            if oldCnt < max then
               return max - oldCnt                 
           end     
        end       
    end
    return 0
end

function xlianGiftBuy(human, buyID)
    local isDouble = false
    local doubleBuyCnt = 0
    local absCnt = YunYingLogic.onCallBack(human, "onBuyLongXue", buyID, true)
    
    if absCnt then
        isDouble = true
    end

    if human.db.moshou.xilian.initTime and not isDouble then
        local intCnt = getXilianInitDoubleCnt(human, buyID)
        if intCnt > 0 then
            human.db.moshou.xilian.giftBuy[buyID] = human.db.moshou.xilian.giftBuy[buyID] or 0
            human.db.moshou.xilian.giftBuy[buyID] = human.db.moshou.xilian.giftBuy[buyID] + 1
            isDouble = true
        end
    end

    BagLogic.cleanMomentItemList()
    for k, config in pairs(MoShouExcel.xiLianGift) do
        if config and config.buyID == buyID then    
            for _, item in ipairs(config.reward) do
                local itemCnt = item[2]
                if isDouble then
                    itemCnt = itemCnt * 2
                end
                BagLogic.updateMomentItem(1, item[1], itemCnt)
            end
            BagLogic.addMomentItemList(human, "moshouXlGift")
            return
        end
    end
end

-- 获取魔兽 洗练中有几条传说属性
function getXilianBestCnt(human)
    if not human.db.moshou or not human.db.moshou.xilian then
        return 0
    end

    local cnt = 0
    for _, db in ipairs(human.db.moshou.xilian) do
        local config = MoShouExcel.moshouXiLian[db.id]
        if config and config.isSS == 1 then
            cnt = cnt + 1
        end
    end
    return cnt
end

-- 魔兽可升级红点
function moshouLVupRed(human)
    local nowLV = getMoshouLv(human)
    local config = MoShouExcel.moshouLv[nowLV + 1]
    if not config then return end

    local lvUpNeed = config.lvUpNeed
    local enough = nil
    for i = 1, #lvUpNeed do
        local itemID = lvUpNeed[i][1]
        local itemCnt = lvUpNeed[i][2]
        local now = BagLogic.getItemCnt(human, itemID)
        if now >= itemCnt then
            enough =(enough or 0) + 1
        end
    end
    if enough and enough >= #lvUpNeed then return true end
end

function moshouSkillUpRed(human) 
    if not MoShouExcel.moshouActive[MOSHOU_ID1] then return end
    if not MOSHOU_SKILL[MOSHOU_ID1] then return end
    local moshouLV = getMoshouLv(human)
    local skillLV = getMoShouSkillLv(human)
    local config = MoShouExcel.moshouSkillLv

    if skillLV >= #config then return end
    local nextSkillLv = skillLV + 1

    if not MOSHOU_SKILL[MOSHOU_ID1][1][nextSkillLv] then return end

	local nowArgeCnt = nil
	local canUpNeedType = config[nextSkillLv].canUpNeed[1] or 0
	local canUpNeedValue = config[nextSkillLv].canUpNeed[2] or 0

	if canUpNeedType == MOSHOU_SKILLLV_ARGE1 then
	    nowArgeCnt = moshouLV
	else
        return
    end
	
	if not nowArgeCnt then return end

	if nowArgeCnt < canUpNeedValue then return end

    local upCost = config[nextSkillLv].upCost
    for i = 1, #upCost do
        local itemID = upCost[i][1]
        local itemCnt = upCost[i][2]
        local nowCnt = BagLogic.getItemCnt(human, itemID)
        if nowCnt < itemCnt then 
            return 
        end
    end

    return true
end

-- 魔兽可精炼红点
function moshouJinglianRed(human)
    local jingLianLV = getMoShouJiLian(human)
    local moshouLV = getMoshouLv(human)
    local jingLianMaxLV = #MoShouExcel.moshouJingLian
    if jingLianLV < jingLianMaxLV then
        local nextConfig = MoShouExcel.moshouJingLian[jingLianLV + 1]
        local lvUpNeed = nextConfig.lvUpNeed
        local enough = nil
        for i = 1, #lvUpNeed do
            local itemID = lvUpNeed[i][1]
            local itemCnt = lvUpNeed[i][2]
            local nowCnt = BagLogic.getItemCnt(human, itemID, itemCnt)
            if nowCnt >= itemCnt then
                enough =(enough or 0) + 1
            end
        end
        if enough and enough >= #lvUpNeed then return true end
    end
end

-- 魔兽可铸魂红点
function moshouZhunHunRed(human)
    local moshouLV = getMoshouLv(human)
    local zhuHunCntMax = MoShouExcel.moshouLv[moshouLV].zhuHunCntMax
    local nowUseCnt = human.db.moshou and human.db.moshou.zhuHun or 0
    if nowUseCnt < zhuHunCntMax then
        local config = MoShouExcel.moshouZhuHun[1]
        local nowCnt = BagLogic.getItemCnt(human, config.costID)
        if nowCnt > 0 then return true end
    end
end

-- 魔兽洗练红点
function moshouXilianUpRed(human)
    if human.db.moshou and human.db.moshou.xilian then
        local itemCnt = BagLogic.getItemCnt(human, ItemDefine.ITEM_LONGXUE_ID)
        for k, db in ipairs(human.db.moshou.xilian) do
            local config = MoShouExcel.moshouXiLian[db.id]
            if itemCnt >= config.need1 then
                return true
            end
        end
    end
end

function isDot(human)
    local cjPrivilege = ChengjiuLogic.checkPrivilege(human, ChengjiuDefine.PRIVILEGE_TYPE_8)
    if not cjPrivilege then
       return ChengjiuLogic.isFinish(human, 402)
    end

    -- 魔兽可升级
    if moshouLVupRed(human) then return true end

    -- 魔兽可精炼
    if moshouJinglianRed(human) then return true end

    -- 魔兽可铸魂
    if moshouZhunHunRed(human) then return true end

    -- 技能可升级
    if moshouSkillUpRed(human)  then return true end
end

------------------------------外部接口相关-----------------------------------------
-- 创建魔兽结构体
function createCombatMoshow(human, combatType, moshowIdArr)
    local cjPrivilege = ChengjiuLogic.checkPrivilege(human, ChengjiuDefine.PRIVILEGE_TYPE_8)
    if not cjPrivilege then return end

    local id
    if combatType == CombatDefine.COMBAT_TYPE35 and moshowIdArr then
        id = moshowIdArr[1]
    else
        id = CombatPosLogic.getCombatMoshou(human, combatType)
    end

    if not id or id < 1 then return end
    
    if not MOSHOU_SKILL[MOSHOU_ID1][id] then return end

    local config = MoShouExcel.moshouActive[MOSHOU_ID1]

    local pet = { }
    pet.isPet = true
    pet.petID = MOSHOU_ID1
    pet.id = id
    pet.lv = getMoshouLv(human)
    
    pet.body = config.body
    pet.jinglianLV = getMoShouJiLian(human)
    pet.zhuHun = getMoShouZhuHunNum(human)
    pet.skillLV = getMoShouSkillLv(human)
    if human.db.moshou and human.db.moshou.lvAttrsAdd then
		pet.lvAttrsAdd = Util.copyTable(human.db.moshou.lvAttrsAdd)
        
        local lvAttrMul = getLvAttrMulFromTalisman(human)
        lvAttrMul = 1 + lvAttrMul

        for attrId, attrVal in pairs(pet.lvAttrsAdd) do
            pet.lvAttrsAdd[attrId] = attrVal * lvAttrMul
        end
	else
		pet.lvAttrsAdd = {}
	end

    if MOSHOU_SKILL[MOSHOU_ID1][id][pet.skillLV] then
        local skillID = MOSHOU_SKILL[1][id][pet.skillLV]
        local skillConfig = SkillExcel.skill[skillID]
        local cd1 = skillConfig.cd[1] or 0
        local cd2 = skillConfig.cd[2] or 0
        pet.icon = skillID
        pet.skillList = { }
        pet.skillList[1] = { skillID, cd1, cd2 }
        pet.cd = cd2
    end
    return pet
end

-- 获取出战魔兽的skillID
function getPutMoshouSkillID(human, combatType, helpList)
    local putOnMoshou
    if combatType == CombatDefine.COMBAT_TYPE36 and helpList then
        putOnMoshou = helpList[1]
    else
        putOnMoshou = CombatPosLogic.getCombatMoshou(human, combatType)
    end
    if not putOnMoshou or putOnMoshou < 1 then return end
    if not MOSHOU_SKILL[MOSHOU_ID1][putOnMoshou] then return end

    local skillLV = getMoShouSkillLv(human)
    local skillID = MOSHOU_SKILL[MOSHOU_ID1][putOnMoshou][skillLV]

    return skillID
end

-- 获取出战魔兽形象
function getCombatMoshouBody(human, combatType)
    local id = CombatPosLogic.getCombatMoshou(human, combatType)
    if not id or id < 1 then return 0 end

    local config = MoShouExcel.moshouActive[MOSHOU_ID1]
    return config and config.body or 0
end
	
-- 获取魔兽等级
function getMoshouLv(human)
    if human.db.moshou and human.db.moshou.lv then
        return human.db.moshou.lv
    end
    return  1 
end

-- 获取精炼等级
function getMoShouJiLian(human)
    return human.db.moshou and human.db.moshou.jingLian or 0
end

-- 获取技能等级
function getMoShouSkillLv(human)
    if human.db.moshou and human.db.moshou.skillLV then
        return human.db.moshou.skillLV
    end
    return 1
end

-- 获取铸魂数
function getMoShouZhuHunNum(human)
    return human.db.moshou and human.db.moshou.zhuHun or 0
end

-- 出战界面
function setHelp(net, moshouID)
    net.type = CombatDefine.HELP_TYPE1
    net.id = moshouID
    net.icon = MoShouExcel.moshouActive[MOSHOU_ID1].icon
end

function setPosHelp(net, moshouID)
    net.type = CombatDefine.HELP_TYPE1
    net.id = moshouID
    net.icon = MoShouExcel.moshouActive[MOSHOU_ID1].icon
end

-- 战斗阵容封装
function setCombatHelp(net, moshouObj, pos)
    net.pos = pos
    net.id = moshouObj.petID
    net.head = moshouObj.head
    net.body = moshouObj.body
    net.args[0] = 0
end

-- 
function setSkillAndBeskill(human, combatInfo)
	for atkPos, atkObj in pairs(CombatImpl.helpList) do
		local skill = human.db.moshou.putOnMoshou
		if atkObj.side == CombatDefine.ATTACK_SIDE and skill then
			atkObj.skillList = {skill}
			atkObj.beSkillList = {}
		end
		--print("===============", atkPos, skill and skill.skillList, skill and skill.beSkillList)
	end
end