|
@@ -5,6 +5,9 @@ import com.alibaba.fastjson.JSONObject;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import com.google.common.collect.Lists;
|
|
|
+import com.itextpdf.io.image.ImageDataFactory;
|
|
|
+import com.itextpdf.layout.Document;
|
|
|
+import com.itextpdf.layout.element.Image;
|
|
|
import com.qmth.boot.api.exception.ApiException;
|
|
|
import com.qmth.boot.tools.excel.ExcelReader;
|
|
|
import com.qmth.boot.tools.excel.enums.ExcelType;
|
|
@@ -16,6 +19,7 @@ import com.qmth.paper.library.business.bean.result.DocManageDetailResult;
|
|
|
import com.qmth.paper.library.business.bean.result.PictureManageDetailResult;
|
|
|
import com.qmth.paper.library.business.entity.PaperLibrary;
|
|
|
import com.qmth.paper.library.business.entity.PaperScanTask;
|
|
|
+import com.qmth.paper.library.business.mapper.DocManageMapper;
|
|
|
import com.qmth.paper.library.business.service.*;
|
|
|
import com.qmth.paper.library.business.templete.service.TaskLogicService;
|
|
|
import com.qmth.paper.library.common.bean.dto.excel.DescribeImportDto;
|
|
@@ -29,9 +33,13 @@ import com.qmth.paper.library.common.entity.*;
|
|
|
import com.qmth.paper.library.common.enums.ExceptionResultEnum;
|
|
|
import com.qmth.paper.library.common.enums.StoreTypeEnum;
|
|
|
import com.qmth.paper.library.common.enums.UploadFileEnum;
|
|
|
+import com.qmth.paper.library.common.lock.LockService;
|
|
|
+import com.qmth.paper.library.common.lock.LockType;
|
|
|
import com.qmth.paper.library.common.service.*;
|
|
|
import com.qmth.paper.library.common.util.*;
|
|
|
import com.qmth.paper.library.common.util.excel.ExcelError;
|
|
|
+import com.qmth.paper.library.common.util.pdf.ItextDocumentInfo;
|
|
|
+import com.qmth.paper.library.common.util.pdf.ItextPdfUtil;
|
|
|
import org.apache.commons.collections4.CollectionUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.apache.poi.ss.usermodel.*;
|
|
@@ -47,7 +55,6 @@ import javax.annotation.Resource;
|
|
|
import java.io.*;
|
|
|
import java.lang.reflect.Field;
|
|
|
import java.util.*;
|
|
|
-import java.util.concurrent.atomic.AtomicInteger;
|
|
|
import java.util.function.Function;
|
|
|
import java.util.stream.Collectors;
|
|
|
import java.util.stream.Stream;
|
|
@@ -91,6 +98,11 @@ public class TaskLogicServiceImpl implements TaskLogicService {
|
|
|
private PictureManageService pictureManageService;
|
|
|
@Resource
|
|
|
private BasicFieldService basicFieldService;
|
|
|
+ @Resource
|
|
|
+ private LockService lockService;
|
|
|
+ @Resource
|
|
|
+ private DocManageMapper docManageMapper;
|
|
|
+
|
|
|
|
|
|
@Transactional
|
|
|
@Override
|
|
@@ -703,6 +715,272 @@ public class TaskLogicServiceImpl implements TaskLogicService {
|
|
|
return map;
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public Map<String, Object> executeStudentPdfGenerateLogic(Map<String, Object> map) {
|
|
|
+ SysUser user = (SysUser) map.get(SystemConstant.USER);
|
|
|
+ Map<Long,Integer> successMap = new HashMap<>();
|
|
|
+ Map<Long,Integer> errorMap = new HashMap<>();
|
|
|
+ List<DocManageDetailResult> toBeGeneratedList;
|
|
|
+ List<String> studentIdList = new ArrayList<>();
|
|
|
+ String rootPath = fileStoreUtil.buildPath(UploadFileEnum.DOWNLOAD, true, String.valueOf(System.currentTimeMillis()));
|
|
|
+ String pictureLocalPath;
|
|
|
+ try {
|
|
|
+ //以用户为粒度加锁
|
|
|
+ lockService.trylock(LockType.STUDENT_PDF_GENERATE, user.getId());
|
|
|
+ TBTask tbTask = (TBTask) map.get(SystemConstant.TASK);
|
|
|
+ DownLoadPaperParams params = JSON.parseObject(tbTask.getRemark(), DownLoadPaperParams.class);
|
|
|
+ String ids = params.getIds();
|
|
|
+ if (StringUtils.isNotEmpty(ids)) {
|
|
|
+ studentIdList = Arrays.asList(ids.split(","));
|
|
|
+ }
|
|
|
+ Long paperScanTaskId = params.getPaperScanTaskId();
|
|
|
+ if (paperScanTaskId == null) {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("扫描任务ID必传");
|
|
|
+ }
|
|
|
+ PaperScanTask scanTask = paperScanTaskService.getById(paperScanTaskId);
|
|
|
+ if(Objects.isNull(scanTask)) {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("扫描任务不存在");
|
|
|
+ }
|
|
|
+ BasicSchool school = basicSchoolService.getById(scanTask.getSchoolId());
|
|
|
+ pictureLocalPath = rootPath + File.separator + school.getName();
|
|
|
+ //查询考生数据
|
|
|
+ if (!studentIdList.isEmpty()) {
|
|
|
+ toBeGeneratedList = docManageMapper.listDetail(paperScanTaskId, studentIdList);
|
|
|
+ } else { //根据查询条件
|
|
|
+ String collegeName = params.getCollegeName();
|
|
|
+ String majorName = params.getMajorName();
|
|
|
+ String className = params.getClassName();
|
|
|
+ String courseName = params.getCourseName();
|
|
|
+ String teacher = params.getTeacher();
|
|
|
+ String teachClass = params.getTeachClass();
|
|
|
+ Boolean isBind = params.getIsBind() == null ? null : params.getIsBind() == 1;
|
|
|
+ String param = params.getParam();
|
|
|
+ Double minScore = params.getMinScore();
|
|
|
+ Double maxScore = params.getMaxScore();
|
|
|
+ Integer minBindCount = params.getMinBindCount();
|
|
|
+ Integer maxBindCount = params.getMaxBindCount();
|
|
|
+ String pictureSymbol = params.getPictureSymbol();
|
|
|
+ Integer pictureCount = params.getPictureCount();
|
|
|
+ toBeGeneratedList = docManageMapper.pageDetail(paperScanTaskId, collegeName, majorName, className, courseName, teacher, teachClass, isBind,
|
|
|
+ param, minScore, maxScore, minBindCount, maxBindCount, pictureSymbol, pictureCount);
|
|
|
+ }
|
|
|
+ toBeGeneratedList = toBeGeneratedList.stream().filter(item -> StringUtils.isEmpty(item.getFilePath())).collect(Collectors.toList());
|
|
|
+
|
|
|
+ //循环考生
|
|
|
+ for(DocManageDetailResult result : toBeGeneratedList) {
|
|
|
+ successMap.putIfAbsent(result.getStudentId(), 0);
|
|
|
+ errorMap.putIfAbsent(result.getStudentId(), 0);
|
|
|
+ List<File> fileList = new ArrayList<>();
|
|
|
+ //创建本地目录
|
|
|
+ String courseNameCode = String.format("%s(%s)", result.getCourseName(), result.getCourseCode());
|
|
|
+ String downloadLocalPath = pictureLocalPath +File.separator + courseNameCode;
|
|
|
+ File dir = new File(downloadLocalPath);
|
|
|
+ if (!dir.exists()) {
|
|
|
+ dir.mkdirs();
|
|
|
+ }
|
|
|
+
|
|
|
+ //查询考生的试卷
|
|
|
+ LambdaQueryWrapper<PaperLibrary> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
+ queryWrapper.eq(PaperLibrary::getStudentId, result.getStudentId());
|
|
|
+ queryWrapper.eq(PaperLibrary::getPaperScanTaskId, paperScanTaskId);
|
|
|
+ List<PaperLibrary> paperLibraryList = paperLibraryService.list(queryWrapper);
|
|
|
+
|
|
|
+ //下载试卷
|
|
|
+ for(PaperLibrary paperLibrary : paperLibraryList) {
|
|
|
+ List<JSONObject> objectList = JSON.parseArray(paperLibrary.getPath(), JSONObject.class);
|
|
|
+ for (JSONObject jsonObject : objectList) {
|
|
|
+ try {
|
|
|
+ File file = paperLibraryCommonService.downloadFile(downloadLocalPath, jsonObject.getString(SystemConstant.PATH),
|
|
|
+ jsonObject.getString(SystemConstant.UPLOAD_TYPE), jsonObject.getString(SystemConstant.TYPE));
|
|
|
+ fileList.add(file);
|
|
|
+ successMap.put(result.getStudentId(), successMap.get(result.getStudentId()) + 1);
|
|
|
+ } catch (Exception e) {
|
|
|
+ //下载失败
|
|
|
+ errorMap.put(result.getStudentId(), errorMap.get(result.getStudentId()) + 1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //试卷写入到pdf
|
|
|
+ if(!fileList.isEmpty()) {
|
|
|
+ String pdfPath = generateStudentPaperPdf(result.getStudentCode(), fileList, school.getName(), courseNameCode);
|
|
|
+ //更新考生
|
|
|
+ ExamStudent student = examStudentService.getById(result.getStudentId());
|
|
|
+ student.setFilePath(pdfPath);
|
|
|
+ examStudentService.updateById(student);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ map.put("size", successMap.size());
|
|
|
+
|
|
|
+ //执行情况写入到excel
|
|
|
+ String path = fileStoreUtil.buildPath(UploadFileEnum.PDF, true);
|
|
|
+ path += File.separator + school.getName();
|
|
|
+ createPdfExcel(path, toBeGeneratedList, successMap, errorMap);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception(e.getMessage());
|
|
|
+ } finally {
|
|
|
+ if (Objects.nonNull(rootPath)) {
|
|
|
+ ConvertUtil.delFolder(rootPath);
|
|
|
+ }
|
|
|
+ lockService.unlock(LockType.STUDENT_PDF_GENERATE,user.getId());
|
|
|
+ }
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String generateStudentPaperPdf(String pdfName, List<File> fileList, String... paths) {
|
|
|
+ StringBuilder pdfRootPath = new StringBuilder(fileStoreUtil.buildPath(UploadFileEnum.PDF, true));
|
|
|
+ for (String path : paths) {
|
|
|
+ pdfRootPath.append(File.separator).append(path);
|
|
|
+ }
|
|
|
+ File dir = new File(pdfRootPath.toString());
|
|
|
+ if (!dir.exists()) {
|
|
|
+ dir.mkdirs();
|
|
|
+ }
|
|
|
+ String pdfPath = pdfRootPath + File.separator + pdfName + SystemConstant.PDF_SUFFIX;
|
|
|
+
|
|
|
+ Document document = null;
|
|
|
+ try {
|
|
|
+ ItextDocumentInfo pageModel = new ItextDocumentInfo();
|
|
|
+ pageModel.rotate();
|
|
|
+ File file = new File(pdfPath);
|
|
|
+ document = pageModel.prepareDocument(file);
|
|
|
+ for (File imageFile : fileList) {
|
|
|
+ Image image = new Image(ImageDataFactory.create(FileUtil.fileConvertToByteArray(imageFile)));
|
|
|
+ document.add(image);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception(e.getMessage());
|
|
|
+ } finally {
|
|
|
+ ItextPdfUtil.closePdf(document);
|
|
|
+ }
|
|
|
+ return pdfPath;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void createPdfExcel(String path, List<DocManageDetailResult> list, Map<Long, Integer> successMap,
|
|
|
+ Map<Long, Integer> errorMap) throws IOException {
|
|
|
+ String fileName = "执行清单-" + System.currentTimeMillis() +".xlsx";
|
|
|
+ XSSFWorkbook wb = new XSSFWorkbook();
|
|
|
+ XSSFSheet sheet = wb.createSheet("数据");
|
|
|
+
|
|
|
+ // 表头
|
|
|
+ CellStyle headerStyle = wb.createCellStyle();
|
|
|
+ headerStyle.setAlignment(HorizontalAlignment.CENTER);
|
|
|
+ headerStyle.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
+ headerStyle.setBorderRight(BorderStyle.THIN);
|
|
|
+ headerStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
|
|
|
+ headerStyle.setBorderLeft(BorderStyle.THIN);
|
|
|
+ headerStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
|
|
|
+ headerStyle.setBorderTop(BorderStyle.THIN);
|
|
|
+ headerStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
|
|
|
+ headerStyle.setBorderBottom(BorderStyle.THIN);
|
|
|
+ headerStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
|
|
|
+ // 背景颜色
|
|
|
+ headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
|
|
|
+ headerStyle.setFillForegroundColor(IndexedColors.GREY_80_PERCENT.getIndex());
|
|
|
+ Font font = wb.createFont();
|
|
|
+ font.setFontHeightInPoints((short) 12);
|
|
|
+ font.setFontName("宋体");
|
|
|
+ font.setColor(IndexedColors.WHITE.getIndex());
|
|
|
+ headerStyle.setFont(font);
|
|
|
+
|
|
|
+ // 数据
|
|
|
+ XSSFCellStyle dataStyle = wb.createCellStyle();
|
|
|
+ dataStyle.setAlignment(HorizontalAlignment.CENTER);
|
|
|
+ dataStyle.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
+ dataStyle.setBorderRight(BorderStyle.THIN);
|
|
|
+ dataStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
|
|
|
+ dataStyle.setBorderLeft(BorderStyle.THIN);
|
|
|
+ dataStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
|
|
|
+ dataStyle.setBorderTop(BorderStyle.THIN);
|
|
|
+ dataStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
|
|
|
+ dataStyle.setBorderBottom(BorderStyle.THIN);
|
|
|
+ dataStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
|
|
|
+ dataStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
|
|
|
+ // 背景颜色
|
|
|
+ dataStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
|
|
|
+ font = wb.createFont();
|
|
|
+ font.setFontHeightInPoints((short) 10);
|
|
|
+ font.setFontName("宋体");
|
|
|
+ dataStyle.setFont(font);
|
|
|
+
|
|
|
+ // 锁定样式
|
|
|
+ XSSFCellStyle lockStyle = wb.createCellStyle();
|
|
|
+ lockStyle.setLocked(true);//设置锁定
|
|
|
+
|
|
|
+ // 未锁定样式
|
|
|
+ XSSFCellStyle unlockStyle = wb.createCellStyle();
|
|
|
+ unlockStyle.setLocked(false);//设置未锁定
|
|
|
+
|
|
|
+ XSSFRow headRow = sheet.createRow(0);
|
|
|
+ // 表头
|
|
|
+ String[] fieldsNameList = {"学号", "姓名", "课程名称(代码)", "是否成功", "成功图片(页)", "失败图片(页)"};
|
|
|
+ for (int i = 0; i < fieldsNameList.length; i++) {
|
|
|
+ XSSFCell cell = headRow.createCell(i);
|
|
|
+ cell.setCellValue(fieldsNameList[i]);
|
|
|
+ cell.setCellStyle(headerStyle);
|
|
|
+ }
|
|
|
+
|
|
|
+ for (int i = 1; i <= list.size(); i++) {
|
|
|
+ DocManageDetailResult result = list.get(i - 1);
|
|
|
+ XSSFRow row = sheet.createRow(i);
|
|
|
+
|
|
|
+ XSSFCell cell0 = row.createCell(0);
|
|
|
+ cell0.setCellStyle(lockStyle);
|
|
|
+ cell0.setCellStyle(dataStyle);
|
|
|
+ cell0.setCellValue(result.getStudentCode());
|
|
|
+
|
|
|
+ XSSFCell cell1 = row.createCell(1);
|
|
|
+ cell1.setCellStyle(lockStyle);
|
|
|
+ cell1.setCellStyle(dataStyle);
|
|
|
+ cell1.setCellValue(result.getStudentName());
|
|
|
+
|
|
|
+ XSSFCell cell2 = row.createCell(2);
|
|
|
+ cell2.setCellStyle(lockStyle);
|
|
|
+ cell2.setCellStyle(dataStyle);
|
|
|
+ cell2.setCellValue(result.getCourseName() + "(" + result.getCourseCode() + ")");
|
|
|
+
|
|
|
+
|
|
|
+ XSSFCell cell3 = row.createCell(3);
|
|
|
+ cell3.setCellStyle(lockStyle);
|
|
|
+ cell3.setCellStyle(dataStyle);
|
|
|
+ String remark = "";
|
|
|
+ if (successMap.get(result.getStudentId()) > 0 && errorMap.get(result.getStudentId()) == 0) {
|
|
|
+ remark = "成功";
|
|
|
+ }
|
|
|
+ if (successMap.get(result.getStudentId()) == 0 && errorMap.get(result.getStudentId()) > 0) {
|
|
|
+ remark = "失败";
|
|
|
+ }
|
|
|
+ if (successMap.get(result.getStudentId()) > 0 && errorMap.get(result.getStudentId()) > 0) {
|
|
|
+ remark = "部分成功";
|
|
|
+ }
|
|
|
+ cell3.setCellValue(remark);
|
|
|
+
|
|
|
+ XSSFCell cell4 = row.createCell(4);
|
|
|
+ cell4.setCellStyle(lockStyle);
|
|
|
+ cell4.setCellStyle(dataStyle);
|
|
|
+ cell4.setCellValue(successMap.get(result.getStudentId()));
|
|
|
+
|
|
|
+ XSSFCell cell5 = row.createCell(5);
|
|
|
+ cell5.setCellStyle(lockStyle);
|
|
|
+ cell5.setCellStyle(dataStyle);
|
|
|
+ cell5.setCellValue(errorMap.get(result.getStudentId()));
|
|
|
+ }
|
|
|
+
|
|
|
+ for (int i = 0; i < fieldsNameList.length; i++) {
|
|
|
+ sheet.autoSizeColumn(i);
|
|
|
+ sheet.setColumnWidth(i, sheet.getColumnWidth(i) * 17 / 6);
|
|
|
+ }
|
|
|
+
|
|
|
+ // sheet添加保护,这个一定要否则光锁定还是可以编辑的
|
|
|
+ sheet.protectSheet(SystemConstant.EXCEL_PROTECT_KEY);
|
|
|
+
|
|
|
+ File file = new File(path, fileName);
|
|
|
+ FileOutputStream outputStream = new FileOutputStream(file);
|
|
|
+ wb.write(outputStream);
|
|
|
+ outputStream.close();
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 生成清单
|
|
|
*
|