feat: 新增产品热度排行接口
This commit is contained in:
@@ -7,6 +7,8 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.klp.common.utils.StringUtils;
|
||||
import com.klp.domain.WmsProduct;
|
||||
import com.klp.mapper.WmsProductMapper;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
import com.klp.domain.bo.WmsProductSalesScriptBo;
|
||||
@@ -14,16 +16,16 @@ import com.klp.domain.vo.WmsProductSalesScriptVo;
|
||||
import com.klp.domain.WmsProductSalesScript;
|
||||
import com.klp.mapper.WmsProductSalesScriptMapper;
|
||||
import com.klp.service.IWmsProductSalesScriptService;
|
||||
import com.klp.domain.vo.HotProductVO;
|
||||
import com.klp.domain.vo.ProductRankingVo;
|
||||
import com.klp.common.utils.redis.RedisUtils;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.Comparator;
|
||||
import com.klp.domain.WmsProduct;
|
||||
import com.klp.mapper.WmsProductMapper;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Collection;
|
||||
import org.redisson.api.RScoredSortedSet;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* 产品销售话术Service业务层处理
|
||||
@@ -36,7 +38,9 @@ import java.util.Collection;
|
||||
public class WmsProductSalesScriptServiceImpl implements IWmsProductSalesScriptService {
|
||||
|
||||
private final WmsProductSalesScriptMapper baseMapper;
|
||||
private final WmsProductMapper wmsProductMapper;
|
||||
|
||||
@Resource
|
||||
private WmsProductMapper wmsProductMapper;
|
||||
|
||||
/**
|
||||
* 查询产品销售话术
|
||||
@@ -125,43 +129,58 @@ public class WmsProductSalesScriptServiceImpl implements IWmsProductSalesScriptS
|
||||
return baseMapper.deleteBatchIds(ids) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取热门产品排行(基于访问频率)
|
||||
*/
|
||||
@Override
|
||||
public List<HotProductVO> getHotProducts(Integer limit) {
|
||||
// 获取所有产品访问频率的key
|
||||
Collection<String> keys = RedisUtils.keys("product:visit:frequency:*");
|
||||
|
||||
List<HotProductVO> hotProducts = keys.stream()
|
||||
.map(key -> {
|
||||
String productIdStr = key.replace("product:visit:frequency:", "");
|
||||
Long productId = Long.valueOf(productIdStr);
|
||||
Long visitCount = RedisUtils.getAtomicValue(key);
|
||||
|
||||
// 查询产品信息
|
||||
WmsProduct product = wmsProductMapper.selectById(productId);
|
||||
if (product == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
HotProductVO vo = new HotProductVO();
|
||||
vo.setProductId(productId);
|
||||
vo.setProductName(product.getProductName());
|
||||
vo.setProductCode(product.getProductCode());
|
||||
vo.setVisitCount(visitCount);
|
||||
return vo;
|
||||
})
|
||||
.filter(vo -> vo != null)
|
||||
.sorted(Comparator.comparing(HotProductVO::getVisitCount).reversed())
|
||||
.limit(limit)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// 设置排名
|
||||
for (int i = 0; i < hotProducts.size(); i++) {
|
||||
hotProducts.get(i).setRank(i + 1);
|
||||
public void recordProductViewCount(Long productId) {
|
||||
String countKey = "product:view:count:" + productId;
|
||||
String rankingKey = "product:view:ranking";
|
||||
|
||||
// 计数器+1(使用原子操作)
|
||||
RedisUtils.incrAtomicValue(countKey);
|
||||
|
||||
// 设置计数器过期时间为1天
|
||||
RedisUtils.expire(countKey, 24 * 60 * 60);
|
||||
|
||||
// 更新排行榜(使用ZSet操作)
|
||||
RScoredSortedSet<String> zSet = RedisUtils.getClient().getScoredSortedSet(rankingKey);
|
||||
zSet.addScore(productId.toString(), 1);
|
||||
|
||||
// 设置排行榜过期时间为1天
|
||||
RedisUtils.expire(rankingKey, 24 * 60 * 60);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ProductRankingVo> getProductRanking() {
|
||||
String rankingKey = "product:view:ranking";
|
||||
|
||||
// 获取排行榜前10名(按访问次数降序)
|
||||
RScoredSortedSet<String> zSet = RedisUtils.getClient().getScoredSortedSet(rankingKey);
|
||||
|
||||
List<ProductRankingVo> rankingList = new ArrayList<>();
|
||||
int rank = 1;
|
||||
|
||||
// 获取前10名的产品ID和分数
|
||||
Collection<String> topProducts = zSet.valueRangeReversed(0, 9);
|
||||
|
||||
for (String productIdStr : topProducts) {
|
||||
Long productId = Long.valueOf(productIdStr);
|
||||
Double score = zSet.getScore(productIdStr);
|
||||
Long viewCount = score != null ? score.longValue() : 0L;
|
||||
|
||||
ProductRankingVo vo = new ProductRankingVo();
|
||||
vo.setProductId(productId);
|
||||
vo.setViewCount(viewCount);
|
||||
vo.setRanking(rank++);
|
||||
|
||||
//补充产品基础信息(这里需要注入ProductMapper)
|
||||
WmsProduct product = wmsProductMapper.selectById(productId);
|
||||
if (product != null) {
|
||||
vo.setProductCode(product.getProductCode());
|
||||
vo.setProductName(product.getProductName());
|
||||
}
|
||||
|
||||
rankingList.add(vo);
|
||||
}
|
||||
|
||||
return hotProducts;
|
||||
|
||||
return rankingList;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,8 +16,6 @@ import com.klp.domain.WmsProduct;
|
||||
import com.klp.mapper.WmsProductMapper;
|
||||
import com.klp.service.IWmsProductService;
|
||||
import com.klp.domain.vo.OrderSummaryVO;
|
||||
import com.klp.domain.vo.HotProductVO;
|
||||
import com.klp.service.IWmsProductSalesScriptService;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -34,7 +32,6 @@ import java.util.Collection;
|
||||
public class WmsProductServiceImpl implements IWmsProductService {
|
||||
|
||||
private final WmsProductMapper baseMapper;
|
||||
private final IWmsProductSalesScriptService iWmsProductSalesScriptService;
|
||||
|
||||
/**
|
||||
* 查询产品
|
||||
@@ -152,8 +149,6 @@ public class WmsProductServiceImpl implements IWmsProductService {
|
||||
vo.setProductRank(baseMapper.selectProductRank());
|
||||
vo.setOrderMaterial(baseMapper.selectOrderMaterial());
|
||||
vo.setCustomerRegion(baseMapper.selectCustomerRegion());
|
||||
// 添加热门产品数据
|
||||
vo.setHotProducts(iWmsProductSalesScriptService.getHotProducts(10));
|
||||
return vo;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user