DROP PROCEDURE IF EXISTS uspFollowUpTanterv
GO

CREATE PROCEDURE uspFollowUpTanterv
   @intezmenyId int
  ,@aktTanevId int
  ,@kovetkezoTanevId int
  ,@tantervId int
AS
BEGIN
  SET NOCOUNT ON;
  DECLARE
  	 @kovTantervId int
    ,@tantervNev nvarchar(510)=(SELECT C_NEV FROM T_TANTERV_OSSZES WHERE ID = @tantervId)
    ,@insertColList nvarchar(max)
    ,@selectColList nvarchar(max)
    ,@updateColList nvarchar(max)
    ,@sql nvarchar(max)
  
  CREATE TABLE #columnMapping (
     insertColName nvarchar(128) COLLATE DATABASE_DEFAULT
    ,selectColName nvarchar(128)
    ,updateColName nvarchar(128)
  )
  INSERT INTO #columnMapping (
     insertColName 
    ,selectColName 
    ,updateColName 
  ) SELECT
     insertColName 
    ,selectColName 
    ,updateColName 
  FROM fnGetFollowUpColumnMapping (NULL,'T_TANTERV')

	IF EXISTS(SELECT 1 FROM T_TANTERV_OSSZES WHERE C_NEV=@tantervNev AND C_TANEVID=@kovetkezoTanevId AND TOROLT='F' AND ISNULL(ELOZOTANEVIREKORDID,0)<>@tantervId)
  BEGIN
    UPDATE T_TANTERV_OSSZES SET ELOZOTANEVIREKORDID=NULL WHERE ELOZOTANEVIREKORDID=@tantervId 
    UPDATE T_TANTERV_OSSZES SET ELOZOTANEVIREKORDID=@tantervId WHERE C_NEV=@tantervNev AND C_TANEVID=@kovetkezoTanevId AND TOROLT='F' AND ISNULL(ELOZOTANEVIREKORDID,0)<>@tantervId
  END
  
  SELECT @kovTantervId = ID FROM T_TANTERV_OSSZES WHERE ELOZOTANEVIREKORDID = @tantervId

  IF (SELECT TOROLT FROM T_TANTERV_OSSZES WHERE ID = @tantervId) = 'T' OR (SELECT TOROLT FROM T_TANTERV_OSSZES WHERE ID = @kovTantervId) = 'T' BEGIN
		RETURN
	END

  IF (@kovTantervId IS NULL) 
  BEGIN
    SELECT @insertColList = ISNULL(@insertColList+','+insertColname,insertColname) FROM #columnMapping 
    SELECT @selectColList = ISNULL(@selectColList+','+selectColName,selectColName) FROM #columnMapping 
    IF (SELECT C_VEDETT FROM T_TANTERV_OSSZES WHERE ID = @tantervId) = 'T' BEGIN
      UPDATE T_TANTERV_OSSZES 
        SET C_VEDETT = 'F' 
      WHERE TOROLT='F' AND C_TANEVID = @kovetkezoTanevId
    END
    
    SET @sql = N'
    INSERT INTO T_TANTERV_OSSZES (
  	  '+@insertColList+'
    ) SELECT 
  		 '+@selectColList+'
    FROM T_TANTERV_OSSZES s
    WHERE s.ID=@tantervId'
    
  END
  ELSE
  BEGIN
    SELECT 
      @updateColList = ISNULL(@updateColList+','+insertColName+' = '+updateColname,insertColName+' = '+updateColname)
    FROM #columnMapping
    WHERE updateColname IS NOT NULL

    IF (SELECT C_VEDETT FROM T_TANTERV_OSSZES WHERE ID = @tantervId) = 'T' BEGIN
      UPDATE T_TANTERV_OSSZES 
        SET C_VEDETT = 'F' 
      WHERE TOROLT='F' AND C_TANEVID = @kovetkezoTanevId
    END
    SET @sql = N'
  	UPDATE t SET
  		'+@updateColList+'
  	FROM T_TANTERV_OSSZES t
  		INNER JOIN T_TANTERV_OSSZES s ON s.ID=@tantervId AND s.ID=t.ELOZOTANEVIREKORDID'
  END

  --PRINT @sql
  EXEC sp_executesql 
     @sql,N'
     @intezmenyId int
    ,@aktTanevId int
    ,@kovetkezoTanevId int
    ,@tantervId int'
    ,@intezmenyId = @intezmenyId 
    ,@aktTanevId = @aktTanevId 
    ,@kovetkezoTanevId = @kovetkezoTanevId 
    ,@tantervId = @tantervId 

END
GO