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.service.SysOrgService;
import com.qmth.teachcloud.common.util.ServletUtil;
import com.qmth.teachcloud.report.business.bean.result.TAExamCourseTeacherResult;
import com.qmth.teachcloud.report.business.bean.result.TeacherClassRankResult;
import com.qmth.teachcloud.report.business.bean.result.TeacherMyClassRankResult;
import com.qmth.teachcloud.report.business.entity.TAExamCourseCollegeTeacher;
import com.qmth.teachcloud.report.business.enums.OrderEnum;
import com.qmth.teachcloud.report.business.enums.SemesterEnum;
import com.qmth.teachcloud.report.business.mapper.TAExamCourseCollegeTeacherMapper;
import com.qmth.teachcloud.report.business.service.TAExamCourseCollegeTeacherService;
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;

    @Override
    public IPage<TeacherClassRankResult> listClassRank(SemesterEnum semester, 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, Objects.nonNull(semester) ? semester.name() : null, examId, courseCode, inspectCollegeId, collegeIds, column, Objects.nonNull(order) ? order.name() : null);
        } else {
            return taExamCourseCollegeTeacherMapper.listClassRank(page, schoolId, Objects.nonNull(semester) ? semester.name() : null, examId, courseCode, inspectCollegeId, Objects.nonNull(sysUser.getOrgId()) ? Arrays.asList(sysUser.getOrgId()) : null, column, Objects.nonNull(order) ? order.name() : null);
        }
    }

    @Override
    public IPage<TeacherMyClassRankResult> listMyClassRank(SemesterEnum semester, 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);
        IPage<TeacherMyClassRankResult> listPage = taExamCourseCollegeTeacherMapper.listMyClassRank(page, schoolId, Objects.nonNull(semester) ? semester.name() : null, examId, sysUser.getId(), courseCode, inspectCollegeId, column, Objects.nonNull(order) ? order.name() : null);
        return listPage;
    }

    @Override
    public IPage<TeacherClassRankResult> listAllTeacherClassRank(SemesterEnum semester, 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, Objects.nonNull(semester) ? semester.name() : null, examId, teachCollegeId, courseCode, teacherId, collegeIds, column, Objects.nonNull(order) ? order.name() : null);
        } else {
            return taExamCourseCollegeTeacherMapper.listAllTeacherClassRank(page, schoolId, Objects.nonNull(semester) ? semester.name() : null, 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(SemesterEnum semester, 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, Objects.nonNull(semester) ? semester.name() : null, examId, courseCode, column, Objects.nonNull(order) ? order.name() : null);
    }
}
