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

import com.yida.data.common.core.entity.system.SystemUser;
import com.yida.data.common.core.entity.system.enums.RoleEnum;
import com.yida.data.common.core.utils.Asserts;

import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.stereotype.Service;

import java.util.List;

import com.yida.data.auth.mapper.UserMapper;
import com.yida.data.auth.service.SecurityService;
import com.yida.data.auth.util.SecurityUtil;
import cn.hutool.core.collection.CollUtil;
import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
public class SecurityServiceImpl implements SecurityService {

    private final UserMapper userMapper;
    private final SecurityUtil securityUtil;

    @Override
    public OAuth2AccessToken getSchoolToken(Long schoolId) {
        List<SystemUser> userList = userMapper.listUserByPerms(schoolId, RoleEnum.ROLE_SCHOOL_PRINCIPAL.getValue());
        Asserts.isTrue(CollUtil.isNotEmpty(userList), "该学校无学校负责人，请创建后重试");
        SystemUser schoolUser = userList.get(0);
        return securityUtil.getOauth2AccessToken(schoolUser);
    }
}
