新增优惠券使用
This commit is contained in:
@@ -79,7 +79,7 @@ public class ShopUserCoupon implements Serializable {
|
||||
private LocalDateTime useTime;
|
||||
|
||||
@Schema(description = "使用订单ID")
|
||||
private Long orderId;
|
||||
private Integer orderId;
|
||||
|
||||
@Schema(description = "是否已使用")
|
||||
private Integer isUse;
|
||||
|
||||
@@ -14,6 +14,7 @@ import org.springframework.util.CollectionUtils;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -46,6 +47,8 @@ public class OrderBusinessService {
|
||||
|
||||
@Resource
|
||||
private ShopUserAddressService shopUserAddressService;
|
||||
@Resource
|
||||
private ShopUserCouponService shopUserCouponService;
|
||||
|
||||
/**
|
||||
* 创建订单
|
||||
@@ -104,7 +107,7 @@ public class OrderBusinessService {
|
||||
|
||||
// 检查前端传入的总金额是否正确(允许小的误差,比如0.01)
|
||||
if (request.getTotalPrice() != null &&
|
||||
request.getTotalPrice().subtract(calculatedTotal).abs().compareTo(new BigDecimal("0.01")) > 0) {
|
||||
request.getTotalPrice().subtract(calculatedTotal).abs().compareTo(new BigDecimal("0.01")) > 0) {
|
||||
log.warn("订单金额计算不一致,前端传入:{},后台计算:{}", request.getTotalPrice(), calculatedTotal);
|
||||
throw new BusinessException("订单金额计算错误,请刷新重试");
|
||||
}
|
||||
@@ -126,7 +129,7 @@ public class OrderBusinessService {
|
||||
*/
|
||||
private BigDecimal validateAndCalculateTotal(OrderCreateRequest request) {
|
||||
if (CollectionUtils.isEmpty(request.getGoodsItems())) {
|
||||
throw new BusinessException("订单商品列表不能为空");
|
||||
throw new BusinessException("订单商品列表不能为空");
|
||||
}
|
||||
|
||||
BigDecimal total = BigDecimal.ZERO;
|
||||
@@ -193,7 +196,7 @@ public class OrderBusinessService {
|
||||
|
||||
// 验证购买数量限制(使用商品级别的限制)
|
||||
if (goods.getCanBuyNumber() != null && goods.getCanBuyNumber() > 0 &&
|
||||
item.getQuantity() > goods.getCanBuyNumber()) {
|
||||
item.getQuantity() > goods.getCanBuyNumber()) {
|
||||
throw new BusinessException("商品购买数量超过限制:" + productName + ",最大购买数量:" + goods.getCanBuyNumber());
|
||||
}
|
||||
|
||||
@@ -202,7 +205,7 @@ public class OrderBusinessService {
|
||||
total = total.add(itemTotal);
|
||||
|
||||
log.debug("商品验证通过 - ID:{},SKU ID:{},名称:{},单价:{},数量:{},小计:{}",
|
||||
goods.getGoodsId(), item.getSkuId(), productName, actualPrice, item.getQuantity(), itemTotal);
|
||||
goods.getGoodsId(), item.getSkuId(), productName, actualPrice, item.getQuantity(), itemTotal);
|
||||
}
|
||||
|
||||
log.info("订单商品验证完成,总金额:{}", total);
|
||||
@@ -274,6 +277,29 @@ public class OrderBusinessService {
|
||||
shopOrder.setPayType(1); // 默认微信支付
|
||||
}
|
||||
|
||||
// 优惠券
|
||||
if (shopOrder.getCouponId() != null && shopOrder.getCouponId() > 0) {
|
||||
ShopUserCoupon coupon = shopUserCouponService.getById(shopOrder.getCouponId());
|
||||
if (coupon != null) {
|
||||
BigDecimal reducePrice = BigDecimal.ZERO;
|
||||
boolean doReduce = true;
|
||||
if (coupon.getType().equals(10)) {
|
||||
reducePrice = coupon.getReducePrice();
|
||||
if (shopOrder.getTotalPrice().compareTo(coupon.getMinPrice()) < 0) doReduce = false;
|
||||
} else if (coupon.getType().equals(20)) {
|
||||
reducePrice = shopOrder.getTotalPrice()
|
||||
.multiply(BigDecimal.valueOf(coupon.getDiscount()).divide(new BigDecimal(100), RoundingMode.HALF_UP));
|
||||
} else if (coupon.getType().equals(30)) {
|
||||
reducePrice = shopOrder.getTotalPrice();
|
||||
}
|
||||
if (doReduce) {
|
||||
shopOrder.setReducePrice(shopOrder.getReducePrice().add(reducePrice));
|
||||
shopOrder.setPayPrice(shopOrder.getPayPrice().subtract(reducePrice));
|
||||
}
|
||||
// todo 商品/分类限制
|
||||
}
|
||||
}
|
||||
|
||||
return shopOrder;
|
||||
}
|
||||
|
||||
@@ -334,7 +360,7 @@ public class OrderBusinessService {
|
||||
*/
|
||||
private boolean isAddressInfoComplete(OrderCreateRequest request) {
|
||||
return request.getAddress() != null && !request.getAddress().trim().isEmpty() &&
|
||||
request.getRealName() != null && !request.getRealName().trim().isEmpty();
|
||||
request.getRealName() != null && !request.getRealName().trim().isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -375,7 +401,7 @@ public class OrderBusinessService {
|
||||
}
|
||||
|
||||
log.debug("地址信息快照创建完成 - 地址ID:{},收货人:{},地址:{}",
|
||||
userAddress.getId(), userAddress.getName(), shopOrder.getAddress());
|
||||
userAddress.getId(), userAddress.getName(), shopOrder.getAddress());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -511,7 +537,7 @@ public class OrderBusinessService {
|
||||
orderGoodsList.add(orderGoods);
|
||||
|
||||
log.debug("准备保存订单商品 - 商品ID:{},名称:{},单价:{},数量:{},小计:{}",
|
||||
goods.getGoodsId(), goods.getName(), goods.getPrice(), item.getQuantity(), itemTotal);
|
||||
goods.getGoodsId(), goods.getName(), goods.getPrice(), item.getQuantity(), itemTotal);
|
||||
}
|
||||
|
||||
// 批量保存订单商品
|
||||
@@ -542,7 +568,7 @@ public class OrderBusinessService {
|
||||
sku.setStock(newStock);
|
||||
shopGoodsSkuService.updateById(sku);
|
||||
log.debug("扣减SKU库存 - SKU ID:{},扣减数量:{},剩余库存:{}",
|
||||
item.getSkuId(), item.getQuantity(), newStock);
|
||||
item.getSkuId(), item.getQuantity(), newStock);
|
||||
}
|
||||
} else {
|
||||
// 单规格商品,扣减商品库存
|
||||
@@ -555,7 +581,7 @@ public class OrderBusinessService {
|
||||
goods.setStock(newStock);
|
||||
shopGoodsService.updateById(goods);
|
||||
log.debug("扣减商品库存 - 商品ID:{},扣减数量:{},剩余库存:{}",
|
||||
item.getGoodsId(), item.getQuantity(), newStock);
|
||||
item.getGoodsId(), item.getQuantity(), newStock);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,6 +34,7 @@ import org.springframework.util.CollectionUtils;
|
||||
import javax.annotation.Resource;
|
||||
import java.math.BigDecimal;
|
||||
import java.sql.Date;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@@ -74,6 +75,8 @@ public class ShopOrderServiceImpl extends ServiceImpl<ShopOrderMapper, ShopOrder
|
||||
private WechatPayCertificateDiagnostic certificateDiagnostic;
|
||||
@Resource
|
||||
private ShopOrderUpdate10550Service shopOrderUpdate10550Service;
|
||||
@Resource
|
||||
private ShopUserCouponService shopUserCouponService;
|
||||
|
||||
|
||||
@Override
|
||||
@@ -249,6 +252,18 @@ public class ShopOrderServiceImpl extends ServiceImpl<ShopOrderMapper, ShopOrder
|
||||
@Override
|
||||
public void updateByOutTradeNo(ShopOrder order) {
|
||||
baseMapper.updateByOutTradeNo(order);
|
||||
// 使用优惠券
|
||||
if (order.getCouponId() != null && order.getCouponId() > 0) {
|
||||
ShopUserCoupon coupon = shopUserCouponService.getById(order.getCouponId());
|
||||
if (coupon != null) {
|
||||
coupon.setStatus(1);
|
||||
coupon.setIsUse(1);
|
||||
coupon.setUseTime(LocalDateTime.now());
|
||||
coupon.setOrderId(order.getOrderId());
|
||||
coupon.setOrderNo(order.getOrderNo());
|
||||
shopUserCouponService.updateById(coupon);
|
||||
}
|
||||
}
|
||||
if (order.getTenantId().equals(10550)) {
|
||||
shopOrderUpdate10550Service.update(order);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user