DROP PROCEDURE IF EXISTS uspFollowUpEszkoz
GO

CREATE PROCEDURE uspFollowUpEszkoz
   @intezmenyId int
  ,@aktTanevId int
  ,@kovetkezoTanevId int
  ,@eszkozId 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
     @kovEszkozId int = (SELECT TOP 1 ID FROM T_ESZKOZ_OSSZES WHERE ELOZOTANEVIREKORDID = @eszkozId)
    ,@eszkozNev nvarchar(200)= (SELECT C_NEV FROM T_ESZKOZ_OSSZES WHERE ID = @eszkozId)
    ,@aktTipusId int = (SELECT TOP 1 C_TIPUS FROM T_ESZKOZ_OSSZES WHERE ID = @eszkozId)
    ,@insertColList nvarchar(max)
    ,@selectColList nvarchar(max)
    ,@updateColList nvarchar(max)
    ,@sql nvarchar(max)
  
  CREATE TABLE #columnMapping (
     insertColName nvarchar(max) COLLATE DATABASE_DEFAULT
    ,selectColName nvarchar(max)
    ,updateColName nvarchar(max)
  )
  INSERT INTO #columnMapping (
     insertColName 
    ,selectColName 
    ,updateColName 
  ) SELECT
     insertColName 
    ,selectColName 
    ,updateColName 
  FROM fnGetFollowUpColumnMapping (NULL,'T_ESZKOZ')

  IF (SELECT TOROLT FROM T_ESZKOZ_OSSZES WHERE ID = @eszkozId) = 'T' OR (SELECT TOROLT FROM T_ESZKOZ_OSSZES WHERE ID = @kovEszkozId) = 'T' 
  BEGIN
    RETURN
  END

  IF (@aktTipusId > 100000)
  BEGIN
    SET @aktTipusId = (SELECT TOP 1 ID FROM T_DICTIONARYITEMBASE_OSSZES WHERE ELOZOTANEVIREKORDID = @aktTipusId AND C_INTEZMENYID = @intezmenyId AND C_TANEVID = @kovetkezoTanevId)
  END

  IF (@kovEszkozId IS NULL AND NOT EXISTS(SELECT 1 FROM T_ESZKOZ_OSSZES WHERE C_NEV=@eszkozNev AND C_TANEVID=@kovetkezoTanevId AND TOROLT='F')) BEGIN
    SELECT @insertColList = ISNULL(@insertColList+','+insertColname,insertColname) FROM #columnMapping 
    SELECT @selectColList = ISNULL(@selectColList+','+selectColName,selectColName) FROM #columnMapping 
    SET @sql = N'
    INSERT INTO T_ESZKOZ_OSSZES (
      '+@insertColList+'
    ) SELECT 
      '+@selectColList+'
    FROM T_ESZKOZ_OSSZES s
      INNER JOIN T_TEREM_OSSZES te ON te.ELOZOTANEVIREKORDID=s.C_TEREMID AND te.TOROLT=''F''
      LEFT JOIN T_FELHASZNALO_OSSZES fh ON fh.ELOZOTANEVIREKORDID = s.C_FELELOSID
    WHERE s.ID=@EszkozId'
  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_ESZKOZ_OSSZES t
        INNER JOIN T_ESZKOZ_OSSZES s ON s.ID = @eszkozId AND s.ID=t.ELOZOTANEVIREKORDID
        LEFT JOIN T_FELHASZNALO_OSSZES fh ON fh.ELOZOTANEVIREKORDID = s.C_FELELOSID
        INNER JOIN T_TEREM_OSSZES te ON te.ELOZOTANEVIREKORDID=s.C_TEREMID AND te.TOROLT=''F'''
  END
  
  --PRINT @sql
  EXEC sp_executesql 
     @sql,N'
     @intezmenyId int
    ,@aktTanevId int
    ,@kovetkezoTanevId int
    ,@eszkozId int
    ,@aktTipusId int'
    ,@intezmenyId = @intezmenyId 
    ,@aktTanevId = @aktTanevId 
    ,@kovetkezoTanevId = @kovetkezoTanevId 
    ,@eszkozId = @eszkozId 
    ,@aktTipusId = @aktTipusId 

END
GO
