package com.yida.data.user.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yida.data.common.core.entity.user.EduStaff;
import com.yida.data.common.core.entity.user.EduTeacherDept;
import com.yida.data.common.core.entity.user.EduUserDept;
import com.yida.data.user.mapper.EduStaffMapper;
import com.yida.data.user.mapper.EduTeacherDeptMapper;
import com.yida.data.user.service.EduTeacherDeptService;
import com.yida.data.user.service.EduUserDeptService;
import com.yida.data.user.vo.TeacherClassVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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

@Service
@RequiredArgsConstructor
public class EduTeachDeptServiceImpl extends ServiceImpl<EduTeacherDeptMapper, EduTeacherDept> implements EduTeacherDeptService {

    private final EduUserDeptService eduUserDeptService;
    private final EduStaffMapper eduStaffMapper;

    @Override
    public Integer countTeacherDept(Long teacherId, Long classId, Integer type, Long courseId) {
        return null;
    }

    @Override
    public List<TeacherClassVO> listTeacherClass(Long teacherId) {
        List<TeacherClassVO> classList = baseMapper.listTeacherClass(teacherId);
        for (TeacherClassVO classVO : classList) {
            EduUserDept clazz = eduUserDeptService.getById(classVO.getClassId());
            EduUserDept grade = eduUserDeptService.getById(classVO.getGradeId());
            if (clazz != null) {
                classVO.setClassName(clazz.getDeptName());
                classVO.setSort(clazz.getOrderNum());
            }
            if (grade != null) {
                classVO.setGradeName(grade.getDeptName());
            }
        }
        classList.sort((x, y) -> {
            return Optional.ofNullable(x.getSort()).orElse(0) - Optional.ofNullable(y.getSort()).orElse(0);
        });
        return classList;
    }

    @Override
    public List<EduTeacherDept> listTeacherDept(List<Long> teacherIds) {
        return baseMapper.listTeacherDept(teacherIds);
    }

    @Override
    public List<EduStaff> listTeacherDept(Long deptId, String type) {
        List<EduTeacherDept> teacherDeptList = list(Wrappers.lambdaQuery(new EduTeacherDept())
            // 教师类型 1表示校区负责人，2表示年级负责人，3表示班主任，4表示任课老师，5表示学段负责人，6表示学校负责人
            .eq(EduTeacherDept::getType, type)
            .eq("3".equals(type), EduTeacherDept::getClassId, deptId)
            .eq("2".equals(type), EduTeacherDept::getGradeId, deptId)
            .eq("1".equals(type), EduTeacherDept::getCampusId, deptId)
            .eq("5".equals(type), EduTeacherDept::getSectionId, deptId)
            .eq("6".equals(type), EduTeacherDept::getSchoolId, deptId)
        );
        if (CollUtil.isEmpty(teacherDeptList)) {
            return new ArrayList<>();
        }
        // 职工id集合
        List<Long> staffIds = teacherDeptList.stream().map(EduTeacherDept::getTeacherId).distinct().collect(Collectors.toList());
        return eduStaffMapper.selectList(Wrappers.lambdaQuery(new EduStaff()).in(EduStaff::getId, staffIds));
    }

    @Override
    public long countTeacherNumber(Long schoolId) {
        return baseMapper.countTeacherNumber(schoolId);
    }
}
