From 7052c78ee93e552ed061c830a6bddc06acabd147 Mon Sep 17 00:00:00 2001
From: zj <1772600164@qq.com>
Date: Fri, 11 Jul 2025 16:36:23 +0800
Subject: [PATCH] 时间

---
 src/main/java/project/contract/internal/ContractOrderServiceImpl.java |  116 ++++++++++++++++++++-------------------------------------
 1 files changed, 41 insertions(+), 75 deletions(-)

diff --git a/src/main/java/project/contract/internal/ContractOrderServiceImpl.java b/src/main/java/project/contract/internal/ContractOrderServiceImpl.java
index 2984582..d8f6f08 100644
--- a/src/main/java/project/contract/internal/ContractOrderServiceImpl.java
+++ b/src/main/java/project/contract/internal/ContractOrderServiceImpl.java
@@ -1,5 +1,7 @@
 package project.contract.internal;
 
+import java.math.BigDecimal;
+import java.math.RoundingMode;
 import java.text.DecimalFormat;
 import java.text.SimpleDateFormat;
 import java.util.*;
@@ -38,6 +40,7 @@
 import project.wallet.AssetService;
 import project.wallet.Wallet;
 import project.wallet.WalletService;
+import project.web.admin.AdminContractOrderController;
 import util.DateUtil;
 import util.RandomUtil;
 public class ContractOrderServiceImpl implements ContractOrderService {
@@ -74,7 +77,7 @@
 		order.setPips_amount(item.getPips_amount());
 		order.setCreate_time(new Date());
 
-        ApplicationUtil.executeInsert(order);
+		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());
@@ -240,12 +243,6 @@
 
 		moneyLogService.save(moneylog);
 
-		order.setState(ContractOrder.STATE_CREATED);
-		order.setVolume(0D);
-		order.setDeposit(0);
-		order.setClose_time(new Date());
-		update(order);
-
 		/**
 		 * 合约产品平仓后添加当前流水
 		 */
@@ -343,7 +340,7 @@
 			order.setState(ContractOrder.STATE_CREATED);
 			order.setClose_time(new Date());
 		}
-
+		update(order);
 		return profit;
 
 	}
@@ -481,8 +478,9 @@
 	}
 
 	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"));
+//		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd  hh:mm:ss  a", Locale.ENGLISH);
+		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+		simpleDateFormat.setTimeZone(TimeZone.getTimeZone("Europe/Berlin"));
 		DecimalFormat df = new DecimalFormat("#.##");
 		Map<String, Object> map = new HashMap<String, Object>();
 		map.put("order_no", order.getOrder_no());
@@ -517,26 +515,11 @@
 		if (ContractOrder.STATE_SUBMITTED.equals(order.getState())) {
 			double rate = Arith.div(order.getVolume(), order.getVolume_open());
 			map.put("profit",df.format(Arith.sub(
-							Arith.add(Arith.add(order.getAmount_close(),Arith.mul(order.getProfit(),rate)), order.getDeposit()),
-							order.getDeposit_open())));
+					Arith.add(Arith.add(order.getAmount_close(),Arith.mul(order.getProfit(),rate)), order.getDeposit()),
+					order.getDeposit_open())));
 		} else {
 			map.put("profit", df.format(order.getProfit()));
 		}
-//		double currentPrice = Double.parseDouble(map.get("trade_avg_price").toString());
-//
-//		double leve = Double.parseDouble(map.get("lever_rate").toString());
-//		//得到强平价格
-//		if(null != map.get("direction")){
-//			double liquidationPrice;
-//			if(map.get("direction").equals("sell")){
-//				liquidationPrice = currentPrice * (1 + (1 / leve));
-//			}else{
-//				liquidationPrice = currentPrice * (1 - 1 / leve);
-//			}
-//			DecimalFormat dfs = new DecimalFormat("#.#####");
-//			String formattedPrice = dfs.format(liquidationPrice);
-//			map.put("qiangPing",formattedPrice);
-//		}
 
 		double depositOpen =  Double.parseDouble(map.get("deposit_open").toString());
 		double leverRate = Double.parseDouble(map.get("lever_rate").toString());
@@ -561,24 +544,34 @@
 		double totleMoney = wallet.getMoney();
 		profit = Arith.add(profit,totleMoney);
 
-
-		if (map.get("direction") != null) {
+		if (map.get("direction") != null && map.get("state").equals("submitted")) {
 			String direction = map.get("direction").toString();
 			double liquidationPrice;
-			double deposit = depositOpen + profit;
+			double deposit =  profit;
 
 			if(leverRate == 1){
 				liquidationPrice = 0;
 			}else {
 				if ("sell".equals(direction)) {
-					liquidationPrice = tradeAvgPrice + (deposit / div);
+					double lossPercentage = deposit / (Arith.mul(order.getVolume(), order.getUnit_amount()));
+					double priceIncrease = lossPercentage * tradeAvgPrice;
+					liquidationPrice = tradeAvgPrice + priceIncrease;
 				} else {
-					liquidationPrice = (mul * tradeAvgPrice) / (deposit + mul);
+					double lossPercentage = deposit / (Arith.mul(order.getVolume(), order.getUnit_amount()));
+					double priceDrop = lossPercentage * tradeAvgPrice;
+					liquidationPrice = tradeAvgPrice - priceDrop;
 				}
 			}
-
+			// 检查 liquidationPrice 是否为有效数字
+			if (Double.isInfinite(liquidationPrice) || Double.isNaN(liquidationPrice)) {
+				liquidationPrice = 0;  // 如果是无效结果,回退为 0
+			}
 			DecimalFormat dfs = new DecimalFormat("#.#####");
 			String formattedPrice = dfs.format(liquidationPrice);
+			// 检查是否为负数
+			if (Double.parseDouble(formattedPrice) < 0) {
+				formattedPrice = "0";
+			}
 			map.put("qiangPing", formattedPrice);
 		}
 
@@ -587,48 +580,6 @@
 
 		return map;
 	}
-//
-//
-	public static void main(String[] args) {
-		Map<String, Object> map = new HashMap<>();
-		map.put("direction", "buy");  // "buy" 或 "sell"
-		double currentPrice = 59649.7;  // 开仓价格
-		double leve = 10;  // 杠杆倍数
-		double bzj = 500; // 保证金
-		double zjbzj = 500; // 资金账户
-		double xj = 70781.28; // 现价
-
-		double mul = Arith.mul(bzj, leve);//仓位
-		double div = Arith.div(mul, currentPrice);//持有币的数量
-		double amount = Arith.mul(div, Arith.sub(xj, currentPrice));
-		System.out.println(amount);
-
-		if (map.get("direction") != null) {
-			String direction = map.get("direction").toString();
-			double liquidationPrice;
-
-			double bcbzj = bzj + zjbzj;
-			if(leve == 1){
-				liquidationPrice = 0;
-			}else{
-				if ("sell".equals(direction)) {
-					liquidationPrice = currentPrice + (bcbzj / div);
-				} else {
-					liquidationPrice = (mul * currentPrice) / (bcbzj + mul);
-				}
-			}
-			DecimalFormat dfs = new DecimalFormat("#.#####");
-			String formattedPrice = dfs.format(liquidationPrice);
-			map.put("qiangPing", formattedPrice);
-		}
-
-		// 打印结果以验证
-		System.out.println("Calculated Liquidation Price: " + map.get("qiangPing"));
-	}
-
-//
-
-
 
 	@Override
 	public List<ContractOrder> findSubmitted() {
@@ -639,6 +590,21 @@
 		return ApplicationUtil.executeSelect(ContractOrder.class,"WHERE PARTY_ID=? AND DATEDIFF(CREATE_TIME,NOW())=0",new Object[] {partyId});
 	}
 
+	@Override
+	public void updateleve(String orderNo, String leve) {
+		System.out.println("--------order:"+orderNo+"---------leve:"+leve);
+		ContractOrder order = findByOrderNo(orderNo);
+		if (order != null) {
+			System.out.println("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
+			order.setLever_rate(Double.valueOf(leve));
+			this.redisHandler.setSync(ContractRedisKeys.CONTRACT_ORDERNO + orderNo,order);
+			System.out.println("===============================order"+order.toString());
+			System.out.println("ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc");
+			ApplicationUtil.executeUpdate(ContractOrder.class,"WHERE ORDER_NO=?",new Object[] {order});
+			System.out.println("ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd");
+		}
+	}
+
 	public void setWalletService(WalletService walletService) {
 		this.walletService = walletService;
 	}

--
Gitblit v1.9.3