diff --git a/blade-common/src/main/java/org/springblade/common/constant/CommonConstant.java b/blade-common/src/main/java/org/springblade/common/constant/CommonConstant.java index b27ab07c..2735c009 100644 --- a/blade-common/src/main/java/org/springblade/common/constant/CommonConstant.java +++ b/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"; + /** * 默认排序字段 */ diff --git a/blade-service-api/blade-system-api/src/main/java/org/springblade/system/cache/SysCache.java b/blade-service-api/blade-system-api/src/main/java/org/springblade/system/cache/SysCache.java index 49cdfe8f..3c3f2b69 100644 --- a/blade-service-api/blade-system-api/src/main/java/org/springblade/system/cache/SysCache.java +++ b/blade-service-api/blade-system-api/src/main/java/org/springblade/system/cache/SysCache.java @@ -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 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 result = getSysClient().getRole(id); + public static String getDeptIds(String tenantId, String deptNames) { + return CacheUtil.get(SYS_CACHE, DEPT_NAME, tenantId + StringPool.DASH + deptNames, () -> { + R 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 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 result = getSysClient().getRoleAlias(id); + public static String getDeptName(Long id) { + return CacheUtil.get(SYS_CACHE, DEPT_NAME_ID, id, () -> { + R 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 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 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 result = getSysClient().getPostName(id); + return result.getData(); + }); + } + + /** + * 获取岗位名集合 + * + * @param postIds 主键集合 + * @return 岗位名 + */ + public static List getPostNames(String postIds) { + return CacheUtil.get(SYS_CACHE, POST_NAMES_ID, postIds, () -> { + R> 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 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 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 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 result = getSysClient().getRoleAlias(id); + return result.getData(); + }); + } + /** * 获取角色名集合 * diff --git a/blade-service-api/blade-system-api/src/main/java/org/springblade/system/feign/ISysClient.java b/blade-service-api/blade-system-api/src/main/java/org/springblade/system/feign/ISysClient.java index 96d62fff..74655eea 100644 --- a/blade-service-api/blade-system-api/src/main/java/org/springblade/system/feign/ISysClient.java +++ b/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 getDept(@RequestParam("id") Long id); + /** + * 获取部门id + * + * @param tenantId 租户id + * @param deptNames 部门名 + * @return 部门id + */ + @GetMapping(DEPT_IDS) + R getDeptIds(@RequestParam("tenantId") String tenantId, @RequestParam("deptNames") String deptNames); + /** * 获取部门名 * @@ -79,6 +95,61 @@ public interface ISysClient { @GetMapping(DEPT_NAME) R getDeptName(@RequestParam("id") Long id); + /** + * 获取部门名 + * + * @param deptIds 主键 + * @return + */ + @GetMapping(DEPT_NAMES) + R> getDeptNames(@RequestParam("deptIds") String deptIds); + + /** + * 获取子部门ID + * + * @param deptId + * @return + */ + @GetMapping(DEPT_CHILD) + R> getDeptChild(@RequestParam("deptId") Long deptId); + + /** + * 获取岗位 + * + * @param id 主键 + * @return Post + */ + @GetMapping(POST) + R getPost(@RequestParam("id") Long id); + + /** + * 获取岗位id + * + * @param tenantId 租户id + * @param postNames 岗位名 + * @return 岗位id + */ + @GetMapping(POST_IDS) + R getPostIds(@RequestParam("tenantId") String tenantId, @RequestParam("postNames") String postNames); + + /** + * 获取岗位名 + * + * @param id 主键 + * @return 岗位名 + */ + @GetMapping(POST_NAME) + R getPostName(@RequestParam("id") Long id); + + /** + * 获取岗位名 + * + * @param postIds 主键 + * @return + */ + @GetMapping(POST_NAMES) + R> getPostNames(@RequestParam("postIds") String postIds); + /** * 获取角色 * @@ -88,6 +159,16 @@ public interface ISysClient { @GetMapping(ROLE) R getRole(@RequestParam("id") Long id); + /** + * 获取角色id + * + * @param tenantId 租户id + * @param roleNames 角色名 + * @return 角色id + */ + @GetMapping(ROLE_IDS) + R getRoleIds(@RequestParam("tenantId") String tenantId, @RequestParam("roleNames") String roleNames); + /** * 获取角色名 * @@ -106,24 +187,6 @@ public interface ISysClient { @GetMapping(ROLE_ALIAS) R getRoleAlias(@RequestParam("id") Long id); - /** - * 获取部门名 - * - * @param deptIds 主键 - * @return - */ - @GetMapping(DEPT_NAMES) - R> getDeptNames(@RequestParam("deptIds") String deptIds); - - /** - * 获取子部门ID - * - * @param deptId - * @return - */ - @GetMapping(DEPT_CHILD) - R> getDeptChild(@RequestParam("deptId") Long deptId); - /** * 获取角色名 * diff --git a/blade-service-api/blade-system-api/src/main/java/org/springblade/system/feign/ISysClientFallback.java b/blade-service-api/blade-system-api/src/main/java/org/springblade/system/feign/ISysClientFallback.java index c9bb38fb..e8964d4c 100644 --- a/blade-service-api/blade-system-api/src/main/java/org/springblade/system/feign/ISysClientFallback.java +++ b/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 getDeptIds(String tenantId, String deptNames) { + return R.fail("获取数据失败"); + } + @Override public R getDeptName(Long id) { return R.fail("获取数据失败"); } @Override - public R getRole(Long id) { + public R> getDeptNames(String deptIds) { return R.fail("获取数据失败"); } @Override - public R getRoleName(Long id) { + public R> getDeptChild(Long deptId) { return R.fail("获取数据失败"); } @Override - public R getRoleAlias(Long id) { + public R getPost(Long id) { return R.fail("获取数据失败"); } @Override - public R> getDeptNames(String deptIds) { + public R getPostIds(String tenantId, String postNames) { return R.fail("获取数据失败"); } @Override - public R> getDeptChild(Long deptId) { + public R getPostName(Long id) { + return R.fail("获取数据失败"); + } + + @Override + public R> getPostNames(String postIds) { + return R.fail("获取数据失败"); + } + + @Override + public R getRole(Long id) { + return R.fail("获取数据失败"); + } + + @Override + public R getRoleIds(String tenantId, String roleNames) { + return R.fail("获取数据失败"); + } + + @Override + public R getRoleName(Long id) { + return R.fail("获取数据失败"); + } + + @Override + public R getRoleAlias(Long id) { return R.fail("获取数据失败"); } diff --git a/blade-service-api/blade-user-api/src/main/java/org/springblade/system/user/cache/UserCache.java b/blade-service-api/blade-user-api/src/main/java/org/springblade/system/user/cache/UserCache.java index 643ddbc8..420f1a6d 100644 --- a/blade-service-api/blade-user-api/src/main/java/org/springblade/system/user/cache/UserCache.java +++ b/blade-service-api/blade-user-api/src/main/java/org/springblade/system/user/cache/UserCache.java @@ -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 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; + } + } diff --git a/blade-service-api/blade-user-api/src/main/java/org/springblade/system/user/feign/IUserClient.java b/blade-service-api/blade-user-api/src/main/java/org/springblade/system/user/feign/IUserClient.java index c7ed3297..109841f3 100644 --- a/blade-service-api/blade-user-api/src/main/java/org/springblade/system/user/feign/IUserClient.java +++ b/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 userInfoById(@RequestParam("userId") Long userId); + + /** + * 根据账号获取用户信息 + * + * @param tenantId 租户id + * @param account 账号 + * @return + */ + @GetMapping(USER_INFO_BY_ACCOUNT) + R userByAccount(@RequestParam("tenantId") String tenantId, @RequestParam("account") String account); + /** * 获取用户信息 * diff --git a/blade-service/blade-system/src/main/java/org/springblade/system/feign/SysClient.java b/blade-service/blade-system/src/main/java/org/springblade/system/feign/SysClient.java index f09574cf..51c4b5d3 100644 --- a/blade-service/blade-system/src/main/java/org/springblade/system/feign/SysClient.java +++ b/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 getDeptIds(String tenantId, String deptNames) { + return R.data(deptService.getDeptIds(tenantId, deptNames)); + } + @Override @GetMapping(DEPT_NAME) public R getDeptName(Long id) { return R.data(deptService.getById(id).getDeptName()); } + @Override + @GetMapping(DEPT_NAMES) + public R> getDeptNames(String deptIds) { + return R.data(deptService.getDeptNames(deptIds)); + } + + @Override + @GetMapping(DEPT_CHILD) + public R> getDeptChild(Long deptId) { + return R.data(deptService.getDeptChild(deptId)); + } + + @Override + public R getPost(Long id) { + return R.data(postService.getById(id)); + } + + @Override + public R getPostIds(String tenantId, String postNames) { + return R.data(postService.getPostIds(tenantId, postNames)); + } + + @Override + public R getPostName(Long id) { + return R.data(postService.getById(id).getPostName()); + } + + @Override + public R> getPostNames(String postIds) { + return R.data(postService.getPostNames(postIds)); + } + @Override @GetMapping(ROLE) public R getRole(Long id) { return R.data(roleService.getById(id)); } + @Override + public R getRoleIds(String tenantId, String roleNames) { + return R.data(roleService.getRoleIds(tenantId, roleNames)); + } + @Override @GetMapping(ROLE_NAME) public R 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> getDeptNames(String deptIds) { - return R.data(deptService.getDeptNames(deptIds)); - } - - @Override - @GetMapping(DEPT_CHILD) - public R> getDeptChild(Long deptId) { - return R.data(deptService.getDeptChild(deptId)); - } - @Override @GetMapping(ROLE_NAMES) public R> getRoleNames(String roleIds) { diff --git a/blade-service/blade-system/src/main/java/org/springblade/system/mapper/PostMapper.java b/blade-service/blade-system/src/main/java/org/springblade/system/mapper/PostMapper.java index dae70afa..67a41904 100644 --- a/blade-service/blade-system/src/main/java/org/springblade/system/mapper/PostMapper.java +++ b/blade-service/blade-system/src/main/java/org/springblade/system/mapper/PostMapper.java @@ -39,4 +39,12 @@ public interface PostMapper extends BaseMapper { */ List selectPostPage(IPage page, PostVO post); + /** + * 获取岗位名 + * + * @param ids + * @return + */ + List getPostNames(Long[] ids); + } diff --git a/blade-service/blade-system/src/main/java/org/springblade/system/mapper/PostMapper.xml b/blade-service/blade-system/src/main/java/org/springblade/system/mapper/PostMapper.xml index f216a2db..e4106d82 100644 --- a/blade-service/blade-system/src/main/java/org/springblade/system/mapper/PostMapper.xml +++ b/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 + + diff --git a/blade-service/blade-system/src/main/java/org/springblade/system/service/IDeptService.java b/blade-service/blade-system/src/main/java/org/springblade/system/service/IDeptService.java index 23793838..5b9fd051 100644 --- a/blade-service/blade-system/src/main/java/org/springblade/system/service/IDeptService.java +++ b/blade-service/blade-system/src/main/java/org/springblade/system/service/IDeptService.java @@ -57,6 +57,15 @@ public interface IDeptService extends IService { */ List lazyTree(String tenantId, Long parentId); + /** + * 获取部门ID + * + * @param tenantId + * @param deptNames + * @return + */ + String getDeptIds(String tenantId, String deptNames); + /** * 获取部门名 * diff --git a/blade-service/blade-system/src/main/java/org/springblade/system/service/IPostService.java b/blade-service/blade-system/src/main/java/org/springblade/system/service/IPostService.java index a688221d..c7c19dfc 100644 --- a/blade-service/blade-system/src/main/java/org/springblade/system/service/IPostService.java +++ b/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 { */ IPage selectPostPage(IPage page, PostVO post); + /** + * 获取岗位ID + * + * @param tenantId + * @param postNames + * @return + */ + String getPostIds(String tenantId, String postNames); + + /** + * 获取岗位名 + * + * @param postIds + * @return + */ + List getPostNames(String postIds); + } diff --git a/blade-service/blade-system/src/main/java/org/springblade/system/service/IRoleService.java b/blade-service/blade-system/src/main/java/org/springblade/system/service/IRoleService.java index 51cbee31..38d9854e 100644 --- a/blade-service/blade-system/src/main/java/org/springblade/system/service/IRoleService.java +++ b/blade-service/blade-system/src/main/java/org/springblade/system/service/IRoleService.java @@ -59,6 +59,15 @@ public interface IRoleService extends IService { */ boolean grant(@NotEmpty List roleIds, List menuIds, List dataScopeIds, List apiScopeIds); + /** + * 获取角色ID + * + * @param tenantId + * @param roleNames + * @return + */ + String getRoleIds(String tenantId, String roleNames); + /** * 获取角色名 * diff --git a/blade-service/blade-system/src/main/java/org/springblade/system/service/impl/DeptServiceImpl.java b/blade-service/blade-system/src/main/java/org/springblade/system/service/impl/DeptServiceImpl.java index cdef08c0..bb3bf3e3 100644 --- a/blade-service/blade-system/src/main/java/org/springblade/system/service/impl/DeptServiceImpl.java +++ b/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 implements ID return ForestNodeMerger.merge(baseMapper.lazyTree(tenantId, parentId)); } + @Override + public String getDeptIds(String tenantId, String deptNames) { + List deptList = baseMapper.selectList(Wrappers.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 getDeptNames(String deptIds) { diff --git a/blade-service/blade-system/src/main/java/org/springblade/system/service/impl/PostServiceImpl.java b/blade-service/blade-system/src/main/java/org/springblade/system/service/impl/PostServiceImpl.java index eefd909f..916ce2f3 100644 --- a/blade-service/blade-system/src/main/java/org/springblade/system/service/impl/PostServiceImpl.java +++ b/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 implement return page.setRecords(baseMapper.selectPostPage(page, post)); } + @Override + public String getPostIds(String tenantId, String postNames) { + List postList = baseMapper.selectList(Wrappers.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 getPostNames(String postIds) { + return baseMapper.getPostNames(Func.toLongArray(postIds)); + } + } diff --git a/blade-service/blade-system/src/main/java/org/springblade/system/service/impl/RoleServiceImpl.java b/blade-service/blade-system/src/main/java/org/springblade/system/service/impl/RoleServiceImpl.java index c4b20438..6b1d7f6c 100644 --- a/blade-service/blade-system/src/main/java/org/springblade/system/service/impl/RoleServiceImpl.java +++ b/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 implements IR return true; } + @Override + public String getRoleIds(String tenantId, String roleNames) { + List roleList = baseMapper.selectList(Wrappers.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 getRoleNames(String roleIds) { return baseMapper.getRoleNames(Func.toLongArray(roleIds)); diff --git a/blade-service/blade-user/pom.xml b/blade-service/blade-user/pom.xml index 33aed430..d3772fde 100644 --- a/blade-service/blade-user/pom.xml +++ b/blade-service/blade-user/pom.xml @@ -20,6 +20,10 @@ org.springblade blade-core-boot + + org.springblade + blade-starter-excel + org.springblade blade-starter-swagger diff --git a/blade-service/blade-user/src/main/java/org/springblade/system/user/controller/UserController.java b/blade-service/blade-user/src/main/java/org/springblade/system/user/controller/UserController.java index 45b4ccb5..e3418b94 100644 --- a/blade-service/blade-user/src/main/java/org/springblade/system/user/controller/UserController.java +++ b/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 user, BladeUser bladeUser, HttpServletResponse response) { + QueryWrapper queryWrapper = Condition.getQueryWrapper(user, UserExcel.class); + List 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 list = new ArrayList<>(); + ExcelUtil.export(response, "用户数据模板", "用户数据表", list, UserExcel.class); + } + } diff --git a/blade-service/blade-user/src/main/java/org/springblade/system/user/excel/UserExcel.java b/blade-service/blade-user/src/main/java/org/springblade/system/user/excel/UserExcel.java new file mode 100644 index 00000000..63e14805 --- /dev/null +++ b/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; + +} diff --git a/blade-service/blade-user/src/main/java/org/springblade/system/user/excel/UserImporter.java b/blade-service/blade-user/src/main/java/org/springblade/system/user/excel/UserImporter.java new file mode 100644 index 00000000..0f450d91 --- /dev/null +++ b/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 { + + private final IUserService service; + private final Boolean isCovered; + + @Override + public void save(List data) { + service.importUser(data, isCovered); + } +} diff --git a/blade-service/blade-user/src/main/java/org/springblade/system/user/feign/UserClient.java b/blade-service/blade-user/src/main/java/org/springblade/system/user/feign/UserClient.java index 43ff977b..95053d00 100644 --- a/blade-service/blade-user/src/main/java/org/springblade/system/user/feign/UserClient.java +++ b/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 userByAccount(String tenantId, String account) { + return R.data(service.userByAccount(tenantId, account)); + } + @Override @GetMapping(USER_INFO) public R userInfo(String tenantId, String account) { diff --git a/blade-service/blade-user/src/main/java/org/springblade/system/user/mapper/UserMapper.java b/blade-service/blade-user/src/main/java/org/springblade/system/user/mapper/UserMapper.java index a671aa83..8fbbc1e4 100644 --- a/blade-service/blade-user/src/main/java/org/springblade/system/user/mapper/UserMapper.java +++ b/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 getUser(String tenantId, String account); + /** + * 获取导出用户数据 + * + * @param queryWrapper + * @return + */ + List exportUser(@Param("ew") Wrapper queryWrapper); + } diff --git a/blade-service/blade-user/src/main/java/org/springblade/system/user/mapper/UserMapper.xml b/blade-service/blade-user/src/main/java/org/springblade/system/user/mapper/UserMapper.xml index 481d2b36..91634443 100644 --- a/blade-service/blade-user/src/main/java/org/springblade/system/user/mapper/UserMapper.xml +++ b/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 + + + diff --git a/blade-service/blade-user/src/main/java/org/springblade/system/user/service/IUserService.java b/blade-service/blade-user/src/main/java/org/springblade/system/user/service/IUserService.java index 4efb661b..e268dc9c 100644 --- a/blade-service/blade-user/src/main/java/org/springblade/system/user/service/IUserService.java +++ b/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 { */ 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 { * @return */ boolean removeUser(String userIds); + + /** + * 导入用户数据 + * + * @param data + * @return + */ + void importUser(List data, Boolean isCovered); + + /** + * 获取导出用户数据 + * + * @param queryWrapper + * @return + */ + List exportUser(Wrapper queryWrapper); } diff --git a/blade-service/blade-user/src/main/java/org/springblade/system/user/service/impl/UserServiceImpl.java b/blade-service/blade-user/src/main/java/org/springblade/system/user/service/impl/UserServiceImpl.java index a0128762..0132b9cd 100644 --- a/blade-service/blade-user/src/main/java/org/springblade/system/user/service/impl/UserServiceImpl.java +++ b/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 implement return page.setRecords(baseMapper.selectUserPage(page, user, deptIdList, tenantId)); } + @Override + public User userByAccount(String tenantId, String account) { + return baseMapper.selectOne(Wrappers.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 implement return deleteLogic(Func.toLongList(userIds)); } + @Override + @Transactional(rollbackFor = Exception.class) + public void importUser(List 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 exportUser(Wrapper queryWrapper) { + List 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; + } + }