package com.ruoyi.dz.service.impl;

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

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.dz.domain.DzSchool;
import com.ruoyi.dz.mapper.DzSchoolMapper;
import com.ruoyi.enums.UserTypeEnum;
import com.ruoyi.system.service.ISysUserService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.dz.mapper.DzTeacherMapper;
import com.ruoyi.dz.domain.DzTeacher;
import com.ruoyi.dz.service.IDzTeacherService;

/**
 * 老师Service业务层处理
 *
 * @author ruoyi
 * @date 2025-09-12
 */
@Service
public class DzTeacherServiceImpl implements IDzTeacherService
{
    @Autowired
    private DzTeacherMapper dzTeacherMapper;
    @Autowired
    private DzSchoolMapper schoolMapper;
    @Autowired
    private ISysUserService userService;

    /**
     * 查询老师
     *
     * @param teacherId 老师主键
     * @return 老师
     */
    @Override
    public DzTeacher selectDzTeacherByTeacherId(Long teacherId)
    {
        return dzTeacherMapper.selectDzTeacherByTeacherId(teacherId);
    }

    /**
     * 根据用户ID查询老师
     *
     * @param userId 用户ID
     * @return 老师
     */
    @Override
    public DzTeacher selectDzTeacherByUserId(Long userId)
    {
        return dzTeacherMapper.selectDzTeacherByUserId(userId);
    }

    /**
     * 查询老师列表
     *
     * @param dzTeacher 老师
     * @return 老师
     */
    @Override
    public List<DzTeacher> selectDzTeacherList(DzTeacher dzTeacher)
    {
        List<DzTeacher> list = dzTeacherMapper.selectDzTeacherList(dzTeacher);
        //填充campusName,得到campusIds
        List<Long> campusIds = list.stream().map(DzTeacher::getCampusId).collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(campusIds)){
            Map<Long, DzSchool> schoolMap = schoolMapper.selectDzSchoolListByIds(campusIds)
                    .stream().collect(Collectors.toMap(DzSchool::getId,school -> school,
                            (existing, replacement) -> existing  // 如果key重复，保留已存在的
                    ));
            for (DzTeacher teacher : list) {
                if (teacher.getCampusId()!=null && schoolMap.containsKey(teacher.getCampusId())){
                    teacher.setCampusName(schoolMap.get(teacher.getCampusId()).getName());
                }
            }
        }
        return list;
    }

    /**
     * 新增老师
     *
     * @param dzTeacher 老师
     * @return 结果
     */
    @Override
    public Long insertDzTeacher(DzTeacher dzTeacher)
    {
        if (null == dzTeacher.getDeptId()){
            DzSchool school = schoolMapper.selectDzSchoolById(dzTeacher.getCampusId());
            if (null!=school){
                dzTeacher.setDeptId(school.getDeptId());
            }
        }
        if (null==dzTeacher.getDeptId()){
            //如果deptId为空，需要从当前用户的机构中获取（非超级管理员）
            dzTeacher.setDeptId(SecurityUtils.getDeptId());
        }
        dzTeacherMapper.insertDzTeacher(dzTeacher);//将老师的userId填充到user的userName中
        Long teacherId = dzTeacher.getTeacherId();
        Long userId = userService.insertRelateUser(UserTypeEnum.Teacher, dzTeacher.getTeacherId(), null, dzTeacher.getDeptId(), String.valueOf(teacherId), dzTeacher.getName());
        //反向更新dzTeacher中的userId
        dzTeacher.setUserId(userId);
        if (StringUtils.isEmpty(dzTeacher.getName())){
            dzTeacher.setName(String.valueOf(teacherId));
        }
        dzTeacherMapper.updateDzTeacher(dzTeacher);
        return teacherId;
    }

    /**
     * 修改老师
     *
     * @param dzTeacher 老师
     * @return 结果
     */
    @Override
    public int updateDzTeacher(DzTeacher dzTeacher)
    {
        return dzTeacherMapper.updateDzTeacher(dzTeacher);
    }

    /**
     * 批量删除老师
     *
     * @param teacherIds 需要删除的老师主键
     * @return 结果
     */
    @Override
    public int deleteDzTeacherByTeacherIds(Long[] teacherIds)
    {
        return dzTeacherMapper.deleteDzTeacherByTeacherIds(teacherIds);
    }

    /**
     * 删除老师信息
     *
     * @param teacherId 老师主键
     * @return 结果
     */
    @Override
    public int deleteDzTeacherByTeacherId(Long teacherId)
    {
        return dzTeacherMapper.deleteDzTeacherByTeacherId(teacherId);
    }
}
