完成菜品管理功能
This commit is contained in:
parent
fe98f53a71
commit
73a57857e0
@ -1,16 +1,18 @@
|
|||||||
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 com.sky.vo.DishVO;
|
||||||
import io.swagger.annotations.Api;
|
import io.swagger.annotations.Api;
|
||||||
import io.swagger.annotations.ApiOperation;
|
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;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 菜品管理
|
* 菜品管理
|
||||||
@ -37,4 +39,56 @@ public class DishController {
|
|||||||
|
|
||||||
return Result.success();
|
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) {
|
||||||
|
log.info("菜品批量删除:{}", ids);
|
||||||
|
dishService.deleteBatch(ids);
|
||||||
|
return Result.success();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据ID查询菜品
|
||||||
|
* @param id
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@GetMapping("/{id}")
|
||||||
|
@ApiOperation("根据ID查询菜品")
|
||||||
|
public Result<DishVO> getById(@PathVariable Long id) {
|
||||||
|
log.info("根据ID查询菜品:{}", id);
|
||||||
|
DishVO dishVO = dishService.getByIdWithFlavor(id);
|
||||||
|
return Result.success(dishVO);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 更新菜品
|
||||||
|
* @param dishDTO
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@PutMapping
|
||||||
|
@ApiOperation("更新菜品")
|
||||||
|
public Result update(@RequestBody DishDTO dishDTO) {
|
||||||
|
log.info("更新菜品:{}", dishDTO);
|
||||||
|
dishService.updateWithFlavor(dishDTO);
|
||||||
|
return Result.success();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
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 org.apache.ibatis.annotations.Select;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -13,4 +15,25 @@ 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);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据菜品id列表批量删除口味数据
|
||||||
|
* @param dishIds
|
||||||
|
*/
|
||||||
|
void deleteByDishIds(List<Long> dishIds);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据菜品id获取口味数据
|
||||||
|
* @param dishId
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Select("select * from dish_flavor where dish_id = #{dishId}")
|
||||||
|
List<DishFlavor> getByDishId(Long dishId);
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,17 @@
|
|||||||
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.Mapper;
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
import org.apache.ibatis.annotations.Select;
|
import org.apache.ibatis.annotations.Select;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@Mapper
|
@Mapper
|
||||||
public interface DishMapper {
|
public interface DishMapper {
|
||||||
|
|
||||||
@ -23,4 +29,39 @@ public interface DishMapper {
|
|||||||
*/
|
*/
|
||||||
@AutoFill(value = OperationType.INSERT)
|
@AutoFill(value = OperationType.INSERT)
|
||||||
void insert(Dish dish);
|
void insert(Dish dish);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 菜品分页查询
|
||||||
|
* @param dishPageQueryDTO
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
Page<DishVO> pageQuery(DishPageQueryDTO dishPageQueryDTO);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据id查询菜品
|
||||||
|
* @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);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据id集合批量删除菜品
|
||||||
|
* @param ids
|
||||||
|
*/
|
||||||
|
void deleteByIds(List<Long> ids);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 更新菜品
|
||||||
|
* @param dish
|
||||||
|
*/
|
||||||
|
@AutoFill(value = OperationType.UPDATE)
|
||||||
|
void update(Dish dish);
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,16 @@
|
|||||||
|
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,11 @@
|
|||||||
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 com.sky.vo.DishVO;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public interface DishService {
|
public interface DishService {
|
||||||
|
|
||||||
@ -10,4 +15,29 @@ public interface DishService {
|
|||||||
*/
|
*/
|
||||||
void saveWithFlavor(DishDTO dishDTO);
|
void saveWithFlavor(DishDTO dishDTO);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 分页查询菜品
|
||||||
|
* @param dishPageQueryDTO
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 菜品批量删除
|
||||||
|
* @param ids
|
||||||
|
*/
|
||||||
|
void deleteBatch(List<Long> ids);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据id查询菜品和对应的口味
|
||||||
|
* @param id
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
DishVO getByIdWithFlavor(Long id);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 更新菜品和对应的口味
|
||||||
|
* @param dishDTO
|
||||||
|
*/
|
||||||
|
void updateWithFlavor(DishDTO dishDTO);
|
||||||
}
|
}
|
||||||
|
@ -1,16 +1,27 @@
|
|||||||
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.result.Result;
|
||||||
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;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -24,8 +35,12 @@ public class DishServiceImpl implements DishService {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private DishFlavorMapper dishFlavorMapper;
|
private DishFlavorMapper dishFlavorMapper;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private SetmealDishMapper setmealDishMapper;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 新增菜品和对应的口味
|
* 新增菜品和对应的口味
|
||||||
|
*
|
||||||
* @param dishDTO
|
* @param dishDTO
|
||||||
*/
|
*/
|
||||||
@Transactional
|
@Transactional
|
||||||
@ -42,7 +57,7 @@ public class DishServiceImpl implements DishService {
|
|||||||
|
|
||||||
|
|
||||||
List<DishFlavor> flavors = dishDTO.getFlavors();
|
List<DishFlavor> flavors = dishDTO.getFlavors();
|
||||||
if (flavors!= null && flavors.size() > 0) {
|
if (flavors != null && flavors.size() > 0) {
|
||||||
//遍历口味列表,设置对应的菜品id
|
//遍历口味列表,设置对应的菜品id
|
||||||
flavors.forEach(flavor -> {
|
flavors.forEach(flavor -> {
|
||||||
flavor.setDishId(dishId);
|
flavor.setDishId(dishId);
|
||||||
@ -52,4 +67,107 @@ public class DishServiceImpl implements DishService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 分页查询菜品列表
|
||||||
|
*
|
||||||
|
* @param dishPageQueryDTO
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
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
|
||||||
|
*/
|
||||||
|
@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);
|
||||||
|
// }
|
||||||
|
//优化版:只需两次sql语句
|
||||||
|
|
||||||
|
//根据菜品id集合批量删除菜品数据
|
||||||
|
dishMapper.deleteByIds(ids);
|
||||||
|
|
||||||
|
//根据菜品id集合批量删除菜品口味数据
|
||||||
|
dishFlavorMapper.deleteByDishIds(ids);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据id查询菜品和对应的口味
|
||||||
|
*
|
||||||
|
* @param id
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public DishVO getByIdWithFlavor(Long id) {
|
||||||
|
//根据id查询菜品信息
|
||||||
|
Dish dish = dishMapper.getById(id);
|
||||||
|
|
||||||
|
//根据id查询菜品口味信息
|
||||||
|
List<DishFlavor> flavors = dishFlavorMapper.getByDishId(id);
|
||||||
|
|
||||||
|
//封装成VO对象并返回
|
||||||
|
DishVO dishVO = new DishVO();
|
||||||
|
BeanUtils.copyProperties(dish, dishVO);
|
||||||
|
dishVO.setFlavors(flavors);
|
||||||
|
|
||||||
|
return dishVO;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 更新菜品和对应的口味
|
||||||
|
*
|
||||||
|
* @param dishDTO
|
||||||
|
*/
|
||||||
|
@Transactional
|
||||||
|
public void updateWithFlavor(DishDTO dishDTO) {
|
||||||
|
Dish dish = new Dish();
|
||||||
|
BeanUtils.copyProperties(dishDTO, dish);
|
||||||
|
|
||||||
|
//修改菜品表的数据
|
||||||
|
dishMapper.update(dish);
|
||||||
|
|
||||||
|
//根据菜品id删除原有的口味数据
|
||||||
|
dishFlavorMapper.deleteByDishId(dishDTO.getId());
|
||||||
|
|
||||||
|
//重新插入新的口味数据
|
||||||
|
List<DishFlavor> flavors = dishDTO.getFlavors();
|
||||||
|
if (flavors != null && flavors.size() > 0) {
|
||||||
|
//遍历口味列表,设置对应的菜品id
|
||||||
|
flavors.forEach(flavor -> {
|
||||||
|
flavor.setDishId(dishDTO.getId());
|
||||||
|
});
|
||||||
|
//向口味表查入n条数据
|
||||||
|
dishFlavorMapper.insertBatch(flavors);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,4 +9,11 @@
|
|||||||
(#{flavor.dishId}, #{flavor.name}, #{flavor.value})
|
(#{flavor.dishId}, #{flavor.name}, #{flavor.value})
|
||||||
</foreach>
|
</foreach>
|
||||||
</insert>
|
</insert>
|
||||||
|
|
||||||
|
<delete id="deleteByDishIds">
|
||||||
|
delete from dish_flavor where dish_id in
|
||||||
|
<foreach collection="dishIds" item="dishId" separator="," open="(" close=")">
|
||||||
|
#{dishId}
|
||||||
|
</foreach>
|
||||||
|
</delete>
|
||||||
</mapper>
|
</mapper>
|
||||||
|
@ -8,4 +8,42 @@
|
|||||||
insert into dish (name, category_id, price, image, description, status, create_time, update_time, create_user, update_user)
|
insert into dish (name, category_id, price, image, description, status, create_time, update_time, create_user, update_user)
|
||||||
values (#{name}, #{categoryId}, #{price}, #{image}, #{description}, #{status}, #{createTime}, #{updateTime}, #{createUser}, #{updateUser})
|
values (#{name}, #{categoryId}, #{price}, #{image}, #{description}, #{status}, #{createTime}, #{updateTime}, #{createUser}, #{updateUser})
|
||||||
</insert>
|
</insert>
|
||||||
|
|
||||||
|
<update id="update">
|
||||||
|
update dish
|
||||||
|
<set>
|
||||||
|
<if test="name!= null and name!= ''">name = #{name},</if>
|
||||||
|
<if test="categoryId!= null">category_id = #{categoryId},</if>
|
||||||
|
<if test="price!= null">price = #{price},</if>
|
||||||
|
<if test="image!= null">image = #{image},</if>
|
||||||
|
<if test="description!= null">description = #{description},</if>
|
||||||
|
<if test="status!= null">status = #{status},</if>
|
||||||
|
<if test="updateTime!= null">update_time = #{updateTime},</if>
|
||||||
|
<if test="updateUser!= null">update_user = #{updateUser},</if>
|
||||||
|
</set>
|
||||||
|
where id = #{id}
|
||||||
|
</update>
|
||||||
|
|
||||||
|
<delete id="deleteByIds">
|
||||||
|
delete from dish where id in
|
||||||
|
<foreach collection="ids" item="id" open="(" separator="," close=")">
|
||||||
|
#{id}
|
||||||
|
</foreach>
|
||||||
|
</delete>
|
||||||
|
|
||||||
|
<select id="pageQuery" resultType="com.sky.vo.DishVO">
|
||||||
|
select d.*, c.name as category_name from dish d left join category c on d.category_id = c.id
|
||||||
|
<where>
|
||||||
|
<if test="name!= null and name!= ''">
|
||||||
|
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>
|
</mapper>
|
||||||
|
12
sky-server/src/main/resources/mapper/SetmealDishMapper.xml
Normal file
12
sky-server/src/main/resources/mapper/SetmealDishMapper.xml
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
<?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" open="(" separator="," close=")">
|
||||||
|
#{dishId}
|
||||||
|
</foreach>
|
||||||
|
</select>
|
||||||
|
</mapper>
|
Loading…
Reference in New Issue
Block a user