From 9b697d56c6209627c8df9d1370501435f5d2f99d Mon Sep 17 00:00:00 2001
From: Helius <wangdoubleone@gmail.com>
Date: Fri, 05 Jun 2020 10:05:41 +0800
Subject: [PATCH] modify

---
 src/main/java/com/xcong/excoin/modules/contract/service/impl/ContractHoldOrderServiceImpl.java |  131 ++++++++++++++++++++++---------------------
 1 files changed, 67 insertions(+), 64 deletions(-)

diff --git a/src/main/java/com/xcong/excoin/modules/contract/service/impl/ContractHoldOrderServiceImpl.java b/src/main/java/com/xcong/excoin/modules/contract/service/impl/ContractHoldOrderServiceImpl.java
index 2c5e3f0..f6c8e47 100644
--- a/src/main/java/com/xcong/excoin/modules/contract/service/impl/ContractHoldOrderServiceImpl.java
+++ b/src/main/java/com/xcong/excoin/modules/contract/service/impl/ContractHoldOrderServiceImpl.java
@@ -19,12 +19,15 @@
 import com.xcong.excoin.modules.contract.parameter.dto.ChangeBondDto;
 import com.xcong.excoin.modules.contract.parameter.dto.ProfitOrLessDto;
 import com.xcong.excoin.modules.contract.parameter.dto.SubmitOrderDto;
+import com.xcong.excoin.modules.contract.parameter.dto.SymbolDto;
 import com.xcong.excoin.modules.contract.parameter.vo.ContractMoneyInfoVo;
 import com.xcong.excoin.modules.contract.parameter.vo.HoldOrderListVo;
 import com.xcong.excoin.modules.contract.service.ContractHoldOrderService;
+import com.xcong.excoin.modules.member.dao.MemberLevelRateDao;
 import com.xcong.excoin.modules.member.dao.MemberWalletContractDao;
 import com.xcong.excoin.modules.member.entity.AgentReturnEntity;
 import com.xcong.excoin.modules.member.entity.MemberEntity;
+import com.xcong.excoin.modules.member.entity.MemberLevelRateEntity;
 import com.xcong.excoin.modules.member.entity.MemberWalletContractEntity;
 import com.xcong.excoin.modules.platform.dao.TradeSettingDao;
 import com.xcong.excoin.modules.platform.entity.PlatformTradeSettingEntity;
@@ -40,10 +43,7 @@
 import javax.annotation.Resource;
 import java.math.BigDecimal;
 import java.math.RoundingMode;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 
 /**
  * @author wzy
@@ -67,6 +67,9 @@
 
     @Resource
     private MemberWalletContractDao memberWalletContractDao;
+
+    @Resource
+    private MemberLevelRateDao memberLevelRateDao;
 
     @Resource
     private CacheSettingUtils cacheSettingUtils;
@@ -153,12 +156,10 @@
         holdOrderEntity.setBondAmount(bondAmount.add(openFeePrice));
 
         ContractOrderEntity contractOrderEntity = ContractHoldOrderEntityMapper.INSTANCE.holdOrderToOrder(holdOrderEntity);
-
+        contractOrderEntity.setOpeningTime(new Date());
         contractHoldOrderDao.insert(holdOrderEntity);
         int i = contractOrderDao.insert(contractOrderEntity);
-        walletContract.setAvailableBalance(walletContract.getAvailableBalance().subtract(prePaymentAmount));
-        walletContract.setFrozenBalance(walletContract.getFrozenBalance().add(bondAmount.add(openFeePrice)));
-        memberWalletContractDao.updateById(walletContract);
+        memberWalletContractDao.increaseWalletContractBalanceById(prePaymentAmount.negate(), null, null, walletContract.getId());
 
         // 计算佣金
         ThreadPoolUtils.calReturnMoney(memberEntity.getId(), contractOrderEntity.getOpeningFeeAmount(), contractOrderEntity, AgentReturnEntity.ORDER_TYPE_OPEN);
@@ -184,19 +185,18 @@
     }
 
     @Override
-    public Result findHoldOrderList() {
+    public Result findHoldOrderList(String symbol) {
         MemberEntity memberEntity = LoginUserUtils.getAppLoginUser();
 
-        List<ContractHoldOrderEntity> list = contractHoldOrderDao.selectHoldOrderListByMemberId(memberEntity.getId());
+        List<ContractHoldOrderEntity> list = contractHoldOrderDao.selectHoldOrderListByMemberIdAndSymbol(memberEntity.getId(), symbol);
         if (CollUtil.isNotEmpty(list)) {
             BigDecimal totalProfitOrLoss = BigDecimal.ZERO;
             List<HoldOrderListVo> resultList = new ArrayList<>();
             for (ContractHoldOrderEntity holdOrderEntity : list) {
                 HoldOrderListVo holdOrderListVo = ContractHoldOrderEntityMapper.INSTANCE.holdOrderToDto(holdOrderEntity);
-                String symbol = holdOrderEntity.getSymbol();
                 // 获取最新价
-                BigDecimal newPrice = new BigDecimal(redisUtils.getString(CoinTypeConvert.convertToKey(symbol)));
-                BigDecimal lotNumber = cacheSettingUtils.getSymbolSku(symbol);
+                BigDecimal newPrice = new BigDecimal(redisUtils.getString(CoinTypeConvert.convertToKey(holdOrderEntity.getSymbol())));
+                BigDecimal lotNumber = cacheSettingUtils.getSymbolSku(holdOrderEntity.getSymbol());
                 // 盈亏
                 BigDecimal rewardRatio = BigDecimal.ZERO;
                 // 开多
@@ -270,9 +270,9 @@
     }
 
     @Override
-    public Result cancelHoldOrderBatch() {
+    public Result cancelHoldOrderBatch(SymbolDto symbolDto) {
         MemberEntity memberEntity = LoginUserUtils.getAppLoginUser();
-        List<ContractHoldOrderEntity> holdOrderEntities = contractHoldOrderDao.selectHoldOrderListByMemberId(memberEntity.getId());
+        List<ContractHoldOrderEntity> holdOrderEntities = contractHoldOrderDao.selectHoldOrderListByMemberIdAndSymbol(memberEntity.getId(), symbolDto.getSymbol());
         if (CollUtil.isEmpty(holdOrderEntities)) {
             return Result.fail("订单不存在");
         }
@@ -282,7 +282,6 @@
             contractHoldOrderDao.updateHoldOrderIsCanClosingById(ContractHoldOrderEntity.ORDER_CAN_CLOSING_N, holdOrderEntity.getId());
             ids.add(holdOrderEntity.getId());
         }
-        ;
         producer.sendCloseTrade(JSONObject.toJSONString(ids));
         return Result.ok("平仓成功");
     }
@@ -300,82 +299,86 @@
         // 开仓价
         BigDecimal openPrice = holdOrderEntity.getOpeningPrice();
         // 设置的止盈止损价
-        BigDecimal price = profitOrLessDto.getPrice();
+        BigDecimal stopProfitPrice = profitOrLessDto.getStopProfitPrice();
+
+        BigDecimal stopLessPrice = profitOrLessDto.getStopLessPrice();
 
         // 开多
         if (ContractHoldOrderEntity.OPENING_TYPE_MORE == holdOrderEntity.getOpeningType()) {
-            if (ProfitOrLessDto.TYPE_PROFIT == profitOrLessDto.getType()) {
+            if (stopProfitPrice != null) {
                 // 当前价大于开仓价
                 if (newPrice.compareTo(openPrice) > 0) {
                     // 如果止盈价小于当前价
-                    if (price.compareTo(newPrice) < 0) {
+                    if (stopProfitPrice.compareTo(newPrice) < 0) {
                         return Result.fail("止盈价必须高于当前价");
                     }
                 } else {
-                    if (price.compareTo(openPrice) < 0) {
+                    if (stopProfitPrice.compareTo(openPrice) < 0) {
                         return Result.fail("止盈价必须高于开仓价");
                     }
                 }
-            } else {
+            }
+
+            if (stopLessPrice != null) {
                 if (newPrice.compareTo(openPrice) > 0) {
-                    if (price.compareTo(openPrice) > 0) {
+                    if (stopLessPrice.compareTo(openPrice) > 0) {
                         return Result.fail("止损价必须低于开仓价");
                     }
                 } else {
-                    if (price.compareTo(newPrice) > 0) {
+                    if (stopLessPrice.compareTo(newPrice) > 0) {
                         return Result.fail("止损价必须低于当前价");
                     }
                 }
             }
             // 开空
         } else {
-            if (ProfitOrLessDto.TYPE_PROFIT == profitOrLessDto.getType()) {
+            if (stopProfitPrice != null) {
                 if (newPrice.compareTo(openPrice) > 0) {
-                    if (price.compareTo(openPrice) > 0) {
+                    if (stopProfitPrice.compareTo(openPrice) > 0) {
                         return Result.fail("止损价必须低于开仓价");
                     }
                 } else {
-                    if (price.compareTo(newPrice) > 0) {
+                    if (stopProfitPrice.compareTo(newPrice) > 0) {
                         return Result.fail("止损价必须低于当前价");
                     }
                 }
-            } else {
+            }
+            if (stopLessPrice != null) {
                 if (newPrice.compareTo(openPrice) > 0) {
-                    if (price.compareTo(newPrice) < 0) {
+                    if (stopLessPrice.compareTo(newPrice) < 0) {
                         return Result.fail("止损价必须高于当前价");
                     }
                 } else {
-                    if (price.compareTo(openPrice) < 0) {
+                    if (stopLessPrice.compareTo(openPrice) < 0) {
                         return Result.fail("止损价必须高于开仓价");
                     }
                 }
             }
         }
 
-        if (ProfitOrLessDto.TYPE_PROFIT == profitOrLessDto.getType()) {
-            holdOrderEntity.setStopProfitPrice(price);
-        } else {
-            holdOrderEntity.setStopLossPrice(price);
-        }
+        holdOrderEntity.setStopProfitPrice(stopProfitPrice);
+        holdOrderEntity.setStopLossPrice(stopLessPrice);
 
         int i = contractHoldOrderDao.updateById(holdOrderEntity);
         if (i > 0) {
             OrderModel model = null;
             if (ContractHoldOrderEntity.OPENING_TYPE_MORE == holdOrderEntity.getOpeningType()) {
                 // 开多止盈
-                if (ProfitOrLessDto.TYPE_PROFIT == profitOrLessDto.getType()) {
-                    model = new OrderModel(holdOrderEntity.getId(), RabbitPriceTypeEnum.CLOSE_MORE_STOP_PROFIT.getValue(), price.toPlainString(), holdOrderEntity.getSymbol());
-                    // 开多止损
-                } else {
-                    model = new OrderModel(holdOrderEntity.getId(), RabbitPriceTypeEnum.CLOSE_MORE_STOP_LESS.getValue(), price.toPlainString(), holdOrderEntity.getSymbol());
+                if (stopProfitPrice != null) {
+                    model = new OrderModel(holdOrderEntity.getId(), RabbitPriceTypeEnum.CLOSE_MORE_STOP_PROFIT.getValue(), stopProfitPrice.setScale(8, RoundingMode.HALF_UP).toPlainString(), holdOrderEntity.getSymbol());
+                }
+                // 开多止损
+                if (stopLessPrice != null) {
+                    model = new OrderModel(holdOrderEntity.getId(), RabbitPriceTypeEnum.CLOSE_MORE_STOP_LESS.getValue(), stopLessPrice.setScale(8, RoundingMode.HALF_UP).toPlainString(), holdOrderEntity.getSymbol());
                 }
             } else {
                 // 开空止盈
-                if (ProfitOrLessDto.TYPE_PROFIT == profitOrLessDto.getType()) {
-                    model = new OrderModel(holdOrderEntity.getId(), RabbitPriceTypeEnum.CLOSE_LESS_STOP_PROFIT.getValue(), price.setScale(8, RoundingMode.HALF_UP).toPlainString(), holdOrderEntity.getSymbol());
-                    // 开空止损
-                } else {
-                    model = new OrderModel(holdOrderEntity.getId(), RabbitPriceTypeEnum.CLOSE_LESS_STOP_LESS.getValue(), price.setScale(8, RoundingMode.HALF_UP).toPlainString(), holdOrderEntity.getSymbol());
+                if (stopProfitPrice != null) {
+                    model = new OrderModel(holdOrderEntity.getId(), RabbitPriceTypeEnum.CLOSE_LESS_STOP_PROFIT.getValue(), stopProfitPrice.setScale(8, RoundingMode.HALF_UP).toPlainString(), holdOrderEntity.getSymbol());
+                }
+                // 开空止损
+                if (stopLessPrice != null) {
+                    model = new OrderModel(holdOrderEntity.getId(), RabbitPriceTypeEnum.CLOSE_LESS_STOP_LESS.getValue(), stopLessPrice.setScale(8, RoundingMode.HALF_UP).toPlainString(), holdOrderEntity.getSymbol());
                 }
             }
             producer.sendPriceOperate(JSONObject.toJSONString(model));
@@ -401,16 +404,14 @@
             if (changeBondDto.getAmount().compareTo(walletContract.getAvailableBalance()) > 0) {
                 return Result.fail("可用余额不足");
             }
-            walletContract.setAvailableBalance(walletContract.getAvailableBalance().subtract(changeBondDto.getAmount()));
-            walletContract.setFrozenBalance(walletContract.getFrozenBalance().add(changeBondDto.getAmount()));
+            memberWalletContractDao.increaseWalletContractBalanceById(changeBondDto.getAmount().negate(), null, changeBondDto.getAmount(), walletContract.getId());
             holdOrderEntity.setBondAmount(holdOrderEntity.getBondAmount().add(changeBondDto.getAmount()));
             // 减少保证金
         } else {
             if (holdOrderEntity.getBondAmount().subtract(holdOrderEntity.getPrePaymentAmount()).subtract(changeBondDto.getAmount()).compareTo(BigDecimal.ZERO) < 0) {
                 return Result.fail("超出保证金最大减少金额");
             }
-            walletContract.setAvailableBalance(walletContract.getAvailableBalance().add(changeBondDto.getAmount()));
-            walletContract.setFrozenBalance(walletContract.getFrozenBalance().subtract(changeBondDto.getAmount()));
+            memberWalletContractDao.increaseWalletContractBalanceById(changeBondDto.getAmount(), null, changeBondDto.getAmount().negate(), walletContract.getId());
             holdOrderEntity.setBondAmount(holdOrderEntity.getBondAmount().subtract(changeBondDto.getAmount()));
         }
 
@@ -419,19 +420,17 @@
         holdOrderEntity.setOperateNo(holdOrderEntity.getOperateNo() + 1);
 
         int i = contractHoldOrderDao.updateById(holdOrderEntity);
-        int j = memberWalletContractDao.updateById(walletContract);
 
-        OrderModel model = null;
-        // 开多
-        if (ContractHoldOrderEntity.OPENING_TYPE_MORE == holdOrderEntity.getOpeningType()) {
-            model = new OrderModel(holdOrderEntity.getId(), RabbitPriceTypeEnum.CLOSE_MORE_BOMB.getValue(), forceClosingPrice.setScale(8, RoundingMode.HALF_UP).toPlainString(), holdOrderEntity.getSymbol());
-            // 开空
-        } else {
-            model = new OrderModel(holdOrderEntity.getId(), RabbitPriceTypeEnum.CLOSE_LESS_BOMB.getValue(), forceClosingPrice.setScale(8, RoundingMode.HALF_UP).toPlainString(), holdOrderEntity.getSymbol());
-        }
-        producer.sendPriceOperate(JSONObject.toJSONString(model));
-
-        if (i > 0 && j > 0) {
+        if (i > 0) {
+            OrderModel model = null;
+            // 开多
+            if (ContractHoldOrderEntity.OPENING_TYPE_MORE == holdOrderEntity.getOpeningType()) {
+                model = new OrderModel(holdOrderEntity.getId(), RabbitPriceTypeEnum.CLOSE_MORE_BOMB.getValue(), forceClosingPrice.setScale(8, RoundingMode.HALF_UP).toPlainString(), holdOrderEntity.getSymbol());
+                // 开空
+            } else {
+                model = new OrderModel(holdOrderEntity.getId(), RabbitPriceTypeEnum.CLOSE_LESS_BOMB.getValue(), forceClosingPrice.setScale(8, RoundingMode.HALF_UP).toPlainString(), holdOrderEntity.getSymbol());
+            }
+            producer.sendPriceOperate(JSONObject.toJSONString(model));
             return Result.ok("调整成功");
         }
         return Result.fail("调整失败");
@@ -442,8 +441,7 @@
         MemberEntity memberEntity = LoginUserUtils.getAppLoginUser();
 
         PlatformTradeSettingEntity tradeSetting = cacheSettingUtils.getTradeSetting();
-        // 获取最新价
-        BigDecimal newPrice = new BigDecimal(redisUtils.getString(CoinTypeConvert.convertToKey(symbol)));
+        BigDecimal newPriceSymbol = new BigDecimal(redisUtils.getString(CoinTypeConvert.convertToKey(symbol)));
 
         // 当前合约委托单
         List<ContractEntrustOrderEntity> entrustOrderEntities = contractEntrustOrderDao.selectEntrustOrderListByMemberId(memberEntity.getId());
@@ -464,8 +462,10 @@
         // 总盈利
         BigDecimal totalProfitOrLess = BigDecimal.ZERO;
         if (CollUtil.isNotEmpty(holdOrderEntities)) {
-            BigDecimal lotNumber = cacheSettingUtils.getSymbolSku(symbol);
             for (ContractHoldOrderEntity holdOrderEntity : holdOrderEntities) {
+                // 获取最新价
+                BigDecimal newPrice = new BigDecimal(redisUtils.getString(CoinTypeConvert.convertToKey(holdOrderEntity.getSymbol())));
+                BigDecimal lotNumber = cacheSettingUtils.getSymbolSku(holdOrderEntity.getSymbol());
                 beUsedBondAmount = beUsedBondAmount.add(holdOrderEntity.getBondAmount());
 
                 // 单个订单盈利
@@ -492,6 +492,8 @@
 
         MemberWalletContractEntity walletContractEntity = memberWalletContractDao.findWalletContractByMemberIdAndSymbol(memberEntity.getId(), CoinTypeEnum.USDT.name());
 
+        MemberLevelRateEntity rateEntity = memberLevelRateDao.selectLeverRateByMemberIdAndSymbol(memberEntity.getId(), symbol);
+
         // 权益
         BigDecimal equity = walletContractEntity.getTotalBalance().add(totalProfitOrLess);
 
@@ -502,8 +504,9 @@
         contractMoneyInfoVo.setEquity(equity);
         contractMoneyInfoVo.setFeeRatio(tradeSetting.getFeeRatio());
         contractMoneyInfoVo.setLeverAgeRatio(tradeSetting.getLeverageRatio());
-        contractMoneyInfoVo.setNewPrice(newPrice);
+        contractMoneyInfoVo.setNewPrice(newPriceSymbol);
         contractMoneyInfoVo.setSymbolSku(cacheSettingUtils.getSymbolSku(symbol));
+        contractMoneyInfoVo.setLeverRate(rateEntity.getLevelRateUp());
         return Result.ok(contractMoneyInfoVo);
     }
 }

--
Gitblit v1.9.1