local ObjHuman = require("core.ObjHuman")
local FanliExcel = require("excel.fanli").fanli
local Config = require("Config")
local ObjHuman = require("core.ObjHuman")
local VipLogic = require("vip.VipLogic")
local MailManager = require("mail.MailManager")
local MailExcel = require("excel.mail")
local PfLogic = require("platform.PfLogic")
local Define = require("platform.Define")
local Json = require("common.Json")
local Log = require("common.Log")

FANLI_LIST = FANLI_LIST or {}
function initAfterHot()
	for k,v in pairs(FANLI_LIST) do
		FANLI_LIST[k] = nil
	end
	
	for k,v in ipairs(FanliExcel) do
        --是属于这个服务器的
        if PfLogic.inServerIndex(v.svrInd) then
           FANLI_LIST[Config.SVR_INDEX.."-"..v.account] = k
        end
	end
end

-- 读取返利
function setFanLi(human)
    -- 有无返利
	local id = FANLI_LIST[human.db.account]
	if not id then
		return 
	end

    if human.db.fanliVer == 1 then
	   return
	end

	local api_param = {}
	api_param.fanLi = 1
    api_param.account = human.db.account
	_G.thread_http.send(Define.REPORT_FANLI_REPORT_PHP,Json.Encode(api_param))
end

-- 返利回调
function onFanLi(human)
	local id = FANLI_LIST[human.db.account]
	if not id then
		return 
	end

	local conf = FanliExcel[id]	
	if not conf then
		return
	end

    if human.db.fanliVer == 1 then
	   return
	end
	human.db.fanliVer = 1

    local items = { }
	items[1] = { 102,  conf.yuanbao }
	local title = MailExcel.mail[2000].title
	local content = MailExcel.mail[2000].content
	local senderName = MailExcel.mail[2000].senderName
	MailManager.add(MailManager.SYSTEM, human.db._id, title, content, items, senderName)

	VipLogic.addExp(human, conf.vipExp)

    return true
end