package com.securitycontrol.auth.service;

import com.google.common.collect.Maps;
import com.securitycontrol.common.core.utils.aes.DateTimeHelper;
import com.securitycontrol.common.security.utils.SecurityUtils;
import com.securitycontrol.system.api.domain.SysOperLog;
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 com.securitycontrol.common.core.constant.Constants;
import com.securitycontrol.common.core.constant.SecurityConstants;
import com.securitycontrol.common.core.utils.ServletUtils;
import com.securitycontrol.common.core.utils.StringUtils;
import com.securitycontrol.common.core.utils.ip.IpUtils;
import com.securitycontrol.system.api.RemoteLogService;
import com.securitycontrol.system.api.domain.SysLogininfor;

import java.util.Map;

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

    /**
     * 记录登录信息
     *
     * @param username 用户名
     * @param title    标题
     * @param detail   详情
     * @return
     */
    public void recordLogininfor(String username, String title, String detail) {
        SysOperLog sysOperLog = new SysOperLog();
        sysOperLog.setGrade(title);
        sysOperLog.setTimes(DateTimeHelper.getNowTime());
        sysOperLog.setOperName(username);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (null != loginUser && null != loginUser.getSysUser()) {
            SysUser sysUser = loginUser.getSysUser();
            sysOperLog.setRoleName(sysUser.getRoleName());
            sysOperLog.setDeptName(sysUser.getOrgName());
        }
        sysOperLog.setOperIp(IpUtils.getIpAddr(ServletUtils.getRequest()));
        sysOperLog.setTitle(title);
        sysOperLog.setRequestMethod("POST");
        sysOperLog.setMethod("com.securitycontrol.auth.controller.login()");
        sysOperLog.setBusinessType(0);
        sysOperLog.setOperUrl("/auth/login");
        sysOperLog.setOperParam(username);
        sysOperLog.setDetail(detail);
        sysOperLog.setLogType("系统日志");
        sysOperLog.setSysMenu("");
        remoteLogService.saveLogs(sysOperLog, SecurityConstants.INNER);
    }
}
