|
@@ -1,17 +1,36 @@
|
|
|
package com.dragoninfo.dcuc.app.business.impl;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
import com.dragoninfo.dcuc.app.business.IResourceBusiness;
|
|
|
+import com.dragoninfo.dcuc.app.config.DcucResourceConfig;
|
|
|
+import com.dragoninfo.dcuc.app.dto.*;
|
|
|
import com.dragoninfo.dcuc.app.entity.ResourceSync;
|
|
|
import com.dragoninfo.dcuc.app.enumresources.ResourceTypeEnum;
|
|
|
import com.dragoninfo.dcuc.app.service.IResourceService;
|
|
|
import com.dragoninfo.dcuc.app.service.IResourceSyncService;
|
|
|
+import com.dragoninfo.dcuc.auth.auth.enumresources.YesNotEnum;
|
|
|
+import com.dragoninfo.duceap.core.response.Result;
|
|
|
+import com.dragoninfo.http.HttpUtil;
|
|
|
+import com.dragonsoft.duceap.base.entity.http.ResponseStatus;
|
|
|
import com.dragonsoft.duceap.base.enums.BooleanEnum;
|
|
|
import com.dragonsoft.duceap.commons.util.collections.CollectionUtils;
|
|
|
+import org.apache.http.Header;
|
|
|
+import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
+import org.apache.http.message.BasicHeader;
|
|
|
+import org.apache.http.util.EntityUtils;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
-
|
|
|
+import java.io.IOException;
|
|
|
import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.concurrent.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* @author mazq
|
|
@@ -20,12 +39,25 @@ import java.util.List;
|
|
|
@Component
|
|
|
public class ResourceBusiness implements IResourceBusiness {
|
|
|
|
|
|
+ Logger logger = LoggerFactory.getLogger(ResourceBusiness.class);
|
|
|
+
|
|
|
@Autowired
|
|
|
IResourceService resourceService;
|
|
|
|
|
|
@Autowired
|
|
|
IResourceSyncService syncService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ DcucResourceConfig resourceConfig;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 使用线程池异步获取应用菜单
|
|
|
+ */
|
|
|
+ private ExecutorService executor = new ThreadPoolExecutor(1, 1,
|
|
|
+ 0L, TimeUnit.MILLISECONDS,
|
|
|
+ new LinkedBlockingQueue<Runnable>());
|
|
|
+
|
|
|
+
|
|
|
@Override
|
|
|
public void resourceSync() {
|
|
|
//先同步应用和服务数据
|
|
@@ -45,4 +77,287 @@ public class ResourceBusiness implements IResourceBusiness {
|
|
|
syncService.batchUpdate(resourceSyncs);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<ResourceRequestResultDTO> getResourceRequestResults(ResourceRequestParamDTO requestParamDTO) {
|
|
|
+ List<ResourceRequestResultDTO> pageInfos = new ArrayList<>();
|
|
|
+ Integer pageSize = requestParamDTO.getPageSize();
|
|
|
+ boolean listingAll = requestParamDTO.isListingAll();
|
|
|
+ pageSize = (pageSize == null ? 50 : pageSize);
|
|
|
+ //全量获取
|
|
|
+ //从currentPageNo页获取
|
|
|
+ if (listingAll) {
|
|
|
+ //全量同步默认每页数量,便于下次同步
|
|
|
+ pageSize = resourceConfig.getPageSize();
|
|
|
+ Integer currentPageNo = requestParamDTO.getCurrentPageNo();
|
|
|
+ for (ResourceTypeEnum resourceEnum : ResourceTypeEnum.values()) {
|
|
|
+ //若是增量获取currentPage = 数据库中上次同步的页数
|
|
|
+ //暂时手动输入currentPage
|
|
|
+ //没有就从第一页开始获获取
|
|
|
+ if (null == currentPageNo) {
|
|
|
+ currentPageNo = 0;
|
|
|
+ }
|
|
|
+ ResourceRequestResultDTO listingInfo = getAllResourceListing(currentPageNo, pageSize, resourceEnum);
|
|
|
+ pageInfos.add(listingInfo);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ //根据页码获取
|
|
|
+ List<ListingParamDTO> listingParams = requestParamDTO.getListingParams();
|
|
|
+ for (ListingParamDTO listingParam : listingParams) {
|
|
|
+ ResourceRequestResultDTO listingInfo = getPageNoResourceListing(pageSize, listingParam);
|
|
|
+ pageInfos.add(listingInfo);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //是否需要保存已获取的页数,下次获取使用增量获取?
|
|
|
+ return pageInfos;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据页码获取资源列表
|
|
|
+ *
|
|
|
+ * @param pageSize
|
|
|
+ * @param listingParam
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private ResourceRequestResultDTO getPageNoResourceListing(Integer pageSize, ListingParamDTO listingParam) {
|
|
|
+ String resourceId = listingParam.getResourceId();
|
|
|
+ List<Integer> pageNos = listingParam.getPageNo();
|
|
|
+ ResourceRequestResultDTO resourceListResult = new ResourceRequestResultDTO(resourceId, new ArrayList<Integer>());
|
|
|
+ resourceListResult.setSuccess(YesNotEnum.YES.getValue());
|
|
|
+ for (Integer pageNo : pageNos) {
|
|
|
+ Result<Map<String, Object>> result = getRemoteResource(resourceId, pageNo, pageSize);
|
|
|
+ //只收集未同步成功的资源id和页码
|
|
|
+ if (!ResponseStatus.SUCCESS_CODE.equals(result.getResult())) {
|
|
|
+ Map<String, Object> resultMap = result.getContent();
|
|
|
+ Integer ePageNo = (Integer) resultMap.get("pageNo");
|
|
|
+ resourceListResult.getPageNos().add(ePageNo);
|
|
|
+ resourceListResult.setSuccess(YesNotEnum.NO.getValue());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //不记录总页数,这里不是全量获取
|
|
|
+ resourceListResult.setResourceId(resourceId);
|
|
|
+ return resourceListResult;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 从currentPageNo页获取全量的资源列表
|
|
|
+ *
|
|
|
+ * @param pageSize
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private ResourceRequestResultDTO getAllResourceListing(Integer currentPageNo, Integer pageSize, ResourceTypeEnum resourceTypeEnum) {
|
|
|
+ Integer pageNo = currentPageNo;
|
|
|
+ Integer pages = currentPageNo;
|
|
|
+ String resourceId = resourceTypeEnum.getResourceId();
|
|
|
+ ResourceRequestResultDTO resourceListResult = new ResourceRequestResultDTO(resourceId, new ArrayList<Integer>());
|
|
|
+ resourceListResult.setSuccess(YesNotEnum.YES.getValue());
|
|
|
+ do {
|
|
|
+ Result<Map<String, Object>> result = getRemoteResource(resourceId, pageNo, pageSize);
|
|
|
+ Map<String, Object> resultMap = result.getContent();
|
|
|
+ //收集未同步成功的资源id和页码
|
|
|
+ if (!ResponseStatus.SUCCESS_CODE.equals(result.getResult())) {
|
|
|
+ Integer ePageNo = (Integer) resultMap.get("pageNo");
|
|
|
+ resourceListResult.getPageNos().add(ePageNo);
|
|
|
+ resourceListResult.setSuccess(YesNotEnum.NO.getValue());
|
|
|
+ }
|
|
|
+ Integer allPages = (Integer) resultMap.get("pages");
|
|
|
+ if (null != allPages) {
|
|
|
+ pages = allPages;
|
|
|
+ }
|
|
|
+ ++pageNo;
|
|
|
+ } while (pageNo <= pages);
|
|
|
+ //收集获取列表的信息
|
|
|
+ //这里是全量获取,记录资源获取的总页数,便于下次全量获取使用
|
|
|
+ resourceListResult.setTotalPages(pages);
|
|
|
+ resourceListResult.setResourceId(resourceId);
|
|
|
+ return resourceListResult;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void getAppMenus(List<String> extAppIds) {
|
|
|
+ if(CollectionUtils.isEmpty(extAppIds)){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ executor.submit(new MenuTask(extAppIds));
|
|
|
+ }
|
|
|
+
|
|
|
+ class MenuTask implements Runnable {
|
|
|
+
|
|
|
+ private List<String> appIds;
|
|
|
+
|
|
|
+ public MenuTask(List<String> appIds) {
|
|
|
+ this.appIds = appIds;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ ArrayList<ResourceSyncDTO> list = new ArrayList<>();
|
|
|
+ try {
|
|
|
+ for (String appId : appIds) {
|
|
|
+ String menuStr = getAppMenuFromRemote(appId);
|
|
|
+ JSONObject menuJSON = JSON.parseObject(menuStr);
|
|
|
+ JSONObject appInfo = menuJSON.getJSONObject("tAppSystem");
|
|
|
+ ResourceSyncDTO dto = new ResourceSyncDTO();
|
|
|
+ dto.setResourceId(ResourceTypeEnum.TJ_APP_MENU.getResourceId());
|
|
|
+ dto.setResourceType(ResourceTypeEnum.TJ_APP_MENU.getResourceType());
|
|
|
+ dto.setResourceProvider(ResourceTypeEnum.TJ_APP_MENU.getResouProvider());
|
|
|
+ dto.setSync(BooleanEnum.FALSE.getValue());
|
|
|
+ dto.setExtId(appId);
|
|
|
+ dto.setAppCode(appInfo.getString("yyxtbh"));
|
|
|
+ dto.setAppName(appInfo.getString("yyxtmc"));
|
|
|
+ dto.setContent(menuJSON.getJSONArray("tAppFunctionList").toJSONString());
|
|
|
+ list.add(dto);
|
|
|
+ }
|
|
|
+ syncService.batchInsertResourceSync(list);
|
|
|
+ }catch (Exception e){
|
|
|
+ logger.error("get MenuResource exception.",e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getAppMenuFromRemote(String appId) throws IOException {
|
|
|
+ String busSraId = resourceConfig.getMenuResource().getBusSraId();
|
|
|
+ String busServiceId = resourceConfig.getMenuResource().getBusServiceId();
|
|
|
+ String busServiceFunc = resourceConfig.getMenuResource().getBusServiceFunc();
|
|
|
+ List<Header> heards = getHeaders(busSraId,busServiceId,busServiceFunc);
|
|
|
+ String url = resourceConfig.getResourceUrl() + appId;
|
|
|
+ CloseableHttpResponse response = HttpUtil.get(url,null,heards);
|
|
|
+ return EntityUtils.toString(response.getEntity());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 从网关获取应用信息和服务信息,并录入数据
|
|
|
+ */
|
|
|
+ private Result<Map<String, Object>> getRemoteResource(String resouceId, Integer pageNo, Integer pageSize) {
|
|
|
+ Map<String, Object> resultMap = new HashMap<>();
|
|
|
+ ResourceTypeEnum resourceEnum = ResourceTypeEnum.getByResourceId(resouceId);
|
|
|
+ HttpResult resultFromRemote;
|
|
|
+ try {
|
|
|
+ resultFromRemote = getResourceFromRemote(resourceEnum, pageNo, pageSize);
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.info("get remote resource error from pageNo:{},pageSize:{},resourceId:{}", pageNo, pageSize, resouceId);
|
|
|
+ logger.error("get remote resource error", e);
|
|
|
+ resultMap.put("pageNo", pageNo);
|
|
|
+ resultMap.put("pageSize", pageSize);
|
|
|
+ resultMap.put("resourceId", resouceId);
|
|
|
+ return Result.fail("400", "get remote resource error", resultMap);
|
|
|
+ }
|
|
|
+ if (!ResponseStatus.SUCCESS_CODE.equals(resultFromRemote.getCode())) {
|
|
|
+ logger.info("get remote resource error from pageNo:{},pageSize:{},resourceId:{}", pageNo, pageSize, resouceId);
|
|
|
+ logger.info("get remote resource error:{}", JSON.toJSONString(resultFromRemote));
|
|
|
+ resultMap.put("pageNo", pageNo);
|
|
|
+ resultMap.put("pageSize", pageSize);
|
|
|
+ resultMap.put("resourceId", resouceId);
|
|
|
+ return Result.fail("400", "get remote resource failed", resultMap);
|
|
|
+ }
|
|
|
+ JSONArray records = resultFromRemote.getRecords();
|
|
|
+ //插入资源数据同步表
|
|
|
+ List<ResourceSyncDTO> dtos = insertRemoteResourceSync(resourceEnum, records);
|
|
|
+ if(resourceConfig.getMenuResource().isMenuEnabled() && ResourceTypeEnum.TJ_APP.equals(resourceEnum)){
|
|
|
+ List<String> ids = dtos.stream().map(item -> item.getExtId()).collect(Collectors.toList());
|
|
|
+ if(CollectionUtils.isNotEmpty(ids)){
|
|
|
+ getAppMenus(ids);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //返回总数量和总页数
|
|
|
+ Integer total = resultFromRemote.getTotals();
|
|
|
+ Integer pages = resultFromRemote.getPages();
|
|
|
+ resultMap.put("pageNo", pageNo);
|
|
|
+ resultMap.put("pageSize", pageSize);
|
|
|
+ resultMap.put("resourceId", resouceId);
|
|
|
+ resultMap.put("total", total);
|
|
|
+ resultMap.put("pages", pages);
|
|
|
+ return Result.success(resultMap);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取应用信息
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private HttpResult getResourceFromRemote(ResourceTypeEnum resourceTypeEnum, Integer pageNo, Integer pageSize) throws IOException {
|
|
|
+ HttpResult httpResult = new HttpResult();
|
|
|
+ String url = resourceConfig.getResourceUrl();
|
|
|
+ //获取应用资源
|
|
|
+ if(ResourceTypeEnum.TJ_APP.equals(resourceTypeEnum)){
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ map.put("page",pageNo);
|
|
|
+ map.put("size",pageSize);
|
|
|
+ String param = JSON.toJSONString(map);
|
|
|
+ String busSraId = resourceConfig.getAppResource().getBusSraId();
|
|
|
+ String busServiceId = resourceConfig.getAppResource().getBusServiceId();
|
|
|
+ String busServiceFunc = resourceConfig.getAppResource().getBusServiceFunc();
|
|
|
+ List<Header> headerList = getHeaders(busSraId,busServiceId,busServiceFunc);
|
|
|
+ String response = HttpUtil.postForm(url, param, headerList, null);
|
|
|
+ if(null == response){
|
|
|
+ httpResult.setCode(ResponseStatus.FAIL_CODE);
|
|
|
+ } else {
|
|
|
+ JSONObject parse = JSONObject.parseObject(response);
|
|
|
+ httpResult.setCode(ResponseStatus.SUCCESS_CODE);
|
|
|
+ httpResult.setPages(parse.getInteger("totalPages"));
|
|
|
+ httpResult.setTotals(parse.getInteger("total"));
|
|
|
+ httpResult.setRecords(parse.getJSONArray("content"));
|
|
|
+ }
|
|
|
+ }else if(ResourceTypeEnum.TJ_SERVICE.equals(resourceTypeEnum)){
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ map.put("page",String.valueOf(pageNo));
|
|
|
+ String param = JSON.toJSONString(map);
|
|
|
+ //获取服务资源
|
|
|
+ //服务资源提供方一次查询所有服务资源,未采用分页获取方式
|
|
|
+ String busSraId = resourceConfig.getServiceResource().getBusSraId();
|
|
|
+ String busServiceId = resourceConfig.getServiceResource().getBusServiceId();
|
|
|
+ String busServiceFunc = resourceConfig.getServiceResource().getBusServiceFunc();
|
|
|
+ List<Header> headerList = getHeaders(busSraId,busServiceId,busServiceFunc);
|
|
|
+ String response = HttpUtil.postJSON(url, param,headerList,null);
|
|
|
+ if(null == response){
|
|
|
+ httpResult.setCode(ResponseStatus.FAIL_CODE);
|
|
|
+ } else {
|
|
|
+ JSONObject parse = JSONObject.parseObject(response);
|
|
|
+ JSONObject data = parse.getJSONObject("data");
|
|
|
+ httpResult.setRecords(data.getJSONArray("content"));
|
|
|
+ httpResult.setCode(parse.getString("status"));
|
|
|
+ httpResult.setPages(data.getInteger("totalPages"));
|
|
|
+ httpResult.setTotals(data.getInteger("totalElements"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return httpResult;
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<Header> getHeaders(String busSraId,String busServiceId,String busServiceFunc) {
|
|
|
+ List<Header> heardList = new ArrayList<>();
|
|
|
+ Header busSraIdHeader = new BasicHeader("bus_sra_id",busSraId);
|
|
|
+ Header serviceIdHeader = new BasicHeader("bus_service_id",busServiceId);
|
|
|
+ Header busServiceFuncHeader = new BasicHeader("bus_service_func",busServiceFunc);
|
|
|
+ heardList.add(busSraIdHeader);
|
|
|
+ heardList.add(serviceIdHeader);
|
|
|
+ heardList.add(busServiceFuncHeader);
|
|
|
+ return heardList;
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<ResourceSyncDTO> insertRemoteResourceSync(ResourceTypeEnum resourceEnum, JSONArray records) {
|
|
|
+ List<ResourceSyncDTO> list = new ArrayList<>();
|
|
|
+ for (Object record : records) {
|
|
|
+ String jsonString = JSON.toJSONString(record);
|
|
|
+ ResourceSyncDTO dto = new ResourceSyncDTO(resourceEnum.getResourceId(),
|
|
|
+ resourceEnum.getResourceType(), resourceEnum.getResouProvider());
|
|
|
+ dto.setContent(jsonString);
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(jsonString);
|
|
|
+ String appCode = jsonObject.getString("yyxtbh");
|
|
|
+ String serviceCode = jsonObject.getString("FWZYBSF");
|
|
|
+ String appName = jsonObject.getString("yyxtmc");
|
|
|
+ String serviceName = jsonObject.getString("FWZYMC");
|
|
|
+ dto.setExtId(jsonObject.getString("id"));
|
|
|
+ dto.setAppCode(appCode);
|
|
|
+ dto.setAppName(appName);
|
|
|
+ dto.setServiceCode(serviceCode);
|
|
|
+ dto.setServiceName(serviceName);
|
|
|
+ dto.setSync(YesNotEnum.NO.getValue());
|
|
|
+ list.add(dto);
|
|
|
+ }
|
|
|
+ if(CollectionUtils.isNotEmpty(list)){
|
|
|
+ syncService.batchInsertResourceSync(list);
|
|
|
+ }
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+
|
|
|
}
|