Browse Source

🎉 增加用户导入导出功能

test
smallchill 5 years ago
parent
commit
63420404ad
  1. 5
      blade-common/src/main/java/org/springblade/common/constant/CommonConstant.java
  2. 167
      blade-service-api/blade-system-api/src/main/java/org/springblade/system/cache/SysCache.java
  3. 99
      blade-service-api/blade-system-api/src/main/java/org/springblade/system/feign/ISysClient.java
  4. 40
      blade-service-api/blade-system-api/src/main/java/org/springblade/system/feign/ISysClientFallback.java
  5. 24
      blade-service-api/blade-user-api/src/main/java/org/springblade/system/user/cache/UserCache.java
  6. 12
      blade-service-api/blade-user-api/src/main/java/org/springblade/system/user/feign/IUserClient.java
  7. 56
      blade-service/blade-system/src/main/java/org/springblade/system/feign/SysClient.java
  8. 8
      blade-service/blade-system/src/main/java/org/springblade/system/mapper/PostMapper.java
  9. 13
      blade-service/blade-system/src/main/java/org/springblade/system/mapper/PostMapper.xml
  10. 9
      blade-service/blade-system/src/main/java/org/springblade/system/service/IDeptService.java
  11. 19
      blade-service/blade-system/src/main/java/org/springblade/system/service/IPostService.java
  12. 9
      blade-service/blade-system/src/main/java/org/springblade/system/service/IRoleService.java
  13. 9
      blade-service/blade-system/src/main/java/org/springblade/system/service/impl/DeptServiceImpl.java
  14. 19
      blade-service/blade-system/src/main/java/org/springblade/system/service/impl/PostServiceImpl.java
  15. 10
      blade-service/blade-system/src/main/java/org/springblade/system/service/impl/RoleServiceImpl.java
  16. 4
      blade-service/blade-user/pom.xml
  17. 42
      blade-service/blade-user/src/main/java/org/springblade/system/user/controller/UserController.java
  18. 89
      blade-service/blade-user/src/main/java/org/springblade/system/user/excel/UserExcel.java
  19. 40
      blade-service/blade-user/src/main/java/org/springblade/system/user/excel/UserImporter.java
  20. 6
      blade-service/blade-user/src/main/java/org/springblade/system/user/feign/UserClient.java
  21. 10
      blade-service/blade-user/src/main/java/org/springblade/system/user/mapper/UserMapper.java
  22. 5
      blade-service/blade-user/src/main/java/org/springblade/system/user/mapper/UserMapper.xml
  23. 29
      blade-service/blade-user/src/main/java/org/springblade/system/user/service/IUserService.java
  24. 56
      blade-service/blade-user/src/main/java/org/springblade/system/user/service/impl/UserServiceImpl.java

5
blade-common/src/main/java/org/springblade/common/constant/CommonConstant.java

@ -53,6 +53,11 @@ public interface CommonConstant {
*/
String DEFAULT_PASSWORD = "123456";
/**
* 默认密码参数值
*/
String DEFAULT_PARAM_PASSWORD = "account.initPassword";
/**
* 默认排序字段
*/

167
blade-service-api/blade-system-api/src/main/java/org/springblade/system/cache/SysCache.java vendored

@ -19,10 +19,8 @@ package org.springblade.system.cache;
import org.springblade.core.cache.utils.CacheUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.system.entity.Dept;
import org.springblade.system.entity.Menu;
import org.springblade.system.entity.Role;
import org.springblade.system.entity.Tenant;
import org.springblade.core.tool.utils.StringPool;
import org.springblade.system.entity.*;
import org.springblade.system.feign.ISysClient;
import java.util.ArrayList;
@ -39,11 +37,17 @@ import static org.springblade.core.cache.constant.CacheConstant.SYS_CACHE;
public class SysCache {
private static final String MENU_ID = "menu:id:";
private static final String DEPT_ID = "dept:id:";
private static final String DEPT_NAME = "dept:name:";
private static final String DEPT_NAME_ID = "deptName:id:";
private static final String DEPT_NAMES_ID = "deptNames:id:";
private static final String DEPT_CHILD_ID = "deptChild:id:";
private static final String DEPT_CHILDIDS_ID = "deptChildIds:id:";
private static final String POST_ID = "post:id:";
private static final String POST_NAME = "post:name:";
private static final String POST_NAME_ID = "postName:id:";
private static final String POST_NAMES_ID = "postNames:id:";
private static final String ROLE_ID = "role:id:";
private static final String ROLE_NAME = "role:name:";
private static final String ROLE_NAME_ID = "roleName:id:";
private static final String ROLE_NAMES_ID = "roleNames:id:";
private static final String ROLE_ALIAS_ID = "roleAlias:id:";
@ -87,53 +91,28 @@ public class SysCache {
}
/**
* 获取部门名
*
* @param id 主键
* @return 部门名
*/
public static String getDeptName(Long id) {
return CacheUtil.get(SYS_CACHE, DEPT_NAME_ID, id, () -> {
R<String> result = getSysClient().getDeptName(id);
return result.getData();
});
}
/**
* 获取角色
* 获取部门id
*
* @param id 主键
* @return Role
* @param tenantId 租户id
* @param deptNames 部门名
* @return
*/
public static Role getRole(Long id) {
return CacheUtil.get(SYS_CACHE, ROLE_ID, id, () -> {
R<Role> result = getSysClient().getRole(id);
public static String getDeptIds(String tenantId, String deptNames) {
return CacheUtil.get(SYS_CACHE, DEPT_NAME, tenantId + StringPool.DASH + deptNames, () -> {
R<String> result = getSysClient().getDeptIds(tenantId, deptNames);
return result.getData();
});
}
/**
* 获取角色名
*
* @param id 主键
* @return 角色名
*/
public static String getRoleName(Long id) {
return CacheUtil.get(SYS_CACHE, ROLE_NAME_ID, id, () -> {
R<String> result = getSysClient().getRoleName(id);
return result.getData();
});
}
/**
* 获取角色别名
* 获取部门名
*
* @param id 主键
* @return 角色别
* @return 部门名
*/
public static String getRoleAlias(Long id) {
return CacheUtil.get(SYS_CACHE, ROLE_ALIAS_ID, id, () -> {
R<String> result = getSysClient().getRoleAlias(id);
public static String getDeptName(Long id) {
return CacheUtil.get(SYS_CACHE, DEPT_NAME_ID, id, () -> {
R<String> result = getSysClient().getDeptName(id);
return result.getData();
});
}
@ -189,6 +168,112 @@ public class SysCache {
return deptIdList;
}
/**
* 获取岗位
*
* @param id 主键
* @return
*/
public static Post getPost(Long id) {
return CacheUtil.get(SYS_CACHE, POST_ID, id, () -> {
R<Post> result = getSysClient().getPost(id);
return result.getData();
});
}
/**
* 获取岗位id
*
* @param tenantId 租户id
* @param postNames 岗位名
* @return
*/
public static String getPostIds(String tenantId, String postNames) {
return CacheUtil.get(SYS_CACHE, POST_NAME, tenantId + StringPool.DASH + postNames, () -> {
R<String> result = getSysClient().getPostIds(tenantId, postNames);
return result.getData();
});
}
/**
* 获取岗位名
*
* @param id 主键
* @return 岗位名
*/
public static String getPostName(Long id) {
return CacheUtil.get(SYS_CACHE, POST_NAME_ID, id, () -> {
R<String> result = getSysClient().getPostName(id);
return result.getData();
});
}
/**
* 获取岗位名集合
*
* @param postIds 主键集合
* @return 岗位名
*/
public static List<String> getPostNames(String postIds) {
return CacheUtil.get(SYS_CACHE, POST_NAMES_ID, postIds, () -> {
R<List<String>> result = getSysClient().getPostNames(postIds);
return result.getData();
});
}
/**
* 获取角色
*
* @param id 主键
* @return Role
*/
public static Role getRole(Long id) {
return CacheUtil.get(SYS_CACHE, ROLE_ID, id, () -> {
R<Role> result = getSysClient().getRole(id);
return result.getData();
});
}
/**
* 获取角色id
*
* @param tenantId 租户id
* @param roleNames 角色名
* @return
*/
public static String getRoleIds(String tenantId, String roleNames) {
return CacheUtil.get(SYS_CACHE, ROLE_NAME, tenantId + StringPool.DASH + roleNames, () -> {
R<String> result = getSysClient().getRoleIds(tenantId, roleNames);
return result.getData();
});
}
/**
* 获取角色名
*
* @param id 主键
* @return 角色名
*/
public static String getRoleName(Long id) {
return CacheUtil.get(SYS_CACHE, ROLE_NAME_ID, id, () -> {
R<String> result = getSysClient().getRoleName(id);
return result.getData();
});
}
/**
* 获取角色别名
*
* @param id 主键
* @return 角色别名
*/
public static String getRoleAlias(Long id) {
return CacheUtil.get(SYS_CACHE, ROLE_ALIAS_ID, id, () -> {
R<String> result = getSysClient().getRoleAlias(id);
return result.getData();
});
}
/**
* 获取角色名集合
*

99
blade-service-api/blade-system-api/src/main/java/org/springblade/system/feign/ISysClient.java

@ -39,10 +39,16 @@ public interface ISysClient {
String API_PREFIX = "/client";
String MENU = API_PREFIX + "/menu";
String DEPT = API_PREFIX + "/dept";
String DEPT_IDS = API_PREFIX + "/dept-ids";
String DEPT_NAME = API_PREFIX + "/dept-name";
String DEPT_NAMES = API_PREFIX + "/dept-names";
String DEPT_CHILD = API_PREFIX + "/dept-child";
String POST = API_PREFIX + "/post";
String POST_IDS = API_PREFIX + "/post-ids";
String POST_NAME = API_PREFIX + "/post-name";
String POST_NAMES = API_PREFIX + "/post-names";
String ROLE = API_PREFIX + "/role";
String ROLE_IDS = API_PREFIX + "/role-ids";
String ROLE_NAME = API_PREFIX + "/role-name";
String ROLE_NAMES = API_PREFIX + "/role-names";
String ROLE_ALIAS = API_PREFIX + "/role-alias";
@ -70,6 +76,16 @@ public interface ISysClient {
@GetMapping(DEPT)
R<Dept> getDept(@RequestParam("id") Long id);
/**
* 获取部门id
*
* @param tenantId 租户id
* @param deptNames 部门名
* @return 部门id
*/
@GetMapping(DEPT_IDS)
R<String> getDeptIds(@RequestParam("tenantId") String tenantId, @RequestParam("deptNames") String deptNames);
/**
* 获取部门名
*
@ -79,6 +95,61 @@ public interface ISysClient {
@GetMapping(DEPT_NAME)
R<String> getDeptName(@RequestParam("id") Long id);
/**
* 获取部门名
*
* @param deptIds 主键
* @return
*/
@GetMapping(DEPT_NAMES)
R<List<String>> getDeptNames(@RequestParam("deptIds") String deptIds);
/**
* 获取子部门ID
*
* @param deptId
* @return
*/
@GetMapping(DEPT_CHILD)
R<List<Dept>> getDeptChild(@RequestParam("deptId") Long deptId);
/**
* 获取岗位
*
* @param id 主键
* @return Post
*/
@GetMapping(POST)
R<Post> getPost(@RequestParam("id") Long id);
/**
* 获取岗位id
*
* @param tenantId 租户id
* @param postNames 岗位名
* @return 岗位id
*/
@GetMapping(POST_IDS)
R<String> getPostIds(@RequestParam("tenantId") String tenantId, @RequestParam("postNames") String postNames);
/**
* 获取岗位名
*
* @param id 主键
* @return 岗位名
*/
@GetMapping(POST_NAME)
R<String> getPostName(@RequestParam("id") Long id);
/**
* 获取岗位名
*
* @param postIds 主键
* @return
*/
@GetMapping(POST_NAMES)
R<List<String>> getPostNames(@RequestParam("postIds") String postIds);
/**
* 获取角色
*
@ -88,6 +159,16 @@ public interface ISysClient {
@GetMapping(ROLE)
R<Role> getRole(@RequestParam("id") Long id);
/**
* 获取角色id
*
* @param tenantId 租户id
* @param roleNames 角色名
* @return 角色id
*/
@GetMapping(ROLE_IDS)
R<String> getRoleIds(@RequestParam("tenantId") String tenantId, @RequestParam("roleNames") String roleNames);
/**
* 获取角色名
*
@ -106,24 +187,6 @@ public interface ISysClient {
@GetMapping(ROLE_ALIAS)
R<String> getRoleAlias(@RequestParam("id") Long id);
/**
* 获取部门名
*
* @param deptIds 主键
* @return
*/
@GetMapping(DEPT_NAMES)
R<List<String>> getDeptNames(@RequestParam("deptIds") String deptIds);
/**
* 获取子部门ID
*
* @param deptId
* @return
*/
@GetMapping(DEPT_CHILD)
R<List<Dept>> getDeptChild(@RequestParam("deptId") Long deptId);
/**
* 获取角色名
*

40
blade-service-api/blade-system-api/src/main/java/org/springblade/system/feign/ISysClientFallback.java

@ -40,33 +40,63 @@ public class ISysClientFallback implements ISysClient {
return R.fail("获取数据失败");
}
@Override
public R<String> getDeptIds(String tenantId, String deptNames) {
return R.fail("获取数据失败");
}
@Override
public R<String> getDeptName(Long id) {
return R.fail("获取数据失败");
}
@Override
public R<Role> getRole(Long id) {
public R<List<String>> getDeptNames(String deptIds) {
return R.fail("获取数据失败");
}
@Override
public R<String> getRoleName(Long id) {
public R<List<Dept>> getDeptChild(Long deptId) {
return R.fail("获取数据失败");
}
@Override
public R<String> getRoleAlias(Long id) {
public R<Post> getPost(Long id) {
return R.fail("获取数据失败");
}
@Override
public R<List<String>> getDeptNames(String deptIds) {
public R<String> getPostIds(String tenantId, String postNames) {
return R.fail("获取数据失败");
}
@Override
public R<List<Dept>> getDeptChild(Long deptId) {
public R<String> getPostName(Long id) {
return R.fail("获取数据失败");
}
@Override
public R<List<String>> getPostNames(String postIds) {
return R.fail("获取数据失败");
}
@Override
public R<Role> getRole(Long id) {
return R.fail("获取数据失败");
}
@Override
public R<String> getRoleIds(String tenantId, String roleNames) {
return R.fail("获取数据失败");
}
@Override
public R<String> getRoleName(Long id) {
return R.fail("获取数据失败");
}
@Override
public R<String> getRoleAlias(Long id) {
return R.fail("获取数据失败");
}

24
blade-service-api/blade-user-api/src/main/java/org/springblade/system/user/cache/UserCache.java vendored

@ -20,6 +20,7 @@ import org.springblade.core.cache.utils.CacheUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.core.tool.utils.StringPool;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.system.user.entity.User;
import org.springblade.system.user.feign.IUserClient;
@ -34,6 +35,7 @@ import static org.springblade.core.launch.constant.FlowConstant.TASK_USR_PREFIX;
*/
public class UserCache {
private static final String USER_CACHE_ID = "user:id:";
private static final String USER_CACHE_ACCOUNT = "user:account:";
private static IUserClient userClient;
@ -56,7 +58,7 @@ public class UserCache {
}
/**
* 获取用户
* 获取用户
*
* @param userId 用户id
* @return
@ -68,4 +70,24 @@ public class UserCache {
});
}
/**
* 获取用户
*
* @param tenantId 租户id
* @param account 账号名
* @return
*/
public static User getUser(String tenantId, String account) {
User userCache = CacheUtil.get(USER_CACHE, USER_CACHE_ACCOUNT, tenantId + StringPool.DASH + account, User.class);
if (userCache == null || userCache.getId() == null) {
R<User> result = getUserClient().userByAccount(tenantId, account);
User user = result.getData();
if (user != null && user.getId() != null) {
CacheUtil.put(USER_CACHE, USER_CACHE_ACCOUNT, tenantId + StringPool.DASH + account, user);
return user;
}
}
return userCache;
}
}

12
blade-service-api/blade-user-api/src/main/java/org/springblade/system/user/feign/IUserClient.java

@ -40,6 +40,7 @@ public interface IUserClient {
String API_PREFIX = "/client";
String USER_INFO = API_PREFIX + "/user-info";
String USER_INFO_BY_ID = API_PREFIX + "/user-info-by-id";
String USER_INFO_BY_ACCOUNT = API_PREFIX + "/user-info-by-account";
String SAVE_USER = API_PREFIX + "/save-user";
/**
@ -51,6 +52,17 @@ public interface IUserClient {
@GetMapping(USER_INFO_BY_ID)
R<User> userInfoById(@RequestParam("userId") Long userId);
/**
* 根据账号获取用户信息
*
* @param tenantId 租户id
* @param account 账号
* @return
*/
@GetMapping(USER_INFO_BY_ACCOUNT)
R<User> userByAccount(@RequestParam("tenantId") String tenantId, @RequestParam("account") String account);
/**
* 获取用户信息
*

56
blade-service/blade-system/src/main/java/org/springblade/system/feign/SysClient.java

@ -38,6 +38,8 @@ public class SysClient implements ISysClient {
private IDeptService deptService;
private IPostService postService;
private IRoleService roleService;
private IMenuService menuService;
@ -58,18 +60,60 @@ public class SysClient implements ISysClient {
return R.data(deptService.getById(id));
}
@Override
public R<String> getDeptIds(String tenantId, String deptNames) {
return R.data(deptService.getDeptIds(tenantId, deptNames));
}
@Override
@GetMapping(DEPT_NAME)
public R<String> getDeptName(Long id) {
return R.data(deptService.getById(id).getDeptName());
}
@Override
@GetMapping(DEPT_NAMES)
public R<List<String>> getDeptNames(String deptIds) {
return R.data(deptService.getDeptNames(deptIds));
}
@Override
@GetMapping(DEPT_CHILD)
public R<List<Dept>> getDeptChild(Long deptId) {
return R.data(deptService.getDeptChild(deptId));
}
@Override
public R<Post> getPost(Long id) {
return R.data(postService.getById(id));
}
@Override
public R<String> getPostIds(String tenantId, String postNames) {
return R.data(postService.getPostIds(tenantId, postNames));
}
@Override
public R<String> getPostName(Long id) {
return R.data(postService.getById(id).getPostName());
}
@Override
public R<List<String>> getPostNames(String postIds) {
return R.data(postService.getPostNames(postIds));
}
@Override
@GetMapping(ROLE)
public R<Role> getRole(Long id) {
return R.data(roleService.getById(id));
}
@Override
public R<String> getRoleIds(String tenantId, String roleNames) {
return R.data(roleService.getRoleIds(tenantId, roleNames));
}
@Override
@GetMapping(ROLE_NAME)
public R<String> getRoleName(Long id) {
@ -82,18 +126,6 @@ public class SysClient implements ISysClient {
return R.data(roleService.getById(id).getRoleAlias());
}
@Override
@GetMapping(DEPT_NAMES)
public R<List<String>> getDeptNames(String deptIds) {
return R.data(deptService.getDeptNames(deptIds));
}
@Override
@GetMapping(DEPT_CHILD)
public R<List<Dept>> getDeptChild(Long deptId) {
return R.data(deptService.getDeptChild(deptId));
}
@Override
@GetMapping(ROLE_NAMES)
public R<List<String>> getRoleNames(String roleIds) {

8
blade-service/blade-system/src/main/java/org/springblade/system/mapper/PostMapper.java

@ -39,4 +39,12 @@ public interface PostMapper extends BaseMapper<Post> {
*/
List<PostVO> selectPostPage(IPage page, PostVO post);
/**
* 获取岗位名
*
* @param ids
* @return
*/
List<String> getPostNames(Long[] ids);
}

13
blade-service/blade-system/src/main/java/org/springblade/system/mapper/PostMapper.xml

@ -24,4 +24,17 @@
select * from blade_post where is_deleted = 0
</select>
<select id="getPostNames" resultType="java.lang.String">
SELECT
post_name
FROM
blade_post
WHERE
id IN
<foreach collection="array" item="ids" index="index" open="(" close=")" separator=",">
#{ids}
</foreach>
and is_deleted = 0
</select>
</mapper>

9
blade-service/blade-system/src/main/java/org/springblade/system/service/IDeptService.java

@ -57,6 +57,15 @@ public interface IDeptService extends IService<Dept> {
*/
List<DeptVO> lazyTree(String tenantId, Long parentId);
/**
* 获取部门ID
*
* @param tenantId
* @param deptNames
* @return
*/
String getDeptIds(String tenantId, String deptNames);
/**
* 获取部门名
*

19
blade-service/blade-system/src/main/java/org/springblade/system/service/IPostService.java

@ -21,6 +21,8 @@ import org.springblade.core.mp.base.BaseService;
import org.springblade.system.entity.Post;
import org.springblade.system.vo.PostVO;
import java.util.List;
/**
* 岗位表 服务类
*
@ -37,4 +39,21 @@ public interface IPostService extends BaseService<Post> {
*/
IPage<PostVO> selectPostPage(IPage<PostVO> page, PostVO post);
/**
* 获取岗位ID
*
* @param tenantId
* @param postNames
* @return
*/
String getPostIds(String tenantId, String postNames);
/**
* 获取岗位名
*
* @param postIds
* @return
*/
List<String> getPostNames(String postIds);
}

9
blade-service/blade-system/src/main/java/org/springblade/system/service/IRoleService.java

@ -59,6 +59,15 @@ public interface IRoleService extends IService<Role> {
*/
boolean grant(@NotEmpty List<Long> roleIds, List<Long> menuIds, List<Long> dataScopeIds, List<Long> apiScopeIds);
/**
* 获取角色ID
*
* @param tenantId
* @param roleNames
* @return
*/
String getRoleIds(String tenantId, String roleNames);
/**
* 获取角色名
*

9
blade-service/blade-system/src/main/java/org/springblade/system/service/impl/DeptServiceImpl.java

@ -32,6 +32,7 @@ import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* 服务实现类
@ -73,6 +74,14 @@ public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements ID
return ForestNodeMerger.merge(baseMapper.lazyTree(tenantId, parentId));
}
@Override
public String getDeptIds(String tenantId, String deptNames) {
List<Dept> deptList = baseMapper.selectList(Wrappers.<Dept>query().lambda().eq(Dept::getTenantId, tenantId).in(Dept::getDeptName, Func.toStrList(deptNames)));
if (deptList != null && deptList.size() > 0) {
return deptList.stream().map(dept -> Func.toStr(dept.getId())).distinct().collect(Collectors.joining(","));
}
return null;
}
@Override
public List<String> getDeptNames(String deptIds) {

19
blade-service/blade-system/src/main/java/org/springblade/system/service/impl/PostServiceImpl.java

@ -17,13 +17,18 @@
package org.springblade.system.service.impl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.tool.utils.Func;
import org.springblade.system.entity.Post;
import org.springblade.system.mapper.PostMapper;
import org.springblade.system.service.IPostService;
import org.springblade.system.vo.PostVO;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;
/**
* 岗位表 服务实现类
*
@ -37,4 +42,18 @@ public class PostServiceImpl extends BaseServiceImpl<PostMapper, Post> implement
return page.setRecords(baseMapper.selectPostPage(page, post));
}
@Override
public String getPostIds(String tenantId, String postNames) {
List<Post> postList = baseMapper.selectList(Wrappers.<Post>query().lambda().eq(Post::getTenantId, tenantId).in(Post::getPostName, Func.toStrList(postNames)));
if (postList != null && postList.size() > 0) {
return postList.stream().map(post -> Func.toStr(post.getId())).distinct().collect(Collectors.joining(","));
}
return null;
}
@Override
public List<String> getPostNames(String postIds) {
return baseMapper.getPostNames(Func.toLongArray(postIds));
}
}

10
blade-service/blade-system/src/main/java/org/springblade/system/service/impl/RoleServiceImpl.java

@ -43,6 +43,7 @@ import org.springframework.validation.annotation.Validated;
import javax.validation.constraints.NotEmpty;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import static org.springblade.common.constant.CommonConstant.API_SCOPE_CATEGORY;
import static org.springblade.common.constant.CommonConstant.DATA_SCOPE_CATEGORY;
@ -122,6 +123,15 @@ public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IR
return true;
}
@Override
public String getRoleIds(String tenantId, String roleNames) {
List<Role> roleList = baseMapper.selectList(Wrappers.<Role>query().lambda().eq(Role::getTenantId, tenantId).in(Role::getRoleName, Func.toStrList(roleNames)));
if (roleList != null && roleList.size() > 0) {
return roleList.stream().map(role -> Func.toStr(role.getId())).distinct().collect(Collectors.joining(","));
}
return null;
}
@Override
public List<String> getRoleNames(String roleIds) {
return baseMapper.getRoleNames(Func.toLongArray(roleIds));

4
blade-service/blade-user/pom.xml

@ -20,6 +20,10 @@
<groupId>org.springblade</groupId>
<artifactId>blade-core-boot</artifactId>
</dependency>
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-starter-excel</artifactId>
</dependency>
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-starter-swagger</artifactId>

42
blade-service/blade-user/src/main/java/org/springblade/system/user/controller/UserController.java

@ -25,6 +25,7 @@ import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.excel.util.ExcelUtil;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
@ -33,16 +34,22 @@ import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.constant.BladeConstant;
import org.springblade.core.tool.constant.RoleConstant;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.StringPool;
import org.springblade.system.user.entity.User;
import org.springblade.system.user.excel.UserExcel;
import org.springblade.system.user.excel.UserImporter;
import org.springblade.system.user.service.IUserService;
import org.springblade.system.user.vo.UserVO;
import org.springblade.system.user.wrapper.UserWrapper;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@ -209,4 +216,39 @@ public class UserController {
return R.data(list);
}
/**
* 导入用户
*/
@PostMapping("import-user")
@ApiOperationSupport(order = 12)
@ApiOperation(value = "导入用户", notes = "传入excel")
public R importUser(MultipartFile file, Integer isCovered) {
UserImporter userImporter = new UserImporter(userService, isCovered == 1);
ExcelUtil.save(file, userImporter, UserExcel.class);
return R.success("操作成功");
}
/**
* 导出用户
*/
@GetMapping("export-user")
@ApiOperationSupport(order = 13)
@ApiOperation(value = "导出用户", notes = "传入user")
public void exportUser(@ApiIgnore @RequestParam Map<String, Object> user, BladeUser bladeUser, HttpServletResponse response) {
QueryWrapper<UserExcel> queryWrapper = Condition.getQueryWrapper(user, UserExcel.class);
List<UserExcel> list = userService.exportUser((!AuthUtil.isAdministrator()) ? queryWrapper.lambda().eq(UserExcel::getTenantId, bladeUser.getTenantId()) : queryWrapper);
ExcelUtil.export(response, "用户数据" + DateUtil.time(), "用户数据表", list, UserExcel.class);
}
/**
* 导出模板
*/
@GetMapping("export-template")
@ApiOperationSupport(order = 14)
@ApiOperation(value = "导出模板")
public void exportUser(HttpServletResponse response) {
List<UserExcel> list = new ArrayList<>();
ExcelUtil.export(response, "用户数据模板", "用户数据表", list, UserExcel.class);
}
}

89
blade-service/blade-user/src/main/java/org/springblade/system/user/excel/UserExcel.java

@ -0,0 +1,89 @@
/*
* Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of the dreamlu.net developer nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* Author: Chill 庄骞 (smallchill@163.com)
*/
package org.springblade.system.user.excel;
import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.excel.annotation.write.style.ContentRowHeight;
import com.alibaba.excel.annotation.write.style.HeadRowHeight;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
/**
* UserDTO
*
* @author Chill
*/
@Data
@ColumnWidth(25)
@HeadRowHeight(20)
@ContentRowHeight(18)
public class UserExcel implements Serializable {
private static final long serialVersionUID = 1L;
@ColumnWidth(15)
@ExcelProperty("租户编号")
private String tenantId;
@ColumnWidth(15)
@ExcelProperty("账户")
private String account;
@ColumnWidth(10)
@ExcelProperty("昵称")
private String name;
@ColumnWidth(10)
@ExcelProperty("姓名")
private String realName;
@ExcelProperty("邮箱")
private String email;
@ColumnWidth(15)
@ExcelProperty("手机")
private String phone;
@ExcelIgnore
@ExcelProperty("角色ID")
private String roleId;
@ExcelIgnore
@ExcelProperty("部门ID")
private String deptId;
@ExcelIgnore
@ExcelProperty("岗位ID")
private String postId;
@ExcelProperty("角色名称")
private String roleName;
@ExcelProperty("部门名称")
private String deptName;
@ExcelProperty("岗位名称")
private String postName;
@ColumnWidth(20)
@ExcelProperty("生日")
private Date birthday;
}

40
blade-service/blade-user/src/main/java/org/springblade/system/user/excel/UserImporter.java

@ -0,0 +1,40 @@
/*
* Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of the dreamlu.net developer nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* Author: Chill 庄骞 (smallchill@163.com)
*/
package org.springblade.system.user.excel;
import lombok.RequiredArgsConstructor;
import org.springblade.core.excel.support.ExcelImporter;
import org.springblade.system.user.service.IUserService;
import java.util.List;
/**
* 用户数据导入类
*
* @author Chill
*/
@RequiredArgsConstructor
public class UserImporter implements ExcelImporter<UserExcel> {
private final IUserService service;
private final Boolean isCovered;
@Override
public void save(List<UserExcel> data) {
service.importUser(data, isCovered);
}
}

6
blade-service/blade-user/src/main/java/org/springblade/system/user/feign/UserClient.java

@ -43,6 +43,12 @@ public class UserClient implements IUserClient {
return R.data(service.getById(userId));
}
@Override
@GetMapping(USER_INFO_BY_ACCOUNT)
public R<User> userByAccount(String tenantId, String account) {
return R.data(service.userByAccount(tenantId, account));
}
@Override
@GetMapping(USER_INFO)
public R<UserInfo> userInfo(String tenantId, String account) {

10
blade-service/blade-user/src/main/java/org/springblade/system/user/mapper/UserMapper.java

@ -16,10 +16,12 @@
*/
package org.springblade.system.user.mapper;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.ibatis.annotations.Param;
import org.springblade.system.user.entity.User;
import org.springblade.system.user.excel.UserExcel;
import java.util.List;
@ -50,4 +52,12 @@ public interface UserMapper extends BaseMapper<User> {
*/
User getUser(String tenantId, String account);
/**
* 获取导出用户数据
*
* @param queryWrapper
* @return
*/
List<UserExcel> exportUser(@Param("ew") Wrapper<UserExcel> queryWrapper);
}

5
blade-service/blade-user/src/main/java/org/springblade/system/user/mapper/UserMapper.xml

@ -74,4 +74,9 @@
WHERE
tenant_id = #{param1} and account = #{param2} and is_deleted = 0
</select>
<select id="exportUser" resultType="org.springblade.system.user.excel.UserExcel">
SELECT id, tenant_id, account, name, real_name, email, phone, birthday, role_id, dept_id, post_id FROM blade_user ${ew.customSqlSegment}
</select>
</mapper>

29
blade-service/blade-user/src/main/java/org/springblade/system/user/service/IUserService.java

@ -17,10 +17,14 @@
package org.springblade.system.user.service;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.core.mp.base.BaseService;
import org.springblade.system.user.entity.User;
import org.springblade.system.user.entity.UserInfo;
import org.springblade.system.user.excel.UserExcel;
import java.util.List;
/**
* 服务类
@ -73,6 +77,15 @@ public interface IUserService extends BaseService<User> {
*/
UserInfo userInfo(String tenantId, String account);
/**
* 根据账号获取用户
*
* @param tenantId
* @param account
* @return
*/
User userByAccount(String tenantId, String account);
/**
* 给用户设置角色
*
@ -108,4 +121,20 @@ public interface IUserService extends BaseService<User> {
* @return
*/
boolean removeUser(String userIds);
/**
* 导入用户数据
*
* @param data
* @return
*/
void importUser(List<UserExcel> data, Boolean isCovered);
/**
* 获取导出用户数据
*
* @param queryWrapper
* @return
*/
List<UserExcel> exportUser(Wrapper<UserExcel> queryWrapper);
}

56
blade-service/blade-user/src/main/java/org/springblade/system/user/service/impl/UserServiceImpl.java

@ -17,6 +17,7 @@
package org.springblade.system.user.service.impl;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
@ -26,16 +27,16 @@ import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.secure.utils.SecureUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.constant.BladeConstant;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.DigestUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.core.tool.utils.*;
import org.springblade.system.cache.ParamCache;
import org.springblade.system.cache.SysCache;
import org.springblade.system.entity.Tenant;
import org.springblade.system.feign.ISysClient;
import org.springblade.system.user.cache.UserCache;
import org.springblade.system.user.entity.User;
import org.springblade.system.user.entity.UserDept;
import org.springblade.system.user.entity.UserInfo;
import org.springblade.system.user.excel.UserExcel;
import org.springblade.system.user.mapper.UserMapper;
import org.springblade.system.user.service.IUserDeptService;
import org.springblade.system.user.service.IUserService;
@ -44,6 +45,9 @@ import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import static org.springblade.common.constant.CommonConstant.DEFAULT_PARAM_PASSWORD;
/**
* 服务实现类
@ -123,6 +127,11 @@ public class UserServiceImpl extends BaseServiceImpl<UserMapper, User> implement
return page.setRecords(baseMapper.selectUserPage(page, user, deptIdList, tenantId));
}
@Override
public User userByAccount(String tenantId, String account) {
return baseMapper.selectOne(Wrappers.<User>query().lambda().eq(User::getTenantId, tenantId).eq(User::getAccount, account));
}
@Override
public UserInfo userInfo(String tenantId, String account) {
UserInfo userInfo = new UserInfo();
@ -173,4 +182,43 @@ public class UserServiceImpl extends BaseServiceImpl<UserMapper, User> implement
return deleteLogic(Func.toLongList(userIds));
}
@Override
@Transactional(rollbackFor = Exception.class)
public void importUser(List<UserExcel> data, Boolean isCovered) {
data.forEach(userExcel -> {
User user = Objects.requireNonNull(BeanUtil.copy(userExcel, User.class));
// 设置部门ID
user.setDeptId(SysCache.getDeptIds(userExcel.getTenantId(), userExcel.getDeptName()));
// 设置岗位ID
user.setPostId(SysCache.getPostIds(userExcel.getTenantId(), userExcel.getPostName()));
// 设置角色ID
user.setRoleId(SysCache.getRoleIds(userExcel.getTenantId(), userExcel.getRoleName()));
// 覆盖数据
if (isCovered) {
// 查询用户是否存在
User oldUser = UserCache.getUser(userExcel.getTenantId(), userExcel.getAccount());
if (oldUser != null && oldUser.getId() != null) {
user.setId(oldUser.getId());
this.updateUser(user);
return;
}
}
// 获取默认密码配置
String initPassword = ParamCache.getValue(DEFAULT_PARAM_PASSWORD);
user.setPassword(DigestUtil.encrypt(initPassword));
this.submit(user);
});
}
@Override
public List<UserExcel> exportUser(Wrapper<UserExcel> queryWrapper) {
List<UserExcel> userList = baseMapper.exportUser(queryWrapper);
userList.forEach(user -> {
user.setRoleName(StringUtil.join(SysCache.getRoleNames(user.getRoleId())));
user.setDeptName(StringUtil.join(SysCache.getDeptNames(user.getDeptId())));
user.setPostName(StringUtil.join(SysCache.getPostNames(user.getPostId())));
});
return userList;
}
}

Loading…
Cancel
Save