From fcbc2e81c04c890cce7bcd9c93b8e83938d80eab Mon Sep 17 00:00:00 2001 From: zhangzhiqiang Date: Tue, 29 Nov 2022 20:33:53 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=A8=E6=88=B7=E5=8E=BBjpa?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../modules/system/service/UserService.java | 10 +- .../system/service/impl/UserServiceImpl.java | 156 ++++++++++-------- 2 files changed, 95 insertions(+), 71 deletions(-) diff --git a/nladmin-system/nlsso-server/src/main/java/org/nl/modules/system/service/UserService.java b/nladmin-system/nlsso-server/src/main/java/org/nl/modules/system/service/UserService.java index 40ba27e..11e55ab 100644 --- a/nladmin-system/nlsso-server/src/main/java/org/nl/modules/system/service/UserService.java +++ b/nladmin-system/nlsso-server/src/main/java/org/nl/modules/system/service/UserService.java @@ -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 queryAll(UserQueryCriteria criteria); + List queryAll(UserQueryCriteria criteria); /** * 导出数据 @@ -107,7 +107,7 @@ public interface UserService { * @param response / * @throws IOException / */ - void download(List queryAll, HttpServletResponse response) throws IOException; + void download(List queryAll, HttpServletResponse response) throws IOException; /** * 用户自助修改资料 diff --git a/nladmin-system/nlsso-server/src/main/java/org/nl/modules/system/service/impl/UserServiceImpl.java b/nladmin-system/nlsso-server/src/main/java/org/nl/modules/system/service/impl/UserServiceImpl.java index b2b5921..c311b84 100644 --- a/nladmin-system/nlsso-server/src/main/java/org/nl/modules/system/service/impl/UserServiceImpl.java +++ b/nladmin-system/nlsso-server/src/main/java/org/nl/modules/system/service/impl/UserServiceImpl.java @@ -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 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 queryAll(UserQueryCriteria criteria) { - List users = userRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder)); - return userMapper.toDto(users); + public List 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 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 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 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(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 queryAll, HttpServletResponse response) throws IOException { + public void download(List queryAll, HttpServletResponse response) throws IOException { List> list = new ArrayList<>(); - for (UserDto userDTO : queryAll) { - List roles = userDTO.getRoles().stream().map(RoleSmallDto::getName).collect(Collectors.toList()); + for (User userDTO : queryAll) { Map 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);