Selaa lähdekoodia

文件导出测试

yq 4 vuotta sitten
vanhempi
commit
e60f676c7e

+ 17 - 0
fiveep-controller/src/main/java/com/bizmatics/controller/web/AlarmPowerController.java

@@ -76,5 +76,22 @@ public class AlarmPowerController {
                                                   @RequestParam Integer size){
         return ApiResult.success(alarmPowerService.page(startTime, endTime, status, siteId, current, size));
     }
+
+
+    /**
+     * 导出
+     * @param startTime 开始时间
+     * @param endTime 结束时间
+     * @param status 状态
+     * @param siteId 站点
+     * @return
+     */
+    @RequestMapping("/export")
+    public ApiResult<String> export(@RequestParam(required = false) Date startTime,
+                                                  @RequestParam(required = false) Date endTime,
+                                                  @RequestParam(required = false) Integer status,
+                                                  @RequestParam(required = false) Integer siteId){
+        return ApiResult.success(alarmPowerService.export(startTime, endTime, status, siteId));
+    }
 }
 

+ 1 - 0
fiveep-controller/src/main/resources/application-dev.properties

@@ -1,6 +1,7 @@
 debug=true
 spring.main.lazy-initialization=false
 spring.main.allow-bean-definition-overriding=true
+temp.basedir=C:/Users/pc/Desktop
 # application
 server.port=8010
 # mybatis-plus

+ 1 - 0
fiveep-controller/src/main/resources/application-prod.properties

@@ -1,6 +1,7 @@
 debug=true
 spring.main.lazy-initialization=false
 spring.main.allow-bean-definition-overriding=true
+temp.basedir=/usr/local/service/fiveep/file
 # application
 server.port=8008
 # mybatis-plus

+ 5 - 3
fiveep-model/src/main/java/com/bizmatics/model/AlarmPower.java

@@ -4,6 +4,8 @@ import com.baomidou.mybatisplus.annotation.IdType;
 import com.baomidou.mybatisplus.annotation.TableId;
 import java.time.LocalDateTime;
 import java.io.Serializable;
+import java.util.Date;
+
 import lombok.Data;
 import lombok.EqualsAndHashCode;
 import lombok.experimental.Accessors;
@@ -42,7 +44,7 @@ public class AlarmPower implements Serializable {
     /**
      * 发送时间
      */
-    private LocalDateTime sendingTime;
+    private Date sendingTime;
 
     /**
      * 告警类型(alert 告警)
@@ -67,7 +69,7 @@ public class AlarmPower implements Serializable {
     /**
      * 发生时间
      */
-    private LocalDateTime soeTime;
+    private Date soeTime;
 
     /**
      * 处理人
@@ -77,7 +79,7 @@ public class AlarmPower implements Serializable {
     /**
      * 处理时间
      */
-    private LocalDateTime handlingTime;
+    private Date handlingTime;
 
     /**
      * 处理内容

+ 2 - 1
fiveep-persistence/src/main/java/com/bizmatics/persistence/mapper/AlarmPowerMapper.java

@@ -1,6 +1,7 @@
 package com.bizmatics.persistence.mapper;
 
 import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.bizmatics.model.AlarmPower;
 import com.bizmatics.common.mvc.base.CrudMapper;
 import org.apache.ibatis.annotations.Param;
@@ -35,7 +36,7 @@ public interface AlarmPowerMapper extends CrudMapper<AlarmPower> {
 
 
 
-    List<AlarmPower> list(IPage<AlarmPower> page,
+    Page<AlarmPower> page(IPage<AlarmPower> page,
                           @Param("userId") Integer userId,
                           @Param("siteId") Integer siteId,
                           @Param("handlingStatus") Integer handlingStatus,

+ 1 - 1
fiveep-persistence/src/main/resources/mapper/mysql/AlarmPowerMapper.xml

@@ -44,7 +44,7 @@
             </if>
         </where>
     </select>
-    <select id="list" resultType="com.bizmatics.model.AlarmPower">
+    <select id="page" resultType="com.bizmatics.model.AlarmPower">
         select *
         from user_site as us
         inner join device as d

+ 5 - 0
fiveep-service/pom.xml

@@ -60,6 +60,11 @@
             <artifactId>mybatis-plus-core</artifactId>
             <version>3.3.2</version>
         </dependency>
+        <dependency>
+            <groupId>cn.afterturn</groupId>
+            <artifactId>easypoi-spring-boot-starter</artifactId>
+            <version>4.1.0</version>
+        </dependency>
 
 
     </dependencies>

+ 11 - 0
fiveep-service/src/main/java/com/bizmatics/service/AlarmPowerService.java

@@ -41,4 +41,15 @@ public interface AlarmPowerService extends CrudService<AlarmPower> {
      */
     CommonPage<AlarmPower> page(Date startTime, Date endTime, Integer status, Integer siteId, Integer current, Integer size);
 
+
+    /**
+     * list
+     * @param startTime 开始时间
+     * @param endTime 结束时间
+     * @param status 状态
+     * @param siteId 站点id
+     * @return
+     */
+    String export(Date startTime, Date endTime, Integer status, Integer siteId);
+
 }

+ 51 - 5
fiveep-service/src/main/java/com/bizmatics/service/impl/AlarmPowerServiceImpl.java

@@ -1,22 +1,32 @@
 package com.bizmatics.service.impl;
 
+import cn.afterturn.easypoi.excel.ExcelExportUtil;
+import cn.afterturn.easypoi.excel.entity.ExportParams;
+import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
+import cn.afterturn.easypoi.handler.inter.IExcelExportServer;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.bizmatics.common.core.bean.CommonPage;
+import com.bizmatics.common.core.exception.BusinessException;
+import com.bizmatics.common.core.util.BeanMapperUtils;
 import com.bizmatics.common.core.util.DateUtils;
+import com.bizmatics.common.core.util.FileUtils;
+import com.bizmatics.common.spring.util.GlobalUtils;
 import com.bizmatics.model.AlarmPower;
 import com.bizmatics.persistence.mapper.AlarmPowerMapper;
 import com.bizmatics.service.AlarmPowerService;
 import com.bizmatics.common.mvc.base.AbstractCrudService;
 import com.bizmatics.service.util.SessionLocal;
+import com.bizmatics.service.vo.AlarmPowerExportVO;
 import com.bizmatics.service.vo.ApCountVO;
 import com.bizmatics.service.vo.CommonIcoVO;
+import org.apache.poi.ss.usermodel.Workbook;
 import org.checkerframework.checker.units.qual.A;
 import org.springframework.stereotype.Service;
 
-import java.util.ArrayList;
-import java.util.Date;
-import java.util.List;
-import java.util.Map;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.util.*;
 
 /**
  * <p>
@@ -65,7 +75,43 @@ public class AlarmPowerServiceImpl extends AbstractCrudService<AlarmPowerMapper,
     @Override
     public CommonPage<AlarmPower> page(Date startTime, Date endTime, Integer status, Integer siteId,Integer current,Integer size) {
         Page<AlarmPower> page = new Page<>(current, size);
-        baseMapper.list(page,SessionLocal.getUserId(),siteId,status,startTime,endTime);
+        page = baseMapper.page(page,SessionLocal.getUserId(),siteId,status,startTime,endTime);
         return this.ToCommonPage(page);
     }
+
+    @Override
+    public String export(Date startTime, Date endTime, Integer status, Integer siteId){
+        Integer userId = SessionLocal.getUserId();
+        Workbook workbook = null;
+        File file = null;
+        try {
+            ExportParams params = new ExportParams(null, "电力告警");
+            workbook = ExcelExportUtil.exportBigExcel(params, AlarmPowerExportVO.class,
+                        (o, i) -> {
+                        Page<AlarmPower> page = new Page<>(i, 30);
+                        page = baseMapper.page(page, userId, siteId, status, startTime, endTime);
+                            return new ArrayList<>(BeanMapperUtils.mapList(page.getRecords(), AlarmPower.class, AlarmPowerExportVO.class));
+                    },null);
+            if (null != workbook) {
+                file = FileUtils.getFile(GlobalUtils.getTempBaseDir(), String.format("%s-%s.xlsx", "电力告警", System.currentTimeMillis() + ""));
+                FileUtils.createFile(file.getAbsolutePath());
+                FileOutputStream allListingFileOutputStream = new FileOutputStream(file);
+                workbook.write(allListingFileOutputStream);
+            } else {
+                throw new BusinessException("表格数据为空");
+            }
+        } catch (Exception e) {
+            log.error("导出文件失败", e);
+            throw new BusinessException("导出文件失败");
+        } finally {
+            if (workbook != null) {
+                try {
+                    workbook.close();
+                } catch (IOException e) {
+                    log.error("===export spec=== 关闭workbook失败", e);
+                }
+            }
+        }
+        return GlobalUtils.getTempBaseDir()+"/"+file.getName();
+    }
 }

+ 99 - 0
fiveep-service/src/main/java/com/bizmatics/service/vo/AlarmPowerExportVO.java

@@ -0,0 +1,99 @@
+package com.bizmatics.service.vo;
+
+import cn.afterturn.easypoi.excel.annotation.Excel;
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableId;
+import lombok.Data;
+
+import java.time.LocalDateTime;
+import java.util.Date;
+
+/**
+ * @author yq
+ * @date 2021/7/13 16:21
+ */
+@Data
+public class AlarmPowerExportVO {
+    /**
+     * 电力告警表ID
+     */
+    private Integer id;
+
+    /**
+     * 阿里云平台设备名称
+     */
+    @Excel(name = "设备名称", height = 6, width = 20)
+    private String deviceCode;
+
+    /**
+     * 告警名称
+     */
+    @Excel(name = "告警名称", height = 6, width = 20)
+    private String alarmName;
+
+    /**
+     * 发送时间
+     */
+    @Excel(name = "发送时间", height = 6, width = 20)
+    private Date sendingTime;
+
+    /**
+     * 告警类型(alert 告警)
+     */
+    @Excel(name = "告警类型", height = 6, width = 20)
+    private String alarmType;
+
+    /**
+     * 测点名称
+     */
+    @Excel(name = "测点名称", height = 6, width = 20)
+    private String measName;
+
+    /**
+     * 遥信值(0  正常  1 告警)
+     */
+    @Excel(name = "遥信值", height = 6, width = 20)
+    private Integer digitalValue;
+
+    /**
+     * 测点描述
+     */
+    @Excel(name = "测点描述", height = 6, width = 20)
+    private String measDesc;
+
+    /**
+     * 发生时间
+     */
+    @Excel(name = "发生时间", height = 6, width = 20)
+    private Date soeTime;
+
+    /**
+     * 处理人
+     */
+    @Excel(name = "处理人", height = 6, width = 20)
+    private String handler;
+
+    /**
+     * 处理时间
+     */
+    @Excel(name = "处理时间", height = 6, width = 20)
+    private Date handlingTime;
+
+    /**
+     * 处理内容
+     */
+    @Excel(name = "处理内容", height = 6, width = 20)
+    private String handlingContent;
+
+    /**
+     * 联系电话
+     */
+    @Excel(name = "联系电话", height = 6, width = 20)
+    private String handlerPhone;
+
+    /**
+     * 处理状态(0 未处理,1 已处理)
+     */
+    @Excel(name = "处理状态", height = 6, width = 20)
+    private Integer handlingStatus;
+}