StudentExamResultServiceImpl.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. package com.keao.edu.user.service.impl;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.keao.edu.auth.api.client.SysUserFeignService;
  4. import com.keao.edu.auth.api.entity.SysUser;
  5. import com.keao.edu.common.dal.BaseDAO;
  6. import com.keao.edu.common.enums.MessageTypeEnum;
  7. import com.keao.edu.common.exception.BizException;
  8. import com.keao.edu.common.page.PageInfo;
  9. import com.keao.edu.common.service.SysMessageService;
  10. import com.keao.edu.common.service.impl.BaseServiceImpl;
  11. import com.keao.edu.im.api.client.ImFeignService;
  12. import com.keao.edu.im.api.entity.MemberChangedMessage;
  13. import com.keao.edu.im.api.entity.PublishMessageDto;
  14. import com.keao.edu.thirdparty.message.provider.JiguangPushPlugin;
  15. import com.keao.edu.user.api.entity.ExamRoom;
  16. import com.keao.edu.user.api.entity.ExamRoomStudentRelation;
  17. import com.keao.edu.user.api.entity.Student;
  18. import com.keao.edu.user.api.enums.StudentExamResultApiDto;
  19. import com.keao.edu.user.dao.ExaminationBasicDao;
  20. import com.keao.edu.user.dao.StudentExamResultDao;
  21. import com.keao.edu.user.dto.ExaminationBasicDto;
  22. import com.keao.edu.user.dto.RecordNotify;
  23. import com.keao.edu.user.dto.RoomStudentListDto;
  24. import com.keao.edu.user.dto.StudentExamResultStatisticsDto;
  25. import com.keao.edu.user.entity.ExaminationBasic;
  26. import com.keao.edu.user.entity.Organization;
  27. import com.keao.edu.user.entity.StudentExamResult;
  28. import com.keao.edu.user.entity.Subject;
  29. import com.keao.edu.user.enums.ExamStatusEnum;
  30. import com.keao.edu.user.enums.LevelEnum;
  31. import com.keao.edu.user.page.StudentExamResultQueryInfo;
  32. import com.keao.edu.user.service.ExamRoomService;
  33. import com.keao.edu.user.service.ExamRoomStudentRelationService;
  34. import com.keao.edu.user.service.OrganizationService;
  35. import com.keao.edu.user.service.StudentExamResultService;
  36. import com.keao.edu.util.collection.MapUtil;
  37. import org.apache.commons.lang3.StringUtils;
  38. import org.slf4j.Logger;
  39. import org.slf4j.LoggerFactory;
  40. import org.springframework.beans.factory.annotation.Autowired;
  41. import org.springframework.stereotype.Service;
  42. import org.springframework.transaction.annotation.Transactional;
  43. import java.util.*;
  44. import java.util.stream.Collectors;
  45. @Service
  46. public class StudentExamResultServiceImpl extends BaseServiceImpl<Long, StudentExamResult> implements StudentExamResultService {
  47. @Autowired
  48. private ExaminationBasicDao examinationBasicDao;
  49. @Autowired
  50. private StudentExamResultDao studentExamResultDao;
  51. @Autowired
  52. private ExamRoomStudentRelationService examRoomStudentRelationService;
  53. @Autowired
  54. private OrganizationService organizationService;
  55. @Autowired
  56. private SysMessageService sysMessageService;
  57. @Autowired
  58. private SysUserFeignService sysUserFeignService;
  59. @Autowired
  60. private ImFeignService imFeignService;
  61. @Autowired
  62. private ExamRoomService examRoomService;
  63. private final static Logger logger = LoggerFactory.getLogger(StudentExamResultServiceImpl.class);
  64. @Override
  65. public BaseDAO<Long, StudentExamResult> getDAO() {
  66. return studentExamResultDao;
  67. }
  68. @Override
  69. public PageInfo<StudentExamResult> queryStudentExamResult(StudentExamResultQueryInfo queryInfo) {
  70. PageInfo<StudentExamResult> pageInfo = new PageInfo<>(queryInfo.getPage(), queryInfo.getRows());
  71. Map<String, Object> params = new HashMap<String, Object>();
  72. MapUtil.populateMap(params, queryInfo);
  73. List<Integer> childOrganIds = organizationService.getChildOrganIds(queryInfo.getOrganId(), true);
  74. params.put("organIds", childOrganIds);
  75. List<StudentExamResult> dataList = new ArrayList<>();
  76. int count = studentExamResultDao.countStudentExamResult(params);
  77. if (count > 0) {
  78. pageInfo.setTotal(count);
  79. params.put("offset", pageInfo.getOffset());
  80. dataList = studentExamResultDao.queryStudentExamResult(params);
  81. List<Long> examIds = dataList.stream().map(StudentExamResult::getExaminationBasicId).collect(Collectors.toList());
  82. List<Integer> studentIds = dataList.stream().map(StudentExamResult::getStudentId).collect(Collectors.toList());
  83. List<Integer> subjectIds = dataList.stream().map(e -> e.getExamRegistration().getSubjectId()).collect(Collectors.toList());
  84. List<Integer> organIds = dataList.stream().map(e -> e.getExamRegistration().getOrganId()).collect(Collectors.toList());
  85. Map<Integer, String> studentIdNameMap = this.getMap("sys_user", "id_", "real_name_", studentIds, Integer.class, String.class);
  86. Map<Integer, String> subjectIdNameMap = this.getMap("subject", "id_", "name_", subjectIds, Integer.class, String.class);
  87. Map<Integer, String> organIdNameMap = this.getMap("organization", "id_", "name_", organIds, Integer.class, String.class);
  88. List<ExaminationBasicDto> exams = examinationBasicDao.getExams(examIds);
  89. Map<Long, ExaminationBasicDto> idExamMap = exams.stream().collect(Collectors.toMap(ExaminationBasic::getId, e -> e));
  90. for (StudentExamResult s : dataList) {
  91. Student student=new Student(s.getStudentId(), studentIdNameMap.get(s.getStudentId()));
  92. s.setExaminationBasic(idExamMap.get(s.getExaminationBasicId()));
  93. s.getExamRegistration().setSysUser(student);
  94. s.getExamRegistration().setSubject(new Subject(s.getExamRegistration().getSubjectId(), subjectIdNameMap.get(s.getExamRegistration().getSubjectId())));
  95. s.getExamRegistration().setOrganization(new Organization(s.getExamRegistration().getOrganId(), organIdNameMap.get(s.getExamRegistration().getOrganId())));
  96. }
  97. }
  98. pageInfo.setRows(dataList);
  99. return pageInfo;
  100. }
  101. @Override
  102. public void updateStudentExamResult(StudentExamResult studentExamResult) {
  103. if(Objects.isNull(studentExamResult.getId())){
  104. throw new BizException("请指定考试结果");
  105. }
  106. StudentExamResult oldStudentExamResult = studentExamResultDao.get(studentExamResult.getId());
  107. if(Objects.isNull(oldStudentExamResult)){
  108. throw new BizException("考试结果不存在");
  109. }
  110. ExaminationBasic examinationBasic = examinationBasicDao.get(studentExamResult.getExaminationBasicId().longValue());
  111. if(Objects.isNull(examinationBasic)){
  112. throw new BizException("考级项目不存在");
  113. }
  114. if(ExamStatusEnum.RESULT_CONFIRM.equals(examinationBasic.getStatus())||ExamStatusEnum.CLOSE.equals(examinationBasic.getStatus())){
  115. throw new BizException("考试结果不可编辑");
  116. }
  117. studentExamResultDao.update(studentExamResult);
  118. }
  119. @Override
  120. @Transactional(rollbackFor = Exception.class)
  121. public void examResultConfirmPush(Long examId) {
  122. if(Objects.isNull(examId)){
  123. return;
  124. }
  125. List<StudentExamResult> studentExamResults = studentExamResultDao.getWithExam(examId);
  126. Set<Long> examIds = studentExamResults.stream().map(StudentExamResult::getExaminationBasicId).collect(Collectors.toSet());
  127. Map<Long, String> examIdNameMap = this.getMap("examination_basic", "id_", "name_", new ArrayList(examIds), Long.class, String.class);
  128. Set<Subject> subjectIds = studentExamResults.stream().map(e -> e.getExamRegistration().getSubject()).collect(Collectors.toSet());
  129. Map<Integer, String> subjectIdNameMap = this.getMap("subject", "id_", "name_", new ArrayList(subjectIds), Integer.class, String.class);
  130. String baseUrl = "4?";
  131. for (StudentExamResult studentExamResult : studentExamResults) {
  132. String examName = examIdNameMap.get(studentExamResult.getExaminationBasicId());
  133. String subjectName = subjectIdNameMap.get(studentExamResult.getExamRegistration().getSubjectId());
  134. String level = LevelEnum.getMsg(studentExamResult.getExamRegistration().getLevel());
  135. Map<Integer, String> userPhoneMap = new HashMap<>();
  136. userPhoneMap.put(studentExamResult.getStudentId(), studentExamResult.getStudentId().toString());
  137. sysMessageService.batchSendMessage(MessageTypeEnum.EXAM_RESULT_CONFIRM_PUSH,
  138. userPhoneMap, null, 0, baseUrl, JiguangPushPlugin.PLUGIN_NAME,
  139. examName, subjectName, level, studentExamResult.getResult().getMsg());
  140. }
  141. }
  142. @Override
  143. public StudentExamResultStatisticsDto getStudentExamResultStatisticsInfo(Integer organId, Integer examId) {
  144. if(Objects.isNull(examId)){
  145. throw new BizException("请指定考级项目");
  146. }
  147. ExaminationBasic examinationBasic = examinationBasicDao.get(examId.longValue());
  148. if(Objects.isNull(examinationBasic)){
  149. throw new BizException("考级项目不存在");
  150. }
  151. if(!ExamStatusEnum.EXAM_END.equals(examinationBasic.getStatus())&&!ExamStatusEnum.RESULT_CONFIRM.equals(examinationBasic.getStatus())){
  152. return null;
  153. }
  154. List<Integer> childOrganIds = organizationService.getChildOrganIds(organId, true);
  155. StudentExamResultStatisticsDto studentExamResultStatisticsInfo = studentExamResultDao.getStudentExamResultStatisticsInfo(childOrganIds, examId);
  156. return studentExamResultStatisticsInfo;
  157. }
  158. @Override
  159. public StudentExamResultApiDto getStudentExamResultApiDto(Long id) {
  160. StudentExamResultApiDto resultApiDto = studentExamResultDao.getStudentExamResultApiDto(id);
  161. ExamRoomStudentRelation studentExamRoom = examRoomStudentRelationService.getStudentExamRoom(id);
  162. resultApiDto.setRoomId(studentExamRoom.getExamRoomId().toString());
  163. return resultApiDto;
  164. }
  165. @Override
  166. public void confirmStudent(Long examRegistrationId) {
  167. studentExamResultDao.confirmStudent(examRegistrationId);
  168. }
  169. @Override
  170. @Transactional(rollbackFor = Exception.class)
  171. public void updateFinishedExam(Long examRegistrationId, Integer finishedExam) {
  172. studentExamResultDao.updateFinishedExam(examRegistrationId,finishedExam);
  173. }
  174. @Override
  175. @Transactional(rollbackFor = Exception.class)
  176. public void recordSync(RecordNotify recordNotify) {
  177. logger.info("recordSync paramJson{}",recordNotify);
  178. if (recordNotify.getType() == 4){
  179. JSONObject jsonObject = JSONObject.parseObject(recordNotify.getOutput());
  180. if("10000".equals(jsonObject.get("fileState"))){
  181. StudentExamResult studentExamResult = studentExamResultDao.findByRoomIdAndUserId(recordNotify.getRoomId(), recordNotify.getUserId());
  182. String videoUrl = studentExamResult.getVideoUrl();
  183. if(StringUtils.isNotEmpty(videoUrl)){
  184. videoUrl += "," + jsonObject.get("fileUrl");
  185. }else {
  186. videoUrl = jsonObject.get("fileUrl").toString();
  187. }
  188. studentExamResult.setVideoUrl(videoUrl);
  189. studentExamResultDao.update(studentExamResult);
  190. }
  191. }
  192. }
  193. @Override
  194. public void updateSessionId(Long examRegistrationId, String sessionId) {
  195. studentExamResultDao.updateSessionId(examRegistrationId,sessionId);
  196. }
  197. @Override
  198. @Transactional(rollbackFor = Exception.class)
  199. public void shieldUserId(Long roomId,Integer shieldFlag) {
  200. ExamRoom examRoom = examRoomService.get(roomId);
  201. SysUser sysUser = sysUserFeignService.queryUserInfo();
  202. String userToString = sysUser.getId().toString();
  203. String shieldUserId = examRoom.getShieldUserId();
  204. //取消屏蔽
  205. if(shieldFlag == 0){
  206. if(StringUtils.isNotEmpty(shieldUserId)){
  207. if(shieldUserId.contains("," + userToString)){
  208. shieldUserId = shieldUserId.replace("," + userToString,"");
  209. }else if(shieldUserId.contains(userToString + ",")){
  210. shieldUserId = shieldUserId.replace(userToString + ",","");
  211. }else {
  212. shieldUserId = "";
  213. }
  214. }else {
  215. shieldUserId = "";
  216. }
  217. }else {
  218. if(StringUtils.isNotEmpty(shieldUserId)){
  219. if(!shieldUserId.contains(userToString)) {
  220. shieldUserId += "," + userToString;
  221. }
  222. }else {
  223. shieldUserId = userToString;
  224. }
  225. }
  226. examRoom.setShieldUserId(shieldUserId);
  227. examRoomService.update(examRoom);
  228. PublishMessageDto publishMessageDto = new PublishMessageDto();
  229. publishMessageDto.setUserId(userToString);
  230. publishMessageDto.setRoomId(roomId.toString());
  231. MemberChangedMessage msg = new MemberChangedMessage(MemberChangedMessage.Shield_UserId, userToString,3);
  232. msg.setShieldUserId(shieldUserId);
  233. publishMessageDto.setMemberChangedMessage(msg);
  234. imFeignService.publishMessage(publishMessageDto);
  235. }
  236. }