package cn.teammodel.controller.admin.service.impl;

import cn.teammodel.common.ErrorCode;
import cn.teammodel.config.exception.ServiceException;
import cn.teammodel.controller.admin.service.ExamService;
import cn.teammodel.model.dto.admin.exam.*;
import cn.teammodel.model.entity.common.Exam;
import cn.teammodel.model.entity.school.School;
import cn.teammodel.repository.ExamRepository;
import cn.teammodel.repository.SchoolRepository;
import cn.teammodel.utils.GroupUtil;
import com.azure.cosmos.models.PartitionKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Service
public class ExamServiceImpl implements ExamService {

    @Resource
    private ExamRepository examRepository;
    @Resource
    private SchoolRepository schoolRepository;
    @Autowired
    private Environment env;
    @Override
    public Map<String, Object> getAnalysis(OverViewDto overViewDto, HttpServletRequest request) {

        Map<String, Object> overView;
        Map<String, Object> examRate;
        String url = env.getProperty("ies.server-url-overview");
        String url_exam = env.getProperty("ies.server-url-exam-rate");
        try {
            Optional<School> school = schoolRepository.findById(overViewDto.getSchool(), new PartitionKey("Base"));
            overView =  GroupUtil.getGroupId(overViewDto,new GroupUtil(env), request,url);
            examRate = GroupUtil.getGroupId(overViewDto,new GroupUtil(env), request,url_exam);
            //SnowflakeIdGenerator generator = new SnowflakeIdGenerator(1);
            //overView.put("reportId",generator.nextId());
            overView.putAll(examRate);
            school.ifPresent(value -> overView.put("schoolName", value.getName()));
            school.flatMap(value -> value.getPeriod().stream().filter(period ->
                    period.getId().equals(overViewDto.getPeriodId())).findFirst()).ifPresent(value1 ->
                    overView.put("semesters", value1.getSemesters()));
        }catch (Exception e) {
            throw new ServiceException(ErrorCode.SYSTEM_ERROR.getCode(), "数据转换错误");

        }
        return overView;
    }

    @Override
    public Map<String, Object> getExamList(AnalysisDto analysisDto, HttpServletRequest request) {
        Map<String, Object> overView;
        String url = env.getProperty("ies.server-url-exam-analysis-list");
        try {
            overView =  GroupUtil.getGroupId(analysisDto,new GroupUtil(env), request,url);
        }catch (Exception e) {
            throw new ServiceException(ErrorCode.SYSTEM_ERROR.getCode(), "数据转换错误");

        }
        return overView;
    }

    @Override
    public Map<String, Object> getExamSimpleAnalysis(FindExamDto findExamDto, HttpServletRequest request) {
        Map<String, Object> simpleView;
        String url_simple = env.getProperty("ies.server-url-analysis-simple");
        try {
            List<Exam> examList = examRepository.findExamById("Exam-"+findExamDto.getCode(), findExamDto.getId());
            simpleView = GroupUtil.getGroupId(findExamDto,new GroupUtil(env), request,url_simple);
            simpleView.put("examList",examList);
        }catch (Exception e) {
            throw new ServiceException(ErrorCode.SYSTEM_ERROR.getCode(), "数据转换错误");

        }
        return simpleView;
    }

    @Override
    public Map<String, Object> getExamRecord(ExamRecordDto examRecordDto, HttpServletRequest request) {
        Map<String, Object> record;
        String url = env.getProperty("ies.server-url-exam-find-record");
        try {
            record = GroupUtil.getGroupId(examRecordDto,new GroupUtil(env), request,url);
        }catch (Exception e) {
            throw new ServiceException(ErrorCode.SYSTEM_ERROR.getCode(), "数据转换错误");

        }
        return record;
    }

    @Override
    public Map<String, Object> getExamByStudent(FindByStudentDto findByStudentDto, HttpServletRequest request) {
        Map<String, Object> studentView;
        String url = env.getProperty("ies.server-url-find-simple-by-student");
        try {
            studentView = GroupUtil.getGroupId(findByStudentDto,new GroupUtil(env), request,url);
        }catch (Exception e) {
            throw new ServiceException(ErrorCode.SYSTEM_ERROR.getCode(), "数据转换错误");

        }
        return studentView;
    }
}
