package com.ruoyi.system.service.impl;

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

import com.ruoyi.common.core.domain.entity.FormalTeacherClass;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.RegisterTeacherClass;
import com.ruoyi.system.mapper.FormalTeacherClassMapper;
import com.ruoyi.system.service.IRegisterTeacherClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.RegisterParentsStudentMapper;
import com.ruoyi.system.domain.RegisterParentsStudent;
import com.ruoyi.system.service.IRegisterParentsStudentService;

/**
 * 注册-家长-学生Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-05-25
 */
@Service
public class RegisterParentsStudentServiceImpl implements IRegisterParentsStudentService 
{
    @Autowired
    private RegisterParentsStudentMapper registerParentsStudentMapper;
    @Autowired
    private IRegisterTeacherClassService registerTeacherClassService;
    @Autowired
    private FormalTeacherClassMapper formalTeacherClassMapper;

    /**
     * 查询注册-家长-学生
     * 
     * @param id 注册-家长-学生主键
     * @return 注册-家长-学生
     */
    @Override
    public RegisterParentsStudent selectRegisterParentsStudentById(Long id)
    {
        return registerParentsStudentMapper.selectRegisterParentsStudentById(id);
    }

    /**
     * 查询注册-家长-学生列表
     * 
     * @param registerParentsStudent 注册-家长-学生
     * @return 注册-家长-学生
     */
    @Override
    public List<RegisterParentsStudent> selectRegisterParentsStudentList(RegisterParentsStudent registerParentsStudent)
    {

        SysUser user = SecurityUtils.getLoginUser().getUser();
        List<SysRole> roles = user.getRoles();
        boolean bl = false;
        for (SysRole role : roles) {
            if("teacher".equals(role.getRoleKey())){
                bl = true;
                break;
            }
        }
        //角色是老师
        if(bl){
            //查询老师班级
            FormalTeacherClass formalTeacherClass = new FormalTeacherClass();
            formalTeacherClass.setTeacherId(user.getUserId());
            List<FormalTeacherClass> formalTeacherClassList = formalTeacherClassMapper.selectFormalTeacherClassList(formalTeacherClass);
            List<Long> classIds = formalTeacherClassList.stream().map(FormalTeacherClass::getClassId).collect(Collectors.toList());
            registerParentsStudent.setClassIds(classIds);
        }

        return registerParentsStudentMapper.selectRegisterParentsStudentList(registerParentsStudent);
    }

    /**
     * 新增注册-家长-学生
     * 
     * @param registerParentsStudent 注册-家长-学生
     * @return 结果
     */
    @Override
    public int insertRegisterParentsStudent(RegisterParentsStudent registerParentsStudent)
    {
        registerParentsStudent.setCreateTime(DateUtils.getNowDate());
        return registerParentsStudentMapper.insertRegisterParentsStudent(registerParentsStudent);
    }

    /**
     * 修改注册-家长-学生
     * 
     * @param registerParentsStudent 注册-家长-学生
     * @return 结果
     */
    @Override
    public int updateRegisterParentsStudent(RegisterParentsStudent registerParentsStudent)
    {
        registerParentsStudent.setUpdateTime(DateUtils.getNowDate());
        registerParentsStudent.setIsPass("1");
        return registerParentsStudentMapper.updateRegisterParentsStudent(registerParentsStudent);
    }

    /**
     * 批量删除注册-家长-学生
     * 
     * @param ids 需要删除的注册-家长-学生主键
     * @return 结果
     */
    @Override
    public int deleteRegisterParentsStudentByIds(Long[] ids)
    {
        return registerParentsStudentMapper.deleteRegisterParentsStudentByIds(ids);
    }

    /**
     * 删除注册-家长-学生信息
     * 
     * @param id 注册-家长-学生主键
     * @return 结果
     */
    @Override
    public int deleteRegisterParentsStudentById(Long id)
    {
        return registerParentsStudentMapper.deleteRegisterParentsStudentById(id);
    }
}
