增加店铺商品索引操作

This commit is contained in:
paulGao 2022-04-20 09:39:03 +08:00
parent 37c5ce541b
commit 0203630d6a
10 changed files with 135 additions and 17 deletions

View File

@ -140,6 +140,14 @@ public class GoodsMessageListener implements RocketMQListener<MessageExt> {
log.error("生成商品索引事件执行异常,商品信息 {}", new String(messageExt.getBody()));
}
break;
case GENERATOR_STORE_GOODS_INDEX:
try {
String storeId = new String(messageExt.getBody());
this.updateGoodsIndex(storeId);
} catch (Exception e) {
log.error("生成店铺商品索引事件执行异常,商品信息 {}", new String(messageExt.getBody()));
}
break;
case UPDATE_GOODS_INDEX_PROMOTIONS:
this.updateGoodsIndexPromotions(new String(messageExt.getBody()));
break;
@ -220,6 +228,14 @@ public class GoodsMessageListener implements RocketMQListener<MessageExt> {
List<String> skuIds = JSONUtil.toList(message, String.class);
goodsCollectionService.deleteSkuCollection(skuIds);
break;
case STORE_GOODS_DELETE:
try {
String storeId = new String(messageExt.getBody());
goodsIndexService.deleteIndex(MapUtil.builder(new HashMap<String, Object>()).put("storeId", storeId).build());
} catch (Exception e) {
log.error("删除店铺商品索引事件执行异常,商品信息 {}", new String(messageExt.getBody()));
}
break;
//商品评价
case GOODS_COMMENT_COMPLETE:
MemberEvaluation memberEvaluation = JSONUtil.toBean(new String(messageExt.getBody()), MemberEvaluation.class);
@ -308,6 +324,22 @@ public class GoodsMessageListener implements RocketMQListener<MessageExt> {
goodsIndexService.updateBulkIndex(goodsIndices);
}
/**
* 更新商品索引根据店铺id
*
* @param storeId 店铺id
*/
private void updateGoodsIndex(String storeId) {
//如果商品通过审核&&并且已上架
GoodsSearchParams searchParams = new GoodsSearchParams();
searchParams.setStoreId(storeId);
for (Goods goods : this.goodsService.queryListByParams(searchParams)) {
this.updateGoodsIndex(goods);
}
}
/**
* 更新商品索引
*

View File

@ -10,10 +10,15 @@ import org.springframework.context.ApplicationEvent;
@Data
public class GeneratorEsGoodsIndexEvent extends ApplicationEvent {
private String goodsId;
private static final long serialVersionUID = -6206752641309458207L;
public GeneratorEsGoodsIndexEvent(Object source, String goodsId) {
private String id;
private String tag;
public GeneratorEsGoodsIndexEvent(Object source, String tag, String id) {
super(source);
this.goodsId = goodsId;
this.tag = tag;
this.id = id;
}
}

View File

@ -3,7 +3,6 @@ package cn.lili.modules.goods.listener;
import cn.lili.common.properties.RocketmqCustomProperties;
import cn.lili.modules.goods.event.GeneratorEsGoodsIndexEvent;
import cn.lili.rocketmq.RocketmqSendCallbackBuilder;
import cn.lili.rocketmq.tags.GoodsTagsEnum;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@ -30,9 +29,9 @@ public class GeneratorEsGoodsIndexListener {
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
public void generatorEsGoodsIndex(GeneratorEsGoodsIndexEvent esGoodsIndexEvent) {
String destination = rocketmqCustomProperties.getGoodsTopic() + ":" + GoodsTagsEnum.GENERATOR_GOODS_INDEX.name();
String destination = rocketmqCustomProperties.getGoodsTopic() + ":" + esGoodsIndexEvent.getTag();
//发送mq消息
rocketMQTemplate.asyncSend(destination, esGoodsIndexEvent.getGoodsId(), RocketmqSendCallbackBuilder.commonCallback());
rocketMQTemplate.asyncSend(destination, esGoodsIndexEvent.getId(), RocketmqSendCallbackBuilder.commonCallback());
}
}

View File

@ -112,6 +112,17 @@ public interface GoodsService extends IService<Goods> {
*/
Boolean updateGoodsMarketAble(List<String> goodsIds, GoodsStatusEnum goodsStatusEnum, String underReason);
/**
* 更新商品上架状态状态
*
* @param storeId 店铺ID
* @param goodsStatusEnum 更新的商品状态
* @param underReason 下架原因
* @return 更新结果
*/
Boolean updateGoodsMarketAbleByStoreId(String storeId, GoodsStatusEnum goodsStatusEnum, String underReason);
/**
* 更新商品上架状态状态
*

View File

@ -151,6 +151,15 @@ public interface GoodsSkuService extends IService<GoodsSku> {
*/
void updateGoodsSkuStatus(Goods goods);
/**
* 更新商品sku状态根据店铺id
*
* @param storeId 店铺id
* @param marketEnable 市场启用状态
* @param authFlag 审核状态
*/
void updateGoodsSkuStatusByStoreId(String storeId, String marketEnable, String authFlag);
/**
* 发送生成ES商品索引
*

View File

@ -316,6 +316,29 @@ public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements
return result;
}
/**
* 更新商品上架状态状态
*
* @param storeId 店铺ID
* @param goodsStatusEnum 更新的商品状态
* @param underReason 下架原因
* @return 更新结果
*/
@Override
public Boolean updateGoodsMarketAbleByStoreId(String storeId, GoodsStatusEnum goodsStatusEnum, String underReason) {
boolean result;
LambdaUpdateWrapper<Goods> updateWrapper = this.getUpdateWrapperByStoreAuthority();
updateWrapper.set(Goods::getMarketEnable, goodsStatusEnum.name());
updateWrapper.set(Goods::getUnderMessage, underReason);
updateWrapper.eq(Goods::getStoreId, storeId);
result = this.update(updateWrapper);
//修改规格商品
this.goodsSkuService.updateGoodsSkuStatusByStoreId(storeId, goodsStatusEnum.name(), null);
return result;
}
@Override
@Transactional(rollbackFor = Exception.class)
public Boolean managerUpdateGoodsMarketAble(List<String> goodsIds, GoodsStatusEnum goodsStatusEnum, String underReason) {

View File

@ -325,7 +325,8 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
@Transactional(rollbackFor = Exception.class)
public void updateGoodsSkuStatus(Goods goods) {
LambdaUpdateWrapper<GoodsSku> updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.eq(GoodsSku::getGoodsId, goods.getId());
updateWrapper.eq(CharSequenceUtil.isNotEmpty(goods.getId()), GoodsSku::getGoodsId, goods.getId());
updateWrapper.eq(CharSequenceUtil.isNotEmpty(goods.getStoreId()), GoodsSku::getStoreId, goods.getStoreId());
updateWrapper.set(GoodsSku::getMarketEnable, goods.getMarketEnable());
updateWrapper.set(GoodsSku::getAuthFlag, goods.getAuthFlag());
updateWrapper.set(GoodsSku::getDeleteFlag, goods.getDeleteFlag());
@ -342,6 +343,31 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
}
}
/**
* 更新商品sku状态根据店铺id
*
* @param storeId 店铺id
* @param marketEnable 市场启用状态
* @param authFlag 审核状态
*/
@Override
@Transactional(rollbackFor = Exception.class)
public void updateGoodsSkuStatusByStoreId(String storeId, String marketEnable, String authFlag) {
LambdaUpdateWrapper<GoodsSku> updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.eq(GoodsSku::getStoreId, storeId);
updateWrapper.set(CharSequenceUtil.isNotEmpty(marketEnable), GoodsSku::getMarketEnable, marketEnable);
updateWrapper.set(CharSequenceUtil.isNotEmpty(authFlag), GoodsSku::getAuthFlag, authFlag);
boolean update = this.update(updateWrapper);
if (Boolean.TRUE.equals(update)) {
if (GoodsStatusEnum.UPPER.name().equals(marketEnable)) {
applicationEventPublisher.publishEvent(new GeneratorEsGoodsIndexEvent("生成店铺商品", GoodsTagsEnum.GENERATOR_STORE_GOODS_INDEX.name(), storeId));
} else if (GoodsStatusEnum.DOWN.name().equals(marketEnable)) {
cache.vagueDel(CachePrefix.GOODS_SKU.getPrefix());
applicationEventPublisher.publishEvent(new GeneratorEsGoodsIndexEvent("删除店铺商品", GoodsTagsEnum.STORE_GOODS_DELETE.name(), storeId));
}
}
}
@Override
public List<GoodsSku> getGoodsSkuByIdFromCache(List<String> ids) {
List<String> keys = new ArrayList<>();
@ -563,7 +589,7 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
if (!GoodsStatusEnum.UPPER.name().equals(goods.getMarketEnable()) || !GoodsAuthEnum.PASS.name().equals(goods.getAuthFlag())) {
return;
}
applicationEventPublisher.publishEvent(new GeneratorEsGoodsIndexEvent("生成商品索引事件", goods.getId()));
applicationEventPublisher.publishEvent(new GeneratorEsGoodsIndexEvent("生成商品", GoodsTagsEnum.GENERATOR_GOODS_INDEX.name(), goods.getId()));
}
/**

View File

@ -71,9 +71,9 @@ public interface EsGoodsIndexService {
/**
* 删除索引
*
* @param goods 商品索引信息
* @param queryFields 查询条件 (key 为字段value为字段值)
*/
void deleteIndex(EsGoodsIndex goods);
void deleteIndex(Map<String, Object> queryFields);
/**
* 删除索引

View File

@ -59,7 +59,6 @@ import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchPage;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import java.io.IOException;
import java.lang.reflect.Field;
@ -287,13 +286,20 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements
}
}
/**
* 删除索引
*
* @param queryFields 查询条件
*/
@Override
public void deleteIndex(EsGoodsIndex goods) {
if (ObjectUtils.isEmpty(goods)) {
//如果对象为空则删除全量
goodsIndexRepository.deleteAll();
public void deleteIndex(Map<String, Object> queryFields) {
NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
for (Map.Entry<String, Object> entry : queryFields.entrySet()) {
boolQueryBuilder.filter(QueryBuilders.termsQuery(entry.getKey(), entry.getValue()));
}
goodsIndexRepository.delete(goods);
queryBuilder.withQuery(boolQueryBuilder);
this.restTemplate.delete(queryBuilder.build(), EsGoodsIndex.class);
}
/**
@ -316,7 +322,6 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements
NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
queryBuilder.withQuery(QueryBuilders.termsQuery("id", ids.toArray()));
this.restTemplate.delete(queryBuilder.build(), EsGoodsIndex.class);
}
@Override

View File

@ -10,6 +10,10 @@ public enum GoodsTagsEnum {
* "生成商品索引"
*/
GENERATOR_GOODS_INDEX("生成商品索引"),
/**
* "生成店铺商品索引"
*/
GENERATOR_STORE_GOODS_INDEX("生成店铺商品索引"),
/**
* "更新商品索引"
*/
@ -31,6 +35,10 @@ public enum GoodsTagsEnum {
* "重置商品索引"
*/
RESET_GOODS_INDEX("重置商品索引"),
/**
* "删除店铺商品索引"
*/
STORE_GOODS_DELETE("删除店铺商品索引"),
/**
* "删除商品"
*/