|
|
@@ -28,7 +28,7 @@ public class UserTeamShareTask implements Runnable {
|
|
|
|
|
|
// 计算分润金额
|
|
|
// 使用 BigDecimal 进行精确计算,避免浮点数精度问题
|
|
|
- BigDecimal shareAmount = totalPrice.multiply(percentage).divide(BigDecimal.valueOf(100), 2, RoundingMode.HALF_UP);
|
|
|
+ BigDecimal shareAmount = totalPrice.multiply(percentage).divide(BigDecimal.valueOf(100), 4, RoundingMode.HALF_UP);
|
|
|
|
|
|
// 查找用户 (userService 是注入的)
|
|
|
User user = userService.findUserByMobileNumber(mobileNumber);
|
|
|
@@ -89,7 +89,47 @@ public class UserTeamShareTask implements Runnable {
|
|
|
/**
|
|
|
* 第二类、直推分润 10%
|
|
|
*/
|
|
|
- public void class2() {}
|
|
|
+ public void class2() {
|
|
|
+ User user = User.dao.findById(order.getUserId());
|
|
|
+ User referrerUser = User.dao.findById(user.getReferrerId());
|
|
|
+
|
|
|
+ // 直推人不存在,直接跳过
|
|
|
+ if (referrerUser == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 确保 order.getTotalPrice() 返回的是 BigDecimal,或者转换为 BigDecimal
|
|
|
+ BigDecimal totalPrice = order.getBigDecimal("total_price");
|
|
|
+ BigDecimal percentage = BigDecimal.valueOf(10);
|
|
|
+ if (totalPrice == null) {
|
|
|
+ throw new RuntimeException("订单总价为空,无法分润");
|
|
|
+ }
|
|
|
+ // 直推10%分润
|
|
|
+ BigDecimal shareAmount = totalPrice.multiply(percentage).divide(BigDecimal.valueOf(100), 4, RoundingMode.HALF_UP);
|
|
|
+ // 更新用户余额
|
|
|
+ BigDecimal currentBalance = referrerUser.getBigDecimal("balance");
|
|
|
+ // 如果 balance 字段为 null,初始化为 0
|
|
|
+ if (currentBalance == null) {
|
|
|
+ currentBalance = BigDecimal.ZERO;
|
|
|
+ }
|
|
|
+ referrerUser.set("balance", currentBalance.add(shareAmount));
|
|
|
+ if (!referrerUser.update()) {
|
|
|
+ throw new RuntimeException("更新用户 " + referrerUser.getMobileNumber() + " 余额失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 记录分润日志
|
|
|
+ String description = "直推分润";
|
|
|
+ DepositLog log = new DepositLog();
|
|
|
+ log.set("create_time", System.currentTimeMillis());
|
|
|
+ log.set("is_deleted", 0);
|
|
|
+ log.set("description", description + " " + percentage.intValue() + "%"); // 描述中带上百分比
|
|
|
+ log.set("amount", shareAmount);
|
|
|
+ log.set("user_id", referrerUser.getLong("id"));
|
|
|
+ if (!log.save()) {
|
|
|
+ throw new RuntimeException("保存分润日志失败 for user " + referrerUser.getMobileNumber());
|
|
|
+ }
|
|
|
+ AppConfig.LOGGER.info("{} {}: {}", description, referrerUser.getMobileNumber(), shareAmount);
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* 第三类、NFT持有者分润
|
|
|
@@ -110,9 +150,9 @@ public class UserTeamShareTask implements Runnable {
|
|
|
Db.tx(() -> {
|
|
|
try {
|
|
|
this.class1();
|
|
|
- this.class2(); // 如果有 DB 操作,也需要放在事务中
|
|
|
- this.class3(); // 如果有 DB 操作,也需要放在事务中
|
|
|
- this.class4(); // 如果有 DB 操作,也需要放在事务中
|
|
|
+ this.class2();
|
|
|
+ this.class3();
|
|
|
+ this.class4();
|
|
|
return true; // 事务成功
|
|
|
} catch (Exception e) {
|
|
|
AppConfig.LOGGER.error("订单分润处理异常,订单ID: {}, 错误: {}", order.getLong("id"), e.getMessage(), e);
|