
ALTER PROCEDURE [dbo].[GetMachineFluxLog]
AS
BEGIN
    
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;
    
    BEGIN tran
    
    	-- dichiarazioni variabili x recupero dati
    	DECLARE @LastIdx	INT = 0
    	DECLARE @NewIdx		INT = 0
    		
    	-- recupero valore ultima riga stato processata...
    	SELECT	@LastIdx = ISNULL(LastIdx,0)
    	FROM	SyncState
    	WHERE	TableName = 'ToMachineFluxLog'
    
    	-- recupero nuovo MAX idx
    	SELECT	@NewIdx = ISNULL(MAX(ID),0)
    	FROM	syn_M2ERP_MACHINE_STATE
		
		-- eseguo SOLO SE ho dati aggiuntivi...
		IF(@NewIdx > @LastIdx)
		BEGIN
    		/* --------------------------------------------------
    		* Inviero solo VARIAZIONI dei valori
			* - CurrentPP (Part Program)
			* - ActiveProdOrder (Ordine in esecuzione)
			*
			* andando ad incrociare ogni record col precedente...
    		* -------------------------------------------------- */
    
			-- processo dati ORDINI
			;WITH cteCurrOrd AS
    		(
    			SELECT	ID
						,ActiveProdOrder
						,Targa as IdxMacchina
						,[DateTime] AS DtEvento
    			FROM	syn_M2ERP_MACHINE_STATE
    			WHERE	ID > @LastIdx AND ID <= @NewIdx
    		)
			, ctePrevOrd AS
    		(
    			SELECT	ID
						,ActiveProdOrder
						,Targa as IdxMacchina
						,[DateTime] AS DtEvento
    			FROM	syn_M2ERP_MACHINE_STATE
    			WHERE	ID >= @LastIdx AND ID < @NewIdx
    		)
			
			-- lego i valori col precedente x calcolare se siano variati x insert valori
    		INSERT INTO	MachineFluxLog(DtEvento, IdxMacchina, CodFlux, Valore, Cnt)
			SELECT	curr.DtEvento, curr.IdxMacchina, 'ProdOrd' AS CodFlux, curr.ActiveProdOrder as Valore, 0 as Cnt
			FROM	cteCurrOrd curr
				INNER JOIN ctePrevOrd prev
				ON	curr.ID = prev.ID+1
			WHERE	curr.ActiveProdOrder <> prev.ActiveProdOrder

			-- processo dati Part Program
    		;WITH cteCurrPP AS
    		(
    			SELECT	ID
						,CurrentPP
						,Targa as IdxMacchina
						,[DateTime] AS DtEvento
    			FROM	syn_M2ERP_MACHINE_STATE
    			WHERE	ID > @LastIdx AND ID <= @NewIdx
    		)
			, ctePrevPP AS
    		(
    			SELECT	ID
						,CurrentPP		
						,Targa as IdxMacchina
						,[DateTime] AS DtEvento
    			FROM	syn_M2ERP_MACHINE_STATE
    			WHERE	ID >= @LastIdx AND ID < @NewIdx
    		)
			
			-- lego i valori col precedente x calcolare se siano variati x insert valori
    		INSERT INTO	MachineFluxLog(DtEvento, IdxMacchina, CodFlux, Valore, Cnt)
			SELECT	curr.DtEvento, curr.IdxMacchina, 'PartProg' as CodFlux, curr.CurrentPP as Valore, 0 as Cnt
			FROM	cteCurrPP curr
				INNER JOIN ctePrevPP prev
				ON	curr.ID = prev.ID+1
			WHERE	curr.CurrentPP <> prev.CurrentPP

    
    		-- aggiorno valore indice processato
    		MERGE SyncState AS tgt  
    		USING (SELECT @NewIdx as LastIdx, 'ToMachineFluxLog' AS TableName) as src (LastIdx, TableName)  
    		ON (tgt.TableName = src.TableName)  
    		WHEN MATCHED THEN
    			UPDATE SET	LastIdx = src.LastIdx  
    						,Note	= 'UPDATED from ' + CAST(@LastIdx AS NVARCHAR(50))
    						,LastUpdate = GETDATE()
    		WHEN NOT MATCHED THEN  
    			INSERT (LastIdx, TableName, Note, LastUpdate)  
    			VALUES (src.LastIdx, src.TableName, 'CREATED', GETDATE());

		END
    		
    COMMIT tran
END