day06
-导入用户地址簿相关代码:
首先根据登录id 查询所有地址
@GetMapping("/list") public R<List<AddressBook>> list(AddressBook addressBook){ addressBook.setUserId(BaseContext.getCurrentId()); log.info("addressBook={}",addressBook); //条件构造器 LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(addressBook.getUserId() != null, AddressBook::getUserId,addressBook.getUserId()); queryWrapper.orderByDesc(AddressBook::getUpdateTime); List<AddressBook> list = addressBookService.list(queryWrapper); return R.success(list); }
然后加入新的
@PostMapping public R<AddressBook> save(@RequestBody AddressBook addressBook){ addressBook.setUserId(BaseContext.getCurrentId()); log.info("addressBook={}",addressBook); addressBookService.save(addressBook); return R.success(addressBook); }
完善页面 就是在请求的基础上 以前是dish 现在用了dto
@GetMapping("/list") public R<List<DishDto>> list(Dish dish){ //构造查询条件 LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(dish.getCategoryId() != null, Dish::getCategoryId, dish.getCategoryId()); //添加条件,查询状态为1(1为起售,0为停售)的菜品 queryWrapper.eq(Dish::getStatus,1); List<Dish> list = dishService.list(queryWrapper); List<DishDto> dishDtoList = list.stream().map((item) -> { DishDto dishDto = new DishDto(); //对象拷贝(每一个list数据) BeanUtils.copyProperties(item,dishDto); Long categoryId = item.getCategoryId(); //分类id //通过categoryId查询到category内容 Category category = categoryService.getById(categoryId); //判空 if(category != null){ String categoryName = category.getName(); dishDto.setCategoryName(categoryName); } //获取当前菜品id Long dishId = item.getId(); //构造条件构造器 LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper= new LambdaQueryWrapper<>(); //添加查询条件 dishFlavorLambdaQueryWrapper.eq(dishId != null,DishFlavor::getDishId,dishId); //select * from dish_flavors where dish_id = ? List<DishFlavor> dishFlavors = dishFlavorService.list(dishFlavorLambdaQueryWrapper); dishDto.setFlavors(dishFlavors); return dishDto; }).collect(Collectors.toList()); return R.success(dishDtoList); }
购物车:
添加菜品 有一个购物车专用的数据库 在对应的页面上点击 然后会返回到contr 这个时候需要判断 这个菜是不是套餐 是套餐之前有没有 没有添加 有了家数
@PostMapping("/add") public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){ log.info("shoppingCart={}",shoppingCart); //设置用户id指定当前是哪个用户的购物车数据 Long currentId = BaseContext.getCurrentId(); shoppingCart.setUserId(currentId); Long dishId = shoppingCart.getDishId(); //条件构造器 LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>(); if(dishId!=null){queryWrapper.eq(ShoppingCart::getDishId,dishId);} else {queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());}; ShoppingCart one = shoppingCartService.getOne(queryWrapper); if(one!=null){ Integer number = one.getNumber(); one.setNumber(number+1); shoppingCartService.updateById(one); } else { shoppingCartService.save(shoppingCart); one=shoppingCart; } return R.success(one); }
查看购物车&清空购物车
查看购物车很简单 就是用一个查卡布list的方式
@GetMapping("/list") public R<List<ShoppingCart>> list(){ Long currentId = BaseContext.getCurrentId(); LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>(); queryWrapper.eq(ShoppingCart::getUserId,currentId); List<ShoppingCart> list = shoppingCartService.list(queryWrapper); return R.success(list); }
清空
DeleteMapping("/clean") public R<String> clean(){ LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId()); //SQL:delete from shopping_cart where user_id = ? shoppingCartService.remove(queryWrapper); return R.success("成功清空购物车"); }
1支付界面:
第一:要先把用户地址设为默认
就是先updata一个构造器 先把其他所有更新为0
然后只要这一个为1
@PutMapping("/default") public R<AddressBook> getDefault(@RequestBody AddressBook addressBook){ addressBook.setUserId(BaseContext.getCurrentId()); //条件构造器 LambdaUpdateWrapper<AddressBook> updateWrapper = new LambdaUpdateWrapper<>(); updateWrapper.eq(addressBook.getUserId() != null,AddressBook::getUserId,addressBook.getUserId()); updateWrapper.set(AddressBook::getIsDefault,0); //将与用户id所关联的所有地址的is_default字段更新为0 addressBookService.update(updateWrapper); addressBook.setIsDefault(1); //再将前端传递的地址id的is_default字段更新为1 addressBookService.updateById(addressBook); return R.success(addressBook); }
第二 添加一个default
直接返回一个默认的地址
@GetMapping("/default") public R<AddressBook> getDefault(){ //当前用户id Long currentId = BaseContext.getCurrentId(); //创建条件构造器 LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(AddressBook::getUserId,currentId); queryWrapper.eq(AddressBook::getIsDefault,1); AddressBook addressBook = addressBookService.getOne(queryWrapper); return R.success(addressBook); }
处理购物车:
package org.example.reggie.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.reggie.common.BaseContext;
import org.example.reggie.common.CustomException;
import org.example.reggie.entity.*;
import org.example.reggie.mapper.OrderMapper;
import org.example.reggie.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
@Service
public class OrderServicelmpl extends ServiceImpl<OrderMapper, Orders> implements OrderService {
@Autowired
private ShoppingCartService shoppingCartService;
@Autowired
private UserService userService;
@Autowired
private AddressBookService addressBookService;
@Autowired
private OrderDetailService orderDetailService;
@Override
public void submit(Orders orders) {
Long currentId = BaseContext.getCurrentId();
LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
queryWrapper.eq(ShoppingCart::getUserId,currentId);
List<ShoppingCart> shoppingCarts = shoppingCartService.list(queryWrapper);
if (shoppingCarts == null || shoppingCarts.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> orderDetailList= shoppingCarts.stream().map((item) -> {
OrderDetail orderDetail = new OrderDetail();
orderDetail.setOrderId(orderId);
orderDetail.setName(item.getName());
orderDetail.setImage(item.getImage());
orderDetail.setDishId(item.getDishId());
orderDetail.setSetmealId(item.getSetmealId());
orderDetail.setDishFlavor(item.getDishFlavor());
orderDetail.setNumber(item.getNumber());
orderDetail.setAmount(item.getAmount());
amount.addAndGet(item.getAmount().multiply(new BigDecimal(item.getNumber())).intValue());
return orderDetail;
}).collect(Collectors.toList());
//向订单表设置属性
orders.setId(orderId);
orders.setNumber(String.valueOf(orderId));
orders.setStatus(2);
orders.setUserId(currentId);
orders.setAddressBookId(addressBookId);
orders.setOrderTime(LocalDateTime.now());
orders.setCheckoutTime(LocalDateTime.now());
orders.setAmount(new BigDecimal(amount.get()));
orders.setPhone(addressBook.getPhone());
orders.setUserName(user.getName());
orders.setConsignee(addressBook.getConsignee());
orders.setAddress(
(addressBook.getProvinceName() == null ? "":addressBook.getProvinceName())+
(addressBook.getCityName() == null ? "":addressBook.getCityName())+
(addressBook.getDistrictName() == null ? "":addressBook.getDistrictName())+
(addressBook.getDetail() == null ? "":addressBook.getDetail())
);
//向订单表插入数据,一条数据
super.save(orders);
//向订单明细表插入数据,多条数据
orderDetailService.saveBatch(orderDetailList);
//清空购物车数据
shoppingCartService.remove(queryWrapper);
}
}