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 { ExamUser user = new ExamUser(); RelatedParty relatedParty = memberService.queryProdidByToken(tokenId); List dataReportList = getDemographicDataReport(examPaperSubmitVM, tokenId, user); Map questionObjectMap = getLongQuestionObjectMap(examPaperSubmitVM); setReportInfo(examPaperSubmitVM, relatedParty, dataReportList); examPaperSubmitVM.setInterfaceContentList(new ArrayList<>()); TExamReport tExamReportQuery = new TExamReport(); tExamReportQuery.setTokenId(tokenId); setExamPaperSubTERList(examPaperSubmitVM, tExamReportQuery); Map lastContentMap = getLastChioce(examPaperSubmitVM); Map choiceMap = getThisChioce(examPaperSubmitVM); setQuestionReport(examPaperSubmitVM); 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())); }); 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; } 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 { questionAnswer.setQuestionItem(item.getItemOrder() + "." + ExamUtil.html2Text(questionObject.getTitleContent())); questionObject.getQuestionItemObjects().stream().forEach(q -> { if (item.getContent().equals(q.getPrefix())) { questionAnswer.setAnswerItem(q.getContent()); } }); questionAnswer.setPermanentId(item.getQuestionReport().getPermanentId()); 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())); } } private Map getLongQuestionObjectMap(@RequestBody @Valid ExamPaperSubmitVM examPaperSubmitVM) { List questionIdList = examPaperSubmitVM.getAnswerItems().stream().map(i -> { return i.getQuestionId().longValue(); }).collect(Collectors.toList()); List tQuestionList = questionService.selectTQuestionByIds(questionIdList); 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); 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); 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 { for (ExamPaperTitleItemVM examPaperTitleItemVM : titleItems) { 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(); tExamReport.setQuestionCount(0); tExamReport.setMemberId(relatedParty.getMemberId().intValue()); tExamReport.setUserId(relatedParty.getUser_id()); tExamReport.setParentId(relatedParty.getUser_parent_id()); tExamReport.setTestId(relatedParty.getTestId()); tExamReport.setFinish(examPaperSubmitVM.getFinish()); setVerifyStatus(examPaperSubmitVM, tExamReport); tExamReport.setQuestionTemplateId(relatedParty.getQuestionTemplateId()); tExamReport.setQuestionOrder(JSON.toJSONString(examPaperSubmitVM.getQuestionOrder())); tExamReport.setOptionOrder(JSON.toJSONString(examPaperSubmitVM.getOptionOrder())); tExamReport.setMemberName(relatedParty.getMemberName()); tExamReport.setReportTemplateId(relatedParty.getReportTemplateId()); 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); 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) { 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) { if (relatedParty.isAutoSendReport()) { tExamReport.setSendHrStatus(send.getCode()); tExamReport.setSendTesterStatus(send.getCode()); tExamReport.setReportTemplateId(relatedParty.getReportTemplateId()); } else { tExamReport.setSendHrStatus(noSend.getCode()); tExamReport.setSendTesterStatus(noSend.getCode()); } } 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(); } }