Day04_苍穹外卖——套餐管理(实战)
目录
- 根据分类id查询菜品
- DishController
- DishService
- DishServiceImpl
- DishMapper
- 新增套餐
- 接口设计
- 代码实现
- SetmealController
- SetmealService
- SetmealServiceImpl
- SetmealMapper
- SetmealDishMapper
- SetmealDishMapper.xml
- 分页查询
- 接口设计
- 代码实现
- SetmealController
- SetmealService
- SetmealServiceImpl
- SetmealMapper
- SetmealMapper.xml
- 根据id查询套餐
- 接口设计
- 代码实现
- SetmealController
- SetmealService
- SetmealServiceImpl
- SetmealMapper
- SetmealMapper.xml
- 修改套餐
- 接口设计
- 代码实现
- SetmealController
- SetmealService
- SetmealServiceImpl
- SetmealMapper
- SetmealMapper.xml
- SetmealDishMapper
- 套餐起售、停售
- 接口设计
- 代码实现
- SetmealController
- SetmealService
- SetmealServiceImpl
- DishMapper
- 批量删除套餐
- 接口设计
- 代码实现
- Controller
- Service
- ServiceImpl
- Mapper
根据分类id查询菜品
在新增套餐的时候,会根据分类id去选择菜品
DishController
@GetMapping("/list")@ApiOperation("根据分类id查询菜品")public Result<List<Dish>> list(Long categoryId){log.info("根据分类id 查询菜品:{}",categoryId);List<Dish> dishList=dishService.list(categoryId);return Result.success(dishList);}
DishService
@GetMapping("/list")@ApiOperation("根据分类id查询菜品")public Result<List<Dish>> list(Long categoryId){log.info("根据分类id 查询菜品:{}",categoryId);List<Dish> dishList=dishService.list(categoryId);return Result.success(dishList);}
DishServiceImpl
@Overridepublic List<Dish> list(Long categoryId) {Dish dish = Dish.builder().categoryId(categoryId).status(1).build();return dishMapper.list(dish);}
DishMapper
@Select("select * from dish where status = 1 and category_id = #{categoryId}")List<Dish> list(Dish dish);
新增套餐
接口设计
代码实现
SetmealController
@PostMapping@ApiOperation("新增套餐")public Result save(@RequestBody SetmealDTO setmealDTO){log.info("新增套餐:{}", setmealDTO);setmealService.save(setmealDTO);return Result.success();}
SetmealService
void save(SetmealDTO setmealDTO);
SetmealServiceImpl
@Override@Transactionalpublic void save(SetmealDTO setmealDTO) {//1.构造套餐基本信息,保存数据到套餐setmeal表中Setmeal setmeal = new Setmeal();BeanUtils.copyProperties(setmealDTO, setmeal);setmeal.setStatus(StatusConstant.DISABLE);setmealMapper.insert(setmeal);//2.构造套餐菜品信息,保存数据到套餐菜品关系表setmeal_dish中List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();setmealDishes.forEach(setmealDish -> {//添加当前菜品的套餐idsetmealDish.setSetmealId(setmeal.getId());});setmealDishMapper.insertBatch(setmealDishes);}
SetmealMapper
@AutoFill(value = OperationType.INSERT)@Options(useGeneratedKeys = true, keyProperty = "id") //获取主键id值@Insert("insert into setmeal values (null, #{categoryId}, #{name}, #{price}, #{status}, #{description}, #{image}, #{createTime}, #{updateTime}, #{createUser}, #{updateUser})")void insert(Setmeal setmeal);
SetmealDishMapper
void insertBatch(List<SetmealDish> setmealDishes);
SetmealDishMapper.xml
<insert id="insertBatch">insert into setmeal_dish values<foreach collection="setmealDishes" item="sd" separator=",">(null,#{sd.setmealId},#{sd.dishId},#{sd.name},#{sd.price},#{sd.copies})</foreach></insert>
分页查询
接口设计
代码实现
SetmealController
@RequestMapping("/page")public Result page(SetmealPageQueryDTO setmealPageQueryDTO) {log.info("分页查询套餐信息", setmealPageQueryDTO);PageResult pageResult = setmealService.page(setmealPageQueryDTO);return Result.success(pageResult);}
SetmealService
PageResult page(SetmealPageQueryDTO setmealPageQueryDTO);
SetmealServiceImpl
@Overridepublic PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {//1.设置分页参数PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());//2.调用mapper方法,强转为 PagePage<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);//3.封装PageResult并返回return new PageResult(page.getTotal(), page.getResult());}
SetmealMapper
Page<SetmealVO> pageQuery(SetmealPageQueryDTO setmealPageQueryDTO);
SetmealMapper.xml
<select id="pageQuery" resultType="com.sky.vo.SetmealVO">select s.*,c.name as categoryName from setmeal s left join category c on s.category_id = c.id<where><if test="name != null and name != ''">and d.name like concat('%',#{name},'%')</if><if test="categoryId != null">and d.category_id = #{categoryId}</if><if test="status != null">and d.status = #{status}</if></where>order by s.create_time desc</select>
根据id查询套餐
接口设计
代码实现
SetmealController
@GetMapping("/{id}")@ApiOperation("根据id查询套餐")public Result<SetmealVO> getById(@PathVariable Long id) {log.info("根据id查询套餐,id为{}", id);SetmealVO setmealVO = setmealService.getByIdWithDish(id);return Result.success(setmealVO);}
SetmealService
SetmealVO getByIdWithDish(Long id);
SetmealServiceImpl
@Overridepublic SetmealVO getByIdWithDish(Long id) {SetmealVO setmealVO = setmealMapper.getByIdWithDish(id);return setmealVO;}
SetmealMapper
SetmealVO getByIdWithDish(Long id);
SetmealMapper.xml
<!--根据id查询套餐信息(回显)--><!--结果集映射 autoMapping设置为true,代表完成结果的自动映射,并自动忽略大小写,驼峰命名规则--><resultMap id="setmealAndDishMap" type="com.sky.vo.SetmealVO" autoMapping="true"><result column="id" property="id"/> <!--主键映射--><!-- 结果映射 --><!--collection:配置一对多规则,将查询出来的每条数据,封装到SetmealDish对象中,然后将对象添加到list集合setmealDishes中属性property:表示要将结果封装到对象的哪个属性中属性ofType:指定要将结果封装到什么对象中collection封装规则:result:配置表字段与属性字段映射关系,coloum表示表字段,property表示属性字段--><collection property="setmealDishes" ofType="com.sky.entity.SetmealDish"><result column="sd_id" property="id"/><result column="setmeal_id" property="setmealId"/><result column="dish_id" property="dishId"/><result column="sd_name" property="name"/><result column="sd_price" property="price"/><result column="copies" property="copies"/></collection></resultMap><!--查询套餐及套餐菜品关系数据--><select id="getByIdWithDish" parameterType="long" resultMap="setmealAndDishMap">select a.*,b.id sd_id,b.setmeal_id,b.dish_id,b.name sd_name,b.price sd_price,b.copiesfrom setmeal aleft joinsetmeal_dish bona.id = b.setmeal_idwhere a.id = #{id}</select>
修改套餐
接口设计
代码实现
SetmealController
@PutMapping@ApiOperation("修改套餐")public Result update(@RequestBody SetmealDTO setmealDTO) {log.info("修改套餐:{}", setmealDTO);setmealService.update(setmealDTO);return Result.success();}
SetmealService
void update(SetmealDTO setmealDTO);
SetmealServiceImpl
@Transactional@Overridepublic void update(SetmealDTO setmealDTO) {Setmeal setmeal = new Setmeal();BeanUtils.copyProperties(setmealDTO,setmeal);//修改套餐表,执行updatesetmealMapper.update(setmeal);//套餐idLong setmealId = setmealDTO.getId();//删除套餐和菜品的关联关系,操作setmeal_dish表,执行deletesetmealDishMapper.deleteBySetmealId(setmealId);List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();setmealDishes.forEach(setmealDish -> {setmealDish.setSetmealId(setmealId);});//重新插入套餐和菜品的关联关系,操作setmeal_dish表,执行insertsetmealDishMapper.insertBatch(setmealDishes);}
SetmealMapper
@AutoFill(OperationType.UPDATE)void update(Setmeal setmeal);
SetmealMapper.xml
<!--修改套餐--><update id="update">update setmeal<set><if test="categoryId != null">category_id = #{categoryId},</if><if test="description != null">description = #{description},</if><if test="image != null">image = #{image},</if><if test="name != null">name = #{name},</if><if test="price != null">price = #{price},</if><if test="status != null">status = #{status},</if><if test="updateTime != null">update_time = #{updateTime},</if><if test="updateUser != null">update_user = #{updateUser}</if></set>where id = #{id}</update>
SetmealDishMapper
@Delete("delete from setmeal_dish where setmeal_id = #{setmealId}")void deleteBySetmealId(Long setmealId);
套餐起售、停售
接口设计
代码实现
SetmealController
@PostMapping("/status/{status}")@ApiOperation("起售、停售套餐")public Result startOrStop(@PathVariable Integer status, Long id) {log.info("起售、停售套餐:status={},id={}", status,id);setmealService.startOrStop(status, id);return Result.success();}
SetmealService
void startOrStop(Integer status, Long id);
SetmealServiceImpl
@Overridepublic void startOrStop(Integer status, Long id) {//起售套餐时,判断套餐内是否有停售菜品,有停售菜品提示"套餐内包含未启售菜品,无法启售"if (status.equals(StatusConstant.ENABLE)) {//select a.* from dish a left join setmeal_dish b on a.id = b.dish_id where b.setmeal_id = ?List<Dish> dishList = dishMapper.getBySetmealId(id);if (!dishList.isEmpty()) {dishList.forEach(dish -> {if (StatusConstant.DISABLE.equals(dish.getStatus())) {throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);}});}}Setmeal setmeal = Setmeal.builder().id(id).status(status).build();setmealMapper.update(setmeal);}
DishMapper
@Select("select a.* from dish a left join setmeal_dish b on a.id = b.dish_id where b.setmeal_id = #{setmealId}")List<Dish> getBySetmealId(Long setmealId);
批量删除套餐
接口设计
代码实现
Controller
@DeleteMapping@ApiOperation("批量删除套餐")public Result delete(@RequestParam List<Long> ids) {log.info("批量删除套餐:{}", ids);setmealService.delete(ids);return Result.success();}
Service
void delete(List<Long> ids);
ServiceImpl
@Override@Transactionalpublic void delete(List<Long> ids) {//1.起售中的套餐不能删除ids.forEach(id -> {Setmeal setmeal = setmealMapper.getById(id);if (setmeal.getStatus().equals(StatusConstant.ENABLE)){throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);}});//2.删除套餐的同时,需要将套餐菜品关系数据删除ids.forEach(setmealId -> {//删除套餐表中的数据setmealMapper.deleteById(setmealId);//删除套餐菜品关系表中的数据setmealDishMapper.deleteBySetmealId(setmealId);});}
Mapper
/*** 根据id查询套餐* @param id*/@Select("select * from setmeal where id = #{id}")Setmeal getById(Long id);/*** 根据id删除套餐* @param setmealId*/@Delete("delete from setmeal where id = #{setmealId}")void deleteById(Long setmealId);