package com.saye.hrs.commons.log;

import com.saye.hrs.commons.date.DateDUtil;
import com.saye.hrs.mapper.system.LoggerMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;

@Component
public class LogUtil {
	private static LoggerMapper loggerMapper;
	
	@Autowired
	public void setLoggerMapper(LoggerMapper loggerMapper) {
		LogUtil.loggerMapper = loggerMapper;
	}
	
	public static void debug(Class c,String msg) {
		Logger logger=LoggerFactory.getLogger(c);
        logger.debug(msg);	
	}

//	public static void info(Class c,String msg) {
//		Logger logger=LoggerFactory.getLogger(c);
//        logger.info(msg);
//	}
//	
//	public static void error(Class c,String msg) {
//		Logger logger=LoggerFactory.getLogger(c);
//        logger.error(msg);
//	}
	
	public static void info(Class c,String msg){
		HashMap<Object, Object> map=new HashMap<Object, Object>();
		
		try {
			String name = c.getName();
			if(name.indexOf("Controller")!=-1){
			

				map.put("logtype", "INFO");
				map.put("information",msg);
				map.put("create_time", DateDUtil.getCurrentDate(DateDUtil.yyyy_MM_dd_HH_mm_ss));
				map.put("user_id","");
				map.put("ip", "");
				loggerMapper.addLog(map);
			
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			Logger logger=LoggerFactory.getLogger(c);
			logger.info(msg);
		}
	}

	public static void info(Class c,String msg,String ip,String userName){
		HashMap<Object, Object> map=new HashMap<Object, Object>();

		try {
			String name = c.getName();
			if(name.indexOf("Controller")!=-1){

				map.put("logtype", "INFO");
				map.put("information",msg);
				map.put("create_time", DateDUtil.getCurrentDate(DateDUtil.yyyy_MM_dd_HH_mm_ss));
				map.put("user_id",userName);
				map.put("ip", ip);
				loggerMapper.addLog(map);

			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			Logger logger=LoggerFactory.getLogger(c);
			logger.info(msg);
		}
	}
	
	public static void error(Class c,String msg){
		HashMap<Object, Object> map=new HashMap<Object, Object>();

		try {
			
			String name = c.getName();
			if(name.indexOf("Controller")!=-1){
					
				map.put("logtype", "ERROR");
				map.put("information",msg);
				map.put("create_time", DateDUtil.getCurrentDate(DateDUtil.yyyy_MM_dd_HH_mm_ss));
				map.put("user_id","");
				map.put("ip", "");
				loggerMapper.addLog(map);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			Logger logger=LoggerFactory.getLogger(c);
			logger.error(msg);
		}
	}

	public static void error(Class c,String msg,String ip,String userName){
		HashMap<Object, Object> map=new HashMap<Object, Object>();

		try {

			String name = c.getName();
			if(name.indexOf("Controller")!=-1){

				map.put("logtype", "ERROR");
				map.put("information",msg);
				map.put("create_time", DateDUtil.getCurrentDate(DateDUtil.yyyy_MM_dd_HH_mm_ss));
				map.put("user_id",userName);
				map.put("ip", ip);
				loggerMapper.addLog(map);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			Logger logger=LoggerFactory.getLogger(c);
			logger.error(msg);
		}
	}
}	
