Advertisement
nmg196

NopCommerce ProductLoadAllPaged

Nov 16th, 2012
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 11.62 KB | None | 0 0
  1. /****** Object:  StoredProcedure [dbo].[ProductLoadAllPaged]    Script Date: 16/11/2012 10:34:06 ******/
  2. SET ANSI_NULLS ON
  3. GO
  4. SET QUOTED_IDENTIFIER ON
  5. GO
  6.  
  7. ALTER PROCEDURE [dbo].[ProductLoadAllPaged]
  8. (
  9.     @CategoryIds        nvarchar(300) = null,   --a list of category IDs (comma-separated list). e.g. 1,2,3
  10.     @ManufacturerId     int = 0,
  11.     @ProductTagId       int = 0,
  12.     @FeaturedProducts   bit = null, --0 featured only , 1 not featured only, null - load all products
  13.     @PriceMin           decimal(18, 4) = null,
  14.     @PriceMax           decimal(18, 4) = null,
  15.     @Keywords           nvarchar(MAX) = null,
  16.     @SearchDescriptions bit = 0,
  17.     @FilteredSpecs      nvarchar(300) = null,   --filter by attributes (comma-separated list). e.g. 14,15,16
  18.     @LanguageId         int = 0,
  19.     @OrderBy            int = 5, --0 position, 5 - Name: A to Z, 6 - Name: Z to A, 10 - Price: Low to High, 11 - Price: High to Low, 15 - creation date
  20.     @PageIndex          int = 0,
  21.     @PageSize           int = 2147483644,
  22.     @ShowHidden         bit = 0,
  23.     @LoadFilterableSpecificationAttributeOptionIds bit = 0, --a value indicating whether we should load the specification attribute option identifiers applied to loaded products (all pages)
  24.     @FilterableSpecificationAttributeOptionIds nvarchar(100) = null OUTPUT, --the specification attribute option identifiers applied to loaded products (all pages). returned as a comma separated list of identifiers
  25.     @TotalRecords       int = null OUTPUT
  26. )
  27. AS
  28. BEGIN
  29.    
  30.     /* Products that filtered by keywords */
  31.     CREATE TABLE #KeywordProducts
  32.     (
  33.         [ProductId] int NOT NULL
  34.     )
  35.  
  36.     DECLARE
  37.         @SearchKeywords bit,
  38.         @sql nvarchar(max),
  39.         @sql_orderby nvarchar(max)
  40.  
  41.     SET NOCOUNT ON
  42.    
  43.     --filter by keywords
  44.     IF ISNULL(@Keywords, '') != ''
  45.     BEGIN
  46.         SET @SearchKeywords = 1
  47.        
  48.         SET @Keywords = isnull(@Keywords, '')
  49.         SET @Keywords = '%' + rtrim(ltrim(@Keywords)) + '%'
  50.        
  51.        
  52.         DECLARE @FTS_Keywords nvarchar(400)
  53.         SET @FTS_Keywords = @Keywords
  54.         SET @FTS_Keywords = '"' +  LTRIM(RTRIM(@FTS_Keywords)) + '*"'
  55.         -- This method cleans multiple spaces between words
  56.         SET @FTS_Keywords = REPLACE(@FTS_Keywords, '  ', ' þ')
  57.         SET @FTS_Keywords = REPLACE(@FTS_Keywords, 'þ ', '')
  58.         SET @FTS_Keywords = REPLACE(@FTS_Keywords, 'þ', '')
  59.         --SET SEARCH WILDCARDS
  60.         SET @FTS_Keywords = REPLACE(@FTS_Keywords, ' ', '*" AND "')
  61.    
  62.         SET @sql = '
  63.         INSERT INTO #KeywordProducts ([ProductId])
  64.         SELECT p.Id
  65.         FROM Product p with (NOLOCK)
  66.         WHERE CONTAINS(p.name, @FTS_Keywords)
  67.         UNION
  68.         SELECT pv.ProductId
  69.         FROM ProductVariant pv with (NOLOCK)
  70.         WHERE PATINDEX(@Keywords, pv.name) > 0
  71.         UNION
  72.         SELECT pv.ProductId
  73.         FROM ProductVariant pv with (NOLOCK)
  74.         WHERE PATINDEX(@Keywords, pv.sku) > 0
  75.         UNION
  76.         SELECT lp.EntityId
  77.         FROM LocalizedProperty lp with (NOLOCK)
  78.         WHERE
  79.             lp.LocaleKeyGroup = N''Product''
  80.             AND lp.LanguageId = ' + ISNULL(CAST(@LanguageId AS nvarchar(max)), '0') + '
  81.             AND lp.LocaleKey = N''Name''
  82.             AND PATINDEX(@Keywords, lp.LocaleValue) > 0'
  83.            
  84.         IF @SearchDescriptions = 1 SET @sql = @sql + '
  85.         UNION
  86.         SELECT p.Id
  87.         FROM Product p with (NOLOCK)
  88.         WHERE PATINDEX(@Keywords, p.ShortDescription) > 0
  89.         UNION
  90.         SELECT p.Id
  91.         FROM Product p with (NOLOCK)
  92.         WHERE PATINDEX(@Keywords, p.FullDescription) > 0
  93.         UNION
  94.         SELECT pv.ProductId
  95.         FROM ProductVariant pv with (NOLOCK)
  96.         WHERE PATINDEX(@Keywords, pv.Description) > 0
  97.         UNION
  98.         SELECT lp.EntityId
  99.         FROM LocalizedProperty lp with (NOLOCK)
  100.         WHERE
  101.             lp.LocaleKeyGroup = N''Product''
  102.             AND lp.LanguageId = ' + ISNULL(CAST(@LanguageId AS nvarchar(max)), '0') + '
  103.             AND lp.LocaleKey = N''ShortDescription''
  104.             AND PATINDEX(@Keywords, lp.LocaleValue) > 0
  105.         UNION
  106.         SELECT lp.EntityId
  107.         FROM LocalizedProperty lp with (NOLOCK)
  108.         WHERE
  109.             lp.LocaleKeyGroup = N''Product''
  110.             AND lp.LanguageId = ' + ISNULL(CAST(@LanguageId AS nvarchar(max)), '0') + '
  111.             AND lp.LocaleKey = N''FullDescription''
  112.             AND PATINDEX(@Keywords, lp.LocaleValue) > 0'
  113.        
  114.         --PRINT (@sql)
  115.         EXEC sp_executesql @sql, N'@Keywords nvarchar(MAX), @FTS_Keywords nvarchar(400)',
  116.              @Keywords = @Keywords,
  117.              @FTS_Keywords = @FTS_Keywords
  118.  
  119.     END
  120.     ELSE
  121.     BEGIN
  122.         SET @SearchKeywords = 0
  123.     END
  124.  
  125.     --filter by category IDs
  126.     SET @CategoryIds = isnull(@CategoryIds, '')
  127.     CREATE TABLE #FilteredCategoryIds
  128.     (
  129.         CategoryId int not null
  130.     )
  131.     INSERT INTO #FilteredCategoryIds (CategoryId)
  132.     SELECT CAST(data as int) FROM dbo.[nop_splitstring_to_table](@CategoryIds, ',')
  133.     DECLARE @CategoryIdsCount int  
  134.     SET @CategoryIdsCount = (SELECT COUNT(1) FROM #FilteredCategoryIds)
  135.  
  136.     --filter by attributes
  137.     SET @FilteredSpecs = isnull(@FilteredSpecs, '')
  138.     CREATE TABLE #FilteredSpecs
  139.     (
  140.         SpecificationAttributeOptionId int not null
  141.     )
  142.     INSERT INTO #FilteredSpecs (SpecificationAttributeOptionId)
  143.     SELECT CAST(data as int) FROM dbo.[nop_splitstring_to_table](@FilteredSpecs, ',')  
  144.     DECLARE @SpecAttributesCount int   
  145.     SET @SpecAttributesCount = (SELECT COUNT(1) FROM #FilteredSpecs)
  146.  
  147.     --paging
  148.     DECLARE @PageLowerBound int
  149.     DECLARE @PageUpperBound int
  150.     DECLARE @RowsToReturn int
  151.     SET @RowsToReturn = @PageSize * (@PageIndex + 1)   
  152.     SET @PageLowerBound = @PageSize * @PageIndex
  153.     SET @PageUpperBound = @PageLowerBound + @PageSize + 1
  154.    
  155.     CREATE TABLE #DisplayOrderTmp
  156.     (
  157.         [Id] int IDENTITY (1, 1) NOT NULL,
  158.         [ProductId] int NOT NULL
  159.     )
  160.  
  161.     SET @sql = '
  162.     INSERT INTO #DisplayOrderTmp ([ProductId])
  163.     SELECT p.Id
  164.     FROM
  165.         Product p with (NOLOCK)'
  166.    
  167.     IF @CategoryIdsCount > 0
  168.     BEGIN
  169.         SET @sql = @sql + '
  170.         LEFT JOIN Product_Category_Mapping pcm with (NOLOCK)
  171.             ON p.Id = pcm.ProductId'
  172.     END
  173.    
  174.     IF @ManufacturerId > 0
  175.     BEGIN
  176.         SET @sql = @sql + '
  177.         LEFT JOIN Product_Manufacturer_Mapping pmm with (NOLOCK)
  178.             ON p.Id = pmm.ProductId'
  179.     END
  180.    
  181.     IF ISNULL(@ProductTagId, 0) != 0
  182.     BEGIN
  183.         SET @sql = @sql + '
  184.         LEFT JOIN Product_ProductTag_Mapping pptm with (NOLOCK)
  185.             ON p.Id = pptm.Product_Id'
  186.     END
  187.    
  188.     IF @ShowHidden = 0
  189.     OR @PriceMin > 0
  190.     OR @PriceMax > 0
  191.     OR @OrderBy = 10 /* Price: Low to High */
  192.     OR @OrderBy = 11 /* Price: High to Low */
  193.     BEGIN
  194.         SET @sql = @sql + '
  195.         LEFT JOIN ProductVariant pv with (NOLOCK)
  196.             ON p.Id = pv.ProductId'
  197.     END
  198.    
  199.     --searching by keywords
  200.     IF @SearchKeywords = 1
  201.     BEGIN
  202.         SET @sql = @sql + '
  203.         JOIN #KeywordProducts kp
  204.             ON  p.Id = kp.ProductId'
  205.     END
  206.    
  207.     SET @sql = @sql + '
  208.     WHERE
  209.         p.Deleted = 0'
  210.    
  211.     --filter by category
  212.     IF @CategoryIdsCount > 0
  213.     BEGIN
  214.         SET @sql = @sql + '
  215.         AND pcm.CategoryId IN (SELECT CategoryId FROM #FilteredCategoryIds)'
  216.        
  217.         IF @FeaturedProducts IS NOT NULL
  218.         BEGIN
  219.             SET @sql = @sql + '
  220.         AND pcm.IsFeaturedProduct = ' + CAST(@FeaturedProducts AS nvarchar(max))
  221.         END
  222.     END
  223.    
  224.     --filter by manufacturer
  225.     IF @ManufacturerId > 0
  226.     BEGIN
  227.         SET @sql = @sql + '
  228.         AND pmm.ManufacturerId = ' + CAST(@ManufacturerId AS nvarchar(max))
  229.        
  230.         IF @FeaturedProducts IS NOT NULL
  231.         BEGIN
  232.             SET @sql = @sql + '
  233.         AND pmm.IsFeaturedProduct = ' + CAST(@FeaturedProducts AS nvarchar(max))
  234.         END
  235.     END
  236.    
  237.     --filter by product tag
  238.     IF ISNULL(@ProductTagId, 0) != 0
  239.     BEGIN
  240.         SET @sql = @sql + '
  241.         AND pptm.ProductTag_Id = ' + CAST(@ProductTagId AS nvarchar(max))
  242.     END
  243.    
  244.     IF @ShowHidden = 0
  245.     BEGIN
  246.         SET @sql = @sql + '
  247.         AND p.Published = 1
  248.         AND pv.Published = 1
  249.         AND pv.Deleted = 0
  250.         AND (getutcdate() BETWEEN ISNULL(pv.AvailableStartDateTimeUtc, ''1/1/1900'') and ISNULL(pv.AvailableEndDateTimeUtc, ''1/1/2999''))'
  251.     END
  252.    
  253.     --min price
  254.     IF @PriceMin > 0
  255.     BEGIN
  256.         SET @sql = @sql + '
  257.         AND (
  258.                 (
  259.                     --special price (specified price and valid date range)
  260.                     (pv.SpecialPrice IS NOT NULL AND (getutcdate() BETWEEN isnull(pv.SpecialPriceStartDateTimeUtc, ''1/1/1900'') AND isnull(pv.SpecialPriceEndDateTimeUtc, ''1/1/2999'')))
  261.                     AND
  262.                     (pv.SpecialPrice >= ' + CAST(@PriceMin AS nvarchar(max)) + ')
  263.                 )
  264.                 OR
  265.                 (
  266.                     --regular price (price isnt specified or date range isnt valid)
  267.                     (pv.SpecialPrice IS NULL OR (getutcdate() NOT BETWEEN isnull(pv.SpecialPriceStartDateTimeUtc, ''1/1/1900'') AND isnull(pv.SpecialPriceEndDateTimeUtc, ''1/1/2999'')))
  268.                     AND
  269.                     (pv.Price >= ' + CAST(@PriceMin AS nvarchar(max)) + ')
  270.                 )
  271.             )'
  272.     END
  273.    
  274.     --max price
  275.     IF @PriceMax > 0
  276.     BEGIN
  277.         SET @sql = @sql + '
  278.         AND (
  279.                 (
  280.                     --special price (specified price and valid date range)
  281.                     (pv.SpecialPrice IS NOT NULL AND (getutcdate() BETWEEN isnull(pv.SpecialPriceStartDateTimeUtc, ''1/1/1900'') AND isnull(pv.SpecialPriceEndDateTimeUtc, ''1/1/2999'')))
  282.                     AND
  283.                     (pv.SpecialPrice <= ' + CAST(@PriceMax AS nvarchar(max)) + ')
  284.                 )
  285.                 OR
  286.                 (
  287.                     --regular price (price isnt specified or date range isnt valid)
  288.                     (pv.SpecialPrice IS NULL OR (getutcdate() NOT BETWEEN isnull(pv.SpecialPriceStartDateTimeUtc, ''1/1/1900'') AND isnull(pv.SpecialPriceEndDateTimeUtc, ''1/1/2999'')))
  289.                     AND
  290.                     (pv.Price <= ' + CAST(@PriceMax AS nvarchar(max)) + ')
  291.                 )
  292.             )'
  293.     END
  294.    
  295.     --filter by specs
  296.     IF @SpecAttributesCount > 0
  297.     BEGIN
  298.         SET @sql = @sql + '
  299.         AND NOT EXISTS (
  300.             SELECT 1
  301.             FROM
  302.                 #FilteredSpecs [fs]
  303.             WHERE
  304.                 [fs].SpecificationAttributeOptionId NOT IN (
  305.                     SELECT psam.SpecificationAttributeOptionId
  306.                     FROM dbo.Product_SpecificationAttribute_Mapping psam
  307.                     WHERE psam.AllowFiltering = 1 AND psam.ProductId = p.Id
  308.                 )
  309.             )'
  310.     END
  311.    
  312.     --sorting
  313.     SET @sql_orderby = ''  
  314.     IF @OrderBy = 5 /* Name: A to Z */
  315.         SET @sql_orderby = ' p.[Name] ASC'
  316.     ELSE IF @OrderBy = 6 /* Name: Z to A */
  317.         SET @sql_orderby = ' p.[Name] DESC'
  318.     ELSE IF @OrderBy = 10 /* Price: Low to High */
  319.         SET @sql_orderby = ' pv.[Price] ASC'
  320.     ELSE IF @OrderBy = 11 /* Price: High to Low */
  321.         SET @sql_orderby = ' pv.[Price] DESC'
  322.     ELSE IF @OrderBy = 15 /* creation date */
  323.         SET @sql_orderby = ' p.[CreatedOnUtc] DESC'
  324.     ELSE /* default sorting, 0 (position) */
  325.     BEGIN
  326.         --category position (display order)
  327.         IF @CategoryIdsCount > 0 SET @sql_orderby = ' pcm.DisplayOrder ASC'
  328.        
  329.         --manufacturer position (display order)
  330.         IF @ManufacturerId > 0
  331.         BEGIN
  332.             IF LEN(@sql_orderby) > 0 SET @sql_orderby = @sql_orderby + ', '
  333.             SET @sql_orderby = @sql_orderby + ' pmm.DisplayOrder ASC'
  334.         END
  335.        
  336.         --name
  337.         IF LEN(@sql_orderby) > 0 SET @sql_orderby = @sql_orderby + ', '
  338.         SET @sql_orderby = @sql_orderby + ' p.[Name] ASC'
  339.     END
  340.    
  341.     SET @sql = @sql + '
  342.     ORDER BY' + @sql_orderby
  343.    
  344.     --PRINT (@sql)
  345.     EXEC sp_executesql @sql
  346.  
  347.     DROP TABLE #FilteredCategoryIds
  348.     DROP TABLE #FilteredSpecs
  349.  
  350.     CREATE TABLE #PageIndex
  351.     (
  352.         [IndexId] int IDENTITY (1, 1) NOT NULL,
  353.         [ProductId] int NOT NULL
  354.     )
  355.     INSERT INTO #PageIndex ([ProductId])
  356.     SELECT ProductId
  357.     FROM #DisplayOrderTmp
  358.     GROUP BY ProductId
  359.     ORDER BY min([Id])
  360.  
  361.     --total records
  362.     SET @TotalRecords = @@rowcount
  363.    
  364.     DROP TABLE #DisplayOrderTmp
  365.  
  366.     --prepare filterable specification attribute option identifier (if requested)
  367.     IF @LoadFilterableSpecificationAttributeOptionIds = 1
  368.     BEGIN      
  369.         CREATE TABLE #FilterableSpecs
  370.         (
  371.             [SpecificationAttributeOptionId] int NOT NULL
  372.         )
  373.         INSERT INTO #FilterableSpecs ([SpecificationAttributeOptionId])
  374.         SELECT DISTINCT [psam].SpecificationAttributeOptionId
  375.         FROM [Product_SpecificationAttribute_Mapping] [psam]
  376.         WHERE [psam].[AllowFiltering] = 1
  377.         AND [psam].[ProductId] IN (SELECT [pi].ProductId FROM #PageIndex [pi])
  378.  
  379.         --build comma separated list of filterable identifiers
  380.         SELECT @FilterableSpecificationAttributeOptionIds = COALESCE(@FilterableSpecificationAttributeOptionIds + ',' , '') + CAST(SpecificationAttributeOptionId as nvarchar(1000))
  381.         FROM #FilterableSpecs
  382.  
  383.         DROP TABLE #FilterableSpecs
  384.     END
  385.  
  386.     --return products
  387.     SELECT TOP (@RowsToReturn)
  388.         p.*
  389.     FROM
  390.         #PageIndex [pi]
  391.         INNER JOIN Product p on p.Id = [pi].[ProductId]
  392.     WHERE
  393.         [pi].IndexId > @PageLowerBound AND
  394.         [pi].IndexId < @PageUpperBound
  395.     ORDER BY
  396.         [pi].IndexId
  397.    
  398.     DROP TABLE #PageIndex
  399. END
  400.  
  401. go
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement