package com.crm.rely.backend.core.dto.base;

import com.crm.rely.backend.core.constant.Constants;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
public class BaseResultDto<T> {

    private int code;

    private String msg;

    private T data;

    public BaseResultDto(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

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

    /**
     * 系统错误 500 system error
     *
     * @return
     */
    public static BaseResultDto systemError() {
        return error(Constants.SYSTEM_ERROR_CODE, Constants.SYSTEM_ERROR);
    }

    /**
     * 默认错误 400 fail
     *
     * @return
     */
    public static BaseResultDto error() {
        return error(Constants.FAIL);
    }

    /**
     * 默认错误code 400
     *
     * @param msg 错误消息内容
     * @return
     */
    public static BaseResultDto error(String msg) {
        return error(Constants.FAILED_CODE, msg);
    }

    /**
     * 错误
     *
     * @param code
     * @param msg
     * @return
     */
    public static BaseResultDto error(int code, String msg) {
        return result(code, msg);
    }

    public static BaseResultDto success() {
        return success(Constants.SUCCESS, null);
    }

    public static BaseResultDto success(Object data) {
        return success(Constants.SUCCESS, data);
    }

    public static BaseResultDto success(String msg) {
        return success(msg, null);
    }

    public static BaseResultDto success(String msg, Object data) {
        return result(Constants.SUCCESS_CODE, msg, data);
    }

    public static BaseResultDto unLogin() {
        return result(Constants.UNLOGIN_CODE);
    }

    public static BaseResultDto restrictedLogin() {
        return result(Constants.RESTRICTED_LOGIN_CODE);
    }

    public static BaseResultDto result(int code) {
        String msg;
        switch (code) {
            case Constants.UNLOGIN_CODE:
                msg = Constants.INVALID;
                break;
            case Constants.RESTRICTED_LOGIN_CODE:
                msg = Constants.RESTRICTED_LOGIN;
                break;
            default:
                msg = null;
                break;
        }
        return result(code, msg, null);
    }

    public static BaseResultDto result(int code, String msg) {

        return result(code, msg, null);
    }

    public static BaseResultDto result(int code, String msg, Object data) {
        return new BaseResultDto(code, msg, data);
    }
}
