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

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yida.data.common.core.entity.user.EduUserDeviceFactory;
import com.yida.data.user.mapper.EduStaffMapper;
import com.yida.data.user.mapper.EduStudentMapper;
import com.yida.data.user.mapper.EduUserDeviceFactoryMapper;
import com.yida.data.user.service.EduUserDeviceFactoryService;
import com.yida.data.user.vo.UserDeviceInfoVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

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

@RequiredArgsConstructor
@Slf4j
@Service
public class EduUserDeviceFactoryServiceImpl extends ServiceImpl<EduUserDeviceFactoryMapper, EduUserDeviceFactory> implements EduUserDeviceFactoryService {
    private final EduStudentMapper eduStudentMapper;
    private final EduStaffMapper eduStaffMapper;

    @Override
    public void batchSaveUserDeviceFactory(List<EduUserDeviceFactory> userDeviceFactoryList) {
        List<String> exist = baseMapper.existUserDeviceFactory(userDeviceFactoryList);
        List<EduUserDeviceFactory> res =
                userDeviceFactoryList.stream().filter(x -> !exist.contains(x.getTypeFlag())).collect(Collectors.toList());
        saveBatch(res);
    }

    @Override
    public UserDeviceInfoVO getUserInfoByDevciceFactory(String flag, String type) {
        EduUserDeviceFactory userDeviceFactory = getOne(Wrappers.lambdaQuery(new EduUserDeviceFactory()).eq(EduUserDeviceFactory::getTypeFlag, flag).eq(EduUserDeviceFactory::getTypeName, type));
        UserDeviceInfoVO vo = new UserDeviceInfoVO();
        vo.setUserId(userDeviceFactory.getUserId());
        vo.setUserType(userDeviceFactory.getUserType());
        if (userDeviceFactory.getUserType() == 0) {
            vo.setStudent(eduStudentMapper.getStudentWithParent(userDeviceFactory.getUserId(), null, null));
        } else {
            vo.setStaff(eduStaffMapper.selectById(userDeviceFactory.getUserId()));
        }
        return vo;
    }
}
