DROP PROCEDURE IF EXISTS [dbo].[uspGetTanuloAktualisanTanultTantargyai]
GO

CREATE PROCEDURE [dbo].[uspGetTanuloAktualisanTanultTantargyai]
    @tanuloId int
   ,@magatartas nvarchar(max)
   ,@szorgalom nvarchar(max)
   ,@tantargyIds nvarchar (max)
   ,@oktatasiNevelesiFeladatId int = null
AS
SET NOCOUNT ON;

SELECT  
  CASE
        WHEN ertekeles.C_TANTARGYID IS NULL AND ertekeles.IsMagatartas > '' AND ertekeles.IsSzorgalom > '' THEN CONCAT(@magatartas, ', ', @szorgalom)
        WHEN ertekeles.C_TANTARGYID IS NULL AND ertekeles.IsMagatartas > '' THEN @magatartas
        WHEN ertekeles.C_TANTARGYID IS NULL AND ertekeles.IsSzorgalom > '' THEN @szorgalom
    WHEN ertekeles.C_TANTARGYID IS NULL THEN CAST(foglalkozas.C_TANTARGYID AS NVARCHAR(max))
        ELSE CAST(ertekeles.C_TANTARGYID AS NVARCHAR(max)) 
    END AS TantargyUid,
    IIF(foglalkozas.C_TANTARGYID IS NOT NULL, 'T', 'F') AS TTFBol,
  IIF(ertekeles.C_TANTARGYID IS NOT NULL OR ertekeles.IsMagatartas > '' OR ertekeles.IsSzorgalom > '', 'T', 'F') AS ErtekelesBol,
    tantargy.C_NEV                 AS Nev,
  tantargy.C_TARGYKATEGORIA      AS TargykategoriaId,
    db.C_NAME             AS TargyKategoriaNev,
    tantargy.C_FOTARGYE            AS IsFotargy,
    tantargy.C_FOTARGYID           AS FotargyId,
    tantargy.C_SORSZAM             AS SorSzam,
    fotargy.C_TARGYKATEGORIA       AS FotargyKategoriaId,
    fotargy.C_NEV                  AS FotargyNev
FROM (
    SELECT DISTINCT
        tcs.C_TANULOID,
        f.C_TANTARGYID,
        tcs.C_TANEVID,
        tcs.C_INTEZMENYID
    FROM T_FOGLALKOZAS f
    INNER JOIN T_TANULOCSOPORT tcs on tcs.C_OSZTALYCSOPORTID = f.C_OSZTALYCSOPORTID
    INNER JOIN T_OSZTALYCSOPORT ocs ON ocs.ID = tcs.C_OSZTALYCSOPORTID
      AND ocs.TOROLT = 'F'
      AND ocs.C_TANEVID = tcs.C_TANEVID
    INNER JOIN T_FELADATELLATASIHELY feh ON ocs.C_FELADATELLATASIHELYID = feh.ID 
      AND feh.TOROLT = 'F'
      AND feh.C_TANEVID = tcs.C_TANEVID
    WHERE f.TOROLT = 'F'
        AND tcs.TOROLT = 'F'
        AND GETDATE() >= tcs.C_BELEPESDATUM
        AND (tcs.C_KILEPESDATUM IS NULL OR GETDATE() <= tcs.C_KILEPESDATUM) 
        AND (@oktatasiNevelesiFeladatId IS NULL OR feh.C_OKTATASINEVELESIFELADATTIPUS = @oktatasiNevelesiFeladatId) 
    AND (@tantargyIds IS NULL OR f.C_TANTARGYID IN (SELECT value FROM STRING_SPLIT(@tantargyIds, ',')))
    AND tcs.C_TANULOID = @tanuloId 
) foglalkozas
FULL JOIN (
    SELECT DISTINCT
        te.C_TANULOID,
    te.C_TANTARGYID,
    CONCAT(te.C_MAGATARTASERTEKID, te.C_MAGATARTASOSZTALYZATID, te.C_MAGATARTASSZOVEG, te.C_MAGATARTASSZOVEGFORMAZOTT) AS IsMagatartas,
    CONCAT(te.C_SZORGALOMERTEKID, te.C_SZORGALOMOSZTALYZATID, te.C_SZORGALOMSZOVEG, te.C_SZORGALOMSZOVEGFORMAZOTT) AS IsSzorgalom,
    te.C_TANEVID,
    te.C_INTEZMENYID
    FROM T_TANULOERTEKELES te
    INNER JOIN T_ERTEKELESTIPUS ertip ON ertip.ID = te.C_TIPUSID AND ertip.TOROLT = 'F' AND ertip.C_ALTANEVID = te.C_TANEVID
    WHERE te.TOROLT = 'F'
  AND ertip.C_ISEGYSZERADHATO = 'F'
  AND (@tantargyIds IS NULL OR te.C_TANTARGYID IN (SELECT value FROM STRING_SPLIT(@tantargyIds, ',')))
  AND te.C_TANULOID = @tanuloId 
) ertekeles                       ON  foglalkozas.C_TANULOID = ertekeles.C_TANULOID 
                                  AND foglalkozas.C_TANTARGYID = ertekeles.C_TANTARGYID 
                                  AND foglalkozas.C_TANEVID = ertekeles.C_TANEVID
                                  AND foglalkozas.C_INTEZMENYID = foglalkozas.C_INTEZMENYID

INNER JOIN T_TANTARGY tantargy     ON  COALESCE(foglalkozas.C_TANTARGYID, ertekeles.C_TANTARGYID) = tantargy.ID
                    AND COALESCE(foglalkozas.C_TANEVID, ertekeles.C_TANEVID) = tantargy.C_TANEVID
                    AND COALESCE(foglalkozas.C_INTEZMENYID, ertekeles.C_INTEZMENYID) = tantargy.C_INTEZMENYID

LEFT JOIN T_TANTARGY fotargy      ON  tantargy.C_FOTARGYID = fotargy.ID
                                  AND tantargy.C_TANEVID = fotargy.C_TANEVID
                    AND tantargy.C_INTEZMENYID = fotargy.C_INTEZMENYID

LEFT JOIN T_DICTIONARYITEMBASE db ON  tantargy.C_TARGYKATEGORIA = db.ID
                  AND tantargy.C_TANEVID = db.C_TANEVID
                  AND tantargy.C_INTEZMENYID = db.C_INTEZMENYID

ORDER BY 1, 2
GO
