From 451f88585ce56b852ceba3a7f20c62bf6fa9f8fe Mon Sep 17 00:00:00 2001
From: Helius <wangdoubleone@gmail.com>
Date: Thu, 01 Apr 2021 17:53:02 +0800
Subject: [PATCH] modify

---
 src/main/java/com/xcong/excoin/modules/documentary/service/impl/FollowOrderOperationServiceImpl.java |   75 ++++++++++++++++++++++++++++++++-----
 1 files changed, 65 insertions(+), 10 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 f51d58b..7f70e63 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
@@ -6,10 +6,13 @@
 import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.Wrapper;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.xcong.excoin.common.contants.AppContants;
 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.response.Result;
 import com.xcong.excoin.common.system.service.CommonService;
+import com.xcong.excoin.modules.coin.entity.MemberAccountMoneyChange;
 import com.xcong.excoin.modules.contract.dao.ContractHoldOrderDao;
 import com.xcong.excoin.modules.contract.dao.ContractOrderDao;
 import com.xcong.excoin.modules.contract.entity.ContractEntrustOrderEntity;
@@ -18,10 +21,7 @@
 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;
-import com.xcong.excoin.modules.documentary.dao.FollowFollowerSettingDao;
-import com.xcong.excoin.modules.documentary.dao.FollowTraderInfoDao;
+import com.xcong.excoin.modules.documentary.dao.*;
 import com.xcong.excoin.modules.documentary.entity.FollowFollowerOrderRelationEntity;
 import com.xcong.excoin.modules.documentary.entity.FollowFollowerProfitEntity;
 import com.xcong.excoin.modules.documentary.entity.FollowFollowerSettingEntity;
@@ -36,10 +36,7 @@
 import com.xcong.excoin.rabbit.pricequeue.OrderModel;
 import com.xcong.excoin.rabbit.producer.FollowProducer;
 import com.xcong.excoin.rabbit.producer.OrderProducer;
-import com.xcong.excoin.utils.CacheSettingUtils;
-import com.xcong.excoin.utils.CalculateUtil;
-import com.xcong.excoin.utils.LogRecordUtils;
-import com.xcong.excoin.utils.ThreadPoolUtils;
+import com.xcong.excoin.utils.*;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
@@ -88,7 +85,10 @@
 
     @Autowired
     private FollowProducer followProducer;
-
+    @Autowired
+    private FollowTraderProfitDetailDao followTraderProfitDetailDao;
+    @Resource
+    private RedisUtils redisUtils;
 
     @Override
     @Transactional(rollbackFor = Exception.class)
@@ -121,9 +121,21 @@
         BigDecimal lotNumber = cacheSettingUtils.getSymbolSku(holdOrderEntity.getSymbol());
         Long tradeMemberId = holdOrderEntity.getMemberId();
         if (CollUtil.isNotEmpty(followerSettings)) {
+            List<Object> followerMemberId =  redisUtils.lGet(AppContants.MEMBER_HAS_FOLLOW, 0, -1);
             for (FollowFollowerSettingEntity followerSetting : followerSettings) {
+                // 加redis锁,同一个用户不能同时触发两个跟单任务,否则会出现金额问题
+                while(true) {
+                    log.info("#跟单用户任务已存在:{}, 当前:{}#", followerMemberId, followerSetting.getMemberId());
+                    log.info("#------->{}#", followerMemberId.contains(followerSetting.getMemberId().intValue()));
+                    if (CollUtil.isEmpty(followerMemberId) || !followerMemberId.contains(followerSetting.getMemberId().intValue())) {
+                        log.info("跳出");
+                        followerMemberId.add(followerSetting.getMemberId());
+                        redisUtils.lSet(AppContants.MEMBER_HAS_FOLLOW, followerMemberId);
+                        break;
+                    }
+                }
 
-            	//更新更新消息提醒的状态
+                //更新更新消息提醒的状态
                 MemberSettingEntity memberSettingEntity = memberSettingDao.selectMemberSettingByMemberId(followerSetting.getMemberId());
                 if(ObjectUtil.isNotEmpty(memberSettingEntity)){
                 	Long memberId = memberSettingEntity.getMemberId();
@@ -243,6 +255,7 @@
                     }
                 }
             }
+            redisUtils.del(AppContants.MEMBER_HAS_FOLLOW);
         }
     }
 
@@ -268,6 +281,48 @@
                 ids.add(orderRelation.getOrderId());
                 rabbitOrderService.cancelHoldOrder(ids);
             }
+            //生成返利记录和资金变化记录
+            List<FollowFollowerOrderRelationEntity> orderRelationDone = followFollowerOrderRelationDao.selectFollowOrderByTradeOrderNo(orderNo);
+            if(CollUtil.isNotEmpty(orderRelationDone)) {
+            	List<Long> orderIds = new ArrayList<>();
+            	for (FollowFollowerOrderRelationEntity orderRelationd : orderRelationDone) {
+                    orderIds.add(orderRelationd.getOrderId());
+                }
+            
+	            if(CollUtil.isNotEmpty(orderIds)){
+	                //获取对应的平仓记录单号
+	                List<String> orderNosList = new ArrayList<>();
+	                for(Long orderId : orderIds) {
+	                    String orderNos = contractOrderDao.selectOrderNoByOrderIds(orderId);
+	                    orderNosList.add(orderNos);
+	                }
+	                //获取总返佣
+	                BigDecimal totalAmount = followTraderProfitDetailDao.selectFollowHoldOrderByFollowOrderNo(orderNosList);
+	                totalAmount = (totalAmount == null?BigDecimal.ZERO:totalAmount.setScale(2, BigDecimal.ROUND_DOWN));
+	                if(totalAmount.compareTo(BigDecimal.ZERO) > 0){
+	                    //增加返佣提醒
+	                    String symbol = contractOrderDao.selectById(orderIds.get(0)).getSymbol();
+	                    String orderNoTrader = orderRelationDone.get(0).getTradeOrderNo();
+	                    Long traderMemberId = orderRelationDone.get(0).getTradeMemberId();
+	                    LogRecordUtils.insertFollowerNotice(traderMemberId,
+	                            NoticeConstant.RETURN_MONEY_TITLE,
+	                            StrUtil.format(NoticeConstant.RETURN_MONEY_CONTENT,
+	                                    orderNoTrader,
+	                                    symbol,
+	                                    totalAmount));
+	                    //带单返利的记录要在资产页面的其他记录
+	                    LogRecordUtils.insertMemberAccountMoneyChange(
+	                            traderMemberId,
+	                            StrUtil.format(NoticeConstant.RETURN_MONEY_CONTENT_MAMC,
+                                        symbol,
+                                        orderNoTrader.substring(8)),
+	                            totalAmount,
+	                            MemberWalletCoinEnum.WALLETCOINCODE.getValue(),
+	                            MemberAccountMoneyChange.STATUS_SUCCESS_INTEGER,
+	                            MemberAccountMoneyChange.TYPE_WALLET_AGENT);
+	                }
+	            }
+            }
         }
     }
 }

--
Gitblit v1.9.1