!361 fix & improve

Merge pull request !361 from OceansDeep/pg
This commit is contained in:
OceansDeep 2024-12-11 08:20:07 +00:00 committed by Gitee
commit b2e0fe1de4
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
2 changed files with 59 additions and 20 deletions

View File

@ -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<M extends BaseMapper<T>, T e
* 更新促销商品信息
*
* @param promotions 促销实体
* @return
* @return 是否更新成功
*/
@Override
@Transactional(rollbackFor = {Exception.class})
@ -283,12 +288,53 @@ public abstract class AbstractPromotionsServiceImpl<M extends BaseMapper<T>, T e
if (promotions.getStartTime() == null || promotions.getEndTime() == null) {
return;
}
QueryWrapper<T> 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<T> 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 <T> 促销活动类型
*/
private static <T extends BasePromotions> @NotNull QueryWrapper<T> getSameWrapper(T promotions) {
QueryWrapper<T> 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));
}
}

View File

@ -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<BulkByScrollResponse>() {
@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);
}
}