菜品批量删除

This commit is contained in:
bunny 2024-01-07 22:41:30 +08:00
parent 7d09b5abac
commit a9fdc30d10
9 changed files with 160 additions and 1 deletions

View File

@ -11,6 +11,7 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.List;
@RestController @RestController
@RequestMapping("/admin/dish") @RequestMapping("/admin/dish")
@ -36,6 +37,7 @@ public class DishController {
/** /**
* 菜品分页查询 * 菜品分页查询
*
* @param dishPageQueryDTO DishPageQueryDTO * @param dishPageQueryDTO DishPageQueryDTO
* @return Result<PageResult> * @return Result<PageResult>
*/ */
@ -46,4 +48,17 @@ public class DishController {
PageResult pageResult = dishService.pageQuery(dishPageQueryDTO); PageResult pageResult = dishService.pageQuery(dishPageQueryDTO);
return Result.success(pageResult); return Result.success(pageResult);
} }
/**
* 菜品批量删除
* @param ids List<Long>
* @return Result<String>
*/
@ApiOperation("菜品批量删除")
@DeleteMapping()
public Result<String> delete(@RequestParam List<Long> ids) {
log.info("菜品批量删除:{}", ids);
dishService.deleteBatch(ids);
return Result.success();
}
} }

View File

@ -11,4 +11,16 @@ public interface DishFlavorMapper {
* @param flavors List<DishFlavor> * @param flavors List<DishFlavor>
*/ */
void insertBatch(List<DishFlavor> flavors); void insertBatch(List<DishFlavor> flavors);
/**
* 删除菜品关联的口味数据
* @param id Long
*/
void deleteByDishId(Long id);
/**
* 删除菜品关联的口味数据
* @param dishIds List<Long>
*/
void deleteByDishIds(List<Long> dishIds);
} }

View File

@ -9,6 +9,8 @@ import com.sky.vo.DishVO;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper @Mapper
public interface DishMapper { public interface DishMapper {
@ -34,4 +36,23 @@ public interface DishMapper {
* @return Page<DishVO> * @return Page<DishVO>
*/ */
Page<DishVO> pageQuery(DishPageQueryDTO dishPageQueryDTO); Page<DishVO> pageQuery(DishPageQueryDTO dishPageQueryDTO);
/**
* 判断当前菜品是否能被删除---是否存在起售菜品
* @param id Long
* @return Dish
*/
Dish getById(Long id);
/**
* 删除菜品表中的菜品数据
* @param id Long
*/
void deleteById(Long id);
/**
* 删除菜品表中的菜品数据
* @param ids List<Long>
*/
void deleteByIds(List<Long> ids);
} }

View File

@ -0,0 +1,15 @@
package com.sky.mapper;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface SetMealDishMapper {
/**
* 查询对应套餐id
* @param dishIds List<Long>
* @return List<Long>
*/
List<Long> getSetMealDishIds(List<Long> dishIds);
}

View File

@ -4,6 +4,8 @@ import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO; import com.sky.dto.DishPageQueryDTO;
import com.sky.result.PageResult; import com.sky.result.PageResult;
import java.util.List;
public interface DishService { public interface DishService {
/** /**
* 新增菜品和口味 * 新增菜品和口味
@ -18,4 +20,10 @@ public interface DishService {
* @return PageResult * @return PageResult
*/ */
PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO); PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO);
/**
* 菜品批量删除
* @param ids List<Long>
*/
void deleteBatch(List<Long> ids);
} }

View File

@ -2,12 +2,16 @@ package com.sky.service.impl;
import com.github.pagehelper.Page; import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO; import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO; import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish; import com.sky.entity.Dish;
import com.sky.entity.DishFlavor; import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper; import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper; import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealDishMapper;
import com.sky.result.PageResult; import com.sky.result.PageResult;
import com.sky.service.DishService; import com.sky.service.DishService;
import com.sky.vo.DishVO; import com.sky.vo.DishVO;
@ -26,6 +30,8 @@ public class DishServiceImpl implements DishService {
private DishMapper dishMapper; private DishMapper dishMapper;
@Resource @Resource
private DishFlavorMapper dishFlavorMapper; private DishFlavorMapper dishFlavorMapper;
@Resource
private SetMealDishMapper setMealDishMapper;
/** /**
* 新增菜品和口味 * 新增菜品和口味
@ -54,6 +60,7 @@ public class DishServiceImpl implements DishService {
/** /**
* 菜品分页查询 * 菜品分页查询
*
* @param dishPageQueryDTO DishPageQueryDTO * @param dishPageQueryDTO DishPageQueryDTO
* @return PageResult * @return PageResult
*/ */
@ -61,6 +68,33 @@ public class DishServiceImpl implements DishService {
public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) { public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize()); PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
Page<DishVO> page = dishMapper.pageQuery(dishPageQueryDTO); Page<DishVO> page = dishMapper.pageQuery(dishPageQueryDTO);
return new PageResult(page.getTotal(),page.getResult()); return new PageResult(page.getTotal(), page.getResult());
}
/**
* 菜品批量删除
*
* @param ids List<Long>
*/
@Override
public void deleteBatch(List<Long> ids) {
// 判断当前菜品是否能被删除---是否存在起售菜品
for (Long id : ids) {
Dish dish = dishMapper.getById(id);
if (dish.getStatus().equals(StatusConstant.ENABLE)) {
throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
}
}
// 判断当前菜品是否能够删除--是否被套餐关联
List<Long> setMealDishIds = setMealDishMapper.getSetMealDishIds(ids);
if (setMealDishIds != null && !setMealDishIds.isEmpty()) {
// 当前菜品被关联了
throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
}
// 删除菜品表中的菜品数据
dishMapper.deleteByIds(ids);
// 删除菜品关联的口味数据
dishFlavorMapper.deleteByDishIds(ids);
} }
} }

View File

@ -10,4 +10,21 @@
( #{df.dishId},#{df.name},#{df.value}) ( #{df.dishId},#{df.name},#{df.value})
</foreach> </foreach>
</insert> </insert>
<!-- 删除菜品关联的口味数据 -->
<delete id="deleteByDishId">
delete
from dish_flavor
where dish_id = #{dishId};
</delete>
<!-- 删除菜品关联的口味数据 -->
<delete id="deleteByDishIds">
delete
from dish_flavor
where dish_id in
<foreach collection="dishIds" open="(" close=")" item="dishId">
#{dishId}
</foreach>
</delete>
</mapper> </mapper>

View File

@ -10,6 +10,23 @@
#{updateTime}, #{createUser}, #{updateUser}); #{updateTime}, #{createUser}, #{updateUser});
</insert> </insert>
<!-- 删除菜品表中的菜品数据 -->
<delete id="deleteById">
delete
from dish
where id = #{id}
</delete>
<!-- 删除菜品表中的菜品数据 -->
<delete id="deleteByIds">
delete
from dish
where id in
<foreach collection="ids" open="(" close=")" separator="," item="id">
#{id}
</foreach>
</delete>
<!-- 根据分类id查询菜品数量 --> <!-- 根据分类id查询菜品数量 -->
<select id="countByCategoryId" resultType="java.lang.Integer"> <select id="countByCategoryId" resultType="java.lang.Integer">
select count(id) select count(id)
@ -35,4 +52,11 @@
</where> </where>
order by d.create_time desc order by d.create_time desc
</select> </select>
<!-- 判断当前菜品是否能被删除 - 是否存在起售菜品 -->
<select id="getById" resultType="com.sky.entity.Dish">
select *
from dish
where id = #{id};
</select>
</mapper> </mapper>

View File

@ -0,0 +1,13 @@
<?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.SetMealDishMapper">
<!-- 查询对应套餐id -->
<select id="getSetMealDishIds" resultType="java.lang.Long">
select *
from setmeal_dish where dish_id in
<foreach collection="dishIds" item="dishId" separator="," open="(" close=")">
#{dishId}
</foreach>
</select>
</mapper>