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


import com.keao.edu.common.dal.BaseDAO;
import com.keao.edu.common.page.PageInfo;
import com.keao.edu.common.service.impl.BaseServiceImpl;
import com.keao.edu.user.dao.SubjectDao;
import com.keao.edu.user.entity.Subject;
import com.keao.edu.user.enums.YesOrNoEnum;
import com.keao.edu.user.page.SubjectQueryInfo;
import com.keao.edu.user.service.SubjectService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

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

    @Autowired
    private SubjectDao subjectDao;

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

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

    @Override
    public void upSetSubject(Subject subject) {
        if(subject.getDelFlag() != null && subject.getDelFlag() == true){
            subjectDao.delete(subject.getId());
            return;
        }
        Integer parentId = upset(subject, null);
        List<Subject> subjects = subject.getSubjects();
        if(subjects != null && subjects.size() > 0){
            subjects.forEach(e->{
                upset(e, parentId);
            });
        }
    }

    private Integer upset(Subject subject,Integer parentId){
        if(parentId != null){
            subject.setParentSubjectId(parentId);
        }
        if(subject.getId() != null){
            subject.setUpdateTime(new Date());
            subjectDao.update(subject);
        }else {
            subjectDao.insert(subject);
        }
        return subject.getId();
    }


    private Subject getTree(Subject sub,YesOrNoEnum yesOrNoEnum,String tenantId){
        //得到根节点对象
        //获取子节点list
        List<Subject> subjects = subjectDao.findByParentId(sub.getId(),yesOrNoEnum,tenantId);
        //如果存在子节点
        if(subjects != null && subjects.size() > 0) {
            //将子节点list放入父节点对象
            sub.setSubjects(subjects);
            //遍历子节点....
            for (Subject subject : subjects) {
                getTree(subject,yesOrNoEnum,tenantId);
            }
        }
        return sub;
    }

    @Override
    public List<Subject> findSubSubjects(Integer tenantId) {
        return subjectDao.findSubSubjects(tenantId);
    }

    @Override
    public List<Subject> findBySubjectByIdList(List<Integer> subjectIdList) {
        return subjectDao.findBySubjectByIdList(StringUtils.join(subjectIdList,","));
    }

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

    @Override
    public List<String> findBySubIds(String subIds) {
        return subjectDao.findBySubIds(subIds);
    }
}
