USE [FastMoneyPro_Remit]
GO
/****** Object:  StoredProcedure [dbo].[PROC_POWERCALLHISTORY]    Script Date: 2019-12-09 오전 9:52:07 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		chris
-- Create date: "2019-10-18"
-- Description:	<Proc PowerCall history>
-- =============================================
-- EXEC [dbo].[procedure_test] @CUSTOMERID = '40154'


CREATE PROCEDURE [dbo].[PROC_POWERCALLHISTORY]  
	-- Add the parameters for the stored procedure here
  @FLAG   VARCHAR(50)  
 ,@methodName VARCHAR(200) = NULL
 ,@rowId   BIGINT   = NULL  
 ,@request NVARCHAR(MAX) = NULL  
 ,@requestPowerCall VARCHAR(MAX) = NULL
 ,@responsePowerCall NVARCHAR(MAX) = NULL
 ,@customerId BIGINT  = NULL  
 ,@errorCode  VARCHAR(10)  = NULL  
 ,@errorMessage NVARCHAR(200) = NULL  
 ,@processId  VARCHAR(20)  = NULL
 ,@orderId VARCHAR(30) = NULL
 ,@buyType VARCHAR(10) = NULL
 ,@chargeType VARCHAR(30) = NULL
 ,@cardName NVARCHAR(100) = NULL
 ,@productPrice money =NULL
 ,@phoneNo varchar(11) = NULL

AS
SET NOCOUNT ON;  
IF @FLAG = 'i'
BEGIN
	IF ISNULL(@processId,'') = ''
		SET @processId = Convert(varchar(10),Getdate(),112) + Replace(Convert(varchar(12),Getdate(),114),':','') + @customerId
	SET @orderId = 'gmesub_' + RIGHT(Convert(varchar(10),Getdate(),112),6) + Replace(Convert(varchar(12),Getdate(),114),':','') + CONVERT(varchar,@customerId)
	INSERT powercallHistory(buyType,cardName,chargeType,productPrice,phoneNo,methodName,customerId,request,requestTime,processId,orderId)
	SELECT @buyType,@cardName,@chargeType,@productPrice,@phoneNo,@methodName, @customerId,@request, GETDATE(), @processId,@orderId

	SET @ROWID = SCOPE_IDENTITY()
	SELECT '0' AS ErrorCode, 'Request Logged Successfully' AS Msg, @rowId AS Id,@processId AS Extra, @orderId AS Extra2
	RETURN
END
IF @FLAG = 'powerCallLog'
BEGIN
	UPDATE powercallHistory 
	SET
	requestPowerCall = @requestPowerCall
	,requestTime = GETDATE()
	WHERE rowId = @rowId
	SELECT '0' AS ErrorCode, 'Response Logged Successfully' AS Msg, @rowId AS Id 
	RETURN 
END
ELSE IF @FLAG = 'powerCallHistroy'
BEGIN
	UPDATE powercallHistory 
	SET
	responsePowerCall = @responsePowerCall
	,responesTime = GETDATE()
	,errorCode = @errorCode
	,errorMessage = @errorMessage
	WHERE rowId = @rowId
	SELECT '0' AS ErrorCode, 'Response Logged Successfully' AS Msg, @rowId AS Id 
	RETURN 
END
ELSE IF @FLAG = 'callBack'
BEGIN
	SELECT @customerId = customerId
	,@request = request
	FROM powerCallHistory
	WHERE orderId = @orderId
	
	UPDATE powerCallHistory 
	SET
	responsePowerCall = @responsePowerCall
	,responesTime = GETDATE()
	,errorCode = @errorCode
	,errorMessage = @errorMessage
	,resOrderId = @orderId
	WHERE customerId = @customerId AND orderId = @orderId
	SELECT '0' AS ErrorCode, 'Response Logged Successfully' AS Msg, @customerId AS Id , @request AS Extra
	RETURN
END