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

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.page.QueryInfo;
import com.keao.edu.common.service.impl.BaseServiceImpl;
import com.keao.edu.common.tenant.TenantContextHolder;
import com.keao.edu.user.dao.ExamSongDao;
import com.keao.edu.user.dao.ExamSubjectDao;
import com.keao.edu.user.dao.ExamSubjectSongDao;
import com.keao.edu.user.dao.ExaminationBasicDao;
import com.keao.edu.user.entity.ExamSong;
import com.keao.edu.user.entity.ExamSubject;
import com.keao.edu.user.entity.ExamSubjectSong;
import com.keao.edu.user.entity.ExaminationBasic;
import com.keao.edu.user.enums.ExamStatusEnum;
import com.keao.edu.user.service.ExamSubjectSongService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

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

@Service
public class ExamSubjectSongServiceImpl extends BaseServiceImpl<Long, ExamSubjectSong> implements ExamSubjectSongService {

	@Autowired
	private ExaminationBasicDao examinationBasicDao;
	@Autowired
	private ExamSubjectSongDao examSubjectSongDao;
	@Autowired
	private ExamSubjectDao examSubjectDao;
	@Autowired
	private ExamSongDao examSongDao;

	@Override
	public BaseDAO<Long, ExamSubjectSong> getDAO() {
		return examSubjectSongDao;
	}

	@Override
	@Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
	public void addExamSubjects(List<ExamSubjectSong> examSubjectSongs) {
		if(CollectionUtils.isEmpty(examSubjectSongs)){
			return;
		}
		ExaminationBasic examinationBasic = examinationBasicDao.get(examSubjectSongs.get(0).getExaminationBasicId().longValue());
		if(Objects.isNull(examinationBasic)){
			throw new BizException("考级项目不存在");
		}
		if(!ExamStatusEnum.SETTING.equals(examinationBasic.getStatus())){
			throw new BizException("此状态考级项目不支持添加考级内容");
		}

		List<ExamSubjectSong> existExamSubjectSongs=examSubjectSongDao.findByExam(examSubjectSongs.get(0).getExaminationBasicId());
		Map<Long, List<Integer>> subjectLevelMap = new HashMap<>();
		Set<Long> existSubjectIds = new HashSet<>();
		if(!CollectionUtils.isEmpty(existExamSubjectSongs)){
			subjectLevelMap = existExamSubjectSongs.stream().collect(Collectors.groupingBy(ExamSubjectSong::getExamSubjectId, Collectors.mapping(ExamSubjectSong::getLevel, Collectors.toList())));
			existSubjectIds = existExamSubjectSongs.stream().map(ExamSubjectSong::getExamSubjectId).collect(Collectors.toSet());
		}

		List<ExamSubject> newExamSubjects = new ArrayList<>();
		for (ExamSubjectSong examSubjectSong : examSubjectSongs) {
			if(Objects.isNull(examSubjectSong.getExaminationBasicId())){
				throw new BizException("请指定考级项目");
			}
			if(Objects.isNull(examSubjectSong.getExamSubjectId())){
				throw new BizException("请选择专业");
			}
			if(Objects.isNull(examSubjectSong.getLevel())){
				throw new BizException("请指定报考等级");
			}
			List<Integer> levels = subjectLevelMap.get(examSubjectSong.getExamSubjectId());
			if(!CollectionUtils.isEmpty(levels)&&levels.contains(examSubjectSong.getLevel())){
				throw new BizException("考级内容等级重复");
			}
			if(Objects.isNull(examSubjectSong.getRegistrationFee())){
				throw new BizException("请指定报名费用");
			}
			if(existSubjectIds.contains(examSubjectSong.getExamSubjectId().longValue())){
				continue;
			}

			examSubjectSong.setTenantId(TenantContextHolder.getTenantId().toString());

			ExamSubject newExamSubject=new ExamSubject();
			newExamSubject.setExaminationBasicId(examSubjectSong.getExaminationBasicId());
			newExamSubject.setTenantId(examSubjectSong.getTenantId());
			newExamSubject.setSubjectId(examSubjectSong.getExamSubjectId().intValue());
			newExamSubject.setTenantId(TenantContextHolder.getTenantId().toString());
			newExamSubjects.add(newExamSubject);
			existSubjectIds.add(newExamSubject.getSubjectId().longValue());
		}
		examSubjectSongDao.batchInsert(examSubjectSongs);
		if(!CollectionUtils.isEmpty(newExamSubjects)){
			examSubjectDao.batchInsert(newExamSubjects);
		}
	}

    @Override
    public List<ExamSong> getExamSubjectSong(Integer examinationBasicId,Long examSubjectId, Integer level) {
		List<ExamSong> examSongs = new ArrayList<>();
		ExamSubjectSong examSubjectSong = examSubjectSongDao.getExamSubjectSong(examinationBasicId,examSubjectId, level);
		if(examSubjectSong == null){
			return examSongs;
		}
		List<ExamSong> practiceSong = examSongDao.getExamSongs(examSubjectSong.getPracticeSongIdList());
		if(practiceSong != null){
			examSongs.addAll(practiceSong);
		}
		List<ExamSong> performSong = examSongDao.getExamSongs(examSubjectSong.getPerformSongIdList());
		if(performSong != null){
			examSongs.addAll(performSong);
		}
		return examSongs;
	}

    @Override
    public List<ExamSubjectSong> getExamSubjectLevels(Integer examinationBasicId, Long examSubjectId) {
		return examSubjectSongDao.getExamSubjectLevels(examinationBasicId,examSubjectId);
    }
}
