|
@@ -48,332 +48,342 @@ import cn.com.qmth.am.service.StudentService;
|
|
|
|
|
|
@Service
|
|
@Service
|
|
public class StudentServiceImpl extends ServiceImpl<StudentDao, StudentEntity> implements StudentService {
|
|
public class StudentServiceImpl extends ServiceImpl<StudentDao, StudentEntity> implements StudentService {
|
|
- private static final Logger log = LoggerFactory.getLogger(StudentService.class);
|
|
|
|
- private static final String[] EXCEL_HEADER = new String[] { "考试ID", "科目代码","考生编号" };
|
|
|
|
- @Autowired
|
|
|
|
- private SysProperty sysProperty;
|
|
|
|
- @Autowired
|
|
|
|
- private StudentService studentService;
|
|
|
|
- @Autowired
|
|
|
|
- private StudentScoreService studentScoreService;
|
|
|
|
- @Autowired
|
|
|
|
- private QuestionService questionService;
|
|
|
|
-
|
|
|
|
- @Override
|
|
|
|
- public void importStudent() {
|
|
|
|
- File dir = new File(sysProperty.getDataDir());
|
|
|
|
- File[] fs = dir.listFiles();
|
|
|
|
- if (fs == null || fs.length == 0) {
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
- for (File file : fs) {
|
|
|
|
- if (!file.isFile() || !file.getName().equals(ImportFileName.STUDENT_IMPORT.getName())) {
|
|
|
|
- continue;
|
|
|
|
- }
|
|
|
|
- InputStream inputStream = null;
|
|
|
|
- ImportResult ret=null;
|
|
|
|
- try {
|
|
|
|
- inputStream = new FileInputStream(file);
|
|
|
|
- ret = studentService.disposeFile(inputStream);
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- String errMsg;
|
|
|
|
- if(e instanceof FileNotFoundException) {
|
|
|
|
- errMsg="未找到文件:" + file.getAbsolutePath();
|
|
|
|
- }else {
|
|
|
|
- errMsg="系统错误:" + e.getMessage();
|
|
|
|
- }
|
|
|
|
- ret=new ImportResult(errMsg);
|
|
|
|
- } finally {
|
|
|
|
- if (inputStream != null) {
|
|
|
|
- try {
|
|
|
|
- inputStream.close();
|
|
|
|
- } catch (IOException e) {
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- moveFile(dir,file,ret);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- private void moveFile(File dir,File file, ImportResult ret) {
|
|
|
|
- try {
|
|
|
|
- boolean succss=CollectionUtils.isEmpty(ret.getErrMsg());
|
|
|
|
- if(succss) {
|
|
|
|
- File sucDir=new File(dir.getAbsoluteFile()+"/success/");
|
|
|
|
- if(!sucDir.exists()) {
|
|
|
|
- sucDir.mkdir();
|
|
|
|
- }
|
|
|
|
- File targetFile=new File(sucDir.getAbsoluteFile()+"/"+file.getName());
|
|
|
|
- if(targetFile.exists()) {
|
|
|
|
- targetFile.delete();
|
|
|
|
- }
|
|
|
|
- FileUtils.copyFile(file, targetFile);
|
|
|
|
- file.delete();
|
|
|
|
- String fname=file.getName().substring(0, file.getName().lastIndexOf("."));
|
|
|
|
- File msgFile=new File(sucDir.getAbsoluteFile()+"/"+fname+"_info.txt");
|
|
|
|
- if(msgFile.exists()) {
|
|
|
|
- msgFile.delete();
|
|
|
|
- }
|
|
|
|
- FileUtils.write(msgFile, ret.getCountInfo(), "utf-8");
|
|
|
|
- }else {
|
|
|
|
- File sucDir=new File(dir.getAbsoluteFile()+"/failed/");
|
|
|
|
- if(!sucDir.exists()) {
|
|
|
|
- sucDir.mkdir();
|
|
|
|
- }
|
|
|
|
- File targetFile=new File(sucDir.getAbsoluteFile()+"/"+file.getName());
|
|
|
|
- if(targetFile.exists()) {
|
|
|
|
- targetFile.delete();
|
|
|
|
- }
|
|
|
|
- FileUtils.copyFile(file, targetFile);
|
|
|
|
- file.delete();
|
|
|
|
- String fname=file.getName().substring(0, file.getName().lastIndexOf("."));
|
|
|
|
- File msgFile=new File(sucDir.getAbsoluteFile()+"/"+fname+"_info.txt");
|
|
|
|
- if(msgFile.exists()) {
|
|
|
|
- msgFile.delete();
|
|
|
|
- }
|
|
|
|
- FileUtils.writeLines(msgFile, StandardCharsets.UTF_8.name(), ret.getErrMsg());
|
|
|
|
- }
|
|
|
|
- } catch (IOException e) {
|
|
|
|
- throw new StatusException("文件处理出错", e);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- private String errorMsg(int lineNum, String msg) {
|
|
|
|
- return "第" + lineNum + "行 " + msg;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- private String trimAndNullIfBlank(String s) {
|
|
|
|
- if (StringUtils.isBlank(s)) {
|
|
|
|
- return null;
|
|
|
|
- }
|
|
|
|
- return s.trim();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @Transactional
|
|
|
|
- @Override
|
|
|
|
- public ImportResult disposeFile(InputStream inputStream) {
|
|
|
|
- List<DataMap> lineList = null;
|
|
|
|
- ExcelReader reader = ExcelReader.create(ExcelType.XLSX, inputStream, 0);
|
|
|
|
- try {
|
|
|
|
- lineList = reader.getDataMapList();
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- throw new StatusException("Excel 解析失败");
|
|
|
|
- }
|
|
|
|
- if (!Arrays.equals(EXCEL_HEADER, reader.getColumnNames())) {
|
|
|
|
- throw new StatusException("Excel表头错误");
|
|
|
|
- }
|
|
|
|
- if (CollectionUtils.isEmpty(lineList)) {
|
|
|
|
- throw new StatusException("Excel无内容");
|
|
|
|
- }
|
|
|
|
- if (100001 < lineList.size()) {
|
|
|
|
- throw new StatusException("数据行数不能超过100000");
|
|
|
|
- }
|
|
|
|
- List<StudentEntity> ss = new ArrayList<>();
|
|
|
|
- ImportResult ret = new ImportResult();
|
|
|
|
- List<String> failRecords = new ArrayList<>();
|
|
|
|
- ret.setErrMsg(failRecords);
|
|
|
|
- for (int i = 0; i < lineList.size(); i++) {
|
|
|
|
- DataMap line = lineList.get(i);
|
|
|
|
-
|
|
|
|
- StringBuilder msg = new StringBuilder();
|
|
|
|
-
|
|
|
|
- StudentEntity imp = new StudentEntity();
|
|
|
|
- imp.setDataStatus(DataStatus.WAITING);
|
|
|
|
- String examId = trimAndNullIfBlank(line.get(EXCEL_HEADER[0]));
|
|
|
|
- if (StringUtils.isBlank(examId)) {
|
|
|
|
- msg.append(" 考试ID不能为空");
|
|
|
|
- } else if (examId.length() > 20) {
|
|
|
|
- msg.append(" 考试ID不能超过20个字符");
|
|
|
|
- } else {
|
|
|
|
- try {
|
|
|
|
- Long examIdVal = Long.parseLong(examId);
|
|
|
|
- imp.setExamId(examIdVal);
|
|
|
|
- } catch (NumberFormatException e) {
|
|
|
|
- msg.append(" 考试ID只能是数字");
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- String subjectCode = trimAndNullIfBlank(line.get(EXCEL_HEADER[1]));
|
|
|
|
- if (StringUtils.isBlank(subjectCode)) {
|
|
|
|
- msg.append(" 科目代码不能为空");
|
|
|
|
- } else if (subjectCode.length() > 100) {
|
|
|
|
- msg.append(" 科目代码不能超过100个字符");
|
|
|
|
- }
|
|
|
|
- imp.setSubjectCode(subjectCode);
|
|
|
|
-
|
|
|
|
- String studentCode = trimAndNullIfBlank(line.get(EXCEL_HEADER[2]));
|
|
|
|
- if (StringUtils.isBlank(studentCode)) {
|
|
|
|
- msg.append(" 考生编号不能为空");
|
|
|
|
- } else if (studentCode.length() > 100) {
|
|
|
|
- msg.append(" 考生编号不能超过100个字符");
|
|
|
|
- }
|
|
|
|
- imp.setStudentCode(studentCode);
|
|
|
|
-
|
|
|
|
- if (msg.length() > 0) {
|
|
|
|
- failRecords.add(errorMsg(i + 2, msg.toString()));
|
|
|
|
- } else {
|
|
|
|
- ss.add(imp);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if (CollectionUtils.isNotEmpty(failRecords)) {
|
|
|
|
- return ret;
|
|
|
|
- }
|
|
|
|
- try {
|
|
|
|
- saveStudentBatch(ret,ss);
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- failRecords.add("系统错误:" + e.getMessage());
|
|
|
|
- }
|
|
|
|
- return ret;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- private void saveStudentBatch(ImportResult ret,List<StudentEntity> ss) {
|
|
|
|
- if (CollectionUtils.isEmpty(ss)) {
|
|
|
|
- ret.setCountInfo("新增数量:0");
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
- List<StudentEntity> all = this.list();
|
|
|
|
- Set<String> set = new HashSet<>();
|
|
|
|
- if (CollectionUtils.isNotEmpty(all)) {
|
|
|
|
- for (StudentEntity s : all) {
|
|
|
|
- String key = s.getExamId() + "-" + s.getSubjectCode() + "-" + s.getStudentCode();
|
|
|
|
- set.add(key);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- List<StudentEntity> adds = new ArrayList<>();
|
|
|
|
- for (StudentEntity s : ss) {
|
|
|
|
- String key = s.getExamId() + "-" + s.getSubjectCode() + "-" + s.getStudentCode();
|
|
|
|
- if (!set.contains(key)) {
|
|
|
|
- adds.add(s);
|
|
|
|
- }else {
|
|
|
|
- set.add(key);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- if (CollectionUtils.isNotEmpty(adds)) {
|
|
|
|
- saveBatch(adds);
|
|
|
|
- }
|
|
|
|
- ret.setCountInfo("新增数量:"+adds.size());
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @Override
|
|
|
|
- public List<StudentEntity> findToDispose() {
|
|
|
|
- QueryWrapper<StudentEntity> wrapper = new QueryWrapper<>();
|
|
|
|
- LambdaQueryWrapper<StudentEntity> lw = wrapper.lambda();
|
|
|
|
- lw.in(StudentEntity::getDataStatus, DataStatus.WAITING,DataStatus.FAILED);
|
|
|
|
- return this.list(wrapper);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @Override
|
|
|
|
- public List<StudentEntity> findToMrking() {
|
|
|
|
- QueryWrapper<StudentEntity> wrapper = new QueryWrapper<>();
|
|
|
|
- LambdaQueryWrapper<StudentEntity> lw = wrapper.lambda();
|
|
|
|
- lw.in(StudentEntity::getDataStatus, DataStatus.WAITING,DataStatus.FAILED,DataStatus.PROCESSING);
|
|
|
|
- return this.list(wrapper);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @Override
|
|
|
|
- public void buildImage(StudentEntity student, Map<Long,QuestionEntity> quetions) {
|
|
|
|
- List<StudentScoreEntity> scores=studentService.getOrCreateScores(student, quetions);
|
|
|
|
- Map<Integer,AnswerImageDto> answerImages=new HashMap<>();
|
|
|
|
- for(StudentScoreEntity score:scores) {
|
|
|
|
- if(DataStatus.WAITING.equals(score.getAnswerStatus())||DataStatus.FAILED.equals(score.getAnswerStatus())) {
|
|
|
|
- studentService.createSlice(score,quetions,answerImages);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @Transactional
|
|
|
|
- @Override
|
|
|
|
- public void createSlice(StudentScoreEntity score, Map<Long,QuestionEntity> quetions,Map<Integer,AnswerImageDto> answerImages) {
|
|
|
|
- QuestionEntity q=quetions.get(score.getQuestionId());
|
|
|
|
- if(q==null) {
|
|
|
|
- studentScoreService.updateAnswerErr(score.getId(),"未找到试题信息");
|
|
|
|
- updateStatus(score.getStudentId(),DataStatus.FAILED);
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
- try {
|
|
|
|
- studentScoreService.createSlice(score,q,answerImages);
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- if(e instanceof StatusException) {
|
|
|
|
- studentScoreService.updateAnswerErr(score.getId(),e.getMessage());
|
|
|
|
- updateStatus(score.getStudentId(),DataStatus.FAILED);
|
|
|
|
- }else {
|
|
|
|
- log.error("系统异常",e);
|
|
|
|
- studentScoreService.updateAnswerErr(score.getId(),"系统异常");
|
|
|
|
- updateStatus(score.getStudentId(),DataStatus.FAILED);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @Transactional
|
|
|
|
- @Override
|
|
|
|
- public List<StudentScoreEntity> getOrCreateScores(StudentEntity student, Map<Long,QuestionEntity> quetions) {
|
|
|
|
- List<StudentScoreEntity> oldscores=studentScoreService.getByStudentId(student.getId());
|
|
|
|
- studentScoreService.add(student,quetions,oldscores);
|
|
|
|
- oldscores=studentScoreService.getByStudentId(student.getId());
|
|
|
|
- updateStatus(student.getId(),DataStatus.PROCESSING);
|
|
|
|
- return oldscores;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @Transactional
|
|
|
|
- @Override
|
|
|
|
- public void updateStatus(Long id,DataStatus to) {
|
|
|
|
- UpdateWrapper<StudentEntity> wrapper = new UpdateWrapper<>();
|
|
|
|
- LambdaUpdateWrapper<StudentEntity> lw = wrapper.lambda();
|
|
|
|
- lw.set(StudentEntity::getDataStatus, to);
|
|
|
|
- lw.eq(StudentEntity::getId, id);
|
|
|
|
- this.update(wrapper);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @Transactional
|
|
|
|
- @Override
|
|
|
|
- public void resetStatus() {
|
|
|
|
- UpdateWrapper<StudentEntity> wrapper = new UpdateWrapper<>();
|
|
|
|
- LambdaUpdateWrapper<StudentEntity> lw = wrapper.lambda();
|
|
|
|
- lw.set(StudentEntity::getDataStatus, DataStatus.WAITING);
|
|
|
|
- lw.eq(StudentEntity::getDataStatus, DataStatus.PROCESSING);
|
|
|
|
- this.update(wrapper);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @Override
|
|
|
|
- public int countBy(Long examId, DataStatus status) {
|
|
|
|
- QueryWrapper<StudentEntity> wrapper = new QueryWrapper<>();
|
|
|
|
- LambdaQueryWrapper<StudentEntity> lw = wrapper.lambda();
|
|
|
|
- if(status!=null) {
|
|
|
|
- lw.eq(StudentEntity::getDataStatus, status);
|
|
|
|
- }
|
|
|
|
- lw.eq(StudentEntity::getExamId, examId);
|
|
|
|
- return this.count(wrapper);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @Transactional
|
|
|
|
- @Override
|
|
|
|
- public void reset(Long examId, String subjectCode) {
|
|
|
|
- studentScoreService.removeBy(examId,subjectCode);
|
|
|
|
- UpdateWrapper<StudentEntity> wrapper = new UpdateWrapper<>();
|
|
|
|
- LambdaUpdateWrapper<StudentEntity> lw = wrapper.lambda();
|
|
|
|
- lw.set(StudentEntity::getDataStatus, DataStatus.WAITING);
|
|
|
|
- if(subjectCode!=null) {
|
|
|
|
- lw.eq(StudentEntity::getSubjectCode, subjectCode);
|
|
|
|
- }
|
|
|
|
- lw.eq(StudentEntity::getExamId, examId);
|
|
|
|
- this.update(wrapper);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @Transactional
|
|
|
|
- @Override
|
|
|
|
- public void clear(Long examId, String subjectCode) {
|
|
|
|
- studentScoreService.removeBy(examId,subjectCode);
|
|
|
|
- questionService.removeBy(examId,subjectCode);
|
|
|
|
- QueryWrapper<StudentEntity> wrapper = new QueryWrapper<>();
|
|
|
|
- LambdaQueryWrapper<StudentEntity> lw = wrapper.lambda();
|
|
|
|
- if(subjectCode!=null) {
|
|
|
|
- lw.eq(StudentEntity::getSubjectCode, subjectCode);
|
|
|
|
- }
|
|
|
|
- lw.eq(StudentEntity::getExamId, examId);
|
|
|
|
- this.remove(wrapper);
|
|
|
|
- }
|
|
|
|
|
|
+
|
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(StudentService.class);
|
|
|
|
+
|
|
|
|
+ private static final String[] EXCEL_HEADER = new String[] { "考试ID", "科目代码", "考生编号" };
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private SysProperty sysProperty;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private StudentService studentService;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private StudentScoreService studentScoreService;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private QuestionService questionService;
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void importStudent() {
|
|
|
|
+ File dir = new File(sysProperty.getDataDir());
|
|
|
|
+ File[] fs = dir.listFiles();
|
|
|
|
+ if (fs == null || fs.length == 0) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ for (File file : fs) {
|
|
|
|
+ if (!file.isFile() || !file.getName().equals(ImportFileName.STUDENT_IMPORT.getName())) {
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ InputStream inputStream = null;
|
|
|
|
+ ImportResult ret = null;
|
|
|
|
+ try {
|
|
|
|
+ inputStream = new FileInputStream(file);
|
|
|
|
+ ret = studentService.disposeFile(inputStream);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ String errMsg;
|
|
|
|
+ if (e instanceof FileNotFoundException) {
|
|
|
|
+ errMsg = "未找到文件:" + file.getAbsolutePath();
|
|
|
|
+ } else {
|
|
|
|
+ errMsg = "系统错误:" + e.getMessage();
|
|
|
|
+ }
|
|
|
|
+ ret = new ImportResult(errMsg);
|
|
|
|
+ } finally {
|
|
|
|
+ if (inputStream != null) {
|
|
|
|
+ try {
|
|
|
|
+ inputStream.close();
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ moveFile(dir, file, ret);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private void moveFile(File dir, File file, ImportResult ret) {
|
|
|
|
+ try {
|
|
|
|
+ boolean succss = CollectionUtils.isEmpty(ret.getErrMsg());
|
|
|
|
+ if (succss) {
|
|
|
|
+ File sucDir = new File(dir.getAbsoluteFile() + "/success/");
|
|
|
|
+ if (!sucDir.exists()) {
|
|
|
|
+ sucDir.mkdir();
|
|
|
|
+ }
|
|
|
|
+ File targetFile = new File(sucDir.getAbsoluteFile() + "/" + file.getName());
|
|
|
|
+ if (targetFile.exists()) {
|
|
|
|
+ targetFile.delete();
|
|
|
|
+ }
|
|
|
|
+ FileUtils.copyFile(file, targetFile);
|
|
|
|
+ file.delete();
|
|
|
|
+ String fname = file.getName().substring(0, file.getName().lastIndexOf("."));
|
|
|
|
+ File msgFile = new File(sucDir.getAbsoluteFile() + "/" + fname + "_info.txt");
|
|
|
|
+ if (msgFile.exists()) {
|
|
|
|
+ msgFile.delete();
|
|
|
|
+ }
|
|
|
|
+ FileUtils.write(msgFile, ret.getCountInfo(), "utf-8");
|
|
|
|
+ } else {
|
|
|
|
+ File sucDir = new File(dir.getAbsoluteFile() + "/failed/");
|
|
|
|
+ if (!sucDir.exists()) {
|
|
|
|
+ sucDir.mkdir();
|
|
|
|
+ }
|
|
|
|
+ File targetFile = new File(sucDir.getAbsoluteFile() + "/" + file.getName());
|
|
|
|
+ if (targetFile.exists()) {
|
|
|
|
+ targetFile.delete();
|
|
|
|
+ }
|
|
|
|
+ FileUtils.copyFile(file, targetFile);
|
|
|
|
+ file.delete();
|
|
|
|
+ String fname = file.getName().substring(0, file.getName().lastIndexOf("."));
|
|
|
|
+ File msgFile = new File(sucDir.getAbsoluteFile() + "/" + fname + "_info.txt");
|
|
|
|
+ if (msgFile.exists()) {
|
|
|
|
+ msgFile.delete();
|
|
|
|
+ }
|
|
|
|
+ FileUtils.writeLines(msgFile, StandardCharsets.UTF_8.name(), ret.getErrMsg());
|
|
|
|
+ }
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ throw new StatusException("文件处理出错", e);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private String errorMsg(int lineNum, String msg) {
|
|
|
|
+ return "第" + lineNum + "行 " + msg;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private String trimAndNullIfBlank(String s) {
|
|
|
|
+ if (StringUtils.isBlank(s)) {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ return s.trim();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Transactional
|
|
|
|
+ @Override
|
|
|
|
+ public ImportResult disposeFile(InputStream inputStream) {
|
|
|
|
+ List<DataMap> lineList = null;
|
|
|
|
+ ExcelReader reader = ExcelReader.create(ExcelType.XLSX, inputStream, 0);
|
|
|
|
+ try {
|
|
|
|
+ lineList = reader.getDataMapList();
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ throw new StatusException("Excel 解析失败");
|
|
|
|
+ }
|
|
|
|
+ if (!Arrays.equals(EXCEL_HEADER, reader.getColumnNames())) {
|
|
|
|
+ throw new StatusException("Excel表头错误");
|
|
|
|
+ }
|
|
|
|
+ if (CollectionUtils.isEmpty(lineList)) {
|
|
|
|
+ throw new StatusException("Excel无内容");
|
|
|
|
+ }
|
|
|
|
+ if (100001 < lineList.size()) {
|
|
|
|
+ throw new StatusException("数据行数不能超过100000");
|
|
|
|
+ }
|
|
|
|
+ List<StudentEntity> ss = new ArrayList<>();
|
|
|
|
+ ImportResult ret = new ImportResult();
|
|
|
|
+ List<String> failRecords = new ArrayList<>();
|
|
|
|
+ ret.setErrMsg(failRecords);
|
|
|
|
+ for (int i = 0; i < lineList.size(); i++) {
|
|
|
|
+ DataMap line = lineList.get(i);
|
|
|
|
+
|
|
|
|
+ StringBuilder msg = new StringBuilder();
|
|
|
|
+
|
|
|
|
+ StudentEntity imp = new StudentEntity();
|
|
|
|
+ imp.setDataStatus(DataStatus.WAITING);
|
|
|
|
+ String examId = trimAndNullIfBlank(line.get(EXCEL_HEADER[0]));
|
|
|
|
+ if (StringUtils.isBlank(examId)) {
|
|
|
|
+ msg.append(" 考试ID不能为空");
|
|
|
|
+ } else if (examId.length() > 20) {
|
|
|
|
+ msg.append(" 考试ID不能超过20个字符");
|
|
|
|
+ } else {
|
|
|
|
+ try {
|
|
|
|
+ Long examIdVal = Long.parseLong(examId);
|
|
|
|
+ imp.setExamId(examIdVal);
|
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
|
+ msg.append(" 考试ID只能是数字");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ String subjectCode = trimAndNullIfBlank(line.get(EXCEL_HEADER[1]));
|
|
|
|
+ if (StringUtils.isBlank(subjectCode)) {
|
|
|
|
+ msg.append(" 科目代码不能为空");
|
|
|
|
+ } else if (subjectCode.length() > 100) {
|
|
|
|
+ msg.append(" 科目代码不能超过100个字符");
|
|
|
|
+ }
|
|
|
|
+ imp.setSubjectCode(subjectCode);
|
|
|
|
+
|
|
|
|
+ String studentCode = trimAndNullIfBlank(line.get(EXCEL_HEADER[2]));
|
|
|
|
+ if (StringUtils.isBlank(studentCode)) {
|
|
|
|
+ msg.append(" 考生编号不能为空");
|
|
|
|
+ } else if (studentCode.length() > 100) {
|
|
|
|
+ msg.append(" 考生编号不能超过100个字符");
|
|
|
|
+ }
|
|
|
|
+ imp.setStudentCode(studentCode);
|
|
|
|
+
|
|
|
|
+ if (msg.length() > 0) {
|
|
|
|
+ failRecords.add(errorMsg(i + 2, msg.toString()));
|
|
|
|
+ } else {
|
|
|
|
+ ss.add(imp);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (CollectionUtils.isNotEmpty(failRecords)) {
|
|
|
|
+ return ret;
|
|
|
|
+ }
|
|
|
|
+ try {
|
|
|
|
+ saveStudentBatch(ret, ss);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ failRecords.add("系统错误:" + e.getMessage());
|
|
|
|
+ }
|
|
|
|
+ return ret;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private void saveStudentBatch(ImportResult ret, List<StudentEntity> ss) {
|
|
|
|
+ if (CollectionUtils.isEmpty(ss)) {
|
|
|
|
+ ret.setCountInfo("新增数量:0");
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ List<StudentEntity> all = this.list();
|
|
|
|
+ Set<String> set = new HashSet<>();
|
|
|
|
+ if (CollectionUtils.isNotEmpty(all)) {
|
|
|
|
+ for (StudentEntity s : all) {
|
|
|
|
+ String key = s.getExamId() + "-" + s.getSubjectCode() + "-" + s.getStudentCode();
|
|
|
|
+ set.add(key);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ List<StudentEntity> adds = new ArrayList<>();
|
|
|
|
+ for (StudentEntity s : ss) {
|
|
|
|
+ String key = s.getExamId() + "-" + s.getSubjectCode() + "-" + s.getStudentCode();
|
|
|
|
+ if (!set.contains(key)) {
|
|
|
|
+ adds.add(s);
|
|
|
|
+ } else {
|
|
|
|
+ set.add(key);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if (CollectionUtils.isNotEmpty(adds)) {
|
|
|
|
+ saveBatch(adds);
|
|
|
|
+ }
|
|
|
|
+ ret.setCountInfo("新增数量:" + adds.size());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public List<StudentEntity> findToDispose() {
|
|
|
|
+ QueryWrapper<StudentEntity> wrapper = new QueryWrapper<>();
|
|
|
|
+ LambdaQueryWrapper<StudentEntity> lw = wrapper.lambda();
|
|
|
|
+ lw.in(StudentEntity::getDataStatus, DataStatus.WAITING, DataStatus.FAILED);
|
|
|
|
+ return this.list(wrapper);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public List<StudentEntity> findToMarking(Long examId) {
|
|
|
|
+ QueryWrapper<StudentEntity> wrapper = new QueryWrapper<>();
|
|
|
|
+ LambdaQueryWrapper<StudentEntity> lw = wrapper.lambda();
|
|
|
|
+ if (examId != null) {
|
|
|
|
+ lw.eq(StudentEntity::getExamId, examId);
|
|
|
|
+ }
|
|
|
|
+ lw.in(StudentEntity::getDataStatus, DataStatus.WAITING, DataStatus.FAILED, DataStatus.PROCESSING);
|
|
|
|
+ return this.list(wrapper);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void buildImage(StudentEntity student, Map<Long, QuestionEntity> quetions) {
|
|
|
|
+ List<StudentScoreEntity> scores = studentService.getOrCreateScores(student, quetions);
|
|
|
|
+ Map<Integer, AnswerImageDto> answerImages = new HashMap<>();
|
|
|
|
+ for (StudentScoreEntity score : scores) {
|
|
|
|
+ if (DataStatus.WAITING.equals(score.getAnswerStatus())
|
|
|
|
+ || DataStatus.FAILED.equals(score.getAnswerStatus())) {
|
|
|
|
+ studentService.createSlice(score, quetions, answerImages);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Transactional
|
|
|
|
+ @Override
|
|
|
|
+ public void createSlice(StudentScoreEntity score, Map<Long, QuestionEntity> quetions,
|
|
|
|
+ Map<Integer, AnswerImageDto> answerImages) {
|
|
|
|
+ QuestionEntity q = quetions.get(score.getQuestionId());
|
|
|
|
+ if (q == null) {
|
|
|
|
+ studentScoreService.updateAnswerErr(score.getId(), "未找到试题信息");
|
|
|
|
+ updateStatus(score.getStudentId(), DataStatus.FAILED);
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ try {
|
|
|
|
+ studentScoreService.createSlice(score, q, answerImages);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ if (e instanceof StatusException) {
|
|
|
|
+ studentScoreService.updateAnswerErr(score.getId(), e.getMessage());
|
|
|
|
+ updateStatus(score.getStudentId(), DataStatus.FAILED);
|
|
|
|
+ } else {
|
|
|
|
+ log.error("系统异常", e);
|
|
|
|
+ studentScoreService.updateAnswerErr(score.getId(), "系统异常");
|
|
|
|
+ updateStatus(score.getStudentId(), DataStatus.FAILED);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Transactional
|
|
|
|
+ @Override
|
|
|
|
+ public List<StudentScoreEntity> getOrCreateScores(StudentEntity student, Map<Long, QuestionEntity> quetions) {
|
|
|
|
+ List<StudentScoreEntity> oldscores = studentScoreService.getByStudentId(student.getId());
|
|
|
|
+ studentScoreService.add(student, quetions, oldscores);
|
|
|
|
+ oldscores = studentScoreService.getByStudentId(student.getId());
|
|
|
|
+ updateStatus(student.getId(), DataStatus.PROCESSING);
|
|
|
|
+ return oldscores;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Transactional
|
|
|
|
+ @Override
|
|
|
|
+ public void updateStatus(Long id, DataStatus to) {
|
|
|
|
+ UpdateWrapper<StudentEntity> wrapper = new UpdateWrapper<>();
|
|
|
|
+ LambdaUpdateWrapper<StudentEntity> lw = wrapper.lambda();
|
|
|
|
+ lw.set(StudentEntity::getDataStatus, to);
|
|
|
|
+ lw.eq(StudentEntity::getId, id);
|
|
|
|
+ this.update(wrapper);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Transactional
|
|
|
|
+ @Override
|
|
|
|
+ public void resetStatus() {
|
|
|
|
+ UpdateWrapper<StudentEntity> wrapper = new UpdateWrapper<>();
|
|
|
|
+ LambdaUpdateWrapper<StudentEntity> lw = wrapper.lambda();
|
|
|
|
+ lw.set(StudentEntity::getDataStatus, DataStatus.WAITING);
|
|
|
|
+ lw.eq(StudentEntity::getDataStatus, DataStatus.PROCESSING);
|
|
|
|
+ this.update(wrapper);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public int countBy(Long examId, DataStatus status) {
|
|
|
|
+ QueryWrapper<StudentEntity> wrapper = new QueryWrapper<>();
|
|
|
|
+ LambdaQueryWrapper<StudentEntity> lw = wrapper.lambda();
|
|
|
|
+ if (status != null) {
|
|
|
|
+ lw.eq(StudentEntity::getDataStatus, status);
|
|
|
|
+ }
|
|
|
|
+ lw.eq(StudentEntity::getExamId, examId);
|
|
|
|
+ return this.count(wrapper);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Transactional
|
|
|
|
+ @Override
|
|
|
|
+ public void reset(Long examId, String subjectCode) {
|
|
|
|
+ studentScoreService.removeBy(examId, subjectCode);
|
|
|
|
+ UpdateWrapper<StudentEntity> wrapper = new UpdateWrapper<>();
|
|
|
|
+ LambdaUpdateWrapper<StudentEntity> lw = wrapper.lambda();
|
|
|
|
+ lw.set(StudentEntity::getDataStatus, DataStatus.WAITING);
|
|
|
|
+ if (subjectCode != null) {
|
|
|
|
+ lw.eq(StudentEntity::getSubjectCode, subjectCode);
|
|
|
|
+ }
|
|
|
|
+ lw.eq(StudentEntity::getExamId, examId);
|
|
|
|
+ this.update(wrapper);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Transactional
|
|
|
|
+ @Override
|
|
|
|
+ public void clear(Long examId, String subjectCode) {
|
|
|
|
+ studentScoreService.removeBy(examId, subjectCode);
|
|
|
|
+ questionService.removeBy(examId, subjectCode);
|
|
|
|
+ QueryWrapper<StudentEntity> wrapper = new QueryWrapper<>();
|
|
|
|
+ LambdaQueryWrapper<StudentEntity> lw = wrapper.lambda();
|
|
|
|
+ if (subjectCode != null) {
|
|
|
|
+ lw.eq(StudentEntity::getSubjectCode, subjectCode);
|
|
|
|
+ }
|
|
|
|
+ lw.eq(StudentEntity::getExamId, examId);
|
|
|
|
+ this.remove(wrapper);
|
|
|
|
+ }
|
|
}
|
|
}
|