|
@@ -1,18 +1,23 @@
|
|
|
package com.qmth.themis.business.templete.impl;
|
|
|
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import com.google.common.collect.Lists;
|
|
|
+import com.google.gson.Gson;
|
|
|
import com.qmth.themis.business.constant.SpringContextHolder;
|
|
|
+import com.qmth.themis.business.constant.SystemConstant;
|
|
|
import com.qmth.themis.business.dto.ExamStudentDtoImport;
|
|
|
-import com.qmth.themis.business.entity.TBExamInvigilateUser;
|
|
|
-import com.qmth.themis.business.entity.TEExamStudent;
|
|
|
+import com.qmth.themis.business.entity.*;
|
|
|
import com.qmth.themis.business.enums.ExamModeEnum;
|
|
|
-import com.qmth.themis.business.forkjoin.ForkJoinTask;
|
|
|
-import com.qmth.themis.business.service.TBExamInvigilateUserService;
|
|
|
+import com.qmth.themis.business.enums.RoleEnum;
|
|
|
+import com.qmth.themis.business.enums.TaskTypeExecEnum;
|
|
|
+import com.qmth.themis.business.service.*;
|
|
|
import com.qmth.themis.business.templete.TaskImportTemplete;
|
|
|
import com.qmth.themis.business.util.ExcelCallback;
|
|
|
import com.qmth.themis.business.util.ExcelError;
|
|
|
import com.qmth.themis.business.util.ExcelUtil;
|
|
|
+import com.qmth.themis.business.util.JacksonUtil;
|
|
|
import com.qmth.themis.common.exception.BusinessException;
|
|
|
import com.qmth.themis.common.util.Result;
|
|
|
import com.qmth.themis.common.util.ResultUtil;
|
|
@@ -23,10 +28,9 @@ import org.springframework.util.LinkedMultiValueMap;
|
|
|
import java.io.File;
|
|
|
import java.io.FileInputStream;
|
|
|
import java.io.IOException;
|
|
|
+import java.math.BigDecimal;
|
|
|
import java.util.*;
|
|
|
-import java.util.concurrent.ExecutionException;
|
|
|
-import java.util.concurrent.ForkJoinPool;
|
|
|
-import java.util.concurrent.Future;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* @Description: 考生导入模版
|
|
@@ -41,8 +45,6 @@ public class ExamStudentTemplete implements TaskImportTemplete {
|
|
|
@Override
|
|
|
public Result importTask(File file, Map<String, Object> map) throws IOException, InterruptedException {
|
|
|
Integer mode = Integer.parseInt(String.valueOf(map.get("mode")));
|
|
|
- Long orgId = Long.parseLong(String.valueOf(map.get("orgId")));
|
|
|
- Long createId = Long.parseLong(String.valueOf(map.get("createId")));
|
|
|
FileInputStream inputStream = new FileInputStream(file);
|
|
|
List<LinkedMultiValueMap<Integer, Object>> finalList = ExcelUtil.excelReader(inputStream, Lists.newArrayList(ExamStudentDtoImport.class), new ExcelCallback() {
|
|
|
@Override
|
|
@@ -72,51 +74,151 @@ public class ExamStudentTemplete implements TaskImportTemplete {
|
|
|
});
|
|
|
//保存数据到数据库
|
|
|
if (Objects.nonNull(finalList) && finalList.size() > 0) {
|
|
|
- log.info("开始forkjoin数据拆分");
|
|
|
+ log.info("开始导入考生数据");
|
|
|
long start = System.currentTimeMillis();
|
|
|
- ForkJoinPool forkJoinPool = new ForkJoinPool();
|
|
|
- for (int i = 0; i < finalList.size(); i++) {
|
|
|
- LinkedMultiValueMap<Integer, Object> multiValueMap = finalList.get(i);
|
|
|
- List<Object> examStudentDtoImportList = multiValueMap.get(i);
|
|
|
- ForkJoinTask forkJoinTask = new ForkJoinTask(500, 0, examStudentDtoImportList.size() - 1, examStudentDtoImportList, map);
|
|
|
- Future<List<TEExamStudent>> result = forkJoinPool.submit(forkJoinTask);
|
|
|
- if (forkJoinTask.isCompletedAbnormally()) {
|
|
|
- //检查任务是否已经抛出异常或已经被取消了,要注意此方法。由于提交任务之后,检测该任务是否有异常,不是阻塞的。所以需要上面的等待任务的完成。才能正确的获取到是否有异常
|
|
|
- throw new BusinessException(forkJoinTask.getException().getMessage());
|
|
|
- }
|
|
|
- try {
|
|
|
- List<TEExamStudent> list = result.get();
|
|
|
- Map<String, String> tbExamInvigilateUserMap = (Map<String, String>) map.get("tbExamInvigilateUserMap");
|
|
|
- if (Objects.nonNull(list) && list.size() > 0) {
|
|
|
- //考场的创建
|
|
|
- TBExamInvigilateUserService tbExamInvigilateUserService = SpringContextHolder.getBean(TBExamInvigilateUserService.class);
|
|
|
- Set<String> roomCodeAndNameSet = new HashSet<>();
|
|
|
- list.forEach(s -> {
|
|
|
- roomCodeAndNameSet.add(s.getRoomCode() + ":" + s.getRoomName());
|
|
|
- });
|
|
|
- List<TBExamInvigilateUser> tbExamInvigilateUserList = new ArrayList<>();
|
|
|
- roomCodeAndNameSet.forEach(s -> {
|
|
|
- if (Objects.isNull(tbExamInvigilateUserMap) || (Objects.nonNull(tbExamInvigilateUserMap) && Objects.isNull(tbExamInvigilateUserMap.get(s)))) {
|
|
|
- String[] strs = s.split(":");
|
|
|
- TBExamInvigilateUser tbExamInvigilateUser = new TBExamInvigilateUser(orgId, createId, strs[0], strs[1]);
|
|
|
- tbExamInvigilateUserList.add(tbExamInvigilateUser);
|
|
|
- }
|
|
|
- });
|
|
|
- tbExamInvigilateUserService.saveBatch(tbExamInvigilateUserList);
|
|
|
+ TEExamStudentService teExamStudentService = SpringContextHolder.getBean(TEExamStudentService.class);
|
|
|
+ TEStudentService teStudentService = SpringContextHolder.getBean(TEStudentService.class);
|
|
|
+ TBUserRoleService tbUserRoleService = SpringContextHolder.getBean(TBUserRoleService.class);
|
|
|
+ TBTaskHistoryService tbTaskHistoryService = SpringContextHolder.getBean(TBTaskHistoryService.class);
|
|
|
+ TBExamInvigilateUserService tbExamInvigilateUserService = SpringContextHolder.getBean(TBExamInvigilateUserService.class);
|
|
|
+ Long orgId = Long.parseLong(String.valueOf(map.get("orgId")));
|
|
|
+ Long createId = Long.parseLong(String.valueOf(map.get("createId")));
|
|
|
+ Long examId = Long.parseLong(String.valueOf(map.get("examId")));
|
|
|
+ List<TEExamStudent> teExamStudentList = new ArrayList<>();
|
|
|
+ List<TEStudent> teStudentList = new ArrayList<>();
|
|
|
+ List<TBUserRole> tbUserRoleList = new ArrayList<>();
|
|
|
+ List<TBExamInvigilateUser> tbExamInvigilateUserList = new ArrayList<>();
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
+ TBTaskHistory tbTaskHistory = null;
|
|
|
+ jsonObject.put(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"), "开始准备处理导入的考生数据");
|
|
|
+ int y = 0, min = 0;
|
|
|
+ try {
|
|
|
+ Gson gson = new Gson();
|
|
|
+ Set<String> roomCodeAndNameSet = new HashSet<>();
|
|
|
+ Map<String, TEExamActivity> teExamActivityMap = (Map<String, TEExamActivity>) map.get("teExamActivityMap");
|
|
|
+ Map tbTaskHistoryMap = (Map) map.get("tbTaskHistory");
|
|
|
+ tbTaskHistoryMap = SystemConstant.timeTransform(tbTaskHistoryMap);
|
|
|
+ tbTaskHistory = gson.fromJson(gson.toJson(tbTaskHistoryMap), TBTaskHistory.class);
|
|
|
+ //保存用户
|
|
|
+ for (int i = 0; i < finalList.size(); i++) {
|
|
|
+ LinkedMultiValueMap<Integer, Object> finalMap = finalList.get(i);
|
|
|
+ List<Object> examStudentDtoImportList = finalMap.get(i);
|
|
|
+ min = 0;
|
|
|
+ int max = 500, size = examStudentDtoImportList.size();
|
|
|
+ if (max >= size) {
|
|
|
+ max = size;
|
|
|
}
|
|
|
- log.info("list.size:{}", list.size());
|
|
|
- } catch (ExecutionException | InterruptedException e) {
|
|
|
- e.printStackTrace();
|
|
|
- } finally {
|
|
|
- if (Objects.nonNull(forkJoinPool)) {
|
|
|
- forkJoinPool.shutdown();
|
|
|
+ while (max <= size) {
|
|
|
+ List subList = examStudentDtoImportList.subList(min, max);
|
|
|
+ for (y = 0; y < subList.size(); y++) {
|
|
|
+ ExamStudentDtoImport examStudentDtoImport = (ExamStudentDtoImport) subList.get(y);
|
|
|
+ Map m = (Map) teExamActivityMap.get(examStudentDtoImport.getExamActivityCode());
|
|
|
+ m = SystemConstant.timeTransform(m);
|
|
|
+ TEExamActivity teExamActivity = gson.fromJson(gson.toJson(m), TEExamActivity.class);
|
|
|
+ //先根据证件号+科目代码查询考生是否存在,存在则更新,不存在则插入
|
|
|
+ QueryWrapper<TEExamStudent> teExamStudentQueryWrapper = new QueryWrapper<>();
|
|
|
+ teExamStudentQueryWrapper.lambda().eq(TEExamStudent::getIdentity, examStudentDtoImport.getIdentity()).eq(TEExamStudent::getCourseCode, examStudentDtoImport.getCourseCode());
|
|
|
+ TEExamStudent teExamStudent = teExamStudentService.getOne(teExamStudentQueryWrapper);
|
|
|
+ //如果为空则插入考生数据,插入考生前先插入学生档案数据
|
|
|
+ if (Objects.isNull(teExamStudent)) {
|
|
|
+ //先插入学生档案数据
|
|
|
+ TEStudent teStudent = new TEStudent(orgId, examStudentDtoImport.getIdentity(), examStudentDtoImport.getName(), createId);
|
|
|
+ teStudentService.save(teStudent);
|
|
|
+ teStudentList.add(teStudent);
|
|
|
+
|
|
|
+ //插入用户角色关系
|
|
|
+ TBUserRole tbUserRole = new TBUserRole(teStudent.getId(), RoleEnum.STUDENT.name());
|
|
|
+ tbUserRoleService.save(tbUserRole);
|
|
|
+ tbUserRoleList.add(tbUserRole);
|
|
|
+
|
|
|
+ teExamStudent = gson.fromJson(gson.toJson(examStudentDtoImport), TEExamStudent.class);
|
|
|
+ teExamStudent.setExamId(examId);
|
|
|
+ teExamStudent.setExamActivityId(teExamActivity.getId());
|
|
|
+ teExamStudent.setStudentId(teStudent.getId());
|
|
|
+ teExamStudent.setCreateId(createId);
|
|
|
+ } else {
|
|
|
+ teExamStudent.setUpdateId(createId);
|
|
|
+ teExamStudent.setName(examStudentDtoImport.getName());
|
|
|
+ teExamStudent.setCourseName(examStudentDtoImport.getCourseName());
|
|
|
+ teExamStudent.setGrade(examStudentDtoImport.getGrade());
|
|
|
+ teExamStudent.setClassNo(examStudentDtoImport.getClassNo());
|
|
|
+ teExamStudent.setRoomCode(examStudentDtoImport.getRoomCode());
|
|
|
+ teExamStudent.setRoomName(examStudentDtoImport.getRoomName());
|
|
|
+ teExamStudent.setExamActivityId(teExamActivity.getId());
|
|
|
+ }
|
|
|
+ roomCodeAndNameSet.add(teExamStudent.getRoomCode() + ":" + teExamStudent.getRoomName());
|
|
|
+ teExamStudentList.add(teExamStudent);
|
|
|
+ }
|
|
|
+ BigDecimal bigDecimal = new BigDecimal(100);
|
|
|
+ BigDecimal progress = new BigDecimal(Double.valueOf(new BigDecimal(max).divide(new BigDecimal(size), 2, BigDecimal.ROUND_HALF_UP).multiply(bigDecimal).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue())).setScale(0, BigDecimal.ROUND_HALF_UP);
|
|
|
+ teExamStudentService.saveOrUpdateBatch(teExamStudentList);
|
|
|
+ log.info("progress:{}", progress);
|
|
|
+ if (progress.intValue() == 100) {
|
|
|
+ tbTaskHistory.setStatus(TaskTypeExecEnum.finish.ordinal());
|
|
|
+ tbTaskHistory.setSummary("共处理了" + size + "条数据");
|
|
|
+ tbTaskHistory.setFinishTime(new Date());
|
|
|
+ jsonObject.put(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"), "考生数据处理完毕," + tbTaskHistory.getSummary());
|
|
|
+ } else {
|
|
|
+ tbTaskHistory.setStatus(TaskTypeExecEnum.running.ordinal());
|
|
|
+ tbTaskHistory.setSummary("正在处理第" + min + "条至" + max + "条数据");
|
|
|
+ jsonObject.put(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"), "考生数据处理中," + tbTaskHistory.getSummary());
|
|
|
+ }
|
|
|
+ tbTaskHistory.setProgress(progress.doubleValue());
|
|
|
+ tbTaskHistoryService.updateById(tbTaskHistory);
|
|
|
+ if (max == size) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ min = max;
|
|
|
+ max += 500;
|
|
|
+ if (max >= size) {
|
|
|
+ max = size;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
+ //考场创建
|
|
|
+ if (Objects.nonNull(roomCodeAndNameSet) && roomCodeAndNameSet.size() > 0) {
|
|
|
+ Map<String, String> tbExamInvigilateUserMap = (Map<String, String>) map.get("tbExamInvigilateUserMap");
|
|
|
+ roomCodeAndNameSet.forEach(s -> {
|
|
|
+ if (Objects.isNull(tbExamInvigilateUserMap) || (Objects.nonNull(tbExamInvigilateUserMap) && Objects.isNull(tbExamInvigilateUserMap.get(s)))) {
|
|
|
+ String[] strs = s.split(":");
|
|
|
+ TBExamInvigilateUser tbExamInvigilateUser = new TBExamInvigilateUser(orgId, createId, strs[0], strs[1]);
|
|
|
+ tbExamInvigilateUserList.add(tbExamInvigilateUser);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ if (Objects.nonNull(tbExamInvigilateUserList) && tbExamInvigilateUserList.size() > 0) {
|
|
|
+ tbExamInvigilateUserService.saveBatch(tbExamInvigilateUserList);
|
|
|
+ jsonObject.put(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"), "创建了" + tbExamInvigilateUserList.size() + "条考场数据");
|
|
|
+ }
|
|
|
+ long end = System.currentTimeMillis();
|
|
|
+ log.info("导入考生数据结束,============耗时============:{}秒", (end - start) / 1000);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ String exceptionStr = "考生数据处理到第" + (min + y + 1) + "条时发生异常:" + e.getMessage();
|
|
|
+ jsonObject.put(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"), exceptionStr);
|
|
|
+ tbTaskHistory.setSummary(exceptionStr);
|
|
|
+ tbTaskHistoryService.updateById(tbTaskHistory);
|
|
|
+ if (Objects.nonNull(teStudentList) && teStudentList.size() > 0) {
|
|
|
+ teStudentService.removeByIds(teStudentList.stream().map(s -> s.getId()).collect(Collectors.toList()));
|
|
|
+ }
|
|
|
+ if (Objects.nonNull(tbUserRoleList) && tbUserRoleList.size() > 0) {
|
|
|
+ tbUserRoleService.removeByIds(tbUserRoleList.stream().map(s -> s.getId()).collect(Collectors.toList()));
|
|
|
+ }
|
|
|
+ if (Objects.nonNull(teExamStudentList) && teExamStudentList.size() > 0) {
|
|
|
+ teExamStudentService.removeByIds(teExamStudentList.stream().map(s -> s.getId()).collect(Collectors.toList()));
|
|
|
+ }
|
|
|
+ if (Objects.nonNull(tbExamInvigilateUserList) && tbExamInvigilateUserList.size() > 0) {
|
|
|
+ tbExamInvigilateUserService.removeByIds(tbExamInvigilateUserList.stream().map(s -> s.getId()).collect(Collectors.toList()));
|
|
|
+ }
|
|
|
+ if (e instanceof BusinessException) {
|
|
|
+ throw new BusinessException(e.getMessage());
|
|
|
+ } else {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ } finally {
|
|
|
+ log.info("jsonObject:{}", jsonObject.toJSONString());
|
|
|
}
|
|
|
- long end = System.currentTimeMillis();
|
|
|
- log.info("forkjoin数据拆分结束,============耗时============:{}秒", (end - start) / 1000);
|
|
|
}
|
|
|
- Map resultMap = new HashMap<>();
|
|
|
- return ResultUtil.ok(resultMap);
|
|
|
+ return ResultUtil.ok(JacksonUtil.parseJson(SystemConstant.SUCCESS));
|
|
|
}
|
|
|
}
|