苍穹外卖--套餐管理--全部功能实现
This commit is contained in:
@@ -2,6 +2,7 @@ package com.sky.controller.admin;
|
||||
|
||||
import com.sky.dto.DishDTO;
|
||||
import com.sky.dto.DishPageQueryDTO;
|
||||
import com.sky.entity.Dish;
|
||||
import com.sky.result.PageResult;
|
||||
import com.sky.result.Result;
|
||||
import com.sky.service.DishService;
|
||||
@@ -77,4 +78,16 @@ public class DishController {
|
||||
dishService.startOrStop(status,id);
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据分类id查询菜品
|
||||
* @param categoryId
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/list")
|
||||
@ApiOperation("根据分类id查询菜品")
|
||||
public Result<List<Dish>> list(Long categoryId){
|
||||
List<Dish> list = dishService.list(categoryId);
|
||||
return Result.success(list);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
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;
|
||||
|
||||
/**
|
||||
* 新增套餐
|
||||
* @param setmealDTO
|
||||
* @return
|
||||
*/
|
||||
@PostMapping
|
||||
@ApiOperation("新增套餐")
|
||||
public Result save(@RequestBody SetmealDTO setmealDTO) {
|
||||
setmealService.saveWithDish(setmealDTO);
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 分页查询
|
||||
* @param setmealPageQueryDTO
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/page")
|
||||
@ApiOperation("分页查询")
|
||||
public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO) {
|
||||
PageResult pageResult = setmealService.pageQuery(setmealPageQueryDTO);
|
||||
return Result.success(pageResult);
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量删除套餐
|
||||
* @param ids
|
||||
* @return
|
||||
*/
|
||||
@DeleteMapping
|
||||
@ApiOperation("批量删除套餐")
|
||||
public Result delete(@RequestParam List<Long> ids){
|
||||
setmealService.deleteBatch(ids);
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据id查询套餐,用于修改页面回显数据
|
||||
*
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/{id}")
|
||||
@ApiOperation("根据id查询套餐")
|
||||
public Result<SetmealVO> getById(@PathVariable Long id) {
|
||||
SetmealVO setmealVO = setmealService.getByIdWithDish(id);
|
||||
return Result.success(setmealVO);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改套餐
|
||||
*
|
||||
* @param setmealDTO
|
||||
* @return
|
||||
*/
|
||||
@PutMapping
|
||||
@ApiOperation("修改套餐")
|
||||
public Result update(@RequestBody SetmealDTO setmealDTO) {
|
||||
setmealService.update(setmealDTO);
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 套餐起售停售
|
||||
* @param status
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/status/{status}")
|
||||
@ApiOperation("套餐起售停售")
|
||||
public Result startOrStop(@PathVariable Integer status, Long id) {
|
||||
setmealService.startOrStop(status, id);
|
||||
return Result.success();
|
||||
}
|
||||
}
|
||||
@@ -58,5 +58,19 @@ public interface DishMapper {
|
||||
@AutoFill(OperationType.UPDATE)
|
||||
void update(Dish dish);
|
||||
|
||||
void startOrStop(Integer status, Long id);
|
||||
|
||||
/**
|
||||
* 动态条件查询菜品
|
||||
* @param dish
|
||||
* @return
|
||||
*/
|
||||
List<Dish> list(Dish dish);
|
||||
|
||||
/**
|
||||
* 根据套餐id查询菜品
|
||||
* @param setmealId
|
||||
* @return
|
||||
*/
|
||||
@Select("select a.* from dish a left join setmeal_dish b on a.id = b.dish_id where b.setmeal_id = #{setmealId}")
|
||||
List<Dish> getBySetmealId(Long setmealId);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package com.sky.mapper;
|
||||
|
||||
import com.sky.entity.SetmealDish;
|
||||
import org.apache.ibatis.annotations.Delete;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -17,4 +20,25 @@ public interface SetmealDishMapper {
|
||||
* @return
|
||||
*/
|
||||
List<Long> getSetmealIdsByDishIds(List<Long> ids);
|
||||
|
||||
/**
|
||||
* 批量保存套餐和菜品的关联关系
|
||||
* @param setmealDishes
|
||||
*/
|
||||
void insertBatch(List<SetmealDish> setmealDishes);
|
||||
|
||||
/**
|
||||
* 根据套餐id删除套餐和菜品的关联关系
|
||||
* @param setmealId
|
||||
*/
|
||||
@Delete("delete from setmeal_dish where setmeal_id = #{setmealId}")
|
||||
void deleteBySetmealId(Long setmealId);
|
||||
|
||||
/**
|
||||
* 根据套餐id查询套餐和菜品的关联关系
|
||||
* @param setmealId
|
||||
* @return
|
||||
*/
|
||||
@Select("select * from setmeal_dish where setmeal_id = #{setmealId}")
|
||||
List<SetmealDish> getBySetmealId(Long setmealId);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,12 @@
|
||||
package com.sky.mapper;
|
||||
|
||||
import com.github.pagehelper.Page;
|
||||
import com.sky.annotation.AutoFill;
|
||||
import com.sky.dto.SetmealPageQueryDTO;
|
||||
import com.sky.entity.Setmeal;
|
||||
import com.sky.enumeration.OperationType;
|
||||
import com.sky.vo.SetmealVO;
|
||||
import org.apache.ibatis.annotations.Delete;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
|
||||
@@ -14,4 +21,38 @@ public interface SetmealMapper {
|
||||
@Select("select count(id) from setmeal where category_id = #{categoryId}")
|
||||
Integer countByCategoryId(Long id);
|
||||
|
||||
/**
|
||||
* 新增套餐
|
||||
* @param setmeal
|
||||
*/
|
||||
@AutoFill(OperationType.INSERT)
|
||||
void insert(Setmeal setmeal);
|
||||
|
||||
/**
|
||||
* 分页查询
|
||||
* @param setmealPageQueryDTO
|
||||
* @return
|
||||
*/
|
||||
Page<SetmealVO> pageQuery(SetmealPageQueryDTO setmealPageQueryDTO);
|
||||
|
||||
/**
|
||||
* 根据id查询套餐
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
@Select("select * from setmeal where id = #{id}")
|
||||
Setmeal getById(Long id);
|
||||
|
||||
/**
|
||||
* 根据id删除套餐
|
||||
* @param setmealId
|
||||
*/
|
||||
@Delete("delete from setmeal where id = #{id}")
|
||||
void deleteById(Long setmealId);
|
||||
|
||||
/**
|
||||
* 更新套餐
|
||||
* @param setmeal
|
||||
*/
|
||||
void update(Setmeal setmeal);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.sky.service;
|
||||
|
||||
import com.sky.dto.DishDTO;
|
||||
import com.sky.dto.DishPageQueryDTO;
|
||||
import com.sky.entity.Dish;
|
||||
import com.sky.result.PageResult;
|
||||
import com.sky.vo.DishVO;
|
||||
|
||||
@@ -45,4 +46,11 @@ public interface DishService {
|
||||
* @param id
|
||||
*/
|
||||
void startOrStop(Integer status, Long id);
|
||||
|
||||
/**
|
||||
* 根据分类id查询菜品
|
||||
* @param categoryId
|
||||
* @return
|
||||
*/
|
||||
List<Dish> list(Long categoryId);
|
||||
}
|
||||
|
||||
50
sky-server/src/main/java/com/sky/service/SetmealService.java
Normal file
50
sky-server/src/main/java/com/sky/service/SetmealService.java
Normal file
@@ -0,0 +1,50 @@
|
||||
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 {
|
||||
|
||||
/**
|
||||
* 新增套餐,同时需要保存套餐和菜品的关联关系
|
||||
* @param setmealDTO
|
||||
*/
|
||||
void saveWithDish(SetmealDTO setmealDTO);
|
||||
|
||||
/**
|
||||
* 分页查询
|
||||
* @param setmealPageQueryDTO
|
||||
* @return
|
||||
*/
|
||||
PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO);
|
||||
|
||||
/**
|
||||
* 批量删除套餐
|
||||
* @param ids
|
||||
*/
|
||||
void deleteBatch(List<Long> ids);
|
||||
|
||||
/**
|
||||
* 根据id查询套餐和关联的菜品数据
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
SetmealVO getByIdWithDish(Long id);
|
||||
|
||||
/**
|
||||
* 修改套餐
|
||||
* @param setmealDTO
|
||||
*/
|
||||
void update(SetmealDTO setmealDTO);
|
||||
|
||||
/**
|
||||
* 套餐起售、停售
|
||||
* @param status
|
||||
* @param id
|
||||
*/
|
||||
void startOrStop(Integer status, Long id);
|
||||
}
|
||||
@@ -148,4 +148,17 @@ public class DishServiceImpl implements DishService {
|
||||
Dish dish = Dish.builder().id(id).status(status).build();
|
||||
dishMapper.update(dish);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据分类id查询菜品
|
||||
* @param categoryId
|
||||
* @return
|
||||
*/
|
||||
public List<Dish> list(Long categoryId) {
|
||||
Dish dish = Dish.builder()
|
||||
.categoryId(categoryId)
|
||||
.status(StatusConstant.ENABLE)
|
||||
.build();
|
||||
return dishMapper.list(dish);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,171 @@
|
||||
package com.sky.service.impl;
|
||||
|
||||
import com.github.pagehelper.Page;
|
||||
import com.github.pagehelper.PageHelper;
|
||||
import com.sky.constant.MessageConstant;
|
||||
import com.sky.constant.StatusConstant;
|
||||
import com.sky.dto.SetmealDTO;
|
||||
import com.sky.dto.SetmealPageQueryDTO;
|
||||
import com.sky.entity.Dish;
|
||||
import com.sky.entity.Setmeal;
|
||||
import com.sky.entity.SetmealDish;
|
||||
import com.sky.exception.DeletionNotAllowedException;
|
||||
import com.sky.exception.SetmealEnableFailedException;
|
||||
import com.sky.mapper.DishMapper;
|
||||
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.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 DishMapper dishMapper;
|
||||
|
||||
/**
|
||||
* 新增套餐,同时需要保存套餐和菜品的关联关系
|
||||
* @param setmealDTO
|
||||
*/
|
||||
@Transactional
|
||||
public void saveWithDish(SetmealDTO setmealDTO) {
|
||||
Setmeal setmeal = new Setmeal();
|
||||
BeanUtils.copyProperties(setmealDTO, setmeal);
|
||||
|
||||
//向套餐表插入数据
|
||||
setmealMapper.insert(setmeal);
|
||||
|
||||
//获取生成的套餐id
|
||||
Long setmealId = setmeal.getId();
|
||||
|
||||
List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
|
||||
setmealDishes.forEach(setmealDish -> {
|
||||
setmealDish.setSetmealId(setmealId);
|
||||
});
|
||||
|
||||
//保存套餐和菜品的关联关系
|
||||
setmealDishMapper.insertBatch(setmealDishes);
|
||||
}
|
||||
|
||||
/**
|
||||
* 分页查询
|
||||
* @param setmealPageQueryDTO
|
||||
* @return
|
||||
*/
|
||||
public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
|
||||
int pageNum = setmealPageQueryDTO.getPage();
|
||||
int pageSize = setmealPageQueryDTO.getPageSize();
|
||||
|
||||
PageHelper.startPage(pageNum, pageSize);
|
||||
Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);
|
||||
return new PageResult(page.getTotal(), page.getResult());
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量删除套餐
|
||||
* @param ids
|
||||
*/
|
||||
@Transactional
|
||||
public void deleteBatch(List<Long> ids) {
|
||||
ids.forEach(id -> {
|
||||
Setmeal setmeal = setmealMapper.getById(id);
|
||||
if(StatusConstant.ENABLE == setmeal.getStatus()){
|
||||
//起售中的套餐不能删除
|
||||
throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
|
||||
}
|
||||
});
|
||||
|
||||
ids.forEach(setmealId -> {
|
||||
//删除套餐表中的数据
|
||||
setmealMapper.deleteById(setmealId);
|
||||
//删除套餐菜品关系表中的数据
|
||||
setmealDishMapper.deleteBySetmealId(setmealId);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据id查询套餐和套餐菜品关系
|
||||
*
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
public SetmealVO getByIdWithDish(Long id) {
|
||||
Setmeal setmeal = setmealMapper.getById(id);
|
||||
List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(id);
|
||||
|
||||
SetmealVO setmealVO = new SetmealVO();
|
||||
BeanUtils.copyProperties(setmeal, setmealVO);
|
||||
setmealVO.setSetmealDishes(setmealDishes);
|
||||
|
||||
return setmealVO;
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改套餐
|
||||
*
|
||||
* @param setmealDTO
|
||||
*/
|
||||
@Transactional
|
||||
public void update(SetmealDTO setmealDTO) {
|
||||
Setmeal setmeal = new Setmeal();
|
||||
BeanUtils.copyProperties(setmealDTO, setmeal);
|
||||
|
||||
//1、修改套餐表,执行update
|
||||
setmealMapper.update(setmeal);
|
||||
|
||||
//套餐id
|
||||
Long setmealId = setmealDTO.getId();
|
||||
|
||||
//2、删除套餐和菜品的关联关系,操作setmeal_dish表,执行delete
|
||||
setmealDishMapper.deleteBySetmealId(setmealId);
|
||||
|
||||
List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
|
||||
setmealDishes.forEach(setmealDish -> {
|
||||
setmealDish.setSetmealId(setmealId);
|
||||
});
|
||||
//3、重新插入套餐和菜品的关联关系,操作setmeal_dish表,执行insert
|
||||
setmealDishMapper.insertBatch(setmealDishes);
|
||||
}
|
||||
|
||||
/**
|
||||
* 套餐起售、停售
|
||||
* @param status
|
||||
* @param id
|
||||
*/
|
||||
public void startOrStop(Integer status, Long id) {
|
||||
//起售套餐时,判断套餐内是否有停售菜品,有停售菜品提示"套餐内包含未启售菜品,无法启售"
|
||||
if(status == StatusConstant.ENABLE){
|
||||
//select a.* from dish a left join setmeal_dish b on a.id = b.dish_id where b.setmeal_id = ?
|
||||
List<Dish> dishList = dishMapper.getBySetmealId(id);
|
||||
if(dishList != null && dishList.size() > 0){
|
||||
dishList.forEach(dish -> {
|
||||
if(StatusConstant.DISABLE == dish.getStatus()){
|
||||
throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Setmeal setmeal = Setmeal.builder()
|
||||
.id(id)
|
||||
.status(status)
|
||||
.build();
|
||||
setmealMapper.update(setmeal);
|
||||
}
|
||||
}
|
||||
@@ -48,4 +48,20 @@
|
||||
</set>
|
||||
where id = #{id}
|
||||
</update>
|
||||
|
||||
<select id="list" resultType="Dish" parameterType="Dish">
|
||||
select * from dish
|
||||
<where>
|
||||
<if test="name != null">
|
||||
and name like concat('%',#{name},'%')
|
||||
</if>
|
||||
<if test="categoryId != null">
|
||||
and category_id = #{categoryId}
|
||||
</if>
|
||||
<if test="status != null">
|
||||
and status = #{status}
|
||||
</if>
|
||||
</where>
|
||||
order by create_time desc
|
||||
</select>
|
||||
</mapper>
|
||||
|
||||
@@ -7,4 +7,13 @@
|
||||
#{id}
|
||||
</foreach>
|
||||
</select>
|
||||
|
||||
<insert id="insertBatch" parameterType="list">
|
||||
insert into setmeal_dish
|
||||
(setmeal_id,dish_id,name,price,copies)
|
||||
values
|
||||
<foreach collection="setmealDishes" item="sd" separator=",">
|
||||
(#{sd.setmealId},#{sd.dishId},#{sd.name},#{sd.price},#{sd.copies})
|
||||
</foreach>
|
||||
</insert>
|
||||
</mapper>
|
||||
64
sky-server/src/main/resources/mapper/SetmealMapper.xml
Normal file
64
sky-server/src/main/resources/mapper/SetmealMapper.xml
Normal file
@@ -0,0 +1,64 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
|
||||
<mapper namespace="com.sky.mapper.SetmealMapper">
|
||||
<insert id="insert" parameterType="Setmeal" useGeneratedKeys="true" keyProperty="id">
|
||||
insert into setmeal
|
||||
(category_id, name, price, status, description, image, create_time, update_time, create_user, update_user)
|
||||
values (#{categoryId}, #{name}, #{price}, #{status}, #{description}, #{image}, #{createTime}, #{updateTime},
|
||||
#{createUser}, #{updateUser})
|
||||
</insert>
|
||||
|
||||
<select id="pageQuery" resultType="com.sky.vo.SetmealVO">
|
||||
select
|
||||
s.*,c.name categoryName
|
||||
from
|
||||
setmeal s
|
||||
left join
|
||||
category c
|
||||
on
|
||||
s.category_id = c.id
|
||||
<where>
|
||||
<if test="name != null">
|
||||
and s.name like concat('%',#{name},'%')
|
||||
</if>
|
||||
<if test="status != null">
|
||||
and s.status = #{status}
|
||||
</if>
|
||||
<if test="categoryId != null">
|
||||
and s.category_id = #{categoryId}
|
||||
</if>
|
||||
</where>
|
||||
order by s.create_time desc
|
||||
</select>
|
||||
|
||||
<update id="update">
|
||||
update setmeal
|
||||
<set>
|
||||
<if test="name != null">
|
||||
name = #{name},
|
||||
</if>
|
||||
<if test="categoryId != null">
|
||||
category_id = #{categoryId},
|
||||
</if>
|
||||
<if test="price != null">
|
||||
price = #{price},
|
||||
</if>
|
||||
<if test="status != null">
|
||||
status = #{status},
|
||||
</if>
|
||||
<if test="description != null">
|
||||
description = #{description},
|
||||
</if>
|
||||
<if test="image != null">
|
||||
image = #{image},
|
||||
</if>
|
||||
<if test="updateTime != null">
|
||||
update_time = #{updateTime},
|
||||
</if>
|
||||
<if test="updateUser != null">
|
||||
update_user = #{updateUser}
|
||||
</if>
|
||||
</set>
|
||||
where id = #{id}
|
||||
</update>
|
||||
</mapper>
|
||||
Reference in New Issue
Block a user