初始化
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
package tech.easyflow.datacenter.excel;
|
||||
|
||||
import cn.idev.excel.context.AnalysisContext;
|
||||
import cn.idev.excel.metadata.data.ReadCellData;
|
||||
import cn.idev.excel.read.listener.ReadListener;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import tech.easyflow.common.entity.LoginAccount;
|
||||
import tech.easyflow.common.util.SpringContextUtil;
|
||||
import tech.easyflow.datacenter.entity.DatacenterTableField;
|
||||
import tech.easyflow.datacenter.service.DatacenterTableService;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.*;
|
||||
|
||||
public class ReadDataListener implements ReadListener<LinkedHashMap<Integer, Object>> {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(ReadDataListener.class);
|
||||
|
||||
private BigInteger tableId;
|
||||
|
||||
private List<DatacenterTableField> fields;
|
||||
|
||||
private LoginAccount loginAccount;
|
||||
|
||||
private final Map<String, Integer> headFieldIndex = new HashMap<>();
|
||||
|
||||
private int successCount = 0;
|
||||
private int errorCount = 0;
|
||||
private int totalCount = 0;
|
||||
|
||||
private final List<JSONObject> errorRows = new ArrayList<>();
|
||||
|
||||
public ReadDataListener() {
|
||||
}
|
||||
|
||||
public ReadDataListener(BigInteger tableId, List<DatacenterTableField> fields, LoginAccount loginAccount) {
|
||||
this.tableId = tableId;
|
||||
this.fields = fields;
|
||||
this.loginAccount = loginAccount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invoke(LinkedHashMap<Integer, Object> o, AnalysisContext analysisContext) {
|
||||
DatacenterTableService service = SpringContextUtil.getBean(DatacenterTableService.class);
|
||||
JSONObject obj = new JSONObject();
|
||||
for (DatacenterTableField field : fields) {
|
||||
String fieldName = field.getFieldName();
|
||||
Integer i = headFieldIndex.get(fieldName);
|
||||
if (i != null) {
|
||||
obj.put(fieldName, o.get(i));
|
||||
}
|
||||
}
|
||||
try {
|
||||
service.saveValue(tableId, obj, loginAccount);
|
||||
successCount++;
|
||||
} catch (Exception e) {
|
||||
errorCount++;
|
||||
log.error("导入数据到数据中枢失败,具体值:{}", obj, e);
|
||||
errorRows.add(obj);
|
||||
}
|
||||
totalCount++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invokeHead(Map<Integer, ReadCellData<?>> headMap, AnalysisContext context) {
|
||||
Set<Map.Entry<Integer, ReadCellData<?>>> entries = headMap.entrySet();
|
||||
for (Map.Entry<Integer, ReadCellData<?>> entry : entries) {
|
||||
Integer key = entry.getKey();
|
||||
String field = entry.getValue().getStringValue();
|
||||
headFieldIndex.put(field, key);
|
||||
}
|
||||
if (headFieldIndex.size() != fields.size()) {
|
||||
throw new RuntimeException("表头字段数量与表结构对应不上!");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
|
||||
|
||||
}
|
||||
|
||||
public List<DatacenterTableField> getFields() {
|
||||
return fields;
|
||||
}
|
||||
|
||||
public void setFields(List<DatacenterTableField> fields) {
|
||||
this.fields = fields;
|
||||
}
|
||||
|
||||
public int getSuccessCount() {
|
||||
return successCount;
|
||||
}
|
||||
|
||||
public int getErrorCount() {
|
||||
return errorCount;
|
||||
}
|
||||
|
||||
public int getTotalCount() {
|
||||
return totalCount;
|
||||
}
|
||||
|
||||
public List<JSONObject> getErrorRows() {
|
||||
return errorRows;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package tech.easyflow.datacenter.excel;
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class ReadResVo {
|
||||
|
||||
/**
|
||||
* 成功数
|
||||
*/
|
||||
private int successCount = 0;
|
||||
/**
|
||||
* 失败数
|
||||
*/
|
||||
private int errorCount = 0;
|
||||
/**
|
||||
* 总数
|
||||
*/
|
||||
private int totalCount = 0;
|
||||
|
||||
/**
|
||||
* 错误行
|
||||
*/
|
||||
private List<JSONObject> errorRows;
|
||||
|
||||
public ReadResVo() {
|
||||
}
|
||||
|
||||
public ReadResVo(int successCount, int errorCount, int totalCount, List<JSONObject> errorRows) {
|
||||
this.successCount = successCount;
|
||||
this.errorCount = errorCount;
|
||||
this.totalCount = totalCount;
|
||||
this.errorRows = errorRows;
|
||||
}
|
||||
|
||||
public int getSuccessCount() {
|
||||
return successCount;
|
||||
}
|
||||
|
||||
public void setSuccessCount(int successCount) {
|
||||
this.successCount = successCount;
|
||||
}
|
||||
|
||||
public int getErrorCount() {
|
||||
return errorCount;
|
||||
}
|
||||
|
||||
public void setErrorCount(int errorCount) {
|
||||
this.errorCount = errorCount;
|
||||
}
|
||||
|
||||
public int getTotalCount() {
|
||||
return totalCount;
|
||||
}
|
||||
|
||||
public void setTotalCount(int totalCount) {
|
||||
this.totalCount = totalCount;
|
||||
}
|
||||
|
||||
public List<JSONObject> getErrorRows() {
|
||||
return errorRows;
|
||||
}
|
||||
|
||||
public void setErrorRows(List<JSONObject> errorRows) {
|
||||
this.errorRows = errorRows;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user