zj
2025-07-14 5ab1a38d6fb2532b26311a4e2f9fd5120227a303
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
package project.web.admin.impl.user;
 
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.providers.encoding.PasswordEncoder;
 
import kernel.exception.BusinessException;
import kernel.util.StringUtils;
import kernel.web.ApplicationUtil;
import kernel.web.Page;
import kernel.web.PagedQueryDao;
import project.Constants;
import project.party.PartyService;
import project.party.model.Party;
import project.party.model.UserRecom;
import project.party.recom.UserRecomService;
import project.syspara.Syspara;
import project.syspara.SysparaService;
import project.user.Agent;
import project.user.QRGenerateService;
import project.wallet.Wallet;
import project.wallet.WalletService;
import project.web.admin.service.user.AdminAgentService;
import project.web.admin.service.user.AgentNodes;
import security.Role;
import security.RoleService;
import security.SecUser;
import security.internal.SecUserService;
 
public class AdminAgentServiceImpl implements AdminAgentService {
    
    private RoleService roleService;
    
    private PartyService partyService;
    
    private PagedQueryDao pagedQueryDao;
    
    private WalletService walletService;
    
    private SecUserService secUserService;
    
    private SysparaService sysparaService;
    
    private PasswordEncoder passwordEncoder;
    
    private UserRecomService userRecomService;
    
    private QRGenerateService qRGenerateService;
    
    private static final Logger logger=LoggerFactory.getLogger(AdminAgentServiceImpl.class);
 
    @Override
    public Page pagedQuery(int pageNo, int pageSize, String name_para, String checkedPartyId) {
 
        StringBuffer queryString = new StringBuffer("SELECT agent.UUID id,party.NAME name,party.USERNAME username,party.USERCODE usercode,party.ROLENAME rolename,party.LOGINAUTHORITY login_authority,party.USERCODE usercode,party.REMARKS remarks,party_parent.NAME name_parent,party_parent.USERNAME username_parent ");
        queryString.append(" FROM T_AGENT agent LEFT JOIN PAT_PARTY party ON agent.PARTY_ID = party.UUID  LEFT JOIN PAT_PARTY party_parent ON agent.PARENT_PARTY_ID = party_parent.UUID  WHERE 1 = 1 ");
 
        Map<String, Object> parameters = new HashMap<String, Object>();
        if (!StringUtils.isNullOrEmpty(name_para)) {
            queryString.append("AND (party.USERNAME like:username OR party.USERCODE like:username ) ");
            parameters.put("username","%"+name_para+"%");
        }
        if (!StringUtils.isNullOrEmpty(checkedPartyId)) {
 
            List<String> checked_list = this.userRecomService.findChildren(checkedPartyId);
            checked_list.add(checkedPartyId);
            if (checked_list.size() == 0) {
                return new Page();
            }
            queryString.append(" and   party.UUID in(:checked_list)");
            parameters.put("checked_list", checked_list);
        }
 
        queryString.append(" order by party.CREATE_TIME desc ");
        Page page = this.pagedQueryDao.pagedQuerySQL(pageNo, pageSize, queryString.toString(), parameters);
        return page;
    }
    
    public Page pagedQueryNetwork(int pageNo, int pageSize, String loginPartyId,String roleName, String usernameOrUid, String targetPartyId) {
        Page page = getPageList(pageNo, pageSize, loginPartyId, usernameOrUid, roleName,targetPartyId);// 获取当前页的用户相关
        /**
         * 页面查询第一层partyId级
         */
        List<String> list_partyId = new ArrayList<String>();
 
        for (int i = 0; i < page.getElements().size(); i++) {
            Map<String, Object> map_party = (Map<String, Object>) page.getElements().get(i);
            list_partyId.add(map_party.get("partyId").toString());
        }
        List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
 
        for (int i = 0; i < list_partyId.size(); i++) {
            int reco_agent = 0;
            /**
             * 所有子集
             */
            List<String> children_all = this.userRecomService.findChildren(list_partyId.get(i));
            /**
             * 正式用户
             */
            List<String> children_member = new ArrayList<>();
            for (int j = 0; j < children_all.size(); j++) {
                String partyId = children_all.get(j);
                Party party = partyService.cachePartyBy(partyId,true);
                if (Constants.SECURITY_ROLE_AGENT.equals(party.getRolename())||Constants.SECURITY_ROLE_AGENTLOW.equals(party.getRolename())) {
                    reco_agent++;
                } else if (Constants.SECURITY_ROLE_MEMBER.equals(party.getRolename())) {
                    children_member.add(partyId);
                }
            }
            
            Map<String, Object> item_result = new HashMap<String, Object>() ;
            
            Party party = partyService.cachePartyBy(list_partyId.get(i),false);
            
            UserRecom userRecom = this.userRecomService.findByPartyId(party.getId());
            if(userRecom != null && !"".equals(userRecom.getReco_id()) ) {
                Party party_parent = partyService.cachePartyBy(userRecom.getReco_id(),true);
                item_result.put("username_parent", party_parent.getUsername());
            }
            Agent agent = this.findByPartyId(party.getId());
            item_result.put("reco_agent", reco_agent);
            item_result.put("reco_member", children_member.size());
            item_result.put("partyId", list_partyId.get(i));
            
            item_result.put("username", party.getUsername());
            item_result.put("usercode", party.getUsercode());
            item_result.put("remarks", party.getRemarks());
            if(agent != null) {
                item_result.put("id", agent.getId());
            }
            
            result.add(item_result);
        }
        
        Page page_result = Page.EMPTY_PAGE;
        page_result.setElements(result);
        return page_result;
    }
    
    private Page getPageList(int pageNo, int pageSize,String loginPartyId,String roleName, String usernameOrUid, String targetPartyId) {
        Map<String, Object> parameters = new HashMap<String, Object>();
        StringBuffer queryString = new StringBuffer();
        queryString.append("SELECT party.ROLENAME AS rolename,party.USERNAME AS username,party.USERCODE AS UID,party.UUID AS partyId ");// 用户
        queryString.append("FROM PAT_PARTY party ");
        queryString.append("LEFT JOIN PAT_USER_RECOM ur ON party.UUID = ur.PARTY_ID ");// 推荐人 根目录判定
        queryString.append("WHERE 1=1 ");
        queryString.append("AND party.ROLENAME IN('" + Constants.SECURITY_ROLE_AGENT + "','"+Constants.SECURITY_ROLE_AGENTLOW+"') ");
        if (!StringUtils.isNullOrEmpty(loginPartyId)) {
            List children = this.userRecomService.findChildren(loginPartyId);
            if (children.size() == 0) {
                return new Page();
            }
            queryString.append(" and party.UUID in (:children) ");
            parameters.put("children", children);
        }
        if (!StringUtils.isNullOrEmpty(targetPartyId)) {
            List children = this.userRecomService.findRecomsToPartyId(targetPartyId);
            if (children.size() == 0) {
                return new Page();
            }
            queryString.append(" and party.UUID in (:children) ");
            parameters.put("children", children);
        }
        if (StringUtils.isNullOrEmpty(targetPartyId) && StringUtils.isNullOrEmpty(usernameOrUid)) {// 目标partyId为空
                                                                                                    // ,username参数为空,的情况下,如果是视图,显示根目录
            queryString.append(" and ur.RECO_ID is NULL ");
        }
        if (!StringUtils.isNullOrEmpty(usernameOrUid)) {
            queryString.append("AND (party.USERNAME like:username OR party.USERCODE like:username ) ");
            parameters.put("username", "%" + usernameOrUid + "%");
        }
        queryString.append("ORDER BY party.USERCODE ASC ");
        Page page = this.pagedQueryDao.pagedQuerySQL(pageNo, pageSize, queryString.toString(), parameters);
        return page;
    }
 
    
    public List<AgentNodes> findAgentNodes(String loginPartyId, String checkedPartyId, String url) {
        Map<String, String> map_checked = new HashMap();
 
        List<UserRecom> parents = userRecomService.getParents(checkedPartyId);
        for (int i = 0; i < parents.size(); i++) {
            UserRecom userRecom = parents.get(i);
            map_checked.put(userRecom.getReco_id().toString(), userRecom.getReco_id().toString());
        }
 
        map_checked.put(checkedPartyId, checkedPartyId);
        List result = new ArrayList();
        List list = new ArrayList();
        AgentNodes root = new AgentNodes();
        root.setHref(url);
        List<Agent> party_list;
        if (StringUtils.isNullOrEmpty(loginPartyId)) {// " FROM Party WHERE reco_id IS NULL or reco_id = '' and
            party_list = ApplicationUtil.executeSelect(Agent.class,"WHERE PARENT_PARTY_ID IS NULL OR PARENT_PARTY_ID=''",null);
            root.setText("所有代理商");
        } else {
            List recom_list = userRecomService.findRecoms(loginPartyId);
            /**
             * 将自己放入列表中
             */
            Party party = this.partyService.cachePartyBy(loginPartyId,true);
            Agent agent=findByPartyId(party.getId());
            party_list = new ArrayList();
            party_list.add(agent);
            /**
             * 将子代理放入列表中
             */
            for (int i = 0; i < recom_list.size(); i++) {
                 party = this.partyService.cachePartyBy(((UserRecom) recom_list.get(i)).getPartyId(),true);
                agent=findByPartyId(party.getId());
                if(agent != null)
                    party_list.add(agent);
            }
            root.setText("线下代理商");
        }
 
        for (int i = 0; i < party_list.size(); i++) {
            Agent agent = (Agent) party_list.get(i);
            AgentNodes nodes = new AgentNodes();
            nodes.setTags(agent.getPartyId().toString());
            nodes.setHref(url + "?partyId=" + agent.getPartyId().toString());
            logger.info("-----------------agent----------"+ agent.getPartyId());
            Party party = this.partyService.cachePartyBy(agent.getPartyId(),true);
            logger.info("-----------------party----------"+party.toString());
            String username = party.getUsername();
            String name = party.getName();
            if ((!StringUtils.isNullOrEmpty(loginPartyId)) && (!StringUtils.isNullOrEmpty(username))
                    && (username.length() == 11)) {
                username = username.substring(0, 3) + "****" + username.substring(7, 11);
            }
 
            Map state = new HashMap();
            if (map_checked.get(agent.getId().toString()) != null) {
                state.put("checked", Boolean.valueOf(true));
                state.put("expanded", Boolean.valueOf(true));
                root.setState(state);
            }
            
            nodes.setText(username);
            findAgentNodesLoop(loginPartyId, nodes, map_checked, url);
            list.add(nodes);
        }
        root.setNodes(list);
        result.add(root);
        return result;
    }
 
    public void findAgentNodesLoop(String loginPartyId, AgentNodes nodes, Map<String, String> map_checked, String url) {
        List recom_list = userRecomService.findRecoms(nodes.getTags());
        List list = new ArrayList();
        for (int i = 0; i < recom_list.size(); i++) { // findPartyByCache
            Party party = this.partyService.cachePartyBy(((UserRecom) recom_list.get(i)).getPartyId(),true);
            /**
             * 如果rolename不是代理商则不添加
             * 
             */
            if(party == null) {
                continue;
            }
            
            if(!Constants.SECURITY_ROLE_AGENT.equals(party.getRolename())||!Constants.SECURITY_ROLE_AGENTLOW.equals(party.getRolename())) {
                continue;
            }
            AgentNodes children_nodes = new AgentNodes();
            children_nodes.setTags(party.getId().toString());
            children_nodes.setHref(url + "?partyId=" + party.getId().toString());
            children_nodes.setText( party.getUsername() );
            Map state = new HashMap();
            if (map_checked.get(party.getId().toString()) != null) {
                state.put("checked", Boolean.valueOf(true));
                state.put("expanded", Boolean.valueOf(true));
                children_nodes.setState(state);
            }
            list.add(children_nodes);
            findAgentNodesLoop(loginPartyId, children_nodes, map_checked, url);
        }
        nodes.setNodes(list);
    }
 
    @Override
    public void save(String name, String username, String password, boolean login_authority,String remarks, String parents_usercode,boolean opera_authority) {
        username = username.trim();
        password = password.trim();
        
        if (secUserService.findUserByLoginName(username) != null) {
            throw new BusinessException("用户名重复");
        }
        /**
         * 用户code
         */
        String usercode = getUsercode();
        
        if (!StringUtils.isNullOrEmpty(parents_usercode)) {
            Party party_parents=partyService.findPartyByUsercode(parents_usercode);
            if (party_parents==null ) {
            throw new BusinessException("推荐码不正确");
            }
            if (!Constants.SECURITY_ROLE_AGENT.equals(party_parents.getRolename()) && !Constants.SECURITY_ROLE_AGENTLOW.equals(party_parents.getRolename())) {
                throw new BusinessException("推荐码不正确");
            }
        }
 
        /**
         * party
         */
        Party party = new Party();
        party.setUsername(username);
        party.setUsercode(usercode);
        party.setName(name);
        party.setLogin_authority(login_authority);
        party.setRemarks(remarks);
        party.setSafeword(passwordEncoder.encodePassword("000000", party.getUsername()));
 
        party.setRolename(opera_authority?Constants.SECURITY_ROLE_AGENT:Constants.SECURITY_ROLE_AGENTLOW);
 
        party = partyService.save(party);
        
        if (!StringUtils.isNullOrEmpty(parents_usercode)) {
            Party party_parents=partyService.findPartyByUsercode(parents_usercode);
            
            if (party_parents==null ) {
                throw new BusinessException("推荐码不正确");
                }
                if (!Constants.SECURITY_ROLE_AGENT.equals(party_parents.getRolename()) && !Constants.SECURITY_ROLE_AGENTLOW.equals(party_parents.getRolename())) {
                    throw new BusinessException("推荐码不正确");
                }
            UserRecom userRecom = new UserRecom();
            userRecom.setPartyId(party.getId());
            userRecom.setReco_id(party_parents.getId().toString());// 父类partyId
            this.userRecomService.save(userRecom);
        }
        /**
         * SecUser
         */
        Role role = this.roleService.findRoleByName(opera_authority?Constants.SECURITY_ROLE_AGENT:Constants.SECURITY_ROLE_AGENTLOW);
 
        SecUser secUser = new SecUser();
        secUser.setPartyId(String.valueOf(party.getId()));
        secUser.getRoles().add(role);
 
        secUser.setUsername(username);
        secUser.setPassword(password);
        secUser.setEnabled(login_authority);
        secUser.setSafeword(passwordEncoder.encodePassword("000000", party.getUsername()));
 
        this.secUserService.saveUser(secUser);
 
        /**
         * usdt账户
         */
        Wallet wallet = new Wallet();
        wallet.setPartyId(party.getId().toString());
        this.walletService.save(wallet);
        /**
         * 5个币账户
         */
        Agent  agent=new Agent();
        agent.setPartyId(party.getId());
        if (!StringUtils.isNullOrEmpty(parents_usercode)) {
            Party party_parents=partyService.findPartyByUsercode(parents_usercode);
            agent.setParent_partyId(party_parents.getId().toString());
        }
        
        ApplicationUtil.executeInsert(agent);
    }
 
    private String getUsercode() {
        Syspara syspara = sysparaService.find("agent_uid_sequence");
        int random = (int) (Math.random() * 3 + 1);
        int user_uid_sequence = syspara.getInteger() + random;
        syspara.setValue(user_uid_sequence);
        sysparaService.update(syspara);
 
        String usercode = String.valueOf(user_uid_sequence);
        return usercode;
    }
    
    public Agent findByPartyId(Serializable partyId) {
        List<Agent> list = ApplicationUtil.executeSelect(Agent.class,"WHERE PARTY_ID=?",new Object[] {partyId});
        return list.size()<=0?null:list.get(0);
    }
 
    @Override
    public Agent get(String id) {
        return ApplicationUtil.executeGet(id,Agent.class);
    }
    
    @Override
    public void update(String id, String name, boolean login_authority, String remarks,boolean opera_authority) {
        Agent agent=    this.get(id);
        Party party = this.partyService.cachePartyBy(agent.getPartyId(),false);
        party.setRolename(opera_authority?Constants.SECURITY_ROLE_AGENT:Constants.SECURITY_ROLE_AGENTLOW);
        party.setName(name);
        party.setRemarks(remarks);
        party.setLogin_authority(login_authority);
        this.partyService.update(party);
        
        SecUser secUser=    secUserService.findUserByPartyId(agent.getPartyId());
        Role role = this.roleService.findRoleByName(opera_authority?Constants.SECURITY_ROLE_AGENT:Constants.SECURITY_ROLE_AGENTLOW);
 
        Set<Role> roles = secUser.getRoles();
        roles.clear();
        roles.add(role);
        secUser.setRoles(roles);
        secUser.setEnabled(login_authority);
 
        this.secUserService.update(secUser);
        
    }
    @Override
    public void update(Agent agent) {
        ApplicationUtil.executeUpdate(agent);
    }
 
    public void setPagedQueryDao(PagedQueryDao pagedQueryDao) {
        this.pagedQueryDao = pagedQueryDao;
    }
 
    public void setUserRecomService(UserRecomService userRecomService) {
        this.userRecomService = userRecomService;
    }
 
    public void setWalletService(WalletService walletService) {
        this.walletService = walletService;
    }
 
    public void setPartyService(PartyService partyService) {
        this.partyService = partyService;
    }
 
    public void setSecUserService(SecUserService secUserService) {
        this.secUserService = secUserService;
    }
 
    public void setRoleService(RoleService roleService) {
        this.roleService = roleService;
    }
 
    public class PosterThread implements Runnable {
        String image_name;
        String usercode;
 
        public void run() {
            try {
                qRGenerateService.generate_poster(image_name, usercode);
            } catch (Exception e) {
                logger.error("error:", e);
            }
 
        }
 
        public PosterThread(String image_name, String usercode) {
            this.image_name = image_name;
            this.usercode = usercode;
        }
 
    }
 
    public void setqRGenerateService(QRGenerateService qRGenerateService) {
        this.qRGenerateService = qRGenerateService;
    }
 
    public void setSysparaService(SysparaService sysparaService) {
        this.sysparaService = sysparaService;
    }
 
 
 
    public void setPasswordEncoder(PasswordEncoder passwordEncoder) {
        this.passwordEncoder = passwordEncoder;
    }
    
 
    
 
 
}