package com.mindskip.xzs.controller.student; import com.mindskip.xzs.base.BaseApiController; import com.mindskip.xzs.base.RestResponse; import com.mindskip.xzs.domain.ExamPaper; import com.mindskip.xzs.domain.ExamPaperDepartment; import com.mindskip.xzs.domain.ExamPaperUser; import com.mindskip.xzs.domain.User; import com.mindskip.xzs.service.ExamPaperAnswerService; import com.mindskip.xzs.service.ExamPaperDepartmentService; import com.mindskip.xzs.service.ExamPaperService; import com.mindskip.xzs.service.ExamPaperUserService; import com.mindskip.xzs.utility.DateTimeUtil; import com.mindskip.xzs.utility.PageInfoHelper; import com.mindskip.xzs.viewmodel.admin.exam.ExamPaperEditRequestVM; import com.mindskip.xzs.viewmodel.admin.exam.ExamPaperEditRequestVO; import com.mindskip.xzs.viewmodel.student.exam.ExamPaperPageResponseVM; import com.mindskip.xzs.viewmodel.student.exam.ExamPaperPageVM; import com.github.pagehelper.PageInfo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationEventPublisher; import org.springframework.web.bind.annotation.*; import javax.validation.Valid; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; @RestController("StudentExamPaperController") @RequestMapping(value = "/api/student/exam/paper") public class ExamPaperController extends BaseApiController { private final ExamPaperService examPaperService; private final ExamPaperAnswerService examPaperAnswerService; private final ApplicationEventPublisher eventPublisher; private final ExamPaperUserService examPaperUserService; private final ExamPaperDepartmentService examPaperDepartmentService; @Autowired public ExamPaperController(ExamPaperService examPaperService, ExamPaperAnswerService examPaperAnswerService, ApplicationEventPublisher eventPublisher, ExamPaperUserService examPaperUserService, ExamPaperDepartmentService examPaperDepartmentService) { this.examPaperService = examPaperService; this.examPaperAnswerService = examPaperAnswerService; this.eventPublisher = eventPublisher; this.examPaperUserService = examPaperUserService; this.examPaperDepartmentService = examPaperDepartmentService; } @RequestMapping(value = "/select/{id}", method = RequestMethod.POST) public RestResponse select(@PathVariable Integer id) { return RestResponse.ok(examPaperService.examPaperToVM(id)); } @RequestMapping(value = "/pageList", method = RequestMethod.POST) public RestResponse> pageList(@RequestBody @Valid ExamPaperPageVM model) { PageInfo pageInfo = examPaperService.studentPage(model); User user = getCurrentUser(); PageInfo page = PageInfoHelper.copyMap(pageInfo, e -> { Integer[] ids = examPaperDepartmentService.getByExamPaperId(e.getId()) .stream().map(ExamPaperDepartment::getDepartmentId).toArray(Integer[]::new); Integer[] userExamPaperIds = examPaperUserService.getByExamPaperId(e.getId()) .stream().map(ExamPaperUser::getUserId).toArray(Integer[]::new); if (Arrays.asList(ids).contains(user.getUserLevel())) { ExamPaperPageResponseVM vm = modelMapper.map(e, ExamPaperPageResponseVM.class); vm.setCreateTime(DateTimeUtil.dateFormat(e.getCreateTime())); return vm; } if (Arrays.asList(userExamPaperIds).contains(user.getId())) { ExamPaperPageResponseVM vm = modelMapper.map(e, ExamPaperPageResponseVM.class); vm.setCreateTime(DateTimeUtil.dateFormat(e.getCreateTime())); return vm; } return null; }); Integer[] userIds = examPaperUserService.getByUserId(user.getId()) .stream().map(ExamPaperUser::getExamPaperId).toArray(Integer[]::new); if (userIds.length > 0) { List papers = examPaperService.gets(userIds); pageInfo.getList().addAll(papers); } page.setList(page.getList().stream().filter(e -> e != null).collect(Collectors.toList())); return RestResponse.ok(page); } @RequestMapping(value = "/edit", method = RequestMethod.POST) public RestResponse edit(@RequestBody @Valid ExamPaperEditRequestVM model) throws Exception { ExamPaperUser examPaperUser = new ExamPaperUser(); User user = getCurrentUser(); model.setDepartmentIds(new Integer[0]); model.setPaperType(7); ExamPaper examPaper = examPaperService.savePaperFromVM(model, getCurrentUser()); examPaperUser.setExamPaperId(examPaper.getId()); examPaperUser.setUserId(user.getId()); examPaperUser.setDeleted("0"); examPaperUserService.add(examPaperUser); return RestResponse.ok(examPaperService.examPaperToVM(examPaper.getId())); } }