ServiceResp.java

package com.example.sirirobot.resp;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;

/**
 * 返回对象主要包含:响应头、返回内容
 *
 * @author tangrh
 */
@ApiModel("返回内容")
public class ServiceResp<T> implements Serializable {

    private static final long serialVersionUID = 3713138005539550112L;
    @ApiModelProperty("响应头")
    private RespHeader head = new RespHeader();
    @ApiModelProperty("响应内容")
    private T body;

    public ServiceResp() {

    }

    public static ServiceResp getInstance() {
        return new ServiceResp();
    }

    public ServiceResp error() {
        this.head.setRespCode(-1);
        this.head.setRespMsg("操作失败");
        this.setBody(null);
        this.head.setResTime(String.valueOf(System.currentTimeMillis()));
        return this;
    }

    public ServiceResp error(String respMsg) {
        this.head.setRespCode(-1);
        this.head.setRespMsg(respMsg);
        this.setBody(null);
        this.head.setResTime(String.valueOf(System.currentTimeMillis()));
        return this;
    }

    public ServiceResp error(int respCode, String respMsg) {
        this.head.setRespCode(respCode);
        this.head.setRespMsg(respMsg);
        this.setBody(null);
        this.head.setResTime(String.valueOf(System.currentTimeMillis()));
        return this;
    }

    public ServiceResp success(String respMsg) {
        this.head.setRespCode(0);
        this.head.setRespMsg(respMsg);
        this.setBody(null);
        this.head.setResTime(String.valueOf(System.currentTimeMillis()));
        return this;
    }

    public ServiceResp success(T body, String respMsg) {
        this.head.setRespCode(0);
        this.head.setRespMsg(respMsg);
        this.setBody(body);
        this.head.setResTime(String.valueOf(System.currentTimeMillis()));
        return this;
    }

    public ServiceResp success(T body) {
        this.head.setRespCode(0);
        this.head.setRespMsg("操作成功");
        this.setBody(body);
        this.head.setResTime(String.valueOf(System.currentTimeMillis()));
        return this;
    }

    public boolean isSuccess() {
        return this.getHead().getRespCode() == 0 ? true : false;
    }

    public boolean hasRecord() {
        return isSuccess() && this.body != null ? true : false;
    }

    public RespHeader getHead() {
        return head;
    }

    public void setHead(RespHeader head) {
        this.head = head;
    }

    public T getBody() {
        return body;
    }

    public void setBody(T body) {
        this.body = body;
    }

    @Override
    public String toString() {
        return "ServiceResp{" +
                "head=" + head +
                ", body=" + body +
                '}';
    }
}

 

posted @ 2022-05-23 11:32  subtlman  阅读(38)  评论(0)    收藏  举报