批量删除菜品
This commit is contained in:
@@ -1,15 +1,17 @@
|
|||||||
package com.sky.controller.admin;
|
package com.sky.controller.admin;
|
||||||
|
|
||||||
import com.sky.dto.DishDTO;
|
import com.sky.dto.DishDTO;
|
||||||
|
import com.sky.dto.DishPageQueryDTO;
|
||||||
|
import com.sky.result.PageResult;
|
||||||
import com.sky.result.Result;
|
import com.sky.result.Result;
|
||||||
import com.sky.service.DishService;
|
import com.sky.service.DishService;
|
||||||
import io.swagger.annotations.Api;
|
import io.swagger.annotations.Api;
|
||||||
|
import io.swagger.annotations.ApiOperation;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
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.PostMapping;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import java.util.List;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/admin/dish")
|
@RequestMapping("/admin/dish")
|
||||||
@@ -22,13 +24,42 @@ public class DishController {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 新增菜品
|
* 新增菜品
|
||||||
|
*
|
||||||
* @param dishDTO
|
* @param dishDTO
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@PostMapping
|
@PostMapping
|
||||||
|
@ApiOperation("新增菜品")
|
||||||
public Result save(@RequestBody DishDTO dishDTO) {
|
public Result save(@RequestBody DishDTO dishDTO) {
|
||||||
log.info("新增菜品: {}", dishDTO);
|
log.info("新增菜品: {}", dishDTO);
|
||||||
dishService.saveWithFlavor(dishDTO);
|
dishService.saveWithFlavor(dishDTO);
|
||||||
return Result.success();
|
return Result.success();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 菜品分页查询
|
||||||
|
*
|
||||||
|
* @param pageQueryDTO
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@GetMapping("/page")
|
||||||
|
@ApiOperation("菜品分页查询")
|
||||||
|
public Result<PageResult> page(DishPageQueryDTO pageQueryDTO) {
|
||||||
|
log.info("菜品分页查询: {}", pageQueryDTO);
|
||||||
|
PageResult pageResult = dishService.pageQuery(pageQueryDTO);
|
||||||
|
return Result.success(pageResult);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 批量删除菜品
|
||||||
|
* @param ids
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@DeleteMapping
|
||||||
|
@ApiOperation("批量删除菜品")
|
||||||
|
public Result delete(@RequestParam List<Long> ids){
|
||||||
|
log.info("批量删除菜品: {}",ids);
|
||||||
|
dishService.deleteBatch(ids);
|
||||||
|
return Result.success();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.sky.mapper;
|
package com.sky.mapper;
|
||||||
|
|
||||||
import com.sky.entity.DishFlavor;
|
import com.sky.entity.DishFlavor;
|
||||||
|
import org.apache.ibatis.annotations.Delete;
|
||||||
import org.apache.ibatis.annotations.Mapper;
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -12,4 +13,11 @@ public interface DishFlavorMapper {
|
|||||||
* @param flavors
|
* @param flavors
|
||||||
*/
|
*/
|
||||||
void insertBatch(List<DishFlavor> flavors);
|
void insertBatch(List<DishFlavor> flavors);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据菜品ID删除对应的口味
|
||||||
|
* @param dishId
|
||||||
|
*/
|
||||||
|
@Delete("delete from dish_flavor where dish_id = #{dishId}")
|
||||||
|
void deleteByDishId(Long dishId);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,12 @@
|
|||||||
package com.sky.mapper;
|
package com.sky.mapper;
|
||||||
|
|
||||||
|
import com.github.pagehelper.Page;
|
||||||
import com.sky.annotation.AutoFill;
|
import com.sky.annotation.AutoFill;
|
||||||
|
import com.sky.dto.DishPageQueryDTO;
|
||||||
import com.sky.entity.Dish;
|
import com.sky.entity.Dish;
|
||||||
import com.sky.enumeration.OperationType;
|
import com.sky.enumeration.OperationType;
|
||||||
|
import com.sky.vo.DishVO;
|
||||||
|
import org.apache.ibatis.annotations.Delete;
|
||||||
import org.apache.ibatis.annotations.Insert;
|
import org.apache.ibatis.annotations.Insert;
|
||||||
import org.apache.ibatis.annotations.Mapper;
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
import org.apache.ibatis.annotations.Select;
|
import org.apache.ibatis.annotations.Select;
|
||||||
@@ -24,4 +28,26 @@ public interface DishMapper {
|
|||||||
*/
|
*/
|
||||||
@AutoFill(OperationType.INSERT)
|
@AutoFill(OperationType.INSERT)
|
||||||
void insert(Dish dish);
|
void insert(Dish dish);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 菜品分页查询
|
||||||
|
* @param pageQueryDTO
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
Page<DishVO> pageQuery(DishPageQueryDTO pageQueryDTO);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据id查找菜品
|
||||||
|
* @param id
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Select("select * from dish where id = #{id}")
|
||||||
|
Dish selectById(Long id);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据id删除菜品
|
||||||
|
* @param id
|
||||||
|
*/
|
||||||
|
@Delete("delete from dish where id = #{id}")
|
||||||
|
void deleteById(Long id);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package com.sky.mapper;
|
||||||
|
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
public interface SetmealDishMapper {
|
||||||
|
/**
|
||||||
|
* 根据菜品id查询套餐id
|
||||||
|
* @param ids
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
List<Long> getSetmealIdsByDishIds(List<Long> ids);
|
||||||
|
}
|
||||||
@@ -1,8 +1,12 @@
|
|||||||
package com.sky.service;
|
package com.sky.service;
|
||||||
|
|
||||||
import com.sky.dto.DishDTO;
|
import com.sky.dto.DishDTO;
|
||||||
|
import com.sky.dto.DishPageQueryDTO;
|
||||||
|
import com.sky.result.PageResult;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public interface DishService {
|
public interface DishService {
|
||||||
|
|
||||||
@@ -11,4 +15,17 @@ public interface DishService {
|
|||||||
* @param dishDTO
|
* @param dishDTO
|
||||||
*/
|
*/
|
||||||
void saveWithFlavor(DishDTO dishDTO);
|
void saveWithFlavor(DishDTO dishDTO);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 菜品分页查询
|
||||||
|
* @param pageQueryDTO
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
PageResult pageQuery(DishPageQueryDTO pageQueryDTO);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 菜品批量删除
|
||||||
|
* @param ids
|
||||||
|
*/
|
||||||
|
void deleteBatch(List<Long> ids);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,20 @@
|
|||||||
package com.sky.service.impl;
|
package com.sky.service.impl;
|
||||||
|
|
||||||
|
import com.github.pagehelper.Page;
|
||||||
|
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.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.service.DishService;
|
import com.sky.service.DishService;
|
||||||
|
import com.sky.vo.DishVO;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.BeanUtils;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@@ -23,6 +32,8 @@ public class DishServiceImpl implements DishService {
|
|||||||
private DishMapper dishMapper;
|
private DishMapper dishMapper;
|
||||||
@Autowired
|
@Autowired
|
||||||
private DishFlavorMapper dishFlavorMapper;
|
private DishFlavorMapper dishFlavorMapper;
|
||||||
|
@Autowired
|
||||||
|
private SetmealDishMapper setmealDishMapper;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 新增菜品
|
* 新增菜品
|
||||||
@@ -50,4 +61,45 @@ public class DishServiceImpl implements DishService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 菜品分页查询
|
||||||
|
* @param pageQueryDTO
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public PageResult pageQuery(DishPageQueryDTO pageQueryDTO) {
|
||||||
|
//设置分页
|
||||||
|
PageHelper.startPage(pageQueryDTO.getPage(), pageQueryDTO.getPageSize());
|
||||||
|
Page<DishVO> page = dishMapper.pageQuery(pageQueryDTO);
|
||||||
|
return new PageResult(page.getTotal(),page.getResult());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 菜品批量删除
|
||||||
|
* @param ids
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Transactional
|
||||||
|
public void deleteBatch(List<Long> ids) {
|
||||||
|
//判断菜品是否能够删除
|
||||||
|
//是否有起售中菜品
|
||||||
|
for (Long id : ids) {
|
||||||
|
Dish dish = dishMapper.selectById(id);
|
||||||
|
if (dish.getStatus().equals(StatusConstant.ENABLE)){
|
||||||
|
throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//是否关联套餐
|
||||||
|
List<Long> setmealIds = setmealDishMapper.getSetmealIdsByDishIds(ids);
|
||||||
|
if (setmealIds != null && !setmealIds.isEmpty()){
|
||||||
|
throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
|
||||||
|
}
|
||||||
|
//删除菜品
|
||||||
|
for (Long id : ids) {
|
||||||
|
dishMapper.deleteById(id);
|
||||||
|
//删除菜品口味
|
||||||
|
dishFlavorMapper.deleteByDishId(id);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,4 +10,22 @@
|
|||||||
values (#{name}, #{categoryId}, #{price}, #{image}, #{description}, #{status}, #{createTime}, #{updateTime},
|
values (#{name}, #{categoryId}, #{price}, #{image}, #{description}, #{status}, #{createTime}, #{updateTime},
|
||||||
#{createUser}, #{updateUser})
|
#{createUser}, #{updateUser})
|
||||||
</insert>
|
</insert>
|
||||||
|
<select id="pageQuery" resultType="com.sky.vo.DishVO">
|
||||||
|
select dish.*, category.name as categoryName
|
||||||
|
from dish
|
||||||
|
left outer join category
|
||||||
|
on dish.category_id = category.id
|
||||||
|
<where>
|
||||||
|
<if test="name != null">
|
||||||
|
and dish.name like concat('%',#{name},'%')
|
||||||
|
</if>
|
||||||
|
<if test="categoryId != null">
|
||||||
|
and dish.category_id = #{categoryId}
|
||||||
|
</if>
|
||||||
|
<if test="status != null">
|
||||||
|
and dish.status = #{status}
|
||||||
|
</if>
|
||||||
|
</where>
|
||||||
|
order by dish.create_time desc
|
||||||
|
</select>
|
||||||
</mapper>
|
</mapper>
|
||||||
13
sky-server/src/main/resources/mapper/SetmealDishMapper.xml
Normal file
13
sky-server/src/main/resources/mapper/SetmealDishMapper.xml
Normal 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">
|
||||||
|
|
||||||
|
<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