xiangpei
2025-05-14 47cd9ecc0eff38ffe6b3b794b2bf197e958f4403
src/main/java/com/mindskip/xzs/controller/student/ExamPaperController.java
@@ -3,11 +3,17 @@
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;
@@ -16,6 +22,9 @@
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")
@@ -24,30 +33,76 @@
    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) {
    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;
    }
    /**
     * 开始考试
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/select/{id}", method = RequestMethod.POST)
    public RestResponse<ExamPaperEditRequestVM> select(@PathVariable Integer id) {
        ExamPaperEditRequestVM vm = examPaperService.examPaperToVM(id);
        return RestResponse.ok(vm);
    public RestResponse<ExamPaperEditRequestVO> select(@PathVariable Integer id) {
        return RestResponse.ok(examPaperService.examPaperToVM(id));
    }
    @RequestMapping(value = "/pageList", method = RequestMethod.POST)
    public RestResponse<PageInfo<ExamPaperPageResponseVM>> pageList(@RequestBody @Valid ExamPaperPageVM model) {
        PageInfo<ExamPaper> pageInfo = examPaperService.studentPage(model);
        User user = getCurrentUser();
        PageInfo<ExamPaperPageResponseVM> page = PageInfoHelper.copyMap(pageInfo, e -> {
            ExamPaperPageResponseVM vm = modelMapper.map(e, ExamPaperPageResponseVM.class);
            vm.setCreateTime(DateTimeUtil.dateFormat(e.getCreateTime()));
            return vm;
            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<ExamPaper> 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<ExamPaperEditRequestVO> 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()));
    }
}