苍穹外卖学习笔记(九)

用户端历史订单模块:

查询历史订单

OrderController

1
2
3
4
5
6
7
8
9
/**
* 历史订单
*/
@GetMapping("/historyOrders")
@ApiOperation(value = "历史订单")
public Result<PageResult> page(int page, int pageSize, Integer status) {
PageResult pageResult = orderService.pageQuery4User(page, pageSize, status);
return Result.success(pageResult);
}

OrderService

1
2
3
4
5
/**
* 分页查询用户订单
*/
PageResult pageQuery4User(int page, int pageSize, Integer status);

OrderServiceImpl

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
/**
* 分页查询用户订单
*/
@Override
public PageResult pageQuery4User(int pageNum, int pageSize, Integer status) {
Page<Orders> page = new Page<>(pageNum, pageSize);
OrdersPageQueryDTO ordersPageQueryDTO = new OrdersPageQueryDTO();
ordersPageQueryDTO.setUserId(BaseContext.getCurrentId());
ordersPageQueryDTO.setStatus(status);
//查询条件
LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(Orders::getUserId, BaseContext.getCurrentId());

if (status != null) {
queryWrapper.eq(Orders::getStatus, status);
}
if (ordersPageQueryDTO.getNumber() != null && !ordersPageQueryDTO.getNumber().isEmpty()) {
queryWrapper.like(Orders::getNumber, ordersPageQueryDTO.getNumber());
}
if (ordersPageQueryDTO.getPhone() != null && !ordersPageQueryDTO.getPhone().isEmpty()) {
queryWrapper.like(Orders::getPhone, ordersPageQueryDTO.getPhone());
}
if (ordersPageQueryDTO.getBeginTime() != null) {
queryWrapper.ge(Orders::getOrderTime, ordersPageQueryDTO.getBeginTime());
}
if (ordersPageQueryDTO.getEndTime() != null) {
queryWrapper.le(Orders::getOrderTime, ordersPageQueryDTO.getEndTime());
}

Page<Orders> ordersPage = orderMapper.selectPage(page, queryWrapper);
List<OrderVO> list = new ArrayList<>();
if (ordersPage != null && ordersPage.getTotal() > 0) {
for (Orders orders : ordersPage.getRecords()) {
Long orderId = orders.getId();

LambdaQueryWrapper<OrderDetail> orderDetailLambdaQueryWrapper = new LambdaQueryWrapper<>();
orderDetailLambdaQueryWrapper.eq(OrderDetail::getOrderId, orderId);
List<OrderDetail> orderDetails = orderDetailMapper.selectList(orderDetailLambdaQueryWrapper);

OrderVO orderVO = new OrderVO();
BeanUtils.copyProperties(orders, orderVO);
orderVO.setOrderDetailList(orderDetails);

list.add(orderVO);
}
}
assert ordersPage != null;//用于断言
return new PageResult(ordersPage.getTotal(), list);
}

查询订单详情

OrderController

1
2
3
4
5
6
7
8
9
/**
* 订单详情
*/
@GetMapping("/orderDetail/{id}")
@ApiOperation(value = "订单详情")
public Result<OrderVO> details(@PathVariable Long id) {
OrderVO orderVO = orderService.details(id);
return Result.success(orderVO);
}

OrderService

1
2
3
4
/**
* 订单详情
*/
OrderVO details(Long id);

OrderServiceImpl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
* 订单详情
*/
@Override
public OrderVO details(Long id) {
Orders orders = orderMapper.selectById(id);
LambdaQueryWrapper<OrderDetail> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(OrderDetail::getOrderId, id);
List<OrderDetail> orderDetailList = orderDetailMapper.selectList(queryWrapper);
OrderVO orderVO = new OrderVO();
BeanUtils.copyProperties(orders, orderVO);
orderVO.setOrderDetailList(orderDetailList);
return orderVO;
}

取消订单

OrderController

1
2
3
4
5
6
7
8
9
/**
* 取消订单
*/
@PutMapping("/cancel/{id}")
@ApiOperation("取消订单")
public Result cancel(@PathVariable("id") Long id) throws Exception {
orderService.userCancelById(id);
return Result.success();
}

OrderService

1
2
3
4
5
/**
* 用户取消订单
*/
void userCancelById(Long id) throws Exception;

OrderServiceImpl

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
/**
* 用户取消订单
*/
@Transactional
@Override
public void userCancelById(Long id) throws Exception {
// 根据订单id查询订单
Orders ordersDB = orderMapper.selectById(id);
//校验订单是否存在
if (ordersDB == null) {
throw new OrderBusinessException(MessageConstant.ORDER_NOT_FOUND);
}
//订单状态 1待付款 2待接单 3已接单 4派送中 5已完成 6已取消
if (ordersDB.getStatus() > 2) {
throw new OrderBusinessException(MessageConstant.ORDER_STATUS_ERROR);
}
Orders orders = Orders.builder()
.id(ordersDB.getId())
.status(Orders.CANCELLED)
.cancelReason("用户取消")
.cancelTime(LocalDateTime.now())
.build();

// 订单处于待接单状态下取消,需要进行退款
if (ordersDB.getStatus().equals(Orders.TO_BE_CONFIRMED)) {
//调用微信支付退款接口
// weChatPayUtil.refund(
// ordersDB.getNumber(), //商户订单号
// ordersDB.getNumber(), //商户退款单号
// new BigDecimal(0.01),//退款金额,单位 元
// new BigDecimal(0.01));//原订单金额

//支付状态修改为 退款
orders.setPayStatus(Orders.REFUND);
}

orderMapper.update(orders, new LambdaQueryWrapper<Orders>().eq(Orders::getId, id));
}

再来一单

OrderController

1
2
3
4
5
6
7
8
9
/**
* 再来一单
*/
@PostMapping("/repetition/{id}")
@ApiOperation("再来一单")
public Result repetition(@PathVariable Long id) {
orderService.repetition(id);
return Result.success();
}

OrderService

1
2
3
4
/**
* 再来一单
*/
void repetition(Long id);

OrderServiceImpl

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
/**
* 再来一单
*/
@Override
@Transactional
public void repetition(Long id) {
Long userId = BaseContext.getCurrentId();
LambdaQueryWrapper<OrderDetail> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(OrderDetail::getOrderId, id);
List<OrderDetail> orderDetailList = orderDetailMapper.selectList(queryWrapper);
List<ShoppingCart> shoppingCartList = orderDetailList.stream().map(x -> {//将订单详情转换为购物车对象
ShoppingCart shoppingCart = new ShoppingCart();

// 将原订单详情里面的菜品信息重新复制到购物车对象中
BeanUtils.copyProperties(x, shoppingCart, "id");
shoppingCart.setUserId(userId);
shoppingCart.setCreateTime(LocalDateTime.now());

return shoppingCart;
}).collect(Collectors.toList());

// 将购物车对象批量添加到数据库
MybatisBatch<ShoppingCart> mybatisBatch = new MybatisBatch<>(sqlSessionFactory, shoppingCartList);
MybatisBatch.Method<ShoppingCart> method = new MybatisBatch.Method<>(ShoppingCartMapper.class);
mybatisBatch.execute(method.insert());
}

商家端订单管理模块:

订单搜索

OrderController

1
2
3
4
5
6
7
8
9
/**
* 订单搜索
*/
@GetMapping("/conditionSearch")
@ApiOperation(value = "订单搜索")
public Result<PageResult> conditionSearch(OrdersPageQueryDTO ordersPageQueryDTO) {
PageResult pageResult = orderService.conditionSearch(ordersPageQueryDTO);
return Result.success(pageResult);
}

OrderService

1
2
3
4
/**
* 订单搜索
*/
PageResult conditionSearch(OrdersPageQueryDTO ordersPageQueryDTO);

OrderServiceImpl

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
/**
* 订单搜索
*/
@Override
public PageResult conditionSearch(OrdersPageQueryDTO ordersPageQueryDTO) {
Page<Orders> page = new Page<>(ordersPageQueryDTO.getPage(), ordersPageQueryDTO.getPageSize());
LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
if (ordersPageQueryDTO.getStatus() != null) {
queryWrapper.eq(Orders::getStatus, ordersPageQueryDTO.getStatus());
}
if (ordersPageQueryDTO.getNumber() != null && !ordersPageQueryDTO.getNumber().isEmpty()) {
queryWrapper.like(Orders::getNumber, ordersPageQueryDTO.getNumber());
}
if (ordersPageQueryDTO.getPhone() != null && !ordersPageQueryDTO.getPhone().isEmpty()) {
queryWrapper.like(Orders::getPhone, ordersPageQueryDTO.getPhone());
}
if (ordersPageQueryDTO.getBeginTime() != null) {
queryWrapper.ge(Orders::getOrderTime, ordersPageQueryDTO.getBeginTime());
}
if (ordersPageQueryDTO.getEndTime() != null) {
queryWrapper.le(Orders::getOrderTime, ordersPageQueryDTO.getEndTime());
}
Page<Orders> ordersPage = orderMapper.selectPage(page, queryWrapper);
// 部分订单状态,需要额外返回订单菜品信息,将Orders转化为OrderVO
List<OrderVO> orderVOList = getOrderVOList(ordersPage);
return new PageResult(page.getTotal(), orderVOList);
}
/**
* 将Orders转化为OrderVO
*/
private List<OrderVO> getOrderVOList(Page<Orders> page) {
// 需要返回订单菜品信息,自定义OrderVO响应结果
List<OrderVO> orderVOList = new ArrayList<>();

List<Orders> ordersList = page.getRecords();
if (!CollectionUtils.isEmpty(ordersList)) {
for (Orders orders : ordersList) {
// 将共同字段复制到OrderVO
OrderVO orderVO = new OrderVO();
BeanUtils.copyProperties(orders, orderVO);
String orderDishes = getOrderDishesStr(orders);
// 将订单菜品信息封装到orderVO中,并添加到orderVOList
orderVO.setOrderDishes(orderDishes);
orderVOList.add(orderVO);
}
}
return orderVOList;
}

/**
* 根据订单id获取菜品信息字符串
*/
private String getOrderDishesStr(Orders orders) {
// 查询订单菜品详情信息(订单中的菜品和数量)
LambdaQueryWrapper<OrderDetail> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(OrderDetail::getOrderId, orders.getId());
List<OrderDetail> orderDetailList = orderDetailMapper.selectList(queryWrapper);

// 将每一条订单菜品信息拼接为字符串(格式:宫保鸡丁*3;)
List<String> orderDishList = orderDetailList.stream().map(x -> {
String orderDish = x.getName() + "*" + x.getNumber() + ";";
return orderDish;
}).toList();
// 将该订单对应的所有菜品信息拼接在一起
return String.join("", orderDishList);
}

各个状态的订单数量统计

OrderController

1
2
3
4
5
6
7
8
9
/**
* 各个状态的订单数量统计
*/
@GetMapping("/statistics")
@ApiOperation("各个状态的订单数量统计")
public Result<OrderStatisticsVO> statistics() {
OrderStatisticsVO orderStatisticsVO = orderService.statistics();
return Result.success(orderStatisticsVO);
}

OrderService

1
2
3
4
/**
* 各个状态的订单数量统计
*/
OrderStatisticsVO statistics();

OrderServiceImpl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
* 各个状态的订单数量统计
*/
@Override
public OrderStatisticsVO statistics() {
// 根据状态,分别查询出待接单、待派送、派送中的订单数量
Integer toBeConfirmed = orderMapper.countStatus(Orders.TO_BE_CONFIRMED);
Integer confirmed = orderMapper.countStatus(Orders.CONFIRMED);
Integer deliveryInProgress = orderMapper.countStatus(Orders.DELIVERY_IN_PROGRESS);

// 将查询出的数据封装到orderStatisticsVO中响应
OrderStatisticsVO orderStatisticsVO = new OrderStatisticsVO();
orderStatisticsVO.setToBeConfirmed(toBeConfirmed);
orderStatisticsVO.setConfirmed(confirmed);
orderStatisticsVO.setDeliveryInProgress(deliveryInProgress);
return orderStatisticsVO;
}

orderMapper

1
2
3
4
5
/**
* 统计订单状态
*/
@Select("select count(id) from orders where status = #{status}")
Integer countStatus(Integer toBeConfirmed);

查询订单详情

OrderController

1
2
3
4
5
6
7
8
9
/**
* 订单详情
*/
@GetMapping("/details/{id}")
@ApiOperation("订单详情")
public Result<OrderVO> details(@PathVariable Long id) {
OrderVO orderVO = orderService.details(id);
return Result.success(orderVO);
}

接单

OrderController

1
2
3
4
5
6
7
8
9
/**
* 接单
*/
@PutMapping("/confirm")
@ApiOperation("接单")
public Result confirm(@RequestBody OrdersConfirmDTO ordersConfirmDTO) {
orderService.confirm(ordersConfirmDTO);
return Result.success();
}

OrderService

1
2
3
4
/**
* 接单
*/
void confirm(OrdersConfirmDTO ordersConfirmDTO);

OrderServiceImpl

1
2
3
4
5
6
7
8
9
10
11
 /**
* 接单
*/
@Override
public void confirm(OrdersConfirmDTO ordersConfirmDTO) {
Orders orders = Orders.builder()
.id(ordersConfirmDTO.getId())
.status(Orders.CONFIRMED)
.build();
orderMapper.updateById(orders);
}

拒单

OrderController

1
2
3
4
5
6
7
8
9
/**
* 拒单
*/
@PutMapping("/rejection")
@ApiOperation("拒单")
public Result rejection(@RequestBody OrdersRejectionDTO ordersRejectionDTO) throws Exception {
orderService.rejection(ordersRejectionDTO);
return Result.success();
}

OrderService

1
2
3
4
/**
* 拒单
*/
void rejection(OrdersRejectionDTO ordersRejectionDTO) throws Exception;

OrderServiceImpl

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
    /**
* 拒单
*/
@Override
public void rejection(OrdersRejectionDTO ordersRejectionDTO) throws Exception{
// 根据id查询订单
Orders ordersDB = orderMapper.selectById(ordersRejectionDTO.getId());

// 订单只有存在且状态为2(待接单)才可以拒单
if (ordersDB == null || !ordersDB.getStatus().equals(Orders.TO_BE_CONFIRMED)) {
throw new OrderBusinessException(MessageConstant.ORDER_STATUS_ERROR);
}

//支付状态
Integer payStatus = ordersDB.getPayStatus();
//订单状态
if (payStatus.equals(Orders.PAID)) {
//调用微信支付退款接口,用户已支付,需要退款
// String refund = weChatPayUtil.refund(
// ordersDB.getNumber(),
// ordersDB.getNumber(),
// new BigDecimal(0.01),
// new BigDecimal(0.01));
// log.info("申请退款:{}", refund);
}

// 拒单需要退款,根据订单id更新订单状态、拒单原因、取消时间
Orders orders = new Orders();
orders.setId(ordersDB.getId());
orders.setStatus(Orders.CANCELLED);
orders.setRejectionReason(ordersRejectionDTO.getRejectionReason());
orders.setCancelTime(LocalDateTime.now());

orderMapper.updateById(orders);
}

取消订单

OrderController

1
2
3
4
5
6
7
8
9
 /**
* 取消订单
*/
@PutMapping("/cancel")
@ApiOperation("取消订单")
public Result cancel(@RequestBody OrdersCancelDTO ordersCancelDTO) throws Exception {
orderService.cancel(ordersCancelDTO);
return Result.success();
}

OrderService

1
2
3
4
/**
* 取消订单
*/
void cancel(OrdersCancelDTO ordersCancelDTO) throws Exception;

OrderServiceImpl

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
     /**
* 取消订单
*/
@Override
public void cancel(OrdersCancelDTO ordersCancelDTO) throws Exception {
// 根据id查询订单
Orders ordersDB = orderMapper.selectById(ordersCancelDTO.getId());

//支付状态
Integer payStatus = ordersDB.getPayStatus();
//订单状态
if (payStatus.equals(Orders.PAID)) {
//调用微信支付退款接口,用户已支付,需要退款
// String refund = weChatPayUtil.refund(
// ordersDB.getNumber(),
// ordersDB.getNumber(),
// new BigDecimal(0.01),
// new BigDecimal(0.01));
// log.info("申请退款:{}", refund);
}

// 管理端取消订单需要退款,根据订单id更新订单状态、取消原因、取消时间
Orders orders = new Orders();
orders.setId(ordersCancelDTO.getId());
orders.setStatus(Orders.CANCELLED);
orders.setCancelReason(ordersCancelDTO.getCancelReason());
orders.setCancelTime(LocalDateTime.now());
orderMapper.updateById(orders);
}

派送订单

OrderController

1
2
3
4
5
6
7
8
9
/**
* 派送订单
*/
@PutMapping("/delivery/{id}")
@ApiOperation("派送订单")
public Result delivery(@PathVariable("id") Long id) {
orderService.delivery(id);
return Result.success();
}

OrderService

1
2
3
4
/**
* 派送订单
*/
void delivery(Long id);

OrderServiceImpl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
* 派送订单
*/
@Override
public void delivery(Long id) {
// 根据id查询订单
Orders ordersDB = orderMapper.selectById(id);

// 订单只有存在且状态为3(已接单)才可以派送
if (ordersDB == null || !ordersDB.getStatus().equals(Orders.CONFIRMED)) {
throw new OrderBusinessException(MessageConstant.ORDER_STATUS_ERROR);
}

// 根据订单id更新订单状态、派送时间
Orders orders = Orders.builder()
.id(ordersDB.getId())
.status(Orders.DELIVERY_IN_PROGRESS)
.build();
orderMapper.updateById(orders);
}

完成订单

OrderController

1
2
3
4
5
6
7
8
9
/**
* 完成订单
*/
@PutMapping("/complete/{id}")
@ApiOperation("完成订单")
public Result complete (@PathVariable("id") Long id) {
orderService.complete(id);
return Result.success();
}

OrderService

1
2
3
4
/**
* 完成订单
*/
void complete(Long id);

OrderServiceImpl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
* 完成订单
*/
@Override
public void complete(Long id) {
// 根据id查询订单
Orders ordersDB = orderMapper.selectById(id);

// 校验订单是否存在,并且状态为4
if (ordersDB == null || !ordersDB.getStatus().equals(Orders.DELIVERY_IN_PROGRESS)) {
throw new OrderBusinessException(MessageConstant.ORDER_STATUS_ERROR);
}

// 根据订单id更新订单状态、完成时间
Orders orders = Orders.builder()
.id(ordersDB.getId())
.status(Orders.COMPLETED)
.deliveryTime(LocalDateTime.now())
.build();
orderMapper.updateById(orders);
}

校验收货地址是否超出配送范围

登录百度地图开放平台:https://lbsyun.baidu.com/
20241015164424

20241015164437

相关接口:

https://lbsyun.baidu.com/index.php?title=webapi/guide/webservice-geocoding

https://lbsyun.baidu.com/index.php?title=webapi/directionlite-v1

application

1
2
3
4
shop:
address: ${sky.shop.address}
baidu:
ak: ${sky.baidu.ak}

OrderServiceImpl

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

@Value("${sky.shop.address}")
private String shopAddress;

@Value("${sky.baidu.ak}")
private String ak;

/**
* 检查客户的收货地址是否超出配送范围
*/
private void checkOutOfRange(String address) {
Map map = new HashMap();
map.put("address",shopAddress);
map.put("output","json");
map.put("ak",ak);

//获取店铺的经纬度坐标
String shopCoordinate = HttpClientUtil.doGet("https://api.map.baidu.com/geocoding/v3", map);

JSONObject jsonObject = JSON.parseObject(shopCoordinate);
if(!jsonObject.getString("status").equals("0")){
throw new OrderBusinessException("店铺地址解析失败");
}

//数据解析
JSONObject location = jsonObject.getJSONObject("result").getJSONObject("location");
String lat = location.getString("lat");
String lng = location.getString("lng");
//店铺经纬度坐标
String shopLngLat = lat + "," + lng;

map.put("address",address);
//获取用户收货地址的经纬度坐标
String userCoordinate = HttpClientUtil.doGet("https://api.map.baidu.com/geocoding/v3", map);

jsonObject = JSON.parseObject(userCoordinate);
if(!jsonObject.getString("status").equals("0")){
throw new OrderBusinessException("收货地址解析失败");
}

//数据解析
location = jsonObject.getJSONObject("result").getJSONObject("location");
lat = location.getString("lat");
lng = location.getString("lng");
//用户收货地址经纬度坐标
String userLngLat = lat + "," + lng;

map.put("origin",shopLngLat);
map.put("destination",userLngLat);
map.put("steps_info","0");

//路线规划
String json = HttpClientUtil.doGet("https://api.map.baidu.com/directionlite/v1/driving", map);

jsonObject = JSON.parseObject(json);
if(!jsonObject.getString("status").equals("0")){
throw new OrderBusinessException("配送路线规划失败");
}

//数据解析
JSONObject result = jsonObject.getJSONObject("result");
JSONArray jsonArray = (JSONArray) result.get("routes");
Integer distance = (Integer) ((JSONObject) jsonArray.get(0)).get("distance");

if(distance > 5000){
//配送距离超过5000米
throw new OrderBusinessException("超出配送范围");
}

改进用户下单

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
/**
* 用户下单
*/
@Override
@Transactional
public OrderSubmitVO submitOrder(OrdersSubmitDTO ordersSubmitDTO) {
//处理各种业务异常(地址簿为空,购物车为空,商品库存不足等)
AddressBook addressBook = addressBookMapper.selectById(ordersSubmitDTO.getAddressBookId());
if (addressBook == null) {
throw new AddressBookBusinessException(MessageConstant.ADDRESS_BOOK_IS_NULL);
}

//检查收货地址是否超出配送范围
checkOutOfRange(addressBook.getProvinceName() + addressBook.getDistrictName() + addressBook.getDetail());

Long currentId = BaseContext.getCurrentId();
ShoppingCart shoppingCart = new ShoppingCart();
shoppingCart.setUserId(currentId);
List<ShoppingCart> ShoppingCartList = shoppingCartMapper.list(shoppingCart);
if (ShoppingCartList == null || ShoppingCartList.isEmpty()) {
throw new AddressBookBusinessException(MessageConstant.SHOPPING_CART_IS_NULL);
}
//向订单表插入1条数据
Orders orders = new Orders();
BeanUtils.copyProperties(ordersSubmitDTO, orders);
orders.setOrderTime(LocalDateTime.now());
orders.setPayStatus(Orders.UN_PAID);
orders.setStatus(Orders.PENDING_PAYMENT);
orders.setNumber(String.valueOf(System.currentTimeMillis()));
orders.setPhone(addressBook.getPhone());
orders.setConsignee(addressBook.getConsignee());
orders.setUserId(currentId);
orders.setAddress(addressBook.getProvinceName() + addressBook.getDistrictName() + addressBook.getDetail());
orders.setTablewareNumber(ordersSubmitDTO.getTablewareNumber());
orders.setTablewareStatus(ordersSubmitDTO.getTablewareStatus());
orders.setRemark(ordersSubmitDTO.getRemark());
orderMapper.insert(orders);

List<OrderDetail> orderDetailList = new ArrayList<>();
//向订单详情表插入多条数据
ShoppingCartList.forEach(cart -> {
OrderDetail orderDetail = new OrderDetail();
BeanUtils.copyProperties(cart, orderDetail);
orderDetail.setOrderId(orders.getId());
orderDetailList.add(orderDetail);
});
MybatisBatch<OrderDetail> mybatisBatch = new MybatisBatch<>(sqlSessionFactory, orderDetailList);
MybatisBatch.Method<OrderDetail> method = new MybatisBatch.Method<>(OrderDetailMapper.class);
mybatisBatch.execute(method.insert());
//如果下单成功,清空购物车数据
LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(ShoppingCart::getUserId, currentId);
shoppingCartMapper.delete(queryWrapper);
//封装返回数据
OrderSubmitVO orderSubmitVO = OrderSubmitVO.builder()
.id(orders.getId())
.orderTime(orders.getOrderTime())
.orderNumber(orders.getNumber())
.orderAmount(orders.getAmount())
.build();
return orderSubmitVO;
}