苍穹外卖开发日志Day03——分类管理

  • 分类管理包含以下功能:
    • 新增分类
    • 分类分页查询
    • 根据id删除分类
    • 修改分类
    • 启用禁用分类
    • 根据类型查询分类

2025.06.05

一、新增分类

1.1 新增分类——功能开发

  • 接口设计

1.2 新增分类——代码实现

1.2.1 CategoryController

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/**
* 分类管理
*/
@RestController
@RequestMapping("/admin/category")
@Slf4j
@Api(tags = "分类管理相关接口")
public class CategoryController {

@Autowired
private CategoryService categoryService;

/**
* 新增分类
*
* @return
*/
@PostMapping
@ApiOperation("新增分类")
public Result save(@RequestBody CategoryDTO categoryDTO) {
log.info("新增分类:{}", categoryDTO);
categoryService.save(categoryDTO);
return Result.success();
}
}

1.2.2 CategoryServiceImpl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
* 分类管理业务层
*/
@Slf4j
@Service
public class CategoryServiceImpl implements CategoryService {

@Autowired
private CategoryMapper categoryMapper;
@Autowired
private SetmealMapper setmealMapper;
@Autowired
private DishMapper dishMapper;

/**
* 新增分类
*
* @param categoryDTO
*/
@Override
public void save(CategoryDTO categoryDTO) {
Category category = new Category();
BeanUtils.copyProperties(categoryDTO, category);

category.setStatus(StatusConstant.ENABLE);
category.setCreateTime(LocalDateTime.now());
category.setUpdateTime(LocalDateTime.now());
category.setCreateUser(BaseContext.getCurrentId());
category.setUpdateUser(BaseContext.getCurrentId());

categoryMapper.insert(category);
}
}

1.2.3 CategoryMapper

1
2
3
4
5
6
7
8
9
10
11
12
@Mapper
public interface CategoryMapper {

/**
* 新增分类
* @param category
*/
@Insert("insert into category (type, name, sort, status, create_time, update_time, create_user, update_user) " +
"VALUES" +
"(#{type},#{name},#{sort},#{status},#{createTime},#{updateTime},#{createUser},#{updateUser})")
void insert(Category category);
}

1.3 新增分类——功能测试

二、分类分页查询

2.1 分类分页查询——功能开发

  • 接口设计

2.2 分类分页查询——代码实现

2.2.1 CategoryController

1
2
3
4
5
6
7
8
9
10
11
12
/**
* 分类分页查询
*
* @return
*/
@GetMapping("/page")
@ApiOperation("分类分页查询")
public Result<PageResult> page(@RequestBody CategoryPageQueryDTO categoryPageQueryDTO) {
log.info("分类分页查询:{}", categoryPageQueryDTO);
PageResult pageResult = categoryService.page(categoryPageQueryDTO);
return Result.success(pageResult);
}

2.2.2 CategoryServiceImpl

1
2
3
4
5
6
7
8
9
10
11
12
/**
* 分类分页查询
*
* @param categoryPageQueryDTO
* @return
*/
@Override
public PageResult page(CategoryPageQueryDTO categoryPageQueryDTO) {
PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());
Page<Category> page = categoryMapper.pageQuery(categoryPageQueryDTO);
return new PageResult(page.getTotal(), page.getResult());
}

2.2.3 CategoryMapper

1
2
3
4
5
6
/**
* 分类分页查询
* @param categoryPageQueryDTO
* @return
*/
Page<Category> pageQuery(CategoryPageQueryDTO categoryPageQueryDTO);

2.2.4 CategoryMapper.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.sky.mapper.CategoryMapper">

<select id="pageQuery" resultType="com.sky.entity.Category">
select * from category
<where>
<if test="name != null and name != ''">
and name like concat('%',#{name},'%')
</if>
<if test="type != null">
and type = #{type}
</if>
</where>
order by sort asc , create_time desc
</select>

</mapper>

2.3 分类分页查询——功能测试

三、根据id删除分类

3.1 根据id删除分类——功能开发

  • 接口设计
  • 注意点:需要注意查询当前分类是否关联了菜品或套餐,如果关联了就抛出业务异常

3.2 根据id删除分类——代码实现

3.2.1 CategoryController

1
2
3
4
5
6
7
8
9
10
11
12
/**
* 根据id删除分类
*
* @return
*/
@DeleteMapping
@ApiOperation("根据id删除分类")
public Result deleteById(Long id) {
log.info("删除分类:{}", id);
categoryService.deleteById(id);
return Result.success();
}

3.2.2 CategoryServiceImpl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
* 根据id删除分类
*
* @param id
*/
@Override
public void deleteById(Long id) {
//查询当前分类是否关联了菜品,如果关联了就抛出业务异常
Integer count = dishMapper.countByCategoryId(id);
if(count > 0){
//当前分类下有菜品,不能删除
throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
}

//查询当前分类是否关联了套餐,如果关联了就抛出业务异常
count = setmealMapper.countByCategoryId(id);
if(count > 0){
//当前分类下有菜品,不能删除
throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
}
categoryMapper.deleteById(id);
}

3.2.3 CategoryMapper

1
2
3
4
5
6
/**
* 根据id删除分类
* @param id
*/
@Delete("delete from category where id = #{id}")
void deleteById(Long id);

3.2.4 DishMapper

1
2
3
4
5
6
7
8
9
10
11
@Mapper
public interface DishMapper {

/**
* 根据分类id查询菜品数量
* @param categoryId
* @return
*/
@Select("select count(id) from dish where category_id = #{categoryId}")
Integer countByCategoryId(Long categoryId);
}

3.2.5 SetmealMapper

1
2
3
4
5
6
7
8
9
10
11
@Mapper
public interface SetmealMapper {

/**
* 根据分类id查询套餐数量
* @param id
* @return
*/
@Select("select count(id) from setmeal where category_id = #{categoryId}")
Integer countByCategoryId(Long id);
}

3.3 根据id删除分类——功能测试

四、修改分类

4.1 修改分类——功能开发

  • 接口设计

4.2 修改分类——代码实现

4.2.1 CategoryController

1
2
3
4
5
6
7
8
9
10
11
12
/**
* 修改分类
*
* @return
*/
@PutMapping
@ApiOperation("修改分类")
public Result update(@RequestBody CategoryDTO categoryDTO) {
log.info("修改分类:{}", categoryDTO);
categoryService.update(categoryDTO);
return Result.success();
}

4.2.2 CategoryServiceImpl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
* 修改分类
*
* @param categoryDTO
*/
@Override
public void update(CategoryDTO categoryDTO) {
Category category = new Category();
BeanUtils.copyProperties(categoryDTO, category);
category.setUpdateTime(LocalDateTime.now());
category.setUpdateUser(BaseContext.getCurrentId());

categoryMapper.update(category);
}

4.2.3 CategoryMapper

1
2
3
4
5
/**
* 修改分类
* @param category
*/
void update(Category category);

4.2.4 CategoryMapper.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<update id="update">
update category
<set>
<if test="type != null">
type = #{type},
</if>
<if test="name != null">
name = #{name},
</if>
<if test="sort != null">
sort = #{sort},
</if>
<if test="status != null">
status = #{status},
</if>
<if test="updateTime != null">
update_time = #{updateTime},
</if>
<if test="updateUser != null">
update_user = #{updateUser}
</if>
</set>
where id = #{id}
</update>

4.3 修改分类——功能测试

五、启用禁用分类

5.1 启用禁用分类——功能开发

  • 接口设计

5.2 启用禁用分类——代码实现

5.2.1 CategoryController

1
2
3
4
5
6
7
8
9
10
11
12
/**
* 启用禁用分类
*
* @return
*/
@PostMapping("/status/{status}")
@ApiOperation("启用禁用分类")
public Result startOrStop(@PathVariable Integer status, Long id) {
log.info("启用禁用分类:{},{}", status,id);
categoryService.startOrStop(status,id);
return Result.success();
}

5.3.2 CategoryServiceImpl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
* 启用禁用分类
*
* @param status
* @param id
*/
@Override
public void startOrStop(Integer status, Long id) {
Category category = Category.builder()
.id(id)
.status(status)
.updateTime(LocalDateTime.now())
.updateUser(BaseContext.getCurrentId())
.build();
categoryMapper.update(category);
}

5.3.3 CategoryMapper

1
2
3
4
5
/**
* 修改分类
* @param category
*/
void update(Category category);

5.3.4 CategoryMapper.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<update id="update">
update category
<set>
<if test="type != null">
type = #{type},
</if>
<if test="name != null">
name = #{name},
</if>
<if test="sort != null">
sort = #{sort},
</if>
<if test="status != null">
status = #{status},
</if>
<if test="updateTime != null">
update_time = #{updateTime},
</if>
<if test="updateUser != null">
update_user = #{updateUser}
</if>
</set>
where id = #{id}
</update>

5.4 启用禁用分类——功能测试

六、根据类型查询分类

6.1 根据类型查询分类——功能开发

  • 接口设计
  • 注意点:需要判断当前分类是否启用

6.2 根据类型查询分类——代码实现

6.2.1 CategoryController

1
2
3
4
5
6
7
8
9
10
11
12
13
/**
* 根据类型查询分类
*
* @param type
* @return
*/
@GetMapping("/list")
@ApiOperation("根据类型查询分类")
public Result<List<Category>> list(Integer type) {
log.info("根据类型查询分类:{}", type);
List<Category> list = categoryService.list(type);
return Result.success(list);
}

6.2.2 CategoryServiceImpl

1
2
3
4
5
6
7
8
9
10
11
/**
* 根据类型查询分类
*
* @param type
* @return
*/
@Override
public List<Category> list(Integer type) {
List<Category> list = categoryMapper.list(type);
return list;
}

6.2.3 CategoryMapper

1
2
3
4
5
6
/**
* 根据类型查询分类
* @param type
* @return
*/
List<Category> list(Integer type);

6.2.4 CategoryMapper.xml

1
2
3
4
5
6
7
8
<select id="list" resultType="com.sky.entity.Category">
select * from category
where status = 1
<if test="type != null">
and type = #{type}
</if>
order by sort asc,create_time desc
</select>

6.3 根据类型查询分类——功能测试