diff --git a/src/main/java/com/cultural/heritage/service/order/impl/OrderServiceImpl.java b/src/main/java/com/cultural/heritage/service/order/impl/OrderServiceImpl.java index 39468bc..2c52694 100644 --- a/src/main/java/com/cultural/heritage/service/order/impl/OrderServiceImpl.java +++ b/src/main/java/com/cultural/heritage/service/order/impl/OrderServiceImpl.java @@ -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 implements private GoodMapper goodMapper; + /** * 获取查询条件 */ @@ -80,6 +82,9 @@ public class OrderServiceImpl extends ServiceImpl implements } + /** + * 封装订单详细信息请求体 + */ @Override public OrderAddRequest initOrderMainInfo(OrderMainInfoAddRequest orderMainInfoAddRequest) { Long userId = orderMainInfoAddRequest.getUserId(); @@ -90,67 +95,74 @@ public class OrderServiceImpl extends ServiceImpl implements String orderNumber = orderMainInfoAddRequest.getOrderNumber(); String orderStatus = orderMainInfoAddRequest.getOrderStatus(); Double totalAmount = orderMainInfoAddRequest.getTotalAmount(); - List 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 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
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 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 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 orderItemAddRequestList = orderItemMainInfoAddRequestList.stream().map(orderItemMainInfoAddRequest -> { - Long goodId = orderItemMainInfoAddRequest.getGoodId(); - Integer quantity = orderItemMainInfoAddRequest.getQuantity(); - OrderItemAddRequest orderItemAddRequest = new OrderItemAddRequest(); - QueryWrapper 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 S getSnapshot(Long id, BaseMapper mapper, Class snapshotClass) { + // 创建查询条件 + QueryWrapper 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); + } + } + }