package com.dcit.logging.support.factory;

import com.dcit.common.utils.KeyUtil;
import com.dcit.common.utils.SpringContextHolder;
import com.dcit.logging.dao.SysOperationLogMapper;
import com.dcit.logging.dto.LoggingDTO;
import com.dcit.logging.model.SysOperationLog;

import lombok.extern.slf4j.Slf4j;

import java.util.TimerTask;

import org.springframework.beans.BeanUtils;

/**
* <p>Title: LogTaskFactory</p>
* <p>Description: </p>
* <p>Company: SJZ DC-IT co. LTD</p> 
* @author ZZZ
* @date 2018年9月6日 下午16:32:07
*/
@Slf4j
public class LogTaskFactory {

    private static SysOperationLogMapper sysOperationLogMapper = SpringContextHolder.getBean(SysOperationLogMapper.class);

    private LogTaskFactory() {

    }

    public static TimerTask loginLog(LoggingDTO loggingDTO) {
        return new TimerTask() {
            @Override
            public void run() {
                try {
                	SysOperationLog sysOperationLog = new SysOperationLog();
                	BeanUtils.copyProperties(loggingDTO, sysOperationLog);
                	sysOperationLog.setId(KeyUtil.getUUIDKey());
                	sysOperationLog.setLogType("用户登录日志");
                    sysOperationLogMapper.insertSelective(sysOperationLog);
                } catch (Exception e) {
                	e.printStackTrace();
                    log.error("写入用户登录日志异常", e.getMessage());
                }
            }
        };
    }

    public static TimerTask exitLog(LoggingDTO loggingDTO) {
        return new TimerTask() {
            @Override
            public void run() {
                try {
                    SysOperationLog sysOperationLog = new SysOperationLog();
                	BeanUtils.copyProperties(loggingDTO, sysOperationLog);
                	sysOperationLog.setId(KeyUtil.getUUIDKey());
                	sysOperationLog.setLogType("用户退出日志");
                    sysOperationLogMapper.insertSelective(sysOperationLog);
                    
                } catch (Exception e) {
                	e.printStackTrace();
                	log.error("写入用户退出日志异常", e.getMessage());
                }
            }
        };
    }

    public static TimerTask registerLog(LoggingDTO loggingDTO) {
        return new TimerTask() {
            @Override
            public void run() {
                try {
                	
                	SysOperationLog sysOperationLog = new SysOperationLog();
                	BeanUtils.copyProperties(loggingDTO, sysOperationLog);
                	sysOperationLog.setId(KeyUtil.getUUIDKey());
                	sysOperationLog.setLogType("用户注册日志");
                    sysOperationLogMapper.insertSelective(sysOperationLog);
                	
                } catch (Exception e) {
                	e.printStackTrace();
                	log.error("写入用户注册日志异常", e.getMessage());
                }
            }
        };
    }

    public static TimerTask bussinssLog(LoggingDTO loggingDTO) {
        return new TimerTask() {
            @Override
            public void run() {
                try {
                    SysOperationLog sysOperationLog = new SysOperationLog();
                	BeanUtils.copyProperties(loggingDTO, sysOperationLog);
                	sysOperationLog.setId(KeyUtil.getUUIDKey());
                	sysOperationLog.setLogType("业务操作日志");
                    sysOperationLogMapper.insertSelective(sysOperationLog);
                    
                } catch (Exception e) {
                	e.printStackTrace();
                	log.error("写入业务操作日志异常", e.getMessage());
                }
            }
        };
    }

    public static TimerTask exceptionLog(LoggingDTO loggingDTO) {
        return new TimerTask() {
            @Override
            public void run() {
                try {
                	
                	SysOperationLog sysOperationLog = new SysOperationLog();
                	BeanUtils.copyProperties(loggingDTO, sysOperationLog);
                	sysOperationLog.setId(KeyUtil.getUUIDKey());
                	sysOperationLog.setLogType("业务异常日志");
                    sysOperationLogMapper.insertSelective(sysOperationLog);
                	
                } catch (Exception e) {
                	e.printStackTrace();
                	log.error("写入业务异常日志异常", e.getMessage());
                }
            }
        };
    }


}
