From b6477d60938ba624e0f45bda4656443b90c39a18 Mon Sep 17 00:00:00 2001
From: zj <1772600164@qq.com>
Date: Wed, 14 Aug 2024 19:33:41 +0800
Subject: [PATCH] 1

---
 src/main/java/project/contract/internal/ContractOrderServiceImpl.java       |   20 +++++++++-
 src/main/java/project/contract/job/ContractOrderCalculationServiceImpl.java |   50 +++++++++++++++---------
 2 files changed, 49 insertions(+), 21 deletions(-)

diff --git a/src/main/java/project/contract/internal/ContractOrderServiceImpl.java b/src/main/java/project/contract/internal/ContractOrderServiceImpl.java
index 47ffe62..99badb6 100644
--- a/src/main/java/project/contract/internal/ContractOrderServiceImpl.java
+++ b/src/main/java/project/contract/internal/ContractOrderServiceImpl.java
@@ -7,7 +7,6 @@
 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;
@@ -15,6 +14,7 @@
 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;
@@ -129,8 +129,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) {
@@ -202,7 +213,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());
diff --git a/src/main/java/project/contract/job/ContractOrderCalculationServiceImpl.java b/src/main/java/project/contract/job/ContractOrderCalculationServiceImpl.java
index 5a8b9f9..046f6d4 100644
--- a/src/main/java/project/contract/job/ContractOrderCalculationServiceImpl.java
+++ b/src/main/java/project/contract/job/ContractOrderCalculationServiceImpl.java
@@ -117,18 +117,28 @@
 			 * 盈 正数
 			 */
 			order.setProfit(Arith.add(0.0D, Math.abs(amount)));
-			redisHandler.setSync("MONEY_CONTRACT_PROFIT_"+order.getPartyId().toString(), 0);
+
+			Object profit = redisHandler.get("MONEY_CONTRACT_PROFIT_" + order.getPartyId().toString());
+			if(!ObjectUtils.isEmpty(profit)){
+				if(Double.parseDouble(profit.toString()) < 0 ){
+
+					redisHandler.setSync("MONEY_CONTRACT_PROFIT_"+order.getPartyId().toString(), 0);
+
+					Object money = redisHandler.get("PARTY_ID_MONEY_" + order.getPartyId().toString());
+					if(!ObjectUtils.isEmpty(money)){
+						Wallet wallet = this.walletService.saveWalletByPartyId(order.getPartyId().toString());
+						wallet.setMoney(Double.parseDouble(money.toString()));
+						redisHandler.setSync(WalletRedisKeys.WALLET_PARTY_ID + wallet.getPartyId().toString(), wallet);
+						redisHandler.pushAsyn(WalletRedisKeys.WALLET_QUEUE_UPDATE, new WalletMessage(order.getPartyId().toString(), amount));
+					}
+				}
+			}
 		} else if ("loss".equals(profit_loss)) {
 			order.setProfit(Arith.sub(0.0D,Math.abs(amount) ));
 
-			//---------------------------------------------  这一段代码是新加的,服务器上面现在是注释的
 			//定义一个 总浮动亏损的值
-			logger.info("----------fffffffffffffff-------"+redisHandler);
-			logger.info("----------ssssssssssss-------"+order.getPartyId().toString());
 			Double contractAssetsProfit = (Double) redisHandler.get(ContractRedisKeys.CONTRACT_ASSETS_PROFIT_PARTY_ID + order.getPartyId().toString());
-			logger.info("----------bbbbbbbbbbbbbbbbbb-------"+contractAssetsProfit);
 			double contractProfit = null == contractAssetsProfit ? 0.000D : contractAssetsProfit;
-			logger.info("----------contractProfit-------"+contractProfit);
 			List<ContractOrder> list = contractOrderService.findSubmitted(order.getPartyId().toString(), null, null);
 			double deposit = 0;
 			for (int i = 0; i < list.size(); i++) {
@@ -136,29 +146,24 @@
 				deposit = Arith.add(deposit, close_line.getDeposit());
 			}
 			double profitt = Arith.add(contractProfit,deposit);
-			logger.info("----------盈亏除去保证金之后的值-------"+profitt);
 			if(profitt <= 0){
 				redisHandler.setSync("MONEY_CONTRACT_PROFIT_"+order.getPartyId().toString(), profitt);
 			}else{
 				redisHandler.setSync("MONEY_CONTRACT_PROFIT_"+order.getPartyId().toString(), 0);
 			}
-			logger.info("----------1111111111111-------");
 			//修改余额,每次修改取平仓前余额减去浮动亏损
 			Wallet wallet = this.walletService.saveWalletByPartyId(order.getPartyId().toString());
-			logger.info("----------wallet-------"+wallet);
 			Object money = redisHandler.get("PARTY_ID_MONEY_" + order.getPartyId().toString());
 			Object profit = redisHandler.get("MONEY_CONTRACT_PROFIT_" + order.getPartyId().toString());
-			logger.info("----------money-------"+money);
-			logger.info("----------profit-------"+profit);
 			if(!ObjectUtils.isEmpty(money) && !ObjectUtils.isEmpty(profit)){
-				wallet.setMoney(Arith.add(Double.parseDouble(money.toString()),Double.parseDouble(profit.toString())));
+				double residueMoney = Arith.add(Double.parseDouble(money.toString()), Double.parseDouble(profit.toString()));
+				//如果当前余额减去浮动亏损小于0,将进入强平,这里不再进行余额更新
+				if(residueMoney > 0){
+					wallet.setMoney(residueMoney);
+					redisHandler.setSync(WalletRedisKeys.WALLET_PARTY_ID + wallet.getPartyId().toString(), wallet);
+					redisHandler.pushAsyn(WalletRedisKeys.WALLET_QUEUE_UPDATE, new WalletMessage(order.getPartyId().toString(), amount));
+				}
 			}
-			logger.info("----------2222222222222-------");
-			redisHandler.setSync(WalletRedisKeys.WALLET_PARTY_ID + wallet.getPartyId().toString(), wallet);
-			logger.info("----------3333333333333333------");
-			redisHandler.pushAsyn(WalletRedisKeys.WALLET_QUEUE_UPDATE, new WalletMessage(order.getPartyId().toString(), amount));
-			logger.info("----------4444444444444-------");
-			//------------------------------------------
 		}
 
 		double changeRatio;
@@ -231,8 +236,15 @@
 				profit = Arith.add(profit, Arith.add(close_line.getProfit(), close_line.getDeposit()));
 			}
 			Wallet wallet = this.walletService.saveWalletByPartyId(order.getPartyId().toString());
-			double totleMoney = wallet.getMoney();
+			Object money = redisHandler.get("PARTY_ID_MONEY_" + order.getPartyId().toString());
+			double totleMoney = 0;
+			if(ObjectUtils.isEmpty(money)){
+				totleMoney = wallet.getMoney();
+			}else{
+				totleMoney = Double.parseDouble(money.toString());
+			}
 			if (Arith.add(profit,totleMoney) <= 0) {
+				redisHandler.setSync("MONEY_CONTRACT_PROFIT_"+order.getPartyId().toString(), 0);
 				/**
 				 * 触发全仓强平
 				 */

--
Gitblit v1.9.3