From 2f180a73edb609c1a5272c026711a75e7e2d6372 Mon Sep 17 00:00:00 2001 From: misworga831 Date: Wed, 11 Dec 2024 16:18:37 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix:=20=E4=BC=98=E5=8C=96=E7=A7=AF=E5=88=86?= =?UTF-8?q?=E5=95=86=E5=93=81=EF=BC=8C=E7=A7=BB=E9=99=A4=E5=BC=82=E6=AD=A5?= =?UTF-8?q?=E5=88=A0=E9=99=A4=E7=B4=A2=E5=BC=95=20(pg)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 重构deleteByQuery方法,移除异步调用,改为同步处理。 添加了异常处理以记录删除索引时的错误日志。 --- .../serviceimpl/EsGoodsIndexServiceImpl.java | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/framework/src/main/java/cn/lili/modules/search/serviceimpl/EsGoodsIndexServiceImpl.java b/framework/src/main/java/cn/lili/modules/search/serviceimpl/EsGoodsIndexServiceImpl.java index 291efa1a..89e258d3 100644 --- a/framework/src/main/java/cn/lili/modules/search/serviceimpl/EsGoodsIndexServiceImpl.java +++ b/framework/src/main/java/cn/lili/modules/search/serviceimpl/EsGoodsIndexServiceImpl.java @@ -72,6 +72,7 @@ import org.springframework.data.elasticsearch.core.SearchPage; import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder; import org.springframework.stereotype.Service; +import java.io.IOException; import java.lang.reflect.Field; import java.util.*; import java.util.concurrent.TimeUnit; @@ -484,20 +485,12 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements deleteByQueryRequest.setQuery(boolQueryBuilder); deleteByQueryRequest.indices(getIndexName()); deleteByQueryRequest.setConflicts("proceed"); - this.client.deleteByQueryAsync(deleteByQueryRequest, RequestOptions.DEFAULT, new ActionListener() { - @Override - public void onResponse(BulkByScrollResponse bulkByScrollResponse) { - if (bulkByScrollResponse.getVersionConflicts() > 0) { - throw new RetryException("删除索引失败,es内容版本冲突"); - } - } - - @Override - public void onFailure(Exception e) { - throw new RetryException("删除索引失败," + e.getMessage()); - } - }); + try { + this.client.deleteByQuery(deleteByQueryRequest, RequestOptions.DEFAULT); + } catch (IOException e) { + log.error("删除索引出现异常", e); + } } From b69a558df099367ff3776996035b2c37757f64fa Mon Sep 17 00:00:00 2001 From: misworga831 Date: Wed, 11 Dec 2024 16:18:58 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fix:=20=E4=BC=98=E5=8C=96=E7=A7=AF=E5=88=86?= =?UTF-8?q?=E5=95=86=E5=93=81=EF=BC=8C=E4=BF=AE=E5=A4=8D=E4=BF=83=E9=94=80?= =?UTF-8?q?=E6=97=B6=E9=97=B4=E6=AE=B5=E9=87=8D=E5=8F=A0=E9=97=AE=E9=A2=98?= =?UTF-8?q?=20(pg)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加方法以检查促销时间段是否重叠 - 更新文档注释以提高可读性 - 使用@NotNull注解来增强空值检查 --- .../AbstractPromotionsServiceImpl.java | 60 ++++++++++++++++--- 1 file changed, 53 insertions(+), 7 deletions(-) diff --git a/framework/src/main/java/cn/lili/modules/promotion/serviceimpl/AbstractPromotionsServiceImpl.java b/framework/src/main/java/cn/lili/modules/promotion/serviceimpl/AbstractPromotionsServiceImpl.java index 3eb688cf..799338d0 100644 --- a/framework/src/main/java/cn/lili/modules/promotion/serviceimpl/AbstractPromotionsServiceImpl.java +++ b/framework/src/main/java/cn/lili/modules/promotion/serviceimpl/AbstractPromotionsServiceImpl.java @@ -1,5 +1,6 @@ package cn.lili.modules.promotion.serviceimpl; +import cn.hutool.core.date.DateUtil; import cn.hutool.core.map.MapBuilder; import cn.hutool.core.text.CharSequenceUtil; import cn.hutool.json.JSONUtil; @@ -12,6 +13,7 @@ import cn.lili.modules.promotion.entity.dos.BasePromotions; import cn.lili.modules.promotion.entity.dos.PromotionGoods; import cn.lili.modules.promotion.entity.dto.search.BasePromotionsSearchParams; import cn.lili.modules.promotion.entity.enums.PromotionsScopeTypeEnum; +import cn.lili.modules.promotion.entity.enums.PromotionsStatusEnum; import cn.lili.modules.promotion.service.AbstractPromotionsService; import cn.lili.modules.promotion.service.PromotionGoodsService; import cn.lili.modules.promotion.tools.PromotionTools; @@ -26,8 +28,11 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationEventPublisher; import org.springframework.transaction.annotation.Transactional; +import javax.validation.constraints.NotNull; import java.util.*; +import static cn.lili.modules.promotion.tools.PromotionTools.queryPromotionStatus; + /** * @author paulG * @since 2021/11/30 @@ -215,7 +220,7 @@ public abstract class AbstractPromotionsServiceImpl, T e * 更新促销商品信息 * * @param promotions 促销实体 - * @return + * @return 是否更新成功 */ @Override @Transactional(rollbackFor = {Exception.class}) @@ -283,12 +288,53 @@ public abstract class AbstractPromotionsServiceImpl, T e if (promotions.getStartTime() == null || promotions.getEndTime() == null) { return; } - QueryWrapper queryWrapper = PromotionTools.checkActiveTime(promotions.getStartTime(), promotions.getEndTime(), this.getPromotionType(), promotions.getStoreId(), promotions.getId()); - long sameNum = this.count(queryWrapper); - //当前时间段是否存在同类活动 - if (sameNum > 0) { - throw new ServiceException(ResultCode.PROMOTION_SAME_ACTIVE_EXIST); - } + + // 遍历编辑活动开始时间的当天开始时间到结束的当天结束时间内的活动 + QueryWrapper querySameWrapper = getSameWrapper(promotions); + + this.list(querySameWrapper).forEach(promotion -> { + if (promotion.getStartTime() == null || promotion.getEndTime() == null) { + return; + } + + if (isOverlapping(promotions.getStartTime(), promotions.getEndTime(), promotion.getStartTime(), promotion.getEndTime())) { + throw new ServiceException(ResultCode.PROMOTION_SAME_ACTIVE_EXIST); + } + + }); + + } + + /** + * 获取相同时间段的促销活动 + * + * @param promotions 促销活动 + * @return 相同时间段的促销活动 + * @param 促销活动类型 + */ + private static @NotNull QueryWrapper getSameWrapper(T promotions) { + QueryWrapper querySameWrapper = new QueryWrapper<>(); + querySameWrapper.eq("store_id", promotions.getStoreId()); + querySameWrapper.eq("delete_flag", false); + querySameWrapper.ne("id", promotions.getId()); + querySameWrapper.and(i -> i.or(queryPromotionStatus(PromotionsStatusEnum.NEW)).or(queryPromotionStatus(PromotionsStatusEnum.START))); + querySameWrapper.and(i -> i.or(j -> j.between("start_time", DateUtil.beginOfDay(promotions.getStartTime()), DateUtil.endOfDay(promotions.getEndTime())) + .or().between("end_time", DateUtil.beginOfDay(promotions.getStartTime()), DateUtil.endOfDay(promotions.getEndTime())))); + return querySameWrapper; + } + + /** + * 判断两个时间段是否有交集 + * + * @param a1 原时间段开始时间 + * @param a2 原时间段结束时间 + * @param b1 新时间段开始时间 + * @param b2 新时间段结束时间 + * @return 是否有交集 + */ + public static boolean isOverlapping(Date a1, Date a2, Date b1, Date b2) { + // 两个时间段有交集的条件是:b1在a1和a2之间,或者b2在a1和a2之间,或者a1在b1和b2之间 + return (b1.before(a2) && b2.after(a1)) || (b1.before(a1) && b2.after(a2)) || (a1.before(b1) && a2.after(b2)); } }