完善了小程序端用户创建订单接口

This commit is contained in:
chen-xin-zhi 2024-11-09 00:01:10 +08:00
parent 735db59666
commit 9d43600ac7

View File

@ -1,6 +1,7 @@
package com.cultural.heritage.service.order.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cultural.heritage.common.ErrorCode;
import com.cultural.heritage.constant.CommonConstant;
@ -48,6 +49,7 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
private GoodMapper goodMapper;
/**
* 获取查询条件
*/
@ -80,6 +82,9 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
}
/**
* 封装订单详细信息请求体
*/
@Override
public OrderAddRequest initOrderMainInfo(OrderMainInfoAddRequest orderMainInfoAddRequest) {
Long userId = orderMainInfoAddRequest.getUserId();
@ -90,67 +95,74 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
String orderNumber = orderMainInfoAddRequest.getOrderNumber();
String orderStatus = orderMainInfoAddRequest.getOrderStatus();
Double totalAmount = orderMainInfoAddRequest.getTotalAmount();
List<OrderItemMainInfoAddRequest> orderItemMainInfoAddRequestList = orderMainInfoAddRequest.getOrderItemMainInfoAddRequestList();
OrderAddRequest orderAddRequest = new OrderAddRequest();
// 获取订单地址信息
AddressSnapshot addressSnapshot = getSnapshot(addressId, addressMapper, AddressSnapshot.class);
// 获取订单联系人信息
ContactsSnapshot contactsSnapshot = getSnapshot(contactsId, contactsMapper, ContactsSnapshot.class);
// 获取订单优惠券信息
CouponSnapshot couponSnapshot = getSnapshot(couponId, couponMapper, CouponSnapshot.class);
// 封装订单明细信息
List<OrderItemAddRequest> orderItemAddRequestList = orderItemMainInfoAddRequestList.stream().map(orderItemMainInfoAddRequest -> {
Long goodId = orderItemMainInfoAddRequest.getGoodId();
Integer quantity = orderItemMainInfoAddRequest.getQuantity();
OrderItemAddRequest orderItemAddRequest = new OrderItemAddRequest();
// 获取订单商品信息
GoodSnapshot goodSnapshot = getSnapshot(goodId, goodMapper, GoodSnapshot.class);
orderItemAddRequest.setGoodSnapshot(goodSnapshot);
orderItemAddRequest.setPriceSnapshot(goodSnapshot.getPrice());
orderItemAddRequest.setQuantity(quantity);
orderItemAddRequest.setItemTotalAmount(goodSnapshot.getPrice() * quantity);
return orderItemAddRequest;
}).toList();
// 创建订单详细信息请求体
OrderAddRequest orderAddRequest = new OrderAddRequest();
orderAddRequest.setUserId(userId);
orderAddRequest.setUserName(userName);
orderAddRequest.setOrderNumber(orderNumber);
orderAddRequest.setOrderStatus(orderStatus);
orderAddRequest.setTotalAmount(totalAmount);
// 获取订单地址信息
QueryWrapper<Address> addressQueryWrapper = new QueryWrapper<>();
addressQueryWrapper.eq("id", addressId);
Address address = addressMapper.selectOne(addressQueryWrapper);
ThrowUtils.throwIf(address == null, ErrorCode.NOT_FOUND_ERROR);
AddressSnapshot addressSnapshot = new AddressSnapshot();
BeanUtils.copyProperties(address, addressSnapshot);
// 获取订单联系人信息
QueryWrapper<Contacts> contactsQueryWrapper = new QueryWrapper<>();
contactsQueryWrapper.eq("id", contactsId);
Contacts contacts = contactsMapper.selectOne(contactsQueryWrapper);
ThrowUtils.throwIf(contacts == null, ErrorCode.NOT_FOUND_ERROR);
ContactsSnapshot contactsSnapshot = new ContactsSnapshot();
BeanUtils.copyProperties(contacts, contactsSnapshot);
// 获取订单优惠券信息
QueryWrapper<Coupon> couponQueryWrapper = new QueryWrapper<>();
couponQueryWrapper.eq("id", couponId);
Coupon coupon = couponMapper.selectOne(couponQueryWrapper);
ThrowUtils.throwIf(coupon == null, ErrorCode.NOT_FOUND_ERROR);
CouponSnapshot couponSnapshot = new CouponSnapshot();
BeanUtils.copyProperties(coupon, couponSnapshot);
orderAddRequest.setAddressSnapshot(addressSnapshot);
orderAddRequest.setContactsSnapshot(contactsSnapshot);
orderAddRequest.setCouponSnapshot(couponSnapshot);
List<OrderItemAddRequest> orderItemAddRequestList = orderItemMainInfoAddRequestList.stream().map(orderItemMainInfoAddRequest -> {
Long goodId = orderItemMainInfoAddRequest.getGoodId();
Integer quantity = orderItemMainInfoAddRequest.getQuantity();
OrderItemAddRequest orderItemAddRequest = new OrderItemAddRequest();
QueryWrapper<Good> goodQueryWrapper = new QueryWrapper<>();
goodQueryWrapper.eq("id", goodId);
Good good = goodMapper.selectOne(goodQueryWrapper);
ThrowUtils.throwIf(good == null, ErrorCode.NOT_FOUND_ERROR);
GoodSnapshot goodSnapshot = new GoodSnapshot();
BeanUtils.copyProperties(good, goodSnapshot);
orderItemAddRequest.setGoodSnapshot(goodSnapshot);
orderItemAddRequest.setPriceSnapshot(good.getPrice());
orderItemAddRequest.setQuantity(quantity);
orderItemAddRequest.setItemTotalAmount(good.getPrice() * quantity);
return orderItemAddRequest;
}).toList();
orderAddRequest.setOrderItemList(orderItemAddRequestList);
return orderAddRequest;
}
/**
* 根据id获取订单(地址联系人优惠券商品)信息
*/
private static <T, S> S getSnapshot(Long id, BaseMapper<T> mapper, Class<S> snapshotClass) {
// 创建查询条件
QueryWrapper<T> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("id", id);
// 查询实体对象
T entity = mapper.selectOne(queryWrapper);
// 判断实体对象是否存在不存在则抛出异常
ThrowUtils.throwIf(entity == null, ErrorCode.NOT_FOUND_ERROR);
try {
// 创建快照对象实例
S snapshot = snapshotClass.getDeclaredConstructor().newInstance();
// 复制属性到快照对象
BeanUtils.copyProperties(entity, snapshot);
return snapshot;
} catch (Exception e) {
throw new BusinessException(ErrorCode.SYSTEM_ERROR);
}
}
}