package com.vci.web.service.impl; import com.vci.corba.common.PLException; import com.vci.corba.omd.ltm.LinkType; 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.BeanUtil; import com.vci.starter.web.util.VciBaseUtil; import com.vci.starter.web.util.VciDateUtil; import com.vci.web.constant.CacheKeyConstant; import com.vci.web.model.OsLinkTypeDO; import com.vci.web.pageModel.*; import com.vci.web.service.*; 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.cache.annotation.CacheEvict; import org.springframework.cache.annotation.Cacheable; 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.web.constant.CacheNameConstant.VCI_OBJECT_SERVICE; /** * 链接类型服务 * @author weidy * @date 2021-2-15 */ @Service public class OsLinkTypeServiceImpl implements OsLinkTypeServiceI { /** * 日志 */ private Logger logger = LoggerFactory.getLogger(getClass()); /** * 平台的调用工具类 */ @Autowired private PlatformClientUtil platformClientUtil; /** * 属性的服务 */ @Autowired private OsAttributeServiceI attributeService; /** * 枚举的服务 */ @Autowired private OsEnumServiceI enumService; /** * 业务类型服务 */ @Autowired private OsBtmServiceI btmService; /** * 业务数据查询服务 */ @Autowired private WebBoServiceI boService; /** * 加载自身 */ @Autowired(required = false) @Lazy private OsLinkTypeServiceI self; /** * 查询所有的链接类型 * * @return 链接类型对象 */ @Override @VciUnLog public List selectAllLink() { try { return linkTypeDO2VOs(Arrays.stream(platformClientUtil.getLinkTypeService().getLinkTypes()).collect(Collectors.toList())); } catch (PLException vciError) { throw WebUtil.getVciBaseException(vciError); } } /** * 查询所有的业务类型映射 * * @return key 是业务的英文名称的小写 */ @Override @VciUnLog @Cacheable(value = VCI_OBJECT_SERVICE,key = CacheKeyConstant.ALL_Link,unless = "#result == null") public Map selectAllLinkMap() { return Optional.ofNullable(self.selectAllLink()).orElseGet(()->new ArrayList<>()).stream().collect(Collectors.toMap(s->s.getId().toLowerCase(),t->t,(o1,o2)->o1)); } /** * 数据对象转换为显示对象 * * @param linkTypes 数据对象 * @return 显示对象 */ @Override public List linkTypeDO2VOs(Collection linkTypes) { List VOS = new ArrayList<>(); Optional.ofNullable(linkTypes).orElseGet(()->new ArrayList<>()).stream().forEach(linkType -> { OsLinkTypeVO vo = linkTypeDO2VO(linkType); VOS.add(vo); }); return VOS; } /** * 数据对象转换为显示对象 * * @param linkType 数据对象 * @return 显示对象 */ @Override public OsLinkTypeVO linkTypeDO2VO(LinkType linkType) { OsLinkTypeVO vo = new OsLinkTypeVO(); if(linkType !=null){ vo.setOid(linkType.oid); vo.setCreator(linkType.creator); vo.setLastModifier(linkType.modifier); try { vo.setCreateTime(VciDateUtil.long2Date(linkType.createTime)); vo.setLastModifyTime(VciDateUtil.long2Date(linkType.modifyTime)); vo.setTs(VciDateUtil.long2Date(linkType.ts)); } catch (Exception e) { e.printStackTrace(); } vo.setDescription(linkType.description); vo.setId(linkType.name); vo.setName(linkType.tag); vo.setFromBtmTypeVOS(btmService.listBtmByIds(Arrays.stream(linkType.btmItemsFrom).collect(Collectors.toSet()))); if(!CollectionUtils.isEmpty(vo.getFromBtmTypeVOS())){ vo.setFromBtmType(Arrays.stream(linkType.btmItemsFrom).collect(Collectors.joining(","))); vo.setFromBtmTypeName(vo.getFromBtmTypeVOS().stream().map(OsBtmTypeVO::getName).collect(Collectors.joining(","))); } vo.setToBtmTypeVOS(btmService.listBtmByIds(Arrays.stream(linkType.btmItemsTo).collect(Collectors.toSet()))); if(!CollectionUtils.isEmpty(vo.getToBtmTypeVOS())){ vo.setToBtmType(Arrays.stream(linkType.btmItemsTo).collect(Collectors.joining(","))); vo.setToBtmTypeName(vo.getToBtmTypeVOS().stream().map(OsBtmTypeVO::getName).collect(Collectors.joining(","))); } vo.setImplClass(linkType.implClass); vo.setShape(linkType.shape); List attributeVOS = attributeService.listAttrByIds(Arrays.stream(linkType.attributes).collect(Collectors.toList())); List linkTypeAttributeVOS = new ArrayList<>(); Optional.ofNullable(attributeVOS).orElseGet(()->new ArrayList<>()).stream().forEach(attributeVO->{ OsLinkTypeAttributeVO linkTypeAttributeVO = new OsLinkTypeAttributeVO(); BeanUtil.convert(attributeVO,linkTypeAttributeVO); linkTypeAttributeVO.setPkLinkType(vo.getOid()); if(StringUtils.isNotBlank(attributeVO.getBtmTypeId())){ linkTypeAttributeVO.setReferFlag(true); linkTypeAttributeVO.setReferBtmTypeId(attributeVO.getBtmTypeId()); } if(StringUtils.isNotBlank(attributeVO.getEnumId())){ linkTypeAttributeVO.setEnumFlag(true); linkTypeAttributeVO.setEnumItemMap(enumService.getEnumValueMap(linkTypeAttributeVO.getEnumId())); } linkTypeAttributeVOS.add(linkTypeAttributeVO); }); vo.setAttributes(linkTypeAttributeVOS); } return vo; } /** * 使用编号获取链接类型 * * @param linkIds 编号 * @return 链接类型 */ @Override public List listLinkTypeIds(Collection linkIds) { if(CollectionUtils.isEmpty(linkIds)){ return null; } Map linkTypeVOMap = self.selectAllLinkMap(); List linkTypeVOS = new ArrayList<>(); linkIds.stream().forEach(id->{ if(linkTypeVOMap.containsKey(id.toLowerCase())){ linkTypeVOS.add(linkTypeVOMap.get(id.toLowerCase())); } }); return linkTypeVOS; } /** * 使用编号获取链接类型 * * @param id 编号 * @return 链接类型 */ @Override public OsLinkTypeVO getLinkTypeById(String id) { if(StringUtils.isBlank(id)){ return null; } return self.selectAllLinkMap().getOrDefault(id.toLowerCase(),null); } /** * 获取链接类型的属性 * * @param linkTypeId 链接类型的编号 * @return 链接类型的属性 */ @Override public List listAttributeByLinkId(String linkTypeId) { OsLinkTypeVO linkTypeVO = getLinkTypeById(linkTypeId); return linkTypeVO.getAttributes(); } /** * 链接类型的列表 * * @param baseQueryObject 查询对象 * @return 链接类型的显示对象 */ @Override public DataGrid gridLinkType(BaseQueryObject baseQueryObject) { return gridObject(baseQueryObject, OsLinkTypeDO.class,self.selectAllLinkMap(),OsLinkTypeVO.class); } /** * 使用主键获取显示对象 * * @param linkTypeOid 链接类型的主键 * @return 链接类型的显示对象 */ @Override public OsLinkTypeVO selectByOid(String linkTypeOid) { List linkTypeVOS = self.selectAllLinkMap().values().stream().collect(Collectors.toList()); return Optional.ofNullable(linkTypeVOS).orElseGet(()->new ArrayList<>()).stream().filter(s->s.getOid().equalsIgnoreCase(linkTypeOid)).findFirst().orElseGet(()->null); } /** * 获取链接类型关联的所有业务类型中属性类型差异的信息 * * @param linkTypeOid 链接类型的主键 * @return 有错误的属性 */ @Override public List checkAttributeTypeDifferent(String linkTypeOid) { if(StringUtils.isBlank(linkTypeOid)){ return new ArrayList<>(); } OsLinkTypeVO linkTypeVO = selectByOid(linkTypeOid); List fromBtmTypeVOS = linkTypeVO.getFromBtmTypeVOS(); List toBtmTypeVOS = linkTypeVO.getToBtmTypeVOS(); List btmTypeVOS = new ArrayList<>(); btmTypeVOS.addAll(fromBtmTypeVOS); btmTypeVOS.addAll(toBtmTypeVOS); List diffList = new ArrayList<>(); btmTypeVOS.stream().forEach(btmTypeVO -> { //查询这个表的信息 String sql = "select t.column_name,t.data_type,t.data_length,t.nullable,t.data_precision,t.data_scale,c.comments from user_tab_columns t " + "inner JOIN user_col_comments c on t.TABLE_NAME = c.table_name and t.COLUMN_NAME = c.column_name where " + "t.table_name = '" + VciBaseUtil.getTableName(btmTypeVO.getId()).toUpperCase(Locale.ROOT) + "' order by t.column_name asc"; Map attributeVOMap = btmTypeVO.getAttributes().stream().collect(Collectors.toMap(s -> s.getId().toLowerCase(Locale.ROOT), t -> t)); List cbosList = boService.queryBySql(sql, new HashMap<>()); if(!CollectionUtils.isEmpty(cbosList)){ cbosList.stream().forEach(cbo->{ String attrId = cbo.getAttributeValue("column_name"); String dataType = cbo.getAttributeValue("data_type"); if(StringUtils.isNotBlank(dataType) && dataType.contains("(")){ dataType = dataType.substring(0,dataType.indexOf("(")); } OsBtmTypeAttributeVO attributeVO = attributeVOMap.getOrDefault(attrId.toLowerCase(Locale.ROOT), null); if(attributeVO!=null){ String vtType = attributeVO.getAttrDataType(); String attrType = ""; VciFieldTypeEnum fieldTypeEnum = VciFieldTypeEnum.forValue(vtType); if(fieldTypeEnum == null) { attrType = "VARCHAR2"; }else { switch (fieldTypeEnum) { case VTString: case VTBoolean: attrType = "VARCHAR2"; break; case VTInteger: case VTLong: case VTDouble: attrType = "NUMBER"; break; case VTDate: attrType = "DATE"; break; case VTDateTime: case VTTime: attrType = "TIMESTAMP"; break; default: attrType = "VARCHAR2"; break; } } if(!attrType.equalsIgnoreCase(dataType)) { diffList.add(attributeVO); } } }); } }); return diffList; } /** * 清除缓存 */ @Override @CacheEvict(value = VCI_OBJECT_SERVICE,key = CacheKeyConstant.ALL_Link) public void clearCache() { } }