﻿-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[fis_widgets_getCache]	
	@account varchar(50)
	,@short_name varchar(50)
	,@login_guid varchar(36)
AS
BEGIN
	SET NOCOUNT ON;
	DECLARE @RET nvarchar(max);
    DECLARE @IsCacheable bit, @Personalized bit;

	SELECT @IsCacheable = [daycache], @personalized = [personalized] 
		FROM [dbo].[fis_widgets] 
		WHERE ([account] = @account OR [account] = '*') AND [short_name] = @short_name;

	IF @IsCacheable = 1
		BEGIN
			DELETE FROM [dbo].[fis_widgets_cache] WHERE [date] <> CAST(GETUTCDATE() as date);

			SELECT TOP(1)
				@RET = [data]
			  FROM [dbo].[fis_widgets_cache]
			  WHERE [account] = @account 
				AND [short_name] = @short_name
				AND [date] = CAST(GETUTCDATE() as date)
				AND ((@Personalized = 1 AND [person_guid] = @login_guid) OR (@Personalized = 0));
		END
	
	SELECT @RET;

END