﻿-- ===================================================================
-- Author: Telek Ákos
-- Created: 2018.06.21.
-- Description: 
--   Dictionary elemek FollowUp-ja
-- ===================================================================
DROP PROCEDURE IF EXISTS uspFollowUpDictionaryItemBase 
GO

CREATE PROCEDURE uspFollowUpDictionaryItemBase
   @pItemId int
  ,@pCurrentTanevId int
  ,@kovetkezoTanevId int
  ,@IntezmenyId int
AS 
BEGIN
  SET NOCOUNT ON;
  DECLARE 
     @typeName nvarchar(64)
    ,@sql nvarchar(max)
    ,@kovItemId int
    ,@isItemTorolt char(1)
    ,@isKovItemTorolt char(1)
    ,@columnList nvarchar(max)
    ,@columnListInsert nvarchar(max)
    ,@itemName nvarchar(500)
    ,@insertColList nvarchar(max)
    ,@selectColList nvarchar(max)
    ,@updateColList nvarchar(max)
  
  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_DICTIONARYITEMBASE')

  SELECT 
     @typeName = d.C_TYPE
    ,@isItemTorolt = d.TOROLT
    ,@itemName = C_NAME
  FROM T_DICTIONARYITEMBASE_OSSZES d
  WHERE d.ID = @pItemId 
    AND d.C_INTEZMENYID = @IntezmenyId 
    AND d.C_TANEVID = @pCurrentTanevId
  
  SELECT 
     @kovItemId = d.ID
    ,@isKovItemTorolt = d.TOROLT
  FROM T_DICTIONARYITEMBASE_OSSZES d
  WHERE d.C_INTEZMENYID = @IntezmenyId
    AND d.C_TANEVID = @kovetkezoTanevId
    AND d.ELOZOTANEVIREKORDID = @pItemId

  IF EXISTS(SELECT 1 FROM T_DICTIONARYITEMBASE_OSSZES WHERE C_TYPE=@typeName AND C_NAME=@itemName AND C_TANEVID=@kovetkezoTanevId AND ISNULL(ELOZOTANEVIREKORDID,0)<>@pItemId AND TOROLT='F') BEGIN
    UPDATE T_DICTIONARYITEMBASE_OSSZES SET ELOZOTANEVIREKORDID = NULL WHERE ELOZOTANEVIREKORDID = @pItemId AND C_TANEVID=@kovetkezoTanevId AND TOROLT='F'
    UPDATE T_DICTIONARYITEMBASE_OSSZES SET ELOZOTANEVIREKORDID = @pItemId WHERE C_NAME=@itemName AND C_TANEVID = @kovetkezoTanevId AND TOROLT='F' AND C_TYPE=@typeName
    SELECT 
       @kovItemId = d.ID
      ,@isKovItemTorolt = d.TOROLT
    FROM T_DICTIONARYITEMBASE_OSSZES d
    WHERE d.C_INTEZMENYID = @IntezmenyId AND d.C_TANEVID = @kovetkezoTanevId AND d.ELOZOTANEVIREKORDID = @pItemId

    SET @sql=N'
      UPDATE T_'+@typeName+'_OSSZES SET ELOZOTANEVIREKORDID = NULL WHERE ELOZOTANEVIREKORDID = @pItemId AND C_ALTANEVID=@kovetkezoTanevId AND TOROLT=''F''
      UPDATE T_'+@typeName+'_OSSZES SET ELOZOTANEVIREKORDID = @pItemId, ELOZOTANEVIREKORDIDA = @pItemId WHERE ID = @kovItemId AND C_ALTANEVID=@kovetkezoTanevId AND TOROLT=''F''' 
    EXEC sp_Executesql @sql,N'@pItemId int,@kovItemId int,@kovetkezoTanevId int',@kovetkezoTanevId = @kovetkezoTanevId,@pItemId = @pItemId,@kovItemId = @kovItemId
  END
    
  IF @isItemTorolt = 'T' OR @isKovItemTorolt = 'T' BEGIN
    RETURN
  END  
  
  IF @kovItemId 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_DICTIONARYITEMBASE (
       '+@insertColList+'
    ) SELECT 
       '+@selectColList+'
    FROM T_DICTIONARYITEMBASE_OSSZES s
    WHERE s.ID = @pItemId 
      AND s.C_TANEVID = @pCurrentTanevId
      
    SET @kovItemId = SCOPE_IDENTITY()'
    
    SELECT @columnList = ISNULL(@columnList + ', ', '') + c.name 
    FROM sys.columns c
      INNER JOIN sys.tables t ON t.object_id = c.object_id AND t.name = 'T_' + @typeName AND t.schema_id = 1
    ORDER BY c.column_id
    SET @columnListInsert = REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(STUFF(@columnList, 1, 2, '@kovItemId'), 'CREATOR', 'MODIFIER'), 'C_ALTANEVID', '@kovetkezoTanevId'), 'ELOZOTANEVIREKORDIDA', 'ID'), 'ELOZOTANEVIREKORDID', 'ID'), 'SERIAL', '0'),'CREATED','LASTCHANGED')
    
    SET @sql += N'
      INSERT INTO T_' + @typeName + ' (' + @columnList + ')
      SELECT ' + @columnListInsert + '
      FROM T_' + @typeName + '_OSSZES
      WHERE ID = @pItemId 
        AND C_ALTANEVID = @pCurrentTanevId'
  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_DICTIONARYITEMBASE_OSSZES t
    INNER JOIN T_DICTIONARYITEMBASE_OSSZES s ON s.ID = t.ELOZOTANEVIREKORDID
    WHERE s.ID = @pItemId
      AND t.ID = @kovItemId
      AND s.C_TANEVID = @pCurrentTanevId
      AND t.C_TANEVID = @kovetkezoTanevId'
    
    SET @columnList = ''
    SELECT @columnList += 'kov.' + c.name + ' = akt.' + c.name + ', '
    FROM sys.columns c
      INNER JOIN sys.tables t ON t.object_id = c.object_id AND t.name = 'T_' + @typeName AND t.schema_id = 1
    WHERE c.name NOT IN ('ID', 'C_ALINTEZMENYID', 'C_ALTANEVID', 'TOROLT', 'ELOZOTANEVIREKORDID', 'ELOZOTANEVIREKORDIDA', 'NNID', 'LASTCHANGED', 'CREATOR', 'CREATED')
    
    SET @columnList += 'kov.LASTCHANGED = akt.LASTCHANGED'
    SET @columnList = REPLACE(@columnList,'akt.SERIAL','kov.SERIAL+1')
    
    SET @sql += N'
      UPDATE kov SET
        ' + @columnList + '
      FROM T_' + @typeName + '_OSSZES kov
      INNER JOIN T_' + @typeName + '_OSSZES akt ON akt.ID = kov.ELOZOTANEVIREKORDID
      WHERE akt.ID = @pItemId
        AND kov.ID = @kovItemId
        AND akt.C_ALTANEVID = @pCurrentTanevId
        AND kov.C_ALTANEVID = @kovetkezoTanevId'
  END
  
  --PRINT @sql
  EXEC sys.sp_executesql @sql,N'
     @kovItemId int
    ,@pItemId int
    ,@pCurrentTanevId int
    ,@kovetkezoTanevId int
    ,@IntezmenyId int'
    ,@kovItemId = @kovItemId
    ,@pItemId = @pItemId
    ,@pCurrentTanevId = @pCurrentTanevId
    ,@kovetkezoTanevId = @kovetkezoTanevId
    ,@IntezmenyId = @IntezmenyId

END
GO
