用户去jpa
This commit is contained in:
@@ -38,7 +38,7 @@ public interface UserService {
|
|||||||
* @param id ID
|
* @param id ID
|
||||||
* @return /
|
* @return /
|
||||||
*/
|
*/
|
||||||
UserDto findById(long id);
|
User findById(long id);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 新增用户
|
* 新增用户
|
||||||
@@ -50,7 +50,7 @@ public interface UserService {
|
|||||||
* 编辑用户
|
* 编辑用户
|
||||||
* @param resources /
|
* @param resources /
|
||||||
*/
|
*/
|
||||||
void update(User resources) throws Exception;
|
void update(User resources) ;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 删除用户
|
* 删除用户
|
||||||
@@ -63,7 +63,7 @@ public interface UserService {
|
|||||||
* @param userName /
|
* @param userName /
|
||||||
* @return /
|
* @return /
|
||||||
*/
|
*/
|
||||||
UserDto findByName(String userName);
|
User findByName(String userName);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 修改密码
|
* 修改密码
|
||||||
@@ -99,7 +99,7 @@ public interface UserService {
|
|||||||
* @param criteria 条件
|
* @param criteria 条件
|
||||||
* @return /
|
* @return /
|
||||||
*/
|
*/
|
||||||
List<UserDto> queryAll(UserQueryCriteria criteria);
|
List<User> queryAll(UserQueryCriteria criteria);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 导出数据
|
* 导出数据
|
||||||
@@ -107,7 +107,7 @@ public interface UserService {
|
|||||||
* @param response /
|
* @param response /
|
||||||
* @throws IOException /
|
* @throws IOException /
|
||||||
*/
|
*/
|
||||||
void download(List<UserDto> queryAll, HttpServletResponse response) throws IOException;
|
void download(List<User> queryAll, HttpServletResponse response) throws IOException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 用户自助修改资料
|
* 用户自助修改资料
|
||||||
|
|||||||
@@ -16,13 +16,20 @@
|
|||||||
package org.nl.modules.system.service.impl;
|
package org.nl.modules.system.service.impl;
|
||||||
|
|
||||||
import cn.dev33.satoken.secure.SaSecureUtil;
|
import cn.dev33.satoken.secure.SaSecureUtil;
|
||||||
|
import cn.hutool.core.map.MapUtil;
|
||||||
import cn.hutool.core.util.ObjectUtil;
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import com.alibaba.fastjson.JSON;
|
||||||
|
import com.alibaba.fastjson.JSONArray;
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.SneakyThrows;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.nl.modules.common.config.FileProperties;
|
import org.nl.modules.common.config.FileProperties;
|
||||||
import org.nl.modules.common.exception.EntityExistException;
|
import org.nl.modules.common.exception.EntityExistException;
|
||||||
import org.nl.modules.common.exception.EntityNotFoundException;
|
import org.nl.modules.common.exception.EntityNotFoundException;
|
||||||
import org.nl.modules.common.utils.*;
|
import org.nl.modules.common.utils.*;
|
||||||
|
import org.nl.modules.common.utils.dto.CurrentUser;
|
||||||
import org.nl.modules.security.service.OnlineUserService;
|
import org.nl.modules.security.service.OnlineUserService;
|
||||||
import org.nl.modules.system.domain.User;
|
import org.nl.modules.system.domain.User;
|
||||||
import org.nl.modules.system.repository.UserRepository;
|
import org.nl.modules.system.repository.UserRepository;
|
||||||
@@ -31,12 +38,16 @@ import org.nl.modules.system.service.dto.RoleSmallDto;
|
|||||||
import org.nl.modules.system.service.dto.UserDto;
|
import org.nl.modules.system.service.dto.UserDto;
|
||||||
import org.nl.modules.system.service.dto.UserQueryCriteria;
|
import org.nl.modules.system.service.dto.UserQueryCriteria;
|
||||||
import org.nl.modules.system.service.mapstruct.UserMapper;
|
import org.nl.modules.system.service.mapstruct.UserMapper;
|
||||||
|
import org.nl.modules.tools.MapOf;
|
||||||
|
import org.nl.modules.wql.WQL;
|
||||||
|
import org.nl.modules.wql.core.bean.WQLObject;
|
||||||
import org.springframework.cache.annotation.CacheConfig;
|
import org.springframework.cache.annotation.CacheConfig;
|
||||||
import org.springframework.cache.annotation.Cacheable;
|
import org.springframework.cache.annotation.Cacheable;
|
||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
import org.springframework.data.domain.Pageable;
|
import org.springframework.data.domain.Pageable;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
import org.springframework.util.CollectionUtils;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
@@ -55,94 +66,108 @@ import java.util.stream.Collectors;
|
|||||||
@CacheConfig(cacheNames = "user")
|
@CacheConfig(cacheNames = "user")
|
||||||
public class UserServiceImpl implements UserService {
|
public class UserServiceImpl implements UserService {
|
||||||
|
|
||||||
private final UserRepository userRepository;
|
|
||||||
private final UserMapper userMapper;
|
private final UserMapper userMapper;
|
||||||
private final FileProperties properties;
|
private final FileProperties properties;
|
||||||
private final RedisUtils redisUtils;
|
private final RedisUtils redisUtils;
|
||||||
private final OnlineUserService onlineUserService;
|
private final OnlineUserService onlineUserService;
|
||||||
// private final FlushSessionUtil flushSessionUtil;
|
|
||||||
// private final RoleService roleService;
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object queryAll(UserQueryCriteria criteria, Pageable pageable) {
|
public Object queryAll(UserQueryCriteria criteria, Pageable pageable) {
|
||||||
Page<User> page = userRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
|
JSONObject o = (JSONObject)JSON.toJSON(criteria);
|
||||||
return PageUtil.toPage(page.map(userMapper::toDto));
|
HashMap map = MapOf.of("user_id", MapUtil.getStr(o, "user_id")
|
||||||
|
, "blurry", MapUtil.getStr(o, "blurry")
|
||||||
|
, "is_used", MapUtil.getStr(o, "is_used")
|
||||||
|
, "startTime", MapUtil.getStr(o, "startTime")
|
||||||
|
, "endTime", MapUtil.getStr(o, "endTime")
|
||||||
|
, "deptId", MapUtil.getStr(o, "deptId")
|
||||||
|
);
|
||||||
|
if (!CollectionUtils.isEmpty(criteria.getDeptIds())){
|
||||||
|
String collectSql = criteria.getDeptIds().stream().map(a -> String.valueOf(a)).collect(Collectors.joining("','"));
|
||||||
|
map.put("deptIds","('"+collectSql+"')");
|
||||||
|
}
|
||||||
|
map.put("flag","1");
|
||||||
|
JSONObject jsonObject = WQL.getWO("SYS_USER").addParamMap(map).pageQuery(pageable.getPageNumber(), pageable.getPageSize(), "user_id");
|
||||||
|
return jsonObject;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<UserDto> queryAll(UserQueryCriteria criteria) {
|
public List<User> queryAll(UserQueryCriteria criteria) {
|
||||||
List<User> users = userRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder));
|
JSONObject o = (JSONObject)JSON.toJSON(criteria);
|
||||||
return userMapper.toDto(users);
|
HashMap map = MapOf.of("user_id", MapUtil.getStr(o, "user_id")
|
||||||
|
, "blurry", MapUtil.getStr(o, "blurry")
|
||||||
|
, "is_used", MapUtil.getStr(o, "is_used")
|
||||||
|
, "startTime", MapUtil.getStr(o, "startTime")
|
||||||
|
, "endTime", MapUtil.getStr(o, "endTime")
|
||||||
|
, "deptId", MapUtil.getStr(o, "deptId")
|
||||||
|
);
|
||||||
|
if (!CollectionUtils.isEmpty(criteria.getDeptIds())){
|
||||||
|
String collectSql = criteria.getDeptIds().stream().map(a -> String.valueOf(a)).collect(Collectors.joining("','"));
|
||||||
|
map.put("deptIds","('"+collectSql+"')");
|
||||||
|
}
|
||||||
|
map.put("flag","1");
|
||||||
|
JSONArray array = WQL.getWO("SYS_USER").addParamMap(map).process().getResultJSONArray(0);
|
||||||
|
List<User> users = array.toJavaList(User.class);
|
||||||
|
return users;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Cacheable(key = "'id:' + #p0")
|
@Cacheable(key = "'id:' + #p0")
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public UserDto findById(long id) {
|
public User findById(long id) {
|
||||||
User user = userRepository.findById(id).orElseGet(User::new);
|
JSONObject result = WQLObject.getWQLObject("sys_user").query("user_id = '" + id + "'").uniqueResult(0);
|
||||||
ValidationUtil.isNull(user.getId(), "User", "id", id);
|
User user = result.toJavaObject(User.class);
|
||||||
return userMapper.toDto(user);
|
return user;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public void create(User resources) {
|
public void create(User resources) {
|
||||||
if (userRepository.findByUsername(resources.getUsername()) != null) {
|
JSONObject result = WQLObject.getWQLObject("sys_user").query("username = '" + resources.getUsername() + "'").uniqueResult(0);
|
||||||
|
if (result != null) {
|
||||||
throw new EntityExistException(User.class, "username", resources.getUsername());
|
throw new EntityExistException(User.class, "username", resources.getUsername());
|
||||||
}
|
}
|
||||||
resources.setCreateBy(SecurityUtils.getCurrentUsername());
|
CurrentUser user = SecurityUtils.getCurrentUser();
|
||||||
userRepository.save(resources);
|
resources.setCreate_id(user.getId());
|
||||||
|
resources.setCreate_name(user.getUsername());
|
||||||
|
WQLObject.getWQLObject("sys_user").insert((JSONObject)JSON.toJSON(resources));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public void update(User resources) throws Exception{
|
@SneakyThrows
|
||||||
User user = userRepository.findById(resources.getId()).orElseGet(User::new);
|
public void update(User resources) {
|
||||||
ValidationUtil.isNull(user.getId(), "User", "id", resources.getId());
|
User user = this.findById(resources.getUser_id());
|
||||||
User user1 = userRepository.findByUsername(resources.getUsername());
|
|
||||||
|
|
||||||
if (user1 != null && !user.getId().equals(user1.getId())) {
|
|
||||||
throw new EntityExistException(User.class, "username", resources.getUsername());
|
|
||||||
}
|
|
||||||
// 如果用户的角色改变
|
|
||||||
if (!resources.getRoles().equals(user.getRoles())) {
|
|
||||||
redisUtils.del(CacheKey.DATA_USER + resources.getId());
|
|
||||||
redisUtils.del(CacheKey.MENU_USER + resources.getId());
|
|
||||||
redisUtils.del(CacheKey.ROLE_AUTH + resources.getId());
|
|
||||||
}
|
|
||||||
redisUtils.del("user::username:" + user.getUsername());
|
redisUtils.del("user::username:" + user.getUsername());
|
||||||
// 如果用户被禁用,则清除用户登录信息
|
// 如果用户被禁用,则清除用户登录信息
|
||||||
if(!resources.getEnabled()){
|
if(!resources.getIs_used()){
|
||||||
onlineUserService.kickOutForUsername(resources.getUsername());
|
onlineUserService.kickOutForUsername(resources.getUsername());
|
||||||
}
|
}
|
||||||
user.setId(resources.getId());
|
resources.setPassword(SaSecureUtil.md5BySalt(resources.getPassword(), "salt"));
|
||||||
user.setUsername(resources.getUsername());
|
WQLObject.getWQLObject("sys_user").update((JSONObject)JSON.toJSON(resources),"user_id ='"+resources.getUser_id()+"'");
|
||||||
user.setEmail(resources.getEmail());
|
|
||||||
user.setEnabled(resources.getEnabled());
|
|
||||||
user.setRoles(resources.getRoles());
|
|
||||||
user.setDept(resources.getDept());
|
|
||||||
user.setPhone(resources.getPhone());
|
|
||||||
user.setNickName(resources.getNickName());
|
|
||||||
user.setGender(resources.getGender());
|
|
||||||
if (ObjectUtil.isNotEmpty(resources.getPassword()))
|
|
||||||
user.setPassword(SaSecureUtil.md5BySalt(resources.getPassword(), "salt"));
|
|
||||||
|
|
||||||
userRepository.save(user);
|
|
||||||
// 清除缓存
|
// 清除缓存
|
||||||
delCaches(user.getId(), user.getUsername());
|
delCaches(user.getUser_id(), user.getUsername());
|
||||||
|
//更新部门用户
|
||||||
|
// 如果用户的角色改变
|
||||||
|
if (!resources.getRoles().equals(user.getRoles())) {
|
||||||
|
redisUtils.del(CacheKey.DATA_USER + resources.getUser_id());
|
||||||
|
redisUtils.del(CacheKey.MENU_USER + resources.getUser_id());
|
||||||
|
redisUtils.del(CacheKey.ROLE_AUTH + resources.getUser_id());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public void updateCenter(User resources) {
|
public void updateCenter(User resources) {
|
||||||
User user = userRepository.findById(resources.getId()).orElseGet(User::new);
|
User user = this.findById(resources.getUser_id());
|
||||||
user.setNickName(resources.getNickName());
|
user.setPerson_name(resources.getPerson_name());
|
||||||
user.setPhone(resources.getPhone());
|
user.setPhone(resources.getPhone());
|
||||||
user.setGender(resources.getGender());
|
user.setGender(resources.getGender());
|
||||||
userRepository.save(user);
|
WQLObject.getWQLObject("sys_user").update((JSONObject)JSON.toJSON(resources),"user_id ='"+resources.getUser_id()+"'");
|
||||||
// 清理缓存
|
// 清理缓存
|
||||||
delCaches(user.getId(), user.getUsername());
|
delCaches(user.getUser_id(), user.getUsername());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -150,43 +175,43 @@ public class UserServiceImpl implements UserService {
|
|||||||
public void delete(Set<Long> ids) {
|
public void delete(Set<Long> ids) {
|
||||||
for (Long id : ids) {
|
for (Long id : ids) {
|
||||||
// 清理缓存
|
// 清理缓存
|
||||||
UserDto user = findById(id);
|
User user = findById(id);
|
||||||
delCaches(user.getId(), user.getUsername());
|
delCaches(user.getUser_id(), user.getUsername());
|
||||||
}
|
}
|
||||||
userRepository.deleteAllByIdIn(ids);
|
String collectSql = ids.stream().map(a -> String.valueOf(a)).collect(Collectors.joining("','"));
|
||||||
|
WQLObject.getWQLObject("sys_user").delete("user_id in ('"+collectSql+"'");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Cacheable(key = "'username:' + #p0")
|
@Cacheable(key = "'username:' + #p0")
|
||||||
public UserDto findByName(String userName) {
|
public User findByName(String userName) {
|
||||||
User user = userRepository.findByUsername(userName);
|
JSONObject result = WQLObject.getWQLObject("sys_user").query("userName = '" + userName + "'").uniqueResult(0);
|
||||||
if (user == null) {
|
if (result == null) {
|
||||||
throw new EntityNotFoundException(User.class, "name", userName);
|
throw new EntityNotFoundException(User.class, "name", userName);
|
||||||
} else {
|
} else {
|
||||||
return userMapper.toDto(user);
|
return result.toJavaObject(User.class);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public void updatePass(String username, String pass) {
|
public void updatePass(String username, String pass) {
|
||||||
userRepository.updatePass(username, pass, new Date());
|
WQLObject.getWQLObject("sys_user").update(MapOf.of("password",pass),"username ='"+username+"'");
|
||||||
redisUtils.del("user::username:" + username);
|
redisUtils.del("user::username:" + username);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public Map<String, String> updateAvatar(MultipartFile multipartFile) {
|
public Map<String, String> updateAvatar(MultipartFile multipartFile) {
|
||||||
User user = userRepository.findByUsername(SecurityUtils.getCurrentUsername());
|
User user = this.findByName(SecurityUtils.getCurrentUsername());
|
||||||
String oldPath = user.getAvatarPath();
|
String oldPath = user.getAvatarPath();
|
||||||
File file = FileUtil.upload(multipartFile, properties.getPath().getAvatar());
|
File file = FileUtil.upload(multipartFile, properties.getPath().getAvatar());
|
||||||
user.setAvatarPath(Objects.requireNonNull(file).getPath());
|
user.setAvatarPath(Objects.requireNonNull(file).getPath());
|
||||||
user.setAvatarName(file.getName());
|
user.setAvatarName(file.getName());
|
||||||
userRepository.save(user);
|
this.update(user);
|
||||||
if (StrUtil.isNotEmpty(oldPath)) {
|
if (StrUtil.isNotEmpty(oldPath)) {
|
||||||
FileUtil.del(oldPath);
|
FileUtil.del(oldPath);
|
||||||
}
|
}
|
||||||
@NotBlank String username = user.getUsername();
|
|
||||||
return new HashMap<String, String>(1) {{
|
return new HashMap<String, String>(1) {{
|
||||||
put("avatar", file.getName());
|
put("avatar", file.getName());
|
||||||
}};
|
}};
|
||||||
@@ -195,22 +220,21 @@ public class UserServiceImpl implements UserService {
|
|||||||
@Override
|
@Override
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public void updateEmail(String username, String email) {
|
public void updateEmail(String username, String email) {
|
||||||
userRepository.updateEmail(username, email);
|
WQLObject.getWQLObject("sys_user").update(MapOf.of("email",email),"username ='"+username+"'");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void download(List<UserDto> queryAll, HttpServletResponse response) throws IOException {
|
public void download(List<User> queryAll, HttpServletResponse response) throws IOException {
|
||||||
List<Map<String, Object>> list = new ArrayList<>();
|
List<Map<String, Object>> list = new ArrayList<>();
|
||||||
for (UserDto userDTO : queryAll) {
|
for (User userDTO : queryAll) {
|
||||||
List<String> roles = userDTO.getRoles().stream().map(RoleSmallDto::getName).collect(Collectors.toList());
|
|
||||||
Map<String, Object> map = new LinkedHashMap<>();
|
Map<String, Object> map = new LinkedHashMap<>();
|
||||||
map.put("用户名", userDTO.getUsername());
|
map.put("用户名", userDTO.getUsername());
|
||||||
map.put("角色", roles);
|
map.put("角色", userDTO.getRoles());
|
||||||
map.put("邮箱", userDTO.getEmail());
|
map.put("邮箱", userDTO.getEmail());
|
||||||
map.put("状态", userDTO.getEnabled() ? "启用" : "禁用");
|
map.put("状态", userDTO.getIs_used() ? "启用" : "禁用");
|
||||||
map.put("手机号码", userDTO.getPhone());
|
map.put("手机号码", userDTO.getPhone());
|
||||||
map.put("修改密码的时间", userDTO.getPwdResetTime());
|
map.put("修改密码的时间", userDTO.getPwdResetTime());
|
||||||
map.put("创建日期", userDTO.getCreateTime());
|
map.put("创建日期", userDTO.getCreate_time());
|
||||||
list.add(map);
|
list.add(map);
|
||||||
}
|
}
|
||||||
FileUtil.downloadExcel(list, response);
|
FileUtil.downloadExcel(list, response);
|
||||||
|
|||||||
Reference in New Issue
Block a user