苍穹外卖--菜品管理-删除功能实现
This commit is contained in:
@@ -11,6 +11,8 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Api(tags = "菜品相关接口")
|
@Api(tags = "菜品相关接口")
|
||||||
@@ -40,4 +42,14 @@ public class DishController {
|
|||||||
|
|
||||||
return Result.success(pageResult);
|
return Result.success(pageResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@DeleteMapping
|
||||||
|
@ApiOperation("菜品批量删除")
|
||||||
|
public Result delete(@RequestParam List<Long> ids){ //@RequestParam:能够使用springmvc框架,对1,2,3 参数进行切割
|
||||||
|
log.info("菜品批量删除:{}",ids);
|
||||||
|
//调用业务层的菜品批量删除功能
|
||||||
|
dishService.deleteBath(ids);
|
||||||
|
return Result.success();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,4 +12,10 @@ public interface DishFlavorMapper {
|
|||||||
* @param flavors
|
* @param flavors
|
||||||
*/
|
*/
|
||||||
void insertBath(List<DishFlavor> flavors);
|
void insertBath(List<DishFlavor> flavors);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 菜品口味的批量删除
|
||||||
|
* @param ids
|
||||||
|
*/
|
||||||
|
void deleteBath(List<Long> ids);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,8 @@ import org.apache.ibatis.annotations.Mapper;
|
|||||||
import org.apache.ibatis.annotations.Options;
|
import org.apache.ibatis.annotations.Options;
|
||||||
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,18 @@ public interface DishMapper {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
Page<DishVO> pageQuery(DishPageQueryDTO dto);
|
Page<DishVO> pageQuery(DishPageQueryDTO dto);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据菜品id,查询菜品数据
|
||||||
|
* @param id
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Select("select * from dish where id = #{id}")
|
||||||
|
Dish getById(Long id);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 批量删除菜品
|
||||||
|
* @param ids
|
||||||
|
*/
|
||||||
|
void deleteBath(List<Long> ids);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package com.sky.mapper;
|
||||||
|
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 菜品--套餐--中间表setmeal_dish
|
||||||
|
*/
|
||||||
|
@Mapper
|
||||||
|
public interface SetmealDishMapper {
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据多个菜品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.dto.DishPageQueryDTO;
|
||||||
import com.sky.result.PageResult;
|
import com.sky.result.PageResult;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public interface DishService {
|
public interface DishService {
|
||||||
/*
|
/*
|
||||||
新增菜品和它的口味
|
新增菜品和它的口味
|
||||||
@@ -16,4 +18,10 @@ public interface DishService {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO);
|
PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 菜品批量删除
|
||||||
|
* @param ids
|
||||||
|
*/
|
||||||
|
void deleteBath(List<Long> ids);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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;
|
||||||
@@ -25,6 +29,8 @@ public class DishServiceImpl implements DishService {
|
|||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private DishFlavorMapper dishFlavorMapper;
|
private DishFlavorMapper dishFlavorMapper;
|
||||||
|
@Autowired
|
||||||
|
private SetmealDishMapper setmealDishMapper;
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -63,4 +69,32 @@ public class DishServiceImpl implements DishService {
|
|||||||
Page<DishVO> page = dishMapper.pageQuery(dto);
|
Page<DishVO> page = dishMapper.pageQuery(dto);
|
||||||
return new PageResult(page.getTotal(),page.getResult());
|
return new PageResult(page.getTotal(),page.getResult());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 菜品批量删除
|
||||||
|
* @param ids
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Transactional
|
||||||
|
public void deleteBath(List<Long> ids) {
|
||||||
|
//1.判断菜品的起售状态,如果是起售中,不能删除,程序立马停止,并提示用户
|
||||||
|
//todo 当前是遍历查询菜品,执行了多次select语句,效率低,我们可以使用一条sql查询出所有菜品,再遍历菜品对象是否有起售状态
|
||||||
|
ids.forEach(id->{
|
||||||
|
Dish dish = dishMapper.getById(id);
|
||||||
|
if (dish.getStatus() == StatusConstant.ENABLE) {
|
||||||
|
//不能删除,程序立马停止,并提示用户
|
||||||
|
throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
//2.判断菜品是否被套餐关联,如果被关联了那么不能删除,程序立马停止,并提示用户
|
||||||
|
List<Long> setmealIds = setmealDishMapper.getSetmealIdsByDishIds(ids);
|
||||||
|
if (setmealIds != null && setmealIds.size() > 0) {
|
||||||
|
//不能删除,程序立马停止,并提示用户
|
||||||
|
throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
|
||||||
|
}
|
||||||
|
//3.如果菜品可以删除,那么相关的口味也要删除掉
|
||||||
|
//根据菜品id删除菜品和口味数据
|
||||||
|
dishMapper.deleteBath(ids);
|
||||||
|
dishFlavorMapper.deleteBath(ids);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,4 +10,10 @@
|
|||||||
(#{flavor.dishId},#{flavor.name},#{flavor.value})
|
(#{flavor.dishId},#{flavor.name},#{flavor.value})
|
||||||
</foreach>
|
</foreach>
|
||||||
</insert>
|
</insert>
|
||||||
|
<delete id="deleteBath">
|
||||||
|
delete from dish_flavor where dish_id in
|
||||||
|
<foreach collection="ids" item="id" separator="," open="(" close=")">
|
||||||
|
#{id}
|
||||||
|
</foreach>
|
||||||
|
</delete>
|
||||||
</mapper>
|
</mapper>
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
insert into dish (name, category_id, price, image, description, create_time, update_time, create_user, update_user,status)
|
insert into dish (name, category_id, price, image, description, create_time, update_time, create_user, update_user,status)
|
||||||
values (#{name},#{categoryId},#{price},#{image},#{description},#{createTime},#{updateTime},#{createUser},#{updateUser},#{status})
|
values (#{name},#{categoryId},#{price},#{image},#{description},#{createTime},#{updateTime},#{createUser},#{updateUser},#{status})
|
||||||
</insert>
|
</insert>
|
||||||
|
|
||||||
<select id="pageQuery" resultType="com.sky.vo.DishVO">
|
<select id="pageQuery" resultType="com.sky.vo.DishVO">
|
||||||
SELECT d.*,c.name categoryName FROM dish d LEFT JOIN category c ON d.category_id = c.id
|
SELECT d.*,c.name categoryName FROM dish d LEFT JOIN category c ON d.category_id = c.id
|
||||||
<where>
|
<where>
|
||||||
@@ -23,4 +24,12 @@
|
|||||||
|
|
||||||
ORDER BY d.create_time DESC
|
ORDER BY d.create_time DESC
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
|
|
||||||
|
<delete id="deleteBath">
|
||||||
|
delete from dish where id in
|
||||||
|
<foreach collection="ids" item="id" separator="," open="(" close=")">
|
||||||
|
#{id}
|
||||||
|
</foreach>
|
||||||
|
</delete>
|
||||||
</mapper>
|
</mapper>
|
||||||
|
|||||||
10
sky-server/src/main/resources/mapper/SetmealDishMapper.xml
Normal file
10
sky-server/src/main/resources/mapper/SetmealDishMapper.xml
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
<?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="ids" item="id" separator="," open="(" close=")">
|
||||||
|
#{id}
|
||||||
|
</foreach>
|
||||||
|
</select>
|
||||||
|
</mapper>
|
||||||
Reference in New Issue
Block a user