UserServiceImpl.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. package com.zoniot.ccrc.service.impl;
  2. import com.alibaba.fastjson.JSON;
  3. import com.github.pagehelper.PageHelper;
  4. import com.zoniot.ccrc.commom.exception.ServiceException;
  5. import com.zoniot.ccrc.commom.model.Pagination;
  6. import com.zoniot.ccrc.commom.utils.JwtTokenUtil;
  7. import com.zoniot.ccrc.commom.utils.RedisUtil;
  8. import com.zoniot.ccrc.commom.utils.UserUtil;
  9. import com.zoniot.ccrc.dao.SiteMapper;
  10. import com.zoniot.ccrc.dao.SiteUserMapper;
  11. import com.zoniot.ccrc.dao.UserMapper;
  12. import com.zoniot.ccrc.dao.UserRoleMapper;
  13. import com.zoniot.ccrc.dto.DeleteUserDto;
  14. import com.zoniot.ccrc.dto.LoginUser;
  15. import com.zoniot.ccrc.dto.UserDto;
  16. import com.zoniot.ccrc.dto.UserRoleDto;
  17. import com.zoniot.ccrc.entity.Site;
  18. import com.zoniot.ccrc.entity.SiteUser;
  19. import com.zoniot.ccrc.entity.User;
  20. import com.zoniot.ccrc.entity.UserRole;
  21. import com.zoniot.ccrc.security.mobile.MobileLoginUserDetailService;
  22. import com.zoniot.ccrc.service.IotService;
  23. import com.zoniot.ccrc.service.TokenService;
  24. import com.zoniot.ccrc.service.UserService;
  25. import lombok.extern.slf4j.Slf4j;
  26. import org.apache.commons.lang3.StringUtils;
  27. import org.springframework.beans.factory.annotation.Autowired;
  28. import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  29. import org.springframework.stereotype.Service;
  30. import org.springframework.transaction.annotation.Transactional;
  31. import javax.annotation.Resource;
  32. import java.math.BigInteger;
  33. import java.time.LocalDateTime;
  34. import java.util.ArrayList;
  35. import java.util.List;
  36. import static com.google.common.collect.Lists.newArrayList;
  37. @Slf4j
  38. @Service
  39. public class UserServiceImpl implements UserService {
  40. @Resource
  41. private UserMapper userMapper;
  42. //@Resource
  43. //private AuthenticationManager authenticationManager;
  44. @Resource
  45. private UserRoleMapper userRoleMapper;
  46. @Autowired
  47. private MobileLoginUserDetailService userDetailsService;
  48. @Autowired
  49. private JwtTokenUtil jwtTokenUtil;
  50. @Autowired
  51. private TokenService tokenService;
  52. @Autowired
  53. private IotService iotService;
  54. @Resource
  55. private SiteUserMapper siteUserMapper;
  56. @Resource
  57. private SiteMapper siteMapper;
  58. @Autowired
  59. private RedisUtil redisUtil;
  60. @Override
  61. public int insertSelective(User user) {
  62. return userMapper.insertSelective(user);
  63. }
  64. @Override
  65. public int update(User user) {
  66. return userMapper.update(user);
  67. }
  68. @Override
  69. public User findUserById(Integer id) {
  70. return userMapper.findUserById(id);
  71. }
  72. @Override
  73. public String login(String username, String password) {
  74. log.info("begin login username=" + username + ",password=" + password);
  75. /*LoginUser loginUser = (LoginUser) userDetailsService.loadUserByUsername(username);
  76. BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
  77. if (!encoder.matches(password, loginUser.getPassword())) {
  78. throw new BadCredentialsException("密码不正确");
  79. }
  80. UsernamePasswordAuthenticationToken upToken = new UsernamePasswordAuthenticationToken(loginUser, password, loginUser.getAuthorities());
  81. Authentication authentication = authenticationManager.authenticate(upToken);
  82. SecurityContextHolder.getContext().setAuthentication(authentication);
  83. return tokenService.saveToken(loginUser);*/
  84. return null;
  85. }
  86. @Override
  87. public String mobileLogin(String mobile, String password) {
  88. log.info("begin login username=" + mobile + ",password=" + password);
  89. /*LoginUser loginUser = (LoginUser) userDetailsService.loadUserByUsername(mobile);
  90. BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
  91. if (!encoder.matches(password, loginUser.getPassword())) {
  92. throw new BadCredentialsException("密码不正确");
  93. }
  94. MobileLoginAuthenticationToken upToken = new MobileLoginAuthenticationToken(loginUser, loginUser.getAuthorities());
  95. Authentication authentication = authenticationManager.authenticate(upToken);
  96. SecurityContextHolder.getContext().setAuthentication(authentication);*/
  97. return null;
  98. }
  99. @Override
  100. public String wechatLogin(User user) {
  101. log.info("begin wechatLogin user:" + JSON.toJSONString(user));
  102. /*LoginUser loginUser = (LoginUser) userDetailsService.loadUserByUsername(user.getUsername());
  103. BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
  104. UsernamePasswordAuthenticationToken upToken = new UsernamePasswordAuthenticationToken(loginUser, null, loginUser.getAuthorities());
  105. Authentication authentication = authenticationManager.authenticate(upToken);
  106. SecurityContextHolder.getContext().setAuthentication(authentication);
  107. return tokenService.saveToken(loginUser);*/
  108. return null;
  109. }
  110. @Override
  111. public void logout() {
  112. LoginUser loginUser = UserUtil.getCurrentUser();
  113. if (loginUser != null) {
  114. tokenService.deleteToken(loginUser.getLoginToken());
  115. if (loginUser.getIotToken() != null) {
  116. iotService.logout(loginUser.getIotToken());
  117. }
  118. }
  119. }
  120. @Override
  121. @Transactional
  122. public void addUser(String username, String name, String mobilePhone, Integer roleId, Integer orgId) {
  123. log.info("begin addUser username={},name={},roleId={},mobilePhone={}", username,name,roleId,mobilePhone);
  124. LoginUser loginUser = UserUtil.getCurrentUser();
  125. /*User u = userMapper.findUserByUsername(username);
  126. if (u != null) {
  127. throw new ServiceException(-900, "用户名已存在");
  128. }
  129. int result = userMapper.findUserByMobilePhoneUnique(null, mobilePhone);
  130. if (result > 0) {
  131. throw new ServiceException(-900, "该手机号码已存在");
  132. }*/
  133. if (loginUser.getType() == 1) {
  134. int result = userMapper.findUserByMobilePhoneUnique(null, mobilePhone);
  135. if (result > 0) {
  136. throw new ServiceException(-900, "该手机号码已存在");
  137. }
  138. }else {
  139. int result = userMapper.findUserByMobilePhoneSiteUnique(null,loginUser.getSiteId(), mobilePhone);
  140. if (result > 0) {
  141. throw new ServiceException(-900, "该站点已存在该手机号码");
  142. }
  143. }
  144. User u = userMapper.findUserByMobile(mobilePhone);
  145. if (u != null) {
  146. if (loginUser.getIsSuperAdmin() == 1) {
  147. throw new ServiceException(-900,"该账号是超级管理员,无法添加");
  148. }
  149. if(!StringUtils.equals(username,u.getUsername()) || !StringUtils.equals(name,u.getName())){
  150. u.setUsername(username);
  151. u.setName(name);
  152. userMapper.update(u);
  153. }
  154. }else {
  155. //添加用户
  156. User user = new User();
  157. user.setUsername(username);
  158. //user.setPassword(encode);
  159. user.setMobilePhone(mobilePhone);
  160. user.setName(name);
  161. user.setStatus(1);
  162. //user.setOrganId(organId != null ? organId : -1);
  163. user.setCreateBy(UserUtil.getCurrentUser().getUsername());
  164. user.setCreateDate(LocalDateTime.now());
  165. user.setUpdateBy(UserUtil.getCurrentUser().getUsername());
  166. user.setUpdateDate(LocalDateTime.now());
  167. user.setIsSuperAdmin(0);
  168. //user.setType(loginUser.getType() == 1 ? 2 : 3);
  169. userMapper.insertSelective(user);
  170. u = user;
  171. }
  172. if (loginUser.getType() != 1) {
  173. SiteUser siteUser = new SiteUser();
  174. siteUser.setSiteId(loginUser.getSiteId());
  175. siteUser.setUserId(u.getId());
  176. siteUser.setStatus(1);
  177. siteUser.setIsAdmin(0);
  178. siteUser.setOrganId(orgId);
  179. //siteUser.setOrganId(organId != null ? organId : -1);
  180. siteUser.setType(3);
  181. siteUser.setCreateBy(loginUser.getUsername());
  182. siteUser.setCreateDate(LocalDateTime.now());
  183. siteUser.setUpdateBy(loginUser.getUsername());
  184. siteUser.setUpdateDate(LocalDateTime.now());
  185. siteUserMapper.insert(siteUser);
  186. //添加用户角色关系
  187. UserRole userRole = new UserRole();
  188. userRole.setUid(u.getId());
  189. userRole.setRid(roleId);
  190. userRole.setStatus(1);
  191. userRole.setCreateBy(loginUser.getUsername());
  192. userRole.setCreateDate(LocalDateTime.now());
  193. userRole.setUpdateBy(loginUser.getUsername());
  194. userRole.setUpdateDate(LocalDateTime.now());
  195. userRoleMapper.insert(userRole);
  196. }
  197. log.info("end addUser");
  198. }
  199. @Override
  200. @Transactional
  201. public void editUser(Integer userId, String name, String mobilePhone, Integer roleId, Integer orgId) {
  202. log.info("begin addUser userId=" + userId + ",name=" + name + ",roleId=" + roleId + ",mobilePhone=" + mobilePhone);
  203. LoginUser loginUser = UserUtil.getCurrentUser();
  204. /*int mobilePhoneUniqueResult = userMapper.findUserByMobilePhoneUnique(userId, mobilePhone);
  205. if (mobilePhoneUniqueResult > 0) {
  206. throw new ServiceException(-900, "该手机号码已存在");
  207. }*/
  208. if (loginUser.getType() == 1) {
  209. int result = userMapper.findUserByMobilePhoneUnique(userId, mobilePhone);
  210. if (result > 0) {
  211. throw new ServiceException(-900, "该手机号码已存在");
  212. }
  213. }else {
  214. int result = userMapper.findUserByMobilePhoneSiteUnique(userId,loginUser.getSiteId(), mobilePhone);
  215. if (result > 0) {
  216. throw new ServiceException(-900, "该站点已存在该手机号码");
  217. }
  218. }
  219. User editUser = userMapper.findUserById(userId);
  220. User user = new User();
  221. user.setId(userId);
  222. user.setName(name);
  223. user.setMobilePhone(mobilePhone);
  224. user.setUpdateBy(loginUser.getUsername());
  225. user.setUpdateDate(LocalDateTime.now());
  226. userMapper.update(user);
  227. //更新用户角色关系
  228. if (loginUser.getType() != 1) {
  229. //先删除关系
  230. userRoleMapper.deleteBySiteIdAndUserId(loginUser.getSiteId(), userId, loginUser.getUsername());
  231. //添加用户角色关系
  232. UserRole userRole = new UserRole();
  233. userRole.setUid(user.getId());
  234. userRole.setRid(roleId);
  235. userRole.setStatus(1);
  236. userRole.setCreateBy(loginUser.getUsername());
  237. userRole.setCreateDate(LocalDateTime.now());
  238. userRole.setUpdateBy(loginUser.getUsername());
  239. userRole.setUpdateDate(LocalDateTime.now());
  240. userRoleMapper.insert(userRole);
  241. }
  242. siteMapper.updateOrgId(userId,orgId);
  243. log.info("end editUser");
  244. }
  245. @Override
  246. public void updateStatus(Integer id) {
  247. log.info("begin updateStatus userId " + id);
  248. LoginUser loginUser = UserUtil.getCurrentUser();
  249. if (loginUser.getType() == 3) {
  250. throw new ServiceException(-900, "普通用户无法启用、禁用用户");
  251. }
  252. User selectedUser = userMapper.findUserById(id);
  253. if(selectedUser != null) {
  254. selectedUser.setId(id);
  255. if(selectedUser.getEnable() == 1) {
  256. selectedUser.setEnable(0);
  257. } else {
  258. selectedUser.setEnable(1);
  259. }
  260. selectedUser.setUpdateBy(UserUtil.getUsername());
  261. selectedUser.setUpdateDate(LocalDateTime.now());
  262. this.update(selectedUser);
  263. log.info("end updateStatus User");
  264. } else {
  265. log.info("用户不存在");
  266. }
  267. }
  268. @Override
  269. @Transactional
  270. public void delectUser(Integer id) {
  271. log.info("begin delectUser userId=" + id);
  272. LoginUser loginUser = UserUtil.getCurrentUser();
  273. User delectUser = userMapper.findUserById(id);
  274. User user = new User();
  275. user.setId(id);
  276. user.setStatus(0);
  277. user.setUpdateBy(UserUtil.getUsername());
  278. user.setUpdateDate(LocalDateTime.now());
  279. int i = this.update(user);
  280. //删除站点用户关系
  281. int j = userMapper.deleteByUserId(id, UserUtil.getUsername());
  282. //删除用户角色
  283. userRoleMapper.deleteByUserId(id, UserUtil.getUsername());
  284. log.info("end delectUser");
  285. }
  286. @Override
  287. public Pagination<UserRoleDto> getUserList(String username, String name, String mobilePhone, Integer organId, Integer roleId, Integer programId, int pageNum, int pageSize) {
  288. LoginUser loginUser = UserUtil.getCurrentUser();
  289. List<UserRoleDto> list = new ArrayList<>();
  290. if (loginUser.getIsSuperAdmin() == 1) {
  291. PageHelper.startPage(pageNum, pageSize);
  292. list = userMapper.findUserList(username, name, mobilePhone);
  293. } else {
  294. PageHelper.startPage(pageNum, pageSize);
  295. list = userMapper.findBySiteId(loginUser.getSiteId(), username, name, mobilePhone, organId, roleId, programId);
  296. }
  297. Pagination<UserRoleDto> pageInfo = new Pagination<>(list);
  298. return pageInfo;
  299. }
  300. @Override
  301. @Transactional
  302. public void batchDelectUser(DeleteUserDto deleteUserDto) {
  303. log.info("begin batchDelectUser deleteUserDto:" + JSON.toJSONString(deleteUserDto));
  304. /*int i = userMapper.batchDelectUser(deleteUserDto.getUserIds(), UserUtil.getCurrentUser().getUsername());
  305. //删除站点用户关系
  306. int j = userMapper.batchDeleteByUserIds(deleteUserDto.getUserIds(), UserUtil.getCurrentUser().getUsername());
  307. //删除用户数据项关系
  308. userRoleProgramService.batchDelByUserId(deleteUserDto.getUserIds(), UserUtil.getCurrentUser().getUsername());
  309. //删除用户角色
  310. userRoleMapper.batchDeleteByUserId(deleteUserDto.getUserIds(), UserUtil.getCurrentUser().getUsername());*/
  311. if (deleteUserDto.getUserIds() != null && deleteUserDto.getUserIds().size() > 0) {
  312. for (Integer userId : deleteUserDto.getUserIds()) {
  313. this.delectUser(userId);
  314. }
  315. }
  316. log.info("end batchDelectUser");
  317. }
  318. @Override
  319. public void changePassword(String oldPassword, String newPassword) {
  320. log.info("begin changePassword oldPassword=" + oldPassword + ",newPassword=" + newPassword);
  321. User user = UserUtil.getCurrentUser();
  322. BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
  323. if (!encoder.matches(oldPassword, user.getPassword())) {
  324. throw new ServiceException("密码不正确");
  325. }
  326. String str = encoder.encode(newPassword);
  327. User newUser = new User();
  328. newUser.setId(user.getId());
  329. newUser.setPassword(str);
  330. user.setUpdateBy(UserUtil.getCurrentUser().getUsername());
  331. user.setUpdateDate(LocalDateTime.now());
  332. int result = userMapper.update(newUser);
  333. log.info("end changePassword result=" + result);
  334. }
  335. @Override
  336. public List<UserDto> unbindUser(Integer siteId) {
  337. return siteUserMapper.unbindUser(siteId);
  338. }
  339. @Override
  340. public List<UserDto> deleteQuery(List<Integer> userIds) {
  341. LoginUser loginUser = UserUtil.getCurrentUser();
  342. List<UserDto> list = newArrayList();
  343. if (loginUser != null && loginUser.getIsSuperAdmin() == 1) {
  344. list = siteUserMapper.deleteQuery(userIds);
  345. } else {
  346. }
  347. return list;
  348. }
  349. @Override
  350. public User findUserByOpenid(String openid) {
  351. return userMapper.findUserByOpenid(openid);
  352. }
  353. @Override
  354. public List<UserDto> getAllUserList(Integer organId, String taskId) {
  355. LoginUser loginUser = UserUtil.getCurrentUser();
  356. List<UserDto> list = newArrayList();
  357. Integer siteId = null;
  358. if (loginUser != null && loginUser.getIsSuperAdmin() != 1) {
  359. siteId = loginUser.getSiteId();
  360. }
  361. list = siteUserMapper.getAllUserList(siteId, organId);
  362. return list;
  363. }
  364. @Override
  365. public void changeSite(Integer siteId) {
  366. }
  367. //判断siteList中是否包含siteId
  368. private boolean checkSite(List<Site> siteList, Integer siteId) {
  369. return siteList.parallelStream().anyMatch(site -> site.getId().equals(siteId));
  370. }
  371. @Override
  372. public void bindOpenId(String openId) {
  373. LoginUser loginUser = UserUtil.getCurrentUser();
  374. User user = userMapper.findUserByOpenid(openId);
  375. if (user == null) {
  376. User userUpdate = new User();
  377. userUpdate.setId(loginUser.getId());
  378. userUpdate.setOpenid(openId);
  379. this.update(userUpdate);
  380. }
  381. }
  382. @Override
  383. public User findUserByName(String name, BigInteger sitId) {
  384. return userMapper.findUserByName(name,sitId);
  385. }
  386. @Override
  387. public void addIntegrationUser(String username, String mobilePhone) {
  388. User userTemp = userMapper.findUserByMobile(mobilePhone);
  389. if (userTemp != null) {
  390. User user = new User();
  391. user.setId(userTemp.getId());
  392. user.setMobilePhone(mobilePhone);
  393. user.setUsername(username);
  394. user.setUpdateBy("admin");
  395. user.setUpdateDate(LocalDateTime.now());
  396. userMapper.update(user);
  397. }else {
  398. //添加用户
  399. User user = new User();
  400. user.setUsername(username);
  401. user.setPassword("");
  402. user.setMobilePhone(mobilePhone);
  403. //user.setName(name);
  404. user.setStatus(1);
  405. //user.setOrganId(organId != null ? organId : -1);
  406. user.setCreateBy("admin");
  407. user.setCreateDate(LocalDateTime.now());
  408. user.setUpdateBy("admin");
  409. user.setUpdateDate(LocalDateTime.now());
  410. user.setIsSuperAdmin(0);
  411. //user.setType(loginUser.getType() == 1 ? 2 : 3);
  412. userMapper.insertSelective(user);
  413. SiteUser siteUser = new SiteUser();
  414. siteUser.setSiteId(1);
  415. siteUser.setUserId(user.getId());
  416. siteUser.setStatus(1);
  417. siteUser.setIsAdmin(0);
  418. siteUser.setOrganId(-1);
  419. siteUser.setType(3);
  420. siteUser.setCreateBy("admin");
  421. siteUser.setCreateDate(LocalDateTime.now());
  422. siteUser.setUpdateBy("admin");
  423. siteUser.setUpdateDate(LocalDateTime.now());
  424. siteUserMapper.insert(siteUser);
  425. //添加用户角色关系
  426. UserRole userRole = new UserRole();
  427. userRole.setUid(user.getId());
  428. userRole.setRid(4);
  429. userRole.setStatus(1);
  430. userRole.setCreateBy("admin");
  431. userRole.setCreateDate(LocalDateTime.now());
  432. userRole.setUpdateBy("admin");
  433. userRole.setUpdateDate(LocalDateTime.now());
  434. userRoleMapper.insert(userRole);
  435. }
  436. }
  437. @Override
  438. public void delectIntegrationUser(String mobilePhone) {
  439. User userTemp = userMapper.findUserByMobile(mobilePhone);
  440. if (userTemp != null) {
  441. Integer id = userTemp.getId();
  442. User user = new User();
  443. user.setId(id);
  444. user.setStatus(0);
  445. user.setUpdateBy("system");
  446. user.setUpdateDate(LocalDateTime.now());
  447. int i = this.update(user);
  448. //删除站点用户关系
  449. int j = userMapper.deleteByUserId(id, "system");
  450. //删除用户角色
  451. userRoleMapper.deleteByUserId(id, "system");
  452. }
  453. }
  454. }