diff --git a/src/main/java/neatlogic/module/cmdb/api/resourcecenter/appmodule/AppModuleResourceListApi.java b/src/main/java/neatlogic/module/cmdb/api/resourcecenter/app/AppResourceListApi.java similarity index 60% rename from src/main/java/neatlogic/module/cmdb/api/resourcecenter/appmodule/AppModuleResourceListApi.java rename to src/main/java/neatlogic/module/cmdb/api/resourcecenter/app/AppResourceListApi.java index 29243743478c30b99a97c3c5b95d6e31da12609a..68263d314efb670f5814b48c6326348056b1a7c1 100644 --- a/src/main/java/neatlogic/module/cmdb/api/resourcecenter/appmodule/AppModuleResourceListApi.java +++ b/src/main/java/neatlogic/module/cmdb/api/resourcecenter/app/AppResourceListApi.java @@ -13,21 +13,23 @@ 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.cmdb.api.resourcecenter.appmodule; +package neatlogic.module.cmdb.api.resourcecenter.app; +import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import neatlogic.framework.auth.core.AuthAction; import neatlogic.framework.cmdb.auth.label.CMDB_BASE; -import neatlogic.framework.cmdb.dto.resourcecenter.ResourceSearchVo; +import neatlogic.framework.cmdb.resourcecenter.datasource.core.IResourceCenterDataSource; +import neatlogic.framework.cmdb.resourcecenter.datasource.core.ResourceCenterDataSourceFactory; import neatlogic.framework.common.constvalue.ApiParamType; import neatlogic.framework.exception.type.ParamNotExistsException; import neatlogic.framework.restful.annotation.*; import neatlogic.framework.restful.constvalue.OperationTypeEnum; import neatlogic.framework.restful.core.privateapi.PrivateApiComponentBase; -import neatlogic.module.cmdb.service.resourcecenter.resource.IResourceCenterResourceService; import org.springframework.stereotype.Service; -import javax.annotation.Resource; +import java.util.ArrayList; +import java.util.List; /** * @author linbq @@ -36,19 +38,16 @@ import javax.annotation.Resource; @Service @AuthAction(action = CMDB_BASE.class) @OperationType(type = OperationTypeEnum.SEARCH) -public class AppModuleResourceListApi extends PrivateApiComponentBase { - - @Resource - private IResourceCenterResourceService resourceCenterResourceService; +public class AppResourceListApi extends PrivateApiComponentBase { @Override public String getToken() { - return "resourcecenter/appmodule/resource/list"; + return "resourcecenter/app/resource/list"; } @Override public String getName() { - return "查询应用模块中资源列表"; + return "查询应用中资源列表"; } @Override @@ -60,10 +59,9 @@ public class AppModuleResourceListApi extends PrivateApiComponentBase { @Param(name = "appSystemId", type = ApiParamType.LONG, desc = "应用id"), @Param(name = "appModuleId", type = ApiParamType.LONG, desc = "应用模块id"), @Param(name = "envId", type = ApiParamType.LONG, desc = "环境id,envId=-2表示无配置环境"), - @Param(name = "typeId", type = ApiParamType.LONG, desc = "类型id"), - @Param(name = "currentPage", type = ApiParamType.INTEGER, desc = "当前页"), - @Param(name = "pageSize", type = ApiParamType.INTEGER, desc = "每页数据条目"), - @Param(name = "needPage", type = ApiParamType.BOOLEAN, desc = "是否需要分页,默认true") + @Param(name = "viewName", type = ApiParamType.STRING, desc = "视图名称"), + @Param(name = "currentPage", type = ApiParamType.INTEGER, defaultValue = "1", desc = "当前页"), + @Param(name = "pageSize", type = ApiParamType.INTEGER, defaultValue = "20", desc = "每页数据条目") }) @Output({ @Param(name = "tableList", type = ApiParamType.JSONARRAY, desc = "资源环境列表") @@ -72,11 +70,23 @@ public class AppModuleResourceListApi extends PrivateApiComponentBase { @Override public Object myDoService(JSONObject paramObj) throws Exception { JSONObject resultObj = new JSONObject(); - ResourceSearchVo searchVo = paramObj.toJavaObject(ResourceSearchVo.class); - if (searchVo.getAppSystemId() == null && searchVo.getAppModuleId() == null) { + Long appSystemId = paramObj.getLong("appSystemId"); + Long appModuleId = paramObj.getLong("appModuleId"); + if (appSystemId == null && appModuleId == null) { throw new ParamNotExistsException("应用id(appSystemId)", "应用模块id(appModuleId)"); } - resultObj.put("tableList", resourceCenterResourceService.getAppModuleResourceList(searchVo)); + Long envId = paramObj.getLong("envId"); + Integer currentPage = paramObj.getInteger("currentPage"); + Integer pageSize = paramObj.getInteger("pageSize"); + Long typeId = paramObj.getLong("typeId"); + List typeIdList = new ArrayList<>(); + if (typeId != null) { + typeIdList.add(typeId); + } + String viewName = paramObj.getString("viewName"); + IResourceCenterDataSource resourceCenterDataSource = ResourceCenterDataSourceFactory.getResourceCenterDataSource(); + JSONArray tableList = resourceCenterDataSource.getAppResourceList(appSystemId, appModuleId, envId, null, viewName, currentPage, pageSize); + resultObj.put("tableList", tableList); return resultObj; } } diff --git a/src/main/java/neatlogic/module/cmdb/api/resourcecenter/app/ListAppSystemForSelectApi.java b/src/main/java/neatlogic/module/cmdb/api/resourcecenter/app/ListAppSystemForSelectApi.java index d6d3b8f15df10e29a5bbca77cb40463c58ae4196..9ddf4eb25f974ba685ad05bc713e743b6d1330a6 100644 --- a/src/main/java/neatlogic/module/cmdb/api/resourcecenter/app/ListAppSystemForSelectApi.java +++ b/src/main/java/neatlogic/module/cmdb/api/resourcecenter/app/ListAppSystemForSelectApi.java @@ -15,23 +15,20 @@ along with this program. If not, see .*/ package neatlogic.module.cmdb.api.resourcecenter.app; -import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import neatlogic.framework.auth.core.AuthAction; import neatlogic.framework.cmdb.auth.label.CMDB; import neatlogic.framework.cmdb.dto.resourcecenter.ResourceVo; +import neatlogic.framework.cmdb.resourcecenter.datasource.core.IResourceCenterDataSource; +import neatlogic.framework.cmdb.resourcecenter.datasource.core.ResourceCenterDataSourceFactory; import neatlogic.framework.common.constvalue.ApiParamType; import neatlogic.framework.common.dto.BasePageVo; import neatlogic.framework.restful.annotation.*; import neatlogic.framework.restful.constvalue.OperationTypeEnum; import neatlogic.framework.restful.core.privateapi.PrivateApiComponentBase; import neatlogic.framework.util.TableResultUtil; -import neatlogic.module.cmdb.dao.mapper.resourcecenter.ResourceMapper; -import org.apache.commons.collections4.CollectionUtils; import org.springframework.stereotype.Service; -import javax.annotation.Resource; -import java.util.ArrayList; import java.util.List; @Service @@ -39,9 +36,6 @@ import java.util.List; @OperationType(type = OperationTypeEnum.SEARCH) public class ListAppSystemForSelectApi extends PrivateApiComponentBase { - @Resource - private ResourceMapper resourceMapper; - @Override public String getToken() { return "resourcecenter/appsystem/list/forselect"; @@ -71,32 +65,8 @@ public class ListAppSystemForSelectApi extends PrivateApiComponentBase { @Override public Object myDoService(JSONObject paramObj) throws Exception { BasePageVo searchVo = paramObj.toJavaObject(BasePageVo.class); - JSONArray defaultValue = searchVo.getDefaultValue(); - if (CollectionUtils.isNotEmpty(defaultValue)) { - List idList = defaultValue.toJavaList(Long.class); - List resourceList = resourceMapper.searchAppSystemListByIdList(idList); - return TableResultUtil.getResult(resourceList); - } else { - int rowNum = resourceMapper.searchAppSystemCount(searchVo); - if (rowNum > 0) { - searchVo.setRowNum(rowNum); - if (searchVo.getNeedPage()) { - List idList = resourceMapper.searchAppSystemIdList(searchVo); - List resourceList = resourceMapper.searchAppSystemListByIdList(idList); - return TableResultUtil.getResult(resourceList, searchVo); - } else { - List allResourceList = new ArrayList<>(); - int pageCount = searchVo.getPageCount(); - for (int currentPage = 1; currentPage <= pageCount; currentPage++) { - searchVo.setCurrentPage(currentPage); - List idList = resourceMapper.searchAppSystemIdList(searchVo); - List resourceList = resourceMapper.searchAppSystemListByIdList(idList); - allResourceList.addAll(resourceList); - } - return TableResultUtil.getResult(allResourceList, searchVo); - } - } - } - return null; + IResourceCenterDataSource resourceCenterDataSource = ResourceCenterDataSourceFactory.getResourceCenterDataSource(); + List tbodyList = resourceCenterDataSource.getAppSystemListForSelect(searchVo); + return TableResultUtil.getResult(tbodyList, searchVo); } } diff --git a/src/main/java/neatlogic/module/cmdb/api/resourcecenter/app/ListAppSystemForTreeApi.java b/src/main/java/neatlogic/module/cmdb/api/resourcecenter/app/ListAppSystemForTreeApi.java index dcc095bb03da6c80e55ce0a7f17d805fec395bc6..d572887299f1a50ec9dd2430aaba8621f017d6ba 100644 --- a/src/main/java/neatlogic/module/cmdb/api/resourcecenter/app/ListAppSystemForTreeApi.java +++ b/src/main/java/neatlogic/module/cmdb/api/resourcecenter/app/ListAppSystemForTreeApi.java @@ -18,33 +18,24 @@ package neatlogic.module.cmdb.api.resourcecenter.app; import com.alibaba.fastjson.JSONObject; import neatlogic.framework.auth.core.AuthAction; import neatlogic.framework.cmdb.auth.label.CMDB; -import neatlogic.framework.cmdb.dto.resourcecenter.AppModuleVo; import neatlogic.framework.cmdb.dto.resourcecenter.AppSystemVo; +import neatlogic.framework.cmdb.resourcecenter.datasource.core.IResourceCenterDataSource; +import neatlogic.framework.cmdb.resourcecenter.datasource.core.ResourceCenterDataSourceFactory; import neatlogic.framework.common.constvalue.ApiParamType; import neatlogic.framework.common.dto.BasePageVo; import neatlogic.framework.restful.annotation.*; import neatlogic.framework.restful.constvalue.OperationTypeEnum; import neatlogic.framework.restful.core.privateapi.PrivateApiComponentBase; import neatlogic.framework.util.TableResultUtil; -import neatlogic.module.cmdb.dao.mapper.resourcecenter.ResourceMapper; -import org.apache.commons.collections4.CollectionUtils; -import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; -import javax.annotation.Resource; -import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; @Service @AuthAction(action = CMDB.class) @OperationType(type = OperationTypeEnum.SEARCH) public class ListAppSystemForTreeApi extends PrivateApiComponentBase { - @Resource - private ResourceMapper resourceMapper; - @Override public String getToken() { return "resourcecenter/appsystem/list/fortree"; @@ -72,42 +63,9 @@ public class ListAppSystemForTreeApi extends PrivateApiComponentBase { @Description(desc = "查询资源应用列表") @Override public Object myDoService(JSONObject paramObj) throws Exception { - List tbodyList = new ArrayList<>(); BasePageVo searchVo = paramObj.toJavaObject(BasePageVo.class); - String keyword = searchVo.getKeyword(); - int count = resourceMapper.getAppSystemIdListCountByKeyword(keyword); - if (count > 0) { - searchVo.setRowNum(count); - List appSystemIdList = resourceMapper.getAppSystemIdListByKeyword(searchVo); - if (CollectionUtils.isEmpty(appSystemIdList)) { - return TableResultUtil.getResult(tbodyList, searchVo); - } - tbodyList = resourceMapper.getAppSystemListByIdList(appSystemIdList); - List hasModuleAppSystemIdList = resourceMapper.getHasModuleAppSystemIdListByAppSystemIdList(appSystemIdList); - if (CollectionUtils.isNotEmpty(hasModuleAppSystemIdList)) { - for (AppSystemVo appSystemVo : tbodyList) { - if (hasModuleAppSystemIdList.contains(appSystemVo.getId())) { - appSystemVo.setIsHasModule(1); - } - } - } - if (StringUtils.isNotEmpty(searchVo.getKeyword())) { - List appModuleList = resourceMapper.getAppModuleListByKeywordAndAppSystemIdList(keyword, appSystemIdList); - if (CollectionUtils.isNotEmpty(appModuleList)) { - Map> appModuleMap = new HashMap<>(); - for (AppModuleVo appModuleVo : appModuleList) { - appModuleMap.computeIfAbsent(appModuleVo.getAppSystemId(), key -> new ArrayList<>()).add(appModuleVo); - } - for (AppSystemVo appSystemVo : tbodyList) { - List appModuleVoList = appModuleMap.get(appSystemVo.getId()); - if (CollectionUtils.isNotEmpty(appModuleVoList)) { - appSystemVo.setAppModuleList(appModuleVoList); - appSystemVo.setIsHasModule(1); - } - } - } - } - } + IResourceCenterDataSource resourceCenterDataSource = ResourceCenterDataSourceFactory.getResourceCenterDataSource(); + List tbodyList = resourceCenterDataSource.getAppSystemListForTree(searchVo); return TableResultUtil.getResult(tbodyList, searchVo); } } diff --git a/src/main/java/neatlogic/module/cmdb/api/resourcecenter/appenv/ListAppEnvForSelectApi.java b/src/main/java/neatlogic/module/cmdb/api/resourcecenter/appenv/ListAppEnvForSelectApi.java index ffa53bb78a70a83d8414878178db89da0398d86e..34f801b297ee31ad283f9879bfaaf98edee34c82 100644 --- a/src/main/java/neatlogic/module/cmdb/api/resourcecenter/appenv/ListAppEnvForSelectApi.java +++ b/src/main/java/neatlogic/module/cmdb/api/resourcecenter/appenv/ListAppEnvForSelectApi.java @@ -15,23 +15,20 @@ along with this program. If not, see .*/ package neatlogic.module.cmdb.api.resourcecenter.appenv; -import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import neatlogic.framework.auth.core.AuthAction; import neatlogic.framework.cmdb.auth.label.CMDB; import neatlogic.framework.cmdb.dto.resourcecenter.ResourceVo; +import neatlogic.framework.cmdb.resourcecenter.datasource.core.IResourceCenterDataSource; +import neatlogic.framework.cmdb.resourcecenter.datasource.core.ResourceCenterDataSourceFactory; import neatlogic.framework.common.constvalue.ApiParamType; import neatlogic.framework.common.dto.BasePageVo; import neatlogic.framework.restful.annotation.*; import neatlogic.framework.restful.constvalue.OperationTypeEnum; import neatlogic.framework.restful.core.privateapi.PrivateApiComponentBase; import neatlogic.framework.util.TableResultUtil; -import neatlogic.module.cmdb.dao.mapper.resourcecenter.ResourceMapper; -import org.apache.commons.collections4.CollectionUtils; import org.springframework.stereotype.Service; -import javax.annotation.Resource; -import java.util.ArrayList; import java.util.List; @Service @@ -39,9 +36,6 @@ import java.util.List; @OperationType(type = OperationTypeEnum.SEARCH) public class ListAppEnvForSelectApi extends PrivateApiComponentBase { - @Resource - private ResourceMapper resourceMapper; - @Override public String getToken() { return "resourcecenter/appenv/list/forselect"; @@ -72,36 +66,8 @@ public class ListAppEnvForSelectApi extends PrivateApiComponentBase { @Override public Object myDoService(JSONObject paramObj) throws Exception { BasePageVo searchVo = paramObj.toJavaObject(BasePageVo.class); - JSONArray defaultValue = searchVo.getDefaultValue(); - if (CollectionUtils.isNotEmpty(defaultValue)) { - List idList = defaultValue.toJavaList(Long.class); - List resourceList = resourceMapper.searchAppEnvListByIdList(idList); - return TableResultUtil.getResult(resourceList); - } else { - int rowNum = resourceMapper.searchAppEnvCount(searchVo); - if (rowNum > 0) { - searchVo.setRowNum(rowNum); - if (searchVo.getNeedPage()) { - List idList = resourceMapper.searchAppEnvIdList(searchVo); - if (CollectionUtils.isNotEmpty(idList)) { - List resourceList = resourceMapper.searchAppEnvListByIdList(idList); - return TableResultUtil.getResult(resourceList, searchVo); - } - } else { - List allResourceList = new ArrayList<>(); - int pageCount = searchVo.getPageCount(); - for (int currentPage = 1; currentPage <= pageCount; currentPage++) { - searchVo.setCurrentPage(currentPage); - List idList = resourceMapper.searchAppEnvIdList(searchVo); - if (CollectionUtils.isNotEmpty(idList)) { - List resourceList = resourceMapper.searchAppEnvListByIdList(idList); - allResourceList.addAll(resourceList); - } - } - return TableResultUtil.getResult(allResourceList, searchVo); - } - } - } - return null; + IResourceCenterDataSource resourceCenterDataSource = ResourceCenterDataSourceFactory.getResourceCenterDataSource(); + List tbodyList = resourceCenterDataSource.getAppEnvListForSelect(searchVo); + return TableResultUtil.getResult(tbodyList, searchVo); } } diff --git a/src/main/java/neatlogic/module/cmdb/api/resourcecenter/appmodule/AppModuleListApi.java b/src/main/java/neatlogic/module/cmdb/api/resourcecenter/appmodule/AppModuleListApi.java index 86e1e929bf2e94ee08d352dfab6f11f8aa7de232..c95724fec75c8d7b9e96e9b2b1825465393e3ec0 100644 --- a/src/main/java/neatlogic/module/cmdb/api/resourcecenter/appmodule/AppModuleListApi.java +++ b/src/main/java/neatlogic/module/cmdb/api/resourcecenter/appmodule/AppModuleListApi.java @@ -20,16 +20,17 @@ import neatlogic.framework.auth.core.AuthAction; import neatlogic.framework.cmdb.auth.label.CMDB; import neatlogic.framework.cmdb.dto.resourcecenter.ResourceSearchVo; import neatlogic.framework.cmdb.dto.resourcecenter.ResourceVo; +import neatlogic.framework.cmdb.resourcecenter.datasource.core.IResourceCenterDataSource; +import neatlogic.framework.cmdb.resourcecenter.datasource.core.ResourceCenterDataSourceFactory; import neatlogic.framework.common.constvalue.ApiParamType; import neatlogic.framework.common.dto.BasePageVo; import neatlogic.framework.restful.annotation.*; import neatlogic.framework.restful.constvalue.OperationTypeEnum; import neatlogic.framework.restful.core.privateapi.PrivateApiComponentBase; import neatlogic.framework.util.TableResultUtil; -import neatlogic.module.cmdb.service.resourcecenter.resource.IResourceCenterResourceService; import org.springframework.stereotype.Service; -import javax.annotation.Resource; +import java.util.List; /** * @author linbq @@ -40,9 +41,6 @@ import javax.annotation.Resource; @OperationType(type = OperationTypeEnum.SEARCH) public class AppModuleListApi extends PrivateApiComponentBase { - @Resource - private IResourceCenterResourceService resourceCenterResourceService; - @Override public String getToken() { return "resourcecenter/appmodule/list"; @@ -73,6 +71,8 @@ public class AppModuleListApi extends PrivateApiComponentBase { @Override public Object myDoService(JSONObject paramObj) throws Exception { ResourceSearchVo searchVo = paramObj.toJavaObject(ResourceSearchVo.class); - return TableResultUtil.getResult(resourceCenterResourceService.getAppModuleList(searchVo), searchVo); + IResourceCenterDataSource resourceCenterDataSource = ResourceCenterDataSourceFactory.getResourceCenterDataSource(); + List tbodyList = resourceCenterDataSource.getAppModuleList(searchVo); + return TableResultUtil.getResult(tbodyList, searchVo); } } diff --git a/src/main/java/neatlogic/module/cmdb/api/resourcecenter/appmodule/AppModuleResourceTypeListApi.java b/src/main/java/neatlogic/module/cmdb/api/resourcecenter/appmodule/AppModuleResourceTypeListApi.java index fc995848eda76a7779c6672d35764bb3de8fdc69..169c8ed755a1463ca261db5c8ed9e5bc8da383f6 100644 --- a/src/main/java/neatlogic/module/cmdb/api/resourcecenter/appmodule/AppModuleResourceTypeListApi.java +++ b/src/main/java/neatlogic/module/cmdb/api/resourcecenter/appmodule/AppModuleResourceTypeListApi.java @@ -7,8 +7,9 @@ import neatlogic.framework.cmdb.auth.label.CMDB; 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.enums.resourcecenter.AppModuleResourceType; import neatlogic.framework.cmdb.exception.ci.CiNotFoundException; +import neatlogic.framework.cmdb.resourcecenter.datasource.core.IResourceCenterDataSource; +import neatlogic.framework.cmdb.resourcecenter.datasource.core.ResourceCenterDataSourceFactory; import neatlogic.framework.common.constvalue.ApiParamType; import neatlogic.framework.common.dto.BasePageVo; import neatlogic.framework.restful.annotation.*; @@ -16,7 +17,6 @@ import neatlogic.framework.restful.constvalue.OperationTypeEnum; import neatlogic.framework.restful.core.privateapi.PrivateApiComponentBase; import neatlogic.module.cmdb.dao.mapper.ci.CiMapper; import neatlogic.module.cmdb.dao.mapper.resourcecenter.ResourceMapper; -import neatlogic.module.cmdb.service.resourcecenter.resource.IResourceCenterResourceService; import org.apache.commons.collections4.CollectionUtils; import org.springframework.stereotype.Service; @@ -38,9 +38,6 @@ public class AppModuleResourceTypeListApi extends PrivateApiComponentBase { @Resource ResourceMapper resourceMapper; - @Resource - private IResourceCenterResourceService resourceCenterResourceService; - @Override public String getName() { return "查询当前模块各环境的需要显示的模型列表"; @@ -72,19 +69,7 @@ public class AppModuleResourceTypeListApi extends PrivateApiComponentBase { public Object myDoService(JSONObject paramObj) throws Exception { JSONArray returnArray = new JSONArray(); Long appModuleId = paramObj.getLong("appModuleId"); - //获取应用环境模型 -// CiVo envCiVo = ciMapper.getCiByName("APPEnv"); -// if (envCiVo == null) { -// throw new CiNotFoundException("APPEnv"); -// } - //获取需要采集的模型 - List resourceTypeNameList = AppModuleResourceType.getNameList(); - List resourceCiVoList = new ArrayList<>(); List envResourceList = new ArrayList<>(); - //获取应用环境实例list -// CiEntityVo envCiEntityVo = new CiEntityVo(); -// envCiEntityVo.setCiId(envCiVo.getId()); -// List envIdList = ciEntityMapper.getCiEntityIdByCiId(envCiEntityVo); BasePageVo search = new BasePageVo(); search.setCurrentPage(1); search.setPageSize(100); @@ -97,9 +82,6 @@ public class AppModuleResourceTypeListApi extends PrivateApiComponentBase { List allCiVoList = ciMapper.getAllCi(null); for (CiVo ci : allCiVoList) { allCiVoMap.put(ci.getId(), ci); - if (resourceTypeNameList.contains(ci.getName())) { - resourceCiVoList.add(ci); - } } ResourceSearchVo searchVo = new ResourceSearchVo(); searchVo.setAppModuleId(appModuleId); @@ -111,27 +93,21 @@ public class AppModuleResourceTypeListApi extends PrivateApiComponentBase { for (ResourceVo envResource : envResourceList) { JSONObject returnObj = new JSONObject(); searchVo.setEnvId(envResource.getId()); - //根据模块id和环境id,获取当前环境下含有资产的 模型idList(resourceTypeIdList) - Set resourceTypeIdSet = resourceMapper.getIpObjectResourceTypeIdListByAppModuleIdAndEnvId(searchVo); - List resourceTypeIdList = new ArrayList<>(resourceTypeIdSet); - Set returnCiVoSet = new HashSet<>(); - if (CollectionUtils.isNotEmpty(resourceTypeIdSet)) { - resourceTypeIdSet = resourceMapper.getOsResourceTypeIdListByAppModuleIdAndEnvId(searchVo); - resourceTypeIdList.addAll(resourceTypeIdSet); + Set typeIdSet = new HashSet<>(); + IResourceCenterDataSource resourceCenterDataSource = ResourceCenterDataSourceFactory.getResourceCenterDataSource(); + Map> viewName2TypeIdListMap = resourceCenterDataSource.getAppResourceTypeIdListByAppSystemIdAndAppModuleIdAndEnvId(null, appModuleId, envResource.getId()); + for (Map.Entry> entry : viewName2TypeIdListMap.entrySet()) { + String viewName = entry.getKey(); + searchVo.setViewName(viewName); + typeIdSet.addAll(entry.getValue()); } - - //循环resourceTypeIdList,将其父级模型的name存在于resourceTypeNameList中的 模型 返回给前端 - if (CollectionUtils.isNotEmpty(resourceTypeIdList)) { - for (Long resourceTypeId : resourceTypeIdList) { - CiVo ciVo = allCiVoMap.get(resourceTypeId); - if (ciVo == null) { - throw new CiNotFoundException(resourceTypeId); - } - String resourceTypeName = resourceCenterResourceService.getResourceTypeName(resourceCiVoList, ciVo); - if (resourceTypeNameList.contains(resourceTypeName)) { - returnCiVoSet.add(ciVo); - } + Set returnCiVoSet = new HashSet<>(); + for (Long typeId : typeIdSet) { + CiVo ciVo = allCiVoMap.get(typeId); + if (ciVo == null) { + throw new CiNotFoundException(typeId); } + returnCiVoSet.add(ciVo); } if (CollectionUtils.isNotEmpty(returnCiVoSet)) { returnObj.put("env", envResource); diff --git a/src/main/java/neatlogic/module/cmdb/api/resourcecenter/appmodule/ListAppModuleListForTreeApi.java b/src/main/java/neatlogic/module/cmdb/api/resourcecenter/appmodule/ListAppModuleListForTreeApi.java index 6a58ddf24c9d42bf2bcc85fdcc80f40c358d994c..f32f8697ba6a9aee832a6ea6f235cf396bfc402e 100644 --- a/src/main/java/neatlogic/module/cmdb/api/resourcecenter/appmodule/ListAppModuleListForTreeApi.java +++ b/src/main/java/neatlogic/module/cmdb/api/resourcecenter/appmodule/ListAppModuleListForTreeApi.java @@ -19,27 +19,19 @@ import com.alibaba.fastjson.JSONObject; import neatlogic.framework.auth.core.AuthAction; import neatlogic.framework.cmdb.auth.label.CMDB; import neatlogic.framework.cmdb.dto.resourcecenter.AppModuleVo; +import neatlogic.framework.cmdb.resourcecenter.datasource.core.IResourceCenterDataSource; +import neatlogic.framework.cmdb.resourcecenter.datasource.core.ResourceCenterDataSourceFactory; import neatlogic.framework.common.constvalue.ApiParamType; import neatlogic.framework.restful.annotation.*; import neatlogic.framework.restful.constvalue.OperationTypeEnum; import neatlogic.framework.restful.core.privateapi.PrivateApiComponentBase; -import neatlogic.module.cmdb.dao.mapper.resourcecenter.ResourceMapper; -import org.apache.commons.collections4.CollectionUtils; import org.springframework.stereotype.Service; -import javax.annotation.Resource; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - @Service @AuthAction(action = CMDB.class) @OperationType(type = OperationTypeEnum.SEARCH) public class ListAppModuleListForTreeApi extends PrivateApiComponentBase { - @Resource - private ResourceMapper resourceMapper; - @Override public String getToken() { return "resourcecenter/appmodule/list/fortree"; @@ -65,26 +57,7 @@ public class ListAppModuleListForTreeApi extends PrivateApiComponentBase { @Override public Object myDoService(JSONObject paramObj) throws Exception { Long appSystemId = paramObj.getLong("appSystemId"); - List tbodyList = resourceMapper.getAppModuleListByAppSystemId(appSystemId); - if (CollectionUtils.isNotEmpty(tbodyList)) { - Map appEnvCountMap = new HashMap<>(); - List> appEnvCountMapList = resourceMapper.getAppEnvCountMapByAppSystemIdGroupByAppModuleId(appSystemId); - for (Map map : appEnvCountMapList) { - Long count = map.get("count"); - Long appModuleId = map.get("appModuleId"); - appEnvCountMap.put(appModuleId, count); - } - for (AppModuleVo appModuleVo : tbodyList) { - Long count = appEnvCountMap.get(appModuleVo.getId()); - if (count == null) { - appModuleVo.setIsHasEnv(0); - } else if (count == 0) { - appModuleVo.setIsHasEnv(0); - } else { - appModuleVo.setIsHasEnv(1); - } - } - } - return tbodyList; + IResourceCenterDataSource resourceCenterDataSource = ResourceCenterDataSourceFactory.getResourceCenterDataSource(); + return resourceCenterDataSource.getAppModuleListForTree(appSystemId); } } diff --git a/src/main/java/neatlogic/module/cmdb/api/resourcecenter/config/DeleteResourceEntityApi.java b/src/main/java/neatlogic/module/cmdb/api/resourcecenter/config/DeleteResourceEntityApi.java new file mode 100644 index 0000000000000000000000000000000000000000..fa5c68aa05a7bd79e99a81adae5fd2d5e2106c04 --- /dev/null +++ b/src/main/java/neatlogic/module/cmdb/api/resourcecenter/config/DeleteResourceEntityApi.java @@ -0,0 +1,91 @@ +/* + * 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.module.cmdb.api.resourcecenter.config; + +import com.alibaba.fastjson.JSONObject; +import neatlogic.framework.asynchronization.threadlocal.TenantContext; +import neatlogic.framework.auth.core.AuthAction; +import neatlogic.framework.cmdb.auth.label.RESOURCECENTER_MODIFY; +import neatlogic.framework.cmdb.dto.resourcecenter.config.ResourceEntityConfigVo; +import neatlogic.framework.cmdb.dto.resourcecenter.config.ResourceEntityVo; +import neatlogic.framework.cmdb.dto.resourcecenter.config.SceneEntityVo; +import neatlogic.framework.common.constvalue.ApiParamType; +import neatlogic.framework.dao.mapper.SchemaMapper; +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.PrivateApiComponentBase; +import neatlogic.module.cmdb.dao.mapper.resourcecenter.ResourceEntityMapper; +import neatlogic.module.cmdb.utils.ResourceEntityFactory; +import org.apache.commons.lang3.StringUtils; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import javax.annotation.Resource; +import java.util.Objects; + +@Service +@AuthAction(action = RESOURCECENTER_MODIFY.class) +@OperationType(type = OperationTypeEnum.DELETE) +@Transactional +public class DeleteResourceEntityApi extends PrivateApiComponentBase { + + @Resource + private ResourceEntityMapper resourceEntityMapper; + + @Resource + private SchemaMapper schemaMapper; + + @Override + public String getName() { + return "删除资源视图配置信息"; + } + + @Input({ + @Param(name = "name", type = ApiParamType.STRING, isRequired = true, desc = "common.name") + }) + @Description(desc = "删除资源视图配置信息") + @Override + public Object myDoService(JSONObject paramObj) throws Exception { + String name = paramObj.getString("name"); + ResourceEntityVo oldResourceEntityVo = resourceEntityMapper.getResourceEntityByName(name); + if (oldResourceEntityVo != null) { + ResourceEntityConfigVo config = oldResourceEntityVo.getConfig(); + if (config != null) { + if (StringUtils.isNotBlank(config.getSceneTemplateName())) { + SceneEntityVo sceneEntityVo = ResourceEntityFactory.getSceneEntityByViewName(config.getSceneTemplateName()); + if (sceneEntityVo != null && Objects.equals(sceneEntityVo.getIsMultiple(), true)) { + resourceEntityMapper.deleteResourceEntityByName(name); + String tableType = schemaMapper.checkTableOrViewIsExists(TenantContext.get().getDataDbName(), name); + if (!Objects.equals(tableType, "BASE TABLE")) { + schemaMapper.deleteView(TenantContext.get().getDataDbName() + "." + name); + } + } + } + } + } + return null; + } + + @Override + public String getToken() { + return "resourcecenter/resourceentity/delete"; + } +} diff --git a/src/main/java/neatlogic/module/cmdb/api/resourcecenter/config/GetApplicationListDisplayApi.java b/src/main/java/neatlogic/module/cmdb/api/resourcecenter/config/GetApplicationListDisplayApi.java new file mode 100644 index 0000000000000000000000000000000000000000..970f6b878c1ea6076bad0eec113127d5c62ddec8 --- /dev/null +++ b/src/main/java/neatlogic/module/cmdb/api/resourcecenter/config/GetApplicationListDisplayApi.java @@ -0,0 +1,61 @@ +/* + * 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.module.cmdb.api.resourcecenter.config; + +import com.alibaba.fastjson.JSONObject; +import neatlogic.framework.auth.core.AuthAction; +import neatlogic.framework.cmdb.auth.label.RESOURCECENTER_MODIFY; +import neatlogic.framework.cmdb.dto.resourcecenter.ApplicationListDisplayVo; +import neatlogic.framework.restful.annotation.Description; +import neatlogic.framework.restful.annotation.Input; +import neatlogic.framework.restful.annotation.OperationType; +import neatlogic.framework.restful.annotation.Output; +import neatlogic.framework.restful.constvalue.OperationTypeEnum; +import neatlogic.framework.restful.core.privateapi.PrivateApiComponentBase; +import neatlogic.module.cmdb.dao.mapper.resourcecenter.ResourceEntityMapper; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; + +@Service +@AuthAction(action = RESOURCECENTER_MODIFY.class) +@OperationType(type = OperationTypeEnum.OPERATE) +public class GetApplicationListDisplayApi extends PrivateApiComponentBase { + + @Resource + private ResourceEntityMapper resourceEntityMapper; + + @Override + public String getName() { + return "获取应用清单显示设置"; + } + + @Input({}) + @Output({}) + @Description(desc = "获取应用清单显示设置") + @Override + public Object myDoService(JSONObject paramObj) throws Exception { + ApplicationListDisplayVo applicationListDisplayVo = resourceEntityMapper.getApplicationListDisplay(); + return applicationListDisplayVo; + } + + @Override + public String getToken() { + return "resourcecenter/applicationlist/display/get"; + } +} diff --git a/src/main/java/neatlogic/module/cmdb/api/resourcecenter/config/GetAssetListDisplayApi.java b/src/main/java/neatlogic/module/cmdb/api/resourcecenter/config/GetAssetListDisplayApi.java new file mode 100644 index 0000000000000000000000000000000000000000..ca0e87b833e1518e77860de13ca4114618ccf119 --- /dev/null +++ b/src/main/java/neatlogic/module/cmdb/api/resourcecenter/config/GetAssetListDisplayApi.java @@ -0,0 +1,61 @@ +/* + * 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.module.cmdb.api.resourcecenter.config; + +import com.alibaba.fastjson.JSONObject; +import neatlogic.framework.auth.core.AuthAction; +import neatlogic.framework.cmdb.auth.label.RESOURCECENTER_MODIFY; +import neatlogic.framework.cmdb.dto.resourcecenter.AssetListDisplayVo; +import neatlogic.framework.restful.annotation.Description; +import neatlogic.framework.restful.annotation.Input; +import neatlogic.framework.restful.annotation.OperationType; +import neatlogic.framework.restful.annotation.Output; +import neatlogic.framework.restful.constvalue.OperationTypeEnum; +import neatlogic.framework.restful.core.privateapi.PrivateApiComponentBase; +import neatlogic.module.cmdb.dao.mapper.resourcecenter.ResourceEntityMapper; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; + +@Service +@AuthAction(action = RESOURCECENTER_MODIFY.class) +@OperationType(type = OperationTypeEnum.OPERATE) +public class GetAssetListDisplayApi extends PrivateApiComponentBase { + + @Resource + private ResourceEntityMapper resourceEntityMapper; + + @Override + public String getName() { + return "获取资产清单显示设置"; + } + + @Input({}) + @Output({}) + @Description(desc = "获取资产清单显示设置") + @Override + public Object myDoService(JSONObject paramObj) throws Exception { + AssetListDisplayVo assetListDisplayVo = resourceEntityMapper.getAssetListDisplay(); + return assetListDisplayVo; + } + + @Override + public String getToken() { + return "resourcecenter/assetlist/display/get"; + } +} diff --git a/src/main/java/neatlogic/module/cmdb/api/resourcecenter/config/GetResourceEntityApi.java b/src/main/java/neatlogic/module/cmdb/api/resourcecenter/config/GetResourceEntityApi.java index 9b93b86835eecf9fd680369c241512671752f53d..b67ba871255b5dc9faad98a314eb6cdeb0548593 100644 --- a/src/main/java/neatlogic/module/cmdb/api/resourcecenter/config/GetResourceEntityApi.java +++ b/src/main/java/neatlogic/module/cmdb/api/resourcecenter/config/GetResourceEntityApi.java @@ -19,6 +19,8 @@ import com.alibaba.fastjson.JSONObject; import neatlogic.framework.auth.core.AuthAction; import neatlogic.framework.cmdb.auth.label.CMDB; import neatlogic.framework.cmdb.dto.ci.CiVo; +import neatlogic.framework.cmdb.dto.resourcecenter.config.ResourceEntityConfigVo; +import neatlogic.framework.cmdb.dto.resourcecenter.config.ResourceEntityRelNodeVo; import neatlogic.framework.cmdb.dto.resourcecenter.config.ResourceEntityVo; import neatlogic.framework.cmdb.dto.resourcecenter.config.SceneEntityVo; import neatlogic.framework.cmdb.exception.resourcecenter.ResourceCenterResourceFoundException; @@ -27,9 +29,11 @@ import neatlogic.framework.common.dto.ValueTextVo; import neatlogic.framework.restful.annotation.*; import neatlogic.framework.restful.constvalue.OperationTypeEnum; import neatlogic.framework.restful.core.privateapi.PrivateApiComponentBase; +import neatlogic.framework.util.UuidUtil; import neatlogic.module.cmdb.dao.mapper.ci.CiMapper; import neatlogic.module.cmdb.dao.mapper.resourcecenter.ResourceEntityMapper; import neatlogic.module.cmdb.utils.ResourceEntityFactory; +import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; import javax.annotation.Resource; @@ -73,24 +77,56 @@ public class GetResourceEntityApi extends PrivateApiComponentBase { @Override public Object myDoService(JSONObject paramObj) throws Exception { String name = paramObj.getString("name"); - SceneEntityVo sceneEntityVo = ResourceEntityFactory.getSceneEntityByViewName(name); - if (sceneEntityVo == null) { - throw new ResourceCenterResourceFoundException(name); - } ResourceEntityVo resourceEntityVo = resourceEntityMapper.getResourceEntityByName(name); if (resourceEntityVo == null) { + SceneEntityVo sceneEntityVo = ResourceEntityFactory.getSceneEntityByViewName(name); + if (sceneEntityVo == null) { + throw new ResourceCenterResourceFoundException(name); + } resourceEntityVo = new ResourceEntityVo(); resourceEntityVo.setName(sceneEntityVo.getName()); - } else if (resourceEntityVo.getCiId() != null) { - CiVo ciVo = ciMapper.getCiById(resourceEntityVo.getCiId()); - if (ciVo != null) { - resourceEntityVo.setCi(ciVo); + resourceEntityVo.setLabel(sceneEntityVo.getLabel()); + resourceEntityVo.setDescription(sceneEntityVo.getDescription()); + resourceEntityVo.setIsMultiple(sceneEntityVo.getIsMultiple()); + List fieldList = ResourceEntityFactory.getFieldListByViewName(name); + resourceEntityVo.setFieldList(fieldList); + return resourceEntityVo; + } + ResourceEntityConfigVo config = resourceEntityVo.getConfig(); + if (config != null) { + if (StringUtils.isNotBlank(config.getMainCi())) { + CiVo ciVo = ciMapper.getCiByName(config.getMainCi()); + if (ciVo != null) { + resourceEntityVo.setCi(ciVo); + ResourceEntityRelNodeVo relNode = config.getRelNode(); + if (relNode == null) { + relNode = new ResourceEntityRelNodeVo(); + relNode.setUuid(UuidUtil.randomUuid()); + relNode.setCiName(ciVo.getName()); + relNode.setCiLabel(ciVo.getLabel()); + config.setRelNode(relNode); + } + } + } + String sceneTemplateName = config.getSceneTemplateName(); + if (StringUtils.isNotBlank(sceneTemplateName)) { + resourceEntityVo.setIsMultiple(true); + SceneEntityVo sceneTemplate = ResourceEntityFactory.getSceneEntityByViewName(sceneTemplateName); + if (sceneTemplate != null) { + resourceEntityVo.setIsMultiple(sceneTemplate.getIsMultiple()); + List fieldList = ResourceEntityFactory.getFieldListByViewName(sceneTemplateName); + resourceEntityVo.setFieldList(fieldList); + } + } else { + resourceEntityVo.setIsMultiple(false); + SceneEntityVo sceneEntityVo = ResourceEntityFactory.getSceneEntityByViewName(name); + if (sceneEntityVo != null) { + resourceEntityVo.setIsMultiple(sceneEntityVo.getIsMultiple()); + List fieldList = ResourceEntityFactory.getFieldListByViewName(name); + resourceEntityVo.setFieldList(fieldList); + } } } - resourceEntityVo.setLabel(sceneEntityVo.getLabel()); - resourceEntityVo.setDescription(sceneEntityVo.getDescription()); - List fieldList = ResourceEntityFactory.getFieldListByViewName(name); - resourceEntityVo.setFieldList(fieldList); return resourceEntityVo; } } diff --git a/src/main/java/neatlogic/module/cmdb/api/resourcecenter/config/ListAppAssetListAllTheadApi.java b/src/main/java/neatlogic/module/cmdb/api/resourcecenter/config/ListAppAssetListAllTheadApi.java new file mode 100644 index 0000000000000000000000000000000000000000..24e8e95b098e2d5dc83d37523b5f8f2f23ac020c --- /dev/null +++ b/src/main/java/neatlogic/module/cmdb/api/resourcecenter/config/ListAppAssetListAllTheadApi.java @@ -0,0 +1,60 @@ +/* + * 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.module.cmdb.api.resourcecenter.config; + +import com.alibaba.fastjson.JSONObject; +import neatlogic.framework.auth.core.AuthAction; +import neatlogic.framework.cmdb.auth.label.RESOURCECENTER_MODIFY; +import neatlogic.framework.cmdb.resourcecenter.datasource.core.IResourceCenterDataSource; +import neatlogic.framework.cmdb.resourcecenter.datasource.core.ResourceCenterDataSourceFactory; +import neatlogic.framework.common.dto.ValueTextVo; +import neatlogic.framework.restful.annotation.Description; +import neatlogic.framework.restful.annotation.Input; +import neatlogic.framework.restful.annotation.OperationType; +import neatlogic.framework.restful.annotation.Output; +import neatlogic.framework.restful.constvalue.OperationTypeEnum; +import neatlogic.framework.restful.core.privateapi.PrivateApiComponentBase; +import neatlogic.framework.util.TableResultUtil; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +@AuthAction(action = RESOURCECENTER_MODIFY.class) +@OperationType(type = OperationTypeEnum.OPERATE) +public class ListAppAssetListAllTheadApi extends PrivateApiComponentBase { + @Override + public String getName() { + return "获取应用清单资产清单所有表头列表"; + } + + @Input({}) + @Output({}) + @Description(desc = "获取应用清单资产清单所有表头列表") + @Override + public Object myDoService(JSONObject paramObj) throws Exception { + IResourceCenterDataSource resourceCenterDataSource = ResourceCenterDataSourceFactory.getResourceCenterDataSource(); + List appAssertAllTheadList = resourceCenterDataSource.getAppAssertAllTheadList(); + return TableResultUtil.getResult(appAssertAllTheadList); + } + + @Override + public String getToken() { + return "resourcecenter/app/assetlist/theadlist"; + } +} diff --git a/src/main/java/neatlogic/module/cmdb/api/resourcecenter/config/ListApplicationAssetListDetailViewApi.java b/src/main/java/neatlogic/module/cmdb/api/resourcecenter/config/ListApplicationAssetListDetailViewApi.java new file mode 100644 index 0000000000000000000000000000000000000000..f0dece8d1a8cab4e940fa1197b0d5874ecad8803 --- /dev/null +++ b/src/main/java/neatlogic/module/cmdb/api/resourcecenter/config/ListApplicationAssetListDetailViewApi.java @@ -0,0 +1,99 @@ +/* + * 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.module.cmdb.api.resourcecenter.config; + +import com.alibaba.fastjson.JSONObject; +import neatlogic.framework.auth.core.AuthAction; +import neatlogic.framework.cmdb.auth.label.RESOURCECENTER_MODIFY; +import neatlogic.framework.cmdb.dto.resourcecenter.config.ResourceEntityConfigVo; +import neatlogic.framework.cmdb.dto.resourcecenter.config.ResourceEntityVo; +import neatlogic.framework.cmdb.dto.resourcecenter.config.SceneEntityVo; +import neatlogic.framework.common.constvalue.ApiParamType; +import neatlogic.framework.common.dto.BasePageVo; +import neatlogic.framework.common.dto.ValueTextVo; +import neatlogic.framework.restful.annotation.*; +import neatlogic.framework.restful.constvalue.OperationTypeEnum; +import neatlogic.framework.restful.core.privateapi.PrivateApiComponentBase; +import neatlogic.framework.util.TableResultUtil; +import neatlogic.module.cmdb.dao.mapper.resourcecenter.ResourceEntityMapper; +import neatlogic.module.cmdb.utils.ResourceEntityFactory; +import org.apache.commons.lang3.StringUtils; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +@Service +@AuthAction(action = RESOURCECENTER_MODIFY.class) +@OperationType(type = OperationTypeEnum.OPERATE) +public class ListApplicationAssetListDetailViewApi extends PrivateApiComponentBase { + + @Resource + private ResourceEntityMapper resourceEntityMapper; + + @Override + public String getName() { + return "获取应用资产清单详情视图列表"; + } + + @Input({ + @Param(name = "keyword", type = ApiParamType.STRING, desc = "common.keyword") + }) + @Output({ + @Param(name = "tbodyList", explode = ValueTextVo[].class, desc = "common.tbodylist") + }) + @Description(desc = "获取应用资产清单详情视图列表") + @Override + public Object myDoService(JSONObject paramObj) throws Exception { + String keyword = paramObj.getString("keyword"); + List tbodyList = new ArrayList<>(); + List resourceEntityList = resourceEntityMapper.getResourceEntityList(); + for (ResourceEntityVo resourceEntityVo : resourceEntityList) { + SceneEntityVo sceneEntityVo = ResourceEntityFactory.getSceneEntityByViewName(resourceEntityVo.getName()); + if (sceneEntityVo == null) { + String config = resourceEntityMapper.getResourceEntityConfigByName(resourceEntityVo.getName()); + if (StringUtils.isNotBlank(config)) { + ResourceEntityConfigVo resourceEntityConfigVo = JSONObject.parseObject(config, ResourceEntityConfigVo.class); + if (resourceEntityConfigVo != null) { + String sceneTemplateName = resourceEntityConfigVo.getSceneTemplateName(); + if (Objects.equals(sceneTemplateName, "scence_application_asset_list_detail")) { + if (StringUtils.isNotBlank(keyword)) { + if (!resourceEntityVo.getName().contains(keyword) && !resourceEntityVo.getLabel().contains(keyword)) { + continue; + } + } + tbodyList.add(new ValueTextVo(resourceEntityVo.getName(), resourceEntityVo.getLabel())); + } + } + } + } + } + BasePageVo basePageVo = new BasePageVo(); + basePageVo.setCurrentPage(1); + basePageVo.setPageSize(tbodyList.size()); + basePageVo.setRowNum(tbodyList.size()); + return TableResultUtil.getResult(tbodyList, basePageVo); + } + + @Override + public String getToken() { + return "resourcecenter/application/assetlist/view/list"; + } +} diff --git a/src/main/java/neatlogic/module/cmdb/api/resourcecenter/config/ListAssetListAllTheadApi.java b/src/main/java/neatlogic/module/cmdb/api/resourcecenter/config/ListAssetListAllTheadApi.java new file mode 100644 index 0000000000000000000000000000000000000000..07bcae49c4df285b3bb98f79f9a9904cf1eaf09d --- /dev/null +++ b/src/main/java/neatlogic/module/cmdb/api/resourcecenter/config/ListAssetListAllTheadApi.java @@ -0,0 +1,59 @@ +/* + * 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.module.cmdb.api.resourcecenter.config; + +import com.alibaba.fastjson.JSONObject; +import neatlogic.framework.auth.core.AuthAction; +import neatlogic.framework.cmdb.auth.label.RESOURCECENTER_MODIFY; +import neatlogic.framework.cmdb.resourcecenter.datasource.core.IResourceCenterDataSource; +import neatlogic.framework.cmdb.resourcecenter.datasource.core.ResourceCenterDataSourceFactory; +import neatlogic.framework.common.dto.ValueTextVo; +import neatlogic.framework.restful.annotation.Description; +import neatlogic.framework.restful.annotation.Input; +import neatlogic.framework.restful.annotation.OperationType; +import neatlogic.framework.restful.annotation.Output; +import neatlogic.framework.restful.constvalue.OperationTypeEnum; +import neatlogic.framework.restful.core.privateapi.PrivateApiComponentBase; +import neatlogic.framework.util.TableResultUtil; + +import java.util.List; + +//@Service +@AuthAction(action = RESOURCECENTER_MODIFY.class) +@OperationType(type = OperationTypeEnum.OPERATE) +public class ListAssetListAllTheadApi extends PrivateApiComponentBase { + @Override + public String getName() { + return "获取资产清单所有表头列表"; + } + + @Input({}) + @Output({}) + @Description(desc = "获取资产清单所有表头列表") + @Override + public Object myDoService(JSONObject paramObj) throws Exception { + IResourceCenterDataSource resourceCenterDataSource = ResourceCenterDataSourceFactory.getResourceCenterDataSource(); + List appAssertAllTheadList = resourceCenterDataSource.getAssertAllTheadList(); + return TableResultUtil.getResult(appAssertAllTheadList); + } + + @Override + public String getToken() { + return "resourcecenter/assetlist/theadlist"; + } +} diff --git a/src/main/java/neatlogic/module/cmdb/api/resourcecenter/config/ListResourceEntityApi.java b/src/main/java/neatlogic/module/cmdb/api/resourcecenter/config/ListResourceEntityApi.java index 97ce8306125149a273a513aab9b3a745c89b4ded..cf825868766420886571d3bc08859a908a9bf3df 100644 --- a/src/main/java/neatlogic/module/cmdb/api/resourcecenter/config/ListResourceEntityApi.java +++ b/src/main/java/neatlogic/module/cmdb/api/resourcecenter/config/ListResourceEntityApi.java @@ -18,9 +18,12 @@ package neatlogic.module.cmdb.api.resourcecenter.config; import com.alibaba.fastjson.JSONObject; import neatlogic.framework.auth.core.AuthAction; import neatlogic.framework.cmdb.auth.label.RESOURCECENTER_MODIFY; +import neatlogic.framework.cmdb.dto.resourcecenter.config.ResourceEntityConfigVo; import neatlogic.framework.cmdb.dto.resourcecenter.config.ResourceEntityVo; import neatlogic.framework.cmdb.dto.resourcecenter.config.SceneEntityVo; import neatlogic.framework.cmdb.enums.resourcecenter.Status; +import neatlogic.framework.common.util.ModuleUtil; +import neatlogic.framework.dto.module.ModuleVo; import neatlogic.framework.restful.annotation.Description; import neatlogic.framework.restful.annotation.OperationType; import neatlogic.framework.restful.annotation.Output; @@ -72,12 +75,11 @@ public class ListResourceEntityApi extends PrivateApiComponentBase { @Override public Object myDoService(JSONObject paramObj) throws Exception { List resultList = new ArrayList<>(); - List viewNameList = ResourceEntityFactory.getViewNameList(); - List resourceEntityList = resourceEntityMapper.getResourceEntityListByNameList(viewNameList); - Map resourceEntityVoMap = resourceEntityList.stream().collect(Collectors.toMap(ResourceEntityVo::getName, e -> e)); + List allResourceEntityList = resourceEntityMapper.getResourceEntityList(); + Map resourceEntityVoMap = allResourceEntityList.stream().collect(Collectors.toMap(ResourceEntityVo::getName, e -> e)); List sceneEntityList = ResourceEntityFactory.getSceneEntityList(); for (SceneEntityVo sceneEntityVo : sceneEntityList) { - ResourceEntityVo resourceEntityVo = resourceEntityVoMap.get(sceneEntityVo.getName()); + ResourceEntityVo resourceEntityVo = resourceEntityVoMap.remove(sceneEntityVo.getName()); if (resourceEntityVo == null) { resourceEntityVo = new ResourceEntityVo(); resourceEntityVo.setStatus(Status.PENDING.getValue()); @@ -85,6 +87,14 @@ public class ListResourceEntityApi extends PrivateApiComponentBase { resourceEntityVo.setName(sceneEntityVo.getName()); resourceEntityVo.setLabel(sceneEntityVo.getLabel()); resourceEntityVo.setDescription(sceneEntityVo.getDescription()); + resourceEntityVo.setIsMultiple(sceneEntityVo.getIsMultiple()); + resourceEntityVo.setModuleId(sceneEntityVo.getModuleId()); + ModuleVo moduleVo = ModuleUtil.getModuleById(sceneEntityVo.getModuleId()); + if (moduleVo != null) { + resourceEntityVo.setModuleName(moduleVo.getName()); + } else { + resourceEntityVo.setModuleName(sceneEntityVo.getModuleId()); + } try { resourceEntityMapper.getResourceEntityViewDataList(sceneEntityVo.getName(), 0, 1); } catch (Exception e) { @@ -98,6 +108,46 @@ public class ListResourceEntityApi extends PrivateApiComponentBase { } resultList.add(resourceEntityVo); } + // 扩展视图 + for (Map.Entry entry : resourceEntityVoMap.entrySet()) { + ResourceEntityVo resourceEntityVo = entry.getValue(); + SceneEntityVo sceneEntityVo = ResourceEntityFactory.getSceneEntityByViewName(resourceEntityVo.getName()); + if (sceneEntityVo == null) { + String config = resourceEntityMapper.getResourceEntityConfigByName(resourceEntityVo.getName()); + if (StringUtils.isNotBlank(config)) { + ResourceEntityConfigVo resourceEntityConfigVo = JSONObject.parseObject(config, ResourceEntityConfigVo.class); + if (resourceEntityConfigVo != null) { + String sceneTemplateName = resourceEntityConfigVo.getSceneTemplateName(); + if (StringUtils.isNotBlank(sceneTemplateName)) { + SceneEntityVo sceneTemplate = ResourceEntityFactory.getSceneEntityByViewName(sceneTemplateName); + if (sceneTemplate != null) { + resourceEntityVo.setIsMultiple(sceneTemplate.getIsMultiple()); + resourceEntityVo.setModuleId(sceneTemplate.getModuleId()); + ModuleVo moduleVo = ModuleUtil.getModuleById(sceneTemplate.getModuleId()); + if (moduleVo != null) { + resourceEntityVo.setModuleName(moduleVo.getName()); + } else { + resourceEntityVo.setModuleName(sceneTemplate.getModuleId()); + } + try { + resourceEntityMapper.getResourceEntityViewDataList(resourceEntityVo.getName(), 0, 1); + } catch (Exception e) { + resourceEntityVo.setStatus(Status.ERROR.getValue()); + String error = resourceEntityVo.getError(); + if (StringUtils.isNotBlank(error)) { + resourceEntityVo.setError(error + e.getMessage()); + } else { + resourceEntityVo.setError(e.getMessage()); + } + } + resultList.add(resourceEntityVo); + } + } + } + } + } + } + resultList.sort((o1, o2) -> o1.getName().compareToIgnoreCase(o2.getName())); return resultList; } } diff --git a/src/main/java/neatlogic/module/cmdb/api/resourcecenter/config/ListSupportMultipleViewSceneTemplate.java b/src/main/java/neatlogic/module/cmdb/api/resourcecenter/config/ListSupportMultipleViewSceneTemplate.java new file mode 100644 index 0000000000000000000000000000000000000000..5e2273120df1632eaa9df68665c48fd87bb1c28e --- /dev/null +++ b/src/main/java/neatlogic/module/cmdb/api/resourcecenter/config/ListSupportMultipleViewSceneTemplate.java @@ -0,0 +1,89 @@ +/* + * 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.module.cmdb.api.resourcecenter.config; + +import com.alibaba.fastjson.JSONObject; +import neatlogic.framework.auth.core.AuthAction; +import neatlogic.framework.cmdb.auth.label.RESOURCECENTER_MODIFY; +import neatlogic.framework.cmdb.dto.resourcecenter.config.ResourceEntityVo; +import neatlogic.framework.cmdb.dto.resourcecenter.config.SceneEntityVo; +import neatlogic.framework.common.constvalue.ApiParamType; +import neatlogic.framework.common.dto.BasePageVo; +import neatlogic.framework.common.dto.ValueTextVo; +import neatlogic.framework.common.util.PageUtil; +import neatlogic.framework.restful.annotation.*; +import neatlogic.framework.restful.constvalue.OperationTypeEnum; +import neatlogic.framework.restful.core.privateapi.PrivateApiComponentBase; +import neatlogic.framework.util.TableResultUtil; +import neatlogic.module.cmdb.utils.ResourceEntityFactory; +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang3.StringUtils; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.List; + +@Service +@AuthAction(action = RESOURCECENTER_MODIFY.class) +@OperationType(type = OperationTypeEnum.SEARCH) +public class ListSupportMultipleViewSceneTemplate extends PrivateApiComponentBase { + @Override + public String getName() { + return "资源中心支持创建多张视图的场景模板列表"; + } + + @Input({ + @Param(name = "keyword", type = ApiParamType.STRING, desc = "common.keyword"), + @Param(name = "currentPage", type = ApiParamType.INTEGER, defaultValue = "1", desc = "common.currentpage"), + @Param(name = "pageSize", type = ApiParamType.INTEGER, defaultValue = "20", desc = "common.pagesize") + }) + @Output({ + @Param(name = "tbodyList", explode = ResourceEntityVo[].class, desc = "common.tbodylist") + }) + @Description(desc = "资源中心支持创建多张视图的场景模板列表") + @Override + public Object myDoService(JSONObject paramObj) throws Exception { + BasePageVo basePageVo = paramObj.toJavaObject(BasePageVo.class); + List tbodyList = new ArrayList<>(); + List sceneEntityList = ResourceEntityFactory.getMultipleSceneEntityList(); + if (CollectionUtils.isNotEmpty(sceneEntityList)) { + String keyword = basePageVo.getKeyword(); + for (SceneEntityVo sceneEntityVo : sceneEntityList) { + if (StringUtils.isNotBlank(keyword)) { + if (!sceneEntityVo.getName().contains(keyword) && !sceneEntityVo.getLabel().contains(keyword)) { + continue; + } + } + ResourceEntityVo resourceEntityVo = new ResourceEntityVo(); + resourceEntityVo.setName(sceneEntityVo.getName()); + resourceEntityVo.setLabel(sceneEntityVo.getLabel()); + List fieldList = ResourceEntityFactory.getFieldListByViewName(sceneEntityVo.getName()); + resourceEntityVo.setFieldList(fieldList); + tbodyList.add(resourceEntityVo); + } + basePageVo.setRowNum(tbodyList.size()); + tbodyList = PageUtil.subList(tbodyList, basePageVo); + } + return TableResultUtil.getResult(tbodyList, basePageVo); + } + + @Override + public String getToken() { + return "resourcecenter/suportmultipleview/scenetemplate"; + } +} diff --git a/src/main/java/neatlogic/module/cmdb/api/resourcecenter/config/SaveApplicationListDisplayApi.java b/src/main/java/neatlogic/module/cmdb/api/resourcecenter/config/SaveApplicationListDisplayApi.java new file mode 100644 index 0000000000000000000000000000000000000000..c788520b07b0c2e73f30ee7433c983ae5a26b098 --- /dev/null +++ b/src/main/java/neatlogic/module/cmdb/api/resourcecenter/config/SaveApplicationListDisplayApi.java @@ -0,0 +1,79 @@ +/* + * 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.module.cmdb.api.resourcecenter.config; + +import com.alibaba.fastjson.JSONObject; +import neatlogic.framework.auth.core.AuthAction; +import neatlogic.framework.cmdb.auth.label.RESOURCECENTER_MODIFY; +import neatlogic.framework.cmdb.dto.resourcecenter.ApplicationListDisplayVo; +import neatlogic.framework.common.constvalue.ApiParamType; +import neatlogic.framework.restful.annotation.*; +import neatlogic.framework.restful.constvalue.OperationTypeEnum; +import neatlogic.framework.restful.core.privateapi.PrivateApiComponentBase; +import neatlogic.framework.util.SnowflakeUtil; +import neatlogic.module.cmdb.dao.mapper.resourcecenter.ResourceEntityMapper; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import javax.annotation.Resource; + +@Service +@AuthAction(action = RESOURCECENTER_MODIFY.class) +@OperationType(type = OperationTypeEnum.OPERATE) +@Transactional +public class SaveApplicationListDisplayApi extends PrivateApiComponentBase { + + @Resource + private ResourceEntityMapper resourceEntityMapper; + + @Override + public String getName() { + return "保存应用清单显示设置"; + } + + @Input({ + @Param(name = "id", type = ApiParamType.LONG, desc = "common.id"), + @Param(name = "config", type = ApiParamType.JSONOBJECT, isRequired = true, desc = "common.config") + }) + @Output({}) + @Description(desc = "保存应用清单显示设置") + @Override + public Object myDoService(JSONObject paramObj) throws Exception { + Long id = paramObj.getLong("id"); + JSONObject config = paramObj.getJSONObject("config"); + ApplicationListDisplayVo applicationListDisplayVo = new ApplicationListDisplayVo(); + if (id != null) { + applicationListDisplayVo.setId(id); + } else { + ApplicationListDisplayVo applicationListDisplay = resourceEntityMapper.getApplicationListDisplay(); + if (applicationListDisplay != null) { + applicationListDisplayVo.setId(applicationListDisplay.getId()); + } else { + applicationListDisplayVo.setId(SnowflakeUtil.uniqueLong()); + } + } + applicationListDisplayVo.setConfig(config); + resourceEntityMapper.insertApplicationListDisplay(applicationListDisplayVo); + return null; + } + + @Override + public String getToken() { + return "resourcecenter/applicationlist/display/save"; + } +} diff --git a/src/main/java/neatlogic/module/cmdb/api/resourcecenter/config/SaveAssertListDisplayApi.java b/src/main/java/neatlogic/module/cmdb/api/resourcecenter/config/SaveAssertListDisplayApi.java new file mode 100644 index 0000000000000000000000000000000000000000..ddfc568abc6bd578dce6aa4e9ec2868ce852f613 --- /dev/null +++ b/src/main/java/neatlogic/module/cmdb/api/resourcecenter/config/SaveAssertListDisplayApi.java @@ -0,0 +1,101 @@ +/* + * 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.module.cmdb.api.resourcecenter.config; + +import com.alibaba.fastjson.JSONObject; +import neatlogic.framework.auth.core.AuthAction; +import neatlogic.framework.cmdb.auth.label.RESOURCECENTER_MODIFY; +import neatlogic.framework.cmdb.dto.ci.CiVo; +import neatlogic.framework.cmdb.dto.resourcecenter.AssetListDisplayVo; +import neatlogic.framework.cmdb.exception.ci.CiNotFoundException; +import neatlogic.framework.common.constvalue.ApiParamType; +import neatlogic.framework.restful.annotation.*; +import neatlogic.framework.restful.constvalue.OperationTypeEnum; +import neatlogic.framework.restful.core.privateapi.PrivateApiComponentBase; +import neatlogic.framework.util.SnowflakeUtil; +import neatlogic.module.cmdb.dao.mapper.ci.CiMapper; +import neatlogic.module.cmdb.dao.mapper.resourcecenter.ResourceEntityMapper; +import org.apache.commons.collections4.CollectionUtils; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import javax.annotation.Resource; +import java.util.List; + +@Service +@AuthAction(action = RESOURCECENTER_MODIFY.class) +@OperationType(type = OperationTypeEnum.OPERATE) +@Transactional +public class SaveAssertListDisplayApi extends PrivateApiComponentBase { + + @Resource + private ResourceEntityMapper resourceEntityMapper; + + @Resource + private CiMapper ciMapper; + + @Override + public String getName() { + return "保存资产清单显示设置"; + } + + @Input({ + @Param(name = "id", type = ApiParamType.LONG, desc = "common.id"), + @Param(name = "rootCiName", type = ApiParamType.STRING, isRequired = true, desc = "根模型名称"), + @Param(name = "config", type = ApiParamType.JSONOBJECT, isRequired = true, desc = "common.config") + }) + @Output({}) + @Description(desc = "保存资产清单显示设置") + @Override + public Object myDoService(JSONObject paramObj) throws Exception { + Long id = paramObj.getLong("id"); + String rootCiName = paramObj.getString("rootCiName"); + JSONObject config = paramObj.getJSONObject("config"); + AssetListDisplayVo assetListDisplayVo = new AssetListDisplayVo(); + if (id != null) { + assetListDisplayVo.setId(id); + } else { + AssetListDisplayVo assetListDisplay = resourceEntityMapper.getAssetListDisplay(); + if (assetListDisplay != null) { + assetListDisplayVo.setId(assetListDisplay.getId()); + } else { + assetListDisplayVo.setId(SnowflakeUtil.uniqueLong()); + } + } + assetListDisplayVo.setRootCiName(rootCiName); + assetListDisplayVo.setConfig(config); + CiVo ciVo = ciMapper.getCiByName(rootCiName); + if (ciVo == null) { + throw new CiNotFoundException(rootCiName); + } + List ciIdList = resourceEntityMapper.getAllResourceTypeCiIdList(); + if (!ciIdList.contains(ciVo.getId())) { + if (CollectionUtils.isNotEmpty(ciIdList)) { + resourceEntityMapper.deleteResourceTypeCi(); + } + resourceEntityMapper.insertResourceTypeCi(ciVo.getId()); + } + resourceEntityMapper.insertAssetListDisplay(assetListDisplayVo); + return null; + } + + @Override + public String getToken() { + return "resourcecenter/assetlist/display/save"; + } +} diff --git a/src/main/java/neatlogic/module/cmdb/api/resourcecenter/config/SaveResourceEntityApi.java b/src/main/java/neatlogic/module/cmdb/api/resourcecenter/config/SaveResourceEntityApi.java index 6e8f95735ed89793e99cf4694cf1d36ebac63d3c..f6869d49e3ef4a0f9c85410c7b1f8e10477699ea 100644 --- a/src/main/java/neatlogic/module/cmdb/api/resourcecenter/config/SaveResourceEntityApi.java +++ b/src/main/java/neatlogic/module/cmdb/api/resourcecenter/config/SaveResourceEntityApi.java @@ -23,13 +23,15 @@ import neatlogic.framework.cmdb.dto.resourcecenter.config.ResourceEntityConfigVo import neatlogic.framework.cmdb.dto.resourcecenter.config.ResourceEntityVo; import neatlogic.framework.cmdb.dto.resourcecenter.config.SceneEntityVo; import neatlogic.framework.cmdb.enums.resourcecenter.Status; -import neatlogic.framework.cmdb.exception.resourcecenter.ResourceCenterResourceFoundException; +import neatlogic.framework.cmdb.exception.resourcecenter.ResourceEntityNameRepeatException; import neatlogic.framework.common.constvalue.ApiParamType; +import neatlogic.framework.dto.FieldValidResultVo; 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.IValid; import neatlogic.framework.restful.core.privateapi.PrivateApiComponentBase; import neatlogic.module.cmdb.dao.mapper.ci.CiMapper; import neatlogic.module.cmdb.dao.mapper.resourcecenter.ResourceEntityMapper; @@ -40,7 +42,6 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; -import java.util.Objects; /** * @author linbq @@ -82,43 +83,68 @@ public class SaveResourceEntityApi extends PrivateApiComponentBase { @Description(desc = "nmcarc.saveresourceentityapi.getname") @Override public Object myDoService(JSONObject paramObj) throws Exception { - ResourceEntityVo resourceEntityVo = paramObj.toJavaObject(ResourceEntityVo.class); + String name = paramObj.getString("name"); + String label = paramObj.getString("label"); + String configStr = paramObj.getString("config"); + String description = paramObj.getString("description"); + ResourceEntityVo resourceEntityVo = new ResourceEntityVo(); + resourceEntityVo.setName(name); + resourceEntityVo.setLabel(label); + resourceEntityVo.setConfigStr(configStr); + resourceEntityVo.setDescription(description); SceneEntityVo sceneEntityVo = ResourceEntityFactory.getSceneEntityByViewName(resourceEntityVo.getName()); - if (sceneEntityVo == null) { - throw new ResourceCenterResourceFoundException(resourceEntityVo.getName()); - } + ResourceEntityConfigVo config = resourceEntityVo.getConfig(); String mainCi = config.getMainCi(); if (StringUtils.isNotBlank(mainCi)) { CiVo mainCiVo = ciMapper.getCiByName(mainCi); if (mainCiVo != null) { resourceEntityVo.setCiId(mainCiVo.getId()); + resourceEntityVo.setCi(mainCiVo); } } - resourceEntityVo.setDescription(sceneEntityVo.getDescription()); - resourceEntityVo.setLabel(sceneEntityVo.getLabel()); - boolean configEquals = false; + if (sceneEntityVo != null) { + resourceEntityVo.setDescription(sceneEntityVo.getDescription()); + resourceEntityVo.setLabel(sceneEntityVo.getLabel()); + } +// boolean configEquals = false; ResourceEntityVo oldResourceEntityVo = resourceEntityMapper.getResourceEntityByName(resourceEntityVo.getName()); if (oldResourceEntityVo != null) { - configEquals = Objects.equals(resourceEntityVo.getConfigStr(), oldResourceEntityVo.getConfigStr()); - if (configEquals) { - return null; - } +// configEquals = Objects.equals(resourceEntityVo.getConfigStr(), oldResourceEntityVo.getConfigStr()); +// if (configEquals) { +// return null; +// } resourceEntityMapper.updateResourceEntityLabelAndDescription(resourceEntityVo); } else { resourceEntityVo.setStatus(Status.PENDING.getValue()); resourceEntityMapper.insertResourceEntity(resourceEntityVo); } - if (!configEquals) { - String error = resourceCenterResourceService.buildResourceView(resourceEntityVo.getName(), resourceEntityVo.getConfig()); - resourceEntityVo.setError(error); - if (StringUtils.isNotBlank(error)) { +// if (!configEquals) { + resourceEntityVo.setError(null); + String sql = resourceCenterResourceService.buildResourceView(resourceEntityVo); + if (StringUtils.isNotBlank(resourceEntityVo.getError())) { resourceEntityVo.setStatus(Status.ERROR.getValue()); } else { resourceEntityVo.setStatus(Status.READY.getValue()); } resourceEntityMapper.updateResourceEntityStatusAndError(resourceEntityVo); - } - return null; + return sql; +// } +// return null; + } + + public IValid name() { + return value -> { + String name = value.getString("name"); + SceneEntityVo sceneEntityVo = ResourceEntityFactory.getSceneEntityByViewName(name); + if (sceneEntityVo != null) { + return new FieldValidResultVo(new ResourceEntityNameRepeatException(name)); + } + ResourceEntityVo resourceEntityVo = resourceEntityMapper.getResourceEntityByName(name); + if (resourceEntityVo != null) { + return new FieldValidResultVo(new ResourceEntityNameRepeatException(name)); + } + return new FieldValidResultVo(); + }; } } diff --git a/src/main/java/neatlogic/module/cmdb/api/resourcecenter/config/getResourceEntityViewDataListApi.java b/src/main/java/neatlogic/module/cmdb/api/resourcecenter/config/getResourceEntityViewDataListApi.java index fe95451d66dc4c91caf614327b2b1fdd4e9ccd91..0d99d1bf2b6cc3c285d3d87aa92d0cbad795df24 100644 --- a/src/main/java/neatlogic/module/cmdb/api/resourcecenter/config/getResourceEntityViewDataListApi.java +++ b/src/main/java/neatlogic/module/cmdb/api/resourcecenter/config/getResourceEntityViewDataListApi.java @@ -5,6 +5,7 @@ import com.alibaba.fastjson.JSONObject; import neatlogic.framework.asynchronization.threadlocal.TenantContext; import neatlogic.framework.auth.core.AuthAction; import neatlogic.framework.cmdb.auth.label.CMDB; +import neatlogic.framework.cmdb.dto.resourcecenter.config.ResourceEntityConfigVo; import neatlogic.framework.common.constvalue.ApiParamType; import neatlogic.framework.common.dto.BasePageVo; import neatlogic.framework.dao.mapper.SchemaMapper; @@ -14,6 +15,8 @@ import neatlogic.framework.restful.core.privateapi.PrivateApiComponentBase; import neatlogic.framework.util.TableResultUtil; import neatlogic.module.cmdb.dao.mapper.resourcecenter.ResourceEntityMapper; import neatlogic.module.cmdb.utils.ResourceEntityFactory; +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; import javax.annotation.Resource; @@ -49,6 +52,18 @@ public class getResourceEntityViewDataListApi extends PrivateApiComponentBase { public Object myDoService(JSONObject paramObj) throws Exception { String name = paramObj.getString("name"); List fieldNameList = ResourceEntityFactory.getFieldNameListByViewName(name); + if (CollectionUtils.isEmpty(fieldNameList)) { + String config = resourceEntityMapper.getResourceEntityConfigByName(name); + if (StringUtils.isNotBlank(config)) { + ResourceEntityConfigVo resourceEntityConfigVo = JSONObject.parseObject(config, ResourceEntityConfigVo.class); + if (resourceEntityConfigVo != null) { + String sceneTemplateName = resourceEntityConfigVo.getSceneTemplateName(); + if (StringUtils.isNotBlank(sceneTemplateName)) { + fieldNameList = ResourceEntityFactory.getFieldNameListByViewName(sceneTemplateName); + } + } + } + } JSONArray theadList = new JSONArray(); List columnNameList = schemaMapper.getTableOrViewAllColumnNameList(TenantContext.get().getDataDbName(), name); for (String fieldName : fieldNameList) { diff --git a/src/main/java/neatlogic/module/cmdb/api/resourcecenter/resource/ResourceAccountListApi.java b/src/main/java/neatlogic/module/cmdb/api/resourcecenter/resource/ResourceAccountListApi.java index 78d0dc221d29447f4f01a2a62c74f9fee75e1063..442a62f5dcaf0cdc3c9afe2a30cb85441de096ee 100644 --- a/src/main/java/neatlogic/module/cmdb/api/resourcecenter/resource/ResourceAccountListApi.java +++ b/src/main/java/neatlogic/module/cmdb/api/resourcecenter/resource/ResourceAccountListApi.java @@ -72,7 +72,7 @@ public class ResourceAccountListApi extends PrivateApiComponentBase { @Override public Object myDoService(JSONObject paramObj) throws Exception { Long resourceId = paramObj.getLong("resourceId"); - if (resourceMapper.checkResourceIsExists(resourceId) == 0) { + if (resourceMapper.getResourceIdByResourceId(resourceId) == null) { throw new ResourceNotFoundException(resourceId); } String protocol = paramObj.getString("protocol"); diff --git a/src/main/java/neatlogic/module/cmdb/api/resourcecenter/resource/ResourceAccountSaveApi.java b/src/main/java/neatlogic/module/cmdb/api/resourcecenter/resource/ResourceAccountSaveApi.java index 743993821382d957febdf2d913e96f6fe1e8223f..d9f73e7fd72b27d959374dd1a6a447092073f666 100644 --- a/src/main/java/neatlogic/module/cmdb/api/resourcecenter/resource/ResourceAccountSaveApi.java +++ b/src/main/java/neatlogic/module/cmdb/api/resourcecenter/resource/ResourceAccountSaveApi.java @@ -82,7 +82,7 @@ public class ResourceAccountSaveApi extends PrivateApiComponentBase { int successCount = 0; List failureReasonList = new ArrayList<>(); Long resourceId = paramObj.getLong("resourceId"); - if (resourceMapper.checkResourceIsExists(resourceId) == 0) { + if (resourceMapper.getResourceIdByResourceId(resourceId) == null) { throw new ResourceNotFoundException(resourceId); } // 查询该资产绑定的公有账号列表,再根据账号ID解绑 diff --git a/src/main/java/neatlogic/module/cmdb/api/resourcecenter/resource/ResourceListApi.java b/src/main/java/neatlogic/module/cmdb/api/resourcecenter/resource/ResourceListApi.java index 35725e357fb79f484c29e542caf424dde080fc51..12cdf650d05899d4e2009154e0fde5907be94cc3 100644 --- a/src/main/java/neatlogic/module/cmdb/api/resourcecenter/resource/ResourceListApi.java +++ b/src/main/java/neatlogic/module/cmdb/api/resourcecenter/resource/ResourceListApi.java @@ -22,24 +22,25 @@ import neatlogic.framework.cmdb.auth.label.CMDB; import neatlogic.framework.cmdb.dto.resourcecenter.ResourceSearchVo; import neatlogic.framework.cmdb.dto.resourcecenter.ResourceVo; import neatlogic.framework.cmdb.enums.group.GroupType; +import neatlogic.framework.cmdb.resourcecenter.datasource.core.IResourceCenterDataSource; +import neatlogic.framework.cmdb.resourcecenter.datasource.core.ResourceCenterDataSourceFactory; import neatlogic.framework.common.constvalue.ApiParamType; import neatlogic.framework.common.dto.BasePageVo; import neatlogic.framework.restful.annotation.*; import neatlogic.framework.restful.constvalue.OperationTypeEnum; import neatlogic.framework.restful.core.privateapi.PrivateApiComponentBase; import neatlogic.framework.util.TableResultUtil; -import neatlogic.module.cmdb.dao.mapper.ci.AttrMapper; -import neatlogic.module.cmdb.dao.mapper.ci.CiMapper; -import neatlogic.module.cmdb.dao.mapper.resourcecenter.ResourceEntityMapper; -import neatlogic.module.cmdb.dao.mapper.resourcecenter.ResourceMapper; import neatlogic.module.cmdb.service.ci.CiAuthChecker; import neatlogic.module.cmdb.service.resourcecenter.resource.IResourceCenterResourceService; import org.apache.commons.collections4.CollectionUtils; -import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; import javax.annotation.Resource; -import java.util.*; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; /** * 查询资源中心数据列表接口 @@ -55,18 +56,6 @@ public class ResourceListApi extends PrivateApiComponentBase { @Resource private IResourceCenterResourceService resourceCenterResourceService; - @Resource - private ResourceMapper resourceMapper; - - @Resource - private ResourceEntityMapper resourceEntityMapper; - - @Resource - private AttrMapper attrMapper; - - @Resource - private CiMapper ciMapper; - @Override public String getToken() { return "resourcecenter/resource/list"; @@ -115,8 +104,6 @@ public class ResourceListApi extends PrivateApiComponentBase { @Description(desc = "nmcarr.resourcelistapi.getname") @Override public Object myDoService(JSONObject jsonObj) throws Exception { - List resourceList = new ArrayList<>(); - List resultList = new ArrayList<>(); ResourceSearchVo searchVo; JSONArray defaultValue = jsonObj.getJSONArray("defaultValue"); if (CollectionUtils.isNotEmpty(defaultValue)) { @@ -127,41 +114,15 @@ public class ResourceListApi extends PrivateApiComponentBase { } resourceCenterResourceService.handleBatchSearchList(searchVo); resourceCenterResourceService.setIpFieldAttrIdAndNameFieldAttrId(searchVo); - if (Objects.equals(searchVo.getRowNum(), 0)) { - int rowNum = 0; - if (noFilterCondition(searchVo)) { - rowNum = resourceMapper.getAllResourceCount(searchVo); - } else { - rowNum = resourceMapper.getResourceCount(searchVo); - } - if (rowNum == 0) { - return TableResultUtil.getResult(resourceList, searchVo); - } - searchVo.setRowNum(rowNum); - } - resourceCenterResourceService.setIsIpFieldSortAndIsNameFieldSort(searchVo); - List idList = resourceMapper.getResourceIdList(searchVo); - if (CollectionUtils.isEmpty(idList)) { - return TableResultUtil.getResult(resourceList, searchVo); - } - resourceList = resourceMapper.getResourceListByIdList(idList); - if (CollectionUtils.isNotEmpty(resourceList)) { - resourceCenterResourceService.addTagAndAccountInformation(resourceList); + IResourceCenterDataSource resourceCenterDataSource = ResourceCenterDataSourceFactory.getResourceCenterDataSource(); + List resultList = resourceCenterDataSource.getResourceList(searchVo); + if (CollectionUtils.isNotEmpty(resultList)) { + resourceCenterResourceService.addTagAndAccountInformation(resultList); } - Set typeIdList = new HashSet<>(); + Set typeIdList = resultList.stream().map(ResourceVo::getTypeId).collect(Collectors.toSet()); List canDeleteTypeIdList = new ArrayList<>(); List canEditTypeIdList = new ArrayList<>(); - //排序 - for (Long id : idList) { - for (ResourceVo resourceVo : resourceList) { - if (Objects.equals(id, resourceVo.getId())) { - resultList.add(resourceVo); - typeIdList.add(resourceVo.getTypeId()); - break; - } - } - } //补充配置项权限 Set withoutCiAuthCiEntityList = new HashSet<>(); @@ -197,55 +158,4 @@ public class ResourceListApi extends PrivateApiComponentBase { return TableResultUtil.getResult(resultList, searchVo); } - /** - * 判断是否有过滤条件 - * @param searchVo - * @return - */ - private boolean noFilterCondition(ResourceSearchVo searchVo) { - if (StringUtils.isNotBlank(searchVo.getKeyword())) { - return false; - } - if (CollectionUtils.isNotEmpty(searchVo.getBatchSearchList())) { - return false; - } - if (CollectionUtils.isNotEmpty(searchVo.getStateIdList())) { - return false; - } - if (CollectionUtils.isNotEmpty(searchVo.getVendorIdList())) { - return false; - } - if (CollectionUtils.isNotEmpty(searchVo.getEnvIdList())) { - return false; - } - if (searchVo.getExistNoEnv()) { - return false; - } - if (CollectionUtils.isNotEmpty(searchVo.getAppSystemIdList())) { - return false; - } - if (CollectionUtils.isNotEmpty(searchVo.getAppModuleIdList())) { - return false; - } - if (CollectionUtils.isNotEmpty(searchVo.getDefaultValue())) { - return false; - } - if (CollectionUtils.isNotEmpty(searchVo.getIdList())) { - return false; - } - if (CollectionUtils.isNotEmpty(searchVo.getInspectStatusList())) { - return false; - } - if (CollectionUtils.isNotEmpty(searchVo.getProtocolIdList())) { - return false; - } - if (CollectionUtils.isNotEmpty(searchVo.getTagIdList())) { - return false; - } - if (CollectionUtils.isNotEmpty(searchVo.getInspectJobPhaseNodeStatusList())) { - return false; - } - return true; - } - } diff --git a/src/main/java/neatlogic/module/cmdb/api/resourcecenter/resource/ResourceTagSaveApi.java b/src/main/java/neatlogic/module/cmdb/api/resourcecenter/resource/ResourceTagSaveApi.java index 0a7378ba563f5b2de1f6834a3f906cbbad07f738..15d9bd94cbde7486a144c410f03c7e64fc98d763 100644 --- a/src/main/java/neatlogic/module/cmdb/api/resourcecenter/resource/ResourceTagSaveApi.java +++ b/src/main/java/neatlogic/module/cmdb/api/resourcecenter/resource/ResourceTagSaveApi.java @@ -83,7 +83,7 @@ public class ResourceTagSaveApi extends PrivateApiComponentBase { if (CollectionUtils.isEmpty(tagArray)) { return null; } - if (resourceMapper.checkResourceIsExists(resourceId) == 0) { + if (resourceMapper.getResourceIdByResourceId(resourceId) == null) { throw new ResourceNotFoundException(resourceId); } List tagList = tagArray.toJavaList(String.class); diff --git a/src/main/java/neatlogic/module/cmdb/api/resourcecenter/resourcetype/SaveResourceTypeApi.java b/src/main/java/neatlogic/module/cmdb/api/resourcecenter/resourcetype/ResourceTypeListTreeApi.java similarity index 47% rename from src/main/java/neatlogic/module/cmdb/api/resourcecenter/resourcetype/SaveResourceTypeApi.java rename to src/main/java/neatlogic/module/cmdb/api/resourcecenter/resourcetype/ResourceTypeListTreeApi.java index bce9bfbcd784f27e75c4d3ffd984dd481f16c2b9..1f19008a75c3cd0a8c6839dfe6f41de5cc8f0367 100644 --- a/src/main/java/neatlogic/module/cmdb/api/resourcecenter/resourcetype/SaveResourceTypeApi.java +++ b/src/main/java/neatlogic/module/cmdb/api/resourcecenter/resourcetype/ResourceTypeListTreeApi.java @@ -1,4 +1,4 @@ -/*Copyright (C) 2024 深圳极向量科技有限公司 All Rights Reserved. +/*Copyright (C) $today.year 深圳极向量科技有限公司 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 @@ -18,61 +18,56 @@ package neatlogic.module.cmdb.api.resourcecenter.resourcetype; import com.alibaba.fastjson.JSONObject; import neatlogic.framework.auth.core.AuthAction; import neatlogic.framework.cmdb.auth.label.CMDB; -import neatlogic.framework.cmdb.dto.ci.CiVo; -import neatlogic.framework.cmdb.exception.ci.CiNotFoundException; +import neatlogic.framework.cmdb.dto.resourcecenter.ResourceTypeVo; +import neatlogic.framework.cmdb.resourcecenter.datasource.core.IResourceCenterDataSource; +import neatlogic.framework.cmdb.resourcecenter.datasource.core.ResourceCenterDataSourceFactory; import neatlogic.framework.common.constvalue.ApiParamType; import neatlogic.framework.restful.annotation.*; import neatlogic.framework.restful.constvalue.OperationTypeEnum; import neatlogic.framework.restful.core.privateapi.PrivateApiComponentBase; -import neatlogic.module.cmdb.dao.mapper.ci.CiMapper; -import neatlogic.module.cmdb.dao.mapper.resourcecenter.ResourceEntityMapper; -import org.apache.commons.collections4.CollectionUtils; +import neatlogic.framework.util.TableResultUtil; import org.springframework.stereotype.Service; -import javax.annotation.Resource; import java.util.List; +/** + * 查询资源类型树列表接口 + * + * @author linbq + * @since 2021/5/27 16:14 + **/ @Service @AuthAction(action = CMDB.class) -@OperationType(type = OperationTypeEnum.UPDATE) -public class SaveResourceTypeApi extends PrivateApiComponentBase { +@OperationType(type = OperationTypeEnum.SEARCH) +public class ResourceTypeListTreeApi extends PrivateApiComponentBase { - @Resource - private ResourceEntityMapper resourceEntityMapper; - - @Resource - private CiMapper ciMapper; + @Override + public String getToken() { + return "resourcecenter/resourcetype/listtree"; + } @Override public String getName() { - return "nmcarr.saveresourcetypeapi.getname"; + return "查询所有资产类型列表树"; } - @Input({ - @Param(name = "ciId", type = ApiParamType.LONG, isRequired = true, desc = "term.cmdb.ciid") - }) - @Output({}) - @Description(desc = "nmcarr.saveresourcetypeapi.getname") @Override - public Object myDoService(JSONObject paramObj) throws Exception { - Long ciId = paramObj.getLong("ciId"); - CiVo ciVo = ciMapper.getCiById(ciId); - if (ciVo == null) { - throw new CiNotFoundException(ciId); - } - List ciIdList = resourceEntityMapper.getAllResourceTypeCiIdList(); - if (ciIdList.contains(ciId)) { - return null; - } - if (CollectionUtils.isNotEmpty(ciIdList)) { - resourceEntityMapper.deleteResourceTypeCi(); - } - resourceEntityMapper.insertResourceTypeCi(ciId); + public String getConfig() { return null; } + @Input({ + @Param(name = "keyword", type = ApiParamType.STRING, desc = "common.keyword") + }) + @Output({ + @Param(explode = ResourceTypeVo[].class, desc = "common.tbodylist") + }) + @Description(desc = "nmcarr.resourcetypetreeapi.getname") @Override - public String getToken() { - return "resourcecenter/resourcetype/save"; + public Object myDoService(JSONObject jsonObj) throws Exception { + String keyword = jsonObj.getString("keyword"); + IResourceCenterDataSource resourceCenterDataSource = ResourceCenterDataSourceFactory.getResourceCenterDataSource(); + List tbodyList = resourceCenterDataSource.getResourceTypeListTree(keyword); + return TableResultUtil.getResult(tbodyList); } } diff --git a/src/main/java/neatlogic/module/cmdb/api/resourcecenter/resourcetype/ResourceTypeTreeApi.java b/src/main/java/neatlogic/module/cmdb/api/resourcecenter/resourcetype/ResourceTypeTreeApi.java index 77de6aa69a3793e6d337c2356dfae830011c7fe9..731a648548cbb83d6f96e65cae15930fd35dda63 100644 --- a/src/main/java/neatlogic/module/cmdb/api/resourcecenter/resourcetype/ResourceTypeTreeApi.java +++ b/src/main/java/neatlogic/module/cmdb/api/resourcecenter/resourcetype/ResourceTypeTreeApi.java @@ -16,28 +16,17 @@ along with this program. If not, see .*/ package neatlogic.module.cmdb.api.resourcecenter.resourcetype; import com.alibaba.fastjson.JSONObject; -import neatlogic.framework.asynchronization.threadlocal.UserContext; import neatlogic.framework.auth.core.AuthAction; import neatlogic.framework.cmdb.auth.label.CMDB; -import neatlogic.framework.cmdb.dto.ci.CiVo; -import neatlogic.framework.cmdb.dto.resourcecenter.ResourceSearchVo; import neatlogic.framework.cmdb.dto.resourcecenter.ResourceTypeVo; +import neatlogic.framework.cmdb.resourcecenter.datasource.core.IResourceCenterDataSource; +import neatlogic.framework.cmdb.resourcecenter.datasource.core.ResourceCenterDataSourceFactory; import neatlogic.framework.common.constvalue.ApiParamType; import neatlogic.framework.restful.annotation.*; import neatlogic.framework.restful.constvalue.OperationTypeEnum; import neatlogic.framework.restful.core.privateapi.PrivateApiComponentBase; -import neatlogic.module.cmdb.dao.mapper.ci.CiMapper; -import neatlogic.module.cmdb.dao.mapper.resourcecenter.ResourceEntityMapper; -import neatlogic.module.cmdb.dao.mapper.resourcecenter.ResourceMapper; -import neatlogic.module.cmdb.service.resourcecenter.resource.IResourceCenterResourceService; -import org.apache.commons.collections4.CollectionUtils; -import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; -import javax.annotation.Resource; -import java.util.*; -import java.util.stream.Collectors; - /** * 查询资源类型树列表接口 * @@ -49,18 +38,6 @@ import java.util.stream.Collectors; @OperationType(type = OperationTypeEnum.SEARCH) public class ResourceTypeTreeApi extends PrivateApiComponentBase { - @Resource - private CiMapper ciMapper; - - @Resource - private ResourceEntityMapper resourceEntityMapper; - - @Resource - private ResourceMapper resourceMapper; - - @Resource - private IResourceCenterResourceService resourceCenterResourceService; - @Override public String getToken() { return "resourcecenter/resourcetype/tree"; @@ -86,113 +63,7 @@ public class ResourceTypeTreeApi extends PrivateApiComponentBase { @Override public Object myDoService(JSONObject jsonObj) throws Exception { String keyword = jsonObj.getString("keyword"); - if (StringUtils.isNotBlank(keyword)) { - keyword = keyword.toLowerCase(); - } - List resultList = new ArrayList<>(); - List authCiVoList = new ArrayList<>(); - List ciIdList = resourceEntityMapper.getAllResourceTypeCiIdList(); - jsonObj.put("typeIdList", ciIdList); - ResourceSearchVo searchVo = resourceCenterResourceService.assembleResourceSearchVo(jsonObj, false); - //先找出所有有权限的配置项的模型idList - if (!searchVo.getIsHasAuth()) { - Set authCiIdList = ciMapper.getAllAuthCi(UserContext.get().getAuthenticationInfoVo()).stream().map(CiVo::getId).collect(Collectors.toSet()); - authCiIdList.addAll(resourceMapper.getResourceTypeIdListByAuth(searchVo)); - if (CollectionUtils.isEmpty(authCiIdList)) { - return resultList; - } - authCiVoList = ciMapper.getCiByIdList(new ArrayList<>(authCiIdList)); - } - - - if (CollectionUtils.isNotEmpty(ciIdList)) { - List ciVoList = ciMapper.getCiByIdList(ciIdList); - ciVoList.sort(Comparator.comparing(CiVo::getLft)); - for (CiVo ciVo : ciVoList) { - Set ciList = new HashSet<>(); - List ciListTmp = ciMapper.getDownwardCiListByLR(ciVo.getLft(), ciVo.getRht()); - //过滤出所有有权限的配置项的模型idList - if (!searchVo.getIsHasAuth()) { - if (CollectionUtils.isNotEmpty(authCiVoList) && CollectionUtils.isNotEmpty(ciListTmp)) { - for (CiVo ci : ciListTmp) { - for (CiVo authCi : authCiVoList) { - if (ci.getLft() <= authCi.getLft() && ci.getRht() >= authCi.getRht()) { - ciList.add(ci); - break; - } - } - } - } - } else { - ciList = new HashSet<>(ciListTmp); - } - int size = ciList.size(); - List resourceTypeVoList = new ArrayList<>(size); - Map resourceTypeMap = new HashMap<>(size); - for (CiVo ci : ciList) { - ResourceTypeVo resourceTypeVo = new ResourceTypeVo(ci.getId(), ci.getParentCiId(), ci.getLabel(), ci.getName()); - resourceTypeMap.put(resourceTypeVo.getId(), resourceTypeVo); - resourceTypeVoList.add(resourceTypeVo); - } - if (StringUtils.isNotBlank(keyword)) { - // 建立父子关系 - for (ResourceTypeVo resourceType : resourceTypeVoList) { - if (resourceType.getParentId() != null) { - ResourceTypeVo parentResourceType = resourceTypeMap.get(resourceType.getParentId()); - if (parentResourceType != null) { - resourceType.setParent(parentResourceType); - parentResourceType.addChild(resourceType); - } - } - } - // 判断节点名称是否与关键字keyword匹配,如果匹配就将该节点及其父子节点的isKeywordMatch字段值设置为1,否则设置为0。 - for (ResourceTypeVo resourceType : resourceTypeVoList) { - if (resourceType.getLabel().toLowerCase().contains(keyword)) { - if (resourceType.getIsKeywordMatch() == null) { - resourceType.setIsKeywordMatch(1); - resourceType.setUpwardIsKeywordMatch(1); - resourceType.setDownwardIsKeywordMatch(1); - } - } else { - if (resourceType.getIsKeywordMatch() == null) { - resourceType.setIsKeywordMatch(0); - } - } - } - // 将isKeywordMatch字段值为0的节点从其父级中移除。 - Iterator iterator = resourceTypeVoList.iterator(); - while (iterator.hasNext()) { - ResourceTypeVo resourceType = iterator.next(); - if (Objects.equals(resourceType.getIsKeywordMatch(), 0)) { - ResourceTypeVo parent = resourceType.getParent(); - if (parent != null) { - parent.removeChild(resourceType); - } - iterator.remove(); - } - } - } else { - for (ResourceTypeVo resourceType : resourceTypeVoList) { - if (resourceType.getParentId() != null) { - ResourceTypeVo parentResourceType = resourceTypeMap.get(resourceType.getParentId()); - if (parentResourceType != null) { - parentResourceType.addChild(resourceType); - } - } - } - } - for (ResourceTypeVo resourceType : resourceTypeVoList) { - if (resourceType.getParentId() != null) { - ResourceTypeVo parentResourceType = resourceTypeMap.get(resourceType.getParentId()); - if (parentResourceType == null) { - resultList.add(resourceType); - } - } else { - resultList.add(resourceType); - } - } - } - } - return resultList; + IResourceCenterDataSource resourceCenterDataSource = ResourceCenterDataSourceFactory.getResourceCenterDataSource(); + return resourceCenterDataSource.getResourceTypeTree(keyword); } } diff --git a/src/main/java/neatlogic/module/cmdb/api/resourcecenter/state/ListStateForSelectApi.java b/src/main/java/neatlogic/module/cmdb/api/resourcecenter/state/ListStateForSelectApi.java index 03bc5876ff3b4ceea55cf10c3b89dbaf252c7b72..96db7d21995b23e5c622a28fee2bb729edb7c0a7 100644 --- a/src/main/java/neatlogic/module/cmdb/api/resourcecenter/state/ListStateForSelectApi.java +++ b/src/main/java/neatlogic/module/cmdb/api/resourcecenter/state/ListStateForSelectApi.java @@ -15,23 +15,20 @@ along with this program. If not, see .*/ package neatlogic.module.cmdb.api.resourcecenter.state; -import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import neatlogic.framework.auth.core.AuthAction; import neatlogic.framework.cmdb.auth.label.CMDB; import neatlogic.framework.cmdb.dto.resourcecenter.ResourceVo; +import neatlogic.framework.cmdb.resourcecenter.datasource.core.IResourceCenterDataSource; +import neatlogic.framework.cmdb.resourcecenter.datasource.core.ResourceCenterDataSourceFactory; import neatlogic.framework.common.constvalue.ApiParamType; import neatlogic.framework.common.dto.BasePageVo; import neatlogic.framework.restful.annotation.*; import neatlogic.framework.restful.constvalue.OperationTypeEnum; import neatlogic.framework.restful.core.privateapi.PrivateApiComponentBase; import neatlogic.framework.util.TableResultUtil; -import neatlogic.module.cmdb.dao.mapper.resourcecenter.ResourceMapper; -import org.apache.commons.collections4.CollectionUtils; import org.springframework.stereotype.Service; -import javax.annotation.Resource; -import java.util.ArrayList; import java.util.List; @Service @@ -39,9 +36,6 @@ import java.util.List; @OperationType(type = OperationTypeEnum.SEARCH) public class ListStateForSelectApi extends PrivateApiComponentBase { - @Resource - private ResourceMapper resourceMapper; - @Override public String getToken() { return "resourcecenter/state/list/forselect"; @@ -71,32 +65,8 @@ public class ListStateForSelectApi extends PrivateApiComponentBase { @Override public Object myDoService(JSONObject paramObj) throws Exception { BasePageVo searchVo = paramObj.toJavaObject(BasePageVo.class); - JSONArray defaultValue = searchVo.getDefaultValue(); - if (CollectionUtils.isNotEmpty(defaultValue)) { - List idList = defaultValue.toJavaList(Long.class); - List resourceList = resourceMapper.searchStateListByIdList(idList); - return TableResultUtil.getResult(resourceList); - } else { - int rowNum = resourceMapper.searchStateCount(searchVo); - if (rowNum > 0) { - searchVo.setRowNum(rowNum); - if (searchVo.getNeedPage()) { - List idList = resourceMapper.searchStateIdList(searchVo); - List resourceList = resourceMapper.searchStateListByIdList(idList); - return TableResultUtil.getResult(resourceList, searchVo); - } else { - List allResourceList = new ArrayList<>(); - int pageCount = searchVo.getPageCount(); - for (int currentPage = 1; currentPage <= pageCount; currentPage++) { - searchVo.setCurrentPage(currentPage); - List idList = resourceMapper.searchStateIdList(searchVo); - List resourceList = resourceMapper.searchStateListByIdList(idList); - allResourceList.addAll(resourceList); - } - return TableResultUtil.getResult(allResourceList, searchVo); - } - } - } - return null; + IResourceCenterDataSource resourceCenterDataSource = ResourceCenterDataSourceFactory.getResourceCenterDataSource(); + List tbodyList = resourceCenterDataSource.getStateListForSelect(searchVo); + return TableResultUtil.getResult(tbodyList, searchVo); } } diff --git a/src/main/java/neatlogic/module/cmdb/api/resourcecenter/vendor/ListVendorForSelectApi.java b/src/main/java/neatlogic/module/cmdb/api/resourcecenter/vendor/ListVendorForSelectApi.java index b5989ec8bd6b19d9755d7eee2b2771c3d277931e..80d7422a1fbf3af2d520df1af3c131c8738fa4d6 100644 --- a/src/main/java/neatlogic/module/cmdb/api/resourcecenter/vendor/ListVendorForSelectApi.java +++ b/src/main/java/neatlogic/module/cmdb/api/resourcecenter/vendor/ListVendorForSelectApi.java @@ -15,23 +15,20 @@ along with this program. If not, see .*/ package neatlogic.module.cmdb.api.resourcecenter.vendor; -import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import neatlogic.framework.auth.core.AuthAction; import neatlogic.framework.cmdb.auth.label.CMDB; import neatlogic.framework.cmdb.dto.resourcecenter.ResourceVo; +import neatlogic.framework.cmdb.resourcecenter.datasource.core.IResourceCenterDataSource; +import neatlogic.framework.cmdb.resourcecenter.datasource.core.ResourceCenterDataSourceFactory; import neatlogic.framework.common.constvalue.ApiParamType; import neatlogic.framework.common.dto.BasePageVo; import neatlogic.framework.restful.annotation.*; import neatlogic.framework.restful.constvalue.OperationTypeEnum; import neatlogic.framework.restful.core.privateapi.PrivateApiComponentBase; import neatlogic.framework.util.TableResultUtil; -import neatlogic.module.cmdb.dao.mapper.resourcecenter.ResourceMapper; -import org.apache.commons.collections4.CollectionUtils; import org.springframework.stereotype.Service; -import javax.annotation.Resource; -import java.util.ArrayList; import java.util.List; @Service @@ -39,9 +36,6 @@ import java.util.List; @OperationType(type = OperationTypeEnum.SEARCH) public class ListVendorForSelectApi extends PrivateApiComponentBase { - @Resource - private ResourceMapper resourceMapper; - @Override public String getToken() { return "resourcecenter/vendor/list/forselect"; @@ -71,32 +65,8 @@ public class ListVendorForSelectApi extends PrivateApiComponentBase { @Override public Object myDoService(JSONObject paramObj) throws Exception { BasePageVo searchVo = paramObj.toJavaObject(BasePageVo.class); - JSONArray defaultValue = searchVo.getDefaultValue(); - if (CollectionUtils.isNotEmpty(defaultValue)) { - List idList = defaultValue.toJavaList(Long.class); - List resourceList = resourceMapper.searchVendorListByIdList(idList); - return TableResultUtil.getResult(resourceList); - } else { - int rowNum = resourceMapper.searchVendorCount(searchVo); - if (rowNum > 0) { - searchVo.setRowNum(rowNum); - if (searchVo.getNeedPage()) { - List idList = resourceMapper.searchVendorIdList(searchVo); - List resourceList = resourceMapper.searchVendorListByIdList(idList); - return TableResultUtil.getResult(resourceList, searchVo); - } else { - List allResourceList = new ArrayList<>(); - int pageCount = searchVo.getPageCount(); - for (int currentPage = 1; currentPage <= pageCount; currentPage++) { - searchVo.setCurrentPage(currentPage); - List idList = resourceMapper.searchVendorIdList(searchVo); - List resourceList = resourceMapper.searchVendorListByIdList(idList); - allResourceList.addAll(resourceList); - } - return TableResultUtil.getResult(allResourceList, searchVo); - } - } - } - return null; + IResourceCenterDataSource resourceCenterDataSource = ResourceCenterDataSourceFactory.getResourceCenterDataSource(); + List tbodyList = resourceCenterDataSource.getVendorListForSelect(searchVo); + return TableResultUtil.getResult(tbodyList, searchVo); } } diff --git a/src/main/java/neatlogic/module/cmdb/dao/mapper/resourcecenter/AppSystemMapper.java b/src/main/java/neatlogic/module/cmdb/dao/mapper/resourcecenter/AppSystemMapper.java index 8bf9f30898a7b540d7ed4204c8fb02f66b7b35fe..a885dda78d44bdd20056fef95c2309306f047cb8 100644 --- a/src/main/java/neatlogic/module/cmdb/dao/mapper/resourcecenter/AppSystemMapper.java +++ b/src/main/java/neatlogic/module/cmdb/dao/mapper/resourcecenter/AppSystemMapper.java @@ -16,10 +16,8 @@ along with this program. If not, see .*/ package neatlogic.module.cmdb.dao.mapper.resourcecenter; import neatlogic.framework.cmdb.crossover.IAppSystemMapper; -import neatlogic.framework.cmdb.dto.resourcecenter.entity.AppEnvironmentVo; import neatlogic.framework.cmdb.dto.resourcecenter.entity.AppModuleVo; import neatlogic.framework.cmdb.dto.resourcecenter.entity.AppSystemVo; -import org.apache.ibatis.annotations.Param; import java.util.List; @@ -36,7 +34,4 @@ public interface AppSystemMapper extends IAppSystemMapper { AppModuleVo getAppModuleById(Long id); List getAppModuleListByIdList(List idList); - - List getAppEnvListByAppSystemIdAndModuleIdList(@Param("appSystemId") Long appSystemId, @Param("appModuleIdList") List appModuleIdList); - } diff --git a/src/main/java/neatlogic/module/cmdb/dao/mapper/resourcecenter/AppSystemMapper.xml b/src/main/java/neatlogic/module/cmdb/dao/mapper/resourcecenter/AppSystemMapper.xml index 8bcbb70121db0ec2326dba605ae715716f244885..758ccd4e5b076023cabd8e57a767464ba33258a7 100644 --- a/src/main/java/neatlogic/module/cmdb/dao/mapper/resourcecenter/AppSystemMapper.xml +++ b/src/main/java/neatlogic/module/cmdb/dao/mapper/resourcecenter/AppSystemMapper.xml @@ -58,32 +58,4 @@ along with this program. If not, see .--> #{id} - - diff --git a/src/main/java/neatlogic/module/cmdb/dao/mapper/resourcecenter/ResourceEntityMapper.java b/src/main/java/neatlogic/module/cmdb/dao/mapper/resourcecenter/ResourceEntityMapper.java index 6ff88f4ade1d3fa20678d622ecb2b03c3f8238a0..0e09d979c9240d6ab96f17be59831348f9b5c436 100644 --- a/src/main/java/neatlogic/module/cmdb/dao/mapper/resourcecenter/ResourceEntityMapper.java +++ b/src/main/java/neatlogic/module/cmdb/dao/mapper/resourcecenter/ResourceEntityMapper.java @@ -16,6 +16,8 @@ along with this program. If not, see .*/ package neatlogic.module.cmdb.dao.mapper.resourcecenter; import neatlogic.framework.cmdb.crossover.IResourceEntityCrossoverMapper; +import neatlogic.framework.cmdb.dto.resourcecenter.ApplicationListDisplayVo; +import neatlogic.framework.cmdb.dto.resourcecenter.AssetListDisplayVo; import neatlogic.framework.cmdb.dto.resourcecenter.ResourceSearchVo; import neatlogic.framework.cmdb.dto.resourcecenter.config.ResourceEntityVo; import org.apache.ibatis.annotations.Param; @@ -28,6 +30,8 @@ public interface ResourceEntityMapper extends IResourceEntityCrossoverMapper { List getResourceEntityListByNameList(List nameList); + List getResourceEntityList(); + String getResourceEntityConfigByName(String name); List getAllResourceTypeCiIdList(ResourceSearchVo searchVo); @@ -36,10 +40,18 @@ public interface ResourceEntityMapper extends IResourceEntityCrossoverMapper { List> getResourceEntityViewDataList(@Param("name") String name, @Param("startNum") int startNum, @Param("pageSize") int pageSize); + ApplicationListDisplayVo getApplicationListDisplay(); + + AssetListDisplayVo getAssetListDisplay(); + void insertResourceEntity(ResourceEntityVo resourceEntityVo); void insertResourceTypeCi(Long ciId); + int insertApplicationListDisplay(ApplicationListDisplayVo applicationListDisplayVo); + + int insertAssetListDisplay(AssetListDisplayVo assetListDisplayVo); + void updateResourceEntity(ResourceEntityVo resourceEntityVo); void updateResourceEntityStatusAndError(ResourceEntityVo resourceEntityVo); diff --git a/src/main/java/neatlogic/module/cmdb/dao/mapper/resourcecenter/ResourceEntityMapper.xml b/src/main/java/neatlogic/module/cmdb/dao/mapper/resourcecenter/ResourceEntityMapper.xml index ce866d5724c41b9671b6ec687083aa084ee25643..b5a64abbad8bf8d9e44f09534a742695237eb487 100644 --- a/src/main/java/neatlogic/module/cmdb/dao/mapper/resourcecenter/ResourceEntityMapper.xml +++ b/src/main/java/neatlogic/module/cmdb/dao/mapper/resourcecenter/ResourceEntityMapper.xml @@ -32,6 +32,12 @@ along with this program. If not, see .--> + + + + + + INSERT INTO cmdb_resourcecenter_entity (`name`, `label`, `status`, `error`, `init_time`, `ci_id`, `config`, `description`) - VALUES (#{name}, #{label}, #{status}, #{error}, NOW(3), #{ciId}, #{xml}, #{configStr} #{description}) + VALUES (#{name}, #{label}, #{status}, #{error}, NOW(3), #{ciId}, #{configStr}, #{description}) ON DUPLICATE KEY UPDATE `label` = #{label}, @@ -82,6 +111,43 @@ along with this program. If not, see .--> VALUES (#{value}) + + INSERT INTO `cmdb_resourcecenter_applicationlist_display` ( + `id`, + `app_system_id`, + `app_module_id`, + `env_id`, + `config` + ) + VALUES + ( + #{id}, + #{appSystemId}, + #{appModuleId}, + #{envId}, + #{configStr, typeHandler=CompressHandler} + ) + ON DUPLICATE KEY UPDATE + `config` = #{configStr, typeHandler=CompressHandler} + + + + INSERT INTO `cmdb_resourcecenter_assetlist_display` ( + `id`, + `root_ci_name`, + `config` + ) + VALUES + ( + #{id}, + #{rootCiName}, + #{configStr, typeHandler=CompressHandler} + ) + ON DUPLICATE KEY UPDATE + `root_ci_name` = #{rootCiName}, + `config` = #{configStr, typeHandler=CompressHandler} + + UPDATE cmdb_resourcecenter_entity 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 e46a2e0beabf99ffa83ff2d845a40bd7ab979e12..9885f27a3fc037042b2c2aae9f812b70dd79f227 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 @@ -46,28 +46,14 @@ public interface ResourceMapper extends IResourceCrossoverMapper { List getResourceListByIdList(List idList); - int getIpObjectResourceCountByAppSystemIdAndAppModuleIdAndEnvIdAndTypeId(ResourceSearchVo searchVo); + int getAppResourceCount(ResourceSearchVo searchVo); - List getIpObjectResourceIdListByAppSystemIdAndAppModuleIdAndEnvIdAndTypeId(ResourceSearchVo searchVo); + List getAppResourceIdList(ResourceSearchVo searchVo); - int getOsResourceCountByAppSystemIdAndAppModuleIdAndEnvIdAndTypeId(ResourceSearchVo searchVo); - - List getOsResourceIdListByAppSystemIdAndAppModuleIdAndEnvIdAndTypeId(ResourceSearchVo searchVo); - - int getOsResourceCountByAppSystemIdAndAppModuleIdListAndEnvIdAndTypeId(ResourceSearchVo searchVo); - - List getOsResourceIdListByAppSystemIdAndAppModuleIdListAndEnvIdAndTypeId(ResourceSearchVo searchVo); - - List getAppInstanceResourceListByIdList(@Param("idList") List idList); - - List getAppInstanceResourceListByIdListAndKeyword(@Param("idList") List idList, @Param("keyword") String keyword); + List getAppResourceListByIdList(ResourceSearchVo searchVo); List getAppInstanceResourceListByIdListSimple(List idList); - List getDbInstanceResourceListByIdList(List idList); - - List getOsResourceListByIdList(List idList); - Long getResourceIdByIpAndPortAndName(ResourceSearchVo searchVo); Long getResourceIdByIpAndPortAndNameWithFilter(ResourceSearchVo searchVo); @@ -80,12 +66,12 @@ public interface ResourceMapper extends IResourceCrossoverMapper { int checkResourceIsExists(Long id); + Long getResourceIdByResourceId(Long id); + List checkResourceIdListIsExists(List idList); List getHasModuleAppSystemIdListByAppSystemIdList(@Param("appSystemIdList") List appSystemIdList); - List getHasEnvAppSystemIdListByAppSystemIdList(@Param("appSystemIdList") List appSystemIdList); - int searchAppModuleCount(ResourceSearchVo searchVo); List searchAppModuleIdList(ResourceSearchVo searchVo); @@ -96,36 +82,16 @@ public interface ResourceMapper extends IResourceCrossoverMapper { List getAppSystemModuleIdListByAppSystemIdAndAppModuleIdList(@Param("appSystemId") Long appSystemId, @Param("appModuleIdList") JSONArray appModuleIdList); - List getAppSystemModuleIdListByAppSystemIdAndAppModuleIdListAndEnvId(@Param("appSystemId") Long appSystemId, @Param("envId") Long envId, @Param("appModuleIdList") JSONArray appModuleIdList); - List getAppModuleListByAppSystemIdList(ResourceSearchVo searchVo); List getAppModuleListByIdListSimple(@Param("idList") List idList, @Param("needOrder") boolean needOrder); - Set getIpObjectResourceTypeIdListByAppModuleIdAndEnvId(ResourceSearchVo searchVo); - - Set getOsResourceTypeIdListByAppModuleIdAndEnvId(ResourceSearchVo searchVo); - - Set getIpObjectResourceTypeIdListByAppSystemIdAndEnvId(ResourceSearchVo searchVo); - - Set getOsResourceTypeIdListByAppSystemIdAndEnvId(ResourceSearchVo searchVo); - - Set getResourceAppSystemIdListByResourceId(Long id); - - List getResourceAppSystemListByResourceIdList(List id); - List getResourceListByResourceVoList(@Param("resourceList") List resourceList,@Param("searchVo") ResourceSearchVo searchVo); - Long getAppSystemIdByResourceId(Long id); - Set getResourceTypeIdListByAppSystemIdAndModuleIdAndEnvIdAndInspectStatusList(ResourceSearchVo searchVo); List getResourceIdListByAppSystemIdAndModuleIdAndEnvId(ResourceVo resourceVo); - List getAppInstanceResourceIdListByAppSystemIdAndModuleIdAndEnvId(ResourceVo resourceVo); - - Integer getAppInstanceResourceIdCountByAppSystemIdAndModuleIdAndEnvId(ResourceVo resourceVo); - /** * 根据类型和IP列表查询资源 * @@ -199,19 +165,17 @@ public interface ResourceMapper extends IResourceCrossoverMapper { List getAppSystemListByKeyword(BasePageVo searchVo); - List getOsEnvListByAppSystemIdAndTypeId(ResourceSearchVo searchVo); - - List getIpObjectEnvListByAppSystemIdAndTypeId(ResourceSearchVo searchVo); + List getAppEnvListByAppSystemId(Long appSystemId); List getAppEnvListByAppSystemIdAndAppModuleId(@Param("appSystemId") Long appSystemId, @Param("appModuleId") Long appModuleId); List> getAppEnvCountMapByAppSystemIdGroupByAppModuleId(Long appSystemId); - List getOsResourceListenPortListByResourceIdList(List resourceIdList); - - List getSoftwareResourceListenPortListByResourceIdList(List resourceIdList); - List getOsResourceListByResourceIdList(List resourceIdList); List getResourceTypeIdListByAuth(ResourceSearchVo searchVo); + + List getAppResourceTypeIdListByViewNameAndAppSystemId(@Param("viewName") String viewName, @Param("appSystemId") Long appSystemId, @Param("appModuleId") Long appModuleId, @Param("envId") Long envId); + + List getAppSystemIdListById(@Param("viewName") String viewName, @Param("id") Long id); } 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 63764146ca73fde63e03a7eacc0080e202990a1e..2e10df831f895c97016824d78263ba8ee1fe2484 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 @@ -497,30 +497,33 @@ along with this program. If not, see .--> + - - - - - - - + + + + + + - + + + + @@ -588,36 +591,10 @@ along with this program. If not, see .--> - - - + SELECT COUNT(DISTINCT `id`) + FROM @{DATA_SCHEMA}.`${viewName}` a + AND a.`app_system_id` = #{appSystemId} @@ -636,106 +613,97 @@ along with this program. If not, see .--> #{inspectStatus} - ORDER BY a.`id` DESC - LIMIT #{startNum}, #{pageSize} - - - - + SELECT DISTINCT a.`id` + FROM @{DATA_SCHEMA}.`${viewName}` a + + + AND a.`app_system_id` = #{appSystemId} + + + AND a.`app_module_id` = #{appModuleId} + + + AND a.`env_id` = #{envId} + + + AND a.`env_id` is null + + + AND a.`inspect_status` IN + + #{inspectStatus} + + + ORDER BY a.`id` DESC LIMIT #{startNum}, #{pageSize} - + - - - - @@ -770,84 +738,10 @@ along with this program. If not, see .--> - - - - SELECT a.`id`, a.`name`, a.`ip`,a.`port`,a.`type_id`, a.`type_name`, a.`type_label` - FROM @{DATA_SCHEMA}.`scence_ipobject_ip_port` a + FROM @{DATA_SCHEMA}.`scence_ipobject_detail` a where a.`id` in #{id} @@ -886,7 +780,7 @@ along with this program. If not, see .--> SELECT a.`id`, a.`name`, a.`ip`, a.`port`, a.`type_id`, a.`type_name`, a.`type_label` - FROM @{DATA_SCHEMA}.`scence_ipobject_ip_port` a + FROM @{DATA_SCHEMA}.`scence_ipobject_detail` a where a.`id` = #{value} + + - - - - - - - - - - - - - - - - - - - - - - SELECT a.`id`, a.`ip`, a.`name` - FROM @{DATA_SCHEMA}.`scence_ipobject_ip_port` a + FROM @{DATA_SCHEMA}.`scence_ipobject_detail` a WHERE a.`type_id` IN #{item} @@ -1355,7 +1085,7 @@ along with this program. If not, see .--> a.`port`, a.`type_name` AS typeName FROM - @{DATA_SCHEMA}.`scence_ipobject_ip_port` a + @{DATA_SCHEMA}.`scence_ipobject_detail` a WHERE a.`ip` = #{ip} @@ -1381,7 +1111,7 @@ along with this program. If not, see .--> a.`port`, a.`type_name` AS typeName FROM - @{DATA_SCHEMA}.`scence_ipobject_ip_port` a + @{DATA_SCHEMA}.`scence_ipobject_detail` a WHERE a.`ip` = #{ip} @@ -1491,7 +1221,7 @@ along with this program. If not, see .--> d.name as protocol, d.port as port FROM `cmdb_resourcecenter_resource_account` c - JOIN @{DATA_SCHEMA}.`scence_ipobject_ip_port` a ON a.id = c.resource_id + JOIN @{DATA_SCHEMA}.`scence_ipobject_detail` a ON a.id = c.resource_id JOIN `cmdb_resourcecenter_account` b ON b.id = c.account_id JOIN `cmdb_resourcecenter_account_protocol` d ON d.id = b.protocol_id @@ -1507,7 +1237,7 @@ along with this program. If not, see .--> SELECT count(DISTINCT a.id) FROM `cmdb_resourcecenter_resource_account` c - JOIN @{DATA_SCHEMA}.`scence_ipobject_ip_port` a ON a.id = c.resource_id + JOIN @{DATA_SCHEMA}.`scence_ipobject_detail` a ON a.id = c.resource_id JOIN `cmdb_resourcecenter_account` b ON b.id = c.account_id JOIN `cmdb_resourcecenter_account_protocol` d ON d.id = b.protocol_id @@ -1856,43 +1586,23 @@ along with this program. If not, see .--> - + - - - + SELECT IFNULL(a.env_id, -2) as id, + IFNULL(a.env_name, '未配置') as name, + IFNULL(a.env_seq_no, 9999) as seqNo, + a.app_module_id as moduleId, + a.app_module_name as moduleName, + a.app_module_abbr_name as moduleAbbrName + FROM @{DATA_SCHEMA}.`scence_ipobject_detail` a + WHERE a.`app_system_id` = #{appSystemId} AND a.app_module_id is not null @@ -1900,7 +1610,7 @@ along with this program. If not, see .--> SELECT DISTINCT a.`env_id` as id, a.`env_name` as name, a.`env_seq_no` as seqNo - FROM @{DATA_SCHEMA}.`scence_ipobject_env_appmodule_appsystem` a + FROM @{DATA_SCHEMA}.`scence_ipobject_detail` a WHERE a.`app_system_id` = #{appSystemId} AND a.`app_module_id` = #{appModuleId} AND a.`env_id` IS NOT NULL @@ -1910,63 +1620,12 @@ along with this program. If not, see .--> SELECT a.`app_module_id` AS appModuleId, COUNT(DISTINCT a.`env_id`) AS count - FROM @{DATA_SCHEMA}.`scence_ipobject_env_appmodule_appsystem` a + FROM @{DATA_SCHEMA}.`scence_ipobject_detail` a WHERE a.`app_system_id` = #{value} AND a.`env_id` IS NOT NULL GROUP BY a.`app_module_id` - - - @@ -2056,20 +1715,6 @@ along with this program. If not, see .--> ${conditionSql} - - + + + + diff --git a/src/main/java/neatlogic/module/cmdb/rebuilddatabaseview/handler/ResourceViewRebuildHandler.java b/src/main/java/neatlogic/module/cmdb/rebuilddatabaseview/handler/ResourceViewRebuildHandler.java index 6a677a5976ae84c50c0bdbd848464a93480ebe7c..428f714822621fa4c1032e22494eabbc0b8fbec2 100644 --- a/src/main/java/neatlogic/module/cmdb/rebuilddatabaseview/handler/ResourceViewRebuildHandler.java +++ b/src/main/java/neatlogic/module/cmdb/rebuilddatabaseview/handler/ResourceViewRebuildHandler.java @@ -15,7 +15,9 @@ along with this program. If not, see .*/ package neatlogic.module.cmdb.rebuilddatabaseview.handler; +import com.alibaba.fastjson.JSONObject; import neatlogic.framework.asynchronization.threadlocal.TenantContext; +import neatlogic.framework.cmdb.dto.resourcecenter.config.ResourceEntityConfigVo; import neatlogic.framework.cmdb.dto.resourcecenter.config.ResourceEntityVo; import neatlogic.framework.cmdb.dto.resourcecenter.config.SceneEntityVo; import neatlogic.framework.cmdb.enums.resourcecenter.Status; @@ -53,13 +55,33 @@ public class ResourceViewRebuildHandler implements IRebuildDataBaseView { @Override public List createViewIfNotExists() { List resultList = new ArrayList<>(); - List sceneEntityList = ResourceEntityFactory.getSceneEntityList(); - for (SceneEntityVo sceneEntityVo : sceneEntityList) { - String tableType = schemaMapper.checkTableOrViewIsExists(TenantContext.get().getDataDbName(), sceneEntityVo.getName()); + List resourceEntityList = resourceEntityMapper.getResourceEntityList(); + for (ResourceEntityVo resourceEntityVo : resourceEntityList) { + String tableType = schemaMapper.checkTableOrViewIsExists(TenantContext.get().getDataDbName(), resourceEntityVo.getName()); if (Objects.equals(tableType, "VIEW")) { continue; } - ViewStatusInfo viewStatusInfo = rebuildSceneEntity(sceneEntityVo); + String config = resourceEntityMapper.getResourceEntityConfigByName(resourceEntityVo.getName()); + SceneEntityVo sceneEntityVo = ResourceEntityFactory.getSceneEntityByViewName(resourceEntityVo.getName()); + if (sceneEntityVo == null) { + if (StringUtils.isBlank(config)) { + continue; + } + ResourceEntityConfigVo resourceEntityConfigVo = JSONObject.parseObject(config, ResourceEntityConfigVo.class); + if (resourceEntityConfigVo == null) { + continue; + } + String sceneTemplateName = resourceEntityConfigVo.getSceneTemplateName(); + if (StringUtils.isBlank(sceneTemplateName)) { + continue; + } + SceneEntityVo sceneTemplate = ResourceEntityFactory.getSceneEntityByViewName(sceneTemplateName); + if (sceneTemplate == null) { + continue; + } + } + resourceEntityVo.setConfigStr(config); + ViewStatusInfo viewStatusInfo = rebuildSceneEntity(resourceEntityVo); resultList.add(viewStatusInfo); } return resultList; @@ -68,24 +90,39 @@ public class ResourceViewRebuildHandler implements IRebuildDataBaseView { @Override public List createOrReplaceView() { List resultList = new ArrayList<>(); - List sceneEntityList = ResourceEntityFactory.getSceneEntityList(); - for (SceneEntityVo sceneEntityVo : sceneEntityList) { - ViewStatusInfo viewStatusInfo = rebuildSceneEntity(sceneEntityVo); + List resourceEntityList = resourceEntityMapper.getResourceEntityList(); + for (ResourceEntityVo resourceEntityVo : resourceEntityList) { + String config = resourceEntityMapper.getResourceEntityConfigByName(resourceEntityVo.getName()); + SceneEntityVo sceneEntityVo = ResourceEntityFactory.getSceneEntityByViewName(resourceEntityVo.getName()); + if (sceneEntityVo == null) { + if (StringUtils.isBlank(config)) { + continue; + } + ResourceEntityConfigVo resourceEntityConfigVo = JSONObject.parseObject(config, ResourceEntityConfigVo.class); + if (resourceEntityConfigVo == null) { + continue; + } + String sceneTemplateName = resourceEntityConfigVo.getSceneTemplateName(); + if (StringUtils.isBlank(sceneTemplateName)) { + continue; + } + SceneEntityVo sceneTemplate = ResourceEntityFactory.getSceneEntityByViewName(sceneTemplateName); + if (sceneTemplate == null) { + continue; + } + } + resourceEntityVo.setConfigStr(config); + ViewStatusInfo viewStatusInfo = rebuildSceneEntity(resourceEntityVo); resultList.add(viewStatusInfo); } return resultList; } - private ViewStatusInfo rebuildSceneEntity(SceneEntityVo sceneEntityVo) { - ResourceEntityVo resourceEntityVo = new ResourceEntityVo(); - resourceEntityVo.setName(sceneEntityVo.getName()); - resourceEntityVo.setLabel(sceneEntityVo.getLabel()); - String config = resourceEntityMapper.getResourceEntityConfigByName(resourceEntityVo.getName()); - if (StringUtils.isNotBlank(config)) { - resourceEntityVo.setConfigStr(config); - String error = resourceCenterResourceService.buildResourceView(resourceEntityVo.getName(), resourceEntityVo.getConfig()); - resourceEntityVo.setError(error); - if (StringUtils.isNotBlank(error)) { + private ViewStatusInfo rebuildSceneEntity(ResourceEntityVo resourceEntityVo) { + if (resourceEntityVo.getConfig() != null) { + resourceEntityVo.setError(null); + String sql = resourceCenterResourceService.buildResourceView(resourceEntityVo); + if (StringUtils.isNotBlank(resourceEntityVo.getError())) { resourceEntityVo.setStatus(Status.ERROR.getValue()); } else { resourceEntityVo.setStatus(Status.READY.getValue()); diff --git a/src/main/java/neatlogic/module/cmdb/resourcecenter/datasource/handler/DefaultResourceCenterDataSourceImpl.java b/src/main/java/neatlogic/module/cmdb/resourcecenter/datasource/handler/DefaultResourceCenterDataSourceImpl.java new file mode 100644 index 0000000000000000000000000000000000000000..798c406048daba553b93499a7c8a07e87d21b48a --- /dev/null +++ b/src/main/java/neatlogic/module/cmdb/resourcecenter/datasource/handler/DefaultResourceCenterDataSourceImpl.java @@ -0,0 +1,1217 @@ +/* + * 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.module.cmdb.resourcecenter.datasource.handler; + +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import neatlogic.framework.asynchronization.threadlocal.UserContext; +import neatlogic.framework.cmdb.dto.ci.CiVo; +import neatlogic.framework.cmdb.dto.cientity.CiEntityVo; +import neatlogic.framework.cmdb.dto.globalattr.GlobalAttrItemVo; +import neatlogic.framework.cmdb.dto.resourcecenter.*; +import neatlogic.framework.cmdb.dto.resourcecenter.config.ResourceEntityConfigVo; +import neatlogic.framework.cmdb.dto.resourcecenter.config.ResourceEntityFieldMappingVo; +import neatlogic.framework.cmdb.dto.resourcecenter.config.ResourceEntityVo; +import neatlogic.framework.cmdb.dto.resourcecenter.config.SceneEntityVo; +import neatlogic.framework.cmdb.exception.resourcecenter.AppModuleNotFoundException; +import neatlogic.framework.cmdb.exception.resourcecenter.AppSystemNotFoundException; +import neatlogic.framework.cmdb.resourcecenter.datasource.core.IResourceCenterDataSource; +import neatlogic.framework.cmdb.resourcecenter.datasource.core.Ordered; +import neatlogic.framework.common.constvalue.InspectStatus; +import neatlogic.framework.common.dto.BasePageVo; +import neatlogic.framework.common.dto.ValueTextVo; +import neatlogic.framework.util.TableResultUtil; +import neatlogic.module.cmdb.dao.mapper.ci.CiMapper; +import neatlogic.module.cmdb.dao.mapper.cientity.CiEntityCachedMapper; +import neatlogic.module.cmdb.dao.mapper.globalattr.GlobalAttrMapper; +import neatlogic.module.cmdb.dao.mapper.resourcecenter.ResourceEntityMapper; +import neatlogic.module.cmdb.dao.mapper.resourcecenter.ResourceMapper; +import neatlogic.module.cmdb.service.resourcecenter.resource.IResourceCenterResourceService; +import neatlogic.module.cmdb.utils.ResourceEntityFactory; +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.collections4.MapUtils; +import org.apache.commons.lang3.StringUtils; +import org.springframework.lang.Nullable; +import org.springframework.stereotype.Component; + +import javax.annotation.PostConstruct; +import javax.annotation.Resource; +import java.util.*; +import java.util.function.BiFunction; +import java.util.stream.Collectors; + +@Component +public class DefaultResourceCenterDataSourceImpl implements IResourceCenterDataSource { + + @Resource + private CiMapper ciMapper; + + @Resource + private GlobalAttrMapper globalAttrMapper; + + @Resource + private CiEntityCachedMapper ciEntityCachedMapper; + + @Resource + private ResourceMapper resourceMapper; + + @Resource + private ResourceEntityMapper resourceEntityMapper; + + @Resource + private IResourceCenterResourceService resourceCenterResourceService; + + private final Map> headFieldHandlerMap = new LinkedHashMap<>(); + + @PostConstruct + public void init() { + headFieldHandlerMap.put(new ValueTextVo("id", "ID"), (resourceVo, cacheData) -> resourceVo.getId()); + headFieldHandlerMap.put(new ValueTextVo("ip", "IP地址"), (resourceVo, cacheData) -> { + JSONObject resultObj = new JSONObject(); + resultObj.put("ip", resourceVo.getIp()); + resultObj.put("port", resourceVo.getPort()); + resultObj.put("ciId", resourceVo.getTypeId()); + resultObj.put("id", resourceVo.getId()); + return resultObj; + }); + headFieldHandlerMap.put(new ValueTextVo("ci", "模型"), (resourceVo, cacheData) -> { + if (resourceVo.getTypeId() == null) { + return null; + } + JSONObject resultObj = new JSONObject(); + CiVo ciVo = cacheData.getObject(resourceVo.getTypeId().toString(), CiVo.class); + if (ciVo != null) { + resultObj.put("id", ciVo.getId()); + resultObj.put("name", ciVo.getName()); + resultObj.put("label", ciVo.getLabel()); + resultObj.put("icon", ciVo.getIcon()); + } else { + resultObj.put("id", resourceVo.getTypeId()); + resultObj.put("name", resourceVo.getTypeName()); + resultObj.put("label", resourceVo.getTypeLabel()); + } + return resultObj; + }); + headFieldHandlerMap.put(new ValueTextVo("name", "名称"), (resourceVo, cacheData) -> resourceVo.getName()); + headFieldHandlerMap.put(new ValueTextVo("monitor", "监控状态"), (resourceVo, cacheData) -> { + JSONObject resultObj = new JSONObject(); + if (StringUtils.isNotBlank(resourceVo.getMonitorStatus())) { + JSONObject statusJson = InspectStatus.getInspectStatusJson(resourceVo.getMonitorStatus()); + if (MapUtils.isNotEmpty(statusJson)) { + resultObj.putAll(statusJson); + } + } + if (resourceVo.getMonitorTime() != null) { + resultObj.put("time", resourceVo.getMonitorTime()); + } + return resultObj; + }); + headFieldHandlerMap.put(new ValueTextVo("inspect", "巡检状态"), (resourceVo, cacheData) -> { + JSONObject resultObj = new JSONObject(); + if (StringUtils.isNotBlank(resourceVo.getInspectStatus())) { + JSONObject statusJson = InspectStatus.getInspectStatusJson(resourceVo.getInspectStatus()); + if (MapUtils.isNotEmpty(statusJson)) { + resultObj.putAll(statusJson); + } + } + if (resourceVo.getInspectTime() != null) { + resultObj.put("time", resourceVo.getInspectTime()); + } + return resultObj; + }); + headFieldHandlerMap.put(new ValueTextVo("appEnvironment", "应用环境"), (resourceVo, cacheData) -> { + if (resourceVo.getEnvId() == null) { + return null; + } + JSONObject resultObj = new JSONObject(); + Object obj = cacheData.get(resourceVo.getEnvId().toString()); + if (obj != null) { + if (obj instanceof GlobalAttrItemVo) { + GlobalAttrItemVo globalAttrItemVo = (GlobalAttrItemVo) obj; + resultObj.put("id", globalAttrItemVo.getId()); + resultObj.put("value", globalAttrItemVo.getValue()); + resultObj.put("attrId", globalAttrItemVo.getAttrId()); + resultObj.put("type", "globalAttr"); + } else if (obj instanceof CiEntityVo) { + CiEntityVo ciEntityVo = (CiEntityVo) obj; + resultObj = getResultObj(ciEntityVo); + resultObj.put("type", "attr"); + } + } else { + resultObj.put("id", resourceVo.getEnvId()); + resultObj.put("name", resourceVo.getEnvName()); + } + return resultObj; + }); + headFieldHandlerMap.put(new ValueTextVo("appModule", "应用模块"), (resourceVo, cacheData) -> { + if (resourceVo.getAppModuleId() == null) { + return null; + } + JSONObject resultObj = new JSONObject(); + CiEntityVo ciEntityVo = cacheData.getObject(resourceVo.getAppModuleId().toString(), CiEntityVo.class); + if (ciEntityVo != null) { + resultObj = getResultObj(ciEntityVo); + resultObj.put("abbrName", resourceVo.getAppModuleAbbrName()); + } else { + resultObj.put("id", resourceVo.getAppModuleId()); + resultObj.put("name", resourceVo.getAppModuleName()); + resultObj.put("abbrName", resourceVo.getAppModuleAbbrName()); + } + return resultObj; + }); + headFieldHandlerMap.put(new ValueTextVo("appSystem", "应用系统"), (resourceVo, cacheData) -> { + if (resourceVo.getAppSystemId() == null) { + return null; + } + JSONObject resultObj = new JSONObject(); + CiEntityVo ciEntityVo = cacheData.getObject(resourceVo.getAppSystemId().toString(), CiEntityVo.class); + if (ciEntityVo != null) { + resultObj = getResultObj(ciEntityVo); + resultObj.put("abbrName", resourceVo.getAppSystemAbbrName()); + } else { + resultObj.put("id", resourceVo.getAppSystemId()); + resultObj.put("name", resourceVo.getAppSystemName()); + resultObj.put("abbrName", resourceVo.getAppSystemAbbrName()); + } + return resultObj; + }); + headFieldHandlerMap.put(new ValueTextVo("allIpList", "IP列表"), (resourceVo, cacheData) -> { + JSONArray resultList = new JSONArray(); + List ipList = resourceVo.getAllIp(); + if (CollectionUtils.isNotEmpty(ipList)) { + for (IpVo ipVo : ipList) { + JSONObject resultObj = new JSONObject(); + CiEntityVo ciEntityVo = cacheData.getObject(ipVo.getId().toString(), CiEntityVo.class); + if (ciEntityVo != null) { + resultObj = getResultObj(ciEntityVo); + } else { + resultObj.put("id", ipVo.getId()); + resultObj.put("name", ipVo.getIp()); + } + resultList.add(resultObj); + } + } + return resultList; + }); + headFieldHandlerMap.put(new ValueTextVo("businessGroupList", "所属部门"), (resourceVo, cacheData) -> { + JSONArray resultList = new JSONArray(); + List bgList = resourceVo.getBgList(); + if (CollectionUtils.isNotEmpty(bgList)) { + for (BgVo bgVo : bgList) { + JSONObject resultObj = new JSONObject(); + CiEntityVo ciEntityVo = cacheData.getObject(bgVo.getBgId().toString(), CiEntityVo.class); + if (ciEntityVo != null) { + resultObj = getResultObj(ciEntityVo); + } else { + resultObj.put("id", bgVo.getBgId()); + resultObj.put("name", bgVo.getBgName()); + } + resultList.add(resultObj); + } + } + return resultList; + }); + headFieldHandlerMap.put(new ValueTextVo("ownerList", "所有者"), (resourceVo, cacheData) -> { + JSONArray resultList = new JSONArray(); + List ownerList = resourceVo.getOwnerList(); + if (CollectionUtils.isNotEmpty(ownerList)) { + for (OwnerVo ownerVo : ownerList) { + JSONObject resultObj = new JSONObject(); + CiEntityVo ciEntityVo = cacheData.getObject(ownerVo.getUserId().toString(), CiEntityVo.class); + if (ciEntityVo != null) { + resultObj = getResultObj(ciEntityVo); + } else { + resultObj.put("id", ownerVo.getUserId()); + resultObj.put("name", ownerVo.getUserName()); + } + resultList.add(resultObj); + } + } + return resultList; + }); + headFieldHandlerMap.put(new ValueTextVo("state", "资产状态"), (resourceVo, cacheData) -> { + if (resourceVo.getStateId() == null) { + return null; + } + JSONObject resultObj = new JSONObject(); + CiEntityVo ciEntityVo = cacheData.getObject(resourceVo.getStateId().toString(), CiEntityVo.class); + if (ciEntityVo != null) { + resultObj = getResultObj(ciEntityVo); + } else { + resultObj.put("id", resourceVo.getStateId()); + resultObj.put("name", resourceVo.getStateName()); + } + return resultObj; + }); + headFieldHandlerMap.put(new ValueTextVo("networkArea", "网络区域"), (resourceVo, cacheData) -> resourceVo.getNetworkArea()); + headFieldHandlerMap.put(new ValueTextVo("maintenanceWindow", "维护窗口"), (resourceVo, cacheData) -> resourceVo.getMaintenanceWindow()); + headFieldHandlerMap.put(new ValueTextVo("tagList", "标签"), (resourceVo, cacheData) -> resourceVo.getTagList()); + headFieldHandlerMap.put(new ValueTextVo("accountList", "账号"), (resourceVo, cacheData) -> resourceVo.getAccountList()); + headFieldHandlerMap.put(new ValueTextVo("description", "描述"), (resourceVo, cacheData) -> resourceVo.getDescription()); + headFieldHandlerMap.put(new ValueTextVo("vendor", "厂商"), (resourceVo, cacheData) -> { + if (resourceVo.getVendorId() == null) { + return null; + } + JSONObject resultObj = new JSONObject(); + CiEntityVo ciEntityVo = cacheData.getObject(resourceVo.getVendorId().toString(), CiEntityVo.class); + if (ciEntityVo != null) { + resultObj = getResultObj(ciEntityVo); + } else { + resultObj.put("id", resourceVo.getVendorId()); + resultObj.put("name", resourceVo.getVendorName()); + } + return resultObj; + + }); + headFieldHandlerMap.put(new ValueTextVo("dataCenter", "数据中心"), (resourceVo, cacheData) -> { + if (resourceVo.getDataCenterId() == null) { + return null; + } + JSONObject resultObj = new JSONObject(); + CiEntityVo ciEntityVo = cacheData.getObject(resourceVo.getDataCenterId().toString(), CiEntityVo.class); + if (ciEntityVo != null) { + resultObj = getResultObj(ciEntityVo); + } else { + resultObj.put("id", resourceVo.getDataCenterId()); + resultObj.put("name", resourceVo.getDataCenterName()); + } + return resultObj; + }); + headFieldHandlerMap.put(new ValueTextVo("fcu", "创建者"), (resourceVo, cacheData) -> { + String fcu = resourceVo.getFcu(); + if (StringUtils.isNotBlank(fcu)) { + JSONObject resultObj = new JSONObject(); + resultObj.put("uuid", fcu); + resultObj.put("initType", "user"); + return resultObj; + } + return null; + }); + headFieldHandlerMap.put(new ValueTextVo("fcd", "创建日期"), (resourceVo, cacheData) -> resourceVo.getFcd()); + headFieldHandlerMap.put(new ValueTextVo("lcu", "修改者"), (resourceVo, cacheData) -> { + String lcu = resourceVo.getLcu(); + if (StringUtils.isNotBlank(lcu)) { + JSONObject resultObj = new JSONObject(); + resultObj.put("uuid", lcu); + resultObj.put("initType", "user"); + return resultObj; + } + return null; + }); + headFieldHandlerMap.put(new ValueTextVo("lcd", "修改日期"), (resourceVo, cacheData) -> resourceVo.getLcd()); + } + + private JSONObject getResultObj(CiEntityVo ciEntityVo) { + JSONObject resultObj = new JSONObject(); + resultObj.put("id", ciEntityVo.getId()); + resultObj.put("name", ciEntityVo.getName()); + resultObj.put("ciId", ciEntityVo.getCiId()); + resultObj.put("ciName", ciEntityVo.getCiName()); + resultObj.put("ciLabel", ciEntityVo.getCiLabel()); + resultObj.put("ciIcon", ciEntityVo.getCiIcon()); + resultObj.put("isVirtual", ciEntityVo.getIsVirtual()); + return resultObj; + } + @Override + public Ordered getOrdered() { + return Ordered.LOWEST_PRECEDENCE; + } + + @Override + public JSONArray getAppResourceList(@Nullable Long appSystemId, @Nullable Long appModuleId, @Nullable Long envId, @Nullable List inspectStatusList, @Nullable String viewName, @Nullable Integer currentPage, @Nullable Integer pageSize) { + if (appSystemId != null && ciEntityCachedMapper.getCiEntityBaseInfoById(appSystemId) == null) { + throw new AppSystemNotFoundException(appSystemId); + } + if (appModuleId != null && ciEntityCachedMapper.getCiEntityBaseInfoById(appModuleId) == null) { + throw new AppModuleNotFoundException(appModuleId); + } + JSONArray tableList = new JSONArray(); + List viewNameList = new ArrayList<>(); + Map> viewName2FieldListMap = new HashMap<>(); + ApplicationListDisplayVo applicationListDisplay = resourceEntityMapper.getApplicationListDisplay(); + if (applicationListDisplay != null) { + JSONObject config = applicationListDisplay.getConfig(); + if (MapUtils.isNotEmpty(config)) { + JSONArray tableSettingList = config.getJSONArray("tableSettingList"); + if (CollectionUtils.isNotEmpty(tableSettingList)) { + for (int i = 0; i < tableSettingList.size(); i++) { + JSONObject tableObj = tableSettingList.getJSONObject(i); + if (MapUtils.isNotEmpty(tableObj)) { + String name = tableObj.getString("viewName"); + if (StringUtils.isNotBlank(viewName) && !Objects.equals(name, viewName)) { + continue; + } + viewNameList.add(name); + JSONArray fieldList = tableObj.getJSONArray("fieldList"); + if (CollectionUtils.isNotEmpty(fieldList)) { + viewName2FieldListMap.put(name, fieldList.toJavaList(String.class)); + } + } + } + } + } + } + if (CollectionUtils.isNotEmpty(viewNameList)) { + ResourceSearchVo searchVo = new ResourceSearchVo(); + if (appSystemId != null) { + searchVo.setAppSystemId(appSystemId); + } + if (appModuleId != null) { + searchVo.setAppModuleId(appModuleId); + } + if (envId != null) { + searchVo.setEnvId(envId); + } + if (currentPage != null) { + searchVo.setCurrentPage(currentPage); + } + if (pageSize != null) { + searchVo.setPageSize(pageSize); + } + if (CollectionUtils.isNotEmpty(inspectStatusList)) { + searchVo.setInspectStatusList(inspectStatusList); + } + for (String name : viewNameList) { + ResourceEntityVo resourceEntityVo = resourceEntityMapper.getResourceEntityByName(name); + if (resourceEntityVo != null) { + searchVo.setViewName(name); + List resourceList = getAppResourceList(searchVo, true); + if (CollectionUtils.isNotEmpty(resourceList)) { + List fieldList = viewName2FieldListMap.get(name); + JSONArray theadList = getTheadList(fieldList); + JSONArray tbodyList = getTbodyList(fieldList, resourceList, resourceEntityVo); + JSONObject tableObj = TableResultUtil.getResult(theadList, tbodyList, searchVo); + tableObj.put("viewName", name); + tableObj.put("viewLabel", resourceEntityVo.getLabel()); + tableList.add(tableObj); + } + } + } + } + return tableList; + } + + @Override + public List getAppResourceList(ResourceSearchVo searchVo, boolean needPage) { + List resultList = new ArrayList<>(); + int rowNum = resourceMapper.getAppResourceCount(searchVo); + if (rowNum > 0) { + searchVo.setRowNum(rowNum); + if (needPage) { + searchVo.setPageSize(100); + } + Integer pageCount = searchVo.getPageCount(); + for (int i = 1; i <= pageCount; i++) { + if (needPage && !Objects.equals(i, searchVo.getCurrentPage())) { + continue; + } + searchVo.setCurrentPage(i); + List resourceIdList = resourceMapper.getAppResourceIdList(searchVo); + if (CollectionUtils.isNotEmpty(resourceIdList)) { + searchVo.setIdList(resourceIdList); + List resourceList = resourceMapper.getAppResourceListByIdList(searchVo); + resultList.addAll(resourceList); + } + if (needPage) { + break; + } + } + } + return resultList; + } + + @Override + public List getAppResourceIdList(ResourceSearchVo searchVo, boolean needPage) { + List resultList = new ArrayList<>(); + int rowNum = resourceMapper.getAppResourceCount(searchVo); + if (rowNum > 0) { + searchVo.setRowNum(rowNum); + if (needPage) { + searchVo.setPageSize(100); + } + Integer pageCount = searchVo.getPageCount(); + for (int i = 1; i <= pageCount; i++) { + if (needPage && !Objects.equals(i, searchVo.getCurrentPage())) { + continue; + } + searchVo.setCurrentPage(i); + List resourceIdList = resourceMapper.getAppResourceIdList(searchVo); + resultList.addAll(resourceIdList); + if (needPage) { + break; + } + } + } + return resultList; + } + + @Override + public JSONArray getTbodyList(List fieldList, List resourceList, ResourceEntityVo resourceEntityVo) { + JSONArray tbodyList = new JSONArray(); + Map keyMap = new HashMap<>(); + for (ValueTextVo key : headFieldHandlerMap.keySet()) { + keyMap.put(key.getValue(), key); + } + JSONObject cacheData = new JSONObject(); + cacheData.put("resourceEntityVo", resourceEntityVo); + if (fieldList.contains("ci")) { + List ciIdList = resourceList.stream().filter(Objects::nonNull).map(ResourceVo::getTypeId).filter(Objects::nonNull).collect(Collectors.toList()); + List ciList = ciMapper.getCiByIdList(ciIdList); + for (CiVo ciVo : ciList) { + cacheData.put(ciVo.getId().toString(), ciVo); + } + } + Set ciEntityIdSet = new HashSet<>(); + if (fieldList.contains("state")) { + List stateIdList = resourceList.stream().filter(Objects::nonNull).map(ResourceVo::getStateId).filter(Objects::nonNull).collect(Collectors.toList()); + ciEntityIdSet.addAll(stateIdList); + } + if (fieldList.contains("vendor")) { + List vendorIdList = resourceList.stream().filter(Objects::nonNull).map(ResourceVo::getVendorId).filter(Objects::nonNull).collect(Collectors.toList()); + ciEntityIdSet.addAll(vendorIdList); + } + if (fieldList.contains("dataCenter")) { + List dataCenterIdList = resourceList.stream().filter(Objects::nonNull).map(ResourceVo::getDataCenterId).filter(Objects::nonNull).collect(Collectors.toList()); + ciEntityIdSet.addAll(dataCenterIdList); + } + if (fieldList.contains("appModule")) { + List appModuleIdList = resourceList.stream().filter(Objects::nonNull).map(ResourceVo::getAppModuleId).filter(Objects::nonNull).collect(Collectors.toList()); + ciEntityIdSet.addAll(appModuleIdList); + } + if (fieldList.contains("appSystem")) { + List appSystemIdList = resourceList.stream().filter(Objects::nonNull).map(ResourceVo::getAppSystemId).filter(Objects::nonNull).collect(Collectors.toList()); + ciEntityIdSet.addAll(appSystemIdList); + } + if (fieldList.contains("appEnvironment")) { + ResourceEntityConfigVo config = resourceEntityVo.getConfig(); + if (config != null) { + List fieldMappingList = config.getFieldMappingList(); + if (CollectionUtils.isNotEmpty(fieldMappingList)) { + for (ResourceEntityFieldMappingVo fieldMappingVo : fieldMappingList) { + if (Objects.equals(fieldMappingVo.getField(), "env_id")) { + List envIdList = resourceList.stream().filter(Objects::nonNull).map(ResourceVo::getEnvId).filter(Objects::nonNull).collect(Collectors.toList()); + if (Objects.equals(fieldMappingVo.getType(), "globalAttr")) { + for (Long envId : envIdList) { + GlobalAttrItemVo globalAttrItemVo = globalAttrMapper.getGlobalAttrItemById(envId); + if (globalAttrItemVo != null) { + cacheData.put(globalAttrItemVo.getId().toString(), globalAttrItemVo); + } + } + } else if (Objects.equals(fieldMappingVo.getType(), "attr")) { + ciEntityIdSet.addAll(envIdList); + } + break; + } + } + } + } + } + if (fieldList.contains("allIpList")) { + List ipIdList = resourceList.stream().filter(Objects::nonNull).map(ResourceVo::getAllIp).filter(Objects::nonNull).flatMap(List::stream).filter(Objects::nonNull).map(IpVo::getId).collect(Collectors.toList()); + ciEntityIdSet.addAll(ipIdList); + } + if (fieldList.contains("ownerList")) { + List userIdList = resourceList.stream().filter(Objects::nonNull).map(ResourceVo::getOwnerList).filter(Objects::nonNull).flatMap(List::stream).filter(Objects::nonNull).map(OwnerVo::getUserId).collect(Collectors.toList()); + if (CollectionUtils.isNotEmpty(userIdList)) { + ResourceEntityConfigVo config = resourceEntityVo.getConfig(); + if (config != null) { + List fieldMappingList = config.getFieldMappingList(); + if (CollectionUtils.isNotEmpty(fieldMappingList)) { + for (ResourceEntityFieldMappingVo fieldMappingVo : fieldMappingList) { + if (Objects.equals(fieldMappingVo.getField(), "user_id")) { + if (Objects.equals(fieldMappingVo.getType(), "attr") && StringUtils.isNotBlank(fieldMappingVo.getToCi())) { + CiVo ciVo = ciMapper.getCiByName(fieldMappingVo.getToCi()); + if (ciVo != null) { + if (Objects.equals(ciVo.getIsVirtual(), 1)) { + CiEntityVo searchVo = new CiEntityVo(); + searchVo.setCiId(ciVo.getId()); + searchVo.setIdList(userIdList); + List virtualCiEntityList = ciEntityCachedMapper.getVirtualCiEntityBaseInfoByIdList(searchVo); + for (CiEntityVo virtualCiEntity : virtualCiEntityList) { + virtualCiEntity.setCiId(ciVo.getId()); + virtualCiEntity.setCiName(ciVo.getName()); + virtualCiEntity.setCiLabel(ciVo.getLabel()); + virtualCiEntity.setCiIcon(ciVo.getIcon()); + virtualCiEntity.setIsVirtual(ciVo.getIsVirtual()); + cacheData.put(virtualCiEntity.getId().toString(), virtualCiEntity); + } + } else { + ciEntityIdSet.addAll(userIdList); + } + } + } + break; + } + } + } + } + } + + } + if (fieldList.contains("businessGroupList")) { + List bgIdList = resourceList.stream().filter(Objects::nonNull).map(ResourceVo::getBgList).filter(Objects::nonNull).flatMap(List::stream).filter(Objects::nonNull).map(BgVo::getBgId).collect(Collectors.toList()); + if (CollectionUtils.isNotEmpty(bgIdList)) { + ResourceEntityConfigVo config = resourceEntityVo.getConfig(); + if (config != null) { + List fieldMappingList = config.getFieldMappingList(); + if (CollectionUtils.isNotEmpty(fieldMappingList)) { + for (ResourceEntityFieldMappingVo fieldMappingVo : fieldMappingList) { + if (Objects.equals(fieldMappingVo.getField(), "bg_id")) { + if (Objects.equals(fieldMappingVo.getType(), "attr") && StringUtils.isNotBlank(fieldMappingVo.getToCi())) { + CiVo ciVo = ciMapper.getCiByName(fieldMappingVo.getToCi()); + if (ciVo != null) { + if (Objects.equals(ciVo.getIsVirtual(), 1)) { + CiEntityVo searchVo = new CiEntityVo(); + searchVo.setCiId(ciVo.getId()); + searchVo.setIdList(bgIdList); + List virtualCiEntityList = ciEntityCachedMapper.getVirtualCiEntityBaseInfoByIdList(searchVo); + for (CiEntityVo virtualCiEntity : virtualCiEntityList) { + virtualCiEntity.setCiId(ciVo.getId()); + virtualCiEntity.setCiName(ciVo.getName()); + virtualCiEntity.setCiLabel(ciVo.getLabel()); + virtualCiEntity.setCiIcon(ciVo.getIcon()); + virtualCiEntity.setIsVirtual(ciVo.getIsVirtual()); + cacheData.put(virtualCiEntity.getId().toString(), virtualCiEntity); + } + } else { + ciEntityIdSet.addAll(bgIdList); + } + } + } + break; + } + } + } + } + } + } + if (CollectionUtils.isNotEmpty(ciEntityIdSet)) { + List ciEntityList = ciEntityCachedMapper.getCiEntityBaseInfoByIdList(new ArrayList<>(ciEntityIdSet)); + for (CiEntityVo ciEntityVo : ciEntityList) { + cacheData.put(ciEntityVo.getId().toString(), ciEntityVo); + } + } + if (fieldList.contains("tagList")) { + resourceCenterResourceService.addTagInformation(resourceList); + } + if (fieldList.contains("accountList")) { + resourceCenterResourceService.addAccountInformation(resourceList); + } + for (ResourceVo resourceVo : resourceList) { + JSONObject tbodyObj = new JSONObject(); + for (String field : fieldList) { + ValueTextVo key = keyMap.get(field); + if (key != null) { + BiFunction biFunction = headFieldHandlerMap.get(key); + tbodyObj.put(key.getValue().toString(), biFunction.apply(resourceVo, cacheData)); + } + } + if (!fieldList.contains("id")) { + tbodyObj.put("id", resourceVo.getId()); + } + tbodyList.add(tbodyObj); + } + return tbodyList; + } + + @Override + public List getResourceList(ResourceSearchVo searchVo) { + List resultList = new ArrayList<>(); + List idList = resourceMapper.getResourceIdList(searchVo); + if (CollectionUtils.isNotEmpty(idList)) { + List resourceList = resourceMapper.getResourceListByIdList(idList); + //排序 + for (Long id : idList) { + for (ResourceVo resourceVo : resourceList) { + if (Objects.equals(id, resourceVo.getId())) { + resultList.add(resourceVo); + break; + } + } + } + if (Objects.equals(searchVo.getRowNum(), 0)) { + int rowNum = 0; + if (noFilterCondition(searchVo)) { + rowNum = resourceMapper.getAllResourceCount(searchVo); + } else { + rowNum = resourceMapper.getResourceCount(searchVo); + } + searchVo.setRowNum(rowNum); + } + } else { + searchVo.setRowNum(0); + } + return resultList; + } + + @Override + public List getResourceTypeTree(String keyword) { + if (StringUtils.isNotBlank(keyword)) { + keyword = keyword.toLowerCase(); + } + List resultList = new ArrayList<>(); + List ciIdList = resourceEntityMapper.getAllResourceTypeCiIdList(); + if (CollectionUtils.isEmpty(ciIdList)) { + return resultList; + } + List authCiVoList = new ArrayList<>(); + ResourceSearchVo searchVo = new ResourceSearchVo(); + searchVo.setTypeIdList(ciIdList); + resourceCenterResourceService.assembleResourceSearchVo(searchVo, false); +// jsonObj.put("typeIdList", ciIdList); +// ResourceSearchVo searchVo = resourceCenterResourceService.assembleResourceSearchVo(jsonObj, false); + //先找出所有有权限的配置项的模型idList + if (!searchVo.getIsHasAuth()) { + Set authCiIdList = ciMapper.getAllAuthCi(UserContext.get().getAuthenticationInfoVo()).stream().map(CiVo::getId).collect(Collectors.toSet()); + authCiIdList.addAll(resourceMapper.getResourceTypeIdListByAuth(searchVo)); + if (CollectionUtils.isEmpty(authCiIdList)) { + return resultList; + } + authCiVoList = ciMapper.getCiByIdList(new ArrayList<>(authCiIdList)); + } + + if (CollectionUtils.isNotEmpty(ciIdList)) { + List ciVoList = ciMapper.getCiByIdList(ciIdList); + ciVoList.sort(Comparator.comparing(CiVo::getLft)); + for (CiVo ciVo : ciVoList) { + Set ciList = new HashSet<>(); + List ciListTmp = ciMapper.getDownwardCiListByLR(ciVo.getLft(), ciVo.getRht()); + //过滤出所有有权限的配置项的模型idList + if (!searchVo.getIsHasAuth()) { + if (CollectionUtils.isNotEmpty(authCiVoList) && CollectionUtils.isNotEmpty(ciListTmp)) { + for (CiVo ci : ciListTmp) { + for (CiVo authCi : authCiVoList) { + if (ci.getLft() <= authCi.getLft() && ci.getRht() >= authCi.getRht()) { + ciList.add(ci); + break; + } + } + } + } + } else { + ciList = new HashSet<>(ciListTmp); + } + int size = ciList.size(); + List resourceTypeVoList = new ArrayList<>(size); + Map resourceTypeMap = new HashMap<>(size); + for (CiVo ci : ciList) { + ResourceTypeVo resourceTypeVo = new ResourceTypeVo(ci.getId(), ci.getParentCiId(), ci.getLabel(), ci.getName()); + resourceTypeMap.put(resourceTypeVo.getId(), resourceTypeVo); + resourceTypeVoList.add(resourceTypeVo); + } + if (StringUtils.isNotBlank(keyword)) { + // 建立父子关系 + for (ResourceTypeVo resourceType : resourceTypeVoList) { + if (resourceType.getParentId() != null) { + ResourceTypeVo parentResourceType = resourceTypeMap.get(resourceType.getParentId()); + if (parentResourceType != null) { + resourceType.setParent(parentResourceType); + parentResourceType.addChild(resourceType); + } + } + } + // 判断节点名称是否与关键字keyword匹配,如果匹配就将该节点及其父子节点的isKeywordMatch字段值设置为1,否则设置为0。 + for (ResourceTypeVo resourceType : resourceTypeVoList) { + if (resourceType.getLabel().toLowerCase().contains(keyword)) { + if (resourceType.getIsKeywordMatch() == null) { + resourceType.setIsKeywordMatch(1); + resourceType.setUpwardIsKeywordMatch(1); + resourceType.setDownwardIsKeywordMatch(1); + } + } else { + if (resourceType.getIsKeywordMatch() == null) { + resourceType.setIsKeywordMatch(0); + } + } + } + // 将isKeywordMatch字段值为0的节点从其父级中移除。 + Iterator iterator = resourceTypeVoList.iterator(); + while (iterator.hasNext()) { + ResourceTypeVo resourceType = iterator.next(); + if (Objects.equals(resourceType.getIsKeywordMatch(), 0)) { + ResourceTypeVo parent = resourceType.getParent(); + if (parent != null) { + parent.removeChild(resourceType); + } + iterator.remove(); + } + } + } else { + for (ResourceTypeVo resourceType : resourceTypeVoList) { + if (resourceType.getParentId() != null) { + ResourceTypeVo parentResourceType = resourceTypeMap.get(resourceType.getParentId()); + if (parentResourceType != null) { + parentResourceType.addChild(resourceType); + } + } + } + } + for (ResourceTypeVo resourceType : resourceTypeVoList) { + if (resourceType.getParentId() != null) { + ResourceTypeVo parentResourceType = resourceTypeMap.get(resourceType.getParentId()); + if (parentResourceType == null) { + resultList.add(resourceType); + } + } else { + resultList.add(resourceType); + } + } + } + } + return resultList; + } + + @Override + public List getResourceTypeListTree(String keyword) { + if (StringUtils.isNotBlank(keyword)) { + keyword = keyword.toLowerCase(); + } + List resultList = new ArrayList<>(); + ResourceEntityVo resourceEntityVo = resourceEntityMapper.getResourceEntityByName("scence_ipobject_detail"); + if (resourceEntityVo == null) { + return resultList; + } + ResourceEntityConfigVo config = resourceEntityVo.getConfig(); + if (config == null) { + return resultList; + } + CiVo ciVo = ciMapper.getCiByName(config.getMainCi()); + if (ciVo == null) { + return resultList; + } + + List ciList = ciMapper.getDownwardCiListByLR(ciVo.getLft(), ciVo.getRht()); + int size = ciList.size(); + List resourceTypeVoList = new ArrayList<>(size); + Map resourceTypeMap = new HashMap<>(size); + for (CiVo ci : ciList) { + ResourceTypeVo resourceTypeVo = new ResourceTypeVo(ci.getId(), ci.getParentCiId(), ci.getLabel(), ci.getName()); + resourceTypeMap.put(resourceTypeVo.getId(), resourceTypeVo); + resourceTypeVoList.add(resourceTypeVo); + } + if (StringUtils.isNotBlank(keyword)) { + // 建立父子关系 + for (ResourceTypeVo resourceType : resourceTypeVoList) { + if (resourceType.getParentId() != null) { + ResourceTypeVo parentResourceType = resourceTypeMap.get(resourceType.getParentId()); + if (parentResourceType != null) { + resourceType.setParent(parentResourceType); + parentResourceType.addChild(resourceType); + } + } + } + // 判断节点名称是否与关键字keyword匹配,如果匹配就将该节点及其父子节点的isKeywordMatch字段值设置为1,否则设置为0。 + for (ResourceTypeVo resourceType : resourceTypeVoList) { + if (resourceType.getLabel().toLowerCase().contains(keyword)) { + if (resourceType.getIsKeywordMatch() == null) { + resourceType.setIsKeywordMatch(1); + resourceType.setUpwardIsKeywordMatch(1); + resourceType.setDownwardIsKeywordMatch(1); + } + } else { + if (resourceType.getIsKeywordMatch() == null) { + resourceType.setIsKeywordMatch(0); + } + } + } + // 将isKeywordMatch字段值为0的节点从其父级中移除。 + Iterator iterator = resourceTypeVoList.iterator(); + while (iterator.hasNext()) { + ResourceTypeVo resourceType = iterator.next(); + if (Objects.equals(resourceType.getIsKeywordMatch(), 0)) { + ResourceTypeVo parent = resourceType.getParent(); + if (parent != null) { + parent.removeChild(resourceType); + } + iterator.remove(); + } + } + } else { + for (ResourceTypeVo resourceType : resourceTypeVoList) { + if (resourceType.getParentId() != null) { + ResourceTypeVo parentResourceType = resourceTypeMap.get(resourceType.getParentId()); + if (parentResourceType != null) { + parentResourceType.addChild(resourceType); + } + } + } + } + for (ResourceTypeVo resourceType : resourceTypeVoList) { + if (resourceType.getParentId() != null) { + ResourceTypeVo parentResourceType = resourceTypeMap.get(resourceType.getParentId()); + if (parentResourceType == null) { + resultList.add(resourceType); + } + } else { + resultList.add(resourceType); + } + } + return resultList; + } + + @Override + public JSONArray getTheadList(List fieldList) { + JSONArray theadList = new JSONArray(); + Map keyMap = new HashMap<>(); + for (ValueTextVo key : headFieldHandlerMap.keySet()) { + keyMap.put(key.getValue(), key); + } + for (String field : fieldList) { + ValueTextVo valueTextVo = keyMap.get(field); + if (valueTextVo != null) { + JSONObject thead = new JSONObject(); + thead.put("key", valueTextVo.getValue()); + thead.put("title", valueTextVo.getText()); + theadList.add(thead); + } + } + return theadList; + } + + @Override + public List getAssertAllTheadList() { + Set keySet = headFieldHandlerMap.keySet(); + return new ArrayList<>(keySet); + } + + @Override + public List getAppAssertAllTheadList() { + List resultList = new ArrayList<>(); + for (ValueTextVo valueTextVo : headFieldHandlerMap.keySet()) { + if (Objects.equals(valueTextVo.getValue(), "tagList")) { + continue; + } + if (Objects.equals(valueTextVo.getValue(), "accountList")) { + continue; + } + resultList.add(valueTextVo); + } + return resultList; + } + + @Override + public List getAppSystemListForTree(BasePageVo searchVo) { + List appSystemList = new ArrayList<>(); + String keyword = searchVo.getKeyword(); + int count = resourceMapper.getAppSystemIdListCountByKeyword(keyword); + if (count > 0) { + searchVo.setRowNum(count); + List appSystemIdList = resourceMapper.getAppSystemIdListByKeyword(searchVo); + if (CollectionUtils.isEmpty(appSystemIdList)) { + return appSystemList; + } + appSystemList = resourceMapper.getAppSystemListByIdList(appSystemIdList); + List hasModuleAppSystemIdList = resourceMapper.getHasModuleAppSystemIdListByAppSystemIdList(appSystemIdList); + if (CollectionUtils.isNotEmpty(hasModuleAppSystemIdList)) { + for (AppSystemVo appSystemVo : appSystemList) { + if (hasModuleAppSystemIdList.contains(appSystemVo.getId())) { + appSystemVo.setIsHasModule(1); + } + } + } + if (StringUtils.isNotEmpty(keyword)) { + List appModuleList = resourceMapper.getAppModuleListByKeywordAndAppSystemIdList(keyword, appSystemIdList); + if (CollectionUtils.isNotEmpty(appModuleList)) { + Map> appModuleMap = new HashMap<>(); + for (AppModuleVo appModuleVo : appModuleList) { + appModuleMap.computeIfAbsent(appModuleVo.getAppSystemId(), key -> new ArrayList<>()).add(appModuleVo); + } + for (AppSystemVo appSystemVo : appSystemList) { + List appModuleVoList = appModuleMap.get(appSystemVo.getId()); + if (CollectionUtils.isNotEmpty(appModuleVoList)) { + appSystemVo.setAppModuleList(appModuleVoList); + appSystemVo.setIsHasModule(1); + } + } + } + } + } + return appSystemList; + } + + @Override + public List getAppSystemListForSelect(BasePageVo searchVo) { + List resourceList = new ArrayList<>(); + JSONArray defaultValue = searchVo.getDefaultValue(); + if (CollectionUtils.isNotEmpty(defaultValue)) { + List idList = defaultValue.toJavaList(Long.class); + resourceList = resourceMapper.searchAppSystemListByIdList(idList); + } else { + int rowNum = resourceMapper.searchAppSystemCount(searchVo); + if (rowNum > 0) { + searchVo.setRowNum(rowNum); + if (searchVo.getNeedPage()) { + List idList = resourceMapper.searchAppSystemIdList(searchVo); + resourceList = resourceMapper.searchAppSystemListByIdList(idList); + } else { + int pageCount = searchVo.getPageCount(); + for (int currentPage = 1; currentPage <= pageCount; currentPage++) { + searchVo.setCurrentPage(currentPage); + List idList = resourceMapper.searchAppSystemIdList(searchVo); + List list = resourceMapper.searchAppSystemListByIdList(idList); + resourceList.addAll(list); + } + } + } + } + return resourceList; + } + + @Override + public List getAppModuleListForTree(Long appSystemId) { + List appModuleList = resourceMapper.getAppModuleListByAppSystemId(appSystemId); + if (CollectionUtils.isNotEmpty(appModuleList)) { + Map appEnvCountMap = new HashMap<>(); + List> appEnvCountMapList = resourceMapper.getAppEnvCountMapByAppSystemIdGroupByAppModuleId(appSystemId); + for (Map map : appEnvCountMapList) { + Long count = map.get("count"); + Long appModuleId = map.get("appModuleId"); + appEnvCountMap.put(appModuleId, count); + } + for (AppModuleVo appModuleVo : appModuleList) { + Long count = appEnvCountMap.get(appModuleVo.getId()); + if (count == null) { + appModuleVo.setIsHasEnv(0); + } else if (count == 0) { + appModuleVo.setIsHasEnv(0); + } else { + appModuleVo.setIsHasEnv(1); + } + } + } + return appModuleList; + } + + @Override + public List getAppModuleList(ResourceSearchVo searchVo) { + int count = resourceMapper.searchAppModuleCount(searchVo); + if (count > 0) { + searchVo.setRowNum(count); + List idList = resourceMapper.searchAppModuleIdList(searchVo); + if (CollectionUtils.isNotEmpty(idList)) { + return resourceMapper.searchAppModule(idList); + } + } + return new ArrayList<>(); + } + + @Override + public List getAppEnvListForSelect(BasePageVo searchVo) { + List appEnvList = new ArrayList<>(); + JSONArray defaultValue = searchVo.getDefaultValue(); + if (CollectionUtils.isNotEmpty(defaultValue)) { + List idList = defaultValue.toJavaList(Long.class); + appEnvList = resourceMapper.searchAppEnvListByIdList(idList); + } else { + int rowNum = resourceMapper.searchAppEnvCount(searchVo); + if (rowNum > 0) { + searchVo.setRowNum(rowNum); + if (searchVo.getNeedPage()) { + List idList = resourceMapper.searchAppEnvIdList(searchVo); + if (CollectionUtils.isNotEmpty(idList)) { + appEnvList = resourceMapper.searchAppEnvListByIdList(idList); + } + } else { + int pageCount = searchVo.getPageCount(); + for (int currentPage = 1; currentPage <= pageCount; currentPage++) { + searchVo.setCurrentPage(currentPage); + List idList = resourceMapper.searchAppEnvIdList(searchVo); + if (CollectionUtils.isNotEmpty(idList)) { + List list = resourceMapper.searchAppEnvListByIdList(idList); + appEnvList.addAll(list); + } + } + } + } + } + return appEnvList; + } + + @Override + public List getAppEnvListByAppSystemId(Long appSystemId) { + List appEnvList = resourceMapper.getAppEnvListByAppSystemId(appSystemId); + appEnvList.sort(Comparator.comparing(AppEnvVo::getSeqNo)); + return appEnvList; + } + + @Override + public List getStateListForSelect(BasePageVo searchVo) { + List stateList = new ArrayList<>(); + JSONArray defaultValue = searchVo.getDefaultValue(); + if (CollectionUtils.isNotEmpty(defaultValue)) { + List idList = defaultValue.toJavaList(Long.class); + stateList = resourceMapper.searchStateListByIdList(idList); + } else { + int rowNum = resourceMapper.searchStateCount(searchVo); + if (rowNum > 0) { + searchVo.setRowNum(rowNum); + if (searchVo.getNeedPage()) { + List idList = resourceMapper.searchStateIdList(searchVo); + stateList = resourceMapper.searchStateListByIdList(idList); + } else { + int pageCount = searchVo.getPageCount(); + for (int currentPage = 1; currentPage <= pageCount; currentPage++) { + searchVo.setCurrentPage(currentPage); + List idList = resourceMapper.searchStateIdList(searchVo); + List list = resourceMapper.searchStateListByIdList(idList); + stateList.addAll(list); + } + } + } + } + return stateList; + } + + @Override + public List getVendorListForSelect(BasePageVo searchVo) { + List vendorList = new ArrayList<>(); + JSONArray defaultValue = searchVo.getDefaultValue(); + if (CollectionUtils.isNotEmpty(defaultValue)) { + List idList = defaultValue.toJavaList(Long.class); + vendorList = resourceMapper.searchVendorListByIdList(idList); + } else { + int rowNum = resourceMapper.searchVendorCount(searchVo); + if (rowNum > 0) { + searchVo.setRowNum(rowNum); + if (searchVo.getNeedPage()) { + List idList = resourceMapper.searchVendorIdList(searchVo); + vendorList = resourceMapper.searchVendorListByIdList(idList); + } else { + int pageCount = searchVo.getPageCount(); + for (int currentPage = 1; currentPage <= pageCount; currentPage++) { + searchVo.setCurrentPage(currentPage); + List idList = resourceMapper.searchVendorIdList(searchVo); + List list = resourceMapper.searchVendorListByIdList(idList); + vendorList.addAll(list); + } + } + } + } + return vendorList; + } + + @Override + public Map> getAppResourceTypeIdListByAppSystemId(Long appSystemId) { + Map> viewName2TypeIdListMap = new HashMap<>(); + List appViewList = getAppViewList(); + if (CollectionUtils.isNotEmpty(appViewList)) { + for (ResourceEntityVo resourceEntityVo : appViewList) { + List typeIdList = resourceMapper.getAppResourceTypeIdListByViewNameAndAppSystemId(resourceEntityVo.getName(), appSystemId, null, null); + viewName2TypeIdListMap.put(resourceEntityVo.getName(), typeIdList); + } + } + return viewName2TypeIdListMap; + } + + @Override + public Map> getAppResourceTypeIdListByAppSystemIdAndAppModuleIdAndEnvId(Long appSystemId, Long appModuleId, Long envId) { + Map> viewName2TypeIdListMap = new HashMap<>(); + List appViewList = getAppViewList(); + if (CollectionUtils.isNotEmpty(appViewList)) { + for (ResourceEntityVo resourceEntityVo : appViewList) { + List typeIdList = resourceMapper.getAppResourceTypeIdListByViewNameAndAppSystemId(resourceEntityVo.getName(), appSystemId, appModuleId, envId); + viewName2TypeIdListMap.put(resourceEntityVo.getName(), typeIdList); + } + } + return viewName2TypeIdListMap; + } + + @Override + public List getAppSystemIdListById(Long id) { + List appSystemIdList = new ArrayList<>(); + List appViewList = getAppViewList(); + if (CollectionUtils.isNotEmpty(appViewList)) { + for (ResourceEntityVo resourceEntityVo : appViewList) { + List list = resourceMapper.getAppSystemIdListById(resourceEntityVo.getName(), id); + appSystemIdList.addAll(list); + } + } + return appSystemIdList; + } + + private List getAppViewList() { + List resultList = new ArrayList<>(); + List resourceEntityList = resourceEntityMapper.getResourceEntityList(); + for (ResourceEntityVo resourceEntityVo : resourceEntityList) { + SceneEntityVo sceneEntityVo = ResourceEntityFactory.getSceneEntityByViewName(resourceEntityVo.getName()); + if (sceneEntityVo == null) { + String config = resourceEntityMapper.getResourceEntityConfigByName(resourceEntityVo.getName()); + if (StringUtils.isNotBlank(config)) { + ResourceEntityConfigVo resourceEntityConfigVo = JSONObject.parseObject(config, ResourceEntityConfigVo.class); + if (Objects.equals(resourceEntityConfigVo.getSceneTemplateName(), "scence_application_asset_list_detail")) { + resourceEntityVo.setConfig(resourceEntityConfigVo); + resultList.add(resourceEntityVo); + } + } + } + } + return resultList; + } + + /** + * 判断是否有过滤条件 + * @param searchVo + * @return + */ + private boolean noFilterCondition(ResourceSearchVo searchVo) { + if (StringUtils.isNotBlank(searchVo.getKeyword())) { + return false; + } + if (CollectionUtils.isNotEmpty(searchVo.getBatchSearchList())) { + return false; + } + if (CollectionUtils.isNotEmpty(searchVo.getStateIdList())) { + return false; + } + if (CollectionUtils.isNotEmpty(searchVo.getVendorIdList())) { + return false; + } + if (CollectionUtils.isNotEmpty(searchVo.getEnvIdList())) { + return false; + } + if (searchVo.getExistNoEnv()) { + return false; + } + if (CollectionUtils.isNotEmpty(searchVo.getAppSystemIdList())) { + return false; + } + if (CollectionUtils.isNotEmpty(searchVo.getAppModuleIdList())) { + return false; + } + if (CollectionUtils.isNotEmpty(searchVo.getDefaultValue())) { + return false; + } + if (CollectionUtils.isNotEmpty(searchVo.getIdList())) { + return false; + } + if (CollectionUtils.isNotEmpty(searchVo.getInspectStatusList())) { + return false; + } + if (CollectionUtils.isNotEmpty(searchVo.getProtocolIdList())) { + return false; + } + if (CollectionUtils.isNotEmpty(searchVo.getTagIdList())) { + return false; + } + if (CollectionUtils.isNotEmpty(searchVo.getInspectJobPhaseNodeStatusList())) { + return false; + } + return true; + } + +} diff --git a/src/main/java/neatlogic/module/cmdb/service/resourcecenter/resource/IResourceCenterResourceService.java b/src/main/java/neatlogic/module/cmdb/service/resourcecenter/resource/IResourceCenterResourceService.java index ecae7f4109c518a925be691fcf61295e23f2fcbd..4be7d47f835616746d0c4597b7ce79b5d8198628 100644 --- a/src/main/java/neatlogic/module/cmdb/service/resourcecenter/resource/IResourceCenterResourceService.java +++ b/src/main/java/neatlogic/module/cmdb/service/resourcecenter/resource/IResourceCenterResourceService.java @@ -15,19 +15,14 @@ along with this program. If not, see .*/ package neatlogic.module.cmdb.service.resourcecenter.resource; -import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import neatlogic.framework.cmdb.crossover.IResourceCenterResourceCrossoverService; -import neatlogic.framework.cmdb.dto.ci.CiVo; import neatlogic.framework.cmdb.dto.resourcecenter.AccountVo; -import neatlogic.framework.cmdb.dto.resourcecenter.AppEnvVo; import neatlogic.framework.cmdb.dto.resourcecenter.ResourceSearchVo; import neatlogic.framework.cmdb.dto.resourcecenter.ResourceVo; -import neatlogic.framework.cmdb.dto.resourcecenter.config.ResourceEntityConfigVo; import neatlogic.framework.cmdb.dto.resourcecenter.config.ResourceEntityVo; import neatlogic.framework.cmdb.dto.tag.TagVo; -import java.util.Collection; import java.util.List; import java.util.Map; @@ -42,6 +37,8 @@ public interface IResourceCenterResourceService extends IResourceCenterResourceC ResourceSearchVo assembleResourceSearchVo(JSONObject jsonObj,boolean isIncludeSon); + void assembleResourceSearchVo(ResourceSearchVo searchVo, boolean isIncludeSon); + /** * 处理批量搜索关键字 * @param resourceSearchVo @@ -83,41 +80,6 @@ public interface IResourceCenterResourceService extends IResourceCenterResourceC */ Map> getResourceTagByResourceIdList(List idList); - /** - * 补充资产的账号信息 - * - * @param idList 资产id列表 - * @param resourceVoList 资产列表 - */ - void addResourceAccount(List idList, List resourceVoList); - - /** - * 补充资产的标签信息 - * - * @param idList 资产id列表 - * @param resourceVoList 资产列表 - */ - void addResourceTag(List idList, List resourceVoList); - - - /** - * 获取对应模块的应用清单列表 - * 其中清单列表有 系统 存储设备 网络设备 应用实例 应用实例集群 DB实例 DB实例集群 访问入口 - * - * @param searchVo resourceSearchVo - * @return tableList - */ - JSONArray getAppModuleResourceList(ResourceSearchVo searchVo); - - /** - * 获取对应的资产类型名称 - * - * @param resourceCiVoList 模型列表 - * @param resourceCiVo 模型 - * @return 模型名称 - */ - public String getResourceTypeName(List resourceCiVoList, CiVo resourceCiVo); - /** * 添加标签和账号信息 * @@ -126,33 +88,23 @@ public interface IResourceCenterResourceService extends IResourceCenterResourceC void addTagAndAccountInformation(List resourceList); /** - * 获取模块列表 + * 添加标签和账号信息 * - * @param searchVo resourceSearchVo - * @return 模块列表 + * @param resourceList */ - List getAppModuleList(ResourceSearchVo searchVo); - + void addTagInformation(List resourceList); /** - * 获取应用巡检批量巡检时的环境列表(环境会包含模块列表,模块还会包含模型列表) + * 添加标签和账号信息 * - * @param searchVo resourceSearchVo - * @return 应用巡检批量巡检时的环境列表 - */ - Collection getAppEnvList(ResourceSearchVo searchVo); - - /** - * 重建资源中心视图 - * @return + * @param resourceList */ - List rebuildResourceEntity(); + void addAccountInformation(List resourceList); /** * 构建单个视图 - * @param viewName - * @param originalConfig + * @param resourceEntityVo * @return */ - String buildResourceView(String viewName, ResourceEntityConfigVo originalConfig); + String buildResourceView(ResourceEntityVo resourceEntityVo); } 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 affcdf7bb6d84948b5a2c305e8236d9d0046b920..df52c449feb420e5795dc0dbc91d453e8ec4a616 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 @@ -15,7 +15,6 @@ along with this program. If not, see .*/ package neatlogic.module.cmdb.service.resourcecenter.resource; -import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import neatlogic.framework.asynchronization.threadlocal.TenantContext; import neatlogic.framework.asynchronization.threadlocal.UserContext; @@ -24,20 +23,13 @@ import neatlogic.framework.cmdb.auth.label.CIENTITY_MODIFY; import neatlogic.framework.cmdb.auth.label.CI_MODIFY; import neatlogic.framework.cmdb.dto.ci.AttrVo; import neatlogic.framework.cmdb.dto.ci.CiVo; -import neatlogic.framework.cmdb.dto.cientity.CiEntityVo; import neatlogic.framework.cmdb.dto.globalattr.GlobalAttrVo; import neatlogic.framework.cmdb.dto.resourcecenter.*; -import neatlogic.framework.cmdb.dto.resourcecenter.config.ResourceEntityConfigVo; -import neatlogic.framework.cmdb.dto.resourcecenter.config.ResourceEntityFieldMappingVo; -import neatlogic.framework.cmdb.dto.resourcecenter.config.ResourceEntityVo; -import neatlogic.framework.cmdb.dto.resourcecenter.config.SceneEntityVo; +import neatlogic.framework.cmdb.dto.resourcecenter.config.*; import neatlogic.framework.cmdb.dto.tag.TagVo; import neatlogic.framework.cmdb.enums.CmdbTenantConfig; -import neatlogic.framework.cmdb.enums.resourcecenter.AppModuleResourceType; -import neatlogic.framework.cmdb.enums.resourcecenter.Status; +import neatlogic.framework.cmdb.enums.RelDirectionType; import neatlogic.framework.cmdb.exception.ci.CiNotFoundException; -import neatlogic.framework.cmdb.exception.resourcecenter.AppModuleNotFoundException; -import neatlogic.framework.cmdb.exception.resourcecenter.AppSystemNotFoundException; import neatlogic.framework.cmdb.exception.resourcecenter.ResourceViewFieldMappingException; import neatlogic.framework.cmdb.utils.ResourceViewGenerateSqlUtil; import neatlogic.framework.cmdb.utils.ResourceViewGenerateSqlUtilForTiDB; @@ -50,15 +42,11 @@ import neatlogic.framework.store.mysql.DatabaseVendor; import neatlogic.framework.store.mysql.DatasourceManager; import neatlogic.framework.transaction.core.EscapeTransactionJob; import neatlogic.framework.util.Md5Util; -import neatlogic.framework.util.TableResultUtil; import neatlogic.module.cmdb.dao.mapper.ci.AttrMapper; import neatlogic.module.cmdb.dao.mapper.ci.CiMapper; import neatlogic.module.cmdb.dao.mapper.cientity.CiEntityMapper; import neatlogic.module.cmdb.dao.mapper.globalattr.GlobalAttrMapper; -import neatlogic.module.cmdb.dao.mapper.resourcecenter.ResourceAccountMapper; -import neatlogic.module.cmdb.dao.mapper.resourcecenter.ResourceEntityMapper; -import neatlogic.module.cmdb.dao.mapper.resourcecenter.ResourceMapper; -import neatlogic.module.cmdb.dao.mapper.resourcecenter.ResourceTagMapper; +import neatlogic.module.cmdb.dao.mapper.resourcecenter.*; import neatlogic.module.cmdb.utils.ResourceEntityFactory; import net.sf.jsqlparser.schema.Table; import net.sf.jsqlparser.statement.create.table.ColDataType; @@ -68,7 +56,6 @@ import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; -import javax.annotation.PostConstruct; import javax.annotation.Resource; import java.util.*; import java.util.stream.Collectors; @@ -110,13 +97,6 @@ public class ResourceCenterResourceServiceImpl implements IResourceCenterResourc @Resource private DataBaseViewInfoMapper dataBaseViewInfoMapper; - public static final Map> searchMap = new HashMap<>(); - - @FunctionalInterface - public interface Action { - List execute(T t); - } - @Override public ResourceSearchVo assembleResourceSearchVo(JSONObject jsonObj) { if(!jsonObj.containsKey("typeId") && !jsonObj.containsKey("typeIdList")){ @@ -207,6 +187,49 @@ public class ResourceCenterResourceServiceImpl implements IResourceCenterResourc return searchVo; } + @Override + public void assembleResourceSearchVo(ResourceSearchVo searchVo, boolean isIncludeSon) { + boolean isHasAuth = AuthActionChecker.check(CI_MODIFY.class, CIENTITY_MODIFY.class); + Long typeId = searchVo.getTypeId(); + List typeIdList = new ArrayList<>(); + if (CollectionUtils.isNotEmpty(searchVo.getTypeIdList())) { + typeIdList.addAll(searchVo.getTypeIdList()); + } + if (typeId != null && !typeIdList.contains(typeId)) { + typeIdList.add(typeId); + } + if (CollectionUtils.isNotEmpty(typeIdList)) { + Set authedCiIdSet = new HashSet<>(); + Set ciIdSet = new HashSet<>(); + for (Long ciId : typeIdList) { + CiVo ciVo = ciMapper.getCiById(ciId); + if (ciVo == null) { + throw new CiNotFoundException(ciId); + } + if (!isHasAuth) { + List authedCiList; + authedCiList = ciMapper.getDownwardCiEntityQueryCiListByLR(ciVo.getLft(), ciVo.getRht(), UserContext.get().getAuthenticationInfoVo(), searchVo.getIsHasAuth()); + if (CollectionUtils.isNotEmpty(authedCiList)) { + if (isIncludeSon) { + List inCludeSonCiList = ciMapper.getBatchDownwardCiListByCiList(authedCiList); + Set ciIdList = inCludeSonCiList.stream().map(CiVo::getId).collect(Collectors.toSet()); + authedCiIdSet.addAll(ciIdList); + } else { + authedCiIdSet.addAll(authedCiList.stream().map(CiVo::getId).collect(Collectors.toSet())); + } + } + } + List ciList = ciMapper.getDownwardCiListByLR(ciVo.getLft(), ciVo.getRht()); + List ciIdList = ciList.stream().map(CiVo::getId).collect(Collectors.toList()); + ciIdSet.addAll(ciIdList); + + } + searchVo.setAuthedTypeIdList(new ArrayList<>(authedCiIdSet)); + searchVo.setTypeIdList(new ArrayList<>(ciIdSet)); + } + searchVo.setIsHasAuth(AuthActionChecker.check(CI_MODIFY.class, CIENTITY_MODIFY.class) || Objects.equals("0", ConfigManager.getConfig(CmdbTenantConfig.IS_RESOURCECENTER_AUTH))); + } + @Override public void handleBatchSearchList(ResourceSearchVo searchVo) { List batchSearchList = searchVo.getBatchSearchList(); @@ -340,46 +363,46 @@ public class ResourceCenterResourceServiceImpl implements IResourceCenterResourc return resourceTagVoMap; } - @Override - public void addResourceAccount(List idList, List resourceVoList) { - Map> resourceAccountVoMap = new HashMap<>(); - List resourceAccountVoList = resourceAccountMapper.getResourceAccountListByResourceIdList(idList); - if (CollectionUtils.isNotEmpty(resourceAccountVoList)) { - Set accountIdSet = resourceAccountVoList.stream().map(ResourceAccountVo::getAccountId).collect(Collectors.toSet()); - List accountList = resourceAccountMapper.getAccountListByIdList(new ArrayList<>(accountIdSet)); - Map accountMap = accountList.stream().collect(Collectors.toMap(AccountVo::getId, e -> e)); - for (ResourceAccountVo resourceAccountVo : resourceAccountVoList) { - resourceAccountVoMap.computeIfAbsent(resourceAccountVo.getResourceId(), k -> new ArrayList<>()).add(accountMap.get(resourceAccountVo.getAccountId())); - } - } - for (ResourceVo resourceVo : resourceVoList) { - List accountVoList = resourceAccountVoMap.get(resourceVo.getId()); - if (CollectionUtils.isNotEmpty(accountVoList)) { - resourceVo.setAccountList(accountVoList); - } - } - } - - @Override - public void addResourceTag(List idList, List resourceVoList) { - Map> resourceTagVoMap = new HashMap<>(); - List resourceTagVoList = resourceTagMapper.getResourceTagListByResourceIdList(idList); - if (CollectionUtils.isNotEmpty(resourceTagVoList)) { - Set tagIdSet = resourceTagVoList.stream().map(ResourceTagVo::getTagId).collect(Collectors.toSet()); - List tagList = resourceTagMapper.getTagListByIdList(new ArrayList<>(tagIdSet)); - Map tagMap = tagList.stream().collect(Collectors.toMap(TagVo::getId, e -> e)); - for (ResourceTagVo resourceTagVo : resourceTagVoList) { - resourceTagVoMap.computeIfAbsent(resourceTagVo.getResourceId(), k -> new ArrayList<>()).add(tagMap.get(resourceTagVo.getTagId())); - } - } +// @Override +// public void addResourceAccount(List idList, List resourceVoList) { +// Map> resourceAccountVoMap = new HashMap<>(); +// List resourceAccountVoList = resourceAccountMapper.getResourceAccountListByResourceIdList(idList); +// if (CollectionUtils.isNotEmpty(resourceAccountVoList)) { +// Set accountIdSet = resourceAccountVoList.stream().map(ResourceAccountVo::getAccountId).collect(Collectors.toSet()); +// List accountList = resourceAccountMapper.getAccountListByIdList(new ArrayList<>(accountIdSet)); +// Map accountMap = accountList.stream().collect(Collectors.toMap(AccountVo::getId, e -> e)); +// for (ResourceAccountVo resourceAccountVo : resourceAccountVoList) { +// resourceAccountVoMap.computeIfAbsent(resourceAccountVo.getResourceId(), k -> new ArrayList<>()).add(accountMap.get(resourceAccountVo.getAccountId())); +// } +// } +// for (ResourceVo resourceVo : resourceVoList) { +// List accountVoList = resourceAccountVoMap.get(resourceVo.getId()); +// if (CollectionUtils.isNotEmpty(accountVoList)) { +// resourceVo.setAccountList(accountVoList); +// } +// } +// } - for (ResourceVo resourceVo : resourceVoList) { - List tagVoList = resourceTagVoMap.get(resourceVo.getId()); - if (CollectionUtils.isNotEmpty(tagVoList)) { - resourceVo.setTagList(tagVoList.stream().map(TagVo::getName).collect(Collectors.toList())); - } - } - } +// @Override +// public void addResourceTag(List idList, List resourceVoList) { +// Map> resourceTagVoMap = new HashMap<>(); +// List resourceTagVoList = resourceTagMapper.getResourceTagListByResourceIdList(idList); +// if (CollectionUtils.isNotEmpty(resourceTagVoList)) { +// Set tagIdSet = resourceTagVoList.stream().map(ResourceTagVo::getTagId).collect(Collectors.toSet()); +// List tagList = resourceTagMapper.getTagListByIdList(new ArrayList<>(tagIdSet)); +// Map tagMap = tagList.stream().collect(Collectors.toMap(TagVo::getId, e -> e)); +// for (ResourceTagVo resourceTagVo : resourceTagVoList) { +// resourceTagVoMap.computeIfAbsent(resourceTagVo.getResourceId(), k -> new ArrayList<>()).add(tagMap.get(resourceTagVo.getTagId())); +// } +// } +// +// for (ResourceVo resourceVo : resourceVoList) { +// List tagVoList = resourceTagVoMap.get(resourceVo.getId()); +// if (CollectionUtils.isNotEmpty(tagVoList)) { +// resourceVo.setTagList(tagVoList.stream().map(TagVo::getName).collect(Collectors.toList())); +// } +// } +// } /** * 获取对应模块的应用清单列表 @@ -388,155 +411,133 @@ public class ResourceCenterResourceServiceImpl implements IResourceCenterResourc * @param searchVo * @return */ - @Override - public JSONArray getAppModuleResourceList(ResourceSearchVo searchVo) { - JSONArray tableList = new JSONArray(); - List resourceCiVoList = ciMapper.getCiListByNameList(AppModuleResourceType.getNameList()); - List resourceTypeIdList = new ArrayList<>(); - Long appSystemId = searchVo.getAppSystemId(); - Long appModuleId = searchVo.getAppModuleId(); - Long typeId = searchVo.getTypeId(); - if (typeId != null) { - CiVo ciVo = ciMapper.getCiById(typeId); - if (ciVo == null) { - throw new CiNotFoundException(typeId); - } - resourceTypeIdList.add(typeId); - } else if (CollectionUtils.isNotEmpty(searchVo.getTypeIdList())) { - resourceTypeIdList.addAll(searchVo.getTypeIdList()); - searchVo.setTypeIdList(null); - } else if (appModuleId != null) { - CiEntityVo ciEntityVo = ciEntityMapper.getCiEntityBaseInfoById(appModuleId); - if (ciEntityVo == null) { - throw new AppModuleNotFoundException(appModuleId); - } - Set resourceTypeIdSet = resourceMapper.getIpObjectResourceTypeIdListByAppModuleIdAndEnvId(searchVo); - resourceTypeIdList.addAll(resourceTypeIdSet); - if (CollectionUtils.isNotEmpty(resourceTypeIdSet)) { - resourceTypeIdSet = resourceMapper.getOsResourceTypeIdListByAppModuleIdAndEnvId(searchVo); - resourceTypeIdList.addAll(resourceTypeIdSet); - } - } else if (appSystemId != null) { - CiEntityVo ciEntityVo = ciEntityMapper.getCiEntityBaseInfoById(appSystemId); - if (ciEntityVo == null) { - throw new AppSystemNotFoundException(appSystemId); - } - Set resourceTypeIdSet = resourceMapper.getIpObjectResourceTypeIdListByAppSystemIdAndEnvId(searchVo); - resourceTypeIdList.addAll(resourceTypeIdSet); - if (CollectionUtils.isNotEmpty(resourceTypeIdSet)) { - resourceTypeIdSet = resourceMapper.getOsResourceTypeIdListByAppSystemIdAndEnvId(searchVo); - resourceTypeIdList.addAll(resourceTypeIdSet); - } - } - - if (CollectionUtils.isNotEmpty(resourceTypeIdList)) { - List ciList = ciMapper.getAllCi(resourceTypeIdList); - for (CiVo ciVo : ciList) { - ResourceTypeVo resourceTypeVo = new ResourceTypeVo(ciVo.getId(), ciVo.getParentCiId(), ciVo.getLabel(), ciVo.getName()); - String resourceTypeName = getResourceTypeName(resourceCiVoList, ciVo); - if (StringUtils.isBlank(resourceTypeName)) { - continue; - } - String actionKey = AppModuleResourceType.getAction(resourceTypeName); - if (StringUtils.isBlank(actionKey)) { - continue; - } - searchVo.setTypeId(ciVo.getId()); - List returnList = searchMap.get(actionKey).execute(searchVo); - if (CollectionUtils.isNotEmpty(returnList)) { - JSONObject tableObj = TableResultUtil.getResult(returnList, searchVo); - tableObj.put("type", resourceTypeVo); - tableList.add(tableObj); - } - } - } - return tableList; - } - - @PostConstruct - public void searchDispatcherInit() { - searchMap.put("ipObject", (searchVo) -> { - int rowNum = resourceMapper.getIpObjectResourceCountByAppSystemIdAndAppModuleIdAndEnvIdAndTypeId(searchVo); - if (rowNum > 0) { - searchVo.setRowNum(rowNum); - List idList = resourceMapper.getIpObjectResourceIdListByAppSystemIdAndAppModuleIdAndEnvIdAndTypeId(searchVo); - if (CollectionUtils.isNotEmpty(idList)) { - return resourceMapper.getResourceListByIdList(idList); - } - } - return new ArrayList<>(); - }); - - searchMap.put("OS", (searchVo) -> { - int rowNum = resourceMapper.getOsResourceCountByAppSystemIdAndAppModuleIdAndEnvIdAndTypeId(searchVo); - if (rowNum > 0) { - searchVo.setRowNum(rowNum); - List idList = resourceMapper.getOsResourceIdListByAppSystemIdAndAppModuleIdAndEnvIdAndTypeId(searchVo); - if (CollectionUtils.isNotEmpty(idList)) { - return resourceMapper.getOsResourceListByIdList(idList); - } - } - return new ArrayList<>(); - }); +// @Deprecated +// @Override +// public JSONArray getAppModuleResourceList(ResourceSearchVo searchVo) { +// JSONArray tableList = new JSONArray(); +// List resourceCiVoList = ciMapper.getCiListByNameList(AppModuleResourceType.getNameList()); +// List resourceTypeIdList = new ArrayList<>(); +// Long appSystemId = searchVo.getAppSystemId(); +// Long appModuleId = searchVo.getAppModuleId(); +// Long typeId = searchVo.getTypeId(); +// if (typeId != null) { +// CiVo ciVo = ciMapper.getCiById(typeId); +// if (ciVo == null) { +// throw new CiNotFoundException(typeId); +// } +// resourceTypeIdList.add(typeId); +// } else if (CollectionUtils.isNotEmpty(searchVo.getTypeIdList())) { +// resourceTypeIdList.addAll(searchVo.getTypeIdList()); +// searchVo.setTypeIdList(null); +// } else if (appModuleId != null) { +// CiEntityVo ciEntityVo = ciEntityMapper.getCiEntityBaseInfoById(appModuleId); +// if (ciEntityVo == null) { +// throw new AppModuleNotFoundException(appModuleId); +// } +// Set resourceTypeIdSet = resourceTempMapper.getIpObjectResourceTypeIdListByAppModuleIdAndEnvId(searchVo); +// resourceTypeIdList.addAll(resourceTypeIdSet); +// if (CollectionUtils.isNotEmpty(resourceTypeIdSet)) { +// resourceTypeIdSet = resourceTempMapper.getOsResourceTypeIdListByAppModuleIdAndEnvId(searchVo); +// resourceTypeIdList.addAll(resourceTypeIdSet); +// } +// } else if (appSystemId != null) { +// CiEntityVo ciEntityVo = ciEntityMapper.getCiEntityBaseInfoById(appSystemId); +// if (ciEntityVo == null) { +// throw new AppSystemNotFoundException(appSystemId); +// } +// Set resourceTypeIdSet = resourceTempMapper.getIpObjectResourceTypeIdListByAppSystemIdAndEnvId(searchVo); +// resourceTypeIdList.addAll(resourceTypeIdSet); +// if (CollectionUtils.isNotEmpty(resourceTypeIdSet)) { +// resourceTypeIdSet = resourceTempMapper.getOsResourceTypeIdListByAppSystemIdAndEnvId(searchVo); +// resourceTypeIdList.addAll(resourceTypeIdSet); +// } +// } +// +// if (CollectionUtils.isNotEmpty(resourceTypeIdList)) { +// List ciList = ciMapper.getAllCi(resourceTypeIdList); +// for (CiVo ciVo : ciList) { +// ResourceTypeVo resourceTypeVo = new ResourceTypeVo(ciVo.getId(), ciVo.getParentCiId(), ciVo.getLabel(), ciVo.getName()); +// String resourceTypeName = getResourceTypeName(resourceCiVoList, ciVo); +// if (StringUtils.isBlank(resourceTypeName)) { +// continue; +// } +// String actionKey = AppModuleResourceType.getAction(resourceTypeName); +// if (StringUtils.isBlank(actionKey)) { +// continue; +// } +// searchVo.setTypeId(ciVo.getId()); +// List returnList = searchMap.get(actionKey).execute(searchVo); +// if (CollectionUtils.isNotEmpty(returnList)) { +// JSONObject tableObj = TableResultUtil.getResult(returnList, searchVo); +// tableObj.put("type", resourceTypeVo); +// tableList.add(tableObj); +// } +// } +// } +// return tableList; +// } -// searchMap.put("StorageDevice", (searchVo) -> { -// int rowNum = resourceCenterMapper.getStorageResourceCount(searchVo); +// @PostConstruct +// @Deprecated +// public void searchDispatcherInit() { +// searchMap.put("ipObject", (searchVo) -> { +// int rowNum = resourceTempMapper.getIpObjectResourceCountByAppSystemIdAndAppModuleIdAndEnvIdAndTypeId(searchVo); // if (rowNum > 0) { // searchVo.setRowNum(rowNum); -// List idList = resourceCenterMapper.getStorageResourceIdList(searchVo); +// List idList = resourceTempMapper.getIpObjectResourceIdListByAppSystemIdAndAppModuleIdAndEnvIdAndTypeId(searchVo); // if (CollectionUtils.isNotEmpty(idList)) { -// return resourceCenterMapper.getStorageResourceListByIdList(idList, TenantContext.get().getDataDbName()); +// return resourceMapper.getResourceListByIdList(idList); // } // } // return new ArrayList<>(); // }); // -// searchMap.put("NetworkDevice", (searchVo) -> { -// int rowNum = resourceCenterMapper.getNetDevResourceCount(searchVo); +// searchMap.put("OS", (searchVo) -> { +// int rowNum = resourceTempMapper.getOsResourceCountByAppSystemIdAndAppModuleIdAndEnvIdAndTypeId(searchVo); // if (rowNum > 0) { // searchVo.setRowNum(rowNum); -// List idList = resourceCenterMapper.getNetDevResourceIdList(searchVo); +// List idList = resourceTempMapper.getOsResourceIdListByAppSystemIdAndAppModuleIdAndEnvIdAndTypeId(searchVo); // if (CollectionUtils.isNotEmpty(idList)) { -// return resourceCenterMapper.getNetDevResourceListByIdList(idList, TenantContext.get().getDataDbName()); +// return resourceTempMapper.getOsResourceListByIdList(idList); // } // } // return new ArrayList<>(); // }); - - searchMap.put("APPIns", (searchVo) -> { - int rowNum = resourceMapper.getIpObjectResourceCountByAppSystemIdAndAppModuleIdAndEnvIdAndTypeId(searchVo); - if (rowNum > 0) { - searchVo.setRowNum(rowNum); - List idList = resourceMapper.getIpObjectResourceIdListByAppSystemIdAndAppModuleIdAndEnvIdAndTypeId(searchVo); - if (CollectionUtils.isNotEmpty(idList)) { - return resourceMapper.getAppInstanceResourceListByIdList(idList); - } - } - return new ArrayList<>(); - }); - - searchMap.put("DBIns", (searchVo) -> { - int rowNum = resourceMapper.getIpObjectResourceCountByAppSystemIdAndAppModuleIdAndEnvIdAndTypeId(searchVo); - if (rowNum > 0) { - searchVo.setRowNum(rowNum); - List idList = resourceMapper.getIpObjectResourceIdListByAppSystemIdAndAppModuleIdAndEnvIdAndTypeId(searchVo); - if (CollectionUtils.isNotEmpty(idList)) { - return resourceMapper.getDbInstanceResourceListByIdList(idList); - } - } - return new ArrayList<>(); - }); - - } - - public String getResourceTypeName(List resourceCiVoList, CiVo resourceCiVo) { - for (CiVo ciVo : resourceCiVoList) { - if (ciVo.getLft() <= resourceCiVo.getLft() && ciVo.getRht() >= resourceCiVo.getRht()) { - return ciVo.getName(); - } - } - return null; - } +// +// searchMap.put("APPIns", (searchVo) -> { +// int rowNum = resourceTempMapper.getIpObjectResourceCountByAppSystemIdAndAppModuleIdAndEnvIdAndTypeId(searchVo); +// if (rowNum > 0) { +// searchVo.setRowNum(rowNum); +// List idList = resourceTempMapper.getIpObjectResourceIdListByAppSystemIdAndAppModuleIdAndEnvIdAndTypeId(searchVo); +// if (CollectionUtils.isNotEmpty(idList)) { +// return resourceMapper.getResourceListByIdList(idList); +// } +// } +// return new ArrayList<>(); +// }); +// +// searchMap.put("DBIns", (searchVo) -> { +// int rowNum = resourceTempMapper.getIpObjectResourceCountByAppSystemIdAndAppModuleIdAndEnvIdAndTypeId(searchVo); +// if (rowNum > 0) { +// searchVo.setRowNum(rowNum); +// List idList = resourceTempMapper.getIpObjectResourceIdListByAppSystemIdAndAppModuleIdAndEnvIdAndTypeId(searchVo); +// if (CollectionUtils.isNotEmpty(idList)) { +// return resourceTempMapper.getDbInstanceResourceListByIdList(idList); +// } +// } +// return new ArrayList<>(); +// }); +// +// } +// @Deprecated +// public String getResourceTypeName(List resourceCiVoList, CiVo resourceCiVo) { +// for (CiVo ciVo : resourceCiVoList) { +// if (ciVo.getLft() <= resourceCiVo.getLft() && ciVo.getRht() >= resourceCiVo.getRht()) { +// return ciVo.getName(); +// } +// } +// return null; +// } /** * 添加标签和账号信息 @@ -561,188 +562,48 @@ public class ResourceCenterResourceServiceImpl implements IResourceCenterResourc } @Override - public List getAppModuleList(ResourceSearchVo searchVo) { - int count = resourceMapper.searchAppModuleCount(searchVo); - if (count > 0) { - searchVo.setRowNum(count); - List idList = resourceMapper.searchAppModuleIdList(searchVo); - if (CollectionUtils.isNotEmpty(idList)) { - return resourceMapper.searchAppModule(idList); - } - } - return new ArrayList<>(); - } - - @Override - public Collection getAppEnvList(ResourceSearchVo searchVo) { - CiEntityVo ciEntityVo = ciEntityMapper.getCiEntityBaseInfoById(searchVo.getAppSystemId()); - if (ciEntityVo == null) { - throw new AppSystemNotFoundException(searchVo.getAppSystemId()); - } - Set resourceTypeIdSet = resourceMapper.getIpObjectResourceTypeIdListByAppSystemIdAndEnvId(searchVo); - List resourceTypeIdList = new ArrayList<>(resourceTypeIdSet); - if (CollectionUtils.isNotEmpty(resourceTypeIdSet)) { - resourceTypeIdSet = resourceMapper.getOsResourceTypeIdListByAppSystemIdAndEnvId(searchVo); - resourceTypeIdList.addAll(resourceTypeIdSet); - } - - if (CollectionUtils.isNotEmpty(resourceTypeIdList)) { - Map returnEnvMap = new HashMap<>(); - Map> envIdModuleIdSetMap = new HashMap<>(); - Map> envIdModuleListMap = new HashMap<>(); - Map> envModuleIdCiIdSetMap = new HashMap<>(); - Map> envModuleIdCiListMap = new HashMap<>(); - List ciList = ciMapper.getAllCi(resourceTypeIdList); - List resourceCiVoList = ciMapper.getCiListByNameList(AppModuleResourceType.getNameList()); - - for (CiVo ciVo : ciList) { - List appEnvList = new ArrayList<>(); - String resourceTypeName = getResourceTypeName(resourceCiVoList, ciVo); - if (StringUtils.isBlank(resourceTypeName)) { - continue; - } - String actionKey = AppModuleResourceType.getAction(resourceTypeName); - if (StringUtils.isBlank(actionKey)) { - continue; - } - searchVo.setTypeId(ciVo.getId()); - if (actionKey.equals("OS")) { - appEnvList.addAll(resourceMapper.getOsEnvListByAppSystemIdAndTypeId(searchVo)); - } else { - appEnvList.addAll(resourceMapper.getIpObjectEnvListByAppSystemIdAndTypeId(searchVo)); - } - - /*数据处理 - 1、returnEnvMap 环境List - 2、envIdModuleIdSetMap 环境id对应的 模块id列表 - 3、envModuleIdCiIdSetMap 环境id+模块id对应的 模型id列表 - */ - if (CollectionUtils.isNotEmpty(appEnvList)) { - for (AppEnvVo envVo : appEnvList) { - //未配置情况,环境id设为-2,因为id为-1的一般都是代表 所有 的意思 - if (envVo.getId() == null) { - envVo.setId(-2L); - envVo.setName("未配置"); - envVo.setSeqNo(9999); - } - Long envId = envVo.getId(); - returnEnvMap.put(envId, envVo); - List appModuleList = envVo.getAppModuleList(); - Set appModuleIdSet = appModuleList.stream().map(AppModuleVo::getId).collect(Collectors.toSet()); - if (envIdModuleIdSetMap.containsKey(envId)) { - for (AppModuleVo moduleVo : appModuleList) { - if (envIdModuleIdSetMap.get(envId).contains(moduleVo.getId())) { - List ciVoList = moduleVo.getCiList(); - for (CiVo ci : ciVoList) { - if (CollectionUtils.isNotEmpty(envModuleIdCiIdSetMap.get(envId + moduleVo.getId())) && !envModuleIdCiIdSetMap.get(envId + moduleVo.getId()).contains(ci.getId())) { - envModuleIdCiIdSetMap.get(envId + moduleVo.getId()).add(ci.getId()); - envModuleIdCiListMap.get(envId + moduleVo.getId()).add(ci); - } - } - } else { - envIdModuleIdSetMap.get(envId).add(moduleVo.getId()); - envIdModuleListMap.get(envId).add(moduleVo); - envModuleIdCiIdSetMap.put(envId + moduleVo.getId(), moduleVo.getCiList().stream().map(CiVo::getId).collect(Collectors.toSet())); - envModuleIdCiListMap.put(envId + moduleVo.getId(), moduleVo.getCiList()); - } - } - envIdModuleIdSetMap.get(envId).addAll(appModuleIdSet); - } else { - envIdModuleIdSetMap.put(envId, appModuleIdSet); - envIdModuleListMap.put(envId, appModuleList); - for (AppModuleVo moduleVo : appModuleList) { - envModuleIdCiIdSetMap.put(envId + moduleVo.getId(), moduleVo.getCiList().stream().map(CiVo::getId).collect(Collectors.toSet())); - envModuleIdCiListMap.put(envId + moduleVo.getId(), moduleVo.getCiList()); - } - } - } - } - } - for (Map.Entry entry : returnEnvMap.entrySet()) { - List appModuleVoList = envIdModuleListMap.get(entry.getKey()); - for (AppModuleVo appModuleVo : appModuleVoList) { - List ciVoList = envModuleIdCiListMap.get(entry.getKey() + appModuleVo.getId()); - appModuleVo.setCiList(ciVoList); - } - entry.getValue().setAppModuleList(appModuleVoList); + public void addTagInformation(List resourceList) { + List idList = resourceList.stream().filter(Objects::nonNull).map(ResourceVo::getId).filter(Objects::nonNull).collect(Collectors.toList()); + Map> tagMap = getResourceTagByResourceIdList(idList); + for (ResourceVo resourceVo : resourceList) { + List tagList = tagMap.get(resourceVo.getId()); + if (CollectionUtils.isNotEmpty(tagList)) { + resourceVo.setTagList(tagList.stream().map(TagVo::getName).collect(Collectors.toList())); } - return returnEnvMap.values().stream().sorted(Comparator.comparing(AppEnvVo::getSeqNo)).collect(Collectors.toList()); } - return null; } @Override - public List rebuildResourceEntity() { - List viewNameList = ResourceEntityFactory.getViewNameList(); - List resourceEntityList = resourceEntityMapper.getResourceEntityListByNameList(viewNameList); - for (ResourceEntityVo resourceEntityVo : resourceEntityList) { -// resourceEntityVo.setError(""); -// resourceEntityVo.setStatus(Status.PENDING.getValue()); -// resourceEntityMapper.updateResourceEntityStatusAndError(resourceEntityVo); -// String xml = resourceEntityMapper.getResourceEntityXmlByName(resourceEntityVo.getName()); -// if (StringUtils.isNotBlank(xml)) { -// resourceEntityVo.setXml(xml); -// ResourceEntityViewBuilder builder = new ResourceEntityViewBuilder(resourceEntityVo); -// builder.buildView(); -// } - String config = resourceEntityMapper.getResourceEntityConfigByName(resourceEntityVo.getName()); - resourceEntityVo.setConfigStr(config); - String error = buildResourceView(resourceEntityVo.getName(), resourceEntityVo.getConfig()); - resourceEntityVo.setError(error); - if (StringUtils.isNotBlank(error)) { - resourceEntityVo.setStatus(Status.ERROR.getValue()); - } else { - resourceEntityVo.setStatus(Status.READY.getValue()); - } - resourceEntityMapper.updateResourceEntityStatusAndError(resourceEntityVo); - } - List sceneEntityList = ResourceEntityFactory.getSceneEntityList(); - for (SceneEntityVo sceneEntityVo : sceneEntityList) { - String viewName = sceneEntityVo.getName(); - String tableType = schemaMapper.checkTableOrViewIsExists(TenantContext.get().getDataDbName(), viewName); - if (tableType == null) { - List fieldNameList = ResourceEntityFactory.getFieldNameListByViewName(viewName); - Table table = new Table(); - table.setName(viewName); - table.setSchemaName(TenantContext.get().getDataDbName()); - List columnDefinitions = new ArrayList<>(); - for (String columnName : fieldNameList) { - ColumnDefinition columnDefinition = new ColumnDefinition(); - columnDefinition.setColumnName(columnName); - columnDefinition.setColDataType(new ColDataType("int")); - columnDefinitions.add(columnDefinition); - } - CreateTable createTable = new CreateTable(); - createTable.setTable(table); - createTable.setColumnDefinitions(columnDefinitions); - createTable.setIfNotExists(true); - EscapeTransactionJob.State s = new EscapeTransactionJob(() -> { - schemaMapper.insertView(createTable.toString()); - }).execute(); - } - } - resourceEntityList = resourceEntityMapper.getResourceEntityListByNameList(viewNameList); - Map resourceEntityVoMap = resourceEntityList.stream().collect(Collectors.toMap(e -> e.getName(), e -> e)); - List resultList = new ArrayList<>(); - for (SceneEntityVo sceneEntityVo : sceneEntityList) { - ResourceEntityVo resourceEntityVo = resourceEntityVoMap.get(sceneEntityVo.getName()); - if (resourceEntityVo == null) { - resourceEntityVo = new ResourceEntityVo(); - resourceEntityVo.setName(sceneEntityVo.getName()); - resourceEntityVo.setLabel(sceneEntityVo.getLabel()); - resourceEntityVo.setStatus(Status.PENDING.getValue()); + public void addAccountInformation(List resourceList) { + List idList = resourceList.stream().map(ResourceVo::getId).collect(Collectors.toList()); + Map> accountMap = getResourceAccountByResourceIdList(idList); + for (ResourceVo resourceVo : resourceList) { + List accountList = accountMap.get(resourceVo.getId()); + if (CollectionUtils.isNotEmpty(accountList)) { + resourceVo.setAccountList(accountList); } - resultList.add(resourceEntityVo); } - return resultList; } @Override - public String buildResourceView(String viewName, ResourceEntityConfigVo originalConfig) { + public String buildResourceView(ResourceEntityVo resourceEntityVo) { + String viewName = resourceEntityVo.getName(); + ResourceEntityConfigVo originalConfig = resourceEntityVo.getConfig(); + String select = null; String error = StringUtils.EMPTY; try { - ResourceEntityConfigVo config = fieldMappingCheckValidityAndFillIdData(viewName, originalConfig); - String select = null; + List relLinkList = getRelLinkListByRelNode(originalConfig.getRelNode()); + originalConfig.setRelLinkList(relLinkList); + List leftJoinList = getLeftJoinList(originalConfig); + List fieldNameList = ResourceEntityFactory.getFieldNameListByViewName(viewName); + if (CollectionUtils.isEmpty(fieldNameList)) { + String sceneTemplateName = originalConfig.getSceneTemplateName(); + if (StringUtils.isNotBlank(sceneTemplateName)) { + fieldNameList = ResourceEntityFactory.getFieldNameListByViewName(sceneTemplateName); + } + } + ResourceEntityConfigVo config = fieldMappingCheckValidityAndFillIdData(viewName, fieldNameList, originalConfig); + config.setLeftJoinList(leftJoinList); if (Objects.equals(DatasourceManager.getDatabaseId(), DatabaseVendor.TIDB.getDatabaseId())) { ResourceViewGenerateSqlUtilForTiDB resourceViewGenerateSqlUtilForTiDB = new ResourceViewGenerateSqlUtilForTiDB(config); select = resourceViewGenerateSqlUtilForTiDB.getSql(); @@ -791,29 +652,30 @@ public class ResourceCenterResourceServiceImpl implements IResourceCenterResourc if (StringUtils.isNotBlank(error)) { String tableType = schemaMapper.checkTableOrViewIsExists(TenantContext.get().getDataDbName(), viewName); if (!Objects.equals(tableType, "BASE TABLE")) { - schemaMapper.deleteView(TenantContext.get().getDataDbName() + "." + viewName); - List fieldNameList = ResourceEntityFactory.getFieldNameListByViewName(viewName); - Table table = new Table(); - table.setName(viewName); - table.setSchemaName(TenantContext.get().getDataDbName()); - List columnDefinitions = new ArrayList<>(); - for (String columnName : fieldNameList) { - ColumnDefinition columnDefinition = new ColumnDefinition(); - columnDefinition.setColumnName(columnName); - columnDefinition.setColDataType(new ColDataType("int")); - columnDefinitions.add(columnDefinition); - } - CreateTable createTable = new CreateTable(); - createTable.setTable(table); - createTable.setColumnDefinitions(columnDefinitions); - createTable.setIfNotExists(true); EscapeTransactionJob.State s = new EscapeTransactionJob(() -> { + schemaMapper.deleteView(TenantContext.get().getDataDbName() + "." + viewName); + List fieldNameList = ResourceEntityFactory.getFieldNameListByViewName(viewName); + Table table = new Table(); + table.setName(viewName); + table.setSchemaName(TenantContext.get().getDataDbName()); + List columnDefinitions = new ArrayList<>(); + for (String columnName : fieldNameList) { + ColumnDefinition columnDefinition = new ColumnDefinition(); + columnDefinition.setColumnName(columnName); + columnDefinition.setColDataType(new ColDataType("int")); + columnDefinitions.add(columnDefinition); + } + CreateTable createTable = new CreateTable(); + createTable.setTable(table); + createTable.setColumnDefinitions(columnDefinitions); + createTable.setIfNotExists(true); schemaMapper.insertView(createTable.toString()); }).execute(); } + resourceEntityVo.setError(error); } } - return error; + return select; } /** @@ -823,13 +685,12 @@ public class ResourceCenterResourceServiceImpl implements IResourceCenterResourc * @param config * @return */ - private ResourceEntityConfigVo fieldMappingCheckValidityAndFillIdData(String viewName, ResourceEntityConfigVo config) { + private ResourceEntityConfigVo fieldMappingCheckValidityAndFillIdData(String viewName, List fieldNameList, ResourceEntityConfigVo config) { ResourceEntityConfigVo newConfig = new ResourceEntityConfigVo(); String mainCi = config.getMainCi(); if (StringUtils.isBlank(mainCi)) { throw new ResourceViewFieldMappingException(viewName); } - List fieldNameList = ResourceEntityFactory.getFieldNameListByViewName(viewName); List fieldMappingList = config.getFieldMappingList(); if (CollectionUtils.isEmpty(fieldMappingList)) { throw new ResourceViewFieldMappingException(viewName, fieldNameList); @@ -991,6 +852,82 @@ public class ResourceCenterResourceServiceImpl implements IResourceCenterResourc } } } + } else if (Objects.equals(type, "newRel")) { + String uuid = fieldMappingVo.getUuid(); + String ciName = fieldMappingVo.getCiName(); + String attr = fieldMappingVo.getAttr(); + if (StringUtils.isBlank(attr)) { + throw new ResourceViewFieldMappingException(viewName, field, "attr", attr); + } + List relLinkList = config.getRelLinkList(); + if (CollectionUtils.isNotEmpty(relLinkList)) { + for (ResourceEntityRelLinkVo relLinkVo : relLinkList) { + if (Objects.equals(relLinkVo.getRightUuid(), uuid)) { + CiVo rightCiVo = ciMapper.getCiByName(ciName); + if (rightCiVo == null) { + throw new ResourceViewFieldMappingException(viewName, field, "ciName", ciName); + } + newFieldMappingVo.setType("rel"); + newFieldMappingVo.setDirection(relLinkVo.getDirection()); + if (Objects.equals(relLinkVo.getDirection(), RelDirectionType.FROM.getValue())) { + CiVo fromCiVo = rightCiVo; + newFieldMappingVo.setFromCi(fromCiVo.getName()); + newFieldMappingVo.setFromCiId(fromCiVo.getId()); + String fromAttr = attr; + newFieldMappingVo.setFromAttr(fromAttr); + if (!defaultAttrList.contains(fromAttr)) { + AttrVo fromAttrVo = getAttrVo(fromCiVo, fromAttr); + if (fromAttrVo == null) { + throw new ResourceViewFieldMappingException(viewName, field, "attr", attr); + } + if (fromAttrVo.getTargetCiId() != null) { + throw new ResourceViewFieldMappingException(viewName, field, "attr", attr); + } + newFieldMappingVo.setFromAttrId(fromAttrVo.getId()); + newFieldMappingVo.setFromAttrCiId(fromAttrVo.getCiId()); + } + newFieldMappingVo.setFromCiAlias(relLinkVo.getRightCiAlias()); + + String toCi = relLinkVo.getLeftCi(); + CiVo toCiVo = ciMapper.getCiByName(toCi); + newFieldMappingVo.setToCi(toCiVo.getName()); + newFieldMappingVo.setToCiId(toCiVo.getId()); + newFieldMappingVo.setToCiIsVirtual(toCiVo.getIsVirtual()); + newFieldMappingVo.setToCiAlias(relLinkVo.getLeftCiAlias()); + } else if (Objects.equals(relLinkVo.getDirection(), RelDirectionType.TO.getValue())) { + CiVo toCiVo = rightCiVo; + newFieldMappingVo.setToCi(toCiVo.getName()); + newFieldMappingVo.setToCiId(toCiVo.getId()); + newFieldMappingVo.setToCiIsVirtual(toCiVo.getIsVirtual()); + newFieldMappingVo.setToCiAlias(relLinkVo.getRightCiAlias()); + String toAttr = attr; + if (StringUtils.isBlank(toAttr)) { + newFieldMappingVo.setToAttr("_id"); + } else { + newFieldMappingVo.setToAttr(toAttr); + if (!defaultAttrList.contains(toAttr)) { + AttrVo toAttrVo = getAttrVo(toCiVo, toAttr); + if (toAttrVo == null) { + throw new ResourceViewFieldMappingException(viewName, field, "attr", attr); + } + if (toAttrVo.getTargetCiId() != null) { + throw new ResourceViewFieldMappingException(viewName, field, "attr", attr); + } + newFieldMappingVo.setToAttrId(toAttrVo.getId()); + newFieldMappingVo.setToAttrCiId(toAttrVo.getCiId()); + newFieldMappingVo.setToAttrCiName(toAttrVo.getCiName()); + } + } + String fromCi = relLinkVo.getLeftCi(); + CiVo fromCiVo = ciMapper.getCiByName(fromCi); + newFieldMappingVo.setFromCi(fromCiVo.getName()); + newFieldMappingVo.setFromCiId(fromCiVo.getId()); + newFieldMappingVo.setFromCiAlias(relLinkVo.getLeftCiAlias()); + } + break; + } + } + } } else if (Objects.equals(type, "globalAttr")) { String fromCi = fieldMappingVo.getFromCi(); if (StringUtils.isBlank(fromCi)) { @@ -1027,13 +964,111 @@ public class ResourceCenterResourceServiceImpl implements IResourceCenterResourc } resultList.add(newFieldMappingVo); } -// if (CollectionUtils.isNotEmpty(fieldNameList)) { -// throw new ResourceViewFieldMappingException(viewName, fieldNameList); -// } newConfig.setFieldMappingList(resultList); return newConfig; } + private List getRelLinkListByRelNode(ResourceEntityRelNodeVo relNode) { + List relLinkList = new ArrayList<>(); + if (relNode != null) { + Map> map = new HashMap<>(); + List children = relNode.getChildren(); + if (CollectionUtils.isNotEmpty(children)) { + for (ResourceEntityRelNodeVo child : children) { + addRelLinkListByRelNode(relLinkList, relNode, child, map); + } + } + } + return relLinkList; + } + + private void addRelLinkListByRelNode(List relLinkList, ResourceEntityRelNodeVo leftNode, ResourceEntityRelNodeVo rightNode, Map> map) { + { + ResourceEntityRelLinkVo relLinkVo = new ResourceEntityRelLinkVo(); + { + Map relNodeAliasMap = map.computeIfAbsent(leftNode.getCiName(), key -> new HashMap<>()); + int size = relNodeAliasMap.size(); + String alias = relNodeAliasMap.get(leftNode); + if (alias == null) { + if (size == 0) { + alias = StringUtils.EMPTY; + } else { + alias = "alias_" + (size + 1); + } + relNodeAliasMap.put(leftNode, alias); + } + relLinkVo.setLeftCi(leftNode.getCiName()); + relLinkVo.setLeftCiAlias(alias); + } + { + Map relNodeAliasMap = map.computeIfAbsent(rightNode.getCiName(), key -> new HashMap<>()); + int size = relNodeAliasMap.size(); + String alias = relNodeAliasMap.get(rightNode); + if (alias == null) { + if (size == 0) { + alias = StringUtils.EMPTY; + } else { + alias = "_alias_" + (size + 1); + } + relNodeAliasMap.put(rightNode, alias); + } + relLinkVo.setRightCi(rightNode.getCiName()); + relLinkVo.setRightCiAlias(alias); + relLinkVo.setRightUuid(rightNode.getUuid()); + } + relLinkVo.setDirection(rightNode.getDirection()); + relLinkList.add(relLinkVo); + } + List children = rightNode.getChildren(); + if (CollectionUtils.isNotEmpty(children)) { + for (ResourceEntityRelNodeVo child : children) { + addRelLinkListByRelNode(relLinkList, rightNode, child, map); + } + } + } + + private List getLeftJoinList(ResourceEntityConfigVo config) { + List resultList = new ArrayList<>(); + List relLinkList = config.getRelLinkList(); + if (CollectionUtils.isNotEmpty(relLinkList)) { + for (ResourceEntityRelLinkVo linkVo : relLinkList) { + String leftCi = linkVo.getLeftCi(); + String rightCi = linkVo.getRightCi(); + CiVo leftCiVo = ciMapper.getCiByName(leftCi); + if (leftCiVo == null) { + throw new CiNotFoundException(leftCi); + } + CiVo rightCiVo = ciMapper.getCiByName(rightCi); + if (rightCiVo == null) { + throw new CiNotFoundException(rightCi); + } + String direction = linkVo.getDirection(); + if (Objects.equals(direction, RelDirectionType.FROM.getValue())) { + ResourceEntityLeftJoinVo leftJoinVo = new ResourceEntityLeftJoinVo(); + leftJoinVo.setDirection(direction); + leftJoinVo.setFromCi(rightCiVo.getName()); + leftJoinVo.setFromCiId(rightCiVo.getId()); + leftJoinVo.setFromCiAlias(linkVo.getRightCiAlias()); + leftJoinVo.setToCi(leftCiVo.getName()); + leftJoinVo.setToCiId(leftCiVo.getId()); + leftJoinVo.setToCiAlias(linkVo.getLeftCiAlias()); + resultList.add(leftJoinVo); + } else if (Objects.equals(direction, RelDirectionType.TO.getValue())) { + ResourceEntityLeftJoinVo leftJoinVo = new ResourceEntityLeftJoinVo(); + leftJoinVo.setDirection(direction); + leftJoinVo.setFromCi(leftCiVo.getName()); + leftJoinVo.setFromCiId(leftCiVo.getId()); + leftJoinVo.setFromCiAlias(linkVo.getLeftCiAlias()); + leftJoinVo.setToCi(rightCiVo.getName()); + leftJoinVo.setToCiId(rightCiVo.getId()); + leftJoinVo.setToCiAlias(linkVo.getRightCiAlias()); + resultList.add(leftJoinVo); + } + } + } + return resultList; + } + private AttrVo getAttrVo(CiVo ciVo, String attrName) { List upwardCiList = ciMapper.getUpwardCiListByLR(ciVo.getLft(), ciVo.getRht()); for (CiVo ci : upwardCiList) { diff --git a/src/main/java/neatlogic/module/cmdb/utils/ResourceEntityFactory.java b/src/main/java/neatlogic/module/cmdb/utils/ResourceEntityFactory.java index 8e88f68bafada4429d173a6d1d9dd08ad351fadf..5f2725dc416151b4c716efe186458bab7263c796 100644 --- a/src/main/java/neatlogic/module/cmdb/utils/ResourceEntityFactory.java +++ b/src/main/java/neatlogic/module/cmdb/utils/ResourceEntityFactory.java @@ -18,7 +18,6 @@ package neatlogic.module.cmdb.utils; import neatlogic.framework.cmdb.annotation.ResourceField; import neatlogic.framework.cmdb.annotation.ResourceType; import neatlogic.framework.cmdb.annotation.ResourceTypes; -import neatlogic.framework.cmdb.dto.resourcecenter.config.ResourceEntityVo; import neatlogic.framework.cmdb.dto.resourcecenter.config.SceneEntityVo; import neatlogic.framework.common.dto.ValueTextVo; import neatlogic.framework.restful.annotation.EntityField; @@ -41,7 +40,6 @@ public class ResourceEntityFactory { * 视图名称与字段列表映射关系 */ private static Map> fieldMap = new HashMap<>(); - private static List resourceEntityList = new ArrayList<>(); /** * 视图信息列表 */ @@ -51,63 +49,6 @@ public class ResourceEntityFactory { */ private static List viewNameList = new ArrayList<>(); -// static { -// Reflections ref = new Reflections("neatlogic.framework.cmdb.dto.resourcecenter.entity", new TypeAnnotationsScanner(), new SubTypesScanner(true)); -// Set> classList = ref.getTypesAnnotatedWith(ResourceType.class, true); -// for (Class c : classList) { -// ResourceEntityVo resourceEntityVo = null; -// Annotation[] classAnnotations = c.getDeclaredAnnotations(); -// for (Annotation annotation : classAnnotations) { -// if (annotation instanceof ResourceType) { -// ResourceType rt = (ResourceType) annotation; -// resourceEntityVo = new ResourceEntityVo(); -// resourceEntityVo.setName(rt.name()); -// resourceEntityVo.setLabel(rt.label()); -// } -// } -// if (resourceEntityVo == null) { -// continue; -// } -// for (Field field : c.getDeclaredFields()) { -// Annotation[] annotations = field.getDeclaredAnnotations(); -// for (Annotation annotation : annotations) { -// if (annotation instanceof ResourceField) { -// ResourceField rf = (ResourceField) annotation; -// if (StringUtils.isNotBlank(rf.name())) { -// ResourceEntityAttrVo attr = new ResourceEntityAttrVo(); -// attr.setField(rf.name()); -// resourceEntityVo.addAttr(attr); -// } -// } -// } -// } -// resourceEntityVo.setType(ViewType.RESOURCE.getValue()); -// resourceEntityList.add(resourceEntityVo); -// } -// classList = ref.getTypesAnnotatedWith(ResourceTypes.class, true); -// for (Class c : classList) { -// ResourceTypes resourceTypes = c.getAnnotation(ResourceTypes.class); -// if (resourceTypes != null) { -// for (ResourceType rt : resourceTypes.value()) { -// ResourceEntityVo resourceEntityVo = new ResourceEntityVo(); -// resourceEntityVo.setName(rt.name()); -// resourceEntityVo.setLabel(rt.label()); -// for (Field field : c.getDeclaredFields()) { -// ResourceField rf = field.getAnnotation(ResourceField.class); -// if (rf != null) { -// if (StringUtils.isNotBlank(rf.name())) { -// ResourceEntityAttrVo attr = new ResourceEntityAttrVo(); -// attr.setField(rf.name()); -// resourceEntityVo.addAttr(attr); -// } -// } -// } -// resourceEntityVo.setType(ViewType.RESOURCE.getValue()); -// resourceEntityList.add(resourceEntityVo); -// } -// } -// } -// } static { Reflections ref = new Reflections("neatlogic"); Set> classList = ref.getTypesAnnotatedWith(ResourceType.class, true); @@ -125,6 +66,8 @@ public class ResourceEntityFactory { sceneEntityVo = new SceneEntityVo(); sceneEntityVo.setName(rt.name()); sceneEntityVo.setLabel(rt.label()); + sceneEntityVo.setIsMultiple(rt.isMultiple()); + sceneEntityVo.setModuleId(rt.moduleId()); sceneEntityVo.setDescription(String.join(";", rt.functionPathList())); } } @@ -139,15 +82,6 @@ public class ResourceEntityFactory { fieldMap.computeIfAbsent(sceneEntityVo.getName(), key -> new ArrayList<>()).add(new ValueTextVo(rf.name(), ef.name())); } } -// Annotation[] annotations = field.getDeclaredAnnotations(); -// for (Annotation annotation : annotations) { -// if (annotation instanceof ResourceField) { -// ResourceField rf = (ResourceField) annotation; -// if (StringUtils.isNotBlank(rf.name())) { -// fieldMap.computeIfAbsent(sceneEntityVo.getName(), key -> new ArrayList<>()).add(new ValueTextVo(rf.name(), "aaa")); -// } -// } -// } } sceneEntityList.add(sceneEntityVo); } @@ -164,6 +98,8 @@ public class ResourceEntityFactory { SceneEntityVo sceneEntityVo = new SceneEntityVo(); sceneEntityVo.setName(rt.name()); sceneEntityVo.setLabel(rt.label()); + sceneEntityVo.setIsMultiple(rt.isMultiple()); + sceneEntityVo.setModuleId(rt.moduleId()); sceneEntityVo.setDescription(String.join(";", rt.functionPathList())); for (Field field : c.getDeclaredFields()) { ResourceField rf = field.getAnnotation(ResourceField.class); @@ -185,12 +121,24 @@ public class ResourceEntityFactory { sceneEntityList.sort(Comparator.comparingInt(e -> viewNameList.indexOf(e.getName()))); } - public static List getResourceEntityList() { - return resourceEntityList; + public static List getSceneEntityList() { + List resultList = new ArrayList<>(); + for (SceneEntityVo sceneEntity : sceneEntityList) { + if (Objects.equals(sceneEntity.getIsMultiple(), false)) { + resultList.add(sceneEntity); + } + } + return resultList; } - public static List getSceneEntityList() { - return sceneEntityList; + public static List getMultipleSceneEntityList() { + List resultList = new ArrayList<>(); + for (SceneEntityVo sceneEntity : sceneEntityList) { + if (Objects.equals(sceneEntity.getIsMultiple(), true)) { + resultList.add(sceneEntity); + } + } + return resultList; } public static SceneEntityVo getSceneEntityByViewName(String viewName) { @@ -200,6 +148,8 @@ public class ResourceEntityFactory { sceneEntityVo = new SceneEntityVo(); sceneEntityVo.setName(sceneEntity.getName()); sceneEntityVo.setLabel(sceneEntity.getLabel()); + sceneEntityVo.setIsMultiple(sceneEntity.getIsMultiple()); + sceneEntityVo.setModuleId(sceneEntity.getModuleId()); sceneEntityVo.setDescription(sceneEntity.getDescription()); } } @@ -224,8 +174,4 @@ public class ResourceEntityFactory { } return new ArrayList<>(fieldList); } - - public static List getViewNameList() { - return new ArrayList<>(viewNameList); - } } diff --git a/src/main/java/neatlogic/module/cmdb/utils/ResourceEntityViewBuilder.java b/src/main/java/neatlogic/module/cmdb/utils/ResourceEntityViewBuilder.java deleted file mode 100644 index 66cbd6e6f426e4bac71477b85b7be52c7bfff395..0000000000000000000000000000000000000000 --- a/src/main/java/neatlogic/module/cmdb/utils/ResourceEntityViewBuilder.java +++ /dev/null @@ -1,1451 +0,0 @@ -/*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.cmdb.utils; - -import neatlogic.framework.asynchronization.threadlocal.TenantContext; -import neatlogic.framework.asynchronization.threadlocal.UserContext; -import neatlogic.framework.cmdb.dto.ci.AttrVo; -import neatlogic.framework.cmdb.dto.ci.CiVo; -import neatlogic.framework.cmdb.dto.resourcecenter.config.*; -import neatlogic.framework.cmdb.enums.RelDirectionType; -import neatlogic.framework.cmdb.enums.resourcecenter.JoinType; -import neatlogic.framework.cmdb.enums.resourcecenter.Status; -import neatlogic.framework.cmdb.enums.resourcecenter.ViewType; -import neatlogic.framework.cmdb.exception.attr.AttrNotFoundException; -import neatlogic.framework.cmdb.exception.ci.CiNotFoundException; -import neatlogic.framework.cmdb.exception.resourcecenter.ResourceCenterConfigIrregularException; -import neatlogic.framework.cmdb.exception.resourcecenter.ResourceCenterResourceFoundException; -import neatlogic.framework.cmdb.exception.resourcecenter.ResourceCenterViewConfigException; -import neatlogic.framework.cmdb.utils.SceneEntityGenerateSqlUtil; -import neatlogic.framework.dao.mapper.DataBaseViewInfoMapper; -import neatlogic.framework.dao.mapper.SchemaMapper; -import neatlogic.framework.dto.DataBaseViewInfoVo; -import neatlogic.framework.transaction.core.EscapeTransactionJob; -import neatlogic.framework.util.Md5Util; -import neatlogic.module.cmdb.dao.mapper.resourcecenter.ResourceEntityMapper; -import neatlogic.module.cmdb.service.ci.CiService; -import net.sf.jsqlparser.expression.Alias; -import net.sf.jsqlparser.expression.StringValue; -import net.sf.jsqlparser.expression.operators.conditional.AndExpression; -import net.sf.jsqlparser.expression.operators.relational.EqualsTo; -import net.sf.jsqlparser.schema.Column; -import net.sf.jsqlparser.schema.Table; -import net.sf.jsqlparser.statement.create.table.ColDataType; -import net.sf.jsqlparser.statement.create.table.ColumnDefinition; -import net.sf.jsqlparser.statement.create.table.CreateTable; -import net.sf.jsqlparser.statement.select.*; -import net.sf.jsqlparser.util.SelectUtils; -import org.apache.commons.collections4.CollectionUtils; -import org.apache.commons.collections4.ListUtils; -import org.apache.commons.lang3.StringUtils; -import org.dom4j.Document; -import org.dom4j.DocumentException; -import org.dom4j.DocumentHelper; -import org.dom4j.Element; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -import java.util.*; -import java.util.stream.Collectors; - -@Deprecated -@Component -public class ResourceEntityViewBuilder { - private final static Logger logger = LoggerFactory.getLogger(ResourceEntityViewBuilder.class); - - private final static List defaultAttrList = Arrays.asList("_id", "_uuid", "_name", "_fcu", "_fcd", "_lcu", "_lcd", "_inspectStatus", "_inspectTime", "_monitorStatus", "_monitorTime", "_typeId", "_typeName", "_typeLabel"); - - private ResourceEntityVo resourceEntityVo; - - private SceneEntityVo sceneEntityVo; - - private String type; - - private final Map ciMap = new HashMap<>(); - private static SchemaMapper schemaMapper; - private static DataBaseViewInfoMapper dataBaseViewInfoMapper; - private static CiService ciService; - private static ResourceEntityMapper resourceEntityMapper; - - - @Autowired - public ResourceEntityViewBuilder( - SchemaMapper _schemaMapper, - CiService _ciService, - ResourceEntityMapper _resourceEntityMapper, - DataBaseViewInfoMapper _dataBaseViewInfoMapper) { - schemaMapper = _schemaMapper; - resourceEntityMapper = _resourceEntityMapper; - ciService = _ciService; - dataBaseViewInfoMapper = _dataBaseViewInfoMapper; - } - - private CiVo getCiByName(String ciName) { - if (!ciMap.containsKey(ciName)) { - CiVo ciVo = ciService.getCiByName(ciName); - ciMap.put(ciName, ciVo); - } - return ciMap.get(ciName); - } - - - private List getAllChildElement(Element fromElement, String elementName) { - List elementList = fromElement.elements(); - List returnList = new ArrayList<>(); - if (CollectionUtils.isNotEmpty(elementList)) { - for (Element element : elementList) { - if (element.getName().equalsIgnoreCase(elementName)) { - returnList.add(element); - } - List tmpElementList = getAllChildElement(element, elementName); - if (CollectionUtils.isNotEmpty(tmpElementList)) { - returnList.addAll(tmpElementList); - } - } - } - return returnList; - } - - public ResourceEntityViewBuilder(ResourceEntityVo resourceEntity) { - try { - String name = resourceEntity.getName(); - String xml = ""; - String description = resourceEntity.getDescription(); - this.type = ""; - Document document = DocumentHelper.parseText(xml); - Element root = document.getRootElement(); - if (Objects.equals(type, ViewType.RESOURCE.getValue())) { - List resourceEntityVoList = ResourceEntityFactory.getResourceEntityList(); - for (ResourceEntityVo entity : resourceEntityVoList) { - if (Objects.equals(entity.getName(), name)) { - resourceEntityVo = createResourceEntityVo(entity); -// resourceEntityVo.setXml(xml); - } - } - if (resourceEntityVo == null) { - throw new ResourceCenterResourceFoundException(name); - } - convertToResourceEntityVo(root); - } else if (Objects.equals(type, ViewType.SCENE.getValue())) { - List sceneEntityList = ResourceEntityFactory.getSceneEntityList(); - for (SceneEntityVo sceneEntity : sceneEntityList) { - if (Objects.equals(sceneEntity.getName(), name)) { - sceneEntityVo = new SceneEntityVo(); - sceneEntityVo.setName(name); - sceneEntityVo.setLabel(sceneEntity.getLabel()); - resourceEntityVo = new ResourceEntityVo(); - resourceEntityVo.setName(name); - resourceEntityVo.setDescription(description); -// resourceEntityVo.setXml(xml); - } - } - if (sceneEntityVo == null) { - throw new ResourceCenterResourceFoundException(name); - } - convertToSceneEntityVo(root); - } - } catch (DocumentException e) { - throw new ResourceCenterConfigIrregularException(e); - } - } - - /** - * 将标签内容转换成ResourceEntityVo - * @param resourceElement - * @return - */ - private void convertToResourceEntityVo(Element resourceElement) { - Map> elementMap = new HashMap<>(); - try { - String viewName = resourceEntityVo.getName(); - String ciName = resourceElement.attributeValue("ci"); - if (StringUtils.isBlank(ciName)) { - throw new ResourceCenterConfigIrregularException(viewName, "ci"); - } - CiVo ciVo = getCiByName(ciName); - if (ciVo == null) { - throw new CiNotFoundException(ciName); - } - resourceEntityVo.setCi(ciVo); - resourceEntityVo.setJoinList(null); - if (CollectionUtils.isNotEmpty(resourceEntityVo.getAttrList())) { - //分析属性 - for (ResourceEntityAttrVo attr : resourceEntityVo.getAttrList()) { - if (!elementMap.containsKey(viewName + "_attr")) { - elementMap.put(viewName + "_attr", getAllChildElement(resourceElement, "attr")); - } - List attrElementList = elementMap.get(viewName + "_attr"); - Optional attrOp = attrElementList.stream().filter(e -> e.attributeValue("field").equalsIgnoreCase(attr.getField())).findFirst(); - if (attrOp.isPresent()) { - Element attrElement = attrOp.get(); - String attrName = attrElement.attributeValue("attr"); - String attrCiName = attrElement.attributeValue("ci"); - CiVo attrCiVo = null; - if (StringUtils.isNotBlank(attrCiName)) { - attrCiVo = getCiByName(attrCiName); - if (attrCiVo == null) { - throw new CiNotFoundException(attrCiName); - } - } - if (StringUtils.isNotBlank(attrName)) { - attr.setAttr(attrName); - if (attrCiVo == null) { - checkAttrIsExists(ciVo, attrName); - attr.setCiId(ciVo.getId()); - attr.setCiName(ciName); - attr.setTableAlias(viewName); - } else { - checkAttrIsExists(attrCiVo, attrName); - attr.setCiName(attrCiName); - attr.setCiId(attrCiVo.getId()); - attr.setCi(attrCiVo); - attr.setTableAlias("target_cientity_" + attrCiName.toLowerCase(Locale.ROOT)); - } - - if (!attrName.startsWith("_")) { - AttrVo attrVo = getCiByName(attr.getCiName()).getAttrByName(attrName); - if (attrVo == null) { - throw new AttrNotFoundException(attr.getCiName(), attrName); - } - attr.setAttrId(attrVo.getId()); - } - } else if (attrElement.getParent().getName().equalsIgnoreCase("join")) { - if (attrCiVo != null) { - attr.setAttr("_id"); - attr.setCiId(attrCiVo.getId()); - attr.setCi(attrCiVo); - attr.setCiName(attrCiName); - attr.setTableAlias("target_cientity_" + attrCiName.toLowerCase(Locale.ROOT)); - } else { - throw new ResourceCenterConfigIrregularException(viewName, "attr", attr.getField(), "ci"); - } - } else { - throw new ResourceCenterConfigIrregularException(viewName, "attr", attr.getField(), "attr"); - } - } else { - if (!elementMap.containsKey(viewName + "_rel")) { - elementMap.put(viewName + "_rel", getAllChildElement(resourceElement, "rel")); - } - List relElementList = elementMap.get(viewName + "_rel"); - Optional relOp = relElementList.stream().filter(e -> e.attributeValue("field").equalsIgnoreCase(attr.getField())).findFirst(); - if (relOp.isPresent()) { - Element attrElement = relOp.get(); - String attrCiName = attrElement.attributeValue("ci"); - CiVo attrCiVo = null; - if (StringUtils.isNotBlank(attrCiName)) { - attrCiVo = getCiByName(attrCiName); - if (attrCiVo == null) { - throw new CiNotFoundException(attrCiName); - } - } - if (attrCiVo != null) { - attr.setAttr("_id"); - attr.setCiId(attrCiVo.getId()); - attr.setCi(attrCiVo); - attr.setCiName(attrCiName); - attr.setTableAlias("target_cientity_" + attrCiName.toLowerCase(Locale.ROOT)); - } else { - throw new ResourceCenterConfigIrregularException(viewName, "rel", attr.getField(), "ci"); - } - } else { - throw new ResourceCenterConfigIrregularException(viewName, "attr或rel", attr.getField()); - } - } - } - //分析连接查询 - Element joinElement = resourceElement.element("join"); - if (joinElement != null) { - List attrElementList = joinElement.elements("attr"); - if (CollectionUtils.isNotEmpty(attrElementList)) { - for (Element attrElement : attrElementList) { - String attrCiName = attrElement.attributeValue("ci"); - String attrFieldName = attrElement.attributeValue("field"); - String joinAttrName = attrElement.attributeValue("joinAttrName"); - if (StringUtils.isBlank(joinAttrName)) { - throw new ResourceCenterConfigIrregularException(viewName, "attr", attrFieldName, "joinAttrName"); - } - checkAttrIsExists(ciVo, joinAttrName); - if (StringUtils.isNotBlank(attrCiName)) { - CiVo joinCiVo = getCiByName(attrCiName); - if (joinCiVo == null) { - throw new CiNotFoundException(attrCiName); - } - ResourceEntityJoinVo joinVo = new ResourceEntityJoinVo(JoinType.ATTR); - joinVo.setCi(joinCiVo); - joinVo.setCiName(joinCiVo.getName()); - joinVo.setField(attrFieldName); - joinVo.setJoinAttrName(joinAttrName); - resourceEntityVo.addJoin(joinVo); - } - } - } - - List relElementList = joinElement.elements("rel"); - if (CollectionUtils.isNotEmpty(relElementList)) { - for (Element relElement : relElementList) { - String relCiName = relElement.attributeValue("ci"); - String relFieldName = relElement.attributeValue("field"); - String relDirection = relElement.attributeValue("direction"); - if (StringUtils.isNotBlank(relCiName)) { - CiVo joinCiVo = getCiByName(relCiName); - if (joinCiVo == null) { - throw new CiNotFoundException(relCiName); - } - ResourceEntityJoinVo joinVo = new ResourceEntityJoinVo(JoinType.REL); - joinVo.setCi(joinCiVo); - joinVo.setCiName(joinCiVo.getName()); - joinVo.setField(relFieldName); - if (StringUtils.isNotBlank(relDirection)) { - joinVo.setDirection(relDirection); - } - resourceEntityVo.addJoin(joinVo); - } - } - } - } - } - resourceEntityVo.setStatus(Status.PENDING.getValue()); - } catch (Exception ex) { - resourceEntityVo.setStatus(Status.ERROR.getValue()); - resourceEntityVo.setError(ex.getMessage()); - } - } -// /** -// * 将标签内容转换成SceneEntityVo -// * @param resourceElement -// * @return -// */ -// private void convertToSceneEntityVo(Element resourceElement) { -// Map> elementMap = new HashMap<>(); -// try { -// String viewName = sceneEntityVo.getName(); -// String ciName = resourceElement.attributeValue("ci"); -// if (StringUtils.isBlank(ciName)) { -// throw new ResourceCenterConfigIrregularException(viewName, "ci"); -// } -// CiVo ciVo = getCiByName(ciName); -// if (ciVo == null) { -// throw new CiNotFoundException(ciName); -// } -// sceneEntityVo.setCi(ciVo); -// resourceEntityVo.setCiId(ciVo.getId()); -// Map attToCiJoinMap = new HashMap<>(); -// Map relFromCiJoinMap = new HashMap<>(); -// Map relToCiJoinMap = new HashMap<>(); -// //分析连接查询 -// List sceneEntityJoinList = new ArrayList<>(); -// sceneEntityVo.setJoinList(sceneEntityJoinList); -// //先分析标签内容,因为后面分析标签时需要从标签中补充默认信息 -// List joinElementList = resourceElement.elements("join"); -// for (Element joinElement : joinElementList) { -// List attrElementList = joinElement.elements("attr"); -// if (CollectionUtils.isNotEmpty(attrElementList)) { -// for (Element attrElement : attrElementList) { -// SceneEntityJoinVo joinVo = convertToSceneEntityJoinVo(viewName, attrElement, JoinType.ATTR); -// String toCi = joinVo.getToCi(); -// if (StringUtils.isBlank(toCi)) { -// throw new ResourceCenterConfigIrregularException(viewName, "join->attr", joinVo.getField(), "toCi"); -// } -// CiVo toCiVo = getCiByName(toCi); -// if (toCiVo == null) { -// throw new CiNotFoundException(toCi); -// } -// joinVo.setToCiVo(toCiVo); -// String fromCi = joinVo.getFromCi(); -// if (StringUtils.isBlank(fromCi)) { -// fromCi = ciName; -// } -// CiVo fromCiVo = getCiByName(fromCi); -// if (fromCiVo == null) { -// throw new CiNotFoundException(ciName); -// } -// joinVo.setFromCiVo(fromCiVo); -// String fromAttr = joinVo.getFromAttr(); -// if (StringUtils.isNotBlank(fromAttr)) { -// if (!fromAttr.startsWith("_")) { -// AttrVo attrVo = fromCiVo.getAttrByName(fromAttr); -// if (attrVo == null) { -// throw new AttrNotFoundException(fromCi, fromAttr); -// } -// joinVo.setFromAttrVo(attrVo); -// } -// } else if (attrElement.getParent().getName().equalsIgnoreCase("join")) { -// joinVo.setFromAttr("_id"); -// } else { -// throw new ResourceCenterConfigIrregularException(viewName, "attr", joinVo.getField(), "fromAttr"); -// } -// if (!sceneEntityJoinList.contains(joinVo)) { -// sceneEntityJoinList.add(joinVo); -// } -// attToCiJoinMap.put(toCi, joinVo); -// } -// } -// -// List relElementList = joinElement.elements("rel"); -// if (CollectionUtils.isNotEmpty(relElementList)) { -// for (Element relElement : relElementList) { -// SceneEntityJoinVo joinVo = convertToSceneEntityJoinVo(viewName, relElement, JoinType.REL); -// if (Objects.equals(joinVo.getDirection(), RelDirectionType.FROM.getValue())) { -// String toCi = joinVo.getToCi(); -// if (StringUtils.isBlank(toCi)) { -// throw new ResourceCenterConfigIrregularException(viewName, "join->rel", joinVo.getField(), "toCi"); -// } -// CiVo toCiVo = getCiByName(toCi); -// if (toCiVo == null) { -// throw new CiNotFoundException(toCi); -// } -// joinVo.setToCiVo(toCiVo); -// String fromCi = joinVo.getFromCi(); -// if (StringUtils.isBlank(fromCi)) { -// fromCi = ciName; -// } -// CiVo fromCiVo = getCiByName(fromCi); -// if (fromCiVo == null) { -// throw new CiNotFoundException(fromCi); -// } -// joinVo.setFromCiVo(fromCiVo); -// relToCiJoinMap.put(toCi, joinVo); -// } else { -// String fromCi = joinVo.getFromCi(); -// if (StringUtils.isBlank(fromCi)) { -// throw new ResourceCenterConfigIrregularException(viewName, "join->rel", joinVo.getField(), "fromCi"); -// } -// CiVo fromCiVo = getCiByName(fromCi); -// if (fromCiVo == null) { -// throw new CiNotFoundException(fromCi); -// } -// joinVo.setFromCiVo(fromCiVo); -// String toCi = joinVo.getToCi(); -// if (StringUtils.isBlank(toCi)) { -// toCi = ciName; -// } -// CiVo toCiVo = getCiByName(toCi); -// if (toCiVo == null) { -// throw new CiNotFoundException(toCi); -// } -// joinVo.setToCiVo(toCiVo); -// relFromCiJoinMap.put(fromCi, joinVo); -// } -// if (!sceneEntityJoinList.contains(joinVo)) { -// sceneEntityJoinList.add(joinVo); -// } -// } -// } -// } -// //分析属性 -// { -// List attrElementList = elementMap.get(viewName + "_attr"); -// if (attrElementList == null) { -// attrElementList = getAllChildElement(resourceElement, "attr"); -// elementMap.put(viewName + "_attr", attrElementList); -// } -// List sceneEntityAttrList = new ArrayList<>(); -// sceneEntityVo.setAttrList(sceneEntityAttrList); -// for (Element attrElement : attrElementList) { -// SceneEntityAttrVo entityAttrVo = convertToSceneEntityAttrVo(viewName, attrElement, JoinType.ATTR); -// String fromCi = entityAttrVo.getFromCi(); -// if (StringUtils.isBlank(fromCi)) { -// fromCi = ciName; -// } -// CiVo fromCiVo = getCiByName(fromCi); -// if (fromCiVo == null) { -// throw new CiNotFoundException(fromCi); -// } -// entityAttrVo.setFromCiVo(fromCiVo); -// String fromAttr = entityAttrVo.getFromAttr(); -// if (StringUtils.isNotBlank(fromAttr)) { -// if (!fromAttr.startsWith("_")) { -// AttrVo attrVo = fromCiVo.getAttrByName(fromAttr); -// if (attrVo == null) { -// throw new AttrNotFoundException(fromCi, fromAttr); -// } -// entityAttrVo.setFromAttrVo(attrVo); -// } -// } else if (attrElement.getParent().getName().equalsIgnoreCase("join")) { -// entityAttrVo.setFromAttr("_id"); -// } -// String toCi = entityAttrVo.getToCi(); -// if (StringUtils.isNotBlank(toCi)) { -// entityAttrVo.setJoinType(JoinType.ATTR); -// CiVo toCiVo = getCiByName(toCi); -// if (toCiVo == null) { -// throw new CiNotFoundException(toCi); -// } -// entityAttrVo.setToCiVo(toCiVo); -// String toAttr = entityAttrVo.getToAttr(); -// if (StringUtils.isNotBlank(toAttr)) { -// if (!toAttr.startsWith("_")) { -// AttrVo attrVo = toCiVo.getAttrByName(toAttr); -// if (attrVo == null) { -// throw new AttrNotFoundException(toCi, toAttr); -// } -// entityAttrVo.setToAttrVo(attrVo); -// } -// } else if (attrElement.getParent().getName().equalsIgnoreCase("join")) { -// entityAttrVo.setToAttr("_id"); -// } else { -// throw new ResourceCenterConfigIrregularException(viewName, "attr", entityAttrVo.getField(), "toAttr"); -// } -// Integer toCiIsVirtual = toCiVo.getIsVirtual(); -// if (Objects.equals(toCiIsVirtual, 1)) { -// entityAttrVo.setToAttrCiId(toCiVo.getId()); -// entityAttrVo.setToAttrCiName(toCiVo.getName()); -// } -// if (StringUtils.isBlank(fromAttr)) { -// SceneEntityJoinVo joinVo = attToCiJoinMap.get(toCi); -// if (joinVo != null) { -// entityAttrVo.setFromCiVo(joinVo.getFromCiVo()); -// entityAttrVo.setFromAttrVo(joinVo.getFromAttrVo()); -// } -// } -// } -// if (!sceneEntityAttrList.contains(entityAttrVo)) { -// sceneEntityAttrList.add(entityAttrVo); -// } -// } -// List relElementList = elementMap.get(viewName + "_rel"); -// if (relElementList == null) { -// relElementList = getAllChildElement(resourceElement, "rel"); -// elementMap.put(viewName + "_rel", relElementList); -// } -// for (Element relElement : relElementList) { -// SceneEntityAttrVo entityAttrVo = convertToSceneEntityAttrVo(viewName, relElement, JoinType.REL); -// entityAttrVo.setJoinType(JoinType.REL); -// String fromCi = entityAttrVo.getFromCi(); -// String toCi = entityAttrVo.getToCi(); -// if (StringUtils.isBlank(fromCi) && StringUtils.isBlank(toCi)) { -// throw new ResourceCenterConfigIrregularException(viewName, "rel", entityAttrVo.getField(), "fromCi或toCi"); -// } -// String direction = entityAttrVo.getDirection(); -// if (StringUtils.isBlank(direction)) { -// if (StringUtils.isNotBlank(fromCi) && StringUtils.isNotBlank(toCi)) { -// direction = RelDirectionType.FROM.getValue(); -// } else if (StringUtils.isNotBlank(fromCi)) { -// direction = RelDirectionType.TO.getValue(); -// } else if (StringUtils.isNotBlank(toCi)) { -// direction = RelDirectionType.FROM.getValue(); -// } -// entityAttrVo.setDirection(direction); -// } -// if (Objects.equals(RelDirectionType.FROM.getValue(), direction)) { -// if (StringUtils.isBlank(toCi)) { -// throw new ResourceCenterConfigIrregularException(viewName, "rel", entityAttrVo.getField(), "toCi"); -// } -// CiVo toCiVo = getCiByName(toCi); -// if (toCiVo == null) { -// throw new CiNotFoundException(toCi); -// } -// entityAttrVo.setToCiVo(toCiVo); -// if (StringUtils.isBlank(fromCi)) { -// SceneEntityJoinVo joinVo = relToCiJoinMap.get(toCi); -// entityAttrVo.setFromCiVo(joinVo.getFromCiVo()); -// } else { -// CiVo fromCiVo = getCiByName(fromCi); -// if (fromCiVo == null) { -// throw new CiNotFoundException(fromCi); -// } -// entityAttrVo.setFromCiVo(fromCiVo); -// } -// } else if (Objects.equals(RelDirectionType.TO.getValue(), direction)) { -// if (StringUtils.isBlank(fromCi)) { -// throw new ResourceCenterConfigIrregularException(viewName, "rel", entityAttrVo.getField(), "fromCi"); -// } -// CiVo fromCiVo = getCiByName(fromCi); -// if (fromCiVo == null) { -// throw new CiNotFoundException(fromCi); -// } -// entityAttrVo.setFromCiVo(fromCiVo); -// if (StringUtils.isBlank(toCi)) { -// SceneEntityJoinVo joinVo = relFromCiJoinMap.get(fromCi); -// entityAttrVo.setToCiVo(joinVo.getToCiVo()); -// } else { -// CiVo toCiVo = getCiByName(toCi); -// if (toCiVo == null) { -// throw new CiNotFoundException(toCi); -// } -// entityAttrVo.setToCiVo(toCiVo); -// } -// } -// -// if (Objects.equals(RelDirectionType.TO.getValue(), direction)) { -// String fromAttr = entityAttrVo.getFromAttr(); -// if (StringUtils.isNotBlank(fromAttr)) { -// if (!fromAttr.startsWith("_")) { -// CiVo fromCiVo = entityAttrVo.getFromCiVo(); -// AttrVo attrVo = fromCiVo.getAttrByName(fromAttr); -// if (attrVo == null) { -// throw new AttrNotFoundException(fromCi, fromAttr); -// } -// entityAttrVo.setFromAttrVo(attrVo); -// } -// } else if (relElement.getParent().getName().equalsIgnoreCase("join")) { -// entityAttrVo.setFromAttr("_id"); -// } else { -// throw new ResourceCenterConfigIrregularException(viewName, "rel", entityAttrVo.getField(), "fromAttr"); -// } -// } else { -// String toAttr = entityAttrVo.getToAttr(); -// if (StringUtils.isNotBlank(toAttr)) { -// if (!toAttr.startsWith("_")) { -// CiVo toCiVo = entityAttrVo.getToCiVo(); -// AttrVo attrVo = toCiVo.getAttrByName(toAttr); -// if (attrVo == null) { -// throw new AttrNotFoundException(toCi, toAttr); -// } -// entityAttrVo.setToAttrVo(attrVo); -// } -// } else if (relElement.getParent().getName().equalsIgnoreCase("join")) { -// entityAttrVo.setToAttr("_id"); -// } else { -// throw new ResourceCenterConfigIrregularException(viewName, "rel", entityAttrVo.getField(), "toAttr"); -// } -// } -// if (!sceneEntityAttrList.contains(entityAttrVo)) { -// sceneEntityAttrList.add(entityAttrVo); -// } -// } -// } -// sceneEntityVo.setStatus(Status.PENDING.getValue()); -// } catch (Exception ex) { -// sceneEntityVo.setStatus(Status.ERROR.getValue()); -// sceneEntityVo.setError(ex.getMessage()); -// } -// } - - /** - * 解析join->attr元素 - * @param attrElement 元素信息 - * @param viewName 视图名称 - * @param ciName ci模型名称 - * @return 返回SceneEntityJoinVo对象 - */ - private SceneEntityJoinVo parseJoinAttrElement(Element attrElement, String viewName, String ciName) { - SceneEntityJoinVo joinVo = convertToSceneEntityJoinVo(viewName, attrElement, JoinType.ATTR); - String toCi = joinVo.getToCi(); - if (StringUtils.isBlank(toCi)) { - throw new ResourceCenterConfigIrregularException(viewName, "join->attr", joinVo.getField(), "toCi"); - } - CiVo toCiVo = getCiByName(toCi); - if (toCiVo == null) { - throw new CiNotFoundException(toCi); - } - joinVo.setToCiVo(toCiVo); - String fromCi = joinVo.getFromCi(); - if (StringUtils.isBlank(fromCi)) { - fromCi = ciName; - } - CiVo fromCiVo = getCiByName(fromCi); - if (fromCiVo == null) { - throw new CiNotFoundException(ciName); - } - joinVo.setFromCiVo(fromCiVo); - String fromAttr = joinVo.getFromAttr(); - if (StringUtils.isNotBlank(fromAttr)) { - if (!fromAttr.startsWith("_")) { - AttrVo attrVo = fromCiVo.getAttrByName(fromAttr); - if (attrVo == null) { - throw new AttrNotFoundException(fromCi, fromAttr); - } - joinVo.setFromAttrVo(attrVo); - } - } else if (attrElement.getParent().getName().equalsIgnoreCase("join")) { - joinVo.setFromAttr("_id"); - } else { - throw new ResourceCenterConfigIrregularException(viewName, "attr", joinVo.getField(), "fromAttr"); - } - return joinVo; - } - - /** - * 解析join->rel元素 - * @param relElement 元素信息 - * @param viewName 视图名称 - * @param ciName ci模型名称 - * @return 返回SceneEntityJoinVo对象 - */ - private SceneEntityJoinVo parseJoinRelElement(Element relElement, String viewName, String ciName) { - SceneEntityJoinVo joinVo = convertToSceneEntityJoinVo(viewName, relElement, JoinType.REL); - if (Objects.equals(joinVo.getDirection(), RelDirectionType.FROM.getValue())) { - String toCi = joinVo.getToCi(); - if (StringUtils.isBlank(toCi)) { - throw new ResourceCenterConfigIrregularException(viewName, "join->rel", joinVo.getField(), "toCi"); - } - CiVo toCiVo = getCiByName(toCi); - if (toCiVo == null) { - throw new CiNotFoundException(toCi); - } - joinVo.setToCiVo(toCiVo); - String fromCi = joinVo.getFromCi(); - if (StringUtils.isBlank(fromCi)) { - fromCi = ciName; - } - CiVo fromCiVo = getCiByName(fromCi); - if (fromCiVo == null) { - throw new CiNotFoundException(fromCi); - } - joinVo.setFromCiVo(fromCiVo); - } else { - String fromCi = joinVo.getFromCi(); - if (StringUtils.isBlank(fromCi)) { - throw new ResourceCenterConfigIrregularException(viewName, "join->rel", joinVo.getField(), "fromCi"); - } - CiVo fromCiVo = getCiByName(fromCi); - if (fromCiVo == null) { - throw new CiNotFoundException(fromCi); - } - joinVo.setFromCiVo(fromCiVo); - String toCi = joinVo.getToCi(); - if (StringUtils.isBlank(toCi)) { - toCi = ciName; - } - CiVo toCiVo = getCiByName(toCi); - if (toCiVo == null) { - throw new CiNotFoundException(toCi); - } - joinVo.setToCiVo(toCiVo); - } - return joinVo; - } - - /** - * 解析attr元素 - * @param attrElement 元素信息 - * @param viewName 视图名称 - * @param ciName ci模型名称 - * @param attToCiJoinMap 属性join信息映射 - * @return 返回SceneEntityAttrVo对象 - */ - private SceneEntityAttrVo parseAttrElement(Element attrElement, String viewName, String ciName, Map attToCiJoinMap) { - SceneEntityAttrVo entityAttrVo = convertToSceneEntityAttrVo(viewName, attrElement, JoinType.ATTR); - String fromCi = entityAttrVo.getFromCi(); - if (StringUtils.isBlank(fromCi)) { - fromCi = ciName; - } - CiVo fromCiVo = getCiByName(fromCi); - if (fromCiVo == null) { - throw new CiNotFoundException(fromCi); - } - entityAttrVo.setFromCiVo(fromCiVo); - String fromAttr = entityAttrVo.getFromAttr(); - if (StringUtils.isNotBlank(fromAttr)) { - if (!fromAttr.startsWith("_")) { - AttrVo attrVo = fromCiVo.getAttrByName(fromAttr); - if (attrVo == null) { - throw new AttrNotFoundException(fromCi, fromAttr); - } - entityAttrVo.setFromAttrVo(attrVo); - } - } else if (attrElement.getParent().getName().equalsIgnoreCase("join")) { - entityAttrVo.setFromAttr("_id"); - } - String toCi = entityAttrVo.getToCi(); - if (StringUtils.isNotBlank(toCi)) { - entityAttrVo.setJoinType(JoinType.ATTR); - CiVo toCiVo = getCiByName(toCi); - if (toCiVo == null) { - throw new CiNotFoundException(toCi); - } - entityAttrVo.setToCiVo(toCiVo); - String toAttr = entityAttrVo.getToAttr(); - if (StringUtils.isNotBlank(toAttr)) { - if (!toAttr.startsWith("_")) { - AttrVo attrVo = toCiVo.getAttrByName(toAttr); - if (attrVo == null) { - throw new AttrNotFoundException(toCi, toAttr); - } - entityAttrVo.setToAttrVo(attrVo); - } - } else if (attrElement.getParent().getName().equalsIgnoreCase("join")) { - entityAttrVo.setToAttr("_id"); - } else { - throw new ResourceCenterConfigIrregularException(viewName, "attr", entityAttrVo.getField(), "toAttr"); - } - Integer toCiIsVirtual = toCiVo.getIsVirtual(); - if (Objects.equals(toCiIsVirtual, 1)) { - entityAttrVo.setToAttrCiId(toCiVo.getId()); - entityAttrVo.setToAttrCiName(toCiVo.getName()); - } - if (StringUtils.isBlank(fromAttr)) { - SceneEntityJoinVo joinVo = attToCiJoinMap.get(toCi); - if (joinVo == null) { - throw new ResourceCenterConfigIrregularException(viewName, "attr", entityAttrVo.getField(), "fromAttr"); - } - entityAttrVo.setFromCiVo(joinVo.getFromCiVo()); - entityAttrVo.setFromAttrVo(joinVo.getFromAttrVo()); - } - } - return entityAttrVo; - } - - /** - * 解析attr元素 - * @param relElement 元素信息 - * @param viewName 视图名称 - * @param relFromCiJoinMap 上游关系join信息映射 - * @param relToCiJoinMap 下游关系join信息映射 - * @return 返回SceneEntityAttrVo对象 - */ - private SceneEntityAttrVo parseRelElement(Element relElement, String viewName, Map relFromCiJoinMap, Map relToCiJoinMap) { - SceneEntityAttrVo entityAttrVo = convertToSceneEntityAttrVo(viewName, relElement, JoinType.REL); - entityAttrVo.setJoinType(JoinType.REL); - String fromCi = entityAttrVo.getFromCi(); - String toCi = entityAttrVo.getToCi(); - if (StringUtils.isBlank(fromCi) && StringUtils.isBlank(toCi)) { - throw new ResourceCenterConfigIrregularException(viewName, "rel", entityAttrVo.getField(), "fromCi或toCi"); - } - String direction = entityAttrVo.getDirection(); - if (StringUtils.isBlank(direction)) { - if (StringUtils.isNotBlank(fromCi) && StringUtils.isNotBlank(toCi)) { - direction = RelDirectionType.FROM.getValue(); - } else if (StringUtils.isNotBlank(fromCi)) { - direction = RelDirectionType.TO.getValue(); - } else if (StringUtils.isNotBlank(toCi)) { - direction = RelDirectionType.FROM.getValue(); - } - entityAttrVo.setDirection(direction); - } - if (Objects.equals(RelDirectionType.FROM.getValue(), direction)) { - if (StringUtils.isBlank(toCi)) { - throw new ResourceCenterConfigIrregularException(viewName, "rel", entityAttrVo.getField(), "toCi"); - } - CiVo toCiVo = getCiByName(toCi); - if (toCiVo == null) { - throw new CiNotFoundException(toCi); - } - entityAttrVo.setToCiVo(toCiVo); - if (StringUtils.isBlank(fromCi)) { - SceneEntityJoinVo joinVo = relToCiJoinMap.get(toCi); - entityAttrVo.setFromCiVo(joinVo.getFromCiVo()); - } else { - CiVo fromCiVo = getCiByName(fromCi); - if (fromCiVo == null) { - throw new CiNotFoundException(fromCi); - } - entityAttrVo.setFromCiVo(fromCiVo); - } - } else if (Objects.equals(RelDirectionType.TO.getValue(), direction)) { - if (StringUtils.isBlank(fromCi)) { - throw new ResourceCenterConfigIrregularException(viewName, "rel", entityAttrVo.getField(), "fromCi"); - } - CiVo fromCiVo = getCiByName(fromCi); - if (fromCiVo == null) { - throw new CiNotFoundException(fromCi); - } - entityAttrVo.setFromCiVo(fromCiVo); - if (StringUtils.isBlank(toCi)) { - SceneEntityJoinVo joinVo = relFromCiJoinMap.get(fromCi); - entityAttrVo.setToCiVo(joinVo.getToCiVo()); - } else { - CiVo toCiVo = getCiByName(toCi); - if (toCiVo == null) { - throw new CiNotFoundException(toCi); - } - entityAttrVo.setToCiVo(toCiVo); - } - } - - if (Objects.equals(RelDirectionType.TO.getValue(), direction)) { - String fromAttr = entityAttrVo.getFromAttr(); - if (StringUtils.isNotBlank(fromAttr)) { - if (!fromAttr.startsWith("_")) { - CiVo fromCiVo = entityAttrVo.getFromCiVo(); - AttrVo attrVo = fromCiVo.getAttrByName(fromAttr); - if (attrVo == null) { - throw new AttrNotFoundException(fromCi, fromAttr); - } - entityAttrVo.setFromAttrVo(attrVo); - } - } else if (relElement.getParent().getName().equalsIgnoreCase("join")) { - entityAttrVo.setFromAttr("_id"); - } else { - throw new ResourceCenterConfigIrregularException(viewName, "rel", entityAttrVo.getField(), "fromAttr"); - } - } else { - String toAttr = entityAttrVo.getToAttr(); - if (StringUtils.isNotBlank(toAttr)) { - if (!toAttr.startsWith("_")) { - CiVo toCiVo = entityAttrVo.getToCiVo(); - AttrVo attrVo = toCiVo.getAttrByName(toAttr); - if (attrVo == null) { - throw new AttrNotFoundException(toCi, toAttr); - } - entityAttrVo.setToAttrVo(attrVo); - } - } else if (relElement.getParent().getName().equalsIgnoreCase("join")) { - entityAttrVo.setToAttr("_id"); - } else { - throw new ResourceCenterConfigIrregularException(viewName, "rel", entityAttrVo.getField(), "toAttr"); - } - } - return entityAttrVo; - } - - /** - * 按顺序将标签内容转换成SceneEntityVo - * @param sceneElement - * @return - */ - private void convertToSceneEntityVo(Element sceneElement) { - try { - String viewName = sceneEntityVo.getName(); - String ciName = sceneElement.attributeValue("ci"); - if (StringUtils.isBlank(ciName)) { - throw new ResourceCenterConfigIrregularException(viewName, "ci"); - } - CiVo ciVo = getCiByName(ciName); - if (ciVo == null) { - throw new CiNotFoundException(ciName); - } - sceneEntityVo.setCi(ciVo); - resourceEntityVo.setCiId(ciVo.getId()); - List sceneEntityAttrList = new ArrayList<>(); - sceneEntityVo.setAttrList(sceneEntityAttrList); - Map attToCiJoinMap = new HashMap<>(); - Map relFromCiJoinMap = new HashMap<>(); - Map relToCiJoinMap = new HashMap<>(); - List elementList = sceneElement.elements(); - for (Element element : elementList) { - String name = element.getName(); - if ("join".equals(name)) { - List attrElementList = element.elements("attr"); - if (CollectionUtils.isNotEmpty(attrElementList)) { - for (Element attrElement : attrElementList) { - SceneEntityJoinVo joinVo = parseJoinAttrElement(attrElement, viewName, ciName); - attToCiJoinMap.put(joinVo.getToCi(), joinVo); - SceneEntityAttrVo entityAttrVo = parseAttrElement(attrElement, viewName, ciName, attToCiJoinMap); - if (!sceneEntityAttrList.contains(entityAttrVo)) { - sceneEntityAttrList.add(entityAttrVo); - } - } - } - List relElementList = element.elements("rel"); - if (CollectionUtils.isNotEmpty(relElementList)) { - for (Element relElement : relElementList) { - SceneEntityJoinVo joinVo = parseJoinRelElement(relElement, viewName, ciName); - if (Objects.equals(joinVo.getDirection(), RelDirectionType.FROM.getValue())) { - relToCiJoinMap.put(joinVo.getToCi(), joinVo); - } else { - relFromCiJoinMap.put(joinVo.getFromCi(), joinVo); - } - SceneEntityAttrVo entityAttrVo = parseRelElement(relElement, viewName, relFromCiJoinMap, relToCiJoinMap); - if (!sceneEntityAttrList.contains(entityAttrVo)) { - sceneEntityAttrList.add(entityAttrVo); - } - } - } - } else if ("attr".equals(name)) { - SceneEntityAttrVo entityAttrVo = parseAttrElement(element, viewName, ciName, attToCiJoinMap); - if (!sceneEntityAttrList.contains(entityAttrVo)) { - sceneEntityAttrList.add(entityAttrVo); - } - } else if ("rel".equals(name)) { - SceneEntityAttrVo entityAttrVo = parseRelElement(element, viewName, relFromCiJoinMap, relToCiJoinMap); - if (!sceneEntityAttrList.contains(entityAttrVo)) { - sceneEntityAttrList.add(entityAttrVo); - } - } - } - List definedFieldList = sceneEntityAttrList.stream().map(SceneEntityAttrVo::getField).collect(Collectors.toList()); - List declaredFieldList = ResourceEntityFactory.getFieldNameListByViewName(viewName); - List undefinedFieldList = ListUtils.removeAll(declaredFieldList, definedFieldList); - if (CollectionUtils.isNotEmpty(undefinedFieldList)) { - throw new ResourceCenterViewConfigException(viewName, String.join(",", undefinedFieldList)); - } - sceneEntityVo.setStatus(Status.PENDING.getValue()); - } catch (Exception ex) { - sceneEntityVo.setStatus(Status.ERROR.getValue()); - sceneEntityVo.setError(ex.getMessage()); - } - } - - /** - * 将标签内容转换成SceneEntityAttrVo - * @param element - * @return - */ - private SceneEntityAttrVo convertToSceneEntityAttrVo(String viewName, Element element, JoinType joinType) { - String field = element.attributeValue("field"); -// String resource = element.attributeValue("resource"); - if (StringUtils.isBlank(field)) { - throw new ResourceCenterConfigIrregularException(viewName, joinType, "field"); - } - List fieldList = ResourceEntityFactory.getFieldNameListByViewName(viewName); - if (!fieldList.contains(field)) { - throw new ResourceCenterResourceFoundException(viewName, field); - } -// if (StringUtils.isBlank(resource)) { -// throw new ResourceCenterConfigIrregularException(viewName, joinType, "resource"); -// } -// checkResourceAndField(resource, field); - String direction = element.attributeValue("direction"); - String fromCi = element.attributeValue("fromCi"); - String toCi = element.attributeValue("toCi"); - String fromAttr = element.attributeValue("fromAttr"); - String toAttr = element.attributeValue("toAttr"); - SceneEntityAttrVo attrVo = new SceneEntityAttrVo(); - attrVo.setField(field); -// attrVo.setResource(resource); - attrVo.setDirection(direction); - attrVo.setFromCi(fromCi); - attrVo.setToCi(toCi); - attrVo.setFromAttr(fromAttr); - attrVo.setToAttr(toAttr); - return attrVo; - } - - /** - * 将标签内容转换成SceneEntityJoinVo - * @param viewName - * @param element - * @param joinType - * @return - */ - private SceneEntityJoinVo convertToSceneEntityJoinVo(String viewName, Element element, JoinType joinType) { - String field = element.attributeValue("field"); -// String resource = element.attributeValue("resource"); - if (StringUtils.isBlank(field)) { - throw new ResourceCenterConfigIrregularException(viewName, joinType, "field"); - } - List fieldList = ResourceEntityFactory.getFieldNameListByViewName(viewName); - if (!fieldList.contains(field)) { - throw new ResourceCenterResourceFoundException(viewName, field); - } -// if (StringUtils.isBlank(resource)) { -// throw new ResourceCenterConfigIrregularException(viewName, joinType, "resource"); -// } -// checkResourceAndField(resource, field); - String direction = element.attributeValue("direction"); - String fromCi = element.attributeValue("fromCi"); - String toCi = element.attributeValue("toCi"); - String fromAttr = element.attributeValue("fromAttr"); - String toAttr = element.attributeValue("toAttr"); - SceneEntityJoinVo joinVo = new SceneEntityJoinVo(joinType); - joinVo.setField(field); -// joinVo.setResource(resource); - joinVo.setDirection(direction); - joinVo.setFromCi(fromCi); - joinVo.setToCi(toCi); - joinVo.setFromAttr(fromAttr); - joinVo.setToAttr(toAttr); - return joinVo; - } - - /** - * 检查resource和field是否合格 - * @param resource - * @param field - */ - @Deprecated - private void checkResourceAndField(String resource, String field) { - List resourceEntityList = ResourceEntityFactory.getResourceEntityList(); - Optional optionalEntity = resourceEntityList.stream().filter(e -> Objects.equals(e.getName(), resource)).findFirst(); - if (!optionalEntity.isPresent()) { - throw new ResourceCenterResourceFoundException(resource); - } - ResourceEntityVo resourceEntityVo = optionalEntity.get(); - Set attrList = resourceEntityVo.getAttrList(); - if (CollectionUtils.isEmpty(attrList)) { - throw new ResourceCenterResourceFoundException(resource, field); - } - Optional optionalAttr = attrList.stream().filter(e -> Objects.equals(e.getField(), field)).findFirst(); - if (!optionalAttr.isPresent()) { - throw new ResourceCenterResourceFoundException(resource, field); - } - } - private ResourceEntityVo createResourceEntityVo(ResourceEntityVo resourceEntityVo) { - ResourceEntityVo resourceEntity = new ResourceEntityVo(); - resourceEntity.setName(resourceEntityVo.getName()); - resourceEntity.setLabel(resourceEntityVo.getLabel()); - Set attrList = resourceEntityVo.getAttrList(); - if (CollectionUtils.isNotEmpty(attrList)) { - for (ResourceEntityAttrVo resourceEntityAttrVo : attrList) { - ResourceEntityAttrVo attrVo = new ResourceEntityAttrVo(); - attrVo.setField(resourceEntityAttrVo.getField()); - resourceEntity.addAttr(attrVo); - } - } - return resourceEntity; - } - /** - * 检查模型中是否存在对应属性 - * @param ciVo - * @param attrName - */ - private boolean checkAttrIsExists(CiVo ciVo, String attrName) { - if (defaultAttrList.contains(attrName)) { - return true; - } - List attrList = ciVo.getAttrList(); - if (CollectionUtils.isNotEmpty(attrList)) { - for (AttrVo attrVo : attrList) { - if (Objects.equals(attrVo.getName(), attrName)) { - return true; - } - } - } - throw new AttrNotFoundException(ciVo.getName(), attrName); - } - - - public void buildView() { - if (Objects.equals(type, ViewType.RESOURCE.getValue())) { - buildResource(); - } else if (Objects.equals(type, ViewType.SCENE.getValue())) { - buildScene(); - } - } - - private void buildResource() { - if (StringUtils.isNotBlank(resourceEntityVo.getError())) { - resourceEntityVo.setStatus(Status.ERROR.getValue()); - resourceEntityMapper.updateResourceEntityStatusAndError(resourceEntityVo); - return; - } - Table mainTable = new Table(); - mainTable.setSchemaName(TenantContext.get().getDbName()); - mainTable.setName("cmdb_cientity"); - mainTable.setAlias(new Alias("ci_base")); - Select select = SelectUtils.buildSelectFromTableAndSelectItems(mainTable); - SelectBody selectBody = select.getSelectBody(); - PlainSelect plainSelect = (PlainSelect) selectBody; - - - plainSelect.addJoins(new Join() - .withRightItem(new SubSelect() - .withSelectBody(buildSubSelectForCi(resourceEntityVo.getCi()).getSelectBody()) - .withAlias(new Alias(resourceEntityVo.getName().toLowerCase(Locale.ROOT)))) - .addOnExpression(new EqualsTo().withLeftExpression(new Column() - .withTable(new Table("ci_base")) - .withColumnName("id")) - .withRightExpression(new Column() - .withTable(new Table(resourceEntityVo.getName())) - .withColumnName("id")))); - - if (CollectionUtils.isNotEmpty(resourceEntityVo.getAttrList())) { - for (ResourceEntityAttrVo entityAttr : resourceEntityVo.getAttrList()) { - SelectExpressionItem selectItem = new SelectExpressionItem(); - if (entityAttr.getAttr().startsWith("_")) { - selectItem.setExpression(new Column(entityAttr.getTableAlias() + "." + entityAttr.getAttr().substring(1))); - } else if (entityAttr.getAttrId() != null) { - selectItem.setExpression(new Column(entityAttr.getTableAlias() + ".`" + entityAttr.getAttrId() + "`")); - } - selectItem.setAlias(new Alias(entityAttr.getField().toLowerCase(Locale.ROOT))); - plainSelect.addSelectItems(selectItem); - } - } - - if (CollectionUtils.isNotEmpty(resourceEntityVo.getJoinList())) { - List joinList = new ArrayList<>(); - for (ResourceEntityJoinVo entityJoin : resourceEntityVo.getJoinList()) { - if (entityJoin.getJoinType() == JoinType.ATTR) { - plainSelect.addJoins(new Join() - //.withLeft(true) - .withRightItem(new Table() - .withName("cmdb_attrentity") - .withSchemaName(TenantContext.get().getDbName()) - .withAlias(new Alias("cmdb_attrentity_" + entityJoin.getField().toLowerCase(Locale.ROOT)))) - .addOnExpression(new EqualsTo() - .withLeftExpression(new Column() - .withTable(new Table("ci_base")) - .withColumnName("id")) - .withRightExpression(new Column() - .withTable(new Table("cmdb_attrentity_" + entityJoin.getField().toLowerCase(Locale.ROOT))) - .withColumnName("from_cientity_id")))); - if (StringUtils.isNotBlank(entityJoin.getJoinAttrName())) { - plainSelect.addJoins(new Join() - .withRightItem(new Table() - .withName("cmdb_attr") - .withSchemaName(TenantContext.get().getDbName()) - .withAlias(new Alias("cmdb_attr_" + entityJoin.getJoinAttrName().toLowerCase(Locale.ROOT))) - ).addOnExpression(new AndExpression() - .withLeftExpression(new EqualsTo() - .withLeftExpression(new Column() - .withTable(new Table("cmdb_attr_" + entityJoin.getJoinAttrName().toLowerCase(Locale.ROOT))) - .withColumnName("id")) - .withRightExpression(new Column() - .withTable(new Table("cmdb_attrentity_" + entityJoin.getField().toLowerCase(Locale.ROOT))) - .withColumnName("attr_id"))) - .withRightExpression(new EqualsTo() - .withLeftExpression(new Column() - .withTable(new Table("cmdb_attr_" + entityJoin.getJoinAttrName().toLowerCase(Locale.ROOT))) - .withColumnName("name")) - .withRightExpression(new StringValue(entityJoin.getJoinAttrName())))) - ); - } - plainSelect.addJoins(new Join() - //.withLeft(true) - .withRightItem(new SubSelect() - .withSelectBody(buildSubSelectForCi(entityJoin.getCi()).getSelectBody()) - .withAlias(new Alias("target_cientity_" + entityJoin.getCi().getName().toLowerCase(Locale.ROOT))) - ).addOnExpression(new EqualsTo() - .withLeftExpression(new Column() - .withTable(new Table("cmdb_attrentity_" + entityJoin.getField())) - .withColumnName("to_cientity_id")) - .withRightExpression(new Column() - .withTable(new Table("target_cientity_" + entityJoin.getCi().getName().toLowerCase(Locale.ROOT))) - .withColumnName("id")))); - } else if (entityJoin.getJoinType() == JoinType.REL) { - plainSelect.addJoins(new Join() - .withRightItem(new Table() - .withName("cmdb_relentity") - .withSchemaName(TenantContext.get().getDbName()) - .withAlias(new Alias("cmdb_relentity_" + entityJoin.getField().toLowerCase(Locale.ROOT)))) - .addOnExpression(new EqualsTo() - .withLeftExpression(new Column() - .withTable(new Table("ci_base")) - .withColumnName("id")) - .withRightExpression(new Column() - .withTable(new Table("cmdb_relentity_" + entityJoin.getField().toLowerCase(Locale.ROOT))) - .withColumnName(entityJoin.getDirection().equals(RelDirectionType.FROM.getValue()) ? "from_cientity_id" : "to_cientity_id")))); - - plainSelect.addJoins(new Join() - .withRightItem(new SubSelect() - .withSelectBody(buildSubSelectForCi(entityJoin.getCi()).getSelectBody()) - .withAlias(new Alias("target_cientity_" + entityJoin.getCi().getName().toLowerCase(Locale.ROOT))) - ).addOnExpression(new EqualsTo() - .withLeftExpression(new Column() - .withTable(new Table("cmdb_relentity_" + entityJoin.getField().toLowerCase(Locale.ROOT))) - .withColumnName(entityJoin.getDirection().equals(RelDirectionType.FROM.getValue()) ? "to_cientity_id" : "from_cientity_id")) - .withRightExpression(new Column() - .withTable(new Table("target_cientity_" + entityJoin.getCi().getName().toLowerCase(Locale.ROOT))) - .withColumnName("id")))); - } - } - if (CollectionUtils.isNotEmpty(joinList)) { - plainSelect.addJoins(joinList); - } - } - - String viewName = resourceEntityVo.getName(); - String selectSql = select.toString(); - String md5 = Md5Util.encryptMD5(selectSql); - String tableType = schemaMapper.checkTableOrViewIsExists(TenantContext.get().getDataDbName(), viewName); - if (tableType != null) { - if (Objects.equals(tableType, "SYSTEM VIEW")) { - return; - } else if (Objects.equals(tableType, "VIEW")) { - DataBaseViewInfoVo dataBaseViewInfoVo = dataBaseViewInfoMapper.getDataBaseViewInfoByViewName(viewName); - if (dataBaseViewInfoVo != null) { - if (Objects.equals(md5, dataBaseViewInfoVo.getMd5())) { - return; - } - } - } - } - - try { - EscapeTransactionJob.State s = new EscapeTransactionJob(() -> { - if (Objects.equals(tableType, "BASE TABLE")) { - schemaMapper.deleteTable(TenantContext.get().getDataDbName() + "." + viewName); - } - String sql = "CREATE OR REPLACE VIEW " + TenantContext.get().getDataDbName() + "." + viewName + " AS " + selectSql; - if (logger.isDebugEnabled()) { - logger.debug(sql); - } - schemaMapper.insertView(sql); - }).execute(); - if (s.isSucceed()) { - resourceEntityVo.setError(""); - resourceEntityVo.setStatus(Status.READY.getValue()); - DataBaseViewInfoVo dataBaseViewInfoVo = new DataBaseViewInfoVo(); - dataBaseViewInfoVo.setViewName(viewName); - dataBaseViewInfoVo.setMd5(md5); - dataBaseViewInfoVo.setLcu(UserContext.get().getUserUuid()); - dataBaseViewInfoMapper.insertDataBaseViewInfo(dataBaseViewInfoVo); - } else { - resourceEntityVo.setError(s.getError()); - resourceEntityVo.setStatus(Status.ERROR.getValue()); - } - } catch (Exception ex) { - resourceEntityVo.setError(ex.getMessage()); - resourceEntityVo.setStatus(Status.ERROR.getValue()); - Table table = new Table(); - table.setName(viewName); - table.setSchemaName(TenantContext.get().getDataDbName()); - List columnDefinitions = new ArrayList<>(); - Set attrList = resourceEntityVo.getAttrList(); - for (ResourceEntityAttrVo attrVo : attrList) { - ColumnDefinition columnDefinition = new ColumnDefinition(); - columnDefinition.setColumnName(attrVo.getField()); - columnDefinition.setColDataType(new ColDataType("int")); - columnDefinitions.add(columnDefinition); - } - CreateTable createTable = new CreateTable(); - createTable.setTable(table); - createTable.setColumnDefinitions(columnDefinitions); - createTable.setIfNotExists(true); - EscapeTransactionJob.State s = new EscapeTransactionJob(() -> { - schemaMapper.insertView(createTable.toString()); - }).execute(); - } finally { - resourceEntityMapper.updateResourceEntityStatusAndError(resourceEntityVo); - } - } - private void buildScene() { - //创建场景视图 - ResourceEntityVo resourceEntityVo = new ResourceEntityVo(); - resourceEntityVo.setName(sceneEntityVo.getName()); - resourceEntityVo.setLabel(sceneEntityVo.getLabel()); - resourceEntityVo.setCiId(sceneEntityVo.getCiId()); - if (StringUtils.isNotBlank(sceneEntityVo.getError())) { - resourceEntityVo.setError(sceneEntityVo.getError()); - resourceEntityVo.setStatus(Status.ERROR.getValue()); - resourceEntityMapper.updateResourceEntityStatusAndError(resourceEntityVo); - return; - } - String viewName = sceneEntityVo.getName(); - SceneEntityGenerateSqlUtil sceneEntityGenerateSqlUtil = new SceneEntityGenerateSqlUtil(sceneEntityVo); - String selectSql = sceneEntityGenerateSqlUtil.getSql(); - String md5 = Md5Util.encryptMD5(selectSql); - String tableType = schemaMapper.checkTableOrViewIsExists(TenantContext.get().getDataDbName(), viewName); - if (tableType != null) { - if (Objects.equals(tableType, "SYSTEM VIEW")) { - return; - } else if (Objects.equals(tableType, "VIEW")) { - DataBaseViewInfoVo dataBaseViewInfoVo = dataBaseViewInfoMapper.getDataBaseViewInfoByViewName(viewName); - if (dataBaseViewInfoVo != null) { - // md5相同就不用更新视图了 - if (Objects.equals(md5, dataBaseViewInfoVo.getMd5())) { - return; - } - } - } - } - try { - - EscapeTransactionJob.State s = new EscapeTransactionJob(() -> { - if (Objects.equals(tableType, "BASE TABLE")) { - schemaMapper.deleteTable(TenantContext.get().getDataDbName() + "." + viewName); - } - String sql = "CREATE OR REPLACE VIEW " + TenantContext.get().getDataDbName() + "." + viewName + " AS " + selectSql; - if (logger.isDebugEnabled()) { - logger.debug(sql); - } - schemaMapper.insertView(sql); - }).execute(); - if (s.isSucceed()) { - resourceEntityVo.setError(""); - resourceEntityVo.setStatus(Status.READY.getValue()); - DataBaseViewInfoVo dataBaseViewInfoVo = new DataBaseViewInfoVo(); - dataBaseViewInfoVo.setViewName(viewName); - dataBaseViewInfoVo.setMd5(md5); - dataBaseViewInfoVo.setLcu(UserContext.get().getUserUuid()); - dataBaseViewInfoMapper.insertDataBaseViewInfo(dataBaseViewInfoVo); - } else { - resourceEntityVo.setError(s.getError()); - resourceEntityVo.setStatus(Status.ERROR.getValue()); - } - - } catch (Exception ex) { - resourceEntityVo.setError(ex.getMessage()); - resourceEntityVo.setStatus(Status.ERROR.getValue()); - if (Objects.equals(tableType, "VIEW")) { - schemaMapper.deleteView(TenantContext.get().getDataDbName() + "." + viewName); - } - List fieldNameList = ResourceEntityFactory.getFieldNameListByViewName(viewName); - Table table = new Table(); - table.setName(viewName); - table.setSchemaName(TenantContext.get().getDataDbName()); - List columnDefinitions = new ArrayList<>(); - for (String columnName : fieldNameList) { - ColumnDefinition columnDefinition = new ColumnDefinition(); - columnDefinition.setColumnName(columnName); - columnDefinition.setColDataType(new ColDataType("int")); - columnDefinitions.add(columnDefinition); - } - CreateTable createTable = new CreateTable(); - createTable.setTable(table); - createTable.setColumnDefinitions(columnDefinitions); - createTable.setIfNotExists(true); - EscapeTransactionJob.State s = new EscapeTransactionJob(() -> { - schemaMapper.insertView(createTable.toString()); - }).execute(); - } finally { - resourceEntityMapper.updateResourceEntityStatusAndError(resourceEntityVo); - } - } - private Select buildSubSelectForCi(CiVo ciVo) { - if (CollectionUtils.isNotEmpty(ciVo.getUpwardCiList())) { - Table mainTable = new Table(); - mainTable.setSchemaName(TenantContext.get().getDbName()); - mainTable.setName("cmdb_cientity"); - mainTable.setAlias(new Alias("ci_base")); - Select select = SelectUtils.buildSelectFromTableAndSelectItems(mainTable); - SelectBody selectBody = select.getSelectBody(); - PlainSelect plainSelect = (PlainSelect) selectBody; - //内置属性统一在这里添加 - plainSelect.addSelectItems(new SelectExpressionItem(new Column("id").withTable(new Table("ci_base")))); - plainSelect.addSelectItems(new SelectExpressionItem(new Column("uuid").withTable(new Table("ci_base")))); - plainSelect.addSelectItems(new SelectExpressionItem(new Column("name").withTable(new Table("ci_base")))); - plainSelect.addSelectItems(new SelectExpressionItem(new Column("fcu").withTable(new Table("ci_base")))); - plainSelect.addSelectItems(new SelectExpressionItem(new Column("fcd").withTable(new Table("ci_base")))); - plainSelect.addSelectItems(new SelectExpressionItem(new Column("lcu").withTable(new Table("ci_base")))); - plainSelect.addSelectItems(new SelectExpressionItem(new Column("lcd").withTable(new Table("ci_base")))); - plainSelect.addSelectItems(new SelectExpressionItem(new Column("inspect_status").withTable(new Table("ci_base"))).withAlias(new Alias("inspectStatus"))); - plainSelect.addSelectItems(new SelectExpressionItem(new Column("inspect_time").withTable(new Table("ci_base"))).withAlias(new Alias("inspectTime"))); - plainSelect.addSelectItems(new SelectExpressionItem(new Column("monitor_status").withTable(new Table("ci_base"))).withAlias(new Alias("monitorStatus"))); - plainSelect.addSelectItems(new SelectExpressionItem(new Column("monitor_time").withTable(new Table("ci_base"))).withAlias(new Alias("monitorTime"))); - - - plainSelect.addSelectItems(new SelectExpressionItem(new Column("id").withTable(new Table("ci_info"))).withAlias(new Alias("typeId"))); - plainSelect.addSelectItems(new SelectExpressionItem(new Column("name").withTable(new Table("ci_info"))).withAlias(new Alias("typeName"))); - plainSelect.addSelectItems(new SelectExpressionItem(new Column("label").withTable(new Table("ci_info"))).withAlias(new Alias("typeLabel"))); - for (AttrVo attrVo : ciVo.getAttrList()) { - if (attrVo.getTargetCiId() == null) { - plainSelect.addSelectItems(new SelectExpressionItem(new Column("`" + attrVo.getId() + "`").withTable(new Table("cmdb_" + attrVo.getCiId())))); - } - } - - plainSelect.addJoins(new Join() - .withRightItem(new Table() - .withName("cmdb_ci") - .withAlias(new Alias("ci_info")) - .withSchemaName(TenantContext.get().getDbName())) - .withOnExpression(new EqualsTo() - .withLeftExpression(new Column() - .withTable(new Table("ci_base")) - .withColumnName("ci_id")) - .withRightExpression(new Column() - .withTable(new Table("ci_info")) - .withColumnName("id")))); - - //生成主SQL,需要join所有父模型数据表 - for (CiVo ci : ciVo.getUpwardCiList()) { - plainSelect.addJoins(new Join() - .withRightItem(new Table() - .withName("cmdb_" + ci.getId()) - .withSchemaName(TenantContext.get().getDataDbName()) - .withAlias(new Alias("cmdb_" + ci.getId()))) - .withOnExpression(new EqualsTo() - .withLeftExpression(new Column() - .withTable(new Table("ci_base")) - .withColumnName("id")) - .withRightExpression(new Column() - .withTable(new Table("cmdb_" + ci.getId())) - .withColumnName("cientity_id")))); - - } - return select; - } else { - Table mainTable = new Table(); - mainTable.setSchemaName(TenantContext.get().getDataDbName()); - mainTable.setName("cmdb_" + ciVo.getId()); - return SelectUtils.buildSelectFromTable(mainTable); - } - } -} diff --git a/src/main/resources/neatlogic/resources/cmdb/changelog/2025-02-20/neatlogic_tenant.sql b/src/main/resources/neatlogic/resources/cmdb/changelog/2025-02-20/neatlogic_tenant.sql new file mode 100644 index 0000000000000000000000000000000000000000..683c6b755ed28b10274c9c82924061abbdd46f92 --- /dev/null +++ b/src/main/resources/neatlogic/resources/cmdb/changelog/2025-02-20/neatlogic_tenant.sql @@ -0,0 +1,17 @@ +CREATE TABLE IF NOT EXISTS `cmdb_resourcecenter_applicationlist_display` ( + `id` BIGINT NOT NULL COMMENT '主键ID', + `app_system_id` BIGINT COMMENT '应用系统ID', + `app_module_id` BIGINT COMMENT '应用模块ID', + `env_id` BIGINT COMMENT '环境ID', + `config` LONGTEXT NOT NULL COMMENT '配置信息', + PRIMARY KEY (`id`), + UNIQUE INDEX `uk` (`app_system_id`, `app_module_id`, `env_id`) +) ENGINE = INNODB CHARSET = utf8mb4 COLLATE = utf8mb4_general_ci; + +CREATE TABLE IF NOT EXISTS `cmdb_resourcecenter_assetlist_display` ( + `id` BIGINT NOT NULL COMMENT '主键ID', + `root_ci_name` VARCHAR (100) NOT NULL COMMENT '根模型名称', + `config` LONGTEXT NOT NULL COMMENT '配置', + PRIMARY KEY (`id`), + UNIQUE INDEX `uk` (`root_ci_name`) +) ENGINE = INNODB CHARSET = utf8mb4 COLLATE = utf8mb4_general_ci; \ No newline at end of file diff --git a/src/main/resources/neatlogic/resources/cmdb/changelog/2025-02-20/version.json b/src/main/resources/neatlogic/resources/cmdb/changelog/2025-02-20/version.json new file mode 100644 index 0000000000000000000000000000000000000000..de376c0414da6ccd103e395d7bbbcf54608b8938 --- /dev/null +++ b/src/main/resources/neatlogic/resources/cmdb/changelog/2025-02-20/version.json @@ -0,0 +1,12 @@ +{ + "content": [ + { + "type": "新增功能", + "detail": [ + { + "msg": "1、CMDB应用清单改成模型层级可配置" + } + ] + } + ] +} diff --git a/src/main/resources/neatlogic/resources/cmdb/sqlscript/ddl.sql b/src/main/resources/neatlogic/resources/cmdb/sqlscript/ddl.sql index a2c83ecb67778d27b8009d9e925186677db1dd89..455edd9f0b3700b2655edbf85594df0d713091e3 100644 --- a/src/main/resources/neatlogic/resources/cmdb/sqlscript/ddl.sql +++ b/src/main/resources/neatlogic/resources/cmdb/sqlscript/ddl.sql @@ -951,6 +951,30 @@ CREATE TABLE `cmdb_resourcecenter_account_tag` CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = 'cmdb_resourcecenter_account_tag'; +-- ---------------------------- +-- Table structure for cmdb_resourcecenter_applicationlist_display +-- ---------------------------- +CREATE TABLE IF NOT EXISTS `cmdb_resourcecenter_applicationlist_display` ( + `id` BIGINT NOT NULL COMMENT '主键ID', + `app_system_id` BIGINT COMMENT '应用系统ID', + `app_module_id` BIGINT COMMENT '应用模块ID', + `env_id` BIGINT COMMENT '环境ID', + `config` LONGTEXT NOT NULL COMMENT '配置信息', + PRIMARY KEY (`id`), + UNIQUE INDEX `uk` (`app_system_id`, `app_module_id`, `env_id`) +) ENGINE = INNODB CHARSET = utf8mb4 COLLATE = utf8mb4_general_ci; + +-- ---------------------------- +-- Table structure for cmdb_resourcecenter_applicationlist_display +-- ---------------------------- +CREATE TABLE IF NOT EXISTS `cmdb_resourcecenter_assetlist_display` ( + `id` BIGINT NOT NULL COMMENT '主键ID', + `root_ci_name` VARCHAR (100) NOT NULL COMMENT '根模型名称', + `config` LONGTEXT NOT NULL COMMENT '配置', + PRIMARY KEY (`id`), + UNIQUE INDEX `uk` (`root_ci_name`) +) ENGINE = INNODB CHARSET = utf8mb4 COLLATE = utf8mb4_general_ci; + -- ---------------------------- -- Table structure for cmdb_resourcecenter_config -- ----------------------------