mall:完善商品分类的后端接口
parent
1976571ae8
commit
6a0f713452
@ -0,0 +1,76 @@
|
|||||||
|
package cn.iocoder.yudao.module.product.controller.admin.category;
|
||||||
|
|
||||||
|
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
|
||||||
|
import cn.iocoder.yudao.module.product.controller.admin.category.vo.ProductCategoryRespVO;
|
||||||
|
import cn.iocoder.yudao.module.product.controller.admin.category.vo.ProductCategoryListReqVO;
|
||||||
|
import cn.iocoder.yudao.module.product.controller.admin.category.vo.ProductCategoryCreateReqVO;
|
||||||
|
import cn.iocoder.yudao.module.product.controller.admin.category.vo.ProductCategoryUpdateReqVO;
|
||||||
|
import cn.iocoder.yudao.module.product.convert.category.CategoryConvert;
|
||||||
|
import cn.iocoder.yudao.module.product.dal.dataobject.category.ProductCategoryDO;
|
||||||
|
import cn.iocoder.yudao.module.product.service.category.ProductCategoryService;
|
||||||
|
import io.swagger.annotations.Api;
|
||||||
|
import io.swagger.annotations.ApiImplicitParam;
|
||||||
|
import io.swagger.annotations.ApiOperation;
|
||||||
|
import org.springframework.security.access.prepost.PreAuthorize;
|
||||||
|
import org.springframework.validation.annotation.Validated;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import javax.validation.Valid;
|
||||||
|
import java.util.Comparator;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
|
||||||
|
|
||||||
|
@Api(tags = "管理后台 - 商品分类")
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/product/category")
|
||||||
|
@Validated
|
||||||
|
public class ProductCategoryController {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private ProductCategoryService categoryService;
|
||||||
|
|
||||||
|
@PostMapping("/create")
|
||||||
|
@ApiOperation("创建商品分类")
|
||||||
|
@PreAuthorize("@ss.hasPermission('product:category:create')")
|
||||||
|
public CommonResult<Long> createProductCategory(@Valid @RequestBody ProductCategoryCreateReqVO createReqVO) {
|
||||||
|
return success(categoryService.createProductCategory(createReqVO));
|
||||||
|
}
|
||||||
|
|
||||||
|
@PutMapping("/update")
|
||||||
|
@ApiOperation("更新商品分类")
|
||||||
|
@PreAuthorize("@ss.hasPermission('product:category:update')")
|
||||||
|
public CommonResult<Boolean> updateProductCategory(@Valid @RequestBody ProductCategoryUpdateReqVO updateReqVO) {
|
||||||
|
categoryService.updateProductCategory(updateReqVO);
|
||||||
|
return success(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeleteMapping("/delete")
|
||||||
|
@ApiOperation("删除商品分类")
|
||||||
|
@ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
|
||||||
|
@PreAuthorize("@ss.hasPermission('product:category:delete')")
|
||||||
|
public CommonResult<Boolean> deleteProductCategory(@RequestParam("id") Long id) {
|
||||||
|
categoryService.deleteProductCategory(id);
|
||||||
|
return success(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/get")
|
||||||
|
@ApiOperation("获得商品分类")
|
||||||
|
@ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
|
||||||
|
@PreAuthorize("@ss.hasPermission('product:category:query')")
|
||||||
|
public CommonResult<ProductCategoryRespVO> getProductCategory(@RequestParam("id") Long id) {
|
||||||
|
ProductCategoryDO category = categoryService.getProductCategory(id);
|
||||||
|
return success(CategoryConvert.INSTANCE.convert(category));
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/list")
|
||||||
|
@ApiOperation("获得商品分类列表")
|
||||||
|
@PreAuthorize("@ss.hasPermission('product:category:query')")
|
||||||
|
public CommonResult<List<ProductCategoryRespVO>> getProductCategoryList(@Valid ProductCategoryListReqVO treeListReqVO) {
|
||||||
|
List<ProductCategoryDO> list = categoryService.getEnableProductCategoryList(treeListReqVO);
|
||||||
|
list.sort(Comparator.comparing(ProductCategoryDO::getSort));
|
||||||
|
return success(CategoryConvert.INSTANCE.convertList(list));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -1,47 +0,0 @@
|
|||||||
package cn.iocoder.yudao.module.product.controller.admin.category.vo;
|
|
||||||
|
|
||||||
import cn.iocoder.yudao.framework.excel.core.annotations.DictFormat;
|
|
||||||
import cn.iocoder.yudao.framework.excel.core.convert.DictConvert;
|
|
||||||
import com.alibaba.excel.annotation.ExcelProperty;
|
|
||||||
import lombok.Data;
|
|
||||||
|
|
||||||
import java.util.Date;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 商品分类 Excel VO
|
|
||||||
*
|
|
||||||
* @author 芋道源码
|
|
||||||
*/
|
|
||||||
@Data
|
|
||||||
public class CategoryExcelVO {
|
|
||||||
|
|
||||||
@ExcelProperty("分类编号")
|
|
||||||
private Long id;
|
|
||||||
|
|
||||||
@ExcelProperty("父分类编号")
|
|
||||||
private Long parentId;
|
|
||||||
|
|
||||||
@ExcelProperty("分类名称")
|
|
||||||
private String name;
|
|
||||||
|
|
||||||
@ExcelProperty("分类图标")
|
|
||||||
private String icon;
|
|
||||||
|
|
||||||
@ExcelProperty("分类图片")
|
|
||||||
private String bannerUrl;
|
|
||||||
|
|
||||||
@ExcelProperty("分类排序")
|
|
||||||
private Integer sort;
|
|
||||||
|
|
||||||
@ExcelProperty("分类描述")
|
|
||||||
private String description;
|
|
||||||
|
|
||||||
@ExcelProperty(value = "开启状态", converter = DictConvert.class)
|
|
||||||
@DictFormat("common_status") // TODO 代码优化:建议设置到对应的 XXXDictTypeConstants 枚举类中
|
|
||||||
private Integer status;
|
|
||||||
|
|
||||||
@ExcelProperty("创建时间")
|
|
||||||
private Date createTime;
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -1,26 +0,0 @@
|
|||||||
package cn.iocoder.yudao.module.product.controller.admin.category.vo;
|
|
||||||
|
|
||||||
import io.swagger.annotations.ApiModel;
|
|
||||||
import io.swagger.annotations.ApiModelProperty;
|
|
||||||
import lombok.Data;
|
|
||||||
import org.springframework.format.annotation.DateTimeFormat;
|
|
||||||
|
|
||||||
import java.util.Date;
|
|
||||||
|
|
||||||
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
|
|
||||||
|
|
||||||
@ApiModel(value = "管理后台 - 商品分类 Excel 导出 Request VO", description = "参数和 CategoryPageReqVO 是一致的")
|
|
||||||
@Data
|
|
||||||
public class CategoryExportReqVO {
|
|
||||||
|
|
||||||
@ApiModelProperty(value = "分类名称", example = "办公文具")
|
|
||||||
private String name;
|
|
||||||
|
|
||||||
@ApiModelProperty(value = "开启状态", example = "0")
|
|
||||||
private Integer status;
|
|
||||||
|
|
||||||
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
|
|
||||||
@ApiModelProperty(value = "创建时间")
|
|
||||||
private Date[] createTime;
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -1,31 +0,0 @@
|
|||||||
package cn.iocoder.yudao.module.product.controller.admin.category.vo;
|
|
||||||
|
|
||||||
import cn.iocoder.yudao.framework.common.pojo.PageParam;
|
|
||||||
import io.swagger.annotations.ApiModel;
|
|
||||||
import io.swagger.annotations.ApiModelProperty;
|
|
||||||
import lombok.Data;
|
|
||||||
import lombok.EqualsAndHashCode;
|
|
||||||
import lombok.ToString;
|
|
||||||
import org.springframework.format.annotation.DateTimeFormat;
|
|
||||||
|
|
||||||
import java.util.Date;
|
|
||||||
|
|
||||||
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
|
|
||||||
|
|
||||||
@ApiModel("管理后台 - 商品分类分页 Request VO")
|
|
||||||
@Data
|
|
||||||
@EqualsAndHashCode(callSuper = true)
|
|
||||||
@ToString(callSuper = true)
|
|
||||||
public class CategoryPageReqVO extends PageParam {
|
|
||||||
|
|
||||||
@ApiModelProperty(value = "分类名称", example = "办公文具")
|
|
||||||
private String name;
|
|
||||||
|
|
||||||
@ApiModelProperty(value = "开启状态", example = "0")
|
|
||||||
private Integer status;
|
|
||||||
|
|
||||||
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
|
|
||||||
@ApiModelProperty(value = "创建时间")
|
|
||||||
private Date[] createTime;
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -1,25 +0,0 @@
|
|||||||
package cn.iocoder.yudao.module.product.controller.admin.category.vo;
|
|
||||||
|
|
||||||
import io.swagger.annotations.ApiModel;
|
|
||||||
import io.swagger.annotations.ApiModelProperty;
|
|
||||||
import lombok.Data;
|
|
||||||
import org.springframework.format.annotation.DateTimeFormat;
|
|
||||||
|
|
||||||
import java.util.Date;
|
|
||||||
|
|
||||||
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
|
|
||||||
|
|
||||||
@Data
|
|
||||||
@ApiModel(value = "管理后台 - 商品分类列表查询 Request VO", description = "参数和 CategoryPageReqVO 是一致的")
|
|
||||||
public class CategoryTreeListReqVO extends CategoryExportReqVO {
|
|
||||||
|
|
||||||
@ApiModelProperty(value = "分类名称", example = "办公文具")
|
|
||||||
private String name;
|
|
||||||
|
|
||||||
@ApiModelProperty(value = "开启状态", example = "0")
|
|
||||||
private Integer status;
|
|
||||||
|
|
||||||
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
|
|
||||||
@ApiModelProperty(value = "创建时间")
|
|
||||||
private Date[] createTime;
|
|
||||||
}
|
|
||||||
@ -1,14 +1,12 @@
|
|||||||
package cn.iocoder.yudao.module.product.controller.admin.category.vo;
|
package cn.iocoder.yudao.module.product.controller.admin.category.vo;
|
||||||
|
|
||||||
import lombok.*;
|
import lombok.*;
|
||||||
import java.util.*;
|
|
||||||
import io.swagger.annotations.*;
|
import io.swagger.annotations.*;
|
||||||
import javax.validation.constraints.*;
|
|
||||||
|
|
||||||
@ApiModel("管理后台 - 商品分类创建 Request VO")
|
@ApiModel("管理后台 - 商品分类创建 Request VO")
|
||||||
@Data
|
@Data
|
||||||
@EqualsAndHashCode(callSuper = true)
|
@EqualsAndHashCode(callSuper = true)
|
||||||
@ToString(callSuper = true)
|
@ToString(callSuper = true)
|
||||||
public class CategoryCreateReqVO extends CategoryBaseVO {
|
public class ProductCategoryCreateReqVO extends ProductCategoryBaseVO {
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -0,0 +1,14 @@
|
|||||||
|
package cn.iocoder.yudao.module.product.controller.admin.category.vo;
|
||||||
|
|
||||||
|
import io.swagger.annotations.ApiModel;
|
||||||
|
import io.swagger.annotations.ApiModelProperty;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@ApiModel(value = "管理后台 - 商品分类列表查询 Request VO")
|
||||||
|
@Data
|
||||||
|
public class ProductCategoryListReqVO {
|
||||||
|
|
||||||
|
@ApiModelProperty(value = "分类名称", example = "办公文具")
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
}
|
||||||
@ -1,40 +0,0 @@
|
|||||||
package cn.iocoder.yudao.module.product.dal.mysql.category;
|
|
||||||
|
|
||||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
|
||||||
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
|
|
||||||
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
|
|
||||||
import cn.iocoder.yudao.module.product.controller.admin.category.vo.CategoryExportReqVO;
|
|
||||||
import cn.iocoder.yudao.module.product.controller.admin.category.vo.CategoryPageReqVO;
|
|
||||||
import cn.iocoder.yudao.module.product.dal.dataobject.category.CategoryDO;
|
|
||||||
import org.apache.ibatis.annotations.Mapper;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 商品分类 Mapper
|
|
||||||
*
|
|
||||||
* @author 芋道源码
|
|
||||||
*/
|
|
||||||
@Mapper
|
|
||||||
public interface CategoryMapper extends BaseMapperX<CategoryDO> {
|
|
||||||
|
|
||||||
default PageResult<CategoryDO> selectPage(CategoryPageReqVO reqVO) {
|
|
||||||
return selectPage(reqVO, new LambdaQueryWrapperX<CategoryDO>()
|
|
||||||
.likeIfPresent(CategoryDO::getName, reqVO.getName())
|
|
||||||
.eqIfPresent(CategoryDO::getStatus, reqVO.getStatus())
|
|
||||||
.betweenIfPresent(CategoryDO::getCreateTime, reqVO.getCreateTime())
|
|
||||||
.orderByDesc(CategoryDO::getId));
|
|
||||||
}
|
|
||||||
|
|
||||||
default List<CategoryDO> selectList(CategoryExportReqVO reqVO) {
|
|
||||||
return selectList(new LambdaQueryWrapperX<CategoryDO>()
|
|
||||||
.likeIfPresent(CategoryDO::getName, reqVO.getName())
|
|
||||||
.eqIfPresent(CategoryDO::getStatus, reqVO.getStatus())
|
|
||||||
.betweenIfPresent(CategoryDO::getCreateTime, reqVO.getCreateTime())
|
|
||||||
.orderByDesc(CategoryDO::getId));
|
|
||||||
}
|
|
||||||
|
|
||||||
default Long selectCountByParentId(Long parentId) {
|
|
||||||
return selectCount(CategoryDO::getParentId, parentId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -0,0 +1,33 @@
|
|||||||
|
package cn.iocoder.yudao.module.product.dal.mysql.category;
|
||||||
|
|
||||||
|
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
|
||||||
|
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
|
||||||
|
import cn.iocoder.yudao.module.product.controller.admin.category.vo.ProductCategoryListReqVO;
|
||||||
|
import cn.iocoder.yudao.module.product.dal.dataobject.category.ProductCategoryDO;
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 商品分类 Mapper
|
||||||
|
*
|
||||||
|
* @author 芋道源码
|
||||||
|
*/
|
||||||
|
@Mapper
|
||||||
|
public interface ProductCategoryMapper extends BaseMapperX<ProductCategoryDO> {
|
||||||
|
|
||||||
|
default List<ProductCategoryDO> selectList(ProductCategoryListReqVO listReqVO) {
|
||||||
|
return selectList(new LambdaQueryWrapperX<ProductCategoryDO>()
|
||||||
|
.likeIfPresent(ProductCategoryDO::getName, listReqVO.getName())
|
||||||
|
.orderByDesc(ProductCategoryDO::getId));
|
||||||
|
}
|
||||||
|
|
||||||
|
default Long selectCountByParentId(Long parentId) {
|
||||||
|
return selectCount(ProductCategoryDO::getParentId, parentId);
|
||||||
|
}
|
||||||
|
|
||||||
|
default List<ProductCategoryDO> selectListByStatus(Integer status) {
|
||||||
|
return selectList(ProductCategoryDO::getStatus, status);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,79 @@
|
|||||||
|
package cn.iocoder.yudao.module.product.service.category;
|
||||||
|
|
||||||
|
import cn.iocoder.yudao.module.product.controller.admin.category.vo.ProductCategoryCreateReqVO;
|
||||||
|
import cn.iocoder.yudao.module.product.controller.admin.category.vo.ProductCategoryListReqVO;
|
||||||
|
import cn.iocoder.yudao.module.product.controller.admin.category.vo.ProductCategoryUpdateReqVO;
|
||||||
|
import cn.iocoder.yudao.module.product.dal.dataobject.category.ProductCategoryDO;
|
||||||
|
|
||||||
|
import javax.validation.Valid;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 商品分类 Service 接口
|
||||||
|
*
|
||||||
|
* @author 芋道源码
|
||||||
|
*/
|
||||||
|
public interface ProductCategoryService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建商品分类
|
||||||
|
*
|
||||||
|
* @param createReqVO 创建信息
|
||||||
|
* @return 编号
|
||||||
|
*/
|
||||||
|
Long createProductCategory(@Valid ProductCategoryCreateReqVO createReqVO);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 更新商品分类
|
||||||
|
*
|
||||||
|
* @param updateReqVO 更新信息
|
||||||
|
*/
|
||||||
|
void updateProductCategory(@Valid ProductCategoryUpdateReqVO updateReqVO);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除商品分类
|
||||||
|
*
|
||||||
|
* @param id 编号
|
||||||
|
*/
|
||||||
|
void deleteProductCategory(Long id);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获得商品分类
|
||||||
|
*
|
||||||
|
* @param id 编号
|
||||||
|
* @return 商品分类
|
||||||
|
*/
|
||||||
|
ProductCategoryDO getProductCategory(Long id);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获得商品分类列表
|
||||||
|
*
|
||||||
|
* @param ids 编号
|
||||||
|
* @return 商品分类列表
|
||||||
|
*/
|
||||||
|
List<ProductCategoryDO> getEnableProductCategoryList(Collection<Long> ids);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获得商品分类列表
|
||||||
|
*
|
||||||
|
* @param listReqVO 查询条件
|
||||||
|
* @return 商品分类列表
|
||||||
|
*/
|
||||||
|
List<ProductCategoryDO> getEnableProductCategoryList(ProductCategoryListReqVO listReqVO);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 验证选择的商品分类是否合法
|
||||||
|
*
|
||||||
|
* @param id 分类编号
|
||||||
|
*/
|
||||||
|
void validateProductCategory(Long id);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获得开启状态的商品分类列表
|
||||||
|
*
|
||||||
|
* @return 商品分类列表
|
||||||
|
*/
|
||||||
|
List<ProductCategoryDO> getEnableProductCategoryList();
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,125 @@
|
|||||||
|
package cn.iocoder.yudao.module.product.service.category;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
|
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
|
||||||
|
import cn.iocoder.yudao.module.product.controller.admin.category.vo.ProductCategoryCreateReqVO;
|
||||||
|
import cn.iocoder.yudao.module.product.controller.admin.category.vo.ProductCategoryListReqVO;
|
||||||
|
import cn.iocoder.yudao.module.product.controller.admin.category.vo.ProductCategoryUpdateReqVO;
|
||||||
|
import cn.iocoder.yudao.module.product.convert.category.CategoryConvert;
|
||||||
|
import cn.iocoder.yudao.module.product.dal.dataobject.category.ProductCategoryDO;
|
||||||
|
import cn.iocoder.yudao.module.product.dal.mysql.category.ProductCategoryMapper;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.validation.annotation.Validated;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
||||||
|
import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 商品分类 Service 实现类
|
||||||
|
*
|
||||||
|
* @author 芋道源码
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
@Validated
|
||||||
|
public class ProductCategoryServiceImpl implements ProductCategoryService {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private ProductCategoryMapper productCategoryMapper;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Long createProductCategory(ProductCategoryCreateReqVO createReqVO) {
|
||||||
|
// 校验父分类存在
|
||||||
|
validateParentProductCategory(createReqVO.getParentId());
|
||||||
|
|
||||||
|
// 插入
|
||||||
|
ProductCategoryDO category = CategoryConvert.INSTANCE.convert(createReqVO);
|
||||||
|
productCategoryMapper.insert(category);
|
||||||
|
// 返回
|
||||||
|
return category.getId();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateProductCategory(ProductCategoryUpdateReqVO updateReqVO) {
|
||||||
|
// 校验分类是否存在
|
||||||
|
validateProductCategoryExists(updateReqVO.getId());
|
||||||
|
// 校验父分类存在
|
||||||
|
validateParentProductCategory(updateReqVO.getParentId());
|
||||||
|
|
||||||
|
// 更新
|
||||||
|
ProductCategoryDO updateObj = CategoryConvert.INSTANCE.convert(updateReqVO);
|
||||||
|
productCategoryMapper.updateById(updateObj);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void deleteProductCategory(Long id) {
|
||||||
|
// 校验分类是否存在
|
||||||
|
validateProductCategoryExists(id);
|
||||||
|
// 校验是否还有子分类
|
||||||
|
if (productCategoryMapper.selectCountByParentId(id) > 0) {
|
||||||
|
throw exception(PRODUCT_CATEGORY_EXISTS_CHILDREN);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 删除
|
||||||
|
productCategoryMapper.deleteById(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void validateParentProductCategory(Long id) {
|
||||||
|
// 如果是根分类,无需验证
|
||||||
|
if (Objects.equals(id, ProductCategoryDO.PARENT_ID_NULL)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// 父分类不存在
|
||||||
|
ProductCategoryDO category = productCategoryMapper.selectById(id);
|
||||||
|
if (category == null) {
|
||||||
|
throw exception(PRODUCT_CATEGORY_PARENT_NOT_EXISTS);
|
||||||
|
}
|
||||||
|
// 父分类不能是二级分类
|
||||||
|
if (Objects.equals(id, ProductCategoryDO.PARENT_ID_NULL)) {
|
||||||
|
throw exception(PRODUCT_CATEGORY_PARENT_NOT_FIRST_LEVEL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void validateProductCategoryExists(Long id) {
|
||||||
|
ProductCategoryDO category = productCategoryMapper.selectById(id);
|
||||||
|
if (category == null) {
|
||||||
|
throw exception(PRODUCT_CATEGORY_NOT_EXISTS);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void validateProductCategory(Long id) {
|
||||||
|
ProductCategoryDO category = productCategoryMapper.selectById(id);
|
||||||
|
if (category == null) {
|
||||||
|
throw exception(PRODUCT_CATEGORY_NOT_EXISTS);
|
||||||
|
}
|
||||||
|
if (ObjectUtil.notEqual(category.getStatus(), CommonStatusEnum.ENABLE.getStatus())) {
|
||||||
|
throw exception(PRODUCT_CATEGORY_DISABLED);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ProductCategoryDO getProductCategory(Long id) {
|
||||||
|
return productCategoryMapper.selectById(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<ProductCategoryDO> getEnableProductCategoryList(Collection<Long> ids) {
|
||||||
|
return productCategoryMapper.selectBatchIds(ids);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<ProductCategoryDO> getEnableProductCategoryList(ProductCategoryListReqVO listReqVO) {
|
||||||
|
return productCategoryMapper.selectList(listReqVO);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<ProductCategoryDO> getEnableProductCategoryList() {
|
||||||
|
return productCategoryMapper.selectListByStatus(CommonStatusEnum.ENABLE.getStatus());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -1,192 +0,0 @@
|
|||||||
package cn.iocoder.yudao.module.product.service.category;
|
|
||||||
|
|
||||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
|
||||||
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
|
|
||||||
import cn.iocoder.yudao.module.product.controller.admin.category.vo.CategoryCreateReqVO;
|
|
||||||
import cn.iocoder.yudao.module.product.controller.admin.category.vo.CategoryExportReqVO;
|
|
||||||
import cn.iocoder.yudao.module.product.controller.admin.category.vo.CategoryPageReqVO;
|
|
||||||
import cn.iocoder.yudao.module.product.controller.admin.category.vo.CategoryUpdateReqVO;
|
|
||||||
import cn.iocoder.yudao.module.product.dal.dataobject.category.CategoryDO;
|
|
||||||
import cn.iocoder.yudao.module.product.dal.mysql.category.CategoryMapper;
|
|
||||||
import org.junit.jupiter.api.Disabled;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
import org.springframework.context.annotation.Import;
|
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId;
|
|
||||||
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
|
|
||||||
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
|
|
||||||
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId;
|
|
||||||
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo;
|
|
||||||
import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.CATEGORY_NOT_EXISTS;
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@link CategoryServiceImpl} 的单元测试类
|
|
||||||
*
|
|
||||||
* @author 芋道源码
|
|
||||||
*/
|
|
||||||
@Import(CategoryServiceImpl.class)
|
|
||||||
public class CategoryServiceImplTest extends BaseDbUnitTest {
|
|
||||||
|
|
||||||
@Resource
|
|
||||||
private CategoryServiceImpl categoryService;
|
|
||||||
|
|
||||||
@Resource
|
|
||||||
private CategoryMapper categoryMapper;
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testCreateCategory_success() {
|
|
||||||
// 准备参数
|
|
||||||
CategoryCreateReqVO reqVO = randomPojo(CategoryCreateReqVO.class);
|
|
||||||
|
|
||||||
// 调用
|
|
||||||
Long categoryId = categoryService.createCategory(reqVO);
|
|
||||||
// 断言
|
|
||||||
assertNotNull(categoryId);
|
|
||||||
// 校验记录的属性是否正确
|
|
||||||
CategoryDO category = categoryMapper.selectById(categoryId);
|
|
||||||
assertPojoEquals(reqVO, category);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testUpdateCategory_success() {
|
|
||||||
// mock 数据
|
|
||||||
CategoryDO dbCategory = randomPojo(CategoryDO.class);
|
|
||||||
categoryMapper.insert(dbCategory);// @Sql: 先插入出一条存在的数据
|
|
||||||
// 准备参数
|
|
||||||
CategoryUpdateReqVO reqVO = randomPojo(CategoryUpdateReqVO.class, o -> {
|
|
||||||
o.setId(dbCategory.getId()); // 设置更新的 ID
|
|
||||||
});
|
|
||||||
|
|
||||||
// 调用
|
|
||||||
categoryService.updateCategory(reqVO);
|
|
||||||
// 校验是否更新正确
|
|
||||||
CategoryDO category = categoryMapper.selectById(reqVO.getId()); // 获取最新的
|
|
||||||
assertPojoEquals(reqVO, category);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testUpdateCategory_notExists() {
|
|
||||||
// 准备参数
|
|
||||||
CategoryUpdateReqVO reqVO = randomPojo(CategoryUpdateReqVO.class);
|
|
||||||
|
|
||||||
// 调用, 并断言异常
|
|
||||||
assertServiceException(() -> categoryService.updateCategory(reqVO), CATEGORY_NOT_EXISTS);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testDeleteCategory_success() {
|
|
||||||
// mock 数据
|
|
||||||
CategoryDO dbCategory = randomPojo(CategoryDO.class);
|
|
||||||
categoryMapper.insert(dbCategory);// @Sql: 先插入出一条存在的数据
|
|
||||||
// 准备参数
|
|
||||||
Long id = dbCategory.getId();
|
|
||||||
|
|
||||||
// 调用
|
|
||||||
categoryService.deleteCategory(id);
|
|
||||||
// 校验数据不存在了
|
|
||||||
assertNull(categoryMapper.selectById(id));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testDeleteCategory_notExists() {
|
|
||||||
// 准备参数
|
|
||||||
Long id = randomLongId();
|
|
||||||
|
|
||||||
// 调用, 并断言异常
|
|
||||||
assertServiceException(() -> categoryService.deleteCategory(id), CATEGORY_NOT_EXISTS);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@Disabled // TODO 请修改 null 为需要的值,然后删除 @Disabled 注解
|
|
||||||
public void testGetCategoryPage() {
|
|
||||||
// mock 数据
|
|
||||||
CategoryDO dbCategory = randomPojo(CategoryDO.class, o -> { // 等会查询到
|
|
||||||
o.setParentId(null);
|
|
||||||
o.setName(null);
|
|
||||||
o.setIcon(null);
|
|
||||||
o.setBannerUrl(null);
|
|
||||||
o.setSort(null);
|
|
||||||
o.setDescription(null);
|
|
||||||
o.setStatus(null);
|
|
||||||
o.setCreateTime(null);
|
|
||||||
});
|
|
||||||
categoryMapper.insert(dbCategory);
|
|
||||||
// 测试 pid 不匹配
|
|
||||||
categoryMapper.insert(cloneIgnoreId(dbCategory, o -> o.setParentId(null)));
|
|
||||||
// 测试 name 不匹配
|
|
||||||
categoryMapper.insert(cloneIgnoreId(dbCategory, o -> o.setName(null)));
|
|
||||||
// 测试 icon 不匹配
|
|
||||||
categoryMapper.insert(cloneIgnoreId(dbCategory, o -> o.setIcon(null)));
|
|
||||||
// 测试 bannerUrl 不匹配
|
|
||||||
categoryMapper.insert(cloneIgnoreId(dbCategory, o -> o.setBannerUrl(null)));
|
|
||||||
// 测试 sort 不匹配
|
|
||||||
categoryMapper.insert(cloneIgnoreId(dbCategory, o -> o.setSort(null)));
|
|
||||||
// 测试 description 不匹配
|
|
||||||
categoryMapper.insert(cloneIgnoreId(dbCategory, o -> o.setDescription(null)));
|
|
||||||
// 测试 status 不匹配
|
|
||||||
categoryMapper.insert(cloneIgnoreId(dbCategory, o -> o.setStatus(null)));
|
|
||||||
// 测试 createTime 不匹配
|
|
||||||
categoryMapper.insert(cloneIgnoreId(dbCategory, o -> o.setCreateTime(null)));
|
|
||||||
// 准备参数
|
|
||||||
CategoryPageReqVO reqVO = new CategoryPageReqVO();
|
|
||||||
reqVO.setName(null);
|
|
||||||
reqVO.setStatus(null);
|
|
||||||
reqVO.setCreateTime(null);
|
|
||||||
|
|
||||||
// 调用
|
|
||||||
PageResult<CategoryDO> pageResult = categoryService.getCategoryPage(reqVO);
|
|
||||||
// 断言
|
|
||||||
assertEquals(1, pageResult.getTotal());
|
|
||||||
assertEquals(1, pageResult.getList().size());
|
|
||||||
assertPojoEquals(dbCategory, pageResult.getList().get(0));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@Disabled // TODO 请修改 null 为需要的值,然后删除 @Disabled 注解
|
|
||||||
public void testGetCategoryList() {
|
|
||||||
// mock 数据
|
|
||||||
CategoryDO dbCategory = randomPojo(CategoryDO.class, o -> { // 等会查询到
|
|
||||||
o.setParentId(null);
|
|
||||||
o.setName(null);
|
|
||||||
o.setIcon(null);
|
|
||||||
o.setBannerUrl(null);
|
|
||||||
o.setSort(null);
|
|
||||||
o.setDescription(null);
|
|
||||||
o.setStatus(null);
|
|
||||||
o.setCreateTime(null);
|
|
||||||
});
|
|
||||||
categoryMapper.insert(dbCategory);
|
|
||||||
// 测试 pid 不匹配
|
|
||||||
categoryMapper.insert(cloneIgnoreId(dbCategory, o -> o.setParentId(null)));
|
|
||||||
// 测试 name 不匹配
|
|
||||||
categoryMapper.insert(cloneIgnoreId(dbCategory, o -> o.setName(null)));
|
|
||||||
// 测试 icon 不匹配
|
|
||||||
categoryMapper.insert(cloneIgnoreId(dbCategory, o -> o.setIcon(null)));
|
|
||||||
// 测试 bannerUrl 不匹配
|
|
||||||
categoryMapper.insert(cloneIgnoreId(dbCategory, o -> o.setBannerUrl(null)));
|
|
||||||
// 测试 sort 不匹配
|
|
||||||
categoryMapper.insert(cloneIgnoreId(dbCategory, o -> o.setSort(null)));
|
|
||||||
// 测试 description 不匹配
|
|
||||||
categoryMapper.insert(cloneIgnoreId(dbCategory, o -> o.setDescription(null)));
|
|
||||||
// 测试 status 不匹配
|
|
||||||
categoryMapper.insert(cloneIgnoreId(dbCategory, o -> o.setStatus(null)));
|
|
||||||
// 测试 createTime 不匹配
|
|
||||||
categoryMapper.insert(cloneIgnoreId(dbCategory, o -> o.setCreateTime(null)));
|
|
||||||
// 准备参数
|
|
||||||
CategoryExportReqVO reqVO = new CategoryExportReqVO();
|
|
||||||
reqVO.setName(null);
|
|
||||||
reqVO.setStatus(null);
|
|
||||||
reqVO.setCreateTime(null);
|
|
||||||
|
|
||||||
// 调用
|
|
||||||
List<CategoryDO> list = categoryService.getCategoryList(reqVO);
|
|
||||||
// 断言
|
|
||||||
assertEquals(1, list.size());
|
|
||||||
assertPojoEquals(dbCategory, list.get(0));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -0,0 +1,126 @@
|
|||||||
|
package cn.iocoder.yudao.module.product.service.category;
|
||||||
|
|
||||||
|
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
|
||||||
|
import cn.iocoder.yudao.module.product.controller.admin.category.vo.ProductCategoryCreateReqVO;
|
||||||
|
import cn.iocoder.yudao.module.product.controller.admin.category.vo.ProductCategoryListReqVO;
|
||||||
|
import cn.iocoder.yudao.module.product.controller.admin.category.vo.ProductCategoryUpdateReqVO;
|
||||||
|
import cn.iocoder.yudao.module.product.dal.dataobject.category.ProductCategoryDO;
|
||||||
|
import cn.iocoder.yudao.module.product.dal.mysql.category.ProductCategoryMapper;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.context.annotation.Import;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId;
|
||||||
|
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
|
||||||
|
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
|
||||||
|
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId;
|
||||||
|
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo;
|
||||||
|
import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.PRODUCT_CATEGORY_NOT_EXISTS;
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@link ProductCategoryServiceImpl} 的单元测试类
|
||||||
|
*
|
||||||
|
* @author 芋道源码
|
||||||
|
*/
|
||||||
|
@Import(ProductCategoryServiceImpl.class)
|
||||||
|
public class ProductCategoryServiceImplTest extends BaseDbUnitTest {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private ProductCategoryServiceImpl productCategoryService;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private ProductCategoryMapper productCategoryMapper;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCreateCategory_success() {
|
||||||
|
// 准备参数
|
||||||
|
ProductCategoryCreateReqVO reqVO = randomPojo(ProductCategoryCreateReqVO.class);
|
||||||
|
// mock 父类
|
||||||
|
ProductCategoryDO parentProductCategory = randomPojo(ProductCategoryDO.class, o -> o.setId(reqVO.getParentId()));
|
||||||
|
productCategoryMapper.insert(parentProductCategory);
|
||||||
|
|
||||||
|
// 调用
|
||||||
|
Long categoryId = productCategoryService.createProductCategory(reqVO);
|
||||||
|
// 断言
|
||||||
|
assertNotNull(categoryId);
|
||||||
|
// 校验记录的属性是否正确
|
||||||
|
ProductCategoryDO category = productCategoryMapper.selectById(categoryId);
|
||||||
|
assertPojoEquals(reqVO, category);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testUpdateCategory_success() {
|
||||||
|
// mock 数据
|
||||||
|
ProductCategoryDO dbCategory = randomPojo(ProductCategoryDO.class);
|
||||||
|
productCategoryMapper.insert(dbCategory);// @Sql: 先插入出一条存在的数据
|
||||||
|
// 准备参数
|
||||||
|
ProductCategoryUpdateReqVO reqVO = randomPojo(ProductCategoryUpdateReqVO.class, o -> {
|
||||||
|
o.setId(dbCategory.getId()); // 设置更新的 ID
|
||||||
|
});
|
||||||
|
// mock 父类
|
||||||
|
ProductCategoryDO parentProductCategory = randomPojo(ProductCategoryDO.class, o -> o.setId(reqVO.getParentId()));
|
||||||
|
productCategoryMapper.insert(parentProductCategory);
|
||||||
|
|
||||||
|
// 调用
|
||||||
|
productCategoryService.updateProductCategory(reqVO);
|
||||||
|
// 校验是否更新正确
|
||||||
|
ProductCategoryDO category = productCategoryMapper.selectById(reqVO.getId()); // 获取最新的
|
||||||
|
assertPojoEquals(reqVO, category);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testUpdateCategory_notExists() {
|
||||||
|
// 准备参数
|
||||||
|
ProductCategoryUpdateReqVO reqVO = randomPojo(ProductCategoryUpdateReqVO.class);
|
||||||
|
|
||||||
|
// 调用, 并断言异常
|
||||||
|
assertServiceException(() -> productCategoryService.updateProductCategory(reqVO), PRODUCT_CATEGORY_NOT_EXISTS);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDeleteCategory_success() {
|
||||||
|
// mock 数据
|
||||||
|
ProductCategoryDO dbCategory = randomPojo(ProductCategoryDO.class);
|
||||||
|
productCategoryMapper.insert(dbCategory);// @Sql: 先插入出一条存在的数据
|
||||||
|
// 准备参数
|
||||||
|
Long id = dbCategory.getId();
|
||||||
|
|
||||||
|
// 调用
|
||||||
|
productCategoryService.deleteProductCategory(id);
|
||||||
|
// 校验数据不存在了
|
||||||
|
assertNull(productCategoryMapper.selectById(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDeleteCategory_notExists() {
|
||||||
|
// 准备参数
|
||||||
|
Long id = randomLongId();
|
||||||
|
|
||||||
|
// 调用, 并断言异常
|
||||||
|
assertServiceException(() -> productCategoryService.deleteProductCategory(id), PRODUCT_CATEGORY_NOT_EXISTS);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetCategoryList() {
|
||||||
|
// mock 数据
|
||||||
|
ProductCategoryDO dbCategory = randomPojo(ProductCategoryDO.class, o -> { // 等会查询到
|
||||||
|
o.setName("奥特曼");
|
||||||
|
});
|
||||||
|
productCategoryMapper.insert(dbCategory);
|
||||||
|
// 测试 name 不匹配
|
||||||
|
productCategoryMapper.insert(cloneIgnoreId(dbCategory, o -> o.setName("奥特块")));
|
||||||
|
// 准备参数
|
||||||
|
ProductCategoryListReqVO reqVO = new ProductCategoryListReqVO();
|
||||||
|
reqVO.setName("特曼");
|
||||||
|
|
||||||
|
// 调用
|
||||||
|
List<ProductCategoryDO> list = productCategoryService.getEnableProductCategoryList(reqVO);
|
||||||
|
// 断言
|
||||||
|
assertEquals(1, list.size());
|
||||||
|
assertPojoEquals(dbCategory, list.get(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,4 @@
|
|||||||
|
<configuration>
|
||||||
|
<!-- 引用 Spring Boot 的 logback 基础配置 -->
|
||||||
|
<include resource="org/springframework/boot/logging/logback/defaults.xml" />
|
||||||
|
</configuration>
|
||||||
Loading…
Reference in New Issue