package com.bjtdba.userCenter.exception;

import lombok.Getter;

public class BusinessException extends RuntimeException {
    @Getter
    private final int code;
    private final String message;

    public BusinessException(int code, String message) {
        super(message);
        this.code = code;
        this.message = message;
    }

    public BusinessException(String message) {
        this(400, message); // 默认400错误码
    }

    // 常用的预定义异常
    public static BusinessException of(String message) {
        return new BusinessException(message);
    }

    public static BusinessException userNotFound() {
        return new BusinessException(404, "用户不存在");
    }

    public static BusinessException userIsExist() {
        return new BusinessException(500, "用户已存在");
    }

    public static BusinessException userAccountIsExist() {
        return new BusinessException(500, "用户账号已存在");
    }

    public static BusinessException insufficientScore() {
        return new BusinessException(403, "用户积分不足");
    }

    public static BusinessException updateScoreFailed() {
        return new BusinessException(500, "积分更新失败，请重试");
    }

    public static BusinessException insufficientContribute() {
        return new BusinessException(403, "用户贡献值不足");
    }

    public static BusinessException createContributeFailed() {
        return new BusinessException(500, "贡献值记录创建失败，请重试");
    }

    public static BusinessException updateContributeFailed() {
        return new BusinessException(500, "贡献值更新失败，请重试");
    }

    public static BusinessException createUserFailed() {
        return new BusinessException(500, "创建用户信息失败");
    }

    public static BusinessException updateUserFailed() {
        return new BusinessException(500, "更新用户信息失败");
    }

    @Override
    public String getMessage() {
        return message;
    }
}