优化管理后台的商品分页的查询

pull/2/head
YunaiV 2022-10-24 22:12:47 +08:00
parent c00ded3ed3
commit 7bf37fcb64
8 changed files with 42 additions and 55 deletions

View File

@ -1,12 +1,11 @@
package cn.iocoder.yudao.module.product.controller.admin.spu.vo;
import lombok.*;
import java.util.*;
import io.swagger.annotations.*;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import org.springframework.format.annotation.DateTimeFormat;
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
@ApiModel("管理后台 - 商品 SPU 分页 Request VO")
@Data
@ -26,7 +25,7 @@ public class ProductSpuPageReqVO extends PageParam {
@ApiModelProperty(value = "商品品牌编号", example = "1")
private Long brandId;
@ApiModelProperty(value = "上下架状态", example = "1", notes = "参见 CommonStatusEnum 枚举值")
@ApiModelProperty(value = "上下架状态", example = "1", notes = "参见 ProductSpuStatusEnum 枚举值")
private Integer status;
@ApiModelProperty(value = "销量最小值", example = "1")
@ -41,8 +40,7 @@ public class ProductSpuPageReqVO extends PageParam {
@ApiModelProperty(value = "市场价最大值", example = "1024")
private Integer marketPriceMax;
// TODO @luowenfeng: 这个可以改成前端基于 tab, 传递不同的条件么?
@ApiModelProperty(value = "tab 状态 null 全部, 0销售中上架 1仓库中下架 2预警中", example = "1")
private Integer tabStatus;
@ApiModelProperty(value = "是否库存告警", example = "true")
private Boolean alarmStock;
}

View File

@ -40,8 +40,7 @@ public interface ProductSkuMapper extends BaseMapperX<ProductSkuDO> {
}
}
// TODO @luowenfeng: selectListByRemind虽然不是很好但是感觉会更清晰一些
default List<ProductSkuDO> selectRemindSpuIds(){
default List<ProductSkuDO> selectListByAlarmStock(){
return selectList(new QueryWrapper<ProductSkuDO>().apply("stock <= warn_stock"));
}

View File

@ -7,7 +7,7 @@ import cn.iocoder.yudao.module.product.controller.admin.spu.vo.ProductSpuPageReq
import cn.iocoder.yudao.module.product.dal.dataobject.spu.ProductSpuDO;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
import java.util.Set;
/**
* spu Mapper
@ -29,8 +29,8 @@ public interface ProductSpuMapper extends BaseMapperX<ProductSpuDO> {
.orderByDesc(ProductSpuDO::getSort));
}
default PageResult<ProductSpuDO> selectPage(ProductSpuPageReqVO reqVO, List<Long> spuIds) {
LambdaQueryWrapperX<ProductSpuDO> productSpuDOLambdaQueryWrapperX = new LambdaQueryWrapperX<ProductSpuDO>()
default PageResult<ProductSpuDO> selectPage(ProductSpuPageReqVO reqVO, Set<Long> alarmStockSpuIds) {
return selectPage(reqVO, new LambdaQueryWrapperX<ProductSpuDO>()
.likeIfPresent(ProductSpuDO::getName, reqVO.getName())
.eqIfPresent(ProductSpuDO::getCategoryId, reqVO.getCategoryId())
.eqIfPresent(ProductSpuDO::getStatus, reqVO.getStatus())
@ -38,16 +38,9 @@ public interface ProductSpuMapper extends BaseMapperX<ProductSpuDO> {
.geIfPresent(ProductSpuDO::getSalesCount, reqVO.getSalesCountMin())
.leIfPresent(ProductSpuDO::getMarketPrice, reqVO.getMarketPriceMax())
.geIfPresent(ProductSpuDO::getMarketPrice, reqVO.getMarketPriceMin())
.orderByDesc(ProductSpuDO::getSort);
// TODO @芋艿: 需要优化下这里的代码
if(reqVO.getTabStatus()!= null && reqVO.getTabStatus() == 2){
productSpuDOLambdaQueryWrapperX.inIfPresent(ProductSpuDO::getId, spuIds);
}else{
productSpuDOLambdaQueryWrapperX.eqIfPresent(ProductSpuDO::getStatus, reqVO.getTabStatus());
.inIfPresent(ProductSpuDO::getId, alarmStockSpuIds) // 库存告警
.eqIfPresent(ProductSpuDO::getStatus, reqVO.getStatus())
.orderByDesc(ProductSpuDO::getSort));
}
return selectPage(reqVO, productSpuDOLambdaQueryWrapperX);
}
}

View File

@ -3,7 +3,6 @@ package cn.iocoder.yudao.module.product.service.sku;
import cn.iocoder.yudao.module.product.controller.admin.sku.vo.ProductSkuCreateOrUpdateReqVO;
import cn.iocoder.yudao.module.product.dal.dataobject.sku.ProductSkuDO;
import javax.validation.Valid;
import java.util.Collection;
import java.util.List;
@ -84,10 +83,10 @@ public interface ProductSkuService {
void deleteSkuBySpuId(Long spuId);
/**
* SPU
* SKU
*
* @return spuId
* @return SKU
*/
List<ProductSkuDO> getRemindSpuIds();
List<ProductSkuDO> getSkusByAlarmStock();
}

View File

@ -1,11 +1,7 @@
package cn.iocoder.yudao.module.product.service.sku;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.product.controller.admin.property.vo.property.ProductPropertyRespVO;
import cn.iocoder.yudao.module.product.controller.admin.property.vo.value.ProductPropertyValueRespVO;
import cn.iocoder.yudao.module.product.controller.admin.sku.vo.ProductSkuBaseVO;
@ -17,18 +13,15 @@ import cn.iocoder.yudao.module.product.enums.ErrorCodeConstants;
import cn.iocoder.yudao.module.product.enums.spu.ProductSpuSpecTypeEnum;
import cn.iocoder.yudao.module.product.service.property.ProductPropertyService;
import cn.iocoder.yudao.module.product.service.property.ProductPropertyValueService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import javax.annotation.Resource;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.*;
/**
@ -138,8 +131,8 @@ public class ProductSkuServiceImpl implements ProductSkuService {
}
@Override
public List<ProductSkuDO> getRemindSpuIds() {
return productSkuMapper.selectRemindSpuIds();
public List<ProductSkuDO> getSkusByAlarmStock() {
return productSkuMapper.selectListByAlarmStock();
}
@Override

View File

@ -1,6 +1,7 @@
package cn.iocoder.yudao.module.product.service.spu;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.module.product.controller.admin.property.vo.ProductPropertyViewRespVO;
@ -28,10 +29,7 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
@ -181,18 +179,21 @@ public class ProductSpuServiceImpl implements ProductSpuService {
@Override
public PageResult<ProductSpuRespVO> getSpuPage(ProductSpuPageReqVO pageReqVO) {
List<Long> remindSpuIds = null;
if (pageReqVO.getTabStatus() != null && pageReqVO.getTabStatus() == 2) {
remindSpuIds = productSkuService.getRemindSpuIds().stream().map(ProductSkuDO::getSpuId).distinct().collect(Collectors.toList());
if (remindSpuIds.isEmpty()) {
remindSpuIds.add(null);
// 库存告警的 SPU 编号的集合
Set<Long> alarmStockSpuIds = null;
if (Boolean.TRUE.equals(pageReqVO.getAlarmStock())) {
alarmStockSpuIds = CollectionUtils.convertSet(productSkuService.getSkusByAlarmStock(), ProductSkuDO::getSpuId);
if (CollUtil.isEmpty(alarmStockSpuIds)) {
return PageResult.empty();
}
}
return ProductSpuConvert.INSTANCE.convertPage(productSpuMapper.selectPage(pageReqVO, remindSpuIds));
// 分页查询
return ProductSpuConvert.INSTANCE.convertPage(productSpuMapper.selectPage(pageReqVO, alarmStockSpuIds));
}
@Override
public PageResult<AppSpuPageRespVO> getSpuPage(AppSpuPageReqVO pageReqVO) {
// TODO 芋艿:貌似实现不太合理
PageResult<ProductSpuDO> productSpuDOPageResult = productSpuMapper.selectPage(ProductSpuConvert.INSTANCE.convert(pageReqVO));
PageResult<AppSpuPageRespVO> pageResult = new PageResult<>();
// TODO @芋艿 这里用convert如何解决

View File

@ -208,6 +208,8 @@ public class ProductSpuServiceImplTest extends BaseDbUnitTest {
Assertions.assertIterableEquals(createReqVO, spuList);
}
// TODO @luowenfeng单测要分情况类似你这个可以分 2 个单测;一个是有预存预警的;一个是没库存预警的;
// 然后,参考其它模块的 getPage 类型的方法的单测。
@Test
void getSpuPage() {
// 准备参数

View File

@ -222,6 +222,7 @@
import {deleteSpu, getSpuPage,} from "@/api/mall/product/spu";
import {getProductCategoryList} from "@/api/mall/product/category";
import {getBrandList} from "@/api/mall/product/brand";
import {ProductSpuStatusEnum} from "@/utils/constants";
export default {
name: "Spu",
@ -260,7 +261,6 @@ export default {
salesCountMax: null,
marketPriceMin: null,
marketPriceMax: null,
tabStatus: null,
},
};
},
@ -341,15 +341,17 @@ export default {
// tab
handleClick(val) {
if (val.name === "all") {
this.queryParams.tabStatus = null;
this.queryParams.status = undefined;
this.queryParams.alarmStock = undefined;
} else if (val.name === "on") {
this.queryParams.tabStatus = 0;
this.queryParams.status = ProductSpuStatusEnum.ENABLE.status;
this.queryParams.alarmStock = undefined;
} else if (val.name === "off") {
this.queryParams.tabStatus = 1;
this.queryParams.status = ProductSpuStatusEnum.DISABLE.status;
this.queryParams.alarmStock = undefined;
} else if (val.name === "remind") {
this.queryParams.tabStatus = 2;
} else {
this.queryParams.tabStatus = null;
this.queryParams.status = undefined;
this.queryParams.alarmStock = true;
}
this.getList();
}