package com.jeequan.jeepay.member.secruity;

import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.exception.JeepayAuthenticationException;
import com.jeequan.jeepay.db.entity.MemberInfo;
import com.jeequan.jeepay.service.impl.MemberInfoService;
import org.springframework.beans.factory.annotation.Autowired;
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;

/**
 * UserDetailsService实现类
 *
 * @author terrfly
 * @modify zhuxiao
 * @date 2021-04-27 15:50
 */
@Service
public class JeeUserDetailsServiceImpl implements UserDetailsService {

    @Autowired private MemberInfoService memberInfoService;

    @Override
    public UserDetails loadUserByUsername(String loginUsernameStr) throws UsernameNotFoundException {

        MemberInfo memberInfo = memberInfoService.getById(loginUsernameStr);
        if(memberInfo == null){ //没有该用户信息
            throw JeepayAuthenticationException.build("用户名/密码错误！");
        }
        if(CS.PUB_USABLE != memberInfo.getState()){ //状态不合法
            throw JeepayAuthenticationException.build("用户状态不可登录，请联系管理员！");
        }

        return new JeeMemberDetails(memberInfo);
    }
}
