package com.example.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.R;
import com.example.dto.DishDto;
import com.example.entity.Category;
import com.example.entity.Dish;
import com.example.entity.DishFlavor;
import com.example.service.CategoryService;
import com.example.service.DishFlavorService;
import com.example.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
@Slf4j
@RestController
@RequestMapping("/dish")
public class DishController {
@Autowired
private DishService dishService;
@Autowired
private CategoryService categoryService;
@Autowired
private DishFlavorService dishFlavorService;
//分页查询
@GetMapping("/page")
public R<Page<DishDto>> pageR(int page,int pageSize,String name){
log.info("page={},pagesize={},name={}",page,pageSize,name);
//构造分页器
Page<Dish> pageinfo=new Page<>(page,pageSize);
Page<DishDto> dishDtoPage=new Page<>();
//构造条件查询器
LambdaQueryWrapper<Dish> lqw=new LambdaQueryWrapper<>();
lqw.like(name!=null,Dish::getName,name);
//使数据按照修改时间降序排列
lqw.orderByDesc(Dish::getUpdateTime);
dishService.page(pageinfo,lqw);
//对象拷贝
BeanUtils.copyProperties(pageinfo,dishDtoPage,"records");
//完成属性拷贝
List<Dish> records = pageinfo.getRecords();
List<DishDto> list=new ArrayList<>();
for (Dish dish:records){
DishDto dishDto=new DishDto();
//对象拷贝,将需要的其余属性从dish拷贝到list
BeanUtils.copyProperties(dish,dishDto);
Long id = dish.getCategoryId();
Category category = categoryService.getById(id);
String categoryName = category.getName();
//成功设置categoryName和其余属性
dishDto.setCategoryName(categoryName);
list.add(dishDto);
}
//设置Page对象属性
dishDtoPage.setRecords(list);
log.info("dishDtoPage:{}",dishDtoPage);
return R.success(dishDtoPage);
}
//添加
@PostMapping
public R<String> save(@RequestBody DishDto dishDto){
log.info("dishDto:{}",dishDto);
dishService.saveWithFlavor(dishDto);
return R.success("新增菜品成功");
}
//按照ID查询
@GetMapping("/{id}")
public R<DishDto> getById(@PathVariable Long id){
DishDto dishDto = dishService.getByIdWithFlavor(id);
log.info("dishDto:{}",dishDto);
return R.success(dishDto);
}
//修改
@PutMapping
public R<String> updateWithFlavor(@RequestBody DishDto dishDto){
log.info("dishDto:{}",dishDto);
dishService.updateWithFlavor(dishDto);
return R.success("修改菜品成功");
}
//删除,批量删除
@DeleteMapping
public R<String> delete(@RequestParam List<Long> ids){
log.info("ids:{}",ids);
dishService.deleteByIds(ids);
return R.success("删除成功");
}
//改变售卖状态
@PostMapping("/status/{status}")
public R<String> statusChange(@PathVariable int status,String ids){
log.info("status:{},ids:{}",status,ids);
dishService.UpdateStatus(status,ids);
String string= status==1?"起售成功!":"停售成功";
return R.success(string);
}
//根据分类id获取菜品数据
@GetMapping("/list")
public R<List<DishDto>> getlistByCategoryId(Dish dish1){
//获取categoryid
log.info("dish1:{}",dish1);
//根据categoryid查询dish,并按照更新时间降序排列
LambdaQueryWrapper<Dish> lqw=new LambdaQueryWrapper<>();
//如果有id,根据id查询
lqw.eq(dish1.getCategoryId()!=null,Dish::getCategoryId,dish1.getCategoryId());
//只查询起售状态得菜品
lqw.eq(Dish::getStatus,1);
lqw.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
List<Dish> list = dishService.list(lqw);
LambdaQueryWrapper<DishFlavor> lqw2=new LambdaQueryWrapper<>();
List<DishDto> dtoList=new ArrayList<>();
for (Dish dish:list){
DishDto dishDto=new DishDto();
BeanUtils.copyProperties(dish,dishDto);
Long id = dish.getId();
lqw2.eq(DishFlavor::getDishId,id);
List<DishFlavor> dishFlavors = dishFlavorService.list(lqw2);
dishDto.setFlavors(dishFlavors);
dtoList.add(dishDto);
lqw2.clear();
}
log.info("DishListByCategoryId:{}",dtoList.toString());
return R.success(dtoList);
}
}
评论0