商品分类代码生成

This commit is contained in:
JeromeSoar
2022-04-24 14:42:51 +08:00
parent 09722c0cd6
commit 4e67b6bbcf
26 changed files with 1319 additions and 4 deletions

View File

@@ -0,0 +1,100 @@
package cn.iocoder.yudao.module.product.controller.admin.category;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;
import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;
import cn.iocoder.yudao.module.product.controller.admin.category.vo.*;
import cn.iocoder.yudao.module.product.dal.dataobject.category.CategoryDO;
import cn.iocoder.yudao.module.product.convert.category.CategoryConvert;
import cn.iocoder.yudao.module.product.service.category.CategoryService;
@Api(tags = "管理后台 - 商品分类")
@RestController
@RequestMapping("/product/category")
@Validated
public class CategoryController {
@Resource
private CategoryService categoryService;
@PostMapping("/create")
@ApiOperation("创建商品分类")
@PreAuthorize("@ss.hasPermission('product:category:create')")
public CommonResult<Long> createCategory(@Valid @RequestBody CategoryCreateReqVO createReqVO) {
return success(categoryService.createCategory(createReqVO));
}
@PutMapping("/update")
@ApiOperation("更新商品分类")
@PreAuthorize("@ss.hasPermission('product:category:update')")
public CommonResult<Boolean> updateCategory(@Valid @RequestBody CategoryUpdateReqVO updateReqVO) {
categoryService.updateCategory(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> deleteCategory(@RequestParam("id") Long id) {
categoryService.deleteCategory(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<CategoryRespVO> getCategory(@RequestParam("id") Long id) {
CategoryDO category = categoryService.getCategory(id);
return success(CategoryConvert.INSTANCE.convert(category));
}
@GetMapping("/list")
@ApiOperation("获得商品分类列表")
@ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
@PreAuthorize("@ss.hasPermission('product:category:query')")
public CommonResult<List<CategoryRespVO>> getCategoryList(@RequestParam("ids") Collection<Long> ids) {
List<CategoryDO> list = categoryService.getCategoryList(ids);
return success(CategoryConvert.INSTANCE.convertList(list));
}
@GetMapping("/page")
@ApiOperation("获得商品分类分页")
@PreAuthorize("@ss.hasPermission('product:category:query')")
public CommonResult<PageResult<CategoryRespVO>> getCategoryPage(@Valid CategoryPageReqVO pageVO) {
PageResult<CategoryDO> pageResult = categoryService.getCategoryPage(pageVO);
return success(CategoryConvert.INSTANCE.convertPage(pageResult));
}
@GetMapping("/export-excel")
@ApiOperation("导出商品分类 Excel")
@PreAuthorize("@ss.hasPermission('product:category:export')")
@OperateLog(type = EXPORT)
public void exportCategoryExcel(@Valid CategoryExportReqVO exportReqVO,
HttpServletResponse response) throws IOException {
List<CategoryDO> list = categoryService.getCategoryList(exportReqVO);
// 导出 Excel
List<CategoryExcelVO> datas = CategoryConvert.INSTANCE.convertList02(list);
ExcelUtils.write(response, "商品分类.xls", "数据", CategoryExcelVO.class, datas);
}
}

View File

@@ -0,0 +1,42 @@
package cn.iocoder.yudao.module.product.controller.admin.category.vo;
import lombok.*;
import java.util.*;
import io.swagger.annotations.*;
import javax.validation.constraints.*;
/**
* 商品分类 Base VO提供给添加、修改、详细的子 VO 使用
* 如果子 VO 存在差异的字段,请不要添加到这里,影响 Swagger 文档生成
*/
@Data
public class CategoryBaseVO {
@ApiModelProperty(value = "父分类编号", required = true, example = "1")
@NotNull(message = "父分类编号不能为空")
private Long pid;
@ApiModelProperty(value = "分类名称", required = true, example = "办公文具")
@NotNull(message = "分类名称不能为空")
private String name;
@ApiModelProperty(value = "分类图标")
private String icon;
@ApiModelProperty(value = "分类图片", required = true)
@NotNull(message = "分类图片不能为空")
private String bannerUrl;
@ApiModelProperty(value = "分类排序", required = true, example = "1")
@NotNull(message = "分类排序不能为空")
private Integer sort;
@ApiModelProperty(value = "分类描述", required = true, example = "描述")
@NotNull(message = "分类描述不能为空")
private String description;
@ApiModelProperty(value = "开启状态", required = true, example = "0")
@NotNull(message = "开启状态不能为空")
private Integer status;
}

View File

@@ -0,0 +1,14 @@
package cn.iocoder.yudao.module.product.controller.admin.category.vo;
import lombok.*;
import java.util.*;
import io.swagger.annotations.*;
import javax.validation.constraints.*;
@ApiModel("管理后台 - 商品分类创建 Request VO")
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class CategoryCreateReqVO extends CategoryBaseVO {
}

View File

@@ -0,0 +1,48 @@
package cn.iocoder.yudao.module.product.controller.admin.category.vo;
import lombok.*;
import java.util.*;
import io.swagger.annotations.*;
import com.alibaba.excel.annotation.ExcelProperty;
import cn.iocoder.yudao.framework.excel.core.annotations.DictFormat;
import cn.iocoder.yudao.framework.excel.core.convert.DictConvert;
/**
* 商品分类 Excel VO
*
* @author 芋道源码
*/
@Data
public class CategoryExcelVO {
@ExcelProperty("分类编号")
private Long id;
@ExcelProperty("父分类编号")
private Long pid;
@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;
}

View File

@@ -0,0 +1,30 @@
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 beginCreateTime;
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
@ApiModelProperty(value = "结束创建时间")
private Date endCreateTime;
}

View File

@@ -0,0 +1,35 @@
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 beginCreateTime;
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
@ApiModelProperty(value = "结束创建时间")
private Date endCreateTime;
}

View File

@@ -0,0 +1,19 @@
package cn.iocoder.yudao.module.product.controller.admin.category.vo;
import lombok.*;
import java.util.*;
import io.swagger.annotations.*;
@ApiModel("管理后台 - 商品分类 Response VO")
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class CategoryRespVO extends CategoryBaseVO {
@ApiModelProperty(value = "分类编号", required = true, example = "2")
private Long id;
@ApiModelProperty(value = "创建时间", required = true)
private Date createTime;
}

View File

@@ -0,0 +1,18 @@
package cn.iocoder.yudao.module.product.controller.admin.category.vo;
import lombok.*;
import java.util.*;
import io.swagger.annotations.*;
import javax.validation.constraints.*;
@ApiModel("管理后台 - 商品分类更新 Request VO")
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class CategoryUpdateReqVO extends CategoryBaseVO {
@ApiModelProperty(value = "分类编号", required = true, example = "2")
@NotNull(message = "分类编号不能为空")
private Long id;
}

View File

@@ -0,0 +1,34 @@
package cn.iocoder.yudao.module.product.convert.category;
import java.util.*;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
import cn.iocoder.yudao.module.product.controller.admin.category.vo.*;
import cn.iocoder.yudao.module.product.dal.dataobject.category.CategoryDO;
/**
* 商品分类 Convert
*
* @author 芋道源码
*/
@Mapper
public interface CategoryConvert {
CategoryConvert INSTANCE = Mappers.getMapper(CategoryConvert.class);
CategoryDO convert(CategoryCreateReqVO bean);
CategoryDO convert(CategoryUpdateReqVO bean);
CategoryRespVO convert(CategoryDO bean);
List<CategoryRespVO> convertList(List<CategoryDO> list);
PageResult<CategoryRespVO> convertPage(PageResult<CategoryDO> page);
List<CategoryExcelVO> convertList02(List<CategoryDO> list);
}

View File

@@ -0,0 +1,58 @@
package cn.iocoder.yudao.module.product.dal.dataobject.category;
import lombok.*;
import java.util.*;
import com.baomidou.mybatisplus.annotation.*;
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
/**
* 商品分类 DO
*
* @author 芋道源码
*/
@TableName("product_category")
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class CategoryDO extends BaseDO {
/**
* 分类编号
*/
@TableId
private Long id;
/**
* 父分类编号
*/
private Long pid;
/**
* 分类名称
*/
private String name;
/**
* 分类图标
*/
private String icon;
/**
* 分类图片
*/
private String bannerUrl;
/**
* 分类排序
*/
private Integer sort;
/**
* 分类描述
*/
private String description;
/**
* 开启状态
*
* 枚举 {@link TODO common_status 对应的类}
*/
private Integer status;
}

View File

@@ -0,0 +1,37 @@
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.getBeginCreateTime(), reqVO.getEndCreateTime())
.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.getBeginCreateTime(), reqVO.getEndCreateTime())
.orderByDesc(CategoryDO::getId));
}
}

View File

@@ -0,0 +1,7 @@
/**
* TODO
*
* @author JeromeSoar
* @since 2022-04-24
*/
package cn.iocoder.yudao.module.product;

View File

@@ -0,0 +1,70 @@
package cn.iocoder.yudao.module.product.service.category;
import java.util.*;
import javax.validation.*;
import cn.iocoder.yudao.module.product.controller.admin.category.vo.*;
import cn.iocoder.yudao.module.product.dal.dataobject.category.CategoryDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
/**
* 商品分类 Service 接口
*
* @author 芋道源码
*/
public interface CategoryService {
/**
* 创建商品分类
*
* @param createReqVO 创建信息
* @return 编号
*/
Long createCategory(@Valid CategoryCreateReqVO createReqVO);
/**
* 更新商品分类
*
* @param updateReqVO 更新信息
*/
void updateCategory(@Valid CategoryUpdateReqVO updateReqVO);
/**
* 删除商品分类
*
* @param id 编号
*/
void deleteCategory(Long id);
/**
* 获得商品分类
*
* @param id 编号
* @return 商品分类
*/
CategoryDO getCategory(Long id);
/**
* 获得商品分类列表
*
* @param ids 编号
* @return 商品分类列表
*/
List<CategoryDO> getCategoryList(Collection<Long> ids);
/**
* 获得商品分类分页
*
* @param pageReqVO 分页查询
* @return 商品分类分页
*/
PageResult<CategoryDO> getCategoryPage(CategoryPageReqVO pageReqVO);
/**
* 获得商品分类列表, 用于 Excel 导出
*
* @param exportReqVO 查询条件
* @return 商品分类列表
*/
List<CategoryDO> getCategoryList(CategoryExportReqVO exportReqVO);
}

View File

@@ -0,0 +1,82 @@
package cn.iocoder.yudao.module.product.service.category;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import java.util.*;
import cn.iocoder.yudao.module.product.controller.admin.category.vo.*;
import cn.iocoder.yudao.module.product.dal.dataobject.category.CategoryDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.product.convert.category.CategoryConvert;
import cn.iocoder.yudao.module.product.dal.mysql.category.CategoryMapper;
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 CategoryServiceImpl implements CategoryService {
@Resource
private CategoryMapper categoryMapper;
@Override
public Long createCategory(CategoryCreateReqVO createReqVO) {
// 插入
CategoryDO category = CategoryConvert.INSTANCE.convert(createReqVO);
categoryMapper.insert(category);
// 返回
return category.getId();
}
@Override
public void updateCategory(CategoryUpdateReqVO updateReqVO) {
// 校验存在
this.validateCategoryExists(updateReqVO.getId());
// 更新
CategoryDO updateObj = CategoryConvert.INSTANCE.convert(updateReqVO);
categoryMapper.updateById(updateObj);
}
@Override
public void deleteCategory(Long id) {
// 校验存在
this.validateCategoryExists(id);
// 删除
categoryMapper.deleteById(id);
}
private void validateCategoryExists(Long id) {
if (categoryMapper.selectById(id) == null) {
throw exception(CATEGORY_NOT_EXISTS);
}
}
@Override
public CategoryDO getCategory(Long id) {
return categoryMapper.selectById(id);
}
@Override
public List<CategoryDO> getCategoryList(Collection<Long> ids) {
return categoryMapper.selectBatchIds(ids);
}
@Override
public PageResult<CategoryDO> getCategoryPage(CategoryPageReqVO pageReqVO) {
return categoryMapper.selectPage(pageReqVO);
}
@Override
public List<CategoryDO> getCategoryList(CategoryExportReqVO exportReqVO) {
return categoryMapper.selectList(exportReqVO);
}
}

View File

@@ -0,0 +1,194 @@
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.setPid(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.setPid(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.setBeginCreateTime(null);
reqVO.setEndCreateTime(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.setPid(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.setPid(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.setBeginCreateTime(null);
reqVO.setEndCreateTime(null);
// 调用
List<CategoryDO> list = categoryService.getCategoryList(reqVO);
// 断言
assertEquals(1, list.size());
assertPojoEquals(dbCategory, list.get(0));
}
}

View File

@@ -0,0 +1 @@
DELETE FROM "product_category";

View File

@@ -0,0 +1,17 @@
CREATE TABLE IF NOT EXISTS "product_category" (
"id" bigint(20) NOT NULL GENERATED BY DEFAULT AS IDENTITY,
"pid" bigint(20) NOT NULL,
"name" varchar(255) NOT NULL,
"icon" varchar(100),
"banner_url" varchar(255) NOT NULL,
"sort" int(11) NOT NULL,
"description" varchar(1024) NOT NULL,
"status" tinyint(4) NOT NULL,
"creator" varchar(64) DEFAULT '',
"create_time" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updater" varchar(64) DEFAULT '',
"update_time" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
"deleted" bit NOT NULL DEFAULT FALSE,
"tenant_id" bigint(20) NOT NULL,
PRIMARY KEY ("id")
) COMMENT '商品分类';