package com.ai.da.common.security;

import com.ai.da.common.response.ResultEnum;
import com.ai.da.common.utils.JSONResponseUtils;
import com.ai.da.common.response.Response;
import com.ai.da.common.security.jwt.JWTTokenHelper;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author: dangweijian
 * @description: 登录成功处理类
 * @create: 2020-07-09 14:58
 **/
@Component
public class UserLoginSuccessHandler implements AuthenticationSuccessHandler {

    @Resource
    private JWTTokenHelper jwtTokenHelper;

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
//        User user = (User) authentication.getPrincipal();
//        AuthPrincipalVo principal = new AuthPrincipalVo();
//        BeanUtils.copyProperties(user, principal);
//        // 获取用户角色
//        List<UserRoleDto> userRoles = roleService.getUserRoles(user.getId(), 0);
//        principal.setRoles(userRoles);
//        // 获取角色部门
//        if(CollUtil.isNotEmpty(userRoles)){
//            principal.setDepts(deptService.getDeptByRoleIds(userRoles.stream().map(UserRoleDto::getRoleId).collect(Collectors.toList())));
//        }
//        // 用户角色权限
//        List<String> authorities = new ArrayList<>(authentication.getAuthorities()).stream().map(GrantedAuthority::getAuthority).collect(Collectors.toList());
//        principal.setAuthorities(authorities);
//        AuthVo authVo = new AuthVo();
//        authVo.setAuthorities(authorities);
//        authVo.setToken(jwtTokenHelper.createToken(principal));
//        authVo.setPrincipal(principal);
//        user.setLastLoginTime(new Date());
//        userService.updateById(user);
        JSONResponseUtils.build(response, Response.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), null));
    }
}
