package com.mindskip.xzs.service.impl; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.mindskip.xzs.domain.*; import com.mindskip.xzs.domain.vo.ExamTemplatesVO; import com.mindskip.xzs.repository.*; import com.mindskip.xzs.service.DepartmentService; import com.mindskip.xzs.service.ExamPaperService; import com.mindskip.xzs.service.ExamTemplatesService; import com.mindskip.xzs.service.ExamTemplatesUserCountService; import com.mindskip.xzs.utility.convert.ExamTemplatesClassConvert; import com.mindskip.xzs.viewmodel.admin.exam.ExamPaperEditRequestVM; import com.mindskip.xzs.viewmodel.admin.exam.ExamPaperEditRequestVO; import com.mindskip.xzs.viewmodel.admin.exam.ExamPaperTitleItemVM; import com.mindskip.xzs.viewmodel.admin.exam.QuestionTypeVM; import lombok.extern.slf4j.Slf4j; import org.springframework.format.annotation.DateTimeFormat; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.CollectionUtils; import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; import java.util.List; import java.util.stream.Collectors; @Service @Slf4j public class ExamTemplatesServiceImpl extends BaseServiceImpl implements ExamTemplatesService { private final ExamTemplatesMapper examTemplatesMapper; private final ExamTemplatesQuestionMapper examTemplatesQuestionMapper; private final ExamTemplatesSubjectMapper examTemplatesSubjectMapper; private final DepartmentService departmentService; private final ExamPaperService examPaperService; private final ExamTemplatesUserCountService examTemplatesUserCountService; private final ExamTemplatesUserMapper examTemplatesUserMapper; public ExamTemplatesServiceImpl(BaseMapper baseMapper, ExamTemplatesMapper examTemplatesMapper, ExamTemplatesQuestionMapper examTemplatesQuestionMapper, ExamTemplatesSubjectMapper examTemplatesSubjectMapper, DepartmentService departmentService, ExamPaperService examPaperService, ExamTemplatesUserCountService examTemplatesUserCountService, ExamTemplatesUserMapper examTemplatesUserMapper) { super(baseMapper); this.examTemplatesMapper = examTemplatesMapper; this.examTemplatesQuestionMapper = examTemplatesQuestionMapper; this.examTemplatesSubjectMapper = examTemplatesSubjectMapper; this.departmentService = departmentService; this.examPaperService = examPaperService; this.examTemplatesUserCountService = examTemplatesUserCountService; this.examTemplatesUserMapper = examTemplatesUserMapper; } @Transactional(rollbackFor = Exception.class) @Override public void add(ExamPaperEditRequestVM model) { if (model.getId() != null) { removes(model.getId()); } ExamTemplates examTemplates = ExamTemplatesClassConvert.INSTANCE.ExamPaperEditRequestVMToExamTemplates(model); if (!CollectionUtils.isEmpty(model.getLimitDateTime())) { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date startTime = null; Date endTime = null; try { startTime = dateFormat.parse(model.getLimitDateTime().get(0)); endTime = dateFormat.parse(model.getLimitDateTime().get(1)); } catch (Exception e) { log.info(e.getMessage()); } examTemplates.setStartTime(startTime); examTemplates.setEndTime(endTime); } examTemplates.setCtime(new Date()); examTemplates.setTitleName(model.getTitleItems().get(0).getName()); examTemplates.setStatus(model.getStatus()); examTemplates.setMenuIds(model.getMenuIds()); examTemplatesMapper.add(examTemplates); List examTemplatesQuestions = ExamTemplatesClassConvert.INSTANCE.QuestionTypeVMListToExamTemplatesQuestionList(model.getQuestionTypeVMS()) .stream().map(e -> { e.setTemplatesId(examTemplates.getId()); return e; }).collect(Collectors.toList()); examTemplatesQuestionMapper.saves(examTemplatesQuestions); List subjects = new ArrayList<>(); for (Integer e : model.getSubjectId()) { ExamTemplatesSubject examTemplatesSubject = new ExamTemplatesSubject(); examTemplatesSubject.setSubjectId(e); examTemplatesSubject.setTemplatesId(examTemplates.getId()); subjects.add(examTemplatesSubject); } examTemplatesSubjectMapper.saves(subjects); if ("0".equals(model.getStatus())) { Object[] obj = Arrays.stream(model.getUserIds()).sorted().distinct().toArray(); Integer[] userIds = new Integer[obj.length]; for (int i = 0; i < obj.length; i++) { userIds[i] = (Integer) obj[i]; } for (Integer userId : userIds) { ExamTemplatesUser templatesUser = new ExamTemplatesUser(); templatesUser.setTemplatesId(examTemplates.getId().toString()); templatesUser.setUserId(userId.toString()); examTemplatesUserMapper.add(templatesUser); } } } @Override public List list(ExamTemplatesVO examTemplatesVO) { return null; } @Override public PageInfo gets(ExamTemplatesVO templatesVO) { return PageHelper.startPage(templatesVO.getPageIndex(), templatesVO.getPageSize(), "id desc").doSelectPageInfo(() -> examTemplatesMapper.gets(templatesVO)); } @Override public PageInfo getByadmins(ExamTemplatesVO templatesVO) { return PageHelper.startPage(templatesVO.getPageIndex(), templatesVO.getPageSize(), "id desc").doSelectPageInfo(() -> examTemplatesMapper.getByadmins(templatesVO)); } @Override public ExamPaperEditRequestVM getById(Integer id) { return restore(id); } @Override public ExamTemplates getByName(String name) { return examTemplatesMapper.getByName(name); } @Override public Integer randomExam(User user) throws Exception { ExamTemplatesUserCount count = new ExamTemplatesUserCount(); ExamPaperEditRequestVM vm = restore(null); count.setExamTemplatesId(vm.getId()); Integer[] id = {user.getId()}; vm.setUserIds(id); vm.setType("1"); vm.setId(null); ExamPaper examPaper = examPaperService.savePaperFromVM(vm, user); ExamPaperEditRequestVO newVM = examPaperService.examPaperToVM(examPaper.getId()); count.setExamPaperId(examPaper.getId()); count.setUserId(user.getId()); examTemplatesUserCountService.add(count); return count.getExamPaperId(); } @Override public Integer randomExam1(User user, Integer templatesId) throws Exception { // ExamTemplatesUserCount count = new ExamTemplatesUserCount(); ExamPaperEditRequestVM vm = restore(templatesId); // count.setExamTemplatesId(vm.getId()); Integer[] id = {user.getId()}; vm.setUserIds(id); vm.setType("1"); vm.setId(null); ExamPaper examPaper = examPaperService.savePaperFromVM(vm, user); ExamPaperEditRequestVO newVM = examPaperService.examPaperToVM(examPaper.getId()); // count.setExamPaperId(examPaper.getId()); // count.setUserId(user.getId()); // examTemplatesUserCountService.add(count); return examPaper.getId(); } @Override public void remove(Integer id) { removes(id); } public ExamPaperEditRequestVM restore(Integer id) { if (id == null) { ExamTemplates examTemplates = examTemplatesMapper.getTime(); id = examTemplates.getId(); } ExamTemplates examTemplates = examTemplatesMapper.getById(id); ExamPaperEditRequestVM vm = ExamTemplatesClassConvert.INSTANCE.ExamTemplatesToExamPaperEditRequestVM(examTemplates); vm.setMenuIds(examTemplates.getMenuIds()); List limitDateTime = new ArrayList<>(); Date startTime = examTemplates.getStartTime(); Date endTime = examTemplates.getEndTime(); if (startTime != null && endTime != null) { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String start = dateFormat.format(startTime); String end = dateFormat.format(endTime); limitDateTime.add(start); limitDateTime.add(end); vm.setLimitDateTime(limitDateTime); } Integer[] ids = examTemplatesSubjectMapper.getTemplatesId(id) .stream().map(ExamTemplatesSubject::getSubjectId).toArray(Integer[]::new); vm.setSubjectId(ids); List questions = examTemplatesQuestionMapper.getByTemplatesId(id); List questionTypeVMList = ExamTemplatesClassConvert.INSTANCE.ExamTemplatesQuestionListToQuestionTypeVMList(questions); vm.setQuestionTypeVMS(questionTypeVMList); ExamPaperTitleItemVM examPaperTitleItemVM = new ExamPaperTitleItemVM(); examPaperTitleItemVM.setName(examTemplates.getTitleName()); List list = new ArrayList<>(); list.add(examPaperTitleItemVM); vm.setTitleItems(list); vm.setAggregateSource(100); if ("0".equals(examTemplates.getStatus())) { List byId = examTemplatesUserMapper.getById(id); List collect = byId.stream().map(ExamTemplatesUser::getUserId).collect(Collectors.toList()); Integer[] userIds = new Integer[collect.size()]; for (int i = 0; i < collect.size(); i++) { userIds[i] = Integer.parseInt(collect.get(i)); } vm.setUserIds(userIds); } return vm; } public void removes(Integer id) { examTemplatesMapper.removeById(id); examTemplatesQuestionMapper.removeByTemplatesId(id); examTemplatesSubjectMapper.removeByTemplatesId(id); } }