package com.yida.data.school.index.service.impl;


import com.yida.data.common.core.entity.user.EduStaff;
import com.yida.data.school.index.service.EduSchoolHomeContactService;
import com.yida.data.school.vo.index.ContactVO;
import com.yida.data.school.vo.index.UserContactInfoVO;
import com.yida.data.system.feign.RemoteYidaAppAccountService;
import com.yida.data.user.dto.ListStaffDTO;
import com.yida.data.user.feign.RemoteStaffService;
import com.yida.data.user.feign.RemoteStudentService;
import com.yida.data.user.feign.RemoteTeacherService;
import com.yida.data.user.vo.ParentInfoVO;

import org.springframework.stereotype.Service;

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

import cn.hutool.core.collection.CollUtil;
import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
public class EduSchoolHomeContactServiceImpl implements EduSchoolHomeContactService {
    private final RemoteYidaAppAccountService remoteYidaAppAccountService;
    private final RemoteStudentService remoteStudentService;
    private final RemoteTeacherService remoteTeacherService;
    private final RemoteStaffService remoteStaffService;

    @Override

    public ContactVO listContact(Long userId, Integer userType, Integer personType, String name,
                                 Long schoolId) {
        ContactVO vo = new ContactVO();
        if (userType == 0) {
            // 查询家长关联的教师
            List<EduStaff> staffList =
                    remoteTeacherService.listTeacherByParent(userId, name).getData();
            if (CollUtil.isNotEmpty(staffList)) {
                vo.setMasterList(staffList.stream().filter(x -> x.getTeacherType() == 3).map(x -> {
                    UserContactInfoVO contactInfoVO = new UserContactInfoVO();
                    contactInfoVO.setAvatar(x.getAvatar());
                    contactInfoVO.setMobile(x.getMobile());
                    contactInfoVO.setName(x.getName());
                    //contactInfoVO.setAppId();
                    return contactInfoVO;
                }).collect(Collectors.toList()));
                vo.setInstructorList(staffList.stream().filter(x -> x.getTeacherType() == 4).map(x -> {
                    UserContactInfoVO contactInfoVO = new UserContactInfoVO();
                    contactInfoVO.setAvatar(x.getAvatar());
                    contactInfoVO.setMobile(x.getMobile());
                    contactInfoVO.setName(x.getName());
                    //contactInfoVO.setAppId();
                    return contactInfoVO;
                }).collect(Collectors.toList()));
            }
        } else {
            if (personType == 0) {
                // 查询教师关联的家长
                List<ParentInfoVO> parentList = remoteStudentService.listParentByTeacher(userId, name).getData();
                if (CollUtil.isNotEmpty(parentList)) {
                    vo.setParentList(parentList.stream().map(x -> {
                        UserContactInfoVO contactInfoVO = new UserContactInfoVO();
                        contactInfoVO.setMobile(x.getMobile());
                        contactInfoVO.setName(x.getParentName());
                        //contactInfoVO.setAppId();
                        return contactInfoVO;
                    }).collect(Collectors.toList()));
                }
            } else if (personType == 1) {
                // 查询教师的同事
                List<EduStaff> staffList = remoteStaffService.listStaff(ListStaffDTO.builder().schoolId(schoolId).name(name).build()).getData();
                if (CollUtil.isNotEmpty(staffList)) {
                    vo.setColleagusList(staffList.stream().map(x -> {
                        UserContactInfoVO contactInfoVO = new UserContactInfoVO();
                        contactInfoVO.setMobile(x.getMobile());
                        contactInfoVO.setName(x.getName());
                        //contactInfoVO.setAppId();
                        return contactInfoVO;
                    }).collect(Collectors.toList()));
                }
            }
        }
        // 查询通讯录
        return vo;
    }
}
