苍穹外卖学习条记(八)

[复制链接]
发表于 2026-1-20 18:33:01 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
套餐管理

  • Controller
  • Service
  • Impl
    套餐管理
  • Controller
    package com.sky.controller.admin;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping(“/admin/setmeal”)
@Api(tags = “套餐管理”)
@Slf4j
public class SetmealController {
  1. @Autowired
  2. private SetmealService setmealService;
  3. /**
  4. * 新增套餐
  5. */
  6. @PostMapping
  7. @ApiOperation("新增套餐")
  8. public Result save(@RequestBody SetmealDTO setmealDTO) {
  9.     log.info("新增套餐");
  10.     setmealService.saveWithDishes(setmealDTO);
  11.     return Result.success();
  12. }
  13. /**
  14. * 分页查询套餐
  15. */
  16. @GetMapping("/page")
  17. @ApiOperation("分页查询套餐")
  18. public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO) {
  19.     log.info("分页查询套餐:{}", setmealPageQueryDTO);
  20.     PageResult pageResult = setmealService.pageQuery(setmealPageQueryDTO);
  21.     return Result.success(pageResult);
  22. }
  23. /**
  24. * 删除套餐
  25. */
  26. @DeleteMapping
  27. @ApiOperation("删除套餐")
  28. public Result delete(@RequestParam List<Long> ids) {
  29.     log.info("删除套餐:{}", ids);
  30.     setmealService.deleteBatch(ids);
  31.     return Result.success();
  32. }
  33. /**
  34. * 根据id查询套餐
  35. */
  36. @GetMapping("/{id}")
  37. @ApiOperation("根据id查询套餐")
  38. public Result<SetmealVO> getById(@PathVariable Long id) {
  39.     log.info("根据id查询套餐:{}", id);
  40.     SetmealVO setmealVO = setmealService.getByIdWithDish(id);
  41.     return Result.success(setmealVO);
  42. }
  43. /**
  44. * 修改套餐
  45. */
  46. @PutMapping
  47. @ApiOperation("修改套餐")
  48. public Result update(@RequestBody SetmealDTO setmealDTO) {
  49.     log.info("修改套餐:{}", setmealDTO);
  50.     setmealService.updateWithDishes(setmealDTO);
  51.     return Result.success();
  52. }
  53. /**
  54. * 修改套餐状态
  55. */
  56. @PostMapping("/status/{status}")
  57. @ApiOperation("修改套餐状态")
  58. public Result updateStatus(@PathVariable Integer status, Long id) {
  59.     log.info("修改套餐状态:{}", id);
  60.     setmealService.updateStatus(status, id);
  61.     return Result.success();
  62. }
复制代码
}
2. Service
package com.sky.service;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.vo.SetmealVO;
import java.util.List;
public interface SetmealService {
  1. /**
  2. * 新增套餐
  3. */
  4. void saveWithDishes(SetmealDTO setmealDTO);
  5. /**
  6. * 分页查询套餐
  7. */
  8. PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO);
  9. /**
  10. * 删除套餐
  11. */
  12. void deleteBatch(List<Long> ids);
  13. /**
  14. * 根据id查询套餐
  15. */
  16. SetmealVO getByIdWithDish(Long id);
  17. /**
  18. * 更新套餐
  19. */
  20. void updateWithDishes(SetmealDTO setmealDTO);
  21. /**
  22. * 更新套餐状态
  23. */
  24. void updateStatus(Integer status, Long id);
复制代码
}
3. Impl
package com.sky.service.impl;
import com.baomidou.mybatisplus.core.batch.MybatisBatch;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sky.constant.MessageConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {
  1. @Autowired
  2. private SetmealMapper setmealMapper;
  3. @Autowired
  4. private SetmealDishMapper setmealDishMapper;
  5. @Autowired
  6. private SqlSessionFactory sqlSessionFactory;
  7. /**
  8. * 新增套餐
  9. */
  10. @Override
  11. @Transactional
  12. public void saveWithDishes(SetmealDTO setmealDTO) {
  13.     // 向套餐表插入一条数据
  14.     Setmeal setmeal = new Setmeal();
  15.     BeanUtils.copyProperties(setmealDTO, setmeal);
  16.     setmealMapper.insert(setmeal);
  17.     setmealMapper.selectById(setmeal.getId());// 获取插入后的主键值
  18.     Long setmealId = setmeal.getId();// 获取套餐id
  19.     // 获取套餐菜品
  20.     List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
  21.     if (setmealDishes != null && !setmealDishes.isEmpty()) {
  22.         setmealDishes.forEach(setmealDish -> {
  23.             setmealDish.setSetmealId(setmealId);// 设置套餐id
  24.         });
  25.         //批量插入套餐菜品
  26.         MybatisBatch<SetmealDish> mybatisBatch = new MybatisBatch<>(sqlSessionFactory, setmealDishes);
  27.         MybatisBatch.Method<SetmealDish> method = new MybatisBatch.Method<>(SetmealDishMapper.class);
  28.         mybatisBatch.execute(method.insert());
  29.     }
  30. }
  31. /**
  32. * 分页查询套餐
  33. */
  34. @Override
  35. @Transactional
  36. public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
  37.     Page<SetmealVO> page = new Page<>(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
  38.     QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<>();
  39.     queryWrapper.isNotNull("s.name");
  40.     // 如果提供了名称,则添加模糊查询条件
  41.     if (StringUtils.isNotBlank(setmealPageQueryDTO.getName())) {
  42.         queryWrapper.like("s.name", setmealPageQueryDTO.getName());
  43.     }
  44.     // 如果提供了分类id,则添加等值查询条件
  45.     if (setmealPageQueryDTO.getCategoryId() != null) {
  46.         queryWrapper.eq("s.category_id", setmealPageQueryDTO.getCategoryId());
  47.     }
  48.     // 如果提供了状态,则添加等值查询条件
  49.     if (setmealPageQueryDTO.getStatus() != null) {
  50.         queryWrapper.eq("s.status", setmealPageQueryDTO.getStatus());
  51.     }
  52.     // 按创建时间降序排序
  53.     queryWrapper.orderByDesc("s.create_time");
  54.     // 执行查询
  55.     System.out.println(queryWrapper.getCustomSqlSegment());
  56.     Page<SetmealVO> setmealVOPage = setmealMapper.selectPage(page, queryWrapper);
  57.     long total = setmealVOPage.getTotal();
  58.     List<SetmealVO> setmealVOList = setmealVOPage.getRecords();
  59.     return new PageResult(total, setmealVOList);
  60. }
  61. /**
  62. * 删除套餐
  63. */
  64. @Override
  65. @Transactional
  66. public void deleteBatch(List<Long> ids) {
  67.     //判断是否存在起售中的套餐
  68.     List<Setmeal> setmeals = setmealMapper.selectBatchIds(ids);
  69.     for (Setmeal setmeal : setmeals) {
  70.         if (setmeal.getStatus() == 1) {
  71.             throw new RuntimeException(MessageConstant.SETMEAL_ON_SALE);
  72.         }
  73.     }
  74.     //删除套餐
  75.     setmealMapper.deleteBatchIds(ids);
  76.     //删除套餐菜品
  77.     LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
  78.     lambdaQueryWrapper.in(SetmealDish::getSetmealId, ids);
  79.     setmealDishMapper.delete(lambdaQueryWrapper);
  80. }
  81. /**
  82. * 根据id查询套餐
  83. */
  84. @Override
  85. @Transactional
  86. public SetmealVO getByIdWithDish(Long id) {
  87.     // 查询套餐
  88.     Setmeal setmeal = setmealMapper.selectById(id);
  89.     // 查询套餐菜品
  90.     LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
  91.     lambdaQueryWrapper.eq(SetmealDish::getSetmealId, id);
  92.     List<SetmealDish> setmealDishes = setmealDishMapper.selectList(lambdaQueryWrapper);
  93.     // 封装数据
  94.     SetmealVO setmealVO = new SetmealVO();
  95.     BeanUtils.copyProperties(setmeal, setmealVO);
  96.     setmealVO.setSetmealDishes(setmealDishes);
  97.     return setmealVO;
  98. }
  99. /**
  100. * 更新套餐
  101. */
  102. @Override
  103. @Transactional
  104. public void updateWithDishes(SetmealDTO setmealDTO) {
  105.     Setmeal setmeal = new Setmeal();
  106.     BeanUtils.copyProperties(setmealDTO, setmeal);
  107.     setmealMapper.updateById(setmeal);
  108.     //删除原有套餐菜品
  109.     LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
  110.     lambdaQueryWrapper.eq(SetmealDish::getSetmealId, setmeal.getId());
  111.     setmealDishMapper.delete(lambdaQueryWrapper);
  112.     //插入新的套餐菜品
  113.     List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
  114.     if (setmealDishes != null && !setmealDishes.isEmpty()) {
  115.         setmealDishes.forEach(setmealDish -> {
  116.             setmealDish.setSetmealId(setmeal.getId());
  117.         });
  118.         //批量插入套餐菜品
  119.         MybatisBatch<SetmealDish> mybatisBatch = new MybatisBatch<>(sqlSessionFactory, setmealDishes);
  120.         MybatisBatch.Method<SetmealDish> method = new MybatisBatch.Method<>(SetmealDishMapper.class);
  121.         mybatisBatch.execute(method.insert());
  122.     }
  123. }
  124. /**
  125. * 更新套餐状态
  126. */
  127. @Override
  128. @Transactional
  129. public void updateStatus(Integer status, Long id) {
  130.     //判断是否存在未启售的菜品
  131.     if (status == 1) {
  132.         LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
  133.         lambdaQueryWrapper.eq(SetmealDish::getSetmealId, id);
  134.         List<SetmealDish> setmealDishes = setmealDishMapper.selectList(lambdaQueryWrapper);
  135.         if (setmealDishes.isEmpty()) {
  136.             throw new RuntimeException(MessageConstant.SETMEAL_ENABLE_FAILED);
  137.         }
  138.     }
  139.     //更新套餐状态
  140.     Setmeal setmeal = setmealMapper.selectById(id);
  141.     if (setmeal == null) {
  142.         throw new RuntimeException(MessageConstant.SETMEAL_NOT_FOUND);
  143.     }
  144.     setmeal.setStatus(status);
  145.     setmealMapper.updateById(setmeal);
  146. }
复制代码
}

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!qidao123.com:ToB企服之家,中国第一个企服评测及软件市场,开放入驻,技术点评得现金
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表