ALTER PROC [dbo].[CalcRebate](
@GameID nvarchar(50),
@UserID nvarchar(50),
@TAX DECIMAL(18,2)
)
As
BEGIN
--DECLARE @TAX DECIMAL(18,2) --税
DECLARE @RATE INT --玩家的上级代理的返点比例
DECLARE @PRATE INT --玩家的上级玩家的返点比例
DECLARE @PUSERID NVARCHAR(50) --玩家的上级玩家ID
DECLARE @MONEY_PUSER DECIMAL(18,2) --玩家的上级玩家的返点金额
DECLARE @CNT_AGENT_REBATE INT --是否给所属代理已经分成,大于0表示已经计算过
DECLARE @CNT_USER_REBATE INT --是否给上级玩家已经分成,大于0表示已经计算过
DECLARE @MONEY_AGENT DECIMAL(18,2) --玩家的上级代理的返点金额
DECLARE @AGENTID_TO INT --玩家的上级代理ID
DECLARE @AGENTID_TO_Name NVARCHAR(50)
DECLARE @BattleAgencyDefaultRebateRate SMALLINT --约战代理的默认返点 给约战代理返点时,如果约战代理没有设置返点,则取默认的约战代理的默认返点
DECLARE @MoneyBAgent DECIMAL(18,2) --直属上级代理的变化前金额
DECLARE @MoneyBAgent1 DECIMAL(18,2) --总代的变化前金额
DECLARE @MoneyBAgent2 DECIMAL(18,2) --一级代理的变化前金额
DECLARE @RoomType INT --房间类型
DECLARE @LEVELID INT --代理级别 1总代 2一级代理 3二级代理
DECLARE @RebateUserRate INT --代理的直属玩家的返点比例
DECLARE @BattleAgentUserID INT --约战代理的UserID
DECLARE @BattleRebateRate INT --约战代理的返点
DECLARE @IsBattleAgent INT --约战模式下,开房的是否是约战代理
DECLARE @AgentID1 INT
DECLARE @RebateRateAgent1 INT
DECLARE @AgencyName1 NVARCHAR(50)
DECLARE @AgentID2 INT
DECLARE @RebateRateAgent2 INT
DECLARE @AgencyName2 NVARCHAR(50)
--DECLARE @MoneyChangeRate INT --金币兑换率
DECLARE @AgencyMoneyChangeLogID INT
SET @AgentID1=0
SET @RebateRateAgent1=0
SET @AgencyName1=''
SET @AgentID2=0
SET @RebateRateAgent2=0
SET @AgencyName2=''
--SET @Tax = 0
SET @RATE = 0
SET @PRATE = 0
SET @PUSERID = 0
SET @MONEY_PUSER = 0
SET @CNT_AGENT_REBATE = 0
SET @MONEY_AGENT = 0
SET @AGENTID_TO = 0
SET @BattleAgencyDefaultRebateRate = 0
SET @AGENTID_TO_Name = ''
SET @MoneyBAgent = 0
SET @LEVELID=0
--SET @MoneyChangeRate=10000
SET @BattleAgentUserID=0
SET @BattleRebateRate=0
SET @IsBattleAgent=0
SELECT @BattleAgencyDefaultRebateRate=[VALUE] FROM CONFIG WHERE [KEY]='BattleAgencyDefaultRebateRate'
--SELECT @MoneyChangeRate=[VALUE] FROM Config WHERE [Key]='MoneyChangeRate'
--SELECT @TAX=TAX FROM [GamePlayer_Log] WHERE GAMEID=@GameID AND AddGold>0 AND Tax>0
IF (@TAX = 0)
RETURN
/*SET @RoomType=0
SELECT @RoomType=RoomType FROM [dbo].[RoomConfig] WHERE AgentName+Name=(SELECT RoomName FROM GameRecord WHERE GameID=@GameID AND UserID=@UserID)
IF @RoomType=2
BEGIN
--约战代理开房 是否约战,约战只给约战代理返点
--普通玩家开房 不扣费 不返点
DECLARE @BattleCreateRoomUserID INT
SELECT @BattleCreateRoomUserID=AgentID FROM [main].[Account_new].[dbo].[Game_Record] WHERE RoomName=(SELECT RoomName FROM GameRecord WHERE GameID=@GameID AND UserID=@UserID)
SELECT @BattleAgentUserID=@BattleCreateRoomUserID,@BattleRebateRate=BattleRebateRate,@IsBattleAgent=IsBattleAgency FROM [AccountExt] WHERE UserID=@BattleCreateRoomUserID
IF @IsBattleAgent<>0
BEGIN
--约战代理开的房间
IF (@BattleRebateRate IS NULL OR @BattleRebateRate=0) AND (@BattleAgencyDefaultRebateRate IS NOT NULL)
SET @BattleRebateRate=@BattleAgencyDefaultRebateRate
SET @MONEY_AGENT=@TAX*@BattleRebateRate/100
SELECT @CNT_AGENT_REBATE=COUNT(*) FROM REBATE WHERE GAMEID=@GAMEID AND USERID=@USERID AND [TYPE]=2 AND TOID=@BattleAgentUserID
IF @CNT_AGENT_REBATE=0
INSERT INTO REBATE(GAMEID,USERID,[TYPE],TOID,REBATE,REBATETIME,IsBalanced) VALUES(@GAMEID,@USERID,2,@BattleAgentUserID,@MONEY_AGENT,GETDATE(),0)--代理直接结算,不需要管理员手动结算
ELSE
UPDATE REBATE SET REBATE=@MONEY_AGENT,REBATETIME=GETDATE() WHERE GAMEID=@GAMEID AND USERID=@USERID AND [TYPE]=2 AND TOID=@BattleAgentUserID
UPDATE ACCOUNTEXT SET REBATE=(SELECT SUM(REBATE) FROM REBATE WHERE ISBALANCED=0 AND TYPE=2 AND TOID=@BattleAgentUserID) WHERE USERID=@BattleAgentUserID
END
ELSE
BEGIN
--普通玩家开的房间
PRINT 'norebate for normal player'
END
END
ELSE*/
BEGIN
--非约战
--SELECT @AGENTID_TO=AGENCYIDBELONGSTO FROM ACCOUNTEXT WHERE USERID=@USERID
DECLARE @AGENTIDORNAME AS NVARCHAR(50)
SELECT @AGENTIDORNAME=ISNULL(AGENTID, '') FROM [Account_new].dbo.[Account] WHERE USERID=@USERID
IF ISNUMERIC(@AGENTIDORNAME) = 1--数字
BEGIN
SET @AGENTID_TO=CONVERT(INTEGER,@AGENTIDORNAME)
END ELSE
BEGIN
SELECT @AGENTID_TO=AGENCYID FROM WEB_AGENCY WHERE AgencyName=@AGENTIDORNAME
END
IF @AGENTID_TO=0 OR NOT EXISTS(SELECT * FROM Web_Agency WHERE AgencyID = @AGENTID_TO)
BEGIN
SELECT @AGENTID_TO=AGENCYID FROM WEB_AGENCY WHERE AGENCYNAME=(SELECT [VALUE] FROM CONFIG WHERE [KEY]='DefaultAgency')
END
BEGIN TRAN
IF (@AGENTID_TO>0)
BEGIN
SELECT @RATE=REBATEPERCENT,@MoneyBAgent=[Account],@AGENTID_TO_Name=AgencyName,@LEVELID=LEVELID,@RebateUserRate=RebateUserPercent FROM WEB_AGENCY WHERE AGENCYID=@AGENTID_TO
END
IF (@RATE>0 AND @AGENTID_TO>0)
BEGIN
--返点给推广代理
IF @LEVELID=1
BEGIN
-- 1 总代直属玩家
-- 2 总代的直属玩家直接返点给总代的35%
SELECT @CNT_AGENT_REBATE=COUNT(*) FROM REBATE WHERE GAMEID=@GAMEID AND USERID=@USERID AND [TYPE]=0 AND TOID=@AGENTID_TO
SELECT @AgentID1=AgencyID,@RebateRateAgent1=RebatePercent,@AgencyName1=AgencyName,@MoneyBAgent1=[Account] FROM Web_Agency WHERE AgencyID=@AGENTID_TO
SET @MONEY_AGENT=@TAX*@RebateRateAgent1/100
IF @CNT_AGENT_REBATE=0
BEGIN
UPDATE Web_Agency SET Rebate=Rebate+@MONEY_AGENT WHERE AgencyID=@AGENTID_TO
UPDATE Web_Agency SET [Account]=[Account]+@MONEY_AGENT WHERE AgencyID=@AGENTID_TO
INSERT INTO [Web_AgencyMoneyChangeLog](AgencyID,AgencyName,MoneyB,ChangeMoney,MoneyA,OperaType,TypeDesc,AddTime,Remark) VALUES(@AGENTID_TO,@AGENTID_TO_Name,@MoneyBAgent,@MONEY_AGENT,@MoneyBAgent+@MONEY_AGENT,1,'返点结算',GETDATE(),@USERID)
SET @AgencyMoneyChangeLogID = 0
SELECT @AgencyMoneyChangeLogID=@@IDENTITY
INSERT INTO Web_AgencyMoneyChangeLogExt([AgencyMoneyChangeLogID],[GameID],[UserID],[Tax]) VALUES (@AgencyMoneyChangeLogID,@GAMEID,@USERID,@TAX)
END
IF @CNT_AGENT_REBATE=0
INSERT INTO REBATE(GAMEID,USERID,[TYPE],TOID,REBATE,REBATETIME,IsBalanced,BalancedTime,BalancedBy) VALUES(@GAMEID,@USERID,0,@AGENTID_TO,@MONEY_AGENT,GETDATE(),1,GetDate(),'admin')--代理直接结算,不需要管理员手动结算
ELSE
UPDATE REBATE SET REBATE=@MONEY_AGENT,REBATETIME=GETDATE() WHERE GAMEID=@GAMEID AND USERID=@USERID AND [TYPE]=0 AND TOID=@AGENTID_TO
END
ELSE IF @LEVELID=2
BEGIN
-- 2 一级代理直属玩家 一级代理的直属玩家返点给总代5%,返给一级代理税的30%,加起来35%
--获取一级代理
SELECT @AgentID2=AgencyID,@RebateRateAgent2=RebatePercent,@AgencyName2=AgencyName,@MoneyBAgent2=[Account] FROM Web_Agency WHERE AgencyID=@AGENTID_TO
IF @AgentID2 > 0
BEGIN
--获取总代
SELECT @AgentID1=AgencyID,@RebateRateAgent1=RebatePercent,@AgencyName1=AgencyName,@MoneyBAgent1=[Account] FROM Web_Agency WHERE AgencyID=(SELECT ParentAgencyID FROM Web_Agency WHERE AgencyID=@AgentID2)
END
--一级代理
SET @MONEY_AGENT=@TAX*@RebateRateAgent2/100
SET @CNT_AGENT_REBATE=0
SELECT @CNT_AGENT_REBATE=COUNT(*) FROM REBATE WHERE GAMEID=@GAMEID AND USERID=@USERID AND [TYPE]=0 AND TOID=@AgentID2
IF @CNT_AGENT_REBATE=0
BEGIN
UPDATE Web_Agency SET Rebate=Rebate+@MONEY_AGENT WHERE AgencyID=@AgentID2
UPDATE Web_Agency SET [Account]=[Account]+@MONEY_AGENT WHERE AgencyID=@AgentID2
INSERT INTO [Web_AgencyMoneyChangeLog](AgencyID,AgencyName,MoneyB,ChangeMoney,MoneyA,OperaType,TypeDesc,AddTime,Remark) VALUES(@AgentID2,@AgencyName2,@MoneyBAgent2,@MONEY_AGENT,@MoneyBAgent2+@MONEY_AGENT,1,'返点结算',GETDATE(),@USERID)
SET @AgencyMoneyChangeLogID = 0
SELECT @AgencyMoneyChangeLogID=@@IDENTITY
INSERT INTO Web_AgencyMoneyChangeLogExt([AgencyMoneyChangeLogID],[GameID],[UserID],[Tax]) VALUES (@AgencyMoneyChangeLogID,@GAMEID,@USERID,@TAX)
END
IF @CNT_AGENT_REBATE=0
INSERT INTO REBATE(GAMEID,USERID,[TYPE],TOID,REBATE,REBATETIME,IsBalanced,BalancedTime,BalancedBy) VALUES(@GAMEID,@USERID,0,@AgentID2,@MONEY_AGENT,GETDATE(),1,GetDate(),'admin')--代理直接结算,不需要管理员手动结算
ELSE
UPDATE REBATE SET REBATE=@MONEY_AGENT,REBATETIME=GETDATE() WHERE GAMEID=@GAMEID AND USERID=@USERID AND [TYPE]=0 AND TOID=@AgentID2
--总代
SET @MONEY_AGENT=@TAX*(@RebateRateAgent1-@RebateRateAgent2)/100
SET @CNT_AGENT_REBATE=0
SELECT @CNT_AGENT_REBATE=COUNT(*) FROM REBATE WHERE GAMEID=@GAMEID AND USERID=@USERID AND [TYPE]=0 AND TOID=@AgentID1
IF @CNT_AGENT_REBATE=0
BEGIN
UPDATE Web_Agency SET Rebate=Rebate+@MONEY_AGENT WHERE AgencyID=@AgentID1
UPDATE Web_Agency SET [Account]=[Account]+@MONEY_AGENT WHERE AgencyID=@AgentID1
INSERT INTO [Web_AgencyMoneyChangeLog](AgencyID,AgencyName,MoneyB,ChangeMoney,MoneyA,OperaType,TypeDesc,AddTime,Remark) VALUES(@AgentID1,@AgencyName1,@MoneyBAgent1,@MONEY_AGENT,@MoneyBAgent1+@MONEY_AGENT,1,'返点结算',GETDATE(),@USERID)
SET @AgencyMoneyChangeLogID = 0
SELECT @AgencyMoneyChangeLogID=@@IDENTITY
INSERT INTO Web_AgencyMoneyChangeLogExt([AgencyMoneyChangeLogID],[GameID],[UserID],[Tax]) VALUES (@AgencyMoneyChangeLogID,@GAMEID,@USERID,@TAX)
END
IF @CNT_AGENT_REBATE=0
INSERT INTO REBATE(GAMEID,USERID,[TYPE],TOID,REBATE,REBATETIME,IsBalanced,BalancedTime,BalancedBy) VALUES(@GAMEID,@USERID,0,@AgentID1,@MONEY_AGENT,GETDATE(),1,GetDate(),'admin')--代理直接结算,不需要管理员手动结算
ELSE
UPDATE REBATE SET REBATE=@MONEY_AGENT,REBATETIME=GETDATE() WHERE GAMEID=@GAMEID AND USERID=@USERID AND [TYPE]=0 AND TOID=@AgentID1
END
ELSE IF @LEVELID=3
BEGIN
-- 二级代理直属玩家 三级代理的玩家赢球后,返点给一二三级代理
--获取一级代理
SELECT @AgentID2=AgencyID,@RebateRateAgent2=RebatePercent,@AgencyName2=AgencyName,@MoneyBAgent2=[Account] FROM Web_Agency WHERE AgencyID=(SELECT ParentAgencyID FROM Web_Agency WHERE AgencyID=@AGENTID_TO)
IF @AgentID2 > 0
BEGIN
--获取总代
SELECT @AgentID1=AgencyID,@RebateRateAgent1=RebatePercent,@AgencyName1=AgencyName,@MoneyBAgent1=[Account] FROM Web_Agency WHERE AgencyID=(SELECT ParentAgencyID FROM Web_Agency WHERE AgencyID=@AgentID2)
END
--二级代理
SET @MONEY_AGENT=@TAX*@RATE/100
SET @CNT_AGENT_REBATE=0
SELECT @CNT_AGENT_REBATE=COUNT(*) FROM REBATE WHERE GAMEID=@GAMEID AND USERID=@USERID AND [TYPE]=0 AND TOID=@AGENTID_TO
IF @CNT_AGENT_REBATE=0
BEGIN
UPDATE Web_Agency SET Rebate=Rebate+@MONEY_AGENT WHERE AgencyID=@AGENTID_TO
UPDATE Web_Agency SET [Account]=[Account]+@MONEY_AGENT WHERE AgencyID=@AGENTID_TO
INSERT INTO [Web_AgencyMoneyChangeLog](AgencyID,AgencyName,MoneyB,ChangeMoney,MoneyA,OperaType,TypeDesc,AddTime,Remark) VALUES(@AGENTID_TO,@AGENTID_TO_Name,@MoneyBAgent,@MONEY_AGENT,@MoneyBAgent+@MONEY_AGENT,1,'返点结算',GETDATE(),@USERID)
SET @AgencyMoneyChangeLogID = 0
SELECT @AgencyMoneyChangeLogID=@@IDENTITY
INSERT INTO Web_AgencyMoneyChangeLogExt([AgencyMoneyChangeLogID],[GameID],[UserID],[Tax]) VALUES (@AgencyMoneyChangeLogID,@GAMEID,@USERID,@TAX)
END
IF @CNT_AGENT_REBATE=0
INSERT INTO REBATE(GAMEID,USERID,[TYPE],TOID,REBATE,REBATETIME,IsBalanced,BalancedTime,BalancedBy) VALUES(@GAMEID,@USERID,0,@AGENTID_TO,@MONEY_AGENT,GETDATE(),1,GetDate(),'admin')--代理直接结算,不需要管理员手动结算
ELSE
UPDATE REBATE SET REBATE=@MONEY_AGENT,REBATETIME=GETDATE() WHERE GAMEID=@GAMEID AND USERID=@USERID AND [TYPE]=0 AND TOID=@AGENTID_TO
--一级代理
SET @MONEY_AGENT=@TAX*(@RebateRateAgent2-@RATE)/100
SET @CNT_AGENT_REBATE=0
SELECT @CNT_AGENT_REBATE=COUNT(*) FROM REBATE WHERE GAMEID=@GAMEID AND USERID=@USERID AND [TYPE]=0 AND TOID=@AgentID2
IF @CNT_AGENT_REBATE=0
BEGIN
UPDATE Web_Agency SET Rebate=Rebate+@MONEY_AGENT WHERE AgencyID=@AgentID2
UPDATE Web_Agency SET [Account]=[Account]+@MONEY_AGENT WHERE AgencyID=@AgentID2
INSERT INTO [Web_AgencyMoneyChangeLog](AgencyID,AgencyName,MoneyB,ChangeMoney,MoneyA,OperaType,TypeDesc,AddTime,Remark) VALUES(@AgentID2,@AgencyName2,@MoneyBAgent2,@MONEY_AGENT,@MoneyBAgent2+@MONEY_AGENT,1,'返点结算',GETDATE(),@USERID)
SET @AgencyMoneyChangeLogID = 0
SELECT @AgencyMoneyChangeLogID=@@IDENTITY
INSERT INTO Web_AgencyMoneyChangeLogExt([AgencyMoneyChangeLogID],[GameID],[UserID],[Tax]) VALUES (@AgencyMoneyChangeLogID,@GAMEID,@USERID,@TAX)
END
IF @CNT_AGENT_REBATE=0
INSERT INTO REBATE(GAMEID,USERID,[TYPE],TOID,REBATE,REBATETIME,IsBalanced,BalancedTime,BalancedBy) VALUES(@GAMEID,@USERID,0,@AgentID2,@MONEY_AGENT,GETDATE(),1,GetDate(),'admin')--代理直接结算,不需要管理员手动结算
ELSE
UPDATE REBATE SET REBATE=@MONEY_AGENT,REBATETIME=GETDATE() WHERE GAMEID=@GAMEID AND USERID=@USERID AND [TYPE]=0 AND TOID=@AgentID2
--总代
SET @MONEY_AGENT=@TAX*(@RebateRateAgent1-@RebateRateAgent2)/100
SET @CNT_AGENT_REBATE=0
SELECT @CNT_AGENT_REBATE=COUNT(*) FROM REBATE WHERE GAMEID=@GAMEID AND USERID=@USERID AND [TYPE]=0 AND TOID=@AgentID1
IF @CNT_AGENT_REBATE=0
BEGIN
UPDATE Web_Agency SET Rebate=Rebate+@MONEY_AGENT WHERE AgencyID=@AgentID1
UPDATE Web_Agency SET [Account]=[Account]+@MONEY_AGENT WHERE AgencyID=@AgentID1
INSERT INTO [Web_AgencyMoneyChangeLog](AgencyID,AgencyName,MoneyB,ChangeMoney,MoneyA,OperaType,TypeDesc,AddTime,Remark) VALUES(@AgentID1,@AgencyName1,@MoneyBAgent1,@MONEY_AGENT,@MoneyBAgent1+@MONEY_AGENT,1,'返点结算',GETDATE(),@USERID)
SET @AgencyMoneyChangeLogID = 0
SELECT @AgencyMoneyChangeLogID=@@IDENTITY
INSERT INTO Web_AgencyMoneyChangeLogExt([AgencyMoneyChangeLogID],[GameID],[UserID],[Tax]) VALUES (@AgencyMoneyChangeLogID,@GAMEID,@USERID,@TAX)
END
IF @CNT_AGENT_REBATE=0
INSERT INTO REBATE(GAMEID,USERID,[TYPE],TOID,REBATE,REBATETIME,IsBalanced,BalancedTime,BalancedBy) VALUES(@GAMEID,@USERID,0,@AgentID1,@MONEY_AGENT,GETDATE(),1,GetDate(),'admin')--代理直接结算,不需要管理员手动结算
ELSE
UPDATE REBATE SET REBATE=@MONEY_AGENT,REBATETIME=GETDATE() WHERE GAMEID=@GAMEID AND USERID=@USERID AND [TYPE]=0 AND TOID=@AgentID1
END
END
--SELECT @PUSERID=PARENTUSERID FROM ACCOUNTEXT WHERE USERID=@USERID
SELECT @PUSERID=ISNULL(ParentID,0) FROM [Account_new].dbo.[Account] WHERE USERID=@USERID
SELECT @PRATE=ISNULL(REBATERATE,0) FROM ACCOUNTEXT WHERE USERID=@PUSERID
IF @PRATE=0
SELECT @PRATE=[VALUE] FROM CONFIG WHERE [KEY]='ShareRebate'
IF (@PRATE>0 AND @PUSERID>0)
BEGIN --玩家上下级
SET @MONEY_PUSER=@TAX*@PRATE/100
SELECT @CNT_USER_REBATE=COUNT(*) FROM REBATE WHERE GAMEID=@GAMEID AND USERID=@USERID AND [TYPE]=1 AND TOID=@PUSERID
IF @CNT_USER_REBATE=0
INSERT INTO REBATE(GAMEID,USERID,[TYPE],TOID,REBATE,REBATETIME,IsBalanced) VALUES(@GAMEID,@USERID,1,@PUSERID,@MONEY_PUSER,GETDATE(),0)
ELSE
UPDATE REBATE SET REBATE=@MONEY_PUSER,REBATETIME=GETDATE() WHERE GAMEID=@GAMEID AND USERID=@USERID AND [TYPE]=1 AND TOID=@PUSERID
IF NOT EXISTS(SELECT USERID FROM ACCOUNTEXT WHERE USERID=@PUSERID)
INSERT INTO ACCOUNTEXT(UserID,AgencyIDBelongsTo,IsBattleAgency,BattleRebateRate,ParentUserID,Rebate,RebateRate,LastRebate,LastCountTime,LastCountAdmin,AllRebate,PaySeed)
SELECT a.UserID,a.AgentID AgencyIDBelongsTo,0 IsBattleAgency, (SELECT [Value] FROM CONFIG WHERE [KEY]='BattleAgencyDefaultRebateRate') BattleRebateRate, ParentId ParentUserID, (SELECT SUM(REBATE) FROM REBATE WHERE ISBALANCED=0 AND TYPE=1 AND TOID=@PUSERID) Rebate, (SELECT [Value] FROM CONFIG WHERE [KEY]='ShareRebate') RebateRate, 0 LastRebate,
NULL LastCountTime, NULL LastCountAdmin, NULL AllRebate, NULL PaySeed FROM [Account_new].[dbo].[Account] a WHERE UserID=@PUSERID
ELSE
UPDATE ACCOUNTEXT SET REBATE=(SELECT SUM(REBATE) FROM REBATE WHERE ISBALANCED=0 AND TYPE=1 AND TOID=@PUSERID) WHERE USERID=@PUSERID
END
IF @@error <> 0 --发生错误
BEGIN
ROLLBACK TRANSACTION
END
ELSE
BEGIN
COMMIT TRANSACTION
END
END
END