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.OverViewDto;
import cn.teammodel.repository.ExamRepository;
import cn.teammodel.utils.GroupUtil;
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 {

    @Autowired
    private Environment env;
    @Resource
    private ExamRepository examRepository;
    @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 {
            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);
            //获取所有考试数据
            //List<Exam> exams = examRepository.findExamByClassId("Exam-"+overViewDto.getSchool(),overViewDto.getClassId(),overViewDto.getPeriodId());

            //List<RMember> rMembers = new ArrayList<>();
            /*for (Map.Entry<String, Object> entry : groupId.entrySet()) {
                String key = entry.getKey();
                Object value = entry.getValue();
                if (key.equals("groups")) {
                    String jsonGroups = JSON.toJSONString(value);
                    rGroupList = JSON.parseObject(jsonGroups, new TypeReference<List<RGroupList>>() {});
                }
            }*/
        }catch (Exception e) {
            throw new ServiceException(ErrorCode.SYSTEM_ERROR.getCode(), "数据转换错误");

        }
        return overView;
    }
}
