diff --git a/adc-da-slrs/src/main/java/com/adc/da/slrs/ImportExcelDatas/POIUtil/POIUtil.java b/adc-da-slrs/src/main/java/com/adc/da/slrs/ImportExcelDatas/POIUtil/POIUtil.java new file mode 100644 index 00000000..922a3c88 --- /dev/null +++ b/adc-da-slrs/src/main/java/com/adc/da/slrs/ImportExcelDatas/POIUtil/POIUtil.java @@ -0,0 +1,81 @@ +package com.adc.da.slrs.ImportExcelDatas.POIUtil; + + +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.CellType; +import org.apache.poi.ss.usermodel.DateUtil; +import org.springframework.stereotype.Component; + +import java.math.BigDecimal; +import java.text.SimpleDateFormat; +import java.util.Date; + +@Component +public class POIUtil { + + + //获取单元格各类型值,返回字符串类型 + public static String getCellValueByCell(Cell cell) { + //判断是否为null或空串 + if (cell==null || cell.toString().trim().equals("")) { + return ""; + } + String cellValue = ""; + CellType cellType = cell.getCellType(); + cell.getCellType(); + switch (cellType) { + case NUMERIC: // 数字 + short format = cell.getCellStyle().getDataFormat(); + if (DateUtil.isCellDateFormatted(cell)) { + SimpleDateFormat sdf = null; + //System.out.println("cell.getCellStyle().getDataFormat()="+cell.getCellStyle().getDataFormat()); + if (format == 20 || format == 32) { + sdf = new SimpleDateFormat("HH:mm"); + } else if (format == 14 || format == 31 || format == 57 || format == 58) { + // 处理自定义日期格式:m月d日(通过判断单元格的格式id解决,id的值是58) + sdf = new SimpleDateFormat("yyyy-MM-dd"); + double value = cell.getNumericCellValue(); + Date date = org.apache.poi.ss.usermodel.DateUtil + .getJavaDate(value); + cellValue = sdf.format(date); + }else {// 日期 + sdf = new SimpleDateFormat("yyyy-MM-dd"); + } + try { + cellValue = sdf.format(cell.getDateCellValue());// 日期 + } catch (Exception e) { + try { + throw new Exception("exception on get date data !".concat(e.toString())); + } catch (Exception e1) { + e1.printStackTrace(); + } + }finally{ + sdf = null; + } + } else { + BigDecimal bd = new BigDecimal(cell.getNumericCellValue()); + cellValue = bd.toPlainString();// 数值 这种用BigDecimal包装再获取plainString,可以防止获取到科学计数值 + } + break; + case STRING: // 字符串 + cellValue = cell.getStringCellValue(); + break; + case BOOLEAN: // Boolean + cellValue = cell.getBooleanCellValue()+"";; + break; + case FORMULA: // 公式 + cellValue = cell.getCellFormula(); + break; + case BLANK: // 空值 + cellValue = ""; + break; + case ERROR: // 故障 + cellValue = "ERROR VALUE"; + break; + default: + cellValue = "UNKNOW VALUE"; + break; + } + return cellValue; + } +} diff --git a/adc-da-slrs/src/main/java/com/adc/da/slrs/ImportExcelDatas/comment/PathMatcher.java b/adc-da-slrs/src/main/java/com/adc/da/slrs/ImportExcelDatas/comment/PathMatcher.java index caeecca1..2a34d582 100644 --- a/adc-da-slrs/src/main/java/com/adc/da/slrs/ImportExcelDatas/comment/PathMatcher.java +++ b/adc-da-slrs/src/main/java/com/adc/da/slrs/ImportExcelDatas/comment/PathMatcher.java @@ -1,10 +1,11 @@ package com.adc.da.slrs.ImportExcelDatas.comment; + +import com.alibaba.fastjson.JSON; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.Row; -import org.apache.poi.xssf.usermodel.XSSFSheet; -import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.apache.poi.xssf.usermodel.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.multipart.MultipartFile; @@ -18,46 +19,64 @@ import java.util.HashMap; public class PathMatcher { private static final Logger logger = LoggerFactory.getLogger(PathMatcher.class); - public void run(MultipartFile target,MultipartFile attach) throws IOException { + + public void run(MultipartFile target, MultipartFile attach) throws IOException { InputStream inputStream = target.getInputStream(); XSSFWorkbook workbook = new XSSFWorkbook(inputStream); - XSSFSheet sheet = workbook.getSheetAt(0); + XSSFSheet mySheet = workbook.getSheetAt(0); HashMap fileMap = getFileMap(attach); - for (Row row : sheet) { - if(row.getCell(1)==null || row.getCell(10)==null){ + for (Row row : mySheet) { + if (row.getCell(1) == null || row.getCell(10) == null) { logger.info("+"); continue; } String latter = row.getCell(1).getStringCellValue().trim();//标准号 - String fileId=""; - fileId = row.getCell(10).getStringCellValue().trim();//文档id + String[] fileIdArray = row.getCell(10).getStringCellValue().trim().split(",");//文档id + + HashMap idMapPath = new HashMap<>(); //文件id和路径的键值对 - StringBuilder builder = new StringBuilder(); - builder.append(fileId) - .append(latter); - if (fileMap.containsKey(builder.toString())){ - Cell cell = row.createCell(9); - cell.setCellType(CellType.STRING); - cell.setCellValue(fileMap.get(builder.toString())); - logger.info("*"); - }else { - logger.info("-"); + + for (String fileId : fileIdArray) { //遍历文件id + + StringBuilder builder = new StringBuilder(); + builder.append(fileId) + .append(latter);//拼接文件id和标准号 + + + if (!fileMap.containsKey(builder.toString())) { + logger.info("-"); + continue; + }else { + Cell cell = row.createCell(9); + + + idMapPath.put(fileId,fileMap.get(builder.toString())); + String json = JSON.toJSONString(idMapPath); + + cell.setCellType(CellType.STRING); + cell.setCellValue(json); + logger.info("*"); + } } + + + + } - File file = new File("C:\\Users\\22501\\Desktop\\foton标准数据\\匹配带入文件路径-企标.xlsx"); + File file = new File("C:\\Users\\22501\\Desktop\\foton标准数据\\匹配带入文件路径-国内-多个路径.xlsx"); FileOutputStream os = new FileOutputStream(file); workbook.write(os); @@ -65,8 +84,7 @@ public class PathMatcher { } - - private HashMap getFileMap(MultipartFile excel) throws IOException { + private HashMap getFileMap(MultipartFile excel) throws IOException { InputStream inputStream = excel.getInputStream(); @@ -88,7 +106,7 @@ public class PathMatcher { String path = strArray[3]; - resultMap.put(builder.toString(),path); + resultMap.put(builder.toString(), path); } diff --git a/adc-da-slrs/src/main/java/com/adc/da/slrs/ImportExcelDatas/comment/StandMatcher.java b/adc-da-slrs/src/main/java/com/adc/da/slrs/ImportExcelDatas/comment/StandMatcher.java index bd613908..b6be69cd 100644 --- a/adc-da-slrs/src/main/java/com/adc/da/slrs/ImportExcelDatas/comment/StandMatcher.java +++ b/adc-da-slrs/src/main/java/com/adc/da/slrs/ImportExcelDatas/comment/StandMatcher.java @@ -38,16 +38,23 @@ public class StandMatcher { for (Row row : sheet) { + /** + * 企标 + */ +// String letter =convertCellValueToString(row.getCell(1));//标准号 +// String publish = convertCellValueToString(row.getCell(4));//发布日期 +// String impl= convertCellValueToString(row.getCell(5));//实施日期 +// String state = convertCellValueToString(row.getCell(3));//状态 +// String replace= convertCellValueToString(row.getCell(6));//代替标准号 + + /** + * 国内 + */ String letter =convertCellValueToString(row.getCell(1));//标准号 - - String publish = convertCellValueToString(row.getCell(4));//发布日期 - - String impl= convertCellValueToString(row.getCell(5));//实施日期 - - String state = convertCellValueToString(row.getCell(3));//状态 - - String replace= convertCellValueToString(row.getCell(6));//代替标准号 - + String publish = convertCellValueToString(row.getCell(3));//发布日期 + String impl= convertCellValueToString(row.getCell(4));//实施日期 + String state = convertCellValueToString(row.getCell(2));//状态 + String replace= convertCellValueToString(row.getCell(5));//代替标准号 @@ -72,7 +79,7 @@ public class StandMatcher { } - File result = new File("C:\\Users\\22501\\Desktop\\foton标准数据\\匹配带入文件id-企标.xlsx"); + File result = new File("C:\\Users\\22501\\Desktop\\foton标准数据\\匹配带入文件id-国内-多个文件id.xlsx"); FileOutputStream os = new FileOutputStream(result); workbook.write(os); } @@ -115,7 +122,15 @@ public class StandMatcher { if (row.getCell(7).getCellType()== NUMERIC){ String s = row.getCell(7).toString(); String fileId =s.substring(0,s.length()-2);//文件id - resultSet.put(strBuilder.toString(),fileId); + String standStr = strBuilder.toString(); + if(resultSet.containsKey(standStr)){ + String s1 = resultSet.get(standStr); + + resultSet.replace(standStr,s1+","+fileId); + }else { + resultSet.put(standStr,fileId); + + } } diff --git a/adc-da-slrs/src/main/java/com/adc/da/slrs/ImportExcelDatas/comment/Standard.java b/adc-da-slrs/src/main/java/com/adc/da/slrs/ImportExcelDatas/comment/Standard.java new file mode 100644 index 00000000..5403054b --- /dev/null +++ b/adc-da-slrs/src/main/java/com/adc/da/slrs/ImportExcelDatas/comment/Standard.java @@ -0,0 +1,169 @@ +package com.adc.da.slrs.ImportExcelDatas.comment; + +import com.adc.da.att.service.IAttFileEOService; +import com.adc.da.att.vo.AttFileVo; +import com.adc.da.slrs.ImportExcelDatas.POIUtil.POIUtil; +import com.adc.da.slrs.sarStandardsInfo.entity.SarStandardsInfo; +import com.adc.da.utils.util.InitStandAttrUtil; +import com.alibaba.fastjson.JSON; +import com.google.gson.Gson; +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.Row; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.io.File; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.TreeMap; + +@Component +public class Standard { + + + @Autowired + private IAttFileEOService attFileEOService; + + + + private static final Logger logger = LoggerFactory.getLogger(Standard.class); + Map standAttrMap = new TreeMap<>(); + private String standType=""; + + + + Standard(){ + /** + * 初始化国内外标准属性字段 + */ + List listStandField = InitStandAttrUtil.queryFieldList; + listStandField.forEach(item ->{ + this.standAttrMap.put(item,""); + }); + } + + + + + public void setStandType(String standType) { + + if (!"INLAND,FOREIGN".contains(standType)){ + logger.error("标准类别错误"); + }else { + this.standType = standType; + } + } + + + + + + public SarStandardsInfo getStandardsInfo(Row row){ + + + SarStandardsInfo sarStandardsInfo = new SarStandardsInfo(); + + + if ("".equals(standType)){ + logger.error("未设置标准类型"); + }else { + sarStandardsInfo.setStandType(standType); + } + + + sarStandardsInfo.setValidFlag("0"); + Map fieldMap = standAttrMap;//标准字段初化 + for (int i=0;i<10;i++){ + Cell cell = row.getCell(i); + String value = POIUtil.getCellValueByCell(cell); + switch (i){ + case 0://标准名称 + sarStandardsInfo.setStandName(value); + break; + case 2://标准状态 + //TODO 标准状态转换为代码 + switch (value){ + case "有效": + value="vsaga7nwub"; + break; + case "作废": + value="chblaarg77"; + break; + case "参考": + value="-"; + break; + case "0": + value="-"; + break; + default:value="-"; + } + sarStandardsInfo.setTextStatus(value); + break; + case 3://发布日期 + //日期格式转换 1970/1/1 时发布日期为空 + if ("1970/1/1".equals(value.trim())){ + value=""; + } + sarStandardsInfo.setIssueTime(value); + break; + case 4://实施日期 + fieldMap.put("SSRQ", value); + break; + case 5://代替标准号 + fieldMap.put("DTBJH", value); + break; + case 6://标准类别 + sarStandardsInfo.setStandSort(value); + break; + case 7://标准号 + sarStandardsInfo.setStandNumber(value); + break; + case 8://标准年份 + sarStandardsInfo.setStandYear(value); + break; + case 9://附件路径 ->上传 + HashMap pathMap = JSON.parseObject(value, HashMap.class); + if (pathMap!=null){ + Cell standCodeCell = row.getCell(1); + String standCode = POIUtil.getCellValueByCell(standCodeCell); + + StringBuilder fileNameBuilder = new StringBuilder(); + fileNameBuilder.append(standCode) //标准号 + .append(sarStandardsInfo.getStandName());//标准名称 + AttFileVo fileInfo = importFile(pathMap,fileNameBuilder.toString());//上传文件 + fieldMap.put("FBGBJBD", fileInfo.getAttId()); + } + break; + } + } + + Gson gson = new Gson(); + String attrInfoJson = gson.toJson(fieldMap); + sarStandardsInfo.setSarStandAttrEOStr(attrInfoJson);//新增修改时属性表信息 + + return sarStandardsInfo; + } + + + private AttFileVo importFile(HashMap pathMap,String customName){ + String realPath = ""; + + for(String path: pathMap.values()) { + String filePath[] = path.split("/"); + for (int i = 3; i < filePath.length; i++) { + realPath = realPath + "/" + filePath[i]; + + } + break; + } + + File file = new File("/data/from12/Attach_swf_bak" + realPath); + + + return attFileEOService.insertFileInfo(file,customName); + } + +} diff --git a/adc-da-slrs/src/main/java/com/adc/da/slrs/ImportExcelDatas/controller/ImportExcelController.java b/adc-da-slrs/src/main/java/com/adc/da/slrs/ImportExcelDatas/controller/ImportExcelController.java index 016a6797..e37d449e 100644 --- a/adc-da-slrs/src/main/java/com/adc/da/slrs/ImportExcelDatas/controller/ImportExcelController.java +++ b/adc-da-slrs/src/main/java/com/adc/da/slrs/ImportExcelDatas/controller/ImportExcelController.java @@ -4,6 +4,7 @@ import com.adc.da.base.web.BaseController; import com.adc.da.common.ReadExcel; import com.adc.da.slrs.ImportExcelDatas.comment.*; import com.adc.da.slrs.ImportExcelDatas.entity.ImportDto; +import com.adc.da.slrs.ImportExcelDatas.service.IImportStandExcelService; import com.adc.da.slrs.ImportExcelDatas.service.ImportExcelService; import com.adc.da.util.exception.AdcDaBaseException; import com.adc.da.util.utils.IOUtils; @@ -155,7 +156,7 @@ public class ImportExcelController extends BaseController { - @ApiOperation("匹配文件id") + @ApiOperation("匹配文件id(多个id)") @PostMapping("/matching") public void matching(MultipartFile targetExcel,MultipartFile attachExcel) throws IOException { StandMatcher standMatcher = new StandMatcher(); @@ -171,4 +172,15 @@ public class ImportExcelController extends BaseController { pathMatcher.run(targetExcel,attachExcel); } + + @Autowired + private IImportStandExcelService iImportStandExcelService; + + @ApiOperation("根据分解后的excel表 导入标准数据和附件") + @PostMapping("importStandFormExcel") + public void importStandFormExcel(MultipartFile targetExcel,String standType) throws IOException { + iImportStandExcelService.importStandExcel(targetExcel,standType); + } + + } diff --git a/adc-da-slrs/src/main/java/com/adc/da/slrs/ImportExcelDatas/service/ImportExcelService.java b/adc-da-slrs/src/main/java/com/adc/da/slrs/ImportExcelDatas/service/ImportExcelService.java index 9513517b..36f41c34 100644 --- a/adc-da-slrs/src/main/java/com/adc/da/slrs/ImportExcelDatas/service/ImportExcelService.java +++ b/adc-da-slrs/src/main/java/com/adc/da/slrs/ImportExcelDatas/service/ImportExcelService.java @@ -18,4 +18,9 @@ public interface ImportExcelService extends IService { public String isExist(MultipartFile exclFile); + + + + + } diff --git a/adc-da-slrs/src/main/java/com/adc/da/slrs/ImportExcelDatas/service/impl/ImportExcelServiceImpl.java b/adc-da-slrs/src/main/java/com/adc/da/slrs/ImportExcelDatas/service/impl/ImportExcelServiceImpl.java index 6ff028c3..0ca0dff9 100644 --- a/adc-da-slrs/src/main/java/com/adc/da/slrs/ImportExcelDatas/service/impl/ImportExcelServiceImpl.java +++ b/adc-da-slrs/src/main/java/com/adc/da/slrs/ImportExcelDatas/service/impl/ImportExcelServiceImpl.java @@ -324,11 +324,14 @@ public class ImportExcelServiceImpl extends ServiceImpl isExist = dicTypeEOService.getTypeIdByDicIdAndTypeName("JKSADFH564S", null, null, null); isExist.forEach(item->{ sarSort.add(item.getDicTypeCode()); - }); + });//标准类别集合 error = importListMap.get("error"); + /** + * 计数 + */ AtomicInteger QYBZcount= new AtomicInteger(); AtomicInteger QYBZcountUpdate= new AtomicInteger(); AtomicInteger HWBZcount= new AtomicInteger(); @@ -350,7 +353,7 @@ public class ImportExcelServiceImpl extends ServiceImpl { + importListMap.get("HWBZ").forEach(item -> { if (true ) { String standID = UUIDUtils.randomUUID20(); @@ -409,14 +412,14 @@ public class ImportExcelServiceImpl extends ServiceImpl { + importListMap.get("GNBZ").forEach(item -> { if (true) { @@ -472,7 +475,7 @@ public class ImportExcelServiceImpl extends ServiceImpl error = new ArrayList<>(); + + private List fileError = new ArrayList<>(); + + + @Async + @Override + public String importStandExcel(MultipartFile file,String standType) throws IOException { + + AtomicInteger countUpdate= new AtomicInteger();//更新 数量 + AtomicInteger countInsert= new AtomicInteger();//新增 数量 + + InputStream inputStream = file.getInputStream(); + XSSFWorkbook workbook = new XSSFWorkbook(inputStream); + XSSFSheet sheet = workbook.getSheetAt(0); + + standard.setStandType(standType); + + int lastRowNum = sheet.getLastRowNum(); + + for (int i = 1; i < lastRowNum; i++) { + XSSFRow row = sheet.getRow(i); + SarStandardsInfo standardsInfo = standard.getStandardsInfo(row); + + QueryWrapper standSaveWrapper = new QueryWrapper<>(); + standSaveWrapper + .eq("STAND_SORT", standardsInfo.getStandSort()) + .eq("STAND_NUMBER", standardsInfo.getStandNumber()) + .eq("STAND_YEAR", standardsInfo.getStandYear()); + + + SarStandardsInfo one = sarStandardsInfoService.getOne(standSaveWrapper); + if (one != null) { + + + standardsInfo.setId(one.getId()); + try { + sarStandardsInfoService.updateSarStandardsInfo(standardsInfo); + countUpdate.getAndIncrement(); + logger.info("执行更新"+countUpdate+"条数据"); + + } catch (Exception e) { + e.printStackTrace(); + } + + + } else { + try { + sarStandardsInfoService.createSarStandardsInfo(standardsInfo); + countInsert.getAndIncrement(); + logger.info("执行新增"+countInsert+"条数据"); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + return "success"; + + } + + + + + +} diff --git a/adc-da-sys/src/main/java/com/adc/da/att/service/IAttFileEOService.java b/adc-da-sys/src/main/java/com/adc/da/att/service/IAttFileEOService.java index 952fdc7f..79db58b3 100644 --- a/adc-da-sys/src/main/java/com/adc/da/att/service/IAttFileEOService.java +++ b/adc-da-sys/src/main/java/com/adc/da/att/service/IAttFileEOService.java @@ -22,4 +22,6 @@ public interface IAttFileEOService extends IService { public String saveFileAttId(File file); + public AttFileVo insertFileInfo(File file,String fileName); + } diff --git a/adc-da-sys/src/main/java/com/adc/da/att/service/impl/AttFileEOServiceImpl.java b/adc-da-sys/src/main/java/com/adc/da/att/service/impl/AttFileEOServiceImpl.java index e81fdfff..554490a5 100644 --- a/adc-da-sys/src/main/java/com/adc/da/att/service/impl/AttFileEOServiceImpl.java +++ b/adc-da-sys/src/main/java/com/adc/da/att/service/impl/AttFileEOServiceImpl.java @@ -358,4 +358,74 @@ public class AttFileEOServiceImpl extends ServiceImpl i } + + /** + * 保存文件并返回文件ID + * + * @param file + */ + @Transactional(rollbackFor = Exception.class) + public AttFileVo insertFileInfo(File file,String customFileName) { + /** + * 1、首先生成文件保存的主键ID + * 2、根据文件ID生成随机路径 + * 3、生成文件名 + * 3、保存文件 + * 4、获取文件相关信息 + * 5、判断当前表是否有存在,如果存在则执行insert语句 如果不存在则创建表结构 + * 5、保存入库并返回主键ID + */ + AttFileVo attFileVo = new AttFileVo(); + String fileId = UUIDUtils.randomUUID20(); + try { + String uuidPath = UUIDUtils.getUUIDPath(fileId); + + String FileSavePath = filePath + uuidPath + "/"; + File dir = new File(FileSavePath); + if (!dir.exists()) { + dir.mkdirs(); + } + + String fileName = file.getName(); + String fileSuffix = fileName.substring(fileName.lastIndexOf(".")+1, fileName.length()); + + String newFileName = fileId +"."+ fileSuffix; + String oldName=customFileName+"."+fileSuffix; + + + + File saveFile = new File(FileSavePath + newFileName); + FileUtils.copyFile(file,saveFile); + //开始存储文件信息 + String tableName = UUIDUtils.getAttTable(); + int existTable = this.baseMapper.existTable(tableName); + if (existTable == 0) { + this.baseMapper.creatTableInfo(tableName); + } + fileId = tableName + "_" + fileId; + AttFileEO attFileEO = new AttFileEO(); + attFileEO.setTableName(tableName); + attFileEO.setId(fileId); + attFileEO.setOldFileName(customFileName); + attFileEO.setFileSuffix(fileSuffix); + attFileEO.setFilePath(uuidPath); + attFileEO.setFileName(oldName); + attFileEO.setValidFlag(ValidFlagEnum.VALID_TRUE.getValue()); + attFileEO.setCreationTime(new Date()); + attFileEO.setModifyTime(new Date()); + this.baseMapper.insertData(attFileEO); + attFileVo.setId(fileId); + attFileVo.setFileName(newFileName); + attFileVo.setFilePath(uuidPath); + attFileVo.setFileSuffix(fileSuffix); + attFileVo.setOldFileName(oldName); + } catch (IOException e) { + log.error(e.getMessage(),e); + } catch (Exception e) { + log.error(e.getMessage(),e); + } + return attFileVo; + } + + }