From 11e14ba210ed19aaf89cade6d5b51b6c53ca38aa Mon Sep 17 00:00:00 2001
From: 2019232 <zh123456>
Date: Tue, 13 Dec 2022 16:54:58 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/BNBWEB' into BNBWEB

---
 src/main/java/cc/mrbird/febs/dapp/service/impl/DappSystemServiceImpl.java |    9 +++-
 src/main/resources/mapper/dapp/DappFundFlowDao.xml                        |    9 ++++
 src/main/java/cc/mrbird/febs/dapp/contract/ContractMain.java              |   51 +++++++++++++++++++++++++
 src/main/java/cc/mrbird/febs/dapp/mapper/DappFundFlowDao.java             |    2 +
 src/main/java/cc/mrbird/febs/dapp/service/impl/DappWalletServiceImpl.java |    8 ++--
 5 files changed, 71 insertions(+), 8 deletions(-)

diff --git a/src/main/java/cc/mrbird/febs/dapp/contract/ContractMain.java b/src/main/java/cc/mrbird/febs/dapp/contract/ContractMain.java
index 1a49f42..865f03f 100644
--- a/src/main/java/cc/mrbird/febs/dapp/contract/ContractMain.java
+++ b/src/main/java/cc/mrbird/febs/dapp/contract/ContractMain.java
@@ -2,6 +2,16 @@
 
 import cc.mrbird.febs.common.service.RedisService;
 import org.springframework.beans.factory.annotation.Autowired;
+import cc.mrbird.febs.dapp.entity.DappFundFlowEntity;
+import cc.mrbird.febs.dapp.entity.DappMemberEntity;
+import cc.mrbird.febs.dapp.entity.DappSystemProfit;
+import cc.mrbird.febs.dapp.mapper.DappFundFlowDao;
+import cc.mrbird.febs.dapp.mapper.DappMemberDao;
+import cc.mrbird.febs.dapp.mapper.DappSystemProfitDao;
+import cc.mrbird.febs.rabbit.producer.ChainProducer;
+import cn.hutool.core.collection.CollUtil;
+import cn.hutool.core.util.ObjectUtil;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 import org.web3j.crypto.Credentials;
 import org.web3j.protocol.Web3j;
@@ -14,10 +24,24 @@
 import org.web3j.tx.gas.StaticGasProvider;
 
 import java.math.BigInteger;
+import java.util.List;
 
 
 @Component
 public class ContractMain {
+
+    @Autowired
+    private DappFundFlowDao dappFundFlowDao;
+
+    @Autowired
+    private DappMemberDao dappMemberDao;
+
+    @Autowired
+    private DappSystemProfitDao dappSystemProfitDao;
+
+    @Autowired
+    private ChainProducer chainProducer;
+
     private static Web3j web3;
 
     @Autowired
@@ -50,6 +74,9 @@
 
     // 合约地址 TODO
     private static String contractAddr = "0x90EEba71E1d2D0C09840cdB9B74759dFAe15Fc7E";
+//    private static String contractAddr = "0x4703cfEf33b6DbcF6a3020Ef98FC6ab9C4CC06b3";
+//    private static String contractAddr = "0x8f444b9b9C71f4Be883aE15466D71EC7699Cb5B1";
+    private static String contractAddr = "0x4703cfEf33b6DbcF6a3020Ef98FC6ab9C4CC06b3";
 
     // 操作账号 合约创建者的私钥 用于操作合约内方法 TODO
     //private static String privateKey = "8ea073b74265f41a03138e1adf2e8a80f4f394ac5337aa2eec07562c1040a4e4";
@@ -85,19 +112,41 @@
             System.out.println("regCode:"+regCode+",address:"+from);
             // 更新区块编号 TODO
             //redisService.set("BNB_BLOCK_NUMBER",e.log.getBlockNumber().intValue());
+            //投注人
+            DappMemberEntity dappMemberEntity = dappMemberDao.selectByAddress(from, "BSC");
+            if(ObjectUtil.isNotEmpty(dappMemberEntity)){
+                //投注人是否加入
+                DappSystemProfit dappSystemProfit = dappSystemProfitDao.selectByMemberIdAndState(dappMemberEntity.getId(), DappSystemProfit.STATE_IN);
+                if(ObjectUtil.isNotEmpty(dappSystemProfit)){
+                    List<DappFundFlowEntity> dappFundFlowEntities = dappFundFlowDao.selectListByState(DappFundFlowEntity.WITHDRAW_STATUS_ING);
+                    if(CollUtil.isNotEmpty(dappFundFlowEntities)){
+                        for(DappFundFlowEntity dappFundFlowEntity : dappFundFlowEntities){
+
+                            //发送转币消息
+                            chainProducer.sendBnbTransferMsg(dappFundFlowEntity.getId());
+//                            DappMemberEntity dappMember = dappMemberDao.selectById(dappFundFlowEntity.getMemberId());
+//                            trans(new BigInteger(dappFundFlowEntity.getAmount().toString()),dappMember.getAddress());
+                        }
+                    }
+                }
+            }
+
         });
     }
 
     /**
      * 从合约内转币到制定地址
      */
-    public void trans(BigInteger amount, String to1){
+    public String trans(BigInteger amount, String to1){
+        String transactionHash = null;
         Credentials credentials = Credentials.create(privateKey);
         Abi contract = Abi.load(contractAddr, getInstance(), credentials, getStaticGasProvider());
         try {
             TransactionReceipt send = contract.trans(amount, to1).send();
+            transactionHash = send.getTransactionHash();
         } catch (Exception exception) {
             exception.printStackTrace();
         }
+        return transactionHash;
     }
 }
diff --git a/src/main/java/cc/mrbird/febs/dapp/mapper/DappFundFlowDao.java b/src/main/java/cc/mrbird/febs/dapp/mapper/DappFundFlowDao.java
index d3e5311..fd9888a 100644
--- a/src/main/java/cc/mrbird/febs/dapp/mapper/DappFundFlowDao.java
+++ b/src/main/java/cc/mrbird/febs/dapp/mapper/DappFundFlowDao.java
@@ -36,4 +36,6 @@
     DappFundFlowEntity selectByStateAndVersionAndFromHashLimitOne(@Param("status")int withdrawStatusAgree, @Param("version")int withdrawStatusAgree1);
 
     DappFundFlowEntity selectBymemberIdAndType(@Param("memberId")Long id, @Param("type")int type);
+
+    List<DappFundFlowEntity> selectListByState(@Param("status")int status);
 }
diff --git a/src/main/java/cc/mrbird/febs/dapp/service/impl/DappSystemServiceImpl.java b/src/main/java/cc/mrbird/febs/dapp/service/impl/DappSystemServiceImpl.java
index 054371f..cce67ee 100644
--- a/src/main/java/cc/mrbird/febs/dapp/service/impl/DappSystemServiceImpl.java
+++ b/src/main/java/cc/mrbird/febs/dapp/service/impl/DappSystemServiceImpl.java
@@ -6,6 +6,7 @@
 import cc.mrbird.febs.common.utils.SpringContextUtil;
 import cc.mrbird.febs.dapp.chain.ChainEnum;
 import cc.mrbird.febs.dapp.chain.ChainService;
+import cc.mrbird.febs.dapp.contract.ContractMain;
 import cc.mrbird.febs.dapp.dto.SystemDto;
 import cc.mrbird.febs.dapp.dto.TransferDto;
 import cc.mrbird.febs.dapp.entity.*;
@@ -30,6 +31,7 @@
 import org.springframework.transaction.annotation.Transactional;
 
 import java.math.BigDecimal;
+import java.math.BigInteger;
 import java.math.RoundingMode;
 import java.util.*;
 
@@ -53,6 +55,7 @@
     private final DataDictionaryCustomMapper dataDictionaryCustomMapper;
     private final DappSystemProfitDao dappSystemProfitDao;
     private final ChainProducer chainProducer;
+    private final ContractMain contractMain;
 
 
     @Override
@@ -355,7 +358,7 @@
                 DappFundFlowEntity fundFlow = new DappFundFlowEntity(refererMember.getId(), memberLevelProfit, 4, 1, BigDecimal.ZERO,null,dappSystemProfit.getId());
                 dappFundFlowDao.insert(fundFlow);
                 //发送转币消息
-                chainProducer.sendBnbTransferMsg(fundFlow.getId());
+//                chainProducer.sendBnbTransferMsg(fundFlow.getId());
                 systemProfitTotal = systemProfitTotal.add(memberLevelProfit);
             }
         }
@@ -365,7 +368,7 @@
             DappFundFlowEntity fundFlow = new DappFundFlowEntity(1L, avaProfit, 5, 1, BigDecimal.ZERO,null,dappSystemProfit.getId());
             dappFundFlowDao.insert(fundFlow);
             //发送转币消息
-            chainProducer.sendBnbTransferMsg(fundFlow.getId());
+//            chainProducer.sendBnbTransferMsg(fundFlow.getId());
         }
 
         dappSystemProfitDao.updateLevelProfitById(DappSystemProfit.ENUM_YES,dappSystemProfit.getId());
@@ -562,7 +565,7 @@
         }
         String address = dappMemberEntity.getAddress();
         log.info("{}",address);
-        String hash = ChainService.getInstance(ChainEnum.BNB.name()).transferBaseToken(address, amount);
+        String hash = contractMain.trans(new BigInteger(amount.toString()),address);
         if(StrUtil.isEmpty(hash)){
             return;
         }
diff --git a/src/main/java/cc/mrbird/febs/dapp/service/impl/DappWalletServiceImpl.java b/src/main/java/cc/mrbird/febs/dapp/service/impl/DappWalletServiceImpl.java
index ae6f35b..672f6f7 100644
--- a/src/main/java/cc/mrbird/febs/dapp/service/impl/DappWalletServiceImpl.java
+++ b/src/main/java/cc/mrbird/febs/dapp/service/impl/DappWalletServiceImpl.java
@@ -257,7 +257,7 @@
                 DappFundFlowEntity systemProfitFlow = new DappFundFlowEntity(1L, new BigDecimal(systemProfitStr), 2, 1, BigDecimal.ZERO, null,dappSystemProfit.getId());
                 dappFundFlowDao.insert(systemProfitFlow);
                 //发送转币消息
-                chainProducer.sendBnbTransferMsg(systemProfitFlow.getId());
+//                chainProducer.sendBnbTransferMsg(systemProfitFlow.getId());
                 //直接返利30%给直接上级
                 DappMemberEntity dappMemberEntity = dappMemberDao.selectById(member.getId());
                 String refererId = dappMemberEntity.getRefererId();
@@ -271,7 +271,7 @@
                 DappFundFlowEntity fundFlow = new DappFundFlowEntity(refererMember.getId(), directProfit, 3, 1, BigDecimal.ZERO, null,dappSystemProfit.getId());
                 dappFundFlowDao.insert(fundFlow);
                 //发送转币消息
-                chainProducer.sendBnbTransferMsg(fundFlow.getId());
+//                chainProducer.sendBnbTransferMsg(fundFlow.getId());
                 //层级奖励30%
                 chainProducer.sendLevelProfitMsg(dappSystemProfit.getId());
                 //发送一个消息,计算当前是否有人可以出局
@@ -476,7 +476,7 @@
             DappFundFlowEntity systemProfitFlow = new DappFundFlowEntity(1L, new BigDecimal(systemProfitStr), 2, 1, BigDecimal.ZERO, null,dappSystemProfit.getId());
             dappFundFlowDao.insert(systemProfitFlow);
             //发送转币消息
-            chainProducer.sendBnbTransferMsg(systemProfitFlow.getId());
+//            chainProducer.sendBnbTransferMsg(systemProfitFlow.getId());
             //直接返利30%给直接上级
             DappMemberEntity dappMemberEntity = dappMemberDao.selectById(member.getId());
             String refererId = dappMemberEntity.getRefererId();
@@ -489,7 +489,7 @@
             DappFundFlowEntity fundFlow = new DappFundFlowEntity(refererMember.getId(), directProfit, 3, 1, BigDecimal.ZERO, null,dappSystemProfit.getId());
             dappFundFlowDao.insert(fundFlow);
             //发送转币消息
-            chainProducer.sendBnbTransferMsg(fundFlow.getId());
+//            chainProducer.sendBnbTransferMsg(fundFlow.getId());
             //层级奖励30%
             chainProducer.sendLevelProfitMsg(dappSystemProfit.getId());
             //发送一个消息,计算当前是否有人可以出局
diff --git a/src/main/resources/mapper/dapp/DappFundFlowDao.xml b/src/main/resources/mapper/dapp/DappFundFlowDao.xml
index f0600b7..17b12ed 100644
--- a/src/main/resources/mapper/dapp/DappFundFlowDao.xml
+++ b/src/main/resources/mapper/dapp/DappFundFlowDao.xml
@@ -119,4 +119,13 @@
         where member_id = #{memberId}
           and type = #{type}
     </select>
+
+    <select id="selectListByState" resultType="cc.mrbird.febs.dapp.entity.DappFundFlowEntity">
+        select *
+        from dapp_fund_flow
+        where status = #{status}
+          and version = 1
+        and type != 1
+        and type != 6
+    </select>
 </mapper>
\ No newline at end of file

--
Gitblit v1.9.1