diff --git a/consumer/src/main/java/cn/lili/listener/GoodsMessageListener.java b/consumer/src/main/java/cn/lili/listener/GoodsMessageListener.java index 790b3b9d..f7a369b0 100644 --- a/consumer/src/main/java/cn/lili/listener/GoodsMessageListener.java +++ b/consumer/src/main/java/cn/lili/listener/GoodsMessageListener.java @@ -259,6 +259,7 @@ public class GoodsMessageListener implements RocketMQListener { searchParams.setCategoryPath(promotions.getScopeId()); List goodsSkuByList = this.goodsSkuService.getGoodsSkuByList(searchParams); List skuIds = goodsSkuByList.stream().map(GoodsSku::getId).collect(Collectors.toList()); + this.goodsIndexService.deleteEsGoodsPromotionByPromotionId(skuIds, promotions.getId()); this.goodsIndexService.updateEsGoodsIndexPromotions(skuIds, promotions, esPromotionKey); } else if (PromotionsScopeTypeEnum.ALL.name().equals(promotions.getScopeType())) { this.goodsIndexService.updateEsGoodsIndexAllByList(promotions, esPromotionKey); diff --git a/framework/src/main/java/cn/lili/modules/promotion/entity/dos/BasePromotions.java b/framework/src/main/java/cn/lili/modules/promotion/entity/dos/BasePromotions.java index 0ec44505..f3366dde 100644 --- a/framework/src/main/java/cn/lili/modules/promotion/entity/dos/BasePromotions.java +++ b/framework/src/main/java/cn/lili/modules/promotion/entity/dos/BasePromotions.java @@ -7,6 +7,9 @@ import com.fasterxml.jackson.annotation.JsonFormat; import io.swagger.annotations.ApiModelProperty; import lombok.Data; import lombok.EqualsAndHashCode; +import org.springframework.data.elasticsearch.annotations.DateFormat; +import org.springframework.data.elasticsearch.annotations.Field; +import org.springframework.data.elasticsearch.annotations.FieldType; import javax.validation.constraints.NotEmpty; import java.util.Date; @@ -35,10 +38,12 @@ public class BasePromotions extends BaseEntity { @ApiModelProperty(value = "活动开始时间", required = true) @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss") + @Field(type = FieldType.Date, format = DateFormat.custom, pattern = "yyyy-MM-dd HH:mm:ss || yyyy-MM-dd || yyyy/MM/dd HH:mm:ss|| yyyy/MM/dd ||epoch_millis") private Date startTime; @ApiModelProperty(value = "活动结束时间", required = true) @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss") + @Field(type = FieldType.Date, format = DateFormat.custom, pattern = "yyyy-MM-dd HH:mm:ss || yyyy-MM-dd || yyyy/MM/dd HH:mm:ss|| yyyy/MM/dd ||epoch_millis") private Date endTime; /** 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 35519572..86853a28 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 @@ -425,6 +425,8 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements // 查询出全部商品 goodsIndices = new ArrayList<>(IterableUtil.toCollection(all)); } + List skuIds = goodsIndices.stream().map(EsGoodsIndex::getId).collect(Collectors.toList()); + this.deleteEsGoodsPromotionByPromotionId(skuIds, promotion.getId()); //更新商品索引 for (EsGoodsIndex goodsIndex : goodsIndices) { this.updateGoodsIndexPromotion(goodsIndex, key, promotion); @@ -481,11 +483,11 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements private void removePromotionByPromotionId(EsGoodsIndex goodsIndex, String promotionId) { Map promotionMap = goodsIndex.getPromotionMap(); if (promotionMap != null && !promotionMap.isEmpty()) { - //如果存在同类型促销活动删除 + //如果存在同促销ID的活动删除 List collect = promotionMap.keySet().stream().filter(i -> i.split("-")[1].equals(promotionId)).collect(Collectors.toList()); collect.forEach(promotionMap::remove); goodsIndex.setPromotionMap(promotionMap); - updateIndex(goodsIndex); + this.updatePromotionsByScript(goodsIndex.getId(), promotionMap); } } @@ -611,8 +613,25 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements } else { promotionMap.put(key, promotion); } - goodsIndex.setPromotionMap(promotionMap); - updateIndex(goodsIndex); + this.updatePromotionsByScript(goodsIndex.getId(), promotionMap); + } + + /** + * 以更新部分字段的方式更新索引促销信息 + * + * @param id 索引id + * @param promotionMap 促销信息 + */ + private void updatePromotionsByScript(String id, Map promotionMap) { + JSONObject jsonObject = JSONUtil.parseObj(promotionMap); + jsonObject.setDateFormat("yyyy-MM-dd HH:mm:ss"); + String s = jsonObject.toString(); + String promotionsStr = s.replace("{", "[").replace("}", "]"); + + UpdateByQueryRequest update = new UpdateByQueryRequest(getIndexName()); + update.setQuery(QueryBuilders.boolQuery().filter(QueryBuilders.termsQuery("id", id))); + update.setScript(new Script("ctx._source." + "promotionMap" + "=" + promotionsStr + ";")); + client.updateByQueryAsync(update, RequestOptions.DEFAULT, this.actionListener()); } /** diff --git a/framework/src/main/java/cn/lili/mybatis/BaseEntity.java b/framework/src/main/java/cn/lili/mybatis/BaseEntity.java index 44dbf740..ab73d591 100644 --- a/framework/src/main/java/cn/lili/mybatis/BaseEntity.java +++ b/framework/src/main/java/cn/lili/mybatis/BaseEntity.java @@ -13,6 +13,9 @@ import org.springframework.data.annotation.CreatedBy; import org.springframework.data.annotation.CreatedDate; import org.springframework.data.annotation.LastModifiedBy; import org.springframework.data.annotation.LastModifiedDate; +import org.springframework.data.elasticsearch.annotations.DateFormat; +import org.springframework.data.elasticsearch.annotations.Field; +import org.springframework.data.elasticsearch.annotations.FieldType; import org.springframework.format.annotation.DateTimeFormat; import java.io.Serializable; @@ -50,6 +53,7 @@ public abstract class BaseEntity implements Serializable { @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @TableField(fill = FieldFill.INSERT) @ApiModelProperty(value = "创建时间", hidden = true) + @Field(type = FieldType.Date, format = DateFormat.custom, pattern = "yyyy-MM-dd HH:mm:ss || yyyy-MM-dd || yyyy/MM/dd HH:mm:ss|| yyyy/MM/dd ||epoch_millis") private Date createTime; @LastModifiedBy @@ -62,6 +66,7 @@ public abstract class BaseEntity implements Serializable { @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @TableField(fill = FieldFill.UPDATE) @ApiModelProperty(value = "更新时间", hidden = true) + @Field(type = FieldType.Date, format = DateFormat.custom, pattern = "yyyy-MM-dd HH:mm:ss || yyyy-MM-dd || yyyy/MM/dd HH:mm:ss|| yyyy/MM/dd ||epoch_millis") private Date updateTime; @TableField(fill = FieldFill.INSERT)