package com.qmth.teachcloud.report.business.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qmth.teachcloud.common.entity.SysUser;
import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
import com.qmth.teachcloud.common.service.SysOrgService;
import com.qmth.teachcloud.common.util.ServletUtil;
import com.qmth.teachcloud.report.business.bean.result.*;
import com.qmth.teachcloud.report.business.entity.TAExamCourseCollegeTeacher;
import com.qmth.teachcloud.report.business.enums.OrderEnum;
import com.qmth.teachcloud.report.business.mapper.TAExamCourseCollegeTeacherMapper;
import com.qmth.teachcloud.report.business.service.ReportCommonService;
import com.qmth.teachcloud.report.business.service.TAExamCourseCollegeInspectService;
import com.qmth.teachcloud.report.business.service.TAExamCourseCollegeTeacherService;
import com.qmth.teachcloud.report.business.service.TAExamCourseRecordService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 参考课程学院下授课教师维度分析表 服务实现类
 * </p>
 *
 * @author wangliang
 * @since 2021-06-01
 */
@Service
public class TAExamCourseCollegeTeacherServiceImpl extends ServiceImpl<TAExamCourseCollegeTeacherMapper, TAExamCourseCollegeTeacher> implements TAExamCourseCollegeTeacherService {

    @Resource
    private TAExamCourseCollegeTeacherMapper taExamCourseCollegeTeacherMapper;

    @Resource
    SysOrgService sysOrgService;

    @Resource
    ReportCommonService reportCommonService;

    @Resource
    TAExamCourseCollegeInspectService taExamCourseCollegeInspectService;

    @Resource
    TAExamCourseRecordService taExamCourseRecordService;

    @Override
    public IPage<TeacherClassRankResult> listClassRank(Long semesterId, Long examId, String courseCode, Long inspectCollegeId, Integer pageNumber, Integer pageSize, String column, OrderEnum order) {
        Long schoolId = Long.valueOf(ServletUtil.getRequestHeaderSchoolId().toString());
        Page<TeacherClassRankResult> page = new Page<>(pageNumber, pageSize);
        SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
        List<Long> collegeIds = sysOrgService.findCollegeIds(sysUser.getOrgId());
        if (Objects.nonNull(collegeIds) && collegeIds.size() > 0) {
            return taExamCourseCollegeTeacherMapper.listClassRank(page, schoolId, semesterId, examId, courseCode, inspectCollegeId, collegeIds, column, Objects.nonNull(order) ? order.name() : null);
        } else {
            return taExamCourseCollegeTeacherMapper.listClassRank(page, schoolId, semesterId, examId, courseCode, inspectCollegeId, Objects.nonNull(sysUser.getOrgId()) ? Arrays.asList(sysUser.getOrgId()) : null, column, Objects.nonNull(order) ? order.name() : null);
        }
    }

    @Override
    public IPage<TeacherMyClassRankResult> listMyClassRank(Long semesterId, Long examId, String courseCode, Long inspectCollegeId, Integer pageNumber, Integer pageSize, String column, OrderEnum order) {
        Long schoolId = Long.valueOf(ServletUtil.getRequestHeaderSchoolId().toString());
        SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
        Page<TeacherMyClassRankResult> page = new Page<>(pageNumber, pageSize);
        return taExamCourseCollegeTeacherMapper.listMyClassRank(page, schoolId, semesterId, examId, sysUser.getId(), courseCode, inspectCollegeId, column, Objects.nonNull(order) ? order.name() : null);
    }

    @Override
    public IPage<TeacherClassRankResult> listAllTeacherClassRank(Long semesterId, Long examId, Long teachCollegeId, String courseCode, Long teacherId, Integer pageNumber, Integer pageSize, String column, OrderEnum order) {
        Long schoolId = Long.valueOf(ServletUtil.getRequestHeaderSchoolId().toString());
        Page<TeacherClassRankResult> page = new Page<>(pageNumber, pageSize);
        SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
        List<Long> collegeIds = sysOrgService.findCollegeIds(sysUser.getOrgId());
        if (Objects.nonNull(collegeIds) && collegeIds.size() > 0) {
            return taExamCourseCollegeTeacherMapper.listAllTeacherClassRank(page, schoolId, semesterId, examId, teachCollegeId, courseCode, teacherId, collegeIds, column, Objects.nonNull(order) ? order.name() : null);
        } else {
            return taExamCourseCollegeTeacherMapper.listAllTeacherClassRank(page, schoolId, semesterId, examId, teachCollegeId, courseCode, teacherId, Objects.nonNull(sysUser.getOrgId()) ? Arrays.asList(sysUser.getOrgId()) : null, column, Objects.nonNull(order) ? order.name() : null);
        }
    }

    /**
     * 查询老师得分
     *
     * @param examId
     * @param courseCode
     * @param collegeId
     * @return
     */
    @Override
    public List<TAExamCourseTeacherResult> findTeacherInfo(Long examId, String courseCode, Long collegeId) {
        return taExamCourseCollegeTeacherMapper.findTeacherInfo(examId, courseCode, collegeId);
    }

    @Override
    public IPage<TeacherClassRankResult> listTeacherClassRank(Long semesterId, Long examId, String courseCode, Integer pageNumber, Integer pageSize, String column, OrderEnum order) {
        Long schoolId = Long.valueOf(ServletUtil.getRequestHeaderSchoolId().toString());
        Page<TeacherClassRankResult> page = new Page<>(pageNumber, pageSize);
        return taExamCourseCollegeTeacherMapper.listTeacherClassRank(page, schoolId, semesterId, examId, courseCode, column, Objects.nonNull(order) ? order.name() : null);
    }

    /**
     * 我的课堂考试分析-图表接口
     *
     * @param schoolId
     * @param semesterId
     * @param examId
     * @param courseCode
     * @param inspectCollegeId
     * @return
     */
    @Override
    public SurveyTeacherViewResult teacherView(Long schoolId, Long semesterId, Long examId, String courseCode, Long inspectCollegeId) {
        SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
        if (Objects.isNull(sysUser)) {
            throw ExceptionResultEnum.NOT_LOGIN.exception();
        }
        // 考试概况
        TAExamCourseCollegeInspectResult taExamCourseCollegeInspectResult = taExamCourseCollegeInspectService.findTeacherInspect(sysUser.getSchoolId(), semesterId, examId, courseCode, inspectCollegeId);
        TeachCourseResult teachCourseResultFinal = null;
        if (Objects.nonNull(taExamCourseCollegeInspectResult)) {
            teachCourseResultFinal = new TeachCourseResult(taExamCourseCollegeInspectResult);
        } else {
            teachCourseResultFinal = new TeachCourseResult();
        }
        List<TeachCourseResult> teachCourseResultList = taExamCourseRecordService.findTeacherInspect(sysUser.getSchoolId(), semesterId, examId, courseCode, inspectCollegeId, sysUser.getId());
        if (Objects.nonNull(teachCourseResultList) && teachCourseResultList.size() > 0) {
            for (TeachCourseResult t : teachCourseResultList) {
                if (t.getAbsent()) {
                    teachCourseResultFinal.setCourseAbsentCount(t.getAbsentCount());
                } else if (!t.getAbsent()) {
                    teachCourseResultFinal.setCourseRealityCount(t.getAbsentCount());
                }
            }
        }
        //取总体和应届平均分
        SurveyTeacherExamCourseResult surveyTeacherExamCourseResult = reportCommonService.findAvgScore(schoolId, courseCode, examId, false);
        //学院学科报表科目描述
        List<SurveyTeacherGradeDistributionResult> surveyTeacherGradeDistributionResultList = reportCommonService.surveyTeacherDistribution(examId, courseCode, inspectCollegeId, false);
        //查找题目信息(该教师在该学院下的数据)
        QuestionListResult questionListResult = reportCommonService.findQuestionInfo(examId, courseCode, null, inspectCollegeId, sysUser.getId());
        return new SurveyTeacherViewResult(surveyTeacherGradeDistributionResultList, teachCourseResultFinal, questionListResult, surveyTeacherExamCourseResult);
    }

    /**
     * 我的课堂考试分析-试题难度得分情况分析接口
     *
     * @param schoolId
     * @param semesterId
     * @param examId
     * @param courseCode
     * @param collegeId
     * @return
     */
    @Override
    public DimensionAnalyzeResult teacherQuestionView(Long schoolId, Long semesterId, Long examId, String courseCode, Long collegeId, Long teacherId) {
        return reportCommonService.findDimensionInfo(examId, courseCode, collegeId, teacherId);
    }

    /**
     * 我的课堂考试分析-各难度水平题目上的作答分析接口
     *
     * @param schoolId
     * @param semesterId
     * @param examId
     * @param courseCode
     * @param collegeId
     * @return
     */
    @Override
    public QuestionInfoResult teacherQuestionAnswerView(Long schoolId, Long semesterId, Long examId, String courseCode, Long collegeId, Long teacherId) {
        return reportCommonService.findSituationOfQuestions(examId, courseCode, collegeId, teacherId);
    }
}
