13 changed files with 411 additions and 29 deletions
-
8cas-admin/pom.xml
-
10cas-admin/src/main/java/com/inscloudtech/web/controller/datacenter/RealEstateController.java
-
51cas-admin/src/main/java/com/inscloudtech/web/controller/querycenter/QueryCenterController.java
-
152cas-common/src/main/java/com/inscloudtech/common/excel/ImportExcelHelper.java
-
147cas-common/src/main/java/com/inscloudtech/common/excel/ImportExcelListener.java
-
15cas-common/src/main/java/com/inscloudtech/common/excel/RowIndex.java
-
13cas-common/src/main/java/com/inscloudtech/common/utils/BeanCopyUtils.java
-
12cas-system/pom.xml
-
20cas-system/src/main/java/com/inscloudtech/datacenter/domain/RealEstate.java
-
3cas-system/src/main/java/com/inscloudtech/datacenter/service/QueryCenterService.java
-
2cas-system/src/main/java/com/inscloudtech/datacenter/service/impl/PlateNumberServiceImpl.java
-
4cas-system/src/main/java/com/inscloudtech/system/service/impl/SysOperLogServiceImpl.java
-
3cas-system/src/main/java/com/inscloudtech/system/service/impl/SysOssServiceImpl.java
@ -0,0 +1,51 @@ |
|||||
|
package com.inscloudtech.web.controller.querycenter; |
||||
|
|
||||
|
|
||||
|
import com.inscloudtech.common.core.controller.BaseController; |
||||
|
import com.inscloudtech.common.core.domain.R; |
||||
|
import com.inscloudtech.common.core.page.TableDataInfo; |
||||
|
import com.inscloudtech.datacenter.domain.dto.QueryCenterQuery; |
||||
|
import com.inscloudtech.datacenter.service.QueryCenterService; |
||||
|
import lombok.RequiredArgsConstructor; |
||||
|
import org.springframework.web.bind.annotation.GetMapping; |
||||
|
import org.springframework.web.bind.annotation.RequestMapping; |
||||
|
import org.springframework.web.bind.annotation.RestController; |
||||
|
|
||||
|
/** |
||||
|
* 查询中心 |
||||
|
* |
||||
|
* @author inscloudtech |
||||
|
* @date 2023-06-13 |
||||
|
*/ |
||||
|
@RequiredArgsConstructor |
||||
|
@RestController |
||||
|
@RequestMapping("/qc/index") |
||||
|
public class QueryCenterController extends BaseController { |
||||
|
|
||||
|
private final QueryCenterService queryCenterService; |
||||
|
|
||||
|
//"检索中心命中数量查询") |
||||
|
@GetMapping("/count") |
||||
|
public R count(QueryCenterQuery query) { |
||||
|
return R.ok(queryCenterService.count(query)); |
||||
|
} |
||||
|
|
||||
|
//"检索中心分页查询") |
||||
|
@GetMapping("/page") |
||||
|
public TableDataInfo page(QueryCenterQuery query) { |
||||
|
return queryCenterService.page(query); |
||||
|
} |
||||
|
|
||||
|
//"mysqlToEs-测试用") |
||||
|
@GetMapping("/mysqlToEs") |
||||
|
public R mysqlToEs(String indexName,boolean create) { |
||||
|
return R.ok(queryCenterService.mysqlToEs(indexName,create)); |
||||
|
} |
||||
|
|
||||
|
//"根据caseId删除数据") |
||||
|
@GetMapping("/deleteData") |
||||
|
public R deleteData(QueryCenterQuery query) { |
||||
|
return R.ok(queryCenterService.deleteDataByCondition(query)); |
||||
|
} |
||||
|
|
||||
|
} |
@ -0,0 +1,152 @@ |
|||||
|
package com.inscloudtech.common.excel; |
||||
|
|
||||
|
|
||||
|
import com.alibaba.excel.EasyExcel; |
||||
|
import com.alibaba.excel.annotation.ExcelProperty; |
||||
|
import com.alibaba.excel.enums.CellExtraTypeEnum; |
||||
|
import com.alibaba.excel.metadata.CellExtra; |
||||
|
import org.slf4j.Logger; |
||||
|
import org.slf4j.LoggerFactory; |
||||
|
import org.springframework.util.CollectionUtils; |
||||
|
|
||||
|
import java.io.InputStream; |
||||
|
import java.lang.reflect.Field; |
||||
|
import java.util.List; |
||||
|
|
||||
|
|
||||
|
/** |
||||
|
* 导入有合并单元格的数据 |
||||
|
* @param <T> |
||||
|
*/ |
||||
|
public class ImportExcelHelper<T> { |
||||
|
|
||||
|
private static final Logger LOGGER = LoggerFactory.getLogger(ImportExcelHelper.class); |
||||
|
|
||||
|
/** |
||||
|
* 返回解析后的List |
||||
|
* |
||||
|
* @param: fileName 文件名 |
||||
|
* @param: clazz Excel对应属性名 |
||||
|
* @param: sheetNo 要解析的sheet |
||||
|
* @param: headRowNumber 正文起始行 |
||||
|
* @return java.util.List<T> 解析后的List |
||||
|
*/ |
||||
|
public List<T> getList(String fileName, Class<T> clazz, Integer sheetNo, Integer headRowNumber) { |
||||
|
ImportExcelListener<T> listener = new ImportExcelListener<>(headRowNumber); |
||||
|
try { |
||||
|
EasyExcel.read(fileName, clazz, listener).extraRead(CellExtraTypeEnum.MERGE).sheet(sheetNo).headRowNumber(headRowNumber).doRead(); |
||||
|
} catch (Exception e) { |
||||
|
LOGGER.error(e.getMessage()); |
||||
|
} |
||||
|
List<CellExtra> extraMergeInfoList = listener.getExtraMergeInfoList(); |
||||
|
if (CollectionUtils.isEmpty(extraMergeInfoList)) { |
||||
|
return listener.getData(); |
||||
|
} |
||||
|
List<T> data = explainMergeData(listener.getData(), extraMergeInfoList, headRowNumber); |
||||
|
return data; |
||||
|
} |
||||
|
|
||||
|
public List<T> getListWithInputStream(InputStream is, Class<T> clazz, Integer sheetNo, Integer headRowNumber) { |
||||
|
ImportExcelListener<T> listener = new ImportExcelListener<>(headRowNumber); |
||||
|
try { |
||||
|
EasyExcel.read(is, clazz, listener).extraRead(CellExtraTypeEnum.MERGE).sheet(sheetNo).headRowNumber(headRowNumber).doRead(); |
||||
|
} catch (Exception e) { |
||||
|
LOGGER.error(e.getMessage()); |
||||
|
} |
||||
|
List<CellExtra> extraMergeInfoList = listener.getExtraMergeInfoList(); |
||||
|
if (CollectionUtils.isEmpty(extraMergeInfoList)) { |
||||
|
return listener.getData(); |
||||
|
} |
||||
|
List<T> data = explainMergeData(listener.getData(), extraMergeInfoList, headRowNumber); |
||||
|
return data; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 处理合并单元格 |
||||
|
* |
||||
|
* @param data 解析数据 |
||||
|
* @param extraMergeInfoList 合并单元格信息 |
||||
|
* @param headRowNumber 起始行 |
||||
|
* @return 填充好的解析数据 |
||||
|
*/ |
||||
|
private List<T> explainMergeData(List<T> data, List<CellExtra> extraMergeInfoList, Integer headRowNumber) { |
||||
|
//循环所有合并单元格信息 |
||||
|
extraMergeInfoList.forEach(cellExtra -> { |
||||
|
int firstRowIndex = cellExtra.getFirstRowIndex() - headRowNumber; |
||||
|
int lastRowIndex = cellExtra.getLastRowIndex() - headRowNumber; |
||||
|
int firstColumnIndex = cellExtra.getFirstColumnIndex(); |
||||
|
int lastColumnIndex = cellExtra.getLastColumnIndex(); |
||||
|
//获取初始值 |
||||
|
Object initValue = getInitValueFromList(firstRowIndex, firstColumnIndex, data); |
||||
|
//设置值 |
||||
|
for (int i = firstRowIndex; i <= lastRowIndex; i++) { |
||||
|
for (int j = firstColumnIndex; j <= lastColumnIndex; j++) { |
||||
|
setInitValueToList(initValue, i, j, data); |
||||
|
} |
||||
|
} |
||||
|
}); |
||||
|
return data; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 设置合并单元格的值 |
||||
|
* |
||||
|
* @param filedValue 值 |
||||
|
* @param rowIndex 行 |
||||
|
* @param columnIndex 列 |
||||
|
* @param data 解析数据 |
||||
|
*/ |
||||
|
public void setInitValueToList(Object filedValue, Integer rowIndex, Integer columnIndex, List<T> data) { |
||||
|
T object = data.get(rowIndex); |
||||
|
|
||||
|
for (Field field : object.getClass().getDeclaredFields()) { |
||||
|
//提升反射性能,关闭安全检查 |
||||
|
field.setAccessible(true); |
||||
|
ExcelProperty annotation = field.getAnnotation(ExcelProperty.class); |
||||
|
if (annotation != null) { |
||||
|
if (annotation.index() == columnIndex) { |
||||
|
try { |
||||
|
field.set(object, filedValue); |
||||
|
break; |
||||
|
} catch (IllegalAccessException e) { |
||||
|
LOGGER.error("设置合并单元格的值异常:"+e.getMessage()); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
|
/** |
||||
|
* 获取合并单元格的初始值 |
||||
|
* rowIndex对应list的索引 |
||||
|
* columnIndex对应实体内的字段 |
||||
|
* |
||||
|
* @param firstRowIndex 起始行 |
||||
|
* @param firstColumnIndex 起始列 |
||||
|
* @param data 列数据 |
||||
|
* @return 初始值 |
||||
|
*/ |
||||
|
private Object getInitValueFromList(Integer firstRowIndex, Integer firstColumnIndex, List<T> data) { |
||||
|
Object filedValue = null; |
||||
|
T object = data.get(firstRowIndex); |
||||
|
for (Field field : object.getClass().getDeclaredFields()) { |
||||
|
//提升反射性能,关闭安全检查 |
||||
|
field.setAccessible(true); |
||||
|
ExcelProperty annotation = field.getAnnotation(ExcelProperty.class); |
||||
|
if (annotation != null) { |
||||
|
if (annotation.index() == firstColumnIndex) { |
||||
|
try { |
||||
|
filedValue = field.get(object); |
||||
|
break; |
||||
|
} catch (IllegalAccessException e) { |
||||
|
LOGGER.error("设置合并单元格的初始值异常:"+e.getMessage()); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
return filedValue; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
} |
@ -0,0 +1,147 @@ |
|||||
|
package com.inscloudtech.common.excel; |
||||
|
|
||||
|
|
||||
|
import cn.hutool.core.bean.BeanUtil; |
||||
|
import cn.hutool.json.JSONUtil; |
||||
|
import com.alibaba.excel.annotation.ExcelProperty; |
||||
|
import com.alibaba.excel.context.AnalysisContext; |
||||
|
import com.alibaba.excel.event.AnalysisEventListener; |
||||
|
import com.alibaba.excel.metadata.CellExtra; |
||||
|
import com.alibaba.excel.read.metadata.holder.ReadRowHolder; |
||||
|
|
||||
|
import org.slf4j.Logger; |
||||
|
import org.slf4j.LoggerFactory; |
||||
|
import org.springframework.beans.BeanUtils; |
||||
|
|
||||
|
|
||||
|
import java.lang.reflect.Field; |
||||
|
import java.lang.reflect.InvocationTargetException; |
||||
|
import java.util.ArrayList; |
||||
|
import java.util.Arrays; |
||||
|
import java.util.List; |
||||
|
import java.util.Objects; |
||||
|
import java.util.stream.Collectors; |
||||
|
|
||||
|
/** |
||||
|
* Excel模板的读取监听类 |
||||
|
* |
||||
|
* @author wangwei |
||||
|
*/ |
||||
|
public class ImportExcelListener<T> extends AnalysisEventListener<T> { |
||||
|
private static final Logger LOGGER = LoggerFactory.getLogger(ImportExcelListener.class); |
||||
|
/** |
||||
|
* 解析的数据 |
||||
|
*/ |
||||
|
List<T> list = new ArrayList<>(); |
||||
|
|
||||
|
/** |
||||
|
* 正文起始行 |
||||
|
*/ |
||||
|
private Integer headRowNumber; |
||||
|
/** |
||||
|
* 合并单元格 |
||||
|
*/ |
||||
|
private List<CellExtra> extraMergeInfoList = new ArrayList<>(); |
||||
|
|
||||
|
public ImportExcelListener(Integer headRowNumber) { |
||||
|
this.headRowNumber = headRowNumber; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 这个每一条数据解析都会来调用 |
||||
|
* |
||||
|
* @param data one row value. Is is same as {@link AnalysisContext#readRowHolder()} |
||||
|
* @param context context |
||||
|
*/ |
||||
|
@Override |
||||
|
public void invoke(T data, AnalysisContext context) { |
||||
|
// 如果一行Excel数据均为空值,则不装载该行数据 |
||||
|
if(isLineNullValue(data)){ |
||||
|
return; |
||||
|
} |
||||
|
LOGGER.info("解析到一条数据: {}", JSONUtil.parseObj(data)); |
||||
|
// 获取Excle行号(从0开始) |
||||
|
ReadRowHolder readRowHolder = context.readRowHolder(); |
||||
|
Integer rowIndex = readRowHolder.getRowIndex(); |
||||
|
// try { |
||||
|
BeanUtil.setProperty(data, "lineNo", rowIndex+1); |
||||
|
// } catch (IllegalAccessException e) { |
||||
|
// LOGGER.error("ImportExcelListener.invoke 设置行号异常: ", e); |
||||
|
// } catch (InvocationTargetException e) { |
||||
|
// LOGGER.error("ImportExcelListener.invoke 设置行号异常: ", e); |
||||
|
// } |
||||
|
list.add(data); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 所有数据解析完成了 都会来调用 |
||||
|
* |
||||
|
* @param context context |
||||
|
*/ |
||||
|
@Override |
||||
|
public void doAfterAllAnalysed(AnalysisContext context) { |
||||
|
LOGGER.info("所有数据解析完成!"); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 返回解析出来的List |
||||
|
*/ |
||||
|
public List<T> getData() { |
||||
|
return list; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 读取额外信息:合并单元格 |
||||
|
*/ |
||||
|
@Override |
||||
|
public void extra(CellExtra extra, AnalysisContext context) { |
||||
|
switch (extra.getType()) { |
||||
|
case MERGE: { |
||||
|
LOGGER.info( |
||||
|
"额外信息是合并单元格,而且覆盖了一个区间,在firstRowIndex:{},firstColumnIndex;{},lastRowIndex:{},lastColumnIndex:{}", |
||||
|
extra.getFirstRowIndex(), extra.getFirstColumnIndex(), extra.getLastRowIndex(), |
||||
|
extra.getLastColumnIndex()); |
||||
|
if (extra.getRowIndex() >= headRowNumber) { |
||||
|
extraMergeInfoList.add(extra); |
||||
|
} |
||||
|
break; |
||||
|
} |
||||
|
default: |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 返回解析出来的合并单元格List |
||||
|
*/ |
||||
|
public List<CellExtra> getExtraMergeInfoList() { |
||||
|
return extraMergeInfoList; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 判断整行单元格数据是否均为空 true是 false否 |
||||
|
*/ |
||||
|
private boolean isLineNullValue(T data) { |
||||
|
if (data instanceof String) { |
||||
|
return Objects.isNull(data); |
||||
|
} |
||||
|
try { |
||||
|
List<Field> fields = Arrays.stream(data.getClass().getDeclaredFields()) |
||||
|
.filter(f -> f.isAnnotationPresent(ExcelProperty.class)) |
||||
|
.collect(Collectors.toList()); |
||||
|
List<Boolean> lineNullList = new ArrayList<>(fields.size()); |
||||
|
for (Field field : fields) { |
||||
|
field.setAccessible(true); |
||||
|
Object value = field.get(data); |
||||
|
if (Objects.isNull(value)) { |
||||
|
lineNullList.add(Boolean.TRUE); |
||||
|
} else { |
||||
|
lineNullList.add(Boolean.FALSE); |
||||
|
} |
||||
|
} |
||||
|
return lineNullList.stream().allMatch(Boolean.TRUE::equals); |
||||
|
} catch (Exception e) { |
||||
|
LOGGER.error("读取数据行[{}]解析失败: {}", data, e.getMessage()); |
||||
|
} |
||||
|
return true; |
||||
|
} |
||||
|
} |
@ -0,0 +1,15 @@ |
|||||
|
package com.inscloudtech.common.excel; |
||||
|
|
||||
|
import com.alibaba.excel.annotation.ExcelIgnore; |
||||
|
import com.baomidou.mybatisplus.annotation.TableField; |
||||
|
import lombok.Data; |
||||
|
import org.dromara.easyes.annotation.IndexField; |
||||
|
|
||||
|
@Data |
||||
|
public class RowIndex { |
||||
|
|
||||
|
@ExcelIgnore |
||||
|
@TableField(exist = false) |
||||
|
@IndexField(exist = false) |
||||
|
private Integer lineNo; |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue