套餐相关接口

This commit is contained in:
slhaf
2024-09-13 13:18:22 +08:00
parent 1e2aec636a
commit add28bfa5d
10 changed files with 161 additions and 7 deletions

View File

@@ -113,6 +113,7 @@ public class DishController {
@PostMapping("/status/{status}") @PostMapping("/status/{status}")
@ApiOperation("菜品起售停售") @ApiOperation("菜品起售停售")
public Result startOrStop(@PathVariable Integer status,Long id){ public Result startOrStop(@PathVariable Integer status,Long id){
log.info("菜品起售停售: 状态:{},ID:{}",status,id);
dishService.startOrStop(status,id); dishService.startOrStop(status,id);
return Result.success(); return Result.success();
} }

View File

@@ -70,8 +70,35 @@ public class SetmealController {
@GetMapping("/{id}") @GetMapping("/{id}")
@ApiOperation("根据id查询套餐") @ApiOperation("根据id查询套餐")
public Result<SetmealVO> getById(@PathVariable Long id){ public Result<SetmealVO> getById(@PathVariable Long id){
log.info("根据id查询套餐"); log.info("根据id查询套餐: {}",id);
SetmealVO setmealVO = setmealService.getByIdWithSetmealDishes(id); SetmealVO setmealVO = setmealService.getByIdWithSetmealDishes(id);
return Result.success(setmealVO); return Result.success(setmealVO);
} }
/**
* 修改套餐
* @param setmealDTO
* @return
*/
@PutMapping
@ApiOperation("修改套餐")
public Result update(@RequestBody SetmealDTO setmealDTO){
log.info("修改套餐: {}",setmealDTO);
setmealService.updateWithSetmealDishes(setmealDTO);
return Result.success();
}
/**
* 套餐起售停售
* @param status
* @param id
* @return
*/
@PostMapping("/status/{status}")
@ApiOperation("套餐起售停售")
public Result startOrStop(@PathVariable Integer status,Long id){
log.info("套餐起售停售: 状态:{},ID:{}",status,id);
setmealService.startOrStop(status,id);
return Result.success();
}
} }

View File

@@ -68,4 +68,6 @@ public interface DishMapper {
*/ */
@Select("select * from dish where category_id = #{categoryId}") @Select("select * from dish where category_id = #{categoryId}")
List<Dish> selectByCategoryId(Long categoryId); List<Dish> selectByCategoryId(Long categoryId);
List<Dish> selectByDishIdsAndStatusDisable(List<Long> dishIds);
} }

View File

@@ -2,6 +2,7 @@ package com.sky.mapper;
import com.sky.entity.Dish; import com.sky.entity.Dish;
import com.sky.entity.SetmealDish; import com.sky.entity.SetmealDish;
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 org.apache.ibatis.annotations.Select;
@@ -22,4 +23,10 @@ public interface SetmealDishMapper {
@Select("select * from setmeal_dish where setmeal_id = #{setmealId}") @Select("select * from setmeal_dish where setmeal_id = #{setmealId}")
List<SetmealDish> selectBySetmealId(Long setmealId); List<SetmealDish> selectBySetmealId(Long setmealId);
@Delete("delete from setmeal_dish where setmeal_id = #{setmealId}")
void deleteBySetmealId(Long setmealId);
@Select("select * from setmeal_dish where dish_id = #{dishId}")
List<SetmealDish> selectByDishId(Long dishId);
} }

View File

@@ -30,4 +30,7 @@ public interface SetmealMapper {
Setmeal selectById(Long id); Setmeal selectById(Long id);
void deleteBatch(List<Long> ids); void deleteBatch(List<Long> ids);
@AutoFill(OperationType.UPDATE)
void update(Setmeal setmeal);
} }

View File

@@ -35,4 +35,17 @@ public interface SetmealService {
* @return * @return
*/ */
SetmealVO getByIdWithSetmealDishes(Long id); SetmealVO getByIdWithSetmealDishes(Long id);
/**
* 修改套餐
* @param setmealDTO
*/
void updateWithSetmealDishes(SetmealDTO setmealDTO);
/**
* 套餐起售停售
* @param status
* @param id
*/
void startOrStop(Integer status, Long id);
} }

View File

@@ -8,10 +8,13 @@ import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO; import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish; import com.sky.entity.Dish;
import com.sky.entity.DishFlavor; import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException; import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper; import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper; import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper; import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult; import com.sky.result.PageResult;
import com.sky.service.DishService; import com.sky.service.DishService;
import com.sky.vo.DishVO; import com.sky.vo.DishVO;
@@ -33,6 +36,8 @@ public class DishServiceImpl implements DishService {
private DishFlavorMapper dishFlavorMapper; private DishFlavorMapper dishFlavorMapper;
@Autowired @Autowired
private SetmealDishMapper setmealDishMapper; private SetmealDishMapper setmealDishMapper;
@Autowired
private SetmealMapper setmealMapper;
/** /**
* 新增菜品 * 新增菜品
@@ -178,7 +183,18 @@ public class DishServiceImpl implements DishService {
.status(status) .status(status)
.build(); .build();
dishMapper.update(dish); dishMapper.update(dish);
//更新菜品所关联套餐的状态 //如果是改为停售,需要同时停售关联套餐
if(status.equals(StatusConstant.DISABLE)) {
//检查是否有关联的套餐
List<SetmealDish> setmealDishes = setmealDishMapper.selectByDishId(id);
if (setmealDishes != null && !setmealDishes.isEmpty()) {
Setmeal setmeal = new Setmeal();
for (SetmealDish setmealDish : setmealDishes) {
setmeal.setId(setmealDish.getSetmealId());
setmeal.setStatus(status);
setmealMapper.update(setmeal);
}
}
}
} }
} }

View File

@@ -10,6 +10,7 @@ import com.sky.entity.Dish;
import com.sky.entity.Setmeal; import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish; import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException; import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper; import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper; import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult; import com.sky.result.PageResult;
@@ -20,6 +21,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List; import java.util.List;
@Service @Service
@@ -29,9 +31,12 @@ public class SetmealServiceImpl implements SetmealService {
private SetmealMapper setmealMapper; private SetmealMapper setmealMapper;
@Autowired @Autowired
private SetmealDishMapper setmealDishMapper; private SetmealDishMapper setmealDishMapper;
@Autowired
private DishMapper dishMapper;
/** /**
* 套餐分页查询 * 套餐分页查询
*
* @param setmealPageQueryDTO * @param setmealPageQueryDTO
* @return * @return
*/ */
@@ -45,6 +50,7 @@ public class SetmealServiceImpl implements SetmealService {
/** /**
* 新增套餐 * 新增套餐
*
* @param setmealDTO * @param setmealDTO
*/ */
@Override @Override
@@ -64,6 +70,7 @@ public class SetmealServiceImpl implements SetmealService {
/** /**
* 批量删除套餐 * 批量删除套餐
*
* @param ids * @param ids
*/ */
@Override @Override
@@ -86,6 +93,7 @@ public class SetmealServiceImpl implements SetmealService {
/** /**
* 根据id查询套餐及关联菜品 * 根据id查询套餐及关联菜品
*
* @param id * @param id
* @return * @return
*/ */
@@ -100,4 +108,60 @@ public class SetmealServiceImpl implements SetmealService {
setmealVO.setSetmealDishes(setmealDishes); setmealVO.setSetmealDishes(setmealDishes);
return setmealVO; return setmealVO;
} }
/**
* 修改套餐
*
* @param setmealDTO
*/
@Override
@Transactional
public void updateWithSetmealDishes(SetmealDTO setmealDTO) {
//更新setmeal基本数据
Setmeal setmeal = new Setmeal();
BeanUtils.copyProperties(setmealDTO, setmeal);
setmealMapper.update(setmeal);
//更新setmeal_dish数据
//删除原setmeal_dish相关数据
setmealDishMapper.deleteBySetmealId(setmealDTO.getId());
//添加setmeal_id
List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealDTO.getId()));
//插入新的数据
setmealDishMapper.insertBatch(setmealDTO.getSetmealDishes());
}
/**
* 套餐起售停售
*
* @param status
* @param id
*/
@Override
public void startOrStop(Integer status, Long id) {
//看是否为起售,如果为起售,需要判断其关联的菜品有停售状态
if (status.equals(StatusConstant.ENABLE)) {
/*
获取其关联的菜品信息--setmeal_dish--setmealDishes--遍历->getDishId
先把dishId放到一个集合中批量查询该dish_id对应且status为0的菜品信息
如果查询到,则抛出删除失败异常
如果没有,则进行下一步,改变状态
*/
List<SetmealDish> setmealDishes = setmealDishMapper.selectBySetmealId(id);
List<Long> dishIds = new ArrayList<>();
setmealDishes.forEach(setmealDish -> dishIds.add(setmealDish.getDishId()));
List<Dish> dishes = dishMapper.selectByDishIdsAndStatusDisable(dishIds);
if (dishes != null && !dishes.isEmpty()){
throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED);
}
}
//不为起售则直接更改状态
Setmeal setmeal = Setmeal.builder()
.status(status)
.id(id)
.build();
setmealMapper.update(setmeal);
}
} }

View File

@@ -48,4 +48,11 @@
</where> </where>
order by dish.create_time desc order by dish.create_time desc
</select> </select>
<select id="selectByDishIdsAndStatusDisable" resultType="com.sky.entity.Dish">
select * from dish where id in
<foreach collection="dishIds" item="id" separator="," open="(" close=")">
#{id}
</foreach>
and status = 0
</select>
</mapper> </mapper>

View File

@@ -9,6 +9,20 @@
VALUES (#{categoryId}, #{name}, #{price}, #{status}, #{description}, #{image}, #{createTime}, #{updateTime}, VALUES (#{categoryId}, #{name}, #{price}, #{status}, #{description}, #{image}, #{createTime}, #{updateTime},
#{createUser}, #{updateUser}) #{createUser}, #{updateUser})
</insert> </insert>
<update id="update">
update setmeal
<set>
<if test="categoryId != null">category_id = #{categoryId},</if>
<if test="description != null">description = #{description},</if>
<if test="image != null">image = #{image},</if>
<if test="name != null">name = #{name},</if>
<if test="price != null">price = #{price},</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="deleteBatch"> <delete id="deleteBatch">
delete from setmeal where id in delete from setmeal where id in
<foreach collection="ids" item="id" separator="," open="(" close=")"> <foreach collection="ids" item="id" separator="," open="(" close=")">