﻿DROP PROCEDURE IF EXISTS uspFollowUpAlkalmazottVegzettseg
GO

CREATE PROCEDURE uspFollowUpAlkalmazottVegzettseg      
   @intezmenyId int      
  ,@aktTanevId int      
  ,@kovetkezoTanevId int      
  ,@vegzettsegId 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       
     @kovVegzettsegId int      
    ,@selectColListV nvarchar(max)      
    ,@insertColListV nvarchar(max)      
    ,@updateColListV nvarchar(max)      
    ,@selectColListKV nvarchar(max)      
    ,@insertColListKV nvarchar(max)      
    ,@updateColListKV nvarchar(max)      
    ,@sql nvarchar(max)      
      
  SET @kovVegzettsegId = (SELECT ID FROM T_VEGZETTSEG_OSSZES WHERE ELOZOTANEVIREKORDID = @vegzettsegId AND TOROLT = 'F' AND C_TANEVID = @kovetkezoTanevId)      
  IF (NOT EXISTS(SELECT 1 FROM T_VEGZETTSEG_OSSZES WHERE ID = @vegzettsegId AND TOROLT='F')) AND (NOT EXISTS(SELECT 1 FROM T_VEGZETTSEG_OSSZES WHERE ELOZOTANEVIREKORDID = @vegzettsegId AND TOROLT='F')) BEGIN      
    RETURN      
  END      
        
  CREATE TABLE #ColumnMapping (selectColName nvarchar(MAX),insertColName nvarchar(MAX),updateColName nvarchar(MAX),tableName nvarchar(MAX))      
  INSERT INTO #ColumnMapping(      
     selectColName      
    ,insertColname      
    ,updateColname      
    ,tableName       
  )      
  SELECT       
     selectColName      
    ,insertColName      
    ,updateColName       
    ,'T_VEGZETTSEG'      
  FROM fnGetFollowUpColumnMapping (NULL,'T_VEGZETTSEG')      
  UNION ALL      
  SELECT       
     selectColName      
    ,insertColName      
    ,updateColName       
    ,'T_KEPESITES_VEGZETTSEG'      
  FROM fnGetFollowUpColumnMapping ('ELOZOTANEVIREKORDID','T_KEPESITES_VEGZETTSEG')      
      
  IF @kovVegzettsegId IS NULL BEGIN      
    SELECT @insertColListV = ISNULL(@insertColListV+','+insertColname,insertColname) FROM #columnMapping WHERE tableName = 'T_VEGZETTSEG'      
    SELECT @selectColListV = ISNULL(@selectColListV+','+selectColName,selectColName) FROM #columnMapping WHERE tableName = 'T_VEGZETTSEG'      
    SELECT @insertColListKV = ISNULL(@insertColListKV+','+insertColname,insertColname) FROM #columnMapping WHERE tableName = 'T_KEPESITES_VEGZETTSEG'      
    SELECT @selectColListKV = ISNULL(@selectColListKV+','+selectColName,selectColName) FROM #columnMapping WHERE tableName = 'T_KEPESITES_VEGZETTSEG'      
          
    SET @sql = N'      
    INSERT INTO T_VEGZETTSEG_OSSZES(      
      '+@insertColListV+'      
    ) SELECT       
       '+@selectColListV+'      
    FROM T_VEGZETTSEG_OSSZES s       
      INNER JOIN T_FELHASZNALO_OSSZES fh ON fh.ELOZOTANEVIREKORDID=s.C_TANARID AND fh.TOROLT=''F''      
    WHERE s.ID=@vegzettsegId AND s.C_TANEVID = @aktTanevId AND fh.C_TANEVID = @kovetkezoTanevId
          
    INSERT INTO T_KEPESITES_VEGZETTSEG (      
      '+@insertColListKV+'      
    ) SELECT       
      '+@selectColListKV+'      
    FROM T_VEGZETTSEG_OSSZES s      
      INNER JOIN T_KEPESITES_VEGZETTSEG kv ON kv.C_VEGZETTSEGID=s.ID      
      INNER JOIN T_KEPESITES_OSSZES k ON k.ELOZOTANEVIREKORDID=kv.C_KEPESITESID AND k.TOROLT=''F''      
      INNER JOIN T_VEGZETTSEG_OSSZES vKov ON vKov.ELOZOTANEVIREKORDID=s.ID AND vKov.TOROLT=''F''
    WHERE NOT EXISTS (SELECT 1 FROM T_KEPESITES_VEGZETTSEG x WHERE x.C_VEGZETTSEGID = vkov.ID AND x.C_KEPESITESID = k.ID)
    AND s.C_TANEVID = @aktTanevId AND vkov.C_TANEVID = @kovetkezoTanevId AND k.C_TANEVID = @kovetkezoTanevId AND s.ID = @vegzettsegId'    
  END      
  ELSE BEGIN      
    SELECT       
      @updateColListV = ISNULL(@updateColListV+','+insertColname+' = '+updateColname,insertColName+' = '+updateColname)      
    FROM #columnMapping WHERE updateColName IS NOT NULL AND tableName = 'T_VEGZETTSEG'      
    SELECT @insertColListKV = ISNULL(@insertColListKV+','+insertColname,insertColname) FROM #columnMapping WHERE tableName = 'T_KEPESITES_VEGZETTSEG'      
    SELECT @selectColListKV = ISNULL(@selectColListKV+','+selectColName,selectColName) FROM #columnMapping WHERE tableName = 'T_KEPESITES_VEGZETTSEG'      
      
    DELETE kv      
    FROM T_KEPESITES_VEGZETTSEG kv       
      INNER JOIN T_KEPESITES_OSSZES kKov ON kKov.ID=kv.C_KEPESITESID AND kKov.TOROLT='F'
      INNER JOIN T_KEPESITES_OSSZES kOld ON kOld.ID=kKov.ELOZOTANEVIREKORDID AND kOld.TOROLT='F'      
      LEFT JOIN T_KEPESITES_VEGZETTSEG kvOld ON kvOld.C_VEGZETTSEGID=@vegzettsegId AND kvOld.C_KEPESITESID=kOld.ID      
    WHERE kv.C_VEGZETTSEGID=@kovVegzettsegId AND kvOld.C_KEPESITESID IS NULL      
      
    SET @sql =N'      
    UPDATE t SET      
      '+@updateColListV+'      
    FROM T_VEGZETTSEG_OSSZES s      
      INNER JOIN T_VEGZETTSEG_OSSZES t ON t.ELOZOTANEVIREKORDID = s.ID AND s.ID=@vegzettsegId
    WHERE s.C_TANEVID = @aktTanevId AND t.C_TANEVID = @kovetkezoTanevId     
        
 INSERT INTO T_KEPESITES_VEGZETTSEG (      
      '+@insertColListKV+'      
    ) SELECT       
      '+@selectColListKV+'      
    FROM T_VEGZETTSEG_OSSZES s      
      INNER JOIN T_KEPESITES_VEGZETTSEG kv ON kv.C_VEGZETTSEGID=s.ID    
      INNER JOIN T_KEPESITES_OSSZES k ON k.ELOZOTANEVIREKORDID=kv.C_KEPESITESID AND k.TOROLT=''F''
      INNER JOIN T_VEGZETTSEG_OSSZES vKov ON vKov.ELOZOTANEVIREKORDID=s.ID
        AND s.C_TANEVID = @aktTanevId AND vKov.C_TANEVID = @kovetkezoTanevId AND vKov.TOROLT=''F''  
    WHERE s.ID=@vegzettsegId AND k.C_TANEVID = @kovetkezoTanevId
      AND NOT EXISTS (SELECT 1 FROM T_KEPESITES_VEGZETTSEG WHERE C_VEGZETTSEGID=@kovVegzettsegId AND C_KEPESITESID=k.ID)'      
  END      
        
  exec sp_executesql      
     @sql,N'      
     @intezmenyId int      
    ,@aktTanevId int      
    ,@kovetkezoTanevId int      
    ,@vegzettsegId int      
    ,@kovVegzettsegId int'      
    ,@intezmenyId = @intezmenyId       
    ,@aktTanevId = @aktTanevId      
    ,@kovetkezoTanevId = @kovetkezoTanevId       
    ,@vegzettsegId = @vegzettsegId       
    ,@kovVegzettsegId = @kovVegzettsegId       
      
END
GO
