xiangpei
2025-05-14 47cd9ecc0eff38ffe6b3b794b2bf197e958f4403
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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;
    }
 
 
    /**
     * 开始考试
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/select/{id}", method = RequestMethod.POST)
    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 -> {
            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()));
    }
}