

昨天的任务还是没有完成,课程中的是已经将分类完善了。今天要加班来完成今天的任务了,不能总在那拖着,要不然就跟不上进度了。不出所料因为一直在处理所遇到的问题,所以进度太慢了,再加上今天周五,该给自己放个假了。今天也只是完成了菜品分类管理部分,明天继续加油。
该模块能够对菜品和套餐增加分类。
在前端页面中找到添加菜品这一选项就会弹出弹窗,如图所示:

添加完成后会有以下提示,并且添加的分类会在数据库中保存信息。
当然这个地方出现的报错先不必理会。

数据库中展示如图所示:

和添加菜品类别相似,也是通过单击跳转页面进行添加。

出现以下页面说明添加成功。

数据库中展示

package com.von.ruiji_take_out.controller;
import com.von.ruiji_take_out.common.R;
import com.von.ruiji_take_out.entity.Category;
import com.von.ruiji_take_out.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {
@Autowired
private CategoryService categoryService;
/**
* 处理分类信息的保存请求
* 该方法接收一个Category对象作为请求体,用于创建新的分类信息
*
* @param category 包含分类信息的Category对象,由请求体中获取
* @return 返回一个R<String>对象,表示操作结果的成功与否及提示信息
*/
@PostMapping
public R<String> save(@RequestBody Category category){
// 打印日志,记录接收到的分类信息
log.info("category:{}",category);
// 调用服务层方法,保存分类信息
categoryService.save(category);
// 返回操作成功的信息
return R.success("新增分类成功");
}
}package com.von.ruiji_take_out.entity;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import java.io.Serializable;
import java.time.LocalDateTime;
/**
* 分类
*/
@Data
public class Category implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
//类型 1 菜品分类 2 套餐分类
private Integer type;
//分类名称
private String name;
//顺序
private Integer sort;
//创建时间
@TableField(fill = FieldFill.INSERT)
private LocalDateTime createTime;
//更新时间
@TableField(fill = FieldFill.INSERT_UPDATE)
private LocalDateTime updateTime;
//创建人
@TableField(fill = FieldFill.INSERT)
private Long createUser;
//修改人
@TableField(fill = FieldFill.INSERT_UPDATE)
private Long updateUser;
//是否删除
private Integer isDeleted;
}如果按照课程来写代码会出现这一情况,无法对其进行数据的展示,解决方案也十分简单。

就是将图片所示位置加上注释就可以了。

页面效果展示

/**
* 处理分页查询请求
*
* @param page 当前页码,用于指定从哪一页开始查询
* @param pageSize 每页记录数,用于限制每页显示的数据量
* @return 返回一个封装了分页查询结果的对象
*/
@GetMapping("/page")
public R<Page> page(int page, int pageSize){
// 分页构造器,用于设置分页参数
Page pageInfo = new Page(page,pageSize);
// 条件构造器,用于构建查询条件
LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper();
// 添加排序条件,按照sort字段升序排列
queryWrapper.orderByAsc(Category::getSort);
// 执行查询,传入分页信息和查询条件
categoryService.page(pageInfo,queryWrapper);
// 返回查询结果,表示操作成功
return R.success(pageInfo);
}找到前端页面对应的删除按钮进行单击操作。
删除分类的时候如果该分类中有菜品则无法删除成功。

还是那个问题,id转换的地方应该出了问题,显示的是删除成功,但是没有删除,如果修改id后功能才会正常。还是不知道这个地方怎么改。
求解!!!

/**
* 处理删除分类的请求
*
* @param ids 需要删除的分类ID
* @return 返回一个表示删除结果的响应对象
*/
@DeleteMapping
public R<String> delete(Long ids){
// 记录删除分类的日志
log.info("删除分类,id为:{}",ids);
// 调用服务层方法执行删除操作
categoryService.remove(ids);
// 返回删除成功的响应
return R.success("分类信息删除成功");
}这里是检查是否符合删除的条件。
package com.von.ruiji_take_out.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.von.ruiji_take_out.common.CustomException;
import com.von.ruiji_take_out.entity.Category;
import com.von.ruiji_take_out.entity.Dish;
import com.von.ruiji_take_out.entity.Setmeal;
import com.von.ruiji_take_out.mapper.CategoryMapper;
import com.von.ruiji_take_out.service.CategoryService;
import com.von.ruiji_take_out.service.DishService;
import com.von.ruiji_take_out.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
@Autowired
private DishService dishService;
@Autowired
private SetmealService setmealService;
/**
* 重写remove方法以删除特定的分类
* 在删除分类之前,需要检查该分类是否关联了菜品或套餐
* 如果分类已经关联了菜品或套餐,则不允许删除,以保持数据的完整性
*
* @param id 要删除的分类的ID
*/
@Override
public void remove(Long id) {
// 创建一个Lambda查询包装器以查询菜品
LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
// 添加查询条件,根据id进行查询
dishLambdaQueryWrapper.eq(Dish::getCategoryId, id);
// 计算符合条件的菜品数量
int count = dishService.count(dishLambdaQueryWrapper);
// 查询当前分类是否关联了菜品,如果已经关联,抛出业务异常
if (count > 0) {
throw new CustomException("该分类已关联菜品,不能删除");
}
// 创建一个Lambda查询包装器以查询套餐
LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
// 添加查询条件,根据id进行查询
setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId, id);
// 计算符合条件的套餐数量
int count1 = setmealService.count(setmealLambdaQueryWrapper);
// 查询当前分类是否关联了套餐,如果已经关联,抛出业务异常
if (count1 > 0) {
throw new CustomException("该分类已关联套餐,不能删除");
}
// 类似地,可以添加对套餐的检查
// 正常删除
super.removeById(id);
}
}这个地方也是id问题,只有修改过id之后才可以进行修改操作。

修改成功之后会有以下提示。

/**
* 更新分类信息的接口方法
* 该方法通过PUT请求接收一个Category对象,其中包含要更新的分类信息
* 它记录了更新操作的日志,并调用服务层方法进行实际的更新操作
*
* @param category 包含更新后信息的分类对象,通过请求体传递
* @return 返回一个表示操作结果的响应对象,包含成功消息
*/
@PutMapping
public R<String> update(@RequestBody Category category){
// 记录更新分类信息的日志
log.info("修改分类信息:{}",category);
// 调用服务层方法,根据传入的分类对象更新数据库中的对应记录
categoryService.updateById(category);
// 返回成功响应,包含成功消息
return R.success("修改分类信息成功");
}