Skip to content

删除菜品


需求分析

业务规则

(1)可以一次删除一个菜品,也可以批量删除菜品

(2)起售中的菜品不能删除

(3)被套餐关联的菜品不能删除

(4)删除菜品后,关联的口味数据也需要删除掉

⚠️ 注意事项

(1)在 dish 表中删除菜品基本数据时,同时,也要把关联在 dish_flavor 表中的数据一块删除

(2)setmeal_dish 表为菜品和套餐关联的中间表

(3)若删除的菜品数据关联着某个套餐,此时,删除失败

(4)若要删除套餐关联的菜品数据,先解除两者关联,再对菜品进行删除

DishController

java
	/**
     * 菜品批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("菜品批量删除")
    public Result delete(@RequestParam List<Long> ids) {
        log.info("菜品批量删除:{}", ids);
        dishService.deleteBatch(ids);//后绪步骤实现
        return Result.success();
    }

DishService

java
/**
 * 菜品批量删除
 *
 * @param ids
 */
void deleteBatch(List<Long> ids);

DishServiceImpl

java
@Autowired
private SetmealDishMapper setmealDishMapper;
/**
 * 菜品批量删除
 *
 * @param ids
 */
@Transactional//事务
public void deleteBatch(List<Long> ids) {
    //判断当前菜品是否能够删除---是否存在起售中的菜品??
    for (Long id : ids) {
        Dish dish = dishMapper.getById(id);//后绪步骤实现
        if (dish.getStatus() == StatusConstant.ENABLE) {
            //当前菜品处于起售中,不能删除
            throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
        }
    }

    //判断当前菜品是否能够删除---是否被套餐关联了??
    List<Long> setmealIds = setmealDishMapper.getSetmealIdsByDishIds(ids);
    if (setmealIds != null && setmealIds.size() > 0) {
        //当前菜品被套餐关联了,不能删除
        throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
    }

    //删除菜品表中的菜品数据
    for (Long id : ids) {
        dishMapper.deleteById(id);//后绪步骤实现
        //删除菜品关联的口味数据
        dishFlavorMapper.deleteByDishId(id);//后绪步骤实现
    }
}

DishMapper

java
/**
 * 根据主键查询菜品
 *
 * @param id
 * @return
 */
@Select("select * from dish where id = #{id}")
Dish getById(Long id);

SetmealDishMapper

java
package com.sky.mapper;

import com.sky.entity.SetmealDish;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;

@Mapper
public interface SetmealDishMapper {
    /**
     * 根据菜品id查询对应的套餐id
     *
     * @param dishIds
     * @return
     */
    //select setmeal_id from setmeal_dish where dish_id in (1,2,3,4)
    List<Long> getSetmealIdsByDishIds(List<Long> dishIds);
}

SetmealDishMapper.xml

xml
<?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">
    <select id="getSetmealIdsByDishIds" resultType="java.lang.Long">
        select setmeal_id from setmeal_dish where dish_id in
        <foreach collection="dishIds" item="dishId" separator="," open="(" close=")">
            #{dishId}
        </foreach>
    </select>
</mapper>

DishMapper

java
/**
 * 根据主键删除菜品数据
 *
 * @param id
 */
@Delete("delete from dish where id = #{id}")
void deleteById(Long id);

DishFlavorMapper

java
/**
 * 根据菜品id删除对应的口味数据
 * @param dishId
 */
@Delete("delete from dish_flavor where dish_id = #{dishId}")
void deleteByDishId(Long dishId);