修改菜品
This commit is contained in:
@@ -5,6 +5,7 @@ import com.sky.dto.DishPageQueryDTO;
|
||||
import com.sky.result.PageResult;
|
||||
import com.sky.result.Result;
|
||||
import com.sky.service.DishService;
|
||||
import com.sky.vo.DishVO;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -62,4 +63,30 @@ public class DishController {
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.sky.mapper;
|
||||
import com.sky.entity.DishFlavor;
|
||||
import org.apache.ibatis.annotations.Delete;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -20,4 +21,13 @@ public interface DishFlavorMapper {
|
||||
*/
|
||||
@Delete("delete from dish_flavor where dish_id = #{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.Select;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Mapper
|
||||
public interface DishMapper {
|
||||
|
||||
@@ -50,4 +52,13 @@ public interface DishMapper {
|
||||
*/
|
||||
@Delete("delete from dish where id = #{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.DishPageQueryDTO;
|
||||
import com.sky.result.PageResult;
|
||||
import com.sky.vo.DishVO;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
@@ -28,4 +29,17 @@ public interface DishService {
|
||||
* @param 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
|
||||
* @return
|
||||
*/
|
||||
@@ -77,6 +78,7 @@ public class DishServiceImpl implements DishService {
|
||||
|
||||
/**
|
||||
* 菜品批量删除
|
||||
*
|
||||
* @param ids
|
||||
*/
|
||||
@Override
|
||||
@@ -96,10 +98,59 @@ public class DishServiceImpl implements DishService {
|
||||
throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
|
||||
}
|
||||
//删除菜品
|
||||
for (Long id : ids) {
|
||||
/*for (Long id : ids) {
|
||||
dishMapper.deleteById(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})
|
||||
</foreach>
|
||||
</insert>
|
||||
<delete id="deleteByDishIds">
|
||||
delete from dish_flavor where dish_id in
|
||||
<foreach collection="dishIds" item="dishId" separator="," open="(" close=")">
|
||||
#{dishId}
|
||||
</foreach>
|
||||
</delete>
|
||||
</mapper>
|
||||
@@ -10,6 +10,26 @@
|
||||
values (#{name}, #{categoryId}, #{price}, #{image}, #{description}, #{status}, #{createTime}, #{updateTime},
|
||||
#{createUser}, #{updateUser})
|
||||
</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 dish.*, category.name as categoryName
|
||||
from dish
|
||||
|
||||
Reference in New Issue
Block a user