DROP PROCEDURE IF EXISTS uspFollowUpTantargy
GO

CREATE PROCEDURE uspFollowUpTantargy
   @intezmenyId int
  ,@aktTanevId int
  ,@kovetkezoTanevId int
  ,@tantargyId int
AS
BEGIN
  SET NOCOUNT ON;
  DECLARE
	   @kovetkezoTantargyId int
    ,@tantargyNev nvarchar(510)=(SELECT C_NEV FROM T_TANTARGY_OSSZES WHERE ID = @tantargyId)
    ,@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_TANTARGY')

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

  SELECT  @kovetkezoTantargyId = ID FROM T_TANTARGY_OSSZES WHERE ELOZOTANEVIREKORDID = @tantargyId AND C_TANEVID=@kovetkezoTanevId

	IF (SELECT TOROLT FROM T_TANTARGY_OSSZES WHERE ID = @tantargyId) = 'T' OR (SELECT TOROLT FROM T_TANTARGY_OSSZES WHERE ID = @kovetkezoTantargyId) = 'T' BEGIN
		RETURN
	END

  IF (@kovetkezoTantargyId 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_TANTARGY_OSSZES (
  	  	 '+@insertColList+'
      ) SELECT 
  	  	 '+@selectColList+'
      FROM T_TANTARGY_OSSZES s
        LEFT JOIN T_TANTARGY_OSSZES ftgy ON ftgy.ELOZOTANEVIREKORDID = s.C_FOTARGYID AND ftgy.TOROLT=''F'' AND ftgy.C_TANEVID = @kovetkezoTanevId
        INNER JOIN T_TARGYKATEGORIATIPUS_OSSZES tkt ON tkt.ELOZOTANEVIREKORDID=s.C_TARGYKATEGORIA AND tkt.TOROLT=''F'' AND tkt.C_ALTANEVID=@kovetkezoTanevId
      WHERE s.ID=@tantargyId'
        
  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_TANTARGY_OSSZES t
  			INNER JOIN T_TANTARGY_OSSZES s ON s.ID = @tantargyId AND s.ID=t.ELOZOTANEVIREKORDID
        LEFT JOIN T_TANTARGY_OSSZES ftgy ON ftgy.ELOZOTANEVIREKORDID = s.C_FOTARGYID AND ftgy.TOROLT=''F'' AND ftgy.C_TANEVID = @kovetkezoTanevId
        INNER JOIN T_TARGYKATEGORIATIPUS_OSSZES tkt ON tkt.ELOZOTANEVIREKORDID=s.C_TARGYKATEGORIA AND tkt.TOROLT=''F'' AND tkt.C_ALTANEVID=@kovetkezoTanevId'
    END

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

END
GO