package com.dayou.exception;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;

import java.util.Objects;

/**
 * Define the error id.
 */
public class ErrorCode {
    public static final ErrorCode PWD_ERROR = ErrorCode("001", "账号/密码错误");
    public static final ErrorCode FORM_EXPIRED = ErrorCode("002", "页面过期，请刷新后重试");
    public static final ErrorCode FORM_DUPLICATE = ErrorCode("003", "请勿重复提交");
    public static final ErrorCode DATA_NOT_EXISTS = ErrorCode("004", "数据不存在");

    public static final ErrorCode RAS_IMPORT_ERROR = ErrorCode("10000", "导入excel出错 {0}");

    public static final ErrorCode NO_AUTH = ErrorCode("10001", "用户无权限");
    public static final ErrorCode UN_LOGIN = ErrorCode("10004", "用户未登录");
    public static final ErrorCode LOGIN_EXPIRE = ErrorCode("10005", "登录过期");
    public static final ErrorCode DEFAULTERROR = ErrorCode("10006", "系统繁忙");
    public static final ErrorCode PARAM_ERROR = ErrorCode("10007", "参数错误");

    public static final ErrorCode INVALID_TOKEN = ErrorCode("10008", "请登录");
    public static final ErrorCode DATA_EXIST = ErrorCode("10009", "数据已存在");

    public static final ErrorCode CUSTOM_ERROR = ErrorCode("10010", "{0}");

    public static final ErrorCode USER_DISABLE = ErrorCode("10011", "用户被禁用,请联系管理员");
    public static final ErrorCode STAGE_NODE_ERROR = ErrorCode("10012", "请选择上个阶段");
    public static final ErrorCode STAGE_NODE_ERROR1 = ErrorCode("10013", "不能选择自己为上阶段");

    public static final ErrorCode PASSWORD_DIFFERENT = ErrorCode("10014", "两次输入的密码不一致,请检查.");

    public static final ErrorCode PASSWORD_ERROR= ErrorCode("10015", "原密码错误,请检查.");

    public static final ErrorCode FILE_EXT_ERROR = ErrorCode("10016", "不支持此文件扩展名");

    public static final ErrorCode BROKERAGE_ITEM_STATE_ERROR = ErrorCode("10017", "项目已被结算，无法再次计算提成");

    public static final ErrorCode BROKERAGE_ITEM_STATE_ERROR1 = ErrorCode("10018", "抱歉,项目已被结算不支持修改项目信息.");

    public static final ErrorCode BROKERAGE_ITEM_STATE_ERROR2 = ErrorCode("10019", "当月回款目标未达成.");

    public static final ErrorCode BROKERAGE_SETTLE_FAILED = ErrorCode("10020", "项目评价失败，请检查项目排期或提成权重。");

    public static final ErrorCode ITEM_EVALUATE_ERROR = ErrorCode("10021", "抱歉！当前项目还未完成，暂时无法评价。");

    public static final ErrorCode GIS_USER_EXPIRY = ErrorCode("10022", "抱歉！该账号已过期，请联系管理员。");

    public static final ErrorCode GIS_USER_DISABLE = ErrorCode("10023", "抱歉！该账号已被禁用，请联系管理员。");

    public static final ErrorCode STAGE_NODE_ERROR2 = ErrorCode("10024", "抱歉，该项目阶段权重之和已超过100%，请检查后修改。");

    public static final ErrorCode STAGE_NODE_ERROR3 = ErrorCode("10025", "抱歉，该阶段参与人员提成系数之和已超过100%，请检查后修改。");

    public static final ErrorCode SETTLE_PASSWORD_ERROR = ErrorCode("10026", "抱歉，查询密码错误！请确认是当前登录用户本人查询。");

    public static final ErrorCode GENERATE_REPORT_ERROR = ErrorCode("10027", "报告生成失败，请联系管理员。");

    public static final ErrorCode WORKFLOW_NOT_EXISTS = ErrorCode("10028", "{0} 工作流不存在，或者已被禁用。无法下单请联系系统管理员。");

    public static final ErrorCode OPENING_NODE_NOT_EXISTS = ErrorCode("10029", "{0} 工作流下单节点不存在。无法下单请联系系统管理员。");

    public static final ErrorCode CURRENT_NODE_NOT_EXISTS = ErrorCode("10030", "当前节点未找到，请联系系统管理员。");

    public static final ErrorCode SORTED_DEPARTMENT_NOT_EXISTS = ErrorCode("10031", "{0} 业务下单还没有配置轮单部门，请先配置轮单部门。");

    public static final ErrorCode COMMIT_STATE_ERROR = ErrorCode("10032", "节点状态错误，请检查。");

    public static final ErrorCode TASK_NOT_FINISHED = ErrorCode("10033", "此节点上的任务还未完成，请完成后再提交流程。");

    public static final ErrorCode MANY_LEADERS_EXCEPTION = ErrorCode("10034", "下一节点由您直接领导审批，但系统找到您有多个直接领导，请手动选择审批人。");

    public static final ErrorCode PERMISSION_ERROR = ErrorCode("10035","抱歉，您无权限处理此任务。");

    public static final ErrorCode PERMISSION_IS_NULL = ErrorCode("10036","{0} 任务没有配置审批人权限，请联系管理员。");

    public static final ErrorCode TAKE_NUMBER_FAILED = ErrorCode("10037","取号失败，估价对象已取号，请勿重复取号。");

    public static final ErrorCode PRODUCTION_EXCEPTION = ErrorCode("10038","该估价对象已出过 {0}");

    public static final ErrorCode NODE_STATE_EXCEPTION = ErrorCode("10039","该节点状态不在 进行中，无法在此节点下完成任务，若需请退回至当前节点。");

    public static final ErrorCode NEXT_NODE_NOT_EXISTS = ErrorCode("10040", "下一节点未找到，请联系系统管理员。");

    public static final ErrorCode P_AMOUNT_BIGGER_O_AMOUNT = ErrorCode("10041", "订单号：[{0}]下的报告号分配金额之后大于订单认领金额，请检查后再认领。");

    public static final ErrorCode DATA_OUT_OFF = ErrorCode("10042", "页面数据超时,请刷新页面。");

    public static final ErrorCode BATCH_COMMIT_ERROR = ErrorCode("10043", "订单号：{0}、产品号：{1}的流程批量提交失败，请检查后重试。");

    private String code;
    private String errorMsg;

    public static ErrorCode ErrorCode(String code, String errorMsg) {
        return new ErrorCode(code, errorMsg);
    }

    private ErrorCode() {
    }


    public ErrorCode(String code, String errorMsg) {
        this.code = code;
        this.errorMsg = errorMsg;
    }

    public static void throwBusinessException(ErrorCode errorCode, Object... values) {
        throw buildBusinessException(errorCode, values);
    }

    public static BusinessException buildBusinessException(ErrorCode errorCode, Object... values) {
        String message = errorCode.errorMsg;
        if (ArrayUtils.isNotEmpty(values)) {
            for (int i = 0; i < values.length; i++) {
                if (message.contains("{") && message.contains("}")) {
                    message = message.replaceFirst("\\{" + i + "\\}", values[i].toString());
                } else {
                    message += " " + values[i].toString() + " ";
                }
            }
        }

        return new BusinessException(message, errorCode.code);
    }

    public static void throwBusinessException(String msg, String code) {
        throw new BusinessException(msg, code == null ? DEFAULTERROR.getCode() : code);
    }


    public String getCode() {
        return code;
    }

    public String getErrorMsg() {
        return errorMsg;
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this)
                .append("id", code)
                .append("errorMsg", errorMsg)
                .toString();
    }

    public void setCode(String code) {
        this.code = code;
    }

    public void setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
    }

    public static void throwExceptionIfNull(Object o){
        if(Objects.isNull(o)){
            throwBusinessException(DATA_NOT_EXISTS);
        }
    }
}
