diff --git a/src/main/java/neatlogic/module/cmdb/api/attr/GetCiAttrListForViewApi.java b/src/main/java/neatlogic/module/cmdb/api/attr/GetCiAttrListForViewApi.java new file mode 100644 index 0000000000000000000000000000000000000000..9b50c48fe830199aed2fa446a26299e0f404bcaf --- /dev/null +++ b/src/main/java/neatlogic/module/cmdb/api/attr/GetCiAttrListForViewApi.java @@ -0,0 +1,108 @@ +/* + * Copyright(c) 2023 NeatLogic Co., Ltd. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package neatlogic.module.cmdb.api.attr; + +import com.alibaba.fastjson.JSONObject; +import neatlogic.framework.auth.core.AuthAction; +import neatlogic.framework.cmdb.auth.label.CMDB_BASE; +import neatlogic.framework.cmdb.dto.ci.AttrVo; +import neatlogic.framework.cmdb.dto.ci.CiVo; +import neatlogic.framework.cmdb.exception.ci.CiNotFoundException; +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.dao.mapper.ci.AttrMapper; +import neatlogic.module.cmdb.dao.mapper.ci.CiMapper; +import org.apache.commons.lang3.StringUtils; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.util.ArrayList; +import java.util.List; + +@Service +@AuthAction(action = CMDB_BASE.class) +@OperationType(type = OperationTypeEnum.SEARCH) +public class GetCiAttrListForViewApi extends PrivateApiComponentBase { + + @Resource + private AttrMapper attrMapper; + + @Resource + private CiMapper ciMapper; + + @Override + public String getToken() { + return "/cmdb/ci/listattr/forview"; + } + + @Override + public String getName() { + return "nmcaa.getciattrlistforviewapi.getname"; + } + + @Override + public String getConfig() { + return null; + } + + @Input({ + @Param(name = "ciId", type = ApiParamType.LONG, desc = "term.cmdb.ciid"), + @Param(name = "ciName", type = ApiParamType.STRING, desc = "term.cmdb.ciname") + }) + @Output({ + @Param(name = "Return", explode = AttrVo[].class) + }) + @Description(desc = "nmcaa.getciattrlistforviewapi.getname") + @Override + public Object myDoService(JSONObject jsonObj) throws Exception { + CiVo ciVo = null; + Long ciId = jsonObj.getLong("ciId"); + if (ciId != null) { + ciVo = ciMapper.getCiById(ciId); + if (ciVo == null) { + throw new CiNotFoundException(ciId); + } + } else { + String ciName = jsonObj.getString("ciName"); + if (StringUtils.isBlank(ciName)) { + throw new ParamNotExistsException("ciId", "ciName"); + } + ciVo = ciMapper.getCiByName(ciName); + if (ciVo == null) { + throw new CiNotFoundException(ciName); + } + } + List resultList = new ArrayList<>(); + List attrNameList = new ArrayList<>(); + List upwardCiList = ciMapper.getUpwardCiListByLR(ciVo.getLft(), ciVo.getRht()); + for (CiVo ci : upwardCiList) { + List attrList = attrMapper.getDeclaredAttrListByCiId(ci.getId()); + for (AttrVo attr : attrList) { + if (attrNameList.contains(attr.getName())) { + continue; + } + attrNameList.add(attr.getName()); + resultList.add(attr); + } + } + + return resultList; + } +} diff --git a/src/main/java/neatlogic/module/cmdb/dao/mapper/ci/AttrMapper.java b/src/main/java/neatlogic/module/cmdb/dao/mapper/ci/AttrMapper.java index c3058c332dc4b49a41d1a3220256a40e37d43a64..7ba2aef9ebb94b469f8c43f5b8631695fa5adf45 100644 --- a/src/main/java/neatlogic/module/cmdb/dao/mapper/ci/AttrMapper.java +++ b/src/main/java/neatlogic/module/cmdb/dao/mapper/ci/AttrMapper.java @@ -26,6 +26,8 @@ import java.util.List; public interface AttrMapper extends IAttrCrossoverMapper { AttrVo getAttrByCiIdAndName(@Param("ciId") Long ciId, @Param("attrName") String attrName); + AttrVo getDeclaredAttrByCiIdAndName(@Param("ciId") Long ciId, @Param("name") String attrName); + List getAllNeedTargetCiAttrList(); List searchAttr(AttrVo attrVo); @@ -50,8 +52,7 @@ public interface AttrMapper extends IAttrCrossoverMapper { List getAttrByCiId(Long ciId); - List getAttrBaseInfoByCiId(Long ciId); - + List getDeclaredAttrListByCiId(Long ciId); int checkAttrNameIsRepeat(AttrVo attrVo); diff --git a/src/main/java/neatlogic/module/cmdb/dao/mapper/ci/AttrMapper.xml b/src/main/java/neatlogic/module/cmdb/dao/mapper/ci/AttrMapper.xml index 7e3295895e1021161f2f82ccdbc9941eb6a3cf45..284b0781a39a69776f9b6cf3fc4cf56a9092dcee 100644 --- a/src/main/java/neatlogic/module/cmdb/dao/mapper/ci/AttrMapper.xml +++ b/src/main/java/neatlogic/module/cmdb/dao/mapper/ci/AttrMapper.xml @@ -46,6 +46,28 @@ LIMIT 1 + -