Browse Source

不动产合并单元格导入

master
583641232@qq.com 1 year ago
parent
commit
242041d514
  1. 8
      cas-admin/pom.xml
  2. 10
      cas-admin/src/main/java/com/inscloudtech/web/controller/datacenter/RealEstateController.java
  3. 51
      cas-admin/src/main/java/com/inscloudtech/web/controller/querycenter/QueryCenterController.java
  4. 152
      cas-common/src/main/java/com/inscloudtech/common/excel/ImportExcelHelper.java
  5. 147
      cas-common/src/main/java/com/inscloudtech/common/excel/ImportExcelListener.java
  6. 15
      cas-common/src/main/java/com/inscloudtech/common/excel/RowIndex.java
  7. 13
      cas-common/src/main/java/com/inscloudtech/common/utils/BeanCopyUtils.java
  8. 12
      cas-system/pom.xml
  9. 20
      cas-system/src/main/java/com/inscloudtech/datacenter/domain/RealEstate.java
  10. 3
      cas-system/src/main/java/com/inscloudtech/datacenter/service/QueryCenterService.java
  11. 2
      cas-system/src/main/java/com/inscloudtech/datacenter/service/impl/PlateNumberServiceImpl.java
  12. 4
      cas-system/src/main/java/com/inscloudtech/system/service/impl/SysOperLogServiceImpl.java
  13. 3
      cas-system/src/main/java/com/inscloudtech/system/service/impl/SysOssServiceImpl.java

8
cas-admin/pom.xml

@ -29,11 +29,6 @@
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
</dependency>
<!-- SqlServer -->
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
</dependency>
<!-- 核心模块-->
<dependency>
@ -59,7 +54,6 @@
</dependency>
</dependencies>
<build>
@ -98,7 +92,7 @@
<password>#</password><!-- #表示启动时不需要密码,事实上对于代码混淆来说,这个密码没什么用,它只是一个启动密码 -->
<excludes>org.spring</excludes>
<packages>com.inscloudtech</packages><!-- 加密的包名,多个包用逗号分开 -->
<cfgfiles>application.yml,application-dev.yml,application-prod.yml,application-soft.yml</cfgfiles>
<cfgfiles>application.yml,application-dev.yml,application-prod.yml</cfgfiles>
<libjars>cas-system-4.7.0.jar</libjars><!-- jar包lib下面要加密的jar依赖文件,多个包用逗号分开 -->
<!-- <code>78828431441AC7E1D92FC694D6886239D41D8CD98F00B204E9800998ECF8427ED41D8CD98F00B204E9800998ECF8427E</code> &lt;!&ndash; 指定机器启动,机器码 &ndash;&gt;-->
</configuration>

10
cas-admin/src/main/java/com/inscloudtech/web/controller/datacenter/RealEstateController.java

@ -2,6 +2,7 @@ package com.inscloudtech.web.controller.datacenter;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONUtil;
import com.inscloudtech.analysiscenter.domain.AnalysisDto;
import com.inscloudtech.common.annotation.Log;
import com.inscloudtech.common.annotation.UpdateLog;
@ -11,6 +12,7 @@ import com.inscloudtech.common.core.domain.R;
import com.inscloudtech.common.core.page.TableDataInfo;
import com.inscloudtech.common.enums.BusinessType;
import com.inscloudtech.common.excel.ExcelResult;
import com.inscloudtech.common.excel.ImportExcelHelper;
import com.inscloudtech.common.utils.BeanCopyUtils;
import com.inscloudtech.common.utils.poi.ExcelUtil;
import com.inscloudtech.datacenter.domain.RealEstate;
@ -68,8 +70,10 @@ public class RealEstateController extends BaseController {
String importResultStr = "";
int importCount = 0;
try {
ExcelResult<RealEstate> excelResult = ExcelUtil.importExcel(file.getInputStream(), RealEstate.class, true);
List<RealEstate> list = excelResult.getList();
ImportExcelHelper<RealEstate> helper = new ImportExcelHelper<>();
List<RealEstate> list = helper.getListWithInputStream(file.getInputStream(),RealEstate.class,0,1);
// ExcelResult<RealEstate> excelResult = ExcelUtil.importExcel(file.getInputStream(), RealEstate.class, true);
// List<RealEstate> list = excelResult.getList();
if(CollectionUtil.isNotEmpty(list)){
list = iRealEstateService.dataTrimAndDeduplication(list,caseId,getUsername());
result = iRealEstateService.importData(list,caseId,null);
@ -185,4 +189,6 @@ public class RealEstateController extends BaseController {
@PathVariable String[] ids) {
return R.ok(iRealEstateService.deleteWithValidByIds(Arrays.asList(ids), true));
}
}

51
cas-admin/src/main/java/com/inscloudtech/web/controller/querycenter/QueryCenterController.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));
}
}

152
cas-common/src/main/java/com/inscloudtech/common/excel/ImportExcelHelper.java

@ -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;
}
}

147
cas-common/src/main/java/com/inscloudtech/common/excel/ImportExcelListener.java

@ -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;
}
}

15
cas-common/src/main/java/com/inscloudtech/common/excel/RowIndex.java

@ -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;
}

13
cas-common/src/main/java/com/inscloudtech/common/utils/BeanCopyUtils.java

@ -205,6 +205,7 @@ public class BeanCopyUtils {
* @return
*/
public static <T> Map getChangedFields(T oldBean, T newBean){
Field[] fields = newBean.getClass().getDeclaredFields();
StringBuilder beforeBuilder = new StringBuilder();
StringBuilder afterBuilder = new StringBuilder();
@ -216,8 +217,14 @@ public class BeanCopyUtils {
}
try {
Object oldValue = field.get(oldBean);
Object newValue = field.get(newBean);
Object oldValue = null;
if(oldBean != null){
oldValue = field.get(oldBean);
}
Object newValue = null;
if(oldBean != null){
newValue = field.get(newBean);
}
if(ObjectUtil.isNotNull(newValue) && !Objects.equals(newValue, oldValue)) {
fieldName = field.getAnnotation(UpdateValueLog.class).fieldName();
beforeBuilder.append(fieldName);
@ -232,7 +239,7 @@ public class BeanCopyUtils {
}
} catch (Exception e) {
System.out.println(e);
e.printStackTrace();
}
}
Map<String,String> result = new HashMap<>(2);

12
cas-system/pom.xml

@ -66,13 +66,6 @@
<artifactId>cas-framework</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.xmlbeans/xmlbeans -->
<!-- <dependency>-->
<!-- <groupId>org.apache.xmlbeans</groupId>-->
<!-- <artifactId>xmlbeans</artifactId>-->
<!-- <version>2.6.0</version>-->
<!-- </dependency>-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
@ -81,6 +74,11 @@
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
</dependency>
<dependency>
<groupId>com.belerweb</groupId>
<artifactId>pinyin4j</artifactId>
</dependency>
</dependencies>
</project>

20
cas-system/src/main/java/com/inscloudtech/datacenter/domain/RealEstate.java

@ -2,6 +2,7 @@ package com.inscloudtech.datacenter.domain;
import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.baomidou.mybatisplus.annotation.*;
import com.inscloudtech.common.annotation.*;
@ -39,7 +40,7 @@ public class RealEstate extends BaseEntity {
/**
* 姓名
*/
@ExcelProperty("姓名")
@ExcelProperty(value = "姓名",index = 0)
@DeduplicationField
@UpdateValueLog(fieldName = "姓名")
@IndexField(fieldType = FieldType.KEYWORD_TEXT, analyzer = "ngram_analyzer")
@ -47,7 +48,7 @@ public class RealEstate extends BaseEntity {
/**
* 房产地址
*/
@ExcelProperty("房产地址")
@ExcelProperty(value = "房产地址",index = 1)
@DeduplicationField
@UpdateValueLog(fieldName = "房产地址")
@IndexField(fieldType = FieldType.KEYWORD)
@ -55,13 +56,13 @@ public class RealEstate extends BaseEntity {
/**
* 面积m2
*/
@ExcelProperty("面积m2")
@ExcelProperty(value = "面积m2",index = 2)
@DeduplicationField
@UpdateValueLog(fieldName = "面积m2")
@IndexField(fieldType = FieldType.DOUBLE)
private BigDecimal area;
@ExcelProperty("购买价格")
@ExcelProperty(value = "购买价格",index = 3)
@DeduplicationField
@UpdateValueLog(fieldName = "购买价格")
@IndexField(fieldType = FieldType.DOUBLE)
@ -70,7 +71,7 @@ public class RealEstate extends BaseEntity {
* 产权转让信息
*/
@DeduplicationField
@ExcelProperty("产权转让信息")
@ExcelProperty(value = "产权转让信息",index = 4)
@UpdateValueLog(fieldName = "产权转让信息")
@IdCardField
@NameField
@ -80,7 +81,7 @@ public class RealEstate extends BaseEntity {
* 其他信息
*/
@DeduplicationField
@ExcelProperty("其他信息")
@ExcelProperty(value = "其他信息",index = 5)
@UpdateValueLog(fieldName = "其他信息")
@IdCardField
@NameField
@ -91,7 +92,7 @@ public class RealEstate extends BaseEntity {
* 备注
*/
@UpdateValueLog(fieldName = "备注")
@ExcelProperty("备注")
@ExcelProperty(value = "备注",index = 6)
@DeduplicationField
@IndexField(fieldType = FieldType.KEYWORD_TEXT, analyzer = "ngram_analyzer")
@TableField(updateStrategy = FieldStrategy.IGNORED)
@ -109,4 +110,9 @@ public class RealEstate extends BaseEntity {
@IndexField(fieldType = FieldType.KEYWORD)
private String analysisResultId;
@ExcelIgnore
@TableField(exist = false)
@IndexField(exist = false)
private Integer lineNo;
}

3
cas-system/src/main/java/com/inscloudtech/datacenter/service/QueryCenterService.java

@ -615,8 +615,7 @@ public class QueryCenterService {
throw new RuntimeException("[业务模块]不能为空!");
}
JSONObject queryInfo = getQueryInfo(initQueryIndex(1));
String[] indexArr = queryInfo.getBean("indexArr", String[].class);
List<String> indexList = Arrays.asList(indexArr);
List<String> indexList = queryInfo.getBeanList("indexArr", String.class);
if(!indexList.contains(indexName)){
throw new RuntimeException("非法操作!");
}

2
cas-system/src/main/java/com/inscloudtech/datacenter/service/impl/PlateNumberServiceImpl.java

@ -26,6 +26,7 @@ import org.dromara.easyes.core.conditions.select.LambdaEsQueryWrapper;
import org.dromara.easyes.core.core.EsWrappers;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.aop.framework.AopContext;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -77,6 +78,7 @@ public class PlateNumberServiceImpl implements PlateNumberService {
}
@Override
@Async
public void invoke(String caseId) {
PlateNumberInfo bo = new PlateNumberInfo();
bo.setCaseId(caseId);

4
cas-system/src/main/java/com/inscloudtech/system/service/impl/SysOperLogServiceImpl.java

@ -1,6 +1,7 @@
package com.inscloudtech.system.service.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ArrayUtil;
@ -98,6 +99,9 @@ public class SysOperLogServiceImpl implements ISysOperLogService {
*/
@Override
public void insertOperlog(SysOperLog operLog) {
operLog.setOperTime(DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN));
String token = DesUtil.getEncryptString(operLog.getOperTime());
operLog.setToken(token);
baseMapper.insert(operLog);
}
/**

3
cas-system/src/main/java/com/inscloudtech/system/service/impl/SysOssServiceImpl.java

@ -178,6 +178,7 @@ public class SysOssServiceImpl implements ISysOssService {
}
@Override
@Async
public void upload2LocalWithBatchId(MultipartFile file, String caseId, String businessModule, String batchId,int uploadResult) {
SysOss sysOss = new SysOss();
sysOss.setUploadResult(uploadResult);
@ -349,7 +350,7 @@ public class SysOssServiceImpl implements ISysOssService {
}
@Override
@Async
// @Async //todo 异步传递用户信息
public SysOss upload2Local(int importCount,String importResult, MultipartFile file, String caseId, String businessModule) {
SysOss sysOss = new SysOss();
sysOss.setErrorInfo(importResult);

Loading…
Cancel
Save