|
|
@@ -11,6 +11,8 @@ import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
import java.time.LocalDateTime;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
import java.util.UUID;
|
|
|
|
|
|
@Service
|
|
|
@@ -20,64 +22,199 @@ public class OrderService {
|
|
|
private final CommissionRepository commissionRepository;
|
|
|
private final UserRepository userRepository;
|
|
|
private final UserService userService;
|
|
|
+ private final ConfigService configService;
|
|
|
|
|
|
public OrderService(OrderRepository orderRepository, CommissionRepository commissionRepository,
|
|
|
- UserRepository userRepository, UserService userService) {
|
|
|
+ UserRepository userRepository, UserService userService,
|
|
|
+ ConfigService configService) {
|
|
|
this.orderRepository = orderRepository;
|
|
|
this.commissionRepository = commissionRepository;
|
|
|
this.userRepository = userRepository;
|
|
|
this.userService = userService;
|
|
|
+ this.configService = configService;
|
|
|
}
|
|
|
|
|
|
@Transactional
|
|
|
- public Order createOrder(Long userId, Integer totalFee, String payType) {
|
|
|
+ public Order createOrder(Long userId, Integer totalFee, String payType, String productType, Boolean isUpgrade) {
|
|
|
Order order = new Order();
|
|
|
order.setUserId(userId);
|
|
|
order.setOutTradeNo(generateOutTradeNo());
|
|
|
order.setTotalFee(totalFee);
|
|
|
order.setStatus("pending");
|
|
|
order.setPayType(payType);
|
|
|
+ order.setProductType(productType != null ? productType : inferProductType(totalFee));
|
|
|
+ order.setIsUpgrade(isUpgrade != null && isUpgrade);
|
|
|
return orderRepository.save(order);
|
|
|
}
|
|
|
|
|
|
+ private String inferProductType(int totalFee) {
|
|
|
+ int annualFee = configService.getInt("pricing.annual", 13100);
|
|
|
+ if (totalFee == annualFee) return "annual";
|
|
|
+ return "practitioner";
|
|
|
+ }
|
|
|
+
|
|
|
@Transactional
|
|
|
public void paySuccess(String outTradeNo) {
|
|
|
Order order = orderRepository.findByOutTradeNo(outTradeNo)
|
|
|
.orElseThrow(() -> new BizException(1005, "Order not found"));
|
|
|
+
|
|
|
+ // 幂等: already paid → skip
|
|
|
+ if ("paid".equals(order.getStatus())) return;
|
|
|
+
|
|
|
order.setStatus("paid");
|
|
|
order.setPaidAt(LocalDateTime.now());
|
|
|
orderRepository.save(order);
|
|
|
|
|
|
- // Upgrade user to VIP for 365 days
|
|
|
- userService.upgradeVip(order.getUserId(), 365);
|
|
|
+ Long buyerId = order.getUserId();
|
|
|
+ String productType = order.getProductType();
|
|
|
+
|
|
|
+ // Upgrade user VIP for 365 days + set vipType
|
|
|
+ userService.upgradeVip(buyerId, 365, productType);
|
|
|
|
|
|
- User buyer = userRepository.findById(order.getUserId())
|
|
|
+ // Generate referral code for the buyer after successful payment
|
|
|
+ userService.generateReferralCodeForUser(buyerId);
|
|
|
+
|
|
|
+ // Commission logic
|
|
|
+ User buyer = userRepository.findById(buyerId)
|
|
|
.orElseThrow(() -> new BizException(1002, "User not found"));
|
|
|
|
|
|
- if (buyer.getInvitedBy() != null) {
|
|
|
- User inviter = userRepository.findById(buyer.getInvitedBy()).orElse(null);
|
|
|
- if (inviter != null) {
|
|
|
- createCommission(order.getId(), order.getUserId(), inviter.getId(), 1, (int)(order.getTotalFee() * 0.3));
|
|
|
-
|
|
|
- if (inviter.getInvitedBy() != null) {
|
|
|
- User inviter2 = userRepository.findById(inviter.getInvitedBy()).orElse(null);
|
|
|
- if (inviter2 != null) {
|
|
|
- createCommission(order.getId(), order.getUserId(), inviter2.getId(), 2,
|
|
|
- (int)(order.getTotalFee() * 0.1));
|
|
|
- }
|
|
|
+ if (buyer.getInvitedBy() == null) return;
|
|
|
+
|
|
|
+ User inviter = userRepository.findById(buyer.getInvitedBy()).orElse(null);
|
|
|
+ if (inviter == null) return;
|
|
|
+
|
|
|
+ if ("annual".equals(productType)) {
|
|
|
+ // US-6.4: C端佣金 (比例分成)
|
|
|
+ handleAnnualCommission(order, buyer, inviter);
|
|
|
+ } else {
|
|
|
+ // US-6.3: B端佣金 (固定金额)
|
|
|
+ handlePractitionerCommission(order, buyer, inviter);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * US-6.4: C端佣金结算 — 按比例分成
|
|
|
+ */
|
|
|
+ private void handleAnnualCommission(Order order, User buyer, User inviter) {
|
|
|
+ // AC 5-7: 收款人必须已付费 (referralCode != null)
|
|
|
+ if (inviter.getReferralCode() == null) return;
|
|
|
+
|
|
|
+ int directRate = configService.getInt("commission.annual.direct_rate", 4000); // 万分比
|
|
|
+ int upstreamRate = configService.getInt("commission.annual.upstream_rate", 500);
|
|
|
+
|
|
|
+ int l1Amount = order.getTotalFee() * directRate / 10000;
|
|
|
+ int l2Amount = order.getTotalFee() * upstreamRate / 10000;
|
|
|
+
|
|
|
+ String l1Remark = String.format("C端年费直接推荐佣金 (%d%%)", directRate / 100);
|
|
|
+ createCommission(order.getId(), buyer.getId(), inviter.getId(), 1, l1Amount, l1Remark);
|
|
|
+
|
|
|
+ if (inviter.getInvitedBy() != null && l2Amount > 0) {
|
|
|
+ User inviter2 = userRepository.findById(inviter.getInvitedBy()).orElse(null);
|
|
|
+ if (inviter2 != null && inviter2.getReferralCode() != null) {
|
|
|
+ String l2Remark = String.format("C端年费上级佣金 (%d%%)", upstreamRate / 100);
|
|
|
+ createCommission(order.getId(), buyer.getId(), inviter2.getId(), 2, l2Amount, l2Remark);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * US-6.3: B端佣金结算 — 固定金额 + 身份半额 + 升级补差
|
|
|
+ */
|
|
|
+ private void handlePractitionerCommission(Order order, User buyer, User inviter) {
|
|
|
+ // 判断推荐人身份 (US-6.3 §3-4)
|
|
|
+ String inviterVipType = inviter.getVipType();
|
|
|
+ if (inviterVipType == null) return; // 推荐人未付费 → 跳过
|
|
|
+
|
|
|
+ int l1Default, l1Amount, l2Amount;
|
|
|
+ String l1Remark;
|
|
|
+
|
|
|
+ if ("practitioner".equals(inviterVipType)) {
|
|
|
+ l1Default = configService.getInt("commission.practitioner.l1", 50000);
|
|
|
+ l1Remark = "直接推荐升级能量师 (L1)";
|
|
|
+ } else if ("annual".equals(inviterVipType)) {
|
|
|
+ // AC 4: C端推荐 → 半额
|
|
|
+ l1Default = configService.getInt("commission.practitioner.l1_cend", 20000);
|
|
|
+ l1Remark = "直接推荐升级能量师 (C端半额)";
|
|
|
+ } else {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ l1Amount = l1Default;
|
|
|
+ l2Amount = configService.getInt("commission.practitioner.l2", 10000);
|
|
|
+
|
|
|
+ // AC 7-10: 升级补差 (isUpgrade=true)
|
|
|
+ if (Boolean.TRUE.equals(order.getIsUpgrade())) {
|
|
|
+ long paidAnnual = sumAnnualCommissionTo(buyer.getId(), inviter.getId());
|
|
|
+ l1Amount = (int) Math.max(0, l1Default - paidAnnual);
|
|
|
+ if (l1Amount <= 0) return; // 补差后无余额 → 不创建
|
|
|
+ }
|
|
|
+
|
|
|
+ // 幂等: 同一订单不重复创建佣金
|
|
|
+ List<Commission> existing = commissionRepository.findByOrderId(order.getId());
|
|
|
+ if (existing.stream().anyMatch(c -> c.getLevel() == 1 && c.getToUserId().equals(inviter.getId()))) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ createCommission(order.getId(), buyer.getId(), inviter.getId(), 1, l1Amount, l1Remark);
|
|
|
+
|
|
|
+ // AC 17: 更新推荐人统计
|
|
|
+ inviter.setDirectCount(inviter.getDirectCount() != null ? inviter.getDirectCount() + 1 : 1);
|
|
|
+ inviter.setConvertedCount(inviter.getConvertedCount() != null ? inviter.getConvertedCount() + 1 : 1);
|
|
|
+ userRepository.save(inviter);
|
|
|
+
|
|
|
+ // L2
|
|
|
+ if (inviter.getInvitedBy() != null && l2Amount > 0) {
|
|
|
+ User inviter2 = userRepository.findById(inviter.getInvitedBy()).orElse(null);
|
|
|
+ if (inviter2 != null) {
|
|
|
+ long paidAnnual2 = sumAnnualCommissionTo(buyer.getId(), inviter2.getId());
|
|
|
+ int l2Actual = (int) Math.max(0, l2Amount - paidAnnual2);
|
|
|
+ if (l2Actual > 0) {
|
|
|
+ createCommission(order.getId(), buyer.getId(), inviter2.getId(), 2, l2Actual, "间接推荐升级能量师 (L2)");
|
|
|
}
|
|
|
+ // AC 18: 更新间接下级统计
|
|
|
+ inviter2.setIndirectCount(inviter2.getIndirectCount() != null ? inviter2.getIndirectCount() + 1 : 1);
|
|
|
+ userRepository.save(inviter2);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private void createCommission(Long orderId, Long fromUserId, Long toUserId, int level, int amount) {
|
|
|
+ /**
|
|
|
+ * 查询同一买家此前已支付给同一收款人的 annual 佣金总和 (用于升级补差)
|
|
|
+ */
|
|
|
+ private long sumAnnualCommissionTo(Long buyerId, Long recipientId) {
|
|
|
+ List<Commission> existing = commissionRepository.findByFromUserId(buyerId);
|
|
|
+ return existing.stream()
|
|
|
+ .filter(c -> c.getToUserId().equals(recipientId))
|
|
|
+ .filter(c -> "settled".equals(c.getStatus()))
|
|
|
+ .mapToLong(Commission::getAmount)
|
|
|
+ .sum();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Calculate commission amounts based on order type.
|
|
|
+ *
|
|
|
+ * C端 (¥131 / 13100分): L1=40%, L2=5%
|
|
|
+ * B端 (种子价¥1,314 / 131400分 or 标准价¥1,986 / 198600分): L1=¥500(50000分), L2=¥100(10000分)
|
|
|
+ * @deprecated 改用 handleAnnualCommission / handlePractitionerCommission
|
|
|
+ */
|
|
|
+ private int[] calculateCommission(int totalFee) {
|
|
|
+ if (totalFee == 13100) {
|
|
|
+ // C端: 比例佣金
|
|
|
+ return new int[]{(int)(totalFee * 0.4), (int)(totalFee * 0.05)};
|
|
|
+ } else {
|
|
|
+ // B端: 固定金额
|
|
|
+ return new int[]{50000, 10000};
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void createCommission(Long orderId, Long fromUserId, Long toUserId, int level, int amount, String remark) {
|
|
|
Commission commission = new Commission();
|
|
|
commission.setOrderId(orderId);
|
|
|
commission.setFromUserId(fromUserId);
|
|
|
commission.setToUserId(toUserId);
|
|
|
commission.setLevel(level);
|
|
|
commission.setAmount(amount);
|
|
|
- commission.setStatus("pending");
|
|
|
+ commission.setStatus("settled");
|
|
|
+ commission.setRemark(remark);
|
|
|
commissionRepository.save(commission);
|
|
|
}
|
|
|
|