新版仿ok交易所-后端
zyy3
2025-09-26 a2ca1f655c816f801f8881136d17d19da915d99b
trading-order-service/src/main/java/com/yami/trading/service/impl/WalletServiceImpl.java
@@ -57,6 +57,7 @@
import java.time.LocalTime;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
@Slf4j
@@ -179,12 +180,12 @@
        wallet.setMoney(wallet.getMoney().add(money));
        if(wallet.getMoney().compareTo(BigDecimal.ZERO) < 0){
            wallet.setMoney(BigDecimal.ZERO);
        }
        wallet.setUpdateTime(now);
        if (wallet.getMoney().doubleValue() <= 0) {
            throw new YamiShopBindException("余额不足");
        }
        wallet.setUpdateTime(now);
        /*if (wallet.getMoney().doubleValue() < 0) {
            throw new YamiShopBindException("余额不足");
        }*/
        updateById(wallet);
        // 账变日志
        MoneyLog moneyLog = new MoneyLog();
@@ -216,45 +217,6 @@
        List<ContractOrder> contractOrders = getContractOrders(partyId);
        if (contractOrders != null) {
//            for (ContractOrder order : contractOrders) {
//                String symbol = order.getSymbol();
//                Item bySymbol = itemService.findBySymbol(symbol);
//                if (bySymbol == null) {
//                    continue;
//                }
//                // 类型不对直接continue
//                if (StringUtils.isNotEmpty(symbolType)) {
//                    if (!bySymbol.getType().equalsIgnoreCase(symbolType)) {
//                        continue;
//                    }
//
//                }
//                // 真正下单里
//                double order_volume = 1;
//
//                if (order.getLeverRate() != null && order.getLeverRate().compareTo(BigDecimal.ZERO) != 0) {
//                    order_volume = order.getVolumeOpen().divide(order.getLeverRate()).doubleValue();
//                } else {
//                    order_volume = order.getVolumeOpen().doubleValue();
//                }
//
//                double amount = Arith.add(Arith.mul(order_volume, order.getUnitAmount().doubleValue()), order.getProfit().doubleValue());
//                money_contract = Arith.add(amount, money_contract);
//                money_contract_deposit = Arith.add(order.getDeposit().doubleValue(), money_contract_deposit);
//                money_contract_profit = Arith.add(order.getProfit().doubleValue(), money_contract_profit);
//                // 只需要计算当日盈亏比例*金额就是当日盈亏
//
//                List<Realtime> realtimes = dataService.realtime(symbol);
//                if (CollectionUtils.isNotEmpty(realtimes)) {
//                    Realtime realtime = realtimes.get(0);
//                    // 当前每张金额*加杠杆后多少金额
//                    // 今天总体涨跌幅
//                    BigDecimal changeRatio = realtime.getClose().subtract(realtime.getOpen()).divide(realtime.getOpen(), 10, RoundingMode.HALF_UP);
//                    Double todayProfit = order.getUnitAmount().multiply(order.getVolumeOpen()).multiply(changeRatio).setScale(10, RoundingMode.HALF_UP).doubleValue();
//                    money_contract_profit_today += todayProfit;
//                }
//            }
            Map<String, List<ContractOrder>> groupedOrders = contractOrders.stream()
                    .collect(Collectors.groupingBy(ContractOrder::getState));
@@ -310,13 +272,35 @@
        if (!"".equals(partyId) && partyId != null) {
            wallet = findByUserId(partyId.toString());
        }
        List<WalletExtend> walletExtends = walletExtendService.findByUserId(partyId);
        AtomicReference<BigDecimal> walletExtendMoneyRef = new AtomicReference<>(BigDecimal.ZERO);
        walletExtends.forEach(f -> {
            double closePrice = getRealtimePrice(f.getWallettype());
            BigDecimal amount = new BigDecimal(String.valueOf(f.getAmount()));
            BigDecimal price = new BigDecimal(String.valueOf(closePrice));
            BigDecimal money = amount.multiply(price);
            walletExtendMoneyRef.updateAndGet(current -> current.add(money));
        });
        BigDecimal walletExtendMoney = walletExtendMoneyRef.get();
        CapitaltWallet userIdWallet = capitaltWalletMapper.selectOne(new LambdaQueryWrapper<CapitaltWallet>()
                .eq(CapitaltWallet::getUserId,partyId.toString()));
        moneys_contract.put("money_wallet", wallet.getMoney().doubleValue());//可用余额
        moneys_contract.put("money_contract", userIdWallet.getMoney().doubleValue()+wallet.getMoney().doubleValue()+money_contract);
        moneys_contract.put("money_contract", userIdWallet.getMoney().doubleValue()+wallet.getMoney().doubleValue()+money_contract+walletExtendMoney.doubleValue());
        return moneys_contract;
    }
    @Override
    public double getRealtimePrice(String symbol) {
        List<Realtime> realtimes = dataService.realtime(symbol);
        double close = 1;
        if (realtimes != null && realtimes.size() > 0) {
            close = realtimes.get(0).getClose().doubleValue();
        } else {
            close = 0;
            //throw new YamiShopBindException("参数错误");
        }
        return close;
    }
    private static LocalDateTime toLocalDateTime(Date date) {
        return LocalDateTime.ofInstant(date.toInstant(), java.time.ZoneId.systemDefault());
    }
@@ -772,6 +756,36 @@
    }
    @Override
    public Wallet updateToBeCovered(Wallet wallet, BigDecimal amount, Integer type) {
        amount = amount.abs();
        if (type == 1) {  //入款
            if(wallet.getAmountToBeCovered().compareTo(BigDecimal.ZERO) > 0){
                BigDecimal availableBalance = amount.subtract(wallet.getAmountToBeCovered());
                if(availableBalance.compareTo(BigDecimal.ZERO) >= 0){
                    wallet.setMoney(wallet.getMoney().add(availableBalance));
                    wallet.setAmountToBeCovered(BigDecimal.ZERO);
                }else {
                    wallet.setAmountToBeCovered(availableBalance.abs());
                }
            } else {
                wallet.setMoney(wallet.getMoney().add(amount));
            }
        } else if (type == 2) { //扣款
            BigDecimal subtract = amount.subtract(wallet.getMoney());
            if (subtract.compareTo(BigDecimal.ZERO) > 0) { //资金不足 放入待补
                wallet.setMoney(BigDecimal.ZERO);
                wallet.setAmountToBeCovered(wallet.getAmountToBeCovered().add(subtract));
            } else {
                wallet.setMoney(subtract.negate());
            }
        }
        if (!updateById(wallet)) {
            throw new YamiShopBindException("操作钱包失败!");
        }
        return wallet;
    }
    @Override
    public void updateExtendWithLockAndFreeze(String partyId, String walletType, double amount, double lockAmount, double freezeAmount) {