package com.qiaoba.module.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.qiaoba.api.auth.service.AuthConfigApiService;
import com.qiaoba.api.auth.service.OnlineUserService;
import com.qiaoba.api.auth.service.SysUserDetailsApiService;
import com.qiaoba.api.system.entity.SysUser;
import com.qiaoba.api.auth.constants.SecurityConstant;
import com.qiaoba.api.auth.entity.LoginUser;
import com.qiaoba.api.auth.entity.SecurityUser;
import com.qiaoba.api.auth.entity.dto.RoleDto;
import com.qiaoba.api.auth.utils.TokenUtil;
import com.qiaoba.common.base.enums.BaseEnum;
import com.qiaoba.common.redis.service.RedisService;
import com.qiaoba.module.system.service.SysMenuService;
import com.qiaoba.module.system.service.SysUserRoleService;
import com.qiaoba.module.system.service.SysUserService;
import lombok.RequiredArgsConstructor;
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 java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 用户权限验证处理
 *
 * @author ailanyin
 * @version 1.0
 * @since 2022-09-22 04:20:28
 */
@Service
@RequiredArgsConstructor
public class SysUserDetailsServiceImpl implements UserDetailsService, SysUserDetailsApiService {

    private final SysUserService sysUserService;
    private final SysMenuService sysMenuService;
    private final SysUserRoleService sysUserRoleService;
    private final RedisService redisService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SecurityUser user = redisService.getObject(SecurityConstant.USER_DETAILS_REDIS_KEY + username, SecurityUser.class);
        return Objects.isNull(user) ? null : new LoginUser(user.getUserId(), user.getDeptId(), user.getUsername(), user.getNickname(), user.getRoles(), user.getRoleKeys(), user.getPermissions());
    }


    private UserDetails createUserDetails(SysUser user) {
        Set<String> perms = sysMenuService.selectPermsByUserId(user.getUserId());
        List<RoleDto> roles = sysUserRoleService.selectRoleDtoByUserId(user.getUserId(), BaseEnum.NORMAL.getCode());
        List<String> roleKeys = roles.stream().map(RoleDto::getRoleKey).collect(Collectors.toList());
        return new LoginUser(user.getUserId(), user.getDeptId(), user.getUsername(), user.getNickname(), roles, roleKeys, perms);
    }

    @Override
    public UserDetails toCache(String username, String deviceSn) {
        SysUser user = sysUserService.selectByUsername(username);
        UserDetails details = createUserDetails(user);
        SecurityUser securityUser = BeanUtil.copyProperties(details, SecurityUser.class);
        redisService.set(SecurityConstant.USER_DETAILS_REDIS_KEY + username, securityUser, TokenUtil.TOKEN_EXPIRE_HOUR_TIME * 3600);
        return details;
    }

}
