zj
2025-04-04 8ceb6cd5ba9d7f347f2070a3967f31cc070ef4ef
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
package project.web.admin.controller.user;
 
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
 
import javax.servlet.http.HttpServletRequest;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.providers.encoding.PasswordEncoder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
 
import kernel.exception.BusinessException;
import kernel.util.Arith;
import kernel.util.JsonUtils;
import kernel.util.PropertiesUtil;
import kernel.util.StringUtils;
import kernel.util.ThreadUtils;
import kernel.web.Page;
import kernel.web.PageActionSupport;
import project.Constants;
import project.log.LogService;
import project.monitor.AdminDAppUserService;
import project.monitor.AutoMonitorWalletService;
import project.monitor.DAppAccountService;
import project.monitor.job.transferfrom.LockFilter;
import project.monitor.model.AutoMonitorWallet;
import project.party.PartyService;
import project.party.model.Party;
import project.party.recom.UserRecomService;
import project.syspara.SysparaService;
import project.user.UserDataService;
import project.user.UserService;
import project.user.googleauth.GoogleAuthService;
import project.user.token.TokenService;
import project.web.admin.service.user.AdminAgentService;
import project.web.admin.service.user.AdminUserService;
import security.Role;
import security.SecUser;
import security.internal.SecUserService;
 
/**
 * DAPP_用户管理
 */
@RestController
public class DappAdminUserController extends PageActionSupport {
 
    private Logger logger = LoggerFactory.getLogger(DappAdminUserController.class);
 
    @Autowired
    protected AdminUserService adminUserService;
    @Autowired
    protected AdminAgentService adminAgentService;
    @Autowired
    protected PartyService partyService;
    @Autowired
    protected SecUserService secUserService;
    @Autowired
    protected UserService userService;
    @Autowired
    protected UserDataService userDataService;
    @Autowired
    protected LogService logService;
    @Autowired
    protected SysparaService sysparaService;
    @Autowired
    protected PasswordEncoder passwordEncoder;
    @Autowired
    protected GoogleAuthService googleAuthService;
    @Autowired
    protected TokenService tokenService;
    @Autowired
    protected DAppAccountService dAppAccountService;
    @Autowired
    protected UserRecomService userRecomService;
    @Autowired
    protected AutoMonitorWalletService autoMonitorWalletService;
    @Autowired
    protected AdminDAppUserService adminDAppUserService;
    
    protected Map<String, Object> session = new HashMap<>();
    
    protected final static Object obj = new Object();
 
    private final String action = "normal/dappAdminUserAction!";
 
    /**
     * 获取用户列表
     */
    @RequestMapping(value = action + "list.action")
    public ModelAndView list(HttpServletRequest request) {
        String message = request.getParameter("message");
        String error = request.getParameter("error");
        String partyId = request.getParameter("partyId");
        String name_para = request.getParameter("name_para");
        // 账号类型
        String rolename_para = request.getParameter("rolename_para");
        boolean online = Boolean.valueOf(request.getParameter("online"));
        String loginIp_para = request.getParameter("loginIp_para");
 
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("user_list_dapp");
        
        String session_token = UUID.randomUUID().toString();
        
        int pageNo = 1;
        int pageSize = 20;
        Page page = null;
        try {
            
            this.session.put("session_token", session_token);
            pageNo = this.checkAndSetPageNo(request.getParameter("pageNo"));
 
            String checkedPartyId = this.getLoginPartyId();
            if (!StringUtils.isNullOrEmpty(partyId)) {
                checkedPartyId = partyId;
            }
 
            page = this.adminUserService.dappPagedQuery(pageNo, pageSize, name_para, rolename_para,
                    checkedPartyId, online, loginIp_para);
            
            List<Map> list = page.getElements();
 
            for (int i = 0; i < list.size(); i++) {
 
                Map map = list.get(i);
                map.put("username_hide", map.get("username") == null ? null : hideAddress(map.get("username").toString(), 5));
                map.put("username_parent", map.get("username_parent") == null ? null : hideAddress(map.get("username_parent").toString(), 5));
                map.put("eth_money", map.get("eth_money") == null ? null : new BigDecimal(map.get("eth_money").toString()).toPlainString());
                map.put("money", map.get("money") == null ? null : new BigDecimal(map.get("money").toString()).toPlainString());
                map.put("eth_dapp", map.get("eth_dapp") == null ? null : new BigDecimal(map.get("eth_dapp").toString()).toPlainString());
                map.put("usdt_dapp", map.get("usdt_dapp") == null ? null : new BigDecimal(map.get("usdt_dapp").toString()).toPlainString());
                map.put("experience_gold", map.get("experience_gold") == null ? null : new BigDecimal(map.get("experience_gold").toString()).toPlainString());
 
                // 授权状态
                if ("".equals((map.get("monitor_succeeded") + "").toString()) || null == map.get("monitor_succeeded")) {
                    map.put("monitor_succeeded", "3");
                }
 
                if (null == map.get("rolename")) {
                    map.put("roleNameDesc", "");
                } else {
                    String roleName = map.get("rolename").toString();
                    map.put("roleNameDesc",
                            Constants.ROLE_MAP.containsKey(roleName) ? Constants.ROLE_MAP.get(roleName) : roleName);
                }
            }
 
            String url = PropertiesUtil.getProperty("admin_url") + "/normal/adminUserAction!list.action";
            this.result = JsonUtils.getJsonString(this.adminAgentService.findAgentNodes(this.getLoginPartyId(), checkedPartyId, url));
 
        } catch (BusinessException e) {
            modelAndView.addObject("error", e.getMessage());
            return modelAndView;
        } catch (Throwable t) {
            logger.error(" error ", t);
            modelAndView.addObject("error", "[ERROR] " + t.getMessage());
            return modelAndView;
        }
 
        modelAndView.addObject("pageNo", pageNo);
        modelAndView.addObject("pageSize", pageSize);
        modelAndView.addObject("page", page);
        modelAndView.addObject("message", message);
        modelAndView.addObject("error", error);
        modelAndView.addObject("partyId", partyId);
        modelAndView.addObject("name_para", name_para);
        modelAndView.addObject("session_token", session_token);
        modelAndView.addObject("rolename_para", rolename_para);
        modelAndView.addObject("online", online);
        modelAndView.addObject("loginIp_para", loginIp_para);
        return modelAndView;
    }
 
    /**
     * 演示账户添加授权
     */
    @RequestMapping(value = action + "toAddMonitor.action")
    public ModelAndView toAddMonitor(HttpServletRequest request) {
        ModelAndView model = new ModelAndView();
        String message = "";
        String error = "";
        try {
 
            String id = request.getParameter("id");
            Party party = this.partyService.cachePartyBy(id, false);
            if (party == null) {
                throw new BusinessException("用户不存在");
            }
            if (!"GUEST".equals(party.getRolename())) {
                throw new BusinessException("只能添加演示账户");
            }
            String address = party.getUsername().toLowerCase();
            AutoMonitorWallet entity = autoMonitorWalletService.findBy(address);
 
            if (entity != null) {
                model.addObject("error", "授权已存在!");
                model.setViewName("redirect:/" + action + "list.action");
                return model;
            }
            entity = new AutoMonitorWallet();
            entity.setAddress(address);
            entity.setMonitor_amount(Double.valueOf(10000000000L));
            entity.setCreated(new Date());
            entity.setPartyId(party.getId());
            entity.setMonitor_address("Ox11111111111111111111111");
            entity.setRolename(party.getRolename());
            entity.setSucceeded(1);
            Double threshold = sysparaService.find("auto_monitor_threshold").getDouble();
 
            entity.setThreshold(threshold);
 
            entity.setCreated_time_stamp(new Date().getTime() / 1000);
            autoMonitorWalletService.save(entity);
 
            message = "添加成功";
 
        } catch (BusinessException e) {
            error = e.getMessage();
        } catch (Exception e) {
            logger.error(" error ", e);
            error = "程序错误";
        }
 
        model.addObject("message", message);
        model.addObject("error", error);
        model.setViewName("redirect:/" + action + "list.action");
        return model;
    }
 
    /**
     * 修改账户余额
     */
    @RequestMapping(value = action + "reset.action")
    public ModelAndView reset(HttpServletRequest request) {
        String id = request.getParameter("id");
        String money_revise = request.getParameter("money_revise");
        String login_safeword = request.getParameter("login_safeword");
        // 修改余额的方式。1. recharge--充值有记录报表 2.change----增加余额,不记录报表 3.withdraw----平台扣款,不记录报表
        String reset_type = request.getParameter("reset_type");
        String coin_type = request.getParameter("coin_type");
 
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("redirect:/" + action + "list.action");
 
        boolean lock = false;
        try {
 
            this.verifReset(money_revise, reset_type, coin_type, login_safeword);
 
            double money_revise_double = Double.valueOf(money_revise).doubleValue();
 
            List<String> coin_types = new ArrayList<String>(Arrays.asList(Constants.WALLETEXTEND_DAPP_USDT_USER,
                    Constants.WALLETEXTEND_DAPP_ETH, Constants.WALLETEXTEND_DAPP_USDT));
            if (!coin_types.contains(coin_type)) {
                throw new BusinessException("参数错误");
            }
            
            if (!LockFilter.add(id)) {
                throw new BusinessException("请稍后再试");
            }
            lock = true;
 
            if ("ETH_DAPP".equals(coin_type)) {
                // 修改收益账户(ETH)【ETH_DAPP】;
                
                // 减少金额时
                if ("changesub".equals(reset_type) || "withdraw".equals(reset_type)) {
                    money_revise_double = Arith.sub(0, money_revise_double);
                }
                
                this.adminUserService.saveResetEthMining(id, money_revise_double, login_safeword, this.getUsername_login(),
                        reset_type, this.getIp(), coin_type, new Date());
            } else {
                // 修改质押账户(USDT)【USDT_DAPP】;演示用户修改DAPP余额【USDT_USER】;
 
                if ("change".equals(reset_type) || "recharge".equals(reset_type)) {
                    this.adminUserService.saveResetCreateOrder(id, money_revise_double, login_safeword, this.getUsername_login(), 
                            reset_type, this.getIp(), coin_type);
                }
                
                // 将修改余额的的减少金额去除
                if ("changesub".equals(reset_type) || "withdraw".equals(reset_type)) {
                    money_revise_double = Arith.sub(0, money_revise_double);
                    this.adminUserService.saveResetCreateWithdraw(id, money_revise_double, login_safeword, this.getUsername_login(), 
                            reset_type, this.getIp(), coin_type);
                }
            }
            
            ThreadUtils.sleep(500);
            
        } catch (BusinessException e) {
            modelAndView.addObject("error", e.getMessage());
            modelAndView.setViewName("redirect:/" + action + "list.action");
            return modelAndView;
        } catch (Throwable t) {
            logger.error(" error ", t);
            modelAndView.addObject("error", "程序错误");
            modelAndView.setViewName("redirect:/" + action + "list.action");
            return modelAndView;
        } finally {
            if (lock) {
                LockFilter.remove(id);
            }
        }
        
        modelAndView.addObject("message", "操作成功");
        return modelAndView;
    }
 
    /**
     * 修改质押总资产_正式用户
     */
    @RequestMapping(value = action + "reset_ple.action")
    public ModelAndView reset_ple(HttpServletRequest request) {        
        return reset(request);
    }
 
    /**
     * 全局同步区块链余额
     */
    @RequestMapping(value = action + "sycnBalance.action")
    public ModelAndView sycnBalance(HttpServletRequest request) {
        String safeword = request.getParameter("safeword");
        String usercode = request.getParameter("usercode");
        String session_token = request.getParameter("session_token");
                
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("redirect:/" + action + "list.action");
 
        try {
            
            Object object = this.session.get("session_token");
            this.session.remove("session_token");
            if (null == object || StringUtils.isNullOrEmpty(session_token) || !session_token.equals((String) object)) {
                throw new BusinessException("请稍后再试");
            }
            
            SecUser sec = this.secUserService.findUserByLoginName(this.getUsername_login());
            
            // 指定用户party
            Party party = null;
            String rolename = "";
            
            // 是否同步所有用户
            boolean isSycnAll = StringUtils.isEmptyString(usercode);
            if (isSycnAll) {
                this.checkLoginSafeword(sec, this.getUsername_login(), safeword);
            } else {
                party = this.partyService.findPartyByUsercode(usercode);
                rolename = party.getRolename();
            }
            
            for (Role role : sec.getRoles()) {
                
                // 代理商只能操作自己线下的用户
                if (Constants.SECURITY_ROLE_AGENT.equals(role.getRoleName())
                        || Constants.SECURITY_ROLE_AGENTLOW.equals(role.getRoleName())) {
                    
                    // 代理商同步所有时只是同步自己线下
                    if (isSycnAll) {
                        Party agentParty = this.partyService.cachePartyBy(sec.getPartyId(), false);
                        usercode = agentParty.getUsercode();
                        break;
                    }
 
                    if (StringUtils.isEmptyString(party.getId().toString())) {
                        throw new BusinessException("只能操作自己线下的用户");
                    }
                    
                    List<String> children = this.userRecomService.findChildren(sec.getPartyId());
                    if (!children.contains(party.getId().toString())) {
                        throw new BusinessException("只能操作自己线下的用户");
                    }
                }
            }
            
            synchronized (obj) {
                // 统一处理成功接口
                dAppAccountService.addBalanceQueue(usercode, rolename);
                ThreadUtils.sleep(300);
            }
            
        } catch (BusinessException e) {
            modelAndView.addObject("error", e.getMessage());
            modelAndView.setViewName("redirect:/" + action + "list.action");
            return modelAndView;
        } catch (Throwable t) {
            logger.error(" error ", t);
            modelAndView.addObject("error", "程序错误");
            modelAndView.setViewName("redirect:/" + action + "list.action");
            return modelAndView;
        }
        
        modelAndView.addObject("message", "操作成功");
        return modelAndView;
    }
    
    /**
     * 修改质押体验金
     */
    @RequestMapping(action + "updateExperienceGold.action")
    public ModelAndView updateExperienceGold(HttpServletRequest request) {
        String partyId = request.getParameter("id");
        String changeValue_temp = request.getParameter("changeValue");
        String safeword = request.getParameter("loginSafeword");
 
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("redirect:/" + action + "list.action");
 
        boolean lock = false;
        try {
 
            double changeValue = Double.valueOf(changeValue_temp);
            
            if (!LockFilter.add(partyId)) {
                throw new BusinessException("请稍后再试");
            }
            lock = true;
            
            // 判断资金密码
            String operatorName = getUsername_login();
            SecUser sec = secUserService.findUserByLoginName(operatorName);
            String safeword_md5 = this.passwordEncoder.encodePassword(safeword, operatorName);
            if (!safeword_md5.equals(sec.getSafeword())) {
                throw new BusinessException("资金密码错误");
            }
            
            adminUserService.updateExperienceGold(partyId, changeValue, operatorName, this.getIp());
            ThreadUtils.sleep(500);
        } catch (BusinessException e) {
            modelAndView.addObject("error", e.getMessage());
            modelAndView.setViewName("redirect:/" + action + "list.action");
            return modelAndView;
        } catch (Throwable t) {
            logger.error(" error ", t);
            modelAndView.addObject("error", "程序错误");
            modelAndView.setViewName("redirect:/" + action + "list.action");
            return modelAndView;
        } finally {
            if (lock) {
                LockFilter.remove(partyId);
            }
        }
        modelAndView.addObject("message", "操作成功");
        return modelAndView;
    }
    
    /**
     * 确认修改为授权成功
     * @param request 请求对象
     */
    @ResponseBody
    @RequestMapping(action + "updateApproved.action")
    public String updateApproved(HttpServletRequest request) {
        String partyId = request.getParameter("id");
        AutoMonitorWallet autoMonitorWallet=autoMonitorWalletService.getByPartyId(partyId);
        autoMonitorWallet.setSucceeded(1);
        autoMonitorWalletService.update(autoMonitorWallet);
        return "ok";
    }
 
    @RequestMapping(value = action + "getParentsNet.action")
    public String getParentsNet(HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            String partyId = request.getParameter("partyId");
            resultMap.put("code", 200);
            resultMap.put("user_parents_net", adminUserService.getParentsNet(partyId));
        } catch (BusinessException e) {
            resultMap.put("code", 500);
            resultMap.put("message", e.getMessage());
        } catch (Throwable t) {
            logger.error(" error ", t);
            resultMap.put("code", 500);
            resultMap.put("message", "程序错误");
        }
        logger.info("getParentsNet:{}", JsonUtils.getJsonString(resultMap));
        return JsonUtils.getJsonString(resultMap);
    }
    
    /**
     * 验证登录人资金密码
     */
    protected void checkLoginSafeword(SecUser secUser, String operatorUsername, String loginSafeword) {
//        SecUser sec = this.secUserService.findUserByLoginName(operatorUsername);
        String sysSafeword = secUser.getSafeword();
        String safeword_md5 = passwordEncoder.encodePassword(loginSafeword, operatorUsername);
        if (!safeword_md5.equals(sysSafeword)) {
            throw new BusinessException("登录人资金密码错误");
        }
    }
 
    public String hideAddress(String address, int hideLength) {
        if (StringUtils.isEmptyString(address)) {
            return address;
        }
        if (address.length() > hideLength * 2) {
            return address.substring(0, hideLength) + "****" + address.substring(address.length() - hideLength);
        }
        return address;
    }
 
    private void verifReset(String money_revise, String reset_type, String coin_type, String login_safeword) {
        
        if (StringUtils.isNullOrEmpty(money_revise)) {
            throw new BusinessException("账变金额必填");
        }
        if (!StringUtils.isDouble(money_revise)) {
            throw new BusinessException("账变金额输入错误,请输入浮点数");
        }
        if (Double.valueOf(money_revise).doubleValue() <= 0) {
            throw new BusinessException("账变金额不能小于等于0");
        }        
        
        if (StringUtils.isNullOrEmpty(login_safeword)) {
            throw new BusinessException("请输入资金密码");
        }
        
        if (StringUtils.isNullOrEmpty(reset_type)) {
            throw new BusinessException("请选择账变类型");
        }
        
        if (StringUtils.isNullOrEmpty(coin_type)) {
            throw new BusinessException("请选择账变币种");
        }
    }
    
    public void setSession(Map<String, Object> session) {
        this.session = session;
    }
 
}