package com.vci.web.service.impl; import com.vci.client.common.providers.ClientServiceProvider; import com.vci.corba.common.PLException; import com.vci.corba.omd.atm.AttributeDef; import com.vci.starter.web.annotation.log.VciUnLog; import com.vci.starter.web.enumpck.VciFieldTypeEnum; import com.vci.starter.web.pagemodel.BaseQueryObject; import com.vci.starter.web.pagemodel.DataGrid; import com.vci.starter.web.util.VciBaseUtil; import com.vci.starter.web.util.VciDateUtil; import com.vci.web.model.OsAttributeDO; import com.vci.web.pageModel.OsAttributeVO; import com.vci.web.service.OsAttributeServiceI; 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.context.annotation.Lazy; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; import java.util.*; import java.util.stream.Collectors; import static com.vci.client.omd.attribpool.ui.VTDataTypePanel.*; /** * 属性池服务 --已经调用了平台的服务,因此不在提供Dao层 * @author weidy * @date 2021-2-15 */ @Service public class OsAttributeServiceImpl implements OsAttributeServiceI { /** * 日志 */ private Logger logger = LoggerFactory.getLogger(getClass()); /** * 平台的调用工具类 */ @Autowired private PlatformClientUtil platformClientUtil; /** * 加载自身 */ @Autowired(required = false) @Lazy private OsAttributeServiceI self; /** * 默认的属性 */ private static List defaultAttributeVOs = new ArrayList<>(); /** * 默认属性的映射,key是小写 */ private static Map defaultAttributeVOMap = new HashMap<>(); /** * 获取默认的属性 * @return 默认的属性列表 */ @Override public List getDefaultAttributeVOs() { return OsAttributeServiceImpl.defaultAttributeVOs; } /** * 设置默认的属性 * @param defaultAttributeVOs 默认的属性列表 */ @Override public void setDefaultAttributeVOs(List defaultAttributeVOs) { OsAttributeServiceImpl.defaultAttributeVOs = defaultAttributeVOs; } /** * 获取的默认属性的映射 * @return 默认的属性映射 */ @Override public Map getDefaultAttributeVOMap() { return OsAttributeServiceImpl.defaultAttributeVOMap; } /** * 设置默认的属性的映射 * @param defaultAttributeVOMap 默认的属性映射 */ @Override public void setDefaultAttributeVOMap(Map defaultAttributeVOMap) { OsAttributeServiceImpl.defaultAttributeVOMap = defaultAttributeVOMap; } /** * 查询所有的属性 * * @return 属性的显示对象 */ @Override public List selectAllAttribute() { //后面两个分页数,完全没有用 try { // return attributeDO2VOs(Arrays.stream(platformClientUtil.getAttributeService().getAttribItems(abFilter,1,1)).collect(Collectors.toList())); // return attributeDO2VOs(Arrays.stream(ServiceProvider.getOMDService().getAttributeService().getAttribItems(abFilter.name,1,1)).collect(Collectors.toList())); return attributeDO2VOs(Arrays.stream(ClientServiceProvider.getOMDService().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(self.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(VciDateUtil.str2Date(String.valueOf(attribItem.createTime),VciDateUtil.DateTimeFormat)); attributeVO.setLastModifyTime(VciDateUtil.str2Date(String.valueOf(attribItem.modifyTime),VciDateUtil.DateTimeFormat)); attributeVO.setTs(VciDateUtil.str2Date(attribItem.ts,VciDateUtil.DateTimeMillFormat)); }catch (Throwable e){ } attributeVO.setLastModifier(attribItem.modifier); attributeVO.setName(attribItem.label); attributeVO.setDescription(attribItem.description); attributeVO.setAttributeDataType(attribItem.vtDataType); attributeVO.setAttributeDataTypeText(VciFieldTypeEnum.getTextByValue(attribItem.vtDataType)); attributeVO.setDefaultValue(attribItem.defValue); attributeVO.setRange(attribItem.rage); 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()); } //链接类型不支持 } } //必输和长度 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 attributeVO 显示对象 * @return 数据对象 */ @Override public AttributeDef attributeVO2DO(OsAttributeVO attributeVO) { AttributeDef attribItem = new AttributeDef(); attribItem.oid = attributeVO.getOid(); attribItem.ts = VciDateUtil.date2Str(attributeVO.getTs(),VciDateUtil.DateTimeFormat); attribItem.creator = attributeVO.getCreator(); attribItem.createTime = attributeVO.getCreateTime() != null?attributeVO.getCreateTime().getTime():null; attribItem.modifier = attributeVO.getLastModifier(); attribItem.modifyTime = attributeVO.getLastModifyTime() != null ? attributeVO.getLastModifyTime().getTime():null; attribItem.name = attributeVO.getId(); attribItem.label = attributeVO.getName(); attribItem.description = attributeVO.getDescription() == null ?"":attributeVO.getDescription(); attribItem.vtDataType = attributeVO.getAttributeDataType() == null ? VciFieldTypeEnum.VTString.name() : attributeVO.getAttributeDataType(); attribItem.defValue = attributeVO.getDefaultValue() == null?"":attributeVO.getDefaultValue(); attribItem.rage = attributeVO.getRange() == null ? "" : attributeVO.getRange(); //other需要自行处理 StringBuffer sb = new StringBuffer(); sb.append(ALLOWNULL).append(" = ").append(attributeVO.isNullableFlag() ? "yes" : "no").append(";"); VciFieldTypeEnum fieldTypeEnum = VciFieldTypeEnum.valueOf(attributeVO.getAttributeDataType()); String[] otherInfos = attribItem.other.split(";"); int length = 0; if(otherInfos!=null&& otherInfos.length > 0){ for(String s : otherInfos){ if(s.contains(LENGTH+" =") || s.contains(LENGTH+"=")){ length = VciBaseUtil.getInt(s.split("=")[1]); break; } } } switch (fieldTypeEnum) { case VTDouble: if(attributeVO.getAttrLength() == null){ attributeVO.setAttrLength(20); } if(attributeVO.getPrecisionLength() == null){ attributeVO.setPrecisionLength(2); } sb.append(ACCURACY).append(" = ").append(attributeVO.getPrecisionLength()).append(";"); sb.append(LENGTH).append(" = ").append(length > attributeVO.getAttrLength()?length:attributeVO.getAttrLength()).append(";"); break; case VTInteger: if (StringUtils.isNotBlank(attributeVO.getEnumId())) { sb.append(ENUMNAME).append(" = ").append(attributeVO.getEnumId()).append(";"); } break; case VTString: if (StringUtils.isNotBlank(attributeVO.getBtmTypeId())) { //参照 sb.append(BTM).append(" = ").append(attributeVO.getBtmTypeId()).append(";"); //链接类型暂时不支持 } sb.append(LENGTH).append(" = ").append(length > attributeVO.getAttrLength()?length:attributeVO.getAttrLength()).append(";"); if (StringUtils.isNotBlank(attributeVO.getEnumId())) { sb.append(ENUMNAME).append(" = ").append(attributeVO.getEnumId()).append(";"); } break; default: //不需要处理 break; } attribItem.other = sb.toString(); if (attribItem.other.endsWith(";")) { attribItem.other = attribItem.other.substring(0, attribItem.other.length() - 1); } return attribItem; } /** * 根据属性的名称获取属性对象 * @param attrCode 属性英文名称 * @return 属性的基本信息 */ @Override public OsAttributeVO getAttr(String attrCode) { if(StringUtils.isBlank(attrCode)){ return null; } return self.selectAllAttributeMap().getOrDefault(attrCode.toLowerCase(),null); } /** * 使用属性编号获取对象--批量 * * @param attrCodes 属性的英文名称 * @return 属性的显示对象 */ @Override public List listAttrByIds(Collection attrCodes) { if(CollectionUtils.isEmpty(attrCodes)){ return null; } Map attributeVOMap = self.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 attribItemList 属性的列表 */ @Override public void batchAddAttribute(List attribItemList) { if(!CollectionUtils.isEmpty(attribItemList)){ attribItemList.stream().forEach(attribItem -> { try { platformClientUtil.getAttributeService().addAttributeDef(attribItem); } catch (PLException e) { throw WebUtil.getVciBaseException(e); } }); } } /** * 批量编辑属性 * @param editAttrList 属性的列表 */ @Override public void batchEditAttribute(List editAttrList) { if(!CollectionUtils.isEmpty(editAttrList)){ editAttrList.stream().forEach(attribItem -> { try { platformClientUtil.getAttributeService().modifyAttributeDef(attribItem); } catch (PLException e) { throw WebUtil.getVciBaseException(e); } }); } } /** * 属性列表 * * @param baseQueryObject 查询对象 * @return 属性的显示对象 */ @Override public DataGrid gridAttribute(BaseQueryObject baseQueryObject) { return gridObject(baseQueryObject, OsAttributeDO.class,self.selectAllAttributeMap(),OsAttributeVO.class); } /** * 是否默认的属性 * * @param attr 属性编号 * @return true表示是默认属性 */ @Override public boolean isDefaultAttr(String attr) { if(StringUtils.isBlank(attr)){ return false; } return getDefaultAttributeVOMap().containsKey(attr.toLowerCase(Locale.ROOT)); } /** * 是否为参照属性 * @param other 配置的其他 * @return true 是参照 */ private boolean isReferAttr(String other){ if(StringUtils.isNotBlank(other) && (other.toLowerCase().contains("btm") || other.toLowerCase().contains("ltm"))){ //还不能确定,因为枚举的时候也会设置btm String[] temp = other.split(";"); for(String s : temp){ if((s.contains("btm") || s.contains("ltm")) && 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; } /** * 清除缓存 */ @Override public void clearCache() { } }