miaolijing пре 3 година
родитељ
комит
6a00a802d2

+ 33 - 2
src/main/java/com/persagy/apm/alarmservice/group/alarmrecord/controller/AlarmRecordController.java

@@ -2,6 +2,7 @@ package com.persagy.apm.alarmservice.group.alarmrecord.controller;
 
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.persagy.apm.alarmservice.group.alarmrecord.enums.EnumAlarmListType;
 import com.persagy.apm.alarmservice.group.alarmrecord.enums.EnumAlarmTreatMode;
 import com.persagy.apm.alarmservice.group.alarmrecord.enums.EnumAlarmTreatState;
 import com.persagy.apm.alarmservice.group.alarmrecord.model.ConvertAlarmRecordTool;
@@ -28,19 +29,34 @@ import com.persagy.apm.energyalarmstarter.alarmdata.service.AlarmItemServiceImpl
 import com.persagy.apm.energyalarmstarter.alarmdata.service.AlarmRecordServiceImpl;
 import com.persagy.apm.alarmservice.group.management.model.equip.dto.QueryEquipDTO;
 import com.persagy.apm.alarmservice.group.management.model.equip.vo.EquipListItem;
+import com.persagy.apm.energyalarmstarter.alarmdata.utils.DateUtils;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
+import org.apache.ibatis.javassist.bytecode.ByteArray;
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.xssf.usermodel.XSSFCellStyle;
+import org.apache.poi.xssf.usermodel.XSSFRow;
+import org.apache.poi.xssf.usermodel.XSSFSheet;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 import org.assertj.core.util.Lists;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.util.CollectionUtils;
+import org.springframework.util.StringUtils;
 import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
+import javax.servlet.http.HttpServletResponse;
 import javax.validation.Valid;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.FileOutputStream;
+import java.io.OutputStream;
 import java.util.ArrayList;
+import java.util.Date;
 import java.util.List;
 import java.util.stream.Collectors;
 
@@ -271,9 +287,24 @@ public class AlarmRecordController {
 
     @ApiOperation(value = "下载")
     @PostMapping("/download")
-    public CommonResult downloadAlarmRecord(
-            @Valid @RequestBody DownloadAlarmRecordDTO downloadAlarmRecordDTO) {
+    public CommonResult downloadAlarmRecord(@Valid @RequestBody DownloadAlarmRecordDTO downloadAlarmRecordDTO) throws Exception {
+        //获取报警id列表
+        List<String> recordIdList = downloadAlarmRecordDTO.getIds();
+        //列表类型
+        String listType = downloadAlarmRecordDTO.getListType();
+        EnumAlarmListType enumAlarmListType = EnumAlarmListType.getByType(listType);
+        //获取报警详情列表
+        List<AlarmRecordItem> alarmRecordItemList = alarmRecordWrapper.getAlarmRecordList(recordIdList);
+        if (!CollectionUtils.isEmpty(alarmRecordItemList) && alarmRecordItemList.size() > 0) {
+            List<AlarmRecordItemVO> alarmRecordItemVOList = new ArrayList<>();
+            for (AlarmRecordItem alarmRecord : alarmRecordItemList) {
+                alarmRecordItemVOList.add(alarmRecordWrapper.convert2AlarmRecordItemVO(alarmRecord));
+            }
+            alarmRecordWrapper.initExcel(enumAlarmListType,alarmRecordItemVOList);
+        }
         return ResultHelper.success();
     }
+
+
 }
 

+ 41 - 0
src/main/java/com/persagy/apm/alarmservice/group/alarmrecord/enums/EnumAlarmListType.java

@@ -0,0 +1,41 @@
+package com.persagy.apm.alarmservice.group.alarmrecord.enums;
+
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+import lombok.Setter;
+import org.apache.commons.lang.StringUtils;
+
+/**
+ * 报警列表类型
+ *
+ **/
+@AllArgsConstructor
+public enum EnumAlarmListType {
+    /**
+     * 报警列表类型
+     */
+    CURRENT("1", "当前报警"),
+    HISTORY("2", "历史报警"),
+    IGNORE("3", "已忽略");
+
+    @Setter
+    @Getter
+    private String type;
+
+    @Setter
+    @Getter
+    private String desc;
+
+    public static EnumAlarmListType getByType(String type) {
+        if (StringUtils.isBlank(type)) {
+            return null;
+        }
+        EnumAlarmListType[] enums = EnumAlarmListType.values();
+        for (EnumAlarmListType item : enums) {
+            if (type.equals(item.getType())) {
+                return item;
+            }
+        }
+        return null;
+    }
+}

+ 3 - 0
src/main/java/com/persagy/apm/alarmservice/group/alarmrecord/model/dto/DownloadAlarmRecordDTO.java

@@ -17,4 +17,7 @@ import java.util.List;
 public class DownloadAlarmRecordDTO {
     @ApiModelProperty(value = "报警记录id")
     private List<String> ids;
+
+    @ApiModelProperty(value = "列表类型")
+    private String listType;
 }

+ 183 - 4
src/main/java/com/persagy/apm/alarmservice/group/alarmrecord/service/AlarmRecordWrapper.java

@@ -2,11 +2,8 @@ package com.persagy.apm.alarmservice.group.alarmrecord.service;
 
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
+import com.persagy.apm.alarmservice.group.alarmrecord.enums.*;
 import com.persagy.apm.alarmservice.project.alarmcondition.model.vo.AlarmConditionStrItem;
-import com.persagy.apm.alarmservice.group.alarmrecord.enums.EnumAlarmLevel;
-import com.persagy.apm.alarmservice.group.alarmrecord.enums.EnumAlarmState;
-import com.persagy.apm.alarmservice.group.alarmrecord.enums.EnumAlarmTreatMode;
-import com.persagy.apm.alarmservice.group.alarmrecord.enums.EnumAlarmTreatState;
 import com.persagy.apm.alarmservice.group.alarmrecord.model.vo.AlarmRecordItemVO;
 import com.persagy.apm.alarmservice.group.alarmrecord.model.vo.AlarmRecordListItem;
 import com.persagy.apm.alarmservice.group.management.model.equip.vo.EquipItem;
@@ -21,11 +18,19 @@ import com.persagy.apm.energyalarmstarter.alarmdata.service.AlarmItemServiceImpl
 import com.persagy.apm.energyalarmstarter.alarmdata.service.AlarmRecordServiceImpl;
 import com.persagy.apm.energyalarmstarter.alarmdata.utils.DateUtils;
 import org.apache.commons.lang3.StringUtils;
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.xssf.usermodel.XSSFCellStyle;
+import org.apache.poi.xssf.usermodel.XSSFRow;
+import org.apache.poi.xssf.usermodel.XSSFSheet;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 import org.assertj.core.util.Lists;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.util.CollectionUtils;
 
+import javax.servlet.http.HttpServletResponse;
+import java.io.FileOutputStream;
+import java.io.OutputStream;
 import java.math.BigDecimal;
 import java.math.RoundingMode;
 import java.util.*;
@@ -348,4 +353,178 @@ public class AlarmRecordWrapper {
         return alarmRecordService.queryCount(queryAlarmRecordDTO);
     }
 
+    /**
+     * 查看报警记录详情
+     *
+     * @param recordIdList 报警记录id列表
+     * @return 报警记录详情列表
+     * @author lixing
+     * @version V1.0 2021/9/21 3:54 下午
+     */
+    public List<AlarmRecordItem> getAlarmRecordList(List<String> recordIdList) throws Exception {
+        if (CollectionUtils.isEmpty(recordIdList) || recordIdList.size() == 0){
+            return null;
+        }
+        QueryAlarmRecordDTO queryAlarmRecordDTO = new QueryAlarmRecordDTO();
+        queryAlarmRecordDTO.setId(recordIdList);
+        DmpResult<List<AlarmRecordItem>> result = alarmRecordService.query(queryAlarmRecordDTO);
+        if (result == null) {
+            return null;
+        }
+        return result.getData();
+    }
+
+    /**
+     * 构造EXCEl文件
+     * @param enumAlarmListType 报警列表类型
+     * @param alarmRecordItemVOList 数据列表
+     */
+    public void initExcel(EnumAlarmListType enumAlarmListType, List<AlarmRecordItemVO> alarmRecordItemVOList){
+        try {
+            //创建Workbook
+            XSSFWorkbook wb = new XSSFWorkbook();
+            XSSFSheet overViewSheet = wb.createSheet();
+            wb.setSheetName(0,enumAlarmListType.getDesc());//设置sheet页标题
+            handleExcelTitle(enumAlarmListType,overViewSheet,wb);// 处理表头
+            //能耗概览Sheet数据填充
+            fillDataTable(enumAlarmListType,overViewSheet,alarmRecordItemVOList);
+            //本地自测
+            String time = DateUtils.date2Str(new Date(),DateUtils.SDFSECOND);
+            String excelName =enumAlarmListType.getDesc()+"-"+time+".xlsx";
+
+//            本地测试
+//            FileOutputStream out = new FileOutputStream("D:\\exl\\"+excelName);
+//            wb.write(out);
+//            out.flush();
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+    /**
+     * 填充 excel文件数据行
+     * @param enumAlarmListType 报警列表类型
+     * @param sheet 文件sheet
+     * @param alarmRecordItemVOList 数据列表
+     */
+    public void fillDataTable(EnumAlarmListType enumAlarmListType,XSSFSheet sheet,List<AlarmRecordItemVO> alarmRecordItemVOList) {
+        try {
+            for (int i = 0; i < alarmRecordItemVOList.size(); i++) {
+                AlarmRecordItemVO alarmRecordItemVO = alarmRecordItemVOList.get(i);
+                XSSFRow row = sheet.createRow(i+1);
+                String groupName = "";//分组名称
+                if (null != alarmRecordItemVO.getGroup() && null != alarmRecordItemVO.getGroup().getName()){
+                    groupName = alarmRecordItemVO.getGroup().getName();
+                }
+                Cell cell = row.createCell(0);cell.setCellValue(groupName);
+                String objName = "";//对象名称
+                if (null != alarmRecordItemVO.getObj() && null != alarmRecordItemVO.getObj().getName()){
+                    objName = alarmRecordItemVO.getObj().getName();
+                }
+                cell = row.createCell(1);cell.setCellValue(objName);
+                String itemName = "";//报警类型
+                if (null != alarmRecordItemVO.getAlarmItem() && null != alarmRecordItemVO.getAlarmItem().getName()){
+                    itemName = alarmRecordItemVO.getAlarmItem().getName();
+                }
+                cell = row.createCell(2);cell.setCellValue(itemName);
+                String levelName = "";//报警级别
+                if (null != alarmRecordItemVO.getAlarmLevel() && null != alarmRecordItemVO.getAlarmLevel().getName()){
+                    levelName = alarmRecordItemVO.getAlarmLevel().getName();
+                }
+                String stateName = "";//报警状态
+                if (null != alarmRecordItemVO.getAlarmState() && null != alarmRecordItemVO.getAlarmState().getName()){
+                    stateName = alarmRecordItemVO.getAlarmState().getName();
+                }
+                String treatStateName = "";//报警处理状态
+                if (null != alarmRecordItemVO.getAlarmTreatState() && null != alarmRecordItemVO.getAlarmTreatState().getName()){
+                    treatStateName = alarmRecordItemVO.getAlarmTreatState().getName();
+                }
+                String triggerTime = "";
+                if (null != alarmRecordItemVO.getTriggerTime()){
+                    triggerTime = DateUtils.date2Str(alarmRecordItemVO.getTriggerTime(),DateUtils.SDF_SECOND);
+                }
+                String treatEndTime = "";
+                if (null != alarmRecordItemVO.getTreatEndTime()){
+                    treatEndTime = DateUtils.date2Str(alarmRecordItemVO.getTreatEndTime(),DateUtils.SDF_SECOND);
+                }
+                String ignoreTime = "";
+                if (null != alarmRecordItemVO.getIgnoreTime()){
+                    ignoreTime = DateUtils.date2Str(alarmRecordItemVO.getIgnoreTime(),DateUtils.SDF_SECOND);
+                }
+                String duration = "";
+                if (null != alarmRecordItemVO.getDuration()){
+                    duration = alarmRecordItemVO.getDuration();
+                }
+                cell = row.createCell(3);cell.setCellValue(triggerTime);
+                if (EnumAlarmListType.CURRENT.getType().equals(enumAlarmListType.getType())){
+                    cell = row.createCell(4);cell.setCellValue(duration);
+                    cell = row.createCell(5);cell.setCellValue(levelName);
+                    cell = row.createCell(6);cell.setCellValue(stateName);
+                    cell = row.createCell(7);cell.setCellValue(treatStateName);
+                }else if (EnumAlarmListType.HISTORY.getType().equals(enumAlarmListType.getType())){
+                    cell = row.createCell(4);cell.setCellValue(treatEndTime);
+                    cell = row.createCell(5);cell.setCellValue(duration);
+                    cell = row.createCell(6);cell.setCellValue(levelName);
+                    cell = row.createCell(7);cell.setCellValue(stateName);
+                    cell = row.createCell(8);cell.setCellValue(treatStateName);
+                }else if (EnumAlarmListType.IGNORE.getType().equals(enumAlarmListType.getType())){
+                    cell = row.createCell(4);cell.setCellValue(ignoreTime);
+                    cell = row.createCell(5);cell.setCellValue(duration);
+                    cell = row.createCell(6);cell.setCellValue(levelName);
+                    cell = row.createCell(7);cell.setCellValue(stateName);
+                    cell = row.createCell(8);cell.setCellValue(treatStateName);
+                }
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+
+    /**
+     * 处理文件标题行
+     * @param enumAlarmListType  报警列表类型
+     * @param sheet 文件sheet
+     * @param wb 文件wb
+     */
+    public void handleExcelTitle(EnumAlarmListType enumAlarmListType,XSSFSheet sheet, XSSFWorkbook wb){
+        XSSFCellStyle cellStyle = wb.createCellStyle();
+        cellStyle.setWrapText(true);
+        //设置行宽
+        sheet.setColumnWidth(0, 5000);sheet.setColumnWidth(1, 5000);
+        sheet.setColumnWidth(2, 7000);sheet.setColumnWidth(3, 5000);
+        //行
+        XSSFRow Row1 = sheet.createRow(0);
+        Cell cell = Row1.createCell(0);cell.setCellStyle(cellStyle);cell.setCellValue("分组名称");
+        cell = Row1.createCell(1);cell.setCellStyle(cellStyle);cell.setCellValue("报警对象");
+        cell = Row1.createCell(2);cell.setCellStyle(cellStyle);cell.setCellValue("报警类型");
+        cell = Row1.createCell(3);cell.setCellStyle(cellStyle);cell.setCellValue("报警开始时间");
+        if (EnumAlarmListType.CURRENT.getType().equals(enumAlarmListType.getType())){
+            sheet.setColumnWidth(4, 3500);sheet.setColumnWidth(5, 3000);
+            sheet.setColumnWidth(6, 3000);sheet.setColumnWidth(7, 5000);
+            cell = Row1.createCell(4);cell.setCellStyle(cellStyle);cell.setCellValue("报警持续时长(h)");
+            cell = Row1.createCell(5);cell.setCellStyle(cellStyle);cell.setCellValue("报警等级");
+            cell = Row1.createCell(6);cell.setCellStyle(cellStyle);cell.setCellValue("报警状态");
+            cell = Row1.createCell(7);cell.setCellStyle(cellStyle);cell.setCellValue("报警处理状态");
+        }else if (EnumAlarmListType.HISTORY.getType().equals(enumAlarmListType.getType())){
+            sheet.setColumnWidth(4, 5000);sheet.setColumnWidth(5, 3500);
+            sheet.setColumnWidth(6, 3000);sheet.setColumnWidth(7, 3000);
+            sheet.setColumnWidth(8, 5000);
+            cell = Row1.createCell(4);cell.setCellStyle(cellStyle);cell.setCellValue("处理完成时间");
+            cell = Row1.createCell(5);cell.setCellStyle(cellStyle);cell.setCellValue("异常持续时长(h)");
+            cell = Row1.createCell(6);cell.setCellStyle(cellStyle);cell.setCellValue("报警等级");
+            cell = Row1.createCell(7);cell.setCellStyle(cellStyle);cell.setCellValue("报警状态");
+            cell = Row1.createCell(8);cell.setCellStyle(cellStyle);cell.setCellValue("报警处理状态");
+        }else if (EnumAlarmListType.IGNORE.getType().equals(enumAlarmListType.getType())){
+            sheet.setColumnWidth(4, 5000);sheet.setColumnWidth(5, 3500);
+            sheet.setColumnWidth(6, 3000);sheet.setColumnWidth(7, 3000);
+            sheet.setColumnWidth(8, 5000);
+            cell = Row1.createCell(4);cell.setCellStyle(cellStyle);cell.setCellValue("忽略时间");
+            cell = Row1.createCell(5);cell.setCellStyle(cellStyle);cell.setCellValue("异常持续时长(h)");
+            cell = Row1.createCell(6);cell.setCellStyle(cellStyle);cell.setCellValue("报警等级");
+            cell = Row1.createCell(7);cell.setCellStyle(cellStyle);cell.setCellValue("报警状态");
+            cell = Row1.createCell(8);cell.setCellStyle(cellStyle);cell.setCellValue("报警处理状态");
+        }
+    }
+
 }