|
@@ -33,6 +33,7 @@ import com.etotem.cfc.entity.SupplyCustomerRelation;
|
|
|
import com.etotem.cfc.mapper.SupplySystemMemberMapper;
|
|
import com.etotem.cfc.mapper.SupplySystemMemberMapper;
|
|
|
import com.etotem.cfc.mapper.SupplyCustomerRelationMapper;
|
|
import com.etotem.cfc.mapper.SupplyCustomerRelationMapper;
|
|
|
import com.etotem.cfc.service.SupplyHierarchyService;
|
|
import com.etotem.cfc.service.SupplyHierarchyService;
|
|
|
|
|
+import com.etotem.cfc.service.CouponService;
|
|
|
import com.etotem.cfc.mapper.CouponMapper;
|
|
import com.etotem.cfc.mapper.CouponMapper;
|
|
|
import com.etotem.cfc.mapper.FamilyCouponMapper;
|
|
import com.etotem.cfc.mapper.FamilyCouponMapper;
|
|
|
import com.etotem.cfc.mapper.ProductMapper;
|
|
import com.etotem.cfc.mapper.ProductMapper;
|
|
@@ -198,6 +199,9 @@ public class ProductOrderService {
|
|
|
@Resource
|
|
@Resource
|
|
|
private FamilyCouponMapper familyCouponMapper;
|
|
private FamilyCouponMapper familyCouponMapper;
|
|
|
|
|
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private CouponService couponService;
|
|
|
|
|
+
|
|
|
public Result<ProductOrderDTO> create(CreateProductOrderDTO dto, Long buyerId) {
|
|
public Result<ProductOrderDTO> create(CreateProductOrderDTO dto, Long buyerId) {
|
|
|
if (buyerId == null) {
|
|
if (buyerId == null) {
|
|
|
return Result.error("请先登录");
|
|
return Result.error("请先登录");
|
|
@@ -251,6 +255,15 @@ public class ProductOrderService {
|
|
|
int pointsCost = dto.getPointsUsed() != null ? dto.getPointsUsed() : 0;
|
|
int pointsCost = dto.getPointsUsed() != null ? dto.getPointsUsed() : 0;
|
|
|
order.setPointsCost(pointsCost);
|
|
order.setPointsCost(pointsCost);
|
|
|
int discountAmount = dto.getDiscountAmount() != null ? dto.getDiscountAmount() : 0;
|
|
int discountAmount = dto.getDiscountAmount() != null ? dto.getDiscountAmount() : 0;
|
|
|
|
|
+ // 优惠券抵扣:校验并计入优惠金额(orderType=PRODUCT)
|
|
|
|
|
+ Long appliedCouponId = null;
|
|
|
|
|
+ if (dto.getCouponId() != null) {
|
|
|
|
|
+ Integer couponDiscount = couponService.apply(buyerId, dto.getCouponId(), "PRODUCT", order.getTotalAmount());
|
|
|
|
|
+ if (couponDiscount != null && couponDiscount > 0) {
|
|
|
|
|
+ discountAmount += couponDiscount;
|
|
|
|
|
+ appliedCouponId = dto.getCouponId();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
int moneyAmount = order.getTotalAmount() - pointsCost - discountAmount;
|
|
int moneyAmount = order.getTotalAmount() - pointsCost - discountAmount;
|
|
|
order.setMoneyAmount(moneyAmount > 0 ? moneyAmount : 0);
|
|
order.setMoneyAmount(moneyAmount > 0 ? moneyAmount : 0);
|
|
|
order.setGiftAmount(productGiftRuleService.getActiveByProductId(product.getId()) != null
|
|
order.setGiftAmount(productGiftRuleService.getActiveByProductId(product.getId()) != null
|
|
@@ -260,7 +273,7 @@ public class ProductOrderService {
|
|
|
order.setRemark(dto.getRemark());
|
|
order.setRemark(dto.getRemark());
|
|
|
order.setCoverImage(product.getCoverImage());
|
|
order.setCoverImage(product.getCoverImage());
|
|
|
order.setDiscountAmount(discountAmount);
|
|
order.setDiscountAmount(discountAmount);
|
|
|
- order.setCouponId(dto.getCouponId());
|
|
|
|
|
|
|
+ order.setCouponId(appliedCouponId);
|
|
|
resolveOrderAddress(order, dto, buyerId);
|
|
resolveOrderAddress(order, dto, buyerId);
|
|
|
if (dto.getPurchaseInfo() != null && !dto.getPurchaseInfo().isEmpty()) {
|
|
if (dto.getPurchaseInfo() != null && !dto.getPurchaseInfo().isEmpty()) {
|
|
|
order.setPurchaseInfo(JSON.toJSONString(dto.getPurchaseInfo()));
|
|
order.setPurchaseInfo(JSON.toJSONString(dto.getPurchaseInfo()));
|
|
@@ -601,6 +614,15 @@ public class ProductOrderService {
|
|
|
int pointsCost = dto.getPointsUsed() != null ? dto.getPointsUsed() : 0;
|
|
int pointsCost = dto.getPointsUsed() != null ? dto.getPointsUsed() : 0;
|
|
|
order.setPointsCost(pointsCost);
|
|
order.setPointsCost(pointsCost);
|
|
|
int multiDiscountAmount = dto.getDiscountAmount() != null ? dto.getDiscountAmount() : 0;
|
|
int multiDiscountAmount = dto.getDiscountAmount() != null ? dto.getDiscountAmount() : 0;
|
|
|
|
|
+ // 优惠券抵扣:校验并计入优惠金额(orderType=PRODUCT)
|
|
|
|
|
+ Long appliedCouponId = null;
|
|
|
|
|
+ if (dto.getCouponId() != null) {
|
|
|
|
|
+ Integer couponDiscount = couponService.apply(buyerId, dto.getCouponId(), "PRODUCT", totalAmount);
|
|
|
|
|
+ if (couponDiscount != null && couponDiscount > 0) {
|
|
|
|
|
+ multiDiscountAmount += couponDiscount;
|
|
|
|
|
+ appliedCouponId = dto.getCouponId();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
int moneyAmount = totalAmount - pointsCost - multiDiscountAmount;
|
|
int moneyAmount = totalAmount - pointsCost - multiDiscountAmount;
|
|
|
order.setMoneyAmount(moneyAmount > 0 ? moneyAmount : 0);
|
|
order.setMoneyAmount(moneyAmount > 0 ? moneyAmount : 0);
|
|
|
order.setStatus("pending");
|
|
order.setStatus("pending");
|
|
@@ -608,7 +630,7 @@ public class ProductOrderService {
|
|
|
order.setRemark(dto.getRemark());
|
|
order.setRemark(dto.getRemark());
|
|
|
order.setCoverImage(firstItem.getCoverImage() != null ? firstItem.getCoverImage() : firstProduct.getCoverImage());
|
|
order.setCoverImage(firstItem.getCoverImage() != null ? firstItem.getCoverImage() : firstProduct.getCoverImage());
|
|
|
order.setDiscountAmount(multiDiscountAmount);
|
|
order.setDiscountAmount(multiDiscountAmount);
|
|
|
- order.setCouponId(dto.getCouponId());
|
|
|
|
|
|
|
+ order.setCouponId(appliedCouponId);
|
|
|
resolveOrderAddress(order, dto, buyerId);
|
|
resolveOrderAddress(order, dto, buyerId);
|
|
|
if (dto.getPurchaseInfo() != null && !dto.getPurchaseInfo().isEmpty()) {
|
|
if (dto.getPurchaseInfo() != null && !dto.getPurchaseInfo().isEmpty()) {
|
|
|
order.setPurchaseInfo(JSON.toJSONString(dto.getPurchaseInfo()));
|
|
order.setPurchaseInfo(JSON.toJSONString(dto.getPurchaseInfo()));
|
|
@@ -812,6 +834,15 @@ public class ProductOrderService {
|
|
|
order.setUpdatedAt(new Date());
|
|
order.setUpdatedAt(new Date());
|
|
|
orderMapper.updateById(order);
|
|
orderMapper.updateById(order);
|
|
|
|
|
|
|
|
|
|
+ // 核销优惠券:订单支付成功后标记为已使用
|
|
|
|
|
+ if (order.getCouponId() != null) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ couponService.markUsed(order.getCouponId(), order.getId());
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("优惠券核销失败: orderNo={}, couponId={}, error={}", orderNo, order.getCouponId(), e.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
// CF值:统一分佣(当前人 + 推荐人)
|
|
// CF值:统一分佣(当前人 + 推荐人)
|
|
|
try {
|
|
try {
|
|
|
cfCommissionService.settle(order.getId(), "product", order.getBuyerId(),
|
|
cfCommissionService.settle(order.getId(), "product", order.getBuyerId(),
|
|
@@ -970,6 +1001,14 @@ public class ProductOrderService {
|
|
|
order.setStatus("cancelled");
|
|
order.setStatus("cancelled");
|
|
|
order.setUpdatedAt(new Date());
|
|
order.setUpdatedAt(new Date());
|
|
|
orderMapper.updateById(order);
|
|
orderMapper.updateById(order);
|
|
|
|
|
+ // 取消订单:归还优惠券
|
|
|
|
|
+ if (order.getCouponId() != null) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ couponService.revert(order.getCouponId());
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("取消订单归还优惠券失败: orderNo={}, couponId={}, error={}", orderNo, order.getCouponId(), e.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
// Restore stock on cancel
|
|
// Restore stock on cancel
|
|
|
productService.increaseStock(order.getProductId(), order.getQuantity());
|
|
productService.increaseStock(order.getProductId(), order.getQuantity());
|
|
|
// 记录退款入库流水
|
|
// 记录退款入库流水
|