根据条件查询对应菜品数据
<p>[TOC]</p>
<h5>简要描述</h5>
<ul>
<li>根据条件查询对应菜品数据</li>
</ul>
<h5>前端页面和服务端的交互过程</h5>
<ul>
<li>页面发送ajax请求,获取第一个分类下的菜品</li>
</ul>
<h5>触发事件</h5>
<ul>
<li>页面数据初始化</li>
</ul>
<h5>接口调用</h5>
<pre><code>//获取菜品数据
if(res[1].code === 1){
this.cartData = res[1].data
}else{
this.$notify({ type:'warning', message:res[1].msg});
}</code></pre>
<h5>请求URL</h5>
<ul>
<li><code>[context-path]/dish/list</code></li>
</ul>
<h5>请求方式</h5>
<ul>
<li>GET </li>
</ul>
<h5>请求参数</h5>
<table>
<thead>
<tr>
<th style="text-align: left;">参数名</th>
<th style="text-align: left;">必选</th>
<th style="text-align: left;">类型</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align: left;">categoryId</td>
<td style="text-align: left;">是</td>
<td style="text-align: left;">BIGINT</td>
<td>菜品分类id</td>
</tr>
<tr>
<td style="text-align: left;">status</td>
<td style="text-align: left;">是</td>
<td style="text-align: left;">INT</td>
<td>1启售0停售</td>
</tr>
</tbody>
</table>
<h5>返回示例</h5>
<pre><code>code: 1
data: [,…]
0: {id: "1605794630641934337", name: "牛油锅", categoryId: "1397844263642378242", price: 6800, code: "",…}
map: {}
msg: null</code></pre>
<h5>返回参数说明</h5>
<table>
<thead>
<tr>
<th style="text-align: left;">参数名</th>
<th style="text-align: left;">类型</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align: left;">code</td>
<td style="text-align: left;">int</td>
<td>编码:1成功,0和其它数字为失败</td>
</tr>
<tr>
<td style="text-align: left;">data</td>
<td style="text-align: left;">object</td>
<td>数据</td>
</tr>
<tr>
<td style="text-align: left;">map</td>
<td style="text-align: left;">Map</td>
<td>动态数据</td>
</tr>
<tr>
<td style="text-align: left;">msg</td>
<td style="text-align: left;">String</td>
<td>错误信息</td>
</tr>
</tbody>
</table>
<h5>备注</h5>
<p>data有两种返回值</p>
<pre><code>public static <T> R<T> success(T object) {
R<T> r = new R<T>();
r.data = object;
r.code = 1;
return r;
}
public static <T> R<T> error(String msg) {
R r = new R();
r.msg = msg;
r.code = 0;
return r;
}</code></pre>
<h5>后端接口请求参数</h5>
<ul>
<li>映射DishDto对象类型参数</li>
</ul>
<h5>后端代码</h5>
<pre><code> @GetMapping("/list")
public R<List<DishDto>> listR(DishDto dish){
log.info("list{}",dish);
LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.like(StringUtils.isNotEmpty(dish.getName()),Dish::getName,dish.getName());
queryWrapper.eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId());
queryWrapper.eq(Dish::getStatus,1);
queryWrapper.orderByDesc(Dish::getUpdateTime);
List<Dish> list = dishService.list(queryWrapper);
List<DishDto> collect = list.stream().map((item) -> {
DishDto dishDto = new DishDto();
BeanUtils.copyProperties(item, dishDto);
Long categoryId = item.getCategoryId();
if (categoryId != null) {
Category category = categoryService.getById(categoryId);
dishDto.setCategoryName(category.getName());
}
Long dishId = item.getId();
LambdaQueryWrapper<DishFlavor> queryWrapper1 = new LambdaQueryWrapper<>();
queryWrapper1.eq(DishFlavor::getDishId, dishId);
List<DishFlavor> flavors = dishFlavorService.list(queryWrapper1);
dishDto.setFlavors(flavors);
return dishDto;
}).collect(Collectors.toList());
return R.success(collect);
}
}
</code></pre>