package com.ningdatech.kqapi.security.password;

import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.ningdatech.kqapi.security.facade.UserAuthLoginFacade;
import com.ningdatech.kqapi.security.model.UserInfoDetails;
import com.ningdatech.kqapi.security.model.dto.UserInfoBO;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Primary;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import static com.ningdatech.kqapi.user.model.enumerization.AccountStatus.ENABLE;

/**
 * <p>
 * PasswordLoginUserDetailService
 * </p>
 *
 * @author WendyYang
 * @since 10:20 2024/4/20
 */
@Primary
@Service
@RequiredArgsConstructor
public class PasswordLoginUserDetailService implements UserDetailsService {

    private final UserAuthLoginFacade userAuthLoginFacade;

    @Override
    public UserInfoDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserInfoBO user = userAuthLoginFacade.queryUserInfoInPasswordAuth(username);
        Assert.notNull(user, "用户不存在");
        Assert.isTrue(ENABLE.equals(user.getAccountStatus()), "用户已被禁用");
        UserInfoDetails details = new UserInfoDetails();
        details.setUserId(user.getUserId());
        details.setUsername(user.getRealName());
        details.setRealName(user.getRealName());
        details.setRole(user.getRole());
        details.setIdentifier(user.getIdentifier());
        details.setPassword(user.getCredential());
        return details;
    }

}
