|
@@ -1,74 +1,91 @@
|
|
|
package org.jeecg.modules.kyTaskTemp.service.impl;
|
|
|
|
|
|
+import com.alibaba.excel.EasyExcel;
|
|
|
+import com.alibaba.excel.EasyExcelFactory;
|
|
|
+import com.alibaba.excel.ExcelReader;
|
|
|
+import com.alibaba.excel.cache.MapCache;
|
|
|
+import com.alibaba.excel.read.metadata.ReadSheet;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.jeecg.common.api.vo.Result;
|
|
|
+import org.jeecg.config.mybatis.ThreadLocalDataHelper;
|
|
|
+import org.jeecg.modules.dataSourceSwitch.annotation.UseMasterDataSource;
|
|
|
import org.jeecg.modules.kyTaskTemp.entity.KyTaskTemp;
|
|
|
import org.jeecg.modules.kyTaskTemp.mapper.KyTaskTempMapper;
|
|
|
import org.jeecg.modules.kyTaskTemp.service.IKyTaskTempService;
|
|
|
-import org.jeecgframework.poi.excel.ExcelImportUtil;
|
|
|
-import org.jeecgframework.poi.excel.entity.ImportParams;
|
|
|
+import org.jeecg.modules.projectImportList.readListener.KyTaskTempImportReadListener;
|
|
|
+import org.springframework.aop.framework.AopContext;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.context.annotation.EnableAspectJAutoProxy;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
-import java.io.FileInputStream;
|
|
|
-import java.io.FileNotFoundException;
|
|
|
-import java.io.IOException;
|
|
|
-import java.io.InputStream;
|
|
|
+import java.io.*;
|
|
|
+import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
+import java.util.concurrent.*;
|
|
|
|
|
|
/**
|
|
|
* @Description: 任务关系映射表
|
|
|
* @Author: jeecg-boot
|
|
|
- * @Date: 2023-09-16
|
|
|
+ * @Date: 2023-09-16
|
|
|
* @Version: V1.0
|
|
|
*/
|
|
|
@Service
|
|
|
@Slf4j
|
|
|
+@UseMasterDataSource
|
|
|
+@EnableAspectJAutoProxy(proxyTargetClass = true, exposeProxy = true)
|
|
|
public class KyTaskTempServiceImpl extends ServiceImpl<KyTaskTempMapper, KyTaskTemp> implements IKyTaskTempService {
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private KyTaskTempMapper tempMapper;
|
|
|
|
|
|
- /**用于文件管理——导入日志——导入外协价格库*/
|
|
|
- public Result<?> importExcel1(String strUrl, Class<KyTaskTemp> clazz){
|
|
|
- InputStream inputStream = null;
|
|
|
- try {
|
|
|
- inputStream = new FileInputStream(strUrl);
|
|
|
- } catch (FileNotFoundException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
|
|
|
- // 获取上传文件对象
|
|
|
- ImportParams params = new ImportParams();
|
|
|
- params.setTitleRows(0);/*表格标题所占行数(默认是0)*/
|
|
|
- params.setHeadRows(1);/*表头所占据的行数行数,默认1,代表标题占据一行*/
|
|
|
- params.setNeedSave(true);
|
|
|
+ /**
|
|
|
+ * 用于文件管理——导入日志——导入外协价格库
|
|
|
+ */
|
|
|
+ public Result<?> importExcel1(String strUrl, Class<KyTaskTemp> clazz) {
|
|
|
+ long startTime = System.currentTimeMillis();
|
|
|
+ File file = new File(strUrl);
|
|
|
+ ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(8, 8, 60, TimeUnit.SECONDS, new ArrayBlockingQueue<>(100));
|
|
|
try {
|
|
|
- List<KyTaskTemp> list = ExcelImportUtil.importExcel(inputStream, clazz, params);
|
|
|
-
|
|
|
- //update-begin-author:taoyan date:20190528 for:批量插入数据
|
|
|
- long start = System.currentTimeMillis();
|
|
|
- this.saveBatch(list);
|
|
|
- //400条 saveBatch消耗时间1592毫秒 循环插入消耗时间1947毫秒
|
|
|
- //1200条 saveBatch消耗时间3687毫秒 循环插入消耗时间5212毫秒
|
|
|
- log.info("消耗时间" + (System.currentTimeMillis() - start) + "毫秒");
|
|
|
- //update-end-author:taoyan date:20190528 for:批量插入数据
|
|
|
- return Result.OK("文件导入成功!数据行数:" + list.size());
|
|
|
- } catch (Exception e) {
|
|
|
- //update-begin-author:taoyan date:20211124 for: 导入数据重复增加提示
|
|
|
- String msg = e.getMessage();
|
|
|
- log.error(msg, e);
|
|
|
- if(msg!=null && msg.indexOf("Duplicate entry")>=0){
|
|
|
- return Result.error("文件导入失败:有重复数据!");
|
|
|
- }else{
|
|
|
- return Result.error("文件导入失败:" + e.getMessage());
|
|
|
+ IKyTaskTempService iKyTaskTempService = (IKyTaskTempService) AopContext.currentProxy();
|
|
|
+ iKyTaskTempService.truncateTable();
|
|
|
+ KyTaskTempImportReadListener readListener = new KyTaskTempImportReadListener(iKyTaskTempService);
|
|
|
+ ExcelReader build = EasyExcel.read(file, clazz, readListener).build();
|
|
|
+ List<ReadSheet> readSheets = build.excelExecutor().sheetList();
|
|
|
+ KyTaskTempImportReadListener.setSheetSize(readSheets.size());
|
|
|
+ ArrayList<Callable<Object>> task = new ArrayList<>();
|
|
|
+ for (ReadSheet readSheet : readSheets) {
|
|
|
+ task.add(() -> {
|
|
|
+ // 设置初始值
|
|
|
+ ExcelReader buildTwo = EasyExcel.read(file, clazz, readListener).readCache(new MapCache()).build();
|
|
|
+ buildTwo.read(EasyExcelFactory.readSheet(readSheet.getSheetNo()).build());
|
|
|
+ return null;
|
|
|
+ });
|
|
|
}
|
|
|
- //update-end-author:taoyan date:20211124 for: 导入数据重复增加提示
|
|
|
- } finally {
|
|
|
- try {
|
|
|
- inputStream.close();
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
+ List<Future<Object>> futures = threadPoolExecutor.invokeAll(task);
|
|
|
+ for (Future<Object> future : futures) {
|
|
|
+ future.get();
|
|
|
}
|
|
|
+ threadPoolExecutor.shutdown();
|
|
|
+ long endTime = System.currentTimeMillis();
|
|
|
+ log.info("导入项目费用花费时间:{}毫秒 | {}分钟", (endTime - startTime), ((endTime - startTime) / 1000 / 60));
|
|
|
+ return Result.OK();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return Result.error("导入失败");
|
|
|
+ } finally {
|
|
|
+ threadPoolExecutor.shutdown();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public void truncateTable() {
|
|
|
+ tempMapper.truncateTable();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void batchSave(List<KyTaskTemp> list) {
|
|
|
+ tempMapper.batchSave(list);
|
|
|
+ }
|
|
|
}
|