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.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.common.entity.SysUser;
import com.qmth.teachcloud.common.service.SysOrgService;
import com.qmth.teachcloud.common.util.ExcelUtil;
import com.qmth.teachcloud.common.util.JacksonUtil;
import com.qmth.teachcloud.common.util.ServletUtil;
import com.qmth.teachcloud.report.business.bean.dto.excel.TAExamCourseDto;
import com.qmth.teachcloud.report.business.entity.TAExamCourse;
import com.qmth.teachcloud.report.business.entity.TAExamTotal;
import com.qmth.teachcloud.report.business.enums.OrderEnum;
import com.qmth.teachcloud.report.business.mapper.TAExamTotalMapper;
import com.qmth.teachcloud.report.business.service.TAExamCourseService;
import com.qmth.teachcloud.report.business.service.TAExamTotalService;
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 TAExamTotalServiceImpl extends ServiceImpl<TAExamTotalMapper, TAExamTotal> implements TAExamTotalService {

    @Resource
    TAExamTotalMapper taExamTotalMapper;

    @Resource
    SysOrgService sysOrgService;

    @Resource
    TAExamCourseService taExamCourseService;

    @Override
    public TAExamTotal getOverview(Long semesterId, Long examId) {
        Long schoolId = Long.valueOf(ServletUtil.getRequestHeaderSchoolId().toString());
        return taExamTotalMapper.getOverview(schoolId, semesterId, examId);
    }

    @Override
    public IPage<TAExamCourse> getCourseSummary(Long semesterId, Long examId, Integer pageNumber, Integer pageSize, String column, OrderEnum order) {
        Long schoolId = Long.valueOf(ServletUtil.getRequestHeaderSchoolId().toString());
        Page<TAExamCourse> 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 taExamTotalMapper.getCourseSummary(page, schoolId, semesterId, examId, collegeIds, column, Objects.nonNull(order) ? order.name() : null);
        } else {
            return taExamTotalMapper.getCourseSummary(page, schoolId, semesterId, examId, Objects.nonNull(sysUser.getOrgId()) ? Arrays.asList(sysUser.getOrgId()) : null, column, Objects.nonNull(order) ? order.name() : null);
        }
    }

    @Override
    public void exportCourseSummary(Long semesterId, Long examId, String column, OrderEnum order) throws Exception {
        IPage<TAExamCourse> taExamCourseIPage = this.getCourseSummary(semesterId, examId, SystemConstant.PAGE_NUMBER, SystemConstant.PAGE_SIZE, column, order);
        Gson gson = new Gson();
        List<TAExamCourseDto> examStudentCourseDtoList = gson.fromJson(JacksonUtil.parseJson(taExamCourseIPage.getRecords()), new TypeToken<List<TAExamCourseDto>>() {
        }.getType());
        ExcelUtil.excelExport("课程分析", TAExamCourseDto.class, examStudentCourseDtoList, ServletUtil.getResponse());
    }

    @Override
    public void exportDataAnalysis(Long semesterId, Long examId, String courseCode) throws Exception {
        SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
        taExamCourseService.courseExport(examId, semesterId, Objects.nonNull(sysUser.getSchoolId()) ? sysUser.getSchoolId() : null, courseCode);
    }
}
