From 53df796c80d42564321c206bf14ef1eaf8cfdb27 Mon Sep 17 00:00:00 2001 From: huangfeng Date: Wed, 31 Jan 2024 16:59:08 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0excel=E5=AF=BC?= =?UTF-8?q?=E5=85=A5CAC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cac/controller/NiecSensorController.java | 17 +++- .../com/xydl/cac/excel/CacExcelListener.java | 69 ++++++++++++++++ src/main/java/com/xydl/cac/excel/CacLine.java | 80 +++++++++++++++++++ .../com/xydl/cac/service/ReportService.java | 3 + .../cac/service/impl/ReportServiceImpl.java | 12 +++ 5 files changed, 177 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/xydl/cac/excel/CacExcelListener.java create mode 100644 src/main/java/com/xydl/cac/excel/CacLine.java diff --git a/src/main/java/com/xydl/cac/controller/NiecSensorController.java b/src/main/java/com/xydl/cac/controller/NiecSensorController.java index 5885212..ff710d3 100644 --- a/src/main/java/com/xydl/cac/controller/NiecSensorController.java +++ b/src/main/java/com/xydl/cac/controller/NiecSensorController.java @@ -12,10 +12,8 @@ import io.swagger.annotations.ApiParam; import lombok.extern.slf4j.Slf4j; import org.springframework.data.domain.Page; import org.springframework.validation.annotation.Validated; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import javax.servlet.http.HttpServletResponse; @@ -84,4 +82,15 @@ public class NiecSensorController extends BasicController { reportService.exportSensor(detail, response.getOutputStream()); } + @PostMapping("importCac") + @ApiOperation("导入CAC") + public Response importCac(@RequestParam("file") MultipartFile file) throws Exception { + if (file != null && file.getInputStream() != null) { + reportService.importCac(file.getInputStream()); + return Response.success("OK"); + } else { + return Response.fail("缺少上传文件"); + } + } + } diff --git a/src/main/java/com/xydl/cac/excel/CacExcelListener.java b/src/main/java/com/xydl/cac/excel/CacExcelListener.java new file mode 100644 index 0000000..498f6c9 --- /dev/null +++ b/src/main/java/com/xydl/cac/excel/CacExcelListener.java @@ -0,0 +1,69 @@ +package com.xydl.cac.excel; + +import com.alibaba.excel.context.AnalysisContext; +import com.alibaba.excel.event.AnalysisEventListener; +import com.xydl.cac.entity.NiecSensor; +import org.apache.commons.lang3.StringUtils; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; + +public class CacExcelListener extends AnalysisEventListener { + + List list = new ArrayList<>(); + int base = 16385; + int type; + LinkedHashMap sensorList = new LinkedHashMap<>(); + + @Override + public void invoke(CacLine line, AnalysisContext analysisContext) { + int row = analysisContext.readRowHolder().getRowIndex(); + if (row > 1) { + if (line.getPointId().contains("遥测")) { + type = 0; + } else if (line.getPointId().contains("遥信")) { + type = 1; + } else { + processDate(line, row + 1); + } + } + + } + + private void processDate(CacLine line, int row) { + if (StringUtils.isNotBlank(line.getSensorCode())) { + NiecSensor sensor = sensorList.get(line.getSensorCode()); + if (sensor == null) { + sensor = new NiecSensor(); + sensor.setSensorCode(line.getSensorCode()); + Integer pointId = Integer.parseInt(line.getPointId()) + base; + if (StringUtils.isBlank(line.getEquipmentCode())) { + throw new RuntimeException("第" + row + "行的被监测设备的唯一标识不能为空"); + } + sensor.setEquipmentId(line.getEquipmentCode()); + if (StringUtils.isBlank(line.getTableName())) { + throw new RuntimeException("第" + row + "行的表名不能为空"); + } + sensor.setTableName(line.getTableName()); + if (StringUtils.isNotBlank(line.getParentName()) && StringUtils.isNotBlank(line.getShortName())) { + sensor.setName(line.getParentName() + line.getShortName()); + } else { + sensor.setName(line.getLongName()); + } + sensorList.put(line.getSensorCode(), sensor); + } + + + if (StringUtils.isBlank(line.getField())) { + throw new RuntimeException("第" + row + "行的字段名不能为空"); + } + list.add(line); + } + } + + @Override + public void doAfterAllAnalysed(AnalysisContext analysisContext) { + + } +} diff --git a/src/main/java/com/xydl/cac/excel/CacLine.java b/src/main/java/com/xydl/cac/excel/CacLine.java new file mode 100644 index 0000000..0280e88 --- /dev/null +++ b/src/main/java/com/xydl/cac/excel/CacLine.java @@ -0,0 +1,80 @@ +package com.xydl.cac.excel; + +import com.alibaba.excel.annotation.ExcelProperty; +import lombok.Data; + +@Data +public class CacLine { + + @ExcelProperty("pointId") + private String pointId; + + @ExcelProperty("longName") + private String longName; + + @ExcelProperty("siteName") + private String siteName; + + @ExcelProperty("fullname") + private String fullname; + + @ExcelProperty("e") + private String e; + + @ExcelProperty("f") + private String f; + + @ExcelProperty("g") + private String g; + + @ExcelProperty("h") + private String h; + + @ExcelProperty("i") + private String i; + + @ExcelProperty("paramindex") + private String paramindex; + + @ExcelProperty("k") + private String k; + + @ExcelProperty("devid") + private String devid; + + @ExcelProperty("grpNo") + private String grpNo; + + @ExcelProperty("itemNo") + private String itemNo; + + @ExcelProperty("o") + private String o; + + @ExcelProperty("p") + private String p; + + @ExcelProperty("sensorCode") + private String sensorCode; + + @ExcelProperty("equipmentCode") + private String equipmentCode; + + @ExcelProperty("phase") + private String phase; + + @ExcelProperty("tableName") + private String tableName; + + @ExcelProperty("field") + private String field; + + @ExcelProperty("parentName") + private String parentName; + + @ExcelProperty("shortName") + private String shortName; + + @ExcelProperty("fieldDesc") + private String fieldDesc; +} diff --git a/src/main/java/com/xydl/cac/service/ReportService.java b/src/main/java/com/xydl/cac/service/ReportService.java index 7f5c6e4..eb3df7e 100644 --- a/src/main/java/com/xydl/cac/service/ReportService.java +++ b/src/main/java/com/xydl/cac/service/ReportService.java @@ -2,10 +2,13 @@ package com.xydl.cac.service; import com.xydl.cac.model.SensorDetail; +import java.io.InputStream; import java.io.OutputStream; import java.util.Map; public interface ReportService { void exportSensor(SensorDetail> detail, OutputStream output) throws Exception; + + void importCac(InputStream input) throws Exception; } diff --git a/src/main/java/com/xydl/cac/service/impl/ReportServiceImpl.java b/src/main/java/com/xydl/cac/service/impl/ReportServiceImpl.java index cf0f841..5500f8d 100644 --- a/src/main/java/com/xydl/cac/service/impl/ReportServiceImpl.java +++ b/src/main/java/com/xydl/cac/service/impl/ReportServiceImpl.java @@ -5,12 +5,15 @@ import com.alibaba.excel.ExcelWriter; import com.alibaba.excel.write.metadata.WriteSheet; import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy; import com.xydl.cac.entity.NiecPoint; +import com.xydl.cac.excel.CacExcelListener; +import com.xydl.cac.excel.CacLine; import com.xydl.cac.model.SensorDetail; import com.xydl.cac.service.ReportService; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; @@ -59,4 +62,13 @@ public class ReportServiceImpl implements ReportService { excelWriter.finish(); } + + @Override + public void importCac(InputStream input) throws Exception { + CacExcelListener listener = new CacExcelListener(); + EasyExcel.read(input, CacLine.class, listener) + .sheet(0) + .doRead(); + + } }