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



CREATE proc [dbo].[proc_mobileOperator]
 	 @flag                               VARCHAR(50)    = NULL
	,@user                               VARCHAR(30)    = NULL
	,@mobileOperatorId                   VARCHAR(30)    = NULL
	,@countryId                     INT            = NULL
	,@operator                           VARCHAR(50)    = NULL
	,@mobileLen							 INT			= NULL
	,@prefix                             INT            = NULL
	,@sortBy                             VARCHAR(50)    = NULL
	,@sortOrder                          VARCHAR(5)     = NULL
	,@pageSize                           INT            = NULL
	,@pageNumber                         INT            = NULL


AS
SET NOCOUNT ON
SET XACT_ABORT ON
BEGIN TRY
	CREATE TABLE #msg(errorCode INT, msg VARCHAR(100), id INT)
	DECLARE
		 @sql			VARCHAR(MAX)
		,@oldValue		VARCHAR(MAX)
		,@newValue		VARCHAR(MAX)
		,@module		VARCHAR(10)
		,@tableAlias		VARCHAR(100)
		,@logIdentifier		VARCHAR(50)
		,@logParamMod		VARCHAR(100)
		,@logParamMain		VARCHAR(100)
		,@table			VARCHAR(MAX)
		,@select_field_list	VARCHAR(MAX)
		,@extra_field_list	VARCHAR(MAX)
		,@sql_filter		VARCHAR(MAX)
		,@modType		VARCHAR(6)
	SELECT
		 @logIdentifier = 'mobileOperatorId'
		,@logParamMain = 'mobileOperator'
		,@logParamMod = 'mobileOperatorMod'
		,@module = '20'
		,@tableAlias = 'Mobile Operator'
	IF @flag = 'i'
	BEGIN
		BEGIN TRANSACTION
			INSERT INTO mobileOperator (
				 countryId
				,operator
				,mobileLen
				,prefix
				,createdBy
				,createdDate
			)
			SELECT
				 @countryId
				,@operator
				,@mobileLen
				,@prefix
				,@user
				,GETDATE()
			
			SET @mobileOperatorId = SCOPE_IDENTITY()
			SET @modType = 'Insert'
			EXEC [dbo].proc_GetColumnToRow  @logParamMain, @logIdentifier, @mobileOperatorId , @newValue OUTPUT
			INSERT INTO #msg(errorCode, msg, id)
			EXEC proc_applicationLogs 'i', NULL, @modType, @tableAlias, @mobileOperatorId, @user, @oldValue, @newValue
			IF EXISTS (SELECT 'x' FROM #msg WHERE errorCode <> '0')
			BEGIN
				IF @@TRANCOUNT > 0
				ROLLBACK TRANSACTION
				EXEC proc_errorHandler 1, 'Failed to add new record.', @mobileOperatorId
				RETURN
			END
		IF @@TRANCOUNT > 0
		COMMIT TRANSACTION
		EXEC proc_errorHandler 0, 'Record has been added successfully.', @mobileOperatorId
	END
	ELSE IF @flag = 'a'
	BEGIN
		SELECT * FROM mobileOperator WITH(NOLOCK) WHERE mobileOperatorId = @mobileOperatorId
	END

	ELSE IF @flag = 'u'
	BEGIN
		BEGIN TRANSACTION
			UPDATE mobileOperator SET
				 operator = @operator
				,mobileLen = @mobileLen
				,prefix = @prefix
				,modifiedBy = @user
				,modifiedDate = GETDATE()
			WHERE mobileOperatorId = @mobileOperatorId
			EXEC [dbo].proc_GetColumnToRow  @logParamMain, @logIdentifier, @mobileOperatorId, @newValue OUTPUT
			INSERT INTO #msg(errorCode, msg, id)			EXEC proc_applicationLogs 'i', NULL, @modType, @tableAlias, @mobileOperatorId, @user, @oldValue, @newValue
			IF EXISTS (SELECT 'x' FROM #msg WHERE errorCode <> '0')
			BEGIN
				IF @@TRANCOUNT > 0
				ROLLBACK TRANSACTION
				EXEC proc_errorHandler 1, 'Failed to update record.', @mobileOperatorId
				RETURN
			END
		IF @@TRANCOUNT > 0
		COMMIT TRANSACTION
		EXEC proc_errorHandler 0, 'Record updated successfully.', @mobileOperatorId
	END
	ELSE IF @flag = 'd'
	BEGIN
		BEGIN TRANSACTION
			UPDATE mobileOperator SET
				 isDeleted = 'Y'
				,modifiedDate  = GETDATE()
				,modifiedBy = @user
			WHERE mobileOperatorId = @mobileOperatorId
			SET @modType = 'Delete'
			EXEC [dbo].proc_GetColumnToRow  @logParamMain, @logIdentifier,  @mobileOperatorId, @oldValue OUTPUT
			INSERT INTO #msg(errorCode, msg, id)
			EXEC proc_applicationLogs 'i', NULL, @modType, @tableAlias,  @mobileOperatorId, @user, @oldValue, @newValue
			IF EXISTS (SELECT 'x' FROM #msg WHERE errorCode <> '0')
			BEGIN
					IF @@TRANCOUNT > 0
					ROLLBACK TRANSACTION
					EXEC proc_errorHandler 1, 'Failed to delete record.', @mobileOperatorId
					RETURN
				END
			IF @@TRANCOUNT > 0
			COMMIT TRANSACTION
		EXEC proc_errorHandler 0, 'Record deleted successfully.', @mobileOperatorId
	END


	ELSE IF @flag IN ('s')
	BEGIN
		IF @sortBy IS NULL
			SET @sortBy = 'mobileOperatorId'
		IF @sortOrder IS NULL
			SET @sortOrder = 'ASC'
		SET @table = '(
				SELECT
					 main.mobileOperatorId
					,main.countryId
					,main.operator
					,main.mobileLen
					,main.prefix
					,main.createdBy
					,main.createdDate
					,main.isDeleted
				FROM mobileOperator main WITH(NOLOCK)
					WHERE main.countryId = ' + CAST(@countryId AS VARCHAR) + '
					) x'
		SET @sql_filter = ''
		SET @sql_filter = @sql_filter + ' AND ISNULL(isDeleted, '''') <> ''Y'''
		SET @select_field_list ='
			 mobileOperatorId
			,countryId
			,operator
			,mobileLen
			,prefix
			,createdBy
			,createdDate
			,isDeleted '
		EXEC dbo.proc_paging
			@table
			,@sql_filter
			,@select_field_list
			,@extra_field_list
			,@sortBy
			,@sortOrder
			,@pageSize
			,@pageNumber
	END
END TRY
BEGIN CATCH
     IF @@TRANCOUNT > 0
     ROLLBACK TRANSACTION
     DECLARE @errorMessage VARCHAR(MAX)
     SET @errorMessage = ERROR_MESSAGE()
     EXEC proc_errorHandler 1, @errorMessage, @mobileOperatorId
END CATCH


GO
