代理返点存储过程
Posted yuanxiaoping_21cn_com
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了代理返点存储过程相关的知识,希望对你有一定的参考价值。
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 [email protected] 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 [email protected] AND [email protected])
IF @RoomType=2
BEGIN
--约战代理开房 是否约战,约战只给约战代理返点
--普通玩家开房 不扣费 不返点
DECLARE @BattleCreateRoomUserID INT
SELECT @BattleCreateRoomUserID=AgentID FROM [main].[Account_new].[dbo].[Game_Record] WHERE RoomName=(SELECT RoomName FROM GameRecord WHERE [email protected] AND [email protected])
SELECT @[email protected],@BattleRebateRate=BattleRebateRate,@IsBattleAgent=IsBattleAgency FROM [AccountExt] WHERE [email protected]
IF @IsBattleAgent<>0
BEGIN
--约战代理开的房间
IF (@BattleRebateRate IS NULL OR @BattleRebateRate=0) AND (@BattleAgencyDefaultRebateRate IS NOT NULL)
SET @[email protected]
SET @[email protected]*@BattleRebateRate/100
SELECT @CNT_AGENT_REBATE=COUNT(*) FROM REBATE WHERE [email protected] AND [email protected] AND [TYPE]=2 AND [email protected]
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 [email protected]_AGENT,REBATETIME=GETDATE() WHERE [email protected] AND [email protected] AND [TYPE]=2 AND [email protected]
UPDATE ACCOUNTEXT SET REBATE=(SELECT SUM(REBATE) FROM REBATE WHERE ISBALANCED=0 AND TYPE=2 AND [email protected]) WHERE [email protected]
END
ELSE
BEGIN
--普通玩家开的房间
PRINT ‘norebate for normal player‘
END
END
ELSE*/
BEGIN
--非约战
--SELECT @AGENTID_TO=AGENCYIDBELONGSTO FROM ACCOUNTEXT WHERE [email protected]
DECLARE @AGENTIDORNAME AS NVARCHAR(50)
SELECT @AGENTIDORNAME=ISNULL(AGENTID, ‘‘) FROM [Account_new].dbo.[Account] WHERE [email protected]
IF ISNUMERIC(@AGENTIDORNAME) = 1--数字
BEGIN
SET @AGENTID_TO=CONVERT(INTEGER,@AGENTIDORNAME)
END ELSE
BEGIN
SELECT @AGENTID_TO=AGENCYID FROM WEB_AGENCY WHERE [email protected]
END
IF @AGENTID_TO=0
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 [email protected]_TO
END
IF (@RATE>0 AND @AGENTID_TO>0)
BEGIN
--返点给推广代理
IF @LEVELID=1 OR @LEVELID=2
BEGIN
-- 1 总代直属玩家
-- 2 一级代理直属玩家 总代和一级代理的直属玩家直接返点给代理税的25%
SET @[email protected]*@RebateUserRate/100
SELECT @CNT_AGENT_REBATE=COUNT(*) FROM REBATE WHERE [email protected] AND [email protected] AND [TYPE]=0 AND [email protected]_TO
IF @CNT_AGENT_REBATE=0
BEGIN
UPDATE Web_Agency SET [email protected]_AGENT WHERE [email protected]_TO
UPDATE Web_Agency SET [Account]=[Account][email protected]_AGENT WHERE [email protected]_TO
INSERT INTO [Web_AgencyMoneyChangeLog](AgencyID,AgencyName,MoneyB,ChangeMoney,MoneyA,OperaType,TypeDesc,AddTime,Remark) VALUES(@AGENTID_TO,@AGENTID_TO_Name,@MoneyBAgent,@MONEY_AGENT,@[email protected]_AGENT,1,‘返点结算‘,GETDATE(),@USERID)
SET @AgencyMoneyChangeLogID = 0
SELECT @[email protected]@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 [email protected]_AGENT,REBATETIME=GETDATE() WHERE [email protected] AND [email protected] AND [TYPE]=0 AND [email protected]_TO
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 [email protected]_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 [email protected])
END
--二级代理
SET @[email protected]*@RATE/100
SET @CNT_AGENT_REBATE=0
SELECT @CNT_AGENT_REBATE=COUNT(*) FROM REBATE WHERE [email protected] AND [email protected] AND [TYPE]=0 AND [email protected]_TO
IF @CNT_AGENT_REBATE=0
BEGIN
UPDATE Web_Agency SET [email protected]_AGENT WHERE [email protected]_TO
UPDATE Web_Agency SET [Account]=[Account][email protected]_AGENT WHERE [email protected]_TO
INSERT INTO [Web_AgencyMoneyChangeLog](AgencyID,AgencyName,MoneyB,ChangeMoney,MoneyA,OperaType,TypeDesc,AddTime,Remark) VALUES(@AGENTID_TO,@AGENTID_TO_Name,@MoneyBAgent,@MONEY_AGENT,@[email protected]_AGENT,1,‘返点结算‘,GETDATE(),@USERID)
SET @AgencyMoneyChangeLogID = 0
SELECT @[email protected]@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 [email protected]_AGENT,REBATETIME=GETDATE() WHERE [email protected] AND [email protected] AND [TYPE]=0 AND [email protected]_TO
--一级代理
SET @[email protected]*(@[email protected])/100
SET @CNT_AGENT_REBATE=0
SELECT @CNT_AGENT_REBATE=COUNT(*) FROM REBATE WHERE [email protected] AND [email protected] AND [TYPE]=0 AND [email protected]
IF @CNT_AGENT_REBATE=0
BEGIN
UPDATE Web_Agency SET [email protected]_AGENT WHERE [email protected]
UPDATE Web_Agency SET [Account]=[Account][email protected]_AGENT WHERE [email protected]
INSERT INTO [Web_AgencyMoneyChangeLog](AgencyID,AgencyName,MoneyB,ChangeMoney,MoneyA,OperaType,TypeDesc,AddTime,Remark) VALUES(@AgentID2,@AgencyName2,@MoneyBAgent2,@MONEY_AGENT,@[email protected]_AGENT,1,‘返点结算‘,GETDATE(),@USERID)
SET @AgencyMoneyChangeLogID = 0
SELECT @[email protected]@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 [email protected]_AGENT,REBATETIME=GETDATE() WHERE [email protected] AND [email protected] AND [TYPE]=0 AND [email protected]
--总代
SET @[email protected]*(@[email protected])/100
SET @CNT_AGENT_REBATE=0
SELECT @CNT_AGENT_REBATE=COUNT(*) FROM REBATE WHERE [email protected] AND [email protected] AND [TYPE]=0 AND [email protected]
IF @CNT_AGENT_REBATE=0
BEGIN
UPDATE Web_Agency SET [email protected]_AGENT WHERE [email protected]
UPDATE Web_Agency SET [Account]=[Account][email protected]_AGENT WHERE [email protected]
INSERT INTO [Web_AgencyMoneyChangeLog](AgencyID,AgencyName,MoneyB,ChangeMoney,MoneyA,OperaType,TypeDesc,AddTime,Remark) VALUES(@AgentID1,@AgencyName1,@MoneyBAgent1,@MONEY_AGENT,@[email protected]_AGENT,1,‘返点结算‘,GETDATE(),@USERID)
SET @AgencyMoneyChangeLogID = 0
SELECT @[email protected]@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 [email protected]_AGENT,REBATETIME=GETDATE() WHERE [email protected] AND [email protected] AND [TYPE]=0 AND [email protected]
END
END
--SELECT @PUSERID=PARENTUSERID FROM ACCOUNTEXT WHERE [email protected]
SELECT @PUSERID=ISNULL(PROMOID,0) FROM [Account_new].dbo.[Account] WHERE [email protected]
SELECT @PRATE=ISNULL(REBATERATE,0) FROM ACCOUNTEXT WHERE [email protected]
IF @PRATE=0
SELECT @PRATE=[VALUE] FROM CONFIG WHERE [KEY]=‘ShareRebate‘
IF (@PRATE>0 AND @PUSERID>0)
BEGIN --玩家上下级
SET @[email protected]*@PRATE/100
SELECT @CNT_USER_REBATE=COUNT(*) FROM REBATE WHERE [email protected] AND [email protected] AND [TYPE]=1 AND [email protected]
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 [email protected]_PUSER,REBATETIME=GETDATE() WHERE [email protected] AND [email protected] AND [TYPE]=1 AND [email protected]
UPDATE ACCOUNTEXT SET REBATE=(SELECT SUM(REBATE) FROM REBATE WHERE ISBALANCED=0 AND TYPE=1 AND [email protected]) WHERE [email protected]
END
IF @@error <> 0 --发生错误
BEGIN
ROLLBACK TRANSACTION
END
ELSE
BEGIN
COMMIT TRANSACTION
END
END
END
以上是关于代理返点存储过程的主要内容,如果未能解决你的问题,请参考以下文章
从存储过程执行 SQL Server 代理作业并返回作业结果