瑞吉外卖-移动端用户下单

用户下单

需求分析

image-20220818100327671

代码的交互过程

image-20220818101000799

准备工作

image-20220818101241566

去支付

发送的请求:

image-20220818102037249

image-20220818102048544

流程:

  • 获取当前用户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){
//获取用户ID
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);

//通过IdWork生成订单号

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("支付成功");
}
}

查看订单

发送的请求

image-20220818203847751

代码实现

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);
}

用户退出登录

发送的请求

image-20220818204116522

代码实现

UserController.java

1
2
3
4
5
@RequestMapping("/loginout")
public R<String> loginout(HttpServletRequest request){
request.getSession().removeAttribute("user");
return R.success("退出成功");
}

当订单完成时,再来一单

发送的请求

image-20220818211327177

代码实现

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("再来一单");
}

瑞吉外卖-移动端用户下单
http://example.com/2022/08/18/瑞吉外卖-移动端用户下单/
作者
zlw
发布于
2022年8月18日
许可协议