zj
2025-05-28 bcd36411feb0351347289476eb7ee9e5dd1c3b9d
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package project.blockchain.internal;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.security.providers.encoding.PasswordEncoder;
import kernel.bo.RecordObjectMapper;
import kernel.exception.BusinessException;
import kernel.web.ApplicationUtil;
import project.Constants;
import project.blockchain.ChannelBlockchain;
import project.blockchain.ChannelBlockchainService;
import project.log.Log;
import project.log.LogService;
import project.user.googleauth.GoogleAuthService;
import security.SecUser;
import security.internal.SecUserService;
 
public class ChannelBlockchainServiceImpl implements ChannelBlockchainService {
 
    private JdbcTemplate jdbcTemplate;
    private LogService logService;
    private SecUserService secUserService;
    private PasswordEncoder passwordEncoder;
    private GoogleAuthService googleAuthService;
 
    public void save(ChannelBlockchain entity, String userName, String safeword, String login_ip, String code,
            String superGoogleAuthCode) {
//        checkEmailCode(code);
        googleAuthService.checkSuperGoogleAuthCode(superGoogleAuthCode);
        SecUser sec = this.secUserService.findUserByLoginName(userName);
        String sysSafeword = sec.getSafeword();
 
        String safeword_md5 = passwordEncoder.encodePassword(safeword, userName);
        if (!safeword_md5.equals(sysSafeword)) {
            throw new BusinessException("资金密码错误");
        }
 
        entity.setId(ApplicationUtil.getCurrentTimeUUID());
        Object[] jdbcParams = ApplicationUtil.getInsertStatement(entity);
        String insertUserSql = (String)jdbcParams[0];
        Object[] sqlParameters = (Object[])jdbcParams[1];
        jdbcTemplate.update(insertUserSql, sqlParameters);
        
        String log = "新增地址,名称[" + entity.getBlockchain_name() + "],地址[" + entity.getAddress() + "]" + "币种["
                + entity.getCoin() + "],图片[" + entity.getImg() + "]," + "ip[" + login_ip + "],验证码:[" + code + "]";
        saveLog(sec, userName, log);
 
        sendCodeToSysUsers(sec, userName, "新增区块链地址地址", "用户[" + userName + "],新增区块链地址地址");
    }
 
    private void sendCodeToSysUsers(SecUser secUser, String operator, String title, String content) {
        List<SecUser> users = secUserService.findAllSysUsers();
        String log = "以下系统用户尚未配置邮箱,无法收到邮件:";
        for (SecUser user : users) {
            if (StringUtils.isEmpty(user.getEmail())) {
                log += user.getUsername() + ",";
            } else {
                log += user.getUsername() + ",";
            }
        }
        saveLog(secUser, operator, log);
    }
 
    public void update(ChannelBlockchain old, ChannelBlockchain entity, String userName, String partyId,
            String safeword, String login_ip, String code, String superGoogleAuthCode) {
        googleAuthService.checkSuperGoogleAuthCode(superGoogleAuthCode);
        SecUser sec = this.secUserService.findUserByLoginName(userName);
        String sysSafeword = sec.getSafeword();
 
        String safeword_md5 = passwordEncoder.encodePassword(safeword, userName);
        if (!safeword_md5.equals(sysSafeword)) {
            throw new BusinessException("资金密码错误");
        }
 
        Object[] jdbcParams=ApplicationUtil.getUpdateStatement(entity, "WHERE UUID=?",new Object[] {entity.getId()});
        String updateUserSql = (String)jdbcParams[0];
        Object[] sqlParameters = (Object[])jdbcParams[1];
        jdbcTemplate.update(updateUserSql, sqlParameters);
 
        String log = "区块链充值地址修改。原名称[" + old.getBlockchain_name() + "],原地址[" + old.getAddress() + "]" + "原币种["
                + old.getCoin() + "],原图片[" + old.getImg() + "]。" + "修改后,新名称[" + entity.getBlockchain_name() + "],新地址["
                + entity.getAddress() + "]" + "新币种[" + entity.getCoin() + "],新图片[" + entity.getImg() + "]," + "ip["
                + login_ip + "],验证码:[" + code + "]";
        saveLog(sec, userName, log);
        sendCodeToSysUsers(sec, userName, "区块链充值地址修改", "用户[" + userName + "],区块链充值地址修改");
    }
 
    public void saveLog(SecUser secUser, String operator, String context) {
        Log log = new Log();
        log.setCategory(Constants.LOG_CATEGORY_OPERATION);
        log.setOperator(operator);
        log.setUsername(secUser.getUsername());
        log.setPartyId(secUser.getPartyId());
        log.setLog(context);
        log.setCreateTime(new Date());
        logService.saveSync(log);
    }
 
    public void delete(String id, String safeword, String userName, String loginIp, String code,
            String superGoogleAuthCode) {
        googleAuthService.checkSuperGoogleAuthCode(superGoogleAuthCode);
        SecUser sec = this.secUserService.findUserByLoginName(userName);
        String sysSafeword = sec.getSafeword();
 
        String safeword_md5 = passwordEncoder.encodePassword(safeword, userName);
        if (!safeword_md5.equals(sysSafeword)) {
            throw new BusinessException("资金密码错误");
        }
 
        ChannelBlockchain entity = findById(id);
        jdbcTemplate.update("DELETE FROM T_CHANNEL_BLOCKCHAIN WHERE UUID=?", entity.getId());
        
        String log = "删除地址,名称[" + entity.getBlockchain_name() + "],地址[" + entity.getAddress() + "]" + "币种["
                + entity.getCoin() + "],图片[" + entity.getImg() + "]," + "ip[" + loginIp + "],验证码:[" + code + "]";
        saveLog(sec, userName, log);
        sendCodeToSysUsers(sec, userName, "区块链充值地址删除", "用户[" + userName + "],区块链充值地址删除");
    }
 
    public List<ChannelBlockchain> findAll() {
        return jdbcTemplate.query("SELECT * FROM T_CHANNEL_BLOCKCHAIN", RecordObjectMapper.newInstance(ChannelBlockchain.class));
    }
 
    public ChannelBlockchain findById(String id) {
        List<ChannelBlockchain> list = jdbcTemplate.query("SELECT * FROM T_CHANNEL_BLOCKCHAIN WHERE UUID=?", 
                RecordObjectMapper.newInstance(ChannelBlockchain.class), id);
        if (null != list && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }
 
    @Override
    public ChannelBlockchain findByNameAndCoinAndAdd(String blockchain_name, String coin, String address) {
        List<ChannelBlockchain> list = new ArrayList<ChannelBlockchain>();
        if (StringUtils.isEmpty(address)) {
            list = jdbcTemplate.query("SELECT * FROM T_CHANNEL_BLOCKCHAIN WHERE BLOCKCHAIN_NAME = ? AND COIN=? ", 
                    RecordObjectMapper.newInstance(ChannelBlockchain.class), blockchain_name, coin );
        } else {
            list = jdbcTemplate.query("SELECT * FROM T_CHANNEL_BLOCKCHAIN WHERE BLOCKCHAIN_NAME = ? AND COIN=? AND ADDRESS =? ", 
                    RecordObjectMapper.newInstance(ChannelBlockchain.class), blockchain_name, coin, address );
        }
        if (list.size() > 0)
            return list.get(0);
        return null;
    }
 
    public List<ChannelBlockchain> findByCoin(String coin) {
        List<ChannelBlockchain> list = jdbcTemplate.query("SELECT * FROM T_CHANNEL_BLOCKCHAIN WHERE COIN=? ", 
                RecordObjectMapper.newInstance(ChannelBlockchain.class), coin );
        if (list.size() > 0)
            return filterBlockchain(list);
        return null;
    }
 
    public List<ChannelBlockchain> findByCoinAndName(String coin, String blockchain_name) {
        List<ChannelBlockchain> list = jdbcTemplate.query("SELECT * FROM T_CHANNEL_BLOCKCHAIN WHERE BLOCKCHAIN_NAME = ? AND COIN=? ", 
                RecordObjectMapper.newInstance(ChannelBlockchain.class), blockchain_name, coin );
        if (list.size() > 0)
            return filterBlockchain(list);
        return null;
    }
 
    /**
     * 过滤充值地址链,随机获取
     * 
     * @param list
     * @return
     */
    public List<ChannelBlockchain> filterBlockchain(List<ChannelBlockchain> list) {
        Map<String, List<ChannelBlockchain>> map = new HashMap<String, List<ChannelBlockchain>>();
        for (ChannelBlockchain cb : list) {
            if (map.containsKey(cb.getBlockchain_name())) {
                map.get(cb.getBlockchain_name()).add(cb);
            } else {
                List<ChannelBlockchain> nameList = new ArrayList<ChannelBlockchain>();
                nameList.add(cb);
                map.put(cb.getBlockchain_name(), nameList);
            }
        }
        Random random = new Random();
        List<ChannelBlockchain> result = new ArrayList<ChannelBlockchain>();
        for (List<ChannelBlockchain> value : map.values()) {
            if (value.size() == 1) {
                result.addAll(value);
            } else {
                int randIndex = random.nextInt(value.size());// 随机抽取一个,减1即为索引
                result.add(value.get(randIndex));
            }
        }
        return result;
    }
 
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
 
    public void setLogService(LogService logService) {
        this.logService = logService;
    }
 
    public void setSecUserService(SecUserService secUserService) {
        this.secUserService = secUserService;
    }
 
    public void setPasswordEncoder(PasswordEncoder passwordEncoder) {
        this.passwordEncoder = passwordEncoder;
    }
 
    public void setGoogleAuthService(GoogleAuthService googleAuthService) {
        this.googleAuthService = googleAuthService;
    }
 
}