USE [FastMoneyPro_Remit]
GO
/****** Object:  StoredProcedure [dbo].[ProcMobileConfig]    Script Date: 11/27/2023 1:22:14 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO




ALTER PROC [dbo].[ProcMobileConfig]
(
	@Flag VARCHAR(30)
	,@User VARCHAR(80) = NULL
	,@GroupName VARCHAR(100) = NULL
	,@GroupDetail VARCHAR(150) = NULL
	,@BroadcastTypeId INT = NULL
	,@CustomerGroupIds VARCHAR(200) = NULL
	,@Title	NVARCHAR(100) = NULL
	,@Body NVARCHAR(MAX) = NULL
	,@ImageURL VARCHAR(150) = NULL
	,@ScheduleDate VARCHAR(30) = NULL
	,@SortBy VARCHAR(50)  = NULL    
	,@SortOrder VARCHAR(5)  = NULL    
	,@PageSize INT    = NULL    
	,@PageNumber INT    = NULL   
	,@RowId INT = NULL
	,@Xml XML = NULL
	,@CustomerId BIGINT = NULL
	,@isRepush VARCHAR(1) = NULL
	,@notificationType VARCHAR(50) = NULL
	,@msgType VARCHAR(25) = NULL
	,@customerType VARCHAR(25) = NULL
	,@nativeCountry VARCHAR(3) = NULL
)
AS
;
------------------------------------------------
	-- #101 - Mobile Changes
	-- Delete customer from group
	-- #1048 - BROADCAST FOR SINGLE CUSTOMER , changes in @flag = 'I-BROADCAST' , 'U-BROADCAST' , 'RE-PUSH'
	-- #9490 - puch -notification for individual customers
	-- #20484- broadcast by country , @flag = groupForCountry
------------------------------------------------
SET NOCOUNT ON;
SET XACT_ABORT ON;
BEGIN
	DECLARE @SelectFieldList VARCHAR(MAX)    
		,@ExtraFieldList  VARCHAR(MAX)    
		,@Table             VARCHAR(MAX)    
		,@SqlFilter        VARCHAR(MAX)   
	DECLARE @customerIdForPush VARCHAR(50)	, @deviceId VARCHAR(MAX) , @entrytype varchar(20)

	IF @BroadCastTypeId = '0'
		SET @BroadCastTypeId = NULL
		 
	IF @Flag = 'I'
	BEGIN
		SELECT 'A'
	END	
	ELSE IF @Flag = 'I-GROUP'
	BEGIN
		INSERT INTO CustomerGroup(GroupName, GroupDetail, BroadcastTypeId, CreatedBy, CreatedDate, IsActive)
		SELECT @GroupName, @GroupDetail, @BroadcastTypeId, @User, GETDATE(), 1 

		SELECT 0, 'Success', NULL
	END
	ELSE IF @Flag = 'S-GROUP-SINGLE'
	BEGIN
		SELECT *
		FROM CustomerGroup (NOLOCK)
		WHERE RowId = @RowId 
	END
	ELSE IF @Flag = 'U-GROUP'
	BEGIN
		UPDATE CustomerGroup
		SET GroupName = @GroupName, GroupDetail = @GroupDetail, BroadcastTypeId = @BroadcastTypeId, ModifiedBy = @User, ModifiedDate = GETDATE()
		WHERE RowId = @RowId 

		SELECT 0, 'Success', NULL
	END
	ELSE IF @Flag = 'DDL-GROUP'
	BEGIN
		SELECT GroupName, RowId
		FROM CustomerGroup (NOLOCK)
		WHERE IsActive = 1
	END
	ELSE IF @Flag = 'DDL-STATIC'
	BEGIN
		SELECT '0' [value], 'Select' [text]
		UNION ALL
		SELECT valueId [value], detailTitle [text]
		FROM staticDataValue (NOLOCK)
		WHERE ISNULL(IsActive, 'Y') = 'Y'
		AND ISNULL(IS_DELETE, 'N') = 'N'
		AND typeId = 8105
	END
	ELSE IF @Flag = 'DELETE'
	BEGIN
		SELECT @BroadcastTypeId = BroadcastTypeId
		FROM BroadCastNotification (NOLOCK)
		WHERE RowId = @RowId
		UPDATE BroadCastNotification
		SET IsActive = CASE WHEN IsActive = 0 THEN 1 ELSE 0 END, ModifiedBy = @User, ModifiedDate = GETDATE()
		WHERE RowId = @RowId

		INSERT INTO BroadCastNotificationLog(BroadCastId,CreatedBy,CreatedDate, ResponseMessage)
		values (@RowId, @user , GETDATE() , @BroadcastTypeId )
		SELECT 0, 'Success', NULL
	END
	ELSE IF @Flag = 'DELETE-GROUP'
	BEGIN
		UPDATE CustomerGroup
		SET IsActive = CASE WHEN IsActive = 0 THEN 1 ELSE 0 END, ModifiedBy = @User, ModifiedDate = GETDATE()
		WHERE RowId = @RowId

		SELECT 0, 'Success', NULL
	END
	ELSE IF @Flag = 'I-BROADCAST'
	BEGIN
		IF @CustomerGroupIds IS NULL 
		BEGIN
			SET @CustomerGroupIds = '0'
		END
		INSERT INTO BroadCastNotification(BroadcastTypeId, CustomerGroupIds, Title, Body, ImageURL, IsSent, CreatedBy
							, CreatedDate, ScheduleDate, IsActive, customerId, notificationType, messageType)
		SELECT @BroadcastTypeId, @CustomerGroupIds, @Title, @Body, @ImageURL, 0, @User
							, GETDATE(), @ScheduleDate, 1 , @CustomerId, @notificationType, @msgType

		SET @RowId = @@IDENTITY

		IF @CustomerId IS NOT NULL
			BEGIN
			 INSERT INTO pushNotificationHistroy(customerId,body,title,createDate,imageURL,sentId,Type,isReservation,isRead,isSend,category,url, isClickable)
			 select @customerid, @Body,@Title,getdate(),'',@RowId,0,0,0,0,@msgType,@ImageURL,IIF(@ImageURL IS NULL,'N','Y')
			 END			 
			ELSE
			 BEGIN
			 INSERT INTO pushNotificationHistroy(customerId,body,title,createDate,imageURL,sentId,Type,isReservation,isRead,isSend,category,url, isClickable)
			  select distinct customerid, @Body,@Title,getdate(),'',@RowId,0,0,0,0,@msgType,@ImageURL,IIF(@ImageURL IS NULL,'N','Y')
			  FROM  CustomerGroupDetail WHERE GroupId=@CustomerGroupIds
			 END

	--INSERT INTO pushNotificationHistroy(customerId,body,title,createDate,imageURL,sentId,Type,isReservation,isRead,isSend,category,url, isClickable)
	--select ISNULL(@customerid,@CustomerGroupIds),@Body,@Title,getdate(),'',@RowId,0,0,0,0,@msgType,@ImageURL,IIF(@ImageURL IS NULL,'N','Y')


		SELECT 0, 'Success', @RowId
	END
	ELSE IF @Flag = 'U-BROADCAST'
	BEGIN
	IF @CustomerGroupIds IS NULL 
		BEGIN
			SET @CustomerGroupIds = '0'
		END
		UPDATE BroadCastNotification
		SET BroadcastTypeId = @BroadcastTypeId, Title = @Title, Body = @Body, ImageURL = @ImageURL 
				, ModifiedBy = @User, ModifiedDate = GETDATE(), notificationType = @notificationType
				, messageType = @msgType
		WHERE RowId = @RowId  

		SELECT 0, 'Success', NULL
	END
	ELSE IF @Flag = 'U-B-GROUP'
	BEGIN
		UPDATE BroadCastNotification
		SET CustomerGroupIds = @CustomerGroupIds, ModifiedBy = @User, ModifiedDate = GETDATE()
		WHERE RowId = @RowId  

		SELECT 0, 'Success', NULL
	END
	ELSE IF @Flag = 'S-BROADCAST-SINGLE'
	BEGIN
		SELECT *
		FROM BroadCastNotification (NOLOCK)
		WHERE RowId = @RowId 
	END
	ELSE IF @Flag = 'S-BROADCAST'
	BEGIN
		IF @SortBy IS NULL
		   SET @SortBy = 'CreatedDate'
		IF @SortOrder IS NULL
		   SET @SortOrder = 'DESC'

		 SET @table = '(
						SELECT BN.RowId
							,customer = CASE WHEN bn.customerid is not null then cm.fullName else cg.GroupName END
							,BN.Title
							,BN.ImageURL
							,IsSent = CASE WHEN IsSent = 0 THEN ''No'' WHEN IsSent = 1 THEN ''Yes'' ELSE ''Failed'' END
							,BN.CreatedDate
							,BN.ScheduleDate
							,BN.CreatedBy
							,IsActive = CASE WHEN BN.IsActive = 0 THEN ''No'' ELSE ''Yes'' END
							,BroadCastType = SV.DetailTitle
							,BN.CustomerGroupIds
							,BN.BroadcastTypeId
					FROM BroadCastNotification BN (NOLOCK)
					INNER JOIN dbo.StaticDataValue SV (NOLOCK) ON BN.BroadcastTypeId = SV.ValueId
					LEFT JOIN dbo.CustomerGroup CG(NOLOCK) ON cg.RowId = BN.customerGroupIds
					LEFT JOIN dbo.CustomerMaster CM (NOLOCK) ON cm.customerId = ISNULL(BN.customerId,'''')
					WHERE 1 = 1 AND BN.isActive = 1   AND ISNULL(BN.entryType,'''') = ''''
				)x'
	
		SET @SqlFilter = ''
		
		IF @BroadCastTypeId IS NOT NULL
			SET @SqlFilter += ' AND X.BroadcastTypeId = ''' + CAST(@BroadcastTypeId AS VARCHAR) + ''''

		IF @Title IS NOT NULL
			SET @SqlFilter += ' AND X.Title LIKE ''' + @Title + '%'''
		
		
		SET @SelectFieldList ='RowId, Title, ImageURL, IsSent, CreatedBy, CreatedDate, ScheduleDate, IsActive
							,BroadCastType, CustomerGroupIds, customer'

		EXEC dbo.proc_paging
                @table
               ,@SqlFilter
               ,@SelectFieldList
               ,@ExtraFieldList
               ,@sortBy
               ,@sortOrder
               ,@pageSize
               ,@pageNumber
	END
	ELSE IF @Flag = 'S-GROUP'
	BEGIN
		IF @SortBy IS NULL
		   SET @SortBy = 'CreatedDate'
		IF @SortOrder IS NULL
		   SET @SortOrder = 'DESC'

		 SET @table = '(
						SELECT CG.RowId
							,CG.GroupName
							,CG.GroupDetail
							,CG.CreatedDate
							,CG.CreatedBy
							,IsActive = CASE WHEN CG.IsActive = 0 THEN ''No'' ELSE ''Yes'' END
							,BroadCastType = SV.DetailTitle
							,BroadcastTypeId
					FROM CustomerGroup CG (NOLOCK)
					INNER JOIN dbo.StaticDataValue SV (NOLOCK) ON CG.BroadcastTypeId = SV.ValueId
					WHERE 1 = 1
				)x'
	
		SET @SqlFilter = ''
		
		IF @BroadCastTypeId IS NOT NULL
			SET @SqlFilter += ' AND X.BroadcastTypeId = ''' + CAST(@BroadcastTypeId AS VARCHAR) + ''''

		IF @GroupName IS NOT NULL
			SET @SqlFilter += ' AND X.GroupName LIKE ''' + @GroupName + '%'''
		
		SET @SelectFieldList ='RowId, GroupName, GroupDetail, CreatedDate, CreatedBy, IsActive, BroadCastType'

		EXEC dbo.proc_paging
                @table
               ,@SqlFilter
               ,@SelectFieldList
               ,@ExtraFieldList
               ,@sortBy
               ,@sortOrder
               ,@pageSize
               ,@pageNumber
	END
	ELSE IF @Flag = 'GROUP-LIST'
	BEGIN
		IF @RowId IS NULL
		BEGIN
			SELECT CG.GroupName, CG.RowId, IsSelected = 'N'
			FROM CustomerGroup CG(NOLOCK)
			WHERE CG.IsActive = 1
			AND CG.BroadcastTypeId = @BroadcastTypeId

			RETURN;
		END
		IF @BroadcastTypeId IS NULL
			SELECT @BroadcastTypeId = BroadcastTypeId FROM BroadCastNotification(NOLOCK) WHERE RowId = @RowId

		SELECT B.RowId, SPL.value customerGroupId
		INTO #GROUP_MAPPED
		FROM BroadCastNotification B
		CROSS APPLY DBO.Split(',', B.CustomerGroupIds) SPL
		WHERE B.RowId = @RowId
		
		SELECT CG.GroupName, CG.RowId, IsSelected = CASE WHEN G.RowId IS NULL THEN 'N' ELSE 'Y' END
		FROM CustomerGroup CG(NOLOCK)
		LEFT JOIN #GROUP_MAPPED G ON CG.RowId = G.customerGroupId
		WHERE CG.IsActive = 1
		AND CG.BroadcastTypeId = @BroadcastTypeId
	END
	ELSE IF @Flag = 'UPLOAD-CUSTOMER'
	BEGIN
		IF @RowId IS NULL
		BEGIN
			SELECT 1, 'Invalid Customer group type!', NULL
			RETURN
		END
		SELECT  membershipId = p.value('@membershipid', 'VARCHAR(100)')
		INTO #TEMP_CUSTOMER
		FROM    @xml.nodes('/root/row') AS tmp ( p );

		DELETE TMP 
		FROM #TEMP_CUSTOMER TMP
		INNER JOIN customerMaster CM(NOLOCK) ON CM.membershipId = TMP.membershipId
		INNER JOIN CustomerGroupDetail CD(NOLOCK) ON CD.CustomerId = CM.customerId
		WHERE CD.GroupId= @RowId

		INSERT INTO CustomerGroupDetail(GroupId, CustomerId, CreatedBy, CreatedDate)
		SELECT @RowId, CM.customerId, @User, GETDATE()
		FROM #TEMP_CUSTOMER T
		INNER JOIN CUSTOMERMASTER CM(NOLOCK) ON CM.membershipId = T.membershipId

		SELECT 0, 'Success', NULL
	END
	ELSE IF @Flag = 'UPLOAD-CUSTOMER-SINGLE'
	BEGIN
		IF @RowId IS NULL
		BEGIN
			SELECT 1, 'Invalid Customer group type!', NULL
			RETURN
		END
		IF NOT EXISTS(SELECT * FROM CUSTOMERMASTER (NOLOCK) WHERE CUSTOMERID = @CustomerId)
		BEGIN
			SELECT 1, 'Invalid customer!', NULL
			RETURN
		END
		IF EXISTS(SELECT * FROM CustomerGroupDetail (NOLOCK) WHERE CustomerId = @CustomerId AND GroupId = @RowId)
		BEGIN
			SELECT 1, 'Customer Already added!', NULL
			RETURN
		END
		
		INSERT INTO CustomerGroupDetail(GroupId, CustomerId, CreatedBy, CreatedDate)
		SELECT @RowId, @CustomerId, @User, GETDATE()
		
		SELECT 0, 'Success', NULL
	END
	ELSE IF @Flag = 'CUSTOMER-LIST'
	BEGIN
		SELECT FullName = CM.FullName + ' | ' + CM.membershipId , RowId = RowId
		FROM CustomerGroupDetail CD(NOLOCK)
		INNER JOIN CUSTOMERMASTER CM(NOLOCK) ON CM.customerId = CD.CustomerId
		WHERE GroupId = @RowId
	END
	ELSE IF @Flag = 'BROADCAST-DETAIL'
	BEGIN
		SELECT RowId, Title = SV.detailTitle + ': ' + BC.Title, BC.Body
		FROM BroadCastNotification BC(NOLOCK)
		INNER JOIN dbo.StaticDataValue SV (NOLOCK) ON BC.BroadcastTypeId = SV.ValueId
		WHERE RowId = @RowId
	END

	ELSE IF @Flag = 'DELETE-CUSTOMER-FROM-GROUP'  --added on feb 26
	BEGIN
          DELETE
          FROM CustomerGroupDetail
          WHERE RowId = @rowid
		IF @@ROWCOUNT = 0
			PRINT 'Customer ID not found'
		ELSE
		SELECT 0 ErrorCode
            , 'Customer Deleted Successfully' Msg
            , @rowid RowId
		RETURN
	END

	ELSE IF @Flag = 'RE-PUSH'
	BEGIN
		SELECT @BroadcastTypeId = BroadcastTypeId, @ImageURL=ImageURL,@Body=Body,@Title=Title,@CustomerGroupIds=CustomerGroupIds , @msgType = messageType, @entrytype = entryType
		FROM BroadCastNotification (NOLOCK)
		WHERE RowId = @RowId
	
		IF @BroadcastTypeId IS NULL
		BEGIN
			SELECT 1, 'Invalid data!', NULL
			RETURN
		END

		SELECT @CustomerId = CUSTOMERID FROM BroadCastNotification BC(NOLOCK) WHERE RowId = @RowId
		IF @CustomerId IS NOT NULL
		BEGIN		
			SELECT RowId, BC.Title, BC.Body, IsBulkNotification = 'false' , SV.detailTitle as BroadcastTypeId  , BC.ImageURL as NavigateURL, BC.notificationType AS ClickActivity --added on 25th feb
			,BC.messageType AS MessageType
			FROM BroadCastNotification BC(NOLOCK)
			INNER JOIN dbo.StaticDataValue SV (NOLOCK) ON BC.BroadcastTypeId = SV.ValueId
			WHERE RowId = @RowId
			IF @BroadcastTypeId = 11430 --EMAIL
			BEGIN
				SELECT SendTo = CM.userName, DeviceType = ISNULL(USR.DeviceType, 'Android')
				FROM CUSTOMERMASTER CM(NOLOCK) 
				LEFT JOIN mobile_userRegistration USR(NOLOCK) ON USR.customerId = CM.customerId
				WHERE cm.customerId = @CustomerId
			END
			ELSE IF @BroadcastTypeId = 11429 --PUSH
			BEGIN
				SELECT SendTo =  USR.deviceId, DeviceType = ISNULL(USR.DeviceType, 'Android') 
				FROM CUSTOMERMASTER CM(NOLOCK) 
				LEFT JOIN mobile_userRegistration USR(NOLOCK) ON USR.customerId = CM.customerId
				WHERE cm.customerId = @CustomerId
			END
		END
		ELSE 
		BEGIN

		PRINT @BroadcastTypeId;

		SELECT RowId, BC.Title, BC.Body, IsBulkNotification = 'true', SV.detailTitle as BroadcastTypeId , BC.ImageURL as NavigateURL, BC.notificationType AS ClickActivity  --added on 25th feb
		, BC.messageType AS MessageType
		FROM BroadCastNotification BC(NOLOCK)
		INNER JOIN dbo.StaticDataValue SV (NOLOCK) ON BC.BroadcastTypeId = SV.ValueId
		WHERE RowId = @RowId

		SELECT BN.RowId, SPL.value customerGroupId
		INTO #TmpCustomerGroupIds
		FROM BroadCastNotification BN(NOLOCK)
		CROSS APPLY DBO.Split(',', BN.CustomerGroupIds) SPL
		WHERE BN.RowId = @RowId

		IF @BroadcastTypeId = 11430 --EMAIL
		BEGIN
			SELECT SendTo = CM.userName, DeviceType = ISNULL(USR.DeviceType, 'Android')
			FROM #TmpCustomerGroupIds TMP
			INNER JOIN CustomerGroupDetail CGD(NOLOCK) ON CGD.GroupId = TMP.customerGroupId
			INNER JOIN CUSTOMERMASTER CM(NOLOCK) ON CM.CUSTOMERID = CGD.CustomerId
				LEFT JOIN mobile_userRegistration USR(NOLOCK) ON USR.customerId = CM.customerId
		END
		ELSE IF @BroadcastTypeId = 11429 --PUSH
		BEGIN
		IF EXISTS (select top 1 1 from countryMaster (nolock) where countryId = @entrytype) 
		BEGIN
		    SELECT customerId 
			into #tempCustomer FROM pushNotificationHistroy WHERE sentId = @RowId

			SELECT  SendTo = USR.deviceId, DeviceType = ISNULL(USR.DeviceType, 'Android')
			INTO #Finaltbl1
			FROM #tempCustomer TMP
			INNER JOIN CUSTOMERMASTER CM(NOLOCK) ON CM.CUSTOMERID = TMP.CustomerId
				LEFT JOIN mobile_userRegistration USR(NOLOCK) ON USR.customerId = CM.customerId
				AND usr.deviceId is not null

			 SELECT * FROM
			(
				SELECT *, 
					   RANK() OVER(PARTITION BY SendTo                                        
					   ORDER BY DeviceType) rank
				FROM #Finaltbl1
			) T 
			WHERE rank = 1 
		END
		ELSE
		BEGIN
			SELECT  SendTo = USR.deviceId, DeviceType = ISNULL(USR.DeviceType, 'Android')
			INTO #Finaltbl
			FROM #TmpCustomerGroupIds TMP
			INNER JOIN CustomerGroupDetail CGD(NOLOCK) ON CGD.GroupId = TMP.customerGroupId
			INNER JOIN CUSTOMERMASTER CM(NOLOCK) ON CM.CUSTOMERID = CGD.CustomerId
				LEFT JOIN mobile_userRegistration USR(NOLOCK) ON USR.customerId = CM.customerId
				AND usr.deviceId is not null

			 SELECT * FROM
			(
				SELECT *, 
					   RANK() OVER(PARTITION BY SendTo                                        
					   ORDER BY DeviceType) rank
				FROM #Finaltbl
			) T 
			WHERE rank = 1 
         
		END
	END
	END
	  IF @isRepush = 'Y'
	  BEGIN

	  	PRINT @BroadcastTypeId;

		INSERT INTO BroadCastNotificationLog(BroadCastId,CreatedBy,CreatedDate, ResponseMessage)
		values (@RowId, @user , GETDATE() , @BroadcastTypeId )
		
		IF @BroadcastTypeId = 11429 -- add notify
		BEGIN		
			IF @CustomerId IS NOT NULL
			BEGIN
			 INSERT INTO pushNotificationHistroy(customerId,body,title,createDate,imageURL,sentId,Type,isReservation,isRead,isSend,category,url, isClickable)
			 select @customerid, @Body,@Title,getdate(),'',@RowId,0,0,0,0,@msgType,@ImageURL,IIF(@ImageURL IS NULL,'N','Y')
			 END			 
			ELSE
			 BEGIN
			 INSERT INTO pushNotificationHistroy(customerId,body,title,createDate,imageURL,sentId,Type,isReservation,isRead,isSend,category,url, isClickable)
			  select distinct customerid, @Body,@Title,getdate(),'',@RowId,0,0,0,0,@msgType,@ImageURL,IIF(@ImageURL IS NULL,'N','Y')
			  FROM  CustomerGroupDetail WHERE GroupId=@CustomerGroupIds
			 END
		  END
      END
	 END


	ELSE IF @Flag = 'IN-BROADCAST'
	BEGIN
		IF @customerType = 'nativeCountry'
		BEGIN
		INSERT INTO BroadCastNotification(BroadcastTypeId, CustomerGroupIds, Title, Body, ImageURL, IsSent, CreatedBy
							, CreatedDate, ScheduleDate, IsActive, customerId, notificationType, messageType,entryType )
			SELECT '11429', '0', 'IME London Notification', @Body, @ImageURL, 0, @User
								, GETDATE(), @ScheduleDate, 1 , @CustomerId, 'OPEN_ACTIVITY_DASHBOARD', @msgType, cast(@nativeCountry as varchar)

			SET @RowId = @@IDENTITY

			SELECT distinct deviceId , cm.customerid , isInserted = 0
			into #tempCustomer1
			FROM CUSTOMERMASTER (NOLOCK) cm 
			INNER JOIN mobile_userRegistration (NOLOCK) MU on mu.customerId = cm.customerId
			WHERE CASE @nativeCountry WHEN 'ALL' THEN 1 
			ELSE cm.country END = CASE @nativeCountry WHEN 'ALL' THEN 1 ELSE @nativeCountry END  and isnull(isactive , 'N') = 'Y'  and mu.deviceId IS NOT NULL and cm.mobileApprovedDate IS NOT NULL 
			and email in( 'Kewal@imelondon.uk.co','stha@yopmail.com')

			WHILE EXISTS (SELECT TOP 1 1 FROM #tempCustomer1 WHERE isInserted = 0)
			BEGIN
			 print 'a'
				SELECT @customerIdForPush = customerId, @deviceId = deviceId
				FROM #tempCustomer1(NOLOCK)	WHERE isInserted = 0

				INSERT INTO pushNotificationHistroy(customerId,body,title,createDate,imageURL,sentId,Type,isReservation,isRead,isSend,category,url, isClickable)
				select @customerIdForPush, @Body,'IME London Notification',getdate(),'',@RowId,0,0,0,0,@msgType,@ImageURL,IIF(@ImageURL IS NULL,'N','Y')

				UPDATE #tempCustomer1 SET isInserted = 1 WHERE customerId = @customerIdForPush
			END

			SELECT 0, 'Success', @RowId
		END
		ELSE
		BEGIN
			INSERT INTO BroadCastNotification(BroadcastTypeId, CustomerGroupIds, Title, Body, ImageURL, IsSent, CreatedBy
								, CreatedDate, ScheduleDate, IsActive, customerId, notificationType, messageType, entryType)
			SELECT '11429', '0', 'JME Notification', @Body, @ImageURL, 0, @User
								, GETDATE(), @ScheduleDate, 1 , @CustomerId, 'OPEN_ACTIVITY_DASHBOARD', @msgType , 'Non-Group'

			SET @RowId = @@IDENTITY
	
			INSERT INTO pushNotificationHistroy(customerId,body,title,createDate,imageURL,sentId,Type,isReservation,isRead,isSend,category,url, isClickable)
			select @customerid, @Body,'JME Notification',getdate(),'',@RowId,0,0,0,0,@msgType,@ImageURL,IIF(@ImageURL IS NULL,'N','Y')
	
			SELECT 0, 'Success', @RowId
		END
	END

	--select * from BroadCastNotification order by rowid desc
	ELSE IF @Flag = 'UN-BROADCAST'
	BEGIN
		UPDATE BroadCastNotification
		SET      Body = @Body
				, ModifiedBy = @User, ModifiedDate = GETDATE()
				, messageType = @msgType
		WHERE RowId = @RowId  

		SELECT 0, 'Success', NULL
	END
	ELSE IF @Flag = 'SN-BROADCAST'
	BEGIN
		IF @SortBy IS NULL
		   SET @SortBy = 'CreatedDate'
		IF @SortOrder IS NULL
		   SET @SortOrder = 'DESC'

		 SET @table = '(
						SELECT BN.RowId
							,fullName = ISNULL(CM.fullName,c.countryname)
							,IsSent = CASE WHEN IsSent = 0 THEN ''No'' WHEN IsSent = 1 THEN ''Yes'' ELSE ''Failed'' END
							,BN.CreatedDate as CreatedDate
							,mobileNumber = cm.mobile
							,message = BN.body
							,email = cm.email
							,title = bn.title
						    ,BN.BroadcastTypeId
							,BN.createdby
					FROM BroadCastNotification BN (NOLOCK)
					INNER JOIN dbo.StaticDataValue SV (NOLOCK) ON BN.BroadcastTypeId = SV.ValueId
					LEFT JOIN dbo.CustomerMaster CM (NOLOCK) ON cm.customerId = ISNULL(BN.customerId,'''')
				    LEFT JOIN Countrymaster c (nolock) ON BN.entryType = CAST(c.countryid as varchar)
					WHERE 1 = 1 AND BN.isActive = 1 AND bn.entryType  IS NOT NULL 
				)x'

	--print @table
	--	SET @SqlFilter = ''
		
	--	IF @BroadCastTypeId IS NOT NULL
	--		SET @SqlFilter += ' AND X.BroadcastTypeId = ''' + CAST(@BroadcastTypeId AS VARCHAR) + ''''

	--	IF @Title IS NOT NULL
	--		SET @SqlFilter += ' AND X.Title LIKE ''' + @Title + '%'''
		
		
		SET @SelectFieldList ='RowId,  IsSent, CreatedDate, message, email,fullName,mobileNumber,title,BroadcastTypeId,createdby'

		EXEC dbo.proc_paging
                @table
               ,@SqlFilter
               ,@SelectFieldList
               ,@ExtraFieldList
               ,@sortBy
               ,@sortOrder
               ,@pageSize
               ,@pageNumber
	END
END

