修改菜品
This commit is contained in:
@@ -5,6 +5,7 @@ import com.sky.dto.DishPageQueryDTO;
|
|||||||
import com.sky.result.PageResult;
|
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;
|
||||||
@@ -62,4 +63,30 @@ public class DishController {
|
|||||||
dishService.deleteBatch(ids);
|
dishService.deleteBatch(ids);
|
||||||
return Result.success();
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ 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.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;
|
||||||
|
|
||||||
@@ -20,4 +21,13 @@ public interface DishFlavorMapper {
|
|||||||
*/
|
*/
|
||||||
@Delete("delete from dish_flavor where dish_id = #{dishId}")
|
@Delete("delete from dish_flavor where dish_id = #{dishId}")
|
||||||
void deleteByDishId(Long dishId);
|
void deleteByDishId(Long dishId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据菜品id集合批量删除口味数据
|
||||||
|
* @param dishIds
|
||||||
|
*/
|
||||||
|
void deleteByDishIds(List<Long> dishIds);
|
||||||
|
|
||||||
|
@Select("select * from dish_flavor where dish_id = #{dishId}")
|
||||||
|
List<DishFlavor> getByDishId(Long dishId);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,8 @@ 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;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@Mapper
|
@Mapper
|
||||||
public interface DishMapper {
|
public interface DishMapper {
|
||||||
|
|
||||||
@@ -50,4 +52,13 @@ public interface DishMapper {
|
|||||||
*/
|
*/
|
||||||
@Delete("delete from dish where id = #{id}")
|
@Delete("delete from dish where id = #{id}")
|
||||||
void deleteById(Long id);
|
void deleteById(Long id);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据菜品ids集合批量删除菜品
|
||||||
|
* @param ids
|
||||||
|
*/
|
||||||
|
void deleteByIds(List<Long> ids);
|
||||||
|
|
||||||
|
@AutoFill(OperationType.UPDATE)
|
||||||
|
void update(Dish dish);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package com.sky.service;
|
|||||||
import com.sky.dto.DishDTO;
|
import com.sky.dto.DishDTO;
|
||||||
import com.sky.dto.DishPageQueryDTO;
|
import com.sky.dto.DishPageQueryDTO;
|
||||||
import com.sky.result.PageResult;
|
import com.sky.result.PageResult;
|
||||||
|
import com.sky.vo.DishVO;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -28,4 +29,17 @@ public interface DishService {
|
|||||||
* @param ids
|
* @param ids
|
||||||
*/
|
*/
|
||||||
void deleteBatch(List<Long> ids);
|
void deleteBatch(List<Long> ids);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据id查询菜品及口味
|
||||||
|
* @param id
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
DishVO getByIdWithFlavor(Long id);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 修改菜品基本信息(及口味)
|
||||||
|
* @param dishDTO
|
||||||
|
*/
|
||||||
|
void updateWithFlavor(DishDTO dishDTO);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -64,6 +64,7 @@ public class DishServiceImpl implements DishService {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 菜品分页查询
|
* 菜品分页查询
|
||||||
|
*
|
||||||
* @param pageQueryDTO
|
* @param pageQueryDTO
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@@ -72,11 +73,12 @@ public class DishServiceImpl implements DishService {
|
|||||||
//设置分页
|
//设置分页
|
||||||
PageHelper.startPage(pageQueryDTO.getPage(), pageQueryDTO.getPageSize());
|
PageHelper.startPage(pageQueryDTO.getPage(), pageQueryDTO.getPageSize());
|
||||||
Page<DishVO> page = dishMapper.pageQuery(pageQueryDTO);
|
Page<DishVO> page = dishMapper.pageQuery(pageQueryDTO);
|
||||||
return new PageResult(page.getTotal(),page.getResult());
|
return new PageResult(page.getTotal(), page.getResult());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 菜品批量删除
|
* 菜品批量删除
|
||||||
|
*
|
||||||
* @param ids
|
* @param ids
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
@@ -86,20 +88,69 @@ public class DishServiceImpl implements DishService {
|
|||||||
//是否有起售中菜品
|
//是否有起售中菜品
|
||||||
for (Long id : ids) {
|
for (Long id : ids) {
|
||||||
Dish dish = dishMapper.selectById(id);
|
Dish dish = dishMapper.selectById(id);
|
||||||
if (dish.getStatus().equals(StatusConstant.ENABLE)){
|
if (dish.getStatus().equals(StatusConstant.ENABLE)) {
|
||||||
throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
|
throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//是否关联套餐
|
//是否关联套餐
|
||||||
List<Long> setmealIds = setmealDishMapper.getSetmealIdsByDishIds(ids);
|
List<Long> setmealIds = setmealDishMapper.getSetmealIdsByDishIds(ids);
|
||||||
if (setmealIds != null && !setmealIds.isEmpty()){
|
if (setmealIds != null && !setmealIds.isEmpty()) {
|
||||||
throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
|
throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
|
||||||
}
|
}
|
||||||
//删除菜品
|
//删除菜品
|
||||||
for (Long id : ids) {
|
/*for (Long id : ids) {
|
||||||
dishMapper.deleteById(id);
|
dishMapper.deleteById(id);
|
||||||
//删除菜品口味
|
//删除菜品口味
|
||||||
dishFlavorMapper.deleteByDishId(id);
|
dishFlavorMapper.deleteByDishId(id);
|
||||||
|
}*/
|
||||||
|
dishMapper.deleteByIds(ids);
|
||||||
|
dishFlavorMapper.deleteByDishIds(ids);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据id查询菜品及口味
|
||||||
|
*
|
||||||
|
* @param id
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public DishVO getByIdWithFlavor(Long id) {
|
||||||
|
//根据id查询菜品
|
||||||
|
Dish dish = dishMapper.selectById(id);
|
||||||
|
|
||||||
|
//根据菜品id查询口味
|
||||||
|
List<DishFlavor> dishFlavors = dishFlavorMapper.getByDishId(id);
|
||||||
|
|
||||||
|
//封装到DishVO对象
|
||||||
|
DishVO dishVO = new DishVO();
|
||||||
|
BeanUtils.copyProperties(dish, dishVO);
|
||||||
|
dishVO.setFlavors(dishFlavors);
|
||||||
|
|
||||||
|
return dishVO;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 修改菜品基本信息(及口味)
|
||||||
|
*
|
||||||
|
* @param dishDTO
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void updateWithFlavor(DishDTO dishDTO) {
|
||||||
|
//修改菜品基本信息
|
||||||
|
Dish dish = new Dish();
|
||||||
|
BeanUtils.copyProperties(dishDTO, dish);
|
||||||
|
dishMapper.update(dish);
|
||||||
|
|
||||||
|
//删除原有口味数据
|
||||||
|
dishFlavorMapper.deleteByDishId(dishDTO.getId());
|
||||||
|
|
||||||
|
//重新插入新的口味数据
|
||||||
|
List<DishFlavor> flavors = dishDTO.getFlavors();
|
||||||
|
if (flavors != null && !flavors.isEmpty()) {
|
||||||
|
//设置dishId
|
||||||
|
flavors.forEach(dishFlavor -> dishFlavor.setDishId(dish.getId()));
|
||||||
|
//插入新的口味数据
|
||||||
|
dishFlavorMapper.insertBatch(flavors);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,4 +10,10 @@
|
|||||||
(#{df.dishId},#{df.name},#{df.value})
|
(#{df.dishId},#{df.name},#{df.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>
|
||||||
@@ -10,6 +10,26 @@
|
|||||||
values (#{name}, #{categoryId}, #{price}, #{image}, #{description}, #{status}, #{createTime}, #{updateTime},
|
values (#{name}, #{categoryId}, #{price}, #{image}, #{description}, #{status}, #{createTime}, #{updateTime},
|
||||||
#{createUser}, #{updateUser})
|
#{createUser}, #{updateUser})
|
||||||
</insert>
|
</insert>
|
||||||
|
<update id="update">
|
||||||
|
update dish
|
||||||
|
<set>
|
||||||
|
<if test="name != null">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" separator="," open="(" close=")">
|
||||||
|
#{id}
|
||||||
|
</foreach>
|
||||||
|
</delete>
|
||||||
<select id="pageQuery" resultType="com.sky.vo.DishVO">
|
<select id="pageQuery" resultType="com.sky.vo.DishVO">
|
||||||
select dish.*, category.name as categoryName
|
select dish.*, category.name as categoryName
|
||||||
from dish
|
from dish
|
||||||
|
|||||||
Reference in New Issue
Block a user