package com.vci.web.service.impl; import com.vci.corba.common.PLException; import com.vci.corba.omd.atm.AttributeDef; import com.vci.enumpck.UI.ItemTypeEnum; import com.vci.pagemodel.OsAttributeVO; import com.vci.starter.web.annotation.log.VciUnLog; import com.vci.starter.web.enumpck.VciFieldTypeEnum; import com.vci.starter.web.exception.VciBaseException; import com.vci.starter.web.util.Lcm.Func; import com.vci.starter.web.util.VciBaseUtil; import com.vci.starter.web.util.VciDateUtil; import com.vci.web.service.WebAttributeServiceI; import com.vci.web.util.PlatformClientUtil; import com.vci.web.util.WebUtil; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; import java.util.*; import java.util.stream.Collectors; /** * @Description 属性服务 * @Author dangsn * @Date 2024/11/28 14:26 */ @Service public class WebAttributeServiceImpl implements WebAttributeServiceI { /** * 日志 */ private Logger logger = LoggerFactory.getLogger(getClass()); /** * 平台的调用工具类 */ @Autowired private PlatformClientUtil platformClientUtil; /** * 使用属性编号获取对象--批量 * * @param attrCodes 属性的英文名称 * @return 属性的显示对象 */ @Override public List listAttrByIds(Collection attrCodes) { if(CollectionUtils.isEmpty(attrCodes)){ return null; } Map attributeVOMap = selectAllAttributeMap(); List attributeVOS = new ArrayList<>(); attrCodes.stream().forEach(attrCode->{ OsAttributeVO attributeVO = attributeVOMap.getOrDefault(attrCode.toLowerCase(),null); if(attributeVO!=null){ attributeVOS.add(attributeVO); } }); return attributeVOS; } /** * 使用属性编号获取对象--批量 * * @param attrCodes 属性的英文名称 * @param attributeVOMap 属性对象 * @return 属性的显示对象 */ @Override public List listAttrByIds(Collection attrCodes,Map attributeVOMap) { if(CollectionUtils.isEmpty(attrCodes)){ return null; } if(attributeVOMap == null){ attributeVOMap = selectAllAttributeMap(); } List attributeVOS = new ArrayList<>(); Map finalAttributeVOMap = attributeVOMap; attrCodes.stream().forEach(attrCode->{ OsAttributeVO attributeVO = finalAttributeVOMap.getOrDefault(attrCode.toLowerCase(),null); if(attributeVO!=null){ attributeVOS.add(attributeVO); } }); return attributeVOS; } /** * 查询所有的属性 * * @return 属性的显示对象 */ @Override public List selectAllAttribute() { //后面两个分页数,完全没有用 try { return attributeDO2VOs(Arrays.stream(platformClientUtil.getAttributeService().getAttributeDefs("",1,1)).collect(Collectors.toList())); } catch (PLException vciError) { throw WebUtil.getVciBaseException(vciError); } } /** * 查询所有的属性映射 * * @return key是属性的英文名称小写,value是属性的显示对象 */ @Override @VciUnLog public Map selectAllAttributeMap() { return Optional.ofNullable(selectAllAttribute()).orElseGet(()->new ArrayList<>()).stream().collect(Collectors.toMap(s->s.getId().toLowerCase(),t->t,(o1,o2)->o1)); } /** * 属性的数据对象转换为显示对象 * * @param attribItems 数据对象 * @return 显示对象 */ @Override public List attributeDO2VOs(Collection attribItems) { List vos = new ArrayList<>(); Optional.ofNullable(attribItems).orElseGet(()->new ArrayList<>()).stream().forEach(attribItem -> { vos.add(attributeDO2VO(attribItem)); }); return vos; } /** * 属性的数据对象转换为显示对象 * * @param attribItem 数据对象 * @return 显示对象 */ @Override public OsAttributeVO attributeDO2VO(AttributeDef attribItem) { OsAttributeVO attributeVO = new OsAttributeVO(); if(attribItem!=null){ attributeVO.setOid(attribItem.oid); attributeVO.setId(attribItem.name); attributeVO.setCreator(attribItem.creator); try { attributeVO.setCreateTime(new Date(attribItem.createTime)); attributeVO.setLastModifyTime(new Date(attribItem.modifyTime)); attributeVO.setTs(VciDateUtil.str2Date(attribItem.ts,VciDateUtil.DateTimeMillFormat)); }catch (Throwable e){ e.printStackTrace(); String errorLog = "属性DO转VO时出错,原因:"+ VciBaseUtil.getExceptionMessage(e); logger.error(errorLog); throw new VciBaseException(errorLog); } attributeVO.setLastModifier(attribItem.modifier); attributeVO.setName(attribItem.label); attributeVO.setDescription(attribItem.description); attributeVO.setAttributeDataType(attribItem.vtDataType); attributeVO.setAttributeDataTypeText(VciFieldTypeEnum.getTextByValue(attribItem.vtDataType)); //获取UI属性类型 attributeVO.setAttributeUIType(ItemTypeEnum.convertAttributeTypeTOUITypeTextByValue(attribItem.vtDataType,false)); //获取UI属性类型文本 attributeVO.setAttributeUITypeText(ItemTypeEnum.convertAttributeTypeTOUITypeTextByValue(attribItem.vtDataType,true)); attributeVO.setDefaultValue(attribItem.defValue); if(Func.isNotBlank(attribItem.rage)){ attributeVO.setRange(attribItem.rage.replace("<","<")); }else{ attributeVO.setRange(attribItem.rage); } attributeVO.setOther(attribItem.other); //处理参照相关属性 if(StringUtils.isNotBlank(attribItem.other)) { if (isReferAttr(attribItem.other)) { //说明这个的确是参照字段 String[] others = attribItem.other.split(";"); for (String s : others) { if (s.toLowerCase().contains("btm") && s.split("=").length > 1) {//必须要判断长度,因为枚举的时候也是包含这个btm的 attributeVO.setBtmTypeId(s.split("=")[1].trim()); } //链接类型不支持 if (s.toLowerCase().contains("link") && s.split("=").length > 1) {//必须要判断长度,因为枚举的时候也是包含这个btm的 attributeVO.setLinkTypeName(s.split("=")[1].trim()); } if (s.toLowerCase().contains("version") && s.split("=").length > 1) {//必须要判断长度,因为枚举的时候也是包含这个btm的 attributeVO.setVersion(WebUtil.getInt(s.split("=")[1].trim())); } } } //必输和长度 String[] others = attribItem.other.split(";"); for (String s : others) { if (s.toLowerCase().contains("allownull") && s.split("=").length > 1) {//必须要判断长度,因为枚举的时候也是包含这个btm的 boolean allownull = false; if (s.split("=")[1].trim().toLowerCase().equals("yes")) { allownull = true; } attributeVO.setNullableFlag(allownull); } if (s.toLowerCase().indexOf("length") > -1 && s.split("=").length > 1) { int length = WebUtil.getInt(s.split("=")[1].trim()); if (length > 0) { attributeVO.setAttrLength(length); } } } //枚举 if(isEnumAttr(attribItem.other)){ for (String s : others) { if(s.contains("enumName") && s.split("=").length>1) { attributeVO.setEnumId(s.split("=")[1].trim()); } } } } } return attributeVO; } /** * 是否为参照属性 * @param other 配置的其他 * @return true 是参照 */ private boolean isReferAttr(String other){ if(StringUtils.isNotBlank(other) && (other.toLowerCase().contains("btm") || other.toLowerCase().contains("link"))){ //还不能确定,因为枚举的时候也会设置btm String[] temp = other.split(";"); for(String s : temp){ if((s.contains("btm") || s.contains("link")) && s.split("=").length>1){ return true; } } } return false; } /** * 是否为枚举的属性 * @param other 配置的内容 * @return true 是枚举 */ private boolean isEnumAttr(String other){ if(StringUtils.isNotBlank(other) && other.contains("enumName")){ //还不能确定,因为枚举的时候也会设置btm String[] temp = other.split(";"); for(String s : temp){ if(s.contains("enumName")&& s.split("=").length>1){ return true; } } } return false; } }