USE [FastMoneyPro_Remit]
GO
/****** Object:  StoredProcedure [dbo].[proc_ApiLogs]    Script Date: 7/4/2019 11:35:48 AM ******/
DROP PROCEDURE [dbo].[proc_ApiLogs]
GO
/****** Object:  StoredProcedure [dbo].[proc_ApiLogs]    Script Date: 7/4/2019 11:35:48 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[proc_ApiLogs](
	 @flag			 VARCHAR(10)	= 	NULL
	,@user  		 VARCHAR(30)	= 	NULL
	,@REQUESTEDBY	 VARCHAR(100)	=	NULL
	,@rowId			 INT			= 	NULL
	,@pageSize		 INT			=	NULL
	,@pageNumber	 INT			=	NULL
	,@sortBy		VARCHAR(50)		=	NULL
	,@sortOrder		VARCHAR(50)		=	NULL	
)AS
SET NOCOUNT ON
SET	XACT_ABORT ON
BEGIN
	DECLARE
		 @table				VARCHAR(MAX)
		,@select_field_list	VARCHAR(MAX)
		,@extra_field_list	VARCHAR(MAX)
		,@sql_filter		VARCHAR(MAX)
		,@VprocessId		VARCHAR(40)
	IF @flag='s'
	BEGIN		
		SET @sortBy='rowId'
		SET @sortOrder='DESC'

		
		SELECT TOP 1 @VprocessId = processId FROM Application_Log.dbo.tblThirdParty_ApiDetailLog(NOLOCK) 
		WHERE methodName='global/sendTxn' AND logBy = @REQUESTEDBY
		ORDER BY ROWID DESC

		SET @table='
		(	
			SELECT rowId
				  ,providerName
				  ,methodName
				  ,controlNo				 
				  ,requestedBy= '''+@REQUESTEDBY+'''
				  ,requestedDate
				  ,responseDate
				  ,errorCode
				  ,errorMessage
				  ,processId
				  ,requestedBy AS CREATEDBY
			  FROM Application_Log.DBO.vwTpApilogs (NOLOCK)
			  WHERE 1=1
		)x'
						
		SET @sql_filter = ''
					
		IF @VprocessId IS NOT NULL  
			SET @sql_filter=@sql_filter + ' AND processId = ''' +@VprocessId+''''	
		ELSE IF @REQUESTEDBY IS NOT NULL  
			SET @sql_filter=@sql_filter + ' AND CREATEDBY = ''' +@REQUESTEDBY+''''	
		ELSE
			SET @sql_filter=@sql_filter + ' AND 1=2 '			
			
		SET @select_field_list = '
								 rowId
								,providerName
								,methodName
								,controlNo								
								,requestedBy
								,requestedDate
								,responseDate
								,errorCode
								,errorMessage
								,processId
								,CREATEDBY
							'
							
		EXEC dbo.proc_paging
		 @table
		,@sql_filter
		,@select_field_list
		,@extra_field_list
		,@sortBy
		,@sortOrder
		,@pageSize
		,@pageNumber
		
		
	END
	IF @flag='a'
	BEGIN
				SELECT rowId
					  ,providerName
					  ,methodName
					  ,controlNo
					  ,requestXml
					  ,responseXml
					  ,requestedBy
					  ,requestedDate
					  ,responseDate
					  ,errorCode
					  ,errorMessage
			  FROM Application_Log.DBO.vwTpApilogs (NOLOCK)
			  WHERE rowId=@rowId
	END
END

GO
