批量删除菜品
This commit is contained in:
@@ -1,15 +1,17 @@
|
||||
package com.sky.controller.admin;
|
||||
|
||||
import com.sky.dto.DishDTO;
|
||||
import com.sky.dto.DishPageQueryDTO;
|
||||
import com.sky.result.PageResult;
|
||||
import com.sky.result.Result;
|
||||
import com.sky.service.DishService;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/admin/dish")
|
||||
@@ -22,13 +24,42 @@ public class DishController {
|
||||
|
||||
/**
|
||||
* 新增菜品
|
||||
*
|
||||
* @param dishDTO
|
||||
* @return
|
||||
*/
|
||||
@PostMapping
|
||||
public Result save(@RequestBody DishDTO dishDTO){
|
||||
log.info("新增菜品: {}",dishDTO);
|
||||
@ApiOperation("新增菜品")
|
||||
public Result save(@RequestBody DishDTO dishDTO) {
|
||||
log.info("新增菜品: {}", dishDTO);
|
||||
dishService.saveWithFlavor(dishDTO);
|
||||
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;
|
||||
|
||||
import com.sky.entity.DishFlavor;
|
||||
import org.apache.ibatis.annotations.Delete;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
import java.util.List;
|
||||
@@ -12,4 +13,11 @@ public interface DishFlavorMapper {
|
||||
* @param 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;
|
||||
|
||||
import com.github.pagehelper.Page;
|
||||
import com.sky.annotation.AutoFill;
|
||||
import com.sky.dto.DishPageQueryDTO;
|
||||
import com.sky.entity.Dish;
|
||||
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.Mapper;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
@@ -24,4 +28,26 @@ public interface DishMapper {
|
||||
*/
|
||||
@AutoFill(OperationType.INSERT)
|
||||
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;
|
||||
|
||||
import com.sky.dto.DishDTO;
|
||||
import com.sky.dto.DishPageQueryDTO;
|
||||
import com.sky.result.PageResult;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public interface DishService {
|
||||
|
||||
@@ -11,4 +15,17 @@ public interface DishService {
|
||||
* @param 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;
|
||||
|
||||
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.DishPageQueryDTO;
|
||||
import com.sky.entity.Dish;
|
||||
import com.sky.entity.DishFlavor;
|
||||
import com.sky.exception.DeletionNotAllowedException;
|
||||
import com.sky.mapper.DishFlavorMapper;
|
||||
import com.sky.mapper.DishMapper;
|
||||
import com.sky.mapper.SetmealDishMapper;
|
||||
import com.sky.result.PageResult;
|
||||
import com.sky.service.DishService;
|
||||
import com.sky.vo.DishVO;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -23,6 +32,8 @@ public class DishServiceImpl implements DishService {
|
||||
private DishMapper dishMapper;
|
||||
@Autowired
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user