AssessmentTemplatePlanServiceImpl.java 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  1. package com.xjrsoft.module.assessment.service.impl;
  2. import cn.dev33.satoken.stp.StpUtil;
  3. import cn.hutool.core.bean.BeanUtil;
  4. import cn.hutool.core.util.IdUtil;
  5. import cn.hutool.core.util.StrUtil;
  6. import com.alibaba.fastjson.JSONObject;
  7. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  8. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  9. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  10. import com.github.yulichang.base.MPJBaseServiceImpl;
  11. import com.github.yulichang.wrapper.MPJLambdaWrapper;
  12. import com.xjrsoft.common.enums.AssessQuestionTypeEnum;
  13. import com.xjrsoft.common.enums.DeleteMark;
  14. import com.xjrsoft.common.utils.VoToColumnUtil;
  15. import com.xjrsoft.common.utils.WeChatUtil;
  16. import com.xjrsoft.config.CommonPropertiesConfig;
  17. import com.xjrsoft.module.assessment.dto.AddAssessmentTemplatePlanDto;
  18. import com.xjrsoft.module.assessment.dto.AssessmentPlanAnswerPageDto;
  19. import com.xjrsoft.module.assessment.dto.AssessmentPlanAnswerStudentDto;
  20. import com.xjrsoft.module.assessment.dto.AssessmentPlanAnswerSubmitDto;
  21. import com.xjrsoft.module.assessment.dto.AssessmentPlanSubmitDto;
  22. import com.xjrsoft.module.assessment.dto.AssessmentQuestionResultDto;
  23. import com.xjrsoft.module.assessment.dto.AssessmentTemplatePlanPageDto;
  24. import com.xjrsoft.module.assessment.dto.AssessmentTemplatePlanSureDto;
  25. import com.xjrsoft.module.assessment.dto.UpdateAssessmentTemplatePlanDto;
  26. import com.xjrsoft.module.assessment.dto.UpdateAssessmentTemplatePlanTimeDto;
  27. import com.xjrsoft.module.assessment.entity.AssessmentPlanAnswerClass;
  28. import com.xjrsoft.module.assessment.entity.AssessmentPlanAnswerResult;
  29. import com.xjrsoft.module.assessment.entity.AssessmentPlanAnswerStudent;
  30. import com.xjrsoft.module.assessment.entity.AssessmentPlanQuestion;
  31. import com.xjrsoft.module.assessment.entity.AssessmentQuestion;
  32. import com.xjrsoft.module.assessment.entity.AssessmentTemplate;
  33. import com.xjrsoft.module.assessment.entity.AssessmentTemplatePlan;
  34. import com.xjrsoft.module.assessment.mapper.AssessmentPlanAnswerClassMapper;
  35. import com.xjrsoft.module.assessment.mapper.AssessmentPlanAnswerResultMapper;
  36. import com.xjrsoft.module.assessment.mapper.AssessmentPlanAnswerStudentMapper;
  37. import com.xjrsoft.module.assessment.mapper.AssessmentPlanQuestionMapper;
  38. import com.xjrsoft.module.assessment.mapper.AssessmentQuestionMapper;
  39. import com.xjrsoft.module.assessment.mapper.AssessmentTemplateMapper;
  40. import com.xjrsoft.module.assessment.mapper.AssessmentTemplatePlanMapper;
  41. import com.xjrsoft.module.assessment.service.IAssessmentTemplatePlanService;
  42. import com.xjrsoft.module.assessment.vo.AssessmentPlanAnswerClassResultVo;
  43. import com.xjrsoft.module.assessment.vo.AssessmentPlanAnswerDetailVo;
  44. import com.xjrsoft.module.assessment.vo.AssessmentPlanAnswerStudentPageVo;
  45. import com.xjrsoft.module.assessment.vo.AssessmentPlanAnswerStudentVo;
  46. import com.xjrsoft.module.assessment.vo.AssessmentPlanAnswerTeacherPageVo;
  47. import com.xjrsoft.module.assessment.vo.AssessmentQuestionOptionResultVo;
  48. import com.xjrsoft.module.assessment.vo.AssessmentQuestionResultVo;
  49. import com.xjrsoft.module.assessment.vo.AssessmentQuestionVo;
  50. import com.xjrsoft.module.assessment.vo.AssessmentTemplatePlanAnswerResultVo;
  51. import com.xjrsoft.module.assessment.vo.AssessmentTemplatePlanPageVo;
  52. import com.xjrsoft.module.assessment.vo.AssessmentTemplatePlanQuestionVo;
  53. import com.xjrsoft.module.assessment.vo.AssessmentTemplatePlanResultVo;
  54. import com.xjrsoft.module.assessment.vo.OneQuestionAnswerVo;
  55. import com.xjrsoft.module.assessment.vo.OneQuestionVo;
  56. import com.xjrsoft.module.assessment.vo.TwoQuestionAnswerVo;
  57. import com.xjrsoft.module.assessment.vo.TwoQuestionVo;
  58. import com.xjrsoft.module.base.entity.BaseClass;
  59. import com.xjrsoft.module.base.mapper.BaseClassMapper;
  60. import com.xjrsoft.module.organization.dto.WeChatSendMessageDto;
  61. import com.xjrsoft.module.organization.service.IWeChatService;
  62. import com.xjrsoft.module.student.entity.BaseStudentSchoolRoll;
  63. import com.xjrsoft.module.teacher.entity.XjrUser;
  64. import com.xjrsoft.module.teacher.mapper.XjrUserMapper;
  65. import lombok.AllArgsConstructor;
  66. import org.apache.poi.ss.usermodel.Cell;
  67. import org.apache.poi.ss.usermodel.CellStyle;
  68. import org.apache.poi.ss.usermodel.Font;
  69. import org.apache.poi.ss.usermodel.HorizontalAlignment;
  70. import org.apache.poi.ss.usermodel.Row;
  71. import org.apache.poi.ss.usermodel.Sheet;
  72. import org.apache.poi.ss.usermodel.VerticalAlignment;
  73. import org.apache.poi.ss.usermodel.Workbook;
  74. import org.apache.poi.ss.util.CellRangeAddress;
  75. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  76. import org.springframework.stereotype.Service;
  77. import org.springframework.transaction.annotation.Transactional;
  78. import java.io.ByteArrayOutputStream;
  79. import java.io.IOException;
  80. import java.text.SimpleDateFormat;
  81. import java.util.ArrayList;
  82. import java.util.Date;
  83. import java.util.HashMap;
  84. import java.util.List;
  85. import java.util.Map;
  86. import java.util.Objects;
  87. import java.util.Random;
  88. import java.util.stream.Collectors;
  89. /**
  90. * @title: 考核计划
  91. * @Author dzx
  92. * @Date: 2024-04-01
  93. * @Version 1.0
  94. */
  95. @Service
  96. @AllArgsConstructor
  97. public class AssessmentTemplatePlanServiceImpl extends MPJBaseServiceImpl<AssessmentTemplatePlanMapper, AssessmentTemplatePlan> implements IAssessmentTemplatePlanService {
  98. private final AssessmentTemplatePlanMapper templatePlanMapper;
  99. private final AssessmentPlanQuestionMapper planQuestionMapper;
  100. private final AssessmentPlanAnswerClassMapper answerClassMapper;
  101. private final AssessmentPlanAnswerStudentMapper studentMapper;
  102. private final AssessmentTemplateMapper assessmentTemplateMapper;
  103. private final AssessmentQuestionMapper questionMapper;
  104. private final AssessmentPlanAnswerResultMapper resultMapper;
  105. private final XjrUserMapper xjrUserMapper;
  106. private final IWeChatService weChatService;
  107. private final CommonPropertiesConfig commonPropertiesConfig;
  108. private final BaseClassMapper baseClassMapper;
  109. private final WeChatUtil weChatUtil;
  110. @Override
  111. @Transactional(rollbackFor = Exception.class)
  112. public Boolean add(AddAssessmentTemplatePlanDto dto) {
  113. AssessmentTemplate template = assessmentTemplateMapper.selectById(dto.getAssessmentTemplateId());
  114. AssessmentTemplatePlan assessmentTemplatePlan = BeanUtil.toBean(dto, AssessmentTemplatePlan.class);
  115. assessmentTemplatePlan.setScore(template.getScore());
  116. templatePlanMapper.insert(assessmentTemplatePlan);
  117. //查询模板下面的题目信息
  118. List<AssessmentQuestion> questionList = questionMapper.selectList(
  119. new QueryWrapper<AssessmentQuestion>().lambda()
  120. .eq(AssessmentQuestion::getDeleteMark, DeleteMark.NODELETE.getCode())
  121. .eq(AssessmentQuestion::getAssessmentTemplateId, dto.getAssessmentTemplateId())
  122. );
  123. Map<Long, Long> parentMap = new HashMap<>();
  124. for (AssessmentQuestion assessmentQuestion : questionList) {
  125. parentMap.put(assessmentQuestion.getId(), IdUtil.getSnowflakeNextId());
  126. }
  127. for (AssessmentQuestion assessmentQuestion : questionList) {
  128. AssessmentPlanQuestion planQuestion = BeanUtil.toBean(assessmentQuestion, AssessmentPlanQuestion.class);
  129. planQuestion.setId(parentMap.get(assessmentQuestion.getId()));
  130. planQuestion.setParentId(parentMap.get(assessmentQuestion.getParentId()));
  131. planQuestion.setAssessmentTemplatePlanId(assessmentTemplatePlan.getId());
  132. planQuestionMapper.insert(planQuestion);
  133. }
  134. List<BaseClass> classList = baseClassMapper.selectList(
  135. new QueryWrapper<BaseClass>().lambda()
  136. .eq(BaseClass::getDeleteMark, DeleteMark.NODELETE.getCode())
  137. .eq(BaseClass::getIsGraduate, 1)
  138. );
  139. List<Long> classIds = new ArrayList<>();
  140. if("all".equals(dto.getType())){
  141. for (BaseClass baseClass : classList) {
  142. classIds.add(baseClass.getId());
  143. }
  144. dto.setClassIds(classIds);
  145. }else if("random".equals(dto.getType())){
  146. List<Integer> randomList = new ArrayList<>();
  147. Random random = new Random();
  148. while(randomList.size() < dto.getRandomNumber()){
  149. int nextInt = random.nextInt(classList.size());
  150. if(!randomList.contains(nextInt) && nextInt >= 0){
  151. randomList.add(nextInt);
  152. }
  153. }
  154. for (Integer i : randomList) {
  155. classIds.add(classList.get(i).getId());
  156. }
  157. dto.setClassIds(classIds);
  158. }
  159. //存入班级,并根据选人方式存入答题学生
  160. for (Long classId : dto.getClassIds()) {
  161. AssessmentPlanAnswerClass answerClass = new AssessmentPlanAnswerClass();
  162. answerClass.setAssessmentTemplatePlanId(assessmentTemplatePlan.getId());
  163. answerClass.setClassId(classId);
  164. answerClassMapper.insert(answerClass);
  165. MPJLambdaWrapper<XjrUser> query = new MPJLambdaWrapper<>();
  166. query.leftJoin(BaseStudentSchoolRoll.class, BaseStudentSchoolRoll::getUserId, XjrUser::getId)
  167. .eq(BaseStudentSchoolRoll::getClassId, classId);
  168. List<XjrUser> studentList = xjrUserMapper.selectJoinList(XjrUser.class, query);
  169. for (XjrUser xjrUser : studentList) {
  170. AssessmentPlanAnswerStudent answer = new AssessmentPlanAnswerStudent();
  171. answer.setAssessmentTemplatePlanId(assessmentTemplatePlan.getId());
  172. answer.setStudentUserId(xjrUser.getId());
  173. studentMapper.insert(answer);
  174. }
  175. }
  176. return true;
  177. }
  178. @Override
  179. @Transactional(rollbackFor = Exception.class)
  180. public Boolean update(UpdateAssessmentTemplatePlanDto dto) {
  181. AssessmentTemplate template = assessmentTemplateMapper.selectById(dto.getAssessmentTemplateId());
  182. AssessmentTemplatePlan assessmentTemplatePlan = BeanUtil.toBean(dto, AssessmentTemplatePlan.class);
  183. assessmentTemplatePlan.setScore(template.getScore());
  184. templatePlanMapper.updateById(assessmentTemplatePlan);
  185. //删除以前的题目
  186. planQuestionMapper.deleteByPlanId(assessmentTemplatePlan.getId(), StpUtil.getLoginIdAsLong());
  187. //查询模板下面的题目信息
  188. List<AssessmentQuestion> questionList = questionMapper.selectList(
  189. new QueryWrapper<AssessmentQuestion>().lambda()
  190. .eq(AssessmentQuestion::getDeleteMark, DeleteMark.NODELETE.getCode())
  191. .eq(AssessmentQuestion::getAssessmentTemplateId, dto.getAssessmentTemplateId())
  192. .orderByAsc(AssessmentQuestion::getCategory)
  193. );
  194. Map<Long, Long> parentMap = new HashMap<>();
  195. for (AssessmentQuestion assessmentQuestion : questionList) {
  196. parentMap.put(assessmentQuestion.getId(), IdUtil.getSnowflakeNextId());
  197. }
  198. for (AssessmentQuestion assessmentQuestion : questionList) {
  199. AssessmentPlanQuestion planQuestion = BeanUtil.toBean(assessmentQuestion, AssessmentPlanQuestion.class);
  200. planQuestion.setId(parentMap.get(assessmentQuestion.getId()));
  201. planQuestion.setParentId(parentMap.get(assessmentQuestion.getParentId()));
  202. planQuestion.setAssessmentTemplatePlanId(assessmentTemplatePlan.getId());
  203. planQuestionMapper.insert(planQuestion);
  204. }
  205. //删除班级和学生
  206. answerClassMapper.deleteByPlanId(assessmentTemplatePlan.getId(), StpUtil.getLoginIdAsLong());
  207. studentMapper.deleteByPlanId(assessmentTemplatePlan.getId(), StpUtil.getLoginIdAsLong());
  208. List<BaseClass> classList = baseClassMapper.selectList(
  209. new QueryWrapper<BaseClass>().lambda()
  210. .eq(BaseClass::getDeleteMark, DeleteMark.NODELETE.getCode())
  211. .eq(BaseClass::getIsGraduate, 1)
  212. );
  213. List<Long> classIds = new ArrayList<>();
  214. if("all".equals(dto.getType())){
  215. for (BaseClass baseClass : classList) {
  216. classIds.add(baseClass.getId());
  217. }
  218. }else if("random".equals(dto.getType())){
  219. List<Integer> randomList = new ArrayList<>();
  220. Random random = new Random();
  221. while(randomList.size() < dto.getRandomNumber()){
  222. int nextInt = random.nextInt(classList.size());
  223. if(!randomList.contains(nextInt) && nextInt >= 0){
  224. randomList.add(nextInt);
  225. }
  226. }
  227. for (Integer i : randomList) {
  228. classIds.add(classList.get(i).getId());
  229. }
  230. }
  231. dto.setClassIds(classIds);
  232. //存入班级,并根据选人方式存入答题学生
  233. for (Long classId : dto.getClassIds()) {
  234. AssessmentPlanAnswerClass answerClass = new AssessmentPlanAnswerClass();
  235. answerClass.setAssessmentTemplatePlanId(assessmentTemplatePlan.getId());
  236. answerClass.setClassId(classId);
  237. answerClassMapper.insert(answerClass);
  238. MPJLambdaWrapper<XjrUser> query = new MPJLambdaWrapper<>();
  239. query.leftJoin(BaseStudentSchoolRoll.class, BaseStudentSchoolRoll::getUserId, XjrUser::getId)
  240. .eq(BaseStudentSchoolRoll::getClassId, classId);
  241. List<XjrUser> studentList = xjrUserMapper.selectJoinList(XjrUser.class, query);
  242. for (XjrUser xjrUser : studentList) {
  243. AssessmentPlanAnswerStudent answer = new AssessmentPlanAnswerStudent();
  244. answer.setAssessmentTemplatePlanId(assessmentTemplatePlan.getId());
  245. answer.setStudentUserId(xjrUser.getId());
  246. studentMapper.insert(answer);
  247. }
  248. }
  249. return true;
  250. }
  251. @Override
  252. public Boolean updateTime(UpdateAssessmentTemplatePlanTimeDto dto) {
  253. AssessmentTemplatePlan assessmentTemplatePlan = BeanUtil.toBean(dto, AssessmentTemplatePlan.class);
  254. templatePlanMapper.updateById(assessmentTemplatePlan);
  255. return true;
  256. }
  257. @Override
  258. @Transactional(rollbackFor = Exception.class)
  259. public Boolean delete(List<Long> ids) {
  260. templatePlanMapper.deleteBatchIds(ids);
  261. planQuestionMapper.delete(Wrappers.lambdaQuery(AssessmentPlanQuestion.class).in(AssessmentPlanQuestion::getAssessmentTemplatePlanId, ids));
  262. answerClassMapper.delete(Wrappers.lambdaQuery(AssessmentPlanAnswerClass.class).in(AssessmentPlanAnswerClass::getAssessmentTemplatePlanId, ids));
  263. studentMapper.delete(Wrappers.lambdaQuery(AssessmentPlanAnswerStudent.class).in(AssessmentPlanAnswerStudent::getAssessmentTemplatePlanId, ids));
  264. return true;
  265. }
  266. @Override
  267. public Page<AssessmentTemplatePlanPageVo> getPage(Page<AssessmentTemplatePlanPageVo> page, AssessmentTemplatePlanPageDto dto) {
  268. return templatePlanMapper.getPage(page, dto);
  269. }
  270. @Override
  271. public Page<AssessmentPlanAnswerStudentPageVo> getStudentPage(Page<AssessmentPlanAnswerStudentPageVo> page, AssessmentPlanAnswerPageDto dto) {
  272. return templatePlanMapper.getStudentPage(page, dto);
  273. }
  274. @Override
  275. public Page<AssessmentPlanAnswerTeacherPageVo> getTeacherPage(Page<AssessmentPlanAnswerTeacherPageVo> page, AssessmentPlanAnswerPageDto dto) {
  276. return templatePlanMapper.getTeacherPage(page, dto);
  277. }
  278. @Override
  279. public Boolean sure(AssessmentTemplatePlanSureDto dto) {
  280. long teacherId = StpUtil.getLoginIdAsLong();
  281. answerClassMapper.sure(dto, teacherId);
  282. studentMapper.sure(dto, teacherId);
  283. return true;
  284. }
  285. @Override
  286. public AssessmentTemplatePlanQuestionVo getQuestionsById(Long id) {
  287. AssessmentTemplatePlan templatePlan = templatePlanMapper.selectById(id);
  288. AssessmentTemplatePlanQuestionVo vo = BeanUtil.toBean(templatePlan, AssessmentTemplatePlanQuestionVo.class);
  289. List<AssessmentPlanQuestion> questionList = planQuestionMapper.selectList(
  290. new QueryWrapper<AssessmentPlanQuestion>().lambda()
  291. .eq(AssessmentPlanQuestion::getDeleteMark, DeleteMark.NODELETE.getCode())
  292. .eq(AssessmentPlanQuestion::getAssessmentTemplatePlanId, id)
  293. );
  294. Integer submitStatus = studentMapper.getSubmitStatus(id, StpUtil.getLoginIdAsLong());
  295. vo.setSubmitStatus(submitStatus);
  296. AssessmentQuestionResultDto resultDto = new AssessmentQuestionResultDto();
  297. resultDto.setAssessmentTemplatePlanId(id);
  298. resultDto.setUserId(StpUtil.getLoginIdAsLong());
  299. List<AssessmentQuestionResultVo> resultList = resultMapper.getResultList(resultDto);
  300. Map<Long, String> resultMap = new HashMap<>();
  301. for (AssessmentQuestionResultVo resultVo : resultList) {
  302. resultMap.put(resultVo.getQuestionId(), resultVo.getAnswer());
  303. }
  304. //循环大题
  305. List<AssessmentQuestionVo> oneList = new ArrayList<>();
  306. for (AssessmentPlanQuestion assessmentQuestion : questionList) {
  307. if(assessmentQuestion.getCategory() == 1){
  308. oneList.add(BeanUtil.toBean(assessmentQuestion, AssessmentQuestionVo.class));
  309. }
  310. }
  311. //循环小题
  312. for (AssessmentQuestionVo oneQuestion : oneList) {
  313. List<AssessmentQuestionVo> twoList = new ArrayList<>();
  314. for (AssessmentPlanQuestion assessmentQuestion : questionList) {
  315. if(assessmentQuestion.getCategory() == 2 && assessmentQuestion.getParentId().equals(oneQuestion.getId())){
  316. AssessmentQuestionVo questionVo = BeanUtil.toBean(assessmentQuestion, AssessmentQuestionVo.class);
  317. questionVo.setValue(resultMap.get(questionVo.getId()));
  318. twoList.add(questionVo);
  319. }
  320. }
  321. oneQuestion.setChildren(twoList);
  322. }
  323. //循环选项
  324. for (AssessmentQuestionVo oneQuestion : oneList) {
  325. for (AssessmentQuestionVo twoQuestion : oneQuestion.getChildren()) {
  326. List<AssessmentQuestionVo> optionList = new ArrayList<>();
  327. for (AssessmentPlanQuestion assessmentQuestion : questionList) {
  328. if(assessmentQuestion.getCategory() == 3 && assessmentQuestion.getParentId().equals(twoQuestion.getId())){
  329. optionList.add(BeanUtil.toBean(assessmentQuestion, AssessmentQuestionVo.class));
  330. }
  331. }
  332. twoQuestion.setOptions(optionList);
  333. }
  334. }
  335. vo.setChildren(oneList);
  336. return vo;
  337. }
  338. @Override
  339. public Boolean saveAnswer(AssessmentPlanSubmitDto dto) {
  340. long studentUserId = StpUtil.getLoginIdAsLong();
  341. Date date = new Date();
  342. List<Long> questionIds = new ArrayList<>();
  343. for (AssessmentPlanAnswerSubmitDto answerSubmitDto : dto.getAnswerList()) {
  344. questionIds.add(answerSubmitDto.getQuestionId());
  345. }
  346. Map<Long, String> questionTypeMaps = new HashMap<>();
  347. planQuestionMapper.selectList(
  348. new QueryWrapper<AssessmentPlanQuestion>().lambda()
  349. .in(AssessmentPlanQuestion::getId, questionIds)
  350. ).forEach(question ->{
  351. questionTypeMaps.put(question.getId(), question.getType());
  352. });
  353. // 学生提交
  354. studentMapper.submitAnswer(dto.getAssessmentTemplatePlanId(), studentUserId);
  355. for (AssessmentPlanAnswerSubmitDto answerSubmitDto : dto.getAnswerList()) {
  356. if("multi_question".equals(questionTypeMaps.get(answerSubmitDto.getQuestionId()))){
  357. String[] split = answerSubmitDto.getAnswerId().split(",");
  358. for (String answerId : split) {
  359. AssessmentPlanAnswerResult answerStudent = new AssessmentPlanAnswerResult();
  360. answerStudent.setAssessmentTemplatePlanId(dto.getAssessmentTemplatePlanId());
  361. answerStudent.setStudentUserId(studentUserId);
  362. answerStudent.setCreateDate(date);
  363. answerStudent.setQuestionId(answerSubmitDto.getQuestionId());
  364. answerStudent.setAnswerId(answerId);
  365. resultMapper.insert(answerStudent);
  366. }
  367. }else{
  368. AssessmentPlanAnswerResult answerStudent = new AssessmentPlanAnswerResult();
  369. answerStudent.setAssessmentTemplatePlanId(dto.getAssessmentTemplatePlanId());
  370. answerStudent.setStudentUserId(studentUserId);
  371. answerStudent.setCreateDate(date);
  372. answerStudent.setQuestionId(answerSubmitDto.getQuestionId());
  373. answerStudent.setAnswerId(answerSubmitDto.getAnswerId());
  374. resultMapper.insert(answerStudent);
  375. }
  376. }
  377. return true;
  378. }
  379. @Override
  380. public List<AssessmentTemplatePlanPageVo> getMobilePage(AssessmentTemplatePlanPageDto dto) {
  381. return templatePlanMapper.getMobilePage(dto);
  382. }
  383. @Override
  384. public Page<AssessmentPlanAnswerStudentVo> getAnswerStudent(Page<AssessmentPlanAnswerStudentDto> page, AssessmentPlanAnswerStudentDto dto) {
  385. List<String> roleList = StpUtil.getRoleList();
  386. if(roleList.size() == 2 && roleList.contains("CLASSTE") && roleList.contains("TEACHER")){
  387. dto.setTeacherId(StpUtil.getLoginIdAsLong());
  388. }
  389. return templatePlanMapper.getAnswerStudent(page, dto);
  390. }
  391. @Override
  392. public List<AssessmentPlanAnswerStudentVo> getAnswerStudentList(AssessmentPlanAnswerStudentDto dto) {
  393. List<String> roleList = StpUtil.getRoleList();
  394. if(roleList.size() == 2 && roleList.contains("CLASSTE") && roleList.contains("TEACHER")){
  395. dto.setTeacherId(StpUtil.getLoginIdAsLong());
  396. }
  397. return this.baseMapper.getAnswerStudentList(dto);
  398. }
  399. @Override
  400. public List<Long> getSemesterClass(Long id) {
  401. return templatePlanMapper.getSemesterClass(id);
  402. }
  403. @Override
  404. public List<AssessmentPlanAnswerClassResultVo> getClassResult(Long id) {
  405. return resultMapper.getClassResult(id);
  406. }
  407. @Override
  408. public AssessmentTemplatePlanResultVo getQuestionResult(Long id) {
  409. AssessmentTemplatePlan templatePlan = templatePlanMapper.selectById(id);
  410. AssessmentTemplatePlanResultVo vo = BeanUtil.toBean(templatePlan, AssessmentTemplatePlanResultVo.class);
  411. vo.setStudentCount(studentMapper.getStudentCount(id));
  412. List<AssessmentPlanQuestion> questionList = planQuestionMapper.selectList(
  413. new QueryWrapper<AssessmentPlanQuestion>().lambda()
  414. .eq(AssessmentPlanQuestion::getDeleteMark, DeleteMark.NODELETE.getCode())
  415. .eq(AssessmentPlanQuestion::getAssessmentTemplatePlanId, id)
  416. );
  417. Map<Long, String> questionTypeMap = questionList.stream().collect(Collectors.toMap(AssessmentPlanQuestion::getAssessmentTemplatePlanId, AssessmentPlanQuestion::getType));
  418. //循环大题
  419. List<OneQuestionVo> oneList = new ArrayList<>();
  420. for (AssessmentPlanQuestion assessmentQuestion : questionList) {
  421. if(assessmentQuestion.getCategory() == 1){
  422. oneList.add(BeanUtil.toBean(assessmentQuestion, OneQuestionVo.class));
  423. }
  424. }
  425. //循环小题
  426. for (OneQuestionVo oneQuestion : oneList) {
  427. List<TwoQuestionVo> twoList = new ArrayList<>();
  428. for (AssessmentPlanQuestion assessmentQuestion : questionList) {
  429. if(assessmentQuestion.getCategory() == 2 && assessmentQuestion.getParentId().equals(oneQuestion.getId())){
  430. TwoQuestionVo questionVo = BeanUtil.toBean(assessmentQuestion, TwoQuestionVo.class);
  431. twoList.add(questionVo);
  432. }
  433. }
  434. oneQuestion.setChildren(twoList);
  435. }
  436. List<AssessmentPlanAnswerDetailVo> questionResult = resultMapper.getQuestionResult(id);
  437. Map<String, Integer> answerCountMap = new HashMap<>();
  438. for (AssessmentPlanAnswerDetailVo countVo : questionResult) {
  439. answerCountMap.put(countVo.getQuestionId().toString() + countVo.getAnswerId(), countVo.getChooseCount());
  440. }
  441. //量表
  442. List<AssessmentPlanAnswerDetailVo> scaleQuestionResult = resultMapper.getScaleQuestionResult(id);
  443. //填空
  444. List<AssessmentPlanAnswerDetailVo> textQuestionResult = resultMapper.getTextQuestionResult(id);
  445. //循环选项
  446. for (OneQuestionVo oneQuestion : oneList) {
  447. for (TwoQuestionVo twoQuestion : oneQuestion.getChildren()) {
  448. List<AssessmentQuestionOptionResultVo> optionList = new ArrayList<>();
  449. if(AssessQuestionTypeEnum.RADIO.getCode().equals(twoQuestion.getType()) || AssessQuestionTypeEnum.MULTI.getCode().equals(twoQuestion.getType())){
  450. for (AssessmentPlanQuestion assessmentQuestion : questionList) {
  451. if(assessmentQuestion.getCategory() == 3 && assessmentQuestion.getParentId().equals(twoQuestion.getId())){
  452. AssessmentQuestionOptionResultVo questionVo = BeanUtil.toBean(assessmentQuestion, AssessmentQuestionOptionResultVo.class);
  453. Integer count = answerCountMap.get(assessmentQuestion.getParentId().toString() + assessmentQuestion.getId().toString());
  454. questionVo.setCount(count == null?0:count);
  455. questionVo.setPercentage(vo.getStudentCount());
  456. optionList.add(questionVo);
  457. }
  458. }
  459. }else if(AssessQuestionTypeEnum.JUDGE.getCode().equals(twoQuestion.getType())){
  460. optionList.add(new AssessmentQuestionOptionResultVo(){{
  461. setName("是");
  462. Integer count = answerCountMap.get(twoQuestion.getId().toString() + "1");
  463. setPercentage(vo.getStudentCount());
  464. setCount(count == null?0:count);
  465. }});
  466. optionList.add(new AssessmentQuestionOptionResultVo(){{
  467. setName("否");
  468. Integer count = answerCountMap.get(twoQuestion.getId().toString() + "0");
  469. setPercentage(vo.getStudentCount());
  470. setCount(count == null?0:count);
  471. }});
  472. }else if(AssessQuestionTypeEnum.SCALE.getCode().equals(twoQuestion.getType())) {
  473. for (AssessmentPlanAnswerDetailVo detailVo : scaleQuestionResult) {
  474. if(detailVo.getQuestionId().equals(twoQuestion.getId())){
  475. optionList.add(new AssessmentQuestionOptionResultVo(){{
  476. setName(detailVo.getAnswerId() + "星");
  477. Integer count = detailVo.getChooseCount();
  478. setPercentage(vo.getStudentCount());
  479. setCount(count == null?0:count);
  480. }});
  481. }
  482. }
  483. }else if(AssessQuestionTypeEnum.TEXT.getCode().equals(twoQuestion.getType())) {
  484. AssessmentQuestionOptionResultVo questionVo = new AssessmentQuestionOptionResultVo();
  485. int notNullCount = 0;
  486. int nullCount = 0;
  487. for (AssessmentPlanAnswerDetailVo detailVo : textQuestionResult) {
  488. if(!Objects.equals(detailVo.getQuestionId(), twoQuestion.getId())){
  489. continue;
  490. }
  491. if(StrUtil.isNotEmpty(detailVo.getAnswerId())){
  492. notNullCount ++;
  493. }else{
  494. nullCount ++;
  495. }
  496. }
  497. Integer count = answerCountMap.get(twoQuestion.getId().toString() + "null");
  498. questionVo.setCount(notNullCount);
  499. questionVo.setName("已答题");
  500. questionVo.setPercentage(vo.getStudentCount());
  501. optionList.add(questionVo);
  502. }
  503. twoQuestion.setResultList(optionList);
  504. }
  505. }
  506. vo.setChildren(oneList);
  507. return vo;
  508. }
  509. @Override
  510. public Boolean sendMsg(AssessmentTemplatePlanSureDto dto) {
  511. //先查询班上的所有学生
  512. MPJLambdaWrapper<XjrUser> queryWrapper = new MPJLambdaWrapper<>();
  513. queryWrapper
  514. .select(XjrUser.class, x -> VoToColumnUtil.fieldsToColumns(XjrUser.class).contains(x.getProperty()))
  515. .innerJoin(AssessmentPlanAnswerStudent.class, AssessmentPlanAnswerStudent::getStudentUserId,XjrUser::getId)
  516. .innerJoin(BaseStudentSchoolRoll.class, BaseStudentSchoolRoll::getUserId,XjrUser::getId)
  517. .eq(BaseStudentSchoolRoll::getClassId , dto.getClassId())
  518. .eq(AssessmentPlanAnswerStudent::getAssessmentTemplatePlanId , dto.getId())
  519. .disableSubLogicDel();
  520. List<XjrUser> userList = xjrUserMapper.selectJoinList(XjrUser.class, queryWrapper);
  521. AssessmentTemplatePlan templatePlan = templatePlanMapper.selectById(dto.getId());
  522. SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
  523. for (XjrUser user : userList) {
  524. WeChatSendMessageDto weChatSendMessageDto = new WeChatSendMessageDto();
  525. weChatSendMessageDto.setUserId(user.getOpenId());
  526. weChatSendMessageDto.setTemplateId(weChatUtil.getAssessmentTemplate());
  527. weChatSendMessageDto.setUrl(StrUtil.format("{}/pages/student/canteen/evaluate?id={}", commonPropertiesConfig.getDomainApp(), dto.getId()));
  528. weChatSendMessageDto.setMsgId(dto.getId().toString());
  529. JSONObject data = new JSONObject();
  530. data.put("thing1.DATA", templatePlan.getName());
  531. data.put("time2.DATA", sdf.format(templatePlan.getStartTime()));
  532. data.put("time3.DATA", sdf.format(templatePlan.getEndTime()));
  533. data.put("thing5.DATA", "test");
  534. data.put("thing8.DATA", "test");
  535. weChatSendMessageDto.setContent(data);
  536. weChatService.sendTemplateMessage(weChatSendMessageDto);
  537. }
  538. return true;
  539. }
  540. @Override
  541. public AssessmentTemplatePlanAnswerResultVo getAnswerStudentResult(Long studentUserId, Long assessmentTemplatePlanId) {
  542. AssessmentTemplatePlan templatePlan = templatePlanMapper.selectById(assessmentTemplatePlanId);
  543. AssessmentTemplatePlanAnswerResultVo vo = BeanUtil.toBean(templatePlan, AssessmentTemplatePlanAnswerResultVo.class);
  544. List<AssessmentPlanQuestion> questionList = planQuestionMapper.selectList(
  545. new QueryWrapper<AssessmentPlanQuestion>().lambda()
  546. .eq(AssessmentPlanQuestion::getDeleteMark, DeleteMark.NODELETE.getCode())
  547. .eq(AssessmentPlanQuestion::getAssessmentTemplatePlanId, assessmentTemplatePlanId)
  548. );
  549. //循环大题
  550. List<OneQuestionAnswerVo> oneList = new ArrayList<>();
  551. for (AssessmentPlanQuestion assessmentQuestion : questionList) {
  552. if(assessmentQuestion.getCategory() == 1){
  553. oneList.add(BeanUtil.toBean(assessmentQuestion, OneQuestionAnswerVo.class));
  554. }
  555. }
  556. //循环小题
  557. for (OneQuestionAnswerVo oneQuestion : oneList) {
  558. List<TwoQuestionAnswerVo> twoList = new ArrayList<>();
  559. for (AssessmentPlanQuestion assessmentQuestion : questionList) {
  560. if(assessmentQuestion.getCategory() == 2 && assessmentQuestion.getParentId().equals(oneQuestion.getId())){
  561. TwoQuestionAnswerVo questionVo = BeanUtil.toBean(assessmentQuestion, TwoQuestionAnswerVo.class);
  562. twoList.add(questionVo);
  563. }
  564. }
  565. oneQuestion.setChildren(twoList);
  566. }
  567. List<AssessmentPlanAnswerDetailVo> questionResult = resultMapper.getStudentQuestionResultList(assessmentTemplatePlanId, studentUserId);
  568. Map<Long, String> answerResultMap = new HashMap<>();
  569. for (AssessmentPlanAnswerDetailVo countVo : questionResult) {
  570. answerResultMap.put(countVo.getQuestionId(), countVo.getAnswerId());
  571. }
  572. //循环选项
  573. for (OneQuestionAnswerVo oneQuestion : oneList) {
  574. for (TwoQuestionAnswerVo twoQuestion : oneQuestion.getChildren()) {
  575. twoQuestion.setResult(answerResultMap.get(twoQuestion.getId()));
  576. }
  577. }
  578. vo.setChildren(oneList);
  579. return vo;
  580. }
  581. @Override
  582. public byte[] answerExportQuery(AssessmentPlanAnswerStudentDto dto) throws IOException {
  583. //查询大题
  584. List<AssessmentPlanQuestion> oneQuesList = planQuestionMapper.selectList(
  585. new QueryWrapper<AssessmentPlanQuestion>().lambda()
  586. .eq(AssessmentPlanQuestion::getDeleteMark, DeleteMark.NODELETE.getCode())
  587. .eq(AssessmentPlanQuestion::getAssessmentTemplatePlanId, dto.getAssessmentTemplatePlanId())
  588. .eq(AssessmentPlanQuestion::getCategory, 1)
  589. .orderByAsc(AssessmentPlanQuestion::getId)
  590. );
  591. //查询二级题目
  592. List<AssessmentPlanQuestion> towQuesList = planQuestionMapper.selectList(
  593. new QueryWrapper<AssessmentPlanQuestion>().lambda()
  594. .eq(AssessmentPlanQuestion::getCategory, 2)
  595. .eq(AssessmentPlanQuestion::getAssessmentTemplatePlanId, dto.getAssessmentTemplatePlanId())
  596. .eq(AssessmentPlanQuestion::getDeleteMark, DeleteMark.NODELETE.getCode())
  597. .orderByAsc(AssessmentPlanQuestion::getId)
  598. );
  599. Map<Long, List<AssessmentPlanQuestion>> towQuesMaps = towQuesList.stream().collect(Collectors.groupingBy(AssessmentPlanQuestion::getParentId));
  600. List<AssessmentPlanAnswerDetailVo> results = resultMapper.getPlanQuestionResultList(dto.getAssessmentTemplatePlanId());
  601. Map<Long, List<AssessmentPlanAnswerDetailVo>> studentAnswerMaps = results.stream().collect(Collectors.groupingBy(AssessmentPlanAnswerDetailVo::getStudentUserId));
  602. //查询已答题学生
  603. List<AssessmentPlanAnswerStudentVo> studentList = this.baseMapper.getAnswerStudentList(dto);
  604. List<List<String>> dataList = new ArrayList<>();
  605. int sortCode = 1;
  606. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  607. for (AssessmentPlanAnswerStudentVo studentVo : studentList) {
  608. List<String> rowData = new ArrayList<>();
  609. rowData.add(sortCode + "");
  610. rowData.add(studentVo.getClassName());
  611. rowData.add(studentVo.getName());
  612. rowData.add(sdf.format(studentVo.getSubmitDate()));
  613. Map<Long, List<AssessmentPlanAnswerDetailVo>> answerMaps = studentAnswerMaps.get(Long.parseLong(studentVo.getStudentUserId()))
  614. .stream().collect(Collectors.groupingBy(AssessmentPlanAnswerDetailVo::getQuestionId));
  615. for (AssessmentPlanQuestion question : towQuesList) {
  616. List<AssessmentPlanAnswerDetailVo> resultList = answerMaps.get(question.getId());
  617. if(resultList.isEmpty()){
  618. rowData.add("");
  619. continue;
  620. }
  621. if(AssessQuestionTypeEnum.MULTI.getCode().equals(question.getType())){
  622. String answerStr = "";
  623. for(int i = 0; i < resultList.size(); i ++){
  624. if(i > 0){
  625. answerStr += "\r\n";
  626. }
  627. answerStr += resultList.get(i).getAnswerId() + "";
  628. }
  629. rowData.add(answerStr);
  630. }else if(AssessQuestionTypeEnum.SCALE.getCode().equals(question.getType())){
  631. rowData.add(resultList.get(0).getAnswerId() + "星");
  632. }else{
  633. List<String> answerList = resultList.stream().map(AssessmentPlanAnswerDetailVo::getAnswerId).collect(Collectors.toList());
  634. rowData.add(answerList.toString().replace("[", "").replace("]", ""));
  635. }
  636. }
  637. dataList.add(rowData);
  638. sortCode ++;
  639. }
  640. Workbook workbook = new XSSFWorkbook();
  641. // 创建一个工作表(sheet)
  642. String sheetName = "数据";
  643. Sheet sheet = workbook.createSheet(sheetName);
  644. createFirstTitle(workbook, sheet, oneQuesList, towQuesMaps);
  645. createSecondTitle(workbook, sheet, towQuesList);
  646. int dataRowNumber = 2;
  647. for (List<String> rowData : dataList) {
  648. Row dataRow = sheet.createRow(dataRowNumber);
  649. for (int i = 0; i < rowData.size(); i ++){
  650. // if(i < 7){
  651. // sheet.autoSizeColumn(i);
  652. // }
  653. String content = rowData.get(i);
  654. Font font = workbook.createFont();
  655. font.setBold(false);// 设置为粗体
  656. font.setFontName("宋体");
  657. font.setFontHeightInPoints((short)12);
  658. CellStyle cellStyle = workbook.createCellStyle();
  659. cellStyle.setFont(font); // 将字体应用到样式
  660. cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
  661. cellStyle.setAlignment(HorizontalAlignment.LEFT);
  662. Cell row1cell2 = dataRow.createCell(i);
  663. row1cell2.setCellValue(content);
  664. row1cell2.setCellStyle(cellStyle);
  665. }
  666. dataRowNumber ++;
  667. }
  668. //写入文件
  669. ByteArrayOutputStream bot = new ByteArrayOutputStream();
  670. workbook.write(bot);
  671. return bot.toByteArray();
  672. }
  673. /**
  674. * 创建第一行表头
  675. */
  676. void createFirstTitle(Workbook workbook, Sheet sheet, List<AssessmentPlanQuestion> oneQuesList, Map<Long, List<AssessmentPlanQuestion>> towQuesMaps) {
  677. int rowNumber = 0;
  678. Font font = workbook.createFont();
  679. font.setBold(true);// 设置为粗体
  680. font.setFontName("宋体");
  681. //font.setColor(IndexedColors.RED.getIndex()); // 设置字体颜色为红色
  682. font.setFontHeightInPoints((short)12);
  683. CellStyle cellStyle = workbook.createCellStyle();
  684. cellStyle.setFont(font); // 将字体应用到样式
  685. cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
  686. cellStyle.setAlignment(HorizontalAlignment.CENTER);
  687. Row row = sheet.createRow(rowNumber);
  688. int cellNumber = 0;
  689. Cell cell = row.createCell(cellNumber);
  690. cell.setCellValue("序号");
  691. cell.setCellStyle(cellStyle);
  692. sheet.addMergedRegion(new CellRangeAddress(rowNumber, 1, cellNumber, cellNumber));
  693. sheet.setColumnWidth(cellNumber, 10 * 3 * 256);
  694. cellNumber ++;
  695. cell = row.createCell(cellNumber);
  696. cell.setCellValue("班级");
  697. cell.setCellStyle(cellStyle);
  698. sheet.addMergedRegion(new CellRangeAddress(rowNumber, 1, cellNumber, cellNumber));
  699. sheet.setColumnWidth(cellNumber, 10 * 3 * 256);
  700. cellNumber ++;
  701. cell = row.createCell(cellNumber);
  702. cell.setCellValue("学生姓名");
  703. cell.setCellStyle(cellStyle);
  704. sheet.addMergedRegion(new CellRangeAddress(rowNumber, 1, cellNumber, cellNumber));
  705. sheet.setColumnWidth(cellNumber, 10 * 3 * 256);
  706. cellNumber ++;
  707. cell = row.createCell(cellNumber);
  708. cell.setCellValue("提交时间");
  709. cell.setCellStyle(cellStyle);
  710. sheet.addMergedRegion(new CellRangeAddress(rowNumber, 1, cellNumber, cellNumber));
  711. sheet.setColumnWidth(cellNumber, 10 * 3 * 256);
  712. cellNumber ++;
  713. for (AssessmentPlanQuestion question : oneQuesList) {
  714. int size = towQuesMaps.get(question.getId()).size();
  715. cell = row.createCell(cellNumber);
  716. cell.setCellValue(question.getName());
  717. cell.setCellStyle(cellStyle);
  718. sheet.addMergedRegion(new CellRangeAddress(rowNumber, rowNumber, cellNumber, cellNumber + size - 1));
  719. cellNumber = cellNumber + size;
  720. }
  721. }
  722. void createSecondTitle(Workbook workbook, Sheet sheet, List<AssessmentPlanQuestion> towQuesList) {
  723. int rowNumber = 1;
  724. Font font = workbook.createFont();
  725. font.setBold(true);// 设置为粗体
  726. font.setFontName("宋体");
  727. //font.setColor(IndexedColors.RED.getIndex()); // 设置字体颜色为红色
  728. font.setFontHeightInPoints((short)12);
  729. CellStyle cellStyle = workbook.createCellStyle();
  730. cellStyle.setFont(font); // 将字体应用到样式
  731. cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
  732. cellStyle.setAlignment(HorizontalAlignment.CENTER);
  733. Row row = sheet.createRow(rowNumber);
  734. int cellNumber = 4;
  735. for (AssessmentPlanQuestion question : towQuesList) {
  736. Cell cell = row.createCell(cellNumber);
  737. cell.setCellValue(question.getName());
  738. cell.setCellStyle(cellStyle);
  739. sheet.setColumnWidth(cellNumber, 30 * 3 * 256);
  740. cellNumber ++;
  741. }
  742. }
  743. }