package com.jiayue.ipfcst.console.controller;

//import cn.hutool.core.date.DateUtil;
//import cn.hutool.core.lang.Assert;
//import com.jiayue.ipfcst.common.core.constant.enums.SysUserStatusEnum;
//import com.jiayue.ipfcst.common.core.exception.BusinessException;
//import com.jiayue.ipfcst.common.core.web.vo.RequestVO;
//import com.jiayue.ipfcst.common.core.web.vo.ResponseVO;
//import com.jiayue.ipfcst.common.data.entity.SysUser;
//import com.jiayue.ipfcst.common.security.util.JwtTokenUtil;
//import com.jiayue.ipfcst.console.service.SysParameterService;
//import com.jiayue.ipfcst.console.service.SysUserService;
//import lombok.SneakyThrows;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.http.MediaType;
//import org.springframework.security.crypto.password.PasswordEncoder;
//import org.springframework.web.bind.annotation.PostMapping;
//import org.springframework.web.bind.annotation.RequestBody;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RestController;
//
//import java.util.Date;
//
///**
// * 安全认证
// *
// * @author zzy
// * @version 1.0
// * @since 2019/11/25 18:08
// */
//@RestController
//@RequestMapping("/auth")
//@Slf4j
//public class AuthController {
//
//    private final SysUserService sysUserService;
//
//    private final PasswordEncoder passwordEncoder;
//
//    private final SysParameterService sysParameterService;
//
//    @Autowired
//    public AuthController(SysUserService sysUserService, PasswordEncoder passwordEncoder, SysParameterService sysParameterService) {
//        this.sysUserService = sysUserService;
//        this.passwordEncoder = passwordEncoder;
//        this.sysParameterService = sysParameterService;
//    }
//
//    @SneakyThrows
//    @PostMapping(value = "/login", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
//    public ResponseVO login(@RequestBody RequestVO<SysUser> user) {
//        String userName = user.getData().getAccount();
//        String password = user.getData().getPassword();
//        SysUser sysUser = this.sysUserService.get(userName);
//        Assert.isFalse(sysUser.getSysUserStatusEnum().compareTo(SysUserStatusEnum.E3) == 0, "账号已被禁用");
//        if (sysUser.getSysUserStatusEnum().compareTo(SysUserStatusEnum.E1) != 0) {
//            //当前时间
//            Long nowT = DateUtil.date().getTime();
//            //系统参数锁定时间
//            Long userLockTime = Long.parseLong(this.sysParameterService.getParameter("user_lock_time"));
//            //时间差
//            Long sT = nowT - userLockTime;
//            //判断锁定时间是否已到，到了改变状态，继续正常登陆，否则继续锁定
//            Assert.isTrue(sT > sysUser.getLockTime().getTime(), "账号已被锁定");
//        }
//        return this.checkUser(sysUser, userName, password);
//    }
//
//    /**
//     * 刷新token
//     *
//     * @return 新的token
//     */
//    @PostMapping(value = "/refreshToken", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
//    public ResponseVO refreshToken(@RequestBody RequestVO<SysUser> user) throws BusinessException {
//        String token = user.getData().getToken();
//        JwtTokenUtil jwtTokenUtil = new JwtTokenUtil();
//        String username = jwtTokenUtil.getUsernameFromToken(token);
//        log.debug("验证用户:" + username);
//        Assert.isTrue(jwtTokenUtil.validateToken(token, username), "token无效");
//        SysUser sysUser = this.sysUserService.get(username);
//        Date lastModifyTime = sysUser.getLastModifyTime();
//        Assert.isTrue(jwtTokenUtil.canTokenBeRefreshed(token, lastModifyTime), "用户信息已修改，请重新登录");
//        String newToken = jwtTokenUtil.refreshToken(token);
//        return ResponseVO.success(newToken);
//    }
//
//    /**
//     * 验证用户
//     *
//     * @param sysUser  系统用户
//     * @param userName 账号
//     * @param password 密码
//     * @return 验证结果
//     * @throws BusinessException 异常
//     */
//    private ResponseVO checkUser(SysUser sysUser, final String userName, final String password) throws BusinessException {
//        if (passwordEncoder.matches(password, sysUser.getPassword())) {//密码不正确，记录错误次数加1
//            int errN = sysUser.getPwdErrNum() + 1;
//            int userLockErrorNumber = Integer.valueOf(this.sysParameterService.getParameter("user_lock_error_number"));
//            if (errN >= userLockErrorNumber) {
//                sysUser.setPwdErrNum(errN);
//                sysUser.setLockTime(new Date());
//                sysUser.setSysUserStatusEnum(SysUserStatusEnum.E2);// 锁定账号
//                this.sysUserService.save(sysUser);
//                throw new BusinessException("密码错误" + errN + "次，账号锁定");
//            } else {
//                sysUser.setPwdErrNum(errN);
//                this.sysUserService.save(sysUser);
//                throw new BusinessException("密码错误" + errN);
//            }
//        }
//        //密码正确
//        sysUser.setPwdErrNum(0);
//        JwtTokenUtil jwtTokenUtil = new JwtTokenUtil();
//        String token = jwtTokenUtil.generateToken(userName);
//        sysUser.setToken(token);
//        log.debug("获取token:" + token);
//        return ResponseVO.success(sysUser);
//    }
//}
