DROP PROCEDURE IF EXISTS uspFollowUpOraterv
GO

CREATE PROCEDURE uspFollowUpOraterv
   @intezmenyId int
  ,@aktTanevId int
  ,@kovetkezoTanevId int
  ,@oratervId int
AS
BEGIN
  SET NOCOUNT ON;

  IF NOT EXISTS (SELECT 1 from T_TANEV where ID = @kovetkezoTanevId and C_KOVETKEZO = 'T' and C_AKTIVALVA is null and C_ISKIVALASZTHATO = 'T')
    BEGIN
      RETURN;
    END

  DECLARE
     @kovOratervId int 
    ,@oraTervNev nvarchar(510)=(SELECT C_NEV FROM T_ORATERV_OSSZES WHERE ID = @oratervId)
    ,@sql nvarchar(max)
    ,@insertColList nvarchar(max)
    ,@selectColList nvarchar(max)
    ,@updateColList nvarchar(max)

  CREATE TABLE #columnMapping(
     insertColName nvarchar(max)
    ,selectColName nvarchar(max)
    ,updateColName nvarchar(max)
  )

  IF EXISTS(SELECT 1 FROM T_ORATERV_OSSZES WHERE TOROLT='F' AND C_TANEVID=@kovetkezoTanevId AND C_NEV=@oraTervNev AND ISNULL(ELOZOTANEVIREKORDID,0)<>@oratervId)
  BEGIN
    UPDATE T_ORATERV_OSSZES SET ELOZOTANEVIREKORDID = NULL WHERE ELOZOTANEVIREKORDID=@oratervId
    UPDATE T_ORATERV_OSSZES SET ELOZOTANEVIREKORDID = @oratervId WHERE C_NEV=@oraTervNev AND TOROLT='F' AND C_TANEVID=@kovetkezoTanevId AND ISNULL(ELOZOTANEVIREKORDID,0)<>@oratervId
  END

  SELECT @kovOratervId = ID FROM T_ORATERV_OSSZES WHERE ELOZOTANEVIREKORDID = @oratervId

  IF (SELECT TOROLT FROM T_ORATERV_OSSZES WHERE ID = @oratervId) = 'T' OR (SELECT TOROLT FROM T_ORATERV_OSSZES WHERE ID = @kovOratervId) = 'T' BEGIN
    RETURN
  END
  
  INSERT INTO #columnMapping(
     insertColName 
    ,selectColName 
    ,updateColName 
  )
  SELECT 
     insertColName 
    ,selectColName 
    ,updateColName 
  FROM fnGetFollowUpColumnMapping (NULL,'T_ORATERV')

IF (@kovOratervId IS NULL) 
  BEGIN
  SELECT @insertColList = ISNULL(@insertColList+','+insertColname,insertColname) FROM #columnMapping 
  SELECT @selectColList = ISNULL(@selectColList+','+selectColName,selectColName) FROM #columnMapping 
  SET @sql = N'
    INSERT INTO T_ORATERV_OSSZES (
      '+@insertColList+'
    ) SELECT 
      '+@selectColList+'
    FROM T_ORATERV_OSSZES s 
      INNER JOIN T_TANTERV_OSSZES tr ON tr.ELOZOTANEVIREKORDID = s.C_TANTERVID AND tr.TOROLT = ''F'' AND tr.C_TANEVID = @kovetkezoTanevId
      INNER JOIN T_EVFOLYAMTIPUS_OSSZES e ON e.ELOZOTANEVIREKORDID = s.C_EVFOLYAM AND e.TOROLT = ''F'' AND e.C_ALTANEVID = @kovetkezoTanevId
    WHERE s.ID = @oratervId'
  END
ELSE
  BEGIN
    SELECT 
      @updateColList = ISNULL(@updateColList+','+insertColName+' = '+updateColname,insertColName+' = '+updateColname)
    FROM #columnMapping
    WHERE updateColname IS NOT NULL

    SET @sql = N' 
    UPDATE t SET
       '+@updateColList+'
    FROM T_ORATERV_OSSZES t
      INNER JOIN T_ORATERV_OSSZES s ON s.ID = @oratervId AND s.ID = t.ELOZOTANEVIREKORDID
      INNER JOIN T_EVFOLYAMTIPUS_OSSZES e ON e.ELOZOTANEVIREKORDID = s.C_EVFOLYAM AND e.TOROLT = ''F'' AND e.C_ALTANEVID = @kovetkezoTanevId
    WHERE t.TOROLT = ''F'' AND t.ID = @kovOratervId'
  END
  --PRINT @sql
  EXEC sp_executesql 
     @sql,N'
     @intezmenyId int
    ,@aktTanevId int
    ,@kovetkezoTanevId int
    ,@oratervId int
    ,@kovOratervId int'
    ,@intezmenyId = @intezmenyId
    ,@aktTanevId = @aktTanevId
    ,@kovetkezoTanevId = @kovetkezoTanevId 
    ,@oratervId = @oratervId 
    ,@kovOratervId = @kovOratervId

END
GO