2025.4.16 菜品管理
This commit is contained in:
parent
21626b0663
commit
4dd7b78917
@ -28,7 +28,7 @@ public class DishController {
|
||||
@ApiOperation("新增菜品")
|
||||
public Result addDish(@RequestBody DishDTO dishDTO){
|
||||
log.info("新增菜品");
|
||||
dishService.addDish(dishDTO);
|
||||
dishService.saveWithFlavor(dishDTO);
|
||||
|
||||
//清理缓存数据
|
||||
String key = "dish_" + dishDTO.getCategoryId();
|
||||
@ -43,7 +43,7 @@ public class DishController {
|
||||
}
|
||||
@DeleteMapping
|
||||
@ApiOperation("删除菜品")
|
||||
public Result deleteDish(Long[] ids){
|
||||
public Result deleteDish(@RequestParam List<Long> ids){
|
||||
log.info("批量删除菜品");
|
||||
dishService.deleteBatch(ids);
|
||||
|
||||
@ -54,14 +54,14 @@ public class DishController {
|
||||
@GetMapping("/{id}")
|
||||
@ApiOperation("根据id查询菜品")
|
||||
public Result<DishVO> queryByIdWithFlavor(@PathVariable Long id){
|
||||
DishVO dishVO=dishService.queryByIdWithFlavor(id);
|
||||
DishVO dishVO=dishService.getByIdWithFlavor(id);
|
||||
return Result.success(dishVO);
|
||||
}
|
||||
@PutMapping
|
||||
@ApiOperation("更新菜品")
|
||||
public Result update(@RequestBody DishDTO dishDTO){
|
||||
log.info("修改菜品:{}", dishDTO);
|
||||
dishService.update(dishDTO);
|
||||
log.info("更新菜品:{}", dishDTO);
|
||||
dishService.updateWithFlavor(dishDTO);
|
||||
|
||||
//将所有的菜品缓存数据清理掉,所有以dish_开头的key
|
||||
cleanCache("dish_*");
|
||||
@ -78,12 +78,11 @@ public class DishController {
|
||||
}
|
||||
@GetMapping("/list")
|
||||
@ApiOperation("根据分类id查询菜品")
|
||||
public Result<List<Dish>> list(Long categoryId){
|
||||
Dish dish=new Dish();
|
||||
dish.setCategoryId(categoryId);
|
||||
List<Dish> dishList=dishService.list(dish);
|
||||
return Result.success(dishList);
|
||||
public Result<List<Dish>> list(Long categoryId) {
|
||||
List<Dish> list = dishService.list(categoryId);
|
||||
return Result.success(list);
|
||||
}
|
||||
|
||||
private void cleanCache(String pattern){
|
||||
Set keys = redisTemplate.keys(pattern);
|
||||
redisTemplate.delete(keys);
|
||||
|
@ -14,6 +14,6 @@ public interface DishFlavorMapper {
|
||||
@Delete("delete from dish_flavor where dish_id=#{id}")
|
||||
void deleteByDishId(Long id);
|
||||
@Select("select * from dish_flavor where dish_id=#{id}")
|
||||
List<DishFlavor> queryByDishId(Long id);
|
||||
List<DishFlavor> getByDishId(Long id);
|
||||
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
package com.sky.mapper;
|
||||
|
||||
import com.github.pagehelper.Page;
|
||||
import com.sky.annotation.AutoFill;
|
||||
import com.sky.dto.DishPageQueryDTO;
|
||||
import com.sky.entity.Dish;
|
||||
@ -13,23 +14,43 @@ import java.util.List;
|
||||
|
||||
@Mapper
|
||||
public interface DishMapper {
|
||||
/**
|
||||
* 插入菜品数据
|
||||
*/
|
||||
@AutoFill(OperationType.INSERT)
|
||||
void insert(Dish dish);
|
||||
/**
|
||||
* 根据主键查询菜品
|
||||
*/
|
||||
@Select("select * from dish where id=#{id}")
|
||||
Dish getById(Long id);
|
||||
|
||||
/**
|
||||
* 分类查询
|
||||
* @param dishPageQueryDTO
|
||||
* @return
|
||||
* 根据主键删除菜品数据
|
||||
*/
|
||||
List<DishVO> list(DishPageQueryDTO dishPageQueryDTO);
|
||||
|
||||
@Select("select * from dish where id=#{id}")
|
||||
Dish queryById(Long id);
|
||||
@Delete("delete from dish where id=#{id}")
|
||||
void deleteById(Long id);
|
||||
|
||||
/**
|
||||
* 根据id动态修改菜品数据
|
||||
*/
|
||||
@AutoFill(OperationType.UPDATE)
|
||||
void update(Dish dish);
|
||||
List<Dish> listByType(Dish dish);
|
||||
|
||||
/**
|
||||
* 动态条件查询菜品
|
||||
*/
|
||||
List<Dish> list(Dish dish);
|
||||
|
||||
|
||||
/**
|
||||
* 根据套餐id查询菜品
|
||||
*/
|
||||
@Select("select a.* from dish a left join setmeal_dish b on a.id = b.dish_id where b.setmeal_id = #{setmealId}")
|
||||
List<Dish> getBySetmealId(Long setmealId);
|
||||
|
||||
/**
|
||||
* 菜品分页查询
|
||||
*/
|
||||
Page<DishVO> pageQuery(DishPageQueryDTO dishPageQueryDTO);
|
||||
}
|
||||
|
@ -8,9 +8,9 @@ import org.apache.ibatis.annotations.Select;
|
||||
import java.util.List;
|
||||
|
||||
@Mapper
|
||||
public interface Setmeal_dishMapper {
|
||||
@Select("select setmeal_id from setmeal_dish where dish_id=#{id}")
|
||||
List<Long> queryByDishId(Long id);
|
||||
public interface SetmealDishMapper {
|
||||
// @Select("select setmeal_id from setmeal_dish where dish_id=#{id}")
|
||||
// List<Long> queryByDishId(Long id);
|
||||
|
||||
void add(SetmealDish setmealDish);
|
||||
@Delete("delete from setmeal_dish where setmeal_id=#{id}")
|
||||
@ -18,4 +18,10 @@ public interface Setmeal_dishMapper {
|
||||
|
||||
@Select("select * from setmeal_dish where setmeal_id = #{setmealId}")
|
||||
List<SetmealDish> getBySetmealId(Long setmealId);
|
||||
|
||||
/**
|
||||
* 根据菜品id查询对应的套餐id
|
||||
*/
|
||||
//select setmeal_id from setmeal_dish where dish_id in (1,2,3,4)
|
||||
List<Long> getSetmealIdsByDishIds(List<Long> dishIds);
|
||||
}
|
@ -9,19 +9,19 @@ import com.sky.vo.DishVO;
|
||||
import java.util.List;
|
||||
|
||||
public interface DishService {
|
||||
void addDish(DishDTO dishDTO);
|
||||
void saveWithFlavor(DishDTO dishDTO);
|
||||
|
||||
PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO);
|
||||
|
||||
void deleteBatch(Long[] ids);
|
||||
void deleteBatch(List<Long> ids);
|
||||
|
||||
DishVO queryByIdWithFlavor(Long id);
|
||||
DishVO getByIdWithFlavor(Long id);
|
||||
|
||||
void update(DishDTO dishDTO);
|
||||
void updateWithFlavor(DishDTO dishDTO);
|
||||
|
||||
void startOrStop(Integer status, Long id);
|
||||
|
||||
List<Dish> list(Dish dish);
|
||||
List<Dish> list(Long categoryId);
|
||||
|
||||
List<DishVO> listWithFlavor(Dish dish);
|
||||
}
|
||||
|
@ -9,13 +9,11 @@ import com.sky.dto.DishPageQueryDTO;
|
||||
import com.sky.entity.Dish;
|
||||
import com.sky.entity.DishFlavor;
|
||||
import com.sky.entity.Setmeal;
|
||||
import com.sky.entity.SetmealDish;
|
||||
import com.sky.exception.DeletionNotAllowedException;
|
||||
import com.sky.exception.SetmealEnableFailedException;
|
||||
import com.sky.mapper.DishFlavorMapper;
|
||||
import com.sky.mapper.DishMapper;
|
||||
import com.sky.mapper.SetmealMapper;
|
||||
import com.sky.mapper.Setmeal_dishMapper;
|
||||
import com.sky.mapper.SetmealDishMapper;
|
||||
import com.sky.result.PageResult;
|
||||
import com.sky.service.DishService;
|
||||
import com.sky.vo.DishVO;
|
||||
@ -34,66 +32,96 @@ public class DishServiceImpl implements DishService {
|
||||
@Autowired
|
||||
private DishFlavorMapper dishFlavorMapper;
|
||||
@Autowired
|
||||
private Setmeal_dishMapper setmeal_dishMapper;
|
||||
private SetmealDishMapper setmealDishMapper;
|
||||
@Autowired
|
||||
private SetmealMapper setmealMapper;
|
||||
@Override
|
||||
public void addDish(DishDTO dishDTO) {
|
||||
/**
|
||||
* 新增菜品和对应的口味
|
||||
*/
|
||||
@Transactional //对两表及以上表操作时需添加@Transactional
|
||||
public void saveWithFlavor(DishDTO dishDTO) {
|
||||
Dish dish=new Dish();
|
||||
BeanUtils.copyProperties(dishDTO,dish);
|
||||
//向菜品表插入1条数据
|
||||
dishMapper.insert(dish);
|
||||
Long dishId=dish.getId();
|
||||
List<DishFlavor> flavorList=dishDTO.getFlavors();
|
||||
for(DishFlavor flavor:flavorList){
|
||||
flavor.setDishId(dishId);
|
||||
}
|
||||
dishFlavorMapper.insertBatch(flavorList);
|
||||
}
|
||||
|
||||
//获取insert语句生成的主键值
|
||||
Long dishId=dish.getId();
|
||||
List<DishFlavor> flavors=dishDTO.getFlavors();
|
||||
if (flavors != null && flavors.size() > 0) {
|
||||
flavors.forEach(dishFlavor -> {
|
||||
dishFlavor.setDishId(dishId);
|
||||
});
|
||||
//向口味表插入n条数据
|
||||
dishFlavorMapper.insertBatch(flavors);//后绪步骤实现
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 菜品分页查询
|
||||
*/
|
||||
@Override
|
||||
public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
|
||||
PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
|
||||
List<DishVO> dishVOList=dishMapper.list(dishPageQueryDTO);
|
||||
Page<DishVO> p= (Page<DishVO>) dishVOList;
|
||||
PageResult pageResult=new PageResult(p.getTotal(),p.getResult());
|
||||
return pageResult;
|
||||
PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
|
||||
Page<DishVO> page = dishMapper.pageQuery(dishPageQueryDTO);
|
||||
return new PageResult(page.getTotal(), page.getResult());
|
||||
}
|
||||
|
||||
/**
|
||||
* 菜品批量删除
|
||||
*/
|
||||
@Override
|
||||
@Transactional
|
||||
public void deleteBatch(Long[] ids) {
|
||||
for(Long id:ids){
|
||||
Dish dish=dishMapper.queryById(id);
|
||||
if(dish.getStatus()== StatusConstant.ENABLE)
|
||||
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);
|
||||
else{
|
||||
List<Long> setmealids=setmeal_dishMapper.queryByDishId(id);
|
||||
if(setmealids!=null && setmealids.size() > 0)
|
||||
throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
|
||||
else {
|
||||
dishMapper.deleteById(id);
|
||||
dishFlavorMapper.deleteByDishId(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
//判断当前菜品是否能够删除---是否被套餐关联了??
|
||||
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);//后绪步骤实现
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据id查询菜品和对应的口味数据
|
||||
*/
|
||||
@Override
|
||||
public DishVO queryByIdWithFlavor(Long id) {
|
||||
Dish dish=dishMapper.queryById(id);
|
||||
public DishVO getByIdWithFlavor(Long id) {
|
||||
//根据id查询菜品数据
|
||||
Dish dish=dishMapper.getById(id);
|
||||
//根据菜品id查询口味数据
|
||||
List<DishFlavor> dishFlavors=dishFlavorMapper.getByDishId(id);
|
||||
//将查询到的数据封装到VO
|
||||
DishVO dishVO=new DishVO();
|
||||
BeanUtils.copyProperties(dish,dishVO);
|
||||
List<DishFlavor> dishFlavors=dishFlavorMapper.queryByDishId(id);
|
||||
dishVO.setFlavors(dishFlavors);
|
||||
|
||||
return dishVO;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据id修改菜品基本信息和对应的口味信息
|
||||
*/
|
||||
@Override
|
||||
public void update(DishDTO dishDTO) {
|
||||
public void updateWithFlavor(DishDTO dishDTO) {
|
||||
Dish dish=new Dish();
|
||||
BeanUtils.copyProperties(dishDTO,dish);
|
||||
//修改菜品表基本信息
|
||||
dishMapper.update(dish);
|
||||
dishFlavorMapper.deleteByDishId(dishDTO.getId()); //先删再添加口味,不要在原来的上面修改!原来可能2种口味,现在3种,也无法修改!
|
||||
|
||||
//删除原有的口味数据
|
||||
dishFlavorMapper.deleteByDishId(dishDTO.getId());
|
||||
//重新插入口味数据
|
||||
List<DishFlavor> flavors = dishDTO.getFlavors();
|
||||
if (flavors != null && flavors.size() > 0) {
|
||||
flavors.forEach(dishFlavor -> {
|
||||
@ -105,27 +133,41 @@ public class DishServiceImpl implements DishService {
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void startOrStop(Integer status, Long id) {
|
||||
Dish dish=new Dish();
|
||||
dish.setId(id);
|
||||
dish.setStatus(status);
|
||||
if(status==StatusConstant.DISABLE){ //如果菜品停售,正在起售的套餐也要停售
|
||||
List<Long> setmealids=setmeal_dishMapper.queryByDishId(id);
|
||||
for(Long setmealid:setmealids){
|
||||
Setmeal setmeal=setmealMapper.getById(setmealid);
|
||||
if(StatusConstant.ENABLE == setmeal.getStatus()){ //若菜品停售,则套餐起售失败!
|
||||
setmeal.setStatus(StatusConstant.DISABLE);
|
||||
Dish dish = Dish.builder()
|
||||
.id(id)
|
||||
.status(status)
|
||||
.build();
|
||||
dishMapper.update(dish);
|
||||
|
||||
if (status == StatusConstant.DISABLE) {
|
||||
// 如果是停售操作,还需要将包含当前菜品的套餐也停售
|
||||
List<Long> dishIds = new ArrayList<>();
|
||||
dishIds.add(id);
|
||||
// select setmeal_id from setmeal_dish where dish_id in (?,?,?)
|
||||
List<Long> setmealIds = setmealDishMapper.getSetmealIdsByDishIds(dishIds);
|
||||
if (setmealIds != null && setmealIds.size() > 0) {
|
||||
for (Long setmealId : setmealIds) {
|
||||
Setmeal setmeal = Setmeal.builder()
|
||||
.id(setmealId)
|
||||
.status(StatusConstant.DISABLE)
|
||||
.build();
|
||||
setmealMapper.update(setmeal);
|
||||
}
|
||||
}
|
||||
}
|
||||
dishMapper.update(dish);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据分类id查询菜品
|
||||
*/
|
||||
@Override
|
||||
public List<Dish> list(Dish dish) {
|
||||
List<Dish> dishList=dishMapper.listByType(dish);
|
||||
return dishList;
|
||||
public List<Dish> list(Long categoryId) {
|
||||
Dish dish = Dish.builder()
|
||||
.categoryId(categoryId)
|
||||
.status(StatusConstant.ENABLE)
|
||||
.build();
|
||||
return dishMapper.list(dish);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -135,7 +177,7 @@ public class DishServiceImpl implements DishService {
|
||||
* @return
|
||||
*/
|
||||
public List<DishVO> listWithFlavor(Dish dish) {
|
||||
List<Dish> dishList = dishMapper.listByType(dish);
|
||||
List<Dish> dishList = dishMapper.list(dish);
|
||||
|
||||
List<DishVO> dishVOList = new ArrayList<>();
|
||||
|
||||
@ -144,7 +186,7 @@ public class DishServiceImpl implements DishService {
|
||||
BeanUtils.copyProperties(d,dishVO);
|
||||
|
||||
//根据菜品id查询对应的口味
|
||||
List<DishFlavor> flavors = dishFlavorMapper.queryByDishId(d.getId());
|
||||
List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());
|
||||
|
||||
dishVO.setFlavors(flavors);
|
||||
dishVOList.add(dishVO);
|
||||
|
@ -13,7 +13,7 @@ import com.sky.exception.DeletionNotAllowedException;
|
||||
import com.sky.exception.SetmealEnableFailedException;
|
||||
import com.sky.mapper.DishMapper;
|
||||
import com.sky.mapper.SetmealMapper;
|
||||
import com.sky.mapper.Setmeal_dishMapper;
|
||||
import com.sky.mapper.SetmealDishMapper;
|
||||
import com.sky.result.PageResult;
|
||||
import com.sky.service.SetmealService;
|
||||
import com.sky.vo.DishItemVO;
|
||||
@ -30,7 +30,7 @@ public class SetmealServiceImpl implements SetmealService {
|
||||
@Autowired
|
||||
private SetmealMapper setmealMapper;
|
||||
@Autowired
|
||||
private Setmeal_dishMapper setmeal_dishMapper;
|
||||
private SetmealDishMapper setmeal_dishMapper;
|
||||
@Autowired
|
||||
private DishMapper dishMapper;
|
||||
@Override
|
||||
|
@ -43,7 +43,7 @@ public class ShoppingCartServiceImpl implements ShoppingCartService {
|
||||
Long dishId = shoppingCartDTO.getDishId();
|
||||
if (dishId != null) {
|
||||
//添加到购物车的是菜品
|
||||
Dish dish = dishMapper.queryById(dishId);
|
||||
Dish dish = dishMapper.getById(dishId);
|
||||
shoppingCart.setName(dish.getName());
|
||||
shoppingCart.setImage(dish.getImage());
|
||||
shoppingCart.setAmount(dish.getPrice());
|
||||
|
@ -6,21 +6,6 @@
|
||||
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})
|
||||
</insert>
|
||||
<select id="list" resultType="com.sky.vo.DishVO">
|
||||
select d.* , c.name as categoryName from dish d left outer join category c on d.category_id = c.id
|
||||
<where>
|
||||
<if test="name != null">
|
||||
and d.name like concat('%',#{name},'%')
|
||||
</if>
|
||||
<if test="categoryId != null">
|
||||
and d.category_id = #{categoryId}
|
||||
</if>
|
||||
<if test="status != null">
|
||||
and d.status = #{status}
|
||||
</if>
|
||||
</where>
|
||||
order by d.create_time desc
|
||||
</select>
|
||||
<select id="listByType" resultType="com.sky.entity.Dish">
|
||||
select * from dish
|
||||
<where>
|
||||
@ -42,4 +27,34 @@
|
||||
</set>
|
||||
where id=#{id}
|
||||
</update>
|
||||
<select id="pageQuery" resultType="com.sky.vo.DishVO">
|
||||
select d.* , c.name as categoryName from dish d left outer join category c on d.category_id = c.id
|
||||
<where>
|
||||
<if test="name != null">
|
||||
and d.name like concat('%',#{name},'%')
|
||||
</if>
|
||||
<if test="categoryId != null">
|
||||
and d.category_id = #{categoryId}
|
||||
</if>
|
||||
<if test="status != null">
|
||||
and d.status = #{status}
|
||||
</if>
|
||||
</where>
|
||||
order by d.create_time desc
|
||||
</select>
|
||||
<select id="list" resultType="com.sky.entity.Dish">
|
||||
select * from dish
|
||||
<where>
|
||||
<if test="name != null">
|
||||
and name like concat('%',#{name},'%')
|
||||
</if>
|
||||
<if test="categoryId != null">
|
||||
and category_id = #{categoryId}
|
||||
</if>
|
||||
<if test="status != null">
|
||||
and status = #{status}
|
||||
</if>
|
||||
</where>
|
||||
order by create_time desc
|
||||
</select>
|
||||
</mapper>
|
@ -1,10 +1,16 @@
|
||||
<?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.Setmeal_dishMapper">
|
||||
<mapper namespace="com.sky.mapper.SetmealDishMapper">
|
||||
|
||||
<insert id="add">
|
||||
insert into setmeal_dish (setmeal_id,dish_id,name,price,copies)
|
||||
values (#{setmealId},#{dishId},#{name},#{price},#{copies})
|
||||
</insert>
|
||||
<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>
|
Loading…
x
Reference in New Issue
Block a user