package com.dkl.framework.security.handle;

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

import com.dkl.common.core.redis.RedisCache;
import com.dkl.framework.util.BackendServiceOutToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import com.alibaba.fastjson2.JSON;
import com.dkl.common.constant.Constants;
import com.dkl.common.core.domain.AjaxResult;
import com.dkl.common.core.domain.model.LoginUser;
import com.dkl.common.utils.MessageUtils;
import com.dkl.common.utils.ServletUtils;
import com.dkl.common.utils.StringUtils;
import com.dkl.framework.manager.AsyncManager;
import com.dkl.framework.manager.factory.AsyncFactory;
import com.dkl.framework.web.service.TokenService;

/**
 * 自定义退出处理类 返回成功
 *
 * @author Dkl
 */
@Configuration
public class LogoutSuccessHandlerImpl implements LogoutSuccessHandler
{
    @Autowired
    private TokenService tokenService;
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private BackendServiceOutToken backendServiceOutToken;
    /**
     * 退出处理
     *
     * @return
     */
    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication)
            throws IOException, ServletException
    {
        LoginUser loginUser = tokenService.getLoginUser(request);
        if (StringUtils.isNotNull(loginUser))
        {
            String userName = loginUser.getUsername();
            // 删除用户缓存记录
            tokenService.delLoginUser(loginUser.getToken());
            backendServiceOutToken.subsystemToken(redisCache.getCacheObject(loginUser.getUser().getPhonenumber()).toString());
            redisCache.deleteObject(loginUser.getUser().getPhonenumber());
            // 记录用户退出日志
            AsyncManager.me().execute(AsyncFactory.recordLogininfor(userName, Constants.LOGOUT, MessageUtils.message("user.logout.success")));
        }
        ServletUtils.renderString(response, JSON.toJSONString(AjaxResult.success(MessageUtils.message("user.logout.success"))));
    }
}
