diff --git a/src/main/java/neatlogic/module/cmdb/dao/mapper/resourcecenter/ResourceMapper.java b/src/main/java/neatlogic/module/cmdb/dao/mapper/resourcecenter/ResourceMapper.java index 66a0f87843e74f7193201a54a88e86ad07d4f21e..47e12b72992b3e7aed05e805dee2363def69236d 100644 --- a/src/main/java/neatlogic/module/cmdb/dao/mapper/resourcecenter/ResourceMapper.java +++ b/src/main/java/neatlogic/module/cmdb/dao/mapper/resourcecenter/ResourceMapper.java @@ -33,7 +33,7 @@ public interface ResourceMapper extends IResourceCrossoverMapper { int getResourceCountByNameKeyword(ResourceSearchVo searchVo); int getResourceCountByIpKeyword(ResourceSearchVo searchVo); - + @Deprecated int getResourceCount(ResourceSearchVo searchVo); int getResourceCountBySql(String sql); @@ -42,12 +42,13 @@ public interface ResourceMapper extends IResourceCrossoverMapper { int getResourceCountByDynamicCondition(@Param("searchVo") ResourceSearchVo searchVo, @Param("conditionSql") String conditionSql); + @Deprecated List getResourceIdList(ResourceSearchVo searchVo); List getResourceIdListBySql(String sql); List getResourceIdListByDynamicCondition(@Param("searchVo") ResourceSearchVo searchVo, @Param("conditionSql") String conditionSql); - + @Deprecated List getResourceListByIdList(List idList); List getResourceListBySql(String sql); diff --git a/src/main/java/neatlogic/module/cmdb/dao/mapper/resourcecenter/ResourceMapper.xml b/src/main/java/neatlogic/module/cmdb/dao/mapper/resourcecenter/ResourceMapper.xml index a40ca5661268f847f4553a9555dbb6a21fe479af..0b1f7dbcfb5e2963a5ebcc9b90a7682be0c7f05a 100644 --- a/src/main/java/neatlogic/module/cmdb/dao/mapper/resourcecenter/ResourceMapper.xml +++ b/src/main/java/neatlogic/module/cmdb/dao/mapper/resourcecenter/ResourceMapper.xml @@ -238,7 +238,7 @@ along with this program. If not, see .--> - + - + SELECT a.`id`, diff --git a/src/main/java/neatlogic/module/cmdb/service/resourcecenter/resource/ResourceCenterResourceServiceImpl.java b/src/main/java/neatlogic/module/cmdb/service/resourcecenter/resource/ResourceCenterResourceServiceImpl.java index 70c716b88191966cb2bf4a7f04f67bf729f32aa7..4afa71afc375752ca60685197789befa8d21a670 100644 --- a/src/main/java/neatlogic/module/cmdb/service/resourcecenter/resource/ResourceCenterResourceServiceImpl.java +++ b/src/main/java/neatlogic/module/cmdb/service/resourcecenter/resource/ResourceCenterResourceServiceImpl.java @@ -39,6 +39,9 @@ import neatlogic.framework.dao.mapper.DataBaseViewInfoMapper; import neatlogic.framework.dao.mapper.SchemaMapper; import neatlogic.framework.dto.DataBaseViewInfoVo; import neatlogic.framework.fulltextindex.utils.FullTextIndexUtil; +import neatlogic.framework.sqlgenerator.$sql; +import neatlogic.framework.sqlgenerator.ExpressionVo; +import neatlogic.framework.sqlgenerator.JoinVo; import neatlogic.framework.store.mysql.DatabaseVendor; import neatlogic.framework.store.mysql.DatasourceManager; import neatlogic.framework.transaction.core.EscapeTransactionJob; @@ -705,24 +708,20 @@ public class ResourceCenterResourceServiceImpl implements IResourceCenterResourc plainSelect = resourceViewGenerateSqlUtil.getSql(); filterItemFieldName2ColumnMap = resourceViewGenerateSqlUtil.getFilterItemFieldName2ColumnMap(); } - plainSelect = supplementBusinessLogicByResourceSearchVo(queryCriteriaVo, plainSelect, filterItemFieldName2ColumnMap); - Column idColumn = filterItemFieldName2ColumnMap.get("id"); + + List joinList = getJoinList(queryCriteriaVo, filterItemFieldName2ColumnMap); + $sql.addJoinList(plainSelect, joinList); + List whereExpressionList = getWhereExpressionList(queryCriteriaVo, filterItemFieldName2ColumnMap); + $sql.addWhereExpressionList(plainSelect, whereExpressionList); if (CollectionUtils.isNotEmpty(searchVo.getKeywordList()) && searchVo.getNameFieldAttrId() != null && searchVo.getIpFieldAttrId() != null) { - Function countFunction = new Function(); - countFunction.withName("COUNT"); - countFunction.withDistinct(true); - countFunction.setParameters(new ExpressionList().addExpressions(new Column("fw.word"))); - // 排序 - OrderByElement orderByMatchCount = new OrderByElement().withExpression(countFunction).withAsc(false); - plainSelect.addOrderByElements(orderByMatchCount); + $sql.addOrderBy(plainSelect, $sql.fun("COUNT", "fw.word").withDistinct(true), "desc"); } + Column idColumn = filterItemFieldName2ColumnMap.get("id"); // 分组 - plainSelect.addGroupByColumnReference(idColumn); + $sql.addGroupBy(plainSelect, idColumn.toString()); // 排序 - OrderByElement orderById = new OrderByElement().withExpression(idColumn).withAsc(false); - plainSelect.addOrderByElements(orderById); - - plainSelect.setLimit(new Limit().withOffset(new LongValue(searchVo.getStartNum())).withRowCount(new LongValue(searchVo.getPageSize()))); + $sql.addOrderBy(plainSelect, idColumn.toString(), "desc"); + $sql.setLimit(plainSelect, searchVo.getStartNum(), searchVo.getPageSize()); return plainSelect.toString(); } catch (Exception e) { logger.error(e.getMessage(), e); @@ -753,14 +752,13 @@ public class ResourceCenterResourceServiceImpl implements IResourceCenterResourc plainSelect = resourceViewGenerateSqlUtil.getSql(); filterItemFieldName2ColumnMap = resourceViewGenerateSqlUtil.getFilterItemFieldName2ColumnMap(); } - plainSelect = supplementBusinessLogicByResourceSearchVo(queryCriteriaVo, plainSelect, filterItemFieldName2ColumnMap); + + List joinList = getJoinList(queryCriteriaVo, filterItemFieldName2ColumnMap); + $sql.addJoinList(plainSelect, joinList); + List whereExpressionList = getWhereExpressionList(queryCriteriaVo, filterItemFieldName2ColumnMap); + $sql.addWhereExpressionList(plainSelect, whereExpressionList); Column column = filterItemFieldName2ColumnMap.get("id"); - Function countFunction = new Function(); - countFunction.withName("COUNT"); - countFunction.withDistinct(true); - countFunction.setParameters(new ExpressionList().addExpressions(column)); - SelectExpressionItem countDistinctItem = new SelectExpressionItem(countFunction); - plainSelect.setSelectItems(Collections.singletonList(countDistinctItem)); + $sql.setSelectColumn(plainSelect, $sql.fun("COUNT", column.toString()).withDistinct(true)); return plainSelect.toString(); } catch (Exception e) { logger.error(e.getMessage(), e); @@ -795,17 +793,7 @@ public class ResourceCenterResourceServiceImpl implements IResourceCenterResourc filterItemFieldName2ColumnMap = resourceViewGenerateSqlUtil.getFilterItemFieldName2ColumnMap(); } Column column = filterItemFieldName2ColumnMap.get("id"); - ExpressionList values = new ExpressionList(); - for (Long id : idList) { - values.addExpressions(new LongValue(id)); - } - InExpression inExpression = new InExpression(column, values); - Expression where = plainSelect.getWhere(); - if (where != null) { - plainSelect.setWhere(new AndExpression(where, inExpression)); - } else { - plainSelect.setWhere(inExpression); - } + $sql.addWhereExpression(plainSelect, $sql.exp(column.toString(), "in", idList)); return plainSelect.toString(); } catch (Exception e) { logger.error(e.getMessage(), e); @@ -1752,7 +1740,7 @@ public class ResourceCenterResourceServiceImpl implements IResourceCenterResourc Column column = filterItemFieldName2ColumnMap.get("env_id"); IsNullExpression isNullExpression = new IsNullExpression(); - isNullExpression.withLeftExpression(column).isUseIsNull(); + isNullExpression.withLeftExpression(column); Expression where = plainSelect.getWhere(); if (where != null) { plainSelect.setWhere(new AndExpression(where, isNullExpression)); @@ -1874,4 +1862,361 @@ public class ResourceCenterResourceServiceImpl implements IResourceCenterResourc } return plainSelect; } + + private List getJoinList(ResourceQueryCriteriaVo queryCriteriaVo, Map filterItemFieldName2ColumnMap) { + List joinList = new ArrayList<>(); + /* + + JOIN fulltextindex_field_cmdb ffc ON ffc.target_id = a.id AND ffc.target_field IN (#{nameFieldAttrId}, #{ipFieldAttrId}) + JOIN fulltextindex_word fw ON ffc.word_id = fw.id + AND (fw.word IN + + #{item} + + ) + + */ + if (CollectionUtils.isNotEmpty(queryCriteriaVo.getKeywordList()) && (queryCriteriaVo.getNameFieldAttrId() != null || queryCriteriaVo.getIpFieldAttrId() != null)) { + { + ExpressionVo expressionVo = $sql.exp( + $sql.exp("ffc.target_id", "=", filterItemFieldName2ColumnMap.get("id").toString()), + "and", + $sql.exp("ffc.target_field", "in", Arrays.asList(queryCriteriaVo.getNameFieldAttrId(), queryCriteriaVo.getIpFieldAttrId()))); + joinList.add($sql.join("join", "fulltextindex_field_cmdb", "ffc").withOn(expressionVo)); + } + { + ExpressionVo expressionVo = $sql.exp( + $sql.exp("fw.id", "=", "ffc.word_id"), + "and", + $sql.exp("fw.word", "in", queryCriteriaVo.getKeywordList())); + joinList.add($sql.join("join", "fulltextindex_word", "fw").withOn(expressionVo)); + } + } + /* + + JOIN fulltextindex_field_cmdb ffc2 ON ffc2.target_id = a.id + + + AND ffc2.target_field = #{nameFieldAttrId} + + + AND ffc2.target_field = #{ipFieldAttrId} + + + JOIN fulltextindex_word fw2 ON ffc2.word_id = fw2.id + AND (fw2.word IN + + #{item} + + ) + + */ + if (CollectionUtils.isNotEmpty(queryCriteriaVo.getBatchSearchList()) && StringUtils.isNotBlank(queryCriteriaVo.getSearchField())) { + { + Long fieldAttrId = null; + if (Objects.equals(queryCriteriaVo.getSearchField(), "name")) { + fieldAttrId = queryCriteriaVo.getNameFieldAttrId(); + } else { + fieldAttrId = queryCriteriaVo.getIpFieldAttrId(); + } + ExpressionVo expressionVo = $sql.exp( + $sql.exp("ffc2.target_id", "=", filterItemFieldName2ColumnMap.get("id").toString()), + "and", + $sql.exp("ffc2.target_field", "=", fieldAttrId)); + joinList.add($sql.join("join", "fulltextindex_field_cmdb", "ffc2").withOn(expressionVo)); + } + { + ExpressionVo expressionVo = $sql.exp( + $sql.exp("fw2.id", "=", "ffc2.word_id"), + "and", + $sql.exp("fw2.word", "in", queryCriteriaVo.getBatchSearchList())); + joinList.add($sql.join("join", "fulltextindex_word", "fw2").withOn(expressionVo)); + } + } + /* + + LEFT JOIN `cmdb_resourcecenter_resource_account` b ON b.`resource_id` = a.`id` + LEFT JOIN `cmdb_resourcecenter_account` c ON c.`id` = b.`account_id` + + */ + if (CollectionUtils.isNotEmpty(queryCriteriaVo.getProtocolIdList())) { + joinList.add($sql.join("left join", "cmdb_resourcecenter_resource_account", "b").withOn($sql.exp("b.resource_id", "=", filterItemFieldName2ColumnMap.get("id").toString()))); + joinList.add($sql.join("left join", "cmdb_resourcecenter_account", "c").withOn($sql.exp("c.id", "=", "b.account_id"))); + } + /* + + LEFT JOIN `cmdb_resourcecenter_resource_tag` d ON d.`resource_id` = a.`id` + + */ + if (CollectionUtils.isNotEmpty(queryCriteriaVo.getTagIdList())) { + joinList.add($sql.join("left join", "cmdb_resourcecenter_resource_tag", "d").withOn($sql.exp("d.resource_id", "=", filterItemFieldName2ColumnMap.get("id").toString()))); + } + /* + + left join autoexec_job_resource_inspect ajri on ajri.resource_id=a.id + left join autoexec_job_phase_node ajpn on ajpn.job_phase_id =ajri.phase_id AND ajpn.resource_id = a.id + + */ + if (CollectionUtils.isNotEmpty(queryCriteriaVo.getInspectJobPhaseNodeStatusList())) { + joinList.add($sql.join("left join", "autoexec_job_resource_inspect", "ajri").withOn($sql.exp("ajri.resource_id", "=", filterItemFieldName2ColumnMap.get("id").toString()))); + ExpressionVo expressionVo = $sql.exp($sql.exp("ajpn.job_phase_id", "=", "ajri.phase_id"), "and", $sql.exp("ajpn.resource_id", "=", filterItemFieldName2ColumnMap.get("id").toString())); + joinList.add($sql.join("left join", "autoexec_job_phase_node", "ajpn").withOn(expressionVo)); + } + /* + + LEFT JOIN cmdb_cientity_group ccg ON ccg.cientity_id = a.id + LEFT JOIN cmdb_group_auth cga ON ccg.group_id = cga.group_id + + + LEFT JOIN cmdb_group cg ON cga.group_id = cg.id AND cg.type in ('autoexec') + + + LEFT JOIN cmdb_group cg ON cga.group_id = cg.id AND cg.type in ('readonly','maintain','autoexec') + + + + */ + if (Objects.equals(queryCriteriaVo.getIsHasAuth(), false)) { + joinList.add($sql.join("left join", "cmdb_cientity_group", "ccg").withOn($sql.exp("ccg.cientity_id", "=", filterItemFieldName2ColumnMap.get("id").toString()))); + joinList.add($sql.join("left join", "cmdb_group_auth", "cga").withOn($sql.exp("cga.group_id", "=", "ccg.group_id"))); + + List strList = new ArrayList<>(); + if (Objects.equals(queryCriteriaVo.getCmdbGroupType(), "autoexec")) { + strList.add("autoexec"); + } else { + strList.add("autoexec"); + strList.add("readonly"); + strList.add("maintain"); + } + ExpressionVo expressionVo = $sql.exp( + $sql.exp("cg.id", "=", "cga.group_id"), + "and", + $sql.exp("cg.type", "in", strList) + ); + joinList.add($sql.join("left join", "cmdb_group", "cg").withOn(expressionVo)); + } + return joinList; + } + + private List getWhereExpressionList(ResourceQueryCriteriaVo queryCriteriaVo, Map filterItemFieldName2ColumnMap) { + List whereExpressionList = new ArrayList<>(); + /* + + AND c.`protocol_id` IN + + #{protocolId} + + + */ + if (CollectionUtils.isNotEmpty(queryCriteriaVo.getProtocolIdList())) { + whereExpressionList.add($sql.exp("c.protocol_id", "in", queryCriteriaVo.getProtocolIdList())); + } + /* + + AND d.`tag_id` IN + + #{tagId} + + + */ + if (CollectionUtils.isNotEmpty(queryCriteriaVo.getTagIdList())) { + whereExpressionList.add($sql.exp("d.tag_id", "in", queryCriteriaVo.getTagIdList())); + } + /* + + AND a.`inspect_status` IN + + #{inspectStatus} + + + */ + if (CollectionUtils.isNotEmpty(queryCriteriaVo.getInspectJobPhaseNodeStatusList())) { + whereExpressionList.add($sql.exp("ajpn.status", "in", queryCriteriaVo.getInspectJobPhaseNodeStatusList())); + } + /* + + + AND a.`type_id` IN + + #{typeId} + + + + AND ( + + + a.`type_id` IN + + #{authedTypeId} + + + + 1 = 0 + + + or ( + cg.id is not null and + a.`type_id` IN + + #{typeId} + + and + ((cga.auth_type = 'common' AND cga.auth_uuid = 'alluser') + + OR cga.auth_uuid IN ( + #{authenticationInfo.userUuid} + + + #{item} + + + + + #{item} + + + ) + + ) + ) + ) + + + */ + if (CollectionUtils.isNotEmpty(queryCriteriaVo.getTypeIdList())) { + if (Objects.equals(queryCriteriaVo.getIsHasAuth(), true)) { + whereExpressionList.add($sql.exp(filterItemFieldName2ColumnMap.get("type_id").toString(), "in", queryCriteriaVo.getTypeIdList())); + } else if (Objects.equals(queryCriteriaVo.getIsHasAuth(), false)) { + ExpressionVo orLeftExpressionVo = null; + if (CollectionUtils.isNotEmpty(queryCriteriaVo.getAuthedTypeIdList())) { + orLeftExpressionVo = $sql.exp(filterItemFieldName2ColumnMap.get("type_id").toString(), "in", queryCriteriaVo.getAuthedTypeIdList()); + } else { + orLeftExpressionVo = $sql.exp(1, "=", 0); + } + ExpressionVo orRightExpressionVo = $sql.exp($sql.exp("cg.id", "is not null"), "and", $sql.exp(filterItemFieldName2ColumnMap.get("type_id").toString(), "in", queryCriteriaVo.getTypeIdList())); + + ExpressionVo orLeftExpressionVo2 = $sql.exp("(", $sql.exp("cga.auth_type", "=", "'common'"), "and", $sql.exp("cga.auth_uuid", "=", "'alluser'"), ")"); + ExpressionVo orRightExpressionVo2 = null; + if (queryCriteriaVo.getAuthenticationInfo() != null) { + List uuidList = new ArrayList<>(); + if (StringUtils.isNotBlank(queryCriteriaVo.getAuthenticationInfo().getUserUuid())) { + uuidList.add(queryCriteriaVo.getAuthenticationInfo().getUserUuid()); + } + if (CollectionUtils.isNotEmpty(queryCriteriaVo.getAuthenticationInfo().getTeamUuidList())) { + uuidList.addAll(queryCriteriaVo.getAuthenticationInfo().getTeamUuidList()); + } + if (CollectionUtils.isNotEmpty(queryCriteriaVo.getAuthenticationInfo().getRoleUuidList())) { + uuidList.addAll(queryCriteriaVo.getAuthenticationInfo().getRoleUuidList()); + } + if (CollectionUtils.isNotEmpty(uuidList)) { + orRightExpressionVo2 = $sql.exp("cga.auth_uuid", "in", uuidList); + } + } + if (orRightExpressionVo2 != null) { + orRightExpressionVo = $sql.exp(orRightExpressionVo, "and", $sql.exp("(", orLeftExpressionVo2, "or", orRightExpressionVo2, ")")); + } else { + orRightExpressionVo = $sql.exp(orRightExpressionVo, "and", orLeftExpressionVo2); + } + ExpressionVo orExpressionVo = $sql.exp("(", orLeftExpressionVo, "or", orRightExpressionVo, ")"); + whereExpressionList.add(orExpressionVo); + } + } + /* + + AND a.`state_id` IN + + #{stateId} + + + */ + if (CollectionUtils.isNotEmpty(queryCriteriaVo.getStateIdList())) { + whereExpressionList.add($sql.exp(filterItemFieldName2ColumnMap.get("state_id").toString(), "in", queryCriteriaVo.getStateIdList())); + } + /* + + AND a.`vendor_id` IN + + #{vendorId} + + + */ + if (CollectionUtils.isNotEmpty(queryCriteriaVo.getVendorIdList())) { + whereExpressionList.add($sql.exp(filterItemFieldName2ColumnMap.get("vendor_id").toString(), "in", queryCriteriaVo.getVendorIdList())); + } + /* + + AND a.`env_id` IN + + #{envId} + + + */ + if (CollectionUtils.isNotEmpty(queryCriteriaVo.getEnvIdList())) { + whereExpressionList.add($sql.exp(filterItemFieldName2ColumnMap.get("env_id").toString(), "in", queryCriteriaVo.getEnvIdList())); + } + /* + + AND a.`env_id` is null + + */ + if (Objects.equals(queryCriteriaVo.getExistNoEnv(), true)) { + whereExpressionList.add($sql.exp(filterItemFieldName2ColumnMap.get("env_id").toString(), "is null")); + } + /* + + AND a.`app_system_id` IN + + #{appSystemId} + + + */ + if (CollectionUtils.isNotEmpty(queryCriteriaVo.getAppSystemIdList())) { + whereExpressionList.add($sql.exp(filterItemFieldName2ColumnMap.get("app_system_id").toString(), "in", queryCriteriaVo.getAppSystemIdList())); + } + /* + + AND a.`app_module_id` IN + + #{appModuleId} + + + */ + if (CollectionUtils.isNotEmpty(queryCriteriaVo.getAppModuleIdList())) { + whereExpressionList.add($sql.exp(filterItemFieldName2ColumnMap.get("app_module_id").toString(), "in", queryCriteriaVo.getAppModuleIdList())); + } + /* + + AND a.`id` IN + + #{id} + + + */ + if (CollectionUtils.isNotEmpty(queryCriteriaVo.getDefaultValue())) { + whereExpressionList.add($sql.exp(filterItemFieldName2ColumnMap.get("id").toString(), "in", queryCriteriaVo.getDefaultValue())); + } + /* + + AND a.`id` IN + + #{id} + + + */ + if (CollectionUtils.isNotEmpty(queryCriteriaVo.getIdList())) { + whereExpressionList.add($sql.exp(filterItemFieldName2ColumnMap.get("id").toString(), "in", queryCriteriaVo.getIdList())); + } + /* + + AND a.`inspect_status` IN + + #{inspectStatus} + + + */ + if (CollectionUtils.isNotEmpty(queryCriteriaVo.getInspectStatusList())) { + whereExpressionList.add($sql.exp(filterItemFieldName2ColumnMap.get("inspect_status").toString(), "in", queryCriteriaVo.getInspectStatusList())); + } + return whereExpressionList; + } }