xiangpei
2024-07-09 53dea0ef5fc8b035397b73b10f7a819ebf381b1c
src/main/java/com/mindskip/xzs/controller/admin/ExamPaperAnswerController.java
@@ -1,19 +1,40 @@
package com.mindskip.xzs.controller.admin;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.mindskip.xzs.base.BaseApiController;
import com.mindskip.xzs.base.RestResponse;
import com.mindskip.xzs.domain.ExamPaperAnswer;
import com.mindskip.xzs.domain.ExamPaperSubject;
import com.mindskip.xzs.domain.Subject;
import com.mindskip.xzs.domain.User;
import com.mindskip.xzs.service.*;
import com.mindskip.xzs.domain.exam.ExamPaperAnswerObject;
import com.mindskip.xzs.domain.vo.ExamPaperDataExportVO;
import com.mindskip.xzs.domain.vo.ExamPaperDataVO;
import com.mindskip.xzs.domain.vo.ExamPaperStatisticVO;
import com.mindskip.xzs.service.ExamPaperAnswerService;
import com.mindskip.xzs.service.ExamPaperSubjectService;
import com.mindskip.xzs.service.SubjectService;
import com.mindskip.xzs.service.UserService;
import com.mindskip.xzs.utility.DateTimeUtil;
import com.mindskip.xzs.utility.ExamUtil;
import com.mindskip.xzs.utility.PageInfoHelper;
import com.mindskip.xzs.viewmodel.student.exampaper.ExamPaperAnswerPageResponseVM;
import com.mindskip.xzs.utility.excel.ExcelUtils;
import com.mindskip.xzs.viewmodel.admin.paper.ExamPaperAnswerPageRequestVM;
import com.github.pagehelper.PageInfo;
import com.mindskip.xzs.viewmodel.student.exampaper.ExamPaperAnswerPageResponseVM;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@RestController("AdminExamPaperAnswerController")
@RequestMapping(value = "/api/admin/examPaperAnswer")
@@ -22,12 +43,14 @@
    private final ExamPaperAnswerService examPaperAnswerService;
    private final SubjectService subjectService;
    private final UserService userService;
    private final ExamPaperSubjectService examPaperSubjectService;
    @Autowired
    public ExamPaperAnswerController(ExamPaperAnswerService examPaperAnswerService, SubjectService subjectService, UserService userService) {
    public ExamPaperAnswerController(ExamPaperAnswerService examPaperAnswerService, SubjectService subjectService, UserService userService, ExamPaperSubjectService examPaperSubjectService) {
        this.examPaperAnswerService = examPaperAnswerService;
        this.subjectService = subjectService;
        this.userService = userService;
        this.examPaperSubjectService = examPaperSubjectService;
    }
@@ -35,22 +58,90 @@
    public RestResponse<PageInfo<ExamPaperAnswerPageResponseVM>> pageJudgeList(@RequestBody ExamPaperAnswerPageRequestVM model) {
        PageInfo<ExamPaperAnswer> pageInfo = examPaperAnswerService.adminPage(model);
        PageInfo<ExamPaperAnswerPageResponseVM> page = PageInfoHelper.copyMap(pageInfo, e -> {
            User user = userService.selectById(e.getCreateUser());
//            if(user.getUserName().)
            ExamPaperAnswerPageResponseVM vm = modelMapper.map(e, ExamPaperAnswerPageResponseVM.class);
            Subject subject = subjectService.selectById(vm.getSubjectId());
//            User user = userService.selectByIdName(e.getCreateUser(), model.getUserName());
//            if (user == null) {
//                return null;
//            }
//            Subject subject = subjectService.selectById(vm.getSubjectId());
            ExamPaperAnswer examPaperAnswer = examPaperAnswerService.getById(vm.getId());
            Integer[] ids = examPaperSubjectService.getByExamPaperId(examPaperAnswer.getExamPaperId())
                    .stream().map(ExamPaperSubject::getSubjectId).toArray(Integer[]::new);
            String name = "";
            if (ids.length > 0) {
                name = subjectService.selectByIds(ids)
                        .stream().map(Subject::getName).collect(Collectors.joining(","));
            }
            vm.setDoTime(ExamUtil.secondToVM(e.getDoTime()));
            vm.setSystemScore(ExamUtil.scoreToVM(e.getSystemScore()));
            vm.setUserScore(ExamUtil.scoreToVM(e.getUserScore()));
            vm.setPaperScore(ExamUtil.scoreToVM(e.getPaperScore()));
            vm.setSubjectName(subject.getName());
            vm.setSubjectName(name);
            vm.setCreateTime(DateTimeUtil.dateFormat(e.getCreateTime()));
            vm.setUserName(user.getUserName());
            return vm;
        });
        if (page.getList().size() > 0) {
            BigDecimal sum = page.getList().stream()
                    .map(ExamPaperAnswerPageResponseVM -> new BigDecimal(ExamPaperAnswerPageResponseVM.getUserScore()))
                    .reduce(BigDecimal.ZERO, BigDecimal::add); // 计算总和
            Double avg = sum.divide(BigDecimal.valueOf(page.getList().size()), 2, RoundingMode.HALF_UP).doubleValue();
            page.getList().get(0).setAvgSource(avg);
        }
        return RestResponse.ok(page);
    }
    @RequestMapping(value = "/paperStatistics", method = RequestMethod.POST)
    public RestResponse<ExamPaperAnswerObject> paperStatistics(@RequestBody @Valid String paperName) {
        ExamPaperAnswerObject object = new ExamPaperAnswerObject();
        String str = JSONObject.parseObject(paperName).get("paperName").toString();
        List<ExamPaperAnswer> examPaperAnswers = examPaperAnswerService.selectByPaperName(str)
                .stream().map(e -> {
                    e.setUserScore(e.getUserScore() / 10);
                    return e;
                }).collect(Collectors.toList());
        if (examPaperAnswers.size() == 0) {
            object.setAdvanced(0);
            object.setAvg(0.0);
            object.setIntermediate(0);
            object.setMax(0);
            object.setMin(0);
            object.setLowGrade(0);
        } else {
            Integer lowGrade = examPaperAnswers.stream().filter(e -> e.getUserScore() < 60 && 0 <= e.getUserScore()).collect(Collectors.toList()).size();
            Integer intermediate = examPaperAnswers.stream().filter(e -> e.getUserScore() < 80 && 60 <= e.getUserScore()).collect(Collectors.toList()).size();
            Integer advanced = examPaperAnswers.stream().filter(e -> e.getUserScore() <= 100 && 80 <= e.getUserScore()).collect(Collectors.toList()).size();
            Integer max = examPaperAnswers.stream().map(ExamPaperAnswer::getUserScore).max(Integer::compareTo).get();
            Integer min = examPaperAnswers.stream().map(ExamPaperAnswer::getUserScore).min(Integer::compareTo).get();
            Double avg = examPaperAnswers.stream().mapToDouble(ExamPaperAnswer::getUserScore).average().getAsDouble();
            BigDecimal two = new BigDecimal(avg);
            Double three = two.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
            object.setAdvanced(advanced);
            object.setAvg(three);
            object.setIntermediate(intermediate);
            object.setMax(max);
            object.setMin(min);
            object.setLowGrade(lowGrade);
        }
        return RestResponse.ok(object);
    }
    @RequestMapping(value = "/statistic", method = RequestMethod.POST)
    public RestResponse<Map<String, Object>> statistic(@RequestBody ExamPaperStatisticVO examPaperStatisticVO) {
        examPaperStatisticVO.setDepartmentId(ObjectUtils.isNotEmpty(examPaperStatisticVO.getDepartmentId()) ? examPaperStatisticVO.getDepartmentId() : getAdminDeptIds());
        return RestResponse.ok(examPaperAnswerService.statistic(examPaperStatisticVO));
    }
    @RequestMapping(value = "/data", method = RequestMethod.POST)
    public RestResponse<Map<String, Object>> data(@RequestBody ExamPaperDataVO examPaperDataVO) {
        return RestResponse.ok(examPaperAnswerService.data(examPaperDataVO));
    }
    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public void export(Integer id, Integer type, HttpServletResponse response) {
        List<ExamPaperDataExportVO> list =  examPaperAnswerService.dataExport(new ExamPaperDataVO().setId(id).setType(type));
        ExcelUtils.export(response, "分数统计", list, ExamPaperDataExportVO.class);
    }
}