UPDATE TBDD_MODULES SET DB_VERSION = '2.2.2.1' where NAME = 'Process-Manager'
GO
ALTER TABLE TBPM_PROFILE 
ADD TARGET_PATH_BLIND_FILE VARCHAR(500)	NOT NULL DEFAULT '';
GO
ALTER TABLE TBPM_FILES_USER_NOT_INDEXED
ADD 
    [GUID]	BIGINT NOT NULL 	IDENTITY (1, 1),
    ADDED_WHEN DATETIME NOT NULL DEFAULT GETDATE(),
	USRID	INTEGER NOT NULL DEFAULT 0
GO
CREATE NONCLUSTERED INDEX [IDXTBPM_FILES_USER_NOT_INDEXED_USRID_AW]
ON [dbo].[TBPM_FILES_USER_NOT_INDEXED] ([USRID])
INCLUDE ([ADDED_WHEN])
GO
-- =============================================
-- Author:		Digital Data MS
-- Create date: 11.06.2021
-- Description:	Gets the next DocumentID in Workflow
-- Changed: 07.04.2022	TabPM_VALIDATION_PROFILE_GROUP_USER
-- Changed_ 21.06.2022	Files2bValidated disappeared during transport
-- =============================================
ALTER PROCEDURE [dbo].[PRPM_GET_NEXT_DOC_INFO]
(
	@PROFIL_ID Integer,
	@DocID BigInt, 
	@UserID Integer
)
AS
BEGIN

	IF @DocID = 0 And EXISTS(SELECT GUID FROM TBPM_FILES_USER_NOT_INDEXED WHERE DATEDIFF(second,ADDED_WHEN,GETDATE()) < 4 AND USRID = @UserID) 
		BEGIN
			PRINT 'NO DocID and datdiff < 5 sec'
			DECLARE @FILEPATH VARCHAR(512)
			SELECT @FILEPATH = FILE_PATH  FROM TBPM_FILES_USER_NOT_INDEXED WHERE  DATEDIFF(second,ADDED_WHEN,GETDATE()) < 4 AND USRID = @UserID;
			PRINT '@FILEPATH'
			select @DocID = DOC_ID From TBPM_PROFILE_FILES where FILE_PATH = @FILEPATH;
		END

	INSERT INTO TBPM_DOCWALKOVER (DocID,UserID) VALUES (@DocID,@UserID)

	--SELECT GUID as DocGUID ,DOC_ID as DocID FROM TBPM_PROFILE_FILES WHERE GUID = (
	--	SELECT MIN(T.DocGUID) FROM [dbPRPM_GET_NEXTo].[FNPM_CUST_GET_DOCID_FOR_USER_OVERVIEW] (@UserID) T where PROFILE_ID = @PROFIL_ID and T.DocID not in 
	--		(SELECT DISTINCT DocID FROM TBPM_DOCWALKOVER WHERE UserID = @UserID))
	SELECT GUID as DocGUID ,DOC_ID as DocID ,
	(SELECT COUNT(GUID) FROM TBPM_VALIDATION_PROFILE_GROUP_USER A WHERE A.DocID NOT IN (SELECT DocID FROM TBPM_DOCWALKOVER WHERE UserID = @UserID)
	and UserID = @UserID) FILES2BValidated
	FROM TBPM_PROFILE_FILES WHERE PROFIL_ID = @PROFIL_ID AND DOC_ID = (
	SELECT MIN(DocID) FROM TBPM_VALIDATION_PROFILE_GROUP_USER WHERE UserID = @UserID AND DocID not in (SELECT DISTINCT DocID FROM TBPM_DOCWALKOVER WHERE UserID = @UserID) )
	

	
	RETURN 
END
GO 

-- =============================================
-- Author:		Digital Data, MS
-- Create date: 05.07.2019
-- Description:	<Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[PRPM_FILES_NOT_INDEXED] 
	-- Add the parameters for the stored procedure here
	@USER_NAME VARCHAR(50),
	@PROFIL_ID INTEGER,
	@FILEPATH VARCHAR(500),
	@DocGUID BIGINT
AS
BEGIN
	DECLARE @USRID INTEGER
	SELECT @USRID = GUID FROM TBDD_USER WHERE USERNAME = @USER_NAME;
	SET @FILEPATH = REPLACE(@FILEPATH,'Windream\\','Windream\')
	IF NOT EXISTS(SELECT * FROM TBPM_FILES_USER_NOT_INDEXED WHERE 
															USRID = @USRID
															AND PROFIL_ID = @PROFIL_ID AND FILE_PATH = @FILEPATH)
		INSERT INTO TBPM_FILES_USER_NOT_INDEXED (USR_NAME,PROFIL_ID,FILE_PATH,DocGUID,USRID) VALUES (@USER_NAME, @PROFIL_ID, @FILEPATH,@DocGUID,@USRID)
	ELSE
		UPDATE TBPM_FILES_USER_NOT_INDEXED SET ADDED_WHEN = GETDATE()
		WHERE USRID = @USRID AND PROFIL_ID = @PROFIL_ID AND FILE_PATH = @FILEPATH
END
GO

