|
@@ -1,38 +1,128 @@
|
|
package com.gyee.gaia.meter.service.saveHistory;
|
|
package com.gyee.gaia.meter.service.saveHistory;
|
|
|
|
|
|
import cn.hutool.core.date.DateTime;
|
|
import cn.hutool.core.date.DateTime;
|
|
-import cn.hutool.core.date.DateUnit;
|
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
import com.gyee.gaia.meter.entity.MeterInfoBottomcode;
|
|
import com.gyee.gaia.meter.entity.MeterInfoBottomcode;
|
|
-import lombok.Data;
|
|
|
|
|
|
+import com.gyee.gaia.meter.entity.MeterPoint;
|
|
|
|
+import com.gyee.gaia.meter.service.impl.MeterInfoBottomcodeServiceImpl;
|
|
|
|
+import com.gyee.gaia.meter.service.impl.MeterPointServiceImpl;
|
|
import org.apache.poi.ss.usermodel.Row;
|
|
import org.apache.poi.ss.usermodel.Row;
|
|
import org.apache.poi.ss.usermodel.Sheet;
|
|
import org.apache.poi.ss.usermodel.Sheet;
|
|
import org.apache.poi.ss.usermodel.Workbook;
|
|
import org.apache.poi.ss.usermodel.Workbook;
|
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
|
+import org.springframework.boot.test.context.SpringBootTest;
|
|
|
|
|
|
-import java.io.File;
|
|
|
|
|
|
+import javax.annotation.Resource;
|
|
import java.io.FileInputStream;
|
|
import java.io.FileInputStream;
|
|
|
|
+import java.io.IOException;
|
|
import java.io.PrintStream;
|
|
import java.io.PrintStream;
|
|
-import java.io.UnsupportedEncodingException;
|
|
|
|
import java.math.BigDecimal;
|
|
import java.math.BigDecimal;
|
|
import java.math.RoundingMode;
|
|
import java.math.RoundingMode;
|
|
-import java.text.SimpleDateFormat;
|
|
|
|
-import java.time.LocalDateTime;
|
|
|
|
-import java.time.ZoneId;
|
|
|
|
-import java.time.format.DateTimeFormatter;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
-import java.util.Date;
|
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
|
|
|
|
+/**
|
|
|
|
+ * idea直接运行test,从底码表读取数据保存到pgsql
|
|
|
|
+ * 1,需要手动修改必要参数 例如:
|
|
|
|
+ //文件路径
|
|
|
|
+ String fileName = "C:\\Users\\24410\\Desktop\\data.xlsx";
|
|
|
|
+ //测点code
|
|
|
|
+ String code = "QSDJL.NX_GD_QSF_DD_P1_L1_001_ZXYG001";
|
|
|
|
+ //开始日期
|
|
|
|
+ String StringDate = "2023/6/1";
|
|
|
|
+ //第几个sheet表
|
|
|
|
+ int sheetValue = 1;
|
|
|
|
+ //开始行
|
|
|
|
+ int startRow = 2;
|
|
|
|
+ //结束行
|
|
|
|
+ int endRow = 29;
|
|
|
|
+ //数据列
|
|
|
|
+ int valueColumn = 3;
|
|
|
|
+ */
|
|
|
|
+@SpringBootTest
|
|
public class ReadFromXLSXTest {
|
|
public class ReadFromXLSXTest {
|
|
|
|
|
|
- public static void main(String[] args) throws UnsupportedEncodingException {
|
|
|
|
- System.setOut(new PrintStream(System.out, true, "UTF-8"));
|
|
|
|
- System.out.println("hello");
|
|
|
|
|
|
+ @Resource
|
|
|
|
+ MeterInfoBottomcodeServiceImpl meterInfoBottomcodeService;
|
|
|
|
+ @Resource
|
|
|
|
+ MeterPointServiceImpl meterPointService;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ @Test
|
|
|
|
+ public void readDataFromFile() throws IOException {
|
|
|
|
+
|
|
|
|
+ //文件路径
|
|
String fileName = "C:\\Users\\24410\\Desktop\\data.xlsx";
|
|
String fileName = "C:\\Users\\24410\\Desktop\\data.xlsx";
|
|
- List<MeterInfoBottomcode> records = readDataFromFile(fileName);
|
|
|
|
- // TODO: 将records保存到数据库中
|
|
|
|
|
|
+ //测点code
|
|
|
|
+ String code = "QSDJL.NX_GD_QSF_DD_P1_L1_001_ZXYG001";
|
|
|
|
+ //开始日期
|
|
|
|
+ String StringDate = "2023/6/1";
|
|
|
|
+ //第几个sheet表
|
|
|
|
+ int sheetValue = 1;
|
|
|
|
+ //开始行
|
|
|
|
+ int startRow = 2;
|
|
|
|
+ //结束行
|
|
|
|
+ int endRow = 29;
|
|
|
|
+ //数据列
|
|
|
|
+ int valueColumn = 3;
|
|
|
|
+
|
|
|
|
+ //根据code获取测点信息
|
|
|
|
+ MeterPoint meterPoint = meterPointService.getOne(new QueryWrapper<MeterPoint>().eq("nem_code", code));
|
|
|
|
+ //时间转为DateTime格式
|
|
|
|
+ DateTime dateTime = DateUtil.parse(StringDate);
|
|
|
|
+ //集合存所有记录
|
|
|
|
+ List<MeterInfoBottomcode> records = new ArrayList<>();
|
|
|
|
+ //文件输入流
|
|
|
|
+ FileInputStream fileInputStream = new FileInputStream(fileName);
|
|
|
|
+ //XSSFWorkbook格式的文件输入流
|
|
|
|
+ Workbook workbook = new XSSFWorkbook(fileInputStream);
|
|
|
|
+ //第几个sheet表,从0开始
|
|
|
|
+ Sheet sheet = workbook.getSheetAt(sheetValue - 1);
|
|
|
|
+ //开始天数,遍历累加以便推移时间
|
|
|
|
+ int day = 0;
|
|
|
|
+ //遍历每行
|
|
|
|
+ for (int i = startRow-1; i < endRow; i += 2) {
|
|
|
|
+ //每天时间
|
|
|
|
+ DateTime dateTime1 = DateUtil.offsetDay(dateTime, day);
|
|
|
|
+ //创建对象
|
|
|
|
+ MeterInfoBottomcode record = new MeterInfoBottomcode();
|
|
|
|
+ //对象赋值,开始时间,截止时间
|
|
|
|
+ record.setStartTime(dateTime1.toLocalDateTime());
|
|
|
|
+ record.setEndTime(DateUtil.endOfDay(dateTime1).toLocalDateTime());
|
|
|
|
+ //对象赋值,名字,编码,场站
|
|
|
|
+ record.setName(meterPoint.getName());
|
|
|
|
+ record.setCode(meterPoint.getNemCode());
|
|
|
|
+ record.setWindpowerstationId(meterPoint.getWindpowerstationId());
|
|
|
|
+ //对象赋值,止码
|
|
|
|
+ Row stopCodeRow = sheet.getRow(i);
|
|
|
|
+ double stopCode = stopCodeRow.getCell(valueColumn - 1).getNumericCellValue();
|
|
|
|
+ record.setEndValue(BigDecimal.valueOf(stopCode).setScale(4, RoundingMode.HALF_EVEN));
|
|
|
|
+ //对象赋值,起码,
|
|
|
|
+ Row electricityRow = sheet.getRow(i + 1);
|
|
|
|
+ double electricity = electricityRow.getCell(valueColumn - 1).getNumericCellValue();
|
|
|
|
+ record.setDayValue(BigDecimal.valueOf(electricity).setScale(4, RoundingMode.HALF_EVEN));
|
|
|
|
+ //对象赋值,起码,第一天止码为上一行,其余为上2行
|
|
|
|
+// if (i == startRow-1) {
|
|
|
|
+ //如果时间为当月第一天,起码为上一行,否则为上2行
|
|
|
|
+ if (dateTime1.equals(DateUtil.beginOfMonth(dateTime1))) {
|
|
|
|
+ Row startCodeRow = sheet.getRow(i - 1);
|
|
|
|
+ double startCode = startCodeRow.getCell(valueColumn - 1).getNumericCellValue();
|
|
|
|
+ record.setStartValue(BigDecimal.valueOf(startCode).setScale(4, RoundingMode.HALF_EVEN));
|
|
|
|
+ } else {
|
|
|
|
+ Row startCodeRow = sheet.getRow(i - 2);
|
|
|
|
+ double startCode = startCodeRow.getCell(valueColumn - 1).getNumericCellValue();
|
|
|
|
+ record.setStartValue(BigDecimal.valueOf(startCode).setScale(4, RoundingMode.HALF_EVEN));
|
|
|
|
+ }
|
|
|
|
+ //添加到集合
|
|
|
|
+ records.add(record);
|
|
|
|
+
|
|
|
|
+ day++;
|
|
|
|
+ }
|
|
|
|
+
|
|
// 打印每天的数据记录
|
|
// 打印每天的数据记录
|
|
|
|
+ System.setOut(new PrintStream(System.out, true, "UTF-8"));
|
|
for (MeterInfoBottomcode record : records) {
|
|
for (MeterInfoBottomcode record : records) {
|
|
System.out.println("name: " + record.getName());
|
|
System.out.println("name: " + record.getName());
|
|
System.out.println("startTime: " + record.getStartTime());
|
|
System.out.println("startTime: " + record.getStartTime());
|
|
@@ -43,64 +133,21 @@ public class ReadFromXLSXTest {
|
|
System.out.println("编码: " + record.getCode());
|
|
System.out.println("编码: " + record.getCode());
|
|
System.out.println("场站: " + record.getWindpowerstationId());
|
|
System.out.println("场站: " + record.getWindpowerstationId());
|
|
System.out.println();
|
|
System.out.println();
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- public static List<MeterInfoBottomcode> readDataFromFile(String fileName) {
|
|
|
|
-
|
|
|
|
- List<MeterInfoBottomcode> records = new ArrayList<>();
|
|
|
|
-
|
|
|
|
- try (FileInputStream fileInputStream = new FileInputStream(new File(fileName));
|
|
|
|
- ) {
|
|
|
|
- Workbook workbook = new XSSFWorkbook(fileInputStream);
|
|
|
|
- Sheet sheet = workbook.getSheetAt(0);
|
|
|
|
- int lastRowNum = sheet.getLastRowNum();
|
|
|
|
-
|
|
|
|
- int day = 0;
|
|
|
|
|
|
|
|
- for (int i = 1; i <= lastRowNum; i += 2) {
|
|
|
|
- day++;
|
|
|
|
|
|
+// QueryWrapper<MeterInfoBottomcode> queryWrapper = new QueryWrapper<>();
|
|
|
|
+// queryWrapper
|
|
|
|
+// .eq("start_time", record.getStartTime())
|
|
|
|
+// .eq("code", record.getCode());
|
|
|
|
+// List<MeterInfoBottomcode> list = meterInfoBottomcodeService.list(queryWrapper);
|
|
|
|
+// if (list.size() > 0) {
|
|
|
|
+// record.update(queryWrapper);
|
|
|
|
+// } else {
|
|
|
|
+// record.insert();
|
|
|
|
+// }
|
|
|
|
|
|
- MeterInfoBottomcode record = new MeterInfoBottomcode();
|
|
|
|
|
|
|
|
- Row dateRow = sheet.getRow(i);
|
|
|
|
-
|
|
|
|
- String StringDate = dateRow.getCell(0).getStringCellValue();
|
|
|
|
- DateTime dateTime = DateUtil.parse(StringDate);
|
|
|
|
-
|
|
|
|
- record.setStartTime(dateTime.toLocalDateTime());
|
|
|
|
- record.setEndTime(DateUtil.endOfDay(dateTime).toLocalDateTime());
|
|
|
|
-
|
|
|
|
- Row infoRow = sheet.getRow(0);
|
|
|
|
-
|
|
|
|
- record.setName(infoRow.getCell(0).getStringCellValue());
|
|
|
|
- record.setCode(infoRow.getCell(1).getStringCellValue());
|
|
|
|
- record.setWindpowerstationId(infoRow.getCell(3).getStringCellValue());
|
|
|
|
-
|
|
|
|
- Row stopCodeRow = sheet.getRow(i);
|
|
|
|
- double stopCode = stopCodeRow.getCell(2).getNumericCellValue();
|
|
|
|
- record.setEndValue(BigDecimal.valueOf(stopCode).setScale(4, RoundingMode.HALF_EVEN));
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- Row electricityRow = sheet.getRow(i + 1);
|
|
|
|
- double electricity = electricityRow.getCell(2).getNumericCellValue();
|
|
|
|
- record.setDayValue(BigDecimal.valueOf(electricity).setScale(4, RoundingMode.HALF_EVEN));
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- Row startCodeRow = sheet.getRow(i-1);
|
|
|
|
- double startCode = startCodeRow.getCell(2).getNumericCellValue();
|
|
|
|
- record.setStartValue(BigDecimal.valueOf(startCode).setScale(4, RoundingMode.HALF_EVEN));
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- records.add(record);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- e.printStackTrace();
|
|
|
|
}
|
|
}
|
|
- return records;
|
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
+
|