﻿DROP PROCEDURE IF EXISTS dbo.uspInsertOptionsIntoSystemSetting
GO

CREATE PROCEDURE dbo.uspInsertOptionsIntoSystemSetting 
   @pPozicio int
  ,@pBeallitasTipus int
  ,@pBeszurandok nvarchar(max)
  ,@pTanevId int = NULL
  ,@pDebugInfo bit = 0
AS
BEGIN
  SET NOCOUNT ON;

  DECLARE @tanevId int
  CREATE TABLE #options ([key] int, [value] nvarchar(max), [type] int)
  CREATE TABLE #newoptions ([key] int, [value] nvarchar(max), [type] int)

  INSERT INTO #newoptions
  SElECT * from OPENJSON(@pBeszurandok, '$')

  IF @pTanevId IS NOT NULL
  BEGIN
    DECLARE tanevCursor CURSOR 
      FOR SELECT ID FROM T_TANEV WHERE ID = @pTanevId AND TOROLT = 'F'
  END
  ELSE
  BEGIN
    DECLARE tanevCursor CURSOR 
      FOR SELECT ID FROM T_TANEV WHERE TOROLT = 'F'
  END

  OPEN tanevCursor
  FETCH NEXT FROM tanevCursor INTO @tanevId
  WHILE @@FETCH_STATUS = 0
  BEGIN
    DECLARE @val nvarchar(max) = (SELECT C_ALAPERTEK FROM T_RENDSZERBEALLITAS WHERE C_TANEVID = @tanevId AND C_BEALLITASTIPUS = @pBeallitasTipus AND TOROLT = 'F')
    DECLARE @result nvarchar(MAX)

    INSERT INTO #options
    SELECT * FROM OPENJSON(@val, '$.Options')
    
    SET @result = JSON_MODIFY(@val, '$.Options', JSON_QUERY('[]'))

    SELECT @result = JSON_MODIFY(@result, 'append $.Options', JSON_QUERY([value]))
    FROM #options
    WHERE [key]<@pPozicio

    SELECT @result = JSON_MODIFY(@result, 'append $.Options', JSON_QUERY(n.[value]))
    FROM #newoptions n
      LEFT JOIN #options o ON o.value = n.value
    WHERE o.[key] IS NULL

    SELECT @result = JSON_MODIFY(@result, 'append $.Options', JSON_QUERY([value]))
    FROM #options
    WHERE [key]>=@pPozicio
    
    IF @pDebugInfo = 1 
    BEGIN
      SELECT '----------- Alapérték: ' + CONVERT(nvarchar(10), @tanevId)
      SELECT @val
      SELECT @result
    END

    UPDATE T_RENDSZERBEALLITAS 
    SET C_ALAPERTEK=@result
      ,SERIAL = SERIAL + 1
      ,LASTCHANGED = GETDATE()
      ,MODIFIER = 0
    WHERE C_TANEVID = @tanevId 
      AND C_BEALLITASTIPUS = @pBeallitasTipus 
      AND TOROLT = 'F'

    TRUNCATE TABLE #options

    SET @val = (SELECT C_ERTEK FROM T_RENDSZERBEALLITAS WHERE C_TANEVID = @tanevId AND C_BEALLITASTIPUS = @pBeallitasTipus AND TOROLT = 'F')

    INSERT INTO #options
    SELECT * FROM OPENJSON(@val, '$.Options')
    
    SET @result = JSON_MODIFY(@val, '$.Options', JSON_QUERY('[]'))

    SELECT @result = JSON_MODIFY(@result, 'append $.Options', JSON_QUERY([value]))
    FROM #options
    WHERE [key]<@pPozicio

    SELECT @result = JSON_MODIFY(@result, 'append $.Options', JSON_QUERY(n.[value]))
    FROM #newoptions n
      LEFT JOIN #options o ON o.value = n.value
    WHERE o.[key] IS NULL

    SELECT @result = JSON_MODIFY(@result, 'append $.Options', JSON_QUERY([value]))
    FROM #options
    WHERE [key]>=@pPozicio

    IF @pDebugInfo = 1
    BEGIN
      SELECT '----------- Érték: ' + CONVERT(nvarchar(10), @tanevId)
      SELECT @val
      SELECT @result
    END

    UPDATE T_RENDSZERBEALLITAS 
    SET C_ERTEK=@result 
      ,SERIAL = SERIAL + 1
      ,LASTCHANGED = GETDATE()
      ,MODIFIER = 0
    WHERE C_TANEVID = @tanevId 
      AND C_BEALLITASTIPUS = @pBeallitasTipus 
      AND TOROLT = 'F'

    TRUNCATE TABLE #options

    FETCH NEXT FROM tanevCursor INTO @tanevId
  END
  CLOSE tanevCursor
  DEALLOCATE tanevCursor
END
GO
