|
@@ -2,14 +2,18 @@ package com.iohao.mmo.excel.listener;
|
|
|
|
|
|
import com.alibaba.excel.context.AnalysisContext;
|
|
import com.alibaba.excel.context.AnalysisContext;
|
|
import com.alibaba.excel.event.AnalysisEventListener;
|
|
import com.alibaba.excel.event.AnalysisEventListener;
|
|
|
|
+import com.alibaba.excel.metadata.CellExtra;
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
|
+import com.iohao.mmo.equip.entity.EquipEntryProperty;
|
|
import com.iohao.mmo.equip.entity.EquipEntryTemplate;
|
|
import com.iohao.mmo.equip.entity.EquipEntryTemplate;
|
|
import com.iohao.mmo.excel.dto.DemoExcelDTO;
|
|
import com.iohao.mmo.excel.dto.DemoExcelDTO;
|
|
|
|
+import com.iohao.mmo.excel.dto.EquipEntryTemplateExcelDTO;
|
|
import com.iohao.mmo.excel.entity.DemoExcel;
|
|
import com.iohao.mmo.excel.entity.DemoExcel;
|
|
import com.iohao.mmo.excel.kit.ConvertKit;
|
|
import com.iohao.mmo.excel.kit.ConvertKit;
|
|
import com.iohao.mmo.excel.service.DemoExcelService;
|
|
import com.iohao.mmo.excel.service.DemoExcelService;
|
|
import com.iohao.mmo.excel.service.EquipEntryTemplateExcelService;
|
|
import com.iohao.mmo.excel.service.EquipEntryTemplateExcelService;
|
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
@@ -17,7 +21,7 @@ import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
|
|
/**
|
|
/**
|
|
- * 装备词条模板excel
|
|
|
|
|
|
+ * 装备词条模板excel(公共列未合并)
|
|
*
|
|
*
|
|
* @author Toby javatangbin@163.com
|
|
* @author Toby javatangbin@163.com
|
|
*/
|
|
*/
|
|
@@ -27,20 +31,28 @@ public class EquipEntryTemplateExcelListener<E, T> extends AnalysisEventListener
|
|
* 每隔2000条存储数据库,然后清理list,方便内存回收
|
|
* 每隔2000条存储数据库,然后清理list,方便内存回收
|
|
*/
|
|
*/
|
|
private static final int BATCH_COUNT = 2000;
|
|
private static final int BATCH_COUNT = 2000;
|
|
- private final List<EquipEntryTemplate> list = new ArrayList<>();
|
|
|
|
|
|
+ private final List<EquipEntryTemplate> equipEntryTemplateList = new ArrayList<>();
|
|
|
|
+
|
|
|
|
+ private EquipEntryTemplate nowEquipEntryTemplate;
|
|
|
|
+ private final List<EquipEntryProperty> equipEntryPropertyList = new ArrayList<>();
|
|
|
|
|
|
/**
|
|
/**
|
|
* 通过构造器注入Service
|
|
* 通过构造器注入Service
|
|
*/
|
|
*/
|
|
private final EquipEntryTemplateExcelService equipEntryTemplateExcelService;
|
|
private final EquipEntryTemplateExcelService equipEntryTemplateExcelService;
|
|
|
|
+ /**
|
|
|
|
+ * 表头行数
|
|
|
|
+ */
|
|
|
|
+ int headRowNumber = 1;
|
|
|
|
|
|
/**
|
|
/**
|
|
* 构造方法
|
|
* 构造方法
|
|
*
|
|
*
|
|
* @param equipEntryTemplateExcelService Service对象
|
|
* @param equipEntryTemplateExcelService Service对象
|
|
*/
|
|
*/
|
|
- public EquipEntryTemplateExcelListener(EquipEntryTemplateExcelService equipEntryTemplateExcelService) {
|
|
|
|
|
|
+ public EquipEntryTemplateExcelListener(EquipEntryTemplateExcelService equipEntryTemplateExcelService,int headRowNumber) {
|
|
this.equipEntryTemplateExcelService = equipEntryTemplateExcelService;
|
|
this.equipEntryTemplateExcelService = equipEntryTemplateExcelService;
|
|
|
|
+ this.headRowNumber = headRowNumber;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -49,20 +61,67 @@ public class EquipEntryTemplateExcelListener<E, T> extends AnalysisEventListener
|
|
@Override
|
|
@Override
|
|
public void invoke(T data, AnalysisContext context) {
|
|
public void invoke(T data, AnalysisContext context) {
|
|
LOGGER.info("解析到一条数据:{}", JSONObject.toJSONString(data));
|
|
LOGGER.info("解析到一条数据:{}", JSONObject.toJSONString(data));
|
|
- DemoExcelDTO excel = ConvertKit.sourceToTarget(data, DemoExcelDTO.class);
|
|
|
|
|
|
+ EquipEntryTemplateExcelDTO excel = ConvertKit.sourceToTarget(data, EquipEntryTemplateExcelDTO.class);
|
|
|
|
+ if(StringUtils.isNotBlank(excel.getCode())){
|
|
|
|
+ // 这里对数据做自定义设置
|
|
|
|
+ nowEquipEntryTemplate = ConvertKit.sourceToTarget(excel, EquipEntryTemplate.class);
|
|
|
|
|
|
- // 这里对数据做自定义设置
|
|
|
|
- EquipEntryTemplate entity = ConvertKit.sourceToTarget(excel, EquipEntryTemplate.class);
|
|
|
|
|
|
+ equipEntryTemplateList.add(nowEquipEntryTemplate);
|
|
|
|
+ }
|
|
|
|
|
|
- list.add(entity);
|
|
|
|
|
|
+ EquipEntryProperty equipEntryProperty = ConvertKit.sourceToTarget(excel, EquipEntryProperty.class);
|
|
|
|
+ List<EquipEntryProperty> lastPropertyList = nowEquipEntryTemplate.getEntryPropertyList();
|
|
|
|
+ if(lastPropertyList==null){
|
|
|
|
+ lastPropertyList = new ArrayList<>();
|
|
|
|
+ }
|
|
|
|
+ lastPropertyList.add(equipEntryProperty);
|
|
|
|
+ nowEquipEntryTemplate.setEntryPropertyList(lastPropertyList);
|
|
|
|
|
|
// 达到BATCH_COUNT了,需要去存储一次数据库,防止数据几万条数据在内存,容易OOM
|
|
// 达到BATCH_COUNT了,需要去存储一次数据库,防止数据几万条数据在内存,容易OOM
|
|
- if (list.size() >= BATCH_COUNT) {
|
|
|
|
|
|
+ if (equipEntryTemplateList.size() >= BATCH_COUNT) {
|
|
saveData();
|
|
saveData();
|
|
// 存储完成清理 list
|
|
// 存储完成清理 list
|
|
- list.clear();
|
|
|
|
|
|
+ equipEntryTemplateList.clear();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+ @Override
|
|
|
|
+ public void extra(CellExtra extra, AnalysisContext context) {
|
|
|
|
+ switch (extra.getType()) {
|
|
|
|
+ case COMMENT: {
|
|
|
|
+ LOGGER.info("额外信息是批注,在rowIndex:{},columnIndex;{},内容是:{}", extra.getRowIndex(), extra.getColumnIndex(),
|
|
|
|
+ extra.getText());
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ case HYPERLINK: {
|
|
|
|
+ if ("Sheet1!A1".equals(extra.getText())) {
|
|
|
|
+ LOGGER.info("额外信息是超链接,在rowIndex:{},columnIndex;{},内容是:{}", extra.getRowIndex(),
|
|
|
|
+ extra.getColumnIndex(), extra.getText());
|
|
|
|
+ } else if ("Sheet2!A1".equals(extra.getText())) {
|
|
|
|
+ LOGGER.info(
|
|
|
|
+ "额外信息是超链接,而且覆盖了一个区间,在firstRowIndex:{},firstColumnIndex;{},lastRowIndex:{},lastColumnIndex:{},"
|
|
|
|
+ + "内容是:{}",
|
|
|
|
+ extra.getFirstRowIndex(), extra.getFirstColumnIndex(), extra.getLastRowIndex(),
|
|
|
|
+ extra.getLastColumnIndex(), extra.getText());
|
|
|
|
+ } else {
|
|
|
|
+ LOGGER.info("Unknown hyperlink!");
|
|
|
|
+ }
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ 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: {
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 所有数据解析完成了 都会来调用
|
|
* 所有数据解析完成了 都会来调用
|
|
*/
|
|
*/
|
|
@@ -76,10 +135,10 @@ public class EquipEntryTemplateExcelListener<E, T> extends AnalysisEventListener
|
|
* 加上存储数据库
|
|
* 加上存储数据库
|
|
*/
|
|
*/
|
|
private void saveData() {
|
|
private void saveData() {
|
|
- LOGGER.info("{}条数据,开始存储数据库!", list.size());
|
|
|
|
|
|
+ LOGGER.info("{}条数据,开始存储数据库!", equipEntryTemplateList.size());
|
|
try {
|
|
try {
|
|
//根据code批量更新或者插入
|
|
//根据code批量更新或者插入
|
|
- equipEntryTemplateExcelService.updateBatch(list);
|
|
|
|
|
|
+ equipEntryTemplateExcelService.updateBatch(equipEntryTemplateList);
|
|
} catch (JsonProcessingException e) {
|
|
} catch (JsonProcessingException e) {
|
|
throw new RuntimeException(e);
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|