feat(goods): 添加商品数量统计接口并优化商品查询

- 新增 getGoodsNumVO 接口,用于获取不同状态的商品数量统计
- 在 GoodsSearchParams 中添加 goodsStatus 字段,用于筛选商品状态
- 修改 queryByParams 方法,支持按商品状态进行筛选
- 新增 GoodsNumVO 类,用于返回商品数量统计结果
-优化商品查询性能,通过聚合查询一次性获取所有状态的商品数量
This commit is contained in:
pikachu1995@126.com
2025-08-27 16:15:56 +08:00
parent 2e019ef933
commit 09d00260f6
6 changed files with 129 additions and 22 deletions

14
DB/index.sql Normal file
View File

@@ -0,0 +1,14 @@
-- 针对WHERE条件、排序和分组的组合索引
CREATE INDEX idx_order_delete_flag_create_time_id_sn ON li_order (delete_flag, create_time DESC, id DESC, sn);
-- 针对WHERE条件和连接条件的组合索引
CREATE INDEX idx_order_status_delete_flag_sn ON li_order (order_status, delete_flag, sn);
-- 针对连接条件的索引
CREATE INDEX idx_order_item_order_sn ON li_order_item (order_sn);
-- 针对过滤条件、排序字段的组合索引
CREATE INDEX idx_li_member_disabled_create_time ON li_member (disabled, create_time DESC);
-- 针对过滤条件、排序字段的组合索引
CREATE INDEX idx_li_goods_delete_flag_create_time ON li_goods (delete_flag, create_time DESC);

View File

@@ -70,6 +70,9 @@ public class GoodsSearchParams extends PageVO {
@ApiModelProperty(value = "审核状态")
private String authFlag;
@ApiModelProperty(value = "商品状态")
private String goodsStatus;
@ApiModelProperty(value = "库存数量")
private Integer leQuantity;
@@ -120,6 +123,23 @@ public class GoodsSearchParams extends PageVO {
if (CharSequenceUtil.isNotEmpty(storeCategoryPath)) {
queryWrapper.like("store_category_path", storeCategoryPath);
}
if (CharSequenceUtil.isNotEmpty(goodsStatus)) {
if(goodsStatus.equals(GoodsStatusEnum.UPPER.name())){
//审核通过+已上架
queryWrapper.eq("auth_flag", GoodsAuthEnum.PASS.name());
queryWrapper.eq("market_enable", GoodsStatusEnum.UPPER.name());
}else if(goodsStatus.equals(GoodsStatusEnum.DOWN.name())){
//审核通过+未上架
queryWrapper.eq("auth_flag", GoodsAuthEnum.PASS.name());
queryWrapper.eq("market_enable", GoodsStatusEnum.DOWN.name());
}else if(goodsStatus.equals(GoodsAuthEnum.TOBEAUDITED.name())){
//待审核
queryWrapper.eq("auth_flag", GoodsAuthEnum.TOBEAUDITED.name());
}else if(goodsStatus.equals(GoodsAuthEnum.REFUSE.name())){
//审核拒绝
queryWrapper.eq("auth_flag", GoodsAuthEnum.REFUSE.name());
}
}
if (selfOperated != null) {
queryWrapper.eq("self_operated", selfOperated);
}

View File

@@ -0,0 +1,17 @@
package cn.lili.modules.goods.entity.vos;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@Data
public class GoodsNumVO {
@ApiModelProperty(value = "出售中的商品数量")
private Integer upperGoodsNum;
@ApiModelProperty(value = "仓库中的商品数量")
private Integer downGoodsNum;
@ApiModelProperty(value = "待审核商品数量")
private Integer auditGoodsNum;
@ApiModelProperty(value = "审核未通过数量")
private Integer refuseGoodsNum;
}

View File

@@ -5,6 +5,7 @@ import cn.lili.modules.goods.entity.dto.GoodsOperationDTO;
import cn.lili.modules.goods.entity.dto.GoodsSearchParams;
import cn.lili.modules.goods.entity.enums.GoodsAuthEnum;
import cn.lili.modules.goods.entity.enums.GoodsStatusEnum;
import cn.lili.modules.goods.entity.vos.GoodsNumVO;
import cn.lili.modules.goods.entity.vos.GoodsVO;
import cn.lili.modules.store.entity.dos.Store;
import com.baomidou.mybatisplus.core.metadata.IPage;
@@ -83,6 +84,14 @@ public interface GoodsService extends IService<Goods> {
*/
IPage<Goods> queryByParams(GoodsSearchParams goodsSearchParams);
/**
* 获取商品数量
*
* @param goodsSearchParams 查询参数
* @return 商品数量
*/
GoodsNumVO getGoodsNumVO(GoodsSearchParams goodsSearchParams);
/**
* 商品查询
@@ -191,6 +200,11 @@ public interface GoodsService extends IService<Goods> {
*/
void categoryGoodsName(String categoryId);
/**
* 添加商品评价数量
*
* @param commentNum 评价数量
* @param goodsId 商品ID
*/
void addGoodsCommentNum(Integer commentNum, String goodsId);
}

View File

@@ -20,6 +20,7 @@ import cn.lili.modules.goods.entity.dto.GoodsParamsDTO;
import cn.lili.modules.goods.entity.dto.GoodsSearchParams;
import cn.lili.modules.goods.entity.enums.GoodsAuthEnum;
import cn.lili.modules.goods.entity.enums.GoodsStatusEnum;
import cn.lili.modules.goods.entity.vos.GoodsNumVO;
import cn.lili.modules.goods.entity.vos.GoodsSkuVO;
import cn.lili.modules.goods.entity.vos.GoodsVO;
import cn.lili.modules.goods.mapper.GoodsMapper;
@@ -281,6 +282,40 @@ public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements
return this.page(PageUtil.initPage(goodsSearchParams), goodsSearchParams.queryWrapper());
}
@Override
public GoodsNumVO getGoodsNumVO(GoodsSearchParams goodsSearchParams) {
GoodsNumVO goodsNumVO = new GoodsNumVO();
// 获取基础查询条件
QueryWrapper<Goods> baseWrapper = goodsSearchParams.queryWrapper();
// 使用聚合查询一次性获取所有状态的商品数量
List<Map<String, Object>> results = this.baseMapper.selectMaps(
baseWrapper.select(
"COUNT(CASE WHEN auth_flag = 'PASS' AND market_enable = 'UPPER' THEN 1 END) as upperGoodsNum",
"COUNT(CASE WHEN auth_flag = 'PASS' AND market_enable = 'DOWN' THEN 1 END) as downGoodsNum",
"COUNT(CASE WHEN auth_flag = 'TOBEAUDITED' THEN 1 END) as auditGoodsNum",
"COUNT(CASE WHEN auth_flag = 'REFUSE' THEN 1 END) as refuseGoodsNum"
)
);
if (!results.isEmpty()) {
Map<String, Object> result = results.get(0);
goodsNumVO.setUpperGoodsNum(((Number) result.get("upperGoodsNum")).intValue());
goodsNumVO.setDownGoodsNum(((Number) result.get("downGoodsNum")).intValue());
goodsNumVO.setAuditGoodsNum(((Number) result.get("auditGoodsNum")).intValue());
goodsNumVO.setRefuseGoodsNum(((Number) result.get("refuseGoodsNum")).intValue());
} else {
// 如果没有结果设置默认值为0
goodsNumVO.setUpperGoodsNum(0);
goodsNumVO.setDownGoodsNum(0);
goodsNumVO.setAuditGoodsNum(0);
goodsNumVO.setRefuseGoodsNum(0);
}
return goodsNumVO;
}
/**
* 商品查询
*

View File

@@ -11,6 +11,7 @@ import cn.lili.modules.goods.entity.dos.GoodsSku;
import cn.lili.modules.goods.entity.dto.GoodsSearchParams;
import cn.lili.modules.goods.entity.enums.GoodsAuthEnum;
import cn.lili.modules.goods.entity.enums.GoodsStatusEnum;
import cn.lili.modules.goods.entity.vos.GoodsNumVO;
import cn.lili.modules.goods.entity.vos.GoodsVO;
import cn.lili.modules.goods.service.GoodsService;
import cn.lili.modules.goods.service.GoodsSkuService;
@@ -53,6 +54,12 @@ public class GoodsManagerController {
return ResultUtil.data(goodsService.queryByParams(goodsSearchParams));
}
@ApiOperation(value = "获取商品数量")
@GetMapping(value = "/goodsNumber")
public ResultMessage<GoodsNumVO> getGoodsNumVO(GoodsSearchParams goodsSearchParams) {
return ResultUtil.data(goodsService.getGoodsNumVO(goodsSearchParams));
}
@ApiOperation(value = "分页获取商品列表")
@GetMapping(value = "/sku/list")
public ResultMessage<IPage<GoodsSku>> getSkuByPage(GoodsSearchParams goodsSearchParams) {
@@ -66,32 +73,16 @@ public class GoodsManagerController {
return ResultUtil.data(goodsService.queryByParams(goodsSearchParams));
}
@PreventDuplicateSubmissions
@ApiOperation(value = "管理员下架商品", notes = "管理员下架商品时使用")
@ApiImplicitParams({
@ApiImplicitParam(name = "goodsId", value = "商品ID", required = true, paramType = "query", allowMultiple = true),
@ApiImplicitParam(name = "reason", value = "下架理由", required = true, paramType = "query")
})
@DemoSite
@PutMapping(value = "/{goodsId}/under")
public ResultMessage<Object> underGoods(@PathVariable String goodsId, @NotEmpty(message = "下架原因不能为空") @RequestParam String reason) {
List<String> goodsIds = Arrays.asList(goodsId.split(","));
if (Boolean.TRUE.equals(goodsService.managerUpdateGoodsMarketAble(goodsIds, GoodsStatusEnum.DOWN, reason))) {
return ResultUtil.success();
}
throw new ServiceException(ResultCode.GOODS_UNDER_ERROR);
}
@PreventDuplicateSubmissions
@ApiOperation(value = "管理员审核商品", notes = "管理员审核商品")
@ApiImplicitParams({
@ApiImplicitParam(name = "goodsIds", value = "商品ID", required = true, paramType = "path", allowMultiple = true, dataType = "int"),
@ApiImplicitParam(name = "authFlag", value = "审核结果", required = true, paramType = "query", dataType = "string")
})
@PutMapping(value = "{goodsIds}/auth")
public ResultMessage<Object> auth(@PathVariable List<String> goodsIds, @RequestParam String authFlag) {
@PutMapping(value = "auth")
public ResultMessage<Object> auth(@RequestParam List<String> goodsId, @RequestParam String authFlag) {
//校验商品是否存在
if (goodsService.auditGoods(goodsIds, GoodsAuthEnum.valueOf(authFlag))) {
if (goodsService.auditGoods(goodsId, GoodsAuthEnum.valueOf(authFlag))) {
return ResultUtil.success();
}
throw new ServiceException(ResultCode.GOODS_AUTH_ERROR);
@@ -100,17 +91,33 @@ public class GoodsManagerController {
@PreventDuplicateSubmissions
@ApiOperation(value = "管理员上架商品", notes = "管理员上架商品时使用")
@PutMapping(value = "/{goodsId}/up")
@PutMapping(value = "/up")
@ApiImplicitParams({
@ApiImplicitParam(name = "goodsId", value = "商品ID", required = true, allowMultiple = true)
})
public ResultMessage<Object> unpGoods(@PathVariable List<String> goodsId) {
public ResultMessage<Object> unpGoods(@RequestParam List<String> goodsId) {
if (Boolean.TRUE.equals(goodsService.updateGoodsMarketAble(goodsId, GoodsStatusEnum.UPPER, ""))) {
return ResultUtil.success();
}
throw new ServiceException(ResultCode.GOODS_UPPER_ERROR);
}
@PreventDuplicateSubmissions
@ApiOperation(value = "管理员下架商品", notes = "管理员下架商品时使用")
@ApiImplicitParams({
@ApiImplicitParam(name = "goodsId", value = "商品ID", required = true, paramType = "query", allowMultiple = true),
@ApiImplicitParam(name = "reason", value = "下架理由", required = true, paramType = "query")
})
@DemoSite
@PutMapping(value = "under")
public ResultMessage<Object> underGoods(@RequestParam List<String> goodsId, @NotEmpty(message = "下架原因不能为空") @RequestParam String reason) {
if (Boolean.TRUE.equals(goodsService.managerUpdateGoodsMarketAble(goodsId, GoodsStatusEnum.DOWN, reason))) {
return ResultUtil.success();
}
throw new ServiceException(ResultCode.GOODS_UNDER_ERROR);
}
@ApiOperation(value = "通过id获取商品详情")
@GetMapping(value = "/get/{id}")