用户下单
需求分析
代码的交互过程
准备工作
去支付
发送的请求:
流程:
- 获取当前用户ID
- 查询当前用户的购物车数据
- 像订单插入数据,一条数据
- 像订单明细表插入数据,多条数据(订单里面可能有多条商品)
- 清空购物车数据
- 因为要操作两个表,因此要加入事务注解
OrderServiceImpl.java的代码为:
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
| @Service public class OrderServiceImpl extends ServiceImpl<OrderMapper, Orders> implements OrderService { @Autowired private OrderService orderService;
@Autowired private ShoppingCartService shoppingCartService;
@Autowired private AddressBookService addressBookService;
@Autowired private UserService userService;
@Autowired private OrderDetailService orderDetailService;
@Transactional public void submit(Orders orders){ Long userId = BaseContext.getId();
LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>(); lambdaQueryWrapper.eq(ShoppingCart::getUserId,userId); List<ShoppingCart> shoppingCarts = shoppingCartService.list(lambdaQueryWrapper); if (shoppingCarts==null){ throw new CustomException("购物车为空,不能下单"); }
Long addressBookId = orders.getAddressBookId(); AddressBook addressBook = addressBookService.getById(addressBookId); if (addressBook==null){ throw new CustomException("地址信息为空,不能下单"); }
User user = userService.getById(userId);
long orderId = IdWorker.getId();
AtomicInteger amount = new AtomicInteger();
List<OrderDetail> orderDetails = shoppingCarts.stream().map((item)->{ OrderDetail orderDetail = new OrderDetail(); orderDetail.setOrderId(orderId); orderDetail.setNumber(item.getNumber()); orderDetail.setImage(item.getImage()); orderDetail.setName(item.getName()); orderDetail.setDishId(item.getDishId()); orderDetail.setSetmealId(item.getSetmealId()); orderDetail.setDishFlavor(item.getDishFlavor()); 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.setStatus(2); orders.setUserId(userId); orders.setOrderTime(LocalDateTime.now()); orders.setCheckoutTime(LocalDateTime.now()); orders.setAmount(new BigDecimal(amount.get())); 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()));
orderService.save(orders);
orderDetailService.saveBatch(orderDetails);
shoppingCartService.remove(lambdaQueryWrapper); } }
|
OrderController.java代码为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| @RestController @Slf4j @RequestMapping("/order") public class OrderController {
@Autowired private OrderService orderService;
@RequestMapping("/submit") public R<String> submit(@RequestBody Orders orders){ orderService.submit(orders); return R.success("支付成功"); } }
|
查看订单
发送的请求
代码实现
OrderController.java:
1 2 3 4 5 6 7 8 9 10 11
| @RequestMapping("/userPage") public R<Page> page(int page,int pageSize){ Page pageInfo = new Page(page,pageSize); LambdaQueryWrapper<Orders> lambdaQueryWrapper = new LambdaQueryWrapper<>(); lambdaQueryWrapper.orderByDesc(Orders::getOrderTime); orderService.page(pageInfo,lambdaQueryWrapper);
return R.success(pageInfo); }
|
用户退出登录
发送的请求
代码实现
UserController.java
1 2 3 4 5
| @RequestMapping("/loginout") public R<String> loginout(HttpServletRequest request){ request.getSession().removeAttribute("user"); return R.success("退出成功"); }
|
当订单完成时,再来一单
发送的请求
代码实现
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
|
@RequestMapping("/again") public R<String> again(@RequestBody Orders orders){ Long id = orders.getId(); Orders orderNew = orderService.getById(id); long id1 = IdWorker.getId(); orderNew.setNumber(String.valueOf(id1)); orderNew.setId(IdWorker.getId()); orderNew.setStatus(2);; orderNew.setOrderTime(LocalDateTime.now()); orderNew.setCheckoutTime(LocalDateTime.now()); orderService.save(orderNew); LambdaQueryWrapper<OrderDetail> lambdaQueryWrapper = new LambdaQueryWrapper<>(); lambdaQueryWrapper.eq(OrderDetail::getOrderId,id); List<OrderDetail> list = orderDetailService.list(lambdaQueryWrapper); list.stream().map((item)->{ long id2 = IdWorker.getId(); item.setOrderId(id1); item.setImage(String.valueOf(id2)); return item; }).collect(Collectors.toList()); orderDetailService.saveBatch(list); return R.success("再来一单"); }
|