商品分类提交~
This commit is contained in:
@@ -1,14 +1,124 @@
|
||||
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.ProductCategoryService;
|
||||
import cn.iocoder.mall.product.api.bo.ProductCategoryBO;
|
||||
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 com.alibaba.dubbo.config.annotation.Reference;
|
||||
import io.swagger.annotations.Api;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import io.swagger.annotations.ApiImplicitParam;
|
||||
import io.swagger.annotations.ApiImplicitParams;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("admins/product/category")
|
||||
@RequestMapping("admins/category")
|
||||
@Api("商品分类")
|
||||
public class AdminsProductCategoryController {
|
||||
|
||||
|
||||
@Reference(validation = "true")
|
||||
private ProductCategoryService productCategoryService;
|
||||
|
||||
@GetMapping("/tree")
|
||||
@ApiOperation("获得分类树结构")
|
||||
public CommonResult<List<ProductCategoryTreeNodeVO>> tree() {
|
||||
List<ProductCategoryBO> productCategories = productCategoryService.getAll().getData();
|
||||
// 创建 ProductCategoryTreeNodeVO Map
|
||||
Map<Integer, ProductCategoryTreeNodeVO> 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());
|
||||
if (parentNode.getChildren() == null) { // 初始化 children 数组
|
||||
parentNode.setChildren(new ArrayList<>());
|
||||
}
|
||||
// 将自己添加到父节点中
|
||||
parentNode.getChildren().add(childNode);
|
||||
});
|
||||
// 获得到所有的根节点
|
||||
List<ProductCategoryTreeNodeVO> rootNodes = treeNodeMap.values().stream()
|
||||
.filter(node -> node.getPid().equals(ProductCategoryConstants.PID_ROOT))
|
||||
.sorted(Comparator.comparing(ProductCategoryTreeNodeVO::getSort))
|
||||
.collect(Collectors.toList());
|
||||
return CommonResult.success(rootNodes);
|
||||
}
|
||||
|
||||
@PostMapping("/add")
|
||||
@ApiOperation(value = "创建商品分类")
|
||||
@ApiImplicitParams({
|
||||
@ApiImplicitParam(name = "pid", value = "父级分类编号", required = true, example = "1"),
|
||||
@ApiImplicitParam(name = "name", value = "分类名字(标识)", required = true, example = "admin/info"),
|
||||
@ApiImplicitParam(name = "description", value = "描述", required = true, example = "1"),
|
||||
@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) {
|
||||
// 创建 ProductCategoryAddDTO 对象
|
||||
ProductCategoryAddDTO productCategoryAddDTO = new ProductCategoryAddDTO().setPid(pid).setName(name)
|
||||
.setDescription(description).setPicUrl(picUrl).setSort(sort);
|
||||
// 创建商品分类
|
||||
CommonResult<ProductCategoryBO> result = productCategoryService.addProductCategory(AdminSecurityContextHolder.getContext().getAdminId(), productCategoryAddDTO);
|
||||
// 返回结果
|
||||
return ProductCategoryConvert.INSTANCE.convert(result);
|
||||
}
|
||||
|
||||
@PostMapping("/update")
|
||||
@ApiOperation(value = "更新商品分类")
|
||||
@ApiImplicitParams({
|
||||
@ApiImplicitParam(name = "id", value = "分类编号", required = true, example = "1"),
|
||||
@ApiImplicitParam(name = "pid", value = "父级分类编号", required = true, example = "1"),
|
||||
@ApiImplicitParam(name = "name", value = "分类名字(标识)", required = true, example = "admin/info"),
|
||||
@ApiImplicitParam(name = "description", value = "描述", required = true, example = "1"),
|
||||
@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<Boolean> update(@RequestParam("id") Integer id,
|
||||
@RequestParam("pid") Integer pid,
|
||||
@RequestParam("name") String name,
|
||||
@RequestParam("description") String description,
|
||||
@RequestParam(value = "picUrl", required = false) String picUrl,
|
||||
@RequestParam("sort") Integer sort) {
|
||||
// 创建 ProductCategoryUpdateDTO 对象
|
||||
ProductCategoryUpdateDTO productCategoryAddDTO = new ProductCategoryUpdateDTO().setId(id).setPid(pid).setName(name)
|
||||
.setDescription(description).setPicUrl(picUrl).setSort(sort);
|
||||
// 更新商品分类
|
||||
return productCategoryService.updateProductCategory(AdminSecurityContextHolder.getContext().getAdminId(), productCategoryAddDTO);
|
||||
}
|
||||
|
||||
@PostMapping("/update_status")
|
||||
@ApiOperation(value = "更新商品分类状态")
|
||||
@ApiImplicitParams({
|
||||
@ApiImplicitParam(name = "id", value = "商品分类编号", required = true, example = "1"),
|
||||
@ApiImplicitParam(name = "status", value = "状态。1 - 开启;2 - 禁用", required = true, example = "1"),
|
||||
})
|
||||
public CommonResult<Boolean> updateStatus(@RequestParam("id") Integer id,
|
||||
@RequestParam("status") Integer status) {
|
||||
return productCategoryService.updateProductCategoryStatus(AdminSecurityContextHolder.getContext().getAdminId(), id, status);
|
||||
}
|
||||
|
||||
@PostMapping("/delete")
|
||||
@ApiOperation(value = "删除商品分类")
|
||||
@ApiImplicitParam(name = "id", value = "商品分类编号", required = true, example = "1")
|
||||
public CommonResult<Boolean> delete(@RequestParam("id") Integer id) {
|
||||
return productCategoryService.deleteProductCategory(AdminSecurityContextHolder.getContext().getAdminId(), id);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,7 +1,10 @@
|
||||
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 org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mappings;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
@@ -18,5 +21,8 @@ public interface ProductCategoryConvert {
|
||||
|
||||
List<ProductCategorySimpleVO> convertToVO(List<ProductCategoryBO> categoryList);
|
||||
|
||||
ProductCategoryTreeNodeVO convert(ProductCategoryBO category);
|
||||
|
||||
}
|
||||
CommonResult<ProductCategoryVO> convert(CommonResult<ProductCategoryBO> result);
|
||||
|
||||
}
|
||||
@@ -1,10 +1,112 @@
|
||||
package cn.iocoder.mall.product.application.vo;
|
||||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
@ApiModel("产品分类树节点 VO")
|
||||
public class ProductCategoryTreeNodeVO {
|
||||
|
||||
|
||||
@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/images/common/wechat_mp_2017_07_31_bak.jpg")
|
||||
private String picUrl;
|
||||
@ApiModelProperty(value = "排序值", required = true, example = "10")
|
||||
private Integer sort;
|
||||
@ApiModelProperty(value = "状态", required = true, notes = "1-开启;2-关闭", example = "1")
|
||||
private Integer status;
|
||||
@ApiModelProperty(value = "创建时间", required = true, example = "时间戳")
|
||||
private Date createTime;
|
||||
@ApiModelProperty(value = "子节点数组")
|
||||
private List<ProductCategoryTreeNodeVO> children;
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public ProductCategoryTreeNodeVO setId(Integer id) {
|
||||
this.id = id;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Integer getPid() {
|
||||
return pid;
|
||||
}
|
||||
|
||||
public ProductCategoryTreeNodeVO setPid(Integer pid) {
|
||||
this.pid = pid;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public ProductCategoryTreeNodeVO setName(String name) {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public ProductCategoryTreeNodeVO setDescription(String description) {
|
||||
this.description = description;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getPicUrl() {
|
||||
return picUrl;
|
||||
}
|
||||
|
||||
public ProductCategoryTreeNodeVO setPicUrl(String picUrl) {
|
||||
this.picUrl = picUrl;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Integer getSort() {
|
||||
return sort;
|
||||
}
|
||||
|
||||
public ProductCategoryTreeNodeVO setSort(Integer sort) {
|
||||
this.sort = sort;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Integer getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public ProductCategoryTreeNodeVO setStatus(Integer status) {
|
||||
this.status = status;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Date getCreateTime() {
|
||||
return createTime;
|
||||
}
|
||||
|
||||
public ProductCategoryTreeNodeVO setCreateTime(Date createTime) {
|
||||
this.createTime = createTime;
|
||||
return this;
|
||||
}
|
||||
|
||||
public List<ProductCategoryTreeNodeVO> getChildren() {
|
||||
return children;
|
||||
}
|
||||
|
||||
public ProductCategoryTreeNodeVO setChildren(List<ProductCategoryTreeNodeVO> children) {
|
||||
this.children = children;
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,4 +1,100 @@
|
||||
package cn.iocoder.mall.product.application.vo;
|
||||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
@ApiModel("产品分类 VO")
|
||||
public class ProductCategoryVO {
|
||||
}
|
||||
|
||||
@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/images/common/wechat_mp_2017_07_31_bak.jpg")
|
||||
private String picUrl;
|
||||
@ApiModelProperty(value = "排序值", required = true, example = "10")
|
||||
private Integer sort;
|
||||
@ApiModelProperty(value = "状态", required = true, notes = "1-开启;2-关闭", example = "1")
|
||||
private Integer status;
|
||||
@ApiModelProperty(value = "创建时间", required = true, example = "时间戳")
|
||||
private Date createTime;
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public ProductCategoryVO setId(Integer id) {
|
||||
this.id = id;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Integer getPid() {
|
||||
return pid;
|
||||
}
|
||||
|
||||
public ProductCategoryVO setPid(Integer pid) {
|
||||
this.pid = pid;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public ProductCategoryVO setName(String name) {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public ProductCategoryVO setDescription(String description) {
|
||||
this.description = description;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getPicUrl() {
|
||||
return picUrl;
|
||||
}
|
||||
|
||||
public ProductCategoryVO setPicUrl(String picUrl) {
|
||||
this.picUrl = picUrl;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Integer getSort() {
|
||||
return sort;
|
||||
}
|
||||
|
||||
public ProductCategoryVO setSort(Integer sort) {
|
||||
this.sort = sort;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Integer getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public ProductCategoryVO setStatus(Integer status) {
|
||||
this.status = status;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Date getCreateTime() {
|
||||
return createTime;
|
||||
}
|
||||
|
||||
public ProductCategoryVO setCreateTime(Date createTime) {
|
||||
this.createTime = createTime;
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user