|
@@ -0,0 +1,136 @@
|
|
|
+package com.iohao.mmo.excel.listener;
|
|
|
+
|
|
|
+import com.alibaba.excel.context.AnalysisContext;
|
|
|
+import com.alibaba.excel.event.AnalysisEventListener;
|
|
|
+import com.alibaba.excel.metadata.CellExtra;
|
|
|
+import com.alibaba.fastjson2.JSONObject;
|
|
|
+import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
+import com.iohao.mmo.common.kit.ConvertKit;
|
|
|
+import com.iohao.mmo.equip.entity.EquipQuality;
|
|
|
+import com.iohao.mmo.excel.dto.EquipQualityExcelDTO;
|
|
|
+import com.iohao.mmo.excel.service.EquipQualityExcelService;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 装备品质库excel(公共列未合并)
|
|
|
+ *
|
|
|
+ * @author Toby javatangbin@163.com
|
|
|
+ */
|
|
|
+public class EquipQualityExcelListener<E, T> extends AnalysisEventListener<T> {
|
|
|
+ private static final Logger LOGGER = LoggerFactory.getLogger(EquipQualityExcelListener.class);
|
|
|
+ /**
|
|
|
+ * 每隔2000条存储数据库,然后清理list,方便内存回收
|
|
|
+ */
|
|
|
+ private static final int BATCH_COUNT = 2000;
|
|
|
+ private final List<EquipQuality> equipQualityList = new ArrayList<>();
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通过构造器注入Service
|
|
|
+ */
|
|
|
+ private final EquipQualityExcelService equipQualityExcelService;
|
|
|
+ /**
|
|
|
+ * 表头行数
|
|
|
+ */
|
|
|
+ int headRowNumber = 1;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 构造方法
|
|
|
+ *
|
|
|
+ * @param equipQualityExcelService Service对象
|
|
|
+ */
|
|
|
+ public EquipQualityExcelListener(EquipQualityExcelService equipQualityExcelService, int headRowNumber) {
|
|
|
+ this.equipQualityExcelService = equipQualityExcelService;
|
|
|
+ this.headRowNumber = headRowNumber;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 每条数据解析完,都会调用此方法
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void invoke(T data, AnalysisContext context) {
|
|
|
+ //装备属性浮动,最小值为最大值的多少比例
|
|
|
+ double minPct = 0.8;
|
|
|
+ LOGGER.info("解析到一条数据:{}", JSONObject.toJSONString(data));
|
|
|
+ EquipQualityExcelDTO excel = ConvertKit.sourceToTarget(data, EquipQualityExcelDTO.class);
|
|
|
+
|
|
|
+ EquipQuality equipQuality = ConvertKit.sourceToTarget(excel, EquipQuality.class);
|
|
|
+ //随机权重
|
|
|
+ if(StringUtils.isNotBlank(excel.getPrizeWeightStr())) {
|
|
|
+ String[] prizeWeightStrArr = excel.getPrizeWeightStr().split("-");
|
|
|
+ equipQuality.setPrizeWeightArr(Arrays.stream(prizeWeightStrArr).mapToInt(Integer::parseInt).toArray());
|
|
|
+ }
|
|
|
+
|
|
|
+ equipQualityList.add(equipQuality);
|
|
|
+ // 达到BATCH_COUNT了,需要去存储一次数据库,防止数据几万条数据在内存,容易OOM
|
|
|
+ if (equipQualityList.size() >= BATCH_COUNT) {
|
|
|
+ saveData();
|
|
|
+ // 存储完成清理 list
|
|
|
+ equipQualityList.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: {
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 所有数据解析完成了 都会来调用
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void doAfterAllAnalysed(AnalysisContext context) {
|
|
|
+ // 这里也要保存数据,确保最后遗留的数据也存储到数据库
|
|
|
+ saveData();
|
|
|
+ LOGGER.info("所有数据解析完成!");
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 加上存储数据库
|
|
|
+ */
|
|
|
+ private void saveData() {
|
|
|
+ LOGGER.info("{}条数据,开始存储数据库!", equipQualityList.size());
|
|
|
+ try {
|
|
|
+ //根据code批量更新或者插入
|
|
|
+ equipQualityExcelService.updateBatch(equipQualityList);
|
|
|
+ } catch (JsonProcessingException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ LOGGER.info("存储数据库成功!");
|
|
|
+ }
|
|
|
+}
|