1
zj
2025-03-20 697c08d94a3c26aaa970c467775989bb548fb6c2
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
package project.web.admin.impl.user;
 
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
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.log.Log;
import project.log.LogService;
import project.party.PartyService;
import project.party.model.Party;
import project.party.model.UserRecom;
import project.party.recom.UserRecomService;
import project.user.Agent;
import project.user.UserDataService;
import project.web.admin.service.user.AdminAgentService;
import project.web.admin.service.user.AdminUserRecomService;
import security.SecUser;
import security.internal.SecUserService;
 
public class AdminUserRecomServiceImpl implements AdminUserRecomService {
    
    protected LogService logService;
    
    protected PartyService partyService;
 
    protected PagedQueryDao pagedQueryDao;
 
    protected SecUserService secUserService;
    
    protected UserDataService userDataService;
    
    protected PasswordEncoder passwordEncoder;
    
    protected UserRecomService userRecomService;
    
    protected AdminAgentService adminAgentService;
    
    @Override
    public Page pagedQuery(int pageNo, int pageSize, String usernameOrUid,String parentUsername,String loginPartyId) {
        StringBuffer queryString = new StringBuffer(" SELECT ");
        queryString.append(" recom.UUID id ,info.USERNAME username,info.UUID partyId,info.ROLENAME rolename ,info.USERCODE usercode,parent_info.USERNAME parent_username ");
        queryString.append(" FROM ");
        queryString.append(" PAT_PARTY info  LEFT JOIN PAT_USER_RECOM recom  ON info.UUID =   recom.PARTY_ID ");
        queryString.append(" LEFT JOIN PAT_PARTY parent_info ON recom.RECO_ID = parent_info.UUID ");
        queryString.append(" where 1=1 ");
        Map<String, Object> parameters = new HashMap<String, Object>();
        
        
        if (!StringUtils.isNullOrEmpty(loginPartyId)) {
            List<String> children = this.userRecomService.findChildren(loginPartyId);
            if (children.size() == 0) {
                return new Page();
            }
            queryString.append(" and info.UUID in (:children) ");
            parameters.put("children", children);
        }
 
        if (!StringUtils.isNullOrEmpty(usernameOrUid)) {
            queryString.append("AND (info.USERNAME like:username OR info.USERCODE like:username ) ");
            parameters.put("username","%"+usernameOrUid+"%");
        }
        if (!StringUtils.isNullOrEmpty(parentUsername)) {
            queryString.append("AND parent_info.USERNAME like:parentUsername ");
            parameters.put("parentUsername","%"+parentUsername+"%");
        }
        queryString.append("AND info.ROLENAME !=:no_rolename ");
        parameters.put("no_rolename",Constants.SECURITY_ROLE_TEST);
        queryString.append(" order by info.CREATE_TIME desc ");
     
        return pagedQueryDao.pagedQuerySQL(pageNo, pageSize, queryString.toString(), parameters);
    }
 
    public void setPagedQueryDao(PagedQueryDao pagedQueryDao) {
        this.pagedQueryDao = pagedQueryDao;
    }
 
    @Override
    public UserRecom get(String id) {
        return ApplicationUtil.executeGet(id,UserRecom.class);
    }
    
    /**
     * 验证登录人资金密码
     * @param operatorUsername
     * @param loginSafeword
     */
    protected void checkLoginSafeword(String operatorUsername,String loginSafeword) {
        SecUser sec = this.secUserService.findUserByLoginName(operatorUsername);
        String sysSafeword = sec.getSafeword();
        String safeword_md5 = passwordEncoder.encodePassword(loginSafeword, operatorUsername);
        if (!safeword_md5.equals(sysSafeword)) {
            throw new BusinessException("登录人资金密码错误");
        }
    }
 
    @Override
    public void update(String partyId, String reco_username,String operator_name,String ip,String loginSafeword) {
        checkLoginSafeword(operator_name,loginSafeword);
        SecUser SecUser =  secUserService.findUserByLoginName(reco_username);
        if (SecUser == null || StringUtils.isNullOrEmpty(SecUser.getPartyId())) {
            throw new BusinessException("无法找到推荐用户");
        }
        if(partyId.equals(SecUser.getPartyId()) ) {
            throw new BusinessException("推荐人UID错误");
        }
        UserRecom userRecom = findByPartyId(partyId);
        String user_last = null;
        if(userRecom == null) {
            userRecom = new UserRecom();
            userRecom.setPartyId(partyId);
            userRecom.setReco_id(SecUser.getPartyId());
            this.userRecomService.save(userRecom);
        }else {
            user_last = userRecom.getReco_id().toString();
             userRecom.setReco_id(SecUser.getPartyId());
             this.userRecomService.update(partyId,SecUser.getPartyId());
        }
        SecUser SecUser_user =  secUserService.findUserByPartyId(partyId);
        Party party_parent = this.partyService.cachePartyBy(SecUser.getPartyId(),true);
        Party party_user = this.partyService.cachePartyBy(SecUser_user.getPartyId(),true);
        /**
         * 如果2个都是代理商,则同时修改代理商关系表
         */
        if((Constants.SECURITY_ROLE_AGENT.equals(party_user.getRolename()) ||Constants.SECURITY_ROLE_AGENTLOW.equals(party_user.getRolename()) )
                && (Constants.SECURITY_ROLE_AGENT.equals(party_parent.getRolename())||Constants.SECURITY_ROLE_AGENTLOW.equals(party_parent.getRolename()))) {
            Agent agent = this.adminAgentService.findByPartyId(party_user.getId());
            agent.setParent_partyId(party_parent.getId());
            this.adminAgentService.update(agent);
        }
        String username_user = SecUser_user.getUsername();
        
        /**
         * 前推荐人
         */
        String username_last = null;
        if(user_last != null) {
            SecUser SecUser_last =  secUserService.findUserByPartyId(user_last);
            username_last = SecUser_last.getUsername();
        }else {
            username_last="无";
        }
        Log log = new Log();
        log.setUsername(username_user);
        log.setCategory(Constants.LOG_CATEGORY_OPERATION);
        log.setOperator(operator_name);
        log.setLog("ip:["+ip+"],修改推荐关系,"
                + "原推荐人[" +username_last + "],"
                        + "修改后的推荐人[" +SecUser.getUsername() + "]");
 
        logService.saveSync(log);
        this.userDataService.saveRegister(partyId);
    }
    
    public UserRecom findById(Serializable id) {
        return ApplicationUtil.executeGet(id,UserRecom.class);
    }
    
    protected UserRecom findByPartyId(String partyId) {
        List<UserRecom> list=ApplicationUtil.executeSelect(UserRecom.class,"WHERE PARTY_ID=?",new Object[] {partyId});
        return list.size()<=0?null:list.get(0);
    }
 
    public void setSecUserService(SecUserService secUserService) {
        this.secUserService = secUserService;
    }
 
    public void setUserDataService(UserDataService userDataService) {
        this.userDataService = userDataService;
    }
 
    public void setLogService(LogService logService) {
        this.logService = logService;
    }
 
    public void setUserRecomService(UserRecomService userRecomService) {
        this.userRecomService = userRecomService;
    }
 
    public void setAdminAgentService(AdminAgentService adminAgentService) {
        this.adminAgentService = adminAgentService;
    }
 
    public void setPartyService(PartyService partyService) {
        this.partyService = partyService;
    }
 
    public void setPasswordEncoder(PasswordEncoder passwordEncoder) {
        this.passwordEncoder = passwordEncoder;
    }
}