xiangpei
2024-07-02 a6a3bb04cdaa334a2868d09b20518ed98575bccb
src/main/java/com/mindskip/xzs/controller/admin/ExamPaperAnswerController.java
@@ -1,27 +1,39 @@
package com.mindskip.xzs.controller.admin;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.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.domain.exam.ExamPaperAnswerObject;
import com.mindskip.xzs.service.*;
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")
@@ -48,10 +60,10 @@
        PageInfo<ExamPaperAnswerPageResponseVM> page = PageInfoHelper.copyMap(pageInfo, e -> {
            ExamPaperAnswerPageResponseVM vm = modelMapper.map(e, ExamPaperAnswerPageResponseVM.class);
            User user = userService.selectByIdName(e.getCreateUser(), model.getUserName());
            if (user == null) {
                return null;
            }
//            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())
@@ -68,12 +80,13 @@
            vm.setSubjectName(name);
            vm.setCreateTime(DateTimeUtil.dateFormat(e.getCreateTime()));
            vm.setUserName(user.getUserName());
            return vm;
        });
        page.setList(page.getList().stream().filter(e -> e != null).collect(Collectors.toList()));
        if (page.getSize() > 0) {
            Double avg = page.getList().stream().mapToInt(ExamPaperAnswerPageResponseVM -> Integer.parseInt(ExamPaperAnswerPageResponseVM.getUserScore())).average().getAsDouble();
        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);
@@ -102,8 +115,10 @@
            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(avg);
            object.setAvg(three);
            object.setIntermediate(intermediate);
            object.setMax(max);
            object.setMin(min);
@@ -111,4 +126,22 @@
        }
        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);
    }
}