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


import com.ym.mec.auth.api.dto.MusicScoreQueryInfo;
import com.ym.mec.biz.dal.dao.SysMusicScoreAccompanimentDao;
import com.ym.mec.biz.dal.entity.Subject;
import com.ym.mec.biz.dal.entity.SysMusicScore;
import com.ym.mec.biz.dal.entity.SysMusicScoreAccompaniment;
import com.ym.mec.biz.dal.entity.SysMusicScoreCategories;
import com.ym.mec.biz.dal.page.SysExamSongQueryInfo;
import com.ym.mec.biz.service.SysMusicScoreAccompanimentService;
import com.ym.mec.biz.service.SysMusicScoreCategoriesService;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.exception.BizException;
import com.ym.mec.common.page.PageInfo;
import com.ym.mec.common.service.impl.BaseServiceImpl;
import com.ym.mec.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 SysMusicScoreAccompanimentServiceImpl extends BaseServiceImpl<Integer, SysMusicScoreAccompaniment> implements SysMusicScoreAccompanimentService {
	
	@Autowired
	private SysMusicScoreAccompanimentDao sysMusicScoreAccompanimentDao;
	@Autowired
	private SysMusicScoreCategoriesService sysMusicScoreCategoriesService;

	@Override
	public BaseDAO<Integer, SysMusicScoreAccompaniment> getDAO() {
		return sysMusicScoreAccompanimentDao;
	}

	@Override
	public void updateAcc(SysMusicScoreAccompaniment sysMusicScoreAccompaniment) {
		SysMusicScoreAccompaniment accompaniment = sysMusicScoreAccompanimentDao.get(sysMusicScoreAccompaniment.getId());
		if(accompaniment == null){
			throw new BizException("操作失败：伴奏信息不存在");
		}
		List<Integer> idList = new ArrayList<>();
		idList.add(accompaniment.getId());
		List<Integer> subjectIds = sysMusicScoreAccompanimentDao.findSubjectByMusicScoreId(accompaniment.getExamSongId(),idList);
		if(subjectIds.size() > 0 && subjectIds.contains(accompaniment.getId())){
			throw new BizException("操作失败：请勿提交重复的伴奏声部");
		}
		sysMusicScoreAccompanimentDao.update(sysMusicScoreAccompaniment);
	}

	@Override
	public List<SysMusicScoreAccompaniment> queryAll(SysExamSongQueryInfo queryInfo) {
		Map<String, Object> params = new HashMap<>();
		MapUtil.populateMap(params, queryInfo);
		return sysMusicScoreAccompanimentDao.queryPage(params);
	}

	@Override
	public List<SysMusicScoreAccompaniment> queryAccPage(SysExamSongQueryInfo queryInfo) {
		if(queryInfo.getCategoriesId() != null){
			List<Integer> categoriesIdList = new ArrayList<>();
			MusicScoreQueryInfo musicScoreQueryInfo = new MusicScoreQueryInfo();
			musicScoreQueryInfo.setParentId(queryInfo.getCategoriesId());
			List<SysMusicScoreCategories> scoreCategories = sysMusicScoreCategoriesService.queryTree(musicScoreQueryInfo);
			if(scoreCategories.size() > 0){
				categoriesIdList = scoreCategories.stream().map(e->e.getId()).collect(Collectors.toList());
			}else {
				categoriesIdList.add(queryInfo.getCategoriesId());
			}
			queryInfo.setCategoriesIdList(categoriesIdList);
		}
		Map<String, Object> params = new HashMap<>();
		MapUtil.populateMap(params, queryInfo);
		return sysMusicScoreAccompanimentDao.queryAccPage(params);
	}

	@Override
	public List<Subject> querySubjectIds() {
		return sysMusicScoreAccompanimentDao.querySubjectIds();
	}

    @Override
    public PageInfo<SysMusicScoreAccompaniment> queryScorePage(SysExamSongQueryInfo queryInfo) {
		if(queryInfo.getCategoriesId() != null){
			List<Integer> categoriesIdList = new ArrayList<>();
			MusicScoreQueryInfo musicScoreQueryInfo = new MusicScoreQueryInfo();
			musicScoreQueryInfo.setParentId(queryInfo.getCategoriesId());
			List<SysMusicScoreCategories> scoreCategories = sysMusicScoreCategoriesService.queryTree(musicScoreQueryInfo);
			if(scoreCategories.size() > 0){
				categoriesIdList = scoreCategories.stream().map(e->e.getId()).collect(Collectors.toList());
			}else {
				categoriesIdList.add(queryInfo.getCategoriesId());
			}
			queryInfo.setCategoriesIdList(categoriesIdList);
		}
		return this.queryPage(queryInfo);
    }
}