From 546766349ba2985f4b86fca9cc456fab48f5ae0a Mon Sep 17 00:00:00 2001 From: lulz1 Date: Fri, 17 May 2024 16:29:50 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E7=94=A8=E6=88=B7=E6=9D=83=E9=99=90?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=E5=A2=9E=E5=8A=A0=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../application/dtos/system/RoleOrgParam.java | 4 +- .../queryies/OrderDetailQuery.java | 2 +- .../application/system/UserController.java | 36 +++++++ .../aggregates/system/SystemOrganization.java | 5 + .../chint/domain/aggregates/user/Role.java | 52 +++++++++- .../aggregates/user/RoleOrganization.java | 7 +- .../domain/aggregates/user/RoleUser.java | 7 +- .../chint/domain/aggregates/user/User.java | 22 ++++- .../domain/repository/RoleRepository.java | 14 ++- .../domain/repository/RoleUserRepository.java | 2 + .../SystemOrganizationRepository.java | 3 + .../domain/service/SystemDomainService.java | 66 +++++++------ .../system/RoleOrganizationVO.java | 12 +++ .../domain/value_object/system/RoleVO.java | 17 ++++ .../system/SystemOrganizationVO.java | 7 +- .../constant/SystemConstant.java | 2 + .../repository/RoleRepositoryImpl.java | 98 ++++++++++++++++++- .../repository/RoleUserRepositoryImpl.java | 5 + .../SystemOrganizationRepositoryImpl.java | 44 ++++++++- .../repository/cache/CacheRoleRepository.java | 20 ++++ .../CacheSystemOrganizationRepository.java | 4 +- .../repository/jdbc/JdbcRoleRepository.java | 4 +- .../jdbc/JdbcRoleUserRepository.java | 4 + .../java/com/chint/RouteApplicationTests.java | 18 +++- 24 files changed, 406 insertions(+), 49 deletions(-) create mode 100644 src/main/java/com/chint/domain/value_object/system/RoleOrganizationVO.java create mode 100644 src/main/java/com/chint/domain/value_object/system/RoleVO.java diff --git a/src/main/java/com/chint/application/dtos/system/RoleOrgParam.java b/src/main/java/com/chint/application/dtos/system/RoleOrgParam.java index bc1453ba..7bd9bfdb 100644 --- a/src/main/java/com/chint/application/dtos/system/RoleOrgParam.java +++ b/src/main/java/com/chint/application/dtos/system/RoleOrgParam.java @@ -1,13 +1,15 @@ package com.chint.application.dtos.system; +import com.chint.domain.value_object.BaseQuery; import lombok.Data; import java.util.List; @Data -public class RoleOrgParam { +public class RoleOrgParam extends BaseQuery { private Long roleId; private String roleName; + private String roleCode; private String roleDesc; private Long userId; private List orgIdList; diff --git a/src/main/java/com/chint/application/queryies/OrderDetailQuery.java b/src/main/java/com/chint/application/queryies/OrderDetailQuery.java index f036ebf9..97878255 100644 --- a/src/main/java/com/chint/application/queryies/OrderDetailQuery.java +++ b/src/main/java/com/chint/application/queryies/OrderDetailQuery.java @@ -341,7 +341,7 @@ public class OrderDetailQuery { changedTicketNo = changedTicketNo.replace("-", ""); orderDetailDto.setChangedTicketNo(changedTicketNo); } - return routeRepository.queryById(orderDetail.getRouteId()); + return orderDetailDto; } return null; } diff --git a/src/main/java/com/chint/application/system/UserController.java b/src/main/java/com/chint/application/system/UserController.java index dd0c0106..f98c0775 100644 --- a/src/main/java/com/chint/application/system/UserController.java +++ b/src/main/java/com/chint/application/system/UserController.java @@ -10,7 +10,11 @@ import com.chint.domain.aggregates.user.UserDepartmentInfo; import com.chint.domain.repository.RoleRepository; import com.chint.domain.repository.RoleUserRepository; import com.chint.domain.repository.UserRepository; +import com.chint.domain.service.SystemDomainService; +import com.chint.domain.value_object.system.RoleOrganizationVO; +import com.chint.domain.value_object.system.RoleVO; import com.chint.infrastructure.util.BaseContext; +import com.chint.infrastructure.util.PageResult; import com.chint.infrastructure.util.Result; import com.chint.interfaces.rest.data_center.user.BatchUserWorker; import com.chint.interfaces.rest.data_center.user.UserHttpRequest; @@ -19,6 +23,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; +import java.util.Optional; import static com.chint.infrastructure.constant.CommonMessageConstant.SUCCESS; @@ -41,6 +46,9 @@ public class UserController { @Autowired private RoleRepository roleRepository; + @Autowired + private SystemDomainService systemDomainService; + @ApiOperation("根据Id查询用户信息") @PostMapping("/query") public Result getUserByEmployeeNo() { @@ -48,6 +56,34 @@ public class UserController { return Result.Success(SUCCESS, BeanUtil.copyProperties(currentUser, UserRes.class)); } + @ApiOperation("分页查看角色数据") + @PostMapping("/query/role") + public Result> queryRole(@RequestBody RoleOrgParam roleOrgParam) { + PageResult rolePageResult = roleRepository.pageQuery(roleOrgParam); + List roleList = rolePageResult.getRecords(); + List roleVOS = roleList + .stream() + .map(Role::mapToVO) + .toList(); + return Result.Success(SUCCESS, PageResult.totalPageNum(rolePageResult.getTotal(), roleVOS)); + } + + @ApiOperation("查看角色数据详情") + @PostMapping("/query/role/detail") + public Result queryRoleDetail(@RequestBody RoleOrgParam roleOrgParam) { + RoleVO res = roleRepository.findById(roleOrgParam.getRoleId()) + .flatMap(role -> { + RoleVO roleVO = role.mapToVO(); + List roleOrganizationVOList = systemDomainService.roleOrgWrapper( + role.getRoleOrganizationList() + ); + roleVO.setRoleOrganizationVOList(roleOrganizationVOList); + return Optional.of(roleVO); + }) + .orElse(null); + return Result.Success(SUCCESS, res); + } + @ApiOperation("根据用户ID添加角色权限") @PostMapping("/add/role") public Result addRoleUser(@RequestBody RoleOrgParam roleOrgParam) { diff --git a/src/main/java/com/chint/domain/aggregates/system/SystemOrganization.java b/src/main/java/com/chint/domain/aggregates/system/SystemOrganization.java index 7360e1de..8240f478 100644 --- a/src/main/java/com/chint/domain/aggregates/system/SystemOrganization.java +++ b/src/main/java/com/chint/domain/aggregates/system/SystemOrganization.java @@ -2,6 +2,7 @@ package com.chint.domain.aggregates.system; import cn.hutool.core.bean.BeanUtil; +import com.chint.domain.value_object.system.RoleOrganizationVO; import com.chint.domain.value_object.system.SystemOrganizationVO; import com.chint.interfaces.rest.data_center.org.OrgSfResponse; import lombok.Data; @@ -87,4 +88,8 @@ public class SystemOrganization implements Serializable { public SystemOrganizationVO mapToVO() { return BeanUtil.copyProperties(this, SystemOrganizationVO.class); } + + public RoleOrganizationVO mapToRoleOrganizationVO() { + return BeanUtil.copyProperties(this, RoleOrganizationVO.class); + } } diff --git a/src/main/java/com/chint/domain/aggregates/user/Role.java b/src/main/java/com/chint/domain/aggregates/user/Role.java index 95bc1924..8ce77285 100644 --- a/src/main/java/com/chint/domain/aggregates/user/Role.java +++ b/src/main/java/com/chint/domain/aggregates/user/Role.java @@ -1,22 +1,30 @@ package com.chint.domain.aggregates.user; +import cn.hutool.core.bean.BeanUtil; import com.chint.application.dtos.system.RoleOrgParam; +import com.chint.domain.value_object.system.RoleVO; import lombok.Data; import org.springframework.data.annotation.Id; import org.springframework.data.relational.core.mapping.MappedCollection; import org.springframework.data.relational.core.mapping.Table; +import java.io.Serial; +import java.io.Serializable; import java.util.ArrayList; import java.util.List; @Data @Table("role") -public class Role { +public class Role implements Serializable { + @Serial + private static final long serialVersionUID = 982691641565825656L; @Id private Long id; private String roleName; + private String roleCode; private String roleDesc; + private Integer sort; @MappedCollection(idColumn = "role_id", keyColumn = "role_key") private List roleOrganizationList; @@ -33,19 +41,57 @@ public class Role { return this; } + public Role removeRoleOrganization(Long orgId) { + if (roleOrganizationList == null) { + roleOrganizationList = new ArrayList<>(); + } + for (RoleOrganization organization : roleOrganizationList) { + if (organization.getOrgId().equals(orgId)) { + roleOrganizationList.remove(organization); + return this; + } + } + return this; + } + public Role updateWithParam(RoleOrgParam roleOrgParam) { + if (roleOrganizationList == null) { + roleOrganizationList = new ArrayList<>(); + } + if (roleOrgParam.getSort() != null) { + this.setSort(roleOrgParam.getSort()); + } + if (roleOrgParam.getRoleDesc() != null) { + this.setRoleDesc(roleOrgParam.getRoleDesc()); + } + if (roleOrgParam.getRoleCode() != null) { + this.setRoleCode(roleOrgParam.getRoleCode()); + } roleOrgParam.getOrgIdList().stream() .map(it -> RoleOrganization.of(roleOrgParam.getRoleId(), it)) .toList() .forEach(this::addRoleOrganization); + List orgIds = roleOrganizationList + .stream() + .map(RoleOrganization::getOrgId) + .toList(); + orgIds.stream() + .filter(it -> !roleOrgParam.getOrgIdList().contains(it)) + .toList() + .forEach(this::removeRoleOrganization); return this; } + public RoleVO mapToVO() { + return BeanUtil.copyProperties(this, RoleVO.class); + } + public static Role buildWithParam(RoleOrgParam roleOrgParam) { Role role = new Role(); role.setRoleName(roleOrgParam.getRoleName()); + role.setRoleCode(roleOrgParam.getRoleCode()); role.setRoleDesc(roleOrgParam.getRoleDesc()); - if(roleOrgParam.getRoleIdList() != null){ + if (roleOrgParam.getOrgIdList() != null) { roleOrgParam.getOrgIdList().stream() .map(it -> RoleOrganization.of(roleOrgParam.getRoleId(), it)) .toList() @@ -53,4 +99,6 @@ public class Role { } return role; } + + } \ No newline at end of file diff --git a/src/main/java/com/chint/domain/aggregates/user/RoleOrganization.java b/src/main/java/com/chint/domain/aggregates/user/RoleOrganization.java index 26351898..a6ad2397 100644 --- a/src/main/java/com/chint/domain/aggregates/user/RoleOrganization.java +++ b/src/main/java/com/chint/domain/aggregates/user/RoleOrganization.java @@ -5,9 +5,14 @@ import lombok.Data; import org.springframework.data.annotation.Id; import org.springframework.data.relational.core.mapping.Table; +import java.io.Serial; +import java.io.Serializable; + @Data @Table("role_organization") -public class RoleOrganization { +public class RoleOrganization implements Serializable { + @Serial + private static final long serialVersionUID = 986481563255172358L; @Id private Long id; private Long roleId; diff --git a/src/main/java/com/chint/domain/aggregates/user/RoleUser.java b/src/main/java/com/chint/domain/aggregates/user/RoleUser.java index e859c2ac..2e75daaa 100644 --- a/src/main/java/com/chint/domain/aggregates/user/RoleUser.java +++ b/src/main/java/com/chint/domain/aggregates/user/RoleUser.java @@ -4,10 +4,15 @@ import lombok.Data; import org.springframework.data.annotation.Id; import org.springframework.data.relational.core.mapping.Table; +import java.io.Serial; +import java.io.Serializable; + @Data @Table("role_user") -public class RoleUser { +public class RoleUser implements Serializable { + @Serial + private static final long serialVersionUID = 1937591655113659058L; @Id private Long id; private Long userId; diff --git a/src/main/java/com/chint/domain/aggregates/user/User.java b/src/main/java/com/chint/domain/aggregates/user/User.java index 10e44c27..4e0d238b 100644 --- a/src/main/java/com/chint/domain/aggregates/user/User.java +++ b/src/main/java/com/chint/domain/aggregates/user/User.java @@ -35,6 +35,7 @@ import java.util.List; import java.util.Map; import java.util.Optional; import java.util.function.Predicate; +import java.util.stream.Stream; import static com.chint.infrastructure.constant.AuthMessageConstant.SF_DATA_NOT_EXIST; import static com.chint.infrastructure.constant.AuthMessageConstant.USER_FSSC_SYSTEM_LIST; @@ -82,6 +83,8 @@ public class User implements Serializable { private List roleOrgCodeList; @Transient private List roleOrgList; + @Transient + private List roleList; public User loadInfoFromDept() { if (this.userDepartmentInfoList == null || this.userDepartmentInfoList.isEmpty()) { @@ -123,10 +126,21 @@ public class User implements Serializable { departmentInfo -> departmentInfo.getCompanyName().equals(finalCompanyCode) : departmentInfo -> departmentInfo.getCompanyCode().equals(finalCompanyCode); - UserDepartmentInfo userDepartmentInfo = userDepartmentInfoList.stream() - .filter(matchCompanyCode) - .findFirst() - .orElseGet(this::getFallbackDeptInfo); + List userDepartmentInfos = userDepartmentInfoList.stream() + .filter(matchCompanyCode).toList(); + + UserDepartmentInfo userDepartmentInfo; + if(userDepartmentInfos.isEmpty()) { + userDepartmentInfo = getFallbackDeptInfo(); + } else if(userDepartmentInfos.size() == 1) { + userDepartmentInfo = userDepartmentInfos.get(0); + } else { + userDepartmentInfo = userDepartmentInfos + .stream() + .filter(UserDepartmentInfo::ifPrimary) + .findFirst() + .orElseGet(this::getFallbackDeptInfo); + } loadInfoFromDeptInfo(userDepartmentInfo); } else { UserDepartmentInfo fallbackDeptInfo = getFallbackDeptInfo(); diff --git a/src/main/java/com/chint/domain/repository/RoleRepository.java b/src/main/java/com/chint/domain/repository/RoleRepository.java index 6f5394c5..b1597e82 100644 --- a/src/main/java/com/chint/domain/repository/RoleRepository.java +++ b/src/main/java/com/chint/domain/repository/RoleRepository.java @@ -1,13 +1,25 @@ package com.chint.domain.repository; +import com.chint.application.dtos.system.RoleOrgParam; import com.chint.domain.aggregates.user.Role; +import com.chint.domain.aggregates.user.User; +import com.chint.domain.value_object.system.SystemOrganizationVO; +import com.chint.infrastructure.util.PageResult; -import javax.swing.text.html.Option; +import java.util.List; import java.util.Optional; public interface RoleRepository { Optional findById(Long id); + PageResult pageQuery(RoleOrgParam roleOrgParam); + Role save(Role role); + + List roleOrgCodeList(Long userId); + + List roleOrgList(Long userId); + + void loadUserRole(User user); } diff --git a/src/main/java/com/chint/domain/repository/RoleUserRepository.java b/src/main/java/com/chint/domain/repository/RoleUserRepository.java index 782e915c..00da37d4 100644 --- a/src/main/java/com/chint/domain/repository/RoleUserRepository.java +++ b/src/main/java/com/chint/domain/repository/RoleUserRepository.java @@ -7,5 +7,7 @@ import java.util.List; public interface RoleUserRepository { List findByUserId(Long userId); + List findByRoleId(Long roleId); + RoleUser save(RoleUser roleUser); } diff --git a/src/main/java/com/chint/domain/repository/SystemOrganizationRepository.java b/src/main/java/com/chint/domain/repository/SystemOrganizationRepository.java index fee8c335..54f99ad8 100644 --- a/src/main/java/com/chint/domain/repository/SystemOrganizationRepository.java +++ b/src/main/java/com/chint/domain/repository/SystemOrganizationRepository.java @@ -23,4 +23,7 @@ public interface SystemOrganizationRepository { List findByParentId(Long parentId); + SystemOrganization queryChild(SystemOrganization systemOrganization); + + List expandOrganizations(List systemOrganizations); } diff --git a/src/main/java/com/chint/domain/service/SystemDomainService.java b/src/main/java/com/chint/domain/service/SystemDomainService.java index c4f5bf22..b7af3f02 100644 --- a/src/main/java/com/chint/domain/service/SystemDomainService.java +++ b/src/main/java/com/chint/domain/service/SystemDomainService.java @@ -7,12 +7,15 @@ import com.chint.domain.aggregates.system.AccountCompany; import com.chint.domain.aggregates.system.SystemCode; import com.chint.domain.aggregates.system.SystemOrganization; import com.chint.domain.aggregates.system.SystemOrganizationExtension; +import com.chint.domain.aggregates.user.Role; import com.chint.domain.aggregates.user.RoleOrganization; import com.chint.domain.aggregates.user.RoleUser; import com.chint.domain.aggregates.user.User; import com.chint.domain.exceptions.NotFoundException; import com.chint.domain.repository.*; +import com.chint.domain.value_object.system.RoleOrganizationVO; import com.chint.infrastructure.echo_framework.annotation.ListenTo; +import com.chint.infrastructure.repository.cache.CacheRoleRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -43,6 +46,7 @@ public class SystemDomainService { @Autowired private RoleRepository roleRepository; + public boolean checkSystemCode(String sysCode) { boolean b = systemCodeRepository.findBySysCode(sysCode) != null; if (!b) { @@ -72,7 +76,8 @@ public class SystemDomainService { public void addOrganizationClerk(OrganizationTicketClerk organizationTicketClerk) { systemOrganizationRepository .findByOrgCodeContaining(organizationTicketClerk.getOrgCode()) - .ifPresent(it -> loadClerk(queryChild(it), organizationTicketClerk.getClerkName())); + .ifPresent(it -> loadClerk(systemOrganizationRepository.queryChild(it), + organizationTicketClerk.getClerkName())); } public void addOrgExtensionField(String orgCode, String fieldName, String fieldValue, String extension) { @@ -162,41 +167,40 @@ public class SystemDomainService { public void loadUserRole(UserRoleCommand command) { User user = command.getUser(); List byUserId = roleUserRepository.findByUserId(user.getUserId()); - List orgListId = byUserId.stream().flatMap(it -> - roleRepository + List roleList = byUserId.stream().flatMap(it -> roleRepository .findById(it.getRoleId()) - .stream() - .flatMap(role -> role.getRoleOrganizationList().stream()) - .map(RoleOrganization::getOrgId) - ).distinct().toList(); - - // 获取所有的 SystemOrganization - List rootOrganizations = systemOrganizationRepository.findByIdIn(orgListId); - - // 展开所有的 SystemOrganization - List allOrganizations = new ArrayList<>(); - rootOrganizations.forEach(rootOrg -> expandOrganizations(rootOrg, allOrganizations)); - - // 设置用户的角色组织列表 - user.setRoleOrgCodeList( - allOrganizations.stream() - .map(SystemOrganization::getOrgShortCode) - .distinct() - .toList() - ); - user.setRoleOrgList( - allOrganizations.stream() - .map(SystemOrganization::mapToVO) - .distinct() - .toList() - ); + .stream()) + .toList(); + user.setRoleList(roleList); + roleRepository.loadUserRole(user); } - private void expandOrganizations(SystemOrganization org, List allOrganizations) { + private void expandOrganizations( SystemOrganization org, List allOrganizations) { + if (org == null) { + return; + } queryChild(org); // 查询子组织 allOrganizations.add(org); // 添加当前组织到列表 - for (SystemOrganization child : org.getChildOrganizationList()) { - expandOrganizations(child, allOrganizations); // 递归展开子组织 + List childOrganizationList = org.getChildOrganizationList(); + if (childOrganizationList != null && !childOrganizationList.isEmpty()) { + for (SystemOrganization child : childOrganizationList) { + expandOrganizations(child, allOrganizations); // 递归展开子组织 + } } } + + public List roleOrgWrapper(List roleOrganizationList) { + List orgIds = roleOrganizationList.stream().map(RoleOrganization::getOrgId).toList(); + List byIdIn = systemOrganizationRepository + .findByIdIn(orgIds); + List allOrganizations = new ArrayList<>(); + for (SystemOrganization systemOrganization : byIdIn) { + expandOrganizations(systemOrganization, allOrganizations); + } + return allOrganizations + .stream() + .distinct() + .map(SystemOrganization::mapToRoleOrganizationVO) + .toList(); + } } diff --git a/src/main/java/com/chint/domain/value_object/system/RoleOrganizationVO.java b/src/main/java/com/chint/domain/value_object/system/RoleOrganizationVO.java new file mode 100644 index 00000000..55a1383d --- /dev/null +++ b/src/main/java/com/chint/domain/value_object/system/RoleOrganizationVO.java @@ -0,0 +1,12 @@ +package com.chint.domain.value_object.system; + + +import lombok.Data; + +@Data +public class RoleOrganizationVO { + private Long id; + private String orgCode; + private String orgName; + private String orgEnName; +} diff --git a/src/main/java/com/chint/domain/value_object/system/RoleVO.java b/src/main/java/com/chint/domain/value_object/system/RoleVO.java new file mode 100644 index 00000000..4aa19d78 --- /dev/null +++ b/src/main/java/com/chint/domain/value_object/system/RoleVO.java @@ -0,0 +1,17 @@ +package com.chint.domain.value_object.system; + +import com.chint.domain.aggregates.user.RoleOrganization; +import lombok.Data; +import org.springframework.data.relational.core.mapping.MappedCollection; + +import java.util.List; + +@Data +public class RoleVO { + private Long id; + private String roleName; + private String roleCode; + private String roleDesc; + private Integer sort; + private List roleOrganizationVOList; +} diff --git a/src/main/java/com/chint/domain/value_object/system/SystemOrganizationVO.java b/src/main/java/com/chint/domain/value_object/system/SystemOrganizationVO.java index 3891e793..300335bf 100644 --- a/src/main/java/com/chint/domain/value_object/system/SystemOrganizationVO.java +++ b/src/main/java/com/chint/domain/value_object/system/SystemOrganizationVO.java @@ -2,8 +2,13 @@ package com.chint.domain.value_object.system; import lombok.Data; +import java.io.Serial; +import java.io.Serializable; + @Data -public class SystemOrganizationVO { +public class SystemOrganizationVO implements Serializable { + @Serial + private static final long serialVersionUID = 7552825188541271771L; private Long id; private String orgCode; private String orgShortCode; diff --git a/src/main/java/com/chint/infrastructure/constant/SystemConstant.java b/src/main/java/com/chint/infrastructure/constant/SystemConstant.java index 45afdce1..876a5f05 100644 --- a/src/main/java/com/chint/infrastructure/constant/SystemConstant.java +++ b/src/main/java/com/chint/infrastructure/constant/SystemConstant.java @@ -9,4 +9,6 @@ public class SystemConstant { public static final Integer H3BPM_LOGIN_TYPE = 3; + public static final String ROLE_JT_ADMIN = "JT_ADMIN"; + } diff --git a/src/main/java/com/chint/infrastructure/repository/RoleRepositoryImpl.java b/src/main/java/com/chint/infrastructure/repository/RoleRepositoryImpl.java index 78a05326..dd55677e 100644 --- a/src/main/java/com/chint/infrastructure/repository/RoleRepositoryImpl.java +++ b/src/main/java/com/chint/infrastructure/repository/RoleRepositoryImpl.java @@ -1,12 +1,25 @@ package com.chint.infrastructure.repository; +import com.chint.application.dtos.system.RoleOrgParam; +import com.chint.domain.aggregates.system.SystemOrganization; import com.chint.domain.aggregates.user.Role; +import com.chint.domain.aggregates.user.RoleOrganization; +import com.chint.domain.aggregates.user.RoleUser; +import com.chint.domain.aggregates.user.User; import com.chint.domain.repository.RoleRepository; +import com.chint.domain.repository.RoleUserRepository; +import com.chint.domain.repository.SystemOrganizationRepository; +import com.chint.domain.value_object.system.SystemOrganizationVO; import com.chint.infrastructure.repository.cache.CacheRoleRepository; import com.chint.infrastructure.repository.jdbc.JdbcRoleRepository; +import com.chint.infrastructure.util.PageResult; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Sort; import org.springframework.stereotype.Repository; +import java.util.List; import java.util.Optional; @Repository @@ -17,6 +30,12 @@ public class RoleRepositoryImpl implements RoleRepository { @Autowired private CacheRoleRepository cacheRoleRepository; + @Autowired + private RoleUserRepository roleUserRepository; + + @Autowired + private SystemOrganizationRepository systemOrganizationRepository; + @Override public Optional findById(Long id) { if (id == null) { @@ -26,17 +45,92 @@ public class RoleRepositoryImpl implements RoleRepository { if (fromCache.isPresent()) { return fromCache; } else { - return cacheRoleRepository.getFromCache(id, jdbcRoleRepository.findById(id)); + Optional byId = jdbcRoleRepository.findById(id); + cacheRoleRepository.evict(id); + cacheRoleRepository.getFromCache(id, byId); + return byId; } } + @Override + public PageResult pageQuery(RoleOrgParam roleOrgParam) { + PageRequest pageRequest = PageRequest.of(roleOrgParam.getPageNum() - 1, + roleOrgParam.getPageSize(), Sort.by("sort")); + Page all = jdbcRoleRepository.findAll(pageRequest); + return PageResult.totalPageNum(all.getTotalElements(), all.getContent()); + } + @Override public Role save(Role role) { Long id = role.getId(); - if(id != null){ + if (id != null) { cacheRoleRepository.evict(id); } + //当role(角色)调整的时候,需要清除相关用户的缓存 + roleUserRepository.findByRoleId(role.getId()) + .stream() + .map(RoleUser::getUserId) + .forEach(userId -> { + cacheRoleRepository.evictRoleOrgCodeList(userId); + cacheRoleRepository.evictRoleOrgList(userId); + }); return jdbcRoleRepository.save(role); } + + @Override + public List roleOrgCodeList(Long userId) { + return List.of(); + } + + @Override + public List roleOrgList(Long userId) { + + return List.of(); + } + + @Override + public void loadUserRole(User user) { + Long userId = user.getUserId(); + List orgCodeList = cacheRoleRepository + .roleOrgCodeList(userId, null); + List systemOrganizationVOList = cacheRoleRepository + .roleOrgList(userId, null); + + if (systemOrganizationVOList == null || systemOrganizationVOList.isEmpty() || + orgCodeList == null || orgCodeList.isEmpty()) { + List roleList = user.getRoleList(); + List orgListId = roleList + .stream() + .flatMap(role -> role.getRoleOrganizationList().stream()) + .map(RoleOrganization::getOrgId) + .distinct() + .toList(); + + // 获取所有的 SystemOrganization + List rootOrganizations = systemOrganizationRepository.findByIdIn(orgListId); + + // 展开所有的 SystemOrganization + rootOrganizations = systemOrganizationRepository.expandOrganizations(rootOrganizations); + + orgCodeList = rootOrganizations.stream() + .map(SystemOrganization::getOrgShortCode) + .distinct() + .toList(); + + systemOrganizationVOList = rootOrganizations.stream() + .map(SystemOrganization::mapToVO) + .distinct() + .toList(); + + cacheRoleRepository.evictRoleOrgCodeList(userId); + cacheRoleRepository.roleOrgCodeList(userId, orgCodeList); + cacheRoleRepository.evictRoleOrgList(userId); + cacheRoleRepository.roleOrgList(userId, systemOrganizationVOList); + } + + // 设置用户的角色组织列表 + user.setRoleOrgCodeList(orgCodeList); + user.setRoleOrgList(systemOrganizationVOList); + } } diff --git a/src/main/java/com/chint/infrastructure/repository/RoleUserRepositoryImpl.java b/src/main/java/com/chint/infrastructure/repository/RoleUserRepositoryImpl.java index 26f4198f..4fdb8610 100644 --- a/src/main/java/com/chint/infrastructure/repository/RoleUserRepositoryImpl.java +++ b/src/main/java/com/chint/infrastructure/repository/RoleUserRepositoryImpl.java @@ -22,6 +22,11 @@ public class RoleUserRepositoryImpl implements RoleUserRepository { return jdbcRoleUserRepository.findByUserId(userId); } + @Override + public List findByRoleId(Long roleId) { + return jdbcRoleUserRepository.findByRoleId(roleId); + } + @CacheEvict(value = "RoleUser", key = "#roleUser.userId") @Override public RoleUser save(RoleUser roleUser) { diff --git a/src/main/java/com/chint/infrastructure/repository/SystemOrganizationRepositoryImpl.java b/src/main/java/com/chint/infrastructure/repository/SystemOrganizationRepositoryImpl.java index 1bab8a9c..4a8e5f6e 100644 --- a/src/main/java/com/chint/infrastructure/repository/SystemOrganizationRepositoryImpl.java +++ b/src/main/java/com/chint/infrastructure/repository/SystemOrganizationRepositoryImpl.java @@ -9,6 +9,7 @@ import org.springframework.stereotype.Repository; import java.util.ArrayList; import java.util.List; +import java.util.Objects; import java.util.Optional; @Repository @@ -51,7 +52,10 @@ public class SystemOrganizationRepositoryImpl implements SystemOrganizationRepos @Override public List findByIdIn(List ids) { List fromCacheByIdIn = cacheSystemOrganizationRepository.getFromCacheByIdIn(ids); - List fromCacheOrg = fromCacheByIdIn.stream().map(SystemOrganization::getId).toList(); + List fromCacheOrg = fromCacheByIdIn.stream() + .filter(Objects::nonNull) + .map(SystemOrganization::getId) + .toList(); List list = ids.stream().filter(it -> !fromCacheOrg.contains(it)).toList(); if (!list.isEmpty()) { List fromDBByIdIn = jdbcSystemOrganizationRepository.findByIdIn(list); @@ -99,4 +103,42 @@ public class SystemOrganizationRepositoryImpl implements SystemOrganizationRepos } return list; } + + @Override + public SystemOrganization queryChild(SystemOrganization systemOrganization) { + List byParentId = findByParentId(systemOrganization.getId()); + if (byParentId.isEmpty()) { + return systemOrganization; + } else { + systemOrganization.setChildOrganizationList(byParentId); + for (SystemOrganization organization : byParentId) { + queryChild(organization); + } + return systemOrganization; + } + } + + @Override + public List expandOrganizations(List systemOrganizations) { + List allOrganizations = new ArrayList<>(); + for (SystemOrganization org : systemOrganizations) { + expandOrganizationRecursively(org, allOrganizations); + } + return allOrganizations; + } + + private void expandOrganizationRecursively(SystemOrganization org, + List allOrganizations) { + if (org == null) { + return; + } + queryChild(org); // 查询子组织 + allOrganizations.add(org); // 添加当前组织到列表 + List childOrganizationList = org.getChildOrganizationList(); + if (childOrganizationList != null && !childOrganizationList.isEmpty()) { + for (SystemOrganization child : childOrganizationList) { + expandOrganizationRecursively(child, allOrganizations); // 递归展开子组织 + } + } + } } diff --git a/src/main/java/com/chint/infrastructure/repository/cache/CacheRoleRepository.java b/src/main/java/com/chint/infrastructure/repository/cache/CacheRoleRepository.java index 0a45e671..9a841833 100644 --- a/src/main/java/com/chint/infrastructure/repository/cache/CacheRoleRepository.java +++ b/src/main/java/com/chint/infrastructure/repository/cache/CacheRoleRepository.java @@ -1,10 +1,12 @@ package com.chint.infrastructure.repository.cache; import com.chint.domain.aggregates.user.Role; +import com.chint.domain.value_object.system.SystemOrganizationVO; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Repository; +import java.util.List; import java.util.Optional; @Repository @@ -19,4 +21,22 @@ public class CacheRoleRepository { @CacheEvict(value = "Role", key = "#id") public void evict(Long id) { } + + @Cacheable(value = "Role::OrgCode", key = "#userId") + public List roleOrgCodeList(Long userId, List roleOrgCodeList) { + return roleOrgCodeList; + } + + @CacheEvict(value = "Role::OrgCode", key = "#userId") + public void evictRoleOrgCodeList(Long userId) { + } + + @Cacheable(value = "Role::Org", key = "#userId") + public List roleOrgList(Long userId, List roleOrgList) { + return roleOrgList; + } + + @CacheEvict(value = "Role::Org", key = "#userId") + public void evictRoleOrgList(Long userId) { + } } diff --git a/src/main/java/com/chint/infrastructure/repository/cache/CacheSystemOrganizationRepository.java b/src/main/java/com/chint/infrastructure/repository/cache/CacheSystemOrganizationRepository.java index 4598417c..de6096b7 100644 --- a/src/main/java/com/chint/infrastructure/repository/cache/CacheSystemOrganizationRepository.java +++ b/src/main/java/com/chint/infrastructure/repository/cache/CacheSystemOrganizationRepository.java @@ -9,6 +9,7 @@ import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Repository; import java.util.List; +import java.util.Objects; @Repository public class CacheSystemOrganizationRepository { @@ -18,7 +19,8 @@ public class CacheSystemOrganizationRepository { public List getFromCacheByIdIn(List ids) { List routeOrderKeys = ids.stream().map(id -> "SystemOrganization::" + id).toList(); - return redisTemplate.opsForValue().multiGet(routeOrderKeys); + List systemOrganizationList = redisTemplate.opsForValue().multiGet(routeOrderKeys); + return Objects.requireNonNullElseGet(systemOrganizationList, List::of); } @Cacheable(value = "SystemOrganization", key = "#systemOrganization.id") diff --git a/src/main/java/com/chint/infrastructure/repository/jdbc/JdbcRoleRepository.java b/src/main/java/com/chint/infrastructure/repository/jdbc/JdbcRoleRepository.java index ce307fb8..a3e391bc 100644 --- a/src/main/java/com/chint/infrastructure/repository/jdbc/JdbcRoleRepository.java +++ b/src/main/java/com/chint/infrastructure/repository/jdbc/JdbcRoleRepository.java @@ -1,10 +1,12 @@ package com.chint.infrastructure.repository.jdbc; -import com.chint.domain.aggregates.standards.Ranks; import com.chint.domain.aggregates.user.Role; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.data.repository.CrudRepository; import org.springframework.stereotype.Repository; @Repository public interface JdbcRoleRepository extends CrudRepository { + Page findAll(Pageable pageable); } diff --git a/src/main/java/com/chint/infrastructure/repository/jdbc/JdbcRoleUserRepository.java b/src/main/java/com/chint/infrastructure/repository/jdbc/JdbcRoleUserRepository.java index 177d6f04..9fcf8dbf 100644 --- a/src/main/java/com/chint/infrastructure/repository/jdbc/JdbcRoleUserRepository.java +++ b/src/main/java/com/chint/infrastructure/repository/jdbc/JdbcRoleUserRepository.java @@ -10,4 +10,8 @@ import java.util.List; public interface JdbcRoleUserRepository extends CrudRepository { List findByUserId(Long userId); + + List findByRoleId(Long roleId); + + } diff --git a/src/test/java/com/chint/RouteApplicationTests.java b/src/test/java/com/chint/RouteApplicationTests.java index 8033b52e..ba23ea3b 100644 --- a/src/test/java/com/chint/RouteApplicationTests.java +++ b/src/test/java/com/chint/RouteApplicationTests.java @@ -99,6 +99,9 @@ class RouteApplicationTests { @Autowired private CTripAirportRequest cTripAirportRequest; + @Autowired + private UserRepository userRepository; + @Value("${FSSC.jt-baseUrl}") private String jtFSSCUrl; @@ -1308,5 +1311,18 @@ class RouteApplicationTests { System.out.println(rankName); } - + @Test + void roleTest(){ + System.out.println(System.currentTimeMillis()); + User byId = userRepository.findByUserEmployeeNo("230615020").loadRoleOrg(); + System.out.println(System.currentTimeMillis()); + byId.loadRoleOrg(); + System.out.println(System.currentTimeMillis()); + byId.loadRoleOrg(); + System.out.println(System.currentTimeMillis()); + byId.loadRoleOrg(); + System.out.println(System.currentTimeMillis()); + byId.loadRoleOrg(); + System.out.println(System.currentTimeMillis()); + } }