|
|
@@ -11,6 +11,7 @@ import common.model.OrderLog;
|
|
|
import common.model.User;
|
|
|
import common.utils.http.MyRet;
|
|
|
import modules.nftt.NfttService;
|
|
|
+import modules.deposit.DepositService;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.HashMap;
|
|
|
@@ -21,6 +22,9 @@ import java.util.stream.Collectors;
|
|
|
public class OrderService {
|
|
|
@Inject
|
|
|
NfttService nfttService;
|
|
|
+
|
|
|
+ @Inject
|
|
|
+ DepositService depositService;
|
|
|
|
|
|
public String hello() {
|
|
|
return "Hello order";
|
|
|
@@ -45,10 +49,13 @@ public class OrderService {
|
|
|
float unitPrice = item.getFloat("price");
|
|
|
float totalPrice = unitPrice * quantity;
|
|
|
|
|
|
- // 判断用户余额并预扣
|
|
|
+ // 判断用户余额
|
|
|
User user = User.dao.findById(userId);
|
|
|
if (user.getBalance() < totalPrice) {
|
|
|
- throw new RuntimeException("余额不足,请充值。需要: " + totalPrice + ",你有:" + user.getBalance());
|
|
|
+ // 余额不足,计算差额并创建通联充值订单
|
|
|
+ float shortageAmount = totalPrice - user.getBalance();
|
|
|
+ int chargeAmount = (int) (shortageAmount * 100); // 1火花=1分钱,转换为分
|
|
|
+ throw new ChargeRequiredException(chargeAmount, shortageAmount, user.getBalance(), totalPrice);
|
|
|
}
|
|
|
user.setBalance(user.getBalance() - totalPrice);
|
|
|
if (!user.update()) {
|
|
|
@@ -114,6 +121,10 @@ public class OrderService {
|
|
|
});
|
|
|
|
|
|
return MyRet.ok(orderType == 1 ? "抢购成功" : "预购成功");
|
|
|
+ } catch (ChargeRequiredException e) {
|
|
|
+ // 余额不足,创建通联充值订单
|
|
|
+ String message = "余额不足,请先完成充值。需要: " + e.requiredAmount + " 火花,你有:" + e.currentBalance + " 火花,还需充值: " + e.shortageAmount + " 火花";
|
|
|
+ return depositService.createTLDepositOrder(e.chargeAmount, userId, e.shortageAmount, e.currentBalance, e.requiredAmount, message).setCode(MyRet.CODE_NO_MONEY);
|
|
|
} catch (Exception e) {
|
|
|
return MyRet.fail(e.getMessage());
|
|
|
}
|
|
|
@@ -473,4 +484,22 @@ public class OrderService {
|
|
|
public static String generateOrderSn() {
|
|
|
return System.currentTimeMillis() + "" + (int)(Math.random() * 10000);
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 余额不足异常,用于触发创建充值订单
|
|
|
+ */
|
|
|
+ private static class ChargeRequiredException extends RuntimeException {
|
|
|
+ int chargeAmount; // 需要充值的金额(分)
|
|
|
+ float shortageAmount; // 缺少的金额(火花)
|
|
|
+ float currentBalance; // 当前余额(火花)
|
|
|
+ float requiredAmount; // 需要的总金额(火花)
|
|
|
+
|
|
|
+ ChargeRequiredException(int chargeAmount, float shortageAmount, float currentBalance, float requiredAmount) {
|
|
|
+ super("余额不足,需要充值");
|
|
|
+ this.chargeAmount = chargeAmount;
|
|
|
+ this.shortageAmount = shortageAmount;
|
|
|
+ this.currentBalance = currentBalance;
|
|
|
+ this.requiredAmount = requiredAmount;
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|