|
@@ -0,0 +1,255 @@
|
|
|
+package com.dragoninfo.dcuc.app.business.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.bean.BeanUtil;
|
|
|
+import com.dragoninfo.dcuc.app.business.IFhAppSyncBusiness;
|
|
|
+import com.dragoninfo.dcuc.app.config.FhAppSyncProperties;
|
|
|
+import com.dragoninfo.dcuc.app.dto.appindex.FhAppIndexRespDTO;
|
|
|
+import com.dragoninfo.dcuc.app.dto.appindex.FhResourceRespDTO;
|
|
|
+import com.dragoninfo.dcuc.app.dto.appindex.FhResponseParamRespDTO;
|
|
|
+import com.dragoninfo.dcuc.app.entity.ApplyInfo;
|
|
|
+import com.dragoninfo.dcuc.app.entity.ApplyOauth;
|
|
|
+import com.dragoninfo.dcuc.app.service.IApplyInfoService;
|
|
|
+import com.dragoninfo.dcuc.app.service.IApplyOauthService;
|
|
|
+import com.dragoninfo.dcuc.common.annotation.CacheLock;
|
|
|
+import com.dragoninfo.dcuc.common.utils.ResponseUtil;
|
|
|
+import com.dragoninfo.dcuc.user.entity.org.OrgInfo;
|
|
|
+import com.dragoninfo.dcuc.user.facade.org.IOrgInfoFacade;
|
|
|
+import com.dragonsoft.duceap.base.entity.http.ResponseDTO;
|
|
|
+import com.dragonsoft.duceap.base.entity.http.ResponseStatus;
|
|
|
+import com.dragonsoft.duceap.base.enums.BooleanEnum;
|
|
|
+import com.dragonsoft.duceap.commons.util.ObjectUtils;
|
|
|
+import com.dragonsoft.duceap.commons.util.collections.CollectionUtils;
|
|
|
+import com.dragonsoft.duceap.commons.util.json.JsonUtils;
|
|
|
+import com.dragonsoft.duceap.commons.util.string.StringUtils;
|
|
|
+import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.http.HttpEntity;
|
|
|
+import org.springframework.http.HttpMethod;
|
|
|
+import org.springframework.http.ResponseEntity;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
+
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author mazq
|
|
|
+ * @date 2023/7/20
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class FhAppSyncBusinessImpl implements IFhAppSyncBusiness {
|
|
|
+
|
|
|
+ private final static String FH_SUCCESS_STATUS = "0200";
|
|
|
+
|
|
|
+ private RestTemplate restTemplate;
|
|
|
+
|
|
|
+ private FhAppSyncProperties fhAppSyncProperties;
|
|
|
+
|
|
|
+ private IApplyInfoService applyInfoService;
|
|
|
+
|
|
|
+ private IOrgInfoFacade orgInfoFacade;
|
|
|
+
|
|
|
+ private IApplyOauthService applyOauthService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ public void setApplyOauthService(IApplyOauthService applyOauthService) {
|
|
|
+ this.applyOauthService = applyOauthService;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ public void setOrgInfoFacade(IOrgInfoFacade orgInfoFacade) {
|
|
|
+ this.orgInfoFacade = orgInfoFacade;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ public void setRestTemplate(RestTemplate restTemplate) {
|
|
|
+ this.restTemplate = restTemplate;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ public void setFhAppSyncProperties(FhAppSyncProperties fhAppSyncProperties) {
|
|
|
+ this.fhAppSyncProperties = fhAppSyncProperties;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ public void setApplyInfoService(IApplyInfoService applyInfoService) {
|
|
|
+ this.applyInfoService = applyInfoService;
|
|
|
+ }
|
|
|
+
|
|
|
+ @CacheLock
|
|
|
+ @Override
|
|
|
+ public ResponseStatus syncAppInfo() {
|
|
|
+ ResponseDTO<FhAppIndexRespDTO> appResource = getAppResource();
|
|
|
+ if (ResponseUtil.isFail(appResource)) {
|
|
|
+ return appResource;
|
|
|
+ }
|
|
|
+ ResponseDTO<List<ApplyInfo>> resp = parseResp(ResponseUtil.getResult(appResource));
|
|
|
+ if (ResponseUtil.isFail(resp)) {
|
|
|
+ return resp;
|
|
|
+ }
|
|
|
+ saveAppInfos(ResponseUtil.getResult(resp));
|
|
|
+ return ResponseStatus.success();
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<ApplyInfo> convertToAppInfo(List<FhResourceRespDTO> fhApps) {
|
|
|
+ if (CollectionUtils.isEmpty(fhApps)) {
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+ List<ApplyInfo> collect = fhApps.stream().map(this::getAppInfo).collect(Collectors.toList());
|
|
|
+
|
|
|
+ // 查询机构信息
|
|
|
+ Set<String> orgCodes = new HashSet<>();
|
|
|
+ collect.forEach(e -> {
|
|
|
+ orgCodes.add(e.getOrgCode());
|
|
|
+ orgCodes.add(e.getManagerOrgCode());
|
|
|
+ });
|
|
|
+ List<OrgInfo> orgInfos = orgInfoFacade.getOrgsByCodes(new ArrayList<>(orgCodes));
|
|
|
+ Map<String, String> orgNameMap = orgInfos.stream()
|
|
|
+ .collect(Collectors.toMap(OrgInfo::getCode, OrgInfo::getFullName));
|
|
|
+
|
|
|
+ // 设置机构信息和密钥信息
|
|
|
+ collect.forEach(e -> {
|
|
|
+ String orgName = orgNameMap.get(e.getOrgCode());
|
|
|
+ e.setOrgName(orgName);
|
|
|
+ String manageOrgName = orgNameMap.get(e.getManagerOrgCode());
|
|
|
+ e.setManagerOrgName(manageOrgName);
|
|
|
+ String apiKey = getApiKeys(e.getApplyCode());
|
|
|
+ String apiSecret = getApiKeys(e.getApplyCode());
|
|
|
+ e.setApiKey(apiKey);
|
|
|
+ e.setSecretKey(apiSecret);
|
|
|
+ });
|
|
|
+
|
|
|
+ return collect;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getApiKeys(String value) {
|
|
|
+ StringBuilder num = new StringBuilder();
|
|
|
+ for (int i = 0; i < 8; i++) {
|
|
|
+ int random = (int) (Math.random() * 10);
|
|
|
+ num.append(random);
|
|
|
+ }
|
|
|
+ return value + num;
|
|
|
+ }
|
|
|
+
|
|
|
+ private ApplyInfo getAppInfo(FhResourceRespDTO dto) {
|
|
|
+ ApplyInfo app = new ApplyInfo();
|
|
|
+ app.setApplyCode(dto.getYyxtbh());
|
|
|
+ app.setApplyName(dto.getYyxtmc());
|
|
|
+ //应用状态和我们自己的系统标识转换
|
|
|
+ Long xtzybs = dto.getXtzybs();
|
|
|
+ if (ObjectUtils.isNotEmpty(xtzybs)) {
|
|
|
+ if (Long.valueOf(1L).equals(xtzybs)) {
|
|
|
+ app.setApplyStatus(BooleanEnum.FALSE.value);
|
|
|
+ } else {
|
|
|
+ app.setApplyStatus(BooleanEnum.TRUE.value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ app.setAppOpsDepName(dto.getYyxtywdwdm());
|
|
|
+ app.setApplyUrl(dto.getYyxtfwdz());
|
|
|
+ app.setOrgCode(dto.getYyxtsqdwdm());
|
|
|
+ app.setManagerOrgCode(dto.getYyxtgldwdm());
|
|
|
+ app.setManagerDescribe(dto.getYyxtsm());
|
|
|
+ app.setDeleted(BooleanEnum.FALSE.value);
|
|
|
+ String sxrq = dto.getSxrq();
|
|
|
+ if (!StringUtils.isEmpty(sxrq)) {
|
|
|
+ app.setLineTime(sxrq);
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ try {
|
|
|
+ Date parse = format.parse(sxrq);
|
|
|
+ app.setRegistrationTime(parse);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("日期解析异常");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ app.setApplyCategory(dto.getYyxtfldm());
|
|
|
+ return app;
|
|
|
+ }
|
|
|
+
|
|
|
+ protected ResponseDTO<List<ApplyInfo>> parseResp(FhAppIndexRespDTO appResource) {
|
|
|
+ if (null == appResource) {
|
|
|
+ log.error("响应结果为空");
|
|
|
+ return ResponseDTO.fail("同步失败", (Object) null);
|
|
|
+ }
|
|
|
+ String messageStatus = appResource.getMessageStatus();
|
|
|
+ String remark = appResource.getRemark();
|
|
|
+ log.info("调用应用同步接口接口返回 messageStatus:{}, remark:{}", messageStatus, remark);
|
|
|
+ if (!FH_SUCCESS_STATUS.equals(messageStatus)) {
|
|
|
+ return ResponseDTO.fail("同步失败", (Object) null);
|
|
|
+ }
|
|
|
+ FhResponseParamRespDTO responseParam = appResource.getResponseParam();
|
|
|
+ Long totalNum = responseParam.getTotalNum();
|
|
|
+ log.info("同步获取的应用资源数量:{}", totalNum);
|
|
|
+ List<FhResourceRespDTO> resources = responseParam.getResources();
|
|
|
+ List<ApplyInfo> applyInfos = convertToAppInfo(resources);
|
|
|
+ return ResponseDTO.success("", applyInfos);
|
|
|
+ }
|
|
|
+
|
|
|
+ protected ResponseDTO<FhAppIndexRespDTO> getAppResource() {
|
|
|
+ String address = fhAppSyncProperties.getAddress();
|
|
|
+ log.info("同步应用资源地址:{}", address);
|
|
|
+ HttpEntity<Map<String,String>> httpEntity = new HttpEntity<>(Collections.emptyMap());
|
|
|
+ ResponseEntity<String> exchange = restTemplate.exchange(address, HttpMethod.POST, httpEntity, String.class);
|
|
|
+ if (!exchange.getStatusCode().is2xxSuccessful()) {
|
|
|
+ log.error("调用同步接口响应异常. 响应结果:{}", JsonUtils.toJSONString(exchange));
|
|
|
+ return ResponseDTO.fail("同步失败", (Object) null);
|
|
|
+ }
|
|
|
+ String body = exchange.getBody();
|
|
|
+ // 解析响应结果
|
|
|
+ if (StringUtils.isBlank(body)) {
|
|
|
+ log.error("同步应用资源,响应内容为空");
|
|
|
+ return ResponseDTO.fail("同步失败", (Object) null);
|
|
|
+ }
|
|
|
+
|
|
|
+ ObjectMapper objectMapper = new ObjectMapper();
|
|
|
+ try {
|
|
|
+ FhAppIndexRespDTO fhAppIndexRespDTO = objectMapper.readValue(body, FhAppIndexRespDTO.class);
|
|
|
+ return ResponseDTO.success("", fhAppIndexRespDTO);
|
|
|
+ } catch (JsonProcessingException e) {
|
|
|
+ log.error("解析同步结果异常.", e);
|
|
|
+ return ResponseDTO.fail("同步失败", (Object) null);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void saveAppInfos(List<ApplyInfo> applyInfoList) {
|
|
|
+ if (CollectionUtils.isEmpty(applyInfoList)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 查询已有的应用信息
|
|
|
+ List<ApplyInfo> all = applyInfoService.findAll();
|
|
|
+ Map<String, ApplyInfo> existMap = all.stream()
|
|
|
+ .collect(Collectors.toMap(ApplyInfo::getApplyCode, e -> e));
|
|
|
+ // 判断插入或是更新
|
|
|
+ Map<Boolean, List<ApplyInfo>> collect = applyInfoList.stream()
|
|
|
+ .collect(Collectors.partitioningBy(e -> existMap.containsKey(e.getApplyCode())));
|
|
|
+ List<ApplyInfo> saveApps = collect.get(Boolean.FALSE);
|
|
|
+ // 保存应用时同时保存密钥信息
|
|
|
+ if (CollectionUtils.isNotEmpty(saveApps)) {
|
|
|
+ applyInfoService.batchSave(saveApps);
|
|
|
+ List<ApplyOauth> oauthList = saveApps.stream()
|
|
|
+ .map(e -> {
|
|
|
+ ApplyOauth applyOauth = new ApplyOauth();
|
|
|
+ applyOauth.setDeleted(BooleanEnum.FALSE.value);
|
|
|
+ applyOauth.setApiKey(e.getApiKey());
|
|
|
+ applyOauth.setSecretKey(e.getSecretKey());
|
|
|
+ applyOauth.setApplyId(e.getId());
|
|
|
+ return applyOauth;
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+ applyOauthService.saveAll(oauthList);
|
|
|
+ }
|
|
|
+ List<ApplyInfo> updateApps = collect.get(Boolean.TRUE);
|
|
|
+ // 更新时不能修改的字段
|
|
|
+ if (CollectionUtils.isNotEmpty(updateApps)) {
|
|
|
+ List<ApplyInfo> existUpdateApps = updateApps.stream()
|
|
|
+ .map(e -> {
|
|
|
+ ApplyInfo existApp = existMap.get(e.getApplyCode());
|
|
|
+ BeanUtil.copyProperties(e, existApp, "id", "apiKey", "secretKey");
|
|
|
+ return existApp;
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+ applyInfoService.batchUpdate(existUpdateApps);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|