From ea8eb9c441bb4452d153579b456467b2f5e958e4 Mon Sep 17 00:00:00 2001
From: Helius <wangdoubleone@gmail.com>
Date: Tue, 04 Aug 2020 16:15:54 +0800
Subject: [PATCH] add trader closing order

---
 src/main/java/com/xcong/excoin/modules/documentary/dao/FollowFollowerOrderRelationDao.java           |    4 ++
 src/main/java/com/xcong/excoin/modules/contract/service/impl/RabbitOrderServiceImpl.java             |   34 +++++++++++++++++
 src/main/java/com/xcong/excoin/modules/documentary/service/FollowOrderOperationService.java          |    5 +-
 src/main/java/com/xcong/excoin/modules/contract/service/impl/ContractHoldOrderServiceImpl.java       |    2 
 src/main/java/com/xcong/excoin/modules/documentary/entity/FollowFollowerOrderRelationEntity.java     |    2 
 src/main/java/com/xcong/excoin/modules/documentary/service/impl/FollowOrderOperationServiceImpl.java |   19 ++++++++-
 src/main/java/com/xcong/excoin/utils/ThreadPoolUtils.java                                            |    2 
 src/main/resources/mapper/documentary/FollowFollowerOrderRelationDao.xml                             |    7 +++
 8 files changed, 67 insertions(+), 8 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 952af58..8627d17 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
@@ -213,7 +213,7 @@
                 relationEntity.setOrderType(FollowFollowerOrderRelationEntity.ORDER_TYPE_HOLD);
                 relationEntity.setTradeId(tradeInfo.getId());
                 relationEntity.setTradeMemberId(tradeInfo.getMemberId());
-                relationEntity.setTradeOrderId(holdOrderEntity.getId());
+                relationEntity.setTradeOrderNo(holdOrderEntity.getOrderNo());
                 followFollowerOrderRelationDao.insert(relationEntity);
 
                 ThreadPoolUtils.sendFollowOrderTask(holdOrderEntity.getId());
diff --git a/src/main/java/com/xcong/excoin/modules/contract/service/impl/RabbitOrderServiceImpl.java b/src/main/java/com/xcong/excoin/modules/contract/service/impl/RabbitOrderServiceImpl.java
index 2a2aac9..0ae1e39 100644
--- a/src/main/java/com/xcong/excoin/modules/contract/service/impl/RabbitOrderServiceImpl.java
+++ b/src/main/java/com/xcong/excoin/modules/contract/service/impl/RabbitOrderServiceImpl.java
@@ -10,6 +10,9 @@
 import com.xcong.excoin.modules.contract.entity.ContractOrderEntity;
 import com.xcong.excoin.modules.contract.mapper.ContractHoldOrderEntityMapper;
 import com.xcong.excoin.modules.contract.service.RabbitOrderService;
+import com.xcong.excoin.modules.documentary.dao.FollowFollowerOrderRelationDao;
+import com.xcong.excoin.modules.documentary.entity.FollowFollowerOrderRelationEntity;
+import com.xcong.excoin.modules.documentary.service.FollowOrderOperationService;
 import com.xcong.excoin.modules.member.dao.MemberDao;
 import com.xcong.excoin.modules.member.dao.MemberWalletContractDao;
 import com.xcong.excoin.modules.member.entity.AgentReturnEntity;
@@ -58,6 +61,11 @@
     @Resource
     private RedisUtils redisUtils;
 
+    @Resource
+    private FollowFollowerOrderRelationDao followFollowerOrderRelationDao;
+    @Resource
+    private FollowOrderOperationService followOrderOperationService;
+
     @Transactional(rollbackFor = Exception.class)
     @Override
     public void cancelHoldOrder(List<Long> ids) {
@@ -76,6 +84,7 @@
         }
     }
 
+    @Transactional(rollbackFor = Exception.class)
     public void cancelHoldOrderMethod(ContractHoldOrderEntity holdOrderEntity) {
         String symbol = holdOrderEntity.getSymbol();
         // 获取最新价
@@ -141,7 +150,32 @@
 
             // 计算佣金
             ThreadPoolUtils.calReturnMoney(memberEntity.getId(), contractOrderEntity.getClosingFeeAmount(), contractOrderEntity, AgentReturnEntity.ORDER_TYPE_CLOSE);
+
+            // 判断当前持仓是否为跟单订单
+            if (ContractOrderEntity.CONTRACTTYPE_DOCUMENTARY == holdOrderEntity.getContractType()) {
+                updateFollowOrderRelation(holdOrderEntity.getId(), contractOrderEntity.getId());
+
+                // 若为交易员,则平仓跟随者订单
+                if (MemberEntity.IS_TRADER_Y.equals(memberEntity.getIsTrader())) {
+                    followOrderOperationService.closingFollowOrders(holdOrderEntity.getOrderNo());
+                }
+            }
         }
 
     }
+
+    /**
+     * 更新跟单订单关系
+     *
+     * @param oldOrderId 当前持仓ID
+     * @param newOrderId 历史订单ID
+     */
+    public void updateFollowOrderRelation(Long oldOrderId, Long newOrderId) {
+        FollowFollowerOrderRelationEntity orderRelationEntity = followFollowerOrderRelationDao.selectNowOneByorderId(oldOrderId);
+        if (orderRelationEntity != null) {
+            orderRelationEntity.setOrderId(newOrderId);
+            orderRelationEntity.setOrderType(FollowFollowerOrderRelationEntity.ORDER_TYPE_HISTORY);
+            followFollowerOrderRelationDao.updateById(orderRelationEntity);
+        }
+    }
 }
diff --git a/src/main/java/com/xcong/excoin/modules/documentary/dao/FollowFollowerOrderRelationDao.java b/src/main/java/com/xcong/excoin/modules/documentary/dao/FollowFollowerOrderRelationDao.java
index 9af8222..f81546d 100644
--- a/src/main/java/com/xcong/excoin/modules/documentary/dao/FollowFollowerOrderRelationDao.java
+++ b/src/main/java/com/xcong/excoin/modules/documentary/dao/FollowFollowerOrderRelationDao.java
@@ -4,6 +4,8 @@
 import com.xcong.excoin.modules.documentary.entity.FollowFollowerOrderRelationEntity;
 import org.apache.ibatis.annotations.Param;
 
+import java.util.List;
+
 /**
  * @author helius
  */
@@ -13,4 +15,6 @@
 	FollowFollowerOrderRelationEntity selectHistoryOneByorderId(@Param("orderId")Long orderId);
 	
 	FollowFollowerOrderRelationEntity selectNowOneByorderId(@Param("orderId")Long orderId);
+
+	List<FollowFollowerOrderRelationEntity> selectFollowHoldOrderByTradeOrderNo(@Param("orderNo") String orderNo);
 }
diff --git a/src/main/java/com/xcong/excoin/modules/documentary/entity/FollowFollowerOrderRelationEntity.java b/src/main/java/com/xcong/excoin/modules/documentary/entity/FollowFollowerOrderRelationEntity.java
index 73a58f4..fa79159 100644
--- a/src/main/java/com/xcong/excoin/modules/documentary/entity/FollowFollowerOrderRelationEntity.java
+++ b/src/main/java/com/xcong/excoin/modules/documentary/entity/FollowFollowerOrderRelationEntity.java
@@ -38,7 +38,7 @@
     /**
      * 跟单订单ID
      */
-    private Long tradeOrderId;
+    private String tradeOrderNo;
 
     /**
      * 交易员用户ID
diff --git a/src/main/java/com/xcong/excoin/modules/documentary/service/FollowOrderOperationService.java b/src/main/java/com/xcong/excoin/modules/documentary/service/FollowOrderOperationService.java
index 757a081..bde0247 100644
--- a/src/main/java/com/xcong/excoin/modules/documentary/service/FollowOrderOperationService.java
+++ b/src/main/java/com/xcong/excoin/modules/documentary/service/FollowOrderOperationService.java
@@ -19,7 +19,8 @@
     /**
      * 跟随者订单平仓
      *
-     * @param id 交易员持仓ID
+     * @param oldOrderId 交易员持仓ID
+     * @param newOrderId 交易员平仓后历史ID
      */
-    public void closingFollowOrders(Long id);
+    public void closingFollowOrders(String orderNo);
 }
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 a07be6c..0173007 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
@@ -13,6 +13,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.service.RabbitOrderService;
 import com.xcong.excoin.modules.documentary.common.NoticeConstant;
 import com.xcong.excoin.modules.documentary.dao.FollowFollowerOrderRelationDao;
 import com.xcong.excoin.modules.documentary.dao.FollowFollowerProfitDao;
@@ -41,6 +42,7 @@
 import javax.annotation.Resource;
 import java.math.BigDecimal;
 import java.math.RoundingMode;
+import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
 
@@ -71,6 +73,9 @@
     private OrderProducer producer;
     @Resource
     private FollowTraderInfoDao followTraderInfoDao;
+
+    @Resource
+    private RabbitOrderService rabbitOrderService;
 
 
     @Override
@@ -178,7 +183,7 @@
                     relationEntity.setOrderType(FollowFollowerOrderRelationEntity.ORDER_TYPE_HOLD);
                     relationEntity.setTradeId(followTraderInfoEntity.getId());
                     relationEntity.setTradeMemberId(followTraderInfoEntity.getMemberId());
-                    relationEntity.setTradeOrderId(id);
+                    relationEntity.setTradeOrderNo(holdOrderEntity.getOrderNo());
                     followFollowerOrderRelationDao.insert(relationEntity);
 
                     // 发送爆仓消息
@@ -212,8 +217,16 @@
         producer.sendPriceOperate(JSONObject.toJSONString(model));
     }
 
-    @Override
-    public void closingFollowOrders(Long id) {
 
+    @Override
+    public void closingFollowOrders(String orderNo) {
+        List<FollowFollowerOrderRelationEntity> orderRelations = followFollowerOrderRelationDao.selectFollowHoldOrderByTradeOrderNo(orderNo);
+        if (CollUtil.isNotEmpty(orderRelations)) {
+            for (FollowFollowerOrderRelationEntity orderRelation : orderRelations) {
+                List<Long> ids= new ArrayList<>();
+                ids.add(orderRelation.getOrderId());
+                rabbitOrderService.cancelHoldOrder(ids);
+            }
+        }
     }
 }
diff --git a/src/main/java/com/xcong/excoin/utils/ThreadPoolUtils.java b/src/main/java/com/xcong/excoin/utils/ThreadPoolUtils.java
index c3f008c..47b89a5 100644
--- a/src/main/java/com/xcong/excoin/utils/ThreadPoolUtils.java
+++ b/src/main/java/com/xcong/excoin/utils/ThreadPoolUtils.java
@@ -59,7 +59,7 @@
                 try {
                     followOrderOperationService.addFollowerOrder(id);
                 } catch (Exception e) {
-                    log.info("--->", e);
+                    log.error("产生跟单任务报错", e);
                 }
             }
         });
diff --git a/src/main/resources/mapper/documentary/FollowFollowerOrderRelationDao.xml b/src/main/resources/mapper/documentary/FollowFollowerOrderRelationDao.xml
index 10e3018..51c0781 100644
--- a/src/main/resources/mapper/documentary/FollowFollowerOrderRelationDao.xml
+++ b/src/main/resources/mapper/documentary/FollowFollowerOrderRelationDao.xml
@@ -25,5 +25,12 @@
 			follow_follower_order_relation 
 		WHERE order_id = #{orderId} and order_type = 1
     </select>
+
+	<select id="selectFollowHoldOrderByTradeOrderNo" resultType="com.xcong.excoin.modules.documentary.entity.FollowFollowerOrderRelationEntity">
+		select a.*
+		from follow_follower_order_relation a, follow_follower_profit b
+		where a.trade_member_id=b.trade_member_id and a.trade_order_no=#{orderNo} and b.is_follow=1
+		and a.order_type=1
+	</select>
 	
 </mapper>
\ No newline at end of file

--
Gitblit v1.9.1