diff --git a/mall-common/src/main/java/com/suisung/mall/common/feignService/SearchService.java b/mall-common/src/main/java/com/suisung/mall/common/feignService/SearchService.java index dcf619b4..45b930b3 100644 --- a/mall-common/src/main/java/com/suisung/mall/common/feignService/SearchService.java +++ b/mall-common/src/main/java/com/suisung/mall/common/feignService/SearchService.java @@ -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 ids); + @PostMapping("/esProduct/create") CommonResult create(@RequestBody ShopProductIndex shopProductIndex); diff --git a/mall-search/src/main/java/com/suisung/mall/search/controller/EsProductController.java b/mall-search/src/main/java/com/suisung/mall/search/controller/EsProductController.java index a745a4cc..b2df924f 100644 --- a/mall-search/src/main/java/com/suisung/mall/search/controller/EsProductController.java +++ b/mall-search/src/main/java/com/suisung/mall/search/controller/EsProductController.java @@ -43,12 +43,11 @@ public class EsProductController { @ApiOperation(value = "根据id批量删除商品") @RequestMapping(value = "/delete/batch", method = RequestMethod.POST) - public CommonResult delete(@RequestParam("ids") List ids) { + public CommonResult delete(@RequestBody List ids) { esProductService.delete(ids); return CommonResult.success(); } - @ApiOperation(value = "添加商品到es") @RequestMapping(value = "/create", method = RequestMethod.POST) public CommonResult create(@RequestBody ShopProductIndex shopProductIndex) { diff --git a/mall-shop/src/main/java/com/suisung/mall/shop/product/service/impl/ShopProductBaseServiceImpl.java b/mall-shop/src/main/java/com/suisung/mall/shop/product/service/impl/ShopProductBaseServiceImpl.java index 0831a718..d2614e77 100644 --- a/mall-shop/src/main/java/com/suisung/mall/shop/product/service/impl/ShopProductBaseServiceImpl.java +++ b/mall-shop/src/main/java/com/suisung/mall/shop/product/service/impl/ShopProductBaseServiceImpl.java @@ -3444,8 +3444,10 @@ public class ShopProductBaseServiceImpl extends BaseServiceImpl 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 BaseServiceImpldelate_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 productIdList=Arrays.asList(productIds); + return removeByProductBatch(productIdList); + } + return removeByProductId(Long.valueOf(product_ids)); + } + + public boolean removeByProductBatch(List productIdList) { + UserDto user = getCurrentUser(); + if (user == null) { + throw new ApiException(ResultCode.NEED_LOGIN); + } + Integer storeId = Convert.toInt(user.getStore_id()); + QueryWrapper itemQueryWrapper = new QueryWrapper<>(); + itemQueryWrapper.in("product_id", productIdList); + if (shopOrderItemService.findOne(itemQueryWrapper) != null) { + throw new ApiException(I18nUtil._("不能删除被购买的商品!")); + } + // 不使用缓存 + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.in("product_id",productIdList); + List 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 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 productIds) { + //todo 判断商品是否参与活动, 有活动的禁止删除:【拼团、砍价 有专题页面的活动,不给删除,或者修正活动数据】 + Date time = new Date(); + QueryWrapper 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 item_rows = shopStoreActivityItemService.find(queryWrapper); + if (CollUtil.isNotEmpty(item_rows)) { + throw new ApiException(I18nUtil._("存在参与拼团及砍价活动中的商品,不可删除!")); + } + List longProductList = productIds.stream() + .map(Long::parseLong) + .collect(Collectors.toList()); + shopProductDetailService.removeBatchByIds(longProductList); + + QueryWrapper metaQueryWrapper = new QueryWrapper<>(); + queryWrapper.in("data_id", longProductList); + List key = shopProductMetaService.findKey(metaQueryWrapper); + List 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 product_ids = shopProductImageService.findKey(new QueryWrapper().in("product_id", longProductList)); + if (CollUtil.isNotEmpty(product_ids)) { + shopProductImageService.remove(product_ids); + } + + shopProductAnalyticsService.removeBatchByIds(longProductList); + + List product_item_ids = shopProductItemService.findKey(new QueryWrapper().in("product_id", longProductList)); + if (CollUtil.isNotEmpty(product_item_ids)) { + shopProductItemService.removeBatchByIds(product_item_ids); + + QueryWrapper cartQueryWrapper = new QueryWrapper<>(); + cartQueryWrapper.in("item_id", product_item_ids); + shopUserCartService.remove(cartQueryWrapper); + + QueryWrapper seqQueryWrapper = new QueryWrapper<>(); + seqQueryWrapper.in("item_id", product_item_ids); + shopProductItemSeqService.remove(seqQueryWrapper); + } + + QueryWrapper itemQueryWrapper = new QueryWrapper<>(); + itemQueryWrapper.in("product_id", longProductList); + List items= shopStoreActivityItemService.list(itemQueryWrapper); + List 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; }