商品批量删除接口优化

This commit is contained in:
liyj 2025-07-09 11:09:06 +08:00
parent 9a94d9addd
commit 774d197c55
3 changed files with 133 additions and 19 deletions

View File

@ -5,10 +5,7 @@ import com.suisung.mall.common.modules.product.ShopProductIndex;
import com.suisung.mall.common.pojo.dto.ProductRecommendDTO;
import com.suisung.mall.common.pojo.dto.ProductSearchDTO;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@ -25,6 +22,9 @@ public interface SearchService {
@GetMapping("/esProduct/delete/{id}")
CommonResult delete(@PathVariable(value = "id") Long id);
@PostMapping("/esProduct/delete/batch")
CommonResult delete(@RequestBody List<Long> ids);
@PostMapping("/esProduct/create")
CommonResult create(@RequestBody ShopProductIndex shopProductIndex);

View File

@ -43,12 +43,11 @@ public class EsProductController {
@ApiOperation(value = "根据id批量删除商品")
@RequestMapping(value = "/delete/batch", method = RequestMethod.POST)
public CommonResult delete(@RequestParam("ids") List<Long> ids) {
public CommonResult delete(@RequestBody List<Long> ids) {
esProductService.delete(ids);
return CommonResult.success();
}
@ApiOperation(value = "添加商品到es")
@RequestMapping(value = "/create", method = RequestMethod.POST)
public CommonResult create(@RequestBody ShopProductIndex shopProductIndex) {

View File

@ -3444,8 +3444,10 @@ public class ShopProductBaseServiceImpl extends BaseServiceImpl<ShopProductBaseM
QueryWrapper<ShopStoreActivityItem> itemQueryWrapper = new QueryWrapper<>();
itemQueryWrapper.eq("product_id", product_id);
shopStoreActivityItemService.remove(itemQueryWrapper);
ShopStoreActivityItem shopStoreActivityItem=shopStoreActivityItemService.findOne(itemQueryWrapper);
if(null!=shopStoreActivityItem){
shopStoreActivityItemService.remove(shopStoreActivityItem.getActivity_item_id());
}
if (!remove(product_id)) {
throw new ApiException(I18nUtil._("删除商品基础表失败!"));
}
@ -6236,17 +6238,130 @@ public class ShopProductBaseServiceImpl extends BaseServiceImpl<ShopProductBaseM
if(productIds.length>delate_batch_limit){
throw new ApiException("最大删除数量为100");
}
Arrays.stream(productIds)
.filter(Objects::nonNull) // 先过滤null
.distinct().forEach(productId->{
try {
removeByProductId(Long.valueOf(productId));
}catch (RuntimeException e){
throw new ApiException(e.getMessage());
}
});
}else {
removeByProductId(Long.valueOf(product_ids));
List<String> productIdList=Arrays.asList(productIds);
return removeByProductBatch(productIdList);
}
return removeByProductId(Long.valueOf(product_ids));
}
public boolean removeByProductBatch(List<String> productIdList) {
UserDto user = getCurrentUser();
if (user == null) {
throw new ApiException(ResultCode.NEED_LOGIN);
}
Integer storeId = Convert.toInt(user.getStore_id());
QueryWrapper<ShopOrderItem> itemQueryWrapper = new QueryWrapper<>();
itemQueryWrapper.in("product_id", productIdList);
if (shopOrderItemService.findOne(itemQueryWrapper) != null) {
throw new ApiException(I18nUtil._("不能删除被购买的商品!"));
}
// 不使用缓存
QueryWrapper<ShopProductBase> queryWrapper = new QueryWrapper<>();
queryWrapper.in("product_id",productIdList);
List<ShopProductBase> shopProductBaseList= shopProductBaseService.list(queryWrapper);
if(shopProductBaseList.size()!=productIdList.size()){
throw new ApiException("存在商品编号不存在,请检查");
}
if (getCurrentUser().isPlatform() || batchCheckDataRights(storeId, shopProductBaseList)) {
if (!removeProductBatch(productIdList)) {
throw new ApiException(ResultCode.FAILED);
}
//更新商品数量统计
if (!shopStoreAnalyticsService.saveProductAnalyticsNum(shopProductBaseList.get(0).getStore_id())) {
throw new ApiException(ResultCode.FAILED);
}
// String message_id = "notice-of-deleting-goods";
// HashMap args = new HashMap();
// args.put("product_id", product_id);
// args.put("des", String.format(I18nUtil._("商品: %s 被平台删除,如有疑问请联系平台。"), product_id));
// messageService.sendNoticeMsg(0, shopProductBase.getStore_id(), message_id, args);
return true;
}
return false;
}
private boolean batchCheckDataRights(Integer storeId, List<ShopProductBase> shopProductBaseList){
if(shopProductBaseList.isEmpty()){
return false;
}
for (ShopProductBase shopProductBase:shopProductBaseList){
if(shopProductBase.getStore_id().intValue()!=storeId.intValue()){
return false;
}
}
return true;
}
public boolean removeProductBatch(List<String> productIds) {
//todo 判断商品是否参与活动 有活动的禁止删除拼团砍价 有专题页面的活动不给删除或者修正活动数据
Date time = new Date();
QueryWrapper<ShopStoreActivityItem> queryWrapper = new QueryWrapper<>();
queryWrapper.in("product_id", productIds)
.lt("activity_item_starttime", time)
.gt("activity_item_endtime", time)
.in("activity_type_id", StateCode.ACTIVITY_TYPE_GROUPBOOKING, StateCode.ACTIVITY_TYPE_CUTPRICE);
List<ShopStoreActivityItem> item_rows = shopStoreActivityItemService.find(queryWrapper);
if (CollUtil.isNotEmpty(item_rows)) {
throw new ApiException(I18nUtil._("存在参与拼团及砍价活动中的商品,不可删除!"));
}
List<Long> longProductList = productIds.stream()
.map(Long::parseLong)
.collect(Collectors.toList());
shopProductDetailService.removeBatchByIds(longProductList);
QueryWrapper<ShopProductMeta> metaQueryWrapper = new QueryWrapper<>();
queryWrapper.in("data_id", longProductList);
List<Serializable> key = shopProductMetaService.findKey(metaQueryWrapper);
List<Integer> meta_id_row = Convert.toList(Integer.class, key);
if (CollUtil.isNotEmpty(meta_id_row)) {
shopProductMetaService.removeBatchByIds(meta_id_row);
}
shopProductValidPeriodService.removeBatchByIds(longProductList);
shopProductPreSaleService.removeBatchByIds(longProductList);
shopProductDataService.removeBatchByIds(longProductList);
shopProductInfoService.removeBatchByIds(longProductList);
shopProductIndexService.removeBatchByIds(longProductList);
List<Serializable> product_ids = shopProductImageService.findKey(new QueryWrapper<ShopProductImage>().in("product_id", longProductList));
if (CollUtil.isNotEmpty(product_ids)) {
shopProductImageService.remove(product_ids);
}
shopProductAnalyticsService.removeBatchByIds(longProductList);
List<Serializable> product_item_ids = shopProductItemService.findKey(new QueryWrapper<ShopProductItem>().in("product_id", longProductList));
if (CollUtil.isNotEmpty(product_item_ids)) {
shopProductItemService.removeBatchByIds(product_item_ids);
QueryWrapper<ShopUserCart> cartQueryWrapper = new QueryWrapper<>();
cartQueryWrapper.in("item_id", product_item_ids);
shopUserCartService.remove(cartQueryWrapper);
QueryWrapper<ShopProductItemSeq> seqQueryWrapper = new QueryWrapper<>();
seqQueryWrapper.in("item_id", product_item_ids);
shopProductItemSeqService.remove(seqQueryWrapper);
}
QueryWrapper<ShopStoreActivityItem> itemQueryWrapper = new QueryWrapper<>();
itemQueryWrapper.in("product_id", longProductList);
List<ShopStoreActivityItem> items= shopStoreActivityItemService.list(itemQueryWrapper);
List<Long> itemIds=items.stream().map(ShopStoreActivityItem::getActivity_item_id).collect(Collectors.toList());
if(CollectionUtil.isNotEmpty(itemIds)){
shopStoreActivityItemService.removeBatchByIds(itemIds);
}
if (!removeBatchByIds(longProductList)) {
throw new ApiException(I18nUtil._("删除商品基础表失败!"));
}
// 是否启用es
boolean esearch_enable = accountBaseConfigService.getConfig("esearch_enable", false);
//调用es删除
if (esearch_enable) {
searchService.delete(longProductList);
}
return true;
}