SyTestMajorService.java 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052
  1. package com.ruoyi.web.service;
  2. import com.fasterxml.jackson.core.JsonProcessingException;
  3. import com.fasterxml.jackson.core.type.TypeReference;
  4. import com.fasterxml.jackson.databind.ObjectMapper;
  5. import com.google.common.collect.Lists;
  6. import com.google.common.collect.Maps;
  7. import com.google.common.collect.Sets;
  8. import com.ruoyi.common.core.domain.AjaxResult;
  9. import com.ruoyi.common.core.domain.entity.SysUser;
  10. import com.ruoyi.common.core.redis.RedisCache;
  11. import com.ruoyi.common.utils.DateUtils;
  12. import com.ruoyi.common.utils.SecurityUtils;
  13. import com.ruoyi.common.utils.StringUtils;
  14. import com.ruoyi.mxjb.domain.MxjbContants;
  15. import com.ruoyi.sy.domain.*;
  16. import com.ruoyi.sy.mapper.*;
  17. import com.ruoyi.web.domain.CategoryDto;
  18. import com.ruoyi.web.domain.PaperDto;
  19. import com.ruoyi.web.domain.QuestionDto;
  20. import com.ruoyi.web.domain.TestMarjorDto;
  21. import org.apache.commons.collections4.CollectionUtils;
  22. import org.apache.commons.lang3.math.NumberUtils;
  23. import org.springframework.stereotype.Service;
  24. import java.util.*;
  25. import java.util.function.Function;
  26. import java.util.stream.Collectors;
  27. @Service
  28. public class SyTestMajorService {
  29. private final SyMajorMapper majorMapper;
  30. private final SyKnowledgeInterstTypeMapper syKnowledgeInterstTypeMapper;
  31. private final SyMajorCategoryMapper syMajorCategoryMapper;
  32. private final SyMajorMajorMapper syMajorMajorMapper;
  33. private final SyOccupationInterstMajorMapper syOccupationInterstMajorMapper;
  34. private final SyOccupationInterstTypeMapper syOccupationInterstTypeMapper;
  35. private final SyPhilosophyViewMajorMapper syPhilosophyViewMajorMapper;
  36. private final SyPhilosophyViewTypeMapper syPhilosophyViewTypeMapper;
  37. private final SyTestAnswersMapper syTestAnswersMapper;
  38. private final SyTestExamineeMapper syTestExamineeMapper;
  39. private final SyTestPaperMapper syTestPaperMapper;
  40. private final SyTestQuestionMapper syTestQuestionMapper;
  41. private final SyTestSelectCategoryMapper syTestSelectCategoryMapper;
  42. private final SyTestSelectMajorMapper syTestSelectMajorMapper;
  43. private final ExamUtilService examUtilService;
  44. private final RedisCache redisCache;
  45. private Set<String> marjorCodeSet;
  46. private ObjectMapper om = new ObjectMapper();
  47. // key1 category, key2 value(RIA...)
  48. // key1 category, key2 value(喜欢, 擅长)
  49. private TypeReference<Map<String, Map<String, Integer>>> keyCatValueTypeReference = new TypeReference<Map<String, Map<String, Integer>>>() {};
  50. private TypeReference<Map<String, Integer>> keyValueTypeReference = new TypeReference<Map<String, Integer>>() {};
  51. public SyTestMajorService(SyMajorMapper majorMapper, SyKnowledgeInterstTypeMapper syKnowledgeInterstTypeMapper, SyMajorCategoryMapper syMajorCategoryMapper,
  52. SyMajorMajorMapper syMajorMajorMapper, SyOccupationInterstMajorMapper syOccupationInterstMajorMapper,
  53. SyOccupationInterstTypeMapper syOccupationInterstTypeMapper, SyPhilosophyViewMajorMapper syPhilosophyViewMajorMapper, SyPhilosophyViewTypeMapper syPhilosophyViewTypeMapper,
  54. SyTestAnswersMapper syTestAnswersMapper, SyTestExamineeMapper syTestExamineeMapper, SyTestPaperMapper syTestPaperMapper, SyTestQuestionMapper syTestQuestionMapper, SyTestSelectCategoryMapper syTestSelectCategoryMapper,
  55. SyTestSelectMajorMapper syTestSelectMajorMapper, ExamUtilService examUtilService, RedisCache redisCache) {
  56. this.majorMapper = majorMapper;
  57. this.syKnowledgeInterstTypeMapper = syKnowledgeInterstTypeMapper;
  58. this.syMajorCategoryMapper = syMajorCategoryMapper;
  59. this.syMajorMajorMapper = syMajorMajorMapper;
  60. this.syOccupationInterstMajorMapper = syOccupationInterstMajorMapper;
  61. this.syOccupationInterstTypeMapper = syOccupationInterstTypeMapper;
  62. this.syPhilosophyViewMajorMapper = syPhilosophyViewMajorMapper;
  63. this.syPhilosophyViewTypeMapper = syPhilosophyViewTypeMapper;
  64. this.syTestAnswersMapper = syTestAnswersMapper;
  65. this.syTestExamineeMapper = syTestExamineeMapper;
  66. this.syTestPaperMapper = syTestPaperMapper;
  67. this.syTestQuestionMapper = syTestQuestionMapper;
  68. this.syTestSelectCategoryMapper = syTestSelectCategoryMapper;
  69. this.syTestSelectMajorMapper = syTestSelectMajorMapper;
  70. this.examUtilService = examUtilService;
  71. this.redisCache = redisCache;
  72. }
  73. public Set<String> loadMarjorCodes() {
  74. if(null == marjorCodeSet) {
  75. // marjorCodeSet = syMajorMajorMapper.selectSyMajorMajorList(new SyMajorMajor()).stream().map( t -> t.getMajorCode()).collect(Collectors.toSet());
  76. marjorCodeSet = majorMapper.selectSyMajorList(new SyMajor()).stream().map( t -> t.getCode()).collect(Collectors.toSet());
  77. }
  78. return marjorCodeSet;
  79. }
  80. public List<SyTestExaminee> getTestListWithStatus(Integer testType, Integer[] states) {
  81. Map cond = Maps.newHashMap();
  82. cond.put("paperId", testType.longValue());
  83. cond.put("customerCode", SecurityUtils.getLoginUser().getUser().getUserId().toString());
  84. cond.put("states", states);
  85. return syTestExamineeMapper.selectSyTestExamineeLatest(cond);
  86. }
  87. public List<SyTestExaminee> getTestList(Integer testType) {
  88. SyTestExaminee cond = new SyTestExaminee();
  89. cond.setPaperId(testType.longValue());
  90. cond.setCustomerCode(SecurityUtils.getLoginUser().getUser().getUserId().toString());
  91. return syTestExamineeMapper.selectSyTestExamineeList(cond);
  92. }
  93. public SyTestExaminee getTestExaminee(Integer testType, Long examineeId) {
  94. SyTestExaminee examinee = syTestExamineeMapper.selectSyTestExamineeById(examineeId);
  95. return examinee;
  96. }
  97. public List<TestMarjorDto.TestMatchStat> toTestMatchStats(SyTestExaminee examinee) {
  98. if (StringUtils.isNotBlank(examinee.getStats())) {
  99. TestMarjorDto.TestStats testStat = null;
  100. try {
  101. testStat = om.readValue(examinee.getStats(), TestMarjorDto.TestStats.class);
  102. return testStat.getMatchStats();
  103. } catch (JsonProcessingException e) {
  104. e.printStackTrace();
  105. }
  106. }
  107. return Lists.newArrayList();
  108. }
  109. public AjaxResult saveUserSelectedCategory(CategoryDto.UserCategory params) {
  110. SysUser sysUser = SecurityUtils.getLoginUser().getUser();
  111. SyTestExaminee examinee = null != params.getExamineeId() ? syTestExamineeMapper.selectSyTestExamineeById(params.getExamineeId()) : getLatestExaminee(params.getTestType(), sysUser.getCode());
  112. SyTestSelectCategory categoryCond = new SyTestSelectCategory();
  113. categoryCond.setExamineeId(examinee.getExamineeId());
  114. List<SyTestSelectCategory> categoryList = syTestSelectCategoryMapper.selectSyTestSelectCategoryList(categoryCond);
  115. Map<String, SyTestSelectCategory> oldCategoryMap = categoryList.stream().collect(Collectors.toMap(SyTestSelectCategory::getMajorCategoryCode, Function.identity()));
  116. SyTestSelectCategory mxjbCategoryCond = new SyTestSelectCategory();
  117. for(String code : Sets.newHashSet(params.getCodes())) {
  118. SyTestSelectCategory oldCategory = oldCategoryMap.remove(code);
  119. if (null != oldCategory) {
  120. mxjbCategoryCond.setId(oldCategory.getId());
  121. mxjbCategoryCond.setState(1L);
  122. syTestSelectCategoryMapper.updateSyTestSelectCategory(mxjbCategoryCond);
  123. } else {
  124. mxjbCategoryCond.setId(null);
  125. mxjbCategoryCond.setState(1L);
  126. mxjbCategoryCond.setExamineeId(examinee.getExamineeId());
  127. mxjbCategoryCond.setCustomerCode(examinee.getCustomerCode());
  128. mxjbCategoryCond.setMajorCategoryCode(code);
  129. syTestSelectCategoryMapper.insertSyTestSelectCategory(mxjbCategoryCond);
  130. }
  131. }
  132. if (oldCategoryMap.size() > 0) {
  133. List<Long> oldIds = oldCategoryMap.values().stream().map(SyTestSelectCategory::getId).collect(Collectors.toList());
  134. syTestSelectCategoryMapper.deleteSyTestSelectCategoryByIds(oldIds.toArray(new Long[oldIds.size()]));
  135. }
  136. return AjaxResult.success();
  137. }
  138. public AjaxResult getUserSelectedMajor() {
  139. SysUser sysUser = SecurityUtils.getLoginUser().getUser();
  140. Map categoryCond = Maps.newHashMap();
  141. categoryCond.put("customerCode", sysUser.getCode());
  142. List<SyMajorCategory> categoryList = syMajorCategoryMapper.selectUserSyMajorMajor(categoryCond);
  143. return AjaxResult.success(categoryList);
  144. }
  145. public AjaxResult saveUserSelectedMajor(CategoryDto.UserMajor param) {
  146. SysUser sysUser = SecurityUtils.getLoginUser().getUser();
  147. SyTestSelectMajor categoryCond = new SyTestSelectMajor();
  148. categoryCond.setCustomerCode(sysUser.getCode());
  149. Map<String, SyTestSelectMajor> oldCategoryMap = Maps.newHashMap();
  150. Set<String> existCodeSet = Sets.newHashSet();
  151. List<Long> deleteIdList = Lists.newArrayList();
  152. for(SyTestSelectMajor major : syTestSelectMajorMapper.selectSyTestSelectMajorList(categoryCond)) {
  153. if (existCodeSet.add(major.getMajorCode())) {
  154. oldCategoryMap.put(major.getMajorCode(), major);
  155. } else {
  156. deleteIdList.add(major.getId());
  157. }
  158. }
  159. if (deleteIdList.size() > 0) {
  160. syTestSelectMajorMapper.deleteSyTestSelectMajorByIds(deleteIdList.toArray(new Long[deleteIdList.size()]));
  161. }
  162. SyTestSelectMajor mxjbCategoryCond = new SyTestSelectMajor();
  163. for(String code : Sets.newHashSet(param.getCodes())) {
  164. SyTestSelectMajor oldCategory = oldCategoryMap.remove(code);
  165. if (null != oldCategory) {
  166. mxjbCategoryCond.setId(oldCategory.getId());
  167. mxjbCategoryCond.setState(1L);
  168. syTestSelectMajorMapper.updateSyTestSelectMajor(mxjbCategoryCond);
  169. } else {
  170. mxjbCategoryCond.setId(null);
  171. mxjbCategoryCond.setState(1L);
  172. mxjbCategoryCond.setCustomerCode(sysUser.getCode());
  173. mxjbCategoryCond.setUserId(sysUser.getUserId());
  174. mxjbCategoryCond.setMajorCode(code);
  175. syTestSelectMajorMapper.insertSyTestSelectMajor(mxjbCategoryCond);
  176. }
  177. }
  178. if (oldCategoryMap.size() > 0) {
  179. List<Long> oldIds = oldCategoryMap.values().stream().map(SyTestSelectMajor::getId).collect(Collectors.toList());
  180. syTestSelectMajorMapper.deleteSyTestSelectMajorByIds(oldIds.toArray(new Long[oldIds.size()]));
  181. }
  182. return AjaxResult.success();
  183. }
  184. public AjaxResult loadAllMajorSubjectTree() {
  185. Map cond = new HashMap();
  186. List<SyMajorCategory> majorCategoryList = syMajorCategoryMapper.selectAllMajorList(cond);;
  187. return AjaxResult.success(buildTree(majorCategoryList));
  188. }
  189. public AjaxResult loadSelectMajorSubjectTree() {
  190. SysUser sysUser = SecurityUtils.getLoginUser().getUser();
  191. SyTestExaminee examineeCond = new SyTestExaminee();
  192. examineeCond.setCustomerCode(sysUser.getCode());
  193. examineeCond.setState(MxjbContants.ExamineeStatusPublish);
  194. List<SyTestExaminee> existList = syTestExamineeMapper.selectSyTestExamineeList(examineeCond);
  195. List<Long> examineeIds = existList.stream().map(t -> t.getExamineeId()).collect(Collectors.toList());
  196. if (CollectionUtils.isEmpty(examineeIds)) {
  197. return AjaxResult.success(examineeIds);
  198. }
  199. Map cond = new HashMap();
  200. cond.put("examineeIds", examineeIds);
  201. List<SyMajorCategory> majorCategoryList = syMajorCategoryMapper.selectSelectedMajorList(cond);;
  202. return AjaxResult.success(buildTree(majorCategoryList));
  203. }
  204. private List<TestMarjorDto.CategoryNode> buildTree(List<SyMajorCategory> majorCategoryList) {
  205. List<TestMarjorDto.CategoryNode> categoryNodeList = Lists.newArrayList();
  206. if (CollectionUtils.isEmpty(majorCategoryList)) {
  207. return categoryNodeList;
  208. }
  209. Map<String, TestMarjorDto.CategoryNode> categoryNodeMap = Maps.newHashMap();
  210. for(SyMajorCategory mc : majorCategoryList) {
  211. TestMarjorDto.CategoryNode n = categoryNodeMap.get(mc.getMajorSubjectCode());
  212. if (null == n) {
  213. n = new TestMarjorDto.CategoryNode(mc.getMajorSubjectCode(), mc.getMajorSubjectName(), Lists.newArrayList());
  214. categoryNodeMap.put(mc.getMajorSubjectCode(), n);
  215. categoryNodeList.add(n);
  216. }
  217. String snKey = mc.getMajorSubjectCode() + "_" + mc.getMajorCategoryCode();
  218. TestMarjorDto.CategoryNode sn = categoryNodeMap.get(snKey);
  219. if (null == sn) {
  220. sn = new TestMarjorDto.CategoryNode(mc.getMajorCategoryCode(), mc.getMajorCategoryName(), Lists.newArrayList());
  221. categoryNodeMap.put(snKey, sn);
  222. n.getNodes().add(sn);
  223. }
  224. TestMarjorDto.CategoryNode ssn = new TestMarjorDto.CategoryNode(mc.getMajorCode(), mc.getMajorName(), null);
  225. sn.getNodes().add(ssn);
  226. }
  227. return categoryNodeList.size() != 1 ? categoryNodeList : categoryNodeList.get(0).getNodes();
  228. }
  229. private List<SyMajorCategory> findMatchCategoryList(Integer testType, String matchCode) {
  230. if (StringUtils.isBlank(matchCode)) {
  231. return Lists.newArrayList();
  232. }
  233. Map cond = new HashMap();
  234. List<SyMajorCategory> majorCategoryList = null;
  235. switch (testType) {
  236. case 1:
  237. cond.put("matchCodes", StringUtils.split(matchCode, ","));
  238. majorCategoryList = syMajorCategoryMapper.selectSyMajorCategoryListByView(cond);
  239. break;
  240. case 2:
  241. cond.put("matchCodes", Lists.newArrayList(matchCode));
  242. majorCategoryList = syMajorCategoryMapper.selectSyMajorCategoryListByOccupation(cond);
  243. break;
  244. case 3:
  245. cond.put("matchCodes", StringUtils.split(matchCode, ","));
  246. majorCategoryList = syMajorCategoryMapper.selectSyMajorCategoryListByKnowledge(cond);
  247. break;
  248. default:
  249. majorCategoryList = Lists.newArrayList();
  250. }
  251. return majorCategoryList;
  252. }
  253. public AjaxResult loadMatchCategoryTree(Integer testType, String matchCode, Integer level) {
  254. List<SyMajorCategory> majorCategoryList = findMatchCategoryList(testType, matchCode);
  255. Map<String, TestMarjorDto.CategoryNode> categoryNodeMap = Maps.newHashMap();
  256. List<TestMarjorDto.CategoryNode> categoryNodeList = Lists.newArrayList();
  257. for(SyMajorCategory mc : majorCategoryList) {
  258. TestMarjorDto.CategoryNode n = categoryNodeMap.get(mc.getMajorSubjectCode());
  259. if (null == n) {
  260. n = new TestMarjorDto.CategoryNode(mc.getMajorSubjectCode(), mc.getMajorSubjectName(), Lists.newArrayList());
  261. categoryNodeMap.put(mc.getMajorSubjectCode(), n);
  262. categoryNodeList.add(n);
  263. }
  264. TestMarjorDto.CategoryNode sn = new TestMarjorDto.CategoryNode(mc.getMajorCategoryCode(), mc.getMajorCategoryName(), null);
  265. n.getNodes().add(sn);
  266. }
  267. return AjaxResult.success(categoryNodeList);
  268. }
  269. public AjaxResult getAllTestMajorResult() throws JsonProcessingException {
  270. Map allResult = new HashMap();
  271. SysUser sysUser = SecurityUtils.getLoginUser().getUser();
  272. Map categoryCond = Maps.newHashMap();
  273. categoryCond.put("customerCode", sysUser.getCode());
  274. List<SyMajorCategory> categoryList = syMajorCategoryMapper.selectUserSyMajorMajor(categoryCond);
  275. allResult.put("selectedList", categoryList);
  276. allResult.put("philosophy", getTestResult(getLatestExaminee(1, sysUser.getCode())));
  277. allResult.put("occupation", getTestResult(getLatestExaminee(2, sysUser.getCode())));
  278. allResult.put("knowledge", getTestResult(getLatestExaminee(3, sysUser.getCode())));
  279. return AjaxResult.success(allResult);
  280. }
  281. public AjaxResult loadTestMajorResult(Integer testType, String customerCode) throws JsonProcessingException {
  282. SyTestExaminee examinee = getLatestExaminee(testType, customerCode);
  283. return loadTestMajorResult(examinee);
  284. }
  285. public AjaxResult loadTestMajorResult(Long examineeId) throws JsonProcessingException {
  286. SyTestExaminee examinee = syTestExamineeMapper.selectSyTestExamineeById(examineeId);
  287. return loadTestMajorResult(examinee);
  288. }
  289. public AjaxResult loadTestMajorResult(SyTestExaminee examinee) throws JsonProcessingException {
  290. TestMarjorDto.TestResultDto dto;
  291. return null == (dto = getTestResult(examinee)) ? AjaxResult.success() : AjaxResult.success(dto);
  292. }
  293. private TestMarjorDto.TestResultDto getTestResult(SyTestExaminee examinee) throws JsonProcessingException {
  294. if (null == examinee) {
  295. return null;
  296. }
  297. boolean testCompleted = examinee.getState() >= MxjbContants.ExamineeStatusPublish;
  298. SyTestPaper paper = syTestPaperMapper.selectSyTestPaperById(examinee.getPaperId());
  299. TestMarjorDto.TestStats testStat = null;
  300. Map<String, TestMarjorDto.TestMatchStat> categoryCountMap = Maps.newHashMap();
  301. Map<String, Map<String, Integer>> catKeyValueMap = null;
  302. String matchCode = null;
  303. boolean noMatch = false;
  304. if (StringUtils.isNotBlank(examinee.getStats())) {
  305. testStat = om.readValue(examinee.getStats(), TestMarjorDto.TestStats.class);
  306. if (CollectionUtils.isNotEmpty(testStat.getMatchStats())) {
  307. for (TestMarjorDto.TestMatchStat ts : testStat.getMatchStats()) {
  308. categoryCountMap.put(ts.getCode(), ts);
  309. }
  310. }
  311. catKeyValueMap = testStat.getCatKeyValueMap();
  312. matchCode = testStat.getMatchCode();
  313. noMatch = testStat.isNoMatch();
  314. }
  315. boolean selectCompleted = noMatch || isSelectCompleted(examinee.getExamineeId());
  316. Map categoryCond = Maps.newHashMap();
  317. categoryCond.put("examineeId", examinee.getExamineeId());
  318. List<SyMajorCategory> selectedList = syMajorCategoryMapper.selectUserSyMajorCategory(categoryCond);
  319. switch (paper.getCategory()) {
  320. case 1:
  321. Map<String, SyPhilosophyViewType> viewTypeMap = Maps.newHashMap();
  322. List<SyPhilosophyViewType> viewTypeList = syPhilosophyViewTypeMapper.selectSyPhilosophyViewTypeList(new SyPhilosophyViewType());
  323. for (SyPhilosophyViewType type : viewTypeList) {
  324. type.setViewMajorList(Lists.newArrayList());
  325. viewTypeMap.put(type.getCode(), type);
  326. TestMarjorDto.TestMatchStat ts = categoryCountMap.get(type.getCode());
  327. if (null != ts) {
  328. Integer total = ts.getCount();
  329. type.setTotal(null != total ? total : 0);
  330. type.setMatched(ts.isMatched());
  331. } else {
  332. type.setTotal(0);
  333. type.setMatched(false);
  334. }
  335. if (null != catKeyValueMap) {
  336. type.setStats(catKeyValueMap.get(type.getCode()));
  337. }
  338. }
  339. for (SyPhilosophyViewMajor viewMajor : syPhilosophyViewMajorMapper.selectSyPhilosophyViewMajorList(new SyPhilosophyViewMajor())) {
  340. viewTypeMap.get(viewMajor.getMatchCode()).getViewMajorList().add(viewMajor);
  341. }
  342. Collections.sort(viewTypeList, new Comparator<SyPhilosophyViewType>() {
  343. @Override
  344. public int compare(SyPhilosophyViewType o1, SyPhilosophyViewType o2) {
  345. int iRet = -o1.getTotal().compareTo(o2.getTotal());
  346. if (0 == iRet) {
  347. iRet = o1.getCode().compareTo(o2.getCode());
  348. }
  349. return iRet;
  350. }
  351. });
  352. return new TestMarjorDto.TestResultDto(examinee.getExamineeId(), selectedList, viewTypeList, matchCode, testCompleted, selectCompleted, noMatch);
  353. case 2:
  354. List<SyOccupationInterstType> occupationTypeList = syOccupationInterstTypeMapper.selectSyOccupationInterstTypeList(new SyOccupationInterstType());
  355. for (SyOccupationInterstType type : occupationTypeList) {
  356. TestMarjorDto.TestMatchStat ts = categoryCountMap.get(type.getCode());
  357. if (null != ts) {
  358. Integer total = ts.getCount();
  359. type.setTotal(null != total ? total : 0);
  360. type.setMatched(ts.isMatched());
  361. } else {
  362. type.setTotal(0);
  363. type.setMatched(false);
  364. }
  365. if (null != catKeyValueMap) {
  366. type.setStats(catKeyValueMap.get(type.getCode()));
  367. }
  368. }
  369. Collections.sort(occupationTypeList, new Comparator<SyOccupationInterstType>() {
  370. @Override
  371. public int compare(SyOccupationInterstType o1, SyOccupationInterstType o2) {
  372. int iRet = -o1.getTotal().compareTo(o2.getTotal());
  373. if (0 == iRet) {
  374. iRet = o1.getCode().compareTo(o2.getCode());
  375. }
  376. return iRet;
  377. }
  378. });
  379. return new TestMarjorDto.TestResultDto(examinee.getExamineeId(), selectedList, occupationTypeList, matchCode, testCompleted, selectCompleted, noMatch);
  380. case 3:
  381. List<SyKnowledgeInterstType> knowledgeTypeList = syKnowledgeInterstTypeMapper.selectSyKnowledgeInterstTypeList(new SyKnowledgeInterstType());
  382. for (SyKnowledgeInterstType type : knowledgeTypeList) {
  383. TestMarjorDto.TestMatchStat ts = categoryCountMap.get(type.getId().toString());
  384. if (null != ts) {
  385. Integer total = ts.getCount();
  386. type.setTotal(null != total ? total : 0);
  387. type.setMatched(ts.isMatched());
  388. } else {
  389. type.setTotal(0);
  390. type.setMatched(false);
  391. }
  392. }
  393. Collections.sort(knowledgeTypeList, new Comparator<SyKnowledgeInterstType>() {
  394. @Override
  395. public int compare(SyKnowledgeInterstType o1, SyKnowledgeInterstType o2) {
  396. int iRet = -o1.getTotal().compareTo(o2.getTotal());
  397. if (0 == iRet) {
  398. iRet = o1.getCode().compareTo(o2.getCode());
  399. }
  400. return iRet;
  401. }
  402. });
  403. return new TestMarjorDto.TestResultDto(examinee.getExamineeId(), selectedList, knowledgeTypeList, matchCode, testCompleted, selectCompleted, noMatch);
  404. }
  405. return null;
  406. }
  407. private TestMarjorDto.TestStats getStats(Integer testType, Long paperId, List<QuestionDto> questionlist) {
  408. SyTestQuestion cond = new SyTestQuestion();
  409. cond.setPaperId(paperId);
  410. Map<Long, SyTestQuestion> stdQuestionMap = syTestQuestionMapper.selectSyTestQuestionList(cond).stream().collect(Collectors.toMap(SyTestQuestion::getQuestionId, Function.identity()));
  411. TestMarjorDto.TestStats testStats = new TestMarjorDto.TestStats();
  412. // key1=category, key2=value, value=score
  413. Map<String, Map<String, Integer>> catKeyValueMap = null;
  414. List<TestMarjorDto.TestMatchStat> matchStats = null;
  415. String matchedCode = null;
  416. boolean noMatch = false;
  417. switch (testType) {
  418. case 1:
  419. // k1=value(A/B/D/F), k2=questionCategory, value=按answer求和, 复选总分(限制总分不重复, 3), 复选评价(限制总分不重复, 6)
  420. catKeyValueMap = Maps.newHashMap();
  421. matchStats = Lists.newArrayList();
  422. for (QuestionDto ques : questionlist) {
  423. String normalAnswer = MxjbContants.getNormalAnswer(ques.getAnswer(), "");
  424. if (StringUtils.isBlank(normalAnswer)) {
  425. continue;
  426. }
  427. SyTestQuestion stdQuestion = stdQuestionMap.get(ques.getQuestionId());
  428. if(StringUtils.isNotBlank(normalAnswer)) {
  429. String[] answerScores = normalAnswer.split(",");
  430. for (int i = 0; i < answerScores.length; i++) {
  431. Integer cnt = Integer.parseInt(answerScores[i]);
  432. String newValue = getAnswerValue(stdQuestion, i);
  433. if (StringUtils.isNotBlank(newValue)) {
  434. Map<String, Integer> valueMap = catKeyValueMap.get(newValue);
  435. if (null == valueMap) {
  436. valueMap = Maps.newHashMap();
  437. catKeyValueMap.put(newValue, valueMap);
  438. }
  439. Integer oldCount = valueMap.get(stdQuestion.getQuesitonCateogry());
  440. valueMap.put(stdQuestion.getQuesitonCateogry(), null != oldCount ? oldCount + cnt : cnt);
  441. }
  442. }
  443. }
  444. }
  445. for (String category : catKeyValueMap.keySet()) {
  446. TestMarjorDto.TestMatchStat testMatchStat = new TestMarjorDto.TestMatchStat();
  447. testMatchStat.setCode(category);
  448. Integer sum = 0;
  449. for (Integer v : catKeyValueMap.get(category).values()) {
  450. sum += v;
  451. }
  452. testMatchStat.setCount(sum);
  453. testMatchStat.setMatched(false);
  454. matchStats.add(testMatchStat);
  455. }
  456. Collections.sort(matchStats, new Comparator<TestMarjorDto.TestMatchStat>() {
  457. @Override
  458. public int compare(TestMarjorDto.TestMatchStat o1, TestMarjorDto.TestMatchStat o2) {
  459. return -o1.getCount().compareTo(o2.getCount());
  460. }
  461. });
  462. matchedCode = getMatchedCodeByTop(matchStats, 3);
  463. break;
  464. case 2:
  465. // k1=questionCategory, k2=value(Like/Familiar), value=对应个数
  466. catKeyValueMap = Maps.newHashMap();
  467. matchStats = Lists.newArrayList();
  468. String newValue;
  469. for (QuestionDto ques : questionlist) {
  470. SyTestQuestion stdQuestion = stdQuestionMap.get(ques.getQuestionId());
  471. String normalAnswer = MxjbContants.getNormalAnswer(ques.getAnswer(), "");
  472. if (StringUtils.isNotBlank(normalAnswer) && StringUtils.isNotBlank((newValue = getAnswerValue(stdQuestion, normalAnswer.charAt(0) - 'A')))) {;
  473. Map<String, Integer> valueMap = catKeyValueMap.get(stdQuestion.getQuesitonCateogry());
  474. if (null == valueMap) {
  475. valueMap = Maps.newHashMap();
  476. catKeyValueMap.put(stdQuestion.getQuesitonCateogry(), valueMap);
  477. }
  478. String valueName = "Like".equals(newValue) ? "喜欢" : "擅长"; // 最多7个
  479. Integer oldCount = valueMap.get(valueName);
  480. valueMap.put(valueName, null != oldCount ? Math.min(oldCount + 1, 7) : 1);
  481. }
  482. }
  483. for (String category : catKeyValueMap.keySet()) {
  484. TestMarjorDto.TestMatchStat testMatchStat = new TestMarjorDto.TestMatchStat();
  485. testMatchStat.setCode(category);
  486. Integer sum = 0;
  487. for (Integer v : catKeyValueMap.get(category).values()) {
  488. sum += v;
  489. }
  490. testMatchStat.setCount(sum);
  491. testMatchStat.setMatched(false);
  492. matchStats.add(testMatchStat);
  493. }
  494. Collections.sort(matchStats, new Comparator<TestMarjorDto.TestMatchStat>() {
  495. @Override
  496. public int compare(TestMarjorDto.TestMatchStat o1, TestMarjorDto.TestMatchStat o2) {
  497. return -o1.getCount().compareTo(o2.getCount());
  498. }
  499. });
  500. List<String> matchCodeList = Lists.newArrayList();
  501. noMatch = getMatchedCode(matchStats, matchCodeList);
  502. matchedCode = StringUtils.join(matchCodeList, ",");
  503. break;
  504. case 3:
  505. // id 对应 questionCategory, value 对应分值
  506. Map<String, Integer> keyValueMap = Maps.newHashMap();
  507. for (QuestionDto ques : questionlist) {
  508. SyTestQuestion stdQuestion = stdQuestionMap.get(ques.getQuestionId());
  509. Integer oldValue = keyValueMap.get(stdQuestion.getQuesitonCateogry());
  510. String normalAnswer = MxjbContants.getNormalAnswer(ques.getAnswer(), "");
  511. if (StringUtils.isNotBlank(normalAnswer)) {
  512. keyValueMap.put(stdQuestion.getQuesitonCateogry(), Integer.parseInt(getAnswerValue(stdQuestion, normalAnswer.charAt(0) - 'A')) + (null != oldValue ? oldValue : 0));
  513. }
  514. }
  515. matchStats = Lists.newArrayList();
  516. for (String category : keyValueMap.keySet()) {
  517. TestMarjorDto.TestMatchStat testMatchStat = new TestMarjorDto.TestMatchStat();
  518. testMatchStat.setCode(category);
  519. testMatchStat.setCount(keyValueMap.get(category));
  520. testMatchStat.setMatched(false);
  521. matchStats.add(testMatchStat);
  522. }
  523. Collections.sort(matchStats, new Comparator<TestMarjorDto.TestMatchStat>() {
  524. @Override
  525. public int compare(TestMarjorDto.TestMatchStat o1, TestMarjorDto.TestMatchStat o2) {
  526. return -o1.getCount().compareTo(o2.getCount());
  527. }
  528. });
  529. matchedCode = getMatchedCodeByTop(matchStats, 3);
  530. break;
  531. }
  532. testStats.setCatKeyValueMap(catKeyValueMap);
  533. testStats.setMatchStats(matchStats);
  534. testStats.setMatchCode(matchedCode);
  535. testStats.setNoMatch(StringUtils.isNotBlank(matchedCode) && (noMatch || CollectionUtils.isEmpty(findMatchCategoryList(testType, matchedCode))));
  536. return testStats;
  537. }
  538. /**
  539. * 根据用户选择查询对应的值
  540. * @param stdQuestion
  541. * @param optionIdx
  542. * @return
  543. */
  544. private String getAnswerValue(SyTestQuestion stdQuestion, int optionIdx) {
  545. switch(optionIdx) {
  546. case 0:
  547. return stdQuestion.getValueA();
  548. case 1:
  549. return stdQuestion.getValueB();
  550. case 2:
  551. return stdQuestion.getValueC();
  552. case 3:
  553. return stdQuestion.getValueD();
  554. case 4:
  555. return stdQuestion.getValueE();
  556. }
  557. return "";
  558. }
  559. public List<SyTestQuestion> getPaperQuestions(Long paperId) {
  560. SyTestQuestion cond = new SyTestQuestion();
  561. cond.setPaperId(paperId);
  562. List<SyTestQuestion> questionsList = syTestQuestionMapper.selectSyTestQuestionList(cond);
  563. return questionsList;
  564. }
  565. public AjaxResult saveTestPaper(PaperDto paperDto) {
  566. Long examineeId = paperDto.getExamineeId();
  567. SyTestExaminee examinee = syTestExamineeMapper.selectSyTestExamineeById(examineeId);
  568. if (examinee.getState() >= MxjbContants.ExamineeStatusReview) {
  569. return AjaxResult.error(examineeId + " 已经在批阅的不可保存答案");
  570. }
  571. String key = examUtilService.getExamineeAnswersKey(MxjbContants.ExamineeTypeTestKey, paperDto.getExamineeId());
  572. if (CollectionUtils.isEmpty(paperDto.getQuestions())) {
  573. Map<String, QuestionDto> questionDtoMap = redisCache.getCacheMap(key);
  574. questionDtoMap.remove("0");
  575. paperDto.setQuestions(Lists.newArrayList(questionDtoMap.values()));
  576. }
  577. if (CollectionUtils.isEmpty(paperDto.getQuestions()) || getPaperQuestions(examinee.getPaperId()).size() != paperDto.getQuestions().size()) {
  578. return AjaxResult.error(examineeId + "未答完题时不能保存");
  579. }
  580. SyTestPaper paper = syTestPaperMapper.selectSyTestPaperById(examinee.getPaperId());
  581. updateTestAnswers(paperDto.getQuestions(), paperDto.getExamineeId());
  582. SyTestExaminee examineeCond = new SyTestExaminee();
  583. examineeCond.setExamineeId(paperDto.getExamineeId());
  584. examineeCond.setState(MxjbContants.ExamineeStatusPublish);
  585. examineeCond.setEndTime(new Date());
  586. TestMarjorDto.TestStats testStats = examinee.getPaperId().equals(4L) ? getMentalHealth(paperDto.getQuestions()) : getStats(paper.getCategory(), paper.getPaperId(), paperDto.getQuestions());
  587. try {
  588. examineeCond.setStats(om.writeValueAsString(testStats));
  589. } catch (JsonProcessingException e) {
  590. return AjaxResult.error(examineeId + " 已经在批阅的不可保存答案");
  591. }
  592. syTestExamineeMapper.updateSyTestExaminee(examineeCond);
  593. redisCache.deleteObject(key);
  594. return AjaxResult.success("成功");
  595. }
  596. public AjaxResult updateExamineeStats(Long examineeId) {
  597. SyTestExaminee examinee = syTestExamineeMapper.selectSyTestExamineeById(examineeId);
  598. SyTestPaper paper = syTestPaperMapper.selectSyTestPaperById(examinee.getPaperId());
  599. SyTestAnswers answerCond = new SyTestAnswers();
  600. answerCond.setExamineeId(examineeId);
  601. List<QuestionDto> questionlist = Lists.newArrayList();
  602. List<SyTestAnswers> answersList = syTestAnswersMapper.selectSyTestAnswersList(answerCond);
  603. for(SyTestAnswers a : answersList) {
  604. QuestionDto dto = new QuestionDto();
  605. dto.setQuestionId(a.getQuestionId());
  606. dto.setAnswer(a.getAnswer());
  607. questionlist.add(dto);
  608. }
  609. SyTestExaminee examineeCond = new SyTestExaminee();
  610. examineeCond.setExamineeId(examinee.getExamineeId());
  611. TestMarjorDto.TestStats testStats = examinee.getPaperId().equals(4L) ? getMentalHealth(questionlist) : getStats(paper.getCategory(), paper.getPaperId(), questionlist);
  612. try {
  613. examineeCond.setStats(om.writeValueAsString(testStats));
  614. } catch (JsonProcessingException e) {
  615. return AjaxResult.error(examineeId + " 统计序列化异常");
  616. }
  617. syTestExamineeMapper.updateSyTestExaminee(examineeCond);
  618. return AjaxResult.success();
  619. }
  620. private TestMarjorDto.TestStats getMentalHealth(List<QuestionDto> questionlist) {
  621. SyTestQuestion cond = new SyTestQuestion();
  622. cond.setPaperId(4L);
  623. Map<Long, SyTestQuestion> stdQuestionMap = Maps.newHashMap();
  624. Map<String, Integer> categoryValueMap = Maps.newLinkedHashMap();
  625. for (SyTestQuestion q : syTestQuestionMapper.selectSyTestQuestionList(cond)) {
  626. stdQuestionMap.put(q.getQuestionId(), q);
  627. categoryValueMap.putIfAbsent(q.getQuesitonCateogry(), 0);
  628. }
  629. for (QuestionDto ques : questionlist) {
  630. String normalAnswer = MxjbContants.getNormalAnswer(ques.getAnswer(), "");
  631. if (StringUtils.isBlank(normalAnswer)) {
  632. continue;
  633. }
  634. SyTestQuestion stdQuestion = stdQuestionMap.get(ques.getQuestionId());
  635. if(StringUtils.isNotBlank(normalAnswer)) {
  636. String value = normalAnswer.equals("A") ? stdQuestion.getValueA() : stdQuestion.getValueB();
  637. Integer v = NumberUtils.toInt(value, 0);
  638. String c = stdQuestion.getQuesitonCateogry();
  639. Integer ov = categoryValueMap.get(c);
  640. categoryValueMap.put(c, null != ov ? ov + v : v);
  641. }
  642. }
  643. TestMarjorDto.TestStats testStats = new TestMarjorDto.TestStats();
  644. List<TestMarjorDto.TestMatchStat> matchStats = Lists.newArrayList();
  645. for (String name : categoryValueMap.keySet()) {
  646. matchStats.add(new TestMarjorDto.TestMatchStat(name, categoryValueMap.get(name), false));
  647. }
  648. testStats.setMatchStats(matchStats);
  649. return testStats;
  650. }
  651. private String getMatchedCodeByTop(List<TestMarjorDto.TestMatchStat> matchStats, Integer top) {
  652. if (CollectionUtils.isEmpty(matchStats)) {
  653. return "";
  654. }
  655. List<String> matchCodeList = Lists.newArrayList();
  656. for (TestMarjorDto.TestMatchStat ts : matchStats) {
  657. matchCodeList.add(ts.getCode());
  658. ts.setMatched(true);
  659. if (matchCodeList.size() == top) {
  660. break;
  661. }
  662. }
  663. return StringUtils.join(matchCodeList, ",");
  664. }
  665. private boolean getMatchedCode(List<TestMarjorDto.TestMatchStat> matchStats, List<String> codeList) {
  666. if (CollectionUtils.isEmpty(matchStats)) {
  667. return true;
  668. }
  669. Set<String> validCodeSet = syOccupationInterstMajorMapper.selectSyOccupationInterstMajorList(new SyOccupationInterstMajor()).stream().map(t -> t.getMatchCode()).collect(Collectors.toSet());
  670. TestMarjorDto.TestMatchStat ts1 = matchStats.get(0);
  671. TestMarjorDto.TestMatchStat ts2 = null ;
  672. String code;
  673. if (matchStats.size() == 1 || (ts1.getCount() - (ts2 = matchStats.get(1)).getCount()) > 4) { // 只有一个或可忽略第二个时
  674. ts1.setMatched(true);
  675. code = ts1.getCode() + "**";
  676. } else {
  677. TestMarjorDto.TestMatchStat ts3 = null ;
  678. String code2;
  679. if (matchStats.size() == 2 || (ts2.getCount() - (ts3 = matchStats.get(2)).getCount()) > 4) { // 只有两个或忽略第3个时
  680. ts1.setMatched(true);
  681. ts2.setMatched(true);
  682. code = ts1.getCode() + ts2.getCode() + "*";
  683. if (!validCodeSet.contains(code)) {
  684. if (ts1.getCount() == ts2.getCount() && validCodeSet.contains((code2 = ts2.getCode() + ts1.getCode() + "*"))) {
  685. matchStats.add(0, matchStats.remove(1)); // 交换 12位置
  686. code = code2;
  687. } else if (validCodeSet.contains((code2 = ts1.getCode() + "**"))) { // 降级
  688. code = code2;
  689. } else if (validCodeSet.contains((code2 = ts2.getCode() + "**"))) {
  690. matchStats.add(0, matchStats.remove(1));
  691. code = code2;
  692. }
  693. }
  694. } else {
  695. code = ts1.getCode() + ts2.getCode() + ts3.getCode();
  696. List<TestMarjorDto.TestMatchStat> needMatchStats = Lists.newArrayList();
  697. List<TestMarjorDto.TestMatchStat> remainMatchStats;
  698. if (ts2.getCount() > ts3.getCount()) { // ts2 固定
  699. remainMatchStats = splitStats(matchStats, 2, ts3.getCount(), needMatchStats);
  700. if (null != (code2 = matchStatsTry(validCodeSet, matchStats, needMatchStats))) {
  701. code = code2;
  702. } else {
  703. matchStats.addAll(needMatchStats);
  704. if (validCodeSet.contains((code2 = ts1.getCode() + ts2.getCode() + "*"))) {
  705. code = code2;
  706. } else if (validCodeSet.contains((code2 = ts1.getCode() + "**"))) {
  707. code = code2;
  708. }
  709. }
  710. matchStats.addAll(remainMatchStats);
  711. } else { // ts2 == ts3
  712. remainMatchStats = splitStats(matchStats, 1, ts2.getCount(), needMatchStats);
  713. if (ts1.getCount() == ts2.getCount()) { // 3 of needMatchStats
  714. needMatchStats.add(0, matchStats.remove(0));
  715. } // t1 + 2 of needMatchStats
  716. if (null != (code2 = matchStatsTry(validCodeSet, matchStats, needMatchStats))) {
  717. code = code2;
  718. } else {
  719. if (matchStats.size() == 0) {
  720. code = needMatchStats.get(0).getCode() + "**";
  721. } else {
  722. code = matchStats.get(0).getCode() + "**";
  723. }
  724. matchStats.addAll(needMatchStats);
  725. }
  726. matchStats.addAll(remainMatchStats);
  727. }
  728. }
  729. }
  730. codeList.add(code);
  731. return !validCodeSet.contains(code);
  732. }
  733. private String matchStatsTry(Set<String> validCodeSet, List<TestMarjorDto.TestMatchStat> matchStats, List<TestMarjorDto.TestMatchStat> needMatchStats) {
  734. String code = null;
  735. if (matchStats.size() == 0) { // 任意组合3个
  736. List<List<TestMarjorDto.TestMatchStat>> permutaionList = Lists.newArrayList();
  737. buildPermutation(needMatchStats.toArray(new TestMarjorDto.TestMatchStat[needMatchStats.size()]), 3, Lists.newArrayList(), Sets.newHashSet(), permutaionList);
  738. for (List<TestMarjorDto.TestMatchStat> tsList : permutaionList) {
  739. TestMarjorDto.TestMatchStat ts1 = tsList.get(0);
  740. TestMarjorDto.TestMatchStat ts2 = tsList.get(1);
  741. TestMarjorDto.TestMatchStat ts3 = tsList.get(2);
  742. if (validCodeSet.contains((code = ts1.getCode() + ts2.getCode() + ts3.getCode()))) {
  743. matchStats.addAll(tsList);
  744. return combineMatch(code, ts1, ts2, ts3);
  745. }
  746. }
  747. } else if(matchStats.size() == 1) { // 任意组合2个
  748. TestMarjorDto.TestMatchStat ts1 = matchStats.get(0);
  749. List<List<TestMarjorDto.TestMatchStat>> permutaionList = Lists.newArrayList();
  750. buildPermutation(needMatchStats.toArray(new TestMarjorDto.TestMatchStat[needMatchStats.size()]), 2, Lists.newArrayList(), Sets.newHashSet(), permutaionList);
  751. for (List<TestMarjorDto.TestMatchStat> matchStatList : permutaionList) {
  752. TestMarjorDto.TestMatchStat ts2 = matchStatList.get(0);
  753. TestMarjorDto.TestMatchStat ts3 = matchStatList.get(1);
  754. if (validCodeSet.contains((code = ts1.getCode() + ts2.getCode() + ts3.getCode()))) {
  755. matchStats.addAll(matchStatList);
  756. return combineMatch(code, ts1, ts2, ts3);
  757. }
  758. }
  759. } else { // 前两个可能交换
  760. TestMarjorDto.TestMatchStat ts1 = matchStats.get(0);
  761. TestMarjorDto.TestMatchStat ts2 = matchStats.get(1);
  762. for (TestMarjorDto.TestMatchStat ts3 : needMatchStats) {
  763. if (validCodeSet.contains((code = ts1.getCode() + ts2.getCode() + ts3.getCode()))) {
  764. matchStats.add(ts3);
  765. return combineMatch(code, ts1, ts2, ts3);
  766. } else if (ts1.getCount() == ts2.getCount()) {
  767. if (validCodeSet.contains((code = ts1.getCode() + ts2.getCode() + ts3.getCode()))) {
  768. matchStats.add(0, matchStats.remove(1)); // 交换 12位置
  769. matchStats.add(ts3);
  770. return combineMatch(code, ts1, ts2, ts3);
  771. }
  772. }
  773. }
  774. }
  775. return null;
  776. }
  777. private String combineMatch(String code, TestMarjorDto.TestMatchStat ts1, TestMarjorDto.TestMatchStat ts2, TestMarjorDto.TestMatchStat ts3) {
  778. ts1.setMatched(true);
  779. ts2.setMatched(true);
  780. ts3.setMatched(true);
  781. return code;
  782. }
  783. private void buildPermutation(TestMarjorDto.TestMatchStat[] matchStats, Integer matchCount, List<TestMarjorDto.TestMatchStat> baseStats,
  784. Set<Integer> usedIndexSet, List<List<TestMarjorDto.TestMatchStat>> permutaionList) {
  785. boolean lastCount = matchCount == baseStats.size() + 1;
  786. for (int i = 0; i < matchStats.length; i++) {
  787. if (usedIndexSet.add(i)) {
  788. List<TestMarjorDto.TestMatchStat> foundList = Lists.newArrayList(baseStats);
  789. foundList.add(matchStats[i]);
  790. if (lastCount) {
  791. permutaionList.add(foundList);
  792. } else {
  793. buildPermutation(matchStats, matchCount, foundList, usedIndexSet, permutaionList);
  794. }
  795. usedIndexSet.remove(i);
  796. }
  797. }
  798. }
  799. /**
  800. * 从除列中删除指定位置后的,并根据count分成两组
  801. * @param matchStats
  802. * @param index
  803. * @param matchCount
  804. * @param needMatchStats
  805. * @return
  806. */
  807. private List<TestMarjorDto.TestMatchStat> splitStats(List<TestMarjorDto.TestMatchStat> matchStats, Integer index, Integer matchCount, List<TestMarjorDto.TestMatchStat> needMatchStats) {
  808. List<TestMarjorDto.TestMatchStat> remainMatchStats = Lists.newArrayList();
  809. for (int i = index; i < matchStats.size(); ) {
  810. TestMarjorDto.TestMatchStat ts = matchStats.remove(i);
  811. if (ts.getCount().equals(matchCount)) {
  812. needMatchStats.add(ts);
  813. } else {
  814. remainMatchStats.add(ts);
  815. }
  816. }
  817. return remainMatchStats;
  818. }
  819. public void updateTestAnswers(List<QuestionDto> questions, Long examineeId) {
  820. SyTestExaminee examinee = syTestExamineeMapper.selectSyTestExamineeById(examineeId);
  821. SyTestAnswers answerCond = new SyTestAnswers();
  822. answerCond.setExamineeId(examineeId);
  823. List<SyTestAnswers> answersList = syTestAnswersMapper.selectSyTestAnswersList(answerCond);
  824. Map<Long, SyTestAnswers> oldQuestionAnswersMap = answersList.stream().collect(Collectors.toMap(SyTestAnswers::getQuestionId, Function.identity()));
  825. Map<Long, QuestionDto> newQuestionAnswerMap = questions.stream().collect(Collectors.toMap(QuestionDto::getQuestionId, Function.identity()));
  826. SyTestQuestion questionCond = new SyTestQuestion();
  827. questionCond.setPaperId(examinee.getPaperId());
  828. List<SyTestQuestion> stdQuestionList = syTestQuestionMapper.selectSyTestQuestionList(questionCond);
  829. SyTestAnswers mxjbAnswersCond = new SyTestAnswers();
  830. stdQuestionList.forEach(t -> {
  831. SyTestAnswers oldAnswer = oldQuestionAnswersMap.remove(t.getQuestionId());
  832. QuestionDto newAnswer = newQuestionAnswerMap.get(t.getQuestionId());
  833. if (null != newAnswer) {
  834. mxjbAnswersCond.setAnswer(newAnswer.getAnswer() + "$@" + StringUtils.join(newAnswer.getAttachments(), "$@"));
  835. mxjbAnswersCond.setDuration(newAnswer.getDuration());
  836. } else {
  837. mxjbAnswersCond.setAnswer(null);
  838. mxjbAnswersCond.setDuration(-1L);
  839. }
  840. if (null != oldAnswer) {
  841. mxjbAnswersCond.setAnswerId(oldAnswer.getAnswerId());
  842. mxjbAnswersCond.setExamineeId(null);
  843. mxjbAnswersCond.setExamineeType(null);
  844. mxjbAnswersCond.setQuestionId(null);
  845. mxjbAnswersCond.setSeq(null);
  846. syTestAnswersMapper.updateSyTestAnswers(mxjbAnswersCond);
  847. } else {
  848. mxjbAnswersCond.setAnswerId(null);
  849. mxjbAnswersCond.setExamineeId(examineeId);
  850. mxjbAnswersCond.setExamineeType(MxjbContants.ExamineeTypeTest);
  851. mxjbAnswersCond.setQuestionId(t.getQuestionId());
  852. mxjbAnswersCond.setSeq(t.getSeq());
  853. syTestAnswersMapper.insertSyTestAnswers(mxjbAnswersCond);
  854. }
  855. });
  856. if (oldQuestionAnswersMap.size() > 0) {
  857. List<Long> oldIds = oldQuestionAnswersMap.values().stream().map(SyTestAnswers::getAnswerId).collect(Collectors.toList());
  858. syTestAnswersMapper.deleteSyTestAnswersByIds(oldIds.toArray(new Long[oldIds.size()]));
  859. }
  860. }
  861. public PaperDto loadPaperByTestMajorTypeId(Integer testType) {
  862. Long paperId = testType.longValue();
  863. SysUser sysUser = SecurityUtils.getLoginUser().getUser();
  864. SyTestExaminee mxjbExaminee = getLatestExaminee(testType, sysUser.getCode());
  865. SyTestPaper syTestPaper = syTestPaperMapper.selectSyTestPaperById(paperId.longValue());
  866. if (null != mxjbExaminee && (mxjbExaminee.getState() < MxjbContants.ExamineeStatusPublish)) { // 未测试完成的先继续测试
  867. PaperDto paperDto = loadPaperDto(syTestPaper, mxjbExaminee, true);
  868. if (MxjbContants.ExamineeStatusExamine.equals(mxjbExaminee.getState())) {
  869. examUtilService.updateTempAnswerAndSave(paperDto, mxjbExaminee.getExamineeId(), MxjbContants.ExamineeTypeTest);
  870. }
  871. return paperDto;
  872. }
  873. if (null != mxjbExaminee) {
  874. if (4 != testType && !isSelectCompleted(mxjbExaminee)) { // 测试完成但未选择的先选择
  875. throw new RuntimeException("请选选择专业类,完成本轮测试, 再进行下一轮");
  876. }
  877. // 关闭上一轮的测试,进入下一轮
  878. SyTestExaminee examineeUpdate = new SyTestExaminee();
  879. examineeUpdate.setExamineeId(mxjbExaminee.getExamineeId());
  880. examineeUpdate.setState(MxjbContants.ExamineeStatusClose);
  881. syTestExamineeMapper.updateSyTestExaminee(examineeUpdate);
  882. }
  883. SyTestExaminee examineeCond = new SyTestExaminee();
  884. PaperDto paperDto = loadPaperDto(syTestPaper, null, false);
  885. if (MxjbContants.ExamineeStatusSign.equals(paperDto.getState())) {
  886. paperDto.setRemaining(paperDto.getMode() * 60);
  887. paperDto.setMinutes(paperDto.getRemaining() / 60);
  888. paperDto.setState(MxjbContants.ExamineeStatusExamine);
  889. examineeCond.setPaperId(paperId);
  890. examineeCond.setCustomerCode(sysUser.getCode());
  891. examineeCond.setState(paperDto.getState());
  892. examineeCond.setBeginTime(paperDto.getBeginTime());
  893. examineeCond.setEndTime(paperDto.getEndTime());
  894. examineeCond.setScoreLevel("W");
  895. examineeCond.setScore(0L);
  896. examineeCond.setScoreRate(0.0);
  897. examineeCond.setRanking(0L);
  898. syTestExamineeMapper.insertSyTestExaminee(examineeCond);
  899. paperDto.setExamineeId(examineeCond.getExamineeId());
  900. }
  901. paperDto.getQuestions().get(0).setCurrent(true);
  902. return paperDto;
  903. }
  904. public PaperDto loadPaperByExaminerId(Long examineeId) {
  905. SyTestExaminee mxjbExaminee = syTestExamineeMapper.selectSyTestExamineeById(examineeId);
  906. if (null == mxjbExaminee) {
  907. throw new RuntimeException("无测试人员");
  908. }
  909. SyTestPaper syTestPaper = syTestPaperMapper.selectSyTestPaperById(mxjbExaminee.getPaperId());
  910. PaperDto paperDto = loadPaperDto(syTestPaper, mxjbExaminee, true);
  911. return paperDto;
  912. }
  913. private PaperDto loadPaperDto(SyTestPaper syTestPaper, SyTestExaminee examinee, boolean loadParse) {
  914. PaperDto paperDto = new PaperDto();
  915. SyTestQuestion cond = new SyTestQuestion();
  916. cond.setPaperId(syTestPaper.getPaperId());
  917. List<SyTestQuestion> questionsList = syTestQuestionMapper.selectSyTestQuestionList(cond);
  918. paperDto.setExamineeType(MxjbContants.ExamineeTypeTestValue);
  919. paperDto.setName(syTestPaper.getName());
  920. paperDto.setPaperId(syTestPaper.getPaperId());
  921. paperDto.setScoringType(MxjbContants.ScoringTypeAuto);
  922. paperDto.setPublish(true);
  923. paperDto.setMode(99*60L); // TODO 竞赛要设置时间
  924. paperDto.setEvaluationState(MxjbContants.EvaluationStatusOpen);
  925. paperDto.setBeginTime(new Date());
  926. paperDto.setEndTime(DateUtils.addMinutes(paperDto.getBeginTime(), paperDto.getMode().intValue()));
  927. if (null == examinee) { // 没有考生
  928. paperDto.setMinutes(0L);
  929. paperDto.setRemaining(0L);
  930. paperDto.setState(MxjbContants.ExamineeStatusSign);
  931. } else {
  932. paperDto.setState(examinee.getState());
  933. if (examinee.getState() >= MxjbContants.ExamineeStatusPublish) {
  934. paperDto.setBeginTime(examinee.getBeginTime());
  935. paperDto.setEndTime(examinee.getEndTime());
  936. paperDto.setRemaining(0L);
  937. } else {
  938. paperDto.setRemaining(paperDto.getMode() * 60);
  939. }
  940. paperDto.setMinutes(paperDto.getRemaining() / 60);
  941. paperDto.setExamineeId(examinee.getExamineeId());
  942. }
  943. final Map<Long, SyTestAnswers> questionAnswersMap;
  944. if(paperDto.getState() >= MxjbContants.ExamineeStatusExamine) {
  945. SyTestAnswers answerCond = new SyTestAnswers();
  946. answerCond.setExamineeId(examinee.getExamineeId());
  947. answerCond.setExamineeType(MxjbContants.ExamineeTypeTest);
  948. questionAnswersMap = syTestAnswersMapper.selectSyTestAnswersList(answerCond).stream().collect(Collectors.toMap(SyTestAnswers::getQuestionId, Function.identity()));
  949. } else {
  950. questionAnswersMap = null;
  951. }
  952. Boolean fillParseAndAnswer = loadParse || MxjbContants.ExamineeStatusPublish.equals(paperDto.getState()) // 自阅卷或发布时可显示成绩
  953. || MxjbContants.ExamineeStatusReview.equals(paperDto.getState());
  954. List<QuestionDto> questionDtoList = paperDto.getQuestions();
  955. questionsList.forEach(t -> {
  956. QuestionDto dto = examUtilService.toQuestionDto(t, fillParseAndAnswer, questionAnswersMap);
  957. dto.setSeq(questionDtoList.size() + 1L);
  958. questionDtoList.add(dto);
  959. });
  960. return paperDto;
  961. }
  962. public boolean isSelectCompleted(SyTestExaminee mxjbExaminee) {
  963. boolean noMatch = false;
  964. if (StringUtils.isNotBlank(mxjbExaminee.getStats())) {
  965. TestMarjorDto.TestStats testStat = null;
  966. try {
  967. testStat = om.readValue(mxjbExaminee.getStats(), TestMarjorDto.TestStats.class);
  968. noMatch = testStat.isNoMatch();
  969. } catch (JsonProcessingException e) {
  970. e.printStackTrace();
  971. }
  972. }
  973. return noMatch || isSelectCompleted(mxjbExaminee.getExamineeId());
  974. }
  975. private boolean isSelectCompleted(Long examineeId) {
  976. SyTestSelectCategory selectCategoryCond = new SyTestSelectCategory();
  977. selectCategoryCond.setExamineeId(examineeId);
  978. boolean selectCompleted = CollectionUtils.isNotEmpty(syTestSelectCategoryMapper.selectSyTestSelectCategoryList(selectCategoryCond));
  979. return selectCompleted;
  980. }
  981. private SyTestExaminee getLatestExaminee(Integer testType, String customerCode) {
  982. Long paperId = testType.longValue();
  983. Map cond = Maps.newHashMap();
  984. cond.put("paperId", paperId);
  985. cond.put("customerCode", customerCode);
  986. cond.put("cnt", 1);
  987. List<SyTestExaminee> existList = syTestExamineeMapper.selectSyTestExamineeLatest(cond);
  988. return CollectionUtils.isNotEmpty(existList) ? existList.get(0) : null;
  989. }
  990. }