一、oceanbase到elasticsearch
对ContractComplexSearchEsDTO 序列化为字符串,再反序列化为 ContractComprehensiveViewDTO
说明:反序列时有字段名称变化,数据类型变化
再拷贝成ContractComprehensiveView
public void obToEs(){
private List<ContractComplexSearchEsDTO> contractList;
//合同dto序列化
String jsonStr = JsonUtils.objToFormatStr(contractList);
//反序列化,转为es的viewdto
List<ContractComprehensiveViewDTO> esViewDtoList = (List<ContractComprehensiveViewDTO>) JsonUtils.strToList(jsonStr, ArrayList.class, ContractComprehensiveViewDTO.class);
List<ContractComprehensiveView> esViewList = new ArrayList<>();
esViewDtoList.forEach(viewDTO -> {
ContractComprehensiveView view = new ContractComprehensiveView();
//es的viewdto复制到view
BeanUtils.copyProperties(viewDTO, view);
。。。
esViewList.add(view);
});
}
@Data
@ExcelIgnoreUnannotated //不加@ExcelProperty的不导出excel
public class ContractComplexSearchEsDTO extends ContractComplexSearchExcelDTO {
/**
* 确认时间
*/
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
@ExcelProperty(value ="确认时间", index = 33, converter = DateTimeConverter.class)// 导出excel列名称
private LocalDateTime signConfirmTime;
/**
* 协助履行人,多个用符号隔开,如 abc,bcd,cedf
*/
private String performerAssistNames ;
}
public class ContractComprehensiveViewDTO {
/**
* 确认时间。注意换名、换类型
*/
@JsonDeserialize(using = LocalDateTimeToDateLongDeserializer.class) //反序列化类型转换
@JsonSerialize(using = DateLongToLocalDateTimeSerializer.class) //序列化类型转化
@JsonSetter("signConfirmTime") //反序列化名称变化,源名称
private Long signEndTime ;
/**
* 列表,人名
*/
@JsonDeserialize(using = StringToListDeserializer.class)
@JsonSerialize(using = ListToStringSerializer.class)
private List<String> performerAssistNames ;
}
public class ContractComprehensiveView{
/**
* 确认时间
*/
@EsField(export = true)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern ="yyyyMMdd",timezone="GMT+8")
@Field(type = FieldType.Long)
private Long signEndTime ;
/**
* 人姓名
* 列表,
* array
*/
@EsField(export = true)
@Field(type = FieldType.Keyword)
private List<String> performerAssistNames ;
}
/**
*
* LocalDate反序列化为Long
* yyyy-MM-dd 或 yyyy-MM-dd HH:mm:ss 转化为yyyyMMddHHmmss
* @author ##@##.com.cn
* @date 2022/02/09
*/
public class LocalDateTimeToDateLongDeserializer extends JsonDeserializer<Long> {
/**
*
* @param parser 解析器
* @param context 上下文
* @return Long
* @throws IOException 异常
*/
@Override
public Long deserialize(JsonParser parser, DeserializationContext context) throws IOException {
if (ObjectUtils.anyNull(parser, parser.getText())) {
return null;
}
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String str = parser.getText();
if(str.length()==10){
str += " 00:00:00";
}
LocalDateTime ldt = LocalDateTime.parse(str,df);
return DateUtils.dateToLong(ldt,DateUtils.FMT_DATE_01);
}
}
/**
* long序列化为LocalDateTime
* yyyyMMdd 或 yyyyMMddHHmmss转化为yyyy-MM-dd HH:mm:ss
*
* @author ##@##.com.cn
* @date 2022/02/09
*/
public class DateLongToLocalDateTimeSerializer extends JsonSerializer<Long> {
/**
* 序列化
*
* @param o 数据对象
* @param jsonGenerator 转换器
* @param serializerProvider 提供者
* @throws IOException 异常
*/
@Override
public void serialize(Long o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
String str = o.toString();
if (str.length() == 8) {
str += "000000";
}
LocalDateTime ldt = DateUtils.strToDate(str, DateUtils.FMT_DATE_01);
String strDt = DateUtils.dateToStr(ldt,DateUtils.FMT_DATE_04);
jsonGenerator.writeString(strDt);
}
}
/**
* 时间格式转化类
*
* @author ##@##.com.cn
* @date 2023/04/04
* @apiNote 时间格式转化类
*/
public class DateTimeConverter implements Converter<LocalDateTime> {
/**
* 功能描述:
* 〈时间转化格式〉
* @date 2022/8/28
*/
private static final String PATTERN_YYYY_MM_DD = "yyyy-MM-dd";
@Override
public Class<?> supportJavaTypeKey() {
return Converter.super.supportJavaTypeKey();
}
@Override
public CellDataTypeEnum supportExcelTypeKey() {
return Converter.super.supportExcelTypeKey();
}
@Override
public WriteCellData<?> convertToExcelData(LocalDateTime value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration){
DateTimeFormatter sdf = DateTimeFormatter.ofPattern(PATTERN_YYYY_MM_DD);
String dateValue = value.format(sdf);
return new WriteCellData<>(dateValue);
}
}
二、elasticSearch到excel
从ElasticSearch查出,提供下载excel
public void esToExcel(){
// 特殊处理
List<ContractComplexSearchExcelDTO> searchExcelList = excelResultProcess(pageEs.getList());
// 调用导出接口
EasyExcelExportUtils.export2Web(response, "合同综合查询导出", "sheet1", ContractComplexSearchExcelDTO.class, searchExcelList);
}
/**
* 报表结果处理
*
* @param esList 从es查出的数据
* @return 导出列表
*/
private List<ContractComplexSearchExcelDTO> excelResultProcess(List<ContractComprehensiveView> esList) {
List<ContractComplexSearchExcelDTO> searchList = Lists.newArrayList();
esList.forEach(view -> {
ContractComprehensiveViewDTO viewDTO = new ContractComprehensiveViewDTO();
BeanUtils.copyProperties(view, viewDTO);
//viewDTO通过序列化,转为searchExcelDTO
String jsonStr = JsonUtils.objToStr(viewDTO);
ContractComplexSearchExcelDTO searchExcelDTO = JsonUtils.jsonToObj(jsonStr, ContractComplexSearchExcelDTO.class);
searchExcelDTO.setOrderNo(rowNum++);
searchExcelDTO.setAssistPerformerNames(listToString(viewDTO.getPerformerAssistNames()));
。。。
searchList.add(searchExcelDTO);
});
return searchList;
}