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
package com.yiboshi.science.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yiboshi.arch.exception.BusinessException;
import com.yiboshi.science.base.BaseServiceImpl;
import com.yiboshi.science.base.Pagination;
import com.yiboshi.science.config.security.SecurityUserHolder;
import com.yiboshi.science.dao.SystemUserDAO;
import com.yiboshi.science.entity.SystemUser;
import com.yiboshi.science.param.dto.ComPersonDTO;
import com.yiboshi.science.param.dto.SystemUserDTO;
import com.yiboshi.science.param.dto.UserDTO;
import com.yiboshi.science.param.query.SystemUserQueryVO;
import com.yiboshi.science.service.SystemUserRoleService;
import com.yiboshi.science.service.SystemUserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Objects;
/**
* 用户信息表 Service 实现类
*
* @author lkl
* @version 2021-08-26
*/
@Service
public class SystemUserServiceImpl extends BaseServiceImpl<SystemUserDAO, SystemUserQueryVO, SystemUserDTO, SystemUser> implements SystemUserService {
@Autowired
private SystemUserDAO systemUserDAO;
@Autowired
private SystemUserRoleService systemUserRoleService;
@Override
protected void setCriteriaForQuery(SystemUserQueryVO vo, QueryWrapper<SystemUserQueryVO> criteria) {
if (Objects.nonNull(vo.getUsername())) {
criteria.like("username", vo.getUsername());
}
if (Objects.nonNull(vo.getPassword())) {
criteria.eq("password", vo.getPassword());
}
if (Objects.nonNull(vo.getLoginName())) {
criteria.eq("login_name", vo.getLoginName());
}
if (Objects.nonNull(vo.getPersonId())) {
criteria.eq("user_id", vo.getPersonId());
}
if (Objects.nonNull(vo.getUserType())) {
criteria.eq("user_type", vo.getUserType());
}
if (Objects.nonNull(vo.getNoteState())) {
criteria.eq("note_state", vo.getNoteState());
}
if (Objects.nonNull(vo.getRoleId())) {
criteria.eq("role_id", vo.getRoleId());
}
if (Objects.nonNull(vo.getPersonName())) {
criteria.like("e.person_name", vo.getPersonName());
}
if (Objects.nonNull(vo.getCertId())) {
criteria.like("e.cert_id", vo.getCertId());
}
if (Objects.nonNull(vo.getSex())) {
criteria.eq("sex", vo.getSex());
}
if (Objects.nonNull(vo.getMobile())) {
criteria.like("e.mobile", vo.getMobile());
}
}
@Override
public Pagination<SystemUserDTO> getListByPage(SystemUserQueryVO vo) {
QueryWrapper criteria = new QueryWrapper();
setCriteriaForQuery(vo, criteria);
Page<SystemUserQueryVO> page = new Page<>(vo.getPageIndex(), vo.getPageSize());
List<SystemUserDTO> dtoList = systemUserDAO.getListByPage(page, criteria).getRecords();
return new Pagination<>(dtoList, page.getTotal(), vo.getPageSize());
}
public SystemUserDTO findByName(String username) {
SystemUserDTO systemUser = systemUserDAO.findByName(username);
if (systemUser == null)
throw new UsernameNotFoundException(String.format("No user found with username '%s'.", username));
systemUser.setRoles(systemUserRoleService.getRolesByUserId(systemUser.getId()));
return systemUser;
}
public void updateRoleById(String userId, String id, String roleId) {
systemUserRoleService.updateRoleByUserId(userId, id, roleId);
}
public String updateStateById(String id, Integer noteState) {
SystemUser systemUser = new SystemUser();
systemUser.setId(id);
systemUser.setNoteState(noteState);
id = this.update(systemUser);
return id;
}
@Transactional
public SystemUser CreateUser(String username, String password, String userId, String roleId, Integer noteState) {
SystemUser systemUser = new SystemUser();
if (!Objects.nonNull(username) || username.equals(""))
systemUser.setUsername(systemUserDAO.getMaxUsername("1521"));
else {
if (this.isExist(username))
throw new BusinessException("该用户名已注册,请更改后再试!");
systemUser.setUsername(username);
}
systemUser.setPassword(password);
systemUser.setPersonId(userId);
systemUser.setNoteState(noteState);
String id = this.insert(systemUser);
systemUserRoleService.addRoleByUserId(id, roleId);
return systemUser;
}
public String updatePwd(UserDTO user) {
if (Objects.isNull(user.getUserId()) || !user.getUserId().equals(SecurityUserHolder.getUserId()))
throw new BusinessException("修改失败,请刷新页面后重试");
SystemUser systemUser = this.getById(user.getUserId());
if (null == systemUser)
throw new BusinessException("系统错误,请稍后再试");
if (!systemUser.getPassword().equals(user.getOldPassword()))
throw new BusinessException("原密码错误,请检查");
SystemUser newUser = new SystemUser();
newUser.setId(systemUser.getId());
newUser.setPassword(user.getNewPassword());
return this.update(newUser);
}
public SystemUser getByCode(String personCode) {
SystemUser systemUser = new SystemUser();
systemUser.setPersonCode(personCode);
return this.getEntity(systemUser);
}
public Boolean isExist(String username) {
Boolean state = false;
if (Objects.nonNull(username) && !username.equals("")) {
SystemUser systemUser = systemUserDAO.getByUserName(username);
if (null != systemUser) {
state = true;
}
}
return state;
}
public String resetPwd(ComPersonDTO dto) {
SystemUser systemUser = this.getById(dto.getId());
if (null == systemUser)
throw new BusinessException("用户不存在,请联系管理员");
String pwd = "kycz123456";
systemUser = new SystemUser();
systemUser.setId(dto.getId());
systemUser.setPassword(pwd);
return this.update(systemUser);
}
public Boolean checkUserNameById(String username, String id) {
Boolean state = false;
SystemUser systemUser = getSystemUserByUsername(username);
if (null != systemUser) {
if (Objects.nonNull(id) && !id.equals("")) {
SystemUser user = this.getById(id);
if (null != user && !systemUser.getId().equals(user.getId()))
state = true;
} else {
state = true;
}
}
return state;
}
public SystemUser getSystemUserByUsername(String username) {
if (username == null || username.equals(""))
return null;
SystemUser systemUser = new SystemUser();
systemUser.setUsername(username);
systemUser = this.getEntity(systemUser);
if (null != systemUser)
return systemUser;
else
return null;
}
public SystemUserDTO getSystemUserById(String id) {
return systemUserDAO.getSystemUserById(id);
}
public SystemUser getByPersonId(String personId) {
SystemUser systemUser = new SystemUser();
systemUser.setPersonId(personId);
systemUser = this.getEntity(systemUser);
return systemUser;
}
public String updateSystemUser(SystemUserDTO dto) {
SystemUser user = this.convert2Entity(dto);
String r = StringUtils.join(dto.getRole().toArray(), ",");
List<String> roles = systemUserRoleService.getRolesListByUserId(dto.getId());
for (String e : roles) {
if (!r.contains(e)) {
systemUserRoleService.deleteRoleByUserId(dto.getId(), e);
}
}
String s = StringUtils.join(roles.toArray(), ",");
for (String e : dto.getRole()) {
if (!s.contains(e)) {
systemUserRoleService.addRoleByUserId(dto.getId(), e);
}
}
return this.update(user);
}
}