苍穹外卖学习笔记(四)

套餐管理

1. Controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
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);


}
}