package com.vci.web.service.impl; import com.vci.client.common.datatype.VTDouble; import com.vci.client.common.datatype.VTInteger; import com.vci.client.common.datatype.VTLong; import com.vci.client.common.datatype.VTString; import com.vci.client.mw.ClientSessionUtility; import com.vci.corba.common.PLException; import com.vci.corba.omd.atm.AttributeDef; import com.vci.dto.OsAttributeDTO; import com.vci.dto.OsEnumDTO; import com.vci.dto.OsEnumItemDTO; import com.vci.omd.dataType.VTDataType; import com.vci.pagemodel.OsEnumItemVO; import com.vci.pagemodel.OsEnumVO; import com.vci.pagemodel.OsUsedAttributeVO; import com.vci.po.OsAttributePO; import com.vci.po.OsEnumPO; import com.vci.starter.poi.bo.ReadExcelOption; import com.vci.starter.poi.bo.WriteExcelData; import com.vci.starter.poi.bo.WriteExcelOption; import com.vci.starter.poi.constant.ExcelLangCodeConstant; import com.vci.starter.poi.util.ExcelUtil; 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.pagemodel.BaseQueryObject; import com.vci.starter.web.pagemodel.BaseResult; import com.vci.starter.web.pagemodel.DataGrid; import com.vci.starter.web.util.*; import com.vci.model.OsAttributeDO; import com.vci.pagemodel.OsAttributeVO; import com.vci.web.properties.UsedNames; import com.vci.web.service.OsAttributeServiceI; import com.vci.web.service.OsBaseServiceI; import com.vci.web.service.OsLinkTypeServiceI; import com.vci.web.util.Func; import com.vci.web.util.PlatformClientUtil; import com.vci.web.util.WebUtil; import org.apache.commons.lang3.StringUtils; import org.apache.poi.hssf.util.HSSFColor; 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 org.springframework.web.multipart.MultipartFile; import javax.swing.*; import java.awt.*; import java.io.File; import java.util.*; import java.util.List; import java.util.stream.Collectors; //import static com.vci.client.omd.attribpool.ui.VTDataTypePanel.*; import static com.vci.omd.constants.AttributeConstants.*; /** * 属性池服务 --已经调用了平台的服务,因此不在提供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 Integer NAME_MAX_LENGTH = 28; /** * 系统中变量配置文件中配置的key */ private final String SYSUSEDNAMES = "sysUsedNames"; /** * 数据库中关键字配置文件中配置的key */ private final String DATABASEUSEDNAMES = "dataBaseUsedNames"; /** * 链接类型服务 */ @Autowired(required = false) @Lazy private OsLinkTypeServiceI osLinkTypeServiceI; /** * 业务类型服务 */ @Autowired(required = false) @Lazy private OsBtmServiceImpl osBtmService; /** * 必填列 */ private List ColumnNameisRed = new ArrayList(); /** * 默认的属性 */ 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().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 attrNames * @return */ @Override public List getByAttributeNames(String[] attrNames) throws PLException { VciBaseUtil.alertNotNull(attrNames,"属性名"); List osAttributeVOS = new ArrayList<>(); for (int i = 0; i < attrNames.length; i++) { AttributeDef[] attributeDefs = platformClientUtil.getAttributeService().getAttributeDefs(attrNames[i].toLowerCase(Locale.ROOT),1,1); if(Func.isNotEmpty(attributeDefs)){ Arrays.stream(attributeDefs).forEach(attributeDef -> { osAttributeVOS.add(attributeDO2VO(attributeDef)); }); } } return osAttributeVOS; } /** * 属性的数据对象转换为显示对象 * * @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){ } 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); 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 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 osAttributeDTO * @return true成功,false失败 */ @Override public boolean addAttribute(OsAttributeDTO osAttributeDTO) throws PLException { //判空 VciBaseUtil.alertNotNull( osAttributeDTO,"创建的属性对象", osAttributeDTO.getId(),"属性名称", osAttributeDTO.getAttributeDataType(),"属性类型" ); //属性英文名称校验(判空、系统中判重、是否关键字、是否合规等) checkName(osAttributeDTO.getId()); //检查默认值与属性类型是否匹配 checkDefValue(osAttributeDTO); //dto对象转换为存储所需对象 osAttributeDTO.setOid(VciBaseUtil.getPk().toUpperCase(Locale.ROOT)); AttributeDef attributeDef = this.osAttributeDTO2AttributeDef(osAttributeDTO); return platformClientUtil.getAttributeService().addAttributeDef(attributeDef); } /** * 修改单条属性 * @param osAttributeDTO * @return true成功,false失败 */ @Override public boolean updateAttribute(OsAttributeDTO osAttributeDTO) throws PLException { //判空 VciBaseUtil.alertNotNull( osAttributeDTO,"修改的属性对象", osAttributeDTO.getId(),"属性名称", osAttributeDTO.getTs(),"事务TS", osAttributeDTO.getAttributeDataType(),"属性类型" ); //名称不允许修改所以不用查重 //但是需要检查属性是否存在 OsAttributeVO osAttributeVO = getByAttributeNames(new String[]{osAttributeDTO.getId()}).get(0); if(Func.isEmpty(osAttributeVO) || Func.isBlank(osAttributeVO.getOid())){ throw new PLException("500",new String[]{"属性在系统中不存在,请刷新后重试!"}); } //检查默认值与属性类型是否匹配 checkDefValue(osAttributeDTO); boolean compatible = isCompatible(osAttributeVO,osAttributeDTO); boolean hasInstance = hasInstance(osAttributeDTO.getName()); //产生数据, 并且不兼容 if(hasInstance && !compatible){ throw new PLException("500",new String[]{"无效变更, 不兼容已产生的数据!"}); } String userId = "developer";//WebThreadLocalUtil.getCurrentUserSessionInfoInThread().getUserId(); osAttributeDTO.setLastModifier(userId); osAttributeDTO.setCreator(osAttributeVO.getCreator()); osAttributeDTO.setCreateTime(osAttributeVO.getCreateTime()); osAttributeDTO.setLastModifyTime(new Date()); try { AttributeDef attributeDef = this.osAttributeDTO2AttributeDef(osAttributeDTO); boolean mdSuccess = platformClientUtil.getAttributeService().modifyAttributeDef(attributeDef); if(!mdSuccess){ return false; } //属性修改成功,修改业务类型, 链接类型中该属性字段 boolean alterApBoolean = this.alterAp(attributeDef.name); if(!alterApBoolean){ logger.error("属性修改完成,但在调整业务类型或链接类型中对应属性名的属性时出现错误!"); throw new PLException("500",new String[]{"属性修改完成,但在调整业务类型或链接类型中对应属性名的属性时出现错误!"}); } return true; } catch (PLException e1) { e1.printStackTrace(); } return false; } /** * DTO对象转实际存储所需的AttributeDef对象 * @param osAttributeDTO * @return */ private AttributeDef osAttributeDTO2AttributeDef(OsAttributeDTO osAttributeDTO) { AttributeDef attributeDef = new AttributeDef(); attributeDef.oid = osAttributeDTO.getOid(); attributeDef.name = osAttributeDTO.getId().toLowerCase().replaceAll(" ", ""); attributeDef.label = osAttributeDTO.getName(); attributeDef.description = osAttributeDTO.getDescription(); attributeDef.vtDataType = (String)osAttributeDTO.getAttributeDataType(); attributeDef.defValue = Func.isBlank(osAttributeDTO.getDefaultValue()) ? "" : osAttributeDTO.getDefaultValue(); if(Func.isBlank(osAttributeDTO.getRange())){ attributeDef.rage = ""; }else{ //特殊字符处理,直接存储<会报错 attributeDef.rage = osAttributeDTO.getRange().replace("<","<"); } attributeDef.ts = Func.format((Func.isNotEmpty(osAttributeDTO.getTs()) ? osAttributeDTO.getTs():new Date()),VciDateUtil.DateTimeMillFormat); attributeDef.creator = "developer";//Func.isBlank(osAttributeDTO.getCreator()) ? WebThreadLocalUtil.getCurrentUserSessionInfoInThread().getUserId():osAttributeDTO.getCreator(); attributeDef.createTime = Func.isEmpty(osAttributeDTO.getCreateTime()) ? System.currentTimeMillis():osAttributeDTO.getCreateTime().getTime(); attributeDef.modifier = "developer";//Func.isBlank(osAttributeDTO.getLastModifier()) ? WebThreadLocalUtil.getCurrentUserSessionInfoInThread().getUserId():osAttributeDTO.getLastModifier(); attributeDef.modifyTime = System.currentTimeMillis(); //other需要自行处理 StringBuffer sb = new StringBuffer(); sb.append(ALLOWNULL).append(" = ").append(osAttributeDTO.isNullableFlag() ? "yes" : "no").append(";"); VciFieldTypeEnum fieldTypeEnum = VciFieldTypeEnum.valueOf(osAttributeDTO.getAttributeDataType()); String[] otherInfos = attributeDef.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(osAttributeDTO.getAttrLength() == null){ osAttributeDTO.setAttrLength(20); } if(osAttributeDTO.getPrecisionLength() == null){ osAttributeDTO.setPrecisionLength(2); } sb.append(ACCURACY).append(" = ").append(osAttributeDTO.getPrecisionLength()).append(";"); sb.append(LENGTH).append(" = ").append(length > osAttributeDTO.getAttrLength()?length:osAttributeDTO.getAttrLength()).append(";"); break; case VTInteger: if (StringUtils.isNotBlank(osAttributeDTO.getEnumId())) { sb.append(ENUMNAME).append(" = ").append(osAttributeDTO.getEnumId()).append(";"); } break; case VTString: if (StringUtils.isNotBlank(osAttributeDTO.getBtmTypeId())) { //参照业务类型 sb.append(BTM).append(" = ").append(osAttributeDTO.getBtmTypeId()).append(";"); } if(StringUtils.isNotBlank(osAttributeDTO.getLinkTypeName())){ //参照链接类型 sb.append(LINKTYPENAME).append(" = ").append(osAttributeDTO.getLinkTypeName()).append(";"); } sb.append(LENGTH).append(" = ").append(length > osAttributeDTO.getAttrLength()?length:osAttributeDTO.getAttrLength()).append(";"); if (StringUtils.isNotBlank(osAttributeDTO.getEnumId())) { sb.append(ENUMNAME).append(" = ").append(osAttributeDTO.getEnumId()).append(";"); } break; default: //不需要处理 break; } attributeDef.other = sb.toString(); if (attributeDef.other.endsWith(";")) { attributeDef.other = attributeDef.other.substring(0, attributeDef.other.length() - 1); } return attributeDef; } /** * 修改属性时, 判断当前输入的属性是否能兼容之前的属性 * @param osAttributeVO 数据库中存储的 * @param osAttributeDTO 修改后的内容 * @return */ private boolean isCompatible(OsAttributeVO osAttributeVO/*数据库中存储的*/,OsAttributeDTO osAttributeDTO/*修改后的内容*/){ String dataType = osAttributeVO.getAttributeDataType(); //String other = osAttributeVO.getOther(); //String newOther = abItem.other == null ? "" : abItem.other; String newType = osAttributeDTO.getAttributeDataType(); if(newType.equals(VTDataType.VTSTRING)){ if(dataType.equals(VTDataType.VTINTEGER) || dataType.equals(VTDataType.VTLONG)){ return true; } if(dataType.equals(VTDataType.VTSTRING)){ int length = osAttributeVO.getAttrLength();//Integer.valueOf(getOtherValueByType(other, "length")); int newLen = osAttributeDTO.getAttrLength();//Integer.valueOf(getOtherValueByType(newOther, "length")); if(length <= newLen){ return true; }else{ return false; } } } if(newType.equals(dataType)){ return true; } return false; } /** * 判断该属性是否已经在业务类型中, 或者链接类型中产生了数据 * @param abName * @return */ private boolean hasInstance(String abName) throws PLException { return osBtmService.hasInstance(abName) && osLinkTypeServiceI.hasInstance(abName); } /** * 检查属性名称是否符合规范 * @param attributeName * @return 没有返回值,存在问题直接抛出错误 */ private void checkName(String attributeName) throws PLException { if(attributeName.equals("")){ throw new PLException("500",new String[]{"注意,属性名不能为空!"}); } if(!attributeName.matches("[a-z A-Z]*")){ throw new PLException("500",new String[]{"注意:属性名只能为英文字母!"}); } int length = attributeName.length(); if(length > NAME_MAX_LENGTH){ throw new PLException("500",new String[]{"属性名过长,属性名长度不能超过"+ NAME_MAX_LENGTH}); } String abName = attributeName.toLowerCase(); //检查属性名是否是为系统基础属性,如createTime,ts,oid等 if(usedBySystem(abName)){ throw new PLException("500",new String[]{"属性名无效,原因:属性名已被系统属性使用!"}); } //检查属性名是否是关键字 if(usedByDataBase(abName)){ throw new PLException("500",new String[]{"属性名无效,原因:属性名是数据库关键字!"}); } //检查属性名是否已存在与系统中 if(platformClientUtil.getAttributeService().checkRowIsExists(abName)){ throw new PLException("500",new String[]{"属性名称在系统中已存在!"}); } } /** * 检查默认值与属性类型是否匹配 * @param osAttributeDTO * @return */ private void checkDefValue(OsAttributeDTO osAttributeDTO) throws PLException { String defValue = osAttributeDTO.getDefaultValue(); String vtType = osAttributeDTO.getAttributeDataType(); String rages = osAttributeDTO.getRange(); if(defValue != null && !defValue.equals("")){ if(vtType.equals(VTDataType.VTSTRING)){ try{ String.valueOf(defValue); }catch(Exception e){ throw new PLException("500",new String[]{"请输入String类型的默认值!"}); } if(rages == null || rages.equals("")){ return; } VTString obj = new VTString(String.valueOf(defValue)); boolean flag = obj.checkRageValueByRage(rages); if(!flag){ throw new PLException("500",new String[]{"默认值与值域冲突!"}); } }else if(vtType.equals(VTDataType.VTINTEGER)){ try{ Integer.valueOf(defValue); }catch(Exception e){ throw new PLException("500",new String[]{"请输入Integer类型的默认值!"}); } if(rages == null || rages.equals("")){ return; } VTInteger obj = new VTInteger(Integer.valueOf(defValue)); boolean flag = obj.checkRageValueByRage(rages); if(!flag){ throw new PLException("500",new String[]{"默认值与值域冲突!"}); } }else if(vtType.equals(VTDataType.VTLONG)){ try{ Long.valueOf(defValue); }catch(Exception e){ throw new PLException("500",new String[]{"请输入Long类型的默认值!"}); } if(rages == null || rages.equals("")){ return; } VTLong obj = new VTLong(Long.valueOf(defValue)); boolean flag = obj.checkRageValueByRage(rages); if(!flag){ throw new PLException("500",new String[]{"默认值与值域冲突!"}); } }else if(vtType.equals(VTDataType.VTDOUBLE)){ try{ Double.valueOf(defValue); }catch(Exception e){ throw new PLException("500",new String[]{"请输入Double类型的默认值!"}); } if(rages == null || rages.equals("")){ return; } VTDouble obj = new VTDouble(Double.valueOf(defValue)); boolean flag = obj.checkRageValueByRage(rages); if(!flag){ throw new PLException("500",new String[]{"默认值与值域冲突!"}); } } } } /** * 检查该属性名是否被系统属性使用 * @param abName * @return */ private boolean usedBySystem(String abName) { boolean flag = false; String[] names = UsedNames.getProperty(SYSUSEDNAMES).toUpperCase().split(","); List nameList = Arrays.asList(names); if(nameList.contains(abName.toUpperCase())){ flag = true; } return flag; } /** * 检查该属性名是否属于数据库关键字 * @param abName * @return */ private boolean usedByDataBase(String abName){ boolean flag = false; String[] names = UsedNames.getProperty(DATABASEUSEDNAMES).toUpperCase().split(","); List nameList = Arrays.asList(names); if(nameList.contains(abName.toUpperCase())){ flag = true; } return flag; } /** * 删除属性 * @param osAttributeDTOS * @return true成功,false失败 */ @Override public boolean deleteAttributes(List osAttributeDTOS) throws PLException { VciBaseUtil.alertNotNull(osAttributeDTOS,"待删除的属性列表"); //平台的deleteEnumTypes方法必传三个参数,oid、name和ts List attributeDefs = new ArrayList<>(); for(OsAttributeDTO osAttributeDTO : osAttributeDTOS){ //oid和ts判空 String oid = osAttributeDTO.getOid(); //name主要用来对缓存数据删除 String name = osAttributeDTO.getName(); Date ts = osAttributeDTO.getTs(); if(Func.isBlank(oid) || Func.isBlank(name) || Func.isEmpty(ts)){ throw new PLException("500",new String[]{"待删除的属性列表中主键【oid】、调整时间【ts】、属性名【name】不能为空!"}); } //判断枚举是否有被引用 List> usedAttrList = this.getUsedAttributeList(name); if(Func.isNotEmpty(usedAttrList)){ throw new PLException("500",new String[]{"删除的属性中,属性名称为:【" + name + "】,已被引用!"}); } AttributeDef attributeDef = new AttributeDef(); attributeDef.oid = oid; attributeDef.name = name; attributeDef.ts = Func.format(ts,VciDateUtil.DateTimeMillFormat); attributeDefs.add(attributeDef); } if(Func.isEmpty(attributeDefs)){ return false; } return platformClientUtil.getAttributeService().deleteAttributeDefs(attributeDefs.toArray(new AttributeDef[attributeDefs.size()])); } /** * 查看属性的使用范围 * @param attributeName * @return key:属性 ,value使用该属性的业务类型 */ @Override public List> getUsedAttributeList(String attributeName) throws PLException { if(Func.isBlank(attributeName)){ throw new PLException("500",new String[]{"请选择要查询应用范围的属性!"}); } String[] btNames = platformClientUtil.getBtmService().getBTNamesByAPName(attributeName); if(Func.isEmpty(btNames)){ return new ArrayList<>(); } List> btmNameMapList = new ArrayList<>(); Arrays.stream(btNames).forEach(btName->{ Map itemMap = new HashMap<>(); itemMap.put("attributeName",attributeName); itemMap.put("source",btName); btmNameMapList.add(itemMap); }); return btmNameMapList; } /** * 导出选中的属性 * @param exportFileName 导出的文件名 * @param attrNames 需要导出的属性名称 * @return */ @Override public String exportAttributes(String exportFileName, String attrNames) throws PLException { if(Func.isBlank(attrNames)){ throw new PLException("500",new String[]{"请勾选要导出的属性!"}); } //界面没传名称,使用默认导出名称 exportFileName = Func.isBlank(exportFileName) ? "属性池中属性导出_" + Func.format(new Date(),"yyyy-MM-dd HHmmss.sss"):exportFileName; //设置列名 List columns = new ArrayList<>( Arrays.asList("属性名", "标签", "描述", "属性类型", "允许为空", "默认值", "使用的枚举英文名称(枚举名)", "使用的枚举中文名称(标签)", "参照的业务类型编号", "参照的业务类型名称", "参照的链接类型名称","版本版次","属性长度", "小数精度位数","小数刻度位数","取值范围","创建时间") ); //写excel String excelPath = LocalFileUtil.getDefaultTempFolder() + File.separator + exportFileName + ".xls"; try { new File(excelPath).createNewFile(); } catch (Throwable e) { throw new VciBaseException(LangBaseUtil.getErrorMsg(e), new String[]{excelPath}, e); } //设置列 List excelDataList = new ArrayList<>(); //设置列头 for (int index = 0; index < columns.size(); index++) { excelDataList.add(new WriteExcelData(0,index, columns.get(index))); } //按照属性名查询属性,然后处理属性导出 List enumNameList = Func.toStrList(attrNames); List osAttributeVOS = this.listAttrByIds(enumNameList); if(Func.isEmpty(osAttributeVOS)){ excelDataList.add(new WriteExcelData(1,1, "根据属性名称未查询到属性信息,请刷新后尝试重新导出!")); }else{ //先按照属性类型排序,不同属性类型导出的数据乱的效果 osAttributeVOS.sort(Comparator.comparing(OsAttributeVO::getAttributeDataType)); for (int i = 0; i < osAttributeVOS.size(); i++) { OsAttributeVO osAttributeVO = osAttributeVOS.get(i); excelDataList.add(new WriteExcelData(i+1,0, osAttributeVO.getId())); excelDataList.add(new WriteExcelData(i+1,1, osAttributeVO.getName())); excelDataList.add(new WriteExcelData(i+1,2, osAttributeVO.getDescription())); excelDataList.add(new WriteExcelData(i+1,3, osAttributeVO.getAttributeDataTypeText()+"("+osAttributeVO.getAttributeDataType()+")")); excelDataList.add(new WriteExcelData(i+1,4, osAttributeVO.isNullableFlag())); excelDataList.add(new WriteExcelData(i+1,5, osAttributeVO.getDefaultValue())); excelDataList.add(new WriteExcelData(i+1,6, osAttributeVO.getEnumId())); excelDataList.add(new WriteExcelData(i+1,7, osAttributeVO.getEnumName())); excelDataList.add(new WriteExcelData(i+1,8, osAttributeVO.getBtmTypeId())); excelDataList.add(new WriteExcelData(i+1,9, osAttributeVO.getBtmTypeName())); excelDataList.add(new WriteExcelData(i+1,10, osAttributeVO.getAttrLength())); excelDataList.add(new WriteExcelData(i+1,11, osAttributeVO.getLinkTypeName())); excelDataList.add(new WriteExcelData(i+1,12, osAttributeVO.getVersion())); excelDataList.add(new WriteExcelData(i+1,13, osAttributeVO.getPrecisionLength())); excelDataList.add(new WriteExcelData(i+1,14, osAttributeVO.getScaleLength())); excelDataList.add(new WriteExcelData(i+1,15, osAttributeVO.getRange())); excelDataList.add(new WriteExcelData(i+1,16, osAttributeVO.getCreateTime())); } } WriteExcelOption excelOption = new WriteExcelOption(excelDataList); ExcelUtil.writeDataToFile(excelPath, excelOption); return excelPath; } /** * 下载属性导入模板 * @param exportFileName * @return * @throws PLException */ @Override public String downloadAttributeTemplate(String exportFileName) throws Exception { //界面没传名称,使用默认导出名称 exportFileName = Func.isBlank(exportFileName) ? "属性池导入模板_" + Func.format(new Date(),"yyyy-MM-dd HHmmss.sss"):exportFileName; //设置列名 List columns = new ArrayList<>( Arrays.asList("属性名", "标签", "描述", "属性类型", "允许为空(是/否)", "默认值", "使用的枚举英文名称", "参照的业务类型编号", "参照的链接类型编号","版本版次","属性长度", "小数精度位数","小数刻度位数","取值范围") ); //设置必填列 ColumnNameisRed.clear(); ColumnNameisRed.add(0); ColumnNameisRed.add(3); ColumnNameisRed.add(10); //写excel String excelPath = LocalFileUtil.getDefaultTempFolder() + File.separator + exportFileName + ".xls"; try { new File(excelPath).createNewFile(); } catch (Throwable e) { throw new VciBaseException(LangBaseUtil.getErrorMsg(e), new String[]{excelPath}, e); } //设置列 List excelDataList = new ArrayList<>(); //设置列头 for (int index = 0; index < columns.size(); index++) { //判断是否为必填列,给必填列设置颜色 if(ColumnNameisRed.contains(index)){ WriteExcelData excelData = new WriteExcelData(0, index, columns.get(index)); excelData.setFontColor(String.valueOf(HSSFColor.HSSFColorPredefined.RED.getIndex())); excelDataList.add(excelData); }else{ excelDataList.add(new WriteExcelData(0,index, columns.get(index))); } } WriteExcelOption excelOption = new WriteExcelOption(excelDataList); ExcelUtil.writeDataToFile(excelPath, excelOption); return excelPath; } /** * 导入属性 * @param file * @return */ @Override public BaseResult importAttributes(File file) throws Exception{ VciBaseUtil.alertNotNull(file,"excel文件"); if(!file.exists()){ throw new VciBaseException("导入的excel文件不存在,{0}",new String[]{file.getPath()}); } try{ //1、读取excel中的数据,组成对象 ReadExcelOption excelOption = new ReadExcelOption(); List poList = ExcelUtil.readDataObjectFromExcel(file, OsAttributePO.class,excelOption,(value, po, fieldName)->{}); //去除都是空的情况 if(CollectionUtils.isEmpty(poList)){ return BaseResult.fail(ExcelLangCodeConstant.IMPORT_CONTENT_NULL,new String[]{}); } //excel判重,数据校验,dto对象转换,存储对象转换,执行保存 List dtoList = new ArrayList<>(); //当前excel中是否重复用的判重Map:(key:判重属性,value:行号) Map excelReapeat = new HashMap<>(); //判断必填属性是否为空,用户是否已存在,以及部门是否填错等校验逻辑 poList.stream().forEach(osAttributePO -> { if(Func.isBlank(osAttributePO.getId())){//属性名判空 throw new VciBaseException("第【"+osAttributePO.getRowIndex()+"】行,enumnameerror"); }else if(Func.isBlank(osAttributePO.getAttributeDataType())){ throw new VciBaseException("第【"+osAttributePO.getRowIndex()+"】行,typeerror"); }else if(excelReapeat.containsKey(osAttributePO.getId())){//属性名表格中判重 throw new VciBaseException("第【"+excelReapeat.get(osAttributePO.getId())+"】行和第【"+osAttributePO.getRowIndex()+"】行数据,属性名重复"); } //属性名校验 try { checkName(osAttributePO.getId()); } catch (PLException e) { e.printStackTrace(); throw new VciBaseException(VciBaseUtil.getExceptionMessage(e)); } //属性名excel中判重处理 excelReapeat.put(osAttributePO.getId(),osAttributePO.getRowIndex()); OsAttributeDTO osAttributeDTO = new OsAttributeDTO(); osAttributeDTO.setOid(VciBaseUtil.getPk().toUpperCase(Locale.ROOT)); osAttributeDTO.setId(osAttributePO.getId()); osAttributeDTO.setName(osAttributePO.getName()); osAttributeDTO.setDescription(osAttributePO.getDescription()); osAttributeDTO.setDefaultValue(osAttributePO.getDefaultValue()); osAttributeDTO.setAttrLength(osAttributePO.getAttrLength()); osAttributeDTO.setAttributeDataType(osAttributePO.getAttributeDataType()); osAttributeDTO.setBtmTypeId(osAttributePO.getBtmTypeId()); //osAttributeDTO.setBtmTypeName(osAttributePO.getBtmname()); osAttributeDTO.setLinkTypeName(osAttributePO.getLinkTypeName()); osAttributeDTO.setVersion(osAttributePO.getVersion()); osAttributeDTO.setEnumId(osAttributePO.getEnumId()); //osAttributeDTO.setEnumName(osAttributePO.getEnumId()); osAttributeDTO.setPrecisionLength(osAttributePO.getPrecisionLength()); osAttributeDTO.setScaleLength(osAttributePO.getScaleLength()); osAttributeDTO.setRange(osAttributePO.getRange()); osAttributeDTO.setNullableFlag("是".equals(osAttributePO.getNullableFlag()) ? true:false); try { //检查默认值与属性类型是否匹配 checkDefValue(osAttributeDTO); } catch (PLException e) { e.printStackTrace(); throw new VciBaseException(e.getMessage()); } dtoList.add(osAttributeDTO); }); //执行保存操作 dtoList.stream().forEach(dto->{ try { boolean b = platformClientUtil.getAttributeService().addAttributeDef(osAttributeDTO2AttributeDef(dto)); if(!b){ throw new VciBaseException("save and return false"); } } catch (PLException e) { e.printStackTrace(); throw new VciBaseException("执行保存时出现错误,错误属性对象名为:【" + dto.getId() + "】,原因:"+e.getMessage()); } }); }catch (Exception e){ if(logger.isErrorEnabled()){ logger.error("读取excel内容时或保存用户信息时出现了错误,具体原因:",VciBaseUtil.getExceptionMessage(e)); } e.printStackTrace(); return BaseResult.fail(VciBaseUtil.getExceptionMessage(e),new String[]{},e); } return BaseResult.success("枚举导入成功!"); } /** * 是否默认的属性 * * @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("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; } /** * 清除缓存 */ @Override public void clearCache() { } /** * 调用修改业务类型和连接类型中对应属性名的属性 * @param apName * @return * @throws PLException */ private boolean alterAp(String apName) throws PLException { return osBtmService.alterAp(apName) && osLinkTypeServiceI.alterAp(apName); } }