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


import com.alibaba.fastjson.JSON;
import com.keao.edu.common.dal.BaseDAO;
import com.keao.edu.common.page.PageInfo;
import com.keao.edu.common.service.impl.BaseServiceImpl;
import com.keao.edu.user.dao.ExamCertificationDao;
import com.keao.edu.user.dto.ExamCertificationDto;
import com.keao.edu.user.entity.ExamCertification;
import com.keao.edu.user.page.ExamCertificationQueryInfo;
import com.keao.edu.user.service.ExamCertificationService;
import com.keao.edu.util.collection.MapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
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;

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

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

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

		List<ExamCertificationDto> dataList = null;
		int count = this.findCount(params);
		if (count > 0) {
			pageInfo.setTotal(count);
			params.put("offset", pageInfo.getOffset());
			dataList = JSON.parseArray(JSON.toJSONString(this.getDAO().queryPage(params)),ExamCertificationDto.class);
			List<Integer> basicIds = dataList.stream().map(e -> e.getExaminationBasicId()).collect(Collectors.toList());
			List<Integer> studentIds = dataList.stream().map(e -> e.getStudentId()).collect(Collectors.toList());
			List<Integer> subjectIds = dataList.stream().map(e -> e.getSubjectId()).collect(Collectors.toList());
			Map<Integer, String> basicNameMap = this.getMap("examination_basic", "id_", "name_", basicIds, Integer.class, String.class);
			Map<Integer, String> subjectNameMap = this.getMap("subject", "id_", "name_", subjectIds, Integer.class, String.class);
			Map<String, Object> paramMap = new HashMap<>();
			paramMap.put("examination_basic_id_",basicIds);
			paramMap.put("student_id_",studentIds);
			Map<Integer, Integer> finishedExamMap = this.getMap("student_exam_result", "examination_basic_id_", "is_finished_exam_", paramMap, Integer.class, Integer.class);
			dataList.forEach(e->{
				e.setSubjectName(subjectNameMap.get(e.getSubjectId()));
				e.setExamBaseName(basicNameMap.get(e.getExaminationBasicId()));
				e.setFinishedExam(finishedExamMap.get(e.getExaminationBasicId()));
			});
		}
		if (count == 0) {
			dataList = new ArrayList<>();
		}
		pageInfo.setRows(dataList);
		return pageInfo;
	}
}