赞
踩
/**
* 根据id查询菜品信息。
*
* @param id
* @return
*/
@GetMapping("/{id}")
public R<DishDto> get(@PathVariable Long id) {
//日志
log.info("DishId:{}", id);
//执行查询。
DishDto dishDto = dishService.getByIdWithFlavor(id);
return R.success(dishDto);
}
业务层代码
@Override
public DishDto getByIdWithFlavor(Long id) {
// 查询dish表中的基本信息。
Dish dish = this.getById(id);
DishDto dishDto = new DishDto();
//对象拷贝
BeanUtils.copyProperties(dish,dishDto);
// 创建条件构造器。
LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(DishFlavor::getDishId, dish.getId());
// 查询dish_flavor表信息。
List<DishFlavor> flavors = dishFlavorService.list(lambdaQueryWrapper);
dishDto.setFlavors(flavors);
return dishDto;
}
/**
* 更新菜品
*
* @param dishDto
* @return
*/
@PutMapping
public R<String> update(@RequestBody DishDto dishDto) {
log.info("更新菜品:{}", dishDto);
//开始更新信息
dishService.updateWithFlavor(dishDto);
return R.success("更新菜品信息成功");
}
业务层代码
@Override
public void updateWithFlavor(DishDto dishDto) {
// 保存dish表中的基本数据。
this.updateById(dishDto);
// 清楚菜品对应的口味信息,在dish_flavor中的删除
LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(DishFlavor::getDishId, dishDto.getId());
dishFlavorService.remove(queryWrapper);
// 插入页面提交的新的菜品的口味信息。
List<DishFlavor> flavors = dishDto.getFlavors();
// 函数式编程,stream流。
flavors =
flavors.stream()
.map(
(item) -> {
//设置dish_id
item.setDishId(dishDto.getId());
return item;
})
.collect(Collectors.toList());
dishFlavorService.saveBatch(flavors);
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。