外卖项目day03-删除菜品-需求分析和功能实现---1

This commit is contained in:
xx
2026-11-21 12:04:11 +08:00
parent f871774096
commit 7b995714cc
6 changed files with 85 additions and 0 deletions
@@ -11,6 +11,8 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@Slf4j
@Api(tags = "菜品相关接口")
@@ -39,4 +41,14 @@ public class DishController {
PageResult pageResult = dishService.pageQuery(dto);
return Result.success(pageResult);
}
@DeleteMapping
@ApiOperation("菜品批量删除")
public Result delete(@RequestParam List<Long> ids){
log.info("菜品批量删除:{}",ids);
//调用业务层的菜品批量删除功能
dishService.deleteBatch(ids);
return Result.success();
}
}
@@ -34,4 +34,12 @@ public interface DishMapper {
* @return
*/
Page<DishVO> pageQuery(DishPageQueryDTO dto);
/**
* 根据id查询菜品
* @param id
* @return
*/
@Select("select * from dish where id = #{id}")
Dish getDishById(Long id);
}
@@ -0,0 +1,15 @@
package com.sky.mapper;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface SetmealDishMaper {
/**
* 根据菜品id查询关联套餐id
* @param ids
* @return
*/
List<Long> getSetmealIdsByDishIds(List<Long> ids);
}
@@ -4,6 +4,8 @@ import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.result.PageResult;
import java.util.List;
public interface DishService {
/*
新增菜品和它的口味
@@ -16,4 +18,10 @@ public interface DishService {
* @return 分页查询结果
*/
PageResult pageQuery(DishPageQueryDTO dto);
/**
* 批量删除菜品
* @param ids 要删除的菜品id
*/
void deleteBatch(List<Long> ids);
}
@@ -2,13 +2,18 @@ package com.sky.service.impl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Employee;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMaper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
@@ -27,6 +32,8 @@ public class DishServiceImpl implements DishService {
@Autowired
private DishFlavorMapper dishFlavorMapper;
@Autowired
private SetmealDishMaper setmealDishMaper;//套餐菜品中间表
/*
新增菜品和它的口味
@@ -67,4 +74,28 @@ public class DishServiceImpl implements DishService {
//获取分页查询结果,封装成PageResult
return new PageResult(page.getTotal(),page.getResult());
}
/**
* 批量删除菜品
* @param ids 要删除的菜品id
*/
@Override
public void deleteBatch(List<Long> ids) {
//1.传入的菜品的起售状态,如果有起售,提示用户不能删除,删除失败
ids.forEach(id->{
//根据每个ID查询出菜品,判断状态,但凡有一个是起售就立即删除失败
Dish dish = dishMapper.getDishById(id);
if (dish.getStatus() == StatusConstant.ENABLE) {
throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
}
});
//2.判断当前菜品是否被套餐关联了
List<Long> setmealIds = setmealDishMaper.getSetmealIdsByDishIds(ids);
if (setmealIds != null && setmealIds.size() > 0) {
//说明菜品是被关联了,那么就批量删除失败
throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
}
}
}
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.sky.mapper.SetmealDishMaper">
<select id="getSetmealIdsByDishIds" resultType="java.lang.Long">
select setmeal_id from setmeal_dish where dish_id in
<foreach collection="ids" open="(" close=")" separator="," item="dishId">
#{dishId}
</foreach>
</select>
</mapper>