xiexiang 2 лет назад
Родитель
Сommit
613262835a

+ 24 - 0
PCS/src/main/java/cn/cslg/permission/common/model/dto/QueryLoginRecordDTO.java

@@ -0,0 +1,24 @@
+package cn.cslg.permission.common.model.dto;
+
+import com.fasterxml.jackson.annotation.JsonFormat;
+import lombok.Data;
+import org.springframework.format.annotation.DateTimeFormat;
+
+
+import java.util.Date;
+
+/**
+ * @Author xiexiang
+ * @Date 2023/8/11
+ */
+@Data
+public class QueryLoginRecordDTO {
+    private Integer id;
+    private Integer queryType;
+    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
+    @JsonFormat(pattern = "yyyy-MM-dd")
+    private Date startTime;
+    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
+    @JsonFormat(pattern = "yyyy-MM-dd")
+    private Date endTime;
+}

+ 58 - 19
PCS/src/main/java/cn/cslg/permission/controller/Export2ExcelController.java

@@ -1,18 +1,26 @@
 package cn.cslg.permission.controller;
 
 import cn.cslg.permission.common.core.base.Constants;
+import cn.cslg.permission.common.model.dto.QueryLoginRecordDTO;
 import cn.cslg.permission.common.model.vo.PersonnelNameVO;
 import cn.cslg.permission.common.model.vo.ReportTenantVO;
 import cn.cslg.permission.common.utils.Response;
 import cn.cslg.permission.service.Export2ExcelService;
+import cn.hutool.core.util.IdUtil;
 import io.swagger.v3.oas.annotations.Operation;
 import io.swagger.v3.oas.annotations.tags.Tag;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.context.annotation.Lazy;
+import org.springframework.core.io.InputStreamResource;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.MediaType;
+import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.*;
 
+import java.io.ByteArrayInputStream;
 import java.io.IOException;
+import java.util.Date;
 import java.util.List;
 
 /**
@@ -28,32 +36,53 @@ import java.util.List;
 public class Export2ExcelController {
     private final Export2ExcelService export2ExcelService;
 
-    @GetMapping("/exportLoginRecord")
+
+    @PostMapping("/exportLoginRecord")
     @Operation(summary = "导出登录日志")
-    public String exportLoginRecord(Integer queryType) throws IOException {
-        export2ExcelService.exportLoginRecord(queryType);
-        return Response.success();
+    public ResponseEntity<InputStreamResource> exportLoginRecord(@RequestBody QueryLoginRecordDTO queryLoginRecordDTO) throws IOException {
+        byte[] fileData = export2ExcelService.exportLoginRecord(queryLoginRecordDTO);
+        //保存生成excel的地址
+        String fileName = IdUtil.simpleUUID() + ".xls";
+        return ResponseEntity.ok().contentLength(fileData.length)
+                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileName + "\"")
+                .contentType(MediaType.parseMediaType("application/octet-stream"))
+                .body(new InputStreamResource(new ByteArrayInputStream(fileData)));
     }
 
     @PostMapping("/exportTotal")
     @Operation(summary = "导出汇总报表")
-    public String exportTotal(@RequestBody List<Integer> tenantIds) throws IOException {
-        export2ExcelService.exportTotal(tenantIds);
-        return Response.success();
+    public ResponseEntity<InputStreamResource> exportTotal(@RequestBody List<Integer> tenantIds) throws IOException {
+        byte[] fileData = export2ExcelService.exportTotal(tenantIds);
+        //保存生成excel的地址
+        String fileName = IdUtil.simpleUUID() + ".xls";
+        return ResponseEntity.ok().contentLength(fileData.length)
+                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileName + "\"")
+                .contentType(MediaType.parseMediaType("application/octet-stream"))
+                .body(new InputStreamResource(new ByteArrayInputStream(fileData)));
     }
 
     @PostMapping("/exportProject")
     @Operation(summary = "导出租户专题库使用报表")
-    public String exportProject(@RequestBody List<Integer> tenantIds) throws IOException {
-        export2ExcelService.exportProjectNameToExcel(tenantIds);
-        return Response.success();
+    public ResponseEntity<InputStreamResource> exportProject(@RequestBody List<Integer> tenantIds) throws IOException {
+        byte[] fileData = export2ExcelService.exportProjectNameToExcel(tenantIds);
+        //保存生成excel的地址
+        String fileName = IdUtil.simpleUUID() + ".xls";
+        return ResponseEntity.ok().contentLength(fileData.length)
+                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileName + "\"")
+                .contentType(MediaType.parseMediaType("application/octet-stream"))
+                .body(new InputStreamResource(new ByteArrayInputStream(fileData)));
     }
 
     @PostMapping("/exportPersonName")
     @Operation(summary = "导出租户人员报表")
-    public String exportPersonName(@RequestBody List<Integer> tenantIds) throws IOException {
-        export2ExcelService.exportPersonnelNameToExcel(tenantIds);
-        return Response.success();
+    public ResponseEntity<InputStreamResource> exportPersonName(@RequestBody List<Integer> tenantIds) throws IOException {
+        byte[] fileData = export2ExcelService.exportPersonnelNameToExcel(tenantIds);
+        //保存生成excel的地址
+        String fileName = IdUtil.simpleUUID() + ".xls";
+        return ResponseEntity.ok().contentLength(fileData.length)
+                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileName + "\"")
+                .contentType(MediaType.parseMediaType("application/octet-stream"))
+                .body(new InputStreamResource(new ByteArrayInputStream(fileData)));
     }
 
     @PostMapping("/getTenantPerson")
@@ -64,16 +93,26 @@ public class Export2ExcelController {
 
     @PostMapping("/exportReport")
     @Operation(summary = "导出租户报告使用报表")
-    public String exportReport(@RequestBody List<Integer> tenantIds) throws IOException {
-        export2ExcelService.exportReportTenant(tenantIds);
-        return Response.success();
+    public ResponseEntity<InputStreamResource> exportReport(@RequestBody List<Integer> tenantIds) throws IOException {
+        byte[] fileData = export2ExcelService.exportReportTenant(tenantIds);
+        //保存生成excel的地址
+        String fileName = IdUtil.simpleUUID() + ".xls";
+        return ResponseEntity.ok().contentLength(fileData.length)
+                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileName + "\"")
+                .contentType(MediaType.parseMediaType("application/octet-stream"))
+                .body(new InputStreamResource(new ByteArrayInputStream(fileData)));
     }
 
     @PostMapping("/exportProjectShared")
     @Operation(summary = "导出租户被分享专题库报表")
-    public String exportProjectShared(@RequestBody List<Integer> tenantIds) throws IOException {
-        export2ExcelService.exportProjectShared(tenantIds);
-        return Response.success();
+    public ResponseEntity<InputStreamResource> exportProjectShared(@RequestBody List<Integer> tenantIds) throws IOException {
+        byte[] fileData = export2ExcelService.exportProjectShared(tenantIds);
+        //保存生成excel的地址
+        String fileName = IdUtil.simpleUUID() + ".xls";
+        return ResponseEntity.ok().contentLength(fileData.length)
+                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileName + "\"")
+                .contentType(MediaType.parseMediaType("application/octet-stream"))
+                .body(new InputStreamResource(new ByteArrayInputStream(fileData)));
     }
 
     @PostMapping("/getPersonnelNameData")

+ 40 - 58
PCS/src/main/java/cn/cslg/permission/service/Export2ExcelService.java

@@ -1,5 +1,6 @@
 package cn.cslg.permission.service;
 
+import cn.cslg.permission.common.model.dto.QueryLoginRecordDTO;
 import cn.cslg.permission.common.model.vo.*;
 import cn.cslg.permission.common.utils.CacheUtils;
 import cn.cslg.permission.common.utils.FileUtils;
@@ -23,6 +24,7 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 import org.springframework.beans.BeanUtils;
 import org.springframework.stereotype.Service;
 
+import java.io.ByteArrayOutputStream;
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
@@ -56,15 +58,11 @@ public class Export2ExcelService {
      * 租户专题库使用情况报表
      * @throws IOException
      */
-    public void exportProjectNameToExcel(List<Integer> tenantIds) throws IOException {
-        //保存生成excel的地址
-        String fileName = IdUtil.simpleUUID() + ".xls";
-        String directoryName = fileUtils.createDirectory();
-        String outPath = fileUtils.getSavePath(directoryName) + fileName;
+    public byte[] exportProjectNameToExcel(List<Integer> tenantIds) throws IOException {
         //查询数据
         List<ProjectUsedByTenantVO> projectUsedByTenantVOS = this.getProjectData(tenantIds);
         //配置导出excel
-        try (FileOutputStream fileOut = new FileOutputStream(outPath)){
+        try (ByteArrayOutputStream out = new ByteArrayOutputStream()){
             //创建excel
             Workbook workbook = new XSSFWorkbook();
             //创建sheet页
@@ -99,9 +97,10 @@ public class Export2ExcelService {
             sheet.setColumnWidth(1, 256 * 20);
             sheet.setColumnWidth(2, 256 * 20);
             sheet.setColumnWidth(3, 256 * 20);
-            workbook.write(fileOut);
+            workbook.write(out);
+            return out.toByteArray();
         } catch (FileNotFoundException e) {
-            e.printStackTrace();
+            throw new FileNotFoundException();
         }
     }
     /**
@@ -149,15 +148,11 @@ public class Export2ExcelService {
      * 租户人员报表
      * @throws IOException
      */
-    public void exportPersonnelNameToExcel(List<Integer> tenantIds) throws IOException {
-        //保存生成excel的地址
-        String fileName = IdUtil.simpleUUID() + ".xls";
-        String directoryName = fileUtils.createDirectory();
-        String outPath = fileUtils.getSavePath(directoryName) + fileName;
+    public byte[] exportPersonnelNameToExcel(List<Integer> tenantIds) throws IOException {
         //查询数据
         List<PersonnelNameVO> personnelNameVOS = this.getPersonnelNameData(tenantIds);
         //配置导出excel
-        try (FileOutputStream fileOut = new FileOutputStream(outPath)){
+        try (ByteArrayOutputStream out = new ByteArrayOutputStream()){
             //创建excel
             Workbook workbook = new XSSFWorkbook();
             //创建sheet页
@@ -192,9 +187,10 @@ public class Export2ExcelService {
             sheet.setColumnWidth(1, 256 * 20);
             sheet.setColumnWidth(2, 256 * 20);
             sheet.setColumnWidth(3, 256 * 20);
-            workbook.write(fileOut);
+            workbook.write(out);
+            return out.toByteArray();
         } catch (FileNotFoundException e) {
-            e.printStackTrace();
+            throw new FileNotFoundException();
         }
     }
     /**
@@ -257,7 +253,7 @@ public class Export2ExcelService {
      * 租户报告使用情况报表
      * @throws IOException
      */
-    public void exportReportTenant(List<Integer> tenantIds) throws IOException {
+    public byte[] exportReportTenant(List<Integer> tenantIds) throws IOException {
         //需要导出到excel的对象集合,查询数据
         List<ReportUsedByTenantVO> reportUsedByTenantVOS = this.reportStr2Object(tenantIds);
         //保存生成excel的地址
@@ -265,7 +261,7 @@ public class Export2ExcelService {
         String directoryName = fileUtils.createDirectory();
         String outPath = fileUtils.getSavePath(directoryName) + fileName;
         //配置导出excel
-        try (FileOutputStream fileOut = new FileOutputStream(outPath)){
+        try (ByteArrayOutputStream out = new ByteArrayOutputStream()){
             //创建excel
             Workbook workbook = new XSSFWorkbook();
             //创建sheet页
@@ -298,9 +294,10 @@ public class Export2ExcelService {
             sheet.setColumnWidth(0, 256 * 30);
             sheet.setColumnWidth(1, 256 * 20);
             sheet.setColumnWidth(2, 256 * 20);
-            workbook.write(fileOut);
+            workbook.write(out);
+            return out.toByteArray();
         } catch (FileNotFoundException e) {
-            e.printStackTrace();
+            throw new FileNotFoundException();
         }
     }
     /**
@@ -371,15 +368,11 @@ public class Export2ExcelService {
      * 租户被分享专题库报表
      * @throws IOException
      */
-    public void exportProjectShared(List<Integer> tenantIds) throws IOException {
+    public byte[] exportProjectShared(List<Integer> tenantIds) throws IOException {
         //需要导出到excel的对象集合,查询数据
         List<ProjectSharedTenantVO> projectSharedTenantVOS = this.getProjectSharedData(tenantIds);
-        //保存生成excel的地址
-        String fileName = IdUtil.simpleUUID() + ".xls";
-        String directoryName = fileUtils.createDirectory();
-        String outPath = fileUtils.getSavePath(directoryName) + fileName;
         //配置导出excel
-        try (FileOutputStream fileOut = new FileOutputStream(outPath)){
+        try (ByteArrayOutputStream out = new ByteArrayOutputStream()){
             //创建excel
             Workbook workbook = new XSSFWorkbook();
             //创建sheet页
@@ -412,9 +405,10 @@ public class Export2ExcelService {
             sheet.setColumnWidth(0, 256 * 30);
             sheet.setColumnWidth(1, 256 * 20);
             sheet.setColumnWidth(2, 256 * 20);
-            workbook.write(fileOut);
+            workbook.write(out);
+            return out.toByteArray();
         } catch (FileNotFoundException e) {
-            e.printStackTrace();
+            throw new FileNotFoundException();
         }
     }
 
@@ -463,8 +457,9 @@ public class Export2ExcelService {
      * 导出汇总报表
      * @param tenantIds
      * @throws IOException
+     * @return
      */
-    public void exportTotal(List<Integer> tenantIds) throws IOException {
+    public byte[] exportTotal(List<Integer> tenantIds) throws IOException {
         List<TotalDataVO> totalDataVOS = new ArrayList<>();
         if(tenantIds.size() > 0) {
             for (Integer tenantId : tenantIds) {
@@ -478,12 +473,8 @@ public class Export2ExcelService {
                 totalDataVOS.add(totalDataVO);
             }
         }
-        //保存生成excel的地址
-        String fileName = IdUtil.simpleUUID() + ".xls";
-        String directoryName = fileUtils.createDirectory();
-        String outPath = fileUtils.getSavePath(directoryName) + fileName;
         //配置导出excel
-        try (FileOutputStream fileOut = new FileOutputStream(outPath)){
+        try (ByteArrayOutputStream out = new ByteArrayOutputStream()){
             //创建excel
             Workbook workbook = new XSSFWorkbook();
             //创建sheet页
@@ -520,9 +511,10 @@ public class Export2ExcelService {
             sheet.setColumnWidth(2, 256 * 20);
             sheet.setColumnWidth(3, 256 * 20);
             sheet.setColumnWidth(4, 256 * 20);
-            workbook.write(fileOut);
+            workbook.write(out);
+            return out.toByteArray();
         } catch (FileNotFoundException e) {
-            e.printStackTrace();
+            throw new FileNotFoundException();
         }
     }
 
@@ -586,18 +578,15 @@ public class Export2ExcelService {
 
     /**
      * 登陆日志报表导出
-     * @param queryType
+     * @param queryLoginRecordDTO
+     * @return
      * @throws IOException
      */
-    public void exportLoginRecord(Integer queryType) throws IOException {
+    public byte[] exportLoginRecord(QueryLoginRecordDTO queryLoginRecordDTO) throws IOException {
         //需要导出到excel的对象集合,查询数据
-        List<LoginRecordExportVO> loginRecordExportVOS = this.loadLoginRecordData(queryType);
-        //保存生成excel的地址
-        String fileName = IdUtil.simpleUUID() + ".xls";
-        String directoryName = fileUtils.createDirectory();
-        String outPath = fileUtils.getSavePath(directoryName) + fileName;
+        List<LoginRecordExportVO> loginRecordExportVOS = this.loadLoginRecordData(queryLoginRecordDTO);
         //配置导出excel
-        try (FileOutputStream fileOut = new FileOutputStream(outPath)){
+        try (ByteArrayOutputStream out = new ByteArrayOutputStream()){
             //创建excel
             Workbook workbook = new XSSFWorkbook();
             //创建sheet页
@@ -640,30 +629,23 @@ public class Export2ExcelService {
             sheet.setColumnWidth(5, 256 * 20);
             sheet.setColumnWidth(6, 256 * 20);
             sheet.setColumnWidth(7, 256 * 20);
-            workbook.write(fileOut);
+            workbook.write(out);
+            return out.toByteArray();
         } catch (FileNotFoundException e) {
-            e.printStackTrace();
+            throw new XiaoShiException("下载失败");
         }
     }
 
     /**
      * 装载登录日志Excel信息
-     * @param queryType
+     * @param queryLoginRecordDTO
      * @return
      */
-    public List<LoginRecordExportVO> loadLoginRecordData(Integer queryType){
+    public List<LoginRecordExportVO> loadLoginRecordData(QueryLoginRecordDTO queryLoginRecordDTO){
         List<LoginRecordExportVO> loginRecordExportVOS = new ArrayList<>();
         List<LoginRecord> loginRecords = new ArrayList<>();
-        //获取当前登陆人信息
-//        PersonnelVO personnelVO = cacheUtils.getLoginUser(loginUtils.getId());
-        if(queryType == null || queryType.equals(0)){
-//            Integer id = personnelVO.getId();
-            Integer id = 328;
-            loginRecords = loginRecordService.getLoginRecord(id, queryType);
-        } else if(queryType.equals(1)){
-//            Integer id = personnelVO.getTenantId();
-            Integer id = 1;
-            loginRecords = loginRecordService.getLoginRecord(id, queryType);
+        if(queryLoginRecordDTO != null){
+            loginRecords = loginRecordService.getLoginRecord(queryLoginRecordDTO);
         } else {
             throw new XiaoShiException("导出失败");
         }

+ 9 - 3
PCS/src/main/java/cn/cslg/permission/service/LoginRecordService.java

@@ -1,5 +1,6 @@
 package cn.cslg.permission.service;
 
+import cn.cslg.permission.common.model.dto.QueryLoginRecordDTO;
 import cn.cslg.permission.common.model.vo.LoginRecordVO;
 import cn.cslg.permission.domain.LoginRecord;
 import cn.cslg.permission.exception.XiaoShiException;
@@ -12,6 +13,7 @@ import org.springframework.context.annotation.Lazy;
 import org.springframework.stereotype.Service;
 
 import java.util.ArrayList;
+import java.util.Date;
 import java.util.List;
 
 /**
@@ -83,16 +85,20 @@ public class LoginRecordService extends ServiceImpl<LoginRecordMapper,LoginRecor
 
     /**
      * 查询登陆记录,queryType为空或者为0,则根据人员查询,为1,则根据租户查询
-     * @param id
-     * @param queryType
+     * @param queryLoginRecordDTO
      * @return
      */
-    public List<LoginRecord> getLoginRecord(Integer id, Integer queryType){
+    public List<LoginRecord> getLoginRecord(QueryLoginRecordDTO queryLoginRecordDTO){
         List<LoginRecord> loginRecords = new ArrayList<>();
+        Integer id = queryLoginRecordDTO.getId();
+        Integer queryType = queryLoginRecordDTO.getQueryType();
+        Date startTime = queryLoginRecordDTO.getStartTime();
+        Date endTime = queryLoginRecordDTO.getEndTime();
         if(id != null && id > 0){
             LambdaQueryWrapper<LoginRecord> queryWrapper = new LambdaQueryWrapper<>();
             if(queryType == null || queryType.equals(0)){
                 queryWrapper.eq(LoginRecord::getPersonnelId, id);
+                queryWrapper.between(LoginRecord::getCreateTime,startTime,endTime);
                 loginRecords = this.list(queryWrapper);
                 return loginRecords;
             } else if(queryType.equals(1)){