package cn.axzo.workflow.server.common.annotation;

import cn.axzo.workflow.server.common.util.DingTalkUtils;
import cn.azxo.framework.common.utils.LogUtil;
import lombok.extern.slf4j.Slf4j;

import static cn.axzo.workflow.server.common.aspectj.RepeatSubmitAspect.argsArrayToString;


/**
 * 用于 ErrorReporter 注解的报告类型枚举
 *
 * @author wangli
 * @since 2024/4/2 10:36
 */
@Slf4j
public enum ReporterType {

    /**
     * 仅发送钉钉
     */
    ONLY_DING_TALK {
        @Override
        public void executeAction(String profile, String title, Boolean sendDingTalk, Object[] args, String shortString, Throwable e, Boolean downgrade) {
            if (sendDingTalk) {
                DingTalkUtils.sendDingTalk(profile, title, argsArrayToString(args), e);
            }
        }

    },
    /**
     * 仅打印日志
     */
    ONLY_LOG {
        @Override
        public void executeAction(String profile, String title, Boolean sendDingTalk, Object[] args, String shortString, Throwable e, Boolean downgrade) {
            if (downgrade) {
                log.warn("ER: {}", e.getMessage());
            } else {
                LogUtil.error(LogUtil.ErrorType.ERROR_BUSINESS, shortString, e.getMessage(), e);
            }
        }
    },
    /**
     * 上述两种行为均执行
     */
    BOTH {
        @Override
        public void executeAction(String profile, String title, Boolean sendDingTalk, Object[] args, String shortString, Throwable e, Boolean downgrade) {
            if (sendDingTalk) {
                DingTalkUtils.sendDingTalk(profile, title, argsArrayToString(args), e);
            }
            if (downgrade) {
                log.warn("ER: {}", e.getMessage());
            } else {
                LogUtil.error(LogUtil.ErrorType.ERROR_BUSINESS, shortString, e.getMessage(), e);
            }
        }
    };

    /**
     * @param profile      当前应用激活的环境
     * @param title        告警消息标题
     * @param sendDingTalk 是否调用钉钉
     * @param args         请求所有的入参
     * @param shortString  用于日志输出的关键信息
     * @param e            异常对象
     */
    public abstract void executeAction(String profile, String title, Boolean sendDingTalk, Object[] args, String shortString, Throwable e, Boolean downgrade);

}
