package com.yonge.cooleshow.biz.dal.service.impl;

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

import com.yonge.toolset.mybatis.page.PageInfo;
import com.yonge.toolset.mybatis.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yonge.cooleshow.biz.dal.dao.SubjectDao;
import com.yonge.cooleshow.biz.dal.entity.Subject;
import com.yonge.cooleshow.biz.dal.queryInfo.SubjectQueryInfo;
import com.yonge.cooleshow.biz.dal.service.SubjectService;
import com.yonge.toolset.mybatis.dal.BaseDAO;
import com.yonge.cooleshow.common.exception.BizException;
import com.yonge.toolset.mybatis.page.PageInfo;

import org.springframework.util.CollectionUtils;

@Service
public class SubjectServiceImpl extends BaseServiceImpl<Long, Subject> implements SubjectService {

    @Autowired
    private SubjectDao subjectDao;

    @Override
    public BaseDAO<Long, Subject> getDAO() {
        return subjectDao;
    }

    @Override
    public List<Subject> findBySubjectByIdList(List<Long> subjectIdList) {
        return subjectDao.findBySubjectIds(subjectIdList);
    }
    @Override
    public List<Subject> findBySubjectByIdList(String subjectIdList) {
        return subjectDao.findBySubjectByIdList(subjectIdList);
    }

    @Override
    public PageInfo<Subject> queryPageTree(SubjectQueryInfo queryInfo) {
        PageInfo<Subject> pageInfo = queryPage(queryInfo);
        for (Subject subject : pageInfo.getRows()) {
            subject = getTree(subject, queryInfo.getDelFlag());
        }
        return pageInfo;
    }

    @Override
	public int deleteById(Long id) {
		return subjectDao.deleteById(id);
	}

	@Override
    public void upSetSubject(Subject subject) {
		
		Subject orginSubject = subjectDao.queryByName(subject.getName());
		
		if(orginSubject != null){
			if(orginSubject.getId() != subject.getId()){
				throw new BizException("名称已存在，请修改名称");
			}
		}
		
		if (subject.getId() != null) {
            subject.setUpdateTime(new Date());
            subjectDao.update(subject);
        } else {
            subjectDao.insert(subject);
        }
    }

    @Override
    public List<Subject> subjectSelect() {
        List<Subject> allList = subjectDao.findAll(new HashMap<>());

        List<Subject> parents = allList.stream()
                .filter(o -> (null == o.getParentSubjectId() || o.getParentSubjectId().equals(0L) ))
                .collect(Collectors.toList());

        List<Subject> subjects = allList.stream()
                .filter(o -> !(null == o.getParentSubjectId() || o.getParentSubjectId().equals(0L)))
                .collect(Collectors.toList());

        for (Subject parent : parents) {
            if (null == parent.getId()) {
                continue;
            }
            for (Subject subject : subjects) {
                if (null == subject.getId() || null == subject.getParentSubjectId()) {
                    continue;
                }
                if (subject.getParentSubjectId().equals(parent.getId())) {
                    List<Subject> children = parent.getSubjects();
                    if(CollectionUtils.isEmpty(children)){
                        children = new ArrayList<>();
                    }
                    children.add(subject);
                    parent.setSubjects(children);
                }
            }
        }
        return parents;
    }

    /***
     * 查询声部树
     * @param: sub
     * @param: yesOrNoEnum
     * @updateTime 2022/4/6 11:33
     * @return: com.yonge.cooleshow.biz.dal.entity.Subject
     */
    private Subject getTree(Subject sub, Boolean delFlag) {
        //得到根节点对象
        //获取子节点list
        List<Subject> subjects = subjectDao.findByParentId(sub.getId(), delFlag);
        //如果存在子节点
        if (subjects != null && subjects.size() > 0) {
            //将子节点list放入父节点对象
            sub.setSubjects(subjects);
            //遍历子节点....
            for (Subject subject : subjects) {
                getTree(subject, delFlag);
            }
        }
        return sub;
    }
}
