package com.fs.common.core.domain;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fs.common.enums.BizResponseEnum;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;


/**
 * @author 曹丽芹
 */
@Getter
@Setter
@Builder
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResponseResult<T> {
    /**
     * 自定义异常码
     */
    private Integer code;

    /**
     * 自定义异常信息
     */
    private String msg;

    /**
     * 响应数据
     */
    private T data;

    /**
     * 额外数据
     */
    private Object ext;

    public ResponseResult() {
    }

    public ResponseResult(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public ResponseResult(BizResponseEnum exceptionEnum) {
        this.code = exceptionEnum.getCode();
        this.msg = exceptionEnum.getMsg();
    }

    public ResponseResult(Integer code, String msg, Object ext) {
        this.code = code;
        this.msg = msg;
        this.ext = ext;
    }

    public ResponseResult(BizResponseEnum exceptionEnum, Object ext) {
        this.code = exceptionEnum.getCode();
        this.msg = exceptionEnum.getMsg();
        this.ext = ext;
    }

    public ResponseResult(Integer code, String msg, T data, Object ext) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.ext = ext;
    }

    public static <T> ResponseResult<T> ok() {
        return ResponseResult.ok(null);
    }

    public static <T> ResponseResult<T> ok(T data) {
        ResponseResult<T> ok = new ResponseResult<>();
        ok.setCode(200);
        ok.setMsg("success");
        ok.setData(data);
        return ok;
    }

    public static <T> ResponseResult<T> fail(BizResponseEnum exceptionEnum) {
        return ResponseResult.fail(exceptionEnum, null);
    }

    public static <T> ResponseResult<T> fail(BizResponseEnum exceptionEnum, Object ext) {
        ResponseResult<T> ok = new ResponseResult<>();
        ok.setCode(exceptionEnum.getCode());
        ok.setMsg(exceptionEnum.getMsg());
        ok.setExt(ext);
        return ok;
    }

    public static <T> ResponseResult<T> fail(Integer code, String msg) {
        ResponseResult<T> ok = new ResponseResult<>();
        ok.setCode(code);
        ok.setMsg(msg);
        return ok;
    }
}
