批量删除套餐、菜品起售停售(TODO)

This commit is contained in:
slhaf
2024-09-12 20:55:15 +08:00
parent e223fbb1a8
commit a65c9e0c69
11 changed files with 115 additions and 5 deletions

View File

@@ -98,9 +98,22 @@ public class DishController {
*/
@GetMapping("/list")
@ApiOperation("根据分类id查询菜品")
public Result<List> list(String categoryId){
public Result<List> list(Long categoryId){
log.info("根据分类id查询菜品: {}",categoryId);
List<Dish> dishes = dishService.selectByCategoryId(categoryId);
return Result.success(dishes);
}
/**
* 菜品起售停售
* @param status
* @param id
* @return
*/
@PostMapping("/status/{status}")
@ApiOperation("菜品起售停售")
public Result startOrStop(@PathVariable Integer status,Long id){
dishService.startOrStop(status,id);
return Result.success();
}
}

View File

@@ -11,6 +11,8 @@ 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")
@Slf4j
@@ -45,4 +47,17 @@ public class SetmealController {
setmealService.insert(setmealDTO);
return Result.success();
}
/**
* 批量删除套餐
* @param ids
* @return
*/
@DeleteMapping
@ApiOperation("批量删除套餐")
public Result delete(@RequestParam List<Long> ids){
log.info("批量删除套餐: {}",ids);
setmealService.deleteBatch(ids);
return Result.success();
}
}

View File

@@ -67,5 +67,5 @@ public interface DishMapper {
* @return
*/
@Select("select * from dish where category_id = #{categoryId}")
List<Dish> selectByCategoryId(String categoryId);
List<Dish> selectByCategoryId(Long categoryId);
}

View File

@@ -15,4 +15,6 @@ public interface SetmealDishMapper {
List<Long> getSetmealIdsByDishIds(List<Long> ids);
void insertBatch(List<SetmealDish> setmealDishes);
void deleteBySetmealIds(List<Long> setmealIds);
}

View File

@@ -8,6 +8,8 @@ import com.sky.enumeration.OperationType;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface SetmealMapper {
@@ -23,4 +25,9 @@ public interface SetmealMapper {
@AutoFill(OperationType.INSERT)
void insert(Setmeal setmeal);
@Select("select * from setmeal where id = #{id}")
Setmeal selectById(Long id);
void deleteBatch(List<Long> ids);
}

View File

@@ -49,5 +49,12 @@ public interface DishService {
* @param categoryId
* @return
*/
List<Dish> selectByCategoryId(String categoryId);
List<Dish> selectByCategoryId(Long categoryId);
/**
* 菜品起售停售
* @param status
* @param id
*/
void startOrStop(Integer status, Long id);
}

View File

@@ -5,6 +5,8 @@ import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public interface SetmealService {
/**
@@ -19,4 +21,10 @@ public interface SetmealService {
* @param setmealDTO
*/
void insert(SetmealDTO setmealDTO);
/**
* 批量删除套餐
* @param ids
*/
void deleteBatch(List<Long> ids);
}

View File

@@ -159,7 +159,26 @@ public class DishServiceImpl implements DishService {
* @return
*/
@Override
public List<Dish> selectByCategoryId(String categoryId) {
public List<Dish> selectByCategoryId(Long categoryId) {
return dishMapper.selectByCategoryId(categoryId);
}
/**
* 菜品起售停售
* @param status
* @param id
*/
@Override
@Transactional
//TODO 菜品关联套餐状态
public void startOrStop(Integer status, Long id) {
//更新菜品状态
Dish dish = Dish.builder()
.id(id)
.status(status)
.build();
dishMapper.update(dish);
//更新菜品所关联套餐的状态
}
}

View File

@@ -2,10 +2,13 @@ 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.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
@@ -52,6 +55,30 @@ public class SetmealServiceImpl implements SetmealService {
//插入套餐包含菜品的信息
List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
//设置setmealId
setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmeal.getId()));
setmealDishMapper.insertBatch(setmealDishes);
}
/**
* 批量删除套餐
* @param ids
*/
@Override
@Transactional
public void deleteBatch(List<Long> ids) {
//查看套餐是否有在售状态
for (Long id : ids) {
Setmeal setmeal = setmealMapper.selectById(id);
if (setmeal.getStatus().equals(StatusConstant.ENABLE)) {
throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
}
}
//批量删除套餐动态SQL
//删除setmeal数据
setmealMapper.deleteBatch(ids);
//删除setmeal_dish数据
setmealDishMapper.deleteBySetmealIds(ids);
}
}

View File

@@ -10,6 +10,12 @@
(#{sd.setmealId},#{sd.dishId},#{sd.name},#{sd.price},#{sd.copies})
</foreach>
</insert>
<delete id="deleteBySetmealIds">
delete from setmeal_dish where setmeal_id in
<foreach collection="setmealIds" item="setmealId" separator="," open="(" close=")">
#{setmealId}
</foreach>
</delete>
<select id="getSetmealIdsByDishIds" resultType="java.lang.Long">
select setmeal_id from setmeal_dish where dish_id in

View File

@@ -3,12 +3,18 @@
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">
<insert id="insert" 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>
<delete id="deleteBatch">
delete from setmeal where id in
<foreach collection="ids" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</delete>
<select id="pageQuery" resultType="com.sky.entity.Setmeal">
select * from setmeal