Commit dbbc8b60 by 黄明步

修改导出年月日数据相关

parent cd4e8948
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
<elasticsearch.version>8.9.1</elasticsearch.version> <elasticsearch.version>8.9.1</elasticsearch.version>
<alibaba.druid.version>1.1.22</alibaba.druid.version> <alibaba.druid.version>1.1.22</alibaba.druid.version>
<jackson.version>2.12.3</jackson.version> <jackson.version>2.12.3</jackson.version>
<easyexcel.version>3.2.1</easyexcel.version>
</properties> </properties>
<dependencyManagement> <dependencyManagement>
<dependencies> <dependencies>
...@@ -125,25 +126,6 @@ ...@@ -125,25 +126,6 @@
<groupId>org.apache.commons</groupId> <groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId> <artifactId>commons-pool2</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
<dependency> <dependency>
<groupId>mysql</groupId> <groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId> <artifactId>mysql-connector-java</artifactId>
...@@ -210,6 +192,12 @@ ...@@ -210,6 +192,12 @@
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>${easyexcel.version}</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
......
package com.gxmailu.ocrCloudPlatform.controller; package com.gxmailu.ocrCloudPlatform.controller;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.util.MapUtils;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONArray;
import com.gxmailu.ocrCloudPlatform.dto.RecordDto; import com.gxmailu.ocrCloudPlatform.dto.RecordDto;
import com.gxmailu.ocrCloudPlatform.entity.AppAbilityRecordAll; import com.gxmailu.ocrCloudPlatform.entity.AppAbilityRecordAll;
import com.gxmailu.ocrCloudPlatform.entity.DeriveCallRecord; import com.gxmailu.ocrCloudPlatform.entity.DeriveCallRecord;
import com.gxmailu.ocrCloudPlatform.excel.handler.ExcelFillCellMergeStrategy;
import com.gxmailu.ocrCloudPlatform.excel.model.CallRecordModel;
import com.gxmailu.ocrCloudPlatform.excel.utils.ExcelStyleUtils;
import com.gxmailu.ocrCloudPlatform.service.AppAbilityRecordAllService; import com.gxmailu.ocrCloudPlatform.service.AppAbilityRecordAllService;
import com.gxmailu.ocrCloudPlatform.utils.DateUtils; import com.gxmailu.ocrCloudPlatform.utils.DateUtils;
import com.gxmailu.ocrCloudPlatform.vo.Result; import com.gxmailu.ocrCloudPlatform.vo.Result;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.*; import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
...@@ -47,135 +61,63 @@ public class AppAbilityRecordAllController { ...@@ -47,135 +61,63 @@ public class AppAbilityRecordAllController {
public void deriveCallRecord(String yearMonthDay, String endYearMonthDay, HttpServletResponse response) { public void deriveCallRecord(String yearMonthDay, String endYearMonthDay, HttpServletResponse response) {
//设置到新的对象上,用以下面的文件名称处理 //设置到新的对象上,用以下面的文件名称处理
String newYearMonthDay = yearMonthDay; String newYearMonthDay = yearMonthDay;
if(StringUtils.isNotEmpty(yearMonthDay)){ if(StrUtil.isNotBlank(yearMonthDay)){
int count = yearMonthDay.split("-").length; int count = yearMonthDay.split("-").length;
switch (count){ switch (count){
case 1: case 1:
//只有年,补01-01 DateTime year = DateUtil.parse(yearMonthDay, "yyyy");
if(StringUtils.isEmpty(endYearMonthDay)){ yearMonthDay = DateUtil.beginOfYear(year).toString();
//默认到当年的12月31日 endYearMonthDay = DateUtil.endOfYear(year).toString();
endYearMonthDay = yearMonthDay + "-12-31";
}
yearMonthDay = yearMonthDay + "-01-01";
break; break;
case 2: case 2:
//只有年月,补01
if(StringUtils.isEmpty(endYearMonthDay)){ DateTime yearMonth = DateUtil.parse(yearMonthDay, "yyyy-MM");
//默认到当月的31日(小月也按31日计算) yearMonthDay = DateUtil.beginOfMonth(yearMonth).toString();
endYearMonthDay = yearMonthDay + "-31"; endYearMonthDay = DateUtil.endOfMonth(yearMonth).toString();
}
yearMonthDay = yearMonthDay + "-01";
break; break;
case 3: case 3:
if(StringUtils.isEmpty(endYearMonthDay)){ DateTime yearMonthDayDateTime = DateUtil.parse(yearMonthDay, "yyyy-MM-dd");
endYearMonthDay = yearMonthDay; yearMonthDay = DateUtil.beginOfDay(yearMonthDayDateTime).toString();
} endYearMonthDay = DateUtil.endOfDay(yearMonthDayDateTime).toString();
break; break;
} }
System.out.println("yearMonthDay==========" + yearMonthDay + ";endYearMonthDay==========" + endYearMonthDay); System.out.println("yearMonthDay==========" + yearMonthDay + ";endYearMonthDay==========" + endYearMonthDay);
yearMonthDay = DateUtils.dateToStringShort(DateUtils.stringToDateShort(yearMonthDay));
//因为sql中使用的是between,其特性是含头不含尾,所以需要给区间末的时间加1天
Calendar calendar = new GregorianCalendar();
calendar.setTime(DateUtils.stringToDateShort(endYearMonthDay));
calendar.add(Calendar.DATE, 1);
endYearMonthDay = DateUtils.dateToStringShort(calendar.getTime());
} }
List<DeriveCallRecord> callRecordList = this.recordAllService.selectCallRecord(yearMonthDay, endYearMonthDay);
Assert.notNull(callRecordList, "未找到相关数据");
try{ try{
//创建一个workBook List<CallRecordModel> callRecordList = this.recordAllService.selectCallRecord(yearMonthDay, endYearMonthDay);
HSSFWorkbook workbook = new HSSFWorkbook(); Assert.notNull(callRecordList, "未找到相关数据");
//创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = workbook.createCellStyle();
//设置边框
//下边框
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
//左边框
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
//上边框
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
//右边框
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
//自动换行
style.setWrapText(true);
//创建一个居中格式
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//上下居中
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
//设置字体
HSSFFont font = workbook.createFont();
font.setFontName("宋体");
style.setFont(font);
//创建一个工作簿
HSSFSheet sheet = workbook.createSheet();
List<Map> mapList = JSONArray.parseArray(com.alibaba.fastjson.JSONObject.toJSONString(callRecordList), Map.class);
//占一行表头
HSSFRow initial_row = sheet.createRow(0);
//设置表头
HSSFCell zero = initial_row.createCell(0);
zero.setCellStyle(style);
zero.setCellValue("法院名称");
HSSFCell one = initial_row.createCell(1);
one.setCellStyle(style);
one.setCellValue("调用厂家名称");
HSSFCell tow = initial_row.createCell(2);
tow.setCellStyle(style);
tow.setCellValue("能力ID");
HSSFCell three = initial_row.createCell(3);
three.setCellStyle(style);
three.setCellValue("调用次数");
//以 mapList 的元素个数为行基础,每一个元素就是一行
for(int row_start = 0, row_end = mapList.size(); row_start < row_end; row_start ++){
TreeMap<String, Object> map = new TreeMap<String, Object>(mapList.get(row_start));
//判断是否存在行,没有则创建
HSSFRow row = sheet.getRow(row_start + 1);
if(row == null){
row = sheet.createRow(row_start + 1);
}
//然后以 map 中字段的数量为列基础,每一个字段就是一列
int column = 0;
for(Object key : map.keySet()){
//判断是否存在列,没有则创建
HSSFCell cell = row.getCell(column);
if(cell == null){
cell = row.createCell(column);
}
//数据写入
cell.setCellValue(String.valueOf(map.get(key)));
//设置样式
cell.setCellStyle(style);
//加列
column ++;
}
}
response.reset();
String fileName = StringUtils.isEmpty(newYearMonthDay) ? "各法院调用统计.xlsx" : "各法院" + newYearMonthDay + "调用统计.xlsx"; String fileName = StringUtils.isEmpty(newYearMonthDay) ? "各法院调用统计.xlsx" : "各法院" + newYearMonthDay + "调用统计.xlsx";
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setHeader("Content-disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8")); response.setCharacterEncoding("utf-8");
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName);
OutputStream outputStream = new BufferedOutputStream(response.getOutputStream()); // 设置单元格样式策略
response.setContentType("application/octet-stream"); HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(ExcelStyleUtils.getHeadStyle(), ExcelStyleUtils.getContentStyle());
response.flushBuffer(); EasyExcel.write(response.getOutputStream(), CallRecordModel.class)
.sheet("导出数据")
workbook.write(outputStream); .head(CallRecordModel.class)
outputStream.flush(); // 自动行宽策略
outputStream.close(); .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
// 自定义合并策略
.registerWriteHandler(new ExcelFillCellMergeStrategy(0, 0, new int[]{0, 1}))
// 设置单元格样式
.registerWriteHandler(horizontalCellStyleStrategy)
.doWrite(callRecordList);
}catch (Exception e){ }catch (Exception e){
log.error("导出异常{0}", e.fillInStackTrace()); log.error("导出异常", e);
// 重置response
response.reset();
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
Map<String, String> map = MapUtils.newHashMap();
map.put("status", "500");
map.put("message", "数据过大,导出文件失败");
try {
response.getWriter().println(JSON.toJSONString(map));
} catch (IOException ex) {
log.error("", ex);
}
} }
} }
......
package com.gxmailu.ocrCloudPlatform.excel.converter;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.gxmailu.ocrCloudPlatform.entity.ConfigAbility;
import com.gxmailu.ocrCloudPlatform.service.ConfigAbilityService;
import java.util.List;
/**
* 识别能力名称转换器
*
* @author Hmb
* @version 1.0.0
* @since 2024/1/21 14:35:30
*/
public class AbilityNameConverter implements Converter<Long> {
/**
* Convert Java objects to excel objects
*
* @param value Java Data.NotNull.
* @param contentProperty Content property.Nullable.
* @param globalConfiguration Global configuration.NotNull.
* @return Data to put into a Excel
*/
@Override
public WriteCellData<?> convertToExcelData(Long value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
ConfigAbilityService abilityService = SpringUtil.getBean(ConfigAbilityService.class);
List<ConfigAbility> abilityList = abilityService.selectAll();
for (ConfigAbility configAbility : abilityList) {
if (configAbility.getId().equals(value.toString())) {
String name = configAbility.getName();
return new WriteCellData<>(StrUtil.isNotBlank(name) ? name : "未知");
}
}
return new WriteCellData<>("未知");
}
}
package com.gxmailu.ocrCloudPlatform.excel.converter;
import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.gxmailu.ocrCloudPlatform.entity.Court;
import com.gxmailu.ocrCloudPlatform.service.CourtService;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
/**
* 法院名称转换器
*
* @author Hmb
* @version 1.0.0
* @since 2024/1/21 14:35:30
*/
public class CourtNameConverter implements Converter<String> {
/**
* Convert Java objects to excel objects
*
* @param value Java Data.NotNull.
* @param contentProperty Content property.Nullable.
* @param globalConfiguration Global configuration.NotNull.
* @return Data to put into a Excel
*/
@Override
public WriteCellData<?> convertToExcelData(String value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
CourtService courtService = SpringUtil.getBean(CourtService.class);
List<Court> courtList = courtService.getCourtList();
Optional<String> resultName = courtList.stream()
.filter(court -> Arrays.stream(court.getIpScope().split(";")).anyMatch(value::contains))
.map(Court::getName)
.findFirst();
return resultName.map(WriteCellData::new).orElseGet(() -> new WriteCellData<>("未知"));
}
}
package com.gxmailu.ocrCloudPlatform.excel.converter;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.gxmailu.ocrCloudPlatform.entity.ConfigApplication;
import com.gxmailu.ocrCloudPlatform.service.ConfigApplicationService;
import java.util.List;
/**
* 调用厂商名称转换器
*
* @author Hmb
* @version 1.0.0
* @since 2024/1/21 14:35:30
*/
public class VendorNameConverter implements Converter<Long> {
/**
* Convert Java objects to excel objects
*
* @param value Java Data.NotNull.
* @param contentProperty Content property.Nullable.
* @param globalConfiguration Global configuration.NotNull.
* @return Data to put into a Excel
*/
@Override
public WriteCellData<?> convertToExcelData(Long value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
ConfigApplicationService applicationService = SpringUtil.getBean(ConfigApplicationService.class);
List<ConfigApplication> applicationList = applicationService.getConfigApplicationAll();
for (ConfigApplication configApplication : applicationList) {
if (configApplication.getId().equals(value)) {
String description = configApplication.getDescription();
return new WriteCellData<>(StrUtil.isNotBlank(description) ? description : "未知");
}
}
return new WriteCellData<>("未知");
}
}
package com.gxmailu.ocrCloudPlatform.excel.handler;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import java.util.List;
import java.util.Objects;
/**
* 自定义合并策略实现类
*
* @author Hmb
* @version 1.0.0
* @since 2024/1/21 12:44:19
*/
public class ExcelFillCellMergeStrategy implements CellWriteHandler {
/**
* 标杆列下标,根据标杆列是否合并来判断后面的单元格是否需要合并
*/
private int standardColumnIndex;
/**
* 合并行的开始下标,即从该行开始向下合并
*/
private int mergeRowIndex;
/**
* 需要合并的列下标,0开始
*/
private int[] mergeColumnIndex;
public ExcelFillCellMergeStrategy() {
}
public ExcelFillCellMergeStrategy(int standardColumnIndex, int mergeRowIndex, int[] mergeColumnIndex) {
this.standardColumnIndex = standardColumnIndex;
this.mergeRowIndex = mergeRowIndex;
this.mergeColumnIndex = mergeColumnIndex;
}
@Override
public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Head head, Integer columnIndex, Integer relativeRowIndex, Boolean isHead) {
}
@Override
public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
}
@Override
public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<WriteCellData<?>> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
// 校验:如果当前是表头,或者是第一行,则不处理。
if (isHead || relativeRowIndex == 0) {
return;
}
CellWriteHandler.super.afterCellDispose(writeSheetHolder, writeTableHolder, cellDataList, cell, head, relativeRowIndex, isHead);
// 当前行
int curRowIndex = cell.getRowIndex();
// 当前列
int curColIndex = cell.getColumnIndex();
if (curRowIndex > mergeRowIndex) {
for (int columnIndex : mergeColumnIndex) {
if (curColIndex == columnIndex) {
mergeWithPrevRow(writeSheetHolder, cell, curRowIndex, curColIndex);
break;
}
}
}
}
private void mergeWithPrevRow(WriteSheetHolder writeSheetHolder, Cell cell, int curRowIndex, int curColIndex) {
// 获取当前行数据
Object curData = cell.getCellType() == CellType.STRING ? cell.getStringCellValue() : cell.getNumericCellValue();
// 获取上一行数据
Row preRow = cell.getSheet().getRow(curRowIndex - 1);
if (preRow == null) {
// 当获取不到上一行数据时,使用缓存sheet中数据
preRow = writeSheetHolder.getCachedSheet().getRow(curRowIndex - 1);
}
// 获取上一行当前列数据
Object preData = null;
if (preRow != null) {
Cell preCell = preRow.getCell(curColIndex);
if (preCell != null) {
if (preCell.getCellType() == CellType.STRING) {
preData = preCell.getStringCellValue();
} else if (preCell.getCellType() == CellType.NUMERIC) {
preData = preCell.getNumericCellValue();
}
}
}
// 获取上一行标杆列数据
Cell preRowFirstCell = preRow.getCell(standardColumnIndex);
Object preRowFirstData = preRowFirstCell.getCellType() == CellType.STRING ? preRowFirstCell.getStringCellValue() : preRowFirstCell.getNumericCellValue();
// 获取本行数据
Row curRowFirst = cell.getSheet().getRow(curRowIndex);
if (curRowFirst == null) {
curRowFirst = writeSheetHolder.getCachedSheet().getRow(curRowIndex);
}
// 获取本行标杆列数据
Cell curRowFirstCell = curRowFirst.getCell(standardColumnIndex);
Object curRowFirstData = null;
if (curRowFirstCell != null) {
curRowFirstData = curRowFirstCell.getCellType() == CellType.STRING ? curRowFirstCell.getStringCellValue() : curRowFirstCell.getNumericCellValue();
}
// 当前列为标杆列并且上下两个单元格一致则合并 或者 当前列不为标杆列并且上下两个单元格一致则合并
boolean isTrue = (curColIndex == standardColumnIndex && Objects.equals(preData, curData)) || (curColIndex != standardColumnIndex && preRowFirstData.equals(curRowFirstData) && Objects.equals(preData, curData));
if (isTrue) {
Sheet sheet = writeSheetHolder.getSheet();
List<CellRangeAddress> mergeRegions = sheet.getMergedRegions();
boolean isMerged = false;
for (int i = 0; i < mergeRegions.size() && !isMerged; i++) {
CellRangeAddress cellRangeAddr = mergeRegions.get(i);
// 若上一个单元格已经被合并,则先移出原有的合并单元,再重新添加合并单元
if (cellRangeAddr.isInRange(curRowIndex - 1, curColIndex)) {
sheet.removeMergedRegion(i);
cellRangeAddr.setLastRow(curRowIndex);
sheet.addMergedRegion(cellRangeAddr);
isMerged = true;
}
}
// 若上一个单元格未被合并,则新增合并单元
if (!isMerged) {
CellRangeAddress cellRangeAddress = new CellRangeAddress(curRowIndex - 1, curRowIndex, curColIndex, curColIndex);
sheet.addMergedRegion(cellRangeAddress);
}
}
}
}
package com.gxmailu.ocrCloudPlatform.excel.model;
import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import lombok.Data;
import java.io.Serializable;
/**
* @author Hmb
* @version 1.0.0
* @since 2024/1/20 21:05:04
*/
@Data
public class CallRecordModel implements Serializable {
private static final long serialVersionUID = 1L;
@ExcelIgnore
private String ip;
@ExcelIgnore
private Long applicationId;
@ExcelIgnore
private Long abilityId;
@ExcelIgnore
@ExcelProperty(value = "法院总调用次数")
private Long ipDocCount;
@ExcelIgnore
@ExcelProperty(value = "总调用次数")
private Integer callCount;
@ExcelIgnore
@ExcelProperty(value = "用户调用次数")
private Long userDocCount;
@ExcelProperty(value = "法院名称", index = 0)
private String courtName;
@ExcelProperty(value = "业务厂商", index = 1)
private String businessVendorName;
@ExcelProperty(value = "识别能力", index = 2)
private String abilityName;
@ExcelProperty(value = "调用次数", index = 3)
private long abilityDocCount;
@ExcelProperty(value = "总识别页数", index = 4)
private Integer fileCount;
}
package com.gxmailu.ocrCloudPlatform.excel.utils;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
/**
* @author Hmb
* @version 1.0.0
* @since 2024/1/20 20:54:43
*/
public class ExcelStyleUtils {
/**
* 标题样式
*
* @return WriteCellStyle
*/
public static WriteCellStyle getHeadStyle() {
// 头的策略
WriteCellStyle headWriteCellStyle = new WriteCellStyle();
// 背景颜色
// headWriteCellStyle.setFillForegroundColor(IndexedColors.LIGHT_TURQUOISE1.getIndex());
// headWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
// 字体
WriteFont headWriteFont = new WriteFont();
headWriteFont.setFontName("宋体");// 设置字体名字
headWriteFont.setFontHeightInPoints((short) 14);// 设置字体大小
headWriteFont.setBold(true);// 字体加粗
headWriteCellStyle.setWriteFont(headWriteFont); // 在样式用应用设置的字体;
// 样式
headWriteCellStyle.setBorderBottom(BorderStyle.THIN);// 设置底边框;
headWriteCellStyle.setBottomBorderColor((short) 0);// 设置底边框颜色;
headWriteCellStyle.setBorderLeft(BorderStyle.THIN); // 设置左边框;
headWriteCellStyle.setLeftBorderColor((short) 0);// 设置左边框颜色;
headWriteCellStyle.setBorderRight(BorderStyle.THIN);// 设置右边框;
headWriteCellStyle.setRightBorderColor((short) 0);// 设置右边框颜色;
headWriteCellStyle.setBorderTop(BorderStyle.THIN);// 设置顶边框;
headWriteCellStyle.setTopBorderColor((short) 0); // 设置顶边框颜色;
headWriteCellStyle.setWrapped(true); // 设置自动换行;
headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);// 设置水平对齐的样式为居中对齐;
headWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER); // 设置垂直对齐的样式为居中对齐;
headWriteCellStyle.setShrinkToFit(true);// 设置文本收缩至合适
return headWriteCellStyle;
}
/**
* 内容样式
*
* @return WriteCellStyle
*/
public static WriteCellStyle getContentStyle() {
// 内容的策略
WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
// 背景绿色
// 这里需要指定 FillPatternType 为FillPatternType.SOLID_FOREGROUND 不然无法显示背景颜色.头默认了 FillPatternType所以可以不指定
// contentWriteCellStyle.setFillForegroundColor(IndexedColors.PALE_BLUE.getIndex());
// contentWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
// 设置字体
WriteFont contentWriteFont = new WriteFont();
contentWriteFont.setFontHeightInPoints((short) 12);// 设置字体大小
contentWriteFont.setFontName("宋体"); // 设置字体名字
contentWriteCellStyle.setWriteFont(contentWriteFont);// 在样式用应用设置的字体;
// 设置样式
contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);// 设置底边框;
contentWriteCellStyle.setBottomBorderColor((short) 0);// 设置底边框颜色;
contentWriteCellStyle.setBorderLeft(BorderStyle.THIN); // 设置左边框;
contentWriteCellStyle.setLeftBorderColor((short) 0);// 设置左边框颜色;
contentWriteCellStyle.setBorderRight(BorderStyle.THIN);// 设置右边框;
contentWriteCellStyle.setRightBorderColor((short) 0);// 设置右边框颜色;
contentWriteCellStyle.setBorderTop(BorderStyle.THIN);// 设置顶边框;
contentWriteCellStyle.setTopBorderColor((short) 0); ///设置顶边框颜色;
contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);// 水平居中
contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);// 垂直居中
contentWriteCellStyle.setWrapped(true); // 设置自动换行;
// contentWriteCellStyle.setShrinkToFit(true);// 设置文本收缩至合适
return contentWriteCellStyle;
}
}
\ No newline at end of file
...@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.extension.service.IService; ...@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.extension.service.IService;
import com.gxmailu.ocrCloudPlatform.dto.RecordDto; import com.gxmailu.ocrCloudPlatform.dto.RecordDto;
import com.gxmailu.ocrCloudPlatform.entity.AppAbilityRecordAll; import com.gxmailu.ocrCloudPlatform.entity.AppAbilityRecordAll;
import com.gxmailu.ocrCloudPlatform.entity.DeriveCallRecord; import com.gxmailu.ocrCloudPlatform.entity.DeriveCallRecord;
import com.gxmailu.ocrCloudPlatform.excel.model.CallRecordModel;
import com.gxmailu.ocrCloudPlatform.vo.Result; import com.gxmailu.ocrCloudPlatform.vo.Result;
import java.io.IOException; import java.io.IOException;
...@@ -20,7 +21,7 @@ import java.util.List; ...@@ -20,7 +21,7 @@ import java.util.List;
*/ */
public interface AppAbilityRecordAllService extends IService<AppAbilityRecordAll> { public interface AppAbilityRecordAllService extends IService<AppAbilityRecordAll> {
List<DeriveCallRecord> selectCallRecord(String yearMonthDay, String endYearMonthDay); List<CallRecordModel> selectCallRecord(String yearMonthDay, String endYearMonthDay) throws IOException;
Result addRecord(AppAbilityRecordAll recordAll); Result addRecord(AppAbilityRecordAll recordAll);
......
package com.gxmailu.ocrCloudPlatform.service; package com.gxmailu.ocrCloudPlatform.service;
import com.gxmailu.ocrCloudPlatform.entity.Court;
import com.gxmailu.ocrCloudPlatform.vo.CourtVO; import com.gxmailu.ocrCloudPlatform.vo.CourtVO;
import com.gxmailu.ocrCloudPlatform.vo.Result; import com.gxmailu.ocrCloudPlatform.vo.Result;
import java.util.List;
public interface CourtService { public interface CourtService {
Result courtPage(CourtVO courtVO); Result courtPage(CourtVO courtVO);
...@@ -17,4 +20,6 @@ public interface CourtService { ...@@ -17,4 +20,6 @@ public interface CourtService {
Result getCourtInfo(String courtCode); Result getCourtInfo(String courtCode);
List<Court> getCourtList();
} }
...@@ -14,10 +14,13 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; ...@@ -14,10 +14,13 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxmailu.ocrCloudPlatform.constant.RedisConstant; import com.gxmailu.ocrCloudPlatform.constant.RedisConstant;
import com.gxmailu.ocrCloudPlatform.dto.RecordDto; import com.gxmailu.ocrCloudPlatform.dto.RecordDto;
import com.gxmailu.ocrCloudPlatform.entity.*; import com.gxmailu.ocrCloudPlatform.entity.*;
import com.gxmailu.ocrCloudPlatform.excel.converter.CourtNameConverter;
import com.gxmailu.ocrCloudPlatform.excel.model.CallRecordModel;
import com.gxmailu.ocrCloudPlatform.mapper.*; import com.gxmailu.ocrCloudPlatform.mapper.*;
import com.gxmailu.ocrCloudPlatform.service.AppAbilityRecordAllService; import com.gxmailu.ocrCloudPlatform.service.AppAbilityRecordAllService;
import com.gxmailu.ocrCloudPlatform.service.ConfigAbilityService; import com.gxmailu.ocrCloudPlatform.service.ConfigAbilityService;
import com.gxmailu.ocrCloudPlatform.service.ConfigApplicationService; import com.gxmailu.ocrCloudPlatform.service.ConfigApplicationService;
import com.gxmailu.ocrCloudPlatform.service.CourtService;
import com.gxmailu.ocrCloudPlatform.utils.SnowFlakeUtil; import com.gxmailu.ocrCloudPlatform.utils.SnowFlakeUtil;
import com.gxmailu.ocrCloudPlatform.vo.AppAbilityRecordVo; import com.gxmailu.ocrCloudPlatform.vo.AppAbilityRecordVo;
import com.gxmailu.ocrCloudPlatform.vo.BrokenLineData; import com.gxmailu.ocrCloudPlatform.vo.BrokenLineData;
...@@ -71,10 +74,13 @@ public class AppAbilityRecordAllServiceImpl extends ServiceImpl<AppAbilityRecord ...@@ -71,10 +74,13 @@ public class AppAbilityRecordAllServiceImpl extends ServiceImpl<AppAbilityRecord
@Resource @Resource
private ConfigApplicationService configApplicationService; private ConfigApplicationService configApplicationService;
@Resource
private CourtService courtService;
@Override @Override
public List<DeriveCallRecord> selectCallRecord(String yearMonthDay, String endYearMonthDay) { public List<CallRecordModel> selectCallRecord(String yearMonthDay, String endYearMonthDay) throws IOException {
return this.deriveCallRecordMapper.selectCallRecord(yearMonthDay, endYearMonthDay); return elasticSearchService.selectCallRecord(yearMonthDay, endYearMonthDay);
} }
@Override @Override
...@@ -173,115 +179,9 @@ public class AppAbilityRecordAllServiceImpl extends ServiceImpl<AppAbilityRecord ...@@ -173,115 +179,9 @@ public class AppAbilityRecordAllServiceImpl extends ServiceImpl<AppAbilityRecord
@Override @Override
public Object getCourtUseSum() { public Object getCourtUseSum() {
List<Court> courtList; List<Court> courtList = courtService.getCourtList();
Object data = redisService.getValue(RedisConstant.COURT_LIST);
if (data != null) {
// JSONArray array = JSONUtil.parseArray(data);
// courtList = new ArrayList<>();
// for (Object o : array) {
// courtList.add(JSONUtil.parseObj(o).toBean(Court.class));
// }
courtList = JSONUtil.toList(data.toString(), Court.class);
} else {
courtList = courtMapper.selectList(new LambdaQueryWrapper<Court>().isNotNull(Court::getIpScope));
redisService.set(RedisConstant.COURT_LIST, courtList, 1L, TimeUnit.DAYS);
}
// JSONArray array = new JSONArray();
// try {
// SearchResponse<Void> searchResponse = client.search(srBuilder -> srBuilder.index(APP_ABILITY_RECORD_INDEX).query(queryBuilder -> queryBuilder.bool(boolQuery -> {
// courtList.forEach(court -> boolQuery.should(q -> q.prefix(pq -> pq.field("ip.keyword").value(court.getIpScope()))));
// return boolQuery;
// })).aggregations("count", agg -> agg.terms(TermsAggregation.of(s -> s.field("ip.keyword")))).trackTotalHits(tb -> tb.enabled(true)), Void.class);
//
// StringTermsAggregate stringTermsAggregate = searchResponse.aggregations()
// .get("count")
// .sterms();
//
// Map<String, JSONObject> resultMap = new HashMap<>();
// Buckets<StringTermsBucket> buckets = stringTermsAggregate.buckets();
// buckets.array().forEach(bucket -> {
// for (Court court : courtList) {
// if (bucket.key().stringValue().startsWith(court.getIpScope())) {
// // 若此 IP 范围已存在,则取出并更新 sum
// if (resultMap.containsKey(court.getIpScope())) {
// JSONObject existingObject = resultMap.get(court.getIpScope());
// existingObject.set("sum", existingObject.getInt("sum") + bucket.docCount());
// } else {
// // 否则,创建新的 JSONObject 并加入 resultMap
// JSONObject object = new JSONObject();
// object.set("name", court.getName());
// object.set("sortName", court.getForSort());
// object.set("sum", bucket.docCount());
// resultMap.put(court.getIpScope(), object);
// }
// break;
// }
// }
// });
//
// array = new JSONArray(resultMap.values());
// // 以各院总数降序排列
// array.sort(Comparator.comparing(obj -> ((JSONObject) obj).getInt("sum")).reversed());
// } catch (IOException e) {
// log.error("从ES中查询各法院调用量发生异常!", e);
// }
return elasticSearchService.getCourtUseSum(courtList); return elasticSearchService.getCourtUseSum(courtList);
} }
// public Object getCourtUseSum() {
// List<Court> courtList;
// Object data = redisService.getValue("courtList");
// if (data != null) {
// JSONArray array = JSONUtil.parseArray(data);
// courtList = new ArrayList<>();
// for (Object o : array) {
// courtList.add(JSONUtil.parseObj(o).toBean(Court.class));
// }
// } else {
// courtList = courtMapper.selectList(new LambdaQueryWrapper<Court>().isNotNull(Court::getIpScope));
// redisService.set("courtList", courtList, 5L, TimeUnit.MINUTES);
// }
// JSONArray array = new JSONArray();
// courtList.forEach(court -> {
// JSONObject object = new JSONObject();
// Long sum = recordAllMapper.selectCount(new LambdaQueryWrapper<AppAbilityRecordAll>().likeRight(AppAbilityRecordAll::getIp, court.getIpScope()));
// if (sum > 0) {
// object.set("name", court.getName());
// object.set("sortName", court.getForSort());
// object.set("sum", sum);
// array.add(object);
// }
// });
// // 以各院总数降序排列
// Comparator.comparing(obj -> ((JSONObject) obj).getInt("number")).reversed();
// return array;
// }
// @Override
// public Object getCourtUseInYear(Integer year, String courtName) {
// Date date;
// if (year == null) {
// date = new Date();
// } else {
// String str = year + "-01-01 00:00:00";
// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// try {
// date = sdf.parse(str);
// } catch (ParseException e) {
// log.error("时间格式转换发生异常!");
// return null;
// }
// }
// DateTime start = DateUtil.beginOfYear(date);
// DateTime end = DateUtil.endOfYear(date);
// JSONObject object = new JSONObject();
// Court court = getCourtInfoByName(courtName);
// LambdaQueryWrapper<AppAbilityRecordAll> wrapper = new LambdaQueryWrapper<AppAbilityRecordAll>().between(AppAbilityRecordAll::getCreatedTime, start, end);
// if (court != null && !StringUtils.isEmpty(court.getIpScope())) {
// wrapper.apply("ip like concat({0},'%')", court.getIpScope());
// }
// object.set("count", recordAllMapper.selectCount(wrapper));
// return object;
// }
@Override @Override
public Object getCourtUseInYear(Integer year, String courtName) { public Object getCourtUseInYear(Integer year, String courtName) {
...@@ -349,51 +249,6 @@ public class AppAbilityRecordAllServiceImpl extends ServiceImpl<AppAbilityRecord ...@@ -349,51 +249,6 @@ public class AppAbilityRecordAllServiceImpl extends ServiceImpl<AppAbilityRecord
} }
} }
// public Result getBrokenLineData(String date) {
// try {
// Date nowDate = new Date();
// String newDateStr = DateUtil.format(nowDate, "yyyy-MM-dd");
// if (StringUtils.isEmpty(date)) {
// date = DateUtil.format(nowDate, "yyyy-MM-dd");
// } else {
// if (date.equals("yesterday")) {
// Calendar cal = Calendar.getInstance();
// cal.setTime(nowDate);
// cal.add(Calendar.DATE, -1);
// date = DateUtil.format(cal.getTime(), "yyyy-MM-dd");
// } else {
// date = DateUtil.format(DateUtil.parse(date), "yyyy-MM-dd");
// }
// }
// List<BrokenLineData> brokenLineData = null;
// if (date.equals(newDateStr)) {
// brokenLineData = recordAllMapper.selectBrokenLineData(date);
// } else {
// Object dateDataObj = redisService.getValue("brokenLineData-" + date);
// if (dateDataObj == null) {
// brokenLineData = recordAllMapper.selectBrokenLineData(date);
// redisService.set("brokenLineData-" + date, brokenLineData, 1, TimeUnit.DAYS);
// } else {
// brokenLineData = com.alibaba.fastjson.JSONArray.parseArray(dateDataObj.toString(), BrokenLineData.class);
// if (brokenLineData.size() > 0) {
// if (!brokenLineData.get(0).getTime().startsWith(newDateStr)) {
// brokenLineData = recordAllMapper.selectBrokenLineData(date);
// redisService.set("brokenLineData-" + date, brokenLineData, 1, TimeUnit.DAYS);
// }
// } else {
// brokenLineData = recordAllMapper.selectBrokenLineData(date);
// redisService.set("brokenLineData-" + date, brokenLineData, 1, TimeUnit.DAYS);
// }
// }
// }
// // System.out.println(brokenLineData);
// return Result.success("获取折线数据成功", brokenLineData);
// } catch (Exception e) {
// log.error("获取折线数据失败", e);
// return Result.serverError("获取折线数据失败", e);
// }
// }
public Result getBrokenLineData(String date) { public Result getBrokenLineData(String date) {
try { try {
DateTime nowDateTime = DateTime.now(); DateTime nowDateTime = DateTime.now();
......
package com.gxmailu.ocrCloudPlatform.service.impl; package com.gxmailu.ocrCloudPlatform.service.impl;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
...@@ -16,6 +17,7 @@ import org.springframework.beans.factory.annotation.Autowired; ...@@ -16,6 +17,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.List; import java.util.List;
import java.util.concurrent.TimeUnit;
@Service @Service
@Slf4j @Slf4j
...@@ -124,4 +126,18 @@ public class CourtServiceImpl implements CourtService { ...@@ -124,4 +126,18 @@ public class CourtServiceImpl implements CourtService {
} }
return getAllCourt(); return getAllCourt();
} }
@Override
public List<Court> getCourtList() {
List<Court> courtList;
Object data = redisService.getValue(RedisConstant.COURT_LIST);
if (data != null) {
courtList = JSONUtil.toList(data.toString(), Court.class);
} else {
courtList = courtMapper.selectList(new LambdaQueryWrapper<Court>().isNotNull(Court::getIpScope));
redisService.set(RedisConstant.COURT_LIST, courtList, 1L, TimeUnit.DAYS);
}
return courtList;
}
} }
...@@ -22,15 +22,18 @@ import com.baomidou.mybatisplus.core.toolkit.Wrappers; ...@@ -22,15 +22,18 @@ import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.gxmailu.ocrCloudPlatform.dto.RecordDto; import com.gxmailu.ocrCloudPlatform.dto.RecordDto;
import com.gxmailu.ocrCloudPlatform.entity.AppAbilityRecord; import com.gxmailu.ocrCloudPlatform.entity.*;
import com.gxmailu.ocrCloudPlatform.entity.AppAbilityRecordAll; import com.gxmailu.ocrCloudPlatform.excel.model.CallRecordModel;
import com.gxmailu.ocrCloudPlatform.entity.Court;
import com.gxmailu.ocrCloudPlatform.entity.ServerInfo;
import com.gxmailu.ocrCloudPlatform.mapper.AppAbilityRecordAllMapper; import com.gxmailu.ocrCloudPlatform.mapper.AppAbilityRecordAllMapper;
import com.gxmailu.ocrCloudPlatform.mapper.ServerInfoMapper; import com.gxmailu.ocrCloudPlatform.mapper.ServerInfoMapper;
import com.gxmailu.ocrCloudPlatform.service.ConfigAbilityService;
import com.gxmailu.ocrCloudPlatform.service.ConfigApplicationService;
import com.gxmailu.ocrCloudPlatform.service.CourtService;
import com.gxmailu.ocrCloudPlatform.vo.BrokenLineData; import com.gxmailu.ocrCloudPlatform.vo.BrokenLineData;
import com.sun.org.apache.xpath.internal.operations.Bool;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.annotation.Resource; import javax.annotation.Resource;
...@@ -54,12 +57,18 @@ public class ElasticSearchService { ...@@ -54,12 +57,18 @@ public class ElasticSearchService {
private static final Map<Character, String> SPECIAL_CHARACTERS_MAP = new HashMap<>(); private static final Map<Character, String> SPECIAL_CHARACTERS_MAP = new HashMap<>();
private static String lastIndexTime = "2023-11-18 07:00:00"; private static String lastIndexTime = "2023-05-11 07:00:00";
@Resource @Resource
private AppAbilityRecordAllMapper recordAllMapper; private AppAbilityRecordAllMapper recordAllMapper;
@Resource @Resource
private ServerInfoMapper serverInfoMapper; private ServerInfoMapper serverInfoMapper;
@Resource
private ConfigAbilityService configAbilityService;
@Resource
private ConfigApplicationService configApplicationService;
@Resource
private CourtService courtService;
static { static {
SPECIAL_CHARACTERS_MAP.put('[', "\\["); SPECIAL_CHARACTERS_MAP.put('[', "\\[");
...@@ -328,17 +337,12 @@ public class ElasticSearchService { ...@@ -328,17 +337,12 @@ public class ElasticSearchService {
// if (CollUtil.isNotEmpty(recordDto.getIpScopeList())) { // if (CollUtil.isNotEmpty(recordDto.getIpScopeList())) {
// for (String ipScope : recordDto.getIpScopeList()) { // for (String ipScope : recordDto.getIpScopeList()) {
// if (StrUtil.isNotBlank(ipScope)) { // if (StrUtil.isNotBlank(ipScope)) {
// boolQuery.filter(q -> q.prefix(pq -> pq.field("ip.keyword").value(ipScope))); // boolQuery.should(q -> q.prefix(pq -> pq.field("ip.keyword").value(ipScope)));
// } // }
// } // }
// } // }
boolQuery.must(mustQuery -> mustQuery.bool(bq -> { boolQuery.must(mustQuery -> mustQuery.bool(bq -> {
// 多个法院ip过滤
// if (CollUtil.isNotEmpty(serverIpList)) {
// List<FieldValue> collect = serverIpList.stream().map(FieldValue::of).collect(Collectors.toList());
// bq.should(mq -> mq.terms(tq -> tq.field("serverIp").terms(tqf -> tqf.value(collect))));
// }
// 多个ip前缀过滤 // 多个ip前缀过滤
if (CollUtil.isNotEmpty(recordDto.getIpScopeList())) { if (CollUtil.isNotEmpty(recordDto.getIpScopeList())) {
for (String ipScope : recordDto.getIpScopeList()) { for (String ipScope : recordDto.getIpScopeList()) {
...@@ -350,7 +354,6 @@ public class ElasticSearchService { ...@@ -350,7 +354,6 @@ public class ElasticSearchService {
return bq; return bq;
})); }));
if (ObjUtil.isNotNull(recordDto.getApplicationId())) { if (ObjUtil.isNotNull(recordDto.getApplicationId())) {
boolQuery.must(mustQuery -> mustQuery.match(mq -> mq.field("applicationId").query(recordDto.getApplicationId().toString()))); boolQuery.must(mustQuery -> mustQuery.match(mq -> mq.field("applicationId").query(recordDto.getApplicationId().toString())));
} }
...@@ -363,6 +366,68 @@ public class ElasticSearchService { ...@@ -363,6 +366,68 @@ public class ElasticSearchService {
return handlePageResultWithHighlight(searchResponse, AppAbilityRecord.class, recordDto); return handlePageResultWithHighlight(searchResponse, AppAbilityRecord.class, recordDto);
} }
public List<CallRecordModel> selectCallRecord(String yearMonthDay, String endYearMonthDay) throws IOException {
SearchResponse<Void> searchResponse = client.search(srBuilder -> srBuilder
.index(APP_ABILITY_RECORD_INDEX)
.query(queryBuilder -> queryBuilder.bool(boolQuery -> {
boolQuery.must(q -> q.range(rangeQueryBuilder -> rangeQueryBuilder
.field("callTime")
.gte(JsonData.of(yearMonthDay))
.lt(JsonData.of(endYearMonthDay))));
return boolQuery;
}))
.size(0)
// 添加聚合查询
.aggregations("ip_agg", ipAgg -> ipAgg.terms(ipTerm -> ipTerm.field("ip.keyword"))
.aggregations("applicationId_agg", applicationAgg -> applicationAgg.terms(applicationTerm -> applicationTerm.field("applicationId"))
.aggregations("ablilityId_agg", abilityAgg -> abilityAgg.terms(abilityTerm -> abilityTerm.field("abilityId"))
.aggregations("fileCount", fileCountAgg -> fileCountAgg.sum(fileCountSum -> fileCountSum.field("fileCount"))))))
.trackTotalHits(tb -> tb.enabled(true)), Void.class);
ArrayList<CallRecordModel> recordModels = new ArrayList<>();
List<Court> courtList = courtService.getCourtList();
List<ConfigAbility> abilityList = configAbilityService.selectAll();
List<ConfigApplication> applicationList = configApplicationService.getConfigApplicationAll();
searchResponse.aggregations().get("ip_agg").sterms().buckets().array().forEach(ipBucket -> {
ipBucket.aggregations().get("applicationId_agg").lterms().buckets().array().forEach(applicationIdBucket -> {
applicationIdBucket.aggregations().get("ablilityId_agg").lterms().buckets().array().forEach(abilityIdBucket -> {
int fileCount = (int) abilityIdBucket.aggregations().get("fileCount").sum().value();
CallRecordModel recordModel = new CallRecordModel();
recordModel.setIp(ipBucket.key().stringValue());
recordModel.setIpDocCount(ipBucket.docCount());
recordModel.setApplicationId(applicationIdBucket.key());
recordModel.setUserDocCount(applicationIdBucket.docCount());
recordModel.setAbilityId(abilityIdBucket.key());
recordModel.setAbilityDocCount(abilityIdBucket.docCount());
recordModel.setFileCount(fileCount);
recordModel.setCourtName(courtList.stream()
.filter(court -> Arrays.stream(court.getIpScope().split(";")).anyMatch(recordModel.getIp()::contains))
.findFirst()
.map(Court::getName)
.orElse("未知"));
recordModel.setBusinessVendorName(applicationList.stream()
.filter(application -> application.getId().equals(recordModel.getApplicationId()))
.findFirst()
.map(ConfigApplication::getDescription)
.orElse("未知"));
recordModel.setAbilityName(abilityList.stream()
.filter(ability -> ability.getId().equals(recordModel.getAbilityId().toString()))
.findFirst()
.map(ConfigAbility::getName)
.orElse("未知"));
recordModels.add(recordModel);
});
});
});
recordModels.sort(Comparator.comparing(CallRecordModel::getCourtName)
.thenComparing(CallRecordModel::getBusinessVendorName)
.thenComparing(CallRecordModel::getAbilityName).reversed());
return recordModels;
}
/** /**
......
...@@ -104,7 +104,7 @@ public class RetransmissionService { ...@@ -104,7 +104,7 @@ public class RetransmissionService {
/** /**
* 重试次数 * 重试次数
*/ */
private static final int MAX_RETRIES = 3; private static final int MAX_RETRIES = 5;
...@@ -201,6 +201,7 @@ public class RetransmissionService { ...@@ -201,6 +201,7 @@ public class RetransmissionService {
res = HttpRequest.post(ocrApi + "/ofs/api/sync/v1/1001") res = HttpRequest.post(ocrApi + "/ofs/api/sync/v1/1001")
.headerMap(getHeaderMap(request), true) .headerMap(getHeaderMap(request), true)
.form(paramMap) .form(paramMap)
.setConnectionTimeout(10 * 1000)
.execute().body(); .execute().body();
// 设置结束时间 // 设置结束时间
endTime = System.currentTimeMillis(); endTime = System.currentTimeMillis();
...@@ -311,6 +312,7 @@ public class RetransmissionService { ...@@ -311,6 +312,7 @@ public class RetransmissionService {
res = HttpRequest.post(ocrApi + "/ofs/api/sync/v2/1001") res = HttpRequest.post(ocrApi + "/ofs/api/sync/v2/1001")
.headerMap(getHeaderMap(request), true) .headerMap(getHeaderMap(request), true)
.form(paramMap) .form(paramMap)
.setConnectionTimeout(10 * 1000)
.execute().body(); .execute().body();
// 设置结束时间 // 设置结束时间
endTime = System.currentTimeMillis(); endTime = System.currentTimeMillis();
...@@ -357,6 +359,7 @@ public class RetransmissionService { ...@@ -357,6 +359,7 @@ public class RetransmissionService {
HttpResponse httpResponse = HttpRequest.post(ocrApi + "/ofs/api/sync/v1/pdf") HttpResponse httpResponse = HttpRequest.post(ocrApi + "/ofs/api/sync/v1/pdf")
.headerMap(getHeaderMap(request), true) .headerMap(getHeaderMap(request), true)
.contentType("application/json;charset=UTF-8") .contentType("application/json;charset=UTF-8")
.setConnectionTimeout(10 * 1000)
.body(body.toJSONString(0)).execute(); .body(body.toJSONString(0)).execute();
Map<String, List<String>> headers = httpResponse.headers(); Map<String, List<String>> headers = httpResponse.headers();
...@@ -417,7 +420,6 @@ public class RetransmissionService { ...@@ -417,7 +420,6 @@ public class RetransmissionService {
} }
} catch (Exception e) { } catch (Exception e) {
log.error("在处理ID为 {} 的PDF下载过程中发生异常", id, e); log.error("在处理ID为 {} 的PDF下载过程中发生异常", id, e);
// returnError(response, 500, "OCR云平台接口异常");
} }
} }
...@@ -432,6 +434,7 @@ public class RetransmissionService { ...@@ -432,6 +434,7 @@ public class RetransmissionService {
HttpResponse httpResponse = HttpRequest.post(ocrApi + "/ofs/api/sync/v1/ofd") HttpResponse httpResponse = HttpRequest.post(ocrApi + "/ofs/api/sync/v1/ofd")
.headerMap(getHeaderMap(request), true) .headerMap(getHeaderMap(request), true)
.contentType("application/json;charset=UTF-8") .contentType("application/json;charset=UTF-8")
.setConnectionTimeout(10 * 1000)
.body(body.toJSONString(0)).execute(); .body(body.toJSONString(0)).execute();
Map<String, List<String>> headers = httpResponse.headers(); Map<String, List<String>> headers = httpResponse.headers();
if (!headers.isEmpty()) { if (!headers.isEmpty()) {
...@@ -490,7 +493,6 @@ public class RetransmissionService { ...@@ -490,7 +493,6 @@ public class RetransmissionService {
} }
} catch (Exception e) { } catch (Exception e) {
log.error("在处理ID为 {} 的PDF下载过程中发生异常", id, e); log.error("在处理ID为 {} 的PDF下载过程中发生异常", id, e);
// returnError(response, 500, "OCR云平台接口异常");
} }
} }
...@@ -530,6 +532,7 @@ public class RetransmissionService { ...@@ -530,6 +532,7 @@ public class RetransmissionService {
res = HttpRequest.post(ocrApi + "/ofs/api/sync/v1/ft/1001") res = HttpRequest.post(ocrApi + "/ofs/api/sync/v1/ft/1001")
.headerMap(getHeaderMap(request), true) .headerMap(getHeaderMap(request), true)
.form(paramMap) .form(paramMap)
.setConnectionTimeout(10 * 1000)
.execute().body(); .execute().body();
// 设置结束时间 // 设置结束时间
endTime = System.currentTimeMillis(); endTime = System.currentTimeMillis();
...@@ -600,6 +603,7 @@ public class RetransmissionService { ...@@ -600,6 +603,7 @@ public class RetransmissionService {
res = HttpRequest.post(ocrApi + "/ofs/api/sync/v2/1001") res = HttpRequest.post(ocrApi + "/ofs/api/sync/v2/1001")
.headerMap(getHeaderMap(request), true) .headerMap(getHeaderMap(request), true)
.form(paramMap) .form(paramMap)
.setConnectionTimeout(10 * 1000)
.execute().body(); .execute().body();
// 设置结束时间 // 设置结束时间
endTime = System.currentTimeMillis(); endTime = System.currentTimeMillis();
...@@ -658,6 +662,7 @@ public class RetransmissionService { ...@@ -658,6 +662,7 @@ public class RetransmissionService {
String res = HttpRequest.post(ocrApi + "/ofs/api/sync/v1/kv/" + code) String res = HttpRequest.post(ocrApi + "/ofs/api/sync/v1/kv/" + code)
.headerMap(getHeaderMap(request), true) .headerMap(getHeaderMap(request), true)
.form(paramMap) .form(paramMap)
.setConnectionTimeout(10 * 1000)
.execute().body(); .execute().body();
// 设置结束时间 // 设置结束时间
long endTime = System.currentTimeMillis(); long endTime = System.currentTimeMillis();
...@@ -705,6 +710,7 @@ public class RetransmissionService { ...@@ -705,6 +710,7 @@ public class RetransmissionService {
String res = HttpRequest.post(ocrApi + "/ofs/api/async/minio/v1/" + code) String res = HttpRequest.post(ocrApi + "/ofs/api/async/minio/v1/" + code)
.headerMap(getHeaderMap(request), true) .headerMap(getHeaderMap(request), true)
.contentType("application/json;charset=UTF-8") .contentType("application/json;charset=UTF-8")
.setConnectionTimeout(10 * 1000)
.body(body.toJSONString(0)).execute().body(); .body(body.toJSONString(0)).execute().body();
// 设置结束时间 // 设置结束时间
long endTime = System.currentTimeMillis(); long endTime = System.currentTimeMillis();
...@@ -747,6 +753,7 @@ public class RetransmissionService { ...@@ -747,6 +753,7 @@ public class RetransmissionService {
String res = HttpRequest.post(ocrApi + "/ofs/api/sync/v1/6002") String res = HttpRequest.post(ocrApi + "/ofs/api/sync/v1/6002")
.headerMap(getHeaderMap(request), true) .headerMap(getHeaderMap(request), true)
.form(paramMap) .form(paramMap)
.setConnectionTimeout(10 * 1000)
.execute().body(); .execute().body();
// 设置结束时间 // 设置结束时间
long endTime = System.currentTimeMillis(); long endTime = System.currentTimeMillis();
...@@ -791,9 +798,8 @@ public class RetransmissionService { ...@@ -791,9 +798,8 @@ public class RetransmissionService {
addServerRequestCount(ocrServerAddress.getIp(), 1); addServerRequestCount(ocrServerAddress.getIp(), 1);
String ocrApi = "http://" + ocrServerAddress.getIp() + ":" + ocrServerAddress.getOcrPort(); String ocrApi = "http://" + ocrServerAddress.getIp() + ":" + ocrServerAddress.getOcrPort();
HttpResponse httpResponse = HttpRequest.post(ocrApi + "/ofs/api/file/convert") HttpResponse httpResponse = HttpRequest.post(ocrApi + "/ofs/api/file/convert")
.header("Authorization", request.getHeader("Authorization")) .headerMap(getHeaderMap(request), true)
.header("X-Timestamp", request.getHeader("X-Timestamp")) .setConnectionTimeout(10 * 1000)
.header("X-Checksum", request.getHeader("X-Checksum"))
.contentType(ContentType.MULTIPART.getValue()) .contentType(ContentType.MULTIPART.getValue())
.form(paramMap).execute(); .form(paramMap).execute();
...@@ -848,28 +854,28 @@ public class RetransmissionService { ...@@ -848,28 +854,28 @@ public class RetransmissionService {
// 保存到ES // 保存到ES
elasticSearchService.recordDataImport(Collections.singletonList(record)); elasticSearchService.recordDataImport(Collections.singletonList(record));
// log.info("修改id:{}的真实客户端请求ip结果:{}", id, mapper.updateById(record)); // // log.info("修改id:{}的真实客户端请求ip结果:{}", id, mapper.updateById(record));
AppAbilityRecordAll recordAll = recordAllMapper.selectById(id); // AppAbilityRecordAll recordAll = recordAllMapper.selectById(id);
if (recordAll == null) { // if (recordAll == null) {
recordAll = new AppAbilityRecordAll(); // recordAll = new AppAbilityRecordAll();
BeanUtils.copyProperties(record, recordAll, AppAbilityRecordAll.class); // BeanUtils.copyProperties(record, recordAll, AppAbilityRecordAll.class);
recordAll.setCreatedTime(DateUtil.date()); // recordAll.setCreatedTime(DateUtil.date());
recordAll.setUpdatedTime(DateUtil.date()); // recordAll.setUpdatedTime(DateUtil.date());
if (StrUtil.isEmpty(recordAll.getServerIp())) { // if (StrUtil.isEmpty(recordAll.getServerIp())) {
recordAll.setServerIp(serverIp); // recordAll.setServerIp(serverIp);
} // }
recordAllMapper.insert(recordAll); // recordAllMapper.insert(recordAll);
// log.info("添加id:{} 的数据到recordAll持久化保存结果:{}", recordAll.getId(), recordAllMapper.insert(recordAll)); // // log.info("添加id:{} 的数据到recordAll持久化保存结果:{}", recordAll.getId(), recordAllMapper.insert(recordAll));
} else { // } else {
AppAbilityRecordAll newRecordAll = new AppAbilityRecordAll(); // AppAbilityRecordAll newRecordAll = new AppAbilityRecordAll();
BeanUtils.copyProperties(record, newRecordAll, AppAbilityRecordAll.class); // BeanUtils.copyProperties(record, newRecordAll, AppAbilityRecordAll.class);
newRecordAll.setUpdatedTime(DateUtil.date()); // newRecordAll.setUpdatedTime(DateUtil.date());
if (StrUtil.isEmpty(newRecordAll.getServerIp())) { // if (StrUtil.isEmpty(newRecordAll.getServerIp())) {
newRecordAll.setServerIp(serverIp); // newRecordAll.setServerIp(serverIp);
} // }
recordAllMapper.updateById(newRecordAll); // recordAllMapper.updateById(newRecordAll);
// log.info("修改recordAll中id:{} 的数据结果:{}", newRecordAll.getId(), recordAllMapper.updateById(newRecordAll)); // // log.info("修改recordAll中id:{} 的数据结果:{}", newRecordAll.getId(), recordAllMapper.updateById(newRecordAll));
} // }
} }
......
...@@ -13,15 +13,26 @@ import co.elastic.clients.elasticsearch._types.aggregations.*; ...@@ -13,15 +13,26 @@ import co.elastic.clients.elasticsearch._types.aggregations.*;
import co.elastic.clients.elasticsearch.core.SearchResponse; import co.elastic.clients.elasticsearch.core.SearchResponse;
import co.elastic.clients.elasticsearch.core.search.TotalHits; import co.elastic.clients.elasticsearch.core.search.TotalHits;
import co.elastic.clients.json.JsonData; import co.elastic.clients.json.JsonData;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxmailu.ocrCloudPlatform.constant.RedisConstant; import com.gxmailu.ocrCloudPlatform.constant.RedisConstant;
import com.gxmailu.ocrCloudPlatform.dto.RecordDto; import com.gxmailu.ocrCloudPlatform.dto.RecordDto;
import com.gxmailu.ocrCloudPlatform.entity.AppAbilityRecord; import com.gxmailu.ocrCloudPlatform.entity.AppAbilityRecord;
import com.gxmailu.ocrCloudPlatform.entity.ConfigAbility;
import com.gxmailu.ocrCloudPlatform.entity.ConfigApplication;
import com.gxmailu.ocrCloudPlatform.entity.Court; import com.gxmailu.ocrCloudPlatform.entity.Court;
import com.gxmailu.ocrCloudPlatform.excel.handler.ExcelFillCellMergeStrategy;
import com.gxmailu.ocrCloudPlatform.excel.model.CallRecordModel;
import com.gxmailu.ocrCloudPlatform.excel.utils.ExcelStyleUtils;
import com.gxmailu.ocrCloudPlatform.mapper.AppAbilityRecordAllMapper; import com.gxmailu.ocrCloudPlatform.mapper.AppAbilityRecordAllMapper;
import com.gxmailu.ocrCloudPlatform.mapper.CourtMapper; import com.gxmailu.ocrCloudPlatform.mapper.CourtMapper;
import com.gxmailu.ocrCloudPlatform.service.AppAbilityRecordAllService; import com.gxmailu.ocrCloudPlatform.service.AppAbilityRecordAllService;
import com.gxmailu.ocrCloudPlatform.service.ConfigAbilityService;
import com.gxmailu.ocrCloudPlatform.service.ConfigApplicationService;
import com.gxmailu.ocrCloudPlatform.service.CourtService;
import com.gxmailu.ocrCloudPlatform.service.impl.ElasticSearchService; import com.gxmailu.ocrCloudPlatform.service.impl.ElasticSearchService;
import com.gxmailu.ocrCloudPlatform.service.impl.RedisService; import com.gxmailu.ocrCloudPlatform.service.impl.RedisService;
import com.gxmailu.ocrCloudPlatform.vo.BrokenLineData; import com.gxmailu.ocrCloudPlatform.vo.BrokenLineData;
...@@ -60,6 +71,12 @@ public class ESTest { ...@@ -60,6 +71,12 @@ public class ESTest {
private ElasticSearchService elasticSearchService; private ElasticSearchService elasticSearchService;
@Autowired @Autowired
private AppAbilityRecordAllService appAbilityRecordAllService; private AppAbilityRecordAllService appAbilityRecordAllService;
@Autowired
private ConfigAbilityService configAbilityService;
@Autowired
private ConfigApplicationService configApplicationService;
@Autowired
private CourtService courtService;
@Test @Test
public void getCourtUseByYear() throws IOException { public void getCourtUseByYear() throws IOException {
...@@ -154,7 +171,7 @@ public class ESTest { ...@@ -154,7 +171,7 @@ public class ESTest {
RecordDto recordDto = new RecordDto(); RecordDto recordDto = new RecordDto();
recordDto.setPage(1); recordDto.setPage(1);
recordDto.setLimit(10); recordDto.setLimit(10);
// recordDto.setIpScopeList(Arrays.asList("147.4.")); recordDto.setIpScopeList(Arrays.asList("147.4.", "147.72."));
// recordDto.setApplicationId(5L); // recordDto.setApplicationId(5L);
// recordDto.setStartTime(DateUtil.parseDate("2023-05-11")); // recordDto.setStartTime(DateUtil.parseDate("2023-05-11"));
// recordDto.setEndTime(DateUtil.parseDate("2023-05-11")); // recordDto.setEndTime(DateUtil.parseDate("2023-05-11"));
...@@ -305,4 +322,92 @@ public class ESTest { ...@@ -305,4 +322,92 @@ public class ESTest {
} }
return courtMapper.selectOne(new LambdaQueryWrapper<Court>().eq(Court::getName, courtName)); return courtMapper.selectOne(new LambdaQueryWrapper<Court>().eq(Court::getName, courtName));
} }
public List<CallRecordModel> getAggregatedData() throws IOException {
String start = "2023-11-24 00:00:00";
String end = "2023-11-24 23:59:59";
// 添加聚合
SearchResponse<Void> searchResponse = client.search(srBuilder -> srBuilder
.index(APP_ABILITY_RECORD_INDEX)
.query(queryBuilder -> queryBuilder.bool(boolQuery -> {
boolQuery.must(q -> q.range(rangeQueryBuilder -> rangeQueryBuilder
.field("callTime")
.gte(JsonData.of(start))
.lt(JsonData.of(end))));
return boolQuery;
}))
.size(0)
.aggregations("ip_agg", ipAgg -> ipAgg.terms(ipTerm -> ipTerm.field("ip.keyword"))
.aggregations("applicationId_agg", applicationAgg -> applicationAgg.terms(applicationTerm -> applicationTerm.field("applicationId"))
.aggregations("ablilityId_agg", abilityAgg -> abilityAgg.terms(abilityTerm -> abilityTerm.field("abilityId"))
.aggregations("fileCount", fileCountAgg -> fileCountAgg.sum(fileCountSum -> fileCountSum.field("fileCount"))))))
.trackTotalHits(tb -> tb.enabled(true)),
Void.class);
ArrayList<CallRecordModel> recordModels = new ArrayList<>();
List<Court> courtList = courtService.getCourtList();
List<ConfigAbility> abilityList = configAbilityService.selectAll();
List<ConfigApplication> applicationList = configApplicationService.getConfigApplicationAll();
searchResponse.aggregations().get("ip_agg").sterms().buckets().array().forEach(ipBucket -> {
ipBucket.aggregations().get("applicationId_agg").lterms().buckets().array().forEach(applicationIdBucket -> {
applicationIdBucket.aggregations().get("ablilityId_agg").lterms().buckets().array().forEach(abilityIdBucket -> {
int fileCount = (int) abilityIdBucket.aggregations().get("fileCount").sum().value();
CallRecordModel recordModel = new CallRecordModel();
recordModel.setIp(ipBucket.key().stringValue());
recordModel.setIpDocCount(ipBucket.docCount());
recordModel.setApplicationId(applicationIdBucket.key());
recordModel.setUserDocCount(applicationIdBucket.docCount());
recordModel.setAbilityId(abilityIdBucket.key());
recordModel.setAbilityDocCount(abilityIdBucket.docCount());
recordModel.setFileCount(fileCount);
recordModel.setCourtName(courtList.stream()
.filter(court -> Arrays.stream(court.getIpScope().split(";")).anyMatch(recordModel.getIp()::contains))
.findFirst()
.map(Court::getName)
.orElse("未知"));
recordModel.setBusinessVendorName(applicationList.stream()
.filter(application -> application.getId().equals(recordModel.getApplicationId()))
.findFirst()
.map(ConfigApplication::getDescription)
.orElse("未知"));
recordModel.setAbilityName(abilityList.stream()
.filter(ability -> ability.getId().equals(recordModel.getAbilityId().toString()))
.findFirst()
.map(ConfigAbility::getName)
.orElse("未知"));
recordModels.add(recordModel);
});
});
});
recordModels.sort(Comparator.comparing(CallRecordModel::getCourtName)
.thenComparing(CallRecordModel::getBusinessVendorName)
.thenComparing(CallRecordModel::getAbilityName).reversed());
recordModels.forEach(System.out::println);
return recordModels;
}
@Test
public void testExport() throws IOException {
// 设置单元格样式
HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(ExcelStyleUtils.getHeadStyle(), ExcelStyleUtils.getContentStyle());
// 获取:用户信息列表。
List<CallRecordModel> list = this.getAggregatedData();
// 处理:导出数据。
EasyExcel.write("D:\\data\\export.xlsx")
.sheet("导出数据")
.head(CallRecordModel.class)
// 自动行宽策略
.registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
// 自定义合并策略
// .registerWriteHandler(new CustomMergeStrategy(Arrays.asList("courtName", "businessVendorName")))
.registerWriteHandler(new ExcelFillCellMergeStrategy(0, 0, new int[]{0, 1}))
// 设置单元格样式
.registerWriteHandler(horizontalCellStyleStrategy)
.doWrite(list);
}
} }
package com.gxmailu.ocrCloudPlatform;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.util.DateUtils;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.gxmailu.ocrCloudPlatform.excel.handler.ExcelFillCellMergeStrategy;
import com.gxmailu.ocrCloudPlatform.excel.model.CallRecordModel;
import com.gxmailu.ocrCloudPlatform.excel.utils.ExcelStyleUtils;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* EasyExcel 测试类。
*
* @author 张德龙
*/
@Slf4j
public class EasyExcelTest {
/**
* 测试:模型映射导出。
*/
@Test
@SneakyThrows
public void testExport() {
// 设置单元格样式
HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(ExcelStyleUtils.getHeadStyle(), ExcelStyleUtils.getContentStyle());
// 获取:用户信息列表。
List<CallRecordModel> list = new ArrayList<>();
// 处理:导出数据。
EasyExcel.write("D:\\data\\export.xlsx")
.sheet("导出数据")
.head(CallRecordModel.class)
// 自动行宽策略
.registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
// 自定义合并策略
.registerWriteHandler(new ExcelFillCellMergeStrategy(0, 0, new int[]{0, 1, 2}))
// 设置单元格样式
.registerWriteHandler(horizontalCellStyleStrategy)
.doWrite(list);
}
// private List<RecordModel> getRecordList() {
// RecordModel recordModel = new RecordModel();
// recordModel.setCourtName("广西壮族自治区高级人民法院");
// recordModel.setBusinessVendorName("华宇");
// recordModel.setAbilityName("通用文字识别");
// recordModel.setCallCount(18);
// recordModel.setFileCount(55);
// RecordModel recordModel00 = new RecordModel();
// recordModel00.setCourtName("广西壮族自治区高级人民法院");
// recordModel00.setBusinessVendorName("华宇");
// recordModel00.setAbilityName("通用文字识别");
// recordModel00.setCallCount(18);
// recordModel00.setFileCount(55);
// RecordModel recordModel1 = new RecordModel();
// recordModel1.setCourtName("广西壮族自治区高级人民法院");
// recordModel1.setBusinessVendorName("华宇");
// recordModel1.setAbilityName("表格文字识别");
// recordModel1.setCallCount(14);
// recordModel1.setFileCount(33);
// RecordModel recordModel2 = new RecordModel();
// recordModel2.setCourtName("广西壮族自治区高级人民法院");
// recordModel2.setBusinessVendorName("北投");
// recordModel2.setAbilityName("通用文字识别");
// recordModel2.setCallCount(423);
// recordModel2.setFileCount(3222);
// RecordModel recordModel3 = new RecordModel();
// recordModel3.setCourtName("南宁市中级人民法院");
// recordModel3.setBusinessVendorName("通达海");
// recordModel3.setAbilityName("通用文字识别");
// recordModel3.setCallCount(323);
// recordModel3.setFileCount(444);
// RecordModel recordModel4 = new RecordModel();
// recordModel4.setCourtName("南宁市中级人民法院");
// recordModel4.setBusinessVendorName("通达海");
// recordModel4.setAbilityName("表格文字识别");
// recordModel4.setCallCount(111);
// recordModel4.setFileCount(333);
// RecordModel recordModel5 = new RecordModel();
// recordModel5.setCourtName("北海市中级人民法院");
// recordModel5.setBusinessVendorName("通达海");
// recordModel5.setAbilityName("通用文字识别");
// recordModel5.setCallCount(23);
// recordModel5.setFileCount(111);
// RecordModel recordModel6 = new RecordModel();
// recordModel6.setCourtName("梧州市中级人民法院");
// recordModel6.setBusinessVendorName("北投");
// recordModel6.setAbilityName("通用文字识别");
// recordModel6.setCallCount(23);
// recordModel6.setFileCount(111);
//
// List<RecordModel> list = new ArrayList<>(Arrays.asList(recordModel, recordModel00, recordModel1, recordModel2, recordModel3, recordModel4, recordModel5, recordModel6));
// RecordModel totalRow = new RecordModel();
// totalRow.setCourtName("总计");
// totalRow.setCallCount(list.stream().mapToInt(RecordModel::getCallCount).sum());
// totalRow.setFileCount(list.stream().mapToInt(RecordModel::getFileCount).sum());
// list.add(totalRow);
// return list;
// }
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment