package com.ichangzuo.core.security.service;

import com.ichangzuo.common.exception.AccountDeletedException;
import com.ichangzuo.common.exception.AccountLockedException;
import com.ichangzuo.common.exception.BusinessException;
import com.ichangzuo.common.result.ResultCode;
import com.ichangzuo.core.security.model.SysUserDetails;
import com.ichangzuo.system.model.dto.UserAuthInfo;
import com.ichangzuo.system.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * 系统用户认证 DetailsService
 *
 * @author Ray
 * @since 2021/10/19
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class SysUserDetailsService implements UserDetailsService {

    private final UserService userService;

    /**
     * 根据用户名获取用户信息
     *
     * @param username 用户名
     * @return 用户信息
     * @throws UsernameNotFoundException 用户名未找到异常
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        try {
            UserAuthInfo userAuthInfo = userService.getUserAuthInfo(username);
            if (userAuthInfo == null) {
                throw new UsernameNotFoundException(username);
            }
            else if(userAuthInfo.getStatus()==0){
                throw new AccountDeletedException(username);
            }
            else if(userAuthInfo.getStatus()==1){
                throw new AccountLockedException(username);
            }
            return new SysUserDetails(userAuthInfo);
        } catch (Exception e) {
            // 记录异常日志
            log.error("认证异常:{}", e.getMessage());
            // 抛出异常
            throw e;
        }
    }
}
