苍穹外卖学习笔记(七)

四.删除菜品

业务规则:

  1. 可以一次删除一个菜品,也可以一次删除多个菜品
  2. 起售中的菜品不能删除
  3. 被套餐关联得菜品不能删除
  4. 删除菜品后,关联得口味数据也需要删除掉
    一共需要操作三个表,注意加@Transactional事物注解
  5. Controller
java 复制代码
    /**
     * 删除菜品
     */
    @DeleteMapping
    @ApiOperation("删除菜品")
    public Result delete(@RequestParam List<Long> ids) {//@RequestParam接收请求参数
        log.info("删除菜品:{}", ids);
        dishService.deleteBatch(ids);
        return Result.success();
    }
  1. Service
java 复制代码
 /**
     * 删除菜品
     */
    void deleteBatch(List<Long> ids);
  1. Impl
java 复制代码
/**
     * 删除菜品
     */
    @Override
    @Transactional
    public void deleteBatch(List<Long> ids) {
        // 判断当前菜品是否可以删除-是否存在启售菜品
        List<Dish> dishes = dishMapper.selectBatchIds(ids);
        for (Dish dish : dishes) {
            if (dish.getStatus().equals(StatusConstant.ENABLE)) {
                throw new RuntimeException(MessageConstant.DISH_ON_SALE);
            }
        }
        // 判断当前菜品是否可以删除-是否存在套餐
        List<Long> setmealIds = setmealDishMapper.getSetmealIdsByDishIds(ids);
        if (setmealIds != null && !setmealIds.isEmpty()) {
            throw new RuntimeException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        // 删除菜品
        dishMapper.deleteBatchIds(ids);
        // 删除口味
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(DishFlavor::getDishId, ids);
        dishFlavorMapper.delete(queryWrapper);
    }
  1. mapper
java 复制代码
@Mapper
public interface SetmealDishMapper extends BaseMapper<SetmealDish> {

    @Select.List({
            @Select("SELECT setmeal_id FROM setmeal_dish WHERE dish_id IN (#{ids})")
    })
    List<Long> getSetmealIdsByDishIds(List<Long> ids);
}

五.修改菜品

  1. 根据id查询菜品
  2. 根据类型查询分类(已实现)
  3. 文件上传(已实现)
  4. 修改菜品
    加上@Transactional事务注解
  5. Controller
java 复制代码
    /**
     * 根据id查询菜品
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品")
    public Result<DishVO> getById(@PathVariable Long id) {//@PathVariable接收请求路径中的参数
        log.info("根据id查询菜品:{}", id);
        DishVO dishVO = dishService.getByIdWithFlavor(id);
        return Result.success(dishVO);
    }

    /**
     * 修改菜品
     */
    @PutMapping
    @ApiOperation("修改菜品")
    public Result update(@RequestBody DishDTO dishDTO) {
        log.info("修改菜品:{}", dishDTO);
        dishService.updateWithFlavor(dishDTO);
        return Result.success();
    }
  1. Service
java 复制代码
  /**
     * 根据id查询菜品和口味
     */
    DishVO getByIdWithFlavor(Long id);

    /**
     * 更新菜品和口味
     */
    void updateWithFlavor(DishDTO dishDTO);
  1. Impl
java 复制代码
   /**
     * 根据id查询菜品和口味
     */
    @Override
    @Transactional
    public DishVO getByIdWithFlavor(Long id) {
        // 查询菜品
        Dish dish = dishMapper.selectById(id);
        // 查询口味
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, id);
        List<DishFlavor> flavors = dishFlavorMapper.selectList(queryWrapper);
        // 封装结果
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish, dishVO);
        dishVO.setFlavors(flavors);
        return dishVO;
    }

    /**
     * 更新菜品和口味
     */
    @Override
    @Transactional
    public void updateWithFlavor(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.updateById(dish);
        // 删除原有口味
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, dish.getId());
        dishFlavorMapper.delete(queryWrapper);
        // 插入新口味
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && !flavors.isEmpty()) {
            flavors.forEach(flavor -> flavor.setDishId(dish.getId()));
            // 批量插入
            MybatisBatch<DishFlavor> mybatisBatch = new MybatisBatch<>(sqlSessionFactory, flavors);
            MybatisBatch.Method<DishFlavor> method = new MybatisBatch.Method<>(DishFlavorMapper.class);
            mybatisBatch.execute(method.insert());
        }
    }

六. 状态

  1. Controller
java 复制代码
   /**
     * 修改菜品状态
     */
    @PostMapping("/status/{status}")
    @ApiOperation("修改菜品状态")
    public Result updateStatus(@RequestParam Long id, @PathVariable Integer status) {//RequestParam接收请求参数,PathVariable接收请求路径中的参数
        log.info("修改菜品状态:{}", id);
        dishService.updateStatus(id, status);
        return Result.success();
    }

    /**
     * 根据分类id查询菜品
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<Dish>> listResult(@RequestParam Long categoryId) {
        log.info("根据分类id查询菜品:{}", categoryId);
        List<Dish> list = dishService.listByCategoryId(categoryId);
        return Result.success(list);
    }
  1. Service
java 复制代码
 /**
     * 更新菜品状态
     */
    void updateStatus(Long id, Integer status);

    /**
     * 根据分类id查询菜品
     */
    List<Dish> listByCategoryId(Long categoryId);
  1. Impl
java 复制代码
  /**
     * 更新菜品状态
     */
    @Override
    @Transactional
    public void updateStatus(Long id, Integer status) {
        Dish dish = dishMapper.selectById(id);
        if (dish == null) {
            throw new RuntimeException(MessageConstant.DISH_NOT_FOUND);
        }
        dish.setStatus(status);
        dishMapper.updateById(dish);

        if (Objects.equals(status, StatusConstant.DISABLE)){
            // 如果是停售操作,还需要将包含当前菜品的套餐也停售
            List<Long> ids = new ArrayList<>();
            ids.add(id);
            // 查询包含当前菜品的套餐
            LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.in(SetmealDish::getDishId, ids);
            List<Long> setmealIds = setmealDishMapper.selectList(queryWrapper).stream().map(SetmealDish::getSetmealId).distinct().toList();
            if (!setmealIds.isEmpty()) {
                throw new RuntimeException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
            }
        }
    }

    /**
     * 根据分类id查询菜品
     */
    @Override
    @Transactional
    public List<Dish> listByCategoryId(Long categoryId) {
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Dish::getCategoryId, categoryId);
        List<Dish> dishes = dishMapper.selectList(queryWrapper);
        log.info("根据分类id查询菜品:{}", dishes);
        return dishes;
    }
相关推荐
腻害兔2 小时前
【若依项目-产品经理视角】深度拆解 RuoYi-Vue-Pro 商城模块:从商品管理到交易引擎,50 张表撑起一整套电商系统
java·大数据·vue.js·产品经理·ai编程
码智社2 小时前
AES加密原理详解及Java实现加解密实战
java·开发语言
克里斯蒂亚诺更新3 小时前
ruoyi角色权限的对应关系
笔记
萧瑟余晖3 小时前
JDK 26 新特性详解
java·开发语言
马优晨4 小时前
Freemarker 完整讲解(后端 Java 模板引擎)
java·开发语言·freemarker·freemarker 完整讲解·freemarker模板引擎
a1117765 小时前
2FA 验证码生成器(github登录验证 app)
笔记·学习
我的xiaodoujiao6 小时前
API 接口自动化测试详细图文教程学习系列32--Allure测试报告2
python·学习·测试工具·pytest
维天说7 小时前
CLI-Switch 2026年3月版历史设计:Hook、TTY 隔离与 JSON 状态
java·服务器·json
破碎的南瓜7 小时前
sqli--sql注入过关笔记(1,2,3,4,5,9)
数据库·笔记·sql
Zane19947 小时前
并发 vs 并行:别再傻傻分不清了,一文讲透 Java 并发编程的第一课
java·后端