package com.jiayue.ipfcst.common.security.handle;

import cn.hutool.json.JSONUtil;
import com.jiayue.ipfcst.common.core.web.vo.ResponseVO;
import com.jiayue.ipfcst.common.security.user.SysUser;
import com.jiayue.ipfcst.common.security.user.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
import org.springframework.stereotype.Component;

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

/**
 * 失败认证处理
 */
@Component("customAuthenticationFailureHandler")
public class CustomAuthenticationFailureHandler extends SimpleUrlAuthenticationFailureHandler {

    @Autowired
    SysUserService sysUserService;

    @Override
    public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
                                        AuthenticationException e) throws IOException, ServletException {
        String username = httpServletRequest.getParameter("username");
        String errorTips = "";
        SysUser user = sysUserService.findByUserName(username);
        if (username.equals("admin") || username.equals("consumer")) {
            errorTips = "用户名或密码不正常";
        } else {
            errorTips = "用户名或密码不正常";
        }

        if (user == null) {
            errorTips = "用户名或密码不正常";
        } else {
            if (user.getStatus().equals("1")) {
                Long xz = (user.getLockTime() + 300000 - System.currentTimeMillis()) / 1000;
                if (xz < 0) {
                    user.setStatus("0");
                    user.setPwdErrNum(1);
                    errorTips = "用户名或密码不正常，还有4次机会";
                    sysUserService.save(user);
                } else {
                    //锁定状态
                    errorTips = "用户已锁定,请隔" + xz + "秒再登录";
                }
            } else {
                int errCount = 5;
                int errNum = user.getPwdErrNum();
                if (errNum < 5) {
                    errorTips = "用户名或密码不正常，还有" + (errCount - errNum) + "次机会";
                    errNum++;
                    user.setPwdErrNum(errNum);
                    sysUserService.save(user);
                } else {
                    errorTips = "用户锁定，稍后再试";
                    user.setPwdErrNum(0);
                    user.setStatus("1");
                    user.setLockTime(System.currentTimeMillis());
                    sysUserService.save(user);
                }

            }

        }
        httpServletResponse.addHeader("Access-Control-Allow-Origin", "*");
        httpServletResponse.setContentType("application/json;charset=UTF-8");
        httpServletResponse.setStatus(401);
        httpServletResponse.getWriter().write(JSONUtil.toJsonStr(ResponseVO.fail(errorTips)));

    }

}
