您的位置:首页 > 娱乐 > 明星 > 网上申报系统入口_辽宁政府招标网公告_惠州百度seo哪家好_seo营销网站

网上申报系统入口_辽宁政府招标网公告_惠州百度seo哪家好_seo营销网站

2024/10/6 22:20:53 来源:https://blog.csdn.net/qq_73340809/article/details/142301880  浏览:    关键词:网上申报系统入口_辽宁政府招标网公告_惠州百度seo哪家好_seo营销网站
网上申报系统入口_辽宁政府招标网公告_惠州百度seo哪家好_seo营销网站

四.删除菜品

业务规则:

  1. 可以一次删除一个菜品,也可以一次删除多个菜品
  2. 起售中的菜品不能删除
  3. 被套餐关联得菜品不能删除
  4. 删除菜品后,关联得口味数据也需要删除掉
    一共需要操作三个表,注意加@Transactional事物注解
  5. Controller
    /*** 删除菜品*/@DeleteMapping@ApiOperation("删除菜品")public Result delete(@RequestParam List<Long> ids) {//@RequestParam接收请求参数log.info("删除菜品:{}", ids);dishService.deleteBatch(ids);return Result.success();}
  1. Service
 /*** 删除菜品*/void deleteBatch(List<Long> ids);
  1. Impl
/*** 删除菜品*/@Override@Transactionalpublic 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
@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
    /*** 根据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
  /*** 根据id查询菜品和口味*/DishVO getByIdWithFlavor(Long id);/*** 更新菜品和口味*/void updateWithFlavor(DishDTO dishDTO);
  1. Impl
   /*** 根据id查询菜品和口味*/@Override@Transactionalpublic 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@Transactionalpublic 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
   /*** 修改菜品状态*/@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
 /*** 更新菜品状态*/void updateStatus(Long id, Integer status);/*** 根据分类id查询菜品*/List<Dish> listByCategoryId(Long categoryId);
  1. Impl
  /*** 更新菜品状态*/@Override@Transactionalpublic 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@Transactionalpublic 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;}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com