如何使用带有 prestasharp 的 Factories GetByFilter 方法获取 5000 多个实体

问题描述

库版本:

1.2.9

NuGet 包网址:

https://www.nuget.org/packages/PrestaSharp/1.2.9

Prestashop 版本:

1.7.7.0

描述错误

分页的 PrestaSharp GetByFilter 总是返回相同的实体列表

由于 ProductFactory 的 GetByFilter 方法在超过 5000 个产品与过滤器匹配时返回 null。我决定通过这样的分页获取它们

_productFactory.GetIdsByFilter(filter,null,"[" + startingIndex.ToString() + "," + count.ToString() + "]");

但即使开始索引(因为循环)改变结果是一样的

完整代码

filter.Add("date_upd","[" + dFrom + "," + dTo + "]");

int i = 0;

List<long> AllProducts = new List<long>();
List<long> products;
while (true) // this loop never breaks
{ 
    int startingIndex = i++  * count;
    products = _productFactory.GetIdsByFilter(filter," + (count).ToString() + "]"); // returns same products in every iteration
    if (products?.Any() == true) // to check the list is not empty
    {
        AllProducts.AddRange(products);
        if (products.Count < count)
        {
            break;
        }
    }
    else
        break;
}

解决方法

您只需要从 'limit' 参数中删除括号。当 Github 文档给出带括号的示例时,这是一个错误。这是一个自己的实现,我并行发送多个请求以加快处理速度。

public async Task<List<PS_Entity>> GetElements(int pageSize = 100) {
            try {
                var numberOfElements = factory.GetIds().Count;
                var numberOfParallelTasks = numberOfElements >= pageSize ? numberOfElements / pageSize : 1;
                var loadElementsTasks = Enumerable.Range(0,numberOfParallelTasks).Select(taskNumber => factory.GetByFilterAsync(null,"id_ASC",$"{taskNumber * pageSize},{pageSize}")).ToList();
                if (numberOfElements % pageSize != 0) {
                    var skiped = numberOfParallelTasks * pageSize;
                    loadElementsTasks.Add(factory.GetByFilterAsync(null,$"{skiped},{numberOfElements - skiped}"));
                }
                var elements = (await Task.WhenAll(loadElementsTasks)).SelectMany(elements => elements).ToList();
                return elements;
            } catch (PrestaSharpException e) {
                if ((int)e.ResponseHttpStatusCode == 404)
                    Console.WriteLine($"No existen {RemoveFactoryFromName(factory)}'s.");
                return new List<PS_Entity>();
            }
        }