package com.bonus.auth.service;

import com.alibaba.nacos.common.utils.UuidUtils;
import com.bonus.common.core.utils.DateUtils;
import com.bonus.common.core.utils.global.SystemGlobal;
import com.bonus.common.log.enums.OperaResult;
import com.bonus.common.log.enums.OperaType;
import com.bonus.config.SystemConfig;
import com.bonus.system.api.domain.SysLogsVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.bonus.common.core.constant.Constants;
import com.bonus.common.core.constant.SecurityConstants;
import com.bonus.common.core.utils.StringUtils;
import com.bonus.common.core.utils.ip.IpUtils;
import com.bonus.system.api.RemoteLogService;
import com.bonus.system.api.domain.SysLogininfor;
import org.springframework.util.ObjectUtils;

import java.util.UUID;

/**
 * 记录日志方法
 *
 * @author bonus
 */
@Component
@Slf4j
public class SysRecordLogService
{
    @Autowired
    private RemoteLogService remoteLogService;

    /**
     * 记录登录信息
     *
     * @param username 用户名
     * @param status 状态
     * @param message 消息内容
     * @return
     */
    public void recordLogininfor(String username, String status, String message)
    {
        SysLogininfor logininfor = new SysLogininfor();
        logininfor.setUserName(username);
        logininfor.setIpaddr(IpUtils.getIpAddr());
        logininfor.setMsg(message);
        // 日志状态
        if (StringUtils.equalsAny(status, Constants.LOGIN_SUCCESS, Constants.LOGOUT, Constants.REGISTER))
        {
            logininfor.setStatus(Constants.LOGIN_SUCCESS_STATUS);
        }
        else if (Constants.LOGIN_FAIL.equals(status))
        {
            logininfor.setStatus(Constants.LOGIN_FAIL_STATUS);
        }
        remoteLogService.saveLogininfor(logininfor, SecurityConstants.INNER);
    }

    /**
     * 记录登录信息
     *
     * @param username 用户名
     * @param
     * @param message 消息内容
     * @return
     */
    public void saveLogs(String username, long startTime, String message,String resultData,String userId,String result) {
        long endTime = System.currentTimeMillis();
        SysLogsVo sysLogsVo = new SysLogsVo();
        String uuid= UUID.randomUUID().toString().replace("-","").toUpperCase();
        sysLogsVo.setLogId(uuid);
        sysLogsVo.setOperaUserName(username);
        sysLogsVo.setIp(IpUtils.getIpAddr());
        sysLogsVo.setModel("系统认证模块");
        sysLogsVo.setOperTime(DateUtils.getTime());
        sysLogsVo.setMethodType(SystemGlobal.POST);
        sysLogsVo.setMethod("login()");
        sysLogsVo.setParams("{\"username\":\""+username+"\"}");
        sysLogsVo.setOperateDetail("用户登录系统");
        sysLogsVo.setOperType(OperaType.LOGIN);
        sysLogsVo.setOperUri("/login");
        sysLogsVo.setLogType(0);
        if (StringUtils.isNotEmpty(result)){
            sysLogsVo.setResult(result);
        }else{
            sysLogsVo.setResult(OperaResult.FAIL);
        }
        if (StringUtils.isNotEmpty(userId)){
            sysLogsVo.setUserId(userId);
        }
        sysLogsVo.setFailureReason(message);
        sysLogsVo.setTitle("系统登录");
        sysLogsVo.setResultData(resultData);
       try{
           long times=endTime-startTime;
           sysLogsVo.setTimes(times+"");
           remoteLogService.addLogs(sysLogsVo, SecurityConstants.INNER);
       }catch (Exception e){
           log.error(e.toString(),e);
       }
    }
    /**
     * 记录IP异常信息
     *
     * @param username 用户名
     * @param
     * @param
     * @return
     */
    public void saveErrorLogs(String username, long startTime,String userId, String errMessage) {
        long endTime = System.currentTimeMillis();
        SysLogsVo sysLogsVo = new SysLogsVo();
        sysLogsVo.setGrade("高");
        String uuid= UUID.randomUUID().toString().replace("-","").toUpperCase();
        sysLogsVo.setOperType("登录");
        sysLogsVo.setOperUri("/login");
        sysLogsVo.setLogType(2);
        sysLogsVo.setResult(OperaResult.SUCCESS);
        if (StringUtils.isNotEmpty(userId)){
            sysLogsVo.setUserId(userId);
        }
        sysLogsVo.setIp(IpUtils.getIpAddr());
        sysLogsVo.setResultData("用户登录成功");
        sysLogsVo.setTitle("系统登录");
        sysLogsVo.setModel("系统认证模块");
        sysLogsVo.setOperTime(DateUtils.getTime());
        sysLogsVo.setMethodType(SystemGlobal.POST);
        sysLogsVo.setMethod("login()");
        sysLogsVo.setLogId(uuid);
        sysLogsVo.setOperaUserName(username);
        sysLogsVo.setIp(IpUtils.getIpAddr());
        sysLogsVo.setParams("{\"username\":\""+username+"\"}");
        sysLogsVo.setOperateDetail("用户登录系统");
        sysLogsVo.setErrType(errMessage);
        try{
            if(startTime != 0) {
                long times = endTime - startTime;
                sysLogsVo.setTimes(times + "");
            }
            remoteLogService.addLogs(sysLogsVo, SecurityConstants.INNER);
        }catch (Exception e){
            log.error(e.toString(),e);
        }
    }
    /**
     * 记录登出信息
     *
     * @param username 用户名
     * @param
     * @param message 消息内容
     * @return
     */
    public void saveLogout(String username, String message,String resultData,String userId,String result) {
        SysLogsVo sysLogsVo = new SysLogsVo();
        String uuid= UUID.randomUUID().toString().replace("-","").toUpperCase();
        sysLogsVo.setLogId(uuid);
        sysLogsVo.setOperaUserName(username);
        sysLogsVo.setIp(IpUtils.getIpAddr());
        sysLogsVo.setModel("系统认证模块");
        sysLogsVo.setLogType(0);
        if (StringUtils.isNotEmpty(userId)){
            sysLogsVo.setUserId(userId);
        }
        sysLogsVo.setOperTime(DateUtils.getTime());
        sysLogsVo.setMethodType(SystemGlobal.POST);
        sysLogsVo.setMethod("logout()");
        sysLogsVo.setParams("{\"username\":\""+username+"\"}");
        sysLogsVo.setOperateDetail("用户退出登录");
        sysLogsVo.setOperType("登出");
        sysLogsVo.setOperUri("/logout");
        if (StringUtils.isNotEmpty(result)){
            sysLogsVo.setResult(result);
        }else{
            sysLogsVo.setResult(OperaResult.SUCCESS);
        }
        sysLogsVo.setFailureReason(message);
        sysLogsVo.setTitle("退出登录");
        sysLogsVo.setResultData(resultData);
        try{
            remoteLogService.addLogs(sysLogsVo, SecurityConstants.INNER);
        }catch (Exception e){
            log.error(e.toString(),e);
        }
    }

    /**
     * 记录登录信息
     *
     * @param username 用户名
     * @param
     * @param message 消息内容
     * @return
     */
    public void saveLogin(String username, String message,String resultData,String userId,String result) {
        SysLogsVo sysLogsVo = new SysLogsVo();
        String uuid= UUID.randomUUID().toString().replace("-","").toUpperCase();
        sysLogsVo.setLogId(uuid);
        sysLogsVo.setOperaUserName(username);
        sysLogsVo.setIp(IpUtils.getIpAddr());
        sysLogsVo.setModel("系统认证模块");
        sysLogsVo.setLogType(0);
        if (StringUtils.isNotEmpty(userId)){
            sysLogsVo.setUserId(userId);
        }
        sysLogsVo.setOperTime(DateUtils.getTime());
        sysLogsVo.setMethodType(SystemGlobal.POST);
        sysLogsVo.setMethod("login()");
        sysLogsVo.setParams("{\"username\":\""+username+"\"}");
        sysLogsVo.setOperateDetail("用户登录");
        sysLogsVo.setOperType(OperaType.LOGIN);
        sysLogsVo.setOperUri("/login");
        if (StringUtils.isNotEmpty(result)){
            sysLogsVo.setResult(result);
        }else{
            sysLogsVo.setResult(OperaResult.SUCCESS);
        }
        sysLogsVo.setFailureReason(message);
        sysLogsVo.setTitle("登录");
        sysLogsVo.setResultData(resultData);
        try{
            remoteLogService.addLogs(sysLogsVo, SecurityConstants.INNER);
        }catch (Exception e){
            log.error(e.toString(),e);
        }
    }

    public void saveRegister(String username, String message,String resultData,String userId,String result) {
        SysLogsVo sysLogsVo = new SysLogsVo();
        String uuid= UUID.randomUUID().toString().replace("-","").toUpperCase();
        sysLogsVo.setLogId(uuid);
        sysLogsVo.setOperaUserName(username);
        sysLogsVo.setIp(IpUtils.getIpAddr());
        sysLogsVo.setModel("系统认证模块");
        sysLogsVo.setLogType(0);
        if (StringUtils.isNotEmpty(userId)){
            sysLogsVo.setUserId(userId);
        }
        sysLogsVo.setOperTime(DateUtils.getTime());
        sysLogsVo.setMethodType(SystemGlobal.POST);
        sysLogsVo.setMethod("register()");
        sysLogsVo.setParams("{\"username\":\""+username+"\"}");
        sysLogsVo.setOperateDetail("用户注册");
        sysLogsVo.setOperType(OperaType.REGISTER);
        sysLogsVo.setOperUri("/register");
        if (StringUtils.isNotEmpty(result)){
            sysLogsVo.setResult(result);
        }else{
            sysLogsVo.setResult(OperaResult.SUCCESS);
        }
        sysLogsVo.setFailureReason(message);
        sysLogsVo.setTitle("注册");
        sysLogsVo.setResultData(resultData);
        try{
            remoteLogService.addLogs(sysLogsVo, SecurityConstants.INNER);
        }catch (Exception e){
            log.error(e.toString(),e);
        }
    }
}
