package com.yami.trading.service.impl; import cn.hutool.core.util.StrUtil; import cn.hutool.json.JSONUtil; 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; import com.yami.trading.bean.constans.WalletConstants; import com.yami.trading.bean.contract.domain.ContractApplyOrder; import com.yami.trading.bean.contract.domain.ContractOrder; import com.yami.trading.bean.data.domain.Realtime; import com.yami.trading.bean.exchange.ExchangeApplyOrder; 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.MoneyLog; import com.yami.trading.bean.model.Wallet; import com.yami.trading.bean.model.WalletExtend; import com.yami.trading.common.constants.ContractRedisKeys; import com.yami.trading.common.constants.WalletRedisKeys; import com.yami.trading.common.exception.BusinessException; import com.yami.trading.common.exception.YamiShopBindException; import com.yami.trading.common.util.ApplicationContextUtils; 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.user.WalletMapper; import com.yami.trading.service.MoneyLogService; import com.yami.trading.service.WalletService; import com.yami.trading.service.contract.ContractApplyOrderService; import com.yami.trading.service.contract.ContractOrderService; import com.yami.trading.service.data.DataService; import com.yami.trading.service.exchange.ExchangeApplyOrderService; import com.yami.trading.service.future.FuturesOrderService; import com.yami.trading.service.item.ItemService; import com.yami.trading.service.syspara.SysparaService; import com.yami.trading.service.user.WalletExtendService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Lazy; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.io.Serializable; 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.stream.Collectors; @Slf4j @Service public class WalletServiceImpl extends ServiceImpl implements WalletService { @Qualifier("dataService") @Autowired @Lazy private DataService dataService; @Autowired private SysparaService sysparaService; @Autowired private ItemService itemService; @Autowired MoneyLogService moneyLogService; @Autowired RedisTemplate redisTemplate; @Autowired WalletExtendService walletExtendService; @Override public Wallet findByUserId(String userId) { Wallet wallet = getOne(Wrappers.query().lambda().eq(Wallet::getUserId, userId)); return wallet; } @Override public List findExtend(String partyId) { List walletExtends = walletExtendService.findByUserId(partyId); return walletExtends; } @Override public List findExtend(String partyId, List list_symbol) { List keys = new LinkedList(); // for (String key : list_symbol) { // keys.add(WalletRedisKeys.WALLET_EXTEND_PARTY_ID + partyId.toString() + key); // } // // TODO: 2023/4/16 // Object[] objects = redisHandler.getList(keys.toArray(new String[0])); // if (objects != null && objects.length > 0) { // List result = new ArrayList(); // for (Object obj : objects) { // if (obj != null) // result.add((WalletExtend) obj); // } // return result; // } return walletExtendService.findByUserIdAndWallettype(partyId,list_symbol); } @Override public WalletExtend saveExtendByPara(String userId, String wallettype) { if (StringUtils.isEmptyString(wallettype) || userId == null || StringUtils.isEmptyString(userId.toString())) { log.error("saveExtendByPara fail,partyId:{},wallettype:{}", new Object[]{userId, wallettype}); throw new RuntimeException("saveExtendByPara fail"); } List walletExtends = walletExtendService.findByUserIdAndWallettype(userId, wallettype); if (CollectionUtils.isNotEmpty(walletExtends)) return walletExtends.get(0); WalletExtend walletExtend = new WalletExtend(); walletExtend.setPartyId(userId); walletExtend.setWallettype(wallettype); walletExtendService.save(walletExtend); return walletExtend; } @Override public void updateExtend(String partyId, String walletType, double amount) { List walletExtends = walletExtendService.findByUserIdAndWallettype(partyId,walletType); WalletExtend walletExtend=null; if (CollectionUtils.isNotEmpty(walletExtends)){ walletExtend=walletExtends.get(0); } if (walletExtend == null) { walletExtend = this.saveExtendByPara(partyId, walletType); } log.info(JSONUtil.toJsonStr(walletExtend)); log.info("=============111111===>"+walletType); walletExtend.setAmount(Arith.add(walletExtend.getAmount(), amount)); if (!walletExtendService.updateById(walletExtend)) { throw new YamiShopBindException("操作钱包失败!"); } redisTemplate.opsForValue().set(WalletRedisKeys.WALLET_EXTEND_PARTY_ID + partyId.toString() + walletType, walletExtend); } @Override public Wallet saveWalletByPartyId(String partyId) { Wallet wallet = findByUserId(partyId); if (wallet != null) { return wallet; } else { wallet = new Wallet(); wallet.setUserId(partyId); save(wallet); return wallet; } } @Override public BigDecimal sumMoney() { return baseMapper.sumMoney(); } @Override @Transactional public void updateMoney(String symbol, String userId, BigDecimal money, BigDecimal amountFee, String category, String walletType, String contentType, String log) { Date now = new Date(); Wallet wallet = findByUserId(userId); BigDecimal amountBefore = wallet.getMoney(); 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("余额不足"); } updateById(wallet); // 账变日志 MoneyLog moneyLog = new MoneyLog(); moneyLog.setCreateTime(now); moneyLog.setSymbol(symbol); moneyLog.setCategory(category); moneyLog.setAmountBefore(amountBefore); moneyLog.setAmount(money); moneyLog.setAmountAfter(wallet.getMoney()); moneyLog.setUserId(userId); moneyLog.setWalletType(walletType); moneyLog.setContentType(contentType); moneyLog.setLog(log); moneyLogService.save(moneyLog); } /* * 获取 所有订单 永续合约总资产、总保证金、总未实现盈利,当日盈利 */ @Override public Map 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;//当日盈亏 List 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 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> groupedOrders = contractOrders.stream() .collect(Collectors.groupingBy(ContractOrder::getState)); //持仓订单 List submittedOrderList = groupedOrders.get(ContractOrder.STATE_SUBMITTED); //总资产计算 if(CollectionUtils.isNotEmpty(submittedOrderList)){ //持仓单盈亏 BigDecimal totalProfit = submittedOrderList.stream() .map(ContractOrder::getProfit) .reduce(BigDecimal.ZERO, BigDecimal::add); //持仓单保证金 BigDecimal totalDeposit = submittedOrderList.stream() .map(ContractOrder::getDepositOpen) .reduce(BigDecimal.ZERO, BigDecimal::add); 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 moneys_contract = new HashMap(); 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); // usdt余额 Wallet wallet = new Wallet(); if (!"".equals(partyId) && partyId != null) { wallet = findByUserId(partyId.toString()); } moneys_contract.put("money_wallet", wallet.getMoney().doubleValue());//可用余额 moneys_contract.put("money_contract", wallet.getMoney().doubleValue()+money_contract); return moneys_contract; } private static LocalDateTime toLocalDateTime(Date date) { return LocalDateTime.ofInstant(date.toInstant(), java.time.ZoneId.systemDefault()); } private static List getContractOrders(Serializable partyId) { ContractOrderService contractOrderService = ApplicationContextUtils.getBean(ContractOrderService.class); QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq(StrUtil.isNotBlank(partyId.toString()), "party_id", partyId); queryWrapper.orderByDesc("create_time"); List contractOrders = contractOrderService.list(queryWrapper); return contractOrders; } // /** // * 获取总资产 // * // * @param userId // * @return // */ // @Override // public AssetsDto getAssets(String userId) { // AssetsDto assetsDto = new AssetsDto(); // Wallet wallet = findByUserId(userId); // assetsDto.setTotal(wallet.getMoney()); // assetsDto.setMoneyWallet(wallet.getMoney()); // return assetsDto; // } @Override public Map getMoneyAll(Serializable partyId) { Map data = new HashMap(); DecimalFormat df2 = new DecimalFormat("#.##"); double money = 0; double money_wallet = 0; double money_coin = 0; double money_all_coin = 0; double money_finance = 0; double money_miner = 0; double money_contractApply = 0; double money_contract = 0; double money_contract_deposit = 0; double money_contract_profit = 0; double money_futures = 0; double money_futures_profit = 0; // 先获取一次所有币种的数据来计算 String data_symbol = ""; List list_symbol = new ArrayList(); List list_items = this.itemService.cacheGetByMarket(""); for (int i = 0; i < list_items.size(); i++) { Item items = list_items.get(i); list_symbol.add(items.getSymbol()); if (i != 0) { data_symbol = data_symbol + "," + items.getSymbol(); } else { data_symbol = items.getSymbol(); } } List realtime_all = this.dataService.realtime(data_symbol); if (realtime_all.size() <= 0) { throw new BusinessException("系统错误,请稍后重试"); } // usdt余额 Wallet wallet = new Wallet(); if (!"".equals(partyId) && partyId != null) { wallet = saveWalletByPartyId(partyId.toString()); } money = wallet.getMoney().doubleValue(); // 钱包USDT money_wallet = wallet.getMoney().doubleValue(); // 币余额 money_coin = this.getMoneyCoin(partyId, realtime_all, list_symbol); money = money + money_coin; // 钱包USDT+币余额 money_all_coin = money; // 理财 todo // money_finance = this.getMoneyFinance(partyId, realtime_all); // money = money + money_finance; // // 矿机 todo // money_miner = this.getMoneyMiner(partyId, realtime_all); //// money_miner = this.getMoneyMinerRedis(partyId, realtime_all); // money = money + money_miner; // 永续委托 money_contractApply = getMoneyContractApply(partyId); money = money + money_contractApply; // Map moneys_contract = this.getMoneyContract(partyId); // // 永续 // money_contract = (Double) moneys_contract.get("money_contract"); // // 永续总保证金 // money_contract_deposit = (Double) moneys_contract.get("money_contract_deposit"); // // 永续总未实现盈亏 // money_contract_profit = (Double) moneys_contract.get("money_contract_profit"); Map moneys_contract = this.getMoneyContractRedis(partyId); if (null != moneys_contract && 0 != moneys_contract.size()) { // 永续 Object money_contract1 = moneys_contract.get("money_contract"); if(money_contract1 instanceof BigDecimal){ money_contract = ((BigDecimal) money_contract1).doubleValue(); }else{ money_contract = (Double) money_contract1; } // 永续总保证金 Object money_contract_deposit1 = moneys_contract.get("money_contract_deposit"); if(money_contract_deposit1 instanceof BigDecimal){ money_contract_deposit = ((BigDecimal) money_contract_deposit1).doubleValue(); }else{ money_contract_deposit = (Double) money_contract_deposit1; } // 永续总未实现盈亏 Object money_contract_profit1 = moneys_contract.get("money_contract_profit"); if(money_contract_deposit1 instanceof BigDecimal){ money_contract_profit = ((BigDecimal) money_contract_profit1).doubleValue(); }else{ money_contract_deposit = (Double) money_contract_deposit1; } } money = money + money_contract; // Map moneys_futures = this.getMoneyFutures(partyId); // // 交割 // money_futures = (Double) moneys_futures.get("money_futures"); // // 交割未实现盈亏 // money_futures_profit = (Double) moneys_futures.get("money_futures_profit"); Map moneys_futures = this.getMoneyFuturesRedis(partyId); if (null != moneys_futures && 0 != moneys_futures.size()) { // 交割 money_futures = (Double) moneys_futures.get("money_futures"); // 交割未实现盈亏 money_futures_profit = (Double) moneys_futures.get("money_futures_profit"); } money = money + money_futures; // 币币交易 money = money + this.getMoneyexchangeApplyOrders(partyId, realtime_all); data.put("total", df2.format(money)); //锁定金额 data.put("lock_money", df2.format(wallet.getLockMoney())); //冻结金额 data.put("freeze_money", df2.format(wallet.getFreezeMoney())); data.put("money_wallet", df2.format(money_wallet)); data.put("money_coin", df2.format(money_coin)); data.put("money_all_coin", df2.format(money_all_coin)); data.put("money_miner", df2.format(money_miner)); data.put("money_finance", df2.format(money_finance)); data.put("money_contract", df2.format(Arith.add(money_contract, money_contractApply))); data.put("money_contract_deposit", df2.format(money_contract_deposit)); data.put("money_contract_profit", df2.format(money_contract_profit)); data.put("money_futures", df2.format(money_futures)); data.put("money_futures_profit", df2.format(money_futures_profit)); return data; } @Override public Map getMoneyAll(Serializable partyId, String symbolType) { Map data = new HashMap(); DecimalFormat df2 = new DecimalFormat("#.##"); double money = 0; double money_wallet = 0; double money_coin = 0; double money_all_coin = 0; double money_finance = 0; double money_miner = 0; double money_contractApply = 0; double money_contract = 0; double money_contract_deposit = 0; double money_contract_profit = 0; double money_futures = 0; double money_futures_profit = 0; // 当前类型,持有的资产 double symbol_type_asserts = 0; // 先获取一次所有币种的数据来计算 String data_symbol = ""; List list_symbol = new ArrayList(); List list_items = this.itemService.findByType(symbolType); for (int i = 0; i < list_items.size(); i++) { Item items = list_items.get(i); list_symbol.add(items.getSymbol()); if (i != 0) { data_symbol = data_symbol + "," + items.getSymbol(); } else { data_symbol = items.getSymbol(); } } List realtime_all = this.dataService.realtime(data_symbol); if (realtime_all.size() <= 0) { throw new BusinessException("系统错误,请稍后重试"); } // usdt余额 Wallet wallet = new Wallet(); if (!"".equals(partyId) && partyId != null) { wallet = saveWalletByPartyId(partyId.toString()); } money = wallet.getMoney().doubleValue(); // 钱包USDT money_wallet = wallet.getMoney().doubleValue(); // 币余额 if(CollectionUtils.isEmpty(list_symbol)){ money_coin = 0; }else{ money_coin = this.getMoneyCoin(partyId, realtime_all, list_symbol); } money = money + money_coin; symbol_type_asserts = symbol_type_asserts+ money_coin; // 钱包USDT+币余额 money_all_coin = money; // 理财 todo // money_finance = this.getMoneyFinance(partyId, realtime_all); // money = money + money_finance; // // 矿机 todo // money_miner = this.getMoneyMiner(partyId, realtime_all); //// money_miner = this.getMoneyMinerRedis(partyId, realtime_all); // money = money + money_miner; // 永续委托 money_contractApply = getMoneyContractApply(partyId, list_symbol); money = money + money_contractApply; symbol_type_asserts = symbol_type_asserts+ money_contractApply; // Map moneys_contract = this.getMoneyContract(partyId); // // 永续 // money_contract = (Double) moneys_contract.get("money_contract"); // // 永续总保证金 // money_contract_deposit = (Double) moneys_contract.get("money_contract_deposit"); // // 永续总未实现盈亏 // money_contract_profit = (Double) moneys_contract.get("money_contract_profit"); Map moneys_contract = this.getMoneyContractDB(partyId, list_symbol); if (null != moneys_contract && 0 != moneys_contract.size()) { // 永续 Object money_contract1 = moneys_contract.get("money_contract"); if(money_contract1 instanceof BigDecimal){ money_contract = ((BigDecimal) money_contract1).doubleValue(); }else{ money_contract = (Double) money_contract1; } // 永续总保证金 Object money_contract_deposit1 = moneys_contract.get("money_contract_deposit"); if(money_contract_deposit1 instanceof BigDecimal){ money_contract_deposit = ((BigDecimal) money_contract_deposit1).doubleValue(); }else{ money_contract_deposit = (Double) money_contract_deposit1; } // 永续总未实现盈亏 Object money_contract_profit1 = moneys_contract.get("money_contract_profit"); if(money_contract_deposit1 instanceof BigDecimal){ money_contract_profit = ((BigDecimal) money_contract_profit1).doubleValue(); }else{ money_contract_deposit = (Double) money_contract_deposit1; } } money = money + money_contract; symbol_type_asserts = symbol_type_asserts+ money_contract; // Map moneys_futures = this.getMoneyFutures(partyId); // // 交割 // money_futures = (Double) moneys_futures.get("money_futures"); // // 交割未实现盈亏 // money_futures_profit = (Double) moneys_futures.get("money_futures_profit"); Map moneys_futures = this.getMoneyFuturesDB(partyId, list_symbol); if (null != moneys_futures && 0 != moneys_futures.size()) { // 交割 money_futures = (Double) moneys_futures.get("money_futures"); // 交割未实现盈亏 money_futures_profit = (Double) moneys_futures.get("money_futures_profit"); } money = money + money_futures; symbol_type_asserts = symbol_type_asserts+ money_futures; // 币币交易 double moneyexchangeApplyOrders = 0; if(CollectionUtils.isEmpty(list_symbol)){ moneyexchangeApplyOrders = this.getMoneyexchangeApplyOrders(partyId, realtime_all); } money = money + moneyexchangeApplyOrders; symbol_type_asserts = symbol_type_asserts+ moneyexchangeApplyOrders; data.put("total", df2.format(money)); data.put("symbol_type_asserts", df2.format(symbol_type_asserts)); //锁定金额 data.put("lock_money", df2.format(wallet.getLockMoney())); //冻结金额 data.put("freeze_money", df2.format(wallet.getFreezeMoney())); data.put("money_wallet", df2.format(money_wallet)); data.put("money_coin", df2.format(money_coin)); data.put("money_all_coin", df2.format(money_all_coin)); data.put("money_miner", df2.format(money_miner)); data.put("money_finance", df2.format(money_finance)); data.put("money_contract", df2.format(Arith.add(money_contract, money_contractApply))); data.put("money_contract_deposit", df2.format(money_contract_deposit)); data.put("money_contract_profit", df2.format(money_contract_profit)); data.put("money_futures", df2.format(money_futures)); data.put("money_futures_profit", df2.format(money_futures_profit)); return data; } /* * 获取 所有订单 永续合约总资产、总保证金、总未实现盈利 redis */ public Map getMoneyContractRedis(Serializable partyId) { BigDecimal contractAssets = (BigDecimal) RedisUtil.get(ContractRedisKeys.CONTRACT_ASSETS_PARTY_ID + partyId.toString()); BigDecimal contractAssetsDeposit = (BigDecimal)RedisUtil.get(ContractRedisKeys.CONTRACT_ASSETS_DEPOSIT_PARTY_ID + partyId.toString()); BigDecimal contractAssetsProfit = (BigDecimal) RedisUtil.get(ContractRedisKeys.CONTRACT_ASSETS_PROFIT_PARTY_ID + partyId.toString()); Map moneys_contract = new HashMap(); moneys_contract.put("money_contract", null == contractAssets ? 0.000D : contractAssets); moneys_contract.put("money_contract_deposit", null == contractAssetsDeposit ? 0.000D : contractAssetsDeposit); moneys_contract.put("money_contract_profit", null == contractAssetsProfit ? 0.000D : contractAssetsProfit); return moneys_contract; } public Map getMoneyContractDB(Serializable partyId, List symbols) { List list = ApplicationContextUtils.getBean(ContractOrderService.class).findSubmitted(partyId.toString(), symbols); // 永续合约:总资产、总保证金、总未实现盈利 Map> contractAssetsMap = new ConcurrentHashMap<>(); for (ContractOrder order : list) { if (ContractOrder.STATE_SUBMITTED.equals(order.getState())) { // 获取 单个订单 永续合约总资产、总保证金、总未实现盈利 Map contractAssetsOrder = getMoneyContractByOrder(order); if (contractAssetsMap.containsKey(order.getPartyId())) { Map contractAssetsOld = contractAssetsMap.get(order.getPartyId()); if (null == contractAssetsOld) { contractAssetsOld = new HashMap<>(); contractAssetsOld.put("money_contract", BigDecimal.ZERO); contractAssetsOld.put("money_contract_deposit", BigDecimal.ZERO); contractAssetsOld.put("money_contract_profit", BigDecimal.ZERO); } contractAssetsOld.put("money_contract", contractAssetsOld.get("money_contract").add(contractAssetsOrder.get("money_contract"))); contractAssetsOld.put("money_contract_deposit", contractAssetsOld.get("money_contract_deposit").add(contractAssetsOrder.get("money_contract_deposit"))); contractAssetsOld.put("money_contract_profit", contractAssetsOld.get("money_contract_profit").add(contractAssetsOrder.get("money_contract_profit"))); contractAssetsMap.put(order.getPartyId(), contractAssetsOld); } else { contractAssetsMap.put(order.getPartyId(), contractAssetsOrder); } } } return contractAssetsMap.get(partyId); } public double getMoneyContractApply(Serializable partyId) { double money_contractApply = 0; ContractApplyOrderService contractApplyOrderService = ApplicationContextUtils.getBean(ContractApplyOrderService.class); List contractApplyOrders = contractApplyOrderService.findSubmitted(partyId.toString(), "", "", ""); if (contractApplyOrders != null) { for (ContractApplyOrder order : contractApplyOrders) { double amount = Arith.mul(order.getVolumeOpen().doubleValue(), order.getUnitAmount().doubleValue()); money_contractApply = Arith.add(amount, money_contractApply); } } return money_contractApply; } public double getMoneyContractApply(Serializable partyId, List symbols) { double money_contractApply = 0; ContractApplyOrderService contractApplyOrderService = ApplicationContextUtils.getBean(ContractApplyOrderService.class); List contractApplyOrders = contractApplyOrderService.findSubmitted(partyId.toString(), "", "", ""); if (contractApplyOrders != null) { for (ContractApplyOrder order : contractApplyOrders) { if(symbols.contains(order.getSymbol())){ double amount = Arith.mul(order.getVolumeOpen().doubleValue(), order.getUnitAmount().doubleValue()); money_contractApply = Arith.add(amount, money_contractApply); } } } return money_contractApply; } @Override public Map getMoneyContractByOrder(ContractOrder order) { Map moneysContract = new HashMap(); if (null == order) { moneysContract.put("money_contract", BigDecimal.ZERO); moneysContract.put("money_contract_deposit", BigDecimal.ZERO); moneysContract.put("money_contract_profit", BigDecimal.ZERO); return moneysContract; } BigDecimal orderVolume = BigDecimal.ONE; if (order.getLeverRate() != null && order.getLeverRate().compareTo(BigDecimal.ZERO) != 0) { orderVolume = order.getVolumeOpen().divide(order.getLeverRate(), 2, BigDecimal.ROUND_HALF_UP); } else { orderVolume = order.getVolumeOpen(); } BigDecimal moneyContract = orderVolume.multiply(order.getUnitAmount()).add(order.getProfit()); BigDecimal moneyContractDeposit = order.getDeposit(); BigDecimal moneyContractProfit = order.getProfit(); moneysContract.put("money_contract", moneyContract); moneysContract.put("money_contract_deposit", moneyContractDeposit); moneysContract.put("money_contract_profit", moneyContractProfit); return moneysContract; } @Override public Map getMoneyFuturesByOrder(FuturesOrder order) { Map moneysFutures = new HashMap(); if (null == order) { moneysFutures.put("money_futures", 0.0); moneysFutures.put("money_futures_profit", 0.0); return moneysFutures; } Double moneyFutures = order.getVolume(); Double moneyFuturesProfit = order.getProfit(); moneysFutures.put("money_futures", moneyFutures); moneysFutures.put("money_futures_profit", moneyFuturesProfit); return moneysFutures; } @Override public void update(String userId, double amount) { Wallet wallet = findByUserId(userId); wallet.setMoney(new BigDecimal(Arith.add(wallet.getMoney().doubleValue(), amount))); if (!updateById(wallet)) { throw new YamiShopBindException("操作钱包失败!"); } } @Override public void updateExtendWithLockAndFreeze(String partyId, String walletType, double amount, double lockAmount, double freezeAmount) { List walletExtends=walletExtendService.findByUserIdAndWallettype(partyId, walletType); WalletExtend walletExtend = walletExtends.get(0); walletExtend.setAmount(Arith.add(walletExtend.getAmount(), amount)); walletExtend.setLockAmount(Arith.add(walletExtend.getLockAmount(), lockAmount)); walletExtend.setFreezeAmount(Arith.add(walletExtend.getFreezeAmount(), freezeAmount)); walletExtendService.updateById(walletExtend); } @Override public void updateWithLockAndFreeze(String partyId, double amount, double lockAmount, double freezeAmount) { Wallet wallet = (Wallet) findByUserId(partyId); wallet.setMoney(new BigDecimal(Arith.add(wallet.getMoney().doubleValue(), amount))); wallet.setLockMoney(new BigDecimal(Arith.add(wallet.getLockMoney().doubleValue(), lockAmount))); wallet.setFreezeMoney(new BigDecimal(Arith.add(wallet.getFreezeMoney().doubleValue(), freezeAmount))); updateById(wallet); } @Override public void updateMoney(String symbol, String userId, BigDecimal moneyRevise) { Wallet wallet = findByUserId(userId); BigDecimal amountBefore = wallet.getMoney(); wallet.setMoney(wallet.getMoney().add(moneyRevise)); wallet.setUpdateTime(new Date()); updateById(wallet); // 账变日志 MoneyLog moneyLog = new MoneyLog(); moneyLog.setCategory(WalletConstants.MONEYLOG_CATEGORY_COIN); moneyLog.setAmountBefore(amountBefore); moneyLog.setAmount(moneyRevise); moneyLog.setAmountAfter(wallet.getMoney().add(moneyRevise)); moneyLog.setUserId(userId); moneyLog.setWalletType(WalletConstants.WALLET_USDT); moneyLog.setContentType(WalletConstants.MONEYLOG_CONTENT_RECHARGE); moneyLogService.save(moneyLog); //钱包日志 // WalletLog walletLog = new WalletLog(); // walletLog.setCategory(Constants.MONEYLOG_CATEGORY_RECHARGE); // walletLog.setPartyId(partyId); // walletLog.setOrder_no(""); // walletLog.setStatus(1); // walletLog.setAmount(money_revise); // walletLog.setWallettype("USDT"); } public double getMoneyCoin(Serializable partyId, List realtime_all, List list_symbol) { double money_coin = 0; List walletExtends = findExtend(partyId.toString(), list_symbol); WalletExtend walletExtend = new WalletExtend(); if (realtime_all.size() <= 0) { String data_symbol = ""; for (int i = 0; i < walletExtends.size(); i++) { walletExtend = walletExtends.get(i); if (walletExtend.getAmount() > 0) { if (i != 0) { data_symbol = data_symbol + "," + walletExtend.getWallettype(); } else { data_symbol = walletExtend.getWallettype(); } } } walletExtend = new WalletExtend(); realtime_all = this.dataService.realtime(data_symbol); if (realtime_all.size() <= 0) { throw new BusinessException("系统错误,请稍后重试"); } } Realtime realtime = null; // 如果2个相同,则说明用户所有币账户已经生成 .toUpperCase()/ if (walletExtends != null && walletExtends.size() != 0) { for (int i = 0; i < walletExtends.size(); i++) { if (null == walletExtends.get(i)) { continue; } walletExtend = walletExtends.get(i); if (walletExtend.getAmount() > 0) { realtime = null; for (Realtime real : realtime_all) { if (real.getSymbol().equals(walletExtend.getWallettype().toLowerCase())) { realtime = real; break; } } if (realtime != null) { money_coin = Arith.add(money_coin, Arith.mul(realtime.getClose().doubleValue(), walletExtend.getAmount())); } } } } return money_coin; } /* * 获取 所有订单 交割合约总资产、总未实现盈利 redis */ public Map getMoneyFuturesRedis(Serializable partyId) { Double futuresAssets = (Double) RedisUtil.get(FuturesRedisKeys.FUTURES_ASSETS_PARTY_ID + partyId.toString()); Double futuresAssetsProfit = (Double) RedisUtil.get(FuturesRedisKeys.FUTURES_ASSETS_PROFIT_PARTY_ID + partyId.toString()); Map moneys_futures = new HashMap(); moneys_futures.put("money_futures", null == futuresAssets ? 0.000D : futuresAssets); moneys_futures.put("money_futures_profit", null == futuresAssetsProfit ? 0.000D : futuresAssetsProfit); return moneys_futures; } public Map getMoneyFuturesDB(Serializable partyId, List symbolos) { List list = ApplicationContextUtils.getBean(FuturesOrderService.class).findSubmitted(partyId.toString(), symbolos); // 交割合约:总资产、总未实现盈利 Map> futuresAssetsMap = new ConcurrentHashMap>(); for (FuturesOrder order : list) { // 获取 单个订单 交割合约总资产、总未实现盈利 Map futuresAssetsOrder = getMoneyFuturesByOrder(order); if (futuresAssetsMap.containsKey(order.getPartyId())) { Map futuresAssetsOld = futuresAssetsMap.get(order.getPartyId().toString()); if (null == futuresAssetsOld) { futuresAssetsOld = new HashMap(); futuresAssetsOld.put("money_futures", 0.000D); futuresAssetsOld.put("money_futures_profit", 0.000D); } futuresAssetsOld.put("money_futures", Arith.add(futuresAssetsOld.get("money_futures"), futuresAssetsOrder.get("money_futures"))); futuresAssetsOld.put("money_futures_profit", Arith.add(futuresAssetsOld.get("money_futures_profit"), futuresAssetsOrder.get("money_futures_profit"))); futuresAssetsMap.put(order.getPartyId().toString(), futuresAssetsOld); } else { futuresAssetsMap.put(order.getPartyId().toString(), futuresAssetsOrder); } } return futuresAssetsMap.get(partyId); } /* * 获取 所有订单 交割合约总资产、总未实现盈利 */ public Map getMoneyFutures(Serializable partyId) { double money_futures = 0; double money_futures_profit = 0; FuturesOrderService futuresOrderService = ApplicationContextUtils.getBean(FuturesOrderService.class); List futuresOrders = futuresOrderService.cacheSubmitted(); if (futuresOrders != null) { for (FuturesOrder order : futuresOrders) { if (partyId.equals(order.getPartyId().toString())) { money_futures = Arith.add(order.getVolume(), money_futures); money_futures_profit = Arith.add(order.getProfit(), money_futures_profit); } } } Map moneys_futures = new HashMap(); moneys_futures.put("money_futures", money_futures); moneys_futures.put("money_futures_profit", money_futures_profit); return moneys_futures; } public double getMoneyexchangeApplyOrders(Serializable partyId, List realtimeall) { double moneyExchange = 0; ExchangeApplyOrderService exchangeApplyOrderService = ApplicationContextUtils.getBean(ExchangeApplyOrderService.class); List exchangeApplyOrders = exchangeApplyOrderService.findSubmitted(); if (exchangeApplyOrders != null) { for (ExchangeApplyOrder order : exchangeApplyOrders) { if (partyId.equals(order.getPartyId().toString())) { if ("open".equals(order.getOffset())) { moneyExchange = Arith.add(moneyExchange, order.getVolume()); } if ("close".equals(order.getOffset())) { Realtime realtime = new Realtime(); if (realtimeall.size() <= 0) { List realtime_list = this.dataService.realtime(order.getSymbol()); if (realtime_list.size() > 0) { realtime = realtime_list.get(0); } else { throw new BusinessException("系统错误,请稍后重试"); } } else { for (Realtime real : realtimeall) { if (real.getSymbol().equals(order.getSymbol())) { realtime = real; break; } } } moneyExchange = Arith.add(moneyExchange, Arith.mul(order.getVolume(), realtime.getClose().doubleValue())); } } } } return moneyExchange; } }