苍穹外卖学习笔记(七)

四.删除菜品

业务规则:

  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;
    }
相关推荐
余生的观澜18 分钟前
4.铝箔缺陷检测项目复盘
笔记
无名指的等待71219 分钟前
SpringBoot中使用ElasticSearch
java·spring boot·后端
jason_renyu39 分钟前
CICD简单描述笔记
笔记·cicd·cicd部署
Tatakai2539 分钟前
Mybatis Plus分页查询返回total为0问题
java·spring·bug·mybatis
武子康42 分钟前
大数据-133 - ClickHouse 基础概述 全面了解
java·大数据·分布式·clickhouse·flink·spark
晓幂1 小时前
CTFShow-信息搜集
笔记·学习
.生产的驴1 小时前
SpringBoot 消息队列RabbitMQ 消费者确认机制 失败重试机制
java·spring boot·分布式·后端·rabbitmq·java-rabbitmq
cyr___1 小时前
Unity教程(十六)敌人攻击状态的实现
学习·游戏·unity·游戏引擎
Code哈哈笑1 小时前
【C++ 学习】多态的基础和原理(10)
java·c++·学习
chushiyunen1 小时前
redisController工具类
java