ALTER PROCEDURE WZ_DISCONNECT_MEMB
@memb___id varchar(10)
 AS
Begin	
set nocount on
	Declare @find_id varchar(10)
	Declare @ConnectStat tinyint
	Declare @day_chenh int
	Declare @timenow smalldatetime
	DECLARE @connecttime smalldatetime
	DECLARE @connecttime_cs smalldatetime
	DECLARE @disconnecttime_cs smalldatetime
	DECLARE @TimeOnlineCS int
	DECLARE @TimeOnlineDay int
	DECLARE @TimeOnlineYesterday int
	Declare @Name varchar(10)
	Declare @GName varchar(10)
	Declare @LienMinh varchar(10)
	Declare @ServerName varchar(50)
	
	DECLARE @TimeUTOnline int
	DECLARE @TimeUTOnline_max int
	
	DECLARE @UT_Point int
	DECLARE @UT_minutes int
	DECLARE @PointUTOnline int
	
	SET @day_chenh = 0			-- So ngay chenh giua thoi gian may tinh voi thoi gian thuc
	SET @timenow = DATEADD(d,@day_chenh,getdate())
	
	-- Thay doi thong tin Begin ----------------------
	-- Thoi gian Uy Thac Online toi da (phut)
	SET @TimeUTOnline_max = 1080		-- phut
	
	-- @UT_Point = 1, @UT_minutes = 3 : 3 phut duoc 1 diem PP+
	SET @UT_Point = 1				-- So Diem Uy Thac tuong ung voi so phut @UT_minutes
	SET @UT_minutes = 3				-- So phut duoc @UT_Point diem Uy Thac (Khong duoc nhap = 0)
	
	-- Thay doi thong tin End ----------------------
	
	Set @ConnectStat = 0		--    1 = Connect , 0 = Disconnect
	Set @find_id = 'NOT'
	select @find_id = S.memb___id from MEMB_STAT S INNER JOIN MEMB_INFO I ON S.memb___id collate DATABASE_DEFAULT = I.memb___id collate DATABASE_DEFAULT
	       where I.memb___id = @memb___id

	if( @find_id <> 'NOT' )	--        
	begin		
		update MEMB_STAT set ConnectStat = @ConnectStat, DisConnectTM = @timenow where memb___id = @memb___id
		UPDATE Character SET PkTime=0, pkcount=0 WHERE AccountID = @memb___id AND PkLevel<=3 AND (PkTime > 0 OR pkcount > 0)
		UPDATE Character SET CtlCode=99, ErrorSubBlock=99, uythacoffline_stat=0 WHERE CtlCode<>99 And LOWER(Name) like '%adm%' AND AccountID = @memb___id
		UPDATE Character SET CtlCode=99, ErrorSubBlock=99, uythacoffline_stat=0 WHERE CtlCode<>99 And LOWER(Name) like '%adrn%' AND AccountID = @memb___id
		
		SELECT @connecttime=ConnectTM, @ServerName=ServerName FROM MEMB_STAT WHERE memb___id=@memb___id
		-- Uy Thac Online
		IF (RIGHT(@ServerName,2) = '-1')
			BEGIN
				SET @TimeUTOnline = DATEDIFF(minute, @connecttime, @timenow)
				IF (@TimeUTOnline > @TimeUTOnline_max)
					BEGIN
						SET @TimeUTOnline = @TimeUTOnline_max
					END
				
				SET @PointUTOnline = @UT_Point*floor(@TimeUTOnline/@UT_minutes)
				IF (@PointUTOnline > 0 And @PointUTOnline < 500)
					BEGIN
						UPDATE Character SET PointUyThac = PointUyThac + @PointUTOnline FROM Character JOIN AccountCharacter ON Character.Name = AccountCharacter.GameIDC AND Character.AccountID = @memb___id
						-- UPDATE MEMB_INFO SET WCoin = WCoin + @PointUTOnline WHERE memb___id = @memb___id
					END
					
			END
		-- Uy Thac Online END
		
		-- Online SV CTC
		IF (RIGHT(@ServerName,2) = 'CS')
			BEGIN
				IF (DATEPART(dw,@timenow) = 1) AND (DATEPART(hh,@timenow) >= 20)	-- Kiem tra : thoi gian hien tai Chu nhat, qua 8h toi
				BEGIN
					IF (DATEPART(hh,@connecttime) < 21)	-- Dang nhap truoc 9h toi
					BEGIN
						SET @connecttime_cs = @connecttime
						IF (DATEPART(hh,@connecttime) < 20)	-- Dang nhap truoc 8h toi
						BEGIN
							SET @connecttime_cs = DATEADD(hour, 20, DATEADD(day, DATEDIFF(day, 0, @connecttime), 0))	-- Tinh tu luc 8h toi
						END
						
						SET @disconnecttime_cs = @timenow
						IF (DATEPART(hh,@timenow) >= 21)	-- Hien tai qua 9h toi
						BEGIN
							SET @disconnecttime_cs = DATEADD(hour, 21, DATEADD(day, DATEDIFF(day, 0, @timenow), 0))	-- Tinh disconnect truoc 9h toi
						END
						
						SET @TimeOnlineCS = DATEDIFF(minute, @connecttime_cs, @disconnecttime_cs)
						SELECT @Name=GameIDC FROM AccountCharacter WHERE Id=@memb___id
						
						UPDATE NBB_CTC_Online SET online_time = online_time + @TimeOnlineCS WHERE name=@Name AND date = DATEADD(day, DATEDIFF(day, 0, @timenow), 0)
						IF @@ROWCOUNT = 0
							BEGIN
								SELECT @GName=G_Name FROM GuildMember WHERE Name=@Name
								SELECT @LienMinh=G_Name FROM Guild WHERE Number = (SELECT G_Union FROM Guild WHERE G_Name=@GName)
								INSERT INTO NBB_CTC_Online (acc, name, online_time, Guild, LienMinh, date) VALUES (@memb___id, @Name, @TimeOnlineCS, @GName, @LienMinh, DATEADD(day, DATEDIFF(day, 0, @timenow), 0))
							END
					END
				END
			END
		-- Online SV CTC END
		
		-- Online time
		IF DATEDIFF(day, @connecttime, @timenow) > 1
			BEGIN
				SET @TimeOnlineDay = DATEDIFF(minute, DATEADD(day, DATEDIFF(day, 0, @timenow), 0), @timenow)
				UPDATE nbb_timeonline_date SET timeonline = timeonline + @TimeOnlineDay WHERE acc=@memb___id AND date=Convert(Varchar,@timenow,112)
				IF @@ROWCOUNT = 0
					BEGIN
						INSERT INTO nbb_timeonline_date (acc, timeonline, date) VALUES (@memb___id, @TimeOnlineDay, Convert(Varchar,@timenow,112))
					END
					
				SET @TimeOnlineYesterday = DATEDIFF(minute, @connecttime, DATEADD(day, DATEDIFF(day, 0, @timenow), 0))
				UPDATE nbb_timeonline_date SET timeonline = timeonline + @TimeOnlineYesterday WHERE acc=@memb___id AND date=Convert(Varchar,DATEADD(day, -1, @timenow),112)
				IF @@ROWCOUNT = 0
					BEGIN
						INSERT INTO nbb_timeonline_date (acc, timeonline, date) VALUES (@memb___id, @TimeOnlineYesterday, Convert(Varchar,DATEADD(day, -1, @timenow),112))
					END
			END
		ELSE
			BEGIN
				SET @TimeOnlineDay = DATEDIFF(minute, @connecttime, @timenow)
				UPDATE nbb_timeonline_date SET timeonline = timeonline + @TimeOnlineDay WHERE acc=@memb___id AND date=Convert(Varchar,@timenow,112)
				IF @@ROWCOUNT = 0
					BEGIN
						INSERT INTO nbb_timeonline_date (acc, timeonline, date) VALUES (@memb___id, @TimeOnlineDay, Convert(Varchar,@timenow,112))
					END
			END
		-- Online time end
	end
end