17808
2023-11-09 f7a47d7de0eaab8c45877659642397fadd2a5dda
成绩统计后端
1个文件已添加
172 ■■■■■ 已修改文件
src/main/java/com/mindskip/xzs/controller/admin/ExamPaperGradeController.java 172 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/mindskip/xzs/controller/admin/ExamPaperGradeController.java
New file
@@ -0,0 +1,172 @@
package com.mindskip.xzs.controller.admin;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mindskip.xzs.base.BaseApiController;
import com.mindskip.xzs.base.RestResponse;
import com.mindskip.xzs.domain.*;
import com.mindskip.xzs.domain.vo.ScoreTemplatesCountVO;
import com.mindskip.xzs.service.*;
import com.mindskip.xzs.viewmodel.admin.paper.ExamPaperGradePageRequestVM;
import org.springframework.beans.factory.annotation.Autowired;
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 java.util.*;
import java.util.stream.Collectors;
@RestController("AdminExamPaperGradeController")
@RequestMapping(value = "/api/admin/examPaperGrade")
public class ExamPaperGradeController extends BaseApiController {
    private final ExamPaperAnswerService examPaperAnswerService;
    private final SubjectService subjectService;
    private final UserService userService;
    private final ExamPaperSubjectService examPaperSubjectService;
    private final ScoreTemplatesUserCountService scoreTemplatesUserCountService;
    private final ExamPaperUserService examPaperUserService;
    private final UserDepartMentService userDepartMentService;
    @Autowired
    public ExamPaperGradeController(ExamPaperAnswerService examPaperAnswerService, SubjectService subjectService, UserService userService, ExamPaperSubjectService examPaperSubjectService, ScoreTemplatesUserCountService scoreTemplatesUserCountService,ExamPaperUserService examPaperUserService,UserDepartMentService userDepartMentService) {
        this.examPaperAnswerService = examPaperAnswerService;
        this.subjectService = subjectService;
        this.userService = userService;
        this.examPaperSubjectService = examPaperSubjectService;
        this.scoreTemplatesUserCountService = scoreTemplatesUserCountService;
        this.examPaperUserService = examPaperUserService;
        this.userDepartMentService = userDepartMentService;
    }
//    @RequestMapping(value = "/page", method = RequestMethod.POST)
//    public RestResponse<PageInfo<ExamPaperAnswerPageResponseVM>> pageJudgeList(@RequestBody ExamPaperGradePageRequestVM grade) {
//        PageInfo<ExamPaperAnswer> pageInfo = examPaperAnswerService.adminPageByGrade(grade);
//        PageInfo<ExamPaperAnswerPageResponseVM> page = PageInfoHelper.copyMap(pageInfo, e -> {
//            ExamPaperAnswerPageResponseVM vm = modelMapper.map(e, ExamPaperAnswerPageResponseVM.class);
//            User user = userService.selectByIdName(e.getCreateUser(), grade.getUserName());
//            if (user == null) {
//                return null;
//            }
//            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(","));
//            }
//           Integer userId = examPaperUserService.getByPaperIdAndCreatUser(e.getExamPaperId(),e.getCreateUser());
//            if(userId == null){
//                return null;
//            }
//            vm.setCreateUser(e.getCreateUser());
//            vm.setUserId(userId);
//            vm.setSubjectName(name);
//            vm.setCreateTime(DateTimeUtil.dateFormat(e.getCreateTime()));
//            vm.setUserName(user.getRealName());
//            return vm;
//        });
//        List<ExamPaperAnswerPageResponseVM> list = page.getList();
//        List<ExamPaperAnswerPageResponseVM> filteredList = list.stream()
//                .filter(exam -> exam != null)
//                .collect(Collectors.toList());
//        ArrayList<ExamPaperAnswerPageResponseVM> list2 = new ArrayList<>();
//        Map<String, Long> collect = filteredList.stream().collect(Collectors.groupingBy(ExamPaperAnswerPageResponseVM::getPaperName, Collectors.counting()));
//        filteredList.forEach(vm -> vm.setCounts(Math.toIntExact(collect.getOrDefault(vm.getPaperName(), 0L))));
//        Map<String, List<ExamPaperAnswerPageResponseVM>> collect1 = filteredList.stream().collect(Collectors.groupingBy(ExamPaperAnswerPageResponseVM::getUserName));
//        List<ExamPaperAnswerPageResponseVM> filteredList1 = new ArrayList<>();
//        Set<String> uniqueUserNames = new HashSet<>();
//        collect1.forEach((key, value) -> {
//            value.forEach(item -> {
//                item.setUserName(key);
//                item.setCounts(value.size());
//            });
//            list2.addAll(value);
//        });
//        for (ExamPaperAnswerPageResponseVM item : list2) {
//            if (uniqueUserNames.add(item.getUserName())) {
//                filteredList1.add(item);
//            }
//        }
//        page.setList(filteredList1);
//        return RestResponse.ok(page);
//    }
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public RestResponse<PageInfo<ExamPaperAnswer>> pageJudgeList(@RequestBody ExamPaperGradePageRequestVM grade) {
        List<ExamPaperAnswer> list = examPaperAnswerService.adminPageByGrade(grade);
        for (ExamPaperAnswer e : list) {
            User user = userService.selectByIdName(e.getCreateUser(), grade.getUserName());
            if (user==null){
                return null;
            }
            ExamPaperAnswer examPaperAnswer = examPaperAnswerService.getById(e.getId());
            Integer[] ids = examPaperSubjectService.getByExamPaperId(examPaperAnswer.getExamPaperId()).stream().map(ExamPaperSubject::getSubjectId).toArray(Integer[]::new);
            String names = "";
            if (ids.length > 0) {
                names = subjectService.selectByIds(ids)
                        .stream().map(Subject::getName).collect(Collectors.joining(","));
            }
            Integer userId = examPaperUserService.getByPaperIdAndCreatUser(e.getExamPaperId(), e.getCreateUser());
            if(userId == null){
                return null;
            }
            e.setUserName(user.getRealName());
        }
        List<ExamPaperAnswer> collect = list.stream()
                .filter(exam -> exam != null)
                .collect(Collectors.toList());
        ArrayList<ExamPaperAnswer> list2 = new ArrayList<>();
        Map<String, Long> collect1 = collect.stream().collect(Collectors.groupingBy(ExamPaperAnswer::getPaperName, Collectors.counting()));
        collect.forEach(t->t.setCounts(Math.toIntExact(collect1.getOrDefault(t.getPaperName(),0L))));
        Map<String, List<ExamPaperAnswer>> collect2 = collect.stream().collect(Collectors.groupingBy(ExamPaperAnswer::getUserName));
        List<ExamPaperAnswer> filteredList1 = new ArrayList<>();
        Set<String> uniqueUserNames = new HashSet<>();
        collect2.forEach((key, value) -> {
            value.forEach(item -> {
                item.setUserName(key);
                item.setCounts(value.size());
            });
            list2.addAll(value);
        });
        for (ExamPaperAnswer item : list2) {
            if (uniqueUserNames.add(item.getUserName())) {
                filteredList1.add(item);
            }
        }
        PageHelper.startPage(grade.getPageIndex(), grade.getPageSize());
        PageInfo<ExamPaperAnswer> pageInfoPageInfo = new PageInfo<>(filteredList1);
        return RestResponse.ok(pageInfoPageInfo);
    }
    @RequestMapping(value = "/details", method = RequestMethod.POST)
    public RestResponse<PageInfo<ExamPaperAnswer>> selectSource(@RequestBody ScoreTemplatesCountVO scoreTemplatesCountVO) throws Exception {
        PageHelper.startPage(scoreTemplatesCountVO.getPageIndex(), scoreTemplatesCountVO.getPageSize());
        List<ExamPaperAnswer> byCreatUser = examPaperAnswerService.getByCreatUser(scoreTemplatesCountVO);
        PageInfo<ExamPaperAnswer> pageInfoPageInfo = new PageInfo<>(byCreatUser);
        return RestResponse.ok(pageInfoPageInfo);
    }
    @RequestMapping(value = "/updates", method = RequestMethod.POST)
    public void updateDepartment(){
        List<User> users = userService.getUsers();
        for (User user : users) {
            Integer id = user.getId();
            Integer userLevel = user.getUserLevel();
            UserDepartment userDepartment = userDepartMentService.selectByUser(id);
            if (userDepartment!=null){
                userDepartment.setDepartmentId(userLevel);
                userDepartMentService.updateUserDepartMent(userDepartment);
            }
        }
    }
}