/**
 * bobotuan.com Inc.
 * Copyright (c) 2004-2015 All Rights Reserved.
 */
package com.qs.mp.common.utils;

import com.qs.mp.common.enums.ServerEnvEnum;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * 日志
 *
 * @author chunping.zhongcp
 * @version $Id: LogUtil.java, v 0.1 2020/5/21 chunping.zhongcp Exp $
 */
@Component
public class LogUtil {


    private static String env;

    @Value(value = "${server.env}")
    public void setEnv(String serverEnv) {
        env = serverEnv;
    }

    /**
     * 打印debug日志
     *
     * @param logger
     * @param message
     */
    public static void debug(final Logger logger, final String message) {
        debug(logger, message, null);
    }

    /**
     * 打印debug日志
     *
     * @param logger
     * @param message
     * @param params
     */
    public static void debug(final Logger logger, final String message, final Object... params) {
        if (logger != null) {
            if (logger.isDebugEnabled()) {
                logger.debug(getLogString(message, params));
            }
        }
    }

    /**
     * 打印info日志
     *
     * @param logger  日志对象
     * @param message 需要打印的消息
     */
    public static void info(final Logger logger, final String message) {
        info(logger, message, null);
    }

    /**
     * 打印info日志
     *
     * @param logger
     * @param message
     * @param params
     */
    public static void info(final Logger logger, final String message, final Object... params) {
        if (logger != null) {
            if (logger.isInfoEnabled()) {
                logger.info(getLogString(message, params));
            }
        }
    }

    /**
     * 打印info日志
     *
     * @param logger
     * @param message
     * @param params
     */
    public static void log(final Logger logger, final String message, final Object... params) {
        if (logger != null) {
            if (logger.isInfoEnabled()) {
                logger.info(logString(message, params));
            }
        }
    }

    /**
     * 打印warn日志
     *
     * @param logger  日志对象
     * @param message 需要打印的消息
     */
    public static void warn(final Logger logger, final String message) {
        warn(logger, message, null);
    }

    /**
     * 打印warn日志
     *
     * @param logger
     * @param message
     * @param params
     */
    public static void warn(final Logger logger, final String message, final Object... params) {
        if (logger != null) {
            if (logger.isWarnEnabled()) {
                logger.warn(getLogString(message, params));
            }
        }
    }

    /**
     * 打印warn日志
     *
     * @param logger  日志对象
     * @param message 需要打印的消息
     */
    public static void warn(final Logger logger, final Throwable throwable, final String message) {
        warn(logger, throwable, message, null);
    }

    /**
     * 打印warn日志
     *
     * @param logger
     * @param throwable
     * @param message
     * @param params
     */
    public static void warn(final Logger logger, final Throwable throwable, final String message,
                            final Object... params) {
        if (logger != null) {
            if (logger.isWarnEnabled()) {
                logger.warn(getLogString(message, params), throwable);
            }
        }
    }

    /**
     * 打印error日志
     *
     * @param logger
     * @param message
     */
    public static void error(final Logger logger, final String message) {
        error(logger, message, null);
    }

    /**
     * 打印error日志
     *
     * @param logger
     * @param message
     * @param params
     */
    public static void error(final Logger logger, final String message, final Object... params) {
        if (logger != null) {
            if (logger.isErrorEnabled()) {
                logger.error(getLogString(message, params));
            }
        }

        if (ServerEnvEnum.PROD.getCode().equals(env)) {
//            DingService.sendAlertDing(getLogString(message, params));
        }
    }

    /**
     * 打印error日志
     *
     * @param logger    日志对象
     * @param throwable 异常堆栈
     * @param message   需要打印的消息
     */
    public static void error(final Logger logger, final Throwable throwable, final String message) {
        error(logger, throwable, message, null);
    }

    /**
     * 打印error日志
     *
     * @param logger
     * @param message
     * @param throwable
     * @param params
     */
    public static void error(final Logger logger, final Throwable throwable, final String message,
                             final Object... params) {
        if (logger != null) {
            if (logger.isErrorEnabled()) {
                logger.error(getLogString(message, params), throwable);
            }
        }
        if(ServerEnvEnum.PROD.getCode().equals(env)){

//            DingService.sendAlertDing(getLogString(message, params) + "/n" + throwable);
        }

    }

    /**
     * 生成输出到日志的字符串。
     *
     * @param msgTemplate 日志描述信息
     * @param param       任意个要输出到日志的参数[可空]
     * @return 输出到日志的字符串
     */
    private static String getLogString(String msgTemplate, Object... param) {

        StringBuilder sb = new StringBuilder();
        if (param == null || param.length == 0) {
            sb.append(msgTemplate);
        } else {
            sb.append(MessageHelper.formatMsg(msgTemplate, param));
        }
        return sb.toString();
    }

    /**
     *
     * @param msgTemplate
     * @param param
     * @return
     */
    private static String logString(String msgTemplate, Object... param) {
        StringBuilder sb = new StringBuilder();
        if (param == null || param.length == 0) {
            sb.append(msgTemplate);
        } else {
            sb.append(MessageHelper.formatMsg(msgTemplate, param));
        }
        return sb.toString();
    }

}
