local Lang = require("common.Lang")
local Util = require("common.Util")
local Msg = require("core.Msg")
local ObjHuman = require("core.ObjHuman")
local ShopExcel = require("excel.shop")
local Broadcast = require("broadcast.Broadcast")
local ItemDefine = require("bag.ItemDefine")
local BagLogic = require("bag.BagLogic")
local Grid = require("bag.Grid")
local ShopDefine = require("shop.ShopDefine")
local ChatPaoMaLogic = require("chat.ChatPaoMaLogic")
local GuideLogic = require("guide.GuideLogic")
local VipLogic = require("vip.VipLogic")
local MoshouLogic = require("moshou.MoshouLogic")
local UnionLivenessLogic = require("union.UnionLivenessLogic")
local UnionDefine = require("union.UnionDefine")
local ChengjiuLogic = require("chengjiu.ChengjiuLogic")
local ChengjiuDefine = require("chengjiu.ChengjiuDefine")
local EquipLogic = require("equip.EquipLogic")
local FuwenGrid = require("fuwen.FuwenGrid")
local HeroGrowUp = require("absAct.HeroGrowUp")
local YunYingLogic = require("yunying.YunYingLogic")
local Config = require("Config")

local function inServerIndex(svrIndexs)
	for i = 1, #svrIndexs do
		local index1 = svrIndexs[i][1]
		local index2 = svrIndexs[i][2]
		if Config.SVR_INDEX >= index1 and Config.SVR_INDEX <= index2 then
			return true
		end
	end
end

local function initGoodsDb(human, shopType, index, refreshTs)
   if not human.db.shop[shopType].goods then return end
   human.db.shop[shopType].goods[index] = {}
   human.db.shop[shopType].goods[index].nowBuy = 0
   human.db.shop[shopType].goods[index].index = index
   human.db.shop[shopType].goods[index].refreshTs = refreshTs
end

-- 花费刷新总次数
function getCostCntMax(human, shopType)
    if shopType == ShopDefine.SHOP_TYPE_BLACKMARKET then
        return VipLogic.getPowerArgs(human, VipLogic.VIP_POWER4) or 0
    end

    local config = ShopExcel.shop[shopType]
    if not config then return 0 end
    return config.costCnt 
end

-- 剩余花费刷新次数
function getLeftCostCnt(human, shopType)
    local maxCnt = getCostCntMax(human, shopType)
    local shopData = human.db.shop[shopType]
    local useCnt = shopData and shopData.costCnt or 0
    return math.max(maxCnt - useCnt, 0)
end

-- 清除删掉的商品 增加限制的数据
function cleanShopGoods(human, shopType)
   local shopTypeConfig = ShopExcel.shop[shopType]
   if not shopTypeConfig then return end
   local shopConfig = ShopExcel[shopType]
   if not shopConfig then return end
   if not human.db.shop then return end
   if not human.db.shop[shopType] then return end
   if not human.db.shop[shopType].goods then return end

   local shopBuy = GuideLogic.getGuideSkip(human, GuideLogic.SKIPTYPE_JUMP_SHOPBUY)
   if shopType == ShopDefine.SHOP_TYPE_BLACKMARKET and not shopBuy then
      for k,v in pairs(human.db.shop[shopType].goods) do
          human.db.shop[shopType].goods[k].index = 0
          break
      end
   end

   -- 清除没有的
   for k,v in pairs(human.db.shop[shopType].goods) do
      local tempConfig = shopConfig[v.index]
      if not tempConfig then
         human.db.shop[shopType].goods[k] = nil
      end
   end

   -- 加入新增的
   if shopTypeConfig.refreshType == ShopDefine.SHOP_REFRESH_TYPE0 then
      for index, v in pairs(shopConfig) do
         if not human.db.shop[shopType].goods[index] then
            if v.limitType ~= ShopDefine.SHOP_LIMIT_TYPE0 then
               initGoodsDb(human, shopType, index, 0)
            end
         end
      end
   end
end


-- 查询商店物品
function query(human, shopType)
	local shopTypeConfig = ShopExcel.shop[shopType]
	if not shopTypeConfig then return end
    if not inServerIndex(shopTypeConfig.serverIndex) then return end

	local msgRet = Msg.gc.GC_SHOP_QUERY
    msgRet.shopInfo.shopType = shopType
    msgRet.shopInfo.mainType = shopTypeConfig.mainType
    msgRet.shopInfo.refreshType = shopTypeConfig.refreshType
    msgRet.shopInfo.name = shopTypeConfig.name
    msgRet.shopInfo.icon = shopTypeConfig.icon
    msgRet.shopList[0] = 0
    for stype, sconfig in pairs(ShopExcel.shop) do
        if sconfig.mainType == shopTypeConfig.mainType and inServerIndex(sconfig.serverIndex) then
            msgRet.shopList[0] = msgRet.shopList[0] + 1
            local net = msgRet.shopList[msgRet.shopList[0]]
            net.shopType = stype
            net.mainType = sconfig.mainType
            net.refreshType = sconfig.refreshType
            net.name = sconfig.name
            net.icon = sconfig.icon
        end
    end

	Grid.makeItem(msgRet.refreshItem,shopTypeConfig.refreshItemID,shopTypeConfig.refreshItemCnt)

	local shopConfig = ShopExcel[shopType]
	if not shopConfig then return end

	local now = os.time()

    initHumanShopDB(human,shopType)
    checkFreeCnt(human,shopType)
	msgRet.nextRefreshTime = 0
	msgRet.freeCnt = human.db.shop[shopType].freeCnt
	msgRet.freeMax = shopTypeConfig.freeCnt
	msgRet.costCnt = getLeftCostCnt(human, shopType)
	msgRet.costMax = getCostCntMax(human, shopType)

    if human.db.shop[shopType].refreshTs > now then
        msgRet.nextRefreshTime = human.db.shop[shopType].refreshTs - now
    end

    cleanShopGoods(human, shopType)

    -- 不刷新的没有记录的 走配置
    local cnt = 0
    if shopTypeConfig.refreshType == ShopDefine.SHOP_REFRESH_TYPE0 then
       for index, v in pairs(shopConfig) do
          if not human.db.shop[shopType].goods[index] then
             local tempConfig = v
             cnt = cnt + 1
             Grid.makeItem(msgRet.list[cnt].itemData, tempConfig.itemID, tempConfig.cnt)
             msgRet.list[cnt].itemIndex = index
             Grid.makeItem(msgRet.list[cnt].needItem, tempConfig.needItemID, tempConfig.price)
		     msgRet.list[cnt].maxCanBuy = tempConfig.limitBuyCnt
		     msgRet.list[cnt].nowBuy = 0
		     msgRet.list[cnt].zhekou  = tempConfig.zhekou
		     msgRet.list[cnt].order  = tempConfig.order
             msgRet.list[cnt].needVipLv = tempConfig.needVipLv
             msgRet.list[cnt].rare = tempConfig.rare
             msgRet.list[cnt].limitType = tempConfig.limitType
          end
       end
    end

    for _, v in pairs(human.db.shop[shopType].goods) do
        local tempConfig = shopConfig[v.index]
        cnt = cnt + 1
        Grid.makeItem(msgRet.list[cnt].itemData, tempConfig.itemID, tempConfig.cnt)
        msgRet.list[cnt].itemIndex = v.index
        Grid.makeItem(msgRet.list[cnt].needItem, tempConfig.needItemID, tempConfig.price)
		msgRet.list[cnt].maxCanBuy = tempConfig.limitBuyCnt
		msgRet.list[cnt].nowBuy = v.nowBuy
		msgRet.list[cnt].zhekou  = tempConfig.zhekou
		msgRet.list[cnt].order  = tempConfig.order
        msgRet.list[cnt].needVipLv = tempConfig.needVipLv
        msgRet.list[cnt].rare = tempConfig.rare
        msgRet.list[cnt].limitType = tempConfig.limitType
    end

	msgRet.list[0] = cnt
	Msg.send(msgRet, human.fd)
end

-- 刷新物品
function refresh(human, shopType)
	local shopConfig = ShopExcel.shop[shopType]
	if not shopConfig then return end
    if not inServerIndex(shopConfig.serverIndex) then return end
	
    cleanShopGoods(human, shopType)

    -- 是否可刷新
    -- 不是主动刷新，主动+日，主动+购完
    if shopConfig.refreshType ~= ShopDefine.SHOP_REFRESH_TYPE1 
    and shopConfig.refreshType ~= ShopDefine.SHOP_REFRESH_TYPE2 
    and shopConfig.refreshType ~= ShopDefine.SHOP_REFRESH_TYPE7 then
        return
    end

    -- 主动+日 主动+购完重置 主动刷新不判断时间
    if shopConfig.refreshType ~= ShopDefine.SHOP_REFRESH_TYPE2 
    or shopConfig.refreshType ~= ShopDefine.SHOP_REFRESH_TYPE7 then
        local now = os.time()
        if human.db.shop[shopType].refreshTs > now  -- 刷新时间未到
           and human.db.shop[shopType].freeCnt <= 0     -- 免费刷新次数小于0
           and human.db.shop[shopType].costCnt >= ShopExcel.shop[shopType].costCnt
           and ShopExcel.shop[shopType].costCnt ~= -1 then -- 花费刷新次数小于0
            return              
        end
    end

	local isFree = nil -- 是否免费

    -- 不需要刷新物品，则可免费刷新
	if shopConfig.refreshItemID == 0 then
        isFree = true
    else
        -- 需要物品，且可免费次数不等于0，且有剩余可免费次数
        if shopConfig.freeCnt ~= 0 and human.db.shop[shopType].freeCnt > 0 then
            isFree = true
        end
    end

	-- 判断道具
	local needItemID = shopConfig.refreshItemID
	local needItemCnt = shopConfig.refreshItemCnt
	
    -- 需要道具
	if isFree ~= true then
        -- 判断剩余刷新次数
        if getCostCntMax(human, shopType) ~= 0 and getCostCntMax(human, shopType) ~= -1 and getLeftCostCnt(human, shopType) < 1 then
            return Broadcast.sendErr(human, Lang.SHOP_REFRESH_ERR_CNT)
        end

        -- 判断道具
		local nowItemCnt = BagLogic.getItemCnt(human, needItemID)
		if nowItemCnt < needItemCnt then
			return Broadcast.sendErr(human, Util.format(Lang.SHOP_ERR_NOITEM,ItemDefine.getValue(needItemID,"name")))
		end

        -- 扣除道具
		BagLogic.delItem(human, needItemID, needItemCnt, "shop_refresh")
        human.db.shop[shopType].costCnt = human.db.shop[shopType].costCnt + 1
    else
        if shopConfig.refreshItemID ~= 0 then
            human.db.shop[shopType].freeCnt = human.db.shop[shopType].freeCnt - 1
        end
	end
	
    -- 刷新
    initHumanShopDB(human,shopType,true)

    if isFree then
        -- 如果没有免费刷新了则刷新时间置0
        if human.db.shop[shopType].freeCnt >= shopConfig.freeCnt then
            human.db.shop[shopType].refreshTs = 0
        end
    end
    
	-- 通知客户端
	query(human, shopType)
end

function buyAllItemsRefresh(human,shopType)
    for itemIndex in pairs(human.db.shop[shopType].goods) do
        local tempConfig = ShopExcel[shopType][itemIndex]
        if tempConfig == nil then
            return
        end
        local maxCanBuy = tempConfig.limitBuyCnt
        if maxCanBuy ~= 0 then 
            local nowBuy = human.db.shop[shopType].goods[itemIndex].nowBuy
            if nowBuy < maxCanBuy then 
                return
            end
        end
    end

    initHumanShopDB(human,shopType,true)

    return true
end

-- 购买物品
function buy(human, shopType, itemID, itemIndex, buyCnt)
	if buyCnt < 1 then return end --购买数量不该小于1呀

    if buyCnt > 30000 then
        return
    end	

    local tempConfig = ShopExcel[shopType][itemIndex]
	if tempConfig == nil then
		return
	end

    -- 商店强制效验
    if tempConfig.itemID ~= itemID then
	    return Broadcast.sendErr(human, Lang.ITEM_BUY_ERROR)
	end

    if tempConfig.needVipLv ~= 0 and VipLogic.getVipLv(human) < tempConfig.needVipLv then
	    return Broadcast.sendErr(human, Lang.VIP_LV_TOO_LOW)
	end

    -- 装备检测数量
    if ItemDefine.isEquip(itemID) then
       if not EquipLogic.checkEmptyCnt(human, buyCnt * tempConfig.cnt) then
          return
       end
    end

    -- 符文检测数量
    if ItemDefine.isFuwen(itemID) then
       if not FuwenGrid.checkEmptyCnt(human, buyCnt * tempConfig.cnt) then
          return
       end
    end

    cleanShopGoods(human, shopType)

	-- 判断上限
	local maxCanBuy = tempConfig.limitBuyCnt
    local dbIndex = itemIndex
	local nowBuy = 0
    for k,v in pairs(human.db.shop[shopType].goods) do
        if v.index == itemIndex then
            nowBuy = v.nowBuy
            dbIndex = k
            break
        end
    end
	
	if maxCanBuy ~= 0 and maxCanBuy - nowBuy < buyCnt then 
		return
	end
	
    if dbIndex == 0 then
        return
    end
	-- 道具判断
	local needItemID = tempConfig.needItemID
	local needItemCnt = tempConfig.price * buyCnt
    
	local nowItemCnt = BagLogic.getItemCnt(human, needItemID)
	
	if nowItemCnt < needItemCnt then
		return Broadcast.sendErr(human, Util.format(Lang.SHOP_ERR_NOITEM,ItemDefine.getValue(needItemID,"name")))
	end

    -- 改db
	if human.db.shop[shopType].goods and human.db.shop[shopType].goods[dbIndex] then
		human.db.shop[shopType].goods[dbIndex].nowBuy = human.db.shop[shopType].goods[dbIndex].nowBuy + buyCnt
        local refreshType = ShopExcel.shop[shopType].refreshType 
        if refreshType == ShopDefine.SHOP_REFRESH_TYPE6 
        or refreshType == ShopDefine.SHOP_REFRESH_TYPE7 then
            buyAllItemsRefresh(human,shopType)
        end
	end

	-- 先删
	BagLogic.delItem(human, needItemID, needItemCnt, "shop_buy", nil, itemID, buyCnt * tempConfig.cnt)
	
	-- 后加
	BagLogic.addItem(human, itemID, buyCnt * tempConfig.cnt, "shop_buy")
	
	-- 加道具
	--ChatPaoMaLogic.trigger(human, ChatPaoMaLogic.TRIGGER_TYPE_1, itemID, buyCnt * tempConfig.cnt, needItemID, needItemCnt)
	
	--通知客户端
	local rewardID = itemID
	local rewardCnt = tempConfig.cnt * buyCnt
	local msgRet = Msg.gc.GC_SHOP_BUY
	msgRet.shopType = shopType
    if ItemDefine.isEquip(rewardID) then
        EquipLogic.makeEquipItemOne(human, msgRet.item)
    else
       	Grid.makeItem(msgRet.item, rewardID, rewardCnt)
    end
	Msg.send(msgRet, human.fd)
	
	query(human, shopType)

	GuideLogic.setDoSpecialGuide(human, GuideLogic.SKIPTYPE_JUMP_SHOPBUY)

    -- 商店购买物品回调
    if shopType == ShopDefine.SHOP_TYPE_UNION then
        UnionLivenessLogic.touchLiveness(human,UnionDefine.UNION_LIVENESS_SHOP,1)
    end

    if shopType == ShopDefine.SHOP_TYPE_BLACKMARKET then
        ChengjiuLogic.onCallback(human,ChengjiuDefine.CJ_TASK_TYPE_4,1)
	    HeroGrowUp.onCallback(human, HeroGrowUp.TASKTYPE10, 1)
        YunYingLogic.onCallBack(human, "onShopBuy",1)   
    end
end


-- 初始化玩家商店数据
function initHumanShopDB(human, shopType,refresh)
	local shopTypeConfig = ShopExcel.shop[shopType]
	local shopConfig = ShopExcel[shopType]
	if not shopConfig then return end
    if not shopTypeConfig then return end
    if not inServerIndex(shopTypeConfig.serverIndex) then return end

    -- 下次刷新时间
    local initDb = nil
    if not human.db.shop or not human.db.shop[shopType] or not human.db.shop[shopType].goods then
        -- 初始化
        initDb = true
    elseif refresh == nil then
        -- 不是初始化也不是刷新
        return
    end

    human.db.shop = human.db.shop or {}
	human.db.shop[shopType] =  human.db.shop[shopType] or {}
    human.db.shop[shopType].freeCnt = human.db.shop[shopType].freeCnt or shopTypeConfig.freeCnt -- 免费刷新次数
	human.db.shop[shopType].costCnt = human.db.shop[shopType].costCnt or 0                      -- 钻石刷新次数
	human.db.shop[shopType].refreshTs = human.db.shop[shopType].refreshTs or 0   
    human.db.shop[shopType].goods = human.db.shop[shopType].goods or {}
    
    -- 固定物品，重置类型
    local now = os.time()
    if shopTypeConfig.refreshGoods == 1 then
	    -- 随机物品，刷新类型
        human.db.shop[shopType].goods = {}
        getRandShopGoods(human,shopType)
    else
       for i, v in pairs(shopConfig) do
            -- 刷新时，活动物品不重置
            local refreshTs = v.refreshTs * 60 * 60
            local goodsRefreshTs = human.db.shop[shopType].goods[i] and human.db.shop[shopType].goods[i].refreshTs or 0
            local endRefreshTs = 0
            if initDb == true or refresh ~= nil then                -- 初始化
                endRefreshTs = now + refreshTs
            else                                -- 其他
                local resetCnt = 0
                if v.limitType == ShopDefine.SHOP_LIMIT_TYPE2 then                -- 日限购
                    local sameDay = Util.isSameDayByTimes(now, goodsRefreshTs)
                    if sameDay ~= true then
                        local dayStart = Util.getDayStartTime(now)
                        endRefreshTs = dayStart + refreshTs
                    end
                elseif v.limitType == ShopDefine.SHOP_LIMIT_TYPE3 then        -- 周限购
                    local sameWeek = Util.isSameWeek(now, goodsRefreshTs)
                    if sameWeek ~= true then
                        local weekStart = Util.getWeekStartTime(now)
                        endRefreshTs = weekStart + refreshTs
                    end
                elseif v.limitType == ShopDefine.SHOP_LIMIT_TYPE4 then        -- 月限购
                    local sameMonth = Util.isSameMonth(now, goodsRefreshTs)
                    if sameMonth ~= true then
                        local monthStart = Util.getMonthStartTime(now)
                        endRefreshTs = monthStart + refreshTs
                    end
                end
            end

            if v.limitType ~= ShopDefine.SHOP_LIMIT_TYPE0 and endRefreshTs > 0 then
               initGoodsDb(human, shopType, i, endRefreshTs)
            end
	    end
    end

    -- 设置免费刷新时间
    if refresh ~= nil then
        if shopTypeConfig.refreshType == ShopDefine.SHOP_REFRESH_TYPE1 
           and human.db.shop[shopType].refreshTs == 0 then
            human.db.shop[shopType].refreshTs = os.time() + shopTypeConfig.refreshTs * 60 * 60
        end
    end
    
    -- 设置商店重置时间
    if shopTypeConfig.refreshType == ShopDefine.SHOP_REFRESH_TYPE3 
    or shopTypeConfig.refreshType == ShopDefine.SHOP_REFRESH_TYPE2 then
        -- 每日自动刷新 或 主动刷新+日刷新
        local dayStart = Util.getDayStartTime(now)
        human.db.shop[shopType].refreshTs = dayStart + 24*60*60            -- 下次刷新时间
    elseif shopTypeConfig.refreshType == ShopDefine.SHOP_REFRESH_TYPE4 then
        -- 周刷新
        local weekStart = Util.getWeekStartTime(now)
        human.db.shop[shopType].refreshTs = weekStart + 7*24*60*60            -- 下次刷新时间
    elseif shopTypeConfig.refreshType == ShopDefine.SHOP_REFRESH_TYPE5 then
        -- 月刷新
        local monthStart = Util.getMonthStartTime(now)
        human.db.shop[shopType].refreshTs = monthStart + 30*24*60*60          -- 下次刷新时间
    end
end

-- 刷新商店物品
local specialTb = {1,5,6,7,8}
function getRandShopGoods(human,shopType)
    local net = human.db.shop[shopType].goods
	local shopTypeConfig = ShopExcel.shop[shopType]
    if not inServerIndex(shopTypeConfig.serverIndex) then return end 
    if shopTypeConfig.refreshGoods ~= 1 then
        return
    end

	local shopConfig = ShopExcel[shopType]
    if not shopConfig then return end
    
    local now = os.time()
    local indexTb = {}

    -- 需要随机物品次数
    local count = 0
    local totalWeight = 0
    local totalWeight1 = 0
    local totalWeight2 = 0
    for k, v in pairs(shopConfig) do
        totalWeight = totalWeight + v.weight

        if v.itemType == 1 then
           totalWeight1 = totalWeight1 + v.weight
        end

        if v.itemType == 2 then
           totalWeight2 = totalWeight2 + v.weight
        end
    end

    for i = 1,shopTypeConfig.goodsCnt do
        local randomNum = math.random(1,totalWeight)
        for j, v in pairs(shopConfig) do
            if indexTb[j] == nil then
                if randomNum <= v.weight then
                    count = count + 1
                    net[count] = {}
                    net[count].nowBuy = 0
                    net[count].index = j
                    net[count].refreshTs = now + shopTypeConfig.refreshTs * 60 * 60
                    indexTb[j] = true
                    totalWeight = totalWeight - v.weight
                    break
                else
                    randomNum = randomNum - v.weight
                end
            end
        end
    end

    -- 特殊商店刷新
     if shopType == ShopDefine.SHOP_TYPE_XUYUAN then
        local len = #shopConfig
        for _,v in pairs(specialTb) do
            local randomNum = 0
            if v == 1 then
               randomNum = math.random(1, totalWeight1)
            else
               randomNum = math.random(1, totalWeight2)
            end
            
            for j, b in pairs(shopConfig) do
                if v == b.itemType or
                  (v ~= 1 and b.itemType == 2) then
                    if randomNum <= b.weight then
                        net[v] = {}
                        net[v].nowBuy = 0
                        net[v].index = j
                        net[v].refreshTs = now + shopTypeConfig.refreshTs * 60 * 60
                        break
                    else
                        randomNum = randomNum - b.weight
                    end
                end
            end
        end
    end
end

function updateDaily(human)
    local shopTypeConfig = ShopExcel.shop
    local len = #shopTypeConfig

    local now = os.time()
    for i ,v in pairs(shopTypeConfig) do

        if inServerIndex(v.serverIndex) then
        -- 日刷新 or 主动+日
          cleanShopGoods(human, i)
          
          if i == ShopDefine.SHOP_TYPE_BLACKMARKET and human.db.shop and human.db.shop[i] then
              human.db.shop[i].costCnt = 0
          end
          
          if v.refreshType == ShopDefine.SHOP_REFRESH_TYPE3 
          or v.refreshType == ShopDefine.SHOP_REFRESH_TYPE2 then
              initHumanShopDB(human,i,true)
          -- 周刷新
          elseif v.refreshType == ShopDefine.SHOP_REFRESH_TYPE4 then
              if human.db.shop and human.db.shop[i] then
                  local sameWeek = Util.isSameWeek(now,human.db.shop[i].refreshTs)
                  if sameWeek ~= true then
                      initHumanShopDB(human,i,true)
                  end
              else
                  initHumanShopDB(human,i,true)
              end
          -- 月刷新
          elseif v.refreshType == ShopDefine.SHOP_REFRESH_TYPE5 then
              if human.db.shop and human.db.shop[i] then
                  local sameMonth = Util.isSameMonth(now,human.db.shop[i].refreshTs)
                  if sameMonth ~= true then
                      initHumanShopDB(human,i,true)
                  end
              else
                  initHumanShopDB(human,i,true)
              end
          -- 没配刷新类型，则根据道具类型来刷新
          else
              if human.db.shop and human.db.shop[i] then
                  for k, v in pairs(human.db.shop[i].goods) do
                      local config = ShopExcel[i][v.index]
                      -- 日限购
                      if config.limitType == ShopDefine.SHOP_LIMIT_TYPE2 then
                          local dayStart = Util.getDayStartTime(now)
                          v.nowBuy = 0
                          v.refreshTs = dayStart + config.refreshTs * 60 * 60
                      -- 周限购
                      elseif config.limitType == ShopDefine.SHOP_LIMIT_TYPE3 then
                          local sameWeek = Util.isSameWeek(now,v.refreshTs)
                          if sameWeek ~= true then
                              local weekStart = Util.getWeekStartTime(now)
                              v.nowBuy = 0
                              v.refreshTs = weekStart + config.refreshTs * 60 * 60
                          end
                      -- 月限购
                      elseif config.limitType == ShopDefine.SHOP_LIMIT_TYPE4 then
                          local sameMonth = Util.isSameMonth(now,v.refreshTs)
                          if sameMonth ~= true then
                              local monthStart = Util.getMonthStartTime(now)
                              v.nowBuy = 0
                              v.refreshTs = monthStart + config.refreshTs * 60 * 60
                          end
                      end
                  end
              end
          end
       end
    end
end

function checkFreeCnt(human,shopType)
    local shopConfig = ShopExcel.shop[shopType]
    if not shopConfig then return end
    if not inServerIndex(shopConfig.serverIndex) then return end

    local now = os.time()
    -- 当前商店免费刷新次数不是满的
    local freeCnt = shopConfig.freeCnt - human.db.shop[shopType].freeCnt
    if freeCnt > 0 then
        local opTs = human.db.shop[shopType].refreshTs - shopConfig.refreshTs*60*60
        local overTime = now - opTs
        local cnt = math.floor(overTime/(shopConfig.refreshTs*60*60))
        if cnt == 0 then
            return
        end
        if cnt >= freeCnt then
            human.db.shop[shopType].freeCnt = shopConfig.freeCnt
            human.db.shop[shopType].refreshTs = 0
        else
            human.db.shop[shopType].freeCnt = human.db.shop[shopType].freeCnt + cnt
            human.db.shop[shopType].refreshTs = human.db.shop[shopType].refreshTs + (shopConfig.refreshTs*60*60*cnt)
        end
    end
end