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.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 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.setOperaTime(DateUtils.getTime());
        sysLogsVo.setMethodType(SystemGlobal.POST);
        sysLogsVo.setMethod("login()");
        sysLogsVo.setParams("{\"username\":\""+username+"\"}");
        sysLogsVo.setOperateDetail("用户登陆系统");
        sysLogsVo.setOperaType("登陆");
        sysLogsVo.setOperaUri("/login");
        sysLogsVo.setLogType(0);
        if (StringUtils.isNotEmpty(result)){
            sysLogsVo.setResult(result);
        }else{
            sysLogsVo.setResult("失败");
        }
        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) {
        long endTime = System.currentTimeMillis();
        SysLogsVo sysLogsVo = new SysLogsVo();
        sysLogsVo.setGrade("高");
        String uuid= UUID.randomUUID().toString().replace("-","").toUpperCase();
        sysLogsVo.setOperaType("登陆");
        sysLogsVo.setOperaUri("/login");
        sysLogsVo.setLogType(2);
        sysLogsVo.setResult("成功");
        if (StringUtils.isNotEmpty(userId)){
            sysLogsVo.setUserId(userId);
        }
        sysLogsVo.setResultData("用户登陆成功");
        sysLogsVo.setTitle("系统登陆");
        sysLogsVo.setModel("系统登陆");
        sysLogsVo.setOperaTime(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("IP异常");
        try{
            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.setOperaTime(DateUtils.getTime());
        sysLogsVo.setMethodType(SystemGlobal.POST);
        sysLogsVo.setMethod("logout()");
        sysLogsVo.setParams("{\"username\":\""+username+"\"}");
        sysLogsVo.setOperateDetail("用户退出登录");
        sysLogsVo.setOperaType("登出");
        sysLogsVo.setOperaUri("/logout");
        if (StringUtils.isNotEmpty(result)){
            sysLogsVo.setResult(result);
        }else{
            sysLogsVo.setResult("成功");
        }
        sysLogsVo.setFailureReason(message);
        sysLogsVo.setTitle("退出登录");
        sysLogsVo.setResultData(resultData);
        try{
            remoteLogService.addLogs(sysLogsVo, SecurityConstants.INNER);
        }catch (Exception e){
            log.error(e.toString(),e);
        }
    }
}
