店铺商品数量,店铺缓存数据2小时。优化

This commit is contained in:
Chopper 2022-06-02 09:16:30 +08:00
parent 90d3bbd79e
commit d4a13c8a82
10 changed files with 107 additions and 30 deletions

View File

@ -60,11 +60,6 @@ public class GoodsMessageListener implements RocketMQListener<MessageExt> {
*/
@Autowired
private EsGoodsIndexService goodsIndexService;
/**
* 店铺
*/
@Autowired
private StoreService storeService;
/**
* 商品
*/
@ -202,7 +197,6 @@ public class GoodsMessageListener implements RocketMQListener<MessageExt> {
//审核商品
case GOODS_AUDIT:
Goods goods = JSONUtil.toBean(new String(messageExt.getBody()), Goods.class);
updateGoodsNum(goods);
updateGoodsIndex(goods);
break;
//删除商品
@ -213,7 +207,6 @@ public class GoodsMessageListener implements RocketMQListener<MessageExt> {
Goods goodsById = this.goodsService.getById(goodsId);
if (goodsById != null) {
this.deleteGoods(goodsById);
this.updateGoodsNum(goodsById);
goodsIndexService.deleteIndex(MapUtil.builder(new HashMap<String, Object>()).put("goodsId", goodsId).build());
}
}
@ -458,22 +451,6 @@ public class GoodsMessageListener implements RocketMQListener<MessageExt> {
distributionGoodsService.removeById(distributionGoods.getId());
}
}
/**
* 修改商品数量
*
* @param goods 信息体
*/
private void updateGoodsNum(Goods goods) {
try {
//更新店铺商品数量
assert goods != null;
storeService.updateStoreGoodsNum(goods.getStoreId());
} catch (Exception e) {
log.error("修改商品数量错误");
}
}
/**
* 商品购买完成
* 1.更新商品购买数量

View File

@ -0,0 +1,49 @@
package cn.lili.timetask.handler.impl.store;
import cn.lili.modules.goods.service.GoodsSkuService;
import cn.lili.modules.store.entity.dos.Store;
import cn.lili.modules.store.entity.enums.StoreStatusEnum;
import cn.lili.modules.store.service.StoreService;
import cn.lili.timetask.handler.EveryDayExecute;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* 店铺信息更新
*
* @author Chopper
* @since 2021/3/15 5:30 下午
*/
@Component
@Slf4j
public class StoreExecute implements EveryDayExecute {
/**
* 店铺
*/
@Autowired
private StoreService storeService;
@Autowired
private GoodsSkuService goodsSkuService;
@Override
public void execute() {
//获取所有开启的店铺
List<Store> storeList = storeService.list(new LambdaQueryWrapper<Store>().eq(Store::getStoreDisable, StoreStatusEnum.OPEN.name()));
for (Store store : storeList) {
try {
Long num = goodsSkuService.countSkuNum(store.getId());
storeService.updateStoreGoodsNum(store.getId(), num);
} catch (Exception e) {
log.error("店铺id为{},更新商品数量失败", store.getId(), e);
}
}
}
}

View File

@ -1,4 +1,4 @@
package cn.lili.timetask.handler.impl.storerating;
package cn.lili.timetask.handler.impl.store;
import cn.lili.common.enums.SwitchEnum;
import cn.lili.modules.member.entity.dos.MemberEvaluation;

View File

@ -465,6 +465,10 @@ public enum CachePrefix {
*/
INIT_INDEX_FLAG,
/**
* 店铺
*/
STORE,
/**
* 店铺分类
*/

View File

@ -212,5 +212,19 @@ public interface GoodsSkuService extends IService<GoodsSku> {
*/
List<String> getSkuIdsByGoodsId(String goodsId);
/**
* 删除并且新增sku即覆盖之前信息
*
* @param goodsSkus
* @return
*/
boolean deleteAndInsertGoodsSkus(List<GoodsSku> goodsSkus);
/**
* 统计sku总数
*
* @param storeId
* @return
*/
Long countSkuNum(String storeId);
}

View File

@ -469,11 +469,23 @@ public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements
return this.count(
new LambdaQueryWrapper<Goods>()
.eq(Goods::getStoreId, storeId)
.eq(Goods::getDeleteFlag, Boolean.FALSE)
.eq(Goods::getAuthFlag, GoodsAuthEnum.PASS.name())
.eq(Goods::getMarketEnable, GoodsStatusEnum.UPPER.name()));
}
/**
* 更新店铺商品数量
*
* @param storeId 信息体
*/
void updateGoodsNum(String storeId) {
Long num = goodsSkuService.countSkuNum(storeId);
storeService.updateStoreGoodsNum(storeId, num);
}
/**
* 更新商品状态
*

View File

@ -601,6 +601,18 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
return count > 0;
}
@Override
public Long countSkuNum(String storeId) {
LambdaQueryWrapper<GoodsSku> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper
.eq(GoodsSku::getStoreId, storeId)
.eq(GoodsSku::getDeleteFlag, Boolean.FALSE)
.eq(GoodsSku::getAuthFlag, GoodsAuthEnum.PASS.name())
.eq(GoodsSku::getMarketEnable, GoodsStatusEnum.UPPER.name());
return this.count(queryWrapper);
}
/**
* 修改库存
*

View File

@ -106,8 +106,9 @@ public interface StoreService extends IService<Store> {
* 更新店铺商品数量
*
* @param storeId 店铺ID
* @param num 商品数量
*/
void updateStoreGoodsNum(String storeId);
void updateStoreGoodsNum(String storeId, Long num);
/**
* 更新店铺收藏数量

View File

@ -3,6 +3,8 @@ package cn.lili.modules.store.serviceimpl;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.map.MapUtil;
import cn.hutool.json.JSONUtil;
import cn.lili.cache.Cache;
import cn.lili.cache.CachePrefix;
import cn.lili.common.properties.RocketmqCustomProperties;
import cn.lili.common.security.AuthUser;
import cn.lili.common.security.context.UserContext;
@ -64,9 +66,17 @@ public class StoreDetailServiceImpl extends ServiceImpl<StoreDetailMapper, Store
@Autowired
private RocketMQTemplate rocketMQTemplate;
@Autowired
private Cache cache;
@Override
public StoreDetailVO getStoreDetailVO(String storeId) {
return this.baseMapper.getStoreDetail(storeId);
StoreDetailVO storeDetailVO = (StoreDetailVO) cache.get(CachePrefix.STORE.getPrefix() + storeId);
if (storeDetailVO == null) {
storeDetailVO = this.baseMapper.getStoreDetail(storeId);
cache.put(CachePrefix.STORE.getPrefix() + storeId, storeDetailVO, 7200L);
}
return storeDetailVO;
}
@Override

View File

@ -270,12 +270,10 @@ public class StoreServiceImpl extends ServiceImpl<StoreMapper, Store> implements
}
@Override
public void updateStoreGoodsNum(String storeId) {
//获取店铺已上架已审核通过商品数量
long goodsNum = goodsService.countStoreGoodsNum(storeId);
public void updateStoreGoodsNum(String storeId, Long num) {
//修改店铺商品数量
this.update(new LambdaUpdateWrapper<Store>()
.set(Store::getGoodsNum, goodsNum)
.set(Store::getGoodsNum, num)
.eq(Store::getId, storeId));
}