商品 Spu + Sku 部分 API 提交

This commit is contained in:
YunaiV
2019-03-04 23:10:48 +08:00
parent 5192472ed5
commit abf1f25033
23 changed files with 859 additions and 88 deletions

View File

@@ -8,8 +8,8 @@ import cn.iocoder.mall.product.api.constant.ProductCategoryConstants;
import cn.iocoder.mall.product.api.dto.ProductCategoryAddDTO;
import cn.iocoder.mall.product.api.dto.ProductCategoryUpdateDTO;
import cn.iocoder.mall.product.application.convert.ProductCategoryConvert;
import cn.iocoder.mall.product.application.vo.ProductCategoryTreeNodeVO;
import cn.iocoder.mall.product.application.vo.ProductCategoryVO;
import cn.iocoder.mall.product.application.vo.admins.AdminProductCategoryTreeNodeVO;
import cn.iocoder.mall.product.application.vo.admins.AdminsProductCategoryVO;
import com.alibaba.dubbo.config.annotation.Reference;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
@@ -33,16 +33,16 @@ public class AdminsProductCategoryController {
@GetMapping("/tree")
@ApiOperation("获得分类树结构")
public CommonResult<List<ProductCategoryTreeNodeVO>> tree() {
public CommonResult<List<AdminProductCategoryTreeNodeVO>> tree() {
List<ProductCategoryBO> productCategories = productCategoryService.getAll().getData();
// 创建 ProductCategoryTreeNodeVO Map
Map<Integer, ProductCategoryTreeNodeVO> treeNodeMap = productCategories.stream().collect(Collectors.toMap(ProductCategoryBO::getId, ProductCategoryConvert.INSTANCE::convert));
Map<Integer, AdminProductCategoryTreeNodeVO> treeNodeMap = productCategories.stream().collect(Collectors.toMap(ProductCategoryBO::getId, ProductCategoryConvert.INSTANCE::convert));
// 处理父子关系
treeNodeMap.values().stream()
.filter(node -> !node.getPid().equals(ProductCategoryConstants.PID_ROOT))
.forEach((childNode) -> {
// 获得父节点
ProductCategoryTreeNodeVO parentNode = treeNodeMap.get(childNode.getPid());
AdminProductCategoryTreeNodeVO parentNode = treeNodeMap.get(childNode.getPid());
if (parentNode.getChildren() == null) { // 初始化 children 数组
parentNode.setChildren(new ArrayList<>());
}
@@ -50,9 +50,9 @@ public class AdminsProductCategoryController {
parentNode.getChildren().add(childNode);
});
// 获得到所有的根节点
List<ProductCategoryTreeNodeVO> rootNodes = treeNodeMap.values().stream()
List<AdminProductCategoryTreeNodeVO> rootNodes = treeNodeMap.values().stream()
.filter(node -> node.getPid().equals(ProductCategoryConstants.PID_ROOT))
.sorted(Comparator.comparing(ProductCategoryTreeNodeVO::getSort))
.sorted(Comparator.comparing(AdminProductCategoryTreeNodeVO::getSort))
.collect(Collectors.toList());
return CommonResult.success(rootNodes);
}
@@ -66,11 +66,11 @@ public class AdminsProductCategoryController {
@ApiImplicitParam(name = "picUrl", value = "分类图片", example = "http://www.iocoder.cn/images/common/wechat_mp_2017_07_31_bak.jpg/"),
@ApiImplicitParam(name = "sort", value = "排序", required = true, example = "1"),
})
public CommonResult<ProductCategoryVO> add(@RequestParam("pid") Integer pid,
@RequestParam("name") String name,
@RequestParam("description") String description,
@RequestParam(value = "picUrl", required = false) String picUrl,
@RequestParam("sort") Integer sort) {
public CommonResult<AdminsProductCategoryVO> add(@RequestParam("pid") Integer pid,
@RequestParam("name") String name,
@RequestParam("description") String description,
@RequestParam(value = "picUrl", required = false) String picUrl,
@RequestParam("sort") Integer sort) {
// 创建 ProductCategoryAddDTO 对象
ProductCategoryAddDTO productCategoryAddDTO = new ProductCategoryAddDTO().setPid(pid).setName(name)
.setDescription(description).setPicUrl(picUrl).setSort(sort);

View File

@@ -0,0 +1,101 @@
package cn.iocoder.mall.product.application.controller.admins;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.admin.sdk.context.AdminSecurityContextHolder;
import cn.iocoder.mall.product.api.ProductSpuService;
import cn.iocoder.mall.product.api.bo.ProductSpuDetailBO;
import cn.iocoder.mall.product.api.dto.ProductSkuAddDTO;
import cn.iocoder.mall.product.api.dto.ProductSkuUpdateDTO;
import cn.iocoder.mall.product.api.dto.ProductSpuAddDTO;
import cn.iocoder.mall.product.api.dto.ProductSpuUpdateDTO;
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.AdminsProductSpuPageVO;
import com.alibaba.dubbo.config.annotation.Reference;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.util.List;
@RestController
@RequestMapping("admins")
@Api("商品 SPU + SKU")
public class AdminsProductSpuController {
@Reference(validation = "true")
private ProductSpuService productSpuService;
@Autowired
private ObjectMapper objectMapper; // jackson 解析
@PostMapping("/spu/add")
@ApiOperation("创建商品")
public CommonResult<AdminsProductSpuDetailVO> add(@RequestParam("name") String name,
@RequestParam("sellPoint") String sellPoint,
@RequestParam("description") String description,
@RequestParam("cid") Integer cid,
@RequestParam("picURLs") List<String> picUrls,
@RequestParam("visible") Boolean visible,
@RequestParam("skuStr") String skuStr) { // TODO 芋艿,因为考虑不使用 json 接受参数,所以这里手动转。
// 创建 ProductSpuAddDTO 对象
ProductSpuAddDTO productSpuAddDTO = new ProductSpuAddDTO().setName(name).setSellPoint(sellPoint)
.setDescription(description).setCid(cid).setPicUrls(picUrls).setVisible(visible)
.setSkus(parseSkus(skuStr, ProductSkuAddDTO.class));
// 保存商品
CommonResult<ProductSpuDetailBO> result = productSpuService.addProductSpu(AdminSecurityContextHolder.getContext().getAdminId(), productSpuAddDTO);
// 返回结果
return ProductSpuConvert.INSTANCE.convert(result);
}
@PostMapping("/spu/update")
@ApiOperation("更新商品")
public CommonResult<Boolean> update(@RequestParam("id") Integer id,
@RequestParam("name") String name,
@RequestParam("sellPoint") String sellPoint,
@RequestParam("description") String description,
@RequestParam("cid") Integer cid,
@RequestParam("picURLs") List<String> picUrls,
@RequestParam("visible") Boolean visible,
@RequestParam("skuStr") String skuStr) { // TODO 芋艿,因为考虑不使用 json 接受参数,所以这里手动转。
// 创建 ProductSpuUpdateDTO 对象
ProductSpuUpdateDTO productSpuUpdateDTO = new ProductSpuUpdateDTO().setId(id).setName(name).setSellPoint(sellPoint)
.setDescription(description).setCid(cid).setPicUrls(picUrls).setVisible(visible)
.setSkus(parseSkus(skuStr, ProductSkuUpdateDTO.class));
// 更新商品
return productSpuService.updateProductSpu(AdminSecurityContextHolder.getContext().getAdminId(), productSpuUpdateDTO);
}
@PostMapping("/spu/update_sort")
@ApiOperation("更新商品的排序")
public CommonResult<Boolean> updateSort(@RequestParam("id") Integer id,
@RequestParam("sort") Integer sort) {
return null;
}
@GetMapping("/spu/page")
@ApiOperation("商品 SPU 分页列表")
public CommonResult<AdminsProductSpuPageVO> spuPage() {
return null;
}
@GetMapping("/spu/info")
@ApiOperation("商品 SPU 明细")
public CommonResult<AdminsProductSpuDetailVO> info() {
return null;
}
private <T> List<T> parseSkus(String skuStr, Class<T> clazz) {
JavaType type = objectMapper.getTypeFactory().constructParametricType(List.class, clazz);
try {
return objectMapper.readValue(skuStr, type);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

View File

@@ -3,7 +3,7 @@ package cn.iocoder.mall.product.application.controller.users;
import cn.iocoder.mall.product.api.ProductCategoryService;
import cn.iocoder.mall.product.api.bo.ProductCategoryBO;
import cn.iocoder.mall.product.application.convert.ProductCategoryConvert;
import cn.iocoder.mall.product.application.vo.ProductCategorySimpleVO;
import cn.iocoder.mall.product.application.vo.users.UsersProductCategoryVO;
import com.alibaba.dubbo.config.annotation.Reference;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
@@ -26,7 +26,7 @@ public class ProductCategoryController {
@GetMapping
@ApiOperation("获得指定编号下的子分类的数组")
@ApiImplicitParam(name = "pid", value = "指定分类编号", required = true, example = "0")
public List<ProductCategorySimpleVO> list(@RequestParam("pid") Integer pid) {
public List<UsersProductCategoryVO> list(@RequestParam("pid") Integer pid) {
List<ProductCategoryBO> result = productCategoryService.getListByPid(pid);
return ProductCategoryConvert.INSTANCE.convertToVO(result);
}

View File

@@ -2,9 +2,9 @@ package cn.iocoder.mall.product.application.convert;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.product.api.bo.ProductCategoryBO;
import cn.iocoder.mall.product.application.vo.ProductCategorySimpleVO;
import cn.iocoder.mall.product.application.vo.ProductCategoryTreeNodeVO;
import cn.iocoder.mall.product.application.vo.ProductCategoryVO;
import cn.iocoder.mall.product.application.vo.users.UsersProductCategoryVO;
import cn.iocoder.mall.product.application.vo.admins.AdminProductCategoryTreeNodeVO;
import cn.iocoder.mall.product.application.vo.admins.AdminsProductCategoryVO;
import org.mapstruct.Mapper;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;
@@ -17,12 +17,12 @@ public interface ProductCategoryConvert {
ProductCategoryConvert INSTANCE = Mappers.getMapper(ProductCategoryConvert.class);
@Mappings({})
ProductCategorySimpleVO convertToVO(ProductCategoryBO category);
UsersProductCategoryVO convertToVO(ProductCategoryBO category);
List<ProductCategorySimpleVO> convertToVO(List<ProductCategoryBO> categoryList);
List<UsersProductCategoryVO> convertToVO(List<ProductCategoryBO> categoryList);
ProductCategoryTreeNodeVO convert(ProductCategoryBO category);
AdminProductCategoryTreeNodeVO convert(ProductCategoryBO category);
CommonResult<ProductCategoryVO> convert(CommonResult<ProductCategoryBO> result);
CommonResult<AdminsProductCategoryVO> convert(CommonResult<ProductCategoryBO> result);
}

View File

@@ -0,0 +1,21 @@
package cn.iocoder.mall.product.application.convert;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.product.api.bo.ProductSpuDetailBO;
import cn.iocoder.mall.product.application.vo.admins.AdminsProductSpuDetailVO;
import org.mapstruct.Mapper;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;
@Mapper
public interface ProductSpuConvert {
ProductSpuConvert INSTANCE = Mappers.getMapper(ProductSpuConvert.class);
@Mappings({})
AdminsProductSpuDetailVO convert(ProductSpuDetailBO productSpuDetailBO);
@Mappings({})
CommonResult<AdminsProductSpuDetailVO> convert(CommonResult<ProductSpuDetailBO> result);
}

View File

@@ -1,4 +1,4 @@
package cn.iocoder.mall.product.application.vo;
package cn.iocoder.mall.product.application.vo.admins;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
@@ -7,7 +7,7 @@ import java.util.Date;
import java.util.List;
@ApiModel("产品分类树节点 VO")
public class ProductCategoryTreeNodeVO {
public class AdminProductCategoryTreeNodeVO {
@ApiModelProperty(value = "分类编号", required = true, example = "1")
private Integer id;
@@ -26,13 +26,13 @@ public class ProductCategoryTreeNodeVO {
@ApiModelProperty(value = "创建时间", required = true, example = "时间戳")
private Date createTime;
@ApiModelProperty(value = "子节点数组")
private List<ProductCategoryTreeNodeVO> children;
private List<AdminProductCategoryTreeNodeVO> children;
public Integer getId() {
return id;
}
public ProductCategoryTreeNodeVO setId(Integer id) {
public AdminProductCategoryTreeNodeVO setId(Integer id) {
this.id = id;
return this;
}
@@ -41,7 +41,7 @@ public class ProductCategoryTreeNodeVO {
return pid;
}
public ProductCategoryTreeNodeVO setPid(Integer pid) {
public AdminProductCategoryTreeNodeVO setPid(Integer pid) {
this.pid = pid;
return this;
}
@@ -50,7 +50,7 @@ public class ProductCategoryTreeNodeVO {
return name;
}
public ProductCategoryTreeNodeVO setName(String name) {
public AdminProductCategoryTreeNodeVO setName(String name) {
this.name = name;
return this;
}
@@ -59,7 +59,7 @@ public class ProductCategoryTreeNodeVO {
return description;
}
public ProductCategoryTreeNodeVO setDescription(String description) {
public AdminProductCategoryTreeNodeVO setDescription(String description) {
this.description = description;
return this;
}
@@ -68,7 +68,7 @@ public class ProductCategoryTreeNodeVO {
return picUrl;
}
public ProductCategoryTreeNodeVO setPicUrl(String picUrl) {
public AdminProductCategoryTreeNodeVO setPicUrl(String picUrl) {
this.picUrl = picUrl;
return this;
}
@@ -77,7 +77,7 @@ public class ProductCategoryTreeNodeVO {
return sort;
}
public ProductCategoryTreeNodeVO setSort(Integer sort) {
public AdminProductCategoryTreeNodeVO setSort(Integer sort) {
this.sort = sort;
return this;
}
@@ -86,7 +86,7 @@ public class ProductCategoryTreeNodeVO {
return status;
}
public ProductCategoryTreeNodeVO setStatus(Integer status) {
public AdminProductCategoryTreeNodeVO setStatus(Integer status) {
this.status = status;
return this;
}
@@ -95,16 +95,16 @@ public class ProductCategoryTreeNodeVO {
return createTime;
}
public ProductCategoryTreeNodeVO setCreateTime(Date createTime) {
public AdminProductCategoryTreeNodeVO setCreateTime(Date createTime) {
this.createTime = createTime;
return this;
}
public List<ProductCategoryTreeNodeVO> getChildren() {
public List<AdminProductCategoryTreeNodeVO> getChildren() {
return children;
}
public ProductCategoryTreeNodeVO setChildren(List<ProductCategoryTreeNodeVO> children) {
public AdminProductCategoryTreeNodeVO setChildren(List<AdminProductCategoryTreeNodeVO> children) {
this.children = children;
return this;
}

View File

@@ -1,4 +1,4 @@
package cn.iocoder.mall.product.application.vo;
package cn.iocoder.mall.product.application.vo.admins;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
@@ -6,7 +6,7 @@ import io.swagger.annotations.ApiModelProperty;
import java.util.Date;
@ApiModel("产品分类 VO")
public class ProductCategoryVO {
public class AdminsProductCategoryVO {
@ApiModelProperty(value = "分类编号", required = true, example = "1")
private Integer id;
@@ -29,7 +29,7 @@ public class ProductCategoryVO {
return id;
}
public ProductCategoryVO setId(Integer id) {
public AdminsProductCategoryVO setId(Integer id) {
this.id = id;
return this;
}
@@ -38,7 +38,7 @@ public class ProductCategoryVO {
return pid;
}
public ProductCategoryVO setPid(Integer pid) {
public AdminsProductCategoryVO setPid(Integer pid) {
this.pid = pid;
return this;
}
@@ -47,7 +47,7 @@ public class ProductCategoryVO {
return name;
}
public ProductCategoryVO setName(String name) {
public AdminsProductCategoryVO setName(String name) {
this.name = name;
return this;
}
@@ -56,7 +56,7 @@ public class ProductCategoryVO {
return description;
}
public ProductCategoryVO setDescription(String description) {
public AdminsProductCategoryVO setDescription(String description) {
this.description = description;
return this;
}
@@ -65,7 +65,7 @@ public class ProductCategoryVO {
return picUrl;
}
public ProductCategoryVO setPicUrl(String picUrl) {
public AdminsProductCategoryVO setPicUrl(String picUrl) {
this.picUrl = picUrl;
return this;
}
@@ -74,7 +74,7 @@ public class ProductCategoryVO {
return sort;
}
public ProductCategoryVO setSort(Integer sort) {
public AdminsProductCategoryVO setSort(Integer sort) {
this.sort = sort;
return this;
}
@@ -83,7 +83,7 @@ public class ProductCategoryVO {
return status;
}
public ProductCategoryVO setStatus(Integer status) {
public AdminsProductCategoryVO setStatus(Integer status) {
this.status = status;
return this;
}
@@ -92,7 +92,7 @@ public class ProductCategoryVO {
return createTime;
}
public ProductCategoryVO setCreateTime(Date createTime) {
public AdminsProductCategoryVO setCreateTime(Date createTime) {
this.createTime = createTime;
return this;
}

View File

@@ -0,0 +1,10 @@
package cn.iocoder.mall.product.application.vo.admins;
import io.swagger.annotations.ApiModel;
@ApiModel(value = "商品 SPU 详细 VO", description = "包括 SKU 信息 VO")
public class AdminsProductSpuDetailVO {
}

View File

@@ -0,0 +1,37 @@
package cn.iocoder.mall.product.application.vo.admins;
import io.swagger.annotations.ApiModel;
import java.util.List;
@ApiModel("商品 SPU 分页 VO")
public class AdminsProductSpuPageVO {
/**
* spu 数组
*/
private List<AdminsProductSpuVO> spus;
/**
* 总数
*/
private Integer count;
public List<AdminsProductSpuVO> getSpus() {
return spus;
}
public AdminsProductSpuPageVO setSpus(List<AdminsProductSpuVO> spus) {
this.spus = spus;
return this;
}
public Integer getCount() {
return count;
}
public AdminsProductSpuPageVO setCount(Integer count) {
this.count = count;
return this;
}
}

View File

@@ -0,0 +1,10 @@
package cn.iocoder.mall.product.application.vo.admins;
import io.swagger.annotations.ApiModel;
@ApiModel(value = "商品 SPU VO", description = "不包括 SKU 信息 VO")
public class AdminsProductSpuVO {
}

View File

@@ -1,10 +1,10 @@
package cn.iocoder.mall.product.application.vo;
package cn.iocoder.mall.product.application.vo.users;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
@ApiModel("商品分类(简单)")
public class ProductCategorySimpleVO {
public class UsersProductCategoryVO {
@ApiModelProperty(value = "分类编号", required = true, example = "1")
private Integer id;