套餐管理 新增套餐
代码准备:
代码的交互过程:
步骤一,已经在新增菜品中实现完成。
步骤二:
添加菜品时,所发送的请求:
DishController.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 @GetMapping("/list") public R<List<Dish>> list (Dish dish) { LambdaQueryWrapper<Dish> lambdaQueryWrapper = new LambdaQueryWrapper <>(); lambdaQueryWrapper.eq(dish.getCategoryId()!=null ,Dish::getCategoryId,dish.getCategoryId()); lambdaQueryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime); lambdaQueryWrapper.eq(Dish::getStatus,1 ); List<Dish> list = dishService.list(lambdaQueryWrapper); return R.success(list); }
显示成功:
步骤四和五,已经实现完成,
步骤六:
点击保存时发送的请求为:
在这个保存套餐的过程中,不是普通的保存,所以不能用自带的方法,因为要同时保存两个表:setmeal和setmeal_dish两个表,因此在SermealService中重写一个保存的方法为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 @Service public class SetmealServiceImpl extends ServiceImpl <SetmealMapper, Setmeal> implements SetmealService { @Autowired private SetmealDishService setmealDishService; @Override @Transactional public void saveWithDish (SetmealDto setmealDto) { this .save(setmealDto); List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes(); setmealDishes.stream().map((item)->{ item.setSetmealId(setmealDto.getId()); return item; }).collect(Collectors.toList()); setmealDishService.saveBatch(setmealDishes); } }
SetmealController.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 @RestController @RequestMapping("/setmeal") @Slf4j public class SetmealController { @Autowired private SetmealService setmealService; @Autowired private SetmealDishService setmealDishService; @PostMapping public R<String> save (@RequestBody SetmealDto setmealDto) { setmealService.saveWithDish(setmealDto); return R.success("成功添加套餐" ); } }
套餐信息分页查询 与菜品管理中的分页查询一致
代码为:
SetmealController.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 @GetMapping("/page") public R<Page> page (int page,int pageSize,String name) { Page<Setmeal> pageInfo = new Page <>(); Page<SetmealDto> setmealDtoPage = new Page <>(); LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper <>(); lambdaQueryWrapper.eq(name!=null ,Setmeal::getName,name); lambdaQueryWrapper.orderByDesc(Setmeal::getCreateTime); setmealService.page(pageInfo,lambdaQueryWrapper); BeanUtils.copyProperties(pageInfo,setmealDtoPage,"records" ); List<Setmeal> records = pageInfo.getRecords(); List<SetmealDto> setmealDtoList = records.stream().map((item)->{ SetmealDto setmealDto = new SetmealDto (); BeanUtils.copyProperties(item,setmealDto); Long categoryId = item.getCategoryId(); Category category = categoryService.getById(categoryId); if (category!=null ){ String categoryName = category.getName(); setmealDto.setCategoryName(categoryName); } return setmealDto; }).collect(Collectors.toList()); setmealDtoPage.setRecords(setmealDtoList); return R.success(setmealDtoPage); }
删除套餐 需求分析:
由于删除套餐前要确定商品是否是停售,因此在解决删除功能之前要先解决停售和启售的代码。
停售(批量停售)和启售(批量启售)的代码:
SetmealController.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 @PostMapping("/status/{status}") public R<String> sale (@PathVariable("status") Integer status,@RequestParam List<Long> ids) { for (Long id:ids){ Setmeal setmeal = setmealService.getById(id); setmeal.setStatus(status); setmealService.updateById(setmeal); } return R.success("修改成功" ); }
删除和批量删除的代码:
SetmealServiceimpl.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 @Override @Transactional public void deleteWithDish (List<Long> ids) { LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper <>(); lambdaQueryWrapper.in(Setmeal::getId,ids); lambdaQueryWrapper.eq(Setmeal::getStatus,1 ); int count = this .count(lambdaQueryWrapper); if (count>0 ){ throw new CustomException ("该套餐是启售状态,不能删除" ); }else { this .removeByIds(ids); LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper1 = new LambdaQueryWrapper <>(); lambdaQueryWrapper1.in(SetmealDish::getSetmealId,ids); setmealDishService.remove(lambdaQueryWrapper1); } }
SetmealController.java
1 2 3 4 5 6 7 8 @DeleteMapping public R<String> delete (@RequestParam List<Long> ids) { setmealService.deleteWithDish(ids); return R.success("删除套餐成功" ); }
修改套餐 需求分析 在套餐管理列表页面点击修改按钮,跳转到修改套餐页面,在修改页面回显套餐相关信息并进行修改,最后点击确定按钮完成修改操作
代码开发-梳理交互过程 在开发代码之前,需要梳理一下修改套餐时前端页面( add.html)和服务端的交互过程:
1、页面发送ajax请求,请求服务端获取分类数据,用于套餐分类下拉框中数据展示
2、页面发送ajax请求,请求服务端,根据id查询当前套餐信息,用于套餐信息回显
SetmealController处理
1 2 3 4 5 6 7 @GetMapping("/{id}") public R<SetmealDto> getById (@PathVariable Long id) { SetmealDto setmealDto=setmealService.getByIdWithDish(id); return R.success(setmealDto); }
SetmealServiceImpl添加getByIdWithDish方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 @Override public SetmealDto getByIdWithDish (Long id) { Setmeal setmeal = this .getById(id); SetmealDto setmealDto = new SetmealDto (); BeanUtils.copyProperties(setmeal, setmealDto); LambdaQueryWrapper<SetmealDish> queryWrapper=new LambdaQueryWrapper <>(); queryWrapper.eq(SetmealDish::getSetmealId,setmeal.getId()); List<SetmealDish> list = setmealDishService.list(queryWrapper); setmealDto.setSetmealDishes(list); return setmealDto; }
以上进行了回显:
点击保存:
1 2 3 4 5 6 7 8 @PutMapping public R<String> update (@RequestBody SetmealDto setmealDto) { setmealService.updateWithDish(setmealDto); return R.success("修改成功" ); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 public void updateWithDish (SetmealDto setmealDto) { this .updateById(setmealDto); LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper <>(); lambdaQueryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId()); setmealDishService.remove(lambdaQueryWrapper); List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes(); setmealDishes.stream().map((item)->{ item.setSetmealId(setmealDto.getId()); return item; }).collect(Collectors.toList()); setmealDishService.saveBatch(setmealDishes); }
订单明细 发送的请求
代码实现 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 @GetMapping("/page") public R<Page> page (int page, int pageSize, String orderId, String beginTime, String endTime ) { Page<Orders> pageInfo = new Page (page, pageSize); Page<OrderDto> dtoPage = new Page <>(); LambdaQueryWrapper<Orders> lambdaQueryWrapper = new LambdaQueryWrapper <>(); lambdaQueryWrapper.eq(orderId!=null ,Orders::getNumber,orderId); if (beginTime!=null &&endTime!=null ){ lambdaQueryWrapper.ge(Orders::getOrderTime,beginTime); lambdaQueryWrapper.le(Orders::getOrderTime,endTime); } lambdaQueryWrapper.orderByDesc(Orders::getOrderTime); orderService.page(pageInfo,lambdaQueryWrapper); BeanUtils.copyProperties(pageInfo,dtoPage,"records" ); List<Orders> orders = pageInfo.getRecords(); List<OrderDto> dtoList = orders.stream().map((item)->{ OrderDto orderDto = new OrderDto (); BeanUtils.copyProperties(item,orderDto); String name="用户" +item.getUserId(); orderDto.setUserName(name); return orderDto; }).collect(Collectors.toList()); dtoPage.setRecords(dtoList); return R.success(dtoPage); }