package com.tzld.piaoquan.growth.common.common.base;

import com.tzld.piaoquan.growth.common.common.enums.ExceptionCodeEnum;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import static com.tzld.piaoquan.growth.common.common.constant.ResponseConstant.SUCCESS_CODE;
import static com.tzld.piaoquan.growth.common.common.constant.ResponseConstant.SUCCESS_MSG;

/**
 * Common Response
 *
 * @author xueyiming
 */
@Setter
@Getter
@ToString
public class CommonResponse<T> {
    /**
     * 返回状态码，0 表示业务成功
     */
    private int code = 0;
    /**
     * 返回消息
     */
    private String msg = "success";
    /**
     * 业务成功时返回数据
     */
    private T data;

    public static <T> CommonResponse<T> success() {
        CommonResponse<T> commonResponse = new CommonResponse<>();
        commonResponse.setCode(SUCCESS_CODE);
        commonResponse.setMsg(SUCCESS_MSG);
        return commonResponse;
    }

    public static <T> CommonResponse<T> success(T data) {
        CommonResponse<T> commonResponse = new CommonResponse<>();
        commonResponse.setCode(SUCCESS_CODE);
        commonResponse.setMsg(SUCCESS_MSG);
        commonResponse.setData(data);
        return commonResponse;
    }

    public static <T> CommonResponse<T> create() {
        return create(SUCCESS_CODE, SUCCESS_MSG, null);
    }

    public static <T> CommonResponse<T> create(T data) {
        return create(SUCCESS_CODE, SUCCESS_MSG, data);
    }

    public static <T> CommonResponse<T> create(ExceptionCodeEnum exceptionCodeEnum) {
        return create(exceptionCodeEnum.getCode(), exceptionCodeEnum.getMsg(), null);
    }

    public static <T> CommonResponse<T> create(ExceptionCodeEnum exceptionCodeEnum, String msg) {
        return create(exceptionCodeEnum.getCode(), msg, null);
    }

    public static <T> CommonResponse<T> create(int code, String msg) {
        return create(code, msg, null);
    }

    public static <T> CommonResponse<T> create(int code, String msg, T data) {
        CommonResponse<T> commonResponse = new CommonResponse<>();
        commonResponse.setCode(code);
        commonResponse.setMsg(msg);
        commonResponse.setData(data);
        return commonResponse;
    }

    public boolean isSuccess() {
        return this.code == SUCCESS_CODE;
    }

}
