USE [master]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: **
-- Create date: 2011-06-22
-- Description: 添加中獎記錄
-- 成功返回失敗返回
-- =============================================
CREATE PROCEDURE prMgame_addPrizesRecord
@intUserId int ,--使用者ID
@vchIptvName varchar(50),--IPTV名稱
@intPrizesType smallint ,--周獎還是終極大獎 0:周獎 1:終極大獎
@intPrizesNo int,--獎品編號
@vchPrizesName varchar(50)=null output,--獎品名稱
@outPrizesNo int=0 output,--獎品編號
@intReturnCode int=1 output,--處理傳回值
@vchMsg varchar(128)=null output --處理資訊
AS
BEGIN
BEGIN TRANSACTION
DECLARE @nowTime DATETIME
DECLARE @nowPrizesSum INT
DECLARE @nowRank INT
DECLARE @nowWeek smallint --當天屬於哪個星期
DECLARE @lestWeek smallint
SELECT @nowTime=GETDATE() FROM dbo.sysobjects --擷取系統時間
--取得第幾周
IF NOT EXISTS (SELECT * FROM WeekTime WHERE WeekStartTime<=@nowTime AND WeekEndTime >=@nowTime)
BEGIN
SET @intReturnCode=1
SET @vchMsg='獎品添加失敗,無法擷取周號。'
ROLLBACK TRANSACTION
RETURN 1
END
SELECT @nowWeek=WeekNo FROM WeekTime WHERE WeekStartTime<=@nowTime AND WeekEndTime >=@nowTime
SET @lestWeek = @nowWeek - 1
print @lestWeek
IF (@intPrizesType = 0)
BEGIN
--周獎
IF EXISTS (SELECT * FROM PrizesRecode WHERE UserId=@intUserId AND WeekNo=@lestWeek AND PrizesType=@intPrizesType)
BEGIN
SET @intReturnCode=1
SET @vchMsg='獎品添加失敗,使用者已經抽過獎了。'
ROLLBACK TRANSACTION
RETURN 1
END
IF EXISTS (SELECT * FROM Prizes WHERE PrizesNo=@intPrizesNo AND PrizesRemainder > 0)
BEGIN
--獎品數量充足
SELECT @nowPrizesSum=PrizesRemainder,@vchPrizesName=PrizesName,@outPrizesNo=PrizesNo FROM Prizes WHERE PrizesNo=@intPrizesNo
SET @nowPrizesSum = @nowPrizesSum - 1
UPDATE Prizes SET PrizesRemainder=@nowPrizesSum WHERE PrizesNo=@outPrizesNo
IF @@ERROR<>0
BEGIN
SET @intReturnCode=1
SET @vchMsg='獎品添加失敗,獎品表更新失敗。'
ROLLBACK TRANSACTION
RETURN 1
END
END
ELSE
BEGIN
--獎品數量不足
SELECT TOP 1 @nowPrizesSum=PrizesRemainder,@vchPrizesName=PrizesName,@outPrizesNo=PrizesNo FROM Prizes WHERE PrizesRemainder > 0 AND PrizesType = @intPrizesType ORDER BY PrizesRemainder DESC
SET @nowPrizesSum = @nowPrizesSum - 1
UPDATE Prizes SET PrizesRemainder=@nowPrizesSum WHERE PrizesNo=@outPrizesNo
IF @@ERROR<>0
BEGIN
SET @intReturnCode=1
SET @vchMsg='獎品添加失敗,獎品表更新失敗。'
ROLLBACK TRANSACTION
RETURN 1
END
END
SELECT @nowRank=WeekRank FROM WeekRank WHERE UserId=@intUserId AND WeekNo=@lestWeek
INSERT INTO PrizesRecode (UserId,IptvName,PrizesNo,PrizesType,RecodeTime,WeekNo,GameRank) VALUES (@intUserId,@vchIptvName,@outPrizesNo,@intPrizesType,@nowTime,@lestWeek,@nowRank)
IF @@ERROR<>0
BEGIN
SET @intReturnCode=1
SET @vchMsg='獎品添加失敗,獎品記錄表更新失敗。'
ROLLBACK TRANSACTION
RETURN 1
END
SET @intReturnCode=0
SET @vchMsg='獎品擷取成功'
COMMIT TRANSACTION
RETURN 0
END
ELSE IF (@intPrizesType = 1)
BEGIN
--終極大獎
IF EXISTS (SELECT * FROM PrizesRecode WHERE UserId=@intUserId AND PrizesType=@intPrizesType)
BEGIN
SET @intReturnCode=1
SET @vchMsg='獎品添加失敗,使用者已經抽過獎了。'
ROLLBACK TRANSACTION
RETURN 1
END
IF EXISTS (SELECT * FROM Prizes WHERE PrizesNo=@intPrizesNo AND PrizesRemainder > 0)
BEGIN
--獎品數量充足
SELECT @nowPrizesSum=PrizesRemainder,@vchPrizesName=PrizesName,@outPrizesNo=PrizesNo FROM Prizes WHERE PrizesNo=@intPrizesNo
SET @nowPrizesSum = @nowPrizesSum - 1
UPDATE Prizes SET PrizesRemainder=@nowPrizesSum WHERE PrizesNo=@outPrizesNo
IF @@ERROR<>0
BEGIN
SET @intReturnCode=1
SET @vchMsg='獎品添加失敗,獎品表更新失敗。'
ROLLBACK TRANSACTION
RETURN 1
END
END
ELSE
BEGIN
--獎品數量不足
SELECT TOP 1 @nowPrizesSum=PrizesRemainder,@vchPrizesName=PrizesName,@outPrizesNo=PrizesNo FROM Prizes WHERE PrizesRemainder > 0 AND PrizesType = @intPrizesType ORDER BY PrizesRemainder DESC
SET @nowPrizesSum = @nowPrizesSum - 1
UPDATE Prizes SET PrizesRemainder=@nowPrizesSum WHERE PrizesNo=@outPrizesNo
IF @@ERROR<>0
BEGIN
SET @intReturnCode=1
SET @vchMsg='獎品添加失敗,獎品表更新失敗。'
ROLLBACK TRANSACTION
RETURN 1
END
END
SELECT @nowRank=AllRank FROM AllRank WHERE UserId=@intUserId
INSERT INTO PrizesRecode (UserId,IptvName,PrizesNo,PrizesType,RecodeTime,GameRank) VALUES (@intUserId,@vchIptvName,@outPrizesNo,@intPrizesType,@nowTime,@nowRank)
IF @@ERROR<>0
BEGIN
SET @intReturnCode=1
SET @vchMsg='獎品添加失敗,獎品記錄表更新失敗。'
ROLLBACK TRANSACTION
RETURN 1
END
SET @intReturnCode=0
SET @vchMsg='獎品擷取成功'
COMMIT TRANSACTION
RETURN 0
END
SET @intReturnCode=1
SET @vchMsg='獎品添加失敗,參數傳入錯誤。'
ROLLBACK TRANSACTION
RETURN 1
END