package com.hz.pm.api.user.security.auth.mh;


import cn.hutool.json.JSONUtil;
import com.hz.pm.api.common.helper.UserInfoHelper;
import com.hz.pm.api.user.convert.UserInfoConvertor;
import com.hz.pm.api.user.security.model.UserFullInfoDTO;
import com.hz.pm.api.user.security.model.UserInfoDetails;
import com.ningdatech.basic.exception.BizException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import static com.hz.pm.api.user.model.enumeration.UserAvailableEnum.DISABLE;

/**
 * <p>
 * AgentLoginUserDetailService
 * </p>
 *
 * @author WendyYang
 * @since 11:22 2023/12/26
 */
@Slf4j
@Service("mhLoginUserDetailService")
@RequiredArgsConstructor
public class MhLoginUserDetailService implements UserDetailsService {

    private final UserInfoHelper userInfoHelper;

    @Override
    public UserInfoDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserFullInfoDTO userInfo = userInfoHelper.getUserFullInfoByMhUserIdOrOpenId(username);
        if (userInfo == null || DISABLE.equals(userInfo.getAvailable())) {
            throw BizException.wrap("用户不存在或已被禁用");
        }
        log.info("登录用户为：{}", JSONUtil.toJsonStr(userInfo));
        return UserInfoConvertor.convert(userInfo);
    }

}
