用户去jpa

This commit is contained in:
zhangzhiqiang
2022-11-29 20:33:53 +08:00
parent 71c7c56b71
commit fcbc2e81c0
2 changed files with 95 additions and 71 deletions

View File

@@ -38,7 +38,7 @@ public interface UserService {
* @param id ID
* @return /
*/
UserDto findById(long id);
User findById(long id);
/**
* 新增用户
@@ -50,7 +50,7 @@ public interface UserService {
* 编辑用户
* @param resources /
*/
void update(User resources) throws Exception;
void update(User resources) ;
/**
* 删除用户
@@ -63,7 +63,7 @@ public interface UserService {
* @param userName /
* @return /
*/
UserDto findByName(String userName);
User findByName(String userName);
/**
* 修改密码
@@ -99,7 +99,7 @@ public interface UserService {
* @param criteria 条件
* @return /
*/
List<UserDto> queryAll(UserQueryCriteria criteria);
List<User> queryAll(UserQueryCriteria criteria);
/**
* 导出数据
@@ -107,7 +107,7 @@ public interface UserService {
* @param response /
* @throws IOException /
*/
void download(List<UserDto> queryAll, HttpServletResponse response) throws IOException;
void download(List<User> queryAll, HttpServletResponse response) throws IOException;
/**
* 用户自助修改资料

View File

@@ -16,13 +16,20 @@
package org.nl.modules.system.service.impl;
import cn.dev33.satoken.secure.SaSecureUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
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.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.nl.modules.common.config.FileProperties;
import org.nl.modules.common.exception.EntityExistException;
import org.nl.modules.common.exception.EntityNotFoundException;
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.system.domain.User;
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.UserQueryCriteria;
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.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
@@ -55,94 +66,108 @@ import java.util.stream.Collectors;
@CacheConfig(cacheNames = "user")
public class UserServiceImpl implements UserService {
private final UserRepository userRepository;
private final UserMapper userMapper;
private final FileProperties properties;
private final RedisUtils redisUtils;
private final OnlineUserService onlineUserService;
// private final FlushSessionUtil flushSessionUtil;
// private final RoleService roleService;
@Override
public Object queryAll(UserQueryCriteria criteria, Pageable pageable) {
Page<User> page = userRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
return PageUtil.toPage(page.map(userMapper::toDto));
JSONObject o = (JSONObject)JSON.toJSON(criteria);
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
public List<UserDto> queryAll(UserQueryCriteria criteria) {
List<User> users = userRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder));
return userMapper.toDto(users);
public List<User> queryAll(UserQueryCriteria criteria) {
JSONObject o = (JSONObject)JSON.toJSON(criteria);
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
@Cacheable(key = "'id:' + #p0")
@Transactional(rollbackFor = Exception.class)
public UserDto findById(long id) {
User user = userRepository.findById(id).orElseGet(User::new);
ValidationUtil.isNull(user.getId(), "User", "id", id);
return userMapper.toDto(user);
public User findById(long id) {
JSONObject result = WQLObject.getWQLObject("sys_user").query("user_id = '" + id + "'").uniqueResult(0);
User user = result.toJavaObject(User.class);
return user;
}
@Override
@Transactional(rollbackFor = Exception.class)
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());
}
resources.setCreateBy(SecurityUtils.getCurrentUsername());
userRepository.save(resources);
CurrentUser user = SecurityUtils.getCurrentUser();
resources.setCreate_id(user.getId());
resources.setCreate_name(user.getUsername());
WQLObject.getWQLObject("sys_user").insert((JSONObject)JSON.toJSON(resources));
}
@Override
@Transactional(rollbackFor = Exception.class)
public void update(User resources) throws Exception{
User user = userRepository.findById(resources.getId()).orElseGet(User::new);
ValidationUtil.isNull(user.getId(), "User", "id", resources.getId());
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());
}
@SneakyThrows
public void update(User resources) {
User user = this.findById(resources.getUser_id());
redisUtils.del("user::username:" + user.getUsername());
// 如果用户被禁用,则清除用户登录信息
if(!resources.getEnabled()){
if(!resources.getIs_used()){
onlineUserService.kickOutForUsername(resources.getUsername());
}
user.setId(resources.getId());
user.setUsername(resources.getUsername());
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);
resources.setPassword(SaSecureUtil.md5BySalt(resources.getPassword(), "salt"));
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());
//更新部门用户
// 如果用户的角色改变
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
@Transactional(rollbackFor = Exception.class)
public void updateCenter(User resources) {
User user = userRepository.findById(resources.getId()).orElseGet(User::new);
user.setNickName(resources.getNickName());
User user = this.findById(resources.getUser_id());
user.setPerson_name(resources.getPerson_name());
user.setPhone(resources.getPhone());
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
@@ -150,43 +175,43 @@ public class UserServiceImpl implements UserService {
public void delete(Set<Long> ids) {
for (Long id : ids) {
// 清理缓存
UserDto user = findById(id);
delCaches(user.getId(), user.getUsername());
User user = findById(id);
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
@Cacheable(key = "'username:' + #p0")
public UserDto findByName(String userName) {
User user = userRepository.findByUsername(userName);
if (user == null) {
public User findByName(String userName) {
JSONObject result = WQLObject.getWQLObject("sys_user").query("userName = '" + userName + "'").uniqueResult(0);
if (result == null) {
throw new EntityNotFoundException(User.class, "name", userName);
} else {
return userMapper.toDto(user);
return result.toJavaObject(User.class);
}
}
@Override
@Transactional(rollbackFor = Exception.class)
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);
}
@Override
@Transactional(rollbackFor = Exception.class)
public Map<String, String> updateAvatar(MultipartFile multipartFile) {
User user = userRepository.findByUsername(SecurityUtils.getCurrentUsername());
User user = this.findByName(SecurityUtils.getCurrentUsername());
String oldPath = user.getAvatarPath();
File file = FileUtil.upload(multipartFile, properties.getPath().getAvatar());
user.setAvatarPath(Objects.requireNonNull(file).getPath());
user.setAvatarName(file.getName());
userRepository.save(user);
this.update(user);
if (StrUtil.isNotEmpty(oldPath)) {
FileUtil.del(oldPath);
}
@NotBlank String username = user.getUsername();
return new HashMap<String, String>(1) {{
put("avatar", file.getName());
}};
@@ -195,22 +220,21 @@ public class UserServiceImpl implements UserService {
@Override
@Transactional(rollbackFor = Exception.class)
public void updateEmail(String username, String email) {
userRepository.updateEmail(username, email);
WQLObject.getWQLObject("sys_user").update(MapOf.of("email",email),"username ='"+username+"'");
}
@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<>();
for (UserDto userDTO : queryAll) {
List<String> roles = userDTO.getRoles().stream().map(RoleSmallDto::getName).collect(Collectors.toList());
for (User userDTO : queryAll) {
Map<String, Object> map = new LinkedHashMap<>();
map.put("用户名", userDTO.getUsername());
map.put("角色", roles);
map.put("角色", userDTO.getRoles());
map.put("邮箱", userDTO.getEmail());
map.put("状态", userDTO.getEnabled() ? "启用" : "禁用");
map.put("状态", userDTO.getIs_used() ? "启用" : "禁用");
map.put("手机号码", userDTO.getPhone());
map.put("修改密码的时间", userDTO.getPwdResetTime());
map.put("创建日期", userDTO.getCreateTime());
map.put("创建日期", userDTO.getCreate_time());
list.add(map);
}
FileUtil.downloadExcel(list, response);