|
|
@@ -17,6 +17,7 @@ import com.shkpr.service.customgateway.zydma.domain.po.PersonnelInfo;
|
|
|
import com.shkpr.service.customgateway.zydma.service.FunctionInfoService;
|
|
|
import com.shkpr.service.customgateway.zydma.service.PersonnelInfoService;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.ObjectUtils;
|
|
|
import org.apache.http.Header;
|
|
|
import org.apache.http.message.BasicHeader;
|
|
|
import org.springframework.http.HttpMethod;
|
|
|
@@ -26,6 +27,7 @@ import java.util.*;
|
|
|
import java.util.function.Consumer;
|
|
|
import java.util.function.Function;
|
|
|
import java.util.stream.Collectors;
|
|
|
+import java.util.stream.Stream;
|
|
|
|
|
|
/**
|
|
|
* 信息同步器
|
|
|
@@ -64,9 +66,8 @@ public class InfoSynchronizer {
|
|
|
/**
|
|
|
* 同步用户信息
|
|
|
*
|
|
|
- * @param userId 用户ID
|
|
|
*/
|
|
|
- public void syncUserInfo(Long userId) {
|
|
|
+ public void syncUserInfo() {
|
|
|
LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, BIZ_TYPE, CLASS_NAME
|
|
|
, "开始同步用户信息,开始滚动拉取数据");
|
|
|
long begin = System.currentTimeMillis();
|
|
|
@@ -76,6 +77,8 @@ public class InfoSynchronizer {
|
|
|
|
|
|
//获取用户
|
|
|
List<MiddlePlatformUser> users = getUsers(endpoint);
|
|
|
+ //获取用户角色
|
|
|
+ List<MiddlePlatformUserRole> userRoles = getUserRoles(endpoint);
|
|
|
|
|
|
//转换用户对象
|
|
|
List<PersonnelInfo> dates = users.stream()
|
|
|
@@ -83,10 +86,18 @@ public class InfoSynchronizer {
|
|
|
|
|
|
//遍历用户,并设置职能信息
|
|
|
dates.forEach(personnelInfo -> {
|
|
|
- //获取启用角色id
|
|
|
- List<String> ids = getRoles(endpoint, personnelInfo.getUid()).stream()
|
|
|
- .filter(MiddlePlatformRole::getIsChecked)
|
|
|
- .map(MiddlePlatformRole::getRoleId).collect(Collectors.toList());
|
|
|
+ //获取当前用户角色id集合
|
|
|
+ List<String> ids = userRoles.stream()
|
|
|
+ //过滤无效用户角色
|
|
|
+ .filter(userRole -> ObjectUtils.allNotNull(userRole, userRole.getUserId()))
|
|
|
+ //匹配当前用户
|
|
|
+ .filter(userRole -> Objects.equals(userRole.getUserId() + "", personnelInfo.getUid()))
|
|
|
+ //获取角色集合
|
|
|
+ .map(MiddlePlatformUserRole::getRoles).flatMap(roles -> roles != null ? roles.stream() : Stream.empty())
|
|
|
+ //获取角色id
|
|
|
+ .filter(role -> role.getRoleId() != null).mapToLong(MiddlePlatformUserRole.Role::getRoleId)
|
|
|
+ //去重复,排序,转字符串
|
|
|
+ .distinct().sorted().mapToObj(String::valueOf).collect(Collectors.toList());
|
|
|
try {
|
|
|
//设置职能信息
|
|
|
personnelInfo.setPostId(objectMapper.writeValueAsString(ids));
|
|
|
@@ -97,7 +108,6 @@ public class InfoSynchronizer {
|
|
|
}
|
|
|
});
|
|
|
|
|
|
-
|
|
|
//批量写入用户
|
|
|
Boolean upserted = personnelInfoService.upsertAll(dates);
|
|
|
long end = System.currentTimeMillis();
|
|
|
@@ -123,7 +133,7 @@ public class InfoSynchronizer {
|
|
|
CallingProperties.CallingEndpoint endpoint = callingProperties.getEndpoints().get(MiddlePlatformMetadata.NAME);
|
|
|
|
|
|
//获取角色
|
|
|
- List<MiddlePlatformRole> roles = getRoles(endpoint, null);
|
|
|
+ List<MiddlePlatformRole> roles = getRoles(endpoint);
|
|
|
|
|
|
//转换职能对象
|
|
|
List<FunctionInfo> dates = roles.stream()
|
|
|
@@ -179,16 +189,15 @@ public class InfoSynchronizer {
|
|
|
* 获取角色
|
|
|
*
|
|
|
* @param endpoint 对接点
|
|
|
- * @param userId 用户id
|
|
|
* @return 角色列表
|
|
|
*/
|
|
|
- public List<MiddlePlatformRole> getRoles(CallingProperties.CallingEndpoint endpoint, String userId) {
|
|
|
+ private List<MiddlePlatformRole> getRoles(CallingProperties.CallingEndpoint endpoint) {
|
|
|
//结果
|
|
|
List<MiddlePlatformRole> result = new ArrayList<>();
|
|
|
//请求地址
|
|
|
- String url = endpoint.getUrl() + MiddlePlatformMetadata.Uri.ROLE_GROUP_LIST;
|
|
|
+ String url = endpoint.getUrl() + MiddlePlatformMetadata.Uri.GET_GROUP_ROLES;
|
|
|
//参数
|
|
|
- Map<String, String> params = Collections.singletonMap(MiddlePlatformMetadata.Params.USER_ID, (userId != null ? userId : "-1"));
|
|
|
+ Map<String, String> params = Collections.singletonMap(MiddlePlatformMetadata.Params.USER_ID, MiddlePlatformMetadata.DefaultValues.USER_ID);
|
|
|
//密钥
|
|
|
IntegrationKey key = MiddlePlatformMetadata.getKey(endpoint.getAccessKey(), endpoint.getSecretKey(), params);
|
|
|
//请求头
|
|
|
@@ -205,7 +214,7 @@ public class InfoSynchronizer {
|
|
|
});
|
|
|
} catch (SelfException e) {
|
|
|
LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_ERROR, BIZ_TYPE, CLASS_NAME
|
|
|
- , String.format("拉取数据失败 error:%s", e)
|
|
|
+ , String.format("拉取角色失败 error:%s", e)
|
|
|
);
|
|
|
}
|
|
|
|
|
|
@@ -233,5 +242,35 @@ public class InfoSynchronizer {
|
|
|
)).values());
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取用户角色
|
|
|
+ *
|
|
|
+ * @param endpoint 对接点
|
|
|
+ * @return 角色列表
|
|
|
+ */
|
|
|
+ private List<MiddlePlatformUserRole> getUserRoles(CallingProperties.CallingEndpoint endpoint) {
|
|
|
+ //请求地址
|
|
|
+ String url = endpoint.getUrl() + MiddlePlatformMetadata.Uri.GET_USER_ROLES;
|
|
|
+ //参数
|
|
|
+ Map<String, String> params = Collections.singletonMap(MiddlePlatformMetadata.Params.USER_ID, MiddlePlatformMetadata.DefaultValues.USER_ID);
|
|
|
+ //密钥
|
|
|
+ IntegrationKey key = MiddlePlatformMetadata.getKey(endpoint.getAccessKey(), endpoint.getSecretKey(), params);
|
|
|
+ //请求头
|
|
|
+ List<Header> headers = Arrays.asList(
|
|
|
+ new BasicHeader(MiddlePlatformMetadata.Headers.APP_KEY, key.getAccessKey()),
|
|
|
+ new BasicHeader(MiddlePlatformMetadata.Headers.TIMESTAMP, key.getTimestamp() + ""),
|
|
|
+ new BasicHeader(MiddlePlatformMetadata.Headers.SIGN, key.getSecretKey())
|
|
|
+ );
|
|
|
|
|
|
+ //请求获取用户角色
|
|
|
+ try {
|
|
|
+ return callingUtil.requestObject(url, HttpMethod.GET, params, headers, new TypeReference<MiddlePlatformResult<List<MiddlePlatformUserRole>>>() {
|
|
|
+ });
|
|
|
+ } catch (SelfException e) {
|
|
|
+ LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_ERROR, BIZ_TYPE, CLASS_NAME
|
|
|
+ , String.format("拉取用户角色失败 error:%s", e)
|
|
|
+ );
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|