商品分类的迁移,继续未完成,继续先提交下~
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
package cn.iocoder.mall.managementweb.controller.product;
|
||||
|
||||
import cn.iocoder.common.framework.vo.CommonResult;
|
||||
import cn.iocoder.mall.managementweb.controller.product.vo.category.ProductCategoryCreateReqVO;
|
||||
import cn.iocoder.mall.managementweb.controller.product.vo.category.ProductCategoryUpdateReqVO;
|
||||
import cn.iocoder.mall.managementweb.manager.product.ProductCategoryManager;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiImplicitParam;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.validation.Valid;
|
||||
|
||||
import static cn.iocoder.common.framework.vo.CommonResult.success;
|
||||
|
||||
/**
|
||||
* 商品分类 Controller
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/product_category")
|
||||
@Api(tags = "商品分类")
|
||||
@Validated
|
||||
public class ProductCategoryController {
|
||||
|
||||
@Autowired
|
||||
private ProductCategoryManager productCategoryManager;
|
||||
|
||||
@PostMapping("/create")
|
||||
@ApiOperation("创建商品分类")
|
||||
public CommonResult<Integer> createProductCategory(@Valid ProductCategoryCreateReqVO createVO) {
|
||||
return success(productCategoryManager.createProductCategory(createVO));
|
||||
}
|
||||
|
||||
@PostMapping("/update")
|
||||
@ApiOperation("更新商品分类")
|
||||
public CommonResult<Boolean> updateProductCategory(@Valid ProductCategoryUpdateReqVO updateVO) {
|
||||
productCategoryManager.updateProductCategory(updateVO);
|
||||
return success(true);
|
||||
}
|
||||
|
||||
@PostMapping("/delete")
|
||||
@ApiOperation("删除商品分类")
|
||||
@ApiImplicitParam(name = "productCategoryId", value = "商品分类编号", required = true)
|
||||
public CommonResult<Boolean> deleteProductCategory(@RequestParam("productCategoryId") Integer productCategoryId) {
|
||||
productCategoryManager.deleteProductCategory(productCategoryId);
|
||||
return success(true);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package cn.iocoder.mall.managementweb.controller.product.vo.category;
|
||||
|
||||
import cn.iocoder.common.framework.enums.CommonStatusEnum;
|
||||
import cn.iocoder.common.framework.validator.InEnum;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
@ApiModel("商品分类创建 Request VO")
|
||||
@Data
|
||||
public class ProductCategoryCreateReqVO {
|
||||
|
||||
@ApiModelProperty(value = "父分类编号", required = true, example = "0")
|
||||
@NotNull(message = "父分类编号不能为空")
|
||||
private Integer pid;
|
||||
@ApiModelProperty(value = "分类名称", required = true, example = "手机")
|
||||
@NotEmpty(message = "分类名称不能为空")
|
||||
private String name;
|
||||
@ApiModelProperty(value = "分类描述", required = true, example = "这个商品很吊")
|
||||
private String description;
|
||||
@ApiModelProperty(value = "分类图片", notes = "一般情况下,只有根分类才有图片", example = "http://www.iocoder.cn/xx.jpg")
|
||||
private String picUrl;
|
||||
@ApiModelProperty(value = "分类排序", required = true, example = "10")
|
||||
@NotNull(message = "分类排序不能为空")
|
||||
private Integer sort;
|
||||
@ApiModelProperty(value = "状态", required = true, example = "1", notes = "见 CommonStatusEnum 枚举")
|
||||
@NotNull(message = "状态不能为空")
|
||||
@InEnum(value = CommonStatusEnum.class, message = "修改状态必须是 {value}")
|
||||
private Integer status;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package cn.iocoder.mall.managementweb.controller.product.vo.category;
|
||||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
|
||||
@ApiModel("商品分类 Response VO")
|
||||
@Data
|
||||
public class ProductCategoryRespVO {
|
||||
|
||||
@ApiModelProperty(value = "分类编号", required = true, example = "1")
|
||||
private Integer id;
|
||||
@ApiModelProperty(value = "父分类编号", required = true, example = "0")
|
||||
private Integer pid;
|
||||
@ApiModelProperty(value = "分类名称", required = true, example = "手机")
|
||||
private String name;
|
||||
@ApiModelProperty(value = "分类描述", required = true, example = "这个商品很吊")
|
||||
private String description;
|
||||
@ApiModelProperty(value = "分类图片", notes = "一般情况下,只有根分类才有图片", example = "http://www.iocoder.cn/xx.jpg")
|
||||
private String picUrl;
|
||||
@ApiModelProperty(value = "分类排序", required = true, example = "10")
|
||||
private Integer sort;
|
||||
@ApiModelProperty(value = "状态", required = true, example = "1", notes = "见 CommonStatusEnum 枚举")
|
||||
private Integer status;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package cn.iocoder.mall.managementweb.controller.product.vo.category;
|
||||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@ApiModel("商品分类 Response VO")
|
||||
@Data
|
||||
public class ProductCategoryTreeNodeRespVO {
|
||||
|
||||
@ApiModelProperty(value = "分类编号", required = true, example = "1")
|
||||
private Integer id;
|
||||
@ApiModelProperty(value = "父分类编号", required = true, example = "0")
|
||||
private Integer pid;
|
||||
@ApiModelProperty(value = "分类名称", required = true, example = "手机")
|
||||
private String name;
|
||||
@ApiModelProperty(value = "分类描述", required = true, example = "这个商品很吊")
|
||||
private String description;
|
||||
@ApiModelProperty(value = "分类图片", notes = "一般情况下,只有根分类才有图片", example = "http://www.iocoder.cn/xx.jpg")
|
||||
private String picUrl;
|
||||
@ApiModelProperty(value = "分类排序", required = true, example = "10")
|
||||
private Integer sort;
|
||||
@ApiModelProperty(value = "状态", required = true, example = "1", notes = "见 CommonStatusEnum 枚举")
|
||||
private Integer status;
|
||||
|
||||
/**
|
||||
* 子节点
|
||||
*/
|
||||
private List<ProductCategoryTreeNodeRespVO> children;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package cn.iocoder.mall.managementweb.controller.product.vo.category;
|
||||
|
||||
import cn.iocoder.common.framework.enums.CommonStatusEnum;
|
||||
import cn.iocoder.common.framework.validator.InEnum;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
@ApiModel("商品分类更新 Request VO")
|
||||
@Data
|
||||
public class ProductCategoryUpdateReqVO {
|
||||
|
||||
@ApiModelProperty(value = "分类编号", required = true, example = "1")
|
||||
@NotNull(message = "分类编号不能为空")
|
||||
private Integer id;
|
||||
@ApiModelProperty(value = "父分类编号", required = true, example = "0")
|
||||
@NotNull(message = "父分类编号不能为空")
|
||||
private Integer pid;
|
||||
@ApiModelProperty(value = "分类名称", required = true, example = "手机")
|
||||
@NotEmpty(message = "分类名称不能为空")
|
||||
private String name;
|
||||
@ApiModelProperty(value = "分类描述", required = true, example = "这个商品很吊")
|
||||
private String description;
|
||||
@ApiModelProperty(value = "分类图片", notes = "一般情况下,只有根分类才有图片", example = "http://www.iocoder.cn/xx.jpg")
|
||||
private String picUrl;
|
||||
@ApiModelProperty(value = "分类排序", required = true, example = "10")
|
||||
@NotNull(message = "分类排序不能为空")
|
||||
private Integer sort;
|
||||
@ApiModelProperty(value = "状态", required = true, example = "1", notes = "见 CommonStatusEnum 枚举")
|
||||
@NotNull(message = "状态不能为空")
|
||||
@InEnum(value = CommonStatusEnum.class, message = "修改状态必须是 {value}")
|
||||
private Integer status;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package cn.iocoder.mall.managementweb.convert.product;
|
||||
|
||||
import cn.iocoder.mall.managementweb.controller.product.vo.category.ProductCategoryCreateReqVO;
|
||||
import cn.iocoder.mall.managementweb.controller.product.vo.category.ProductCategoryRespVO;
|
||||
import cn.iocoder.mall.managementweb.controller.product.vo.category.ProductCategoryTreeNodeRespVO;
|
||||
import cn.iocoder.mall.managementweb.controller.product.vo.category.ProductCategoryUpdateReqVO;
|
||||
import cn.iocoder.mall.productservice.rpc.category.dto.ProductCategoryCreateReqDTO;
|
||||
import cn.iocoder.mall.productservice.rpc.category.dto.ProductCategoryRespDTO;
|
||||
import cn.iocoder.mall.productservice.rpc.category.dto.ProductCategoryUpdateReqDTO;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
@Mapper
|
||||
public interface ProductCategoryConvert {
|
||||
|
||||
ProductCategoryConvert INSTANCE = Mappers.getMapper(ProductCategoryConvert.class);
|
||||
|
||||
ProductCategoryCreateReqDTO convert(ProductCategoryCreateReqVO bean);
|
||||
|
||||
ProductCategoryUpdateReqDTO convert(ProductCategoryUpdateReqVO bean);
|
||||
|
||||
ProductCategoryRespVO convert(ProductCategoryRespDTO bean);
|
||||
|
||||
ProductCategoryTreeNodeRespVO convertTreeNode(ProductCategoryRespDTO bean);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
package cn.iocoder.mall.managementweb.manager.product;
|
||||
|
||||
import cn.iocoder.common.framework.vo.CommonResult;
|
||||
import cn.iocoder.mall.managementweb.controller.product.vo.category.ProductCategoryCreateReqVO;
|
||||
import cn.iocoder.mall.managementweb.controller.product.vo.category.ProductCategoryTreeNodeRespVO;
|
||||
import cn.iocoder.mall.managementweb.controller.product.vo.category.ProductCategoryUpdateReqVO;
|
||||
import cn.iocoder.mall.managementweb.convert.product.ProductCategoryConvert;
|
||||
import cn.iocoder.mall.productservice.enums.category.ProductCategoryIdEnum;
|
||||
import cn.iocoder.mall.productservice.rpc.category.ProductCategoryRpc;
|
||||
import cn.iocoder.mall.productservice.rpc.category.dto.ProductCategoryListQueryReqDTO;
|
||||
import cn.iocoder.mall.productservice.rpc.category.dto.ProductCategoryRespDTO;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.dubbo.config.annotation.Reference;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 商品分类表 Manager
|
||||
*/
|
||||
@Service
|
||||
@Slf4j
|
||||
public class ProductCategoryManager {
|
||||
|
||||
@Reference(version = "${dubbo.consumer.ProductCategoryRpc.version}")
|
||||
private ProductCategoryRpc productCategoryRpc;
|
||||
|
||||
/**
|
||||
* 创建商品分类表
|
||||
*
|
||||
* @param createVO 创建商品分类表 VO
|
||||
* @return 商品分类表
|
||||
*/
|
||||
public Integer createProductCategory(ProductCategoryCreateReqVO createVO) {
|
||||
CommonResult<Integer> createProductCategoryResult = productCategoryRpc.createProductCategory(ProductCategoryConvert.INSTANCE.convert(createVO));
|
||||
createProductCategoryResult.checkError();
|
||||
return createProductCategoryResult.getData();
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新商品分类表
|
||||
*
|
||||
* @param updateVO 更新商品分类表 VO
|
||||
*/
|
||||
public void updateProductCategory(ProductCategoryUpdateReqVO updateVO) {
|
||||
CommonResult<Boolean> updateProductCategoryResult = productCategoryRpc.updateProductCategory(ProductCategoryConvert.INSTANCE.convert(updateVO));
|
||||
updateProductCategoryResult.checkError();
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除商品分类表
|
||||
*
|
||||
* @param productCategoryId 商品分类表编号
|
||||
*/
|
||||
public void deleteProductCategory(Integer productCategoryId) {
|
||||
CommonResult<Boolean> deleteProductCategoryResult = productCategoryRpc.deleteProductCategory(productCategoryId);
|
||||
deleteProductCategoryResult.checkError();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得商品分类树结构
|
||||
*
|
||||
* @return 商品分类树结构
|
||||
*/
|
||||
public List<ProductCategoryTreeNodeRespVO> treeProductCategory() {
|
||||
// 获得商品分类全列表
|
||||
CommonResult<List<ProductCategoryRespDTO>> listProductCategories = productCategoryRpc.listProductCategories(new ProductCategoryListQueryReqDTO());
|
||||
listProductCategories.checkError();
|
||||
// 构建菜单树
|
||||
return buildProductCategoryTree(listProductCategories.getData());
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建商品分类树
|
||||
*
|
||||
* @param productCategories 商品分类列表
|
||||
* @return 商品分类树
|
||||
*/
|
||||
private static List<ProductCategoryTreeNodeRespVO> buildProductCategoryTree(List<ProductCategoryRespDTO> productCategories) {
|
||||
// 排序,保证菜单的有序性
|
||||
productCategories.sort(Comparator.comparing(ProductCategoryRespDTO::getSort));
|
||||
// 构建菜单树
|
||||
// 使用 LinkedHashMap 的原因,是为了排序 。实际也可以用 Stream API ,就是太丑了。
|
||||
Map<Integer, ProductCategoryTreeNodeRespVO> treeNodeMap = new LinkedHashMap<>();
|
||||
productCategories.forEach(category -> treeNodeMap.put(category.getId(), ProductCategoryConvert.INSTANCE.convertTreeNode(category)));
|
||||
// 处理父子关系
|
||||
treeNodeMap.values().stream().filter(node -> !node.getPid().equals(ProductCategoryIdEnum.ROOT.getId())).forEach((childNode) -> {
|
||||
// 获得父节点
|
||||
ProductCategoryTreeNodeRespVO parentNode = treeNodeMap.get(childNode.getPid());
|
||||
if (parentNode == null) {
|
||||
log.error("[buildProductCategoryTree][category({}) 找不到父商品分类({})]", childNode.getId(), childNode.getPid());
|
||||
return;
|
||||
}
|
||||
// 将自己添加到父节点中
|
||||
if (parentNode.getChildren() == null) {
|
||||
parentNode.setChildren(new ArrayList<>());
|
||||
}
|
||||
parentNode.getChildren().add(childNode);
|
||||
});
|
||||
// 获得到所有的根节点
|
||||
return treeNodeMap.values().stream().filter(node -> node.getPid().equals(ProductCategoryIdEnum.ROOT.getId()))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -49,6 +49,8 @@ dubbo:
|
||||
version: 1.0.0
|
||||
ErrorCodeRpc:
|
||||
version: 1.0.0
|
||||
ProductCategoryRpc:
|
||||
version: 1.0.0
|
||||
|
||||
# Swagger 配置项
|
||||
swagger:
|
||||
|
||||
Reference in New Issue
Block a user