当前位置: 首页 > article >正文

苍穹外卖学习笔记(八)

套餐管理

  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);


}

}


http://www.kler.cn/news/308762.html

相关文章:

  • 【案例71】配置https之后 IE打不开登陆页面 Uclient没有问题
  • 《微信小程序实战(2) · 组件封装》
  • 【重学 MySQL】二十七、七种 join 连接
  • 宝塔Linux部署 Vue + Spring Boot + MySQL + Redis
  • Parallels Desktop 20 for Mac 正式发布,更新了哪些新功能(附下载链接)!
  • 深度学习驱动超材料设计领域发展
  • 用Inno Setup打包QT程序输出安装包
  • 消息队列的幂等问题解决方案
  • 51单片机+proteus+学习3(串口、矩阵按键)
  • 了解华为云容器引擎(Cloud Container Engine)
  • 关于http的206状态码和416状态码的意义、断点续传以及CORS使用Access-Control-Allow-Origin来允许跨域请求
  • 网络运维故障处理案例
  • 武汉传媒学院联合创龙教仪建设DSP教学实验箱,基于DSP C6000平台搭建
  • Pytorch详解-模型模块(RNN,CNN,FNN,LSTM,GRU,TCN,Transformer)
  • 了解云容器实例云容器实例(Cloud Container Instance)
  • JavaSE入门
  • 多线程同步
  • 【数据结构】经典题
  • 初始MYSQL数据库(5)—— 索引
  • LabVIEW减速机加载控制系统
  • HarmonyOS 实现沉浸式效果
  • Spring自定义注解
  • 超全网络安全面试题汇总(2024版)
  • 速盾:网页游戏可以开cdn吗?
  • selenium元素定位:元素点击交互异常解决方法
  • 1.数据结构-双链表
  • YOLOv8改进 - 注意力篇 - 引入CBAM注意力机制
  • TCP.IP四层模型
  • Redis命令:redis-cli
  • 【乐企】基础请求封装