Skip to content

Commit

Permalink
菜品修改
Browse files Browse the repository at this point in the history
  • Loading branch information
Q-1515 committed Jul 22, 2022
1 parent 4584d27 commit 71654ed
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.reggie.result.PageResult;
import com.reggie.result.R;
import com.reggie.service.DishService;
import com.reggie.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -66,4 +67,37 @@ public R<String> delete(@RequestParam List<Long> ids) {
return R.success("批量删除菜品成功");
}

/**
* 根据id查询菜品和关联的口味
*
* @param id 菜品id
* @return DishVO 菜品的参数
*/
@GetMapping("/{id}")
@ApiOperation("根据id查询菜品")
public R<DishVO> select(@PathVariable Long id){
log.info("根据id查询菜品:{}", id);
DishVO dishVO = dishService.getByIdWithFlavor(id);
return R.success(dishVO);
}

/**
* 修改菜品
*
* @param dishDTO 修改的菜品数据
* @return success
*/
@PutMapping
@ApiOperation("修改菜品")
public R<String> update(@RequestBody DishDTO dishDTO){
log.info("修改菜品:{}", dishDTO);
dishService.updateWithFlavor(dishDTO);
return R.success("修改成功");
}






}
8 changes: 6 additions & 2 deletions reggie_server/src/main/java/com/reggie/mapper/DishMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public interface DishMapper {
Long cuntByCategoryId(Long id);

@AutoFill(type = AutoFillConstant.INSERT)
//插入数据到菜品表
//插入数据到菜品表
void insert(Dish dish);

//菜品信息分页查询
Expand All @@ -27,6 +27,10 @@ public interface DishMapper {
//根据id删除菜品
void deleteById(Long dishId);

//根据id查询菜品和关联的口味
DishVO getByIdWithFlavor(Long id);


//更新菜品数据
@AutoFill(type = AutoFillConstant.UPDATE)
void update(Dish dish);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.reggie.dto.DishDTO;
import com.reggie.dto.DishPageQueryDTO;
import com.reggie.result.PageResult;
import com.reggie.vo.DishVO;

import java.util.List;

Expand All @@ -16,4 +17,10 @@ public interface DishService {

//批量删除菜品
void delete(List<Long> ids);

//根据id查询菜品和关联的口味
DishVO getByIdWithFlavor(Long id);

//修改菜品
void updateWithFlavor(DishDTO dishDTO);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.reggie.constant.MessageConstant;
import com.reggie.constant.StatusConstant;
import com.reggie.dto.DishDTO;
import com.reggie.dto.DishPageQueryDTO;
import com.reggie.entity.Dish;
Expand Down Expand Up @@ -93,9 +94,8 @@ public void delete(List<Long> ids) {
//判断所有菜品是否是启用的
ids.forEach(dishId -> {
Dish dish = dishMapper.selectById(dishId);

//如果菜品起售丢出异常
if (dish.getStatus() == 1){
// 起售的菜品不能删除
if (StatusConstant.DISABLE == dish.getStatus()) {
throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
}
});
Expand All @@ -104,18 +104,57 @@ public void delete(List<Long> ids) {
List<Long> setmealId = setmealDishMapper.getSetmealIdsByDishIds(ids);

//如果绑定了套餐则丢出异常
if (setmealId != null && setmealId.size() > 0){
if (setmealId != null && setmealId.size() > 0) {
throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
}

ids.forEach(dishId ->{
ids.forEach(dishId -> {
// 删除菜品表中的数据
dishMapper.deleteById(dishId);
dishMapper.deleteById(dishId);
// 删除菜品口味表中的数据
dishFlavorMapper.deleteByDishId(dishId);
dishFlavorMapper.deleteByDishId(dishId);
});
}

/**
* 根据id查询菜品和关联的口味
*
* @param id 菜品id
* @return DishVO 菜品的参数
*/
public DishVO getByIdWithFlavor(Long id) {
return dishMapper.getByIdWithFlavor(id);
}

/**
* 修改菜品
*
* @param dishDTO 修改的菜品数据
*/
public void updateWithFlavor(DishDTO dishDTO) {
//拷贝菜品数据
Dish dish = new Dish();
BeanUtils.copyProperties(dishDTO, dish);

//更新菜品数据
dishMapper.update(dish);

//获取菜品id
Long dishId = dishDTO.getId();
//根据菜品id删除口味
dishFlavorMapper.deleteByDishId(dishId);

//获取新的口味数据
List<DishFlavor> flavors = dishDTO.getFlavors();
if (flavors != null && flavors.size() > 0) {
//为口味绑定菜品id
flavors.forEach( flavor->{
flavor.setDishId(dishId);
});
}

//插入新的口味数据
dishFlavorMapper.insert(flavors);

}
}
6 changes: 5 additions & 1 deletion reggie_server/src/main/resources/mapper/DishFlavorMapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@

<!--删除菜品口味表中的数据-->
<delete id="deleteByDishId">
delete from dish_flavor where dish_id =#{dishId}
delete
from dish_flavor
where dish_id = #{dishId}
</delete>


</mapper>
49 changes: 49 additions & 0 deletions reggie_server/src/main/resources/mapper/DishMapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,53 @@
from dish
where id = #{dishId}
</delete>

<!--菜品vo对象 注入 关联的口味对象-->
<resultMap id="DishVOMap" type="com.reggie.vo.DishVO" autoMapping="true">
<id column="id" property="id"/>
<collection property="flavors" resultMap="DishFlavorMap"/>
</resultMap>
<resultMap id="DishFlavorMap" type="com.reggie.entity.DishFlavor" autoMapping="true">
<result property="name" column="dfname"/>
</resultMap>

<!--根据id查询菜品和关联的口味-->
<select id="getByIdWithFlavor" resultMap="DishVOMap">
select dish.*, df.name as dfname, df.value
from dish
left join dish_flavor df on dish.id = df.dish_id
where dish.id = #{id}
</select>

<!--更新菜品数据-->
<update id="update">
update dish
<set>
<if test="categoryId != null">
category_id = #{categoryId},
</if>
<if test="name != null">
name = #{name},
</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>
</mapper>

0 comments on commit 71654ed

Please sign in to comment.