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

This commit is contained in:
chen-xin-zhi 2024-11-08 21:20:31 +08:00
parent 4b4e21c52a
commit 735db59666
5 changed files with 216 additions and 4 deletions

View File

@ -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<Long> addOrder(@RequestBody OrderAddRequest orderAddRequest) {
public BaseResponse<Long> 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);
}

View File

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

View File

@ -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<OrderItemMainInfoAddRequest> orderItemMainInfoAddRequestList;
@Serial
private static final long serialVersionUID = 1L;
}

View File

@ -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<Order> {
@ -12,4 +14,10 @@ public interface OrderService extends IService<Order> {
* 获取查询条件
*/
QueryWrapper<Order> getQueryWrapper(OrderQueryRequest orderQueryRequest);
/**
* 封装订单详细信息请求体
*/
OrderAddRequest initOrderMainInfo(OrderMainInfoAddRequest orderMainInfoAddRequest);
}

View File

@ -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<OrderMapper, Order> 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<OrderMapper, Order> 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<OrderItemMainInfoAddRequest> orderItemMainInfoAddRequestList = orderMainInfoAddRequest.getOrderItemMainInfoAddRequestList();
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;
}
}