package project.wallet.internal; import kernel.exception.BusinessException; import kernel.util.Arith; import kernel.util.StringUtils; import kernel.util.UUIDGenerator; import kernel.web.ApplicationUtil; import kernel.web.ResultObject; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import project.data.DataService; import project.data.model.Realtime; import project.party.PartyService; import project.redis.RedisHandler; import project.syspara.SysparaService; import project.wallet.*; import java.io.Serializable; import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; public class WalletGatherServiceImpl implements WalletGatherService { private JdbcTemplate jdbcTemplate; private RedisHandler redisHandler; private WalletService walletService; // 构造函数,传入 DataSource 初始化 jdbcTemplate public WalletGatherServiceImpl(JdbcTemplate jdbcTemplate,RedisHandler redisHandler,WalletService walletService) { this.jdbcTemplate = jdbcTemplate; this.redisHandler = redisHandler; this.walletService = walletService; } @Override public WalletGather getWalletGatherByPartyId(Serializable partyId, DataService dataService) { //先从redis中获取 // WalletGather walletGather = (WalletGather)redisHandler.get(WalletRedisKeys.WALLET_GATHER_PARTY_ID + partyId.toString()); WalletGather walletGather = null; if(null == walletGather){ List walletGathers = jdbcTemplate.query( "SELECT UUID id, PARTY_ID partyId, USDT_MONEY usdtMoney, BTC_MONEY btcMoney, ETH_MONEY ethMoney, USDC_MONEY usdcMoney FROM T_WALLET_GATHER WHERE PARTY_ID=?", new Object[]{partyId}, BeanPropertyRowMapper.newInstance(WalletGather.class) ); // 处理查询结果,可以检查列表是否为空 if (!walletGathers.isEmpty()) { walletGather = walletGathers.get(0); } } if(null == walletGather){ walletGather = save(partyId.toString()); } System.out.println("判断dataService是否为空"+dataService); if(null != dataService){ //查总数 List symbols = new ArrayList<>(); if(walletGather.getBtcMoney() > 0){ symbols.add("btc"); } if(walletGather.getEthMoney() > 0){ symbols.add("eth"); } if(walletGather.getUsdcMoney() > 0){ symbols.add("usdc"); } System.out.println("组装symbols"+symbols); System.out.println("组装symbols"+symbols.size()); String data_symbol = ""; double money_coin = 0; if(symbols.size() > 0){ for (int i = 0; i < symbols.size(); i++) { if (i != 0) { data_symbol = data_symbol + "," + symbols.get(i); } else { data_symbol = symbols.get(i); } } System.out.println("组装data_symbol"+data_symbol); List realtime_all = dataService.realtime(data_symbol); System.out.println("查询realtime_all"+realtime_all); if (realtime_all.size() <= 0) { throw new BusinessException("系统错误,请稍后重试"); } Realtime realtime = null; for (int i = 0; i < symbols.size(); i++) { for (Realtime real : realtime_all) { realtime = null; if (real.getSymbol().equals(symbols.get(i).toLowerCase())) { realtime = real; } if (realtime != null) { if("btc".equals(symbols.get(i))){ money_coin = Arith.add(money_coin, Arith.mul(realtime.getClose(), walletGather.getBtcMoney())); }else if("eth".equals(symbols.get(i))){ money_coin = Arith.add(money_coin, Arith.mul(realtime.getClose(), walletGather.getEthMoney())); }else if("usdc".equals(symbols.get(i))){ money_coin = Arith.add(money_coin, Arith.mul(realtime.getClose(), walletGather.getUsdcMoney())); } } } } }else { money_coin = walletGather.getUsdtMoney(); } walletGather.setTotalMoney(money_coin); } return walletGather; } @Override public WalletGather save(String partyId) { WalletGather walletGather = new WalletGather(); walletGather.setId(UUIDGenerator.getUUID()); walletGather.setPartyId(partyId); ApplicationUtil.executeInsert(walletGather); redisHandler.setAsyn(WalletRedisKeys.WALLET_GATHER_PARTY_ID + walletGather.getPartyId().toString(),walletGather); return walletGather; } @Override public void update(String partyId, String currency, double amount,String mark) { System.out.println("update 方法:参数partyId="+partyId+",参数currency="+currency+",参数amount="+amount+",参数mark="+mark); WalletGather walletGather = getWalletGatherByPartyId(partyId,null); if("usdt".equals(currency)){ if("add".equals(mark)){ walletGather.setUsdtMoney(Arith.add(walletGather.getUsdtMoney(), amount)); }else { walletGather.setUsdtMoney(Arith.sub(walletGather.getUsdtMoney(), amount)); } }else if("btc".equals(currency)){ if("add".equals(mark)){ walletGather.setBtcMoney(Arith.add(walletGather.getBtcMoney(), amount)); }else { walletGather.setBtcMoney(Arith.sub(walletGather.getBtcMoney(), amount)); } }else if("eth".equals(currency)){ if("add".equals(mark)){ walletGather.setEthMoney(Arith.add(walletGather.getEthMoney(), amount)); }else { walletGather.setEthMoney(Arith.sub(walletGather.getEthMoney(), amount)); } }else if("usdc".equals(currency)){ if("add".equals(mark)){ walletGather.setUsdcMoney(Arith.add(walletGather.getUsdcMoney(), amount)); }else { walletGather.setUsdcMoney(Arith.sub(walletGather.getUsdcMoney(), amount)); } } ApplicationUtil.executeUpdate(walletGather); redisHandler.setAsyn(WalletRedisKeys.WALLET_GATHER_PARTY_ID + walletGather.getPartyId().toString(),walletGather); } @Override public ResultObject transfer(String partyId,String currency, String fromTo, String amount,String safeword,PartyService partyService,SysparaService sysparaService) { // 交易所提现是否需要资金密码 String exchange_withdraw_need_safeword = sysparaService.find("exchange_withdraw_need_safeword").getValue(); if(StringUtils.isEmptyString(exchange_withdraw_need_safeword)) { throw new BusinessException("系统参数错误"); } if ("true".equals(exchange_withdraw_need_safeword)) { if (StringUtils.isEmptyString(safeword)) { ResultObject resultObject = new ResultObject(); resultObject.setCode("1"); resultObject.setMsg("资金密码不能为空"); return resultObject; } if (safeword.length() < 6 || safeword.length() > 12) { ResultObject resultObject = new ResultObject(); resultObject.setCode("1"); resultObject.setMsg("资金密码必须6-12位"); return resultObject; } if (!partyService.checkSafeword(safeword, partyId)) { ResultObject resultObject = new ResultObject(); resultObject.setCode("1"); resultObject.setMsg("资金密码错误"); return resultObject; } } WalletGather walletGather = getWalletGatherByPartyId(partyId,null); List walletGathers = jdbcTemplate.query( "SELECT UUID id, PARTY_ID partyId, USDT_MONEY usdtMoney, BTC_MONEY btcMoney, ETH_MONEY ethMoney, USDC_MONEY usdcMoney FROM T_WALLET_GATHER WHERE PARTY_ID=?", new Object[]{partyId}, BeanPropertyRowMapper.newInstance(WalletGather.class) ); // 处理查询结果,可以检查列表是否为空 if (!walletGathers.isEmpty()) { walletGather = walletGathers.get(0); } WalletExtend walletExtend = walletService.saveExtendByPara(partyId, currency); double volume = Double.parseDouble(amount); if("1".equals(fromTo)){ //资金账号=>交易账户 System.out.println("资金账号=>交易账户"); return this.transferGathertoExtend(walletGather,walletExtend,currency,volume); }else { //交易账户=>资金账户 System.out.println("交易账户=>资金账户"); return this.transferExtendtoGather(walletGather,walletExtend,currency,volume); } } @Override public ResultObject getParameter(String partyId, String currency, String fromTo) { ResultObject resultObject = new ResultObject(); if(org.apache.commons.lang3.StringUtils.isBlank(partyId) || org.apache.commons.lang3.StringUtils.isBlank(currency) || org.apache.commons.lang3.StringUtils.isBlank(fromTo)){ resultObject.setCode("1"); resultObject.setCode("参数错误"); return resultObject; } resultObject.setCode("0"); resultObject.setCode("操作成功"); if("1".equals(fromTo)){ WalletGather walletGather = this.getWalletGatherByPartyId(partyId,null); if("usdt".equals(currency)){ resultObject.setData(walletGather.getUsdtMoney()); }else if("btc".equals(currency)){ resultObject.setData(walletGather.getBtcMoney()); }else if("eth".equals(currency)){ resultObject.setData(walletGather.getEthMoney()); }else if("usdc".equals(currency)){ resultObject.setData(walletGather.getUsdcMoney()); } return resultObject; }else { //交易账户=>资金账户 WalletExtend walletExtend = walletService.saveExtendByPara(partyId, currency); resultObject.setData(walletExtend.getAmount()); return resultObject; } } public ResultObject transferExtendtoGather(WalletGather walletGather,WalletExtend walletExtend,String currency,double amount){ ResultObject resultObject = new ResultObject(); resultObject.setCode("0"); if(new BigDecimal(walletExtend.getAmount()).compareTo(new BigDecimal(amount)) >= 0){ if("usdt".equals(currency)){ walletGather.setUsdtMoney(Arith.add(walletGather.getUsdtMoney(), amount)); }else if("btc".equals(currency)){ walletGather.setBtcMoney(Arith.add(walletGather.getBtcMoney(), amount)); }else if("eth".equals(currency)){ walletGather.setEthMoney(Arith.add(walletGather.getEthMoney(), amount)); }else if("usdc".equals(currency)){ walletGather.setUsdcMoney(Arith.add(walletGather.getUsdcMoney(), amount)); } }else { resultObject.setCode("1"); resultObject.setMsg("划转资金不足"); return resultObject; } ApplicationUtil.executeUpdate(walletGather); redisHandler.setAsyn(WalletRedisKeys.WALLET_GATHER_PARTY_ID + walletGather.getPartyId().toString(),walletGather); walletService.updateExtend(walletExtend.getPartyId().toString(), walletExtend.getWallettype(), -amount); resultObject.setMsg("划转成功"); return resultObject; } public ResultObject transferGathertoExtend(WalletGather walletGather,WalletExtend walletExtend,String currency,double amount){ ResultObject resultObject = new ResultObject(); resultObject.setCode("0"); if("usdt".equals(currency)){ if(new BigDecimal(walletGather.getUsdtMoney()).compareTo(new BigDecimal(amount)) >= 0){ walletGather.setUsdtMoney(Arith.sub(walletGather.getUsdtMoney(), amount)); }else { resultObject.setCode("1"); resultObject.setMsg("划转资金不足"); return resultObject; } }else if("btc".equals(currency)){ if(new BigDecimal(walletGather.getUsdtMoney()).compareTo(new BigDecimal(amount)) >= 0){ walletGather.setBtcMoney(Arith.sub(walletGather.getBtcMoney(), amount)); }else { resultObject.setCode("1"); resultObject.setMsg("划转资金不足"); return resultObject; } }else if("eth".equals(currency)){ if(new BigDecimal(walletGather.getUsdtMoney()).compareTo(new BigDecimal(amount)) >= 0){ walletGather.setEthMoney(Arith.sub(walletGather.getEthMoney(), amount)); }else { resultObject.setCode("1"); resultObject.setMsg("划转资金不足"); return resultObject; } }else if("usdc".equals(currency)){ if(new BigDecimal(walletGather.getUsdtMoney()).compareTo(new BigDecimal(amount)) >= 0){ walletGather.setUsdcMoney(Arith.sub(walletGather.getUsdcMoney(), amount)); }else { resultObject.setCode("1"); resultObject.setMsg("划转资金不足"); return resultObject; } } ApplicationUtil.executeUpdate(walletGather); redisHandler.setAsyn(WalletRedisKeys.WALLET_GATHER_PARTY_ID + walletGather.getPartyId().toString(),walletGather); walletService.updateExtend(walletExtend.getPartyId().toString(), walletExtend.getWallettype(), amount); resultObject.setMsg("划转成功"); return resultObject; } }