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


import com.keao.edu.auth.api.client.SysUserFeignService;
import com.keao.edu.auth.api.entity.SysUser;
import com.keao.edu.common.dal.BaseDAO;
import com.keao.edu.common.service.impl.BaseServiceImpl;
import com.keao.edu.user.api.entity.Student;
import com.keao.edu.user.dao.*;
import com.keao.edu.user.dto.ExamCertificationDto;
import com.keao.edu.user.dto.NeedCheckingDetailDto;
import com.keao.edu.user.entity.ExamCertification;
import com.keao.edu.user.entity.StudentExamResult;
import com.keao.edu.user.entity.Subject;
import com.keao.edu.user.service.ExamCertificationService;
import com.keao.edu.user.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ExamCertificationServiceImpl extends BaseServiceImpl<Long, ExamCertification> implements ExamCertificationService {
	
	@Autowired
	private ExamCertificationDao examCertificationDao;
	@Autowired
	private ExamRoomStudentRelationDao examRoomStudentRelationDao;
	@Autowired
	private SubjectDao subjectDao;
	@Autowired
	private SysUserFeignService sysUserFeignService;
	@Autowired
	private StudentService studentService;
	@Autowired
	private StudentExamResultDao studentExamResultDao;
	@Autowired
	private SysConfigDao sysConfigDao;

	@Override
	public BaseDAO<Long, ExamCertification> getDAO() {
		return examCertificationDao;
	}

	@Override
	public ExamCertification findByStuAndBasicId(Integer studentId, Long examRegistrationId) {
		return examCertificationDao.findByStuAndBasicId(studentId,examRegistrationId);
	}

	@Override
	public List<ExamCertificationDto> queryCertificationPage(Long examRegistrationId) {
		SysUser sysUser = sysUserFeignService.queryUserInfo();
		List<ExamCertificationDto> dataList = examCertificationDao.queryExamCertificationDtoPage(sysUser.getId(),examRegistrationId);
		if(dataList == null || dataList.size() < 0){
			return dataList;
		}
		List<Integer> subjectIds = dataList.stream().map(e -> e.getSubjectId()).collect(Collectors.toList());
		Map<Integer, String> subjectNameMap = this.getMap("subject", "id_", "name_", subjectIds, Integer.class, String.class);
		Student student = studentService.get(sysUser.getId());
		dataList.forEach(e->{
			e.setSubjectName(subjectNameMap.get(e.getSubjectId()));
			e.setRealName(sysUser.getRealName());
			e.setGender(sysUser.getGender());
			e.setCertificatePhoto(student.getCertificatePhoto());
		});
		return dataList;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public NeedCheckingDetailDto needCheckingDetail(Long examRegistrationId) {
		NeedCheckingDetailDto needCheckingDetailDto = examCertificationDao.needCheckingDetail(examRegistrationId);
		//等待学员数
		String signInTime = needCheckingDetailDto.getSignInTime();
		SysUser sysUser = sysUserFeignService.queryUserInfo();
		Integer waitNum = examRoomStudentRelationDao.sumWaitNum(needCheckingDetailDto.getExamRoomId(),signInTime,sysUser.getId(),needCheckingDetailDto.getExamRoomStudentRelationId());
		needCheckingDetailDto.setWaitNum(waitNum);
		needCheckingDetailDto.setDesc(sysConfigDao.findConfigValue("exam_room_desc"));
		return needCheckingDetailDto;
	}

	@Override
	public ExamCertificationDto findDetailByStudentId(Long examRegistrationId) {
		ExamCertificationDto examCertificationDto = examCertificationDao.getExamCertificationDto(examRegistrationId);
		Subject subject = subjectDao.get(examCertificationDto.getSubjectId());
		examCertificationDto.setSubjectName(subject.getName());
		Student student = studentService.getStudent(examCertificationDto.getStudentId());
		examCertificationDto.setRealName(student.getRealName());
		examCertificationDto.setGender(student.getGender());
		examCertificationDto.setCertificatePhoto(student.getCertificatePhoto());
		StudentExamResult studentExamResult = studentExamResultDao.findByRegistrationId(examRegistrationId);
		examCertificationDto.setConfirmStatus(studentExamResult.getConfirmStatus());
		return examCertificationDto;
	}

	@Override
	public void batchInsert(List<ExamCertification> examCertifications) {
		examCertificationDao.batchInsert(examCertifications);
	}

	@Override
	public int deleteWithRegist(List<Long> registIds) {
		return examCertificationDao.deleteWithRegist(registIds);
	}
}
