DROP PROCEDURE IF EXISTS uspFollowUpTerem
GO

CREATE PROCEDURE uspFollowUpTerem
   @intezmenyId int
  ,@aktTanevId int
  ,@kovetkezoTanevId int
  ,@teremId 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
     @kovTeremId int
    ,@teremNev nvarchar(200)=(SELECT C_NEV FROM T_TEREM_OSSZES WHERE ID = @teremId)
    ,@insertColList nvarchar(max)
    ,@selectColList nvarchar(max)
    ,@updateColList nvarchar(max)
    ,@sql nvarchar(max)

  DECLARE @felelosId int = (
  SELECT fh.ID FROM T_FELHASZNALO_OSSZES fh
    INNER JOIN T_TEREM_OSSZES t ON t.C_TEREMFELELOSID=fh.ELOZOTANEVIREKORDID AND t.TOROLT='F' AND t.ID=@teremId
  WHERE fh.TOROLT='F')

  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_TEREM')

  UPDATE #columnMapping SET selectColName = 'sz.ID', updateColName = 'sz.ID' where insertColName = 'C_SZERVEZETID'

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

  SELECT @kovTeremId=ID FROM T_TEREM_OSSZES WHERE ELOZOTANEVIREKORDID = @teremId AND TOROLT='F' AND C_TANEVID=@kovetkezoTanevId
  IF (SELECT TOROLT FROM T_TEREM_OSSZES WHERE ID = @teremId) = 'T' OR (SELECT TOROLT FROM T_TEREM_OSSZES WHERE ID = @kovTeremId) = 'T' 
  BEGIN
    RETURN
  END

  IF (@kovTeremId 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_TEREM_OSSZES (
      ' + @insertColList + '
    ) SELECT 
       ' + @selectColList + '
    FROM T_TEREM_OSSZES s 
      INNER JOIN T_MUKODESIHELY_OSSZES mh ON mh.ELOZOTANEVIREKORDID = s.C_MUKODESIHELYID AND mh.TOROLT = ''F''
      LEFT JOIN T_SZERVEZET_OSSZES sz ON sz.ELOZOTANEVIREKORDID = s.C_SZERVEZETID AND sz.TOROLT = ''F''
    WHERE s.ID = @teremId
    AND s.TOROLT = ''F'''
  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_TEREM_OSSZES s 
      INNER JOIN T_TEREM_OSSZES t ON t.ELOZOTANEVIREKORDID = s.ID AND t.TOROLT=''F''
      INNER JOIN T_MUKODESIHELY_OSSZES mh ON mh.ELOZOTANEVIREKORDID = s.C_MUKODESIHELYID AND mh.TOROLT = ''F''
      LEFT JOIN T_SZERVEZET_OSSZES sz ON sz.ELOZOTANEVIREKORDID = s.C_SZERVEZETID AND sz.TOROLT = ''F''
    WHERE s.ID = @teremId
    AND s.TOROLT=''F'''
  END
  
  --PRINT @sql
  exec sp_executesql
     @sql,N'
     @intezmenyId int
    ,@aktTanevId int
    ,@kovetkezoTanevId int
    ,@teremId int
    ,@felelosId int'
    ,@intezmenyId = @intezmenyId
    ,@aktTanevId = @aktTanevId
    ,@kovetkezoTanevId = @kovetkezoTanevId 
    ,@teremId = @teremId 
    ,@felelosId = @felelosId 

END
GO