jack89son jack89son
关注数: 14 粉丝数: 43 发帖数: 4,215 关注贴吧数: 13
保存点东西 DECLARE @CurrentTime DATETIME, @CurrentMonth DATETIME, @CalculateMonth DATETIME, @BalanceAtLastTime INT = 0, @RowCount INT, @RetiredBy VARCHAR(20), @OnLineDateTime DATETIME, @IsUpdate BIT, @PointExpireMonth DATETIME, @WynnID DATETIME SET @CurrentTime = GETDATE() SELECT @OnLineDateTime = ItemName FROM [GlobalSetting] WHERE ItemCode = 'GLOBALSETTING_ONE_BUCKET_ONLINE_DATETIME' SET @CurrentMonth = DATEADD(MM,DATEDIFF(MM,0,@CurrentTime),0) -- If current month is the same as go live month,we needn't to run this sp, -- because the go live month will be calculate at the beginning of next month. IF @CurrentMonth = DATEADD(MM,DATEDIFF(MM,0,@OnLineDateTime),0) RETURN -- Calculate the begining of current month. SET @CalculateMonth = DATEADD(MM,-1,@CurrentMonth) SELECT @PointExpireMonth = [dbo].[GCM_fn_PointAging_GetPointEarnedMonth](@CalculateMonth) IF @CalculateMonth = DATEADD(MM,DATEDIFF(MM,0,@OnLineDateTime),0) BEGIN SET @IsUpdate = 1 SET @CalculateMonth = @OnLineDateTime SELECT @RowCount = COUNT(1) FROM PointExpireStatistic WHERE ModifiedOn > @OnLineDateTime END ELSE BEGIN SET @IsUpdate = 0 SELECT @RowCount = COUNT(1) FROM PointExpireStatistic WHERE PointEarnedMonth = @CalculateMonth END IF @RowCount > 0 RETURN -- Create temp table for void points earned which earned month is not same with void month. IF Object_id('tempdb..#PointsEarnedVoid') <> 0 BEGIN DROP TABLE #PointsEarnedVoid END CREATE TABLE #PointsEarnedVoid(WynnID NUMERIC(8,0), EarnedMonth DATETIME, PointsEarnedVoid INT) -- Create temp table for return points consume which return month is not same with consume month. IF Object_id('tempdb..#PointsReturn') <> 0 BEGIN DROP TABLE #PointsReturn END CREATE TABLE #PointsReturn(WynnID NUMERIC(8,0), PointsReturn INT) -- Create temp table for remain points earned void. IF Object_id('tempdb..#PointRemainNegativeAmount') <> 0 BEGIN DROP TABLE #PointRemainNegativeAmount END CREATE TABLE #PointRemainNegativeAmount(WynnID NUMERIC(8,0), NegativeAmount NUMERIC(18, 0)) -- Create temp table for normal consume in current month. IF Object_id('tempdb..#PointsConsume') <> 0 BEGIN DROP TABLE #PointsConsume END CREATE TABLE #PointsConsume(WynnID NUMERIC(8,0), PointsConsume NUMERIC(18, 0)) -- Create temp table for points normal earn in current month. IF Object_id('tempdb..#PointsEarn') <> 0 BEGIN DROP TABLE #PointsEarn END CREATE TABLE #PointsEarn(WynnID NUMERIC(8,0), PointsEarn NUMERIC(18, 0)) -- Create temp table for recording all the critical expire amount greater than consume amount. IF Object_id('tempdb..#TotalExpireAmount') <> 0 BEGIN DROP TABLE #TotalExpireAmount END CREATE TABLE #TotalExpireAmount(WynnID NUMERIC(8,0), PointEarnedMonth DATETIME, PointsConsume INT, ExpireAmount INT) -- Create temp table for recording the first record that critical expire amount greater than consume amount. IF Object_id('tempdb..#CriticalExpireAmount') <> 0 BEGIN DROP TABLE #CriticalExpireAmount END CREATE TABLE #CriticalExpireAmount(WynnID NUMERIC(8,0), PointEarnedMonth DATETIME, PointsConsume INT, ExpireAmount INT) -- Create temp table for recording the patron who have negative balance. IF Object_id('tempdb..#PatronWithNegativeBalance') <> 0 BEGIN DROP TABLE #PatronWithNegativeBalance END CREATE TABLE #PatronWithNegativeBalance(WynnID NUMERIC(8,0), PointAvailable INT) -- Store the points to be expired in current month. IF Object_id('tempdb..#PointsExpireOfCurrentMonth') <> 0 BEGIN DROP TABLE #PointsExpireOfCurrentMonth END CREATE TABLE #PointsExpireOfCurrentMonth(WynnID NUMERIC(8,0), PointExpireAmount INT) -- Get the points earned that void month is not same with earned month. INSERT INTO #PointsEarnedVoid SELECT * FROM GCM_fn_PointAging_GetPointEarnedVoid(@WynnID,@CalculateMonth,@CurrentMonth) -- Get the points return that void month is not same with issued month. INSERT INTO #PointsReturn SELECT * FROM GCM_fn_PointAging_GetPointReturn(@WynnID,@CalculateMonth,@CurrentMonth) -- Get the point consumed in current month. INSERT INTO #PointsConsume SELECT * FROM GCM_fn_PointAging_GetPointConsume(@WynnID,@CalculateMonth,@CurrentMonth) -- Calculate the points earned of current month. INSERT INTO #PointsEarn SELECT WynnID,SUM(PointsEarn) FROM ( SELECT WynnID,PointsEarn FROM GCM_fn_PointAging_GetPointEarn(@WynnID,@CalculateMonth,@CurrentMonth) UNION ALL SELECT WynnID,0 FROM #PointsConsume ) PointsEarn GROUP BY WynnID DECLARE @PointsEarnedVoid INT = 0, -- one player's points earned voided in current month. @RemainEarnedVoid INT = 0, @TotalRemainEarnedVoid INT = 0, @PointsEarnedMonth DATETIME, -- the month of points earned that voided in current month. @PointsReturn INT = 0, -- the points return in current month and issued in the current month before. @PointsConsume NUMERIC(18, 0) = 0, -- the points consumed in current month. @RemainConsume NUMERIC(18, 0) = 0, @PointsEarn NUMERIC(18, 0) = 0, -- the poins earned in current month. @RemainEarn NUMERIC(18, 0) = 0, -- the points to be expired of current month. @PointsExpireId BIGINT, @ExpireAmountDeductedMonth DATETIME, @TotalExpireAmount NUMERIC(18, 0) , @ExpireAmountAddedMonth DATETIME, @NegativeBalance INT = 0, @ActualPointsReturn INT = 0 DECLARE Cursor_PointsSpecialVoid CURSOR SCROLL DYNAMIC FOR SELECT DISTINCT WynnID FROM #PointsEarnedVoid UNION SELECT DISTINCT WynnID FROM #PointsReturn --begin cursor for sepical void such as void earn and void issue which the month of create time is not same as the month of modify time. OPEN Cursor_PointsSpecialVoid FETCH NEXT FROM Cursor_PointsSpecialVoid INTO @WynnID WHILE(@@FETCH_STATUS=0) BEGIN -- Get points earned voided that the void time is not same with earned time DECLARE Cursor_PointsEarnedVoid CURSOR SCROLL DYNAMIC FOR SELECT EarnedMonth,PointsEarnedVoid FROM #PointsEarnedVoid WHERE WynnID = @WynnID OPEN Cursor_PointsEarnedVoid FETCH NEXT FROM Cursor_PointsEarnedVoid INTO @PointsEarnedMonth,@PointsEarnedVoid WHILE(@@FETCH_STATUS=0) BEGIN UPDATE PointExpireStatistic SET PointEarnedAmount = PointEarnedAmount - @PointsEarnedVoid, ModifiedOn = @CurrentTime WHERE PointEarnedMonth = @PointsEarnedMonth AND WynnID = @WynnID; WITH FiltedExpireAmount AS ( SELECT PointEarnedMonth,PointExpireAmount FROM PointExpireStatistic WHERE WynnId = @WynnID AND PointEarnedMonth >= @PointsEarnedMonth AND IsExpire = 0 AND PointExpireAmount > 0 ) SELECT TOP 1 @ExpireAmountDeductedMonth = FEA1.PointEarnedMonth , @TotalExpireAmount = SUM(FEA2.PointExpireAmount) FROM FiltedExpireAmount FEA1 INNER JOIN FiltedExpireAmount FEA2 ON FEA1.PointEarnedMonth >= FEA2.PointEarnedMonth GROUP BY FEA1.PointEarnedMonth HAVING SUM(FEA2.PointExpireAmount) >= @PointsEarnedVoid ORDER BY FEA1.PointEarnedMonth IF @TotalExpireAmount IS NOT NULL AND @TotalExpireAmount <> 0 BEGIN SET @TotalExpireAmount = @TotalExpireAmount - @PointsEarnedVoid UPDATE PointExpireStatistic SET PointExpireAmount = CASE WHEN PointEarnedMonth < @ExpireAmountDeductedMonth THEN 0 ELSE @TotalExpireAmount END, ModifiedOn = @CurrentTime WHERE WynnID = @WynnID AND IsExpire = 0 AND PointEarnedMonth BETWEEN @PointsEarnedMonth AND @ExpireAmountDeductedMonth SET @RemainEarnedVoid = 0 END ELSE BEGIN SELECT @TotalExpireAmount = SUM(PointExpireAmount) FROM PointExpireStatistic WHERE WynnId = @WynnID AND PointEarnedMonth >= @PointsEarnedMonth AND IsExpire = 0 AND PointExpireAmount > 0 IF @TotalExpireAmount IS NOT NULL UPDATE PointExpireStatistic SET PointExpireAmount = 0, ModifiedOn = @CurrentTime WHERE WynnID = @WynnID AND PointEarnedMonth >= @PointsEarnedMonth AND IsExpire = 0 SET @RemainEarnedVoid = ABS(ISNULL(@TotalExpireAmount,0) - @PointsEarnedVoid) END SET @TotalRemainEarnedVoid = @TotalRemainEarnedVoid + @RemainEarnedVoid SET @TotalExpireAmount = NULL FETCH NEXT FROM Cursor_PointsEarnedVoid INTO @PointsEarnedMonth,@PointsEarnedVoid END CLOSE Cursor_PointsEarnedVoid DEALLOCATE Cursor_PointsEarnedVoid -- Get points return that the void time is not same with issued time SELECT @PointsReturn = ISNULL(PointsReturn,0) FROM #PointsReturn WHERE WynnID = @WynnID; -- Get over consume last month. SELECT TOP 1 @BalanceAtLastTime = OverConsumeAmount FROM PointExpireStatistic WHERE WynnID = @WynnID ORDER BY PointEarnedMonth DESC SET @NegativeBalance = @BalanceAtLastTime - @TotalRemainEarnedVoid IF @NegativeBalance < 0 AND @PointsReturn IS NOT NULL AND @PointsReturn > 0 BEGIN SET @ActualPointsReturn = @PointsReturn - ABS(@NegativeBalance) SET @NegativeBalance = @NegativeBalance + @PointsReturn IF @NegativeBalance > 0 SET @NegativeBalance = 0 END ELSE SET @ActualPointsReturn = @PointsReturn
首页 1 2 下一页