﻿DROP PROCEDURE IF EXISTS uspFollowUpTanuloFelmentes
GO

CREATE PROCEDURE uspFollowUpTanuloFelmentes
   @intezmenyId int
  ,@aktTanevId int
  ,@kovetkezoTanevId int
  ,@felmentesId 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 @kovFelmentesId int = (SELECT ID FROM T_TANULOMENTESSEG_OSSZES WHERE ELOZOTANEVIREKORDID=@felmentesId AND TOROLT='F')
  DECLARE @elsoNap datetime = (select C_KEZDONAP from T_TANEV_OSSZES WHERE ID = @kovetkezoTanevId)
  DECLARE 
     @selectColList nvarchar(max)
    ,@insertColList nvarchar(max)
    ,@updateColList nvarchar(max)
    ,@sql nvarchar(max)

  CREATE TABLE #ColumnMapping (
     selectColName nvarchar(max)
    ,insertColName nvarchar(max)
    ,updateColName nvarchar(max)
  )
  INSERT INTO #ColumnMapping (
     selectColName
    ,insertColName
    ,updateColName
  ) SELECT
     selectColName
    ,insertColName
    ,updateColName
  FROM fnGetFollowUpColumnMapping (NULL,'T_TANULOMENTESSEG')

  IF (SELECT TOROLT FROM T_TANULOMENTESSEG_OSSZES WHERE ID = @felmentesId)='T' BEGIN
    RETURN
  END
  
  IF @kovFelmentesId 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_TANULOMENTESSEG_OSSZES (
      '+@insertColList+'
    ) SELECT
      '+@selectColList+'
    FROM T_TANULOMENTESSEG_OSSZES s
      INNER JOIN T_FELHASZNALO_OSSZES t ON t.ID = s.C_TANULOID
      INNER JOIN T_FELHASZNALO_OSSZES tKov ON tKov.C_OKTATASIAZONOSITO = t.C_OKTATASIAZONOSITO AND tKov.C_TANEVID = @kovetkezoTanevId AND tKov.TOROLT=''F''
      INNER JOIN T_TANTARGY_OSSZES tgy ON tgy.ELOZOTANEVIREKORDID=s.C_TANTARGYID AND tgy.TOROLT=''F''
    WHERE s.ID=@felmentesId AND ISNULL(s.C_VEGE,''2200-01-01'')>@elsoNap'
    END
  ELSE BEGIN
    IF (SELECT C_VEGE FROM T_TANULOMENTESSEG_OSSZES WHERE ID = @felmentesId)<=@elsoNap BEGIN
      UPDATE T_TANULOMENTESSEG_OSSZES SET 
         TOROLT='T' 
      WHERE ELOZOTANEVIREKORDID=@felmentesId AND TOROLT='F'
    END
    ELSE BEGIN
      SELECT 
        @updateColList = ISNULL(@updateColList+','+insertColName+' = '+updateColname,insertColName+' = '+updateColname)
      FROM #columnMapping WHERE updateColName IS NOT NULL
    END
      SET @sql = N'
      UPDATE t SET
        '+@updateColList+'
      FROM T_TANULOMENTESSEG_OSSZES t
        INNER JOIN T_TANULOMENTESSEG_OSSZES s ON s.ID=t.ELOZOTANEVIREKORDID AND s.TOROLT=''F'' AND s.ID=@felmentesId
        INNER JOIN T_TANTARGY_OSSZES tgy ON tgy.ELOZOTANEVIREKORDID=s.C_TANTARGYID AND tgy.TOROLT=''F'''
  END

  --PRINT @sql
  exec sp_executesql 
     @sql,N'
     @intezmenyId int
    ,@aktTanevId int
    ,@kovetkezoTanevId int
    ,@felmentesId int
    ,@kovFelmentesId int
    ,@elsoNap datetime'
    ,@intezmenyId = @intezmenyId 
    ,@aktTanevId = @aktTanevId 
    ,@kovetkezoTanevId = @kovetkezoTanevId 
    ,@felmentesId = @felmentesId 
    ,@kovFelmentesId = @kovFelmentesId
    ,@elsoNap = @elsoNap

END
GO
