From 48e20b2b0f03c52a904271b177678f9f8818754d Mon Sep 17 00:00:00 2001
From: Helius <wangdoubleone@gmail.com>
Date: Tue, 06 Apr 2021 15:20:50 +0800
Subject: [PATCH] add follow change bond

---
 src/main/java/com/xcong/excoin/modules/documentary/service/impl/FollowOrderOperationServiceImpl.java |   68 ++++++++++++++++++++++++++++++++++
 1 files changed, 68 insertions(+), 0 deletions(-)

diff --git a/src/main/java/com/xcong/excoin/modules/documentary/service/impl/FollowOrderOperationServiceImpl.java b/src/main/java/com/xcong/excoin/modules/documentary/service/impl/FollowOrderOperationServiceImpl.java
index f218e29..eae5138 100644
--- a/src/main/java/com/xcong/excoin/modules/documentary/service/impl/FollowOrderOperationServiceImpl.java
+++ b/src/main/java/com/xcong/excoin/modules/documentary/service/impl/FollowOrderOperationServiceImpl.java
@@ -10,6 +10,7 @@
 import com.xcong.excoin.common.enumerates.CoinTypeEnum;
 import com.xcong.excoin.common.enumerates.MemberWalletCoinEnum;
 import com.xcong.excoin.common.enumerates.RabbitPriceTypeEnum;
+import com.xcong.excoin.common.exception.GlobalException;
 import com.xcong.excoin.common.response.Result;
 import com.xcong.excoin.common.system.service.CommonService;
 import com.xcong.excoin.modules.coin.entity.MemberAccountMoneyChange;
@@ -19,6 +20,7 @@
 import com.xcong.excoin.modules.contract.entity.ContractHoldOrderEntity;
 import com.xcong.excoin.modules.contract.entity.ContractOrderEntity;
 import com.xcong.excoin.modules.contract.mapper.ContractHoldOrderEntityMapper;
+import com.xcong.excoin.modules.contract.parameter.dto.ChangeBondDto;
 import com.xcong.excoin.modules.contract.service.RabbitOrderService;
 import com.xcong.excoin.modules.documentary.common.NoticeConstant;
 import com.xcong.excoin.modules.documentary.dao.*;
@@ -368,4 +370,70 @@
             }
         }
     }
+
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public void changeFollowOrdersBond(Long id, BigDecimal bond, Integer type) {
+        log.info("==跟单调整保证金:{}, {}, {}==", id, bond, type);
+        ContractHoldOrderEntity contractHoldOrderEntity = contractHoldOrderDao.selectById(id);
+        if (contractHoldOrderEntity == null) {
+            log.info("持仓不存在:{}", id);
+            return;
+        }
+
+        // 交易员信息
+        FollowTraderInfoEntity followTraderInfoEntity = followTraderInfoDao.selectTraderInfoByMemberId(contractHoldOrderEntity.getMemberId());
+
+        List<FollowFollowerOrderRelationEntity> relations = followFollowerOrderRelationDao.selectFollowHoldOrderByTradeOrderNo(contractHoldOrderEntity.getOrderNo());
+        if (CollUtil.isNotEmpty(relations)) {
+            for (FollowFollowerOrderRelationEntity relation : relations) {
+                ContractHoldOrderEntity followHoldOrder = contractHoldOrderDao.selectById(relation.getOrderId());
+                MemberEntity memberEntity = memberDao.selectById(relation.getMemberId());
+                MemberWalletContractEntity wallet = memberWalletContractDao.findWalletContractByMemberIdAndSymbol(memberEntity.getId(), CoinTypeEnum.USDT.name());
+
+                // 增加保证金
+                if (type == ChangeBondDto.TYPE_ADD) {
+                    if (bond.compareTo(wallet.getAvailableBalance()) > 0) {
+                        log.info("可用金额不足:{}, {}", memberEntity.getId(), followHoldOrder.getOrderNo());
+                        LogRecordUtils.insertFollowerNotice(memberEntity.getId(), NoticeConstant.CHANGE_BOND_TITLE, NoticeConstant.CHANGE_BOND_ADD_CONTENT_FAIL);
+                        continue;
+                    }
+
+                    memberWalletContractDao.increaseWalletContractBalanceById(bond.negate(), null, null, wallet.getId());
+                    followHoldOrder.setBondAmount(followHoldOrder.getBondAmount().add(bond));
+                    LogRecordUtils.insertFollowerNotice(memberEntity.getId(), NoticeConstant.CHANGE_BOND_TITLE, StrUtil.format(NoticeConstant.CHANGE_BOND_CONTENT_SUCCESS, bond, followTraderInfoEntity.getNickname()));
+                } else {
+                    if (followHoldOrder.getBondAmount().subtract(followHoldOrder.getPrePaymentAmount()).subtract(bond).compareTo(BigDecimal.ZERO) < 0) {
+                        log.info("超出保证金最大减少金额-1");
+                        LogRecordUtils.insertFollowerNotice(memberEntity.getId(), NoticeConstant.CHANGE_BOND_TITLE, NoticeConstant.CHANGE_BOND_ADD_CONTENT_FAIL);
+                        continue;
+                    }
+
+                    BigDecimal profitOrLoss = CalculateUtil.calProfitOrLoss(followHoldOrder, memberEntity);
+                    if (profitOrLoss.compareTo(BigDecimal.ZERO) < 0) {
+                        BigDecimal canReduceMax = followHoldOrder.getBondAmount().subtract(followHoldOrder.getPrePaymentAmount()).add(profitOrLoss);
+                        if (canReduceMax.subtract(bond).compareTo(BigDecimal.ZERO) < 0) {
+                            log.info("超出保证金最大减少金额-2");
+                            LogRecordUtils.insertFollowerNotice(memberEntity.getId(), NoticeConstant.CHANGE_BOND_TITLE, NoticeConstant.CHANGE_BOND_ADD_CONTENT_FAIL);
+                            continue;
+                        }
+                    }
+
+                    memberWalletContractDao.increaseWalletContractBalanceById(bond, null, null, wallet.getId());
+                    followHoldOrder.setBondAmount(followHoldOrder.getBondAmount().subtract(bond));
+                    LogRecordUtils.insertFollowerNotice(memberEntity.getId(), NoticeConstant.CHANGE_BOND_TITLE, StrUtil.format(NoticeConstant.CHANGE_BOND_CONTENT_SUCCESS, bond.negate(), followTraderInfoEntity.getNickname()));
+                }
+
+                BigDecimal forceClosingPrice = CalculateUtil.getForceSetPrice(followHoldOrder.getBondAmount(), followHoldOrder.getOpeningPrice(), followHoldOrder.getSymbolCnt(), followHoldOrder.getSymbolSku(), followHoldOrder.getOpeningType(), memberEntity);
+                followHoldOrder.setForceClosingPrice(forceClosingPrice);
+                followHoldOrder.setOperateNo(followHoldOrder.getOperateNo() + 1);
+                int i = contractHoldOrderDao.updateById(followHoldOrder);
+                if (i > 0) {
+                    sendOrderBombMsg(followHoldOrder.getId(), followHoldOrder.getOpeningType(), forceClosingPrice, followHoldOrder.getSymbol(), followHoldOrder.getOperateNo(), followHoldOrder.getMemberId());
+                } else {
+                    throw new GlobalException("更新失败");
+                }
+            }
+        }
+    }
 }

--
Gitblit v1.9.1