package com.qmth.teachcloud.mark.service.impl;

import com.qmth.teachcloud.common.bean.dto.CampusExamRoomDto;
import com.qmth.teachcloud.common.bean.dto.CoursePaperNumberDto;
import com.qmth.teachcloud.common.bean.dto.DataPermissionRule;
import com.qmth.teachcloud.common.bean.dto.SemesterExamDto;
import com.qmth.teachcloud.common.entity.BasicExam;
import com.qmth.teachcloud.common.entity.BasicSemester;
import com.qmth.teachcloud.common.entity.SysUser;
import com.qmth.teachcloud.common.enums.mark.MarkPaperStatus;
import com.qmth.teachcloud.common.service.BasicRoleDataPermissionService;
import com.qmth.teachcloud.common.service.BasicSemesterService;
import com.qmth.teachcloud.common.util.ServletUtil;
import com.qmth.teachcloud.mark.entity.MarkPaper;
import com.qmth.teachcloud.mark.entity.MarkStudent;
import com.qmth.teachcloud.mark.service.MarkPaperService;
import com.qmth.teachcloud.mark.service.MarkStudentService;
import com.qmth.teachcloud.mark.service.ScanConditionService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 查询条件 服务实现类
 * </p>
 */
@Service
public class ScanConditionServiceImpl implements ScanConditionService {

    @Resource
    private BasicSemesterService basicSemesterService;
    @Resource
    private MarkPaperService markPaperService;
    @Resource
    private MarkStudentService markStudentService;
    @Resource
    private BasicRoleDataPermissionService basicRoleDataPermissionService;

    @Override
    public List<SemesterExamDto> scanSemesterList(Boolean enable) {
        Long schoolId = Long.valueOf(ServletUtil.getRequestHeaderSchoolId().toString());
        List<BasicSemester> semesterList = basicSemesterService.list(schoolId, enable);
        List<SemesterExamDto> list = new ArrayList<>();
        for (BasicSemester basicSemester : semesterList) {
            SemesterExamDto semesterExamDto = new SemesterExamDto();
            semesterExamDto.setId(String.valueOf(basicSemester.getId()));
            semesterExamDto.setName(basicSemester.getName());
            semesterExamDto.setEnable(basicSemester.getEnable());

            List<BasicExam> examList = basicSemesterService.listBySemesterId(schoolId, basicSemester.getId());
            if (CollectionUtils.isNotEmpty(examList)) {
                List<SemesterExamDto.ExamDto> examDtoList = new ArrayList<>();
                for (BasicExam basicExam : examList) {
                    SemesterExamDto.ExamDto examDto = new SemesterExamDto.ExamDto();
                    examDto.setId(String.valueOf(basicExam.getId()));
                    examDto.setName(basicExam.getName());
                    examDto.setEnable(basicExam.getEnable());
                    examDtoList.add(examDto);
                }
                semesterExamDto.setExamList(examDtoList);
                list.add(semesterExamDto);
            }
        }
        return list;
    }

    @Override
    public List<CoursePaperNumberDto> scanCourseList(Long examId) {
        SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
        DataPermissionRule dpr = basicRoleDataPermissionService.findDataPermission(sysUser.getSchoolId(), sysUser.getId(), ServletUtil.getRequest().getServletPath());
        List<MarkPaper> markPaperList = markPaperService.listByExamId(examId, MarkPaperStatus.FORMAL, dpr);
        Map<String, List<MarkPaper>> map = markPaperList.stream().collect(Collectors.groupingBy(m -> m.getCourseCode()));
        List<CoursePaperNumberDto> coursePaperNumberDtoList = new ArrayList<>();
        for (Map.Entry<String, List<MarkPaper>> entry : map.entrySet()) {
            CoursePaperNumberDto coursePaperNumberDto = new CoursePaperNumberDto();
            coursePaperNumberDto.setCode(entry.getKey());
            coursePaperNumberDto.setName(entry.getValue().get(0).getCourseName());
            List<CoursePaperNumberDto.PaperNumberDto> paperNumberDtoList = new ArrayList<>();
            for (MarkPaper markPaper : entry.getValue()) {
                CoursePaperNumberDto.PaperNumberDto paperNumberDto = new CoursePaperNumberDto.PaperNumberDto();
                paperNumberDto.setCoursePaperNumber(markPaper.getPaperNumber());
                paperNumberDto.setCoursePaperId(markPaper.getCoursePaperId());
                paperNumberDto.setMarkFinished(markPaper.getStatus() != null && markPaper.getStatus().equals(MarkPaperStatus.FINISH));
                paperNumberDtoList.add(paperNumberDto);
            }
            coursePaperNumberDto.setPaperNumberList(paperNumberDtoList);
            coursePaperNumberDtoList.add(coursePaperNumberDto);
        }
        return coursePaperNumberDtoList;
    }

    @Override
    public List<CampusExamRoomDto> scanCampusList(Long examId, String courseCode, String coursePaperId) {
        SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
        DataPermissionRule dpr = basicRoleDataPermissionService.findDataPermission(sysUser.getSchoolId(), sysUser.getId(), ServletUtil.getRequest().getServletPath());
        List<MarkStudent> markStudentList = markStudentService.listScanCollegeByExamIdAndCourseCodeAndCoursePaperId(examId, courseCode, coursePaperId, MarkPaperStatus.FORMAL.name(), dpr);
        Map<String, List<MarkStudent>> map = markStudentList.stream().filter(m -> StringUtils.isNotBlank(m.getExamPlace())).collect(Collectors.groupingBy(x -> x.getExamPlace()));
        List<CampusExamRoomDto> campusExamRoomDtoList = new ArrayList<>();
        for (Map.Entry<String, List<MarkStudent>> entry : map.entrySet()) {
            CampusExamRoomDto campusExamRoomDto = new CampusExamRoomDto();
            campusExamRoomDto.setName(entry.getKey());
            campusExamRoomDto.setExamRoomList(entry.getValue().stream().filter(m -> StringUtils.isNotBlank(m.getExamRoom())).map(MarkStudent::getExamRoom).distinct().collect(Collectors.toList()));
            campusExamRoomDtoList.add(campusExamRoomDto);
        }
        return campusExamRoomDtoList;
    }
}
