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.ExamReviewDao;
import com.keao.edu.user.dto.ExamReviewDto;
import com.keao.edu.user.dto.ExamRoomStudentRelationDto;
import com.keao.edu.user.entity.ExamReview;
import com.keao.edu.user.entity.Subject;
import com.keao.edu.user.enums.YesOrNoEnum;
import com.keao.edu.user.page.ExamReviewQueryInfo;
import com.keao.edu.user.service.ExamReviewService;
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 org.springframework.transaction.annotation.Transactional;

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

@Service
public class ExamReviewServiceImpl extends BaseServiceImpl<Long, ExamReview> implements ExamReviewService {
	
	@Autowired
	private ExamReviewDao examReviewDao;
	@Autowired
	private OrganizationServiceImpl organizationService;

	@Override
	public BaseDAO<Long, ExamReview> getDAO() {
		return examReviewDao;
	}

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

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

		List<ExamReviewDto> dataList = null;
		int count = examReviewDao.countExamResult(params);
		if (count > 0) {
			pageInfo.setTotal(count);
			params.put("offset", pageInfo.getOffset());
			dataList = examReviewDao.findExamResult(params);
			List<Integer> studentIds = dataList.stream().map(ExamReviewDto::getStudentId).collect(Collectors.toList());
			List<Integer> subjectIds = dataList.stream().map(e->e.getExamRegistration().getSubjectId()).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);
			for (ExamReviewDto e : dataList) {
				e.setStudentInfo(new SysUser(e.getStudentId(),studentIdNameMap.get(e.getStudentId())));
				e.getExamRegistration().setSubject(new Subject(e.getExamRegistration().getSubjectId(), subjectIdNameMap.get(e.getExamRegistration().getSubjectId())));
			}
		}
		pageInfo.setRows(dataList);
		return pageInfo;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public int update(ExamReview examReview) {
		if(Objects.isNull(examReview.getId())){
			throw new BizException("考试结果不存在");
		}
		ExamReview existExamReview = examReviewDao.get(examReview.getId());
		if(Objects.isNull(existExamReview)){
			throw new BizException("考试结果不存在");
		}
		if(YesOrNoEnum.YES.equals(existExamReview.getEnableEdit())){
			throw new BizException("编辑已锁定");
		}
		return super.update(examReview);
	}
}
