| | |
| | | Integer oldSort = currentGoods != null ? currentGoods.getGoodsSort() : null; |
| | | Integer newSort = req.getSort(); |
| | | |
| | | // 收集需要更新ES的商品ID |
| | | List<String> goodsIdsToUpdateEs = new ArrayList<>(); |
| | | goodsIdsToUpdateEs.add(req.getGoodsId()); |
| | | |
| | | // 处理数据库中的重复排序值,确保唯一性 |
| | | resolveDuplicateSorts(); |
| | | List<String> duplicateResolvedGoodsIds = resolveDuplicateSorts(); |
| | | goodsIdsToUpdateEs.addAll(duplicateResolvedGoodsIds); |
| | | |
| | | // 重新获取当前商品的排序值(可能已被resolveDuplicateSorts修改) |
| | | currentGoods = this.getById(req.getGoodsId()); |
| | |
| | | // 根据操作类型执行不同的排序逻辑 |
| | | if (oldSort == null) { |
| | | // 新增排序 |
| | | adjustForInsert(newSort); |
| | | List<String> affectedGoodsIds = adjustForInsert(newSort); |
| | | goodsIdsToUpdateEs.addAll(affectedGoodsIds); |
| | | // 处理可能产生的重复排序值 |
| | | List<String> afterInsertResolvedGoodsIds = resolveDuplicateSorts(); |
| | | goodsIdsToUpdateEs.addAll(afterInsertResolvedGoodsIds); |
| | | } else if (!oldSort.equals(newSort)) { |
| | | // 修改排序 |
| | | if (newSort > oldSort) { |
| | | // 排序值增大:将大于等于新排序值的商品+1,直到遇到断层 |
| | | adjustForIncrease(newSort); |
| | | List<String> affectedGoodsIds = adjustForIncrease(newSort); |
| | | goodsIdsToUpdateEs.addAll(affectedGoodsIds); |
| | | // 处理可能产生的重复排序值 |
| | | List<String> afterIncreaseResolvedGoodsIds = resolveDuplicateSorts(); |
| | | goodsIdsToUpdateEs.addAll(afterIncreaseResolvedGoodsIds); |
| | | } else { |
| | | // 排序值减小:将大于等于新排序值且小于原排序值的商品+1,直到遇到断层 |
| | | adjustForDecrease(oldSort, newSort); |
| | | List<String> affectedGoodsIds = adjustForDecrease(oldSort, newSort); |
| | | goodsIdsToUpdateEs.addAll(affectedGoodsIds); |
| | | // 处理可能产生的重复排序值 |
| | | List<String> afterDecreaseResolvedGoodsIds = resolveDuplicateSorts(); |
| | | goodsIdsToUpdateEs.addAll(afterDecreaseResolvedGoodsIds); |
| | | } |
| | | } |
| | | |
| | |
| | | result = this.update(updateWrapper); |
| | | |
| | | // 更新ES索引 |
| | | this.updateEsGoods(Collections.singletonList(req.getGoodsId())); |
| | | this.updateEsGoods(goodsIdsToUpdateEs); |
| | | |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * 处理数据库中的重复排序值,确保唯一性 |
| | | * @return 受影响的商品ID列表 |
| | | */ |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void resolveDuplicateSorts() { |
| | | public List<String> resolveDuplicateSorts() { |
| | | // 查询所有有排序值的商品 |
| | | LambdaQueryWrapper<Goods> queryWrapper = new LambdaQueryWrapper<>(); |
| | | queryWrapper.isNotNull(Goods::getGoodsSort); |
| | |
| | | } |
| | | } |
| | | |
| | | // 收集受影响的商品ID |
| | | List<String> affectedGoodsIds = toUpdate.stream() |
| | | .map(Goods::getId) |
| | | .collect(Collectors.toList()); |
| | | |
| | | // 批量更新 |
| | | if (!toUpdate.isEmpty()) { |
| | | this.updateBatchById(toUpdate); |
| | | } |
| | | |
| | | return affectedGoodsIds; |
| | | } |
| | | |
| | | /** |
| | |
| | | * 插入新排序值的处理逻辑 |
| | | * 将所有大于等于新排序值的商品排序值+1,直到遇到断层 |
| | | * @param newSort 新的排序值 |
| | | * @return 受影响的商品ID列表 |
| | | */ |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void adjustForInsert(Integer newSort) { |
| | | if (newSort == null) return; |
| | | public List<String> adjustForInsert(Integer newSort) { |
| | | if (newSort == null) return Collections.emptyList(); |
| | | |
| | | // 查询所有大于等于新排序值的商品,按排序值升序排列 |
| | | LambdaQueryWrapper<Goods> query = Wrappers.<Goods>lambdaQuery(); |
| | |
| | | } |
| | | } |
| | | |
| | | // 收集受影响的商品ID |
| | | List<String> affectedGoodsIds = toUpdate.stream() |
| | | .map(Goods::getId) |
| | | .collect(Collectors.toList()); |
| | | |
| | | // 批量更新 |
| | | if (!toUpdate.isEmpty()) { |
| | | this.updateBatchById(toUpdate); |
| | | } |
| | | |
| | | return affectedGoodsIds; |
| | | } |
| | | |
| | | /** |
| | | * 排序值增大的处理逻辑 |
| | | * 将所有大于等于新排序值的商品排序值+1,直到遇到断层 |
| | | * @param newSort 新排序值 |
| | | * @return 受影响的商品ID列表 |
| | | */ |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void adjustForIncrease(Integer newSort) { |
| | | if (newSort == null) return; |
| | | public List<String> adjustForIncrease(Integer newSort) { |
| | | if (newSort == null) return Collections.emptyList(); |
| | | |
| | | // 查询所有大于等于新排序值的商品,按排序值升序排列 |
| | | LambdaQueryWrapper<Goods> query = Wrappers.<Goods>lambdaQuery(); |
| | |
| | | } |
| | | } |
| | | |
| | | // 收集受影响的商品ID |
| | | List<String> affectedGoodsIds = toUpdate.stream() |
| | | .map(Goods::getId) |
| | | .collect(Collectors.toList()); |
| | | |
| | | // 批量更新 |
| | | if (!toUpdate.isEmpty()) { |
| | | this.updateBatchById(toUpdate); |
| | | } |
| | | |
| | | return affectedGoodsIds; |
| | | } |
| | | |
| | | /** |
| | |
| | | * 将大于等于新排序值且小于原排序值的商品排序值+1,直到遇到断层 |
| | | * @param oldSort 原排序值 |
| | | * @param newSort 新排序值 |
| | | * @return 受影响的商品ID列表 |
| | | */ |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void adjustForDecrease(Integer oldSort, Integer newSort) { |
| | | if (oldSort == null || newSort == null || oldSort.equals(newSort)) return; |
| | | public List<String> adjustForDecrease(Integer oldSort, Integer newSort) { |
| | | if (oldSort == null || newSort == null || oldSort.equals(newSort)) return Collections.emptyList(); |
| | | |
| | | // 查询大于等于新排序值且小于原排序值的商品,按排序值降序排列 |
| | | // 查询大于等于新排序值且小于原排序值的商品,按排序值升序排列 |
| | | LambdaQueryWrapper<Goods> query = Wrappers.<Goods>lambdaQuery(); |
| | | query.ge(Goods::getGoodsSort, newSort); |
| | | query.lt(Goods::getGoodsSort, oldSort); |
| | | query.isNotNull(Goods::getGoodsSort); |
| | | query.orderByDesc(Goods::getGoodsSort); |
| | | query.orderByAsc(Goods::getGoodsSort); |
| | | List<Goods> goodsList = this.list(query); |
| | | |
| | | // 检查连续性,遇到断层则停止 |
| | | List<Goods> toUpdate = new ArrayList<>(); |
| | | int expectedSort = oldSort - 1; |
| | | for (int i = goodsList.size() - 1; i >= 0; i--) { |
| | | Goods goods = goodsList.get(i); |
| | | int expectedSort = newSort; |
| | | for (Goods goods : goodsList) { |
| | | if (goods.getGoodsSort().equals(expectedSort)) { |
| | | goods.setGoodsSort(goods.getGoodsSort() + 1); |
| | | toUpdate.add(goods); |
| | | expectedSort--; |
| | | expectedSort++; |
| | | } else { |
| | | // 遇到断层,停止调整 |
| | | break; |
| | | } |
| | | } |
| | | |
| | | // 收集受影响的商品ID |
| | | List<String> affectedGoodsIds = toUpdate.stream() |
| | | .map(Goods::getId) |
| | | .collect(Collectors.toList()); |
| | | |
| | | // 批量更新 |
| | | if (!toUpdate.isEmpty()) { |
| | | this.updateBatchById(toUpdate); |
| | | } |
| | | |
| | | return affectedGoodsIds; |
| | | } |
| | | |
| | | @Override |