目录
一、新增套餐
1 需求分析
2 数据模型
3 代码实现
3.1 实体类SetmealDish
3.2 SetmealDto
3.3 SetmealDishMapper
3.4 SetmealDishService
3.5 SetmealDishServiceImpl
3.6 SetmealController
3.7 DishController
3.8 SetmealService
3.9 SetmealServiceImpl
3.10 SetmealController
4 功能测试
二、套餐分页信息查询
2.1 需求分析
2.2 代码实现
2.2.1 SetmealController
2.3 功能测试
三、删除套餐
3.1 需求分析
3.2 代码实现
3.2.1 SetmealService
3.2.2 SetmealServiceImpl
3.2.3 SetmealController
3.3 功能测试
前言:新增套餐、套餐分页查询以及删除套餐操作
一、新增套餐
1 需求分析
2 数据模型
3 代码实现
3.1 实体类SetmealDish
package com.runa.reggie.entity;import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import lombok.Data;
import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;/*** 套餐菜品关系*/
@Data
public class SetmealDish implements Serializable {private static final long serialVersionUID = 1L;private Long id;//套餐idprivate Long setmealId;//菜品idprivate Long dishId;//菜品名称 (冗余字段)private String name;//菜品原价private BigDecimal price;//份数private Integer copies;//排序private Integer sort;@TableField(fill = FieldFill.INSERT)private LocalDateTime createTime;@TableField(fill = FieldFill.INSERT_UPDATE)private LocalDateTime updateTime;@TableField(fill = FieldFill.INSERT)private Long createUser;@TableField(fill = FieldFill.INSERT_UPDATE)private Long updateUser;//是否删除private Integer isDeleted;
}
3.2 SetmealDto
package com.runa.reggie.dto;import com.runa.reggie.entity.Setmeal;
import com.runa.reggie.entity.SetmealDish;
import lombok.Data;
import java.util.List;@Data
public class SetmealDto extends Setmeal {private List<SetmealDish> setmealDishes;private String categoryName;
}
3.3 SetmealDishMapper
package com.runa.reggie.mapper;import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.runa.reggie.entity.SetmealDish;
import org.apache.ibatis.annotations.Mapper;@Mapper
public interface SetmealDishMapper extends BaseMapper<SetmealDish> {
}
3.4 SetmealDishService
package com.runa.reggie.service;import com.baomidou.mybatisplus.extension.service.IService;
import com.runa.reggie.entity.SetmealDish;public interface SetmealDishService extends IService<SetmealDish> {
}
3.5 SetmealDishServiceImpl
package com.runa.reggie.service.impl;import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.runa.reggie.entity.SetmealDish;
import com.runa.reggie.mapper.SetmealDishMapper;
import com.runa.reggie.service.SetmealDishService;
import org.springframework.stereotype.Service;@Service
public class SetmealDishServiceImpl extends ServiceImpl<SetmealDishMapper, SetmealDish> implements SetmealDishService {
}
3.6 SetmealController
package com.runa.reggie.controller;import com.runa.reggie.service.SetmealDishService;
import com.runa.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {@Autowiredprivate SetmealService setmealService;@Autowiredprivate SetmealDishService setmealDishService;
}
3.7 DishController
package com.runa.reggie.controller;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.runa.reggie.common.R;
import com.runa.reggie.dto.DishDto;
import com.runa.reggie.entity.Category;
import com.runa.reggie.entity.Dish;
import com.runa.reggie.service.CategoryService;
import com.runa.reggie.service.DishFlavorService;
import com.runa.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;import java.util.List;
import java.util.stream.Collectors;/*** 菜品管理*/
@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {@Autowiredprivate DishService dishService;@Autowiredprivate DishFlavorService dishFlavorService;@Autowiredprivate CategoryService categoryService;/*** 新增菜品* @param dishDto* @return*/@PostMappingpublic R<String> save(@RequestBody DishDto dishDto){log.info(dishDto.toString());dishService.saveWithFlavor(dishDto);return R.success("新增菜品成功");}@GetMapping("/page")public R<Page> page(int page, int pageSize, String name){// 构造分页构造器对象Page<Dish> pageInfo = new Page<>(page, pageSize);Page<DishDto> dishDtoPage = new Page<>();//条件构造器LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();// 添加过滤条件queryWrapper.like(name != null,Dish::getName,name);// 添加排序条件queryWrapper.orderByDesc(Dish::getUpdateTime);//执行分页查询dishService.page(pageInfo,queryWrapper);// 对象拷贝BeanUtils.copyProperties(pageInfo, dishDtoPage,"records");List<Dish> records = pageInfo.getRecords();List<DishDto> list = records.stream().map((item) -> {DishDto dishDto = new DishDto();BeanUtils.copyProperties(item,dishDto);Long categoryId = item.getCategoryId();// 分类idCategory category = categoryService.getById(categoryId);String categoryName = category.getName();dishDto.setCategoryName(categoryName);return dishDto;}).collect(Collectors.toList());dishDtoPage.setRecords(list);return R.success(dishDtoPage);}/*** 根据ID 查询菜品和对应口味信息 回显* @param id* @return*/@GetMapping("/{id}")public R<DishDto> get(@PathVariable Long id){log.info("要查询显示的菜品信息是:{}",id);DishDto dishDto = dishService.getByIdWithFlavor(id);return R.success(dishDto);}/*** 修改菜品* @param dishDto* @return*/@PutMappingpublic R<String> update(@RequestBody DishDto dishDto){log.info(dishDto.toString());dishService.updateWithFlavor(dishDto);return R.success("修改菜品成功");}/*** 根据条件查询对于菜品数据* @param dish* @return*/@GetMapping("/list")public R<List<Dish>> list(Dish dish){// 构造查询条件LambdaQueryWrapper<Dish> queryWrapper =new LambdaQueryWrapper<>();queryWrapper.eq(dish.getCategoryId() != null,Dish::getCategoryId,dish.getCategoryId());// 添加条件,查询状态为1(起售状态)的菜品queryWrapper.eq(Dish::getStatus,1);// 天添加排序条件queryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);List<Dish> list = dishService.list(queryWrapper);return R.success(list);}
}
3.8 SetmealService
package com.runa.reggie.service;import com.baomidou.mybatisplus.extension.service.IService;
import com.runa.reggie.dto.SetmealDto;
import com.runa.reggie.entity.Setmeal;public interface SetmealService extends IService<Setmeal> {/*** 新增套餐,同时保存套餐和菜品的关联关系* @param setmealDto*/public void saveWithDish(SetmealDto setmealDto);
}
3.9 SetmealServiceImpl
package com.runa.reggie.service.impl;import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.runa.reggie.dto.SetmealDto;
import com.runa.reggie.entity.Setmeal;
import com.runa.reggie.entity.SetmealDish;
import com.runa.reggie.mapper.SetmealMapper;
import com.runa.reggie.service.SetmealDishService;
import com.runa.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;import java.util.List;
import java.util.stream.Collectors;@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {@Autowiredprivate SetmealDishService setmealDishService;/*** 新增套餐,同时保存套餐和菜品的关联关系* @param setmealDto*/@Override@Transactionalpublic void saveWithDish(SetmealDto setmealDto) {//保存套餐基本信息,操作setmeal,执行insertthis.save(setmealDto);List<SetmealDish> setmealDishs = setmealDto.getSetmealDishes();setmealDishs.stream().map((item) -> {item.setSetmealId(setmealDto.getId());return item;}).collect(Collectors.toList());//保存套餐和菜品的关联信息,操作set_meal_dish 执行insertsetmealDishService.saveBatch(setmealDishs);}
}
3.10 SetmealController
package com.runa.reggie.controller;import com.runa.reggie.common.R;
import com.runa.reggie.dto.SetmealDto;
import com.runa.reggie.service.SetmealDishService;
import com.runa.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {@Autowiredprivate SetmealService setmealService;@Autowiredprivate SetmealDishService setmealDishService;/*** 新增套餐* @param setmealDto* @return*/@PostMappingpublic R<String> save(@RequestBody SetmealDto setmealDto){log.info("套餐信息:{}",setmealDto);setmealService.saveWithDish(setmealDto);return R.success("新增套餐成功");}}
4 功能测试
二、套餐分页信息查询
2.1 需求分析
2.2 代码实现
2.2.1 SetmealController
package com.runa.reggie.controller;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.runa.reggie.common.R;
import com.runa.reggie.dto.SetmealDto;
import com.runa.reggie.entity.Category;
import com.runa.reggie.entity.Setmeal;
import com.runa.reggie.service.CategoryService;
import com.runa.reggie.service.SetmealDishService;
import com.runa.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;import java.util.List;
import java.util.stream.Collectors;@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {@Autowiredprivate SetmealService setmealService;@Autowiredprivate CategoryService categoryService;@Autowiredprivate SetmealDishService setmealDishService;/*** 新增套餐* @param setmealDto* @return*/@PostMappingpublic R<String> save(@RequestBody SetmealDto setmealDto){log.info("套餐信息:{}",setmealDto);setmealService.saveWithDish(setmealDto);return R.success("新增套餐成功");}/*** 套餐分页查询* @param page* @param pageSize* @param name* @return*/@GetMapping("/page")public R<Page> page(int page, int pageSize, String name){//分页构造器对象Page<Setmeal> pageInfo = new Page<>(page,pageSize);Page<SetmealDto> dtoPage = new Page<>();LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();//添加查询条件,根据name进行like模糊查询queryWrapper.like(name != null,Setmeal::getName,name);//添加排序条件,根据更新时间降序排列queryWrapper.orderByDesc(Setmeal::getUpdateTime);setmealService.page(pageInfo,queryWrapper);//对象拷贝BeanUtils.copyProperties(pageInfo,dtoPage,"records");List<Setmeal> records = pageInfo.getRecords();List<SetmealDto> list = records.stream().map((item) -> {SetmealDto setmealDto = new SetmealDto();//对象拷贝BeanUtils.copyProperties(item,setmealDto);//分类idLong categoryId = item.getCategoryId();//根据分类id查询分类对象Category category = categoryService.getById(categoryId);if(category != null){//分类名称String categoryName = category.getName();setmealDto.setCategoryName(categoryName);}return setmealDto;}).collect(Collectors.toList());dtoPage.setRecords(list);return R.success(dtoPage);}}
2.3 功能测试
三、删除套餐
3.1 需求分析
3.2 代码实现
3.2.1 SetmealService
package com.runa.reggie.service;import com.baomidou.mybatisplus.extension.service.IService;
import com.runa.reggie.dto.SetmealDto;
import com.runa.reggie.entity.Setmeal;import java.util.List;public interface SetmealService extends IService<Setmeal> {/*** 新增套餐,同时保存套餐和菜品的关联关系* @param setmealDto*/public void saveWithDish(SetmealDto setmealDto);/*** 删除套餐,同时需要删除套餐和菜品的关联数据* @param ids*/public void removeWithDish(List<Long> ids);
}
3.2.2 SetmealServiceImpl
package com.runa.reggie.service.impl;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.runa.reggie.common.CustomException;
import com.runa.reggie.dto.SetmealDto;
import com.runa.reggie.entity.Setmeal;
import com.runa.reggie.entity.SetmealDish;
import com.runa.reggie.mapper.SetmealMapper;
import com.runa.reggie.service.SetmealDishService;
import com.runa.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;import java.util.List;
import java.util.stream.Collectors;@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {@Autowiredprivate SetmealDishService setmealDishService;/*** 新增套餐,同时保存套餐和菜品的关联关系* @param setmealDto*/@Override@Transactionalpublic void saveWithDish(SetmealDto setmealDto) {//保存套餐基本信息,操作setmeal,执行insertthis.save(setmealDto);List<SetmealDish> setmealDishs = setmealDto.getSetmealDishes();setmealDishs.stream().map((item) -> {item.setSetmealId(setmealDto.getId());return item;}).collect(Collectors.toList());//保存套餐和菜品的关联信息,操作set_meal_dish 执行insertsetmealDishService.saveBatch(setmealDishs);}/*** 删除套餐,同时需要删除套餐和菜品的关联数据* @param ids*/@Transactionalpublic void removeWithDish(List<Long> ids) {//select count(*) from setmeal where id in (1,2,3) and status = 1//查询套餐状态,确定是否可用删除LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper();queryWrapper.in(Setmeal::getId,ids);queryWrapper.eq(Setmeal::getStatus,1);int count = this.count(queryWrapper);if(count > 0){//如果不能删除,抛出一个业务异常throw new CustomException("套餐正在售卖中,不能删除");}//如果可以删除,先删除套餐表中的数据---setmealthis.removeByIds(ids);//delete from setmeal_dish where setmeal_id in (1,2,3)LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper<>();lambdaQueryWrapper.in(SetmealDish::getSetmealId,ids);//删除关系表中的数据----setmeal_dishsetmealDishService.remove(lambdaQueryWrapper);}
}
3.2.3 SetmealController
package com.runa.reggie.controller;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.runa.reggie.common.R;
import com.runa.reggie.dto.SetmealDto;
import com.runa.reggie.entity.Category;
import com.runa.reggie.entity.Setmeal;
import com.runa.reggie.service.CategoryService;
import com.runa.reggie.service.SetmealDishService;
import com.runa.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;import java.util.List;
import java.util.stream.Collectors;@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {@Autowiredprivate SetmealService setmealService;@Autowiredprivate CategoryService categoryService;@Autowiredprivate SetmealDishService setmealDishService;/*** 新增套餐* @param setmealDto* @return*/@PostMappingpublic R<String> save(@RequestBody SetmealDto setmealDto){log.info("套餐信息:{}",setmealDto);setmealService.saveWithDish(setmealDto);return R.success("新增套餐成功");}/*** 套餐分页查询* @param page* @param pageSize* @param name* @return*/@GetMapping("/page")public R<Page> page(int page, int pageSize, String name){//分页构造器对象Page<Setmeal> pageInfo = new Page<>(page,pageSize);Page<SetmealDto> dtoPage = new Page<>();LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();//添加查询条件,根据name进行like模糊查询queryWrapper.like(name != null,Setmeal::getName,name);//添加排序条件,根据更新时间降序排列queryWrapper.orderByDesc(Setmeal::getUpdateTime);setmealService.page(pageInfo,queryWrapper);//对象拷贝BeanUtils.copyProperties(pageInfo,dtoPage,"records");List<Setmeal> records = pageInfo.getRecords();List<SetmealDto> list = records.stream().map((item) -> {SetmealDto setmealDto = new SetmealDto();//对象拷贝BeanUtils.copyProperties(item,setmealDto);//分类idLong categoryId = item.getCategoryId();//根据分类id查询分类对象Category category = categoryService.getById(categoryId);if(category != null){//分类名称String categoryName = category.getName();setmealDto.setCategoryName(categoryName);}return setmealDto;}).collect(Collectors.toList());dtoPage.setRecords(list);return R.success(dtoPage);}/*** 删除套餐* @param ids* @return*/@DeleteMappingpublic R<String> delete(@RequestParam List<Long> ids){log.info("ids:{}",ids);setmealService.removeWithDish(ids);return R.success("套餐数据删除成功");}}
3.3 功能测试
数据库修改状态:0