|
@@ -3,6 +3,7 @@ package cn.kdan.compdf.service.impl;
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
import cn.kdan.compdf.config.ExcelWidthStyleStrategy;
|
|
|
import cn.kdan.compdf.config.RedisConstantServer;
|
|
|
+import cn.kdan.compdf.dto.DashExportXlsxDTO;
|
|
|
import cn.kdan.compdf.dto.DashboardStatisticsDTO;
|
|
|
import cn.kdan.compdf.entity.BackgroundConvertData;
|
|
|
import cn.kdan.compdf.entity.dto.AnalysisDataDTO;
|
|
@@ -14,10 +15,18 @@ import cn.kdan.compdf.enums.ResponseEnum;
|
|
|
import cn.kdan.compdf.exception.CommonException;
|
|
|
import cn.kdan.compdf.mapper.BackgroundConvertDataMapper;
|
|
|
import cn.kdan.compdf.service.BackgroundConvertDataService;
|
|
|
+import cn.kdan.compdf.service.BackgroundToolsService;
|
|
|
+import cn.kdan.compdf.service.BackgroundUserProjectService;
|
|
|
import cn.kdan.compdf.service.BackgroundUserService;
|
|
|
import cn.kdan.compdf.utils.MyLocalDateUtil;
|
|
|
+import cn.kdan.compdf.utils.TokenUtil;
|
|
|
+import cn.kdan.compdf.vo.ProjectQueryVO;
|
|
|
+import cn.kdan.compdf.vo.ToolQueryVO;
|
|
|
import com.alibaba.excel.EasyExcel;
|
|
|
+import com.alibaba.excel.ExcelWriter;
|
|
|
import com.alibaba.excel.util.MapUtils;
|
|
|
+import com.alibaba.excel.write.metadata.WriteSheet;
|
|
|
+import com.alibaba.excel.write.metadata.WriteTable;
|
|
|
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
@@ -26,20 +35,21 @@ import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.cache.CacheManager;
|
|
|
import org.springframework.cache.annotation.CacheEvict;
|
|
|
-import org.springframework.cache.annotation.Cacheable;
|
|
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
+import javax.servlet.ServletOutputStream;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import java.io.IOException;
|
|
|
import java.math.BigDecimal;
|
|
|
import java.net.URLEncoder;
|
|
|
import java.time.LocalDate;
|
|
|
import java.time.LocalDateTime;
|
|
|
-import java.time.LocalTime;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
import java.time.temporal.ChronoUnit;
|
|
|
import java.util.*;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
@@ -59,9 +69,16 @@ public class BackgroundConvertDataServiceImpl extends ServiceImpl<BackgroundConv
|
|
|
|
|
|
private final BackgroundUserService userService;
|
|
|
|
|
|
- public BackgroundConvertDataServiceImpl(StringRedisTemplate redisTemplate, BackgroundUserService userService) {
|
|
|
+ private final BackgroundUserProjectService backgroundUserProjectService;
|
|
|
+ private final BackgroundToolsService toolsService;
|
|
|
+
|
|
|
+ private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
|
|
|
+
|
|
|
+ public BackgroundConvertDataServiceImpl(StringRedisTemplate redisTemplate, BackgroundUserService userService, BackgroundUserProjectService backgroundUserProjectService, BackgroundToolsService toolsService) {
|
|
|
this.redisTemplate = redisTemplate;
|
|
|
this.userService = userService;
|
|
|
+ this.backgroundUserProjectService = backgroundUserProjectService;
|
|
|
+ this.toolsService = toolsService;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -527,17 +544,109 @@ public class BackgroundConvertDataServiceImpl extends ServiceImpl<BackgroundConv
|
|
|
listHand.add(head2);
|
|
|
listHand.add(head3);
|
|
|
listHand.add(head4);
|
|
|
+
|
|
|
+ // 查询类型
|
|
|
+ Integer queryType = queryDTO.getQueryType();
|
|
|
+ if (ObjectUtil.isEmpty(queryDTO.getTimeZone())){
|
|
|
+ queryDTO.setTimeZone(0);
|
|
|
+ }
|
|
|
+
|
|
|
+ LocalDateTime startDate = LocalDateTime.now();
|
|
|
+ LocalDateTime endDate = LocalDateTime.now();
|
|
|
+ if (queryType == 0) {
|
|
|
+ Integer days = queryDTO.getDays();
|
|
|
+ if (days > 30) {
|
|
|
+ days = 30;
|
|
|
+ queryDTO.setDays(30);
|
|
|
+ } else if (days < 1) {
|
|
|
+ days = 1;
|
|
|
+ queryDTO.setDays(1);
|
|
|
+ }
|
|
|
+ startDate = LocalDateTime.now();
|
|
|
+ endDate = startDate.minus(days, ChronoUnit.DAYS);
|
|
|
+ } else if (queryType == 1) {
|
|
|
+ LocalDate startDateTime = queryDTO.getStartDateTime();
|
|
|
+ LocalDate endDateTime = queryDTO.getEndDateTime();
|
|
|
+
|
|
|
+ if (startDateTime.isAfter(endDateTime)) {
|
|
|
+ // 查询参数异常 开始时间大于结束时间
|
|
|
+ throw new CommonException(BackgroundErrorEnum.QUEUE_PARAMETER_ERROR);
|
|
|
+ } else if (endDateTime.isAfter(MyLocalDateUtil.timeZoneConversion(LocalDateTime.now(), queryDTO.getTimeZone()).toLocalDate())) {
|
|
|
+ throw new CommonException(BackgroundErrorEnum.QUEUE_PARAMETER_ERROR);
|
|
|
+ }
|
|
|
+ // 开始时间等于结束时间
|
|
|
+ endDateTime = endDateTime.plusDays(1);
|
|
|
+ startDate = MyLocalDateUtil.timeZoneConversion(startDateTime, -queryDTO.getTimeZone());
|
|
|
+ endDate = MyLocalDateUtil.timeZoneConversion(endDateTime, -queryDTO.getTimeZone());
|
|
|
+ }
|
|
|
+
|
|
|
try {
|
|
|
// 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
|
|
|
String fileName = URLEncoder.encode("Dashboard表单", "UTF-8").replaceAll("\\+", "%20");
|
|
|
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
|
|
|
- EasyExcel.write(response.getOutputStream())
|
|
|
- .head(listHand)
|
|
|
+ ServletOutputStream outputStream = response.getOutputStream();
|
|
|
+ ExcelWriter excelWriter = EasyExcel.write(outputStream)
|
|
|
.registerWriteHandler(new ExcelWidthStyleStrategy())
|
|
|
.autoCloseStream(Boolean.FALSE)
|
|
|
.registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
|
|
|
- .sheet("Dashboard")
|
|
|
- .doWrite(dashboardExportObjects);
|
|
|
+ .build();
|
|
|
+
|
|
|
+ excelWriter.write(dashboardExportObjects, EasyExcel.writerSheet("Dashboard").head(listHand).build());
|
|
|
+
|
|
|
+ List<ProjectQueryVO> projectQuery = backgroundUserProjectService.getProjectQuery(TokenUtil.getRequestHeader().getTenantId());
|
|
|
+ List<ToolQueryVO> toolQueryS = toolsService.getToolQueryS();
|
|
|
+ LocalDateTime finalStartDate = startDate;
|
|
|
+ LocalDateTime finalEndDate = endDate;
|
|
|
+ projectQuery.forEach(projectQueryVO -> {
|
|
|
+ // 创建 WriteSheet 对象,指定 sheet 名称
|
|
|
+ WriteSheet writeSheet = EasyExcel.writerSheet(projectQueryVO.getProjectName()).build();
|
|
|
+ List<DashExportXlsxDTO> dashExportXlsxDTOS = new ArrayList<>();
|
|
|
+ if (queryDTO.getIsCN()){
|
|
|
+ dashExportXlsxDTOS.add(new DashExportXlsxDTO("时间: " + formatter.format(finalEndDate) + "-" + formatter.format(finalStartDate), "", "", "", ""));
|
|
|
+ dashExportXlsxDTOS.add(new DashExportXlsxDTO("功能", "成功", "失败", "失败率", "平均处理时长"));
|
|
|
+ }else {
|
|
|
+ dashExportXlsxDTOS.add(new DashExportXlsxDTO("Date: " + formatter.format(finalEndDate) + "-" + formatter.format(finalStartDate), "", "", "", ""));
|
|
|
+ dashExportXlsxDTOS.add(new DashExportXlsxDTO("Features", "Successful Requests", "Error Requests", "Error Ratio", "Average Process Time"));
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ toolQueryS.forEach(toolQueryVO -> {
|
|
|
+ DashboardQueryDTO toolQueryDTO = new DashboardQueryDTO();
|
|
|
+ BeanUtils.copyProperties(queryDTO, toolQueryDTO);
|
|
|
+ toolQueryDTO.setProjectId(projectQueryVO.getProjectId());
|
|
|
+ toolQueryDTO.setToolId(toolQueryVO.getId());
|
|
|
+ AnalysisDataDTO analysisData = this.getAnalysisData(toolQueryDTO);
|
|
|
+ dashExportXlsxDTOS.add(new DashExportXlsxDTO(queryDTO.getIsCN() ? toolQueryVO.getCnName() : toolQueryVO.getEnName(),
|
|
|
+ analysisData.getSuccessfulRequest().toString(),
|
|
|
+ analysisData.getErrorRequest().toString(),
|
|
|
+ analysisData.getErrorRatio(),
|
|
|
+ analysisData.getAverageProcessTime()));
|
|
|
+ });
|
|
|
+ DashboardQueryDTO toolQueryDTO = new DashboardQueryDTO();
|
|
|
+ BeanUtils.copyProperties(queryDTO, toolQueryDTO);
|
|
|
+ toolQueryDTO.setProjectId(projectQueryVO.getProjectId());
|
|
|
+ AnalysisDataDTO analysisData = this.getAnalysisData(toolQueryDTO);
|
|
|
+
|
|
|
+ dashExportXlsxDTOS.add(new DashExportXlsxDTO("", "", "", "", ""));
|
|
|
+ if (queryDTO.getIsCN()){
|
|
|
+ dashExportXlsxDTOS.add(new DashExportXlsxDTO("累计",
|
|
|
+ analysisData.getSuccessfulRequest().toString(),
|
|
|
+ analysisData.getErrorRequest().toString(),
|
|
|
+ analysisData.getErrorRatio(),
|
|
|
+ analysisData.getAverageProcessTime()));
|
|
|
+ dashExportXlsxDTOS.add(new DashExportXlsxDTO("注:数据为所选期间内的累积数据。", "", "", "", ""));
|
|
|
+ }else {
|
|
|
+ dashExportXlsxDTOS.add(new DashExportXlsxDTO("Total",
|
|
|
+ analysisData.getSuccessfulRequest().toString(),
|
|
|
+ analysisData.getErrorRequest().toString(),
|
|
|
+ analysisData.getErrorRatio(),
|
|
|
+ analysisData.getAverageProcessTime()));
|
|
|
+ dashExportXlsxDTOS.add(new DashExportXlsxDTO("Note: The count is the accumulated data within the time range.", "", "", "", ""));
|
|
|
+ }
|
|
|
+
|
|
|
+ excelWriter.write(dashExportXlsxDTOS, writeSheet);
|
|
|
+ });
|
|
|
+ excelWriter.finish();
|
|
|
} catch (Exception e) {
|
|
|
// 重置response
|
|
|
response.reset();
|
|
@@ -664,6 +773,4 @@ public class BackgroundConvertDataServiceImpl extends ServiceImpl<BackgroundConv
|
|
|
return queryWrapper;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-
|
|
|
}
|