USE [FastMoneyPro_Remit]
GO
/****** Object:  StoredProcedure [dbo].[PROC_CHECK_KFTC_LIMIT]    Script Date: 7/4/2019 11:35:48 AM ******/
--DROP PROCEDURE [dbo].[PROC_CHECK_KFTC_LIMIT]
GO
/****** Object:  StoredProcedure [dbo].[PROC_CHECK_KFTC_LIMIT]    Script Date: 7/4/2019 11:35:48 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-----------------------------------------------------------------------------------
-- PROCEDURE	ID	:	PROC_CHECK_KFTC_LIMIT
-- PROCEDURE	NAME	:	KFTC 이용기관 출금이체 한도 초과 체크
-- DEV		DATE	:	2019.04.18
-- DEVELOPER		:	김한성
-- DESC			:	
-- TEXT		DESC	:	돋움체, 9PT, TAB=8
-- RETURN	VALUE	:	
-- SAMPLE		:	
----------------------------------------------------------------------------------- 
 
CREATE OR ALTER PROCEDURE [dbo].[PROC_CHECK_KFTC_LIMIT]  
(  
	@flag   VARCHAR(50)  = NULL  
)  
AS  
  
SET  NOCOUNT ON  
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED  
----------------------------------------------------------------------------------  
-- Procedure Start  
----------------------------------------------------------------------------------  
DECLARE @V_DTS_TODAY 	VARCHAR(8)  
DECLARE @V_WITHDRAWAL 	MONEY  
DECLARE @V_PHONE_NUMBER VARCHAR(500)  
SET  	@V_PHONE_NUMBER = 'John^01041637562|Subash^01030436864|Eric^01085567562|Choi^01053554993|Pagna^01059580644|Max^01092161470|Pralhad^01074518940'  
--SET  @V_PHONE_NUMBER = 'Max^01092161470'  
DECLARE @V_TEMP MONEY
SET 	@V_WITHDRAWAL = 0
SET 	@V_TEMP = 0
  
DECLARE @V_TODAY  VARCHAR(20)  
DECLARE @V_TOMORROW VARCHAR(20)  
DECLARE @V_ROWID BIGINT  
  
--DECLARE @P_DTS_INSERT NVARCHAR(14)      
--SET @P_DTS_INSERT = REPLACE(REPLACE(REPLACE(CONVERT(NVARCHAR(20), GETDATE(), 120), '-',''), ' ', ''),':','')      
BEGIN  
	IF (@flag = 'WITHDRAW')  
	BEGIN  
		SELECT  @V_TODAY     = CONVERT(date, GETDATE())  
		SELECT  @V_TOMORROW  = CONVERT(date, DATEADD(DAY, 1, @V_TODAY))  
		SELECT  @V_DTS_TODAY = CONVERT(VARCHAR(8), GETDATE(), 112) --AS [YYYYMMDD]  
		--PRINT @V_DTS_TODAY  
    
	--------------------------------------------------------------------
	-- KFTC 출금이체 API를 통한 고객의 출금이체 총금액을 구하는 로직
	--------------------------------------------------------------------
	-- select sum(CAST(tranAmt as MONEY)) from KFTC_CUSTOMER_TRANSFER(nolock) where apiTranDtm > '20190417'   
  
	--------------------------------------------------------------------
	-- 해외송금에서 출금이체 API를 이용한 금액
	--------------------------------------------------------------------
		SELECT  @V_TEMP = SUM(CAST(tranAmt AS MONEY))   
		FROM  	KFTC_CUSTOMER_TRANSFER(NOLOCK)   
		WHERE  	apiTranDtm > @V_DTS_TODAY  
		
		SET 	@V_WITHDRAWAL = @V_WITHDRAWAL + @V_TEMP
	--------------------------------------------------------------------
	-- 국내송금에서 출금이체 API를 이용한 금액
	--------------------------------------------------------------------
		SELECT 	@V_TEMP = SUM(CAST(reqTranAmt AS MONEY)) 
		FROM 	KFTC_DOMESTIC_HISTORY(NOLOCK) 
		WHERE 	accountType = 'autodebit' 
		AND  	transferType= 'withdraw'
		AND 	resRspCode = 'A0000'
		AND 	reqTransferDtime > @V_TODAY
		
		SET 	@V_WITHDRAWAL = @V_WITHDRAWAL + @V_TEMP
	--------------------------------------------------------------------
	-- TOPUP에서 출금이체 API를 이용한 금액
	--------------------------------------------------------------------
		SELECT 	@V_TEMP = SUM(CAST(reqTranAmt AS MONEY)) 
		FROM 	KFTC_DOMESTIC_HISTORY(NOLOCK) 
		WHERE 	accountType = 'KFTC-TOPUP' 
		AND  	transferType= 'withdraw'
		AND 	resRspCode = 'A0000'
		AND 	reqTransferDtime > @V_TODAY
		
		SET 	@V_WITHDRAWAL = @V_WITHDRAWAL + @V_TEMP

	-------------------------------------------------------------------
	-- KFTC 출금이체가 하루동안 40억의 70%(28억)를 초과하면 문자메시지를 전송
	--------------------------------------------------------------------
		IF   @V_WITHDRAWAL > CONVERT(MONEY, '2800000000')
		BEGIN  
			IF NOT EXISTS (  
				SELECT  1  
				FROM    TBL_KFTC_LIMIT(NOLOCK)   
				WHERE   CHECK_TYPE = 'WITHDRAW'  
				AND  LIMIT_CHECK_1 BETWEEN @V_TODAY AND @V_TOMORROW  
			)  
			BEGIN  
				INSERT INTO KT_SMS.dbo.SDK_SMS_SEND ( USER_ID, SCHEDULE_TYPE, SUBJECT, SMS_MSG, NOW_DATE, SEND_DATE, CALLBACK, DEST_INFO)  
				SELECT 'globalmoney',0,'Notice','[GME Notice] KFTC withdrawal limit exceeded 70%',FORMAT(GETDATE(),'yyyyMMddHHmmss'),FORMAT(GETDATE(),'yyyyMMddHHmmss'),'1588-6864',@V_PHONE_NUMBER  
      
				INSERT INTO TBL_KFTC_LIMIT ( CHECK_TYPE, LIMIT_CHECK_1 )  
				SELECT 'WITHDRAW', GETDATE()  
			END    
		END 

	--------------------------------------------------------------------
	-- KFTC 출금이체가 하루동안 40억의 90%(36억)를 초과하면 문자메시지를 전송
	--------------------------------------------------------------------
		IF   @V_WITHDRAWAL > CONVERT(MONEY, '3600000000')
		BEGIN  
			IF NOT EXISTS (  
				SELECT  1  
				FROM    TBL_KFTC_LIMIT(NOLOCK)   
				WHERE   CHECK_TYPE = 'WITHDRAW'  
				AND  LIMIT_CHECK_2 BETWEEN @V_TODAY AND @V_TOMORROW  
			)  
			BEGIN  
				INSERT INTO KT_SMS.dbo.SDK_SMS_SEND ( USER_ID, SCHEDULE_TYPE, SUBJECT, SMS_MSG, NOW_DATE, SEND_DATE, CALLBACK, DEST_INFO)  
				SELECT 'globalmoney',0,'Notice','[GME Notice] KFTC withdrawal limit exceeded 90%',FORMAT(GETDATE(),'yyyyMMddHHmmss'),FORMAT(GETDATE(),'yyyyMMddHHmmss'),'1588-6864',@V_PHONE_NUMBER  
      
				SELECT  @V_ROWID = [rowId]  
				FROM    TBL_KFTC_LIMIT(NOLOCK)  
				WHERE   CHECK_TYPE = 'WITHDRAW'  
				AND  LIMIT_CHECK_1 BETWEEN @V_TODAY AND @V_TOMORROW  
      
				IF   @V_ROWID IS NULL  
				BEGIN  
					INSERT INTO TBL_KFTC_LIMIT ( CHECK_TYPE, LIMIT_CHECK_2 )  
					SELECT 'WITHDRAW', GETDATE()  
				END  
				ELSE  
				BEGIN  
					UPDATE  TBL_KFTC_LIMIT   
					SET  LIMIT_CHECK_2 = GETDATE()   
					WHERE  [rowId] = @V_ROWID  
				END     
			END    
		END  
	END  
END  
----------------------------------------------------------------------------------  
-- Procedure End  
----------------------------------------------------------------------------------  
SET TRANSACTION ISOLATION LEVEL READ COMMITTED  
SET  NOCOUNT OFF  
  
GO

