linzhijie
2021-03-11 93af1c6ffb9ae0e894689ad3a37b548e57d54cff
src/main/java/com/ots/project/exam/restcontroller/ExamPaperAnswerController.java
@@ -1,4 +1,6 @@
package com.ots.project.exam.restcontroller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ots.common.enums.LangTypeEnum;
@@ -26,16 +28,20 @@
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
@@ -51,41 +57,53 @@
    @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<String, String> examInfoMap = getDemographicReport(tokenId, user);
            // dataReportList 对应基础数据导出content那个字段
            List<DataReport> dataReportList = getDemographicDataReport(examPaperSubmitVM, tokenId, user);
            // 3、把question取出来
            Map<Long, QuestionObject> 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<String, String> lastContentMap = getLastChioce(examPaperSubmitVM);
            // 8、这次选什么
            Map<String, String> 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);
@@ -98,21 +116,25 @@
            throw e;
        }
    }
    private void setExamPaperSubTERList(@RequestBody @Valid ExamPaperSubmitVM examPaperSubmitVM, TExamReport tExamReportQuery) {
        List<TExamReport> 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<DataReport> dataReportList) {
        List<InterfaceContent> interfaceContentList = examPaperSubmitVM.getInterfaceContentList();
        if (!StringUtils.equals(examPaperSubmitVM.getIsCallTain(), "callTain")) {
            // 要取原来的数据
            setInterfaceContent(examPaperSubmitVM, interfaceContentList);
            return;
        }
        // 模拟调用
        Map<String, String> thaiCalculationMap = new HashMap<>();
        MAQReport maqReport = new MAQReport();
        maqReport.setQuestionnaireTaker(relatedParty.getMemberName());
@@ -129,6 +151,7 @@
        thaiCalculationMap.put("P_Creat02", "80");
        ReportResultData resultData = maqReport.getTemplateParameters(thaiCalculationMap, Brief, LangTypeEnum.English);
        Map<String, String> thaiDataMap = resultData.getTextMap();
        InterfaceContent interfaceContent = new InterfaceContent();
        interfaceContent.setPartOrder(examPaperSubmitVM.getPartOrder());
        interfaceContent.setDataReportList(new ArrayList<>());
@@ -136,10 +159,13 @@
        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<InterfaceContent> interfaceContentList) {
        if (ExamUtil.isListNotEmpty(examPaperSubmitVM.getTExamReportList())) {
            List<InterfaceContent> interfaceContentOldList = null;
@@ -150,7 +176,8 @@
            }
        }
    }
    // 测试包名称、产品名称、测试开始时间、测试完成时间的
    private void setReportInfo(@RequestBody @Valid ExamPaperSubmitVM examPaperSubmitVM, RelatedParty relatedParty, List<DataReport> dataReportList) {
        dataReportList.add(new DataReport("Test Package", relatedParty.getTestName(), "", examPaperSubmitVM.getLangType()));
        dataReportList.add(new DataReport("Test", relatedParty.getProdName(), "", examPaperSubmitVM.getLangType()));
@@ -160,7 +187,8 @@
        dataReportList.add(new DataReport("Begin Time", beginTime, "", examPaperSubmitVM.getLangType()));
        dataReportList.add(new DataReport("End Time", endTime, "", examPaperSubmitVM.getLangType()));
    }
    // 这次选什么
    private Map<String, String> getThisChioce(@RequestBody @Valid ExamPaperSubmitVM examPaperSubmitVM) {
        Map<String, String> choiceMap = new LinkedHashMap<String, String>();
        examPaperSubmitVM.getAnswerItems().stream().forEach(it -> {
@@ -168,7 +196,8 @@
        });
        return choiceMap;
    }
    // 上一次选什么
    private Map<String, String> getLastChioce(@RequestBody @Valid ExamPaperSubmitVM examPaperSubmitVM) {
        Map<String, String> lastContentMap = new LinkedHashMap<String, String>();
        examPaperSubmitVM.getAnswerItems().stream().forEach(it -> {
@@ -176,34 +205,40 @@
        });
        return lastContentMap;
    }
    // 4、形成题目、回答选项
    private void setQuestionAndAnswer(@RequestBody @Valid ExamPaperSubmitVM examPaperSubmitVM, String tokenId, RelatedParty relatedParty, List<DataReport> dataReportList, Map<Long, QuestionObject> questionObjectMap) {
        List<QuestionAnswer> 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);
@@ -213,26 +248,28 @@
            dataReportList.add(new DataReport(questionAnswers.get(i).getPermanentId(), questionAnswers.get(i).getAnswerItem(), UserTypeEnum.SYS_USER.getUserType(), examPaperSubmitVM.getLangType()));
        }
    }
    // 把question取出来
    private Map<Long, QuestionObject> getLongQuestionObjectMap(@RequestBody @Valid ExamPaperSubmitVM examPaperSubmitVM) {
        List<Long> questionIdList = examPaperSubmitVM.getAnswerItems().stream().map(i -> {
            return i.getQuestionId().longValue();
        }).collect(Collectors.toList());
        //把quesitonId取出来
        List<TQuestion> tQuestionList = questionService.selectTQuestionByIds(questionIdList);
        //题目id,把题干,选项之类的内容取出来
        Map<Long, QuestionObject> questionObjectMap = new HashMap<Long, QuestionObject>();
        tQuestionList.stream().forEach(it -> {
            questionObjectMap.put(it.getId(), JSONObject.parseObject(it.getContent(), QuestionObject.class));
        });
        return questionObjectMap;
    }
    //入人口学的键值
    private Map<String, String> getDemographicReport(@PathVariable String tokenId, ExamUser user) {
        Map<String, String> examInfoMap = new LinkedHashMap<String, String>();
        try {
            List<EntDemographyInfo> 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()));
@@ -242,12 +279,13 @@
        }
        return examInfoMap;
    }
    //入人口学的键值
    private List<DataReport> getDemographicDataReport(ExamPaperSubmitVM examPaperSubmitVM, @PathVariable String tokenId, ExamUser user) {
        List<DataReport> dataReportList = new ArrayList<>();
        try {
            List<EntDemographyInfo> entDemographyInfoList = memberService.queryDemographyParamInfo(tokenId);
            //2、获取用户的值
            EntDemographyInfo entDemographyInfo = entDemographyInfoList.get(0);
            user.setId(entDemographyInfo.getMemberId().intValue());
            entDemographyInfoList.stream().forEach(item -> {
@@ -259,10 +297,11 @@
        }
        return dataReportList;
    }
    private void setQuestionReport(@RequestBody @Valid ExamPaperSubmitVM examPaperSubmitVM) {
        List<ExamPaperTitleItemVM> titleItems = examPaperSubmitVM.getTitleItems();
        List<ExamPaperSubmitItemVM> anserItems = examPaperSubmitVM.getAnswerItems();
        // 人格测试题,只有一段的
        if (Objects.isNull(titleItems)) {
            if (Objects.isNull(anserItems)) {
                return;
@@ -272,22 +311,23 @@
                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<DataReport> dataReportList, Map<String, String> lastContentMap, Map<String, String> choiceMap) {
        TExamReport tExamReport = new TExamReport();
        TExamReport tExamReportQuery = new TExamReport();
        tExamReport.setContent(JSON.toJSON(dataReportList).toString());
        tExamReport.setProductId(examPaperSubmitVM.getId().longValue());
        tExamReport.setQuestionReport(getQuestionReportsJson(examPaperSubmitVM));
        tExamReport.setQuestionReport(getQuestionReportsJson(examPaperSubmitVM));// 报告字段
        ExamPaperAnswerInfo examPaperAnswerInfo = examPaperAnswerService.calculateExamPaperAnswer(examPaperSubmitVM, user);
        TExamPaper examPaper = examPaperAnswerInfo.getExamPaper();
        tExamReport.setProductName(examPaper.getName());
@@ -297,99 +337,124 @@
        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<TExamReport> 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<TExamReport> 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<QuestionReport> questionReportList = JsonUtil.parseArray(tExamReport.getQuestionReport(), QuestionReport.class);
        for (QuestionReport questionReport : questionReportList) {
//            questionReport = null; // 测试用
            List<QuestionEditRequestVM> 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<TExamReport> tExamReportList) {
        if (Objects.isNull(examPaperSubmitVM.getRemainTime())) {
            return;
        }
        Map<Integer, Integer> 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);
@@ -397,9 +462,10 @@
            submitReport.setSignalObject(signalObject);
        }
    }
    // 自动发送状态报告需要处理的内容
    private void setAutoSendReportStatus(RelatedParty relatedParty, TExamReport tExamReport) {
        // 0:待发送;1:已发送;2:不发送
        if (relatedParty.isAutoSendReport()) {
            tExamReport.setSendHrStatus(send.getCode());
            tExamReport.setSendTesterStatus(send.getCode());
@@ -409,7 +475,8 @@
            tExamReport.setSendTesterStatus(noSend.getCode());
        }
    }
    // 获得QuestionReport列表的Json字符串入t_exam_roport表
    private String getQuestionReportsJson(@RequestBody @Valid ExamPaperSubmitVM examPaperSubmitVM) {
        List<ExamPaperTitleItemVM> titleItems = examPaperSubmitVM.getTitleItems();
        List<ExamPaperSubmitItemVM> answerItems = examPaperSubmitVM.getAnswerItems();
@@ -428,7 +495,8 @@
        }
        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());