diff --git a/src/main/java/com/cultural/heritage/controller/order/OrderController.java b/src/main/java/com/cultural/heritage/controller/order/OrderController.java index ca91477..92bd743 100644 --- a/src/main/java/com/cultural/heritage/controller/order/OrderController.java +++ b/src/main/java/com/cultural/heritage/controller/order/OrderController.java @@ -15,6 +15,7 @@ import com.cultural.heritage.model.dto.order.OrderAddRequest; import com.cultural.heritage.model.dto.order.OrderItemAddRequest; import com.cultural.heritage.model.dto.order.OrderQueryRequest; import com.cultural.heritage.model.dto.order.OrderUpdateRequest; +import com.cultural.heritage.model.dto.order.capital.OrderMainInfoAddRequest; import com.cultural.heritage.model.entity.Order; import com.cultural.heritage.model.entity.OrderItems; import com.cultural.heritage.model.vo.OrderVO; @@ -92,13 +93,18 @@ public class OrderController { /** * 用户创建订单 - * @param orderAddRequest 订单创建请求体 + * @param orderMainInfoAddRequest 订单创建请求体 * @return 是否创建成功 */ @PostMapping("/add") @Transactional(rollbackFor = Exception.class) @Operation(summary = "小程序端创建用户订单", description = "参数:订单创建请求体,排序字段 sortField: totalAmount,权限:所有人,方法名:addOrder") - public BaseResponse addOrder(@RequestBody OrderAddRequest orderAddRequest) { + public BaseResponse addOrder(@RequestBody OrderMainInfoAddRequest orderMainInfoAddRequest) { + if (orderMainInfoAddRequest == null) { + throw new BusinessException(ErrorCode.PARAMS_ERROR); + } + // 封装订单详细信息请求体 + OrderAddRequest orderAddRequest = orderService.initOrderMainInfo(orderMainInfoAddRequest); if (orderAddRequest == null) { throw new BusinessException(ErrorCode.PARAMS_ERROR); } diff --git a/src/main/java/com/cultural/heritage/model/dto/order/capital/OrderItemMainInfoAddRequest.java b/src/main/java/com/cultural/heritage/model/dto/order/capital/OrderItemMainInfoAddRequest.java new file mode 100644 index 0000000..3f7cb9a --- /dev/null +++ b/src/main/java/com/cultural/heritage/model/dto/order/capital/OrderItemMainInfoAddRequest.java @@ -0,0 +1,27 @@ +package com.cultural.heritage.model.dto.order.capital; + +import lombok.Data; + +import java.io.Serial; +import java.io.Serializable; + +@Data +public class OrderItemMainInfoAddRequest implements Serializable { + + + /** + * 商品id + */ + private Long goodId; + + + /** + * 购买 + */ + private Integer quantity; + + + + @Serial + private static final long serialVersionUID = 1L; +} diff --git a/src/main/java/com/cultural/heritage/model/dto/order/capital/OrderMainInfoAddRequest.java b/src/main/java/com/cultural/heritage/model/dto/order/capital/OrderMainInfoAddRequest.java new file mode 100644 index 0000000..32e6b09 --- /dev/null +++ b/src/main/java/com/cultural/heritage/model/dto/order/capital/OrderMainInfoAddRequest.java @@ -0,0 +1,68 @@ +package com.cultural.heritage.model.dto.order.capital; + +import lombok.Data; + +import java.io.Serial; +import java.io.Serializable; +import java.util.List; + +@Data +public class OrderMainInfoAddRequest implements Serializable { + + + /** + * 用户id + */ + private Long userId; + + + /** + * 用户昵称 + */ + private String userName; + + + /** + * 订单编号 + */ + private String orderNumber; + + + /** + * 地址id + */ + private Long addressId; + + + /** + * 联系人id + */ + private Long contactsId; + + + /** + * 优惠券id + */ + private Long couponId; + + + /** + * 订单总金额 + */ + private Double totalAmount; + + /** + * 订单状态 + */ + private String orderStatus; + + + /** + * 订单主要明细信息 + */ + private List orderItemMainInfoAddRequestList; + + + @Serial + private static final long serialVersionUID = 1L; +} diff --git a/src/main/java/com/cultural/heritage/service/order/OrderService.java b/src/main/java/com/cultural/heritage/service/order/OrderService.java index d577d80..1fb6e06 100644 --- a/src/main/java/com/cultural/heritage/service/order/OrderService.java +++ b/src/main/java/com/cultural/heritage/service/order/OrderService.java @@ -2,7 +2,9 @@ package com.cultural.heritage.service.order; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.service.IService; +import com.cultural.heritage.model.dto.order.OrderAddRequest; import com.cultural.heritage.model.dto.order.OrderQueryRequest; +import com.cultural.heritage.model.dto.order.capital.OrderMainInfoAddRequest; import com.cultural.heritage.model.entity.Order; public interface OrderService extends IService { @@ -12,4 +14,10 @@ public interface OrderService extends IService { * 获取查询条件 */ QueryWrapper getQueryWrapper(OrderQueryRequest orderQueryRequest); + + + /** + * 封装订单详细信息请求体 + */ + OrderAddRequest initOrderMainInfo(OrderMainInfoAddRequest orderMainInfoAddRequest); } 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 4e9fefc..39468bc 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 @@ -5,21 +5,49 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.cultural.heritage.common.ErrorCode; import com.cultural.heritage.constant.CommonConstant; import com.cultural.heritage.exception.BusinessException; -import com.cultural.heritage.mapper.OrderMapper; +import com.cultural.heritage.exception.ThrowUtils; +import com.cultural.heritage.mapper.*; +import com.cultural.heritage.model.dto.order.OrderAddRequest; +import com.cultural.heritage.model.dto.order.OrderItemAddRequest; import com.cultural.heritage.model.dto.order.OrderQueryRequest; -import com.cultural.heritage.model.entity.Order; +import com.cultural.heritage.model.dto.order.capital.OrderItemMainInfoAddRequest; +import com.cultural.heritage.model.dto.order.capital.OrderMainInfoAddRequest; +import com.cultural.heritage.model.dto.snapshot.AddressSnapshot; +import com.cultural.heritage.model.dto.snapshot.ContactsSnapshot; +import com.cultural.heritage.model.dto.snapshot.CouponSnapshot; +import com.cultural.heritage.model.dto.snapshot.GoodSnapshot; +import com.cultural.heritage.model.entity.*; import com.cultural.heritage.service.order.OrderService; import com.cultural.heritage.utils.SqlUtils; +import jakarta.annotation.Resource; import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service; import java.util.Date; +import java.util.List; @Service public class OrderServiceImpl extends ServiceImpl implements OrderService { + @Resource + private AddressMapper addressMapper; + + + @Resource + private ContactsMapper contactsMapper; + + + @Resource + private CouponMapper couponMapper; + + + @Resource + private GoodMapper goodMapper; + + /** * 获取查询条件 */ @@ -50,4 +78,79 @@ public class OrderServiceImpl extends ServiceImpl implements sortField); return queryWrapper; } + + + @Override + public OrderAddRequest initOrderMainInfo(OrderMainInfoAddRequest orderMainInfoAddRequest) { + Long userId = orderMainInfoAddRequest.getUserId(); + String userName = orderMainInfoAddRequest.getUserName(); + Long addressId = orderMainInfoAddRequest.getAddressId(); + Long contactsId = orderMainInfoAddRequest.getContactsId(); + Long couponId = orderMainInfoAddRequest.getCouponId(); + String orderNumber = orderMainInfoAddRequest.getOrderNumber(); + String orderStatus = orderMainInfoAddRequest.getOrderStatus(); + Double totalAmount = orderMainInfoAddRequest.getTotalAmount(); + + List orderItemMainInfoAddRequestList = orderMainInfoAddRequest.getOrderItemMainInfoAddRequestList(); + + 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; + } + + + + }