--Create the table which is checking a user playing in other server
 
USE RYLBillingDB_LOG

GO

CREATE TABLE dbo.[TblCurrentUser_Log] (
	[strClientid] [varchar] (20)  NULL ,
	[UID] [int] NULL ,
	[strIp] [varchar] (15)  NULL ,
	[tinyServerID] [tinyint] NULL ,
	[FirstLoginTime] [smalldatetime] NULL ,
	[LogoutTime] [smalldatetime] NULL 
) 

GO

USE youxiuser

GO 
 
CREATE TABLE dbo.[TblCurrentUser_Temp] (
	[strClientID] [varchar] (20)  NOT NULL ,
	[UID] [int] NOT NULL ,
	[strIP] [varchar] (15)  NOT NULL ,
	[tinyServerID] [tinyint] NOT NULL ,
	[FirstLoginTime] [smalldatetime] NOT NULL CONSTRAINT [DF_TblCurrentUserTemp_Firs] DEFAULT (getdate()),
) 

GO

CREATE TABLE dbo.[TblCurrentUser] (
	[strClientID] [varchar] (20)  NOT NULL ,
	[UID] [int] NOT NULL ,
	[strIP] [varchar] (15)  NOT NULL ,
	[tinyServerID] [tinyint] NOT NULL ,
	[FirstLoginTime] [smalldatetime] NOT NULL CONSTRAINT [DF_TblCurrentUser_Firs] DEFAULT (getdate()),
	CONSTRAINT [PK_TblCurrentUser] PRIMARY KEY  NONCLUSTERED 
	(
		[strClientID]
	) WITH  FILLFACTOR = 90   
) 

GO

CREATE VIEW dbo.UView_CurrentUser
AS
	SELECT * FROM TblCurrentUser_Temp
	UNION ALL
	SELECT * FROM TblCurrentUser
	
GO

CREATE PROC dbo.USPCheckBilling_Login @strClientID varchar(20),@UID int,@Check int,@ClientIP varchar(15),@ServerID Tinyint
AS
	SET NOCOUNT ON
	
	DECLARE @FLAG INT
	DECLARE @BillingType Char(2)
	DECLARE @PlayTime INT
	DECLARE @Crmindex INT
	
	SET @FLAG = 1 --ERROR
	SET @BillingType ='N'
	SET @PlayTime = 0
	SET @Crmindex = 0

	--CHECKING
	IF EXISTS (SELECT strClientID FROM UView_CurrentUser WHERE strClientID = @strClientID)
	BEGIN

		SELECT @FLAG,@BillingType,@PlayTime,@Crmindex
		
	END
	ELSE
	BEGIN		
		
		INSERT TblCurrentUser_Temp (strClientID,UID,strIP,tinyServerID)
		VALUES (@strClientID,@UID,@ClientIP,@ServerID)
		
		IF @@ROWCOUNT <> 1
		BEGIN
			SELECT @FLAG,@BillingType,@PlayTime,@Crmindex
		END
		ELSE
		BEGIN
				
			SET @FLAG = 0 --SUCCESS		
		
			SELECT @FLAG,@BillingType,@PlayTime,@Crmindex
		END
			
	END
	
GO


CREATE PROC dbo.USPCheckBilling_CharIDLogin @strClientID varchar(20),@UID int,@Check int,@ClientIP varchar(15),@ServerID Tinyint
AS
	SET NOCOUNT ON
	
	DECLARE @FLAG INT
	DECLARE @BillingType Char(2)
	DECLARE @PlayTime INT
	DECLARE @Crmindex INT
	
	SET @FLAG = 1 --ERROR
	SET @BillingType ='N'
	SET @PlayTime = 0
	SET @Crmindex = 0

	--CHECKING
	IF EXISTS (SELECT strClientID FROM UView_CurrentUser WHERE strClientID = @strClientID)
	BEGIN

		SELECT @FLAG,@BillingType,@PlayTime,@Crmindex
		
	END
	ELSE
	BEGIN		
		
		INSERT TblCurrentUser (strClientID,UID,strIP,tinyServerID)
		VALUES (@strClientID,@UID,@ClientIP,@ServerID)
		
		IF @@ROWCOUNT <> 1
		BEGIN
			SELECT @FLAG,@BillingType,@PlayTime,@Crmindex
		END
		ELSE
		BEGIN
				
			SET @FLAG = 0 --SUCCESS		
		
			SELECT @FLAG,@BillingType,@PlayTime,@Crmindex
		END
			
	END
	
GO

CREATE PROC dbo.USPCheckBilling_LogOut @ClientID VARCHAR(20)
AS
	SET NOCOUNT ON
	
	DECLARE @FLAG INT
		
	IF EXISTS (SELECT  strClientID FROM TblCurrentUser_Temp WHERE strClientID = @ClientID)
	BEGIN
	
		DELETE TblCurrentUser_Temp WHERE strClientID = @ClientID
		
		IF @@ROWCOUNT <> 1
		BEGIN
			SET @FLAG = 1 --ERROR
		END
		
		SET @FLAG = 0 --SUCCESS
		
		SELECT @FLAG
					
	END
	ELSE
	BEGIN
	
		IF EXISTS (SELECT  strClientID FROM TblCurrentUser WHERE strClientID = @ClientID)
		BEGIN
			-- LOG WRITE
			INSERT RYLBillingDB_LOG.dbo.TblCurrentUser_LOG (strClientid,UID,strIp,tinyServerID,FirstLoginTime,LogoutTime)
			SELECT strClientid,UID,strIp,tinyServerID,FirstLoginTime,GETDATE() 
			FROM TblCurrentUser WHERE strClientID = @ClientID
			
			IF @@ROWCOUNT <> 1
			BEGIN
				SET @FLAG = 2 --ERROR
				SELECT @FLAG
				RETURN			
			END
			
			DELETE TblCurrentUser WHERE strClientID = @ClientID
			
			IF @@ROWCOUNT <> 1
			BEGIN
				SET @FLAG = 3 --ERROR
				SELECT @FLAG
				RETURN
			END
			
			SET @FLAG = 0 --SUCCESS			
			SELECT @FLAG
		
		END
		ELSE
		BEGIN
			SET @FLAG = 4
			SELECT @FLAG
		END
	END


	
	
	


	