package com.limap.common.log.manager.factory;

import java.util.TimerTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.limap.common.constant.Constants;
import com.limap.common.utils.LogUtils;
import com.limap.common.utils.ServletUtils;
import com.limap.common.utils.StringUtils;
import com.limap.common.utils.ip.AddressUtils;
import com.limap.common.utils.ip.IpUtils;
import com.limap.common.utils.spring.SpringUtils;
import com.limap.common.core.domain.entity.SysOperLog;
import com.limap.common.core.domain.entity.SysLogininfor;
import com.limap.common.api.SystemApi;
import eu.bitwalker.useragentutils.UserAgent;

/**
 * 异步工厂（产生任务用）
 * 
 * @author limap
 */
public class AsyncFactory
{
    private static final Logger sys_user_logger = LoggerFactory.getLogger("sys-user");

    /**
     * 记录登录信息
     * 
     * @param username 用户名
     * @param status 状态
     * @param message 消息
     * @param args 列表
     * @return 任务task
     */
    public static TimerTask recordLogininfor(final String username, final String status, final String message,
            final Object... args)
    {
        String os = "";
        String browser = "";
        String ip = "";
        try{
            UserAgent userAgent   = UserAgent.parseUserAgentString(ServletUtils.getRequest().getHeader("User-Agent"));
            // 获取客户端操作系统
            os = userAgent.getOperatingSystem().getName();
            // 获取客户端浏览器
            browser = userAgent.getBrowser().getName();
            ip = IpUtils.getIpAddr();
        } catch (Exception ex){

        }

        String finalBrowser = browser;
        String finalOs = os;
        String finalIp = ip;
        return new TimerTask()
        {
            @Override
            public void run()
            {
                String address = AddressUtils.getRealAddressByIP(finalIp);
                StringBuilder s = new StringBuilder();
                s.append(LogUtils.getBlock(finalIp));
                s.append(address);
                s.append(LogUtils.getBlock(username));
                s.append(LogUtils.getBlock(status));
                s.append(LogUtils.getBlock(message));
                // 打印信息到日志
                sys_user_logger.info(s.toString(), args);

                SysLogininfor logininfor = new SysLogininfor();
                logininfor.setUserName(username);
                logininfor.setIpaddr(finalIp);
                logininfor.setLoginLocation(address);
                logininfor.setBrowser(finalBrowser);
                logininfor.setOs(finalOs);
                logininfor.setMsg(message);
                // 日志状态
                if (StringUtils.equalsAny(status, Constants.LOGIN_SUCCESS, Constants.LOGOUT, Constants.REGISTER))
                {
                    logininfor.setStatus(Constants.SUCCESS);
                }
                else if (Constants.LOGIN_FAIL.equals(status))
                {
                    logininfor.setStatus(Constants.FAIL);
                }
                // 插入数据
                SpringUtils.getBean(SystemApi.class).insertLogininfor(logininfor);
            }
        };
    }

    /**
     * 操作日志记录
     * 
     * @param operLog 操作日志信息
     * @return 任务task
     */
    public static TimerTask recordOper(final SysOperLog operLog)
    {
        return new TimerTask()
        {
            @Override
            public void run()
            {
                // 远程查询操作地点
                operLog.setOperLocation(AddressUtils.getRealAddressByIP(operLog.getOperIp()));
                SpringUtils.getBean(SystemApi.class).insertOperlog(operLog);
            }
        };
    }
}
