package com.thhy.secure.modules.biz.exam.service.impl; import com.thhy.general.common.BasicStatus; import com.thhy.general.config.SysUserInfo; import com.thhy.general.exception.BasicException; import com.thhy.general.utils.DeepCopyUtils; import com.thhy.general.utils.PagingUtils; import com.thhy.general.utils.UserInfoUtils; import com.thhy.secure.modules.biz.exam.entity.*; import com.thhy.secure.modules.biz.exam.mapper.*; import com.thhy.secure.modules.biz.exam.service.ExamService; import org.checkerframework.checker.units.qual.A; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.io.Serializable; import java.util.*; /** * 服务实现类 * @author zhang_xiao_bo * @since 2023-05-26 09:14:42 */ @Service public class ExamServiceImpl implements ExamService { @Autowired private ExamMapper examMapper; @Autowired private ExamRecordMapper recordMapper; @Autowired private QuestionMapper questionMapper; @Autowired private AnswerMapper answerMapper; @Autowired private ExamReAnswerMapper reAnswerMapper; public Exam get(Serializable id){ return examMapper.queryById(id); } public List findList(ExamDto examDto){ SysUserInfo userInfo = UserInfoUtils.getInstance().getUserInfo(); if(examDto==null)examDto = new ExamDto(); examDto.setCompanyId(userInfo.getCompanyId()); return examMapper.findList(examDto); } /** * 小程序考核列表 * @param examDto * @return */ @Override public List miniList(ExamDto examDto) { SysUserInfo userInfo = UserInfoUtils.getInstance().getUserInfo(); if(examDto==null)examDto = new ExamDto(); examDto.setCompanyId(userInfo.getCompanyId()); if(userInfo.getUserType()==1){ //官服人员 return examMapper.miniList(examDto); }else { //劳务人员 String platUserId = examMapper.queryPlatUser(userInfo.getUserId()); examDto.setUserId(platUserId); return examMapper.miniList(examDto); } } /** * 增加和修改 * @param exam */ @Transactional public void addExam(Exam exam){ SysUserInfo userInfo = UserInfoUtils.getInstance().getUserInfo(); if(exam.getExamId() == null){ //增加操作 exam.setCreateUser(userInfo.getUserId()); exam.setCompanyId(userInfo.getCompanyId()); examMapper.insert(exam); }else{ //修改操作 exam.setUpdateTime(new Date()); examMapper.update(exam); } } /** * 修改 * @param exam */ public void update(Exam exam){ examMapper.update(exam); } /** * 删除 * @param examId */ public void delete(Serializable examId){ examMapper.deletelogic(examId); } @Override public List examList(ExamDto examDto) { SysUserInfo userInfo = UserInfoUtils.getInstance().getUserInfo(); if(examDto==null)examDto = new ExamDto(); examDto.setCompanyId(userInfo.getCompanyId()); return examMapper.examList(examDto); } @Override public List examReportList(ExamDto examDto) { SysUserInfo userInfo = UserInfoUtils.getInstance().getUserInfo(); if(examDto==null)examDto=new ExamDto(); examDto.setCompanyId(userInfo.getCompanyId()); List reportList = examMapper.examReportList(examDto); for(ExamSampleListVo es : reportList){ examDto.setExamId(es.getExamId()); int planExamUser = recordMapper.planUserCountByExam(examDto); int totalExamUser = recordMapper.countByExam(examDto); examDto.setIsPass(1); int passExamUser = recordMapper.countByExam(examDto); int noPassExanUser = totalExamUser - passExamUser; es.setPlanUserCount(planExamUser); es.setTotalUserCount(totalExamUser); es.setPassUserCount(passExamUser); es.setNoPassUserCount(noPassExanUser); es.setNoExamUserCount(planExamUser-totalExamUser); } return reportList; } @Override public Map goExam(ExamDto examDto) { Map map = new HashMap<>(); Exam exam = examMapper.queryById(examDto.getExamId()); List qaList = questionMapper.findAllQue(examDto.getExamId()); if(qaList.size()<1){ throw new BasicException(BasicStatus.QUESTION_COUNT_IS_ZERO); } Integer queCount = 0; /*if(qaList.size(){ List answerList = answerMapper.findAllByQueId(new Answer().setQueId(ql.getQueId())); ql.setAnswerList(answerList); }); map.put("queList",qaList); queCount = qaList.size(); }else { List newList = new ArrayList<>(); //int[] randomArray = new int[exam.getQuestionCount()]; Random random = new Random(); for(int i= 0;i{ List answerList = answerMapper.findAllByQueId(new Answer().setQueId(nl.getQueId())); nl.setAnswerList(answerList); }); map.put("queList",newList); queCount = newList.size(); }*/ qaList.forEach(ql->{ List answerList = answerMapper.findAllByQueId(new Answer().setQueId(ql.getQueId())); ql.setAnswerList(answerList); }); map.put("queList",qaList); queCount = qaList.size(); SysUserInfo userInfo = UserInfoUtils.getInstance().getUserInfo(); ExamRecord examRecord = new ExamRecord(); examRecord.setExamId(examDto.getExamId()); examRecord.setUserId(userInfo.getUserId()); examRecord.setStartTime(new Date()); examRecord.setQueCount(queCount); recordMapper.insert(examRecord); map.put("recordId",examRecord.getRecordId()); return map; } @Override public Map commitAnswer(CommitAnswerDto commitAnswerDto) { Map map = new HashMap<>(); String answerIds = commitAnswerDto.getAnswerIds(); ExamReAnswer examReAnswer = new ExamReAnswer(); DeepCopyUtils.copy(commitAnswerDto,examReAnswer); ExamRecord examRecord = recordMapper.queryById(examReAnswer.getRecordId()); String[] answerArray = new String[]{answerIds}; if(answerIds.contains(",")){ answerArray = answerIds.split(","); } for(String answerId : answerArray){ examReAnswer.setExamId(examRecord.getExamId()); examReAnswer.setAnswerId(answerId); reAnswerMapper.insert(examReAnswer); } int commitedCount = reAnswerMapper.countByRecordId(examReAnswer.getRecordId()); System.out.println("当前答题数量"+commitedCount); System.out.println("题库数量"+examRecord.getQueCount()); if(commitedCount < examRecord.getQueCount()){ //继续答题 map.put("isFinish",0); }else if(commitedCount == examRecord.getQueCount()){ //答题结束 map.put("isFinish",1); double totalScore = reAnswerMapper.examScoreCul(examReAnswer.getRecordId()); map.put("totalScore",totalScore); recordMapper.ExamFinish(examRecord.setIsFinish(1).setScore(totalScore)); } return map; } public static void main(String[] args) { int[] randomArray = new int[20]; Random random = new Random(); for(int i= 0;i<20;i++){ randomArray[i] = random.nextInt(1000); } for(int i= 0;i