瑞吉外卖 —— 4、菜品分类管理

目录

1、公共字段自动填充 

1.1、分析

1.2、代码

1.2.1、设置自动填充字段

1.2.2、BaseContext 工具类

1.2.3、在登录过滤器 LoginCheckFilter 中将登录的员工 id 存储到 BaseContext

1.2.4、元数据对象处理器

2、新增分类

2.1、分析

2.1.1、需求分析 

2.1.2、数据模型

2.1.3、执行流程

2.2、代码

3、分类信息分页查询

3.1、分析

3.1.1、需求分析

3.1.2、执行流程

3.2、代码

4、删除分类

4.1、分析

4.1.1、需求分析

4.1.2、执行流程

4.2、代码

4.3、功能完善

4.3.1、自定义异常类 CustomException

4.3.2、为 CustomException 异常设置处理方法

4.3.3、在 CategoryServiceImpl 中添加根据 id 删除分类的方法

4.3.4、修改 CategoryController 中的删除分类方法

5、修改分类

5.1、需求分析


1、公共字段自动填充 

1.1、分析

1.2、代码

1.2.1、设置自动填充字段

在 Employee 类的对应字段加上 @TableField 注解,并设置 fill 属性值

    @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;

1.2.2、BaseContext 工具类

为了动态的获取员工的 id,这里使用 threadLocal 这个局部变量来获取和存储员工 id 

package com.itheima.reggie.common;

/**
 * @Author zhang
 * @Date 2022/9/2 - 14:43
 * @Version 1.0
 */
// 基于ThreadLocal封装工具类,用户保存和获取当前登录用户id
public class BaseContext {

    private static ThreadLocal<Long> threadLocal = new ThreadLocal<>();

    public static void setCurrentId(Long id){
        threadLocal.set(id);
    }

    public static Long getCurrentId(){
        return threadLocal.get();
    }

}

1.2.3、在登录过滤器 LoginCheckFilter 中将登录的员工 id 存储到 BaseContext

 在登录检查到已登陆后将登录的员工 id 存储到自定义的 BaseContext 工具类

        // 已登陆,放行
        if(request.getSession().getAttribute("employee") != null){
            Long id = Thread.currentThread().getId();
            log.info("线程id为{}", id);
            //log.info("用户已登录,放行,用户id为{}", request.getSession().getAttribute("employee"));
            Long employeeId = (Long) request.getSession().getAttribute("employee");
            BaseContext.setCurrentId(employeeId);

            filterChain.doFilter(request, response);
            return;
        }

1.2.4、元数据对象处理器

package com.itheima.reggie.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @Author zhang
 * @Date 2022/9/2 - 10:54
 * @Version 1.0
 */
// 自定义元数据对象处理器
@Component
@Slf4j
public class MyMetaObjecthandler implements MetaObjectHandler {

    /**
     * 插入操作自动填充
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("公共字段自动填充[insert]...");
        log.info(metaObject.toString());
        metaObject.setValue("createTime", LocalDateTime.now());
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("createUser", BaseContext.getCurrentId());
        metaObject.setValue("updateUser", BaseContext.getCurrentId());
    }

    /**
     * 插入操作自动填充
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("公共字段自动填充[update]...");
        log.info(metaObject.toString());
        Long id = Thread.currentThread().getId();
        log.info("线程id为{}", id);
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("updateUser", BaseContext.getCurrentId());
    }

}

2、新增分类

2.1、分析

2.1.1、需求分析 

2.1.2、数据模型

新增分类,其实就是将我们新增窗口录入的分类数据插入到 category 表,表结构如下:

从资料去复制实体 Category 类到 entity 包,然后创建对应的 mapper、service及其实现类、controller

2.1.3、执行流程

2.2、代码

package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.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;

/**
 * @Author zhang
 * @Date 2022/9/2 - 15:13
 * @Version 1.0
 */
@RestController
@Slf4j
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 新增分类
     * @param category
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody Category category){
        log.info("category:{}", category.toString());
        categoryService.save(category);
        return R.success("新增分类成功");
    }

}

3、分类信息分页查询

3.1、分析

3.1.1、需求分析

3.1.2、执行流程

3.2、代码

    /**
     * 分类信息分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(
            int page,
            int pageSize
    ){
        Page<Category> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Category::getSort);
        categoryService.page(pageInfo);
        return R.success(pageInfo);
    }

4、删除分类

4.1、分析

4.1.1、需求分析

4.1.2、执行流程

4.2、代码

    /**
     * 根据id删除对应的分类
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(Long ids){
        log.info("删除id为{}的分类", ids);
        categoryService.removeById(ids);
        return R.success("分类信息删除成功");
    }

4.3、功能完善

4.3.1、自定义异常类 CustomException

自定义异常类 CustomException 用于删除当前分类时,若该分类关联了菜品或套餐,则不能删除,抛出该异常。

public class CustomException extends RuntimeException{

    public CustomException(String message){
        super(message);
    }

}

4.3.2、为 CustomException 异常设置处理方法

在 GlobalExceptionHandle 全局异常处理类中添加捕获 CustomException 异常后的处理方法

    /**
     * 异常处理方法:删除分类异常
     * @return
     */
    @ExceptionHandler(CustomException.class)
    public R<String> exceptionHandler(CustomException exception){
        log.error(exception.getMessage());
        return R.error(exception.getMessage());
    }

4.3.3、在 CategoryServiceImpl 中添加根据 id 删除分类的方法

package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishSerivce;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author zhang
 * @Date 2022/9/2 - 15:11
 * @Version 1.0
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private DishSerivce dishSerivce;

    @Autowired
    private SetmealService setmealService;

    /**
     * 根据id删除分类信息,删除之前需要判断是否关联菜品或套餐
     * @param id
     */
    @Override
    public void remove(Long id) {
        // 查询当前分类是否关联了菜品,如果已关联,抛出一个业务异常
        LambdaQueryWrapper<Dish> dishQueryWrapper = new LambdaQueryWrapper();
        dishQueryWrapper.eq(Dish::getCategoryId, id);
        int dishCount = dishSerivce.count(dishQueryWrapper);
        if(dishCount > 0){
            throw new CustomException("当前分类下关联了菜品,删除失败");
        }

        // 查询当前分类是否关联了套餐,如果已关联,抛出一个业务异常
        LambdaQueryWrapper<Setmeal> setmealQueryWrapper = new LambdaQueryWrapper();
        setmealQueryWrapper.eq(Setmeal::getCategoryId, id);
        int setmealCount = setmealService.count(setmealQueryWrapper);
        if(setmealCount > 0){
            throw new CustomException("当前分类下关联了套餐,删除失败");
        }

        // 正常删除分类
        super.removeById(id);
    }

}

4.3.4、修改 CategoryController 中的删除分类方法

    /**
     * 根据id删除对应的分类
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(Long ids){
        log.info("删除id为{}的分类", ids);
        //categoryService.removeById(ids);
        categoryService.remove(ids);
        return R.success("分类信息删除成功");
    }

5、修改分类

5.1、需求分析

接口:

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值