用户地址簿相关功能
代码开发
@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {
@Autowired
private AddressBookService addressBookService;
/**
* 新增
*/
@PostMapping
public Result<AddressBook> save(@RequestBody AddressBook addressBook) {
addressBook.setUserId(BaseContext.getCurrentId());
log.info("addressBook:{}", addressBook);
addressBookService.save(addressBook);
return Result.success(addressBook);
}
/**
* 设置默认地址
*/
@PutMapping("default")
public Result<AddressBook> setDefault(@RequestBody AddressBook addressBook) {
log.info("addressBook:{}", addressBook);
LambdaUpdateWrapper<AddressBook> wrapper = new LambdaUpdateWrapper<>();
wrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
wrapper.set(AddressBook::getIsDefault, 0);
//SQL:update address_book set is_default = 0 where user_id = ?
addressBookService.update(wrapper);
addressBook.setIsDefault(1);
//SQL:update address_book set is_default = 1 where id = ?
addressBookService.updateById(addressBook);
return Result.success(addressBook);
}
/**
* 根据id查询地址
*/
@GetMapping("/{id}")
public Result get(@PathVariable Long id) {
AddressBook addressBook = addressBookService.getById(id);
if (addressBook != null) {
return Result.success(addressBook);
} else {
return Result.error("没有找到该对象");
}
}
/**
* 查询默认地址
*/
@GetMapping("default")
public Result<AddressBook> getDefault() {
LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
queryWrapper.eq(AddressBook::getIsDefault, 1);
//SQL:select * from address_book where user_id = ? and is_default = 1
AddressBook addressBook = addressBookService.getOne(queryWrapper);
if (null == addressBook) {
return Result.error("没有找到该对象");
} else {
return Result.success(addressBook);
}
}
/**
* 查询指定用户的全部地址
*/
@GetMapping("/list")
public Result<List<AddressBook>> list(AddressBook addressBook) {
addressBook.setUserId(BaseContext.getCurrentId());
log.info("addressBook:{}", addressBook);
//条件构造器
LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(null != addressBook.getUserId(), AddressBook::getUserId, addressBook.getUserId());
queryWrapper.orderByDesc(AddressBook::getUpdateTime);
//SQL:select * from address_book where user_id = ? order by update_time desc
return Result.success(addressBookService.list(queryWrapper));
}
}
菜品展示
代码开发
代码开发-梳理交互过程
在开发代码之前,需要梳理一下前端页面和服务端的交互过程:
1、页面(front/index.html)发送ajax请求,获取分类数据(菜品分类和套餐分类)
2、页面发送ajax请求,获取第一个分类下的菜品或者套餐
开发菜品展示功能,其实就是在服务端编写代码去处理前端页面发送的这2次请求即可。
改造DishController中的list方法:
@GetMapping("/list")
public Result<List<DishDto>> getDishByCategory(Dish dish){
LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
dishLambdaQueryWrapper.eq(Dish::getStatus,1);
dishLambdaQueryWrapper.eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId());
dishLambdaQueryWrapper.like(StringUtils.hasLength(dish.getName()),Dish::getName,dish.getName());
dishLambdaQueryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
List<Dish> dishes = dishService.list(dishLambdaQueryWrapper);
List<DishDto> dishDtos = new LinkedList<>();
dishes.forEach(dishItem->{
DishDto dishDto = new DishDto();
BeanUtils.copyProperties(dishItem,dishDto);
Long dishId = dishDto.getId();
LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId,dishId);
List<DishFlavor> flavors = dishFlavorService.list(dishFlavorLambdaQueryWrapper);
dishDto.setFlavors(flavors);
dishDtos.add(dishDto);
});
return Result.success(dishDtos);
}
在SetmealController里添加list方法显示套餐信息:
@GetMapping("/list")
public Result<List<Setmeal>> list( Setmeal setmeal){
log.info(setmeal.toString());
LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
setmealLambdaQueryWrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
setmealLambdaQueryWrapper.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());
setmealLambdaQueryWrapper.orderByDesc(Setmeal::getUpdateTime);
List<Setmeal> setmeals = setmealService.list(setmealLambdaQueryWrapper);
return Result.success(setmeals);
}
购物车
代码开发
在开发代码之前,需要梳理一下购物车操作时前端页面和服务端的交互过程:
1、点击 [加入购物车] 或者 [+] 按钮,页面发送ajax请求,请求服务端,将菜品或者套餐添加到购物车
添加菜品到购物车
@PostMapping("/add")
public Result<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
log.info(shoppingCart.toString());
Long userId = BaseContext.getCurrentId();
shoppingCart.setUserId(userId);
LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId,userId);
shoppingCartLambdaQueryWrapper.eq(shoppingCart.getDishId()!=null,ShoppingCart::getDishId,shoppingCart.getDishId());
shoppingCartLambdaQueryWrapper.eq(shoppingCart.getSetmealId()!=null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
ShoppingCart shoppingCartInfo = shoppingCartService.getOne(shoppingCartLambdaQueryWrapper);
if (shoppingCartInfo!=null){
Integer number = shoppingCartInfo.getNumber();
shoppingCartInfo.setNumber(number+1);
shoppingCartService.updateById(shoppingCartInfo);
}else {
shoppingCart.setNumber(1);
shoppingCartService.save(shoppingCart);
}
return Result.success(shoppingCart);
}
2、点击购物车图标,页面发送ajax请求,请求服务端查询购物车中的菜品和套餐
查询购物车
@GetMapping("list")
public Result<List<ShoppingCart>> list(){
Long userId = BaseContext.getCurrentId();
LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId,userId);
List<ShoppingCart> shoppingCarts = shoppingCartService.list(shoppingCartLambdaQueryWrapper);
return Result.success(shoppingCarts);
}
3、点击清空购物车按钮,页面发送ajax请求,请求服务端来执行清空购物车操作
清空购物车
@DeleteMapping("/clean")
public Result<String> clean(){
Long userId = BaseContext.getCurrentId();
LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(ShoppingCart::getUserId,userId);
shoppingCartService.remove(queryWrapper);
return Result.success("操作成功");
}
4、点击 [-] 按钮,页面发送ajax请求,请求服务端,将菜品或者套餐从购物车减少
减少商品数量
@PostMapping("/sub")
public Result<String> sub(@RequestBody ShoppingCart shoppingCart){
log.info(shoppingCart.toString());
Long userId = BaseContext.getCurrentId();
LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(userId!=null,ShoppingCart::getUserId,userId);
queryWrapper.eq(shoppingCart.getSetmealId()!=null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
queryWrapper.eq(shoppingCart.getDishId()!=null,ShoppingCart::getDishId,shoppingCart.getDishId());
shoppingCart = shoppingCartService.getOne(queryWrapper);
if (shoppingCart!=null&&shoppingCart.getNumber()-1>1){
shoppingCart.setNumber(shoppingCart.getNumber()-1);
shoppingCartService.updateById(shoppingCart);
}else {
shoppingCartService.remove(queryWrapper);
}
return Result.success("操作成功");
}
开发购物车功能,其实就是在服务端编写代码去处理前端页面发送的这3次请求即可。
下单
代码开发
代码开发-梳理交互过程
在开发代码之前,需要梳理一下用户下单操作时前端页面和服务端的交互过程:
1、在购物车中点击 【去结算】 按钮,页面跳转到订单确认页面
2、在订单确认页面,发送ajax请求,请求服务端获取当前登录用户的默认地址
3、在订单确认页面,发送ajax请求,请求服务端获取当前登录用户的购物车数据
4、在订单确认页面点击 【去支付】 按钮,发送ajax请求,请求服务端完成下单操作
开发用户下单功能,其实就是在服务端编写代码去处理前端页面发送的请求即可。
在OrderService添加submit方法用于用户下单
@Override
@Transactional
public void submit(Orders orders) {
//获取当前用户id
Long currentId = BaseContext.getCurrentId();
//查询当前用户的购物车数据
LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
queryWrapper.eq(ShoppingCart::getUserId,currentId);
List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
if (list==null||list.size()==0){
throw new CustomException("购物车为空,不能下单");
}
//查询用户数据
User user = userService.getById(currentId);
//查询地址数据
Long addressBookId = orders.getAddressBookId();
AddressBook addressBook = addressBookService.getById(addressBookId);
if(addressBook==null){
throw new CustomException("地址有误,不能下单");
}
long orderId = IdWorker.getId();//订单号
AtomicInteger amount=new AtomicInteger(0);
List<OrderDetail> orderDetails=list.stream().map((item)->{
OrderDetail orderDetail = new OrderDetail();
orderDetail.setOrderId(orderId);
orderDetail.setNumber(item.getNumber());
orderDetail.setDishFlavor(item.getDishFlavor());
orderDetail.setDishId(item.getDishId());
orderDetail.setSetmealId(item.getSetmealId());
orderDetail.setName(item.getName());
orderDetail.setImage(item.getImage());
orderDetail.setAmount(item.getAmount());
amount.addAndGet(item.getAmount().multiply(new BigDecimal(item.getNumber())).intValue());
return orderDetail;
}).collect(Collectors.toList());
//向订单表中插入一条数据
orders.setNumber(String.valueOf(orderId));
orders.setId(orderId);
orders.setOrderTime(new Date());
orders.setCheckoutTime(new Date());
orders.setStatus(2);
orders.setAmount(new BigDecimal(amount.get()));//计算总金额
orders.setUserId(currentId);
orders.setUserName(user.getName());
orders.setConsignee(addressBook.getConsignee());
orders.setPhone(addressBook.getPhone());
orders.setAddress((addressBook.getProvinceName()==null?"":addressBook.getProvinceName())
+(addressBook.getCityName()==null?"":addressBook.getCityName())
+(addressBook.getDistrictName()==null?"":addressBook.getDistrictName())
+(addressBook.getDetail()==null?"":addressBook.getDetail()));
this.save(orders);
//向订单明细表中插入多条数据
orderDetailService.saveBatch(orderDetails);
//清空购物车数据
shoppingCartService.remove(queryWrapper);
}
标签:queryWrapper,LambdaQueryWrapper,addressBook,瑞吉,shoppingCart,Result,外卖,new
From: https://www.cnblogs.com/godofball/p/17229579.html