package com.srs.comprehensive.service.impl;

import com.srs.common.core.domain.entity.SysDept;
import com.srs.comprehensive.domain.CascaderEntity;
import com.srs.comprehensive.domain.SrsClass;
import com.srs.comprehensive.domain.SrsMajors;
import com.srs.comprehensive.domain.SrsStudent;
import com.srs.comprehensive.mapper.SrsClassMapper;
import com.srs.comprehensive.mapper.SrsMajorsMapper;
import com.srs.comprehensive.mapper.SrsStudentMapper;
import com.srs.comprehensive.service.ICascaderDataStudentService;
import com.srs.system.mapper.SysDeptMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class CascaderDataStudentServiceImpl implements ICascaderDataStudentService {

    @Autowired
    private SrsMajorsMapper srsMajorsMapper;
    @Autowired
    private SrsClassMapper srsClassMapper;
    @Autowired
    private SrsStudentMapper srsStudentMapper;
    @Autowired

    private SysDeptMapper sysDeptMapper;

    /**
     * 获取学院下所有信息
     * @param level 几层
     * @return 学院-专业-班级-学生
     */
    public static List<SrsMajors> srsMajorList;
    public static List<SrsClass> srsClassList;
    @Override
    public List<CascaderEntity> getCascadeData(int level) {
        SysDept depts = sysDeptMapper.selectDeptByDeptCode("1160804");
        List<CascaderEntity> list = new ArrayList<>();
        SysDept sysDept = new SysDept();
        sysDept.setParentId(depts.getDeptId());

        List<SysDept> deptList = sysDeptMapper.selectDeptList(sysDept);
        if (level>=2){
            srsMajorList = srsMajorsMapper.selectSrsMajorsNameList();
        }
        if (level>=3){
            srsClassList = srsClassMapper.selectSrsClassNameList();
        }



        for (SysDept dept : deptList) {
            CascaderEntity c = new CascaderEntity();
            c.setValue(dept.getDeptId());
            c.setLabel(dept.getDeptName());
            if (level >= 2) // 查询两层或更多
            {
                List<CascaderEntity> majorData = getMajorData(dept.getDeptId(), level);
                c.setChildren(majorData);
            }
            list.add(c);
        }

        return list;
    }

    @Override
    public List<String> getXZJGDept(Long parentId) {
        SysDept  sysDept=new SysDept();
        sysDept.setParentId(parentId);
        List<SysDept> sysDepts = sysDeptMapper.selectDeptList(sysDept);
        return sysDepts.stream().map(SysDept::getDeptName).collect(Collectors.toList());
    }


    /**
     * 构建专业数据
     * @param deptId
     * @param level
     * @return
     */
    private List<CascaderEntity> getMajorData(long deptId, long level) {
        List<CascaderEntity> majorData = new ArrayList<>();
        SrsMajors srsMajors = new SrsMajors();
        srsMajors.setCollegeId(deptId);
//        List<SrsMajors> majors = srsMajorsMapper.selectSrsMajorsList(srsMajors);
        List<SrsMajors> majors = srsMajorList.stream().filter(x -> x.getCollegeId().equals(deptId)).collect(Collectors.toList());
        for (SrsMajors major : majors) {
            CascaderEntity c = new CascaderEntity();
            c.setValue(major.getMajorId());
            c.setLabel(major.getMajorName());
            if (level >= 3) // 查三层层或更多
            {
                List<CascaderEntity> classData = getClassData(major.getMajorId(), level);
                c.setChildren(classData);
            }
            majorData.add(c);
        }
        return majorData;
    }

    /**
     * 构建班级数据
     * @param majorId
     * @param level
     * @return
     */
    private List<CascaderEntity> getClassData(long majorId, long level) {
        List<CascaderEntity> classData = new ArrayList<>();
        SrsClass srsClass = new SrsClass();
        srsClass.setMajorId(majorId);
//        List<SrsClass> classList = srsClassMapper.selectSrsClassNameList(srsClass);
        List<SrsClass> classList = srsClassList.stream().filter(x -> x.getMajorId().equals(majorId)).collect(Collectors.toList());
        for (SrsClass classs : classList) {
            CascaderEntity c = new CascaderEntity();
            c.setValue(classs.getClassId());
            c.setLabel(classs.getClassName());
            classData.add(c);
        }

        return classData;
    }



}
