自学内容网 自学内容网

苍穹外卖学习笔记(八)

套餐管理

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


}

}


原文地址:https://blog.csdn.net/qq_73340809/article/details/142317527

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!