From bcd36411feb0351347289476eb7ee9e5dd1c3b9d Mon Sep 17 00:00:00 2001
From: zj <1772600164@qq.com>
Date: Wed, 28 May 2025 02:18:06 +0800
Subject: [PATCH] 修改充值地址

---
 src/main/java/project/contract/internal/ContractOrderServiceImpl.java |  200 +++++++++++++++++++++++++++++++++-----------------
 1 files changed, 132 insertions(+), 68 deletions(-)

diff --git a/src/main/java/project/contract/internal/ContractOrderServiceImpl.java b/src/main/java/project/contract/internal/ContractOrderServiceImpl.java
index 1b7b665..e309390 100644
--- a/src/main/java/project/contract/internal/ContractOrderServiceImpl.java
+++ b/src/main/java/project/contract/internal/ContractOrderServiceImpl.java
@@ -1,24 +1,22 @@
 package project.contract.internal;
 
+import java.math.BigDecimal;
+import java.math.RoundingMode;
 import java.text.DecimalFormat;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.Comparator;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.text.SimpleDateFormat;
+import java.util.*;
 import java.util.Map.Entry;
 import java.util.concurrent.ConcurrentHashMap;
 
 import org.apache.commons.collections.CollectionUtils;
-import org.apache.commons.lang3.ObjectUtils;
 
 import kernel.util.Arith;
 import kernel.util.DateUtils;
 import kernel.util.StringUtils;
 import kernel.web.ApplicationUtil;
 import kernel.web.Page;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.util.ObjectUtils;
 import project.Constants;
 import project.contract.ContractApplyOrder;
 import project.contract.ContractApplyOrderService;
@@ -80,25 +78,25 @@
 
         ApplicationUtil.executeInsert(order);
 		redisHandler.setSync(ContractRedisKeys.CONTRACT_ORDERNO + order.getOrder_no(), order);
-		
+
 		Map<String, ContractOrder> map = (Map<String, ContractOrder>) redisHandler.get(ContractRedisKeys.CONTRACT_SUBMITTED_ORDER_PARTY_ID + order.getPartyId().toString());
 		if (map == null) map = new ConcurrentHashMap<String, ContractOrder>();
 		map.put(order.getOrder_no(), order);
 		redisHandler.setSync(ContractRedisKeys.CONTRACT_SUBMITTED_ORDER_PARTY_ID + order.getPartyId().toString(), map);
 		// 获取单个订单的合约总资产、总保证金、总未实现盈利
 		Map<String, Double> contractAssetsOrder = this.assetService.getMoneyContractByOrder(order);
-		
+
 		Double contractAssets = (Double) this.redisHandler.get(ContractRedisKeys.CONTRACT_ASSETS_PARTY_ID + order.getPartyId().toString());
 		Double contractAssetsDeposit = (Double) this.redisHandler.get(ContractRedisKeys.CONTRACT_ASSETS_DEPOSIT_PARTY_ID + order.getPartyId().toString());
 		Double contractAssetsProfit = (Double) this.redisHandler.get(ContractRedisKeys.CONTRACT_ASSETS_PROFIT_PARTY_ID + order.getPartyId().toString());
-		
-		this.redisHandler.setSync(ContractRedisKeys.CONTRACT_ASSETS_PARTY_ID + order.getPartyId().toString(), 
+
+		this.redisHandler.setSync(ContractRedisKeys.CONTRACT_ASSETS_PARTY_ID + order.getPartyId().toString(),
 				Arith.add(null == contractAssets ? 0.000D : contractAssets, contractAssetsOrder.get("money_contract")));
-		this.redisHandler.setSync(ContractRedisKeys.CONTRACT_ASSETS_DEPOSIT_PARTY_ID + order.getPartyId().toString(), 
-				Arith.add(null == contractAssetsDeposit ? 0.000D : contractAssetsDeposit, contractAssetsOrder.get("money_contract_deposit")));		
-		this.redisHandler.setSync(ContractRedisKeys.CONTRACT_ASSETS_PROFIT_PARTY_ID + order.getPartyId().toString(),  
+		this.redisHandler.setSync(ContractRedisKeys.CONTRACT_ASSETS_DEPOSIT_PARTY_ID + order.getPartyId().toString(),
+				Arith.add(null == contractAssetsDeposit ? 0.000D : contractAssetsDeposit, contractAssetsOrder.get("money_contract_deposit")));
+		this.redisHandler.setSync(ContractRedisKeys.CONTRACT_ASSETS_PROFIT_PARTY_ID + order.getPartyId().toString(),
 				Arith.add(null == contractAssetsProfit ? 0.000D : contractAssetsProfit, contractAssetsOrder.get("money_contract_profit")));
-		
+
 		/**
 		 * 进入市场
 		 */
@@ -106,7 +104,7 @@
 		applyOrder.setState(ContractApplyOrder.STATE_CREATED);
 
 		this.contractApplyOrderService.update(applyOrder);
-		
+
 		Party party = this.partyService.cachePartyBy(order.getPartyId(), false);
 		if (Constants.SECURITY_ROLE_MEMBER.equals(party.getRolename())) {
 			tipService.saveTip(order.getId().toString(), TipConstants.CONTRACT_ORDER);
@@ -133,8 +131,19 @@
 		double profit = this.settle(order, volume);
 		update(order);
 //		if (profit > 0) {
+
 		Wallet wallet = this.walletService.saveWalletByPartyId(order.getPartyId());
 		double amount_before = wallet.getMoney();
+		Object money = redisHandler.get("PARTY_ID_MONEY_" + order.getPartyId().toString());
+		if(ObjectUtils.isEmpty(money)){
+			wallet.setMoney(wallet.getMoney());
+		}else{
+			wallet.setMoney(Double.parseDouble(money.toString()));
+		}
+
+		if (Arith.add(wallet.getMoney(), profit) < 0) {
+			profit = Arith.sub(0, wallet.getMoney());
+		}
 
 //		wallet.setMoney(Arith.add(wallet.getMoney(), profit));/
 		if (Arith.add(wallet.getMoney(), profit) < 0) {
@@ -168,7 +177,7 @@
 
 	/**
 	 * 根据用户批量赎回订单
-	 * 
+	 *
 	 * @param partyId
 	 */
 	public void saveCloseRemoveAllByPartyId(String partyId) {
@@ -180,9 +189,9 @@
 					saveClose(order.getPartyId().toString(), order.getOrder_no());
 				}
 				redisHandler.remove(ContractRedisKeys.CONTRACT_ORDERNO + order.getOrder_no());
-			}			
+			}
 			redisHandler.remove(ContractRedisKeys.CONTRACT_SUBMITTED_ORDER_PARTY_ID + partyId);
-			
+
 			this.redisHandler.remove(ContractRedisKeys.CONTRACT_ASSETS_PARTY_ID + partyId);
 			this.redisHandler.remove(ContractRedisKeys.CONTRACT_ASSETS_DEPOSIT_PARTY_ID + partyId);
 			this.redisHandler.remove(ContractRedisKeys.CONTRACT_ASSETS_PROFIT_PARTY_ID + partyId);
@@ -197,7 +206,7 @@
 		if (order == null || !ContractOrder.STATE_SUBMITTED.equals(order.getState()) || !partyId.equals(order.getPartyId().toString()) || order.getVolume() <= 0) {
 			return null; //状态已改变,退出处理
 		}
-		
+
 		/**
 		 * 收益
 		 */
@@ -206,7 +215,12 @@
 
 		Wallet wallet = this.walletService.saveWalletByPartyId(order.getPartyId());
 		double amount_before = wallet.getMoney();
-
+		Object money = redisHandler.get("PARTY_ID_MONEY_" + order.getPartyId().toString());
+		if(ObjectUtils.isEmpty(money)){
+			wallet.setMoney(wallet.getMoney());
+		}else{
+			wallet.setMoney(Double.parseDouble(money.toString()));
+		}
 
 		if (Arith.add(wallet.getMoney(), profit) < 0) {
 			profit = Arith.sub(0, wallet.getMoney());
@@ -228,12 +242,6 @@
 
 		moneyLogService.save(moneylog);
 
-		order.setState(ContractOrder.STATE_CREATED);
-		order.setVolume(0D);
-		order.setDeposit(0);
-		order.setClose_time(new Date());
-		update(order);
-
 		/**
 		 * 合约产品平仓后添加当前流水
 		 */
@@ -249,30 +257,30 @@
 		String id = null == order.getId() ? ApplicationUtil.getCurrentTimeUUID() : order.getId().toString();
 		ApplicationUtil.executeInsertOrUpdate(order, "WHERE UUID=?", new Object[] {id});
 		redisHandler.setSync(ContractRedisKeys.CONTRACT_ORDERNO + order.getOrder_no(), order);
-		
+
 		if (ContractOrder.STATE_SUBMITTED.equals(order.getState())) {
 			Map<String, ContractOrder> map = (Map<String, ContractOrder>) redisHandler.get(ContractRedisKeys.CONTRACT_SUBMITTED_ORDER_PARTY_ID + order.getPartyId().toString());
 			if (null == map) map = new ConcurrentHashMap<String, ContractOrder>();
 			ContractOrder orderOld = map.get(order.getOrder_no());
-			
+
 			map.put(order.getOrder_no(), order);
 			redisHandler.setSync(ContractRedisKeys.CONTRACT_SUBMITTED_ORDER_PARTY_ID + order.getPartyId().toString(), map);
-			
+
 			// 获取单个订单的合约总资产、总保证金、总未实现盈利
 			Map<String, Double> contractAssetsOrder = this.assetService.getMoneyContractByOrder(order);
 			Map<String, Double> contractAssetsOrderOld = this.assetService.getMoneyContractByOrder(orderOld);
-			
+
 			Double contractAssets = (Double) this.redisHandler.get(ContractRedisKeys.CONTRACT_ASSETS_PARTY_ID + order.getPartyId().toString());
 			Double contractAssetsDeposit = (Double) this.redisHandler.get(ContractRedisKeys.CONTRACT_ASSETS_DEPOSIT_PARTY_ID + order.getPartyId().toString());
 			Double contractAssetsProfit = (Double) this.redisHandler.get(ContractRedisKeys.CONTRACT_ASSETS_PROFIT_PARTY_ID + order.getPartyId().toString());
-			
-			this.redisHandler.setSync(ContractRedisKeys.CONTRACT_ASSETS_PARTY_ID + order.getPartyId().toString(), 
+
+			this.redisHandler.setSync(ContractRedisKeys.CONTRACT_ASSETS_PARTY_ID + order.getPartyId().toString(),
 					Arith.add(null == contractAssets ? 0.000D : contractAssets, contractAssetsOrder.get("money_contract") - contractAssetsOrderOld.get("money_contract")));
-			this.redisHandler.setSync(ContractRedisKeys.CONTRACT_ASSETS_DEPOSIT_PARTY_ID + order.getPartyId().toString(), 
-					Arith.add(null == contractAssetsDeposit ? 0.000D : contractAssetsDeposit, contractAssetsOrder.get("money_contract_deposit") - contractAssetsOrderOld.get("money_contract_deposit")));		
-			this.redisHandler.setSync(ContractRedisKeys.CONTRACT_ASSETS_PROFIT_PARTY_ID + order.getPartyId().toString(),  
+			this.redisHandler.setSync(ContractRedisKeys.CONTRACT_ASSETS_DEPOSIT_PARTY_ID + order.getPartyId().toString(),
+					Arith.add(null == contractAssetsDeposit ? 0.000D : contractAssetsDeposit, contractAssetsOrder.get("money_contract_deposit") - contractAssetsOrderOld.get("money_contract_deposit")));
+			this.redisHandler.setSync(ContractRedisKeys.CONTRACT_ASSETS_PROFIT_PARTY_ID + order.getPartyId().toString(),
 					Arith.add(null == contractAssetsProfit ? 0.000D : contractAssetsProfit, contractAssetsOrder.get("money_contract_profit") - contractAssetsOrderOld.get("money_contract_profit")));
-			
+
 		} else if (ContractOrder.STATE_CREATED.equals(order.getState())) { // 平仓后,移除持仓列表
 			Map<String, ContractOrder> map = (Map<String, ContractOrder>) redisHandler.get(ContractRedisKeys.CONTRACT_SUBMITTED_ORDER_PARTY_ID + order.getPartyId().toString());
 			ContractOrder orderOld = null;
@@ -281,21 +289,21 @@
 				map.remove(order.getOrder_no());
 			}
 			redisHandler.setSync(ContractRedisKeys.CONTRACT_SUBMITTED_ORDER_PARTY_ID + order.getPartyId().toString(), map);
-			
+
 			// 获取单个订单的合约总资产、总保证金、总未实现盈利
 			Map<String, Double> contractAssetsOrderOld = this.assetService.getMoneyContractByOrder(orderOld);
-			
+
 			Double contractAssets = (Double) this.redisHandler.get(ContractRedisKeys.CONTRACT_ASSETS_PARTY_ID + order.getPartyId().toString());
 			Double contractAssetsDeposit = (Double) this.redisHandler.get(ContractRedisKeys.CONTRACT_ASSETS_DEPOSIT_PARTY_ID + order.getPartyId().toString());
 			Double contractAssetsProfit = (Double) this.redisHandler.get(ContractRedisKeys.CONTRACT_ASSETS_PROFIT_PARTY_ID + order.getPartyId().toString());
-			
-			this.redisHandler.setSync(ContractRedisKeys.CONTRACT_ASSETS_PARTY_ID + order.getPartyId().toString(), 
+
+			this.redisHandler.setSync(ContractRedisKeys.CONTRACT_ASSETS_PARTY_ID + order.getPartyId().toString(),
 					Arith.add(null == contractAssets ? 0.000D : contractAssets, 0.000D - contractAssetsOrderOld.get("money_contract")));
-			this.redisHandler.setSync(ContractRedisKeys.CONTRACT_ASSETS_DEPOSIT_PARTY_ID + order.getPartyId().toString(), 
-					Arith.add(null == contractAssetsDeposit ? 0.000D : contractAssetsDeposit, 0.000D - contractAssetsOrderOld.get("money_contract_deposit")));		
-			this.redisHandler.setSync(ContractRedisKeys.CONTRACT_ASSETS_PROFIT_PARTY_ID + order.getPartyId().toString(),  
+			this.redisHandler.setSync(ContractRedisKeys.CONTRACT_ASSETS_DEPOSIT_PARTY_ID + order.getPartyId().toString(),
+					Arith.add(null == contractAssetsDeposit ? 0.000D : contractAssetsDeposit, 0.000D - contractAssetsOrderOld.get("money_contract_deposit")));
+			this.redisHandler.setSync(ContractRedisKeys.CONTRACT_ASSETS_PROFIT_PARTY_ID + order.getPartyId().toString(),
 					Arith.add(null == contractAssetsProfit ? 0.000D : contractAssetsProfit, 0.000D - contractAssetsOrderOld.get("money_contract_profit")));
-			
+
 			// 平仓则纪录数据(委托平仓,订单直接平仓)
 			this.userDataService.saveClose(order);
 			Party party = this.partyService.cachePartyBy(order.getPartyId(), false);
@@ -307,7 +315,7 @@
 
 	/**
 	 * 收益结算,平仓时计算
-	 * 
+	 *
 	 * @param closevolume 平仓的张数
 	 */
 	public double settle(ContractOrder order, double volume) {
@@ -317,7 +325,11 @@
 		 */
 		double rate = Arith.div(volume, order.getVolume_open());
 
-		//profit = Arith.mul(Arith.add(order.getDeposit(), order.getProfit()), rate);
+		//平仓收益
+//		double mul = Arith.mul(order.getDeposit_open(), order.getLever_rate());//仓位
+//		double div = Arith.div(mul, order.getTrade_avg_price());//持有币的数量
+//		double amount = Arith.mul(div, Arith.sub(order.getClose_avg_price(), order.getTrade_avg_price()));
+
 		profit = Arith.add(Arith.mul(order.getDeposit_open(),rate),Arith.mul(order.getProfit(),rate));
 		order.setAmount_close(Arith.add(order.getAmount_close(), profit));
 		order.setVolume(Arith.sub(order.getVolume(), volume));
@@ -327,7 +339,7 @@
 			order.setState(ContractOrder.STATE_CREATED);
 			order.setClose_time(new Date());
 		}
-
+		update(order);
 		return profit;
 
 	}
@@ -335,7 +347,7 @@
 	public ContractOrder findByOrderNo(String order_no) {
 		ContractOrder order = (ContractOrder) this.redisHandler.get(ContractRedisKeys.CONTRACT_ORDERNO + order_no);
 		if (null != order) return order;
-		
+
 		List<ContractOrder> list = ApplicationUtil.executeSelect(ContractOrder.class,"WHERE ORDER_NO=?",new Object[] {order_no});
 		return list.size()<=0?null:list.get(0);
 	}
@@ -360,11 +372,11 @@
 			}
 			return list;
 		}
-		
+
 		Page page = new Page(1,Integer.MAX_VALUE,Integer.MAX_VALUE);
 		StringBuilder whereSql=new StringBuilder("WHERE 1=1 ");
 		ArrayList<Object> params=new ArrayList<Object>();
-		
+
 		if (!StringUtils.isNullOrEmpty(partyId)) {
 			whereSql.append("AND PARTY_ID=? ");
 			params.add(partyId);
@@ -374,7 +386,7 @@
 			whereSql.append("AND SYMBOL=? ");
 			params.add(symbol);
 		}
-		
+
 		if (!StringUtils.isNullOrEmpty(direction)) {
 			whereSql.append("AND DIRECTION=? ");
 			params.add(direction);
@@ -384,18 +396,18 @@
 		params.add("submitted");
 		params.add(page.getFirstElementNumber());
 		params.add(page.getPageSize());
-		
+
 		return ApplicationUtil.executeSelect(ContractOrder.class,whereSql.toString(),params.toArray(new Object[params.size()]));
 	}
 
 	public List<Map<String, Object>> getPaged(int pageNo, int pageSize, String partyId, String symbol, String type,String startTime, String endTime) {
 		if (pageNo <= 0) pageNo = 1;
 		Page page = new Page(pageNo, pageSize, Integer.MAX_VALUE);
-		
+
 		StringBuilder whereSql=new StringBuilder("WHERE PARTY_ID=? ");
 		ArrayList<Object> params=new ArrayList<Object>();
 		params.add(partyId);
-		
+
 		if (!StringUtils.isNullOrEmpty(symbol)) {
 			whereSql.append("AND SYMBOL=? ");
 			params.add(symbol);
@@ -408,19 +420,19 @@
 			whereSql.append("AND STATE=? ");
 			params.add("created");
 		}
-		
+
 		if (!StringUtils.isNullOrEmpty(startTime)) {
 			whereSql.append("AND DATE(CREATE_TIME)>=DATE('"+startTime+" 00:00:00') ");
 		}
-		
+
 		if (!StringUtils.isNullOrEmpty(endTime)) {
 			whereSql.append("AND DATE(CREATE_TIME)<=DATE('"+endTime+" 23:59:59') ");
 		}
-		
+
 		whereSql.append("ORDER BY CREATE_TIME DESC LIMIT ?,?");
 		params.add(page.getFirstElementNumber());
 		params.add(pageSize);
-		
+
 		return bulidData(ApplicationUtil.executeSelect(ContractOrder.class,whereSql.toString(),params.toArray(new Object[params.size()])));
 	}
 
@@ -431,14 +443,14 @@
 			for (Entry<String, ContractOrder> entry : ordersMap.entrySet()) {
 				if (StringUtils.isNotEmpty(symbol)) {
 					if (symbol.equals(entry.getValue().getSymbol())) {
-						ordersList.add(entry.getValue());								
+						ordersList.add(entry.getValue());
 					}
 				} else {
 					ordersList.add(entry.getValue());
 				}
 			}
-			
-			// 创建时间 降序排列			
+
+			// 创建时间 降序排列
 			if (ordersList.size() > 0) {
 				Collections.sort(ordersList, new Comparator<ContractOrder>() {
 					@Override
@@ -465,14 +477,16 @@
 	}
 
 	public Map<String, Object> bulidOne(ContractOrder order) {
+		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd  hh:mm:ss  a", Locale.ENGLISH);
+		simpleDateFormat.setTimeZone(TimeZone.getTimeZone("America/New_York"));
 		DecimalFormat df = new DecimalFormat("#.##");
 		Map<String, Object> map = new HashMap<String, Object>();
 		map.put("order_no", order.getOrder_no());
 		map.put("name", itemService.cacheBySymbol(order.getSymbol(), false).getName());
 		map.put("symbol", order.getSymbol());
-		map.put("create_time", DateUtils.format(order.getCreate_time(), DateUtils.DF_yyyyMMddHHmmss));
+		map.put("create_time", simpleDateFormat.format(order.getCreate_time()));
 		if (order.getClose_time() != null) {
-			map.put("close_time", DateUtils.format(order.getClose_time(), DateUtils.DF_yyyyMMddHHmmss));
+			map.put("close_time", simpleDateFormat.format(order.getClose_time()));
 		} else {
 			map.put("close_time", "");
 		}
@@ -502,7 +516,57 @@
 							Arith.add(Arith.add(order.getAmount_close(),Arith.mul(order.getProfit(),rate)), order.getDeposit()),
 							order.getDeposit_open())));
 		} else {
-			map.put("profit", df.format(Arith.sub(Arith.add(order.getAmount_close(), order.getDeposit()), order.getDeposit_open())));
+			map.put("profit", df.format(order.getProfit()));
+		}
+
+		double depositOpen =  Double.parseDouble(map.get("deposit_open").toString());
+		double leverRate = Double.parseDouble(map.get("lever_rate").toString());
+		double tradeAvgPrice =  Double.parseDouble(map.get("trade_avg_price").toString());
+
+		double mul = Arith.mul(depositOpen, leverRate);//仓位
+		double div = Arith.div(mul, tradeAvgPrice);//持有币的数量
+		/**
+		 * 计算全仓收益 保证金
+		 */
+		double profit = 0;
+		List<ContractOrder> list = findSubmitted(order.getPartyId().toString(), null, null);
+		for (int f = 0; f < list.size(); f++) {
+			ContractOrder close_line = list.get(f);
+			if(close_line.getProfit() > 0){
+				profit = Arith.add(profit, Arith.add(close_line.getProfit(), close_line.getDeposit()));
+			}else{
+				profit = Arith.add(profit, close_line.getDeposit());
+			}
+		}
+		Wallet wallet = this.walletService.saveWalletByPartyId(order.getPartyId().toString());
+		double totleMoney = wallet.getMoney();
+		profit = Arith.add(profit,totleMoney);
+
+		if (map.get("direction") != null) {
+			String direction = map.get("direction").toString();
+			double liquidationPrice;
+			double deposit =  profit;
+
+			if(leverRate == 1){
+				liquidationPrice = 0;
+			}else {
+				if ("sell".equals(direction)) {
+					double lossPercentage = deposit / (Arith.mul(order.getVolume(), order.getUnit_amount()));
+					double priceIncrease = lossPercentage * tradeAvgPrice;
+					liquidationPrice = tradeAvgPrice + priceIncrease;
+				} else {
+					double lossPercentage = deposit / (Arith.mul(order.getVolume(), order.getUnit_amount()));
+					double priceDrop = lossPercentage * tradeAvgPrice;
+					liquidationPrice = tradeAvgPrice - priceDrop;
+				}
+			}
+			DecimalFormat dfs = new DecimalFormat("#.#####");
+			String formattedPrice = dfs.format(liquidationPrice);
+			// 检查是否为负数
+			if (Double.parseDouble(formattedPrice) < 0) {
+				formattedPrice = "0";
+			}
+			map.put("qiangPing", formattedPrice);
 		}
 
 		map.put("volume", order.getVolume());
@@ -557,11 +621,11 @@
 	public void setPartyService(PartyService partyService) {
 		this.partyService = partyService;
 	}
-	
+
 	public void setTipService(TipService tipService) {
 		this.tipService = tipService;
 	}
-	
+
 	public void setAssetService(AssetService assetService) {
 		this.assetService = assetService;
 	}

--
Gitblit v1.9.3