﻿USE [FastMoneyPro_Remit]
GO

/****** Object:  StoredProcedure [dbo].[KFTC_LOG_CUSTOMER_INFO]    Script Date: 7/4/2019 11:35:48 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
  
ALTER  PROC [dbo].[KFTC_LOG_CUSTOMER_INFO]          
(          
@flag     VARCHAR(20)          
,@customerId   BIGINT   = NULL          
,@accessToken   VARCHAR(400)  = NULL          
,@tokenType    VARCHAR(10)  = NULL          
,@expiresIn    INT    = NULL          
,@refreshToken   VARCHAR(400)  = NULL          
,@scope     VARCHAR(30)  = NULL          
,@userName    NVARCHAR(100) = NULL          
,@userInfo    VARCHAR(8)  = NULL          
,@userGender   CHAR(1)   = NULL          
,@userCellNo   VARCHAR(15)  = NULL          
,@userEmail    VARCHAR(100) = NULL          
,@XML     XML    = NULL          
,@userCi    VARCHAR(4000) = NULL          
,@fintechUseNo   VARCHAR(30)  = NULL          
,@userSeqNo    VARCHAR(10)  = NULL             
,@apiTranDtm   VARCHAR(17)  = NULL              
,@rspCode    VARCHAR(5)  = NULL              
,@rspMessage   VARCHAR(10)  = NULL          
,@dpsBankCodeStd  VARCHAR(3)  = NULL             
,@dpsAccountNumMasked VARCHAR(20)  = NULL          
,@dpsPrintContent  NVARCHAR(20)    = NULL          
,@bankTranId   VARCHAR(20)  = NULL             
,@bankTranDate   VARCHAR(8)  = NULL             
,@bankCodeTran   VARCHAR(3)  = NULL             
,@bankRspCode   VARCHAR(3)  = NULL              
,@bankCodeStd   VARCHAR(3)  = NULL              
,@accountNumMasked  VARCHAR(20)  = NULL          
,@printContent   NVARCHAR(20) = NULL            
,@accountName   NVARCHAR(20) = NULL           
,@tranAmt  MONEY  = NULL            
,@apiTranId    VARCHAR(20)  = NULL          
,@clientId    VARCHAR(50)  = NULL          
,@clientSecret   VARCHAR(50)  = NULL          
,@clientUseCode   VARCHAR(10)  = NULL          
,@rowId     BIGINT   = NULL          
,@errorCode    VARCHAR(10)  = NULL          
,@errorMsg    VARCHAR(500) = NULL          
,@remittance_check  CHAR(1)   = NULL          
,@kftcLogId    BIGINT   = NULL          
,@accountNumber   VARCHAR(20)  = NULL          
,@accHolderInfoType  VARCHAR(100)  = NULL    
,@accHolderInfo   VARCHAR(50)  = NULL    
,@bankCode  VARCHAR(5) = NULL  
)          
AS          
SET NOCOUNT ON;          
SET XACT_ABORT ON;        
    
DECLARE @msg VARCHAR(100)    
      
BEGIN TRY          
          
   
 IF @flag = 'I'       
 BEGIN     
 DECLARE @accessTokenExpTime DATETIME     
 SET @accessTokenExpTime = DATEADD(SECOND, @expiresIn, GETDATE())    
      
 DECLARE @firstName VARCHAR(100)    
 SELECT @firstName = firstName    
 FROM  CUSTOMERMASTER(NOLOCK)    
 WHERE  customerId = @customerId    
    
 IF EXISTS(SELECT 1 FROM KFTC_CUSTOMER_MASTER (NOLOCK) WHERE customerId = @customerId)    
 BEGIN    
  IF ( REPLACE(@firstName, ' ', '') = REPLACE(@userName, ' ', '') )    
  BEGIN    
   UPDATE KFTC_CUSTOMER_MASTER SET    
   userSeqNo = @userSeqNo    
   ,accessToken = @accessToken    
   ,tokenType = @tokenType    
   ,expiresIn = @expiresIn    
   ,accessTokenRegTime = GETDATE()    
   ,accessTokenExpTime = @accessTokenExpTime    
   ,refreshToken = @refreshToken    
   ,scope = @scope    
   ,userCi = @userCi    
   ,userName = @userName    
   ,userInfo = @userInfo    
   ,userGender = @userGender    
   ,userCellNo = @userCellNo    
   ,userEmail = @userEmail    
   ,approvedBy = 'system'    
   ,approvedDate = GETDATE()    
   WHERE customerId = @customerId    
  END    
  ELSE    
  BEGIN    
   UPDATE KFTC_CUSTOMER_MASTER SET    
   userSeqNo = @userSeqNo    
   ,accessToken = @accessToken    
   ,tokenType = @tokenType    
   ,expiresIn = @expiresIn    
   ,accessTokenRegTime = GETDATE()    
   ,accessTokenExpTime = @accessTokenExpTime    
   ,refreshToken = @refreshToken    
   ,scope = @scope    
   ,userCi = @userCi    
   ,userName = @userName    
   ,userInfo = @userInfo    
   ,userGender = @userGender    
   ,userCellNo = @userCellNo    
   ,userEmail = @userEmail    
   WHERE customerId = @customerId    
    END    
    
  SET @msg = 'Auto debit account refreshed successfully!'    
 END     
 ELSE    
 BEGIN    
  EXEC proc_errorHandler 1, 'There are no real-name data.!', null    
  RETURN    
 END    
          
  --PARSING XML DATA          
  DECLARE @hDoc AS INT, @SQL NVARCHAR (MAX)          
          
  EXEC sp_xml_preparedocument @hDoc OUTPUT, @XML          
          
  DECLARE @TEMPTABLE TABLE (fintechUseNo VARCHAR(30), accountAlias NVARCHAR(50), bankCodeStd VARCHAR(3), bankName NVARCHAR(20), accountNumMasked VARCHAR(20), accountState CHAR(2)          
          ,accountNum VARCHAR(20), accountName NVARCHAR(100), accountType CHAR(1), inquiryAgreeYn CHAR(1), transferAgreeYn VARCHAR(14), inquiryAgreeDtime VARCHAR(14), transferAgreeDtime VARCHAR(14))          
             
  INSERT INTO @TEMPTABLE(fintechUseNo, accountAlias, bankCodeStd, bankName, accountNum, accountNumMasked, accountName, accountType, inquiryAgreeYn, transferAgreeYn, accountState,          
         inquiryAgreeDtime, transferAgreeDtime)          
          
  SELECT fintechUseNo, accountAlias, bankCodeStd, bankName, accountNum, accountNumMasked, accountName, accountType, inquiryAgreeYn, transferAgreeYn, accountState,          
         inquiryAgreeDtime, transferAgreeDtime          
  FROM OPENXML(@hDoc, '/root/row')          
  WITH           
  (          
   fintechUseNo VARCHAR(30) '@fintechUseNo',          
   accountAlias NVARCHAR(50) '@accountAlias',          
   bankCodeStd VARCHAR(3) '@bankCodeStd',          
   bankName NVARCHAR(20) '@bankName',          
   accountNum VARCHAR(20) '@accountNum',          
   accountNumMasked VARCHAR(20) '@accountNumMasked',          
   accountName NVARCHAR(100) '@accountName',          
   accountType CHAR(1) '@accountType',          
   inquiryAgreeYn CHAR(1) '@inquiryAgreeYn',          
   transferAgreeYn CHAR(1) '@transferAgreeYn',          
   inquiryAgreeDtime VARCHAR(14) '@inquiryAgreeDtime',          
   accountState CHAR(2) '@accountState',          
   transferAgreeDtime VARCHAR(14) '@transferAgreeDtime'          
  )          
          
  DELETE T           
  FROM @TEMPTABLE T          
  INNER JOIN KFTC_CUSTOMER_SUB S(NOLOCK) ON  S.customerId = @customerId AND userSeqNo = @userSeqNo AND S.fintechUseNo = T.fintechUseNo AND S.accountNum = T.accountNum          
          
  INSERT INTO KFTC_CUSTOMER_SUB(customerId, userSeqNo, fintechUseNo, accountAlias, bankCodeStd, bankName, accountNum, accountNumMasked, accountName, accountType, inquiryAgreeYn, transferAgreeYn,          
         inquiryAgreeDtime, transferAgreeDtime, accountState)          
  SELECT @customerId, @userSeqNo, fintechUseNo, accountAlias, bankCodeStd, bankName, accountNum, accountNumMasked, accountName, accountType, inquiryAgreeYn, transferAgreeYn,          
         inquiryAgreeDtime, transferAgreeDtime, accountState           
  FROM @TEMPTABLE          
             
  EXEC sp_xml_removedocument @hDoc          
          
  EXEC proc_errorHandler 0, @msg, @accessToken;          
END          
ELSE IF @flag = 'S'          
BEGIN          
 DECLARE @LASTSYNC DATETIME = NULL          
          
 SELECT S.RowId,M.accessToken AS [access_token], M.tokenType, M.scope,   M.userCi,          
 S.customerId,      S.userSeqNo, S.fintechUseNo,  S.bankCodeStd,          
 S.bankName,       S.accountNum, S.accountNumMasked, S.accountName,          
 DATEDIFF(DD, ISNULL(M.AccountSyncDT, GETDATE()), GETDATE()),          
 BankCode = s.bankCodeStd,                   
 IsSyncAccList = CASE WHEN DATEDIFF(DD, ISNULL(M.AccountSyncDT, GETDATE()), GETDATE()) > 1 THEN 'Y' ELSE 'N' END,          
 IsShowRefresh = CASE WHEN DATEDIFF(MM, accessTokenRegTime, GETDATE()) >= 11 THEN 'Y' ELSE 'N' END,          
 IsInboundPennyTestRequested = CASE WHEN s.bankCodeStd = icbl.bankCode AND s.accountNum = icbl.accountNo THEN 1 ELSE 0 END
 ,cm.email as Email ,M.accessTokenExpTime  as IdExpiry          
 FROM KFTC_CUSTOMER_SUB S(NOLOCK)          
 INNER JOIN KFTC_CUSTOMER_MASTER M(NOLOCK) ON M.customerId = S.customerId
 INNER JOIN dbo.customerMaster cm(NOLOCK) ON cm.customerId =  m.customerId           
 LEFT JOIN dbo.INBOUND_CUSTOMERBANKLIST(NOLOCK)icbl ON m.customerId = icbl.customerId             
 WHERE S.customerId = @customerId  ORDER BY S.RowId ASC           
      
 UPDATE KFTC_CUSTOMER_MASTER SET AccountSyncDT = GETDATE() WHERE customerId = @customerId          
END          
ELSE IF @flag = 's-send'          
BEGIN          
 DECLARE @v_accHolderInfoType VARCHAR(100)          
 DECLARE @v_accHolderInfo VARCHAR(50)          
 DECLARE @v_userInfo   VARCHAR(8)          
 DECLARE @v_userGender CHAR(1)          
 DECLARE @v_combination  VARCHAR(50)          
 DECLARE @v_national   CHAR(1)          
 DECLARE @v_lastDigit  VARCHAR(5)          
        
 SELECT  @v_userInfo = userInfo,           
 @v_userGender = CASE WHEN userGender = 'M' THEN '7' ELSE '8' END,          
 @v_accHolderInfoType = accHolderInfoType,          
 @v_accHolderInfo = accHolderInfo          
 FROM KFTC_CUSTOMER_MASTER(NOLOCK)          
 WHERE   customerId = @customerId          
          
 SELECT * FROM (          
	SELECT  0 AS RowId,			customerType,
			accountNo = '', 	accountNumMasked='KRW ' + FORMAT(ISNULL(availableBalance,0),'0'), autoDebit = '', walletName = 'GME Wallet', 
			[type] = 'wallet', 	fintechUseNo = '', 	accountName = '', 	bankCode = '','' AS [isApproved], '' AS [accHolderInfoType], '' AS [accHolderInfo]			
	FROM  customerMaster (NOLOCK)           
	WHERE  customerId = @customerId          
          
	UNION ALL          
          
	SELECT 	S.RowId,      
			'' AS [customerType],    
			S.accountNum AS [accountNo],           
			S.accountNumMasked,           
			'Auto Debit' AS [autoDebit],          
			S.bankName AS [walletName],           
			'autodebit' AS [type],           
			S.fintechUseNo AS [fintechUseNo],           
			S.accountName AS [accountName],           
			S.bankCodeStd AS [bankCode],          
			isApproved = CASE WHEN ApprovedBy IS NULL THEN 'N' ELSE 'Y' END,          
			M.accHolderInfoType, M.accHolderInfo			
	FROM KFTC_CUSTOMER_SUB S(NOLOCK)          
	INNER JOIN KFTC_CUSTOMER_MASTER M(NOLOCK) ON M.customerId = S.customerId           
	WHERE S.customerId = @customerId          
  )x ORDER BY x.RowId ASC           
END          
ELSE IF @flag = 'DELETE'          
BEGIN          
 -- 삭제하기전에 KFTC_CUSTOMER_SUB_DELETED 테이블에 삭제할 데이타를 저장             
 INSERT INTO KFTC_CUSTOMER_SUB_DELETED (masterId,    customerId,  userSeqNo,    fintechUseNo,   accountAlias,           
    bankCodeStd,   bankName,   accountNum,   accountNumMasked,  accountName,           
    accountType,   inquiryAgreeYn, transferAgreeYn,  accountState,   inquiryAgreeDtime,           
    transferAgreeDtime, RejectedBy,  RejectedDate,   RejectNote)          
          
 SELECT          0,      customerId,  userSeqNo,    fintechUseNo,   accountAlias,           
    bankCodeStd,   bankName,   accountNum,   accountNumMasked,  accountName,           
    accountType,   inquiryAgreeYn, transferAgreeYn,  accountState,   inquiryAgreeDtime,           
    transferAgreeDtime, @customerId,  GETDATE(),    'Deleted by customer'          
 FROM   KFTC_CUSTOMER_SUB (NOLOCK)           
 WHERE   customerId = @customerId           
 AND   fintechUseNo = @fintechUseNo          
             
 -- KFTC_CUSTOMER_SUB 데이타 삭제          
 DELETE FROM KFTC_CUSTOMER_SUB           
 WHERE   customerId = @customerId           
 AND   fintechUseNo = @fintechUseNo          
          
 EXEC proc_errorHandler 0, 'Account deleted successfully!', @customerId;          
END          
ELSE IF @flag = 'DELETE_ACC'          
BEGIN          
 -- 삭제하기전에 KFTC_CUSTOMER_SUB_DELETED 테이블에 삭제할 데이타를 저장          
 INSERT INTO KFTC_CUSTOMER_SUB_DELETED (masterId,    customerId,  userSeqNo,    fintechUseNo,   accountAlias,           
    bankCodeStd,  bankName,   accountNum,   accountNumMasked,  accountName,           
    accountType,   inquiryAgreeYn, transferAgreeYn,  accountState,   inquiryAgreeDtime,           
    transferAgreeDtime, RejectedBy,  RejectedDate,   RejectNote)          
          
 SELECT          0,      customerId,  userSeqNo,    fintechUseNo,   accountAlias,           
    bankCodeStd,   bankName,   accountNum,   accountNumMasked,  accountName,           
    accountType,   inquiryAgreeYn, transferAgreeYn,  accountState,   inquiryAgreeDtime,           
    transferAgreeDtime, @customerId,  GETDATE(),    'KFTC Auto Sync'          
 FROM   KFTC_CUSTOMER_SUB (NOLOCK)           
 WHERE   customerId = @customerId           
 AND   fintechUseNo = @fintechUseNo          
 AND   accountNum = @accountNumber          
             
 -- KFTC_CUSTOMER_SUB 데이타 삭제          
 DELETE FROM KFTC_CUSTOMER_SUB           
 WHERE   customerId = @customerId           
 AND   fintechUseNo = @fintechUseNo           
 AND   accountNum = @accountNumber          
          
 EXEC proc_errorHandler 0, 'Account deleted successfully!', @customerId;          
END          
ELSE IF @flag = 'I-TRAN'          
BEGIN          
 DECLARE @accountNum NVARCHAR(20)          
 SELECT @accountNum = accountNum FROM KFTC_CUSTOMER_SUB (NOLOCK) WHERE CUSTOMERID = @customerId AND fintechUseNo = @fintechUseNo          
          
 SET @dpsPrintContent = @accountNum + '_' + @dpsPrintContent          
          
 INSERT INTO KFTC_CUSTOMER_TRANSFER(customerId, fintechUseNo, apiTranId, apiTranDtm, rspCode, dpsBankCodeStd, dpsAccountNumMasked, dpsPrintContent, bankTranId,           
   bankTranDate, bankCodeTran, bankRspCode, bankCodeStd, accountNumMasked, printContent, accountName, tranAmt, remittance_check,           
   errorCode,           
   errorMsg)          
          
 SELECT @customerId, @fintechUseNo, @apiTranId, @apiTranDtm, @rspCode, @dpsBankCodeStd, @dpsAccountNumMasked, @dpsPrintContent, @bankTranId,           
   @bankTranDate, @bankCodeTran, @bankRspCode, @bankCodeStd, @accountNumMasked, @printContent, @accountName, @tranAmt, @remittance_check,          
   CASE WHEN @remittance_check = 'Y' THEN '0' ELSE '1' END,          
   CASE WHEN @remittance_check = 'Y' THEN 'SUCCESSFULLY VALIDATED!' ELSE 'ERROR WHILE KFTC CheckRemittant METHOD!' END          
 
 --*************************************************
 --for KFTC AUTO DEBIT LIMIT SMS
 --*************************************************

 EXEC PROC_CHECK_KFTC_LIMIT 'WITHDRAW'         

 --*************************************************
 --************************************************* 

 DECLARE @TEMPID BIGINT = @@IDENTITY          
          
 EXEC proc_errorHandler 0, 'Data Saved Successfully!', @TEMPID;          
 
 ----MAKE A WITHDRAW VOUCHER ENTRY FROM CMS ACCOUNT TO WALLET
  EXEC [proc_WithdrawBalance_Autodebit] @tranId = @TEMPID
 
          
END          
ELSE IF @flag = 'U-TRAN'          
BEGIN     
 IF EXISTS(SELECT TOP(1) 'A' FROM KFTC_CUSTOMER_TRANSFER(NOLOCK) WHERE rowId = @rowId  AND tranAmt <> ISNULL(@tranAmt,0))     
 BEGIN  
  EXEC proc_errorHandler 1, 'Sorry, Failed to refund amount.Contact GME Support', null;    
  RETURN  
 END    
 UPDATE KFTC_CUSTOMER_TRANSFER SET           
  errorCode = @errorCode          
  ,errorMsg = @errorMsg          
 WHERE rowId = @rowId      
 EXEC proc_errorHandler 0, 'Success', null;        
END          
ELSE IF @flag = 'SYNC-LIST-CUSTOMER'          
BEGIN          
 SELECT @clientId = dbo.fnadecryptstring(clientId), @clientSecret = dbo.fnadecryptstring(clientSecret) FROM KFTC_GME_MASTER (NOLOCK)           
          
 SELECT clientId = @clientId, clientSecret = @clientSecret, refreshToken, scope, customerId          
 FROM KFTC_CUSTOMER_MASTER (NOLOCK)           
 WHERE  accessTokenExpTime BETWEEN CAST(DATEADD(DAY, -1, GETDATE()) AS DATE) AND CAST(CAST(DATEADD(DAY, 1, GETDATE()) AS DATE) AS VARCHAR) + ' 23:59:59'          
END          
ELSE IF @flag = 'SYNC-LIST-GME'          
BEGIN          
 SELECT  clientId = dbo.fnadecryptstring(clientId), clientSecret  = dbo.fnadecryptstring(clientSecret)          
 FROM  KFTC_GME_MASTER (NOLOCK)           
 WHERE  accessToken IS NULL OR accessToken = ''           
 OR  accessTokenExpTime BETWEEN CAST(DATEADD(DAY, -1, GETDATE()) AS DATE) AND CAST(CAST(DATEADD(DAY, 1, GETDATE()) AS DATE) AS VARCHAR) + ' 23:59:59'          
END          
ELSE IF @flag = 'AUTO-SYNC-SUCCESS'          
BEGIN          
 SET @accessTokenExpTime = DATEADD(SECOND, @expiresIn, GETDATE())          
          
 UPDATE KFTC_CUSTOMER_MASTER SET          
  accessToken = @accessToken          
  ,tokenType = @tokenType          
  ,expiresIn = @expiresIn          
  ,accessTokenExpTime = @accessTokenExpTime          
  ,refreshToken = @refreshToken          
 WHERE customerId = @customerId          
END          
ELSE IF @flag = 'SYNC-SUCCESS-GME'          
BEGIN          
 SET @accessTokenExpTime = DATEADD(SECOND, @expiresIn, GETDATE())          
          
 UPDATE KFTC_GME_MASTER SET          
  accessToken = @accessToken          
  ,expiresIn = @expiresIn          
  ,accessTokenExpTime = @accessTokenExpTime          
  ,clientUseCode = dbo.fnaencryptstring(@clientUseCode)          
END          
ELSE IF @flag = 's-refresh'          
BEGIN          
 SELECT userSeqNo, userCi, userName, userInfo, userGender,          
  UserCellNo, UserEmail, PhoneCarrier = ''          
 FROM KFTC_CUSTOMER_MASTER (NOLOCK)           
 WHERE customerId = @customerId          
END          
ELSE IF @flag = 'txn-error'          
BEGIN          
 UPDATE KFTC_CUSTOMER_TRANSFER SET errorCode = @errorCode, errorMsg = @errorMsg WHERE ROWID = @kftcLogId          
END          
ELSE IF @flag= 'kftc-Lanaguage'          
BEGIN          
 Select 'eng' [key], 'English'  Value UNION ALL          
 Select 'kor' [key], 'Korean'  Value UNION ALL          
 Select 'vnm' [key], 'Vietnamese' Value UNION ALL          
 Select 'idn' [key], 'Indonesia' Value UNION ALL          
 Select 'khm' [key], 'Khmer' Value UNION ALL          
 Select 'phl' [key], 'Philippines' Value UNION ALL          
 Select 'npl' [key], 'Nepalese' Value UNION ALL          
 Select 'bgd' [key], 'Bangladesh' Value UNION ALL          
 Select 'pak' [key], 'Pakistan' Value UNION ALL          
 Select 'rus' [key], 'Russian' Value UNION ALL          
 Select 'uzb' [key], 'Uzbekistan' Value UNION ALL          
 Select 'mng' [key], 'Mongolia'  Value UNION ALL          
 Select 'lka' [Key], 'Sri Lanka' Value          
END          
  --KFTC에서 지원하는 은행 리스트      
ELSE IF @flag = 'kftc-Banklist'      
BEGIN      
 SELECT  rowId, bankCode, bankName        
 FROM KoreanBankList(nolock)        
 WHERE IsActive='1' AND  ISKFTC='Y'  
 ORDER BY bankCode     
END      
ELSE IF @flag='kftcCredentials'          
BEGIN          
 SELECT TOP 1 '0' ErrorCode,'Msg' AS Msg,dbo.FNADecryptString(clientId) AS Id,dbo.FNADecryptString(clientSecret) AS Extra FROM dbo.KFTC_GME_MASTER(NOLOCK)          
END          
ELSE IF @flag = 'CUSTOMER-MASTER'          
BEGIN          
 SELECT  idType, idNumber,           
  dob = FORMAT(dob,'yyMMdd'),           
  CASE WHEN gender = '97' THEN '7'           
  ELSE '8'           
  END AS [gender],          
  CASE WHEN nativeCountry='238' THEN '1' -- 미국          
  WHEN nativeCountry='113' THEN '2' -- 일본          
  WHEN nativeCountry='45'  THEN '3' -- 중국          
   ELSE '4'           
  END AS [country]          
 FROM  customerMaster(NOLOCK)          
 WHERE  customerId = @customerId          
END          
ELSE IF @flag = 'SAVE-REAL-NAME'      
BEGIN    
 IF EXISTS(SELECT 1 FROM KFTC_CUSTOMER_MASTER (NOLOCK) WHERE customerId = @customerId)    
 BEGIN    
  UPDATE  KFTC_CUSTOMER_MASTER     
  SET  accHolderInfoType = @accHolderInfoType,    
  accHolderInfo = @accHolderInfo    
  WHERE  customerId = @customerId    
 END     
 ELSE    
 BEGIN    
  INSERT INTO KFTC_CUSTOMER_MASTER (customerId, accHolderInfoType, accHolderInfo)    
  SELECT @customerId, @accHolderInfoType, @accHolderInfo    
 END     
      
 EXEC proc_errorHandler 0, 'Real name check successfully!', null;    
END    
ELSE IF @flag = 'CHK-REAL-NAME'      
BEGIN    
 SELECT   CM.customerId, KCM.accHolderInfoType, KCM.accHolderInfo     
 FROM   CUSTOMERMASTER(nolock) CM    
 INNER JOIN  KFTC_CUSTOMER_MASTER(nolock) KCM  ON    CM.customerId = KCM.customerId    
 --AND   CM.idNumber = KCM.accHolderInfo    
 WHERE   CM.customerId=@customerId    
END     
ELSE IF @flag = 'GET-KFTC-LOG-ID'          
BEGIN        
 SELECT '0' ErrorCode,'Success',s.RowId AS id   
 FROM dbo.KFTC_CUSTOMER_SUB s(NOLOCK)   
 INNER JOIN dbo.KFTC_CUSTOMER_MASTER m(NOLOCK) ON s.customerId=m.customerId  
 WHERE m.customerId=@customerId  AND s.accountNum=@accountNumber AND s.fintechUseNo=@fintechUseNo     
END   
ELSE IF @FLAG = 'ACCOUNTINFO'   -- 2019.10 계좌해지시 사용하기 위해 추가
BEGIN    
	SELECT	C.CustomerId,		C.AccessToken,	S.FintechUseNo,				S.AccountNum,				S.AccountName,
		C.AccHolderInfoType,C.AccHolderInfo,S.bankCodeStd AS BankCode,  S.inquiryAgreeYn,           S.transferAgreeYn 
	FROM KFTC_CUSTOMER_MASTER(NOLOCK) C  
	INNER JOIN KFTC_CUSTOMER_SUB S(NOLOCK) ON S.customerId = C.customerId  
	WHERE S.customerId = @CUSTOMERID AND S.fintechUseNo = @fintechUseNo  
END    
ELSE IF @flag = 's-refresh-v4'          
BEGIN       
 SELECT userSeqNo, userCi, ISNULL(userName,cm.fullName) AS userName, CONVERT(VARCHAR(10),ISNULL(userInfo,FORMAT(dob,'yyyyMMdd')),120)  AS userInfo
, userGender,          
  ISNULL(UserCellNo,cm.mobile) AS UserCellNo, ISNULL(cm.customerEmail,cm.email) AS UserEmail, PhoneCarrier = '' ,cm.bankAccountNo AS AccountNum
  ,kb.bankCode AS BankCodeStd      
 FROM dbo.customerMaster(NOLOCK) cm 
 LEFT JOIN  KFTC_CUSTOMER_MASTER (NOLOCK)  kcm ON cm.customerId=kcm.customerId
 INNER JOIN dbo.KoreanBankList (NOLOCK) kb ON kb.rowId=cm.bankName WHERE cm.customerId = @customerId          
END    
END TRY          
BEGIN CATCH          
    IF @@TRANCOUNT <> 0          
        ROLLBACK TRANSACTION;          
            
    DECLARE @errorMessage VARCHAR(MAX);          
    SET @errorMessage = ERROR_MESSAGE();          
           
    EXEC proc_errorHandler 1, @errorMessage, @userEmail;          
           
END CATCH; 