diff --git a/src/main/java/neatlogic/module/inspect/api/configfile/ListInspectConfigFileResourceApi.java b/src/main/java/neatlogic/module/inspect/api/configfile/ListInspectConfigFileResourceApi.java index c01e4772c2ab3b9ee1e051df81a9d7a446d19c5d..c9be836de40f9e19870eeed8af44f32455ad3700 100644 --- a/src/main/java/neatlogic/module/inspect/api/configfile/ListInspectConfigFileResourceApi.java +++ b/src/main/java/neatlogic/module/inspect/api/configfile/ListInspectConfigFileResourceApi.java @@ -110,14 +110,15 @@ public class ListInspectConfigFileResourceApi extends PrivateApiComponentBase { } } } else { - IResourceCenterResourceCrossoverService resourceCenterResourceCrossoverService = CrossoverServiceFactory.getApi(IResourceCenterResourceCrossoverService.class); - resourceCenterResourceCrossoverService.handleBatchSearchList(searchVo); + resourceCrossoverService.handleBatchSearchList(searchVo); + resourceCrossoverService.setIpFieldAttrIdAndNameFieldAttrId(searchVo); int count = inspectConfigFileMapper.getInspectResourceCount(searchVo); if (count > 0) { searchVo.setRowNum(count); + resourceCrossoverService.setIsIpFieldSortAndIsNameFieldSort(searchVo); List idList = inspectConfigFileMapper.getInspectResourceIdList(searchVo); if (CollectionUtils.isNotEmpty(idList)) { - Map> tagMap = resourceCenterResourceCrossoverService.getResourceTagByResourceIdList(idList); + Map> tagMap = resourceCrossoverService.getResourceTagByResourceIdList(idList); List autoexecJobPhaseNodeList = autoexecJobMapper.getAutoexecJobNodeListByResourceIdList(idList); Map autoexecJobPhaseNodeMap = autoexecJobPhaseNodeList.stream().collect(Collectors.toMap(e -> e.getResourceId(), e -> e)); inspectResourceList = inspectConfigFileMapper.getInspectResourceListByIdList(idList); diff --git a/src/main/java/neatlogic/module/inspect/api/report/ExportInspectResourceReportApi.java b/src/main/java/neatlogic/module/inspect/api/report/ExportInspectResourceReportApi.java new file mode 100644 index 0000000000000000000000000000000000000000..d292c8d224a8bcde63044c2c17097b7dbdc1a850 --- /dev/null +++ b/src/main/java/neatlogic/module/inspect/api/report/ExportInspectResourceReportApi.java @@ -0,0 +1,222 @@ +/*Copyright (C) 2024 深圳极向量科技有限公司 All Rights Reserved. + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with this program. If not, see .*/ + +package neatlogic.module.inspect.api.report; + +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import neatlogic.framework.auth.core.AuthAction; +import neatlogic.framework.cmdb.crossover.ICiCrossoverMapper; +import neatlogic.framework.cmdb.crossover.IResourceCenterResourceCrossoverService; +import neatlogic.framework.cmdb.dto.ci.CiVo; +import neatlogic.framework.cmdb.dto.resourcecenter.ResourceSearchVo; +import neatlogic.framework.cmdb.dto.resourcecenter.ResourceVo; +import neatlogic.framework.cmdb.exception.ci.CiNotFoundException; +import neatlogic.framework.common.constvalue.ApiParamType; +import neatlogic.framework.common.constvalue.InspectStatus; +import neatlogic.framework.crossover.CrossoverServiceFactory; +import neatlogic.framework.inspect.auth.INSPECT_BASE; +import neatlogic.framework.inspect.dao.mapper.InspectMapper; +import neatlogic.framework.inspect.dto.InspectResourceVo; +import neatlogic.framework.restful.annotation.Description; +import neatlogic.framework.restful.annotation.Input; +import neatlogic.framework.restful.annotation.OperationType; +import neatlogic.framework.restful.annotation.Param; +import neatlogic.framework.restful.constvalue.OperationTypeEnum; +import neatlogic.framework.restful.core.privateapi.PrivateBinaryStreamApiComponentBase; +import neatlogic.framework.util.FileUtil; +import neatlogic.framework.util.TimeUtil; +import neatlogic.framework.util.excel.ExcelBuilder; +import neatlogic.framework.util.excel.SheetBuilder; +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.poi.hssf.util.HSSFColor; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.xssf.streaming.SXSSFWorkbook; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.io.OutputStream; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +@Service +@AuthAction(action = INSPECT_BASE.class) +@OperationType(type = OperationTypeEnum.SEARCH) +public class ExportInspectResourceReportApi extends PrivateBinaryStreamApiComponentBase { + + private static Logger logger = LoggerFactory.getLogger(ExportInspectResourceReportApi.class); + + @Resource + private InspectMapper inspectMapper; + + @Override + public String getToken() { + return "inspect/resource/report/export"; + } + + @Override + public String getName() { + return "nmiar.exportinspectresourcereportapi.getname"; + } + + @Override + public String getConfig() { + return null; + } + + @Input({ + @Param(name = "keyword", type = ApiParamType.STRING, xss = true, desc = "common.keyword"), + @Param(name = "typeId", type = ApiParamType.LONG, isRequired = true, desc = "common.typeid"), + @Param(name = "protocolIdList", type = ApiParamType.JSONARRAY, desc = "term.cmdb.protocolidlist"), + @Param(name = "stateIdList", type = ApiParamType.JSONARRAY, desc = "term.cmdb.stateidlist"), + @Param(name = "vendorIdList", type = ApiParamType.JSONARRAY, desc = "term.cmdb.vendoridlist"), + @Param(name = "envIdList", type = ApiParamType.JSONARRAY, desc = "term.cmdb.envidlist"), + @Param(name = "appSystemIdList", type = ApiParamType.JSONARRAY, desc = "term.appsystemidlist"), + @Param(name = "appModuleIdList", type = ApiParamType.JSONARRAY, desc = "term.cmdb.appmoduleidlist"), + @Param(name = "typeIdList", type = ApiParamType.JSONARRAY, desc = "term.cmdb.typeidlist"), + @Param(name = "tagIdList", type = ApiParamType.JSONARRAY, desc = "common.tagidlist"), + @Param(name = "inspectStatusList", type = ApiParamType.JSONARRAY, desc = "term.inspect.inspectstatuslist"), + @Param(name = "searchField", type = ApiParamType.STRING, desc = "term.cmdb.searchfield"), + @Param(name = "batchSearchList", type = ApiParamType.JSONARRAY, desc = "term.cmdb.batchsearchlist"), + @Param(name = "defaultValue", type = ApiParamType.JSONARRAY, desc = "common.defaultvalue"), + }) + @Description(desc = "nmiar.exportinspectresourcereportapi.getname") + @Override + public Object myDoService(JSONObject paramObj, HttpServletRequest request, HttpServletResponse response) throws Exception { + ICiCrossoverMapper ciCrossoverMapper = CrossoverServiceFactory.getApi(ICiCrossoverMapper.class); + Long typeId = paramObj.getLong("typeId"); + CiVo ciVo = ciCrossoverMapper.getCiById(typeId); + if (ciVo == null) { + throw new CiNotFoundException(typeId); + } + String fileNameEncode = ciVo.getId() + "_" + ciVo.getLabel() + ".xlsx"; + fileNameEncode = FileUtil.getEncodedFileName(fileNameEncode); + response.setContentType("application/vnd.ms-excel;charset=utf-8"); + response.setHeader("Content-Disposition", " attachment; filename=\"" + fileNameEncode + "\""); + + ExcelBuilder builder = new ExcelBuilder(SXSSFWorkbook.class); + SheetBuilder sheetBuilder = builder.withBorderColor(HSSFColor.HSSFColorPredefined.GREY_40_PERCENT) + .withHeadFontColor(HSSFColor.HSSFColorPredefined.WHITE) + .withHeadBgColor(HSSFColor.HSSFColorPredefined.DARK_BLUE) + .withColumnWidth(30) + .addSheet("数据") + .withHeaderList(getHeaderList()) + .withColumnList(getColumnList()); + Workbook workbook = builder.build(); + List inspectResourceVoList = null; + JSONArray defaultValue = paramObj.getJSONArray("defaultValue"); + if (CollectionUtils.isNotEmpty(defaultValue)) { + List idList = defaultValue.toJavaList(Long.class); + inspectResourceVoList = inspectMapper.getInspectResourceListByIdList(idList); + for (ResourceVo resourceVo : inspectResourceVoList) { + Map dataMap = resourceConvertDataMap(resourceVo); + sheetBuilder.addData(dataMap); + } + } else { + IResourceCenterResourceCrossoverService resourceCrossoverService = CrossoverServiceFactory.getApi(IResourceCenterResourceCrossoverService.class); + ResourceSearchVo searchVo = resourceCrossoverService.assembleResourceSearchVo(paramObj); + resourceCrossoverService.handleBatchSearchList(searchVo); + resourceCrossoverService.setIpFieldAttrIdAndNameFieldAttrId(searchVo); + int rowNum = inspectMapper.getInspectResourceCount(searchVo); + if (rowNum > 0) { + searchVo.setPageSize(100); + searchVo.setRowNum(rowNum); + if (StringUtils.isNotBlank(searchVo.getKeyword())) { + int ipKeywordCount = inspectMapper.getInspectResourceCountByIpKeyword(searchVo); + if (ipKeywordCount > 0) { + searchVo.setIsIpFieldSort(1); + } else { + int nameKeywordCount = inspectMapper.getInspectResourceCountByNameKeyword(searchVo); + if (nameKeywordCount > 0) { + searchVo.setIsNameFieldSort(1); + } + } + } + for (int i = 1; i <= searchVo.getPageCount(); i++) { + searchVo.setCurrentPage(i); + List idList = inspectMapper.getInspectResourceIdList(searchVo); + if (CollectionUtils.isNotEmpty(idList)) { + inspectResourceVoList = inspectMapper.getInspectResourceListByIdList(idList); + for (ResourceVo resourceVo : inspectResourceVoList) { + Map dataMap = resourceConvertDataMap(resourceVo); + sheetBuilder.addData(dataMap); + } + } + } + } + } + try (OutputStream os = response.getOutputStream()) { + workbook.write(os); + } catch (IOException e) { + logger.error(e.getMessage(), e); + } + return null; + } + + /** + * 表头信息 + * @return + */ + private List getHeaderList() { + List headerList = new ArrayList<>(); + headerList.add("资产id"); + headerList.add("IP地址"); + headerList.add("类型"); + headerList.add("名称"); + headerList.add("巡检状态"); + headerList.add("描述"); + return headerList; + } + + /** + * 每列对应的key + * @return + */ + private List getColumnList() { + List columnList = new ArrayList<>(); + columnList.add("resourceId"); + columnList.add("ip:port"); + columnList.add("typeLabel"); + columnList.add("name"); + columnList.add("inspectStatus"); + columnList.add("description"); + return columnList; + } + + /** + * 资产对象转换成excel中一行数据dataMap + * @param resourceVo 资产对象 + */ + private Map resourceConvertDataMap(ResourceVo resourceVo) { + Map dataMap = new HashMap<>(); + dataMap.put("ip:port", resourceVo.getIp() + (resourceVo.getPort() != null ? ":" + resourceVo.getPort() : StringUtils.EMPTY)); + dataMap.put("typeLabel", resourceVo.getTypeLabel()); + dataMap.put("resourceId", resourceVo.getId()); + dataMap.put("name", resourceVo.getName()); + dataMap.put("description", resourceVo.getDescription()); + dataMap.put("inspectStatus", StringUtils.isNotBlank(resourceVo.getInspectStatus()) ? InspectStatus.getText(resourceVo.getInspectStatus()) + " " + + (resourceVo.getInspectTime() != null ? TimeUtil.convertDateToString(resourceVo.getInspectTime(), TimeUtil.YYYY_MM_DD_HH_MM_SS) + : StringUtils.EMPTY) : StringUtils.EMPTY); + return dataMap; + } +} diff --git a/src/main/java/neatlogic/module/inspect/api/report/InspectResourceReportSearchApi.java b/src/main/java/neatlogic/module/inspect/api/report/InspectResourceReportSearchApi.java index 8e5caf79d0994b321b4441410f20d5db9ee366f4..90a8db769f591ccba736f8fd6c926be3bf9a24fa 100644 --- a/src/main/java/neatlogic/module/inspect/api/report/InspectResourceReportSearchApi.java +++ b/src/main/java/neatlogic/module/inspect/api/report/InspectResourceReportSearchApi.java @@ -81,6 +81,8 @@ public class InspectResourceReportSearchApi extends PrivateApiComponentBase { public Object myDoService(JSONObject paramObj) throws Exception { IResourceCenterResourceCrossoverService resourceCrossoverService = CrossoverServiceFactory.getApi(IResourceCenterResourceCrossoverService.class); ResourceSearchVo searchVo = resourceCrossoverService.assembleResourceSearchVo(paramObj); + resourceCrossoverService.handleBatchSearchList(searchVo); + resourceCrossoverService.setIpFieldAttrIdAndNameFieldAttrId(searchVo); return TableResultUtil.getResult(inspectReportService.getInspectResourceReportList(searchVo), searchVo); } diff --git a/src/main/java/neatlogic/module/inspect/service/InspectReportServiceImpl.java b/src/main/java/neatlogic/module/inspect/service/InspectReportServiceImpl.java index 49d01e37d1671105b88cbbd8320678b90e2c50f6..1c4f3cf69dc8dc15c096b04c98afb176231d7b19 100644 --- a/src/main/java/neatlogic/module/inspect/service/InspectReportServiceImpl.java +++ b/src/main/java/neatlogic/module/inspect/service/InspectReportServiceImpl.java @@ -153,8 +153,6 @@ public class InspectReportServiceImpl implements InspectReportService { public List getInspectResourceReportList(ResourceSearchVo searchVo) { if (CollectionUtils.isEmpty(searchVo.getIdList())) { List inspectResourceVoList = null; - IResourceCenterResourceCrossoverService resourceCenterResourceCrossoverService = CrossoverServiceFactory.getApi(IResourceCenterResourceCrossoverService.class); - resourceCenterResourceCrossoverService.handleBatchSearchList(searchVo); int resourceCount = inspectMapper.getInspectResourceCount(searchVo); if (resourceCount > 0) { searchVo.setRowNum(resourceCount);