DROP PROCEDURE IF EXISTS uspFollowUpTerem
GO

CREATE PROCEDURE uspFollowUpTerem
   @intezmenyId int
  ,@aktTanevId int
  ,@kovetkezoTanevId int
  ,@teremId int
AS
BEGIN
  SET NOCOUNT ON;
  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(128) COLLATE DATABASE_DEFAULT
    ,selectColName nvarchar(128)
    ,updateColName nvarchar(128)
  )
  INSERT INTO #columnMapping (
     insertColName 
    ,selectColName 
    ,updateColName 
  ) SELECT
     insertColName 
    ,selectColName 
    ,updateColName 
  FROM fnGetFollowUpColumnMapping (NULL,'T_TEREM')

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