diff --git a/src/main/java/neatlogic/framework/cmdb/crossover/IResourceCenterResourceCrossoverService.java b/src/main/java/neatlogic/framework/cmdb/crossover/IResourceCenterResourceCrossoverService.java index 65881124b4c1ee80d816380ee9a7fe3443cad676..00f2ca2a1ed86c551dee658ccbba35eae804976a 100644 --- a/src/main/java/neatlogic/framework/cmdb/crossover/IResourceCenterResourceCrossoverService.java +++ b/src/main/java/neatlogic/framework/cmdb/crossover/IResourceCenterResourceCrossoverService.java @@ -67,5 +67,13 @@ public interface IResourceCenterResourceCrossoverService extends ICrossoverServi */ Map> getResourceTagByResourceIdList(List idList); + String buildGetResourceIdListSql(ResourceSearchVo searchVo); + + String buildGetResourceCountSql(ResourceSearchVo searchVo); + + String buildGetResourceListSql(List idList, List selectFieldNameList); + + String buildGetResourceListSql(List idList); + } diff --git a/src/main/java/neatlogic/framework/cmdb/crossover/IResourceCrossoverMapper.java b/src/main/java/neatlogic/framework/cmdb/crossover/IResourceCrossoverMapper.java index fd692bcdd69400ec8e6fe765b55f3c684433a6eb..e2237c56fc086ec68cd7a983d9065520e688eebf 100644 --- a/src/main/java/neatlogic/framework/cmdb/crossover/IResourceCrossoverMapper.java +++ b/src/main/java/neatlogic/framework/cmdb/crossover/IResourceCrossoverMapper.java @@ -33,14 +33,20 @@ public interface IResourceCrossoverMapper extends ICrossoverService { int getResourceCount(ResourceSearchVo searchVo); + int getResourceCountBySql(String sql); + int getResourceCountByDynamicCondition(@Param("searchVo") ResourceSearchVo searchVo, @Param("conditionSql") String conditionSql); List getResourceIdList(ResourceSearchVo searchVo); + List getResourceIdListBySql(String sql); + List getResourceIdListByDynamicCondition(@Param("searchVo") ResourceSearchVo searchVo, @Param("conditionSql") String conditionSql); List getResourceListByIdList(List idList); + List getResourceListBySql(String sql); + List getAppInstanceResourceListByIdListSimple(List idList); Long getResourceIdByIpAndPortAndName(ResourceSearchVo searchVo); diff --git a/src/main/java/neatlogic/framework/cmdb/dto/resourcecenter/ApplicationListDisplayVo.java b/src/main/java/neatlogic/framework/cmdb/dto/resourcecenter/ApplicationListDisplayVo.java index d9b6fcc6aa296e4dacd6a028c19eda3cec0d735e..c5c985e2b214f03efcf1a1c716bf2d9dddfa760b 100644 --- a/src/main/java/neatlogic/framework/cmdb/dto/resourcecenter/ApplicationListDisplayVo.java +++ b/src/main/java/neatlogic/framework/cmdb/dto/resourcecenter/ApplicationListDisplayVo.java @@ -21,7 +21,9 @@ import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.annotation.JSONField; -public class ApplicationListDisplayVo { +import java.io.Serializable; + +public class ApplicationListDisplayVo implements Serializable { private Long id; private Long appSystemId; diff --git a/src/main/java/neatlogic/framework/cmdb/dto/resourcecenter/AssetListDisplayVo.java b/src/main/java/neatlogic/framework/cmdb/dto/resourcecenter/AssetListDisplayVo.java index 549d9b44a4fa9efdb448995f4ef3138e8281cb74..70960094af5c90069828e8a00acc84406e872763 100644 --- a/src/main/java/neatlogic/framework/cmdb/dto/resourcecenter/AssetListDisplayVo.java +++ b/src/main/java/neatlogic/framework/cmdb/dto/resourcecenter/AssetListDisplayVo.java @@ -21,7 +21,9 @@ import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.annotation.JSONField; -public class AssetListDisplayVo { +import java.io.Serializable; + +public class AssetListDisplayVo implements Serializable { private Long id; private String rootCiName; diff --git a/src/main/java/neatlogic/framework/cmdb/dto/resourcecenter/config/ResourceEntityConfigVo.java b/src/main/java/neatlogic/framework/cmdb/dto/resourcecenter/config/ResourceEntityConfigVo.java index 4d1f458bfbedbef69e2c3c616ba8c1654f46ab46..b4e6df29db346571faf64b47b6a20fca3c5cad8b 100644 --- a/src/main/java/neatlogic/framework/cmdb/dto/resourcecenter/config/ResourceEntityConfigVo.java +++ b/src/main/java/neatlogic/framework/cmdb/dto/resourcecenter/config/ResourceEntityConfigVo.java @@ -36,6 +36,10 @@ public class ResourceEntityConfigVo { private String sceneTemplateName; + List selectItemFieldNameList; + + List filterItemFieldNameList; + public String getMainCi() { return mainCi; } @@ -91,4 +95,20 @@ public class ResourceEntityConfigVo { public void setSceneTemplateName(String sceneTemplateName) { this.sceneTemplateName = sceneTemplateName; } + + public List getSelectItemFieldNameList() { + return selectItemFieldNameList; + } + + public void setSelectItemFieldNameList(List selectItemFieldNameList) { + this.selectItemFieldNameList = selectItemFieldNameList; + } + + public List getFilterItemFieldNameList() { + return filterItemFieldNameList; + } + + public void setFilterItemFieldNameList(List filterItemFieldNameList) { + this.filterItemFieldNameList = filterItemFieldNameList; + } } diff --git a/src/main/java/neatlogic/framework/cmdb/dto/resourcecenter/config/ResourceEntityVo.java b/src/main/java/neatlogic/framework/cmdb/dto/resourcecenter/config/ResourceEntityVo.java index 669f7778cae18c76b49abb333173ed03c2a26cfd..75b23389493325bd971b0a6ed3b41d6e9ecc46a3 100644 --- a/src/main/java/neatlogic/framework/cmdb/dto/resourcecenter/config/ResourceEntityVo.java +++ b/src/main/java/neatlogic/framework/cmdb/dto/resourcecenter/config/ResourceEntityVo.java @@ -24,9 +24,10 @@ import neatlogic.framework.common.dto.ValueTextVo; import neatlogic.framework.restful.annotation.EntityField; import org.apache.commons.lang3.StringUtils; +import java.io.Serializable; import java.util.*; -public class ResourceEntityVo { +public class ResourceEntityVo implements Serializable { @EntityField(name = "common.name", type = ApiParamType.STRING) private String name; @EntityField(name = "common.cnname", type = ApiParamType.STRING) diff --git a/src/main/java/neatlogic/framework/cmdb/dto/resourcecenter/config/ResourceQueryCriteriaVo.java b/src/main/java/neatlogic/framework/cmdb/dto/resourcecenter/config/ResourceQueryCriteriaVo.java new file mode 100644 index 0000000000000000000000000000000000000000..2412d89ce711a8be85a588061d8587a29d6e891a --- /dev/null +++ b/src/main/java/neatlogic/framework/cmdb/dto/resourcecenter/config/ResourceQueryCriteriaVo.java @@ -0,0 +1,276 @@ +/* + * Copyright (C) 2025 深圳极向量科技有限公司 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.framework.cmdb.dto.resourcecenter.config; + +import com.alibaba.fastjson.JSONArray; +import neatlogic.framework.cmdb.dto.resourcecenter.ResourceSearchVo; +import neatlogic.framework.common.constvalue.ApiParamType; +import neatlogic.framework.dto.AuthenticationInfoVo; +import neatlogic.framework.restful.annotation.EntityField; + +import java.util.List; + +public class ResourceQueryCriteriaVo { + private List keywordList; + @EntityField(name = "协议id列表", type = ApiParamType.JSONARRAY) + private List protocolIdList; + @EntityField(name = "标签id列表", type = ApiParamType.JSONARRAY) + private List tagIdList; + @EntityField(name = "巡检作业状态列表", type = ApiParamType.JSONARRAY) + private List inspectJobPhaseNodeStatusList; + @EntityField(name = "是否有cmdb管理权限(模型或配置项管理权限)或 is.resourcecenter.auth = 1", type = ApiParamType.BOOLEAN) + private Boolean isHasAuth; + @EntityField(name = "校验团体类型", type = ApiParamType.STRING) + private String cmdbGroupType; + @EntityField(name = "批量搜索值列表", type = ApiParamType.JSONARRAY) + private List batchSearchList; + @EntityField(name = "批量搜索字段", type = ApiParamType.STRING) + private String searchField; + @EntityField(name = "类型id列表", type = ApiParamType.JSONARRAY) + private List typeIdList; + @EntityField(name = "满足权限的类型id列表", type = ApiParamType.JSONARRAY) + private List authedTypeIdList; + @EntityField(name = "状态id列表", type = ApiParamType.JSONARRAY) + private List stateIdList; + @EntityField(name = "厂商id列表", type = ApiParamType.JSONARRAY) + private List vendorIdList; + @EntityField(name = "应用环境id列表", type = ApiParamType.JSONARRAY) + private List envIdList; + @EntityField(name = "是否存在未配置环境", type = ApiParamType.BOOLEAN) + private Boolean isExistNoEnv; + @EntityField(name = "应用系统id列表", type = ApiParamType.JSONARRAY) + private List appSystemIdList; + @EntityField(name = "应用模块id列表", type = ApiParamType.JSONARRAY) + private List appModuleIdList; + @EntityField(name = "默认值", type = ApiParamType.JSONARRAY) + private JSONArray defaultValue; + @EntityField(name = "资产id列表", type = ApiParamType.JSONARRAY) + private List idList; + @EntityField(name = "巡检状态列表", type = ApiParamType.JSONARRAY) + private List inspectStatusList; + @EntityField(name = "用户分组角色列表", type = ApiParamType.JSONARRAY) + private AuthenticationInfoVo authenticationInfo; + @EntityField(name = "IP字段映射的属性ID", type = ApiParamType.LONG) + private Long ipFieldAttrId; + @EntityField(name = "name字段映射的属性ID", type = ApiParamType.LONG) + private Long nameFieldAttrId; + + public ResourceQueryCriteriaVo() { + } + + public ResourceQueryCriteriaVo(ResourceSearchVo searchVo) { + this.keywordList = searchVo.getKeywordList(); + this.protocolIdList = searchVo.getProtocolIdList(); + this.tagIdList = searchVo.getTagIdList(); + this.inspectJobPhaseNodeStatusList = searchVo.getInspectJobPhaseNodeStatusList(); + this.isHasAuth = searchVo.getIsHasAuth(); + this.cmdbGroupType = searchVo.getCmdbGroupType(); + this.batchSearchList = searchVo.getBatchSearchList(); + this.searchField = searchVo.getSearchField(); + this.typeIdList = searchVo.getTypeIdList(); + this.authedTypeIdList = searchVo.getAuthedTypeIdList(); + this.stateIdList = searchVo.getStateIdList(); + this.vendorIdList = searchVo.getVendorIdList(); + this.envIdList = searchVo.getEnvIdList(); + this.isExistNoEnv = searchVo.getExistNoEnv(); + this.appSystemIdList = searchVo.getAppSystemIdList(); + this.appModuleIdList = searchVo.getAppModuleIdList(); + this.defaultValue = searchVo.getDefaultValue(); + this.idList = searchVo.getIdList(); + this.inspectStatusList = searchVo.getInspectStatusList(); + this.authenticationInfo = searchVo.getAuthenticationInfo(); + this.ipFieldAttrId = searchVo.getIpFieldAttrId(); + this.nameFieldAttrId = searchVo.getNameFieldAttrId(); + } + + public List getKeywordList() { + return keywordList; + } + + public void setKeywordList(List keywordList) { + this.keywordList = keywordList; + } + + public List getProtocolIdList() { + return protocolIdList; + } + + public void setProtocolIdList(List protocolIdList) { + this.protocolIdList = protocolIdList; + } + + public List getTagIdList() { + return tagIdList; + } + + public void setTagIdList(List tagIdList) { + this.tagIdList = tagIdList; + } + + public List getInspectJobPhaseNodeStatusList() { + return inspectJobPhaseNodeStatusList; + } + + public void setInspectJobPhaseNodeStatusList(List inspectJobPhaseNodeStatusList) { + this.inspectJobPhaseNodeStatusList = inspectJobPhaseNodeStatusList; + } + + public Boolean getIsHasAuth() { + return isHasAuth; + } + + public void setIsHasAuth(Boolean isHasAuth) { + this.isHasAuth = isHasAuth; + } + + public String getCmdbGroupType() { + return cmdbGroupType; + } + + public void setCmdbGroupType(String cmdbGroupType) { + this.cmdbGroupType = cmdbGroupType; + } + + public List getBatchSearchList() { + return batchSearchList; + } + + public void setBatchSearchList(List batchSearchList) { + this.batchSearchList = batchSearchList; + } + + public String getSearchField() { + return searchField; + } + + public void setSearchField(String searchField) { + this.searchField = searchField; + } + + public List getTypeIdList() { + return typeIdList; + } + + public void setTypeIdList(List typeIdList) { + this.typeIdList = typeIdList; + } + + public List getAuthedTypeIdList() { + return authedTypeIdList; + } + + public void setAuthedTypeIdList(List authedTypeIdList) { + this.authedTypeIdList = authedTypeIdList; + } + + public List getStateIdList() { + return stateIdList; + } + + public void setStateIdList(List stateIdList) { + this.stateIdList = stateIdList; + } + + public List getVendorIdList() { + return vendorIdList; + } + + public void setVendorIdList(List vendorIdList) { + this.vendorIdList = vendorIdList; + } + + public List getEnvIdList() { + return envIdList; + } + + public void setEnvIdList(List envIdList) { + this.envIdList = envIdList; + } + + public Boolean getExistNoEnv() { + return isExistNoEnv; + } + + public void setExistNoEnv(Boolean existNoEnv) { + isExistNoEnv = existNoEnv; + } + + public List getAppSystemIdList() { + return appSystemIdList; + } + + public void setAppSystemIdList(List appSystemIdList) { + this.appSystemIdList = appSystemIdList; + } + + public List getAppModuleIdList() { + return appModuleIdList; + } + + public void setAppModuleIdList(List appModuleIdList) { + this.appModuleIdList = appModuleIdList; + } + + public JSONArray getDefaultValue() { + return defaultValue; + } + + public void setDefaultValue(JSONArray defaultValue) { + this.defaultValue = defaultValue; + } + + public List getIdList() { + return idList; + } + + public void setIdList(List idList) { + this.idList = idList; + } + + public List getInspectStatusList() { + return inspectStatusList; + } + + public void setInspectStatusList(List inspectStatusList) { + this.inspectStatusList = inspectStatusList; + } + + public AuthenticationInfoVo getAuthenticationInfo() { + return authenticationInfo; + } + + public void setAuthenticationInfo(AuthenticationInfoVo authenticationInfo) { + this.authenticationInfo = authenticationInfo; + } + + public Long getIpFieldAttrId() { + return ipFieldAttrId; + } + + public void setIpFieldAttrId(Long ipFieldAttrId) { + this.ipFieldAttrId = ipFieldAttrId; + } + + public Long getNameFieldAttrId() { + return nameFieldAttrId; + } + + public void setNameFieldAttrId(Long nameFieldAttrId) { + this.nameFieldAttrId = nameFieldAttrId; + } +} diff --git a/src/main/java/neatlogic/framework/cmdb/enums/CmdbTenantConfig.java b/src/main/java/neatlogic/framework/cmdb/enums/CmdbTenantConfig.java index 92fbcb0f8bd7ef4b9b4b81509927a99a5a24ad1a..4614770e0023baeac1a77ba0bb0ce860ecd1d7c5 100644 --- a/src/main/java/neatlogic/framework/cmdb/enums/CmdbTenantConfig.java +++ b/src/main/java/neatlogic/framework/cmdb/enums/CmdbTenantConfig.java @@ -19,7 +19,8 @@ import neatlogic.framework.config.ITenantConfig; import neatlogic.framework.util.$; public enum CmdbTenantConfig implements ITenantConfig { - IS_RESOURCECENTER_AUTH("is.resourcecenter.auth", "0", "nfce.cmdbtenantconfig.isresourcecenterauth") + IS_RESOURCECENTER_AUTH("is.resourcecenter.auth", "0", "nfce.cmdbtenantconfig.isresourcecenterauth"), + RESOURCECENTER_DATA_COMPARISON_MODE_ENABLE("resourcecenter.data.comparison.mode.enable", "0", "是否开启资产清单新SQL和旧SQL查询结果对比模式,结果不一致会打印error日志"), ; String key; diff --git a/src/main/java/neatlogic/framework/cmdb/utils/ResourceViewGenerateSqlUtil.java b/src/main/java/neatlogic/framework/cmdb/utils/ResourceViewGenerateSqlUtil.java index a1ce3acd5f0edd79252c8662c0d7629e6bda1064..aa0a5d19716bf2940fc2623377fd5208ee46f15b 100644 --- a/src/main/java/neatlogic/framework/cmdb/utils/ResourceViewGenerateSqlUtil.java +++ b/src/main/java/neatlogic/framework/cmdb/utils/ResourceViewGenerateSqlUtil.java @@ -43,26 +43,44 @@ public class ResourceViewGenerateSqlUtil { private CiVo mainCiVo; private List fieldMappingList; private List leftJoinList; + + List selectItemFieldNameList; + + List filterItemFieldNameList; //sql语句中已经存在的表 private Map joinedTableMap; //sql语句关联表中相等的列 private Map equalColumnMap; + private Map filterItemFieldName2ColumnMap; + public ResourceViewGenerateSqlUtil(ResourceEntityConfigVo config) { this.mainCiVo = config.getMainCiVo(); this.fieldMappingList = config.getFieldMappingList(); this.leftJoinList = config.getLeftJoinList(); + this.selectItemFieldNameList = config.getSelectItemFieldNameList(); + this.filterItemFieldNameList = config.getFilterItemFieldNameList(); } - public String getSql() { + public PlainSelect getSql() { PlainSelect plainSelect = initPlainSelectByMainResourceId(mainCiVo); for (ResourceEntityLeftJoinVo leftJoinVo : leftJoinList) { addJoinTable(leftJoinVo, plainSelect); } for (ResourceEntityFieldMappingVo fieldMappingVo : fieldMappingList) { - addJoinTableByFieldMapping(fieldMappingVo, plainSelect, mainCiVo); + String field = fieldMappingVo.getField(); + if (selectItemFieldNameList.contains(field) || filterItemFieldNameList.contains(field)) { + Column column = addJoinTableByFieldMapping(fieldMappingVo, plainSelect, mainCiVo); + if (filterItemFieldNameList.contains(field)) { + filterItemFieldName2ColumnMap.put(field, column); + } + } } - return plainSelect.toString(); + return plainSelect; + } + + public Map getFilterItemFieldName2ColumnMap() { + return this.filterItemFieldName2ColumnMap; } private void addJoinTable(Table table) { @@ -114,6 +132,7 @@ public class ResourceViewGenerateSqlUtil { private PlainSelect initPlainSelectByMainResourceId(CiVo mainCiVo) { joinedTableMap = new HashMap<>(); equalColumnMap = new HashMap<>(); + filterItemFieldName2ColumnMap = new HashMap<>(); String tableName = "cmdb_" + mainCiVo.getId(); String tableAlias = tableName + "_" + mainCiVo.getName(); Table cmdbCiIdTable = new Table(TenantContext.get().getDataDbName(), tableName).withAlias(new Alias(tableAlias).withUseAs(false)); @@ -298,11 +317,15 @@ public class ResourceViewGenerateSqlUtil { toAttr = toAttr.substring(1); } Column column = new Column(cmdbCiIdTable, toAttr); - plainSelect.addSelectItems(new SelectExpressionItem(column).withAlias(new Alias(field))); + if (selectItemFieldNameList.contains(field)) { + plainSelect.addSelectItems(new SelectExpressionItem(column).withAlias(new Alias(field))); + } return column; } else { Column column = new Column(cmdbCiIdTable, "`" + toAttrId + "`"); - plainSelect.addSelectItems(new SelectExpressionItem(column).withAlias(new Alias(field))); + if (selectItemFieldNameList.contains(field)) { + plainSelect.addSelectItems(new SelectExpressionItem(column).withAlias(new Alias(field))); + } return column; } } else { @@ -324,7 +347,9 @@ public class ResourceViewGenerateSqlUtil { toAttr = toAttr.substring(1); } Column column = new Column(attrCiTable, toAttr); - plainSelect.addSelectItems(new SelectExpressionItem(column).withAlias(new Alias(field))); + if (selectItemFieldNameList.contains(field)) { + plainSelect.addSelectItems(new SelectExpressionItem(column).withAlias(new Alias(field))); + } return column; } else { String tableName = "cmdb_" + toAttrCiId; @@ -340,7 +365,9 @@ public class ResourceViewGenerateSqlUtil { addEqualColumn(cmdbCiIdTableCientityIdColumn, table3IdColumn); } Column column = new Column(cmdbCiIdTable, "`" + toAttrId + "`"); - plainSelect.addSelectItems(new SelectExpressionItem(column).withAlias(new Alias(field))); + if (selectItemFieldNameList.contains(field)) { + plainSelect.addSelectItems(new SelectExpressionItem(column).withAlias(new Alias(field))); + } return column; } } @@ -359,7 +386,9 @@ public class ResourceViewGenerateSqlUtil { addEqualColumn(cmdbCiIdTableCientityIdColumn, attrCiTableIdColumn); } Column column = new Column(cmdbCiIdTable, "`" + fromAttrId + "`"); - plainSelect.addSelectItems(new SelectExpressionItem(column).withAlias(new Alias(field))); + if (selectItemFieldNameList.contains(field)) { + plainSelect.addSelectItems(new SelectExpressionItem(column).withAlias(new Alias(field))); + } return column; } } else if (Objects.equals(type, "rel")) { @@ -404,7 +433,9 @@ public class ResourceViewGenerateSqlUtil { } } Column column = new Column(attrCiTable, fromAttr); - plainSelect.addSelectItems(new SelectExpressionItem(column).withAlias(new Alias(field))); + if (selectItemFieldNameList.contains(field)) { + plainSelect.addSelectItems(new SelectExpressionItem(column).withAlias(new Alias(field))); + } return column; } else { String tableName = "cmdb_" + fromAttrCiId; @@ -420,7 +451,9 @@ public class ResourceViewGenerateSqlUtil { addEqualColumn(cmdbCiIdTableCientityIdColumn, attrCiTableIdColumn); } Column column = new Column(cmdbCiIdTable, "`" + fromAttrId + "`"); - plainSelect.addSelectItems(new SelectExpressionItem(column).withAlias(new Alias(field))); + if (selectItemFieldNameList.contains(field)) { + plainSelect.addSelectItems(new SelectExpressionItem(column).withAlias(new Alias(field))); + } return column; } } else { @@ -463,7 +496,9 @@ public class ResourceViewGenerateSqlUtil { } } Column column = new Column(attrCiTable, toAttr); - plainSelect.addSelectItems(new SelectExpressionItem(column).withAlias(new Alias(field))); + if (selectItemFieldNameList.contains(field)) { + plainSelect.addSelectItems(new SelectExpressionItem(column).withAlias(new Alias(field))); + } return column; } else { String tableName = "cmdb_" + toAttrCiId; @@ -479,7 +514,9 @@ public class ResourceViewGenerateSqlUtil { addEqualColumn(cmdbCiIdTableCientityIdColumn, attrCiTableIdColumn); } Column column = new Column(cmdbCiIdTable, "`" + toAttrId + "`"); - plainSelect.addSelectItems(new SelectExpressionItem(column).withAlias(new Alias(field))); + if (selectItemFieldNameList.contains(field)) { + plainSelect.addSelectItems(new SelectExpressionItem(column).withAlias(new Alias(field))); + } return column; } } @@ -535,10 +572,14 @@ public class ResourceViewGenerateSqlUtil { addEqualColumn(globalAttritemTableAttrIdColumn, cmdbCientityGlobalattritemTableAttrIdColumn); } Column column = new Column(globalAttritemTable, toAttr); - plainSelect.addSelectItems(new SelectExpressionItem(column).withAlias(new Alias(field))); + if (selectItemFieldNameList.contains(field)) { + plainSelect.addSelectItems(new SelectExpressionItem(column).withAlias(new Alias(field))); + } return column; } else if (Objects.equals(type, "empty")) { - plainSelect.addSelectItems(new SelectExpressionItem(new NullValue()).withAlias(new Alias(field))); + if (selectItemFieldNameList.contains(field)) { + plainSelect.addSelectItems(new SelectExpressionItem(new NullValue()).withAlias(new Alias(field))); + } return null; } else { //非下拉框属性 @@ -574,7 +615,9 @@ public class ResourceViewGenerateSqlUtil { fromAttr = "label"; } Column column = new Column(new Table("cientity_" + fromCi), fromAttr); - plainSelect.addSelectItems(new SelectExpressionItem(column).withAlias(new Alias(field))); + if (selectItemFieldNameList.contains(field)) { + plainSelect.addSelectItems(new SelectExpressionItem(column).withAlias(new Alias(field))); + } return column; } } diff --git a/src/main/java/neatlogic/framework/cmdb/utils/ResourceViewGenerateSqlUtilForTiDB.java b/src/main/java/neatlogic/framework/cmdb/utils/ResourceViewGenerateSqlUtilForTiDB.java index 3c2a60ee23c890f80eef74eeccc8cd10dc1f8356..92485d016524ffb3eca76c9ba8d5956a07c8a455 100644 --- a/src/main/java/neatlogic/framework/cmdb/utils/ResourceViewGenerateSqlUtilForTiDB.java +++ b/src/main/java/neatlogic/framework/cmdb/utils/ResourceViewGenerateSqlUtilForTiDB.java @@ -43,27 +43,45 @@ public class ResourceViewGenerateSqlUtilForTiDB { private CiVo mainCiVo; private List fieldMappingList; private List leftJoinList; + + List selectItemFieldNameList; + + List filterItemFieldNameList; //sql语句中已经存在的表 private Map joinedTableMap; private Map joinedSubSelectMap; //sql语句关联表中相等的列 private Map equalColumnMap; + private Map filterItemFieldName2ColumnMap; + public ResourceViewGenerateSqlUtilForTiDB(ResourceEntityConfigVo config) { this.mainCiVo = config.getMainCiVo(); this.fieldMappingList = config.getFieldMappingList(); this.leftJoinList = config.getLeftJoinList(); + this.selectItemFieldNameList = config.getSelectItemFieldNameList(); + this.filterItemFieldNameList = config.getFilterItemFieldNameList(); } - public String getSql() { + public PlainSelect getSql() { PlainSelect plainSelect = initPlainSelectByMainResourceId(mainCiVo); for (ResourceEntityLeftJoinVo leftJoinVo : leftJoinList) { addJoinTable(leftJoinVo, plainSelect); } for (ResourceEntityFieldMappingVo fieldMappingVo : fieldMappingList) { - addJoinTableByFieldMapping(fieldMappingVo, plainSelect, mainCiVo); + String field = fieldMappingVo.getField(); + if (selectItemFieldNameList.contains(field) || filterItemFieldNameList.contains(field)) { + Column column = addJoinTableByFieldMapping(fieldMappingVo, plainSelect, mainCiVo); + if (filterItemFieldNameList.contains(field)) { + filterItemFieldName2ColumnMap.put(field, column); + } + } } - return plainSelect.toString(); + return plainSelect; + } + + public Map getFilterItemFieldName2ColumnMap() { + return this.filterItemFieldName2ColumnMap; } private void addJoinTable(Table table) { @@ -118,6 +136,7 @@ public class ResourceViewGenerateSqlUtilForTiDB { joinedTableMap = new HashMap<>(); equalColumnMap = new HashMap<>(); joinedSubSelectMap = new HashMap<>(); + filterItemFieldName2ColumnMap = new HashMap<>(); String tableName = "cmdb_" + mainCiVo.getId(); String tableAlias = tableName + "_" + mainCiVo.getName(); Table cmdbCiIdTable = new Table(TenantContext.get().getDataDbName(), tableName).withAlias(new Alias(tableAlias).withUseAs(false)); @@ -298,11 +317,15 @@ public class ResourceViewGenerateSqlUtilForTiDB { toAttr = toAttr.substring(1); } Column column = new Column(cmdbCiIdTable, toAttr); - plainSelect.addSelectItems(new SelectExpressionItem(column).withAlias(new Alias(field))); + if (selectItemFieldNameList.contains(field)) { + plainSelect.addSelectItems(new SelectExpressionItem(column).withAlias(new Alias(field))); + } return column; } else { Column column = new Column(cmdbCiIdTable, "`" + toAttrId + "`"); - plainSelect.addSelectItems(new SelectExpressionItem(column).withAlias(new Alias(field))); + if (selectItemFieldNameList.contains(field)) { + plainSelect.addSelectItems(new SelectExpressionItem(column).withAlias(new Alias(field))); + } return column; } } else { @@ -323,7 +346,9 @@ public class ResourceViewGenerateSqlUtilForTiDB { toAttr = toAttr.substring(1); } Column column = new Column(attrCiTable.getAlias().getName() + "." + toAttr); - plainSelect.addSelectItems(new SelectExpressionItem(column).withAlias(new Alias(field))); + if (selectItemFieldNameList.contains(field)) { + plainSelect.addSelectItems(new SelectExpressionItem(column).withAlias(new Alias(field))); + } return column; } else { String tableName = "cmdb_" + toAttrCiId; @@ -339,7 +364,9 @@ public class ResourceViewGenerateSqlUtilForTiDB { addEqualColumn(cmdbCiIdTableCientityIdColumn, table3IdColumn); } Column column = new Column(cmdbCiIdTable, "`" + toAttrId + "`"); - plainSelect.addSelectItems(new SelectExpressionItem(column).withAlias(new Alias(field))); + if (selectItemFieldNameList.contains(field)) { + plainSelect.addSelectItems(new SelectExpressionItem(column).withAlias(new Alias(field))); + } return column; } } @@ -358,7 +385,9 @@ public class ResourceViewGenerateSqlUtilForTiDB { addEqualColumn(cmdbCiIdTableCientityIdColumn, attrCiTableIdColumn); } Column column = new Column(cmdbCiIdTable, "`" + fromAttrId + "`"); - plainSelect.addSelectItems(new SelectExpressionItem(column).withAlias(new Alias(field))); + if (selectItemFieldNameList.contains(field)) { + plainSelect.addSelectItems(new SelectExpressionItem(column).withAlias(new Alias(field))); + } return column; } } else if (Objects.equals(type, "rel")) { @@ -405,7 +434,9 @@ public class ResourceViewGenerateSqlUtilForTiDB { } } Column column = new Column(attrCiTable.getAlias().getName() + "." + fromAttr); - plainSelect.addSelectItems(new SelectExpressionItem(column).withAlias(new Alias(field))); + if (selectItemFieldNameList.contains(field)) { + plainSelect.addSelectItems(new SelectExpressionItem(column).withAlias(new Alias(field))); + } return column; } else { String tableName = "cmdb_" + fromAttrCiId; @@ -421,7 +452,9 @@ public class ResourceViewGenerateSqlUtilForTiDB { addEqualColumn(cmdbCiIdTableCientityIdColumn, attrCiTableIdColumn); } Column column = new Column(cmdbCiIdTable, "`" + fromAttrId + "`"); - plainSelect.addSelectItems(new SelectExpressionItem(column).withAlias(new Alias(field))); + if (selectItemFieldNameList.contains(field)) { + plainSelect.addSelectItems(new SelectExpressionItem(column).withAlias(new Alias(field))); + } return column; } } else { @@ -467,7 +500,9 @@ public class ResourceViewGenerateSqlUtilForTiDB { } } Column column = new Column(attrCiTable.getAlias().getName() + "." + toAttr); - plainSelect.addSelectItems(new SelectExpressionItem(column).withAlias(new Alias(field))); + if (selectItemFieldNameList.contains(field)) { + plainSelect.addSelectItems(new SelectExpressionItem(column).withAlias(new Alias(field))); + } return column; } else { String tableName = "cmdb_" + toAttrCiId; @@ -483,7 +518,9 @@ public class ResourceViewGenerateSqlUtilForTiDB { addEqualColumn(cmdbCiIdTableCientityIdColumn, attrCiTableIdColumn); } Column column = new Column(cmdbCiIdTable, "`" + toAttrId + "`"); - plainSelect.addSelectItems(new SelectExpressionItem(column).withAlias(new Alias(field))); + if (selectItemFieldNameList.contains(field)) { + plainSelect.addSelectItems(new SelectExpressionItem(column).withAlias(new Alias(field))); + } return column; } } @@ -538,10 +575,14 @@ public class ResourceViewGenerateSqlUtilForTiDB { addEqualColumn(globalAttritemTableAttrIdColumn, cmdbCientityGlobalattritemTableAttrIdColumn); } Column column = new Column(globalAttritemTable, toAttr); - plainSelect.addSelectItems(new SelectExpressionItem(column).withAlias(new Alias(field))); + if (selectItemFieldNameList.contains(field)) { + plainSelect.addSelectItems(new SelectExpressionItem(column).withAlias(new Alias(field))); + } return column; } else if (Objects.equals(type, "empty")) { - plainSelect.addSelectItems(new SelectExpressionItem(new NullValue()).withAlias(new Alias(field))); + if (selectItemFieldNameList.contains(field)) { + plainSelect.addSelectItems(new SelectExpressionItem(new NullValue()).withAlias(new Alias(field))); + } return null; } else { //非下拉框属性 @@ -577,7 +618,9 @@ public class ResourceViewGenerateSqlUtilForTiDB { fromAttr = "label"; } Column column = new Column(new Table("cientity_" + fromCi), fromAttr); - plainSelect.addSelectItems(new SelectExpressionItem(column).withAlias(new Alias(field))); + if (selectItemFieldNameList.contains(field)) { + plainSelect.addSelectItems(new SelectExpressionItem(column).withAlias(new Alias(field))); + } return column; } }