package com.CUST.brain.common.security.service;

import com.CUST.brain.dao.domain.system.SysPermission;
import com.CUST.brain.dao.domain.system.SysRole;
import com.CUST.brain.dao.domain.system.SysUser;
import com.CUST.brain.service.SysPermissionService;
import com.CUST.brain.service.SysRoleService;
import com.CUST.brain.service.SysUserService;
import com.CUST.brain.common.security.LoginUser;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
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;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户详情服务实现类
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Resource
    private SysUserService userService;

    @Resource
    private SysRoleService roleService;

    @Resource
    private SysPermissionService permissionService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 1. 查询用户
        SysUser user = userService.getByUsername(username);
        if (user == null) {
            throw new UsernameNotFoundException("用户名或密码错误");
        }

        // 2. 查询用户角色
        List<SysRole> roles = roleService.getRolesByUserId(user.getUserId());
        List<String> roleCodes = roles.stream()
                .map(SysRole::getRoleCode)
                .collect(Collectors.toList());

        // 3. 查询用户权限
        List<SysPermission> permissions = permissionService.getPermissionsByUserId(user.getUserId());
        List<String> permissionCodes = permissions.stream()
                .map(SysPermission::getPermissionCode)
                .collect(Collectors.toList());

        // 4. 合并角色和权限
        List<SimpleGrantedAuthority> authorities = roleCodes.stream()
                .map(role -> new SimpleGrantedAuthority("ROLE_" + role))
                .collect(Collectors.toList());
        authorities.addAll(permissionCodes.stream()
                .map(SimpleGrantedAuthority::new)
                .collect(Collectors.toList()));

        // 5. 返回LoginUser对象
        LoginUser loginUser = new LoginUser();
        loginUser.setUserId(user.getUserId());
        loginUser.setUsername(user.getUsername());
        loginUser.setPassword(user.getPassword());
        loginUser.setRealName(user.getRealName());
        loginUser.setPhone(user.getPhone());
        loginUser.setEmail(user.getEmail());
        loginUser.setStatus(user.getStatus());
        loginUser.setLastLoginTime(user.getLastLoginTime());
        loginUser.setLastLoginIp(user.getLastLoginIp());
        loginUser.setRoles(roleCodes);
        loginUser.setPermissions(permissionCodes);
        loginUser.setAuthorities(authorities);
        return loginUser;
    }
} 