package com.keao.edu.user.service.impl;

import com.keao.edu.auth.api.entity.SysUser;
import com.keao.edu.common.dal.BaseDAO;
import com.keao.edu.common.exception.BizException;
import com.keao.edu.common.page.PageInfo;
import com.keao.edu.common.service.impl.BaseServiceImpl;
import com.keao.edu.user.dao.ExaminationBasicDao;
import com.keao.edu.user.dao.StudentExamResultDao;
import com.keao.edu.user.dto.StudentExamResultStatisticsDto;
import com.keao.edu.user.entity.ExaminationBasic;
import com.keao.edu.user.entity.Organization;
import com.keao.edu.user.entity.StudentExamResult;
import com.keao.edu.user.entity.Subject;
import com.keao.edu.user.enums.ExamStatusEnum;
import com.keao.edu.user.page.StudentExamResultQueryInfo;
import com.keao.edu.user.service.OrganizationService;
import com.keao.edu.user.service.StudentExamResultService;
import com.keao.edu.util.collection.MapUtil;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class StudentExamResultServiceImpl extends BaseServiceImpl<Long, StudentExamResult> implements StudentExamResultService {

	@Autowired
	private ExaminationBasicDao examinationBasicDao;
	@Autowired
	private StudentExamResultDao studentExamResultDao;
	@Autowired
	private OrganizationService organizationService;

	@Override
	public BaseDAO<Long, StudentExamResult> getDAO() {
		return studentExamResultDao;
	}

	@Override
	public PageInfo<StudentExamResult> queryStudentExamResult(StudentExamResultQueryInfo queryInfo) {
		PageInfo<StudentExamResult> pageInfo = new PageInfo<>(queryInfo.getPage(), queryInfo.getRows());
		Map<String, Object> params = new HashMap<String, Object>();
		MapUtil.populateMap(params, queryInfo);

		List<Integer> childOrganIds = organizationService.getChildOrganIds(queryInfo.getOrganId(), true);
		params.put("organIds", childOrganIds);

		List<StudentExamResult> dataList = new ArrayList<>();
		int count = this.findCount(params);
		if (count > 0) {
			pageInfo.setTotal(count);
			params.put("offset", pageInfo.getOffset());
			dataList = this.getDAO().queryPage(params);
			List<Integer> studentIds = dataList.stream().map(StudentExamResult::getStudentId).collect(Collectors.toList());
			List<Integer> subjectIds = dataList.stream().map(e -> e.getExamRegistration().getSubjectId()).collect(Collectors.toList());
			List<Integer> organIds = dataList.stream().map(e -> e.getExamRegistration().getOrganId()).collect(Collectors.toList());
			Map<Integer, String> studentIdNameMap = this.getMap("sys_user", "id_", "real_name_", studentIds, Integer.class, String.class);
			Map<Integer, String> subjectIdNameMap = this.getMap("subject", "id_", "name_", subjectIds, Integer.class, String.class);
			Map<Integer, String> organIdNameMap = this.getMap("organization", "id_", "name_", organIds, Integer.class, String.class);
			for (StudentExamResult s : dataList) {
				s.getExamRegistration().setSysUser(new SysUser(s.getStudentId(), studentIdNameMap.get(s.getStudentId())));
				s.getExamRegistration().setSubject(new Subject(s.getExamRegistration().getSubjectId(), subjectIdNameMap.get(s.getExamRegistration().getSubjectId())));
				s.getExamRegistration().setOrganization(new Organization(s.getExamRegistration().getOrganId(), organIdNameMap.get(s.getExamRegistration().getSubjectId())));
			}
		}
		pageInfo.setRows(dataList);
		return pageInfo;
	}

	@Override
	public void updateStudentExamResult(StudentExamResult studentExamResult) {
		if(Objects.isNull(studentExamResult.getId())){
			throw new BizException("请指定考试结果");
		}
		StudentExamResult oldStudentExamResult = studentExamResultDao.get(studentExamResult.getId());
		if(Objects.isNull(oldStudentExamResult)){
			throw new BizException("考试结果不存在");
		}
		ExaminationBasic examinationBasic = examinationBasicDao.get(studentExamResult.getExaminationBasicId().longValue());
		if(Objects.isNull(examinationBasic)){
			throw new BizException("考级项目不存在");
		}
		if(ExamStatusEnum.RESULT_CONFIRM.equals(examinationBasic.getStatus())||ExamStatusEnum.CLOSE.equals(examinationBasic.getStatus())){
			throw new BizException("考试结果不可编辑");
		}
		studentExamResultDao.update(studentExamResult);
	}

	@Override
	public StudentExamResultStatisticsDto getStudentExamResultStatisticsInfo(Integer organId, Integer examId) {
		List<Integer> childOrganIds = organizationService.getChildOrganIds(organId, true);
		StudentExamResultStatisticsDto studentExamResultStatisticsInfo = studentExamResultDao.getStudentExamResultStatisticsInfo(childOrganIds, examId);
		return studentExamResultStatisticsInfo;
	}
}
