All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.didiglobal.logi.security.common.Result Maven / Gradle / Ivy

Go to download

logi-security 提供项目大多都需要的一些基础功能(用户、角色、权限、登录、注册、操作记录)

There is a newer version: 2.10.19
Show newest version
package com.didiglobal.logi.security.common;

import com.didiglobal.logi.security.common.enums.ResultCode;
import com.didiglobal.logi.security.exception.LogiSecurityException;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;


/**
 * 非分页统一的返回规范
 *
 * @author cjm
 */
@EqualsAndHashCode(callSuper = true)
@Data
@ApiModel(description = "统一返回格式")
public class Result extends BaseResult {

    @ApiModelProperty(value = "返回数据")
    protected T data;

    public boolean successed() {
        return getCode() != null && ResultCode.SUCCESS.getCode().equals(getCode());
    }

    public boolean duplicate() {
        return getCode() != null && ResultCode.RESOURCE_DUPLICATION.getCode().equals(getCode());
    }

    public boolean failed() {
        return !successed();
    }

    public Result() {}

    private Result(Integer code) {
        this.code = code;
    }

    private Result(Integer code, String msg) {
        this.code = code;
        this.message = msg;
    }

    public static  Result build(boolean succ) {
        if (succ) {
            return success();
        }
        return fail();
    }

    public static  Result success(T data) {
        Result ret = new Result<>(ResultCode.SUCCESS.getCode());
        ret.setMessage(ResultCode.SUCCESS.getMessage());
        ret.setData(data);
        return ret;
    }

    public static  Result success() {
        return new Result<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage());
    }

    public static  Result fail(ResultCode resultCode) {
        Result ret = new Result<>(resultCode.getCode());
        ret.setMessage(resultCode.getMessage());
        return ret;
    }

    public static  Result fail(Integer code, String msg) {
        Result ret = new Result<>(code);
        ret.setMessage(msg);
        return ret;
    }

    public static  Result fail(String msg) {
        Result ret = new Result<>(ResultCode.COMMON_FAIL.getCode());
        ret.setMessage(msg);
        return ret;
    }

    public static  Result fail() {
        Result result = new Result<>();
        result.setCode(ResultCode.COMMON_FAIL.getCode());
        result.setMessage(ResultCode.COMMON_FAIL.getMessage());
        return result;
    }

    public static  Result fail(LogiSecurityException e) {
        String[] s = e.getMessage().split("-", 2);
        return Result.fail(Integer.parseInt(s[0]), s[1]);
    }

    public static  Result buildSucc(T data) {
        Result result = new Result<>();
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMessage(ResultCode.SUCCESS.getMessage());
        result.setData(data);
        return result;
    }

    public static  Result buildFrom(Result result) {
        Result resultT = new Result<>();
        resultT.setCode(result.getCode());
        resultT.setMessage(result.getMessage());
        return resultT;
    }

    public static  Result buildParamIllegal(String msg) {
        Result result = new Result<>();
        result.setCode(ResultCode.PARAM_NOT_VALID.getCode());
        result.setMessage(ResultCode.PARAM_NOT_VALID.getMessage() + ":" + msg + ",请检查后再提交!");
        return result;
    }

    public static  Result buildNotExist(String msg) {
        Result result = new Result<>();
        result.setCode(ResultCode.RESOURCE_TYPE_NOT_EXISTS.getCode());
        result.setMessage(msg);
        return result;
    }

    public static  Result buildDuplicate(String msg) {
        Result result = new Result<>();
        result.setCode(ResultCode.RESOURCE_DUPLICATION.getCode());
        result.setMessage(msg);
        return result;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy