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


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

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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.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;

@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();
			Integer categoriesId = null;
			if(StringUtils.isNotEmpty(queryInfo.getCategoriesId())){
				categoriesId = Integer.parseInt(queryInfo.getCategoriesId());
			}
			musicScoreQueryInfo.setParentId(categoriesId);
			musicScoreQueryInfo.setEnable(queryInfo.getEnable());
			musicScoreQueryInfo.setOrganId(queryInfo.getOrganId());
			List<SysMusicScoreCategories> scoreCategories = sysMusicScoreCategoriesService.queryTree(musicScoreQueryInfo);
			if(scoreCategories.size() > 0){
				categoriesIdList = scoreCategories.stream().map(e->e.getId()).collect(Collectors.toList());
			}else {
				categoriesIdList.add(categoriesId);
			}
			queryInfo.setCategoriesIdList(categoriesIdList);
		}
		Map<String, Object> params = new HashMap<>();
		MapUtil.populateMap(params, queryInfo);
		return sysMusicScoreAccompanimentDao.queryAccPage(params);
	}

	@Override
	public List<Subject> querySubjectIds(Integer categoriesId) {
		List<Integer> categoriesIdList = new ArrayList<>();
		if(categoriesId != null){
			MusicScoreQueryInfo musicScoreQueryInfo = new MusicScoreQueryInfo();
			musicScoreQueryInfo.setParentId(categoriesId);
			List<SysMusicScoreCategories> scoreCategories = sysMusicScoreCategoriesService.queryTree(musicScoreQueryInfo);
			if(scoreCategories.size() > 0){
				List<SysMusicScoreCategories> allScoreCategories = new ArrayList<>();
				allScoreCategories.addAll(scoreCategories);
				for (SysMusicScoreCategories scoreCategory : scoreCategories) {
					if(scoreCategory.getSysMusicScoreCategoriesList() != null){
						allScoreCategories.addAll(scoreCategory.getSysMusicScoreCategoriesList());
					}
				}
				categoriesIdList = allScoreCategories.stream().map(e->e.getId()).collect(Collectors.toList());
			}else {
				categoriesIdList.add(categoriesId);
			}
		}
		return sysMusicScoreAccompanimentDao.querySubjectIds(categoriesIdList);
	}

    @Override
    public PageInfo<SysMusicScoreAccompaniment> queryScorePage(SysExamSongQueryInfo queryInfo) {
		if(queryInfo.getCategoriesIds() != null){
			List<Integer> categoriesIdList = new ArrayList<>();
			String[] categoriesIds = queryInfo.getCategoriesIds().split(",");

			for(String fc : categoriesIds){
				SysMusicScoreCategories sysMusicScoreCategories = sysMusicScoreCategoriesService.get(Integer.parseInt(fc));
				
				if(sysMusicScoreCategories == null){
					throw new BizException("分类找不到");
				}
				categoriesIdList.add(Integer.parseInt(fc));
				sysMusicScoreCategories = sysMusicScoreCategoriesService.getChildTree(sysMusicScoreCategories);
				getAllCategoryIdList(categoriesIdList, sysMusicScoreCategories.getSysMusicScoreCategoriesList());
			}
			queryInfo.setCategoriesIds(categoriesIdList.stream().map(String :: valueOf).collect(Collectors.joining(",")));
		}
		if(StringUtils.isNotBlank(queryInfo.getFilterCategoriesIds())){
			String[] filterCategoriesIds = queryInfo.getFilterCategoriesIds().split(",");

			List<Integer> filterCategoriesIdList = new ArrayList<>();
			
			for(String fc : filterCategoriesIds){
				SysMusicScoreCategories sysMusicScoreCategories = sysMusicScoreCategoriesService.get(Integer.parseInt(fc));
				
				if(sysMusicScoreCategories == null){
					throw new BizException("分类找不到");
				}
				filterCategoriesIdList.add(Integer.parseInt(fc));
				sysMusicScoreCategories = sysMusicScoreCategoriesService.getChildTree(sysMusicScoreCategories);
				getAllCategoryIdList(filterCategoriesIdList, sysMusicScoreCategories.getSysMusicScoreCategoriesList());
			}
			queryInfo.setFilterCategoriesIds(filterCategoriesIdList.stream().map(String :: valueOf).collect(Collectors.joining(",")));
		}
		return this.queryPage(queryInfo);
    }
    
    private void getAllCategoryIdList(List<Integer> categoryIdList, List<SysMusicScoreCategories> sysMusicScoreCategoriesList){
    	
    	if(sysMusicScoreCategoriesList != null && sysMusicScoreCategoriesList.size() > 0){
    		for(SysMusicScoreCategories sysMusicScoreCategories : sysMusicScoreCategoriesList){
    			categoryIdList.add(sysMusicScoreCategories.getId());
    			getAllCategoryIdList(categoryIdList, sysMusicScoreCategories.getSysMusicScoreCategoriesList());
    		}
    	}
    	
    }
}