公共字段自动填充
@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;
@Override
public void insertFill(MetaObject metaObject) {
//填充创建时间
metaObject.setValue("createTime", LocalDateTime.now());
//填充 更新的时间
metaObject.setValue("updateTime", LocalDateTime.now());
//BaseContext工具类获取当前登陆人员信息
//填充创建人信息
metaObject.setValue("createUser", httpServletRequest.getSession().getAttribute("employee"));
//填充更新人信息
metaObject.setValue("updateUser", httpServletRequest.getSession().getAttribute("employee"));
/*
这里有Bug,就是封装好的BaseContext通过ThreadLocal获取不了对象,虽然都是一个线程的
但就是获取不到,所以这里先写死了,后面慢慢再改吧
* */
/*//填充创建人信息
metaObject.setValue("createUser", 1L);
//填充更新人信息
metaObject.setValue("updateUser", 1L);*/
}
/**
* @param metaObject 更新时自动填充
*/
@Override
public void updateFill(MetaObject metaObject) {
//因为是更新,所以不用操作创建时间
//更新 更新的时间
metaObject.setValue("updateTime", LocalDateTime.now());
//更新更新人员
/*
这里有Bug,就是封装好的BaseContext通过ThreadLocal获取不了对象,所以这里先写死了,后面慢慢再改吧
metaObject.setValue("updateUser", 1L);
* */
metaObject.setValue("updateUser", httpServletRequest.getSession().getAttribute("employee"));
}
新增分类
@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {
@Autowired
private CategoryService categoryService;
/**
* 新增菜品分类
* @param category 接收菜品分类对象
* @return
*/
@PostMapping("")
public Result<Category> save(@RequestBody Category category){
log.info("新增菜品分类");
categoryService.save(category);
return Result.success(category);
}
如果菜品名字重复,使用全局异常处理器
分类分页查询
@GetMapping("page")
public Result<Page> listCategory(int page,int pageSize){
//分页构造器
Page pageInfo = new Page(page, pageSize);
//过滤条件
LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper();
lambdaQueryWrapper.orderByDesc(Category::getSort);
categoryService.page(pageInfo,lambdaQueryWrapper);
return Result.success(pageInfo);
}
更新和删除分类
只有该分类没有商品时才能删除
@DeleteMapping()
public Result<String> delCategory(Long id){
categoryService.removeCategory(id);
return Result.success("删除成功");
}
/**
* 更新菜品分类
*
* @param category 传回来的菜品分类对象
*/
@PutMapping()
public Result<String> updateCategory(@RequestBody Category category) {
log.info("更新种类{}", category);
categoryService.updateById(category);
return Result.success("菜品种类更新完成");
}
自定义删除函数
@Override
public void removeCategory(Long id) {
//查询当前菜品分类下是否还有菜品,如果有菜品就不能删除,抛出异常打断
LambdaQueryWrapper<Dish> dishLambdaQueryWarpper = new LambdaQueryWrapper();
//看看所有的菜品下有没有目标分类与之关联
dishLambdaQueryWarpper.eq(Dish::getCategoryId, id);
int countInDishById=dishService.count(dishLambdaQueryWarpper);
if (countInDishById>0){
//已经与菜品关联了,抛异常,不许删
throw new CustomerException("已经与菜品关联了,请删除完菜品再来删除");
}
//查询当前菜品分类是否关联了套餐,如果有套餐就不能删除,抛出异常打断
LambdaQueryWrapper<Setmeal> setmealLambdaQueryWarpper = new LambdaQueryWrapper();
setmealLambdaQueryWarpper.eq(Setmeal::getCategoryId, id);
//看看所有的套餐下有没有目标分类与之关联
int countInSetmealById = setmealService.count(setmealLambdaQueryWarpper);
if (countInSetmealById>0){
//已经与套餐关联了,抛异常,不许删
throw new CustomerException("已经与套餐关联了,请删除完套餐再来删除");
}
//上面的测试都通过了,没有任何阻碍了,允许删除,直接调用父接口继承的MP的删除方法就行
super.removeById(id);
}
全局异常处理
@ExceptionHandler({CustomerException.class})
public Result<String> exceptionHandlerCustomer(CustomerException customerException){
log.error(customerException.getMessage());
//直接返回处理信息
return Result.error(customerException.getMessage());
}