package com.njcn.product.security;

import lombok.RequiredArgsConstructor;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
@RequiredArgsConstructor
public class MyUserDetailsService implements UserDetailsService {




    @Override
    public MyUserDetails loadUserByUsername(String username) throws UsernameNotFoundException {


        if("system_event".equals(username)){
            return new MyUserDetails("12345678910","system_event", "@#001njcnpqs","10001",
                    new ArrayList<>());
        }


           // 这里应该从数据库中获取用户信息
            PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
            String encodedPassword = passwordEncoder.encode("@#001njcnpqs");
/*
            LambdaQueryWrapper<PqsUser> userWrapper = new LambdaQueryWrapper<>();
            userWrapper.eq(PqsUser::getLoginname,username);
            PqsUser pqsUser = pqsUserMapper.selectOne(userWrapper);
            if(Objects.isNull(pqsUser)){
                throw new UsernameNotFoundException("User not found with username: " + username);
            }

            LambdaQueryWrapper<PqsUserSet> userSetWrapper = new LambdaQueryWrapper<>();
            userSetWrapper.eq(PqsUserSet::getUserIndex,pqsUser.getUserIndex());
            PqsUserSet pqsUserSet = pqsUserSetMapper.selectOne(userSetWrapper);
            String deptId = pqsUserSet.getDeptsIndex();*/


        return new MyUserDetails("12345678910","cdf", encodedPassword,"beac2cdb4acc4826d4519ea4177f3bfa",
                new ArrayList<>());


    }
}
