



CREATE PROC blog_GenericGetEntryIDs_10
(
	@ItemCount int, 
	@PostType int,
	@PostConfig int,
	@BlogID int,
	@CategoryID int = null,
	@CategoryName nvarchar(100) = null,
	@StartDate datetime = null,
	@StopDate datetime = null,
	@CategoryType int = null,
	@BlogGroupID int=null,
	@Author nvarchar(100) = null
)
as

/*
	Generic Entry Collection Proc With Categories

	All possible combinations will be filter by PostTye, PostConfig, and BlogID

	# of records will be controlled rowcount

	Order of precidence:
		CategoryID
		CategoryName
		StartDate
		Default
*/
SET NOCOUNT ON
set rowcount @ItemCount
--ѯ
if(@Author is not null)
begin
	SELECT 
			bc.[ID]
		FROM 
			blog_Content bc with(nolock)
		WHERE 
			bc.PostType | @PostType = @PostType
			and bc.Author = @Author
		ORDER BY 
			bc.[dateadded] desc
	return
end
--û
if(@BlogGroupID is not null)
Begin
	if(@StartDate is not null and @StopDate is null)
		begin
		Set @StopDate = DateAdd(day,1,@StartDate)
		
		SELECT 
			bc.[ID]
		FROM 
			blog_Content bc with(nolock)
			INNER JOIN blog_Links bl with(nolock) on bc.ID = bl.PostID
		WHERE 
			bc.PostType | @PostType = @PostType
			and bc.BlogID in (Select BlogID from blog_UsersInGroups where GroupID=@BlogGroupID)
			and bc.DateAdded >= @StartDate and bc.DateAdded <= @StopDate
			and bl.CategoryID =808
		ORDER BY 
			bc.[dateadded] desc
		end
	else
		begin
	SELECT 
			bc.[ID]
		FROM 
			blog_Content bc with(nolock)
			INNER JOIN blog_Links bl with(nolock) on bc.ID = bl.PostID
		WHERE 
			bc.PostType | @PostType = @PostType
			and bc.BlogID in (Select BlogID from blog_UsersInGroups where GroupID=@BlogGroupID)
			and bl.CategoryID =808
		ORDER BY 
			bc.[dateadded] desc
		end
		return
End
--
if(@CategoryType is not null)
Begin
	if(@StartDate is not null and @StopDate is null)
		begin
		Set @StopDate = DateAdd(day,1,@StartDate)
		
		SELECT 
			bc.[ID]
		FROM 
			blog_Content bc with(nolock)
			INNER JOIN blog_Links bl with(nolock) on bc.ID = bl.PostID
			INNER JOIN blog_LinkCategories bcat with(nolock) on bl.CategoryID = bcat.CategoryID
		WHERE 
			bc.PostType | @PostType = @PostType
			and bl.CategoryID in(Select CategoryID from blog_LinkCategories where CategoryType=@CategoryType)
			and bc.DateAdded >= @StartDate and bc.DateAdded <= @StopDate
		ORDER BY 
			bc.[dateadded] desc
		end
	else
		begin 
	SELECT 
			bc.[ID]
		FROM 
			blog_Content bc with(nolock)
			INNER JOIN blog_Links bl with(nolock) on bc.ID = bl.PostID
		WHERE 
			bc.PostType | @PostType = @PostType
			and bl.CategoryID in(Select CategoryID from blog_LinkCategories where CategoryType=@CategoryType)
		ORDER BY 
			bc.[dateadded] desc
		end
		
		return
End


if(@BlogID is not null)
Begin
--Do we have a CategoryID?
if(@CategoryID is not null)
Begin
	--we will filter by categoryID. Should we also filter by date?
	if(@StartDate is null)
	Begin
		-- No Date Filter
		SELECT 
			bc.[ID]
		FROM 
			blog_Content bc with(nolock)
			INNER JOIN blog_Links bl with(nolock) on bc.ID = bl.PostID
		WHERE 
			bc.BlogID = @BlogID
			and bc.PostConfig & @PostConfig = @PostConfig 
			and bc.PostType | @PostType = @PostType
			and bl.CategoryID = @CategoryID
		ORDER BY 
			bc.[dateadded] desc
	End
	Else
	Begin
		--Filter by CategoryID and Date. 

		--If we only have a start date and no stop date, add 24 hours to to stopdate
		if(@StartDate is not null and @StopDate is null)
		Set @StopDate = DateAdd(day,1,@StartDate)
		
		-- No Date Filter
		SELECT 
			bc.[ID]
		FROM 
			blog_Content bc with(nolock)
			INNER JOIN blog_Links bl with(nolock) on bc.ID = bl.PostID
		WHERE 
			bc.BlogID = @BlogID
			and bc.PostConfig & @PostConfig = @PostConfig 
			and bc.PostType | @PostType = @PostType
			and bl.CategoryID = @CategoryID
			and bc.DateAdded >= @StartDate and bc.DateAdded <= @StopDate
		ORDER BY 
			bc.[dateadded] desc
	End
End
-- Do we have a CategoryName? (CategoryID will override this value)
else if(@CategoryName is not null)
Begin
	--We will filter by categryName (Title)
	--Should we also filter by Date?
	if(@StartDate is null)
	Begin
		-- Filter by CategoryName and not Date
		SELECT 
			bc.[ID] 
		FROM 
			blog_Content bc with(nolock)
			INNER JOIN blog_Links bl with(nolock) on bc.ID = bl.PostID
			INNER JOIN blog_LinkCategories bcat with(nolock) on bl.CategoryID = bcat.CategoryID

		WHERE 
			bc.BlogID = @BlogID
			and bc.PostConfig & @PostConfig = @PostConfig 
			and bc.PostType | @PostType = @PostType
			and bcat.Title = @CategoryName
		ORDER BY 
			bc.[dateadded] desc
	End
	Else
	Begin
		--Filter by CategoryName (Title) and Date

		--If we only have a start date and no stop date, add 24 hours to to stopdate
		if(@StartDate is not null and @StopDate is null)
		Set @StopDate = DateAdd(day,1,@StartDate)
		
		SELECT 
			bc.[ID]
		FROM 
			blog_Content bc with(nolock)
			INNER JOIN blog_Links bl with(nolock) on bc.ID = bl.PostID
			INNER JOIN blog_LinkCategories bcat with(nolock) on bl.CategoryID = bcat.CategoryID
		WHERE 
			bc.BlogID = @BlogID
			and bc.PostConfig & @PostConfig = @PostConfig 
			and bc.PostType | @PostType = @PostType
			and bcat.Title = @CategoryName
			and bc.DateAdded >= @StartDate and bc.DateAdded <= @StopDate
		ORDER BY 
			bc.[dateadded] desc
	End
End
else if(@StartDate is not null)
Begin
	--No categoryID or Category was found. We will ONLY filter by dates

	--If we only have a start date and no stop date, add 24 hours to to stopdate
	if(@StartDate is not null and @StopDate is null)
	Set @StopDate = DateAdd(day,1,@StartDate)

	SELECT 
		bc.[ID]
	FROM 
		blog_Content bc with(nolock)
	WHERE 
		bc.BlogID = @BlogID
		and bc.PostConfig & @PostConfig = @PostConfig 
		and bc.PostType | @PostType = @PostType
		and bc.DateAdded >= @StartDate and bc.DateAdded <= @StopDate
	ORDER BY 
		bc.[dateadded] desc

End
Else
Begin
	--All else has failed :)
	--We will just select the last x number of items
	SELECT 
		bc.[ID]
	FROM 
		blog_Content bc with(nolock)
	WHERE 
		bc.BlogID = @BlogID
		and bc.PostConfig & @PostConfig = @PostConfig 
		and bc.PostType | @PostType = @PostType
	ORDER BY 
		bc.[dateadded] desc
End
End
Else
--BlogID is Null
Begin
--Do we have a CategoryID?
if(@CategoryID is not null)
Begin
	--we will filter by categoryID. Should we also filter by date?
	if(@StartDate is null)
	Begin
		-- No Date Filter
		SELECT 
			bc.[ID]
		FROM 
			blog_Content bc with(nolock)
			INNER JOIN blog_Links bl with(nolock) on bc.ID = bl.PostID
			INNER JOIN blog_Config bcc with(nolock) on bc.BlogID = bcc.BlogID and bcc.IsAggregated = 1 
		WHERE 
			bc.PostConfig & @PostConfig = @PostConfig 
			and bc.PostType | @PostType = @PostType
			
		ORDER BY 
			bc.[dateadded] desc
	End
	Else
	Begin
		--Filter by CategoryID and Date. 

		--If we only have a start date and no stop date, add 24 hours to to stopdate
		if(@StartDate is not null and @StopDate is null)
		Set @StopDate = DateAdd(day,1,@StartDate)
		
		-- No Date Filter
		SELECT 
			bc.[ID]
		FROM 
			blog_Content bc with(nolock)
			INNER JOIN blog_Links bl with(nolock) on bc.ID = bl.PostID
			INNER JOIN blog_Config bcc with(nolock) on bc.BlogID = bcc.BlogID and bcc.IsAggregated = 1 
		WHERE 
			bc.PostConfig & @PostConfig = @PostConfig 
			and bc.PostType | @PostType = @PostType
			and bl.CategoryID = @CategoryID 
			and bc.DateAdded >= @StartDate and bc.DateAdded <= @StopDate
		ORDER BY 
			bc.[dateadded] desc
	End
End
-- Do we have a CategoryName? (CategoryID will override this value)
else if(@CategoryName is not null)
Begin
	--We will filter by categryName (Title)
	--Should we also filter by Date?
	if(@StartDate is null)
	Begin
		-- Filter by CategoryName and not Date
		SELECT 
			bc.[ID] 
		FROM 
			blog_Content bc with(nolock)
			INNER JOIN blog_Links bl with(nolock) on bc.ID = bl.PostID
			INNER JOIN blog_LinkCategories bcat with(nolock) on bl.CategoryID = bcat.CategoryID
			INNER JOIN blog_Config bcc with(nolock) on bc.BlogID = bcc.BlogID and bcc.IsAggregated = 1 
		WHERE 
			bc.PostConfig & @PostConfig = @PostConfig 
			and bc.PostType | @PostType = @PostType
			and bcat.Title = @CategoryName
		ORDER BY 
			bc.[dateadded] desc
	End
	Else
	Begin
		--Filter by CategoryName (Title) and Date

		--If we only have a start date and no stop date, add 24 hours to to stopdate
		if(@StartDate is not null and @StopDate is null)
		Set @StopDate = DateAdd(day,1,@StartDate)
		
		SELECT 
			bc.[ID]
		FROM 
			blog_Content bc with(nolock)
			INNER JOIN blog_Links bl with(nolock) on bc.ID = bl.PostID
			INNER JOIN blog_LinkCategories bcat with(nolock) on bl.CategoryID = bcat.CategoryID
			INNER JOIN blog_Config bcc with(nolock) on bc.BlogID = bcc.BlogID and bcc.IsAggregated = 1 
		WHERE 
			bc.PostConfig & @PostConfig = @PostConfig 
			and bc.PostType | @PostType = @PostType
			and bcat.Title = @CategoryName
			and bc.DateAdded >= @StartDate and bc.DateAdded <= @StopDate
		ORDER BY 
			bc.[dateadded] desc
	End
End
else if(@StartDate is not null)
Begin
	--No categoryID or Category was found. We will ONLY filter by dates

	--If we only have a start date and no stop date, add 24 hours to to stopdate
	if(@StartDate is not null and @StopDate is null)
	Set @StopDate = DateAdd(day,1,@StartDate)

	SELECT 
		bc.[ID]
	FROM 
		blog_Content bc with(nolock)
		INNER JOIN blog_Config bcc with(nolock) on bc.BlogID = bcc.BlogID and bcc.IsAggregated = 1 
	WHERE 
		bc.PostConfig & @PostConfig = @PostConfig 
		and bc.PostType | @PostType = @PostType
		and bc.DateAdded >= @StartDate and bc.DateAdded <= @StopDate
		
	ORDER BY 
		bc.[dateadded] desc

End
Else
Begin
	--All else has failed :)
	--We will just select the last x number of items
	SELECT 
		bc.[ID]
	FROM 
		blog_Content bc with(nolock)
		INNER JOIN blog_Config bcc with(nolock) on bc.BlogID = bcc.BlogID and bcc.IsAggregated = 1 
	WHERE 
		bc.PostConfig & @PostConfig = @PostConfig 
		and bc.PostType | @PostType = @PostType
	ORDER BY 
		bc.[dateadded] desc
End
End

GO
