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 71654ed commit 6e494c4
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public R<String> delete(@RequestParam List<Long> ids) {
*/
@GetMapping("/{id}")
@ApiOperation("根据id查询菜品")
public R<DishVO> select(@PathVariable Long id){
public R<DishVO> select(@PathVariable Long id) {
log.info("根据id查询菜品:{}", id);
DishVO dishVO = dishService.getByIdWithFlavor(id);
return R.success(dishVO);
Expand All @@ -89,15 +89,26 @@ public R<DishVO> select(@PathVariable Long id){
*/
@PutMapping
@ApiOperation("修改菜品")
public R<String> update(@RequestBody DishDTO dishDTO){
public R<String> update(@RequestBody DishDTO dishDTO) {
log.info("修改菜品:{}", dishDTO);
dishService.updateWithFlavor(dishDTO);
return R.success("修改成功");
}




/**
* 菜品起售、停售
*
* @param status 菜品状态
* @param id 菜品id
* @return success
*/
@PostMapping("/status/{status}")
@ApiOperation("菜品起售、停售")
public R<String> startOrStop(@PathVariable Integer status, Long id) {
log.info("菜品起售、停售---status:{},id:{}", status, id);
dishService.startOrStop(status, id);
return R.success("状态修改成功");
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ public interface DishMapper {
//更新菜品数据
@AutoFill(type = AutoFillConstant.UPDATE)
void update(Dish dish);

}
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
package com.reggie.mapper;

import com.reggie.annotation.AutoFill;
import com.reggie.constant.AutoFillConstant;
import com.reggie.entity.Setmeal;
import com.reggie.entity.SetmealDish;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

@Mapper
public interface SetmealMapper {

//根据分类id查询对应的套餐数量
Long cuntByCategoryId(Long id);

//根据套餐id更新套餐数据
@AutoFill(type = AutoFillConstant.UPDATE)
void updatesByIds(Setmeal setmeal);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,7 @@ public interface DishService {

//修改菜品
void updateWithFlavor(DishDTO dishDTO);

//菜品起售、停售
void startOrStop(Integer status, Long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.reggie.dto.DishPageQueryDTO;
import com.reggie.entity.Dish;
import com.reggie.entity.DishFlavor;
import com.reggie.entity.Setmeal;
import com.reggie.entity.SetmealDish;
import com.reggie.exception.DeletionNotAllowedException;
import com.reggie.mapper.DishFlavorMapper;
Expand All @@ -23,7 +24,11 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Semaphore;

/**
* 菜品业务实现
Expand All @@ -39,6 +44,9 @@ public class DishServiceImpl implements DishService {
@Autowired
private SetmealDishMapper setmealDishMapper; //套餐菜品绑定DAO

@Autowired
private SetmealMapper setmealMapper; //套餐DAO

/**
* 添加菜品
*
Expand Down Expand Up @@ -131,6 +139,7 @@ public DishVO getByIdWithFlavor(Long id) {
*
* @param dishDTO 修改的菜品数据
*/
@Transactional
public void updateWithFlavor(DishDTO dishDTO) {
//拷贝菜品数据
Dish dish = new Dish();
Expand All @@ -148,13 +157,47 @@ public void updateWithFlavor(DishDTO dishDTO) {
List<DishFlavor> flavors = dishDTO.getFlavors();
if (flavors != null && flavors.size() > 0) {
//为口味绑定菜品id
flavors.forEach( flavor->{
flavors.forEach(flavor -> {
flavor.setDishId(dishId);
});
//插入新的口味数据
dishFlavorMapper.insert(flavors);
}

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

/**
* 菜品起售、停售
*
* @param status 菜品状态
* @param id 菜品id
*/
@Transactional
public void startOrStop(Integer status, Long id) {
Dish dish = Dish.builder()
.id(id)
.status(status)
.build();
//更新菜品状态
dishMapper.update(dish);

//如果菜品状态为停售相关的套餐也得停售
if (StatusConstant.DISABLE == status) {
List<Long> ids = new ArrayList<>();
ids.add(id);

//通过id查询套餐如果有多个都将停售
List<Long> setmealIds = setmealDishMapper.getSetmealIdsByDishIds(ids);
if (setmealIds != null && setmealIds.size() > 0) {

Setmeal setmeal = new Setmeal();
setmeal.setStatus(StatusConstant.DISABLE);
//循环停售套餐
for (Long setmealId : setmealIds) {
setmeal.setId(setmealId);
setmealMapper.updatesByIds(setmeal);
}
}
}
}
}
1 change: 1 addition & 0 deletions reggie_server/src/main/resources/mapper/DishMapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,5 @@
</set>
where id = #{id}
</update>

</mapper>
32 changes: 32 additions & 0 deletions reggie_server/src/main/resources/mapper/SetmealMapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,36 @@
where category_id = #{id}
</select>

<!--根据套餐id更新套餐数据-->
<update id="updatesByIds">
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="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 6e494c4

Please sign in to comment.