商品分页

pull/1/head
YunaiV 2019-03-05 22:52:29 +08:00
parent 83fdf181b8
commit 15a9425396
13 changed files with 376 additions and 6 deletions

View File

@ -82,7 +82,7 @@ public class AdminController {
@GetMapping("/page") @GetMapping("/page")
@ApiOperation(value = "管理员分页") @ApiOperation(value = "管理员分页")
@ApiImplicitParams({ @ApiImplicitParams({
@ApiImplicitParam(name = "nickname", value = "昵称,模糊匹配", required = true, example = "小王"), @ApiImplicitParam(name = "nickname", value = "昵称,模糊匹配", example = "小王"),
@ApiImplicitParam(name = "pageNo", value = "页码,从 0 开始", example = "0"), @ApiImplicitParam(name = "pageNo", value = "页码,从 0 开始", example = "0"),
@ApiImplicitParam(name = "pageSize", value = "每页条数", required = true, example = "10"), @ApiImplicitParam(name = "pageSize", value = "每页条数", required = true, example = "10"),
}) })

View File

@ -1,10 +1,14 @@
package cn.iocoder.mall.admin.api.dto; package cn.iocoder.mall.admin.api.dto;
import javax.validation.constraints.NotNull;
public class AdminPageDTO { public class AdminPageDTO {
private String nickname; private String nickname;
@NotNull(message = "页码不能为空")
private Integer pageNo; private Integer pageNo;
@NotNull(message = "每页条数不能为空")
private Integer pageSize; private Integer pageSize;
public Integer getPageNo() { public Integer getPageNo() {

View File

@ -4,8 +4,10 @@ import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.admin.sdk.context.AdminSecurityContextHolder; import cn.iocoder.mall.admin.sdk.context.AdminSecurityContextHolder;
import cn.iocoder.mall.product.api.ProductSpuService; import cn.iocoder.mall.product.api.ProductSpuService;
import cn.iocoder.mall.product.api.bo.ProductSpuDetailBO; import cn.iocoder.mall.product.api.bo.ProductSpuDetailBO;
import cn.iocoder.mall.product.api.bo.ProductSpuPageBO;
import cn.iocoder.mall.product.api.dto.ProductSkuAddOrUpdateDTO; import cn.iocoder.mall.product.api.dto.ProductSkuAddOrUpdateDTO;
import cn.iocoder.mall.product.api.dto.ProductSpuAddDTO; import cn.iocoder.mall.product.api.dto.ProductSpuAddDTO;
import cn.iocoder.mall.product.api.dto.ProductSpuPageDTO;
import cn.iocoder.mall.product.api.dto.ProductSpuUpdateDTO; import cn.iocoder.mall.product.api.dto.ProductSpuUpdateDTO;
import cn.iocoder.mall.product.application.convert.ProductSpuConvert; import cn.iocoder.mall.product.application.convert.ProductSpuConvert;
import cn.iocoder.mall.product.application.vo.admins.AdminsProductSpuDetailVO; import cn.iocoder.mall.product.application.vo.admins.AdminsProductSpuDetailVO;
@ -103,8 +105,18 @@ public class AdminsProductSpuController {
@GetMapping("/spu/page") @GetMapping("/spu/page")
@ApiOperation("商品 SPU 分页列表") @ApiOperation("商品 SPU 分页列表")
public CommonResult<AdminsProductSpuPageVO> spuPage() { @ApiImplicitParams({
return null; @ApiImplicitParam(name = "name", value = "商品名称,模糊匹配", example = "小王"),
@ApiImplicitParam(name = "pageNo", value = "页码,从 0 开始", example = "0"),
@ApiImplicitParam(name = "pageSize", value = "每页条数", required = true, example = "10"),
})
public CommonResult<AdminsProductSpuPageVO> spuPage(@RequestParam(value = "name", required = false) String name,
@RequestParam(value = "pageNo", defaultValue = "0") Integer pageNo,
@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
// 创建 ProductSpuPageDTO 对象
ProductSpuPageDTO productSpuPageDTO = new ProductSpuPageDTO().setName(name).setPageNo(pageNo).setPageSize(pageSize);
CommonResult<ProductSpuPageBO> result = productSpuService.getProductSpuPage(productSpuPageDTO);
return ProductSpuConvert.INSTANCE.convert2(result);
} }
@GetMapping("/spu/info") @GetMapping("/spu/info")

View File

@ -2,7 +2,9 @@ package cn.iocoder.mall.product.application.convert;
import cn.iocoder.common.framework.vo.CommonResult; import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.product.api.bo.ProductSpuDetailBO; import cn.iocoder.mall.product.api.bo.ProductSpuDetailBO;
import cn.iocoder.mall.product.api.bo.ProductSpuPageBO;
import cn.iocoder.mall.product.application.vo.admins.AdminsProductSpuDetailVO; import cn.iocoder.mall.product.application.vo.admins.AdminsProductSpuDetailVO;
import cn.iocoder.mall.product.application.vo.admins.AdminsProductSpuPageVO;
import org.mapstruct.Mapper; import org.mapstruct.Mapper;
import org.mapstruct.Mappings; import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers; import org.mapstruct.factory.Mappers;
@ -18,4 +20,7 @@ public interface ProductSpuConvert {
@Mappings({}) @Mappings({})
CommonResult<AdminsProductSpuDetailVO> convert(CommonResult<ProductSpuDetailBO> result); CommonResult<AdminsProductSpuDetailVO> convert(CommonResult<ProductSpuDetailBO> result);
@Mappings({})
CommonResult<AdminsProductSpuPageVO> convert2(CommonResult<ProductSpuPageBO> result);
} }

View File

@ -1,10 +1,104 @@
package cn.iocoder.mall.product.application.vo.admins; package cn.iocoder.mall.product.application.vo.admins;
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.util.List;
@ApiModel(value = "商品 SPU VO", description = "不包括 SKU 信息 VO") @ApiModel(value = "商品 SPU VO", description = "不包括 SKU 信息 VO")
public class AdminsProductSpuVO { public class AdminsProductSpuVO {
@ApiModelProperty(value = "SPU 编号", required = true, example = "1")
private Integer id;
// ========== 基本信息 =========
@ApiModelProperty(value = "SPU 名字", required = true, example = "厮大牛逼")
private String name;
@ApiModelProperty(value = "卖点", required = true, example = "各种 MQ 骚操作")
private String sellPoint;
@ApiModelProperty(value = "描述", required = true, example = "你就说强不强")
private String description;
@ApiModelProperty(value = "分类编号", required = true, example = "反正我是信了")
private Integer cid;
@ApiModelProperty(value = "商品主图地址的数组", required = true, example = "http://www.iocoder.cn")
private List<String> picUrls;
// ========== 其他信息 =========
@ApiModelProperty(value = "是否上架商品(是否可见)", required = true, example = "true")
private Boolean visible;
@ApiModelProperty(value = "排序字段", required = true, example = "10")
private Integer sort;
public Integer getId() {
return id;
}
public AdminsProductSpuVO setId(Integer id) {
this.id = id;
return this;
}
public String getName() {
return name;
}
public AdminsProductSpuVO setName(String name) {
this.name = name;
return this;
}
public String getSellPoint() {
return sellPoint;
}
public AdminsProductSpuVO setSellPoint(String sellPoint) {
this.sellPoint = sellPoint;
return this;
}
public String getDescription() {
return description;
}
public AdminsProductSpuVO setDescription(String description) {
this.description = description;
return this;
}
public Integer getCid() {
return cid;
}
public AdminsProductSpuVO setCid(Integer cid) {
this.cid = cid;
return this;
}
public List<String> getPicUrls() {
return picUrls;
}
public AdminsProductSpuVO setPicUrls(List<String> picUrls) {
this.picUrls = picUrls;
return this;
}
public Boolean getVisible() {
return visible;
}
public AdminsProductSpuVO setVisible(Boolean visible) {
this.visible = visible;
return this;
}
public Integer getSort() {
return sort;
}
public AdminsProductSpuVO setSort(Integer sort) {
this.sort = sort;
return this;
}
} }

View File

@ -3,11 +3,14 @@ package cn.iocoder.mall.product.api;
import cn.iocoder.common.framework.vo.CommonResult; import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.product.api.bo.ProductSpuBO; import cn.iocoder.mall.product.api.bo.ProductSpuBO;
import cn.iocoder.mall.product.api.bo.ProductSpuDetailBO; import cn.iocoder.mall.product.api.bo.ProductSpuDetailBO;
import cn.iocoder.mall.product.api.bo.ProductSpuPageBO;
import cn.iocoder.mall.product.api.dto.ProductSpuAddDTO; import cn.iocoder.mall.product.api.dto.ProductSpuAddDTO;
import cn.iocoder.mall.product.api.dto.ProductSpuPageDTO;
import cn.iocoder.mall.product.api.dto.ProductSpuUpdateDTO; import cn.iocoder.mall.product.api.dto.ProductSpuUpdateDTO;
public interface ProductSpuService { public interface ProductSpuService {
// TODO 芋艿,需要去改改
ProductSpuBO getProductSpu(Integer id); ProductSpuBO getProductSpu(Integer id);
CommonResult<ProductSpuDetailBO> addProductSpu(Integer adminId, ProductSpuAddDTO productSpuAddDTO); CommonResult<ProductSpuDetailBO> addProductSpu(Integer adminId, ProductSpuAddDTO productSpuAddDTO);
@ -16,4 +19,6 @@ public interface ProductSpuService {
CommonResult<Boolean> updateProductSpuSort(Integer adminId, Integer spuId, Integer sort); CommonResult<Boolean> updateProductSpuSort(Integer adminId, Integer spuId, Integer sort);
CommonResult<ProductSpuPageBO> getProductSpuPage(ProductSpuPageDTO productSpuPageDTO);
} }

View File

@ -1,15 +1,123 @@
package cn.iocoder.mall.product.api.bo; package cn.iocoder.mall.product.api.bo;
import java.util.List;
public class ProductSpuBO { public class ProductSpuBO {
/**
* SPU
*/
private Integer id; private Integer id;
// ========== 基本信息 =========
/**
* SPU
*/
private String name;
/**
*
*/
private String sellPoint;
/**
*
*/
private String description;
/**
*
*/
private Integer cid;
/**
*
*
*
*
* 800*80015
*/
private List<String> picUrls;
// ========== 其他信息 =========
/**
*
*
* true
* false
*/
private Boolean visible;
/**
*
*/
private Integer sort;
public Integer getId() { public Integer getId() {
return id; return id;
} }
public void setId(Integer id) { public ProductSpuBO setId(Integer id) {
this.id = id; this.id = id;
return this;
}
public String getName() {
return name;
}
public ProductSpuBO setName(String name) {
this.name = name;
return this;
}
public String getSellPoint() {
return sellPoint;
}
public ProductSpuBO setSellPoint(String sellPoint) {
this.sellPoint = sellPoint;
return this;
}
public String getDescription() {
return description;
}
public ProductSpuBO setDescription(String description) {
this.description = description;
return this;
}
public Integer getCid() {
return cid;
}
public ProductSpuBO setCid(Integer cid) {
this.cid = cid;
return this;
}
public List<String> getPicUrls() {
return picUrls;
}
public ProductSpuBO setPicUrls(List<String> picUrls) {
this.picUrls = picUrls;
return this;
}
public Boolean getVisible() {
return visible;
}
public ProductSpuBO setVisible(Boolean visible) {
this.visible = visible;
return this;
}
public Integer getSort() {
return sort;
}
public ProductSpuBO setSort(Integer sort) {
this.sort = sort;
return this;
} }
} }

View File

@ -0,0 +1,34 @@
package cn.iocoder.mall.product.api.bo;
import java.util.List;
public class ProductSpuPageBO {
/**
* Spu
*/
private List<ProductSpuBO> spus;
/**
*
*/
private Integer count;
public List<ProductSpuBO> getSpus() {
return spus;
}
public ProductSpuPageBO setSpus(List<ProductSpuBO> spus) {
this.spus = spus;
return this;
}
public Integer getCount() {
return count;
}
public ProductSpuPageBO setCount(Integer count) {
this.count = count;
return this;
}
}

View File

@ -0,0 +1,41 @@
package cn.iocoder.mall.product.api.dto;
import javax.validation.constraints.NotNull;
public class ProductSpuPageDTO {
private String name;
@NotNull(message = "页码不能为空")
private Integer pageNo;
@NotNull(message = "每页条数不能为空")
private Integer pageSize;
public String getName() {
return name;
}
public ProductSpuPageDTO setName(String name) {
this.name = name;
return this;
}
public Integer getPageNo() {
return pageNo;
}
public ProductSpuPageDTO setPageNo(Integer pageNo) {
this.pageNo = pageNo;
return this;
}
public Integer getPageSize() {
return pageSize;
}
public ProductSpuPageDTO setPageSize(Integer pageSize) {
this.pageSize = pageSize;
return this;
}
}

View File

@ -13,6 +13,7 @@ import cn.iocoder.mall.product.dataobject.ProductSpuDO;
import org.mapstruct.Mapper; import org.mapstruct.Mapper;
import org.mapstruct.Mapping; import org.mapstruct.Mapping;
import org.mapstruct.Mappings; import org.mapstruct.Mappings;
import org.mapstruct.Named;
import org.mapstruct.factory.Mappers; import org.mapstruct.factory.Mappers;
import java.util.ArrayList; import java.util.ArrayList;
@ -25,9 +26,14 @@ public interface ProductSpuConvert {
ProductSpuConvert INSTANCE = Mappers.getMapper(ProductSpuConvert.class); ProductSpuConvert INSTANCE = Mappers.getMapper(ProductSpuConvert.class);
@Mappings({}) @Mappings({
@Mapping(source = "picUrls", target = "picUrls", qualifiedByName = "translatePicUrlsFromString")
})
ProductSpuBO convert(ProductSpuDO spu); ProductSpuBO convert(ProductSpuDO spu);
@Mappings({})
List<ProductSpuBO> convert(List<ProductSpuDO> spus);
@Mappings({ @Mappings({
@Mapping(source = "picUrls", target = "picUrls", ignore = true) @Mapping(source = "picUrls", target = "picUrls", ignore = true)
}) })
@ -79,4 +85,9 @@ public interface ProductSpuConvert {
return spuDetail; return spuDetail;
} }
@Named("translatePicUrlsFromString")
default List<String> translatePicUrlsFromString(String picUrls) {
return StringUtil.split(picUrls, ",");
}
} }

View File

@ -1,8 +1,11 @@
package cn.iocoder.mall.product.dao; package cn.iocoder.mall.product.dao;
import cn.iocoder.mall.product.dataobject.ProductSpuDO; import cn.iocoder.mall.product.dataobject.ProductSpuDO;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import java.util.List;
@Repository @Repository
public interface ProductSpuMapper { public interface ProductSpuMapper {
@ -12,4 +15,10 @@ public interface ProductSpuMapper {
void update(ProductSpuDO productSpuDO); void update(ProductSpuDO productSpuDO);
List<ProductSpuDO> selectListByNameLikeOrderBySortAsc(@Param("name") String name,
@Param("offset") Integer offset,
@Param("limit") Integer limit);
Integer selectCountByNameLike(@Param("name") String name);
} }

View File

@ -9,10 +9,12 @@ import cn.iocoder.mall.product.api.ProductSpuService;
import cn.iocoder.mall.product.api.bo.ProductAttrDetailBO; import cn.iocoder.mall.product.api.bo.ProductAttrDetailBO;
import cn.iocoder.mall.product.api.bo.ProductSpuBO; import cn.iocoder.mall.product.api.bo.ProductSpuBO;
import cn.iocoder.mall.product.api.bo.ProductSpuDetailBO; import cn.iocoder.mall.product.api.bo.ProductSpuDetailBO;
import cn.iocoder.mall.product.api.bo.ProductSpuPageBO;
import cn.iocoder.mall.product.api.constant.ProductErrorCodeEnum; import cn.iocoder.mall.product.api.constant.ProductErrorCodeEnum;
import cn.iocoder.mall.product.api.constant.ProductSpuConstants; import cn.iocoder.mall.product.api.constant.ProductSpuConstants;
import cn.iocoder.mall.product.api.dto.ProductSkuAddOrUpdateDTO; import cn.iocoder.mall.product.api.dto.ProductSkuAddOrUpdateDTO;
import cn.iocoder.mall.product.api.dto.ProductSpuAddDTO; import cn.iocoder.mall.product.api.dto.ProductSpuAddDTO;
import cn.iocoder.mall.product.api.dto.ProductSpuPageDTO;
import cn.iocoder.mall.product.api.dto.ProductSpuUpdateDTO; import cn.iocoder.mall.product.api.dto.ProductSpuUpdateDTO;
import cn.iocoder.mall.product.convert.ProductSpuConvert; import cn.iocoder.mall.product.convert.ProductSpuConvert;
import cn.iocoder.mall.product.dao.ProductSkuMapper; import cn.iocoder.mall.product.dao.ProductSkuMapper;
@ -169,6 +171,19 @@ public class ProductSpuServiceImpl implements ProductSpuService {
return CommonResult.success(true); return CommonResult.success(true);
} }
@Override
public CommonResult<ProductSpuPageBO> getProductSpuPage(ProductSpuPageDTO productSpuPageDTO) {
ProductSpuPageBO productSpuPage = new ProductSpuPageBO();
// 查询分页数据
int offset = productSpuPageDTO.getPageNo() * productSpuPageDTO.getPageSize();
productSpuPage.setSpus(ProductSpuConvert.INSTANCE.convert(productSpuMapper.selectListByNameLikeOrderBySortAsc(productSpuPageDTO.getName(),
offset, productSpuPageDTO.getPageSize())));
// 查询分页总数
productSpuPage.setCount(productSpuMapper.selectCountByNameLike(productSpuPageDTO.getName()));
// 返回结果
return CommonResult.success(productSpuPage);
}
private CommonResult<Boolean> validProductSku(List<ProductSkuAddOrUpdateDTO> productSkuAddDTOs, List<ProductAttrDetailBO> productAttrDetailBOs) { private CommonResult<Boolean> validProductSku(List<ProductSkuAddOrUpdateDTO> productSkuAddDTOs, List<ProductAttrDetailBO> productAttrDetailBOs) {
// 创建 ProductAttrDetailBO 的映射。其中KEY 为 ProductAttrDetailBO.attrValueId ,即规格值的编号 // 创建 ProductAttrDetailBO 的映射。其中KEY 为 ProductAttrDetailBO.attrValueId ,即规格值的编号
Map<Integer, ProductAttrDetailBO> productAttrDetailBOMap = productAttrDetailBOs.stream().collect( Map<Integer, ProductAttrDetailBO> productAttrDetailBOMap = productAttrDetailBOs.stream().collect(

View File

@ -2,11 +2,17 @@
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.iocoder.mall.product.dao.ProductSpuMapper"> <mapper namespace="cn.iocoder.mall.product.dao.ProductSpuMapper">
<sql id="FIELDS">
id, name, sell_point, description, cid,
pic_urls, visible, sort, create_time
</sql>
<select id="selectById" parameterType="Integer" resultType="ProductSpuDO"> <select id="selectById" parameterType="Integer" resultType="ProductSpuDO">
SELECT SELECT
id <include refid="FIELDS" />
FROM product_spu FROM product_spu
WHERE id = #{id} WHERE id = #{id}
AND deleted = 0
</select> </select>
<insert id="insert" parameterType="ProductSpuDO" useGeneratedKeys="true" keyColumn="id" keyProperty="id"> <insert id="insert" parameterType="ProductSpuDO" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
@ -50,4 +56,30 @@
WHERE id = #{id} WHERE id = #{id}
</update> </update>
<select id="selectListByNameLikeOrderBySortAsc" resultType="ProductSpuDO">
SELECT
<include refid="FIELDS" />
FROM product_spu
<where>
<if test="name != null">
name LIKE "%"#{name}"%"
</if>
AND deleted = 0
</where>
ORDER BY sort ASC
LIMIT #{offset}, #{limit}
</select>
<select id="selectCountByNameLike" resultType="Integer">
SELECT
COUNT(1)
FROM product_spu
<where>
<if test="name != null">
name LIKE "%"#{name}"%"
</if>
AND deleted = 0
</where>
</select>
</mapper> </mapper>