|
|
@@ -1,6 +1,8 @@
|
|
|
package com.shkpr.service.customgateway.zydma.components;
|
|
|
|
|
|
+import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
import com.fasterxml.jackson.core.type.TypeReference;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import com.global.base.log.LogLevelFlag;
|
|
|
import com.global.base.log.LogPrintMgr;
|
|
|
import com.shkpr.service.customgateway.core.constants.LogFlagBusiType;
|
|
|
@@ -9,20 +11,20 @@ import com.shkpr.service.customgateway.core.exception.SelfException;
|
|
|
import com.shkpr.service.customgateway.core.properties.CallingProperties;
|
|
|
import com.shkpr.service.customgateway.core.utils.CallingUtil;
|
|
|
import com.shkpr.service.customgateway.zydma.constants.MiddlePlatformMetadata;
|
|
|
-import com.shkpr.service.customgateway.zydma.domain.MiddlePlatformPage;
|
|
|
-import com.shkpr.service.customgateway.zydma.domain.MiddlePlatformResult;
|
|
|
-import com.shkpr.service.customgateway.zydma.domain.MiddlePlatformUser;
|
|
|
+import com.shkpr.service.customgateway.zydma.domain.*;
|
|
|
+import com.shkpr.service.customgateway.zydma.domain.po.FunctionInfo;
|
|
|
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.http.Header;
|
|
|
import org.apache.http.message.BasicHeader;
|
|
|
import org.springframework.http.HttpMethod;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
-import java.util.Arrays;
|
|
|
-import java.util.Collections;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.List;
|
|
|
+import java.util.*;
|
|
|
+import java.util.function.Consumer;
|
|
|
+import java.util.function.Function;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
@@ -43,13 +45,19 @@ public class InfoSynchronizer {
|
|
|
final
|
|
|
CallingProperties callingProperties;
|
|
|
final
|
|
|
+ ObjectMapper objectMapper;
|
|
|
+ final
|
|
|
PersonnelInfoService personnelInfoService;
|
|
|
final
|
|
|
+ FunctionInfoService functionInfoService;
|
|
|
+ final
|
|
|
CallingUtil callingUtil;
|
|
|
|
|
|
- public InfoSynchronizer(CallingProperties callingProperties, PersonnelInfoService personnelInfoService, CallingUtil callingUtil) {
|
|
|
+ public InfoSynchronizer(CallingProperties callingProperties, ObjectMapper objectMapper, PersonnelInfoService personnelInfoService, FunctionInfoService functionInfoService, CallingUtil callingUtil) {
|
|
|
this.callingProperties = callingProperties;
|
|
|
+ this.objectMapper = objectMapper;
|
|
|
this.personnelInfoService = personnelInfoService;
|
|
|
+ this.functionInfoService = functionInfoService;
|
|
|
this.callingUtil = callingUtil;
|
|
|
}
|
|
|
|
|
|
@@ -69,13 +77,23 @@ public class InfoSynchronizer {
|
|
|
//获取用户
|
|
|
List<MiddlePlatformUser> users = getUsers(endpoint);
|
|
|
|
|
|
- //遍历用户
|
|
|
- //todo 用户角色信息获取
|
|
|
-
|
|
|
//转换用户对象
|
|
|
List<PersonnelInfo> dates = users.stream()
|
|
|
.map(MiddlePlatformUser::toPersonnelInfo).collect(Collectors.toList());
|
|
|
|
|
|
+ //遍历用户,并设置职能信息
|
|
|
+ dates.forEach(personnelInfo -> {
|
|
|
+ List<String> ids = getRoles(endpoint, personnelInfo.getUid()).stream().map(MiddlePlatformRole::getRoleId).collect(Collectors.toList());
|
|
|
+ try {
|
|
|
+ personnelInfo.setPostId(objectMapper.writeValueAsString(ids));
|
|
|
+ } catch (JsonProcessingException e) {
|
|
|
+ LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_ERROR, BIZ_TYPE, CLASS_NAME
|
|
|
+ , String.format("职能id集合json序列化失败 error:%s", e)
|
|
|
+ );
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+
|
|
|
//批量写入用户
|
|
|
Boolean upserted = personnelInfoService.upsertAll(dates);
|
|
|
long end = System.currentTimeMillis();
|
|
|
@@ -89,6 +107,37 @@ public class InfoSynchronizer {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * 同步职能信息
|
|
|
+ *
|
|
|
+ */
|
|
|
+ public void syncFunctionInfo() {
|
|
|
+ LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, BIZ_TYPE, CLASS_NAME
|
|
|
+ , "开始同步职能信息,开始拉取数据");
|
|
|
+ long begin = System.currentTimeMillis();
|
|
|
+
|
|
|
+ //对接点
|
|
|
+ CallingProperties.CallingEndpoint endpoint = callingProperties.getEndpoints().get(MiddlePlatformMetadata.NAME);
|
|
|
+
|
|
|
+ //获取角色
|
|
|
+ List<MiddlePlatformRole> roles = getRoles(endpoint, null);
|
|
|
+
|
|
|
+ //转换职能对象
|
|
|
+ List<FunctionInfo> dates = roles.stream()
|
|
|
+ .map(MiddlePlatformRole::toFunctionInfo).collect(Collectors.toList());
|
|
|
+
|
|
|
+ //批量写入职能
|
|
|
+ Boolean upserted = functionInfoService.upsertAll(dates);
|
|
|
+ long end = System.currentTimeMillis();
|
|
|
+ LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, BIZ_TYPE, CLASS_NAME
|
|
|
+ , String.format(
|
|
|
+ "结束执行同步职能信息,迁移状态:%s,用时(毫秒):%d"
|
|
|
+ , upserted
|
|
|
+ , (end - begin)
|
|
|
+ )
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* 获取用户
|
|
|
*
|
|
|
* @param endpoint 对接点
|
|
|
@@ -121,4 +170,64 @@ public class InfoSynchronizer {
|
|
|
return Collections.emptyList();
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取角色
|
|
|
+ *
|
|
|
+ * @param endpoint 对接点
|
|
|
+ * @param userId 用户id
|
|
|
+ * @return 角色列表
|
|
|
+ */
|
|
|
+ public List<MiddlePlatformRole> getRoles(CallingProperties.CallingEndpoint endpoint, String userId) {
|
|
|
+ //结果
|
|
|
+ List<MiddlePlatformRole> result = new ArrayList<>();
|
|
|
+ //请求地址
|
|
|
+ String url = endpoint.getUrl() + MiddlePlatformMetadata.Uri.ROLE_GROUP_LIST;
|
|
|
+ //参数
|
|
|
+ Map<String, String> params = Collections.singletonMap(MiddlePlatformMetadata.Params.USER_ID, (userId != null ? userId : "-1"));
|
|
|
+ //密钥
|
|
|
+ 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())
|
|
|
+ );
|
|
|
+
|
|
|
+ //请求结果项
|
|
|
+ Map<String, List<MiddlePlatformSubsystem>> items = Collections.emptyMap();
|
|
|
+ try {
|
|
|
+ items = callingUtil.requestObject(url, HttpMethod.GET, params, headers, new TypeReference<MiddlePlatformResult<Map<String, List<MiddlePlatformSubsystem>>>>() {
|
|
|
+ });
|
|
|
+ } catch (SelfException e) {
|
|
|
+ LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_ERROR, BIZ_TYPE, CLASS_NAME
|
|
|
+ , String.format("拉取数据失败 error:%s", e)
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ //获取子系统信息
|
|
|
+ List<MiddlePlatformSubsystem> subsystems = items.getOrDefault(MiddlePlatformMetadata.Results.ROLE_LIST, Collections.emptyList());
|
|
|
+ //递归子系统,获取角色
|
|
|
+ subsystems.forEach(new Consumer<MiddlePlatformSubsystem>() {
|
|
|
+ @Override
|
|
|
+ public void accept(MiddlePlatformSubsystem subsystem) {
|
|
|
+ //填入角色
|
|
|
+ if (subsystem.getRoleList() != null) result.addAll(subsystem.getRoleList());
|
|
|
+ //递归子系统
|
|
|
+ if (subsystem.getChild() != null) subsystem.getChild().forEach(this);
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ //根据角色id排序去重
|
|
|
+ return new ArrayList<>(result.stream()
|
|
|
+ .sorted(Comparator.comparingLong(role -> Long.parseLong(role.getRoleId()))) // 如果是int类型
|
|
|
+ .collect(Collectors.toMap(
|
|
|
+ MiddlePlatformRole::getRoleId,
|
|
|
+ Function.identity(),
|
|
|
+ (existing, replacement) -> existing,
|
|
|
+ LinkedHashMap::new
|
|
|
+ )).values());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
}
|