| | |
| | | package com.vci.web.service.impl; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.vci.client.common.providers.ClientServiceProvider; |
| | | import com.vci.corba.common.PLException; |
| | | import com.vci.corba.omd.atm.AttributeDef; |
| | | import com.vci.corba.omd.data.BusinessObject; |
| | | import com.vci.corba.omd.etm.EnumItem; |
| | | import com.vci.corba.omd.etm.EnumType; |
| | | import com.vci.dto.OsEnumDTO; |
| | | import com.vci.dto.OsEnumItemDTO; |
| | | import com.vci.omd.objects.OtherInfo; |
| | | import com.vci.omd.utils.ObjectTool; |
| | | import com.vci.pagemodel.*; |
| | | 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.UserSecretEnum; |
| | | import com.vci.starter.web.exception.VciBaseException; |
| | | import com.vci.starter.web.pagemodel.BaseResult; |
| | | import com.vci.starter.web.pagemodel.DataGrid; |
| | | import com.vci.starter.web.pagemodel.PageHelper; |
| | | import com.vci.starter.web.util.VciBaseUtil; |
| | | import com.vci.starter.web.util.VciDateUtil; |
| | | import com.vci.web.constant.EnumIdConstant; |
| | | import com.vci.web.pageModel.KeyValue; |
| | | import com.vci.web.pageModel.OsEnumItemVO; |
| | | import com.vci.web.pageModel.OsEnumVO; |
| | | import com.vci.web.pageModel.OsStatusVO; |
| | | import com.vci.starter.web.util.*; |
| | | import com.vci.constant.EnumIdConstant; |
| | | import com.vci.web.service.OsEnumServiceI; |
| | | import com.vci.web.service.OsLifeCycleServiceI; |
| | | import com.vci.web.service.WebBoServiceI; |
| | | 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.util.CollectionUtils; |
| | | import org.springframework.web.util.HtmlUtils; |
| | | |
| | | import java.io.File; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | import static com.vci.web.constant.EnumIdConstant.LC_STATUS_SUBFIX; |
| | | import static com.vci.constant.EnumIdConstant.LC_STATUS_SUBFIX; |
| | | |
| | | /** |
| | | * 枚举服务--已经调用平台底层了,不再提供dao层 |
| | |
| | | private WebBoServiceI boService; |
| | | |
| | | /** |
| | | * 日志 |
| | | */ |
| | | private Logger logger = LoggerFactory.getLogger(getClass()); |
| | | |
| | | |
| | | /** |
| | | * 加载自身 |
| | | */ |
| | | @Autowired(required = false) |
| | | @Lazy |
| | | private OsEnumServiceI self; |
| | | |
| | | /** |
| | | * 必填列 |
| | | */ |
| | | private List<Integer> ColumnNameisRed = new ArrayList<Integer>(); |
| | | |
| | | /** |
| | | * 日志 |
| | | */ |
| | | private Logger logger = LoggerFactory.getLogger(getClass()); |
| | | |
| | | /** |
| | | * 使用编号获取枚举明细 |
| | |
| | | int userSecret = WebUtil.getInt(WebUtil.getCurrentUserSessionInfoNotException().getUserSecret()); |
| | | List<OsEnumItemVO> itemVOS = new ArrayList<>(); |
| | | if(enumVO!=null && !CollectionUtils.isEmpty(enumVO.getItems())){ |
| | | itemVOS = enumVO.getItems().stream().filter(item->WebUtil.getInt(item.getId()) <= userSecret).collect(Collectors.toList()); |
| | | itemVOS = enumVO.getItems().stream().filter(item->WebUtil.getInt(item.getValue()) <= userSecret).collect(Collectors.toList()); |
| | | } |
| | | return itemVOS; |
| | | }else{ |
| | | return enumVOMap.getOrDefault(enumCode.toLowerCase(),new OsEnumVO()).getItems(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取枚举的内容 |
| | | * @param enumCode 枚举的编号(英文名称) |
| | |
| | | } |
| | | |
| | | /** |
| | | * 枚举定义列表查询(缓存里面找且带name查询条件) |
| | | * @param enumName |
| | | * @return 枚举的显示对象 |
| | | */ |
| | | @Override |
| | | public List<OsEnumVO> getEnumTypeList(String enumName) throws PLException { |
| | | return enumDO2VOs(Arrays.stream(platformClientUtil.getEnumService().getEnumTypes(enumName,1,1)).collect(Collectors.toList())); |
| | | } |
| | | |
| | | /** |
| | | * 查看枚举的使用范围 |
| | | * @param enumName 枚举名称 |
| | | * @return |
| | | * @throws PLException |
| | | */ |
| | | @Override |
| | | public List<Map<String,String>> getUsedEnumList(String enumName) throws PLException { |
| | | if(Func.isBlank(enumName)){ |
| | | throw new PLException("500",new String[]{"请选择要查询应用范围的枚举!"}); |
| | | } |
| | | String[] attrubuteNames = platformClientUtil.getAttributeService().getAttrubyteNamesByEMName(enumName); |
| | | if(Func.isEmpty(attrubuteNames)){ |
| | | return new ArrayList<>(); |
| | | } |
| | | List<Map<String,String>> attrubuteMapList = new ArrayList<>(); |
| | | Arrays.stream(attrubuteNames).forEach(attrName->{ |
| | | Map<String, String> itemMap = new HashMap<>(); |
| | | itemMap.put("enumName",enumName); |
| | | itemMap.put("source",attrName); |
| | | attrubuteMapList.add(itemMap); |
| | | }); |
| | | return attrubuteMapList; |
| | | } |
| | | |
| | | /** |
| | | * 根据枚举英文名称获取枚举类型 |
| | | * @param id |
| | | * @return |
| | | */ |
| | | @Override |
| | | public OsEnumVO getEnumTypeById(String id) throws PLException { |
| | | if (Func.isBlank(id)) { |
| | | return null; |
| | | } |
| | | EnumType enumType = platformClientUtil.getEnumService().getEnumTypeByName(id); |
| | | return enumDO2VO(enumType); |
| | | } |
| | | |
| | | /** |
| | | * 新增枚举类型 |
| | | * @param osEnumDTO |
| | | * @return |
| | | */ |
| | | @Override |
| | | public boolean addEnumType(OsEnumDTO osEnumDTO) throws PLException { |
| | | //1、枚举值判空 |
| | | VciBaseUtil.alertNotNull( |
| | | osEnumDTO,"枚举类型对象", |
| | | osEnumDTO.getId(),"枚举名称", |
| | | osEnumDTO.getEnumValueDataType(),"枚举的类型", |
| | | osEnumDTO.getLength(),"枚举长度" |
| | | ); |
| | | //2、枚举类型名称正则校验和查重 |
| | | if(!osEnumDTO.getId().matches("^[A-Za-z]+$")){ |
| | | throw new PLException("500", new String[] { "枚举名称只能为英文字母!"}); |
| | | } |
| | | OsEnumVO osEnumVO = this.getEnumTypeById(osEnumDTO.getId()); |
| | | if(Func.isNotEmpty(osEnumVO) && Func.isNotBlank(osEnumVO.getOid())){ |
| | | throw new PLException("500", new String[] { "枚举名称已存在,请修改枚举名称!"}); |
| | | } |
| | | //枚举项名称判重等操作 |
| | | checkOsEnumItem(osEnumDTO); |
| | | //默认值处理 |
| | | String userId = WebThreadLocalUtil.getCurrentUserSessionInfoInThread().getUserId(); |
| | | osEnumDTO.setCreator(userId); |
| | | //手动设置主键,否则平台直接用没有主键的枚举放进了缓存中,导致后续查询的数据没有oid |
| | | osEnumDTO.setOid(VciBaseUtil.getPk().toUpperCase(Locale.ROOT)); |
| | | osEnumDTO.setLastModifier(userId); |
| | | return platformClientUtil.getEnumService().addEnumType(this.osEnumDTO2EnumType(osEnumDTO)); |
| | | } |
| | | |
| | | /** |
| | | * 修改枚举类型 |
| | | * @param osEnumDTO |
| | | * @return |
| | | */ |
| | | @Override |
| | | public boolean updateEnumType(OsEnumDTO osEnumDTO) throws PLException { |
| | | //1、枚举值判空 |
| | | VciBaseUtil.alertNotNull( |
| | | osEnumDTO,"枚举类型对象", |
| | | osEnumDTO.getOid(),"枚举的主键", |
| | | osEnumDTO.getId(),"枚举名称", |
| | | osEnumDTO.getEnumValueDataType(),"枚举的类型", |
| | | osEnumDTO.getLength(),"枚举长度" |
| | | ); |
| | | //2、枚举类型名称(名称不允许修改)所以可以用名称查询是否存在 |
| | | OsEnumVO osEnumVO = this.getEnumTypeById(osEnumDTO.getId()); |
| | | if(Func.isEmpty(osEnumVO) || Func.isBlank(osEnumVO.getOid())){ |
| | | throw new PLException("500", new String[] { "当前修改枚举对象不存在,请刷新后重试!"}); |
| | | } |
| | | //3、枚举项判重,和长度等校验 |
| | | checkOsEnumItem(osEnumDTO); |
| | | //将osEnumVO中的默认值赋值给osEnumDTO对象 |
| | | String userId = WebThreadLocalUtil.getCurrentUserSessionInfoInThread().getUserId(); |
| | | osEnumDTO.setLastModifier(userId); |
| | | osEnumDTO.setCreator(osEnumVO.getCreator()); |
| | | osEnumDTO.setCreateTime(osEnumVO.getCreateTime()); |
| | | //修改和删除时ts参数为前端必传参数 |
| | | return platformClientUtil.getEnumService().modifyEnumType(osEnumDTO2EnumType(osEnumDTO)); |
| | | } |
| | | |
| | | /** |
| | | * 删除枚举类型(包含删除枚举项功能) |
| | | * @param osEnumDTOS |
| | | * @return |
| | | */ |
| | | @Override |
| | | public boolean deleteEnumTypes(List<OsEnumDTO> osEnumDTOS) throws PLException { |
| | | VciBaseUtil.alertNotNull(osEnumDTOS,"待删除的枚举列表"); |
| | | //平台的deleteEnumTypes方法必传三个参数,oid、name和ts |
| | | List<EnumType> enumTypes = new ArrayList<>(); |
| | | for(OsEnumDTO osEnumDTO : osEnumDTOS){ |
| | | //oid和ts判空 |
| | | String oid = osEnumDTO.getOid(); |
| | | //name主要用来对缓存数据删除 |
| | | String name = osEnumDTO.getName(); |
| | | Date ts = osEnumDTO.getTs(); |
| | | if(Func.isBlank(oid) || Func.isBlank(name) || Func.isEmpty(ts)){ |
| | | throw new PLException("500",new String[]{"待删除的枚举列表中主键【oid】、调整时间【ts】、属性名【name】不能为空!"}); |
| | | } |
| | | //判断枚举是否有被引用 |
| | | List<Map<String, String>> usedEnumList = this.getUsedEnumList(name); |
| | | if(Func.isNotEmpty(usedEnumList)){ |
| | | throw new PLException("500",new String[]{"删除的枚举中,枚举名称为:【" + name + "】,已被引用!"}); |
| | | } |
| | | EnumType enumType = new EnumType(); |
| | | enumType.oid = oid; |
| | | enumType.name = name; |
| | | enumType.ts = Func.format(ts,VciDateUtil.DateTimeMillFormat); |
| | | enumTypes.add(enumType); |
| | | } |
| | | if(Func.isEmpty(enumTypes)){ |
| | | return false; |
| | | } |
| | | return platformClientUtil.getEnumService().deleteEnumTypes(enumTypes.toArray(new EnumType[enumTypes.size()])); |
| | | } |
| | | |
| | | /** |
| | | * 导出枚举类型 |
| | | * @param exportFileName 导出的文件名 |
| | | * @param enumNames 需要导出的枚举名称 |
| | | * @return |
| | | */ |
| | | @Override |
| | | public String exportEnumTypes(String exportFileName,String enumNames,boolean flag/*控制导出的列名是否和导入模板一致*/) throws PLException { |
| | | if(Func.isBlank(enumNames)){ |
| | | throw new PLException("500",new String[]{"请勾选要导出的枚举!"}); |
| | | } |
| | | //界面没传名称,使用默认导出名称 |
| | | exportFileName = Func.isBlank(exportFileName) ? "枚举类型导出_" + Func.format(new Date(),"yyyy-MM-dd HHmmss.sss"):exportFileName; |
| | | //设置列名 |
| | | List<String> columns = this.getCloumns(flag); |
| | | |
| | | //写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<WriteExcelData> excelDataList = new ArrayList<>(); |
| | | //设置列头 |
| | | for (int index = 0; index < columns.size(); index++) { |
| | | excelDataList.add(new WriteExcelData(0,index, columns.get(index))); |
| | | } |
| | | //按照枚举名查询枚举,然后处理枚举导出 |
| | | List<String> enumNameList = Func.toStrList(enumNames); |
| | | int startRow = 1; |
| | | for (int i = 0; i < enumNameList.size(); i++) { |
| | | //根据枚举名称查询枚举和枚举项 |
| | | OsEnumVO osEnumVO = this.getEnumTypeById(enumNameList.get(i)); |
| | | //OsEnumVO osEnumVO = getTestJson(i); |
| | | //查询结果不应该为空 |
| | | if(Func.isEmpty(osEnumVO) && Func.isBlank(osEnumVO.getOid())){ |
| | | throw new PLException("500",new String[]{"未查询到枚举名为【" + enumNameList.get(i) + "】的枚举,请刷新后重新导出!"}); |
| | | } |
| | | //按照导出列名控制变量和枚举项不为空时决定是否需要考虑合并行问题 |
| | | List<OsEnumItemVO> enumVOItems = osEnumVO.getItems(); |
| | | //判断是按照什么列名导出 |
| | | if(flag){ |
| | | for (int j = 0; j < enumVOItems.size(); j++) { |
| | | OsEnumItemVO osEnumItemVO = enumVOItems.get(j); |
| | | excelDataList.add(new WriteExcelData(i+j+1,0, osEnumVO.getId())); |
| | | excelDataList.add(new WriteExcelData(i+j+1,1, osEnumVO.getName())); |
| | | excelDataList.add(new WriteExcelData(i+j+1,2, osEnumVO.getEnumValueDataType())); |
| | | excelDataList.add(new WriteExcelData(i+j+1,3, osEnumVO.getLength())); |
| | | |
| | | excelDataList.add(new WriteExcelData(i+j+1,4,osEnumItemVO.getName())); |
| | | excelDataList.add(new WriteExcelData(i+j+1,5, osEnumItemVO.getValue())); |
| | | excelDataList.add(new WriteExcelData(i+j+1,6, osEnumItemVO.getDescription())); |
| | | } |
| | | }else{ |
| | | //枚举项不为空时需要进行合并行处理 |
| | | if(Func.isNotEmpty(enumVOItems) || enumVOItems.size() > 1){ |
| | | //excelDataList.add(new WriteExcelData(startRow,(enumVOItems.size()+startRow)-1,true,true,0, osEnumVO.getOid())); |
| | | excelDataList.add(new WriteExcelData(startRow,(enumVOItems.size()+startRow)-1,true,true,0, osEnumVO.getId())); |
| | | excelDataList.add(new WriteExcelData(startRow,(enumVOItems.size()+startRow)-1,true,true,1, osEnumVO.getName())); |
| | | excelDataList.add(new WriteExcelData(startRow,(enumVOItems.size()+startRow)-1,true,true,2, osEnumVO.getEnumValueDataType()+"("+osEnumVO.getEnumValueDataTypeText()+")")); |
| | | excelDataList.add(new WriteExcelData(startRow,(enumVOItems.size()+startRow)-1,true,true,3, osEnumVO.getLength())); |
| | | //excelDataList.add(new WriteExcelData(startRow,(enumVOItems.size()+startRow)-1,true,true,5, osEnumVO.getCreator())); |
| | | excelDataList.add(new WriteExcelData(startRow,(enumVOItems.size()+startRow)-1,true,true,4, Func.format(osEnumVO.getCreateTime(),"yyyy年MM月dd日 hh:mm:ss"))); |
| | | |
| | | //处理枚举项写入 |
| | | for (int j = 0; j < enumVOItems.size(); j++) { |
| | | OsEnumItemVO osEnumItemVO = enumVOItems.get(j); |
| | | excelDataList.add(new WriteExcelData(startRow+j,5, true,osEnumItemVO.getName())); |
| | | excelDataList.add(new WriteExcelData(startRow+j,6, true, osEnumItemVO.getValue())); |
| | | excelDataList.add(new WriteExcelData(startRow+j,7, true, osEnumItemVO.getDescription())); |
| | | } |
| | | |
| | | startRow += enumVOItems.size(); |
| | | }else{ |
| | | //枚举项为空时就不需要合并行 |
| | | //excelDataList.add(new WriteExcelData(startRow,0, true,osEnumVO.getOid())); |
| | | excelDataList.add(new WriteExcelData(startRow,0, true, osEnumVO.getId())); |
| | | excelDataList.add(new WriteExcelData(startRow,1, true, osEnumVO.getName())); |
| | | excelDataList.add(new WriteExcelData(startRow,1, true, osEnumVO.getEnumValueDataType()+"("+osEnumVO.getEnumValueDataTypeText()+")")); |
| | | excelDataList.add(new WriteExcelData(startRow,3, true, osEnumVO.getLength())); |
| | | //excelDataList.add(new WriteExcelData(startRow,5, true, osEnumVO.getCreator())); |
| | | excelDataList.add(new WriteExcelData(startRow,4, true, Func.format(osEnumVO.getCreateTime(),"yyyy年MM月dd日 hh:mm:ss"))); |
| | | } |
| | | //记录下上一组数据结束时的行号,方便用做合并行的起始行 |
| | | startRow += 1; |
| | | } |
| | | } |
| | | WriteExcelOption excelOption = new WriteExcelOption(excelDataList); |
| | | ExcelUtil.writeDataToFile(excelPath, excelOption); |
| | | return excelPath; |
| | | } |
| | | |
| | | /** |
| | | * 下载导入模板 |
| | | * @param exportFileName |
| | | * @return |
| | | */ |
| | | @Override |
| | | public String downloadEnumTemplate(String exportFileName) { |
| | | //界面没传名称,使用默认导出名称 |
| | | exportFileName = Func.isBlank(exportFileName) ? "枚举导入模板_" + Func.format(new Date(),"yyyy-MM-dd HHmmss.sss"):exportFileName; |
| | | //设置列名 |
| | | List<String> columns = this.getCloumns(true); |
| | | //写excel |
| | | String excelPath = LocalFileUtil.getDefaultTempFolder() + File.separator + exportFileName + ".xls"; |
| | | //设置必填列 |
| | | ColumnNameisRed.clear(); |
| | | ColumnNameisRed.add(0); |
| | | ColumnNameisRed.add(2); |
| | | ColumnNameisRed.add(3); |
| | | ColumnNameisRed.add(4); |
| | | ColumnNameisRed.add(5); |
| | | try { |
| | | new File(excelPath).createNewFile(); |
| | | } catch (Throwable e) { |
| | | throw new VciBaseException(LangBaseUtil.getErrorMsg(e), new String[]{excelPath}, e); |
| | | } |
| | | //设置列 |
| | | List<WriteExcelData> 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))); |
| | | } |
| | | } |
| | | List<OsEnumVO> exportEnumTempExample = this.getExportEnumTempExample(); |
| | | for (int i = 0; i < exportEnumTempExample.size(); i++) { |
| | | OsEnumVO osEnumVO = exportEnumTempExample.get(i); |
| | | List<OsEnumItemVO> enumVOItems = osEnumVO.getItems(); |
| | | for (int j = 0; j < enumVOItems.size(); j++) { |
| | | OsEnumItemVO osEnumItemVO = enumVOItems.get(j); |
| | | excelDataList.add(new WriteExcelData(i+j+1,0, osEnumVO.getId())); |
| | | excelDataList.add(new WriteExcelData(i+j+1,1, osEnumVO.getName())); |
| | | excelDataList.add(new WriteExcelData(i+j+1,2, osEnumVO.getEnumValueDataType())); |
| | | excelDataList.add(new WriteExcelData(i+j+1,3, osEnumVO.getLength())); |
| | | |
| | | excelDataList.add(new WriteExcelData(i+j+1,4,osEnumItemVO.getName())); |
| | | excelDataList.add(new WriteExcelData(i+j+1,5, osEnumItemVO.getValue())); |
| | | excelDataList.add(new WriteExcelData(i+j+1,6, osEnumItemVO.getDescription())); |
| | | } |
| | | } |
| | | WriteExcelOption excelOption = new WriteExcelOption(excelDataList); |
| | | ExcelUtil.writeDataToFile(excelPath, excelOption); |
| | | return excelPath; |
| | | } |
| | | |
| | | /** |
| | | * 获取导出或导入模板的列名 |
| | | * @param flag 是否获取导入模板列名 |
| | | * @return |
| | | */ |
| | | private List<String> getCloumns(boolean flag){ |
| | | if(flag){ |
| | | return new ArrayList<>( |
| | | Arrays.asList("枚举名称", "标签", "返回类型", "长度", "枚举项名称(当前枚举下有枚举项时必填)", |
| | | "枚举值(当前枚举下有枚举项时必填)", "描述","*注意*:第二行开始的数据为示例数据,导入前请将其删除," + |
| | | "当导入的枚举下具备多个枚举项时,应按照示例enumb的写法" |
| | | ) |
| | | ); |
| | | } |
| | | return new ArrayList<>( |
| | | Arrays.asList("枚举名称", "标签", "返回类型", "长度", |
| | | "创建时间", "枚举项名称", "枚举值", "描述" |
| | | ) |
| | | ); |
| | | } |
| | | |
| | | /** |
| | | * 导入枚举 |
| | | * @param file |
| | | * @return |
| | | * @throws Exception |
| | | */ |
| | | @Override |
| | | public BaseResult importEnumTypes(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<OsEnumPO> poList = ExcelUtil.readDataObjectFromExcel(file, OsEnumPO.class,excelOption,(value, po, fieldName)->{}); |
| | | //去除都是空的情况 |
| | | if(CollectionUtils.isEmpty(poList)){ |
| | | return BaseResult.fail(ExcelLangCodeConstant.IMPORT_CONTENT_NULL,new String[]{}); |
| | | } |
| | | //将枚举和枚举项处理成一对多关系的对象 |
| | | Map<String, OsEnumPO> groupPOMap = new HashMap<>(); |
| | | for (OsEnumPO po : poList) { |
| | | //拼接用来作为分组的key |
| | | String key = po.getId() + "_" + po.getName() + "_" + po.getEnumValueDataType() + "_" + po.getLength(); |
| | | OsEnumPO group = groupPOMap.get(key); |
| | | if (group == null) { |
| | | group = new OsEnumPO(); |
| | | //拿出现的首行做后续的提示行 |
| | | group.setRowIndex(po.getRowIndex()); |
| | | group.setId(po.getId()); |
| | | group.setName(po.getName()); |
| | | group.setEnumValueDataType(po.getEnumValueDataType()); |
| | | group.setLength(po.getLength()); |
| | | groupPOMap.put(key, group); |
| | | } |
| | | |
| | | OsEnumItemDTO itemDTO = new OsEnumItemDTO(); |
| | | itemDTO.setName(po.getEnumItemName()); |
| | | itemDTO.setValue(po.getValue()); |
| | | itemDTO.setDescription(po.getDescription()); |
| | | |
| | | group.getItems().add(itemDTO); |
| | | } |
| | | Collection<OsEnumPO> newPOList = groupPOMap.values(); |
| | | //数据库查询是否有已存在的枚举名,方便后续做判重处理 |
| | | List<OsEnumVO> osEnumVOList = this.listEnumByIdCollection(poList.stream().map(OsEnumPO::getId).collect(Collectors.toSet())); |
| | | List<String> repeatEnumId = new ArrayList<>(); |
| | | if(Func.isNotEmpty(osEnumVOList)){ |
| | | repeatEnumId = osEnumVOList.stream().map(OsEnumVO::getId).collect(Collectors.toList()); |
| | | } |
| | | //当前excel中是否重复用的判重Map:(key:判重属性,value:行号) |
| | | Map<String, String> excelReapeat = new HashMap<>(); |
| | | //判断必填属性是否为空等等 |
| | | List<String> finalRepeatEnumId = repeatEnumId; |
| | | newPOList.stream().forEach(osEnumPO -> { |
| | | if(Func.isBlank(osEnumPO.getId())){//枚举名 |
| | | throw new VciBaseException("第【"+osEnumPO.getRowIndex()+"】行,enumnameerror"); |
| | | }else if(Func.isBlank(osEnumPO.getEnumValueDataType())){ |
| | | throw new VciBaseException("第【"+osEnumPO.getRowIndex()+"】行,typeerror"); |
| | | }else if(Func.isEmpty(osEnumPO.getLength())){ |
| | | throw new VciBaseException("第【"+osEnumPO.getRowIndex()+"】行,lengtherror"); |
| | | }else if(!osEnumPO.getId().matches("^[A-Za-z]+$")){ |
| | | throw new VciBaseException("第【"+osEnumPO.getRowIndex()+"】行数据,枚举名称只能为英文字母"); |
| | | }else if(excelReapeat.containsKey(osEnumPO.getId())){//枚举名表格中判重 |
| | | throw new VciBaseException("第【"+excelReapeat.get(osEnumPO.getId())+"】行和第【"+osEnumPO.getRowIndex()+"】行数据,枚举名重复"); |
| | | }else if (Func.isNotEmpty(finalRepeatEnumId) && finalRepeatEnumId.contains(osEnumPO.getId())){//2、判断枚举名是否与系统中重复 |
| | | throw new VciBaseException("第【"+osEnumPO.getRowIndex()+"】行,枚举名在系统中已经存在,请修改!"); |
| | | } |
| | | //先对枚举名excel中需要判重处理 |
| | | excelReapeat.put(osEnumPO.getId(),osEnumPO.getRowIndex()); |
| | | }); |
| | | //保存逻辑 |
| | | for (OsEnumPO osEnumPO : newPOList) { |
| | | OsEnumDTO osEnumDTO = new OsEnumDTO(); |
| | | //生成存储的DTO对象 |
| | | osEnumDTO.setId(osEnumPO.getId()); |
| | | osEnumDTO.setName(osEnumPO.getName()); |
| | | osEnumDTO.setEnumValueDataType(osEnumPO.getEnumValueDataType()); |
| | | osEnumDTO.setLength(osEnumPO.getLength()); |
| | | osEnumDTO.setItems(osEnumPO.getItems()); |
| | | |
| | | //调用新增枚举方法 |
| | | boolean addBoolean = this.addEnumType(osEnumDTO); |
| | | if(!addBoolean){ |
| | | throw new PLException("500",new String[]{"保存枚举名为【" + osEnumDTO.getId() + "】的数据时出现错误!"}); |
| | | } |
| | | } |
| | | }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("枚举导入成功!"); |
| | | } |
| | | |
| | | /** |
| | | * 获取导入模板的示例数据 |
| | | * @return |
| | | */ |
| | | private List<OsEnumVO> getExportEnumTempExample(){ |
| | | String testJosn = "[{\"enumValueDataType\":\"String\",\"id\":\"enuma\",\"items\":[{\"description\":\"enumitem1.1\",\"name\":\"enumitem1.1\",\"value\":\"1\"},{\"description\":\"enumitem1.2\",\"name\":\"enumitem1.2\",\"value\":\"2\"}],\"length\":2,\"name\":\"enuma\"},{\"enumValueDataType\":\"String\",\"id\":\"enumb\",\"items\":[{\"description\":\"enumitem2.1\",\"name\":\"enumitem2.1\",\"value\":\"1\"},{\"description\":\"enumitem2.2\",\"name\":\"enumitem2.2\",\"value\":\"2\"},{\"description\":\"enumitem2.3\",\"name\":\"enumitem2.3\",\"value\":\"3\"}],\"length\":6,\"name\":\"enumb\"}]"; |
| | | List<OsEnumVO> OsEnumVOs = JSON.parseArray(testJosn,OsEnumVO.class); |
| | | return OsEnumVOs; |
| | | } |
| | | |
| | | /** |
| | | * 枚举项校验 |
| | | * @param osEnumDTO |
| | | * @return 校验失败直接抛出异常,否则不会做任何返回 |
| | | */ |
| | | private void checkOsEnumItem(OsEnumDTO osEnumDTO) throws PLException { |
| | | List<OsEnumItemDTO> items = osEnumDTO.getItems(); |
| | | if (Func.isNotEmpty(items)) { |
| | | //获取正则,主要是针对Integer类型的时候 |
| | | String regular = "Integer".equals(osEnumDTO.getEnumValueDataType()) ? "^-?\\d+$" : ""; |
| | | //利用set的add返回值进行name判重 |
| | | Set<String> uniqueNames = new HashSet<>(); |
| | | for (OsEnumItemDTO item : items) { |
| | | // 判断name属性是否有重复的值 |
| | | if (!uniqueNames.add(item.getName())) { |
| | | throw new PLException("500", new String[] {"以下枚举项名称: 【" + item.getName()+ "】重复,请修改枚举项名后重试!"}); |
| | | } |
| | | //判断枚举值是否超过设定长度 |
| | | if (item.getValue().length() > osEnumDTO.getLength()) { |
| | | throw new PLException("500", new String[] {"枚举项名称为:【" + item.getName() + "】的枚举值长度,超过限定长度【" + osEnumDTO.getLength() + "】"}); |
| | | } |
| | | //枚举类型正则校验 |
| | | if(Func.isNotBlank(regular) && !item.getValue().matches(regular)){ |
| | | throw new PLException("500", new String[] { "枚举值只能为【" + |
| | | ("String".equals(osEnumDTO.getEnumValueDataType()) ? "字符串":"整型") + "】类型!"}); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 新平台枚举DTO对象转平台EnumType对象 |
| | | * @param osEnumDTO |
| | | * @return |
| | | */ |
| | | private EnumType osEnumDTO2EnumType(OsEnumDTO osEnumDTO){ |
| | | EnumType enumType = new EnumType(); |
| | | //enumType.oid = osEnumDTO.getOid(); |
| | | //枚举名统一转成小写,避免后期出现其他问题 |
| | | enumType.name = osEnumDTO.getId().toLowerCase(Locale.ROOT); |
| | | enumType.label = osEnumDTO.getName(); |
| | | enumType.length = osEnumDTO.getLength(); |
| | | enumType.creator = osEnumDTO.getCreator(); |
| | | enumType.ts = Func.format((Func.isNotEmpty(osEnumDTO.getTs()) ? osEnumDTO.getTs():new Date()),VciDateUtil.DateTimeMillFormat); |
| | | enumType.createTime = Func.isNotEmpty(osEnumDTO.getCreateTime()) ? osEnumDTO.getCreateTime().getTime():System.currentTimeMillis(); |
| | | enumType.modifier = osEnumDTO.getLastModifier(); |
| | | enumType.modifyTime = System.currentTimeMillis(); |
| | | enumType.type = osEnumDTO.getEnumValueDataType(); |
| | | //处理枚举项 |
| | | List<EnumItem> enumItems = new ArrayList<>(); |
| | | List<OsEnumItemDTO> items = osEnumDTO.getItems(); |
| | | if(Func.isNotEmpty(items)){ |
| | | items.stream().forEach(item->{ |
| | | EnumItem enumItem = new EnumItem(); |
| | | enumItem.name = item.getName(); |
| | | enumItem.value = item.getValue(); |
| | | enumItem.description = item.getDescription(); |
| | | enumItems.add(enumItem); |
| | | }); |
| | | } |
| | | enumType.items = enumItems.toArray(new EnumItem[enumItems.size()]); |
| | | return enumType; |
| | | } |
| | | |
| | | /** |
| | | * 查询所有的枚举映射 |
| | | * |
| | | * @return key是枚举的英文名称 |
| | |
| | | /** |
| | | * 枚举的数据对象转换为显示对象 |
| | | * |
| | | * @param enumItem 数据对象 |
| | | * @param enumType 数据对象 |
| | | * @return 显示对象 |
| | | */ |
| | | @Override |
| | | public OsEnumVO enumDO2VO(EnumType enumItem) { |
| | | public OsEnumVO enumDO2VO(EnumType enumType) { |
| | | OsEnumVO enumVO = new OsEnumVO(); |
| | | if(enumItem!=null){ |
| | | enumVO.setOid(enumItem.oid); |
| | | enumVO.setCreator(enumItem.creator); |
| | | enumVO.setLastModifier(enumItem.modifier); |
| | | enumVO.setId(enumItem.name); |
| | | enumVO.setName(enumItem.label); |
| | | enumVO.setEnumValueDataType(enumItem.type); |
| | | enumVO.setLength((int) enumItem.length); |
| | | if(enumType!=null){ |
| | | enumVO.setOid(enumType.oid); |
| | | enumVO.setCreator(enumType.creator); |
| | | enumVO.setLastModifier(enumType.modifier); |
| | | enumVO.setId(enumType.name); |
| | | enumVO.setName(enumType.label); |
| | | enumVO.setEnumValueDataType(enumType.type); |
| | | enumVO.setEnumValueDataTypeText("String".equalsIgnoreCase(enumType.type) ? "字符串":"整型"); |
| | | enumVO.setLength((int) enumType.length); |
| | | |
| | | try { |
| | | enumVO.setLastModifyTime(new Date(enumItem.createTime)); |
| | | enumVO.setLastModifyTime(new Date(enumType.createTime)); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | try { |
| | | enumVO.setCreateTime(new Date(enumItem.createTime)); |
| | | enumVO.setCreateTime(new Date(enumType.createTime)); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | try { |
| | | enumVO.setTs(VciDateUtil.str2Date(enumItem.ts,VciDateUtil.DateTimeMillFormat)); |
| | | enumVO.setTs(VciDateUtil.str2Date(enumType.ts,VciDateUtil.DateTimeMillFormat)); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | //枚举项处理 |
| | | List<OsEnumItemVO> itemVOS = new ArrayList<>(); |
| | | Map<String,String> itemVOMap = new HashMap<>(); |
| | | if(enumItem.items!=null && enumItem.items.length > 0){ |
| | | for(int i = 0 ; i < enumItem.items.length ; i ++){ |
| | | if(enumType.items!=null && enumType.items.length > 0){ |
| | | for(int i = 0 ; i < enumType.items.length ; i ++){ |
| | | OsEnumItemVO enumItemVO = new OsEnumItemVO(); |
| | | EnumItem enumChild = enumItem.items[i]; |
| | | enumItemVO.setId(enumChild.value); |
| | | EnumItem enumChild = enumType.items[i]; |
| | | enumItemVO.setValue(enumChild.value); |
| | | enumItemVO.setName(enumChild.name); |
| | | enumItemVO.setDescription(enumChild.description); |
| | | itemVOS.add(enumItemVO); |
| | |
| | | if(!CollectionUtils.isEmpty(enumItemVO)){ |
| | | enumItemVO.forEach(itemVO->{ |
| | | KeyValue keyValue = new KeyValue(); |
| | | keyValue.setKey(itemVO.getId()); |
| | | keyValue.setKey(itemVO.getValue()); |
| | | keyValue.setValue(itemVO.getName()); |
| | | keyValueList.add(keyValue); |
| | | }); |
| | |
| | | } |
| | | |
| | | /** |
| | | * 参照枚举的信息 |
| | | * 参照枚举的信息 不建议使用这个方法,因为是sql拼接做的查询 |
| | | * |
| | | * @param conditionMap 查询条件 |
| | | * @param pageHelper 分页 |
| | |
| | | */ |
| | | @Override |
| | | public DataGrid<OsEnumVO> referDataGrid(Map<String, String> conditionMap, PageHelper pageHelper) { |
| | | DataGrid<OsEnumVO> dataGrid = queryObjectServiceInfoBySql(conditionMap, pageHelper, "plenum",OsEnumVO.class,null); |
| | | DataGrid<OsEnumVO> dataGrid = queryObjectServiceInfoBySql(conditionMap, pageHelper, "plenumtype",OsEnumVO.class,null); |
| | | return dataGrid; |
| | | } |
| | | |
| | |
| | | return dataGrid; |
| | | } |
| | | |
| | | /** |
| | | * 根据枚举类型查询枚举:枚举名 , 枚举 |
| | | * @param enumType:String, Integer |
| | | * @return |
| | | */ |
| | | public List<Map<String,List<String>>> getEnumMapByType(String enumType){ |
| | | List<Map<String,List<String>>> enumMapList = new ArrayList<>(); |
| | | EnumType[] emArray = null; |
| | | try { |
| | | emArray = platformClientUtil.getEnumService().getEnumTypesByType(enumType); |
| | | } catch (PLException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | if(emArray == null || emArray.length == 0){ |
| | | return null; |
| | | } |
| | | EnumType emItem; |
| | | for(int i = 0; i < emArray.length; i++){ |
| | | emItem = emArray[i]; |
| | | EnumItem[] emChildren = emItem.items; |
| | | ArrayList<String> valueList = new ArrayList<String>(); |
| | | for(int k = 0; k < emChildren.length; k++){ |
| | | EnumItem emChild = emChildren[k]; |
| | | String value = emChild.value; |
| | | String symbol = ""; |
| | | //当为Integer时需要将枚举项值拼接上= |
| | | if("Integer".equals(enumType)){ |
| | | symbol = "="; |
| | | } |
| | | valueList.add(symbol + value); |
| | | } |
| | | Map<String, List<String>> enumMap = new HashMap<>(); |
| | | enumMap.put(emItem.name, valueList); |
| | | enumMapList.add(enumMap); |
| | | } |
| | | return enumMapList; |
| | | } |
| | | |
| | | /** |
| | | * 清除缓存 |
| | |
| | | public void clearCache() { |
| | | |
| | | } |
| | | |
| | | } |