package com.ym.mec.biz.service.impl;


import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dao.SysMusicScoreAccompanimentDao;
import com.ym.mec.biz.dal.dao.SysMusicScoreDao;
import com.ym.mec.biz.dal.dto.MusicScoreDto;
import com.ym.mec.biz.dal.entity.SysMusicScore;
import com.ym.mec.biz.dal.entity.SysMusicScoreAccompaniment;
import com.ym.mec.biz.service.SysMusicScoreService;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.exception.BizException;
import com.ym.mec.common.service.impl.BaseServiceImpl;
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 SysMusicScoreServiceImpl extends BaseServiceImpl<Integer, SysMusicScore> implements SysMusicScoreService {
	
	@Autowired
	private SysMusicScoreDao sysMusicScoreDao;
	@Autowired
	private SysMusicScoreAccompanimentDao sysMusicScoreAccompanimentDao;
	@Autowired
	private SysUserFeignService sysUserFeignService;

	@Override
	public BaseDAO<Integer, SysMusicScore> getDAO() {
		return sysMusicScoreDao;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMusicScore(MusicScoreDto musicScoreDto) {
		SysMusicScore sysMusicScore = musicScoreDto.getSysMusicScore();
		sysMusicScoreDao.update(sysMusicScore);
		List<Integer> delExamSongAccompanimentIds = musicScoreDto.getDelExamSongAccompanimentIds();
		if(delExamSongAccompanimentIds != null && delExamSongAccompanimentIds.size() > 0){
			sysMusicScoreAccompanimentDao.batchDel(delExamSongAccompanimentIds);
		}
		List<SysMusicScoreAccompaniment> sysMusicScoreAccompaniments = musicScoreDto.getSysMusicScoreAccompaniments();
		if(sysMusicScoreAccompaniments != null && sysMusicScoreAccompaniments.size() > 0){
			List<Integer> subjectIdList = sysMusicScoreAccompaniments.stream().map(e -> e.getSubjectId()).collect(Collectors.toList());
			subjectIdList.removeAll(Collections.singleton(null));
			if(subjectIdList.size() > 0){
				List<Integer> idList = sysMusicScoreAccompaniments.stream().map(e -> e.getId()).collect(Collectors.toList());
				List<Integer> subjectIds = sysMusicScoreAccompanimentDao.findSubjectByMusicScoreId(sysMusicScore.getId(),idList);
				int subjectSize = subjectIds.size() + subjectIdList.size();
				subjectIdList.addAll(subjectIds);
				int size = new HashSet<>(subjectIdList).size();
				if(size < subjectSize){
					throw new BizException("操作失败：请勿提交重复的伴奏声部");
				}
			}
			//获取需要修改的伴奏
			List<SysMusicScoreAccompaniment> updateCollect = sysMusicScoreAccompaniments.stream().filter(e -> e.getId() != null).collect(Collectors.toList());
			if(updateCollect.size() > 0){
				sysMusicScoreAccompanimentDao.batchUpdate(updateCollect);
			}
			//获取需要新增的伴奏
			List<SysMusicScoreAccompaniment> addCollect = sysMusicScoreAccompaniments.stream().filter(e -> e.getId() == null).collect(Collectors.toList());
			if(addCollect.size() > 0){
				sysMusicScoreAccompanimentDao.batchInsert(addCollect,sysMusicScore.getId());
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void add(MusicScoreDto musicScoreDto) {
		SysUser sysUser = sysUserFeignService.queryUserInfo();
		if(sysUser == null){
			throw new BizException("请登录");
		}
		SysMusicScore sysMusicScore = musicScoreDto.getSysMusicScore();
		sysMusicScore.setCreateUserId(sysUser.getId());
		sysMusicScoreDao.insert(sysMusicScore);
		List<SysMusicScoreAccompaniment> sysMusicScoreAccompaniments = musicScoreDto.getSysMusicScoreAccompaniments();
		if(sysMusicScoreAccompaniments != null && sysMusicScoreAccompaniments.size() > 0){
			sysMusicScoreAccompanimentDao.batchInsert(sysMusicScoreAccompaniments,sysMusicScore.getId());
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void del(Integer id) {
		sysMusicScoreDao.delete(id);
		sysMusicScoreAccompanimentDao.deleteBySongId(id);
	}
}