From 1998d0e9a5cb8cb33a4680a56b221b701a28d947 Mon Sep 17 00:00:00 2001
From: Administrator <admin>
Date: Mon, 13 Dec 2021 11:55:34 +0800
Subject: [PATCH] 1. 新增考试时间到达后成绩计算接口 2. 修改个人考试查询接口,新增考试状态,成绩id,考试开始时间(防止考试中途关闭浏览器)

---
 src/main/java/org/springblade/modules/exam/service/impl/ExamScoreServiceImpl.java |  333 ++++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 257 insertions(+), 76 deletions(-)

diff --git a/src/main/java/org/springblade/modules/exam/service/impl/ExamScoreServiceImpl.java b/src/main/java/org/springblade/modules/exam/service/impl/ExamScoreServiceImpl.java
index 21d750c..1a48faa 100644
--- a/src/main/java/org/springblade/modules/exam/service/impl/ExamScoreServiceImpl.java
+++ b/src/main/java/org/springblade/modules/exam/service/impl/ExamScoreServiceImpl.java
@@ -3,8 +3,7 @@
 
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
-import lombok.AllArgsConstructor;
-import org.springblade.common.utils.arg;
+import org.apache.commons.lang3.StringUtils;
 import org.springblade.core.log.exception.ServiceException;
 import org.springblade.core.mp.support.Condition;
 import org.springblade.modules.FTP.FtpUtil;
@@ -22,7 +21,6 @@
 import org.springblade.modules.exam.vo.ExamResultVO;
 import org.springblade.modules.exam.vo.ExamScoreVO;
 import org.springblade.modules.exam.vo.UpdateParamVo;
-import org.springblade.modules.system.entity.Role;
 import org.springblade.modules.system.entity.User;
 import org.springblade.modules.system.service.IRoleService;
 import org.springblade.modules.system.service.IUserService;
@@ -37,8 +35,8 @@
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.*;
-
-import static com.bstek.ureport.expression.model.condition.Join.and;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicInteger;
 
 /**
  * 考试成绩服务实现类
@@ -168,7 +166,6 @@
 				apply1.setIsExam(2);
 				applyService.updateById(apply1);
 
-
 				//数据同步
 				String s =
 					"update sys_apply set is_exam = " + apply1.getIsExam() + " " +"where id = " + "'" + apply1.getId() + "';" +
@@ -195,7 +192,7 @@
 				//数据同步
 				String s =
 					"update sys_training_registration set is_exam = " + trainingRegistration1.getIsExam() + " " +"where id = " + "'" + trainingRegistration1.getId() + "';" +
-						"update blade_user set is_apply = " + user.getIsApply() + " " +"where id = " + "'" + user.getId() + "'";
+						"update blade_user set is_train = " + user.getIsTrain() + " " +"where id = " + "'" + user.getId() + "'";
 				FtpUtil.sqlFileUpload(s);
 			}
 
@@ -245,91 +242,194 @@
 	@Override
 	public void importExamScore(List<ExamScoreExcel> examScoreExcelList, Boolean isCovered) {
 		if (examScoreExcelList.size()>0){
+			List<Object> errorList = new ArrayList<>();
+			//导入状态,默认为true ,如果有一个出现问题则为 false
+			AtomicBoolean status = new AtomicBoolean(true);
+			//遍历
 			examScoreExcelList.forEach(examScoreExcel -> {
 				if (null!=examScoreExcel.getLearnGrade() && null!=examScoreExcel.getIdCardNo() && examScoreExcel.getIdCardNo()!=""){
 					//使用考试名称匹配考试信息
-//					ExamPaper examPaper = examPaperService.getExamInfoByExamName(examScoreExcel.getExamName());
+					List<ExamPaper> examPaperList = examPaperService.getExamInfoByExamName(examScoreExcel.getExamName());
+					ExamPaper examPaper = examPaperList.get(0);
 					//查询出成绩数据
-//					ExamScore examScore = baseMapper.getExamScoreInfoByIdCardNo(examScoreExcel.getIdCardNo(),examPaper.getId());
-					//根据身份证号查询用户
-					User user0 = userService.getUserInfoByIdCardNo(examScoreExcel.getIdCardNo());
-					if (null==user0){
-						throw new ServiceException("用户:["+examScoreExcel.getIdCardNo()+"]不存在");
-					}
-					ExamScore examScore1 = new ExamScore();
-					examScore1.setCandidateNo(examScoreExcel.getCandidateNo());
-					examScore1.setUserId(user0.getId().toString());
-					//根据准考证号和用户id查询考试成绩
-					ExamScore examScore = this.getOne(Condition.getQueryWrapper(examScore1));
-					if (null!=examScore) {
-						//查询当前保安信息
-						UserVO user = userService.getUserAgeById(Long.parseLong(examScore.getUserId()));
-						examScore.setLearnGrade(examScoreExcel.getLearnGrade());
-						if (examScore.getTheoryGrade() >= 60 && examScore.getLearnGrade() >= 60) {
-							//合格
-							examScore.setQualified(0);
-							//正式考试通过生成保安证编号
+					if (examPaperList.size()>0){
+						//取第一个
+						List<ExamScore> examScoreList = baseMapper.getExamScoreInfoByIdCardNo(examScoreExcel.getIdCardNo(),new SimpleDateFormat("yyyy-MM-dd").format(examPaper.getStartTime()));
+						if (examScoreList.size()==0){
+							//根据身份证号查询用户
+							User user0 = userService.getUserInfoByIdCardNo(examScoreExcel.getIdCardNo());
+							if (null==user0){
+								throw new ServiceException("没有找到用户:["+examScoreExcel.getIdCardNo()+"]相关考试成绩信息,请检查身份证是否有误!");
+//								throw new ServiceException("用户:["+examScoreExcel.getIdCardNo()+"]不存在");
+							}
+						}
+						//遍历集合
+						examScoreList.forEach(examScore -> {
+//							ExamScore examScore1 = new ExamScore();
+//							examScore1.setCandidateNo(examScoreExcel.getCandidateNo());
+//							examScore1.setUserId(user0.getId().toString());
+//							//根据准考证号和用户id查询考试成绩
+//							ExamScore examScore = this.getOne(Condition.getQueryWrapper(examScore1));
+							if (null!=examScore) {
+								//查询当前保安信息
+								UserVO user = userService.getUserAgeById(Long.parseLong(examScore.getUserId()));
+								examScore.setLearnGrade(examScoreExcel.getLearnGrade());
+								if (examScore.getTheoryGrade() >= 60 && examScore.getLearnGrade() >= 60) {
+									//合格
+									examScore.setQualified(0);
+									//修改制证状态为未制证
+									user.setUserType(7);
+									//正式考试通过生成保安证编号
 //							if (examPaper.getExamType()==1) {
-								//去生成保安证编号
-								String pre = SecurityPaperUtil.getSecurityPaper();
-								//查询当前年份已有的保安证编号
-								int count = userService.getSecurityPaperCount(pre);
-								String result = null;
-								if (count == 0) {
-									result = pre + "00001";
+									//如果已有保安证编号,不更新用户信息
+									if (null==user.getSecuritynumber() || user.getSecuritynumber().equals("")) {
+										//去生成保安证编号
+										String pre = SecurityPaperUtil.getSecurityPaper();
+										//查询当前年份已有的保安证编号
+										int count = userService.getSecurityPaperCount(pre);
+										String result = null;
+										if (count == 0) {
+											result = pre + "00001";
+										} else {
+											//格式化
+											DecimalFormat decimalFormat = new DecimalFormat("00000");
+											count++;
+											result = pre + (decimalFormat.format(count));
+										}
+										user.setSecuritynumber(result);
+										//发证日期
+//								user.setPaperTime(new Date());
+										//修改为持证保安
+										user.setHold("1");
+										user.setUpdateTime(new Date());
+										//更新保安数据
+										userService.updateById(user);
+
+										String s1 =
+											"update blade_user set hold = " + "'" + user.getHold() + "'" +
+												",securitynumber = " + "'" + user.getSecuritynumber() + "'" +
+												",update_time = " + "'" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(user.getUpdateTime()) + "'" +
+												",user_type = " + "'" + user.getUserType() + "'" +
+												" " + "where id = " + "'" + user.getId() + "'";
+										FtpUtil.sqlFileUpload(s1);
+									}
 								} else {
-									//格式化
-									DecimalFormat decimalFormat = new DecimalFormat("00000");
-									count++;
-									result = pre + (decimalFormat.format(count));
+									//不合格
+									examScore.setQualified(1);
 								}
-								user.setSecuritynumber(result);
-								//发证日期
-								user.setPaperTime(new Date());
-								//修改为持证保安
-								user.setHold("1");
-								//分配保安角色
-//								Role role = new Role();
-//								role.setRoleAlias("保安");
-//								Role oneRole = roleService.getOne(Condition.getQueryWrapper(role));
-//								user.setRoleId(oneRole.getId().toString());
-								//更新保安数据
-								userService.updateById(user);
+								//总成绩
+								if (user.getAge() <= 50) {
+									if (null!=examScore.getTheoryGrade()) {
+										examScore.setAllGrade(Math.round((examScore.getTheoryGrade() + examScoreExcel.getLearnGrade()) / 2));
+									}else {
+										examScore.setAllGrade(Math.round(examScoreExcel.getLearnGrade() / 2));
+									}
+								}
+								if (user.getAge() > 50) {
+									if (null!=examScore.getTheoryGrade()) {
+										examScore.setAllGrade(Integer.parseInt(String.valueOf(Math.round(examScore.getTheoryGrade() * 0.5)))
+											+ Integer.parseInt(String.valueOf(Math.round(examScoreExcel.getLearnGrade() * 0.5))));
+									}else {
+										examScore.setAllGrade(Math.round(examScoreExcel.getLearnGrade() / 2));
+									}
+								}
 
+								//内网同步
 								String s1 =
-									"update blade_user set hold = " + "'" +  user.getHold() + "'" +
-										",securitynumber = " + "'" + user.getSecuritynumber() + "'" +
-										",paper_time = " + "'" + new SimpleDateFormat("yyyy-MM-dd").format(user.getPaperTime()) + "'" +
-										" " +"where id = " + "'" + user.getId() + "'";
+									"update exam_score set learn_grade = " + "'" + examScore.getLearnGrade() + "'" +
+										",all_grade = " + "'" + examScore.getAllGrade() + "'" +
+										",qualified = " + "'" + examScore.getQualified() + "'" +
+										" " + "where id = " + "'" + examScore.getId() + "'";
 								FtpUtil.sqlFileUpload(s1);
-//							}
-						} else {
-							//不合格
-							examScore.setQualified(1);
-						}
-						//总成绩
-						if (user.getAge()<=50) {
-							examScore.setAllGrade(Math.round((examScore.getTheoryGrade()+examScoreExcel.getLearnGrade())/2));
-						}
-						if (user.getAge()>50) {
-							examScore.setAllGrade(Integer.parseInt(String.valueOf(Math.round(examScore.getTheoryGrade()*0.5)))
-								+ Integer.parseInt(String.valueOf(Math.round(examScoreExcel.getLearnGrade()*0.5))));
-						}
+								//更新成绩数据
+								baseMapper.updateById(examScore);
+							}else {
+								errorList.add(examScoreExcel.getIdCardNo());
+								status.set(false);
+							}
+						});
 
-						//内网同步
-						String s1 =
-							"update exam_score set learn_grade = " + "'" + examScore.getLearnGrade() + "'" +
-								",all_grade = " + "'" + examScore.getAllGrade() + "'" +
-								",qualified = " + "'" + examScore.getQualified() + "'" +
-								" " +"where id = " + "'" + examScore.getId() + "'";
-						FtpUtil.sqlFileUpload(s1);
-						//更新成绩数据
-						baseMapper.updateById(examScore);
+					}else {
+						throw new ServiceException("导入失败!考试名称:["+examScoreExcel.getExamName()+"]不存在!请核对!");
 					}
 				}
 			});
+			//如果所有数据导入有一个异常
+			if (!status.get()){
+				String errorAccount = StringUtils.join(errorList, "\\\n");
+				throw new ServiceException("用户:["+errorAccount+"]实操成绩导入失败!账号与准考证号不匹配!");
+			}
 		}
 	}
+
+
+//	/**
+//	 * 导入实操成绩--测试
+//	 * @param examScoreExcelList
+//	 * @param isCovered 是否覆盖
+//	 */
+//	@Override
+//	public void importExamScore(List<ExamScoreExcel> examScoreExcelList, Boolean isCovered) {
+//		if (examScoreExcelList.size()>0){
+//			List<Object> errorList = new ArrayList<>();
+//			//导入状态,默认为true ,如果有一个出现问题则为 false
+//			AtomicBoolean status = new AtomicBoolean(true);
+//			AtomicInteger num = new AtomicInteger();
+//			//遍历
+//			examScoreExcelList.forEach(examScoreExcel -> {
+//				//根据身份证号查询培训信息
+//				User user = userService.getUserInfoByIdCardNo(examScoreExcel.getIdCardNo());
+////				TrainingRegistration registration = new TrainingRegistration();
+////				registration.setCandidateNo(examScoreExcel.getCandidateNo());
+////				TrainingRegistration one1 = trainingRegistrationService.getOne(Condition.getQueryWrapper(registration));
+////				测试生成考试成绩
+//				ExamScore examScore1 = new ExamScore();
+//				examScore1.setCandidateNo(examScoreExcel.getCandidateNo());
+//				examScore1.setUserId(user.getId().toString());
+//				//根据准考证号和用户id查询考试成绩
+//				ExamScore examScore = this.getOne(Condition.getQueryWrapper(examScore1));
+//				examScore.setQualified(0);
+//				//根据身份证号查询用户
+//				examScore.setTheoryGrade(examScoreExcel.getTheoryGrade());
+//				examScore.setAllGrade((examScore.getLearnGrade()+examScore.getTheoryGrade())/2);
+//
+//				//新增
+//				this.updateById(examScore);
+//
+//				//生成准考证号
+//				if (null==user.getSecuritynumber() || user.getSecuritynumber().equals("") ) {
+//					//去生成保安证编号
+//					String pre = SecurityPaperUtil.getSecurityPaper();
+//					//查询当前年份已有的保安证编号
+//					int count = userService.getSecurityPaperCount(pre);
+//					String result = null;
+//					if (count == 0) {
+//						result = pre + "00001";
+//					} else {
+//						//格式化
+//						DecimalFormat decimalFormat = new DecimalFormat("00000");
+//						count++;
+//						result = pre + (decimalFormat.format(count));
+//					}
+//					user.setSecuritynumber(result);
+//					//修改为持证保安
+//					user.setHold("1");
+////					user.setRegistered(examScoreExcel.getExamName());
+//					//更新保安数据
+//					userService.updateById(user);
+//					num.getAndIncrement();
+//				}
+//			});
+//			//如果所有数据导入有一个异常
+//			if (!status.get()){
+//				String errorAccount = StringUtils.join(errorList, "\\\n");
+//				throw new ServiceException("用户:["+errorAccount+"]实操成绩导入失败!账号与准考证号不匹配!");
+//			}
+//			if (status.get()){
+//				throw new ServiceException("更新成功:"+ num.get());
+//			}
+//		}
+//	}
 
 	@Override
 	public List<Map<String, Object>> scoreStatistics(String deptid,String jurisdiction) {
@@ -616,4 +716,85 @@
 		});
 		return false;
 	}
+
+	/**
+	 * 点击了开始考试后超过60分钟后再考试的,计算之前的考试成绩
+	 * @param examScore
+	 * @return
+	 */
+	@Override
+	public boolean getExamScore(ExamScoreVO examScore) {
+		//查询考试成绩
+		ExamScore score = this.getById(examScore.getId());
+		//查询考试记录
+		ExamAnswerRecord examAnswerRecord = new ExamAnswerRecord();
+		examAnswerRecord.setScoreId(examScore.getId());
+		List<ExamAnswerRecord> list = examAnswerRecordService.list(Condition.getQueryWrapper(examAnswerRecord));
+		//设置默认理论成绩为0
+		int count = 0;
+		//计算成绩
+		if (list.size()>0){
+			//成绩相加
+			count = list.stream().mapToInt(ExamAnswerRecord::getAnswerScore).sum();
+
+		}else {
+			//没有考试记录,本次考试理论成绩为 0
+			count = 0;
+		}
+		int age = 0;
+		UserVO user = new UserVO();
+		//查询培训报名数据
+		TrainingRegistration trainingRegistration = new TrainingRegistration();
+		trainingRegistration.setCandidateNo(score.getCandidateNo());
+		trainingRegistration.setCancel(1);
+		TrainingRegistration trainingRegistration1 = trainingRegistrationService.getOne(Condition.getQueryWrapper(trainingRegistration));
+		//查询用户年龄,根据年龄计算总分
+		user = userService.getUserAgeById(Long.parseLong(trainingRegistration1.getUserId()));
+		//修改用户的培训状态,理论考试结束
+		user.setIsTrain(3);
+		age = user.getAge();
+		userService.updateById(user);
+		//修改为已考试
+		trainingRegistration1.setIsExam(2);
+		trainingRegistrationService.updateById(trainingRegistration1);
+
+		//数据同步
+		String s =
+			"update sys_training_registration set is_exam = " + trainingRegistration1.getIsExam() + " " +"where id = " + "'" + trainingRegistration1.getId() + "';" +
+				"update blade_user set is_train = " + user.getIsTrain() + " " +"where id = " + "'" + user.getId() + "'";
+		FtpUtil.sqlFileUpload(s);
+
+		//设置理论得分
+		score.setTheoryGrade(count);
+		//计算总成绩,此时没有实操成绩,总成绩为实操成绩和理论成绩和的一半
+		if (age<=50) {
+			score.setAllGrade(Math.round(count / 2));
+		}
+		if (age>50) {
+			score.setAllGrade(Integer.parseInt(String.valueOf(Math.round(count*0.5))));
+		}
+		//设置状态
+		if (count>=60){
+			score.setQualified(2);
+		}else {
+			score.setQualified(1);
+		}
+		//修改成绩数据
+		int i = baseMapper.updateById(score);
+
+		String s1 =
+			"update exam_score set exam_id = " + score.getExamId() +
+				",theory_grade = " + "'" + score.getTheoryGrade() + "'" +
+				",all_grade = " + "'" + score.getAllGrade() + "'" +
+				",exam_end_time = " + "'" + new SimpleDateFormat("yyyy-MM-dd HH:mm:dd").format(new Date()) + "'" +
+				",qualified = " + "'" + score.getQualified() + "'" +
+				" " +"where id = " + "'" + score.getId() + "'";
+		FtpUtil.sqlFileUpload(s1);
+
+		if (i>0){
+			//返回结果
+			return true;
+		}
+		return false;
+	}
 }

--
Gitblit v1.9.3