From 5192472ed5d5e08b90f0ab057c082e5c42285379 Mon Sep 17 00:00:00 2001 From: YunaiV <> Date: Mon, 4 Mar 2019 19:35:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=95=86=E5=93=81=E5=88=86=E7=B1=BB=E6=8F=90?= =?UTF-8?q?=E4=BA=A4~?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../application/config/MVCConfiguration.java | 4 +- .../AdminsProductCategoryController.java | 118 +++++++++++++++++- .../convert/ProductCategoryConvert.java | 8 +- .../vo/ProductCategoryTreeNodeVO.java | 104 ++++++++++++++- .../application/vo/ProductCategoryVO.java | 98 ++++++++++++++- .../product/api/ProductCategoryService.java | 2 +- .../product/api/bo/ProductCategoryBO.java | 45 +++++++ .../constant/ProductCategoryConstants.java | 2 +- .../api/dto/ProductCategoryAddDTO.java | 2 +- .../api/dto/ProductCategoryUpdateDTO.java | 2 +- .../config/ServiceExceptionConfiguration.java | 26 ++++ .../product/dataobject/ProductCategoryDO.java | 1 + .../service/ProductCategoryServiceImpl.java | 20 ++- .../mapper/ProductCategoryMapper.xml | 4 +- 14 files changed, 416 insertions(+), 20 deletions(-) create mode 100644 product/product-service-impl/src/main/java/cn/iocoder/mall/product/config/ServiceExceptionConfiguration.java diff --git a/admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/config/MVCConfiguration.java b/admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/config/MVCConfiguration.java index 8cfdabb64..fe9bebbf4 100644 --- a/admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/config/MVCConfiguration.java +++ b/admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/config/MVCConfiguration.java @@ -22,8 +22,8 @@ public class MVCConfiguration implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { // registry.addInterceptor(securityInterceptor).addPathPatterns("/user/**", "/admin/**"); // 只拦截我们定义的接口 - registry.addInterceptor(adminSecurityInterceptor).addPathPatterns("/admin/**") - .excludePathPatterns("/admin/passport/login"); // 排除登陆接口 + registry.addInterceptor(adminSecurityInterceptor).addPathPatterns("/admins/**") + .excludePathPatterns("/admins/passport/login"); // 排除登陆接口 } @Override diff --git a/product/product-application/src/main/java/cn/iocoder/mall/product/application/controller/admins/AdminsProductCategoryController.java b/product/product-application/src/main/java/cn/iocoder/mall/product/application/controller/admins/AdminsProductCategoryController.java index c1b3ecf7e..c49f29834 100644 --- a/product/product-application/src/main/java/cn/iocoder/mall/product/application/controller/admins/AdminsProductCategoryController.java +++ b/product/product-application/src/main/java/cn/iocoder/mall/product/application/controller/admins/AdminsProductCategoryController.java @@ -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> tree() { + List productCategories = productCategoryService.getAll().getData(); + // 创建 ProductCategoryTreeNodeVO Map + Map 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 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 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 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 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 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 delete(@RequestParam("id") Integer id) { + return productCategoryService.deleteProductCategory(AdminSecurityContextHolder.getContext().getAdminId(), id); + } } \ No newline at end of file diff --git a/product/product-application/src/main/java/cn/iocoder/mall/product/application/convert/ProductCategoryConvert.java b/product/product-application/src/main/java/cn/iocoder/mall/product/application/convert/ProductCategoryConvert.java index 687715dbe..5c6efb1b8 100644 --- a/product/product-application/src/main/java/cn/iocoder/mall/product/application/convert/ProductCategoryConvert.java +++ b/product/product-application/src/main/java/cn/iocoder/mall/product/application/convert/ProductCategoryConvert.java @@ -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 convertToVO(List categoryList); + ProductCategoryTreeNodeVO convert(ProductCategoryBO category); -} + CommonResult convert(CommonResult result); + +} \ No newline at end of file diff --git a/product/product-application/src/main/java/cn/iocoder/mall/product/application/vo/ProductCategoryTreeNodeVO.java b/product/product-application/src/main/java/cn/iocoder/mall/product/application/vo/ProductCategoryTreeNodeVO.java index 532e0401f..4cbeb167a 100644 --- a/product/product-application/src/main/java/cn/iocoder/mall/product/application/vo/ProductCategoryTreeNodeVO.java +++ b/product/product-application/src/main/java/cn/iocoder/mall/product/application/vo/ProductCategoryTreeNodeVO.java @@ -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 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 getChildren() { + return children; + } + + public ProductCategoryTreeNodeVO setChildren(List children) { + this.children = children; + return this; + } } \ No newline at end of file diff --git a/product/product-application/src/main/java/cn/iocoder/mall/product/application/vo/ProductCategoryVO.java b/product/product-application/src/main/java/cn/iocoder/mall/product/application/vo/ProductCategoryVO.java index 5b5c1c70c..b848a5ec7 100644 --- a/product/product-application/src/main/java/cn/iocoder/mall/product/application/vo/ProductCategoryVO.java +++ b/product/product-application/src/main/java/cn/iocoder/mall/product/application/vo/ProductCategoryVO.java @@ -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; + } + +} \ No newline at end of file diff --git a/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/ProductCategoryService.java b/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/ProductCategoryService.java index 55bd12082..27da26221 100644 --- a/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/ProductCategoryService.java +++ b/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/ProductCategoryService.java @@ -18,7 +18,7 @@ public interface ProductCategoryService { /** * @return 返回所有产品分类们 */ - List getAll(); + CommonResult> getAll(); CommonResult addProductCategory(Integer adminId, ProductCategoryAddDTO productCategoryAddDTO); diff --git a/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/bo/ProductCategoryBO.java b/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/bo/ProductCategoryBO.java index af90b1afe..ae56b4d2d 100644 --- a/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/bo/ProductCategoryBO.java +++ b/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/bo/ProductCategoryBO.java @@ -1,5 +1,7 @@ package cn.iocoder.mall.product.api.bo; +import java.util.Date; + /** * 商品分类 BO */ @@ -19,6 +21,10 @@ public class ProductCategoryBO { * 名称 */ private String name; + /** + * 描述 + */ + private String description; /** * 分类图片 */ @@ -27,6 +33,17 @@ public class ProductCategoryBO { * 排序值 */ private Integer sort; + /** + * 状态 + * + * 1-开启 + * 2-关闭 + */ + private Integer status; + /** + * 创建时间 + */ + private Date createTime; public Integer getId() { return id; @@ -67,4 +84,32 @@ public class ProductCategoryBO { public void setSort(Integer sort) { this.sort = sort; } + + public String getDescription() { + return description; + } + + public ProductCategoryBO setDescription(String description) { + this.description = description; + return this; + } + + public Integer getStatus() { + return status; + } + + public ProductCategoryBO setStatus(Integer status) { + this.status = status; + return this; + } + + public Date getCreateTime() { + return createTime; + } + + public ProductCategoryBO setCreateTime(Date createTime) { + this.createTime = createTime; + return this; + } + } diff --git a/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/constant/ProductCategoryConstants.java b/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/constant/ProductCategoryConstants.java index 1e83e5ad6..ebf6bfb99 100644 --- a/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/constant/ProductCategoryConstants.java +++ b/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/constant/ProductCategoryConstants.java @@ -9,7 +9,7 @@ public class ProductCategoryConstants { /** * 状态 - 关闭 */ - public static final Integer STATUS_DISABLE = 1; + public static final Integer STATUS_DISABLE = 2; /** * 父分类编号 - 根节点 diff --git a/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/dto/ProductCategoryAddDTO.java b/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/dto/ProductCategoryAddDTO.java index 8c677c7b5..4b839944c 100644 --- a/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/dto/ProductCategoryAddDTO.java +++ b/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/dto/ProductCategoryAddDTO.java @@ -25,7 +25,7 @@ public class ProductCategoryAddDTO { /** * 分类图片 */ - @NotNull(message = "分类图片不能为空") +// @NotNull(message = "分类图片不能为空") private String picUrl; /** * 排序值 diff --git a/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/dto/ProductCategoryUpdateDTO.java b/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/dto/ProductCategoryUpdateDTO.java index 611b6e133..ed9737310 100644 --- a/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/dto/ProductCategoryUpdateDTO.java +++ b/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/dto/ProductCategoryUpdateDTO.java @@ -30,7 +30,7 @@ public class ProductCategoryUpdateDTO { /** * 分类图片 */ - @NotNull(message = "分类图片不能为空") +// @NotNull(message = "分类图片不能为空") private String picUrl; /** * 排序值 diff --git a/product/product-service-impl/src/main/java/cn/iocoder/mall/product/config/ServiceExceptionConfiguration.java b/product/product-service-impl/src/main/java/cn/iocoder/mall/product/config/ServiceExceptionConfiguration.java new file mode 100644 index 000000000..e5f1e1ea3 --- /dev/null +++ b/product/product-service-impl/src/main/java/cn/iocoder/mall/product/config/ServiceExceptionConfiguration.java @@ -0,0 +1,26 @@ +package cn.iocoder.mall.product.config; + +import cn.iocoder.common.framework.util.ServiceExceptionUtil; +import cn.iocoder.mall.product.api.constant.ProductErrorCodeEnum; +import org.springframework.boot.context.event.ApplicationReadyEvent; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.event.EventListener; + +@Configuration +public class ServiceExceptionConfiguration { + + @EventListener(ApplicationReadyEvent.class) // 可参考 https://www.cnblogs.com/ssslinppp/p/7607509.html + public void initMessages() { +// 从 service_exception_message.properties 加载错误码的方案 +// Properties properties; +// try { +// properties = PropertiesLoaderUtils.loadAllProperties("classpath:service_exception_message.properties"); +// } catch (IOException e) { +// throw new RuntimeException(e); +// } + for (ProductErrorCodeEnum item : ProductErrorCodeEnum.values()) { + ServiceExceptionUtil.put(item.getCode(), item.getMessage()); + } + } + +} \ No newline at end of file diff --git a/product/product-service-impl/src/main/java/cn/iocoder/mall/product/dataobject/ProductCategoryDO.java b/product/product-service-impl/src/main/java/cn/iocoder/mall/product/dataobject/ProductCategoryDO.java index b68542bd4..1152ab5e2 100644 --- a/product/product-service-impl/src/main/java/cn/iocoder/mall/product/dataobject/ProductCategoryDO.java +++ b/product/product-service-impl/src/main/java/cn/iocoder/mall/product/dataobject/ProductCategoryDO.java @@ -7,6 +7,7 @@ import cn.iocoder.common.framework.dataobject.BaseDO; */ public class ProductCategoryDO extends BaseDO { + @Deprecated public static final Integer STATUS_ENABLE = 1; /** diff --git a/product/product-service-impl/src/main/java/cn/iocoder/mall/product/service/ProductCategoryServiceImpl.java b/product/product-service-impl/src/main/java/cn/iocoder/mall/product/service/ProductCategoryServiceImpl.java index 1a0915e72..cebdae3f0 100644 --- a/product/product-service-impl/src/main/java/cn/iocoder/mall/product/service/ProductCategoryServiceImpl.java +++ b/product/product-service-impl/src/main/java/cn/iocoder/mall/product/service/ProductCategoryServiceImpl.java @@ -1,5 +1,6 @@ package cn.iocoder.mall.product.service; +import cn.iocoder.common.framework.constant.SysErrorCodeEnum; import cn.iocoder.common.framework.dataobject.BaseDO; import cn.iocoder.common.framework.util.ServiceExceptionUtil; import cn.iocoder.common.framework.vo.CommonResult; @@ -32,15 +33,15 @@ public class ProductCategoryServiceImpl implements ProductCategoryService { } @Override - public List getAll() { + public CommonResult> getAll() { List categoryList = productCategoryMapper.selectList(); - return ProductCategoryConvert.INSTANCE.convertToBO(categoryList); + return CommonResult.success(ProductCategoryConvert.INSTANCE.convertToBO(categoryList)); } @Override public CommonResult addProductCategory(Integer adminId, ProductCategoryAddDTO productCategoryAddDTO) { // 校验父分类是否存在 - if (ProductCategoryConstants.PID_ROOT.equals(productCategoryAddDTO.getPid()) + if (!ProductCategoryConstants.PID_ROOT.equals(productCategoryAddDTO.getPid()) && productCategoryMapper.selectById(productCategoryAddDTO.getPid()) == null) { return ServiceExceptionUtil.error(ProductErrorCodeEnum.PRODUCT_CATEGORY_PARENT_NOT_EXISTS.getCode()); } @@ -65,7 +66,7 @@ public class ProductCategoryServiceImpl implements ProductCategoryService { return ServiceExceptionUtil.error(ProductErrorCodeEnum.PRODUCT_CATEGORY_PARENT_NOT_SELF.getCode()); } // 校验父分类是否存在 - if (ProductCategoryConstants.PID_ROOT.equals(productCategoryUpdateDTO.getPid()) + if (!ProductCategoryConstants.PID_ROOT.equals(productCategoryUpdateDTO.getPid()) && productCategoryMapper.selectById(productCategoryUpdateDTO.getPid()) == null) { return ServiceExceptionUtil.error(ProductErrorCodeEnum.PRODUCT_CATEGORY_PARENT_NOT_EXISTS.getCode()); } @@ -78,6 +79,10 @@ public class ProductCategoryServiceImpl implements ProductCategoryService { @Override public CommonResult updateProductCategoryStatus(Integer adminId, Integer productCategoryId, Integer status) { + // 校验参数 + if (!isValidStatus(status)) { + return CommonResult.error(SysErrorCodeEnum.VALIDATION_REQUEST_PARAM_ERROR.getCode(), "变更状态必须是开启(1)或关闭(2)"); // TODO 有点搓 + } // 校验分类是否存在 ProductCategoryDO productCategory = productCategoryMapper.selectById(productCategoryId); if (productCategory == null) { @@ -107,11 +112,16 @@ public class ProductCategoryServiceImpl implements ProductCategoryService { } // TODO 芋艿:考虑下,是否需要判断下该分类下是否有商品 // 标记删除商品分类 - ProductCategoryDO updateProductCategory = new ProductCategoryDO(); + ProductCategoryDO updateProductCategory = new ProductCategoryDO().setId(productCategoryId); updateProductCategory.setDeleted(BaseDO.DELETED_YES); productCategoryMapper.update(updateProductCategory); // TODO 操作日志 return CommonResult.success(true); } + private boolean isValidStatus(Integer status) { + return ProductCategoryConstants.STATUS_ENABLE.equals(status) + || ProductCategoryConstants.STATUS_DISABLE.equals(status); + } + } \ No newline at end of file diff --git a/product/product-service-impl/src/main/resources/mapper/ProductCategoryMapper.xml b/product/product-service-impl/src/main/resources/mapper/ProductCategoryMapper.xml index 7f8023b5d..a67772ca7 100644 --- a/product/product-service-impl/src/main/resources/mapper/ProductCategoryMapper.xml +++ b/product/product-service-impl/src/main/resources/mapper/ProductCategoryMapper.xml @@ -3,7 +3,7 @@ - id, pid, name, descrption, pic_url, + id, pid, name, description, pic_url, sort, status, create_time @@ -43,7 +43,7 @@ - UPDATE resource + UPDATE product_category pid = #{pid},