|
@@ -0,0 +1,346 @@
|
|
|
+package cn.com.qmth.stmms.ms.marking.api;
|
|
|
+
|
|
|
+import cn.com.qmth.stmms.ms.commons.utils.ServletUtil;
|
|
|
+import cn.com.qmth.stmms.ms.commons.utils.excel.ExcelError;
|
|
|
+import cn.com.qmth.stmms.ms.commons.utils.excel.ExcelReader;
|
|
|
+import cn.com.qmth.stmms.ms.commons.utils.excel.ExcelReaderHandle;
|
|
|
+import cn.com.qmth.stmms.ms.commons.utils.excel.ExportUtils;
|
|
|
+import cn.com.qmth.stmms.ms.commons.web.PageableDTO;
|
|
|
+import cn.com.qmth.stmms.ms.core.cache.ParamCache;
|
|
|
+import cn.com.qmth.stmms.ms.core.domain.*;
|
|
|
+import cn.com.qmth.stmms.ms.core.domain.task.MarkTaskLevel;
|
|
|
+import cn.com.qmth.stmms.ms.core.domain.task.MarkTaskScore;
|
|
|
+import cn.com.qmth.stmms.ms.core.domain.user.MarkUser;
|
|
|
+import cn.com.qmth.stmms.ms.core.domain.user.Role;
|
|
|
+import cn.com.qmth.stmms.ms.core.repository.*;
|
|
|
+import cn.com.qmth.stmms.ms.core.vo.Subject;
|
|
|
+import cn.com.qmth.stmms.ms.marking.assembler.ChangeLevelAssembler;
|
|
|
+import cn.com.qmth.stmms.ms.marking.assembler.PaperAssembler;
|
|
|
+import cn.com.qmth.stmms.ms.marking.dto.*;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import org.apache.commons.lang.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.data.domain.Page;
|
|
|
+import org.springframework.data.domain.PageRequest;
|
|
|
+import org.springframework.data.domain.Pageable;
|
|
|
+import org.springframework.data.domain.Sort;
|
|
|
+import org.springframework.data.jpa.domain.Specification;
|
|
|
+import org.springframework.http.HttpStatus;
|
|
|
+import org.springframework.http.ResponseEntity;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.transaction.interceptor.TransactionAspectSupport;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import javax.persistence.criteria.CriteriaBuilder;
|
|
|
+import javax.persistence.criteria.Predicate;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.*;
|
|
|
+import java.util.concurrent.Semaphore;
|
|
|
+import java.util.concurrent.atomic.AtomicInteger;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+@RestController
|
|
|
+@RequestMapping("/api/paperCheck")
|
|
|
+public class PaperCheckApi {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private PaperRepo paperRepo;
|
|
|
+ @Resource
|
|
|
+ private PaperAssembler paperAssembler;
|
|
|
+ @Autowired
|
|
|
+ private StudentRepo studentRepo;
|
|
|
+ @Resource
|
|
|
+ private MarkSubjectRepo markSubjectRepo;
|
|
|
+ @Resource
|
|
|
+ private PaperCheckRepo paperCheckRepo;
|
|
|
+ @Resource
|
|
|
+ private PaperCheckStudentRepo paperCheckStudentRepo;
|
|
|
+ @Resource
|
|
|
+ private ChangeLevelRepo changeLevelRepo;
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询抽查任务
|
|
|
+ *
|
|
|
+ * @param workId 工作ID
|
|
|
+ */
|
|
|
+ @GetMapping("/list")
|
|
|
+ public List<PaperCheck> list(@RequestParam Long workId) {
|
|
|
+ return paperCheckRepo.findByWorkIdOrderByCreatedOnDesc(workId);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 新增抽查任务
|
|
|
+ *
|
|
|
+ * @param workId 工作ID
|
|
|
+ * @param subject 科目
|
|
|
+ * @param name 名称
|
|
|
+ * @param remark 备注
|
|
|
+ */
|
|
|
+ @PostMapping("/add")
|
|
|
+ @Transactional
|
|
|
+ public ResponseEntity add(@RequestParam Long workId,
|
|
|
+ @RequestParam Subject subject,
|
|
|
+ @RequestParam String name,
|
|
|
+ @RequestParam String remark) {
|
|
|
+ PaperCheck paperCheck = paperCheckRepo.selectByWorkIdAndName(workId, name);
|
|
|
+ if (paperCheck != null) {
|
|
|
+ throw new RuntimeException("名称已存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ MarkSubject markSubject = markSubjectRepo.findOne(workId + "-" + subject);
|
|
|
+ // 查询打分任务是否完成
|
|
|
+ int waitCount = paperRepo.countByWorkIdAndSubjectAndLevelIsNotNullAndScoreIsNull(workId, subject);
|
|
|
+ if (!MarkStage.SCORE.equals(markSubject.getStage()) || waitCount > 0) {
|
|
|
+ throw new RuntimeException("该科目打分任务还未完成,不可进行试卷抽查");
|
|
|
+ }
|
|
|
+
|
|
|
+ paperCheck = new PaperCheck();
|
|
|
+ paperCheck.setWorkId(workId);
|
|
|
+ paperCheck.setSubject(subject.name());
|
|
|
+ paperCheck.setStage(markSubject.getStage());
|
|
|
+ paperCheck.setName(name);
|
|
|
+ paperCheck.setRemark(remark);
|
|
|
+ paperCheck.setStudentCount(0);
|
|
|
+ paperCheck.setStartDeal(false);
|
|
|
+ paperCheck.setSelectInfo(JSON.toJSONString(new PaperCheckSelectInfoDTO(0, null)));
|
|
|
+ paperCheck.setCreatedId(ServletUtil.getUserId());
|
|
|
+ paperCheck.setCreatedOn(new Date());
|
|
|
+ paperCheckRepo.save(paperCheck);
|
|
|
+
|
|
|
+ return new ResponseEntity(HttpStatus.OK);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 导入考生名单
|
|
|
+ *
|
|
|
+ * @param paperCheckId 试卷抽查任务ID
|
|
|
+ * @param file 考生名单excel文件
|
|
|
+ */
|
|
|
+ @PostMapping("/importStudent")
|
|
|
+ @Transactional
|
|
|
+ public List<ExcelError> importStudent(@RequestParam Long paperCheckId,
|
|
|
+ @RequestParam MultipartFile file) throws Exception {
|
|
|
+ PaperCheck paperCheck = paperCheckRepo.findOne(paperCheckId);
|
|
|
+ if (paperCheck == null) {
|
|
|
+ throw new RuntimeException("任务不存在");
|
|
|
+ }
|
|
|
+ if (paperCheck.getStartDeal()) {
|
|
|
+ throw new RuntimeException("开始批量处理,无法执行此操作");
|
|
|
+ }
|
|
|
+
|
|
|
+ List<Paper> paperList = new ArrayList<>();
|
|
|
+ List<String> examNumberList = new ArrayList<>();
|
|
|
+
|
|
|
+ ExcelReader excelReader = new ExcelReader(PaperCheckImportStudentDTO.class);
|
|
|
+ List<ExcelError> excelErrors = excelReader.reader(file.getInputStream(), obj -> {
|
|
|
+ try {
|
|
|
+ PaperCheckImportStudentDTO dto = (PaperCheckImportStudentDTO) obj;
|
|
|
+ String examNumber = dto.getExamNumber();
|
|
|
+
|
|
|
+ // 考号是否重复
|
|
|
+ if (examNumberList.contains(examNumber)) {
|
|
|
+ throw new RuntimeException("准考证号重复");
|
|
|
+ } else {
|
|
|
+ examNumberList.add(examNumber);
|
|
|
+ }
|
|
|
+
|
|
|
+ Student student = studentRepo.findByWorkIdAndExamNumber(paperCheck.getWorkId(), examNumber);
|
|
|
+ if (student == null) {
|
|
|
+ throw new RuntimeException("准考证号" + examNumber + "不存在此考生");
|
|
|
+ } else if (student.isAbsent()) {
|
|
|
+ throw new RuntimeException("准考证号" + examNumber + "的考生状态为缺考");
|
|
|
+ }
|
|
|
+ Subject subject = Subject.valueOf(paperCheck.getSubject());
|
|
|
+ Paper paper = paperRepo.findByWorkIdAndSubjectAndExamNumber(paperCheck.getWorkId(), subject, examNumber);
|
|
|
+ if (paper == null) {
|
|
|
+ throw new RuntimeException("准考证号" + examNumber + "考生" + subject.getName() + "试卷未上传");
|
|
|
+ }
|
|
|
+ paperList.add(paper);
|
|
|
+ return null;
|
|
|
+ } catch (RuntimeException e) {
|
|
|
+ //手动回滚
|
|
|
+ TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
|
|
|
+ ExcelError excelError = new ExcelError();
|
|
|
+ excelError.setExcelErrorType(e.getMessage());
|
|
|
+ return excelError;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ String errors = errorsString(excelErrors);
|
|
|
+ if (errors.length() > 0) {
|
|
|
+ throw new Exception(errors);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!CollectionUtils.isEmpty(paperList)) {
|
|
|
+ // 删除旧数据
|
|
|
+ paperCheckStudentRepo.deleteByPaperCheckId(paperCheckId);
|
|
|
+ List<PaperCheckStudent> data = new ArrayList<>();
|
|
|
+ for (Paper paper : paperList) {
|
|
|
+ PaperCheckStudent paperCheckStudent = new PaperCheckStudent(paperCheck.getWorkId(), paperCheck.getSubject(), paperCheck.getStage(), paperCheck.getId(), paper.getId(), paper.getSecretNumber());
|
|
|
+ if (data.size() == 2000) {
|
|
|
+ paperCheckStudentRepo.save(paperCheckStudent);
|
|
|
+ data.clear();
|
|
|
+ }
|
|
|
+ data.add(paperCheckStudent);
|
|
|
+ }
|
|
|
+ //将剩下的数据也导入
|
|
|
+ if (!data.isEmpty()) {
|
|
|
+ paperCheckStudentRepo.save(data);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更新抽查任务中考生数量
|
|
|
+ paperCheckRepo.updateStudentCountById(paperList.size(), paperCheckId);
|
|
|
+ }
|
|
|
+ return excelErrors;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 批量处理
|
|
|
+ *
|
|
|
+ * @param paperCheckId 试卷抽查任务ID
|
|
|
+ */
|
|
|
+ @PostMapping("/listStudent")
|
|
|
+ @Transactional
|
|
|
+ public PageableDTO listStudent(@RequestParam Long paperCheckId,
|
|
|
+ Pageable pageable) {
|
|
|
+ Specification<PaperCheckStudent> specification = (root, query, builder) -> {
|
|
|
+ List<Predicate> predicates = new ArrayList<>();
|
|
|
+ predicates.add(builder.equal(root.get("paperCheckId"), paperCheckId));
|
|
|
+ return builder.and(predicates.toArray(new Predicate[predicates.size()]));
|
|
|
+ };
|
|
|
+ Sort sort = new Sort(Sort.Direction.ASC, "secretNumber");
|
|
|
+ Pageable pageable1 = new PageRequest(pageable.getPageNumber(), pageable.getPageSize(), sort);
|
|
|
+
|
|
|
+ Page<PaperCheckStudent> paperCheckStudents = paperCheckStudentRepo.findAll(specification, pageable1);
|
|
|
+ List<Paper> papers = paperRepo.findAll(paperCheckStudents.getContent().stream().map(PaperCheckStudent::getPaperId).collect(Collectors.toList()));
|
|
|
+
|
|
|
+ List<PaperDTO> paperDTOs = new ArrayList<>();
|
|
|
+ papers.forEach(p -> paperDTOs.add(paperAssembler.toInspectionDTO(p)));
|
|
|
+
|
|
|
+ // 更新处理标记
|
|
|
+ paperCheckRepo.updateStartDealById(paperCheckId);
|
|
|
+ return new PageableDTO(paperDTOs, paperCheckStudents.getTotalElements(), paperCheckStudents.getTotalPages(), pageable.getPageNumber());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 批量处理历史
|
|
|
+ *
|
|
|
+ * @param paperCheckId 试卷抽查任务ID
|
|
|
+ */
|
|
|
+ @PostMapping("/listHistory")
|
|
|
+ @Transactional
|
|
|
+ public PageableDTO listHistory(@RequestParam Long paperCheckId,
|
|
|
+ Pageable pageable) {
|
|
|
+ Page<ChangeLevel> changeLevelPage = changeLevelRepo.findByPaperCheckId(paperCheckId, pageable);
|
|
|
+ List<PaperDTO> paperDTOs = new ArrayList<>();
|
|
|
+ for (ChangeLevel changeLevel : changeLevelPage) {
|
|
|
+ PaperDTO paperDTO = paperAssembler.toInspectionDTO(paperRepo.findOne(changeLevel.getPaperId()));
|
|
|
+ // 新档位为改档建议档位
|
|
|
+ paperDTO.setLevel(changeLevel.getSuggestLevel());
|
|
|
+ // 旧档位为改档前档位
|
|
|
+ paperDTO.setOriginLevel(changeLevel.getOriginalLevel());
|
|
|
+ paperDTOs.add(paperDTO);
|
|
|
+ }
|
|
|
+ return new PageableDTO(paperDTOs, changeLevelPage.getTotalElements(), changeLevelPage.getTotalPages(), pageable.getPageNumber());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新选中的数据信息
|
|
|
+ *
|
|
|
+ * @param paperCheckId 试卷抽查任务ID
|
|
|
+ * @param pageNumber 当前页码
|
|
|
+ * @param secretNumber 考生任务密号
|
|
|
+ */
|
|
|
+ @PostMapping("/updateSelectInfo")
|
|
|
+ @Transactional
|
|
|
+ public ResponseEntity updateSelectInfo(@RequestParam Long paperCheckId,
|
|
|
+ @RequestParam Integer pageNumber,
|
|
|
+ @RequestParam String secretNumber) {
|
|
|
+ PaperCheck paperCheck = paperCheckRepo.findOne(paperCheckId);
|
|
|
+ if (paperCheck == null) {
|
|
|
+ throw new RuntimeException("抽查任务不存在");
|
|
|
+ }
|
|
|
+ PaperCheckSelectInfoDTO paperCheckSelectInfoDTO = new PaperCheckSelectInfoDTO(pageNumber, secretNumber);
|
|
|
+ paperCheckRepo.updateSelectInfoById(JSON.toJSONString(paperCheckSelectInfoDTO), paperCheck.getId());
|
|
|
+ return new ResponseEntity(HttpStatus.OK);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取选中信息
|
|
|
+ *
|
|
|
+ * @param paperCheckId 试卷抽查任务ID
|
|
|
+ */
|
|
|
+ @GetMapping("/getSelectInfo")
|
|
|
+ public PaperCheckSelectInfoDTO getSelectInfo(@RequestParam Long paperCheckId) {
|
|
|
+ PaperCheck paperCheck = paperCheckRepo.findOne(paperCheckId);
|
|
|
+ PaperCheckSelectInfoDTO paperCheckSelectInfoDTO = new PaperCheckSelectInfoDTO();
|
|
|
+ if (paperCheck == null || StringUtils.isBlank(paperCheck.getSelectInfo())) {
|
|
|
+ return paperCheckSelectInfoDTO;
|
|
|
+ }
|
|
|
+ return JSON.parseObject(paperCheck.getSelectInfo(), PaperCheckSelectInfoDTO.class);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 导出信息
|
|
|
+ *
|
|
|
+ * @param paperCheckId 试卷抽查任务ID
|
|
|
+ */
|
|
|
+ @PostMapping("/export")
|
|
|
+ public void export(@RequestParam Long paperCheckId, HttpServletResponse response) {
|
|
|
+ List<PaperCheckExportDTO> paperCheckExportDTOS = new ArrayList<>();
|
|
|
+ PaperCheck paperCheck = paperCheckRepo.findOne(paperCheckId);
|
|
|
+ List<Object[]> objects = paperCheckStudentRepo.findByPaperCheckId(paperCheckId, paperCheck.getWorkId(), paperCheck.getSubject(), paperCheck.getStage().ordinal());
|
|
|
+ AtomicInteger i = new AtomicInteger(1);
|
|
|
+ objects.forEach(o -> {
|
|
|
+ PaperCheckExportDTO paperCheckExportDTO = new PaperCheckExportDTO();
|
|
|
+ paperCheckExportDTO.setSequence(i.getAndIncrement());
|
|
|
+ paperCheckExportDTO.setSubjectName(Subject.valueOf(String.valueOf(o[0])).getName());
|
|
|
+ paperCheckExportDTO.setExamNumber(String.valueOf(o[1]));
|
|
|
+ paperCheckExportDTO.setSecretNumber(String.valueOf(o[2]));
|
|
|
+
|
|
|
+ Object level = o[3];
|
|
|
+ Object originLevel = o[4];
|
|
|
+ // 没有改档记录
|
|
|
+ if (originLevel == null) {
|
|
|
+ paperCheckExportDTO.setOriginLevel(String.valueOf(level));
|
|
|
+ paperCheckExportDTO.setLevel("-");
|
|
|
+ paperCheckExportDTO.setUpdateLevel("否");
|
|
|
+ } else {
|
|
|
+ paperCheckExportDTO.setOriginLevel(String.valueOf(originLevel));
|
|
|
+ paperCheckExportDTO.setLevel(String.valueOf(level));
|
|
|
+ // 有改档记录且更新前后档位未变动,说明有评卷员未确认,是否改档标记为否
|
|
|
+ paperCheckExportDTO.setUpdateLevel(originLevel.equals(level) ? "否" : "是");
|
|
|
+ }
|
|
|
+ paperCheckExportDTOS.add(paperCheckExportDTO);
|
|
|
+ });
|
|
|
+ String fileName = "试卷抽查数据";
|
|
|
+ ExportUtils.exportEXCEL(fileName, PaperCheckExportDTO.class, paperCheckExportDTOS, response);
|
|
|
+ }
|
|
|
+
|
|
|
+ private String errorsString(List<ExcelError> excelErrors) {
|
|
|
+ StringJoiner sj = new StringJoiner(";");
|
|
|
+ if (excelErrors != null && !excelErrors.isEmpty()) {
|
|
|
+ int forint = excelErrors.size() < 10 ? excelErrors.size() : 9;
|
|
|
+ for (int i = 0; i < forint; i++) {
|
|
|
+ ExcelError excelError = excelErrors.get(i);
|
|
|
+ StringBuffer sb = new StringBuffer();
|
|
|
+ sb.append("第").append(excelError.getRow()).append("行,").append(excelError.getExcelErrorType());
|
|
|
+ sj.add(sb.toString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return sj.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ List<String> list = new ArrayList<>();
|
|
|
+ list.add("11");
|
|
|
+ list.add("22");
|
|
|
+ list.add("33");
|
|
|
+ list.add("44");
|
|
|
+ }
|
|
|
+}
|