package com.skr.mdm.result;

import com.skr.mdm.enums.ResultCodeEnum;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author nfq
 * @date 2020/7/9 16:42
 * 数据传输对象
 */
@Data
@NoArgsConstructor
public class ResponseResult {

    private String code;

    private String msg;

    private Object data;

    private String secretKey;

    private String sign;

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

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

    private void resultCode(ResultCodeEnum resultCodeEnum) {
        this.code = resultCodeEnum.getCode();
        this.msg = resultCodeEnum.getMsg();
    }

    private void resultFailure(String msg) {
        this.code = ResultCodeEnum.FAILURE.getCode();
        this.msg = msg;
    }

    private void resultClientFailure(String msg) {
        this.code = ResultCodeEnum.CLIENT_ERROR.getCode();
        this.msg = msg;
    }

    public static ResponseResult success() {
        ResponseResult result = new ResponseResult();
        result.resultCode(ResultCodeEnum.SUCCESS);
        return result;
    }

    public static<T> ResponseResult success(T data) {
        ResponseResult result = new ResponseResult();
        result.resultCode(ResultCodeEnum.SUCCESS);
        result.setData(data);
        return result;
    }

    public static ResponseResult failure() {
        ResponseResult result = new ResponseResult();
        result.resultCode(ResultCodeEnum.FAILURE);
        return result;
    }

    public static<T> ResponseResult failure(T data) {
        ResponseResult result = new ResponseResult();
        result.resultCode(ResultCodeEnum.FAILURE);
        result.setData(data);
        return result;
    }

    public static ResponseResult failure(ResultCodeEnum resultCodeEnum) {
        ResponseResult result = new ResponseResult();
        result.setCode(resultCodeEnum.getCode());
        result.setMsg(resultCodeEnum.getMsg());
        return result;
    }

    public static<T> ResponseResult failure(ResultCodeEnum resultCodeEnum, T data) {
        ResponseResult result = new ResponseResult();
        result.setCode(resultCodeEnum.getCode());
        result.setMsg(resultCodeEnum.getMsg());
        result.setData(data);
        return result;
    }


    public static ResponseResult failure(String msg) {
        ResponseResult result = new ResponseResult();
        result.resultClientFailure(msg);
        return result;
    }

    public static ResponseResult failure(String code, String msg) {
        ResponseResult result = new ResponseResult();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

}
