龚焕茏
2024-05-07 49429bad1036c81c056faeadfa009c53ba777fad
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
109
110
111
112
113
114
115
package com.mindskip.xzs.controller.admin;
 
import com.mindskip.xzs.base.BaseApiController;
import com.mindskip.xzs.base.RestResponse;
import com.mindskip.xzs.domain.ExamPaper;
import com.mindskip.xzs.domain.ExamPaperSubject;
import com.mindskip.xzs.domain.vo.PaperExcelVO;
import com.mindskip.xzs.domain.vo.UserCountExcelVO;
import com.mindskip.xzs.service.ExamPaperDepartmentService;
import com.mindskip.xzs.service.ExamPaperService;
import com.mindskip.xzs.service.ExamPaperSubjectService;
import com.mindskip.xzs.service.ExamTemplatesUserCountService;
import com.mindskip.xzs.utility.DateTimeUtil;
import com.mindskip.xzs.utility.PageInfoHelper;
import com.mindskip.xzs.utility.excel.ExcelUtils;
import com.mindskip.xzs.viewmodel.admin.exam.ExamPaperEditRequestVO;
import com.mindskip.xzs.viewmodel.admin.exam.ExamPaperPageRequestVM;
import com.mindskip.xzs.viewmodel.admin.exam.ExamPaperEditRequestVM;
import com.mindskip.xzs.viewmodel.admin.exam.ExamResponseVM;
import com.github.pagehelper.PageInfo;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
 
@RestController("AdminExamPaperController")
@RequestMapping(value = "/api/admin/exam/paper")
public class ExamPaperController extends BaseApiController {
 
    private final ExamPaperService examPaperService;
    private final ExamPaperSubjectService examPaperSubjectService;
    private final ExamPaperDepartmentService examPaperDepartmentService;
    private final ExamTemplatesUserCountService examTemplatesUserCountService;
 
    @Autowired
    public ExamPaperController(ExamPaperService examPaperService, ExamPaperSubjectService examPaperSubjectService, ExamPaperDepartmentService examPaperDepartmentService, ExamTemplatesUserCountService examTemplatesUserCountService) {
        this.examPaperService = examPaperService;
        this.examPaperSubjectService = examPaperSubjectService;
        this.examPaperDepartmentService = examPaperDepartmentService;
        this.examTemplatesUserCountService = examTemplatesUserCountService;
    }
 
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public RestResponse<PageInfo<ExamResponseVM>> pageList(@RequestBody ExamPaperPageRequestVM model) {
        model.setType("0");
        PageInfo<ExamPaper> pageInfo = examPaperService.page(model);
        PageInfo<ExamResponseVM> page = PageInfoHelper.copyMap(pageInfo, e -> {
            ExamResponseVM vm = modelMapper.map(e, ExamResponseVM.class);
            vm.setCreateTime(DateTimeUtil.dateFormat(e.getCreateTime()));
            vm.setSubjectId(examPaperSubjectService.getByExamPaperId(vm.getId())
                    .stream().map(ExamPaperSubject::getSubjectId).toArray(Integer[]::new));
            return vm;
        });
        return RestResponse.ok(page);
    }
 
 
 
    @RequestMapping(value = "/taskExamPage", method = RequestMethod.POST)
    public RestResponse<PageInfo<ExamResponseVM>> taskExamPageList(@RequestBody ExamPaperPageRequestVM model) {
        PageInfo<ExamPaper> pageInfo = examPaperService.taskExamPage(model);
        PageInfo<ExamResponseVM> page = PageInfoHelper.copyMap(pageInfo, e -> {
            ExamResponseVM vm = modelMapper.map(e, ExamResponseVM.class);
            vm.setCreateTime(DateTimeUtil.dateFormat(e.getCreateTime()));
            Integer[] ids = examPaperSubjectService.getByExamPaperId(vm.getId())
                    .stream().map(ExamPaperSubject::getSubjectId).toArray(Integer[]::new);
            vm.setSubjectId(ids);
            return vm;
        });
        return RestResponse.ok(page);
    }
 
 
 
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public RestResponse<ExamPaperEditRequestVO> edit(@RequestBody @Valid ExamPaperEditRequestVM model) throws Exception {
        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];
        }
        model.setUserIds(userIds);
        ExamPaper examPaper = examPaperService.savePaperFromVM(model, getCurrentUser());
        ExamPaperEditRequestVO newVM = examPaperService.examPaperToVM(examPaper.getId());
        return RestResponse.ok(newVM);
    }
 
    @RequestMapping(value = "/select/{id}", method = RequestMethod.POST)
    public RestResponse<ExamPaperEditRequestVO> select(@PathVariable Integer id) {
        ExamPaperEditRequestVO vm = examPaperService.examPaperToVM(id);
        return RestResponse.ok(vm);
    }
 
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    public RestResponse delete(@PathVariable Integer id) {
        ExamPaper examPaper = examPaperService.selectById(id);
        examPaper.setDeleted(true);
        examPaperService.updateByIdFilter(examPaper);
        examPaperDepartmentService.removeByExamPaperId(id);
        examPaperSubjectService.removeByExamPaperId(id);
        return RestResponse.ok();
    }
 
    @RequestMapping(value = "/exportTemplatesId/{id}", method = RequestMethod.GET)
    @SneakyThrows
    public void exportTemplatesId(@PathVariable("id") Integer id, HttpServletResponse response, HttpServletRequest request) {
        List<PaperExcelVO> list = examPaperService.getPaperExcelById(id);
        ExcelUtils.export(response,"个人练习",list,PaperExcelVO.class);
    }
}