产品列表按固定位置插入内容推送异常求助(超200产品时丢失)
大数量产品列表中内容推送位置丢失问题修复
需求
- 第1个产品卡片后插入单栏内容推送
- 第1个产品后,每新增12个产品卡片(即累计第13、26、39…个产品后)插入双栏内容推送
- 双栏推送后,再新增8个产品卡片(即累计第21、41、61…个产品后)插入单栏内容推送
- 需将指定内容列表按固定位置2、15、16、25合并至产品列表中
问题
当产品数量超过200个时,内容推送的位置出现丢失或错位情况。
原代码问题分析
原代码采用i % 13 == 0和i % 21 == 0的模运算判断推送插入时机,但这两个条件的周期会产生重叠冲突,无法严格匹配需求中的“1→12→8→12→8…”循环规则。例如当产品数量达到26时,26%13=0会触发双栏推送,但此时距离上一次单栏推送(第21个产品后)仅间隔5个产品,不符合“双栏后再8个产品插入单栏”的要求,最终导致大数量产品时推送位置完全混乱。
修复后的代码
public List<ProductInfo> GetProductListDetails1(IEnumerable<IMasterProduct> cards, int? skipCount, int? takeCount, string productListId) { var productListItem = this.SitecoreContext.GetItem<Models.IProductList>(productListId); // 获取单栏、双栏推送的Sitecore内容项 List<Item> singlePushItems = productListItem.Item.GetChildren() .Where(x => x.TemplateID.ToString() == "{BEDD19B0-0AA3-4AC8-A714-B92D23623FAA}")?.ToList() ?? new List<Item>(); List<Item> doublePushItems = productListItem.Item.GetChildren() .Where(x => x.TemplateID.ToString() == "{93FCEC36-8E81-4852-A452-720B4076C2C5}")?.ToList() ?? new List<Item>(); // 过滤有效产品卡片,避免空引用 var validCards = cards?.Where(p => p != null).ToList() ?? new List<IMasterProduct>(); List<ProductInfo> mergedList = new List<ProductInfo>(); int productCount = 0; int lastPushPosition = 0; foreach (var card in validCards) { // 添加产品卡片到结果列表(根据实际需求填充ProductInfo属性) mergedList.Add(new ProductInfo { ProductName = card.ProductName, ProductId = card.ProductId // 其他产品相关属性... }); productCount++; // 按规则插入内容推送 if (productCount == 1) { // 第1个产品后插入单栏推送 AddSinglePush(mergedList, singlePushItems); lastPushPosition = productCount; } else if (productCount - lastPushPosition == 12) { // 距离上次推送满12个产品,插入双栏推送 AddDoublePush(mergedList, doublePushItems); lastPushPosition = productCount; } else if (productCount - lastPushPosition == 8) { // 距离上次推送满8个产品,插入单栏推送 AddSinglePush(mergedList, singlePushItems); lastPushPosition = productCount; } } // 处理固定位置插入(位置为合并后列表的1-based索引,倒序插入避免索引偏移) List<int> fixedInsertPositions = new List<int> { 2, 15, 16, 25 }; foreach (var pos in fixedInsertPositions.OrderByDescending(x => x)) { if (pos <= mergedList.Count) { AddSinglePushAtPosition(mergedList, pos, singlePushItems); } } return mergedList; } // 辅助方法:添加单栏推送内容 private void AddSinglePush(List<ProductInfo> list, List<Item> singlePushItems) { if (singlePushItems.Any()) { list.Add(new ProductInfo { IsContentPush = true, PushType = "SingleColumn", PushContent = singlePushItems[0]["Content"] // 替换为实际内容字段名 }); } } // 辅助方法:添加双栏推送内容 private void AddDoublePush(List<ProductInfo> list, List<Item> doublePushItems) { if (doublePushItems.Any()) { list.Add(new ProductInfo { IsContentPush = true, PushType = "DoubleColumn", PushContent = doublePushItems[0]["Content"] // 替换为实际内容字段名 }); } } // 辅助方法:在指定位置插入单栏推送 private void AddSinglePushAtPosition(List<ProductInfo> list, int position, List<Item> singlePushItems) { if (singlePushItems.Any() && position > 0 && position <= list.Count) { // 转换为0-based索引插入 list.Insert(position - 1, new ProductInfo { IsContentPush = true, PushType = "SingleColumn", PushContent = singlePushItems[0]["Content"] }); } }
修复说明
- 增量计数替代模运算:通过记录
lastPushPosition,计算当前产品与上次推送的间隔数,严格遵循需求中的推送周期,彻底解决大数量产品时的位置错位问题。 - 边界情况处理:对空产品列表、空推送内容列表做兜底处理,避免空引用异常。
- 固定位置插入优化:采用倒序插入的方式,防止插入操作导致后续元素索引偏移,确保固定位置内容准确插入。
- 代码模块化:将推送内容的添加逻辑拆分为辅助方法,提升代码可读性和维护性。
内容的提问来源于stack exchange,提问作者Bavyasrisekar
相关产品推荐
相关产品推荐

