当前位置: 首页> 文旅> 艺术 > 淮安网站建设工作室_网络营销推广方法论文_游戏推广赚佣金平台_seo网页优化平台

淮安网站建设工作室_网络营销推广方法论文_游戏推广赚佣金平台_seo网页优化平台

时间:2025/7/10 7:30:58来源:https://blog.csdn.net/qq_73340809/article/details/142317527 浏览次数:0次
淮安网站建设工作室_网络营销推广方法论文_游戏推广赚佣金平台_seo网页优化平台

套餐管理

  1. Controller
  2. Service
  3. Impl
    套餐管理
  4. 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 {

@Autowired
private SetmealService setmealService;/*** 新增套餐*/
@PostMapping
@ApiOperation("新增套餐")
public Result save(@RequestBody SetmealDTO setmealDTO) {log.info("新增套餐");setmealService.saveWithDishes(setmealDTO);return Result.success();
}/*** 分页查询套餐*/
@GetMapping("/page")
@ApiOperation("分页查询套餐")
public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO) {log.info("分页查询套餐:{}", setmealPageQueryDTO);PageResult pageResult = setmealService.pageQuery(setmealPageQueryDTO);return Result.success(pageResult);
}/*** 删除套餐*/
@DeleteMapping
@ApiOperation("删除套餐")
public Result delete(@RequestParam List<Long> ids) {log.info("删除套餐:{}", ids);setmealService.deleteBatch(ids);return Result.success();
}/*** 根据id查询套餐*/
@GetMapping("/{id}")
@ApiOperation("根据id查询套餐")
public Result<SetmealVO> getById(@PathVariable Long id) {log.info("根据id查询套餐:{}", id);SetmealVO setmealVO = setmealService.getByIdWithDish(id);return Result.success(setmealVO);
}/*** 修改套餐*/
@PutMapping
@ApiOperation("修改套餐")
public Result update(@RequestBody SetmealDTO setmealDTO) {log.info("修改套餐:{}", setmealDTO);setmealService.updateWithDishes(setmealDTO);return Result.success();
}/*** 修改套餐状态*/
@PostMapping("/status/{status}")
@ApiOperation("修改套餐状态")
public Result updateStatus(@PathVariable Integer status, Long id) {log.info("修改套餐状态:{}", id);setmealService.updateStatus(status, id);return Result.success();
}

}
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 {

/*** 新增套餐*/
void saveWithDishes(SetmealDTO setmealDTO);/*** 分页查询套餐*/
PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO);/*** 删除套餐*/
void deleteBatch(List<Long> ids);/*** 根据id查询套餐*/
SetmealVO getByIdWithDish(Long id);/*** 更新套餐*/
void updateWithDishes(SetmealDTO setmealDTO);/*** 更新套餐状态*/
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 {

@Autowired
private SetmealMapper setmealMapper;@Autowired
private SetmealDishMapper setmealDishMapper;@Autowired
private SqlSessionFactory sqlSessionFactory;/*** 新增套餐*/
@Override
@Transactional
public void saveWithDishes(SetmealDTO setmealDTO) {// 向套餐表插入一条数据Setmeal setmeal = new Setmeal();BeanUtils.copyProperties(setmealDTO, setmeal);setmealMapper.insert(setmeal);setmealMapper.selectById(setmeal.getId());// 获取插入后的主键值Long setmealId = setmeal.getId();// 获取套餐id// 获取套餐菜品List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();if (setmealDishes != null && !setmealDishes.isEmpty()) {setmealDishes.forEach(setmealDish -> {setmealDish.setSetmealId(setmealId);// 设置套餐id});//批量插入套餐菜品MybatisBatch<SetmealDish> mybatisBatch = new MybatisBatch<>(sqlSessionFactory, setmealDishes);MybatisBatch.Method<SetmealDish> method = new MybatisBatch.Method<>(SetmealDishMapper.class);mybatisBatch.execute(method.insert());}
}/*** 分页查询套餐*/
@Override
@Transactional
public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {Page<SetmealVO> page = new Page<>(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<>();queryWrapper.isNotNull("s.name");// 如果提供了名称,则添加模糊查询条件if (StringUtils.isNotBlank(setmealPageQueryDTO.getName())) {queryWrapper.like("s.name", setmealPageQueryDTO.getName());}// 如果提供了分类id,则添加等值查询条件if (setmealPageQueryDTO.getCategoryId() != null) {queryWrapper.eq("s.category_id", setmealPageQueryDTO.getCategoryId());}// 如果提供了状态,则添加等值查询条件if (setmealPageQueryDTO.getStatus() != null) {queryWrapper.eq("s.status", setmealPageQueryDTO.getStatus());}// 按创建时间降序排序queryWrapper.orderByDesc("s.create_time");// 执行查询System.out.println(queryWrapper.getCustomSqlSegment());Page<SetmealVO> setmealVOPage = setmealMapper.selectPage(page, queryWrapper);long total = setmealVOPage.getTotal();List<SetmealVO> setmealVOList = setmealVOPage.getRecords();return new PageResult(total, setmealVOList);
}/*** 删除套餐*/
@Override
@Transactional
public void deleteBatch(List<Long> ids) {//判断是否存在起售中的套餐List<Setmeal> setmeals = setmealMapper.selectBatchIds(ids);for (Setmeal setmeal : setmeals) {if (setmeal.getStatus() == 1) {throw new RuntimeException(MessageConstant.SETMEAL_ON_SALE);}}//删除套餐setmealMapper.deleteBatchIds(ids);//删除套餐菜品LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper<>();lambdaQueryWrapper.in(SetmealDish::getSetmealId, ids);setmealDishMapper.delete(lambdaQueryWrapper);
}/*** 根据id查询套餐*/
@Override
@Transactional
public SetmealVO getByIdWithDish(Long id) {// 查询套餐Setmeal setmeal = setmealMapper.selectById(id);// 查询套餐菜品LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper<>();lambdaQueryWrapper.eq(SetmealDish::getSetmealId, id);List<SetmealDish> setmealDishes = setmealDishMapper.selectList(lambdaQueryWrapper);// 封装数据SetmealVO setmealVO = new SetmealVO();BeanUtils.copyProperties(setmeal, setmealVO);setmealVO.setSetmealDishes(setmealDishes);return setmealVO;
}/*** 更新套餐*/
@Override
@Transactional
public void updateWithDishes(SetmealDTO setmealDTO) {Setmeal setmeal = new Setmeal();BeanUtils.copyProperties(setmealDTO, setmeal);setmealMapper.updateById(setmeal);//删除原有套餐菜品LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper<>();lambdaQueryWrapper.eq(SetmealDish::getSetmealId, setmeal.getId());setmealDishMapper.delete(lambdaQueryWrapper);//插入新的套餐菜品List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();if (setmealDishes != null && !setmealDishes.isEmpty()) {setmealDishes.forEach(setmealDish -> {setmealDish.setSetmealId(setmeal.getId());});//批量插入套餐菜品MybatisBatch<SetmealDish> mybatisBatch = new MybatisBatch<>(sqlSessionFactory, setmealDishes);MybatisBatch.Method<SetmealDish> method = new MybatisBatch.Method<>(SetmealDishMapper.class);mybatisBatch.execute(method.insert());}
}/*** 更新套餐状态*/
@Override
@Transactional
public void updateStatus(Integer status, Long id) {//判断是否存在未启售的菜品if (status == 1) {LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper<>();lambdaQueryWrapper.eq(SetmealDish::getSetmealId, id);List<SetmealDish> setmealDishes = setmealDishMapper.selectList(lambdaQueryWrapper);if (setmealDishes.isEmpty()) {throw new RuntimeException(MessageConstant.SETMEAL_ENABLE_FAILED);}}//更新套餐状态Setmeal setmeal = setmealMapper.selectById(id);if (setmeal == null) {throw new RuntimeException(MessageConstant.SETMEAL_NOT_FOUND);}setmeal.setStatus(status);setmealMapper.updateById(setmeal);}

}

关键字:淮安网站建设工作室_网络营销推广方法论文_游戏推广赚佣金平台_seo网页优化平台

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: