From bdb57962dba08b4177d500639ec56fbd74306c67 Mon Sep 17 00:00:00 2001
From: zyy <zyy@email.com>
Date: Fri, 06 Feb 2026 17:39:04 +0800
Subject: [PATCH] 提现

---
 trading-order-service/src/main/java/com/yami/trading/service/impl/WalletServiceImpl.java |  178 ++++++++++++++++++++++++++++++++++++++++++++---------------
 1 files changed, 133 insertions(+), 45 deletions(-)

diff --git a/trading-order-service/src/main/java/com/yami/trading/service/impl/WalletServiceImpl.java b/trading-order-service/src/main/java/com/yami/trading/service/impl/WalletServiceImpl.java
index 0ac07ba..aaa0c4f 100644
--- a/trading-order-service/src/main/java/com/yami/trading/service/impl/WalletServiceImpl.java
+++ b/trading-order-service/src/main/java/com/yami/trading/service/impl/WalletServiceImpl.java
@@ -1,6 +1,9 @@
 package com.yami.trading.service.impl;
 
+import cn.hutool.core.util.StrUtil;
 import cn.hutool.json.JSONUtil;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
@@ -12,6 +15,7 @@
 import com.yami.trading.bean.future.domain.FuturesOrder;
 import com.yami.trading.bean.future.domain.FuturesRedisKeys;
 import com.yami.trading.bean.item.domain.Item;
+import com.yami.trading.bean.model.CapitaltWallet;
 import com.yami.trading.bean.model.MoneyLog;
 import com.yami.trading.bean.model.Wallet;
 import com.yami.trading.bean.model.WalletExtend;
@@ -23,7 +27,9 @@
 import com.yami.trading.common.util.Arith;
 import com.yami.trading.common.util.RedisUtil;
 import com.yami.trading.common.util.StringUtils;
+import com.yami.trading.dao.CapitaltWalletMapper;
 import com.yami.trading.dao.user.WalletMapper;
+import com.yami.trading.service.CapitaltWalletService;
 import com.yami.trading.service.MoneyLogService;
 import com.yami.trading.service.WalletService;
 import com.yami.trading.service.contract.ContractApplyOrderService;
@@ -46,8 +52,13 @@
 import java.math.BigDecimal;
 import java.math.RoundingMode;
 import java.text.DecimalFormat;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
 import java.util.*;
 import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.stream.Collectors;
 
 @Slf4j
 @Service
@@ -64,7 +75,8 @@
     @Autowired
     MoneyLogService moneyLogService;
 
-
+    @Autowired
+    CapitaltWalletMapper capitaltWalletMapper;
     @Autowired
     RedisTemplate redisTemplate;
 
@@ -165,11 +177,15 @@
         Date now = new Date();
         Wallet wallet = findByUserId(userId);
         BigDecimal amountBefore = wallet.getMoney();
+
         wallet.setMoney(wallet.getMoney().add(money));
-        wallet.setUpdateTime(now);
-        if (wallet.getMoney().doubleValue() < 0) {
+        if(wallet.getMoney().compareTo(BigDecimal.ZERO) < 0){
             throw new YamiShopBindException("余额不足");
         }
+        wallet.setUpdateTime(now);
+        /*if (wallet.getMoney().doubleValue() < 0) {
+            throw new YamiShopBindException("余额不足");
+        }*/
         updateById(wallet);
         // 账变日志
         MoneyLog moneyLog = new MoneyLog();
@@ -191,58 +207,63 @@
     /*
      * 获取 所有订单 永续合约总资产、总保证金、总未实现盈利,当日盈利
      */
+    @Override
     public Map<String, Double> getMoneyContract(Serializable partyId, String symbolType) {
-        double money_contract = 0;
-        double money_contract_deposit = 0;
-        double money_contract_profit = 0;
-        double money_contract_profit_today = 0;
+        double money_contract = 0;//总资产
+        double money_contract_deposit = 0;//总保证金
+        double money_contract_profit = 0;//浮动盈亏
+        double money_contract_profit_today = 0;//当日盈亏
 
-        ContractOrderService contractOrderService = ApplicationContextUtils.getBean(ContractOrderService.class);
-        List<ContractOrder> contractOrders = contractOrderService.findSubmitted(partyId.toString(), "", "");
+        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;
+            Map<String, List<ContractOrder>> groupedOrders = contractOrders.stream()
+                    .collect(Collectors.groupingBy(ContractOrder::getState));
+            //持仓订单
+            List<ContractOrder> submittedOrderList = groupedOrders.get(ContractOrder.STATE_SUBMITTED);
 
-                if (order.getLeverRate() != null && order.getLeverRate().compareTo(BigDecimal.ZERO) != 0) {
-                    order_volume = order.getVolumeOpen().divide(order.getLeverRate()).doubleValue();
-                } else {
-                    order_volume = order.getVolumeOpen().doubleValue();
-                }
+            //总资产计算
+            if(CollectionUtils.isNotEmpty(submittedOrderList)){
+                //持仓单盈亏
+                BigDecimal totalProfit = submittedOrderList.stream()
+                        .map(ContractOrder::getProfit)
+                        .reduce(BigDecimal.ZERO, BigDecimal::add);
 
-                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);
-                // 只需要计算当日盈亏比例*金额就是当日盈亏
+                //持仓单保证金
+                BigDecimal totalDeposit = submittedOrderList.stream()
+                        .map(ContractOrder::getDepositOpen)
+                        .reduce(BigDecimal.ZERO, BigDecimal::add);
 
-                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;
-                }
+                money_contract = totalProfit.add(totalDeposit).doubleValue();
             }
+
+            //浮动盈亏
+            if(CollectionUtils.isNotEmpty(submittedOrderList)){
+                BigDecimal totalProfit = submittedOrderList.stream()
+                        .map(ContractOrder::getProfit)
+                        .reduce(BigDecimal.ZERO, BigDecimal::add);
+                money_contract_profit = totalProfit.doubleValue();
+            }
+
+            //当日盈亏
+            LocalDateTime startOfDay = LocalDate.now().atStartOfDay();
+            LocalDateTime endOfDay = LocalDate.now().atTime(LocalTime.MAX);
+            // 获取今天的订单
+            BigDecimal totalRevenue = contractOrders.stream()
+                    .filter(order -> {
+                        LocalDateTime orderDate = toLocalDateTime(order.getCreateTime()); // 转换为 LocalDateTime
+                        return !orderDate.isBefore(startOfDay) && !orderDate.isAfter(endOfDay);
+                    })
+                    .map(ContractOrder::getProfit) // 获取订单的收益
+                    .reduce(BigDecimal.ZERO, BigDecimal::add);
+
+
+            money_contract_profit_today = totalRevenue.doubleValue();
         }
 
         Map<String, Double> moneys_contract = new HashMap<String, Double>();
-        moneys_contract.put("money_contract", money_contract);
+
         moneys_contract.put("money_contract_deposit", money_contract_deposit);
         moneys_contract.put("money_contract_profit", money_contract_profit);
         moneys_contract.put("money_contract_profit_today", money_contract_profit_today);
@@ -251,9 +272,46 @@
         if (!"".equals(partyId) && partyId != null) {
             wallet = findByUserId(partyId.toString());
         }
-        moneys_contract.put("money_wallet", wallet.getMoney().doubleValue());
-
+        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+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());
+    }
+
+    private static List<ContractOrder> getContractOrders(Serializable partyId) {
+        ContractOrderService contractOrderService = ApplicationContextUtils.getBean(ContractOrderService.class);
+        QueryWrapper<ContractOrder> queryWrapper = new QueryWrapper<>();
+        queryWrapper.eq(StrUtil.isNotBlank(partyId.toString()), "party_id", partyId);
+        queryWrapper.orderByDesc("create_time");
+        List<ContractOrder> contractOrders = contractOrderService.list(queryWrapper);
+        return contractOrders;
     }
 
 //    /**
@@ -698,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) {

--
Gitblit v1.9.3