This commit is contained in:
Cauchy
2023-10-23 16:41:00 +08:00
parent 9b37d2126a
commit 3a59fdb860
5 changed files with 200 additions and 11 deletions

View File

@@ -80,6 +80,8 @@ public class BuyOrderController {
private ExpressFeeService expressFeeService;
@Autowired
private SysConfigService sysConfigService;
@Autowired
private BuyOrderProductService buyOrderProductService;
@RequestMapping(value = "/decomposeShipment", method = RequestMethod.GET)
public R decomposeShipment(@RequestParam("userId") Integer userId) {
@@ -103,8 +105,8 @@ public class BuyOrderController {
*/
@RequestMapping(path = "/orderList", method = RequestMethod.POST)
public R orderList(@RequestBody BuyOrderListRequestVo requestVo) {
List<BuyOrderResponseVo> page = buyOrderService.orderList(requestVo);
return R.ok().put("result", page);
Map<String, Object> result = buyOrderService.orderList(requestVo);
return R.ok().put("result", result);
}
/**
@@ -121,6 +123,7 @@ public class BuyOrderController {
/**
* 下单
* TODO 原下单接口,新版本上线后废弃
*
* @param buyOrder 订单
* @return R
@@ -210,6 +213,103 @@ public class BuyOrderController {
return R.ok(result);
}
/**
* 下单
*
* @param buyOrder
* @return
* @throws IOException
*/
@RequestMapping(value = "placeOrder", method = RequestMethod.POST)
public R placeOrder(@RequestBody BuyOrder buyOrder) throws IOException {
List<BuyOrderProduct> buyOrderProductList = buyOrder.getProductInfoList();
// 订单总金额
BigDecimal totalPrice = new BigDecimal(0);
// 遍历商品总价计算
for (BuyOrderProduct buyOrderProduct : buyOrderProductList) {
Integer productId = buyOrderProduct.getProductId();
int quantity = buyOrderProduct.getQuantity();
ShopProduct product = shopProductService.getById(productId);
BigDecimal price = getRealPrice(product);
if (!handleStock(buyOrderProduct, product)) {
return R.error(500, "库存不足");
}
totalPrice = totalPrice.add(price.multiply(BigDecimal.valueOf(quantity)));
//buyOrderDetail.setProductName(product.getProductName());
//buyOrderDetail.setProductPrice(product.getPrice());
int originWeight = product.getWeight();
int originWeightIntValue = originWeight / 100;
int originWeightDecimalValue = originWeightIntValue % 100;
//buyOrderProduct.setWeight(BigDecimal.valueOf(originWeightIntValue).add(BigDecimal.valueOf(originWeightDecimalValue).divide(new BigDecimal(100),
//MathContext.DECIMAL64)));
// buyOrderDetail.setProductUrl(product.getProductImages());
// buyOrderDetail.setOrderStatus(Constants.ORDER_STATUS_TO_BE_PAID);
}
totalPrice = totalPrice.subtract(useCouponAmount(buyOrder));
totalPrice = totalPrice.add(getShoppingAmount(buyOrder));
String orderSn = IdWorker.getTimeId().substring(0, 32);
buyOrder.setOrderSn(orderSn);
buyOrder.setPaymentDate(new Date());
QueryWrapper<UserAddress> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("id", buyOrder.getAddressId());
UserAddress userAddress = userAddressService.getOne(queryWrapper);
UserAddressVo vo = new UserAddressVo();
vo = userAddressService.getAddressName(vo, userAddress.getRegionCode());
buyOrder.setProvince(vo.getProvince());
buyOrder.setCity(vo.getCity());
buyOrder.setDistrict(vo.getCounty());
buyOrder.setAddress(vo.getDetailAddress());
buyOrderService.save(buyOrder);
for (BuyOrderProduct buyOrderDetail : buyOrderProductList) {
buyOrderDetail.setOrderId(buyOrder.getOrderId());
//buyOrderDetail.setUserId(buyOrder.getUserId());
if (Constants.BUY_TYPE_CART.equals(buyOrder.getBuyType())) {
handleBuyCart(buyOrder, buyOrderDetail);
}
}
// buyOrderDetailService.saveBatch(buyOrderDetails);
buyOrderProductService.saveBatch(buyOrderProductList);
// 1. 虚拟币支付
if (Constants.PAYMENT_METHOD_VIRTUAL.equals(buyOrder.getPaymentMethod())) {
buyOrder.setOrderStatus(Constants.ORDER_STATUS_TO_BE_SHIPPED);
MyUserEntity user = this.myUserService.getById(buyOrder.getUserId());
if (usePeanutCoin(user, totalPrice)) {
// 更新订单状态
buyOrderService.updateOrderStatus(user.getId(), buyOrder.getOrderSn(), "0");
recordTransaction(buyOrder, user, totalPrice);
addEbookToUser(buyOrderProductList, buyOrder, 0);
} else {
return R.error(500, "花生币余额不足!");
}
}
// 2. 微信支付
if (Constants.PAYMENT_METHOD_WECHAT_PAY.equals(buyOrder.getPaymentMethod())) {
rabbitTemplate.convertAndSend(
DelayQueueConfig.ORDER_TO_BE_PAY_EXCHANGE,
DelayQueueConfig.ORDER_TO_BE_PAY_ROUTING_KEY,
buyOrder.getOrderId(),
messagePostProcessor()
);
WechatPaymentInfo paymentInfo = new WechatPaymentInfo();
paymentInfo.setOrderSn(orderSn);
paymentInfo.setBuyOrderId(buyOrder.getOrderId());
paymentInfo.setTotalAmount(totalPrice);
wxpayService.prepay(paymentInfo);
}
Map<String, Object> result = new HashMap<>();
result.put("orderSn", buyOrder.getOrderSn());
result.put("money", totalPrice);
return R.ok(result);
}
/**
* 计算运费
*
* @param vo
* @return
*/
@RequestMapping(path = "/calculateTransportPrice", method = RequestMethod.POST)
public R getTransportPrice(@RequestBody ProductTransportVo vo) {
String regionCode = vo.getRegionCode();
@@ -443,6 +543,7 @@ public class BuyOrderController {
/**
* 处理商品库存
* TODO 新版本上线后删除此方法
*
* @param buyOrderDetail
* @param product
@@ -459,6 +560,24 @@ public class BuyOrderController {
return true;
}
/**
* 处理商品库存
*
* @param buyOrderProduct 订单商品信息
* @param product 商品
* @return boolean
*/
private boolean handleStock(BuyOrderProduct buyOrderProduct, ShopProduct product) {
int quantity = buyOrderProduct.getQuantity();
if (product.getProductStock() - quantity < 0) {
return false;
}
product.setProductStock(product.getProductStock() - quantity);
product.setSumSales(product.getSumSales() + quantity);
shopProductService.updateById(product);
return true;
}
/**
* 使用优惠券
*
@@ -543,8 +662,9 @@ public class BuyOrderController {
/**
* 给用户添加电子书
* TODO 新版本上线删除此接口
* @param products
*
* @param products
* @param buyOrder
*/
private void addEbookToUser(List<BuyOrderDetail> products, BuyOrder buyOrder) {
@@ -557,8 +677,26 @@ public class BuyOrderController {
}
}
/**
* 给用户添加电子书
* TODO 这里的参数 0 没用 新版本上线后删除
*
* @param products
* @param buyOrder
*/
private void addEbookToUser(List<BuyOrderProduct> products, BuyOrder buyOrder, Integer x) {
List<Integer> productIds = products.stream().map(BuyOrderProduct::getProductId).collect(Collectors.toList());
for (Integer productId : productIds) {
List<Integer> collect = shopProductBookService.getBaseMapper().selectList(new LambdaQueryWrapper<ShopProductBookEntity>()
.eq(ShopProductBookEntity::getProductId, productId)
.eq(ShopProductBookEntity::getDelFlag, 0)).stream().map(ShopProductBookEntity::getBookId).collect(Collectors.toList());
userEbookBuyService.addBookForUser(buyOrder.getUserId(), collect);
}
}
/**
* 购物车
* TODO 新版本上线后删除此方法
*
* @param buyOrder
* @param buyOrderDetail
@@ -572,6 +710,21 @@ public class BuyOrderController {
}
}
/**
* 购物车
*
* @param buyOrder 订单
* @param buyOrderProduct
*/
private void handleBuyCart(BuyOrder buyOrder, BuyOrderProduct buyOrderProduct) {
List<OrderCartEntity> orderCartList = orderCartService.getBaseMapper().selectList(new QueryWrapper<OrderCartEntity>()
.eq("user_id", buyOrder.getUserId()).eq("product_id", buyOrderProduct.getProductId()));
if (orderCartList.size() > 0) {
List<Integer> collect = orderCartList.stream().map(OrderCartEntity::getCartId).collect(Collectors.toList());
orderCartService.removeByIds(collect);
}
}
private MessagePostProcessor messagePostProcessor() {
return message -> {
//设置有效期30分钟