新增- 商品分类更新,商品分类状态更新,商品分类删除接口

pull/2/MERGE
jiangweifan 2020-05-07 19:07:30 +08:00
parent 20f2ad7416
commit 016600ab2e
11 changed files with 342 additions and 21 deletions

View File

@ -10,9 +10,11 @@ public enum ProductErrorCodeEnum {
// ========== PRODUCT CATEGORY 模块 ==========
PRODUCT_CATEGORY_PARENT_NOT_EXISTS(1003001000, "父分类不存在"),
PRODUCT_CATEGORY_NOT_EXISTS(1003001001, "商品分类不存在"),
PRODUCT_CATEGORY_STATUS_NOT_EXISTS(1003001001, "商品分类状态不存在"),
PRODUCT_CATEGORY_PARENT_NOT_SELF(1003001002, "不能设置自己为父分类"),
PRODUCT_CATEGORY_STATUS_EQUALS(1002001003, "商品分类已经是该状态"),
PRODUCT_CATEGORY_DELETE_ONLY_DISABLE(1002001004, "只有关闭的商品分类才可以删除"),
PRODUCT_CATEGORY_DELETE_ONLY_NO_CHILD(1002001004, "只有无子分类的商品分类才可以删除"),
PRODUCT_CATEGORY_MUST_ENABLE(1002001005, "只有开启的商品分类,才可以使用"),
PRODUCT_CATEGORY_PARENT_CAN_NOT_BE_LEVEL2(1002001005, "父分类必须是一级分类"),

View File

@ -4,6 +4,7 @@ import cn.iocoder.mall.product.biz.bo.category.ProductCategoryAddBO;
import cn.iocoder.mall.product.biz.bo.category.ProductCategoryAllListBO;
import cn.iocoder.mall.product.biz.dataobject.product.ProductCategoryDO;
import cn.iocoder.mall.product.biz.dto.category.ProductCategoryAddDTO;
import cn.iocoder.mall.product.biz.dto.category.ProductCategoryUpdateDTO;
import org.mapstruct.Mapper;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;
@ -24,7 +25,6 @@ public interface ProductCategoryConvert {
* @param category
* @return
*/
@Mappings({})
ProductCategoryAllListBO convertToAllListBO(ProductCategoryDO category);
@ -33,24 +33,27 @@ public interface ProductCategoryConvert {
* @param category
* @return
*/
@Mappings({})
List<ProductCategoryAllListBO> convertToAllListBO(List<ProductCategoryDO> category);
/**
* - DTODO
* - DTODO
* @param productCategoryAddDTO
* @return
*/
@Mappings({})
ProductCategoryDO convertToDO(ProductCategoryAddDTO productCategoryAddDTO);
/**
* - DOBO
* - DOBO
* @param category
* @return
*/
@Mappings({})
ProductCategoryAddBO convertToAddBO(ProductCategoryDO category);
/**
* - DTODO
* @param productCategoryUpdateDTO
* @return
*/
ProductCategoryDO convertToDO(ProductCategoryUpdateDTO productCategoryUpdateDTO);
}

View File

@ -0,0 +1,24 @@
package cn.iocoder.mall.product.biz.dto.category;
import lombok.Data;
import lombok.experimental.Accessors;
/**
* @Author: jiangweifan
* @Date: 2020/5/6
* @Description: - DTO
*/
@Data
@Accessors(chain = true)
public class ProductCategoryDeleteDTO {
/**
* id
*/
private Integer adminId;
/**
*
*/
private Integer id;
}

View File

@ -1,46 +1,44 @@
package cn.iocoder.mall.product.biz.dto.product;
package cn.iocoder.mall.product.biz.dto.category;
import lombok.Data;
import lombok.experimental.Accessors;
import javax.validation.constraints.NotNull;
/**
* DTO
* @Author: jiangweifan
* @Date: 2020/5/6
* @Description: - DTO
*/
@Data
@Accessors(chain = true)
public class ProductCategoryUpdateDTO {
/**
* id
*/
private Integer adminId;
/**
*
*/
@NotNull(message = "编号不能为空")
private Integer id;
/**
*
*/
@NotNull(message = "父分类编号不能为空")
private Integer pid;
/**
*
*/
@NotNull(message = "名称不能为空")
private String name;
/**
*
*/
@NotNull(message = "描述不能为空")
private String description;
/**
*
*/
// @NotNull(message = "分类图片不能为空")
private String picUrl;
/**
*
*/
@NotNull(message = "排序值不能为空")
private Integer sort;
}

View File

@ -0,0 +1,28 @@
package cn.iocoder.mall.product.biz.dto.category;
import lombok.Data;
import lombok.experimental.Accessors;
/**
* @Author: jiangweifan
* @Date: 2020/5/6
* @Description: - DTO
*/
@Data
@Accessors(chain = true)
public class ProductCategoryUpdateStatusDTO {
/**
* id
*/
private Integer adminId;
/**
*
*/
private Integer id;
/**
*
*/
private Integer status;
}

View File

@ -3,6 +3,10 @@ package cn.iocoder.mall.product.biz.service.product;
import cn.iocoder.mall.product.biz.bo.category.ProductCategoryAddBO;
import cn.iocoder.mall.product.biz.bo.category.ProductCategoryAllListBO;
import cn.iocoder.mall.product.biz.dto.category.ProductCategoryAddDTO;
import cn.iocoder.mall.product.biz.dto.category.ProductCategoryDeleteDTO;
import cn.iocoder.mall.product.biz.dto.category.ProductCategoryUpdateDTO;
import cn.iocoder.mall.product.biz.dto.category.ProductCategoryUpdateStatusDTO;
import java.util.List;
@ -25,4 +29,25 @@ public interface ProductCategoryService {
* @return
*/
ProductCategoryAddBO addProductCategory(ProductCategoryAddDTO productCategoryAddDTO);
/**
*
* @param productCategoryUpdateDTO
* @return
*/
Boolean updateProductCategory(ProductCategoryUpdateDTO productCategoryUpdateDTO);
/**
*
* @param productCategoryUpdateStatusDTO
* @return
*/
Boolean updateProductCategoryStatus(ProductCategoryUpdateStatusDTO productCategoryUpdateStatusDTO);
/**
*
* @param productCategoryDeleteDTO
* @return
*/
Boolean deleteProductCategory(ProductCategoryDeleteDTO productCategoryDeleteDTO);
}

View File

@ -8,11 +8,17 @@ import cn.iocoder.mall.product.biz.convert.category.ProductCategoryConvert;
import cn.iocoder.mall.product.biz.dao.product.ProductCategoryMapper;
import cn.iocoder.mall.product.biz.dataobject.product.ProductCategoryDO;
import cn.iocoder.mall.product.biz.dto.category.ProductCategoryAddDTO;
import cn.iocoder.mall.product.biz.dto.category.ProductCategoryDeleteDTO;
import cn.iocoder.mall.product.biz.dto.category.ProductCategoryUpdateDTO;
import cn.iocoder.mall.product.biz.dto.category.ProductCategoryUpdateStatusDTO;
import cn.iocoder.mall.product.biz.enums.ProductErrorCodeEnum;
import cn.iocoder.mall.product.biz.enums.product.ProductCategoryConstants;
import cn.iocoder.mall.product.biz.service.product.ProductCategoryService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.sql.Wrapper;
import java.util.*;
/**
@ -56,6 +62,99 @@ public class ProductCategoryServiceImpl implements ProductCategoryService {
return ProductCategoryConvert.INSTANCE.convertToAddBO(productCategory);
}
/**
*
* @param productCategoryUpdateDTO
* @return
*/
@Override
public Boolean updateProductCategory(ProductCategoryUpdateDTO productCategoryUpdateDTO) {
// 校验当前分类是否存在
if (productCategoryMapper.selectById(productCategoryUpdateDTO.getId()) == null) {
throw ServiceExceptionUtil.exception(ProductErrorCodeEnum.PRODUCT_CATEGORY_NOT_EXISTS.getCode());
}
// 校验父分类
validParent(productCategoryUpdateDTO.getPid());
// 校验不能设置自己为父分类
if (productCategoryUpdateDTO.getId().equals(productCategoryUpdateDTO.getPid())) {
throw ServiceExceptionUtil.exception(ProductErrorCodeEnum.PRODUCT_CATEGORY_PARENT_NOT_SELF.getCode());
}
// 校验父分类是否存在
if (!ProductCategoryConstants.PID_ROOT.equals(productCategoryUpdateDTO.getPid())
&& productCategoryMapper.selectById(productCategoryUpdateDTO.getPid()) == null) {
throw ServiceExceptionUtil.exception(ProductErrorCodeEnum.PRODUCT_CATEGORY_PARENT_NOT_EXISTS.getCode());
}
// 更新到数据库
ProductCategoryDO productCategoryDO = ProductCategoryConvert.INSTANCE.convertToDO(productCategoryUpdateDTO);
productCategoryMapper.updateById(productCategoryDO);
// TODO jiangweifan 操作日志
return true;
}
/**
*
* @param productCategoryUpdateStatusDTO
* @return
*/
@Override
public Boolean updateProductCategoryStatus(ProductCategoryUpdateStatusDTO productCategoryUpdateStatusDTO) {
Integer productCategoryId = productCategoryUpdateStatusDTO.getId();
Integer status = productCategoryUpdateStatusDTO.getStatus();
// 校验商品分类是否存在
ProductCategoryDO productCategoryDO = productCategoryMapper.selectById(productCategoryId);
if (productCategoryDO == null) {
throw ServiceExceptionUtil.exception(ProductErrorCodeEnum.PRODUCT_CATEGORY_NOT_EXISTS.getCode());
}
// 判断更新状态是否存在
if (!ProductCategoryConstants.STATUS_ENABLE.equals(status)
&& !ProductCategoryConstants.STATUS_DISABLE.equals(status)) {
throw ServiceExceptionUtil.exception(ProductErrorCodeEnum.PRODUCT_CATEGORY_STATUS_NOT_EXISTS.getCode());
}
// 如果状态相同,则返回错误
if (productCategoryDO.getStatus().equals(status)) {
throw ServiceExceptionUtil.exception(ProductErrorCodeEnum.PRODUCT_CATEGORY_STATUS_EQUALS.getCode());
}
// 更新商品分类
productCategoryDO.setId(productCategoryId).setStatus(status);
productCategoryMapper.updateById(productCategoryDO);
// TODO jiangweifan 操作日志
return true;
}
/**
*
* @param productCategoryDeleteDTO
* @return
*/
@Override
public Boolean deleteProductCategory(ProductCategoryDeleteDTO productCategoryDeleteDTO) {
Integer productCategoryId = productCategoryDeleteDTO.getId();
// 校验分类是否存在
ProductCategoryDO productCategory = productCategoryMapper.selectById(productCategoryId);
if (productCategory == null) {
throw ServiceExceptionUtil.exception(ProductErrorCodeEnum.PRODUCT_CATEGORY_NOT_EXISTS.getCode());
}
// 只有禁用的商品分类才可以删除
if (ProductCategoryConstants.STATUS_ENABLE.equals(productCategory.getStatus())) {
throw ServiceExceptionUtil.exception(ProductErrorCodeEnum.PRODUCT_CATEGORY_DELETE_ONLY_DISABLE.getCode());
}
// 只有不存在子分类才可以删除
Integer childCount = productCategoryMapper.selectCount(
Wrappers.<ProductCategoryDO>lambdaQuery().eq(ProductCategoryDO::getPid, productCategoryId)
);
if (childCount > 0) {
throw ServiceExceptionUtil.exception(ProductErrorCodeEnum.PRODUCT_CATEGORY_DELETE_ONLY_NO_CHILD.getCode());
}
// TODO jiangweifan 补充只有不存在商品才可以删除
// 标记删除商品分类
ProductCategoryDO updateProductCategory = new ProductCategoryDO()
.setId(productCategoryId);
updateProductCategory.setDeleted(DeletedStatusEnum.DELETED_YES.getValue());
productCategoryMapper.updateById(updateProductCategory);
// TODO jiangweifan 操作日志
return true;
}
/**
*
* @param pid

View File

@ -5,24 +5,28 @@ import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.product.biz.bo.category.ProductCategoryAddBO;
import cn.iocoder.mall.product.biz.bo.category.ProductCategoryAllListBO;
import cn.iocoder.mall.product.biz.dto.category.ProductCategoryAddDTO;
import cn.iocoder.mall.product.biz.dto.category.ProductCategoryDeleteDTO;
import cn.iocoder.mall.product.biz.dto.category.ProductCategoryUpdateDTO;
import cn.iocoder.mall.product.biz.dto.category.ProductCategoryUpdateStatusDTO;
import cn.iocoder.mall.product.biz.enums.product.ProductCategoryConstants;
import cn.iocoder.mall.product.biz.service.product.ProductCategoryService;
import cn.iocoder.mall.product.rest.convert.category.ProductCategoryConvert;
import cn.iocoder.mall.product.rest.request.category.AdminsProductCategoryAddRequest;
import cn.iocoder.mall.product.rest.request.category.AdminsProductCategoryUpdateRequest;
import cn.iocoder.mall.product.rest.request.category.AdminsProductCategoryUpdateStatusRequest;
import cn.iocoder.mall.product.rest.response.category.AdminsProductCategoryAddResponse;
import cn.iocoder.mall.product.rest.response.category.AdminsProductCategoryTreeNodeResponse;
import cn.iocoder.mall.security.core.context.AdminSecurityContextHolder;
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.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import static cn.iocoder.common.framework.vo.CommonResult.success;
/**
@ -75,4 +79,33 @@ public class AdminsProductCategoryController {
return success(ProductCategoryConvert.INSTANCE.convertToAddResponse(addProductCategoryBO));
}
@PostMapping("/update")
@ApiOperation(value = "更新商品分类")
public CommonResult<Boolean> update(@RequestBody AdminsProductCategoryUpdateRequest adminsProductCategoryUpdateRequest) {
// 创建 ProductCategoryUpdateDTO 对象
ProductCategoryUpdateDTO productCategoryUpdateDTO = ProductCategoryConvert.INSTANCE.convertToUpdateDTO(AdminSecurityContextHolder.getContext().getAdminId(), adminsProductCategoryUpdateRequest);
// 更新商品分类
return success(productCategoryService.updateProductCategory(productCategoryUpdateDTO));
}
@PostMapping("/update_status")
@ApiOperation(value = "更新商品分类状态")
public CommonResult<Boolean> updateStatus(@RequestBody AdminsProductCategoryUpdateStatusRequest adminsProductCategoryUpdateStatusRequest) {
// 创建 ProductCategoryUpdateStatusDTO 对象
ProductCategoryUpdateStatusDTO productCategoryUpdateStatusDTO = ProductCategoryConvert.INSTANCE.convertToUpdateStatusDTO(AdminSecurityContextHolder.getContext().getAdminId(),
adminsProductCategoryUpdateStatusRequest);
// 更新商品分类状态
return success(productCategoryService.updateProductCategoryStatus(productCategoryUpdateStatusDTO));
}
@PostMapping("/delete")
@ApiOperation(value = "删除商品分类")
@ApiImplicitParam(name = "id", value = "商品分类编号", required = true, example = "1")
public CommonResult<Boolean> delete(@RequestParam("id") Integer id) {
// 创建 ProductCategoryDeleteDTO 对象
ProductCategoryDeleteDTO productCategoryDeleteDTO = ProductCategoryConvert.INSTANCE.convertToDeleteDTO(AdminSecurityContextHolder.getContext().getAdminId(), id);
// 删除商品分类
return success(productCategoryService.deleteProductCategory(productCategoryDeleteDTO));
}
}

View File

@ -3,7 +3,12 @@ package cn.iocoder.mall.product.rest.convert.category;
import cn.iocoder.mall.product.biz.bo.category.ProductCategoryAddBO;
import cn.iocoder.mall.product.biz.bo.category.ProductCategoryAllListBO;
import cn.iocoder.mall.product.biz.dto.category.ProductCategoryAddDTO;
import cn.iocoder.mall.product.biz.dto.category.ProductCategoryDeleteDTO;
import cn.iocoder.mall.product.biz.dto.category.ProductCategoryUpdateDTO;
import cn.iocoder.mall.product.biz.dto.category.ProductCategoryUpdateStatusDTO;
import cn.iocoder.mall.product.rest.request.category.AdminsProductCategoryAddRequest;
import cn.iocoder.mall.product.rest.request.category.AdminsProductCategoryUpdateRequest;
import cn.iocoder.mall.product.rest.request.category.AdminsProductCategoryUpdateStatusRequest;
import cn.iocoder.mall.product.rest.response.category.AdminsProductCategoryAddResponse;
import cn.iocoder.mall.product.rest.response.category.AdminsProductCategoryTreeNodeResponse;
import org.mapstruct.Mapper;
@ -25,7 +30,6 @@ public interface ProductCategoryConvert {
* @param productCategoryAllListBO
* @return
*/
@Mappings({})
AdminsProductCategoryTreeNodeResponse convertToTreeNodeResponse(ProductCategoryAllListBO productCategoryAllListBO);
@ -34,7 +38,6 @@ public interface ProductCategoryConvert {
* @param adminsProductCategoryAddRequest
* @return
*/
@Mappings({})
ProductCategoryAddDTO convertToAddDTO(Integer adminId, AdminsProductCategoryAddRequest adminsProductCategoryAddRequest);
/**
@ -42,6 +45,27 @@ public interface ProductCategoryConvert {
* @param productCategoryAddBO
* @return
*/
@Mappings({})
AdminsProductCategoryAddResponse convertToAddResponse(ProductCategoryAddBO productCategoryAddBO);
/**
* - RequestDTO
* @param adminsProductCategoryUpdateRequest
* @return
*/
ProductCategoryUpdateDTO convertToUpdateDTO(Integer adminId, AdminsProductCategoryUpdateRequest adminsProductCategoryUpdateRequest);
/**
* - RequestDTO
* @param adminsProductCategoryUpdateStatusRequest
* @return
*/
ProductCategoryUpdateStatusDTO convertToUpdateStatusDTO(Integer adminId, AdminsProductCategoryUpdateStatusRequest adminsProductCategoryUpdateStatusRequest);
/**
* - RequestDTO
* @param adminId id
* @param id id
* @return
*/
ProductCategoryDeleteDTO convertToDeleteDTO(Integer adminId, Integer id);
}

View File

@ -0,0 +1,54 @@
package cn.iocoder.mall.product.rest.request.category;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;
import javax.validation.constraints.NotNull;
/**
* @Author: jiangweifan
* @Date: 2020/5/6
* @Description: - Request
*/
@ApiModel("更新商品分类Request")
@Data
@Accessors(chain = true)
public class AdminsProductCategoryUpdateRequest {
/**
*
*/
@ApiModelProperty(name = "id", value = "分类编号", required = true, example = "1")
@NotNull(message = "编号不能为空")
private Integer id;
/**
*
*/
@ApiModelProperty(name = "pid", value = "父级分类编号", required = true, example = "1")
@NotNull(message = "父分类编号不能为空")
private Integer pid;
/**
*
*/
@ApiModelProperty(name = "name", value = "分类名字(标识)", required = true, example = "admin/info")
@NotNull(message = "名称不能为空")
private String name;
/**
*
*/
@ApiModelProperty(name = "description", value = "描述", required = true, example = "1")
@NotNull(message = "描述不能为空")
private String description;
/**
*
*/
@ApiModelProperty(name = "picUrl", value = "分类图片", example = "http://www.iocoder.cn/images/common/wechat_mp_2017_07_31_bak.jpg/")
private String picUrl;
/**
*
*/
@ApiModelProperty(name = "sort", value = "排序", required = true, example = "1")
@NotNull(message = "排序值不能为空")
private Integer sort;
}

View File

@ -0,0 +1,31 @@
package cn.iocoder.mall.product.rest.request.category;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;
import javax.validation.constraints.NotNull;
/**
* @Author: jiangweifan
* @Date: 2020/5/6
* @Description: - Request
*/
@ApiModel("更新商品分类状态Request")
@Data
@Accessors(chain = true)
public class AdminsProductCategoryUpdateStatusRequest {
/**
*
*/
@ApiModelProperty(name = "id", value = "分类编号", required = true, example = "1")
@NotNull(message = "编号不能为空")
private Integer id;
/**
*
*/
@ApiModelProperty(name = "status", value = "状态。1 - 开启2 - 禁用", required = true, example = "1")
@NotNull(message = "状态不能为空")
private Integer status;
}