whycxzp
2025-04-30 0bc5c9794f156d678197568d2343aca0a92a38c4
src/main/java/com/whyc/service/ExcelService.java
@@ -22,8 +22,11 @@
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@Service
public class ExcelService {
@@ -54,7 +57,7 @@
        //创建一个工作簿
        XSSFWorkbook workbook = new XSSFWorkbook();
        //创建sheet1
        //createSheet1(workbook,battGroupId,testRecordCount);
        createSheet1(workbook,battGroupId,testRecordCount);
        //创建sheet2 -- 每月电压,12个月,取每月1号零点零分
        //创建sheet3 -- 每月内阻, 12个月,取每月1号零点零分
        createSheet2And3(workbook,battGroupId);
@@ -317,29 +320,65 @@
        XSSFCell cellOfSheet4 = sheet4.createRow(0).createCell(0);
        cellOfSheet4.setCellValue( battInfo.getPowerName()+ battInfo.getBattGroupName()+"温度数据");
        int columnCount = 2;
        //30天+2列其他列
        int columnCount = 30+2;
        //查询历史实时表. 存在的月份. 如果存在的话,取第一笔记录(104条)
        //首先查询当前月份对应的日期
        List<String> tableListLike = commonMapper.getTableListLike("db_power_history", "tb_batt_realdata_"+battGroupId);
        //当前月份往前推11个月
        //获取当前日期往前30天的凌晨零秒的数据. 往前推30天
        List<String> tableListInDB = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        for (int i = 0; i < 12; i++) {
            int year = calendar.get(Calendar.YEAR);
            int month = calendar.get(Calendar.MONTH) + 1;
            String monthStr;
            if (month < 10) {
                monthStr = "0" + month;
            } else {
                monthStr = String.valueOf(month);
            }
            if (tableListLike.contains("tb_batt_realdata_" + battGroupId + "_" + year + "_" + monthStr)) {
                //存在,列数+1
                columnCount++;
                tableListInDB.add("tb_batt_realdata_" + battGroupId + "_" + year + "_" + monthStr);
            }
            calendar.add(Calendar.MONTH, -1);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        String monthStr;
        if (month < 10) {
            monthStr = "0" + month;
        } else {
            monthStr = String.valueOf(month);
        }
        //当前时间往前30天
        calendar.add(Calendar.DAY_OF_MONTH, -30);
        Date timeAgo = calendar.getTime();
        int yearAgo = calendar.get(Calendar.YEAR);
        int monthAgo = calendar.get(Calendar.MONTH) + 1;
        String monthAgoStr;
        if (monthAgo < 10) {
            monthAgoStr = "0" + (monthAgo);
        } else {
            monthAgoStr = String.valueOf(monthAgo);
        }
        String tableName  = "tb_batt_realdata_" + battGroupId + "_" + year + "_" + monthStr;
        //30天前的时间
        String tableNameAgo = "tb_batt_realdata_" + battGroupId + "_" + yearAgo + "_" + monthAgoStr;
        if (tableListLike.contains(tableName)) {
            tableListInDB.add(tableName);
        }
        if (tableListLike.contains(tableNameAgo)) {
            tableListInDB.add(tableNameAgo);
        }
        //获取所有数据
        List<BattRealTimeDataHistory> dataList = new ArrayList<>();
        for (int i = 0; i < tableListInDB.size(); i++) {
            String tableNameTmp = tableListInDB.get(i);
            List<BattRealTimeDataHistory> list;
            if(tableNameTmp.equals(tableName)){
                list = battRTDataHisService.getFirstRecordListOfDay(tableNameTmp, null);
            }else{
                //说明是上个月的记录,需要记录时间大于30天前的时间
                list = battRTDataHisService.getFirstRecordListOfDay(tableNameTmp, timeAgo);
            }
            dataList.addAll(list);
        }
        //按时间排序和分组
        dataList.sort(Comparator.comparing(BattRealTimeDataHistory::getRecordTime));
        Map<Date, List<BattRealTimeDataHistory>> dataListMap = dataList.stream().collect(Collectors.groupingBy(BattRealTimeDataHistory::getRecordTime));
        Set<Date> dateKeySet = dataListMap.keySet();
        dataListMap.forEach((key,data)->{
            addStatistics(data, "tmp");
        });
        //给列数赋值
        columnCount = dateKeySet.size()+2;
        //列根据实际情况变动
        sheet4.addMergedRegion(new CellRangeAddress(0, 0,  0, columnCount-1));
@@ -348,7 +387,7 @@
            if(j==0){
                sheet4.setColumnWidth(j, 170 * 20);
            }else {
                sheet4.setColumnWidth(j, 128 * 20);
                sheet4.setColumnWidth(j, 138 * 20);
            }
        }
        //行宽
@@ -380,35 +419,33 @@
                } else if (i == 107) {
                    sheet4.createRow(i + 2).createCell(0).setCellValue("单节最小温度");
                }else if (i == 108) {
                    sheet4.createRow(i + 2).createCell(0).setCellValue("单节最大温度");
                    sheet4.createRow(i + 2).createCell(0).setCellValue("单节最大差值");
                }
            }
        }
        //创建第三行
        XSSFRow row2OfSheet4 = sheet4.createRow(2);
        row2OfSheet4.setHeightInPoints(40);
        sheet4.getRow(0).createCell(columnCount-1);
        for (int i = 0; i <= tableListInDB.size(); i++) { //这个是列的遍历
            if (i == tableListInDB.size()) {
                //最后一列备注:全部填空
                row2OfSheet4.createCell(i + 1);
                for (int j = 1; j <= 108; j++) { //这个是行的遍历
                    sheet4.getRow(j + 2).createCell(i + 1);
                }
            } else {
                //获取数据
                List<BattRealTimeDataHistory> list = battRTDataHisService.getFirstRecordList(tableListInDB.get(i));
                list.sort(Comparator.comparing(BattRealTimeDataHistory::getMonNum));
                //第三行的时间列填充
                Date testStartTime = list.get(0).getTestStartTime();
                //获取年月
                String testMonth = DateUtil.YYYY_DOT_MM.format(testStartTime);
                row2OfSheet4.createCell(i + 1).setCellValue(testMonth);
                addStatistics(list, "vol", "res");
                for (int j = 1; j <= 108; j++) { //这个是行的遍历
                    sheet4.getRow(j + 2).createCell(i + 1).setCellValue((list.get(j - 1).getMonVol()));
                }
        //填充数据列,key是列日期. value是列数据
        AtomicInteger count = new AtomicInteger();
        //对dateKeySet按时间排序
        Stream<Date> dateKeySetSorted = dateKeySet.stream().sorted(Comparator.comparing(Date::getTime));
        dateKeySetSorted.forEach(key -> {
            List<BattRealTimeDataHistory> value = dataListMap.get(key);
            //第三行时间列填充
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd\nHH:mm:ss");
            row2OfSheet4.createCell(count.get() + 1).setCellValue(format.format(key));
            for (int j = 1; j <= 108; j++) { //这个是行的遍历
                sheet4.getRow(j + 2).createCell(count.get() + 1).setCellValue(value.get(j - 1).getMonTmp());
            }
            count.getAndIncrement();
        });
        //最后一列备注:全部填空
        row2OfSheet4.createCell(columnCount-1);
        for (int j = 1; j <= 108; j++) { //这个是行的遍历
            sheet4.getRow(j + 2).createCell(columnCount-1);
        }
        addGlobalStylesToAllCells(sheet4, workbook);
@@ -417,6 +454,16 @@
        XSSFCellStyle cellStyleOriginal = cellOfSheet4.getCellStyle();
        XSSFCellStyle cellStyleNew = getCellStyleFont(cellStyleOriginal,workbook, true, 15);
        cellOfSheet4.setCellStyle(cellStyleNew);
        //第三行 所有列水平垂直居中,自动换行
        for (int i = 1; i < columnCount; i++) {
            XSSFCell cell = sheet4.getRow(2).getCell(i);
            XSSFCellStyle cellStyle = cell.getCellStyle();
            cellStyle.setAlignment(HorizontalAlignment.CENTER);
            cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
            cellStyle.setWrapText(true);
            cell.setCellStyle(cellStyle);
        }
        createChart(sheet4, columnCount,"温度折线图");
@@ -889,10 +936,19 @@
                //最大差值
                float tmpGap = tmpMax - tmpMin;
                Date now = new Date();
                hisAvg.setMonTmp(tmpAvg);
                hisAvg.setRecordTime(now);
                hisMax.setMonTmp(tmpMax);
                hisMax.setRecordTime(now);
                hisMin.setMonTmp(tmpMin);
                hisMin.setRecordTime(now);
                hisGap.setMonTmp(tmpGap);
                hisGap.setRecordTime(now);
            }