!8 修复商品索引参数问题

Merge pull request !8 from OceansDeep/feature/pg
This commit is contained in:
OceansDeep 2021-06-21 16:10:17 +00:00 committed by Gitee
commit 5fc1e7b30f
7 changed files with 77 additions and 62 deletions

View File

@ -6,10 +6,14 @@ import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.hibernate.validator.constraints.Length;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
/**
* 商品关联参数
@ -17,6 +21,7 @@ import javax.persistence.Table;
* @author pikachu
* @date 2020-02-23 9:14:33
*/
@EqualsAndHashCode(callSuper = true)
@Data
@Entity
@Table(name = "li_goods_params")
@ -52,4 +57,11 @@ public class GoodsParams extends BaseEntity {
@Length(max = 100, message = "参数值字符不能大于120")
private String paramValue;
@TableField(value = "is_index")
@ApiModelProperty(value = "是否可索引0 不显示 1 显示", required = true)
@NotNull(message = "是否可索引必选")
@Min(value = 0, message = "是否可索引传值不正确")
@Max(value = 1, message = "是否可索引传值不正确")
private Integer isIndex = 0;
}

View File

@ -2,11 +2,13 @@ package cn.lili.modules.goods.serviceimpl;
import cn.lili.modules.goods.entity.dos.CategoryParameterGroup;
import cn.lili.modules.goods.entity.dos.GoodsParams;
import cn.lili.modules.goods.entity.dos.Parameters;
import cn.lili.modules.goods.entity.vos.GoodsParamsGroupVO;
import cn.lili.modules.goods.entity.vos.GoodsParamsVO;
import cn.lili.modules.goods.mapper.GoodsParamsMapper;
import cn.lili.modules.goods.service.CategoryParameterGroupService;
import cn.lili.modules.goods.service.GoodsParamsService;
import cn.lili.modules.goods.service.ParametersService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
@ -36,6 +38,9 @@ public class GoodsParamsServiceImpl extends ServiceImpl<GoodsParamsMapper, Goods
@Autowired
private CategoryParameterGroupService categoryParameterGroupService;
@Autowired
private ParametersService parametersService;
@Override
public void addParams(List<GoodsParams> paramList, String goodsId) {
//先删除现有商品参数
@ -43,10 +48,12 @@ public class GoodsParamsServiceImpl extends ServiceImpl<GoodsParamsMapper, Goods
//循环添加参数
if (paramList != null) {
for (GoodsParams param : paramList) {
Parameters parameters = parametersService.getById(param.getParamId());
GoodsParams goodsParams = new GoodsParams();
goodsParams.setGoodsId(goodsId);
goodsParams.setParamName(param.getParamName());
goodsParams.setParamValue(param.getParamValue());
goodsParams.setIsIndex(parameters.getIsIndex());
goodsParams.setParamId(param.getId());
this.save(goodsParams);
}

View File

@ -88,13 +88,12 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
@Override
public void add(List<Map<String, Object>> skuList, Goods goods) {
//检查是否需要生成索引
boolean needIndex = checkNeedIndex(goods);
// 检查是否需要生成索引
List<GoodsSku> newSkuList;
//如果有规格
if (skuList != null && !skuList.isEmpty()) {
//添加商品sku
newSkuList = this.addGoodsSku(skuList, goods, needIndex);
// 添加商品sku
newSkuList = this.addGoodsSku(skuList, goods);
} else {
throw new ServiceException("规格必须要有一个!");
}
@ -103,24 +102,9 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
generateEsCheck(goods);
}
private boolean checkNeedIndex(Goods goods) {
if (goods.getParams() != null && !goods.getParams().isEmpty()) {
List<GoodsParams> goodsParams = JSONUtil.toList(goods.getParams(), GoodsParams.class);
for (GoodsParams goodsParam : goodsParams) {
Parameters parameters = parametersService.getById(goodsParam.getParamId());
if (parameters.getIsIndex() == 1) {
return true;
}
}
}
return false;
}
@Override
public void update(List<Map<String, Object>> skuList, Goods goods, Boolean regeneratorSkuFlag) {
//检查是否需要生成索引
boolean needIndex = checkNeedIndex(goods);
//是否存在规格
// 是否存在规格
if (skuList == null || skuList.isEmpty()) {
throw new ServiceException("规格必须要有一个!");
}
@ -138,8 +122,8 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
this.removeByIds(oldSkuIds);
//删除sku相册
goodsGalleryService.removeByIds(oldSkuIds);
//添加商品sku
newSkuList = this.addGoodsSku(skuList, goods, needIndex);
// 添加商品sku
newSkuList = this.addGoodsSku(skuList, goods);
//发送mq消息
String destination = rocketmqCustomProperties.getGoodsTopic() + ":" + GoodsTagsEnum.SKU_DELETE.name();
@ -161,9 +145,7 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
this.updateBatchById(newSkuList);
}
this.updateStock(newSkuList);
if (Boolean.TRUE.equals(needIndex)) {
generateEsCheck(goods);
}
generateEsCheck(goods);
}
/**
@ -206,9 +188,9 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
Map<String, Object> map = new HashMap<>();
GoodsSku goodsSku = this.getGoodsSkuByIdFromCache(skuId);
GoodsVO goodsVO = goodsService.getGoodsVO(goodsId);
//如果规格为空则使用商品ID进行查询
if (goodsSku == null) {
GoodsVO goodsVO = goodsService.getGoodsVO(goodsId);
skuId = goodsVO.getSkuList().get(0).getId();
goodsSku = this.getGoodsSkuByIdFromCache(skuId);
//如果使用商品ID无法查询SKU则返回错误
@ -219,8 +201,7 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
//获取当前商品的索引信息
EsGoodsIndex goodsIndex = goodsIndexService.findById(skuId);
if (goodsIndex == null) {
goodsIndex = new EsGoodsIndex(goodsSku);
goodsIndex.setPromotionMap(promotionService.getGoodsCurrentPromotionMap(goodsIndex));
goodsIndex = goodsIndexService.resetEsGoodsIndex(goodsSku, goodsVO.getGoodsParamsList());
}
//商品规格
GoodsSkuVO goodsSkuDetail = this.getGoodsSkuVO(goodsSku);
@ -456,6 +437,10 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
for (GoodsSku goodsSku : goodsSkuList) {
EsGoodsIndex esGoodsOld = goodsIndexService.findById(goodsSku.getId());
EsGoodsIndex goodsIndex = new EsGoodsIndex(goodsSku);
if (goods.getParams() != null && !goods.getParams().isEmpty()) {
List<GoodsParams> goodsParams = JSONUtil.toList(goods.getParams(), GoodsParams.class);
goodsIndex = new EsGoodsIndex(goodsSku, goodsParams);
}
//如果商品库存不为0并且es中有数据
if (goodsSku.getQuantity() > 0 && esGoodsOld == null) {
goodsIndexService.addIndex(goodsIndex);
@ -501,7 +486,7 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
* @param skuList sku列表
* @param goods 商品信息
*/
private List<GoodsSku> addGoodsSku(List<Map<String, Object>> skuList, Goods goods, Boolean needIndex) {
private List<GoodsSku> addGoodsSku(List<Map<String, Object>> skuList, Goods goods) {
List<GoodsSku> skus = new ArrayList<>();
List<EsGoodsIndex> goodsIndices = new ArrayList<>();
for (Map<String, Object> skuVO : skuList) {
@ -517,11 +502,9 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
stringRedisTemplate.opsForValue().set(GoodsSkuService.getStockCacheKey(goodsSku.getId()), goodsSku.getQuantity().toString());
}
this.saveBatch(skus);
if (Boolean.TRUE.equals(needIndex)) {
String destination = rocketmqCustomProperties.getGoodsTopic() + ":" + GoodsTagsEnum.GENERATOR_GOODS_INDEX.name();
//发送mq消息
rocketMQTemplate.asyncSend(destination, JSONUtil.toJsonStr(goodsIndices), RocketmqSendCallbackBuilder.commonCallback());
}
String destination = rocketmqCustomProperties.getGoodsTopic() + ":" + GoodsTagsEnum.GENERATOR_GOODS_INDEX.name();
//发送mq消息
rocketMQTemplate.asyncSend(destination, JSONUtil.toJsonStr(goodsIndices), RocketmqSendCallbackBuilder.commonCallback());
return skus;
}

View File

@ -1,9 +1,11 @@
package cn.lili.modules.search.entity.dos;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.lili.common.elasticsearch.EsSuffix;
import cn.lili.common.utils.StringUtils;
import cn.lili.modules.goods.entity.dos.GoodsParams;
import cn.lili.modules.goods.entity.dos.GoodsSku;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModelProperty;
@ -275,22 +277,38 @@ public class EsGoodsIndex implements Serializable {
this.intro = sku.getIntro();
this.grade = sku.getGrade();
this.releaseTime = new Date();
if (StringUtils.isNotEmpty(sku.getSpecs())) {
List<EsGoodsAttribute> attributes = new ArrayList<>();
JSONObject jsonObject = JSONUtil.parseObj(sku.getSpecs());
for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
if (!entry.getKey().equals("images")) {
EsGoodsAttribute attribute = new EsGoodsAttribute();
attribute.setType(0);
attribute.setName(entry.getKey());
attribute.setValue(entry.getValue().toString());
attributes.add(attribute);
}
}
this.attrList = attributes;
}
// if (CharSequenceUtil.isNotEmpty(sku.getSpecs())) {
// List<EsGoodsAttribute> attributes = new ArrayList<>();
// JSONObject jsonObject = JSONUtil.parseObj(sku.getSpecs());
// for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
// if (!entry.getKey().equals("images")) {
// EsGoodsAttribute attribute = new EsGoodsAttribute();
// attribute.setType(1);
// attribute.setName(entry.getKey());
// attribute.setValue(entry.getValue().toString());
// attributes.add(attribute);
// }
// }
// this.attrList = attributes;
// }
}
}
public EsGoodsIndex(GoodsSku sku, List<GoodsParams> goodsParams) {
this(sku);
if (goodsParams != null && !goodsParams.isEmpty()) {
List<EsGoodsAttribute> attributes = new ArrayList<>();
for (GoodsParams goodsParam : goodsParams) {
EsGoodsAttribute attribute = new EsGoodsAttribute();
if (goodsParam.getIsIndex() == 1) {
attribute.setType(1);
attribute.setName(goodsParam.getParamName());
attribute.setValue(goodsParam.getParamValue());
attributes.add(attribute);
}
}
this.attrList = attributes;
}
}
public void setGoodsSku(GoodsSku sku) {

View File

@ -1,5 +1,6 @@
package cn.lili.modules.search.service;
import cn.lili.modules.goods.entity.dos.GoodsParams;
import cn.lili.modules.goods.entity.dos.GoodsSku;
import cn.lili.modules.promotion.entity.dos.PromotionGoods;
import cn.lili.modules.promotion.entity.dto.BasePromotion;
@ -145,7 +146,8 @@ public interface EsGoodsIndexService {
* 重置当前商品索引
*
* @param goodsSku 商品sku信息
* @param goodsParams 商品参数
* @return 商品索引
*/
EsGoodsIndex resetEsGoodsIndex(GoodsSku goodsSku);
EsGoodsIndex resetEsGoodsIndex(GoodsSku goodsSku, List<GoodsParams> goodsParams);
}

View File

@ -8,6 +8,7 @@ import cn.hutool.extra.pinyin.PinyinUtil;
import cn.lili.common.elasticsearch.BaseElasticsearchService;
import cn.lili.common.elasticsearch.EsSuffix;
import cn.lili.config.elasticsearch.ElasticsearchProperties;
import cn.lili.modules.goods.entity.dos.GoodsParams;
import cn.lili.modules.goods.entity.dos.GoodsSku;
import cn.lili.modules.goods.entity.dos.GoodsWords;
import cn.lili.modules.goods.entity.enums.GoodsWordsTypeEnum;
@ -367,11 +368,12 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements
* 重置当前商品索引
*
* @param goodsSku 商品sku信息
* @param goodsParams 商品参数
* @return 商品索引
*/
@Override
public EsGoodsIndex resetEsGoodsIndex(GoodsSku goodsSku) {
EsGoodsIndex index = new EsGoodsIndex(goodsSku);
public EsGoodsIndex resetEsGoodsIndex(GoodsSku goodsSku, List<GoodsParams> goodsParams) {
EsGoodsIndex index = new EsGoodsIndex(goodsSku, goodsParams);
//获取活动信息
Map<String, Object> goodsCurrentPromotionMap = promotionService.getGoodsCurrentPromotionMap(index);
//写入促销信息

View File

@ -68,23 +68,14 @@ public class ElasticsearchController {
String goodsId = null;
//库存锁是在redis做的所以生成索引同时更新一下redis中的库存数量
for (GoodsSku goodsSku : list) {
boolean needIndex = false;
if (goodsId == null || !goodsId.equals(goodsSku.getGoodsId())) {
goodsId = goodsSku.getGoodsId();
Goods goods = goodsService.getById(goodsId);
EsGoodsIndex index = new EsGoodsIndex(goodsSku);
if (goods.getParams() != null && !goods.getParams().isEmpty()) {
List<GoodsParams> goodsParams = JSONUtil.toList(goods.getParams(), GoodsParams.class);
for (GoodsParams goodsParam : goodsParams) {
Parameters parameters = parametersService.getById(goodsParam.getParamId());
if (parameters.getIsIndex() == 1) {
needIndex = true;
break;
}
}
index = new EsGoodsIndex(goodsSku, goodsParams);
}
}
if (Boolean.TRUE.equals(needIndex)) {
EsGoodsIndex index = new EsGoodsIndex(goodsSku);
Map<String, Object> goodsCurrentPromotionMap = promotionService.getGoodsCurrentPromotionMap(index);
index.setPromotionMap(goodsCurrentPromotionMap);
esGoodsIndices.add(index);