package com.ots.project.exam.restcontroller; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.ots.common.enums.LangTypeEnum; import com.ots.common.enums.TestMemberStatusEnum; import com.ots.common.enums.UserTypeEnum; import com.ots.common.utils.StringUtils; import com.ots.common.utils.Threads; import com.ots.common.utils.bean.BeanUtils; import com.ots.project.common.BaseApiController; import com.ots.project.exam.domain.*; import com.ots.project.exam.dto.*; import com.ots.project.exam.service.*; import com.ots.project.exam.service.impl.TExamPaperServiceImpl; import com.ots.project.exam.viewmodel.ExamPaperSubmitItemVM; import com.ots.project.exam.viewmodel.ExamPaperSubmitVM; import com.ots.project.exam.viewmodel.ExamPaperTitleItemVM; import com.ots.project.exam.viewmodel.QuestionEditRequestVM; import com.ots.project.tool.exam.DateTimeUtil; import com.ots.project.tool.exam.ExamUtil; import com.ots.project.tool.exam.JsonUtil; import com.ots.project.tool.report.MAQ.MAQReport; import com.ots.project.tool.report.ReportResultData; import lombok.AllArgsConstructor; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import javax.validation.Valid; import java.util.*; import java.util.stream.Collectors; import static com.ots.common.enums.ReportTypeEnum.Brief; import static com.ots.common.enums.SenderCode.noSend; import static com.ots.common.enums.SenderCode.send; @RestController("StudentExamPaperAnswerController") @RequestMapping(value = "/api/exam/exampaper/answer") @AllArgsConstructor public class ExamPaperAnswerController extends BaseApiController { @Autowired private ExamPaperAnswerService examPaperAnswerService; @Autowired private MemberService memberService; @Autowired private ITQuestionService questionService; @Autowired private ITExamReportService examReportService; @Autowired private IEntTestMemberService entTestMemberService; @Autowired private ExamPaperController examPaperController; @Autowired private TExamPaperServiceImpl examPaperService; private static final Logger logger = LoggerFactory.getLogger(Threads.class); // 答题提交 @RequestMapping(value = "/answerSubmit/{tokenId}", method = RequestMethod.POST) public RestResponse answerSubmit(@RequestBody @Valid ExamPaperSubmitVM examPaperSubmitVM, @PathVariable String tokenId) { logger.info("tokenId:" + tokenId); logger.info("itemOrder:" + examPaperSubmitVM.getItemOrder()); try { // 1、查询出用户的信息 ExamUser user = new ExamUser(); // testId等 RelatedParty relatedParty = memberService.queryProdidByToken(tokenId); //入人口学的键值 // Map examInfoMap = getDemographicReport(tokenId, user); // dataReportList 对应基础数据导出content那个字段 List dataReportList = getDemographicDataReport(examPaperSubmitVM, tokenId, user); // 3、把question取出来 Map questionObjectMap = getLongQuestionObjectMap(examPaperSubmitVM); // 4、测试包名称、产品名称、测试开始时间、测试完成时间的 // setReportInfo(examPaperSubmitVM, relatedParty, examInfoMap); setReportInfo(examPaperSubmitVM, relatedParty, dataReportList); examPaperSubmitVM.setInterfaceContentList(new ArrayList<>()); TExamReport tExamReportQuery = new TExamReport(); tExamReportQuery.setTokenId(tokenId); setExamPaperSubTERList(examPaperSubmitVM, tExamReportQuery); // 5、保存调用泰国团队的数据,第一段结束调用泰国团队 // setRemoteCallReport(examPaperSubmitVM, tokenId, relatedParty, dataReportList); //6、形成题目、回答选项,需要权限等等 // setQuestionAndAnswer(examPaperSubmitVM, tokenId, relatedParty, dataReportList, questionObjectMap); // 7、上一次选什么 Map lastContentMap = getLastChioce(examPaperSubmitVM); // 8、这次选什么 Map choiceMap = getThisChioce(examPaperSubmitVM); // 9、设置报表的内容 setQuestionReport(examPaperSubmitVM); // 10、题目入库 ExamPaperAnswerInfo examPaperAnswerInfo = answerSubmitResult(examPaperSubmitVM, tokenId, relatedParty, user, dataReportList, lastContentMap, choiceMap); return RestResponse.ok(examPaperAnswerInfo.getSubmitReport()); } catch (Exception e) { logger.error("{}", e.getMessage(), e); TExamReport tExamReportUpdate = new TExamReport(); tExamReportUpdate.setTokenId(tokenId); tExamReportUpdate.setFinish(0); List tExamReportList = examReportService.selectTExamReportList(tExamReportUpdate); tExamReportUpdate.setRemark(tExamReportList.get(0).getRemark() + "," + e.getMessage()); examReportService.updateTExamReportDeleteLastOneByTokenId(tExamReportUpdate); throw e; } } private void setExamPaperSubTERList(@RequestBody @Valid ExamPaperSubmitVM examPaperSubmitVM, TExamReport tExamReportQuery) { List tExamReportList = examReportService.selectTExamReportList(tExamReportQuery); if (ExamUtil.isListEmpty(tExamReportList)) { tExamReportList = new ArrayList<>(); } examPaperSubmitVM.setTExamReportList(tExamReportList); } private void setRemoteCallReport(ExamPaperSubmitVM examPaperSubmitVM, @PathVariable String tokenId, RelatedParty relatedParty, List dataReportList) { List interfaceContentList = examPaperSubmitVM.getInterfaceContentList(); if (!StringUtils.equals(examPaperSubmitVM.getIsCallTain(), "callTain")) { // 要取原来的数据 setInterfaceContent(examPaperSubmitVM, interfaceContentList); return; } // 模拟调用 Map thaiCalculationMap = new HashMap<>(); MAQReport maqReport = new MAQReport(); maqReport.setQuestionnaireTaker(relatedParty.getMemberName()); maqReport.setQuestionnaireID(tokenId); maqReport.setReportGenerationDate(DateTimeUtil.dateShortFormat(new Date())); thaiCalculationMap.put("P_SDE_T1", "95"); thaiCalculationMap.put("P_IM_T1", "95"); thaiCalculationMap.put("P_INCON_T", "95"); thaiCalculationMap.put("P_INCON_T1", "95"); thaiCalculationMap.put("P_SDE23", "95"); thaiCalculationMap.put("P_IM24", "95"); thaiCalculationMap.put("P_INCON25", "96"); thaiCalculationMap.put("P_Flex01", "80"); thaiCalculationMap.put("P_Creat02", "80"); ReportResultData resultData = maqReport.getTemplateParameters(thaiCalculationMap, Brief, LangTypeEnum.English); Map thaiDataMap = resultData.getTextMap(); InterfaceContent interfaceContent = new InterfaceContent(); interfaceContent.setPartOrder(examPaperSubmitVM.getPartOrder()); interfaceContent.setDataReportList(new ArrayList<>()); List iterFDataReportList = interfaceContent.getDataReportList(); thaiDataMap.forEach((key, value) -> { iterFDataReportList.add(new DataReport(key, value, UserTypeEnum.SYS_USER.getUserType(), examPaperSubmitVM.getLangType())); }); // 把旧的report表数据拿出来 setInterfaceContent(examPaperSubmitVM, interfaceContentList); interfaceContentList.add(interfaceContent); } private void setInterfaceContent(ExamPaperSubmitVM examPaperSubmitVM, List interfaceContentList) { if (ExamUtil.isListNotEmpty(examPaperSubmitVM.getTExamReportList())) { List interfaceContentOldList = null; String interfaceContentStr = examPaperSubmitVM.getTExamReportList().get(0).getInterfaceContent(); if (StringUtils.isNotEmpty(interfaceContentStr)) { interfaceContentOldList = JsonUtil.toJsonListObject(interfaceContentStr, InterfaceContent.class); interfaceContentList.addAll(interfaceContentOldList); } } } // 测试包名称、产品名称、测试开始时间、测试完成时间的 private void setReportInfo(@RequestBody @Valid ExamPaperSubmitVM examPaperSubmitVM, RelatedParty relatedParty, List dataReportList) { dataReportList.add(new DataReport("Test Package", relatedParty.getTestName(), "", examPaperSubmitVM.getLangType())); dataReportList.add(new DataReport("Test", relatedParty.getProdName(), "", examPaperSubmitVM.getLangType())); List answerItemList = examPaperSubmitVM.getAnswerItems(); String beginTime = answerItemList.get(0).getStartTime(); String endTime = answerItemList.get(answerItemList.size() - 1).getEndTime(); dataReportList.add(new DataReport("Begin Time", beginTime, "", examPaperSubmitVM.getLangType())); dataReportList.add(new DataReport("End Time", endTime, "", examPaperSubmitVM.getLangType())); } // 这次选什么 private Map getThisChioce(@RequestBody @Valid ExamPaperSubmitVM examPaperSubmitVM) { Map choiceMap = new LinkedHashMap(); examPaperSubmitVM.getAnswerItems().stream().forEach(it -> { choiceMap.put(it.getItemOrder().toString(), it.getContent()); }); return choiceMap; } // 上一次选什么 private Map getLastChioce(@RequestBody @Valid ExamPaperSubmitVM examPaperSubmitVM) { Map lastContentMap = new LinkedHashMap(); examPaperSubmitVM.getAnswerItems().stream().forEach(it -> { lastContentMap.put(it.getItemOrder().toString(), it.getLastContent()); }); return lastContentMap; } // 4、形成题目、回答选项 private void setQuestionAndAnswer(@RequestBody @Valid ExamPaperSubmitVM examPaperSubmitVM, String tokenId, RelatedParty relatedParty, List dataReportList, Map questionObjectMap) { List questionAnswers = examPaperSubmitVM.getAnswerItems().stream().map(item -> { //题干,回答项 QuestionAnswer questionAnswer = new QuestionAnswer(); QuestionObject questionObject = questionObjectMap.get(item.getQuestionId().longValue()); try { //1.取题干 questionAnswer.setQuestionItem(item.getItemOrder() + "." + ExamUtil.html2Text(questionObject.getTitleContent())); //2、取回答的选项 questionObject.getQuestionItemObjects().stream().forEach(q -> { if (item.getContent().equals(q.getPrefix())) { questionAnswer.setAnswerItem(q.getContent()); } }); // 3、题目永久编号 questionAnswer.setPermanentId(item.getQuestionReport().getPermanentId()); // 4、本题目在那一段 questionAnswer.setPartOrder(item.getQuestionReport().getPartOrder()); } catch (Exception e) { } return questionAnswer; }).collect(Collectors.toList()); for (int i = 0; i < questionAnswers.size(); i++) { if (StringUtils.isEmpty(questionAnswers.get(i).getAnswerItem())) { break; } // 插入泰国团队进来的数据,造数据,如果跳到了下一段,那就插入泰国团队的数据 if (i == 0 || questionAnswers.get(i - 1).getPartOrder() != questionAnswers.get(i).getPartOrder()) { if (ExamUtil.isListNotEmpty(examPaperSubmitVM.getInterfaceContentList())) { InterfaceContent interfaceContent = examPaperSubmitVM.getInterfaceContentList().get(0); dataReportList.addAll(interfaceContent.getDataReportList()); } } dataReportList.add(new DataReport(questionAnswers.get(i).getPermanentId(), questionAnswers.get(i).getAnswerItem(), UserTypeEnum.SYS_USER.getUserType(), examPaperSubmitVM.getLangType())); } } // 把question取出来 private Map getLongQuestionObjectMap(@RequestBody @Valid ExamPaperSubmitVM examPaperSubmitVM) { List questionIdList = examPaperSubmitVM.getAnswerItems().stream().map(i -> { return i.getQuestionId().longValue(); }).collect(Collectors.toList()); //把quesitonId取出来 List tQuestionList = questionService.selectTQuestionByIds(questionIdList); //题目id,把题干,选项之类的内容取出来 Map questionObjectMap = new HashMap(); tQuestionList.stream().forEach(it -> { questionObjectMap.put(it.getId(), JSONObject.parseObject(it.getContent(), QuestionObject.class)); }); return questionObjectMap; } //入人口学的键值 private Map getDemographicReport(@PathVariable String tokenId, ExamUser user) { Map examInfoMap = new LinkedHashMap(); try { List entDemographyInfoList = memberService.queryDemographyParamInfo(tokenId); //2、获取用户的值 EntDemographyInfo entDemographyInfo = entDemographyInfoList.get(0); user.setId(entDemographyInfo.getMemberId().intValue()); entDemographyInfoList.stream().forEach(item -> examInfoMap.put(item.getParamName(), item.getParamValue())); } catch (Exception e) { RelatedParty relatedParty = memberService.queryProdidByToken(tokenId); user.setId(relatedParty.getMemberId().intValue()); } return examInfoMap; } //入人口学的键值 private List getDemographicDataReport(ExamPaperSubmitVM examPaperSubmitVM, @PathVariable String tokenId, ExamUser user) { List dataReportList = new ArrayList<>(); try { List entDemographyInfoList = memberService.queryDemographyParamInfo(tokenId); //2、获取用户的值 EntDemographyInfo entDemographyInfo = entDemographyInfoList.get(0); user.setId(entDemographyInfo.getMemberId().intValue()); entDemographyInfoList.stream().forEach(item -> { dataReportList.add(new DataReport(item.getParamName(), item.getParamValue(), "", examPaperSubmitVM.getLangType())); }); } catch (Exception e) { RelatedParty relatedParty = memberService.queryProdidByToken(tokenId); user.setId(relatedParty.getMemberId().intValue()); } return dataReportList; } private void setQuestionReport(@RequestBody @Valid ExamPaperSubmitVM examPaperSubmitVM) { List titleItems = examPaperSubmitVM.getTitleItems(); List anserItems = examPaperSubmitVM.getAnswerItems(); // 人格测试题,只有一段的 if (Objects.isNull(titleItems)) { if (Objects.isNull(anserItems)) { return; } for (ExamPaperSubmitItemVM examPaperSubmitItemVM : anserItems) { QuestionReport questionReport = examPaperSubmitItemVM.getQuestionReport(); questionReport.setId(questionReport.getQuestionOrder() + 1); questionReport.setTimeSpent(ExamUtil.timeSpent(DateTimeUtil.parseToOften(questionReport.getStartTime()), DateTimeUtil.parseToOften(questionReport.getEndTime()))); } // else 工作问卷测试题 } else { for (ExamPaperTitleItemVM examPaperTitleItemVM : titleItems) { // 记录JAQ小题 QuestionReport questionReport = examPaperTitleItemVM.getQuestionReport(); questionReport.setId(questionReport.getQuestionOrder() + 1); questionReport.setTimeSpent(ExamUtil.timeSpent(DateTimeUtil.parseToOften(questionReport.getStartTime()), DateTimeUtil.parseToOften(questionReport.getEndTime()))); } } } private ExamPaperAnswerInfo answerSubmitResult(@RequestBody @Valid ExamPaperSubmitVM examPaperSubmitVM, @PathVariable String tokenId, RelatedParty relatedParty, ExamUser user, List dataReportList, Map lastContentMap, Map choiceMap) { TExamReport tExamReport = new TExamReport(); TExamReport tExamReportQuery = new TExamReport(); tExamReport.setContent(JSON.toJSON(dataReportList).toString()); tExamReport.setProductId(examPaperSubmitVM.getId().longValue()); tExamReport.setQuestionReport(getQuestionReportsJson(examPaperSubmitVM));// 报告字段 ExamPaperAnswerInfo examPaperAnswerInfo = examPaperAnswerService.calculateExamPaperAnswer(examPaperSubmitVM, user); TExamPaper examPaper = examPaperAnswerInfo.getExamPaper(); tExamReport.setProductName(examPaper.getName()); tExamReport.setTokenId(tokenId); tExamReport.setStates("1"); tExamReport.setLastContent(JSON.toJSON(lastContentMap).toString()); tExamReport.setChoice(JSON.toJSON(choiceMap).toString()); tExamReport.setDoTime(examPaperSubmitVM.getDoTime()); tExamReport.setLangType(examPaperSubmitVM.getLangType()); // 是否自动发送状态报告 setAutoSendReportStatus(relatedParty, tExamReport); BeanUtils.copyBeanProp(tExamReportQuery, tExamReport); tExamReportQuery.setProductId(null); tExamReportQuery.setProductName(null); List tExamReportList = examPaperSubmitVM.getTExamReportList(); // ExamPaperAnswer examPaperAnswer = examPaperAnswerInfo.getExamPaperAnswer(); // questionCount 做题数目 tExamReport.setQuestionCount(0); //member_id tExamReport.setMemberId(relatedParty.getMemberId().intValue()); // user_id tExamReport.setUserId(relatedParty.getUser_id()); // parent_id tExamReport.setParentId(relatedParty.getUser_parent_id()); // test_id tExamReport.setTestId(relatedParty.getTestId()); // finish tExamReport.setFinish(examPaperSubmitVM.getFinish()); setVerifyStatus(examPaperSubmitVM, tExamReport); // questionTemplateId tExamReport.setQuestionTemplateId(relatedParty.getQuestionTemplateId()); // 随机题目编号 tExamReport.setQuestionOrder(JSON.toJSONString(examPaperSubmitVM.getQuestionOrder())); // 随机的选项编号 tExamReport.setOptionOrder(JSON.toJSONString(examPaperSubmitVM.getOptionOrder())); // 设置成员名称 tExamReport.setMemberName(relatedParty.getMemberName()); // 模版名称 tExamReport.setReportTemplateId(relatedParty.getReportTemplateId()); // tExamReport // 用户答题的时间 setRemainPartTime(examPaperSubmitVM, tExamReport, tExamReportList); // 更新报告 updateTExamReport(examPaperSubmitVM, tokenId, relatedParty, tExamReport, tExamReportList); // 调用泰国团队 callThaiInteface(examPaperSubmitVM, tokenId, tExamReport, examPaperAnswerInfo); return examPaperAnswerInfo; } private void updateTExamReport(@RequestBody @Valid ExamPaperSubmitVM examPaperSubmitVM, @PathVariable String tokenId, RelatedParty relatedParty, TExamReport tExamReport, List tExamReportList) { // 开始时间没有更新就更新一下 if (Objects.isNull(relatedParty.getStartTime())) { entTestMemberService.updateTestMemberStatus(TestMemberStatusEnum.answering, memberService.queryProdidByToken(tokenId).getMemberId()); } if (tExamReportList.size() > 0) { // 更新本段的答题时间,先把原来的段读出来 examReportService.updateTExamReportByTokenId(tExamReport); } else { examReportService.insertTExamReport(tExamReport); } //更新测试包的状态 if (!Objects.isNull(examPaperSubmitVM.getFinish())) { // 校验每一题是否都答了,没有意义,因为有的人会提交空的答题记录上来 checkCaqQuestionItem(tExamReport); // MAQ 有答完不改状态的情况 TestMemberStatusEnum testMemberStatusEnum = TestMemberStatusEnum.codeOf(examPaperSubmitVM.getFinish().toString()); if (Objects.equals(Integer.parseInt(TestMemberStatusEnum.completed.getCode()), tExamReport.getVerifyStatus())) { testMemberStatusEnum = TestMemberStatusEnum.completed; } entTestMemberService.updateTestMemberStatus(testMemberStatusEnum, tExamReport.getMemberId()); } } private void checkCaqQuestionItem(TExamReport tExamReport) { if (!Objects.equals(tExamReport.getQuestionTemplateId(), "job")) { return; } List questionReportList = JsonUtil.parseArray(tExamReport.getQuestionReport(), QuestionReport.class); for (QuestionReport questionReport : questionReportList) { // questionReport = null; // 测试用 List questionItems = questionReport.getQuestionItems(); } } private void setVerifyStatus(@RequestBody @Valid ExamPaperSubmitVM examPaperSubmitVM, TExamReport tExamReport) { if (Objects.equals(examPaperSubmitVM.getFinish(), 6)) { tExamReport.setVerifyStatus(Integer.parseInt(TestMemberStatusEnum.timeOut.getCode())); } else { // 更新用户的答题状态 tExamReport.setVerifyStatus(examPaperSubmitVM.getVerifyStatus()); } } private void setRemainPartTime(@RequestBody @Valid ExamPaperSubmitVM examPaperSubmitVM, TExamReport tExamReport, List tExamReportList) { if (Objects.isNull(examPaperSubmitVM.getRemainTime())) { return; } Map remainPartTimeMap = null; if (tExamReportList.size() > 0) { remainPartTimeMap = JsonUtil.parseObject(tExamReportList.get(0).getRemainPartTime(), HashMap.class); } else { remainPartTimeMap = new HashMap<>(); } remainPartTimeMap.put(examPaperSubmitVM.getPartOrder(), examPaperSubmitVM.getRemainTime()); tExamReport.setRemainPartTime(JsonUtil.toJSONString(remainPartTimeMap)); } private void callThaiInteface(ExamPaperSubmitVM examPaperSubmitVM, @PathVariable String tokenId, TExamReport tExamReport, ExamPaperAnswerInfo examPaperAnswerInfo) { if (Objects.equals(examPaperSubmitVM.getIsCallTain(), "CallThai")) { SignalObject signalObject = examPaperService.callTaiData(tokenId, tExamReport.getLangType(), examPaperSubmitVM); SubmitReport submitReport = examPaperAnswerInfo.getSubmitReport(); submitReport.setSignalObject(signalObject); } } // 自动发送状态报告需要处理的内容 private void setAutoSendReportStatus(RelatedParty relatedParty, TExamReport tExamReport) { // 0:待发送;1:已发送;2:不发送 if (relatedParty.isAutoSendReport()) { tExamReport.setSendHrStatus(send.getCode()); tExamReport.setSendTesterStatus(send.getCode()); tExamReport.setReportTemplateId(relatedParty.getReportTemplateId()); } else { tExamReport.setSendHrStatus(noSend.getCode()); tExamReport.setSendTesterStatus(noSend.getCode()); } } // 获得QuestionReport列表的Json字符串入t_exam_roport表 private String getQuestionReportsJson(@RequestBody @Valid ExamPaperSubmitVM examPaperSubmitVM) { List titleItems = examPaperSubmitVM.getTitleItems(); List answerItems = examPaperSubmitVM.getAnswerItems(); List questionReportList = null; if (Objects.isNull(titleItems)) { if (Objects.isNull(answerItems)) { return ""; } questionReportList = answerItems.stream().map(answer -> { return answer.getQuestionReport(); }).collect(Collectors.toList()); } else { questionReportList = titleItems.stream().map(title -> { return title.getQuestionReport(); }).collect(Collectors.toList()); } return JsonUtil.toJsonStr(questionReportList); } // 点击开始答题的时候,改成答题中,仅执行一次 @RequestMapping(value = "/updateTestMemberStatus/{tokenId}", method = RequestMethod.POST) public RestResponse updateTestMemberStatus(@PathVariable String tokenId) { entTestMemberService.updateTestMemberStatus(TestMemberStatusEnum.answering, memberService.queryProdidByToken(tokenId).getMemberId()); return RestResponse.ok(); } }