黑马-苍穹外卖-Day3-批量删除菜品

批量删除菜品

控制层

    /**
     * 批量删除菜品
     * @param ids
     * @return
     */
    @DeleteMapping()
    @ApiOperation("批量删除菜品")
    public Result delete(@RequestParam List<Long> ids) {  // 批量,既可以删一个,也可删多个
        // @RequestParam List<Long> ids,传进来的其实是String ids,可以手动处理,根据逗号分隔,这里加注解是扔给MVC处理了

        log.info("批量删除菜品:{}", ids);
        dishService.deleteBatch(ids);
        return Result.success();
    }

业务层

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

业务层的实现类

/**
     * 批量删除菜品
     * @param ids
     */
    @Transactional  // 事务
    public void deleteBatch(List<Long> ids) {
        // 判断当前菜品是否能够删除 -- 是否存在启售中的菜品 -- status
        // 遍历数组,根据id查
        // 只要其中一个菜品是启售中,就直接抛异常,不允许删除
        for (Long id : ids) {
            Dish dish = dishMapper.getById(id);  // 根据主键查询,返回查到的结果
            if(dish.getStatus() == StatusConstant.ENABLE) {
                // 当前菜品处于启售中,不能删除
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }

        // 判断当前菜品是否能够删除 -- 是否被套餐关联
        // setmeal_dish表,根据菜品id(dish_id)查,查套餐id(setmeal_id),看看能不能查上来,有的话就不行
        List<Long> setmealIds = setmealDishMapper.getSetmealIdsByDishIds(ids);  // 返回查到的套餐id列表
        if(setmealIds != null && setmealIds.size() > 0) {
            // 当前菜品被套餐关联,不能删除
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        // 删除菜品表中的菜品数据
        for (Long id : ids) {
            dishMapper.deleteById(id);  // 根据id删除菜品表中的菜品
            // 删除菜品关联的口味数据 -- 不用查有没有,根据菜品id尝试去删就行了
            dishFlavorMapper.deleteByDishId(id);
        }
    }

涉及的Mapper

dishMapper
  • 判断当前菜品是否能够删除 – 是否存在启售中的菜品 – status
    /**
     * 根据主键查询菜品
     * @param id
     * @return
     */
    @Select("select * from dish where id = #{id}")
    Dish getById(Long id);
  • 根据id删除菜品表中的菜品
    /**
     * 根据主键(id)删除菜品表中的菜品
     * @param id
     */
    @Delete("delete from dish where id = #{id}")
    void deleteById(Long id);
setmealDishMapper
  • 判断当前菜品是否能够删除 – 是否被套餐关联
  • setmeal_dish表,根据菜品id(dish_id)查
    /**
     * 根据菜品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);
    <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>
dishFlavorMapper
  • 删除菜品关联的口味数据 – 不用查有没有,根据菜品id尝试去删就行了
    /**
     * 根据菜品id删除口味
     * @param dishId
     */
    @Delete("delete from dish_flavor where dish_id = #{dishId}")
    void deleteByDishId(Long dishId);

改进

在这里插入图片描述

        // 根据菜品id集合批量删除菜品数据
        // delete from dish where id in (?, ?)
        dishMapper.deleteByIds(ids);

        // 根据菜品id集合批量删除关联的口味数据
        // delete from dish_flavor where id in (?, ?)
        dishFlavorMapper.deleteByDishIds(ids);
  • DishMapper
    /**
     * 根据菜品ID集合批量删除菜品
     * @param ids
     */
    void deleteByIds(List<Long> ids);
    <delete id="deleteByIds">
        delete from dish where id in
        <foreach collection="ids" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </delete>
  • DishFlavorMapper
    /**
     * 根据菜品id集合批量删除关联的口味数据
     * @param dishIds
     */
    void deleteByDishIds(List<Long> dishIds);
    <delete id="deleteByDishIds">
        delete from dish_flavor where dish_id in
        <foreach collection="dishIds" open="(" close=")" separator="," item="dishId">
            #{dishId}
        </foreach>
    </delete>
  • 6
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值