删除菜品业务代码开发
This commit is contained in:
@@ -3,16 +3,17 @@ package com.sky.controller.admin;
|
||||
|
||||
import com.sky.constant.MessageConstant;
|
||||
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;
|
||||
|
||||
|
||||
/**
|
||||
@@ -42,4 +43,36 @@ public class DishController {
|
||||
dishService.saveWithFlavor(dishDTO);
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 菜品分页查询
|
||||
* @param dishPageQueryDTO
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/page")
|
||||
@ApiOperation("菜品分页查询")
|
||||
public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO) {
|
||||
log.info("菜品分页查询:{}", dishPageQueryDTO);
|
||||
|
||||
PageResult pageResult = dishService.pageQuery(dishPageQueryDTO);
|
||||
|
||||
return Result.success(pageResult);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 菜品批量删除
|
||||
* @param ids
|
||||
* @return
|
||||
*/
|
||||
@DeleteMapping
|
||||
@ApiOperation("菜品批量删除")
|
||||
public Result delete(@RequestParam List<Long> ids){ //SpringMVC动态地将字符串解析成数字
|
||||
log.info("菜品批量删除:{}", ids);
|
||||
|
||||
dishService.deleteBatch(ids);
|
||||
|
||||
return Result.success();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,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;
|
||||
@@ -14,4 +15,12 @@ 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.Mapper;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
|
||||
@@ -24,4 +28,29 @@ public interface DishMapper {
|
||||
*/
|
||||
@AutoFill(value = OperationType.INSERT) //公共字段自动填充 不需要手动赋值
|
||||
void insert(Dish dish);
|
||||
|
||||
|
||||
/**
|
||||
* 菜品分页查询
|
||||
* @param dishPageQueryDTO
|
||||
* @return
|
||||
*/
|
||||
Page<DishVO> pageQuery(DishPageQueryDTO dishPageQueryDTO);
|
||||
|
||||
|
||||
/**
|
||||
* 根据主键查询菜品
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
@Select("select * from dish where id = #{id}")
|
||||
Dish getById(Long id);
|
||||
|
||||
|
||||
/**
|
||||
* 根据主键删除菜品id
|
||||
* @param id
|
||||
*/
|
||||
@Delete("delete from dish where id = #{id}")
|
||||
void deleteById(Long id);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.sky.mapper;
|
||||
|
||||
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Mapper
|
||||
public interface SetmealDishMapper {
|
||||
|
||||
|
||||
/**
|
||||
* 根据菜品id查询多个套餐id
|
||||
* @param dishIds
|
||||
* @return
|
||||
*/
|
||||
List<Long> getSetmealIdsByDishIds(List<Long> dishIds);
|
||||
}
|
||||
@@ -1,6 +1,10 @@
|
||||
package com.sky.service;
|
||||
|
||||
import com.sky.dto.DishDTO;
|
||||
import com.sky.dto.DishPageQueryDTO;
|
||||
import com.sky.result.PageResult;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface DishService {
|
||||
|
||||
@@ -10,4 +14,19 @@ public interface DishService {
|
||||
* @param dishDTO
|
||||
*/
|
||||
public void saveWithFlavor(DishDTO dishDTO);
|
||||
|
||||
|
||||
/**
|
||||
* 菜品分页查询
|
||||
* @param dishPageQueryDTO
|
||||
* @return
|
||||
*/
|
||||
PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO);
|
||||
|
||||
|
||||
/**
|
||||
* 菜品批量删除
|
||||
* @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 org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -20,10 +29,10 @@ public class DishServiceImpl implements DishService {
|
||||
|
||||
@Autowired
|
||||
private DishMapper dishMapper;
|
||||
|
||||
|
||||
@Autowired
|
||||
private DishFlavorMapper dishFlavorMapper;
|
||||
@Autowired
|
||||
private SetmealDishMapper setmealDishMapper;
|
||||
|
||||
/**
|
||||
* 新增菜品和对应的口味数据
|
||||
@@ -53,4 +62,51 @@ public class DishServiceImpl implements DishService {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 菜品分页查询
|
||||
* @param dishPageQueryDTO
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
|
||||
|
||||
PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
|
||||
Page<DishVO> page = dishMapper.pageQuery(dishPageQueryDTO);
|
||||
return new PageResult(page.getTotal(), page.getResult());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 菜品批量删除
|
||||
* @param ids
|
||||
*/
|
||||
@Override
|
||||
@Transactional //涉及多个表的数据操作 开启事务注解 保持数据的一致性
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
//判断当前菜品是否能够被删除 是否被套餐关联
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,4 +9,21 @@
|
||||
values
|
||||
(#{name}, #{categoryId}, #{price}, #{image}, #{description}, #{createTime}, #{updateTime}, #{createUser}, #{updateUser}, #{status})
|
||||
</insert>
|
||||
|
||||
|
||||
<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>
|
||||
</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="dishIds" item="dishId" separator="," open="(" close=")">
|
||||
#{dishId}
|
||||
</foreach>
|
||||
|
||||
</select>
|
||||
</mapper>
|
||||
Reference in New Issue
Block a user