package com.ywt.service;

import java.util.HashMap;
import java.util.Map;

public class ServiceResult {

    private int code;

    private String msg;

    private Map<String, Object> data;

    public ServiceResult() {
        this.data = new HashMap<>();
    }

    public ServiceResult succeed() {
        this.code = ServiceResultCode.Succeed;
        return this;
    }

    public ServiceResult succeed(String msg) {
        this.code = ServiceResultCode.Succeed;
        this.msg = msg;
        return this;
    }

    public ServiceResult succeed(String msg, Map<String, Object> data) {
        this.code = ServiceResultCode.Succeed;
        this.msg = msg;
        this.data = data;
        return this;
    }

    public ServiceResult failed() {
        this.code = ServiceResultCode.Failed;
        return this;
    }

    public ServiceResult failed(String msg) {
        this.code = ServiceResultCode.Failed;
        this.msg = msg;
        return this;
    }

    public ServiceResult error() {
        this.code = ServiceResultCode.ERROR;
        return this;
    }

    public ServiceResult error(String msg) {
        this.code = ServiceResultCode.ERROR;
        this.msg = msg;
        return this;
    }
    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Map<String, Object> getData() {
        return data;
    }

    public void setData(Map<String, Object> data) {
        this.data = data;
    }

    public boolean success() {
        return this.code == ServiceResultCode.Succeed;
    }

    public boolean fail() {
        return this.code != ServiceResultCode.Succeed;
    }

    public boolean isSuccessful() {
        return this.code == ServiceResultCode.Succeed;
    }

    static class ServiceResultCode {
        static final int Succeed = 1;
        static final int Failed = 0;
        static final int ERROR = 2;
    }


}
