|
@@ -2,15 +2,29 @@ package org.jeecg.modules.wzOutboundOrderBNew.monitor;
|
|
|
|
|
|
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.fastjson.JSONObject;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
+import org.apache.poi.ss.usermodel.Workbook;
|
|
|
|
+import org.apache.poi.ss.usermodel.Sheet;
|
|
|
|
+import org.apache.poi.ss.usermodel.Row;
|
|
|
|
+import org.apache.shiro.SecurityUtils;
|
|
import org.checkerframework.checker.index.qual.SameLen;
|
|
import org.checkerframework.checker.index.qual.SameLen;
|
|
|
|
+import org.jeecg.common.system.vo.LoginUser;
|
|
|
|
+import org.jeecg.modules.message.websocket.WebSocket;
|
|
|
|
+import org.jeecg.modules.system.service.ISysUserService;
|
|
import org.jeecg.modules.wzOutboundOrderBNew.entity.WzOutboundOrderBNew;
|
|
import org.jeecg.modules.wzOutboundOrderBNew.entity.WzOutboundOrderBNew;
|
|
import org.jeecg.modules.wzOutboundOrderBNew.mapper.WzOutboundOrderBNewMapper;
|
|
import org.jeecg.modules.wzOutboundOrderBNew.mapper.WzOutboundOrderBNewMapper;
|
|
import org.jeecg.modules.wzOutboundOrderBNew.service.IWzOutboundOrderBNewService;
|
|
import org.jeecg.modules.wzOutboundOrderBNew.service.IWzOutboundOrderBNewService;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
+import javax.annotation.Resource;
|
|
import java.nio.file.LinkOption;
|
|
import java.nio.file.LinkOption;
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
+import java.util.concurrent.ExecutorService;
|
|
|
|
+import java.util.concurrent.Executors;
|
|
|
|
+import java.util.concurrent.atomic.AtomicInteger;
|
|
|
|
|
|
/**
|
|
/**
|
|
* ClassName: WzOutboundOrderBNewListerner
|
|
* ClassName: WzOutboundOrderBNewListerner
|
|
@@ -24,15 +38,21 @@ import java.util.List;
|
|
@Slf4j
|
|
@Slf4j
|
|
public class WzOutboundOrderBNewListerner extends AnalysisEventListener<WzOutboundOrderBNew> {
|
|
public class WzOutboundOrderBNewListerner extends AnalysisEventListener<WzOutboundOrderBNew> {
|
|
private IWzOutboundOrderBNewService wzOutboundOrderBNewService;
|
|
private IWzOutboundOrderBNewService wzOutboundOrderBNewService;
|
|
|
|
+
|
|
//每隔1000条存储数据库,实际使用中可以3000条,然后清理list,方便内存回收
|
|
//每隔1000条存储数据库,实际使用中可以3000条,然后清理list,方便内存回收
|
|
private static final int BATCH_COUNT = 3000;
|
|
private static final int BATCH_COUNT = 3000;
|
|
List<WzOutboundOrderBNew> list = new ArrayList<>();
|
|
List<WzOutboundOrderBNew> list = new ArrayList<>();
|
|
|
|
+ private WebSocket webSocket;
|
|
|
|
+
|
|
|
|
+ Integer currentRow = 0;
|
|
|
|
+ Integer totalRows = 0;
|
|
|
|
|
|
public WzOutboundOrderBNewListerner() {
|
|
public WzOutboundOrderBNewListerner() {
|
|
}
|
|
}
|
|
|
|
|
|
- public WzOutboundOrderBNewListerner(IWzOutboundOrderBNewService wzOutboundOrderBNewService) {
|
|
|
|
|
|
+ public WzOutboundOrderBNewListerner(IWzOutboundOrderBNewService wzOutboundOrderBNewService, WebSocket webSocket) {
|
|
this.wzOutboundOrderBNewService = wzOutboundOrderBNewService;
|
|
this.wzOutboundOrderBNewService = wzOutboundOrderBNewService;
|
|
|
|
+ this.webSocket = webSocket;
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
@@ -52,7 +72,9 @@ public class WzOutboundOrderBNewListerner extends AnalysisEventListener<WzOutbou
|
|
public void doAfterAllAnalysed(AnalysisContext analysisContext){
|
|
public void doAfterAllAnalysed(AnalysisContext analysisContext){
|
|
//这里也要保存数据,确保最后遗留的数据也存储到数据库
|
|
//这里也要保存数据,确保最后遗留的数据也存储到数据库
|
|
saveData();
|
|
saveData();
|
|
- log.info("所有数据解析完成!");
|
|
|
|
|
|
+ log.info("当前表所有数据解析完成!");
|
|
|
|
+
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
|
|
//存储数据库
|
|
//存储数据库
|
|
@@ -60,6 +82,139 @@ public class WzOutboundOrderBNewListerner extends AnalysisEventListener<WzOutbou
|
|
log.info("{}条数据,开始存储数据库!", list.size());
|
|
log.info("{}条数据,开始存储数据库!", list.size());
|
|
wzOutboundOrderBNewService.saveBatch(list);
|
|
wzOutboundOrderBNewService.saveBatch(list);
|
|
log.info("数据存储数据库成功!");
|
|
log.info("数据存储数据库成功!");
|
|
|
|
+ if((currentRow+1)==totalRows){
|
|
|
|
+ sendProgressToFrontEnd(100);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public boolean hasNext(AnalysisContext analysisContext) {
|
|
|
|
+ currentRow = analysisContext.getCurrentRowNum();
|
|
|
|
+ totalRows = analysisContext.getTotalCount();
|
|
|
|
+
|
|
|
|
+ int progress = 0;
|
|
|
|
+ // 根据当前行数和总行数计算进度
|
|
|
|
+ if(currentRow>0){
|
|
|
|
+ progress = (currentRow * 100) / totalRows;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 将进度信息发送到前端
|
|
|
|
+ sendProgressToFrontEnd(progress);
|
|
|
|
+ try {
|
|
|
|
+ Thread.sleep(1000);
|
|
|
|
+ } catch (InterruptedException e) {
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
+ }
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private void sendProgressToFrontEnd(int progress) {
|
|
|
|
+ // 这里实现将进度数据发送到前端的逻辑
|
|
|
|
+ // 例如,可以通过WebSocket、HTTP Long Polling或其他方式发送
|
|
|
|
+ System.out.println("进度:" + progress + "%");
|
|
|
|
+
|
|
|
|
+ LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
|
|
|
|
+ String userId = sysUser.getId();
|
|
|
|
+ System.out.println(userId);
|
|
|
|
+
|
|
|
|
+ //创建业务消息信息
|
|
|
|
+ JSONObject obj = new JSONObject();
|
|
|
|
+ obj.put("cmd", "user");//业务类型
|
|
|
|
+ obj.put("msgId", userId);//消息id
|
|
|
|
+ obj.put("msgTxt", progress);//消息内容
|
|
|
|
+ System.out.println(obj.toJSONString());
|
|
|
|
+ //单个用户发送 (userId为用户id)
|
|
|
|
+ webSocket.sendMessage(userId, obj.toJSONString());
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+//public class WzOutboundOrderBNewListerner extends AnalysisEventListener<WzOutboundOrderBNew> {
|
|
|
|
+// private IWzOutboundOrderBNewService wzOutboundOrderBNewService;
|
|
|
|
+//
|
|
|
|
+// private ExecutorService executorService = Executors.newFixedThreadPool(5);
|
|
|
|
+//
|
|
|
|
+// private ThreadLocal<ArrayList<WzOutboundOrderBNew>> wzOutboundOrderBNewList = ThreadLocal.withInitial(ArrayList::new);
|
|
|
|
+//
|
|
|
|
+// //每隔1000条存储数据库,实际使用中可以3000条,然后清理list,方便内存回收
|
|
|
|
+// private static final int batchSize = 1000;
|
|
|
|
+//
|
|
|
|
+// private static AtomicInteger count = new AtomicInteger(1);
|
|
|
|
+//// List<WzOutboundOrderBNew> list = new ArrayList<>();
|
|
|
|
+//
|
|
|
|
+// public WzOutboundOrderBNewListerner() {
|
|
|
|
+// }
|
|
|
|
+//
|
|
|
|
+// public WzOutboundOrderBNewListerner(IWzOutboundOrderBNewService wzOutboundOrderBNewService) {
|
|
|
|
+// this.wzOutboundOrderBNewService = wzOutboundOrderBNewService;
|
|
|
|
+// }
|
|
|
|
+//
|
|
|
|
+// @Override
|
|
|
|
+// public void invoke(WzOutboundOrderBNew wzOutboundOrderBNew, AnalysisContext analysisContext){
|
|
|
|
+//// System.out.println(wzOutboundOrderBNew);
|
|
|
|
+// wzOutboundOrderBNewList.get().add(wzOutboundOrderBNew);
|
|
|
|
+//// System.out.println(list);
|
|
|
|
+// //达到BATCH_COUNT了,需要去存储一次数据库,防止数据几万条数据在内存,容易OOM
|
|
|
|
+// if(wzOutboundOrderBNewList.get().size() >= batchSize){
|
|
|
|
+//// saveData();
|
|
|
|
+// asyncSaveData();
|
|
|
|
+// //存储完成清理list
|
|
|
|
+//// list.clear();
|
|
|
|
+// }
|
|
|
|
+// }
|
|
|
|
+//
|
|
|
|
+// @Override
|
|
|
|
+// public void doAfterAllAnalysed(AnalysisContext analysisContext){
|
|
|
|
+// log.info("一个Sheet全部处理完");
|
|
|
|
+// if (wzOutboundOrderBNewList.get().size() >= batchSize) {
|
|
|
|
+// saveData();
|
|
|
|
+// }
|
|
|
|
+// }
|
|
|
|
+//
|
|
|
|
+// //存储数据库
|
|
|
|
+// public void saveOne(WzOutboundOrderBNew data){
|
|
|
|
+// wzOutboundOrderBNewService.save(data);
|
|
|
|
+// log.info("第" + count.getAndAdd(1) + "次插入1条数据");
|
|
|
|
+// }
|
|
|
|
+// private void saveData(){
|
|
|
|
+// if (!wzOutboundOrderBNewList.get().isEmpty()) {
|
|
|
|
+// log.info("{}条数据,开始存储数据库!", wzOutboundOrderBNewList.get().size());
|
|
|
|
+// wzOutboundOrderBNewService.saveBatch(wzOutboundOrderBNewList.get());
|
|
|
|
+// log.info("第" + count.getAndAdd(1) + "次插入" + wzOutboundOrderBNewList.get().size() + "条数据");
|
|
|
|
+// wzOutboundOrderBNewList.get().clear();
|
|
|
|
+// }
|
|
|
|
+// }
|
|
|
|
+//
|
|
|
|
+// public void asyncSaveData() {
|
|
|
|
+// if (!wzOutboundOrderBNewList.get().isEmpty()) {
|
|
|
|
+// ArrayList<WzOutboundOrderBNew> wzOutboundOrderBNews = (ArrayList<WzOutboundOrderBNew>) wzOutboundOrderBNewList.get().clone();
|
|
|
|
+// executorService.execute(new SaveTask(wzOutboundOrderBNews, wzOutboundOrderBNewService));
|
|
|
|
+// wzOutboundOrderBNewList.get().clear();
|
|
|
|
+// }
|
|
|
|
+// }
|
|
|
|
+//
|
|
|
|
+// static class SaveTask implements Runnable {
|
|
|
|
+// private List<WzOutboundOrderBNew> wzOutboundOrderBNewList;
|
|
|
|
+// private IWzOutboundOrderBNewService wzOutboundOrderBNewService;
|
|
|
|
+//
|
|
|
|
+// public SaveTask(List<WzOutboundOrderBNew> wzOutboundOrderBNewList, IWzOutboundOrderBNewService wzOutboundOrderBNewService) {
|
|
|
|
+// this.wzOutboundOrderBNewList = wzOutboundOrderBNewList;
|
|
|
|
+// this.wzOutboundOrderBNewService = wzOutboundOrderBNewService;
|
|
|
|
+// }
|
|
|
|
+//
|
|
|
|
+// @Override
|
|
|
|
+// public void run() {
|
|
|
|
+// wzOutboundOrderBNewService.saveBatch(wzOutboundOrderBNewList);
|
|
|
|
+// log.info("第" + count.getAndAdd(1) + "次插入" + wzOutboundOrderBNewList.size() + "条数据");
|
|
|
|
+// }
|
|
|
|
+// }
|
|
|
|
+//
|
|
|
|
+//}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|