套餐管理为项目实战,大部分的代码和菜品管理如出一辙,这里主要是对黑马给出的答案文档作出补充.
在对照答案文档的时候发现了以下方面需要做出补充:
在编写代码时候发现没有对集合进行非空判断,故做出补充
补充代码:
/**
* 修改套餐
*
* @param setmealDTO
* @return
*/
@Transactional
public void update(SetmealDTO setmealDTO) {
Setmeal setmeal = new Setmeal();
BeanUtils.copyProperties(setmealDTO, setmeal);
setmealMapper.update(setmeal);
Long setmealId = setmealDTO.getId();
setmealDishMapper.deleteBySetmealId(setmealId);
//设置SetmealDish中的套餐id
List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
if (setmealDishes != null && setmealDishes.size() > 0) {
setmealDishes.forEach(setmealDish -> {
setmealDish.setSetmealId(setmealId);
});
setmealDishMapper.insertBatch(setmealDishes);
}
}
@AutoFill(value = OperationType.UPDATE)
void update(Setmeal setmeal);
<update id="update" parameterType="setmeal">
update setmeal
<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="status != null">
status = #{status},
</if>
<if test="description != null">
description = #{description},
</if>
<if test="image != null">
image = #{image},
</if>
<if test="updateTime != null">
update_time = #{updateTime},
</if>
<if test="updateUser != null">
update_user = #{updateUser},
</if>
</set>
where id = #{id}
</update>
在写完起售停售套餐后发现答案和实现的不一致,在前后端联调测试后发现想要达到的效果一致
/**
* 套餐起售停售
*
* @param status
* @param id
*/
public void startOrStop(Integer status, Long id) {
//起售套餐时,如果套餐内包含停售的菜品,则不能起售
if (status == StatusConstant.ENABLE) {
List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(id);
if (setmealDishes != null && setmealDishes.size() > 0) {
setmealDishes.forEach(setmealDish -> {
Long dishId = setmealDish.getDishId();
Dish dish = dishMapper.getById(dishId);
if (dish.getStatus() == StatusConstant.DISABLE) {
throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
}
});
}
}
Setmeal setmeal = Setmeal.builder()
.id(id)
.status(status)
.build();
setmealMapper.update(setmeal);
}
代码实现了功能,免去了sql语句
黑马原代码:
/**
* 套餐起售、停售
* @param status
* @param id
*/
public void startOrStop(Integer status, Long id) {
//起售套餐时,判断套餐内是否有停售菜品,有停售菜品提示"套餐内包含未启售菜品,无法启售"
if(status == StatusConstant.ENABLE){
//select a.* from dish a left join setmeal_dish b on a.id = b.dish_id where b.setmeal_id = ?
List<Dish> dishList = dishMapper.getBySetmealId(id);
if(dishList != null && dishList.size() > 0){
dishList.forEach(dish -> {
if(StatusConstant.DISABLE == dish.getStatus()){
throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
}
});
}
}
Setmeal setmeal = Setmeal.builder()
.id(id)
.status(status)
.build();
setmealMapper.update(setmeal);
}
DishMapper类:
/**
* 根据套餐id查询菜品
* @param setmealId
* @return
*/
@Select("select a.* from dish a left join setmeal_dish b on a.id = b.dish_id where b.setmeal_id = #{setmealId}")
List<Dish> getBySetmealId(Long setmealId);
写完套餐管理发现黑马没有对起售停售菜品进行实现,故做出补充
补充代码
/**
* 菜品的起售和停售
* @param status
* @param id
* @return
*/
@PostMapping("status/{status}")
@ApiOperation("菜品的起售和停售")
public Result<String> startOrStop(@PathVariable Integer status,Long id){
dishService.startOrStop(status,id);
return Result.success();
}
/**
* 菜品的起售和停售
* @param status
* @param id
*/
void startOrStop(Integer status, Long id);
/**
* 菜品的起售和停售
*
* @param status
* @param id
*/
@Transactional
public void startOrStop(Integer status, Long id) {
List<Long> dishId = new ArrayList<Long>();
dishId.add(id);
//菜品停售,则包含菜品的套餐同时停售
if (status == StatusConstant.DISABLE) {
List<Long> setmealIds = setmealDishMapper.getSetmealIdsByDishIds(dishId);
if (setmealIds != null && setmealIds.size() > 0) {
setmealIds.forEach(Id -> {
Setmeal setmeal = Setmeal.builder()
.id(Id)
.status(status)
.build();
setmealMapper.update(setmeal);
});
}
}
Dish dish=Dish.builder()
.id(id)
.status(status)
.build();
dishMapper.update(dish);
}
最后进行前后端联调测试,发现没有问题(别忘了提交代码到git仓库)