package com.securitycontrol.auth.service;

import com.securitycontrol.common.core.constant.SecurityConstants;
import com.securitycontrol.common.core.utils.ServletUtils;
import com.securitycontrol.common.core.utils.aes.DateTimeHelper;
import com.securitycontrol.common.core.utils.ip.IpUtils;
import com.securitycontrol.common.core.utils.uuid.IdUtils;
import com.securitycontrol.common.security.utils.SecurityUtils;
import com.securitycontrol.system.api.RemoteLogService;
import com.securitycontrol.system.api.domain.SysLog;
import com.securitycontrol.system.api.domain.SysUser;
import com.securitycontrol.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import sun.security.krb5.internal.PAData;

import javax.annotation.Resource;
import java.util.UUID;

/**
 * 记录日志方法
 *
 * @author czc
 */
@Component
public class SysRecordLogService {

    @Resource
    private RemoteLogService remoteLogService;

    /**
     * 记录登录信息
     *
     * @param username  用户名
     * @param model     操作模块
     * @param detail    详情
     * @param operaType 1.登录 2.登出
     * @return
     */
    public void recordLogininfor(String username, Long userId, String model, String detail, int operaType,int result,String params,long startTime) {
        long endTime = System.currentTimeMillis();

        SysLog sysLog = new SysLog();
        String id = IdUtils.getuid();
        sysLog.setLogId(id);
        sysLog.setOperTime(DateTimeHelper.getNowTime());
        sysLog.setOperaUserName(username);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (null != loginUser && null != loginUser.getSysUser()) {
            SysUser sysUser = loginUser.getSysUser();
            sysLog.setUserId(sysUser.getUserId());
        }
        if(userId != null){
            sysLog.setUserId(userId);
        }
        sysLog.setTitle("用户登录");
        long times=endTime-startTime;
        sysLog.setTimes(times+"");
        sysLog.setIp(IpUtils.getIpAddr(ServletUtils.getRequest()));
        sysLog.setModel(model);
        sysLog.setOperType(model);
        sysLog.setOperateDetail(model);
        sysLog.setMethod(operaType == 1 ? "com.securitycontrol.auth.controller.login()" : "com.securitycontrol.auth.controller.logout()");
        sysLog.setResult(result);
        sysLog.setOperUri(operaType == 1 ? "/auth/login" : "/auth/logout");
        sysLog.setFailureReason(detail);
        sysLog.setParams(params);
        sysLog.setLogType(0);
        remoteLogService.saveSysLog(sysLog, SecurityConstants.INNER);
    }

    /**
     * 记录登录信息
     *
     * @param username  用户名
     * @param model     操作模块
     * @param detail    详情
     * @param operaType 1.登录 2.登出
     * @return
     */
    public void errorLogs(String username, Long userId, String model, String detail, int operaType,int result,String params,long startTime) {
        SysLog sysLog = new SysLog();
        long endTime = System.currentTimeMillis();
        sysLog.setLogType(2);

        sysLog.setOperaUserName(username);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (null != loginUser && null != loginUser.getSysUser()) {
            SysUser sysUser = loginUser.getSysUser();
            sysLog.setUserId(sysUser.getUserId());
        }
        String id = IdUtils.getuid();
        sysLog.setLogId(id);
        if(userId != null){
            sysLog.setUserId(userId);
        }
        sysLog.setTitle("用户登录");
        long times=endTime-startTime;
        sysLog.setTimes(times+"");
        sysLog.setIp(IpUtils.getIpAddr(ServletUtils.getRequest()));
        sysLog.setModel(model);
        sysLog.setOperType(model);
        sysLog.setOperateDetail("用户登录系统");
        sysLog.setMethod("com.securitycontrol.auth.controller.login()" );
        sysLog.setResult(result);
        sysLog.setOperUri("/auth/login");
        sysLog.setFailureReason(detail);
        sysLog.setParams(params);
        sysLog.setGrade("高");
        sysLog.setErrType("IP异常");
        sysLog.setFailureReason("IP异常");
        sysLog.setOperTime(DateTimeHelper.getNowTime());
        remoteLogService.saveSysLog(sysLog, SecurityConstants.INNER);
    }

}
