Helius
2020-07-02 cbf27426370f1fc9c8ae27b717c8d1f175ea358e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package com.xcong.excoin.modules.coin.service.impl;
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.xcong.excoin.common.enumerates.CoinTypeEnum;
import com.xcong.excoin.modules.blackchain.service.EthService;
import com.xcong.excoin.modules.coin.service.BlockCoinService;
import com.xcong.excoin.modules.member.dao.MemberCoinAddressDao;
import com.xcong.excoin.modules.member.dao.MemberCoinChargeDao;
import com.xcong.excoin.modules.member.dao.MemberDao;
import com.xcong.excoin.modules.member.dao.MemberWalletCoinDao;
import com.xcong.excoin.modules.member.entity.MemberCoinAddressEntity;
import com.xcong.excoin.modules.member.entity.MemberCoinChargeEntity;
import com.xcong.excoin.modules.member.entity.MemberEntity;
import com.xcong.excoin.modules.member.entity.MemberWalletCoinEntity;
import com.xcong.excoin.utils.LogRecordUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
 
/**
 * @author wzy
 * @date 2020-07-02
 **/
@Slf4j
@Service
public class BlockCoinServiceImpl implements BlockCoinService {
 
    @Resource
    private MemberCoinAddressDao memberCoinAddressDao;
    @Resource
    private MemberDao memberDao;
    @Resource
    private MemberCoinChargeDao memberCoinChargeDao;
    @Resource
    private MemberWalletCoinDao memberWalletCoinDao;
 
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateEthUsdt() {
        List<MemberCoinAddressEntity> list = memberCoinAddressDao.selectAllBlockAddressBySymbolAndTag(CoinTypeEnum.USDT.name(), "ERC20");
        if (CollUtil.isNotEmpty(list)) {
            EthService ethService = new EthService();
            for (MemberCoinAddressEntity addressEntity : list) {
                String address = addressEntity.getAddress();
                Long memberId = addressEntity.getMemberId();
 
                if (StrUtil.isNotBlank(address)) {
                    continue;
                }
 
                BigDecimal balance = ethService.tokenGetBalance(address);
                if (balance != null && balance.compareTo(new BigDecimal("0.1")) > 0) {
                    balance = balance.setScale(8, RoundingMode.CEILING);
 
                    BigDecimal early = BigDecimal.ZERO;
                    MemberCoinChargeEntity chargeEntity = memberCoinChargeDao.selectNewestChargeRecord(memberId, CoinTypeEnum.USDT.name(), "ERC20");
                    if (chargeEntity != null) {
                        BigDecimal lastAmount = chargeEntity.getLastAmount();
                        if (lastAmount != null) {
                            early = lastAmount;
                        }
                    }
 
                    MemberWalletCoinEntity walletCoinEntity = memberWalletCoinDao.selectWalletCoinBymIdAndCode(memberId, CoinTypeEnum.USDT.name());
                    if (walletCoinEntity == null) {
                        continue;
                    }
 
                    if (balance.compareTo(early) > 0) {
                        BigDecimal newBalance = balance.subtract(early);
                        memberWalletCoinDao.updateBlockBalance(memberId, newBalance, balance, 0);
 
                        insertCoinCharge(address, memberId, newBalance, CoinTypeEnum.USDT.name(), "ERC20", balance);
                        // TODO 钉钉发送, 短信提醒
                    }
                }
            }
        }
    }
 
    @Override
    public void updateEth() {
        List<MemberCoinAddressEntity> list = memberCoinAddressDao.selectAllBlockAddressBySymbol(CoinTypeEnum.ETH.name());
        if (CollUtil.isNotEmpty(list)) {
            for (MemberCoinAddressEntity coinAddressEntity : list) {
                String address = coinAddressEntity.getAddress();
                Long memberId = coinAddressEntity.getMemberId();
 
                BigDecimal balance = EthService.getEthBlance(address);
                if (balance != null && new BigDecimal("0.008").compareTo(balance) < 0) {
                    MemberWalletCoinEntity walletCoin = memberWalletCoinDao.selectWalletCoinBymIdAndCode(memberId, CoinTypeEnum.ETH.name());
 
                    if (walletCoin == null) {
                        continue;
                    }
 
                    BigDecimal early = BigDecimal.ZERO;
                    MemberCoinChargeEntity coinChargeEntity = memberCoinChargeDao.selectNewestChargeRecord(memberId, CoinTypeEnum.ETH.name(), null);
                    if (coinChargeEntity != null) {
                        BigDecimal lastAmount = coinChargeEntity.getLastAmount();
                        if (lastAmount != null) {
                            early = lastAmount;
                        }
                    }
 
                    balance = balance.setScale(8, RoundingMode.CEILING);
                    if (balance.compareTo(early) > 0) {
                        log.info("#ETH更新:{},{},{}#", memberId, balance, early);
 
                        BigDecimal newBalance = balance.subtract(early);
                        memberWalletCoinDao.updateBlockBalance(memberId, newBalance, balance, 0);
                        insertCoinCharge(address, memberId, newBalance, CoinTypeEnum.ETH.name(), null, balance);
 
                        // 插入财务记录
                        LogRecordUtils.insertMemberAccountMoneyChange(memberId, "转入", newBalance, CoinTypeEnum.ETH.name(), 1, 1);
                        // TODO 钉钉消息, 短信提醒
                    }
                }
            }
        }
    }
 
    @Override
    public void updateBtcUsdt() {
 
    }
 
    @Override
    public void updateBtc() {
 
    }
 
    private String generateNo() {
        // 生成订单号
        Long timestamp = System.currentTimeMillis();
        // 随机数
        int random = (int) (Math.random() * 10);
        return String.valueOf(timestamp).substring(2) + random;
    }
 
    public void insertCoinCharge(String address, Long memberId, BigDecimal newBalance, String symbol, String tag, BigDecimal lastAmount) {
        MemberCoinChargeEntity memberCoinChargeEntity = new MemberCoinChargeEntity();
        memberCoinChargeEntity.setAddress(address);
        memberCoinChargeEntity.setMemberId(memberId);
        memberCoinChargeEntity.setAmount(newBalance);
        memberCoinChargeEntity.setSymbol(symbol);
        memberCoinChargeEntity.setTag(tag);
        memberCoinChargeEntity.setStatus(1);
        memberCoinChargeEntity.setLastAmount(lastAmount);
        memberCoinChargeEntity.setOrderCode(generateNo());
        memberCoinChargeDao.insert(memberCoinChargeEntity);
    }
}