| | |
| | | package com.vci.ubcs.ddl.service.impl; |
| | | |
| | | <<<<<<< HEAD |
| | | import com.alibaba.nacos.client.naming.NacosNamingService; |
| | | |
| | | ======= |
| | | import com.vci.starter.web.util.VciBaseUtil; |
| | | >>>>>>> dd3a6c681f938fd53d5f6cc3a4fcee40e71b259c |
| | | import com.vci.starter.word.bo.WordMergeStartTableDataBO; |
| | | import com.alibaba.cloud.commons.lang.StringUtils; |
| | | import com.alibaba.nacos.shaded.com.google.protobuf.ServiceException; |
| | | import com.vci.ubcs.ddl.bo.DdlTableBO; |
| | | import com.vci.ubcs.ddl.bo.DdlTableInDataBaseBO; |
| | | import com.vci.ubcs.ddl.enums.BusinessTypeEnum; |
| | | import com.vci.ubcs.ddl.enums.ModifyTableTaskEnum; |
| | | import com.vci.ubcs.ddl.processor.ddl.DdlMapperProcessStrategy; |
| | | import com.vci.ubcs.ddl.processor.ddl.DdlMapperProcessor; |
| | | import com.vci.ubcs.ddl.processor.dll.DllMapperProcessor; |
| | | import com.vci.ubcs.ddl.processor.dll.DllMapperProcessorStrategy; |
| | | import com.vci.ubcs.ddl.properties.DdlExportWordFieldProperties; |
| | | import com.vci.ubcs.ddl.properties.DdlPropertise; |
| | | import com.vci.ubcs.ddl.service.IDdlService; |
| | | import com.vci.ubcs.omd.cache.BtmTypeCache; |
| | | import com.vci.ubcs.omd.dto.BtmTypeDTO; |
| | | import com.vci.ubcs.omd.entity.ModifyAttributeInfoDO; |
| | | import com.vci.ubcs.omd.vo.BtmTypeAttributeVO; |
| | | import com.vci.ubcs.omd.vo.BtmTypeVO; |
| | | import com.vci.ubcs.omd.vo.LinkTypeAttributeVO; |
| | | import com.vci.ubcs.omd.vo.LinkTypeVO; |
| | | import com.vci.ubcs.omd.constant.BtmTypeFieldConstant; |
| | | import com.vci.ubcs.omd.constant.BtmTypeLcStatusConstant; |
| | | import com.vci.ubcs.omd.dto.BtmAndLinkTypeDdlDTO; |
| | | import com.vci.ubcs.omd.entity.ModifyAttributeInfo; |
| | | import com.vci.ubcs.omd.vo.*; |
| | | import com.vci.ubcs.starter.exception.VciBaseException; |
| | | import com.vci.ubcs.starter.web.enumpck.BooleanEnum; |
| | | import com.vci.ubcs.starter.web.enumpck.VciFieldTypeEnum; |
| | | import com.vci.ubcs.starter.web.util.VciBaseUtil; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.springblade.core.secure.utils.AuthUtil; |
| | | import org.springblade.core.tool.api.R; |
| | | import org.springblade.core.tool.utils.BeanUtil; |
| | | import org.springblade.core.tool.utils.Func; |
| | |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.util.CollectionUtils; |
| | | |
| | | import javax.validation.constraints.NotNull; |
| | | import java.util.*; |
| | | import java.util.concurrent.ConcurrentHashMap; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | |
| | | /** |
| | | * word导出的时候的域字段映射 |
| | | */ |
| | | @Autowired |
| | | private DdlExportWordFieldProperties wordFieldProperties; |
| | | // @Autowired |
| | | // private DdlExportWordFieldProperties wordFieldProperties; |
| | | |
| | | /** |
| | | * ddl数据操作服务 |
| | | */ |
| | | private final DdlMapperProcessor ddlMapper = DdlMapperProcessStrategy.getProcessor(); |
| | | |
| | | |
| | | /** |
| | | * dll数据操作服务 |
| | | */ |
| | | private final DllMapperProcessor dllMapper = DllMapperProcessorStrategy.getProcessor(); |
| | | |
| | | /** |
| | | * 表名前缀 |
| | | */ |
| | | private final String TABLE_PREFIX = "PL"; |
| | | private static final String YES = "Y"; |
| | | |
| | | private static final String NO = "N"; |
| | | |
| | | private final Map<String, BtmTypeVO> idBtmMap = new ConcurrentHashMap<>(); |
| | | |
| | | private final Map<String, LinkTypeVO> idLinkMap = new ConcurrentHashMap<>(); |
| | | |
| | | private void putBtm(@NotNull BtmTypeVO... voList) throws ServiceException { |
| | | String collect = Arrays.stream(voList).map(BtmTypeVO::getId).filter(idBtmMap::containsKey).collect(Collectors.joining(",")); |
| | | if (StringUtils.isBlank(collect)) { |
| | | Arrays.stream(voList).forEach(vo -> { |
| | | idBtmMap.put(vo.getId(), vo); |
| | | }); |
| | | } else { |
| | | throw new ServiceException("业务类型:[" + collect + "]正在被其他用户操作"); |
| | | } |
| | | } |
| | | |
| | | private void putLink(@NotNull LinkTypeVO... voList) throws ServiceException { |
| | | String collect = Arrays.stream(voList).map(LinkTypeVO::getId).filter(idLinkMap::containsKey).collect(Collectors.joining(",")); |
| | | if (StringUtils.isBlank(collect)) { |
| | | Arrays.stream(voList).forEach(vo -> { |
| | | idLinkMap.put(vo.getId(), vo); |
| | | }); |
| | | } else { |
| | | throw new ServiceException("链接类型:[" + collect + "]正在被其他用户操作"); |
| | | } |
| | | } |
| | | |
| | | private void removeBtm(@NotNull BtmTypeVO... btmTypeVO) throws ServiceException { |
| | | if (Arrays.stream(btmTypeVO).allMatch(s -> idBtmMap.containsKey(s.getId()))) { |
| | | for (BtmTypeVO vo : btmTypeVO) { |
| | | idBtmMap.remove(vo.getId()); |
| | | } |
| | | } else { |
| | | throw new ServiceException("解锁对象不存在"); |
| | | } |
| | | } |
| | | |
| | | private void removeLink(@NotNull LinkTypeVO... linkTypeVO) throws ServiceException { |
| | | if (Arrays.stream(linkTypeVO).allMatch(s -> idLinkMap.containsKey(s.getId()))) { |
| | | for (LinkTypeVO vo : linkTypeVO) { |
| | | idLinkMap.remove(vo.getId()); |
| | | } |
| | | } else { |
| | | throw new ServiceException("解锁对象不存在"); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 校验业务类型的数据库表中是否有数据 |
| | |
| | | */ |
| | | @Override |
| | | public void createDbTablesByOidCollection(Collection<String> oidCollection) throws VciBaseException { |
| | | VciBaseUtil.alertCollectionNotNull("业务类型/链接类型的主键集合", oidCollection); |
| | | VciBaseUtil.alertNotNull("业务类型/链接类型的主键集合", oidCollection); |
| | | List<BtmTypeVO> btmTypeVOList = BtmTypeCache.listBtmTypeByOidCollection(oidCollection); |
| | | // List<OsLinkTypeVO> linkTypeVOList = linkTypeService.listLinkTypeByOidCollection(oidCollection); |
| | | if (!CollectionUtils.isEmpty(btmTypeVOList)) { |
| | |
| | | } else { |
| | | String tableName = btmTypeVO.getTableName(); |
| | | if (!checkTableExistByTableName(tableName)) { |
| | | String attributeSql = dllMapper.getCreateSqlByAttributeForBtm(btmTypeVO.getAttributes()); |
| | | String attributeSql = getCreateSqlByAttributeForBtm(btmTypeVO.getAttributes()); |
| | | dllMapper.createTableBySql(tableName, attributeSql); |
| | | if (StringUtils.isNotBlank(btmTypeVO.getName())) { |
| | | dllMapper.commentTable(tableName, btmTypeVO.getName()); |
| | | } |
| | | btmTypeVO.getAttributes().stream().forEach(s -> { |
| | | btmTypeVO.getAttributes().forEach(s -> { |
| | | dllMapper.commentColumnTable(tableName, s.getId(), s.getName()); |
| | | }); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取创建的sql语句中属性部分 |
| | | * |
| | | * @param attributeVOList 属性的立碑 |
| | | * @return sql语句 |
| | | */ |
| | | @Override |
| | | public String getCreateSqlByAttributeForBtm(List<BtmTypeAttributeVO> attributeVOList) { |
| | | StringBuilder sb = new StringBuilder(); |
| | | attributeVOList.forEach(a -> { |
| | | sb.append(a.getId()).append(StringPool.SPACE); |
| | | VciFieldTypeEnum fieldTypeEnum = VciFieldTypeEnum.forValue(a.getAttrDataType()); |
| | | sb.append(dllMapper.getColumnTypeSql(fieldTypeEnum, a)); |
| | | }); |
| | | return sb.substring(0, sb.lastIndexOf(",")); |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | @Override |
| | | public void changeColumnForBtm(List<BtmTypeAttributeVO> modifyLengthAttrVOList) throws VciBaseException { |
| | | VciBaseUtil.alertCollectionNotNull("要修改长度的属性集", modifyLengthAttrVOList); |
| | | VciBaseUtil.alertNotNull("要修改长度的属性集", modifyLengthAttrVOList); |
| | | Map<String, List<BtmTypeAttributeVO>> btmTypeHasAttributeVOMap = modifyLengthAttrVOList.stream().collect(Collectors.groupingBy(BtmTypeAttributeVO::getPkBtmType)); |
| | | btmTypeHasAttributeVOMap.forEach((k, v) -> { |
| | | BtmTypeVO btmTypeVO = BtmTypeCache.getDetail(k); |
| | |
| | | * @param attributeVOList 要添加的属性字段 |
| | | */ |
| | | private void changeColumnsForTable(String tableName, List<BtmTypeAttributeVO> attributeVOList) { |
| | | String attributeSql = dllMapper.getCreateSqlByAttributeForBtm(attributeVOList); |
| | | String attributeSql = getCreateSqlByAttributeForBtm(attributeVOList); |
| | | //先判断表格是否存在 |
| | | boolean tableExist = false; |
| | | try { |
| | |
| | | */ |
| | | private void commentColumnsForTable(String tableName, List<BtmTypeAttributeVO> attributeVOList) { |
| | | if (StringUtils.isNotBlank(tableName) && !CollectionUtils.isEmpty(attributeVOList)) { |
| | | attributeVOList.stream().forEach(s -> { |
| | | attributeVOList.forEach(s -> { |
| | | ddlMapper.commentColumnTable(tableName, s.getId(), s.getName()); |
| | | }); |
| | | } |
| | |
| | | */ |
| | | @Override |
| | | public void addColumn2TableForBtm(List<BtmTypeAttributeVO> addAttrDOList) throws VciBaseException { |
| | | VciBaseUtil.alertCollectionNotNull("要添加到数据库表中的属性集", addAttrDOList); |
| | | Map<String, List<BtmTypeAttributeVO>> btmTypeHasAttributeVOMap = addAttrDOList.stream().collect(Collectors.groupingBy(BtmTypeAttributeVO::getPkBtmType)); |
| | | btmTypeHasAttributeVOMap.forEach((k, v) -> { |
| | | VciBaseUtil.alertNotNull("要添加到数据库表中的属性集", addAttrDOList); |
| | | Map<String, List<BtmTypeAttributeVO>> btmTypeHasAttributeMap = addAttrDOList.stream().collect(Collectors.groupingBy(BtmTypeAttributeVO::getPkBtmType)); |
| | | btmTypeHasAttributeMap.forEach((k, v) -> { |
| | | BtmTypeVO btmTypeVO = BtmTypeCache.getDetail(k); |
| | | if (btmTypeVO == null || StringUtils.isBlank(btmTypeVO.getOid())) { |
| | | throw new VciBaseException("要修改属性列长度的业务类型不存在"); |
| | |
| | | * @param attributeVOList 属性的显示对象 |
| | | * @throws VciBaseException 执行或者获取sql语句的时候出现错误会抛出异常 |
| | | */ |
| | | <<<<<<< HEAD |
| | | private void addColumnForTable(String tableName, List<OmdBtmTypeAttributeVO> attributeVOList) throws VciBaseException{ |
| | | ======= |
| | | private void addColumnForTable(String tableName, List<BtmTypeAttributeVO> attributeVOList) { |
| | | >>>>>>> dd3a6c681f938fd53d5f6cc3a4fcee40e71b259c |
| | | String attributeSql = dllMapper.getCreateSqlByAttributeForBtm(attributeVOList); |
| | | String attributeSql = getCreateSqlByAttributeForBtm(attributeVOList); |
| | | //先判断表格是否存在 |
| | | if (!checkTableExistByTableName(tableName)) { |
| | | BtmTypeAttributeVO attributeVO = attributeVOList.get(0); |
| | |
| | | */ |
| | | @Override |
| | | public void addColumn2TableForLink(List<LinkTypeAttributeVO> addAttrDOListForLinkType) throws VciBaseException { |
| | | VciBaseUtil.alertNotNull("要添加到数据库表中属性集",addAttrDOListForLinkType); |
| | | Map<String, List<LinkTypeAttributeVO>> linkTypeHasAttributeVOMap = addAttrDOListForLinkType.stream().collect(Collectors.groupingBy(LinkTypeAttributeVO::getPkLinkType)); |
| | | linkTypeHasAttributeVOMap.forEach((k,v) -> { |
| | | |
| | | }); |
| | | } |
| | | |
| | | /** |
| | |
| | | * @return 不同的地方,每一个业务类型或者链接类型一条数据 |
| | | */ |
| | | @Override |
| | | public List<ModifyAttributeInfoDO> checkDifferent(List<BtmTypeVO> btmTypeVOList, List<LinkTypeVO> linkTypeVOList) throws VciBaseException { |
| | | return null; |
| | | public List<ModifyAttributeInfo> checkDifferent(List<BtmTypeVO> btmTypeVOList, List<LinkTypeVO> linkTypeVOList) throws VciBaseException { |
| | | if (CollectionUtils.isEmpty(btmTypeVOList) && CollectionUtils.isEmpty(linkTypeVOList)) { |
| | | throw new VciBaseException("要检查的业务类型和链接类型为空"); |
| | | } |
| | | List<ModifyAttributeInfo> modifyAttributeInfoList = new ArrayList<>(); |
| | | if (!CollectionUtils.isEmpty(btmTypeVOList)) { |
| | | modifyAttributeInfoList.addAll(checkBtmType(btmTypeVOList)); |
| | | } |
| | | if (!CollectionUtils.isEmpty(linkTypeVOList)) { |
| | | modifyAttributeInfoList.addAll(checkLinkType(linkTypeVOList)); |
| | | } |
| | | if (CollectionUtils.isEmpty(modifyAttributeInfoList)) { |
| | | reflexDifferent(modifyAttributeInfoList, btmTypeVOList, linkTypeVOList); |
| | | } |
| | | return modifyAttributeInfoList; |
| | | } |
| | | |
| | | /** |
| | | * 检查业务类型 |
| | | * |
| | | * @param btmTypeVOList 业务类型 |
| | | * @return 检查结果 |
| | | */ |
| | | private List<ModifyAttributeInfo> checkBtmType(List<BtmTypeVO> btmTypeVOList) { |
| | | List<ModifyAttributeInfo> modifyAttributeInfoDOList = new ArrayList<>(); |
| | | btmTypeVOList.stream().filter(s -> !isCompatibilityTable(s.getId(),null)).forEach(btm -> { |
| | | ModifyAttributeInfo modifyAttributeInfo = new ModifyAttributeInfo(); |
| | | modifyAttributeInfo.setId(btm.getId()); |
| | | modifyAttributeInfo.setName(btm.getName()); |
| | | modifyAttributeInfo.setTableName(btm.getTableName()); |
| | | modifyAttributeInfo.setBusinessType(BusinessTypeEnum.BTM.getValue()); |
| | | // 1. 视图和新建的,直接创建,记录创建历史 |
| | | if (btm.isViewFlag() || !checkTableExistByTableName(btm.getTableName())){ |
| | | modifyAttributeInfo.setTaskName(ModifyTableTaskEnum.CREATE.getValue()); |
| | | modifyAttributeInfo.setModifyColumnReason(ModifyTableTaskEnum.CREATE.getValue()); |
| | | }else { |
| | | VciBaseUtil.alertNotNull(btm.getAttributes(),"业务类型" + btm.getId() + "的属性"); |
| | | List<DdlTableInDataBaseBO> columnDataInTable = selectTableColumnInfo(btm.getTableName()); |
| | | if (CollectionUtils.isEmpty(columnDataInTable)){ |
| | | modifyAttributeInfo.setAddAttributes(btm.getAttributes().stream().map(s -> s.getId().toLowerCase().trim()).collect(Collectors.joining(","))); |
| | | modifyAttributeInfo.setTaskName(ModifyTableTaskEnum.EDIT.getValue()); |
| | | modifyAttributeInfo.setModifyColumnReason(ModifyTableTaskEnum.CREATE.getValue()); |
| | | }else { |
| | | // 2. 存在的才判断,分新增属性、属性更改、属性删除分别操作 |
| | | // 3. 属性操作中特别判断[是否控制版本][是否控制密级][是否控制生命周期]来添加对应属性 |
| | | Map<String, DdlTableInDataBaseBO> tableColumMap = columnDataInTable.stream().collect(Collectors.toMap(t -> t.getId().toLowerCase().trim(), k -> k)); |
| | | Map<String, BtmTypeAttributeVO> attributeMap = btm.getAttributes().stream().collect(Collectors.toMap(t -> t.getId().toLowerCase().trim(), k -> k)); |
| | | List<String> addList = new ArrayList<>(); |
| | | List<String> editList = new ArrayList<>(); |
| | | List<String> deleteList = new ArrayList<>(); |
| | | List<String> beforeList = new ArrayList<>(); |
| | | attributeMap.forEach((k,v) -> { |
| | | String attributeLowId = k.toLowerCase(); |
| | | if (!checkIsDefaultColumn(attributeLowId)) { |
| | | if (tableColumMap.containsKey(k)) { |
| | | //说明以前也有,那就看是否需要修改 |
| | | DdlTableInDataBaseBO columnInDB = tableColumMap.get(k); |
| | | if (columnInDB.getAttributeLength() == null) { |
| | | columnInDB.setAttributeLength(0); |
| | | } |
| | | boolean needEdit = false; |
| | | if (!v.getName().equalsIgnoreCase(columnInDB.getName())) { |
| | | //说明需要修改 |
| | | needEdit = true; |
| | | } |
| | | if (needEdit || checkNeedEditAttr(v, columnInDB)) { |
| | | editList.add(v.getId()); |
| | | beforeList.add(columnInDB.getId() + StringPool.SPACE + columnInDB.getAttrDataType() + "(" + columnInDB.getAttributeLength() + ")" + StringPool.SPACE + columnInDB.getNullableFlag() |
| | | + StringPool.SPACE + columnInDB.getPrecisionLength() + StringPool.SPACE + columnInDB.getScaleLength() + StringPool.SPACE + columnInDB.getName()); |
| | | } |
| | | } else { |
| | | addList.add(v.getId()); |
| | | } |
| | | } else { |
| | | //针对默认属性,如果以前没有,现在有可以添加,如果以前 |
| | | if (!tableColumMap.containsKey(k)) { |
| | | boolean needAddNewField = false; |
| | | if (!BtmTypeLcStatusConstant.EMTYPE_LIFE_CYCLE.equalsIgnoreCase(btm.getLifeCycleId()) && BtmTypeFieldConstant.LIFECYCLE_MANAGE_FIELD_MAP.containsKey(attributeLowId)) { |
| | | needAddNewField = true; |
| | | } |
| | | if (StringUtils.isNotBlank(btm.getRevisionRuleId()) && BtmTypeFieldConstant.REVISION_MANAGE_FIELD_MAP.containsKey(attributeLowId)) { |
| | | needAddNewField = true; |
| | | } |
| | | if (btm.isSecretFlag() && BtmTypeFieldConstant.SECRET_MANAGE_FIELD_MAP.containsKey(attributeLowId)) { |
| | | needAddNewField = true; |
| | | } |
| | | if (needAddNewField) { |
| | | addList.add(v.getId()); |
| | | } |
| | | } |
| | | } |
| | | }); |
| | | tableColumMap.forEach((k, v) -> { |
| | | if (!attributeMap.containsKey(k)) { |
| | | deleteList.add(k); |
| | | } |
| | | }); |
| | | modifyAttributeInfo.setOrderModifyUserName(AuthUtil.getUserAccount()); |
| | | modifyAttributeInfo.setOrderModifyDate(new Date()); |
| | | modifyAttributeInfo.setModifyColumnReason(ModifyTableTaskEnum.CONSISTENCY_CHECK.getValue()); |
| | | modifyAttributeInfo.setTaskName(ModifyTableTaskEnum.CONSISTENCY_CHECK.getValue()); |
| | | modifyAttributeInfo.setAddAttributes(String.join(",", addList)); |
| | | modifyAttributeInfo.setModifyAttributes(String.join(",", editList)); |
| | | modifyAttributeInfo.setDeleteAttributes(String.join(",", deleteList)); |
| | | modifyAttributeInfo.setBeforeModifyAttributes(String.join(",", beforeList)); |
| | | } |
| | | } |
| | | }); |
| | | return modifyAttributeInfoDOList; |
| | | } |
| | | |
| | | private boolean checkIsDefaultColumn(String attributeLowId) { |
| | | return ((BtmTypeFieldConstant.BASIC_FIELD_MAP.containsKey(attributeLowId) |
| | | && !VciBaseUtil.inArray(new String[]{"id", "name", "description"}, attributeLowId)) |
| | | || BtmTypeFieldConstant.LINK_TYPE_FIELD_MAP.containsKey(attributeLowId) |
| | | || BtmTypeFieldConstant.LIFECYCLE_MANAGE_FIELD_MAP.containsKey(attributeLowId) |
| | | || BtmTypeFieldConstant.REVISION_MANAGE_FIELD_MAP.containsKey(attributeLowId) |
| | | || BtmTypeFieldConstant.SECRET_MANAGE_FIELD_MAP.containsKey(attributeLowId)); |
| | | } |
| | | |
| | | /** |
| | | * 检查链接类型 |
| | | * |
| | | * @param linkTypeVOList 链接类型 |
| | | * @return 检查结果 |
| | | */ |
| | | private List<ModifyAttributeInfo> checkLinkType(List<LinkTypeVO> linkTypeVOList) { |
| | | List<ModifyAttributeInfo> modifyAttributeInfoDOList = new ArrayList<>(); |
| | | linkTypeVOList.stream().filter(s -> !isCompatibilityTable(null,s.getId())).forEach(link -> { |
| | | ModifyAttributeInfo modifyAttributeInfo = new ModifyAttributeInfo(); |
| | | modifyAttributeInfo.setId(link.getId()); |
| | | modifyAttributeInfo.setName(link.getName()); |
| | | modifyAttributeInfo.setTableName(link.getTableName()); |
| | | modifyAttributeInfo.setBusinessType(BusinessTypeEnum.LINK.getValue()); |
| | | // 1. 视图和新建的,直接创建,记录创建历史 |
| | | if (!checkTableExistByTableName(link.getTableName())){ |
| | | modifyAttributeInfo.setTaskName(ModifyTableTaskEnum.CREATE.getValue()); |
| | | modifyAttributeInfo.setModifyColumnReason(ModifyTableTaskEnum.CREATE.getValue()); |
| | | }else { |
| | | VciBaseUtil.alertNotNull(link.getAttributes(), "链接类型" + link.getId() + "的属性"); |
| | | List<DdlTableInDataBaseBO> columnDataInTable = selectTableColumnInfo(link.getTableName()); |
| | | if (CollectionUtils.isEmpty(columnDataInTable)) { |
| | | modifyAttributeInfo.setAddAttributes(link.getAttributes().stream().map(s -> s.getId().toLowerCase().trim()).collect(Collectors.joining(","))); |
| | | modifyAttributeInfo.setTaskName(ModifyTableTaskEnum.EDIT.getValue()); |
| | | modifyAttributeInfo.setModifyColumnReason(ModifyTableTaskEnum.CREATE.getValue()); |
| | | } else { |
| | | // 2. 存在的才判断,分新增属性、属性更改、属性删除分别操作 |
| | | // 3. 属性操作中特别判断[是否控制版本][是否控制密级][是否控制生命周期]来添加对应属性 |
| | | Map<String, DdlTableInDataBaseBO> tableColumMap = columnDataInTable.stream().collect(Collectors.toMap(t -> t.getId().toLowerCase().trim(), k -> k)); |
| | | Map<String, LinkTypeAttributeVO> attributeMap = link.getAttributes().stream().collect(Collectors.toMap(t -> t.getId().toLowerCase().trim(), k -> k)); |
| | | List<String> addList = new ArrayList<>(); |
| | | List<String> editList = new ArrayList<>(); |
| | | List<String> deleteList = new ArrayList<>(); |
| | | List<String> beforeList = new ArrayList<>(); |
| | | attributeMap.forEach((k, v) -> { |
| | | String attributeLowId = k.toLowerCase(); |
| | | if (!(BtmTypeFieldConstant.LINK_TYPE_FIELD_MAP.containsKey(attributeLowId))) { |
| | | if (tableColumMap.containsKey(k)) { |
| | | //说明以前也有,那就看是否需要修改 |
| | | DdlTableInDataBaseBO columnInDB = tableColumMap.get(k); |
| | | if (columnInDB.getAttributeLength() == null) { |
| | | columnInDB.setAttributeLength(0); |
| | | } |
| | | boolean needEdit = false; |
| | | if (!v.getName().equalsIgnoreCase(columnInDB.getName())) { |
| | | //说明需要修改 |
| | | needEdit = true; |
| | | } |
| | | BtmTypeAttributeVO btmTypeAttributeVO = new BtmTypeAttributeVO(); |
| | | BeanUtil.copy(v, btmTypeAttributeVO); |
| | | if (needEdit || checkNeedEditAttr(btmTypeAttributeVO, columnInDB)) { |
| | | editList.add(v.getId()); |
| | | beforeList.add(columnInDB.getId() + StringPool.SPACE + columnInDB.getAttrDataType() + "(" + columnInDB.getAttributeLength() + ")" + StringPool.SPACE + columnInDB.getNullableFlag() |
| | | + StringPool.SPACE + columnInDB.getPrecisionLength() + StringPool.SPACE + columnInDB.getScaleLength() + StringPool.SPACE + columnInDB.getName()); |
| | | } |
| | | } else { |
| | | addList.add(v.getId()); |
| | | } |
| | | } else { |
| | | //针对默认属性,如果以前没有,现在有可以添加,如果以前 |
| | | if (!tableColumMap.containsKey(k)) { |
| | | boolean needAddNewField = false; |
| | | if (BtmTypeFieldConstant.LINK_TYPE_FIELD_MAP.containsKey(attributeLowId)) { |
| | | //是链接类型的属性,以前没有就添加上 |
| | | needAddNewField = true; |
| | | } |
| | | if (needAddNewField) { |
| | | addList.add(v.getId()); |
| | | } |
| | | } |
| | | } |
| | | }); |
| | | tableColumMap.forEach((k, v) -> { |
| | | if (!attributeMap.containsKey(k)) { |
| | | deleteList.add(k); |
| | | } |
| | | }); |
| | | modifyAttributeInfo.setOrderModifyUserName(AuthUtil.getUserAccount()); |
| | | modifyAttributeInfo.setOrderModifyDate(new Date()); |
| | | modifyAttributeInfo.setModifyColumnReason(ModifyTableTaskEnum.CONSISTENCY_CHECK.getValue()); |
| | | modifyAttributeInfo.setTaskName(ModifyTableTaskEnum.CONSISTENCY_CHECK.getValue()); |
| | | modifyAttributeInfo.setAddAttributes(String.join(",", addList)); |
| | | modifyAttributeInfo.setModifyAttributes(String.join(",", editList)); |
| | | modifyAttributeInfo.setDeleteAttributes(String.join(",", deleteList)); |
| | | modifyAttributeInfo.setBeforeModifyAttributes(String.join(",", beforeList)); |
| | | } |
| | | |
| | | } |
| | | }); |
| | | return modifyAttributeInfoDOList; |
| | | } |
| | | |
| | | /** |
| | | * 判断是否需要修改属性 |
| | | * |
| | | * @param btmTypeAttributeVO 属性的显示对象 |
| | | * @param tableInDB 数据库表里的对象 |
| | | * @return true表示需要修改,false表示不修改 |
| | | */ |
| | | private boolean checkNeedEditAttr(BtmTypeAttributeVO btmTypeAttributeVO, DdlTableInDataBaseBO tableInDB) { |
| | | if (tableInDB.getAttributeLength() == null) { |
| | | tableInDB.setAttributeLength(0); |
| | | } |
| | | if (tableInDB.getPrecisionLength() == null) { |
| | | tableInDB.setPrecisionLength(20); |
| | | } |
| | | if (tableInDB.getScaleLength() == null) { |
| | | tableInDB.setScaleLength(0); |
| | | } |
| | | String nullInDb = ""; |
| | | if (YES.equalsIgnoreCase(tableInDB.getNullableFlag())) { |
| | | nullInDb = BooleanEnum.TRUE.getValue(); |
| | | } else if (NO.equalsIgnoreCase(tableInDB.getNullableFlag())) { |
| | | nullInDb = BooleanEnum.FASLE.getValue(); |
| | | } |
| | | if (!(String.valueOf(btmTypeAttributeVO.isNullableFlag()).equalsIgnoreCase(nullInDb))) { |
| | | return true; |
| | | } |
| | | if (StringUtils.isNotBlank(btmTypeAttributeVO.getDefaultValue())) { |
| | | return true; |
| | | } |
| | | |
| | | |
| | | List<VciFieldTypeEnum> fieldTypes = dllMapper.listFieldByColumnStr(tableInDB.getAttrDataType()); |
| | | if (CollectionUtils.isEmpty(fieldTypes)) { |
| | | throw new VciBaseException("没有找到对应的字段类型"); |
| | | } |
| | | Set<String> names = fieldTypes.stream().map(Enum::name).collect(Collectors.toSet()); |
| | | String attributeDataType = btmTypeAttributeVO.getAttrDataType(); |
| | | if (names.size() > 1) { |
| | | return !names.contains(attributeDataType); |
| | | } else { |
| | | VciFieldTypeEnum fieldType = fieldTypes.get(0); |
| | | if (attributeDataType.equalsIgnoreCase(fieldType.name())) { |
| | | switch (fieldType) { |
| | | case VTString: |
| | | if (btmTypeAttributeVO.getAttributeLength().intValue() != tableInDB.getAttributeLength().intValue()) { |
| | | return true; |
| | | } |
| | | break; |
| | | case VTDouble: |
| | | //不超过128 |
| | | if (btmTypeAttributeVO.getPrecisionLength().intValue() != tableInDB.getPrecisionLength().intValue()) { |
| | | return true; |
| | | } |
| | | if (btmTypeAttributeVO.getScaleLength().intValue() != tableInDB.getScaleLength().intValue()) { |
| | | return true; |
| | | } |
| | | break; |
| | | default: |
| | | break; |
| | | } |
| | | } |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | /** |
| | |
| | | * @throws VciBaseException 参数为空或者执行出错的时候会抛出异常 |
| | | */ |
| | | @Override |
| | | public void reflexDifferent(List<ModifyAttributeInfoDO> differentAttributeList) throws VciBaseException { |
| | | public void reflexDifferent(List<ModifyAttributeInfo> differentAttributeList, List<BtmTypeVO> btmTypeVOList, List<LinkTypeVO> linkTypeVOList) throws VciBaseException { |
| | | VciBaseUtil.alertNotNull(differentAttributeList, "要清理的数据库的信息为空"); |
| | | Map<String, BtmTypeVO> idBtmTypeMap = btmTypeVOList.stream().collect(Collectors.toMap(BtmTypeVO::getId, t -> t, (o1, o2) -> o1)); |
| | | Map<String, LinkTypeVO> idLinkTypeMap = linkTypeVOList.stream().collect(Collectors.toMap(LinkTypeVO::getId, t -> t, (o1, o2) -> o1)); |
| | | differentAttributeList.forEach(s -> { |
| | | // BtmTypeVO btmType = idBtmTypeMap.get(id); |
| | | if (ModifyTableTaskEnum.CREATE.getValue().equalsIgnoreCase(s.getTaskName())) { |
| | | //直接创建表格 |
| | | // createDbTableForBtm(btmType); |
| | | createDbTablesById(s.getId()); |
| | | } else { |
| | | String addAttributes = s.getAddAttributes(); |
| | | if (StringUtils.isNotBlank(s.getModifyAttributes()) || StringUtils.isNotBlank(s.getDeleteAttributes())) { |
| | | //需要去判断是否可以更改 |
| | | if (checkTableHasDataByTableName(s.getTableName())) { |
| | | //有数据的话,得需要判断属性是否可以修改 |
| | | if (StringUtils.isNotBlank(s.getDeleteAttributes())) { |
| | | s.setHandleResult("表格存在数据,不允许删除字段"); |
| | | return; |
| | | } |
| | | List<DdlTableInDataBaseBO> columnInDbList = selectTableColumnInfo(s.getTableName()); |
| | | Map<String, DdlTableInDataBaseBO> columnInDbMap = new HashMap<>(); |
| | | if (!CollectionUtils.isEmpty(columnInDbList)) { |
| | | columnInDbMap = columnInDbList.stream().collect(Collectors.toMap(t -> t.getId().toLowerCase(), k -> k)); |
| | | } |
| | | List<BtmTypeAttributeVO> attributeVOList = idBtmTypeMap.getOrDefault(s.getId(), new BtmTypeVO()).getAttributes().stream().filter(vo -> Func.toStrList(",", s.getAddAttributes()).stream().anyMatch(id -> StringUtils.equals(vo.getId().toLowerCase(), id))).collect(Collectors.toList()); |
| | | //有可能之前判断的时候数据库存在,而现在不存在了 |
| | | List<String> unExitInDbIdList = new ArrayList<>(); |
| | | Map<String, DdlTableInDataBaseBO> finalColumnInDbMap = columnInDbMap; |
| | | List<BtmTypeAttributeVO> needEditAttributeVOList = new ArrayList<>(); |
| | | if (!CollectionUtils.isEmpty(attributeVOList)) { |
| | | attributeVOList.forEach(t -> { |
| | | if (finalColumnInDbMap.containsKey(t.getId().toLowerCase().trim())) { |
| | | needEditAttributeVOList.add(t); |
| | | } else { |
| | | unExitInDbIdList.add(t.getId()); |
| | | } |
| | | }); |
| | | } |
| | | if (!CollectionUtils.isEmpty(needEditAttributeVOList)) { |
| | | List<BtmTypeAttributeVO> unModifyAttrVOList = needEditAttributeVOList.stream().filter(t -> !checkNotCanEditAttr(t.getAttrDataType(), finalColumnInDbMap.get(t.getId().trim().toLowerCase()))).collect(Collectors.toList()); |
| | | if (!CollectionUtils.isEmpty(unModifyAttrVOList)) { |
| | | s.setHandleResult("表格存在数据," + unModifyAttrVOList.stream().map(BtmTypeAttributeVO::getId).collect(Collectors.joining(",")) + "这些字段不能修改"); |
| | | return; |
| | | } |
| | | needEditAttributeVOList.stream().forEach(t -> { |
| | | String attributeLowerId = t.getId().trim().toLowerCase(); |
| | | DdlTableInDataBaseBO columInDb = finalColumnInDbMap.get(attributeLowerId); |
| | | if (!t.getName().equalsIgnoreCase(columInDb.getName())) { |
| | | ddlMapper.commentColumnTable(s.getTableName(), t.getId(), t.getName()); |
| | | } |
| | | if (columInDb.getAttributeLength() != null && t.getAttributeLength() != null |
| | | && t.getAttributeLength() < columInDb.getAttributeLength()) { |
| | | t.setAttributeLength(columInDb.getAttributeLength()); |
| | | } |
| | | List<BtmTypeAttributeVO> attributeVOS = new ArrayList<>(); |
| | | |
| | | boolean nowNeedNull = false; |
| | | if (!YES.equalsIgnoreCase(columInDb.getNullableFlag()) && !t.isNullableFlag()) { |
| | | //说明以前是不能为空的,但是以前也是不为空的,修改会报错,所以这个调整一下 |
| | | t.setNullableFlag(true); |
| | | } else if (!YES.equalsIgnoreCase(columInDb.getNullableFlag()) && t.isNullableFlag()) { |
| | | //以前不为空,现在为空 |
| | | nowNeedNull = true; |
| | | } |
| | | attributeVOS.add(t); |
| | | String attributeSql = getCreateSqlByAttributeForBtm(attributeVOS); |
| | | if (nowNeedNull) { |
| | | attributeSql += StringPool.SPACE + "null" + StringPool.SPACE; |
| | | } |
| | | ddlMapper.modifyTableBySql(s.getTableName(), attributeSql); |
| | | }); |
| | | } |
| | | if (!CollectionUtils.isEmpty(unExitInDbIdList)) { |
| | | if (StringUtils.isBlank(addAttributes)) { |
| | | addAttributes = String.join(",", unExitInDbIdList); |
| | | } else { |
| | | addAttributes += "," + String.join(",", unExitInDbIdList); |
| | | ; |
| | | } |
| | | } |
| | | } else { |
| | | //说明没有数据,那可以把修改的列和删除的列都删除,然后重新添加 |
| | | if (StringUtils.isNotBlank(s.getModifyAttributes())) { |
| | | List<BtmTypeAttributeVO> attributeVOList = idBtmTypeMap.getOrDefault(s.getId(), new BtmTypeVO()).getAttributes().stream().filter(vo -> Func.toStrList(",", s.getModifyAttributes()).stream().anyMatch(id -> StringUtils.equals(vo.getId(), id))).collect(Collectors.toList()); |
| | | if (!CollectionUtils.isEmpty(attributeVOList)) { |
| | | attributeVOList.forEach(t -> { |
| | | List<BtmTypeAttributeVO> attributeVOS = new ArrayList<>(); |
| | | attributeVOS.add(t); |
| | | dropColumnByName(s.getTableName(), t.getId()); |
| | | addColumnForTable(s.getTableName(), attributeVOS); |
| | | ddlMapper.commentColumnTable(s.getTableName(), t.getId(), t.getName()); |
| | | }); |
| | | } |
| | | } |
| | | //需要删除的列 |
| | | if (StringUtils.isNotBlank(s.getDeleteAttributes())) { |
| | | List<String> attributeIdList = VciBaseUtil.str2List(s.getDeleteAttributes()); |
| | | Optional.ofNullable(attributeIdList).orElseGet(ArrayList::new).forEach(t -> { |
| | | dropColumnByName(s.getTableName(), t); |
| | | }); |
| | | } |
| | | } |
| | | } |
| | | if (StringUtils.isNotBlank(addAttributes)) { |
| | | //只是添加属性值 |
| | | if (BusinessTypeEnum.LINK.getValue().equalsIgnoreCase(s.getBusinessType())) { |
| | | List<LinkTypeAttributeVO> attributeVOList = idLinkTypeMap.getOrDefault(s.getId(), new LinkTypeVO()).getAttributes().stream().filter(vo -> Func.toStrList(",", s.getAddAttributes()).stream().anyMatch(id -> StringUtils.equals(vo.getId(), id))).collect(Collectors.toList()); |
| | | //是链接类型 |
| | | // List<LinkTypeAttributeVO> attributeVOList = linkTypeService.listAttributeVOByIdsForLink(s.getId(), VciBaseUtil.str2List(s.getAddAttributes())); |
| | | addColumn2TableForLink(attributeVOList); |
| | | } else { |
| | | List<BtmTypeAttributeVO> attributeVOList = idBtmTypeMap.getOrDefault(s.getId(), new BtmTypeVO()).getAttributes().stream().filter(vo -> Func.toStrList(",", s.getAddAttributes()).stream().anyMatch(id -> StringUtils.equals(vo.getId(), id))).collect(Collectors.toList()); |
| | | addColumn2TableForBtm(attributeVOList); |
| | | } |
| | | } |
| | | } |
| | | }); |
| | | } |
| | | |
| | | private boolean checkNotCanEditAttr(String attributeDataType, DdlTableInDataBaseBO tableInDB) { |
| | | if (tableInDB != null && StringUtils.isNotBlank(attributeDataType)) { |
| | | List<VciFieldTypeEnum> fieldTypeEnums = dllMapper.listFieldByColumnStr(tableInDB.getAttrDataType()); |
| | | Set<String> names = fieldTypeEnums.stream().map(Enum::name).collect(Collectors.toSet()); |
| | | if (names.contains(attributeDataType)) { |
| | | return true; |
| | | } |
| | | return true; |
| | | } else { |
| | | //因为数据库里没有这个表格时 |
| | | return true; |
| | | } |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | @Override |
| | | public List<DdlTableBO> listTableInfoInDB(Collection<String> tableNamesCollections) throws VciBaseException { |
| | | VciBaseUtil.alertCollectionNotNull("数据库表名称不能为空", tableNamesCollections); |
| | | VciBaseUtil.alertNotNull("数据库表名称不能为空", tableNamesCollections); |
| | | List<DdlTableBO> tableBOList = new ArrayList<>(); |
| | | tableNamesCollections.stream().forEach(s -> { |
| | | tableBOList.add(ddlMapper.selectTableComment(s)); |
| | |
| | | * |
| | | * @param tableDataBO 要写入的数据 |
| | | * @return word 文件路径 |
| | | */ |
| | | *//* |
| | | @Override |
| | | public String writeDataToWord(WordMergeStartTableDataBO tableDataBO) { |
| | | return null; |
| | | } |
| | | }*/ |
| | | |
| | | /** |
| | | * 判断是否为兼容性的表,这些表不应该被创建和修改 |
| | |
| | | /** |
| | | * 业务类型数据库新增或修改表 |
| | | * |
| | | * @param dto 业务类型传输对象 |
| | | * @param ddlDTO 业务类型链接类型传输对象 |
| | | * @return 执行结果 |
| | | */ |
| | | @Override |
| | | public R submitBtmType(BtmTypeDTO dto) { |
| | | VciBaseUtil.alertNotNull(dto, "业务类型", dto.getAttributesDTOList(), "业务类型属性"); |
| | | BtmTypeVO btmTypeVO = Objects.requireNonNull(BeanUtil.copy(dto, BtmTypeVO.class)); |
| | | if (Func.isNotBlank(dto.getTableName())) { |
| | | checkModifyOrCreateTable(btmTypeVO); |
| | | } else { |
| | | if (checkTableExistById(dto.getId())) { |
| | | String tableName = getTableNameByBtmTypeId(dto.getId()); |
| | | btmTypeVO.setTableName(tableName); |
| | | checkModifyOrCreateTable(btmTypeVO); |
| | | } else { |
| | | String tableName = TABLE_PREFIX + StringPool.UNDERSCORE + dto.getDomain().toUpperCase(Locale.ROOT) + StringPool.UNDERSCORE + dto.getId().toUpperCase(Locale.ROOT); |
| | | btmTypeVO.setTableName(tableName); |
| | | createDbTableForBtm(btmTypeVO); |
| | | public R<List<ModifyAttributeInfo>> submit(BtmAndLinkTypeDdlDTO ddlDTO) { |
| | | try { |
| | | List<ModifyAttributeInfo> changedList = new ArrayList<>(); |
| | | if (!CollectionUtils.isEmpty(ddlDTO.getBtmTypeList())) { |
| | | List<BtmTypeVO> btmTypeList = ddlDTO.getBtmTypeList(); |
| | | putBtm(btmTypeList.toArray(new BtmTypeVO[0])); |
| | | changedList.addAll(checkDifferent(btmTypeList, null)); |
| | | removeBtm(btmTypeList.toArray(new BtmTypeVO[0])); |
| | | } |
| | | } |
| | | return R.success("000000"); |
| | | } |
| | | |
| | | /** |
| | | * 检查业务类型的表是否存在,如果存在则比较差别进行修改,不存在则创建 |
| | | * |
| | | * @param btmTypeVO 业务类型 |
| | | */ |
| | | private void checkModifyOrCreateTable(BtmTypeVO btmTypeVO) { |
| | | if (checkTableExistByTableName(btmTypeVO.getTableName())) { |
| | | // 表已存在,校验属性 |
| | | List<BtmTypeVO> vos = new ArrayList<>(); |
| | | vos.add(btmTypeVO); |
| | | List<ModifyAttributeInfoDO> differentAttributeList = checkDifferent(vos, null); |
| | | if (CollectionUtils.isEmpty(differentAttributeList)) { |
| | | // 可以直接删除再创建 |
| | | dropTableByName(btmTypeVO.getTableName()); |
| | | createDbTableForBtm(btmTypeVO); |
| | | } else { |
| | | // TODO: 修改的暂时还没做 |
| | | if (!CollectionUtils.isEmpty(ddlDTO.getLinkTypeList())) { |
| | | List<LinkTypeVO> linkTypeList = ddlDTO.getLinkTypeList(); |
| | | putLink(linkTypeList.toArray(new LinkTypeVO[0])); |
| | | changedList.addAll(checkDifferent(null,linkTypeList)); |
| | | removeLink(linkTypeList.toArray(new LinkTypeVO[0])); |
| | | } |
| | | } else { |
| | | createDbTableForBtm(btmTypeVO); |
| | | R<List<ModifyAttributeInfo>> result = R.success("数据库操作成功"); |
| | | result.setData(changedList); |
| | | return result; |
| | | } catch (ServiceException e) { |
| | | throw new RuntimeException(e.getMessage()); |
| | | } |
| | | } |
| | | |
| | |
| | | */ |
| | | @Override |
| | | public DdlTableBO getTableColumnByTableName(String tableName) { |
| | | VciBaseUtil.alertNotNull(tableName,"表名"); |
| | | VciBaseUtil.alertNotNull(tableName, "表名"); |
| | | DdlTableBO tableBO = getTableInfoByTableName(tableName); |
| | | List<DdlTableInDataBaseBO> columnInfo = selectTableColumnInfo(tableName); |
| | | tableBO.setColumns(columnInfo); |
| | |
| | | |
| | | /** |
| | | * 表名获取表信息 |
| | | * |
| | | * @param tableName 表名 |
| | | * @return 表信息 |
| | | */ |
| | | private DdlTableBO getTableInfoByTableName(String tableName){ |
| | | private DdlTableBO getTableInfoByTableName(String tableName) { |
| | | VciBaseUtil.alertNotNull(tableName, "表格名称"); |
| | | return ddlMapper.selectTableComment(tableName); |
| | | } |
| | |
| | | * @return 表信息集合 |
| | | */ |
| | | @Override |
| | | public List<DdlTableBO> getAllTableInfo() { |
| | | public List<BtmTypeVO> getAllTableInfo() { |
| | | List<String> allTableName = listAllTableName(); |
| | | if (!CollectionUtils.isEmpty(allTableName)) { |
| | | List<DdlTableBO> allTableBO = listTableInfoInDB(allTableName); |
| | | List<BtmTypeVO> btmTypeVOList = new ArrayList<>(); |
| | | allTableBO.forEach(table -> { |
| | | table.setColumns(selectTableColumnInfo(table.getTableName())); |
| | | BtmTypeVO vo = new BtmTypeVO(); |
| | | vo.setTableName(table.getTableName().toLowerCase()); |
| | | vo.setDescription(table.getTableDesc()); |
| | | List<DdlTableInDataBaseBO> columns = selectTableColumnInfo(table.getTableName()); |
| | | List<BtmTypeAttributeVO> attributes = new ArrayList<>(); |
| | | columns.forEach(col -> { |
| | | BtmTypeAttributeVO attributeVO = new BtmTypeAttributeVO(); |
| | | attributeVO.setId(col.getId().toLowerCase()); |
| | | attributeVO.setName(col.getName()); |
| | | attributeVO.setNullableFlag(col.getNullableFlag()); |
| | | attributeVO.setAttributeLength(col.getAttributeLength()); |
| | | attributeVO.setPrecisionLength(col.getPrecisionLength()); |
| | | attributeVO.setScaleLength(col.getScaleLength()); |
| | | VciFieldTypeEnum field = dllMapper.getFieldTypeByColumnStr(col.getAttrDataType(), col.getAttributeLength()); |
| | | if (field != null){ |
| | | attributeVO.setAttrDataType(field.name()); |
| | | } |
| | | if(col.getPrecisionLength() != null){ |
| | | attributeVO.setAttrDataType(VciFieldTypeEnum.VTDouble.name()); |
| | | } |
| | | attributes.add(attributeVO); |
| | | }); |
| | | vo.setAttributes(attributes); |
| | | btmTypeVOList.add(vo); |
| | | }); |
| | | return allTableBO; |
| | | return btmTypeVOList; |
| | | } |
| | | return new ArrayList<>(); |
| | | } |