123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- package com.kmall.admin.haikong.utils;
- import com.google.common.base.Strings;
- import com.kmall.admin.utils.data.response.ResponseMessageData;
- import com.kmall.admin.utils.oms.result.Result;
- import java.util.List;
- import java.util.Map;
- /**
- * @author Scott Chen
- * @date 2017/4/20
- */
- public class ResponseMessage {
- private static final String SUCCESS_CODE = "0";
- private static final String SUCCESS_INFO = "成功";
- public static final String ERROR_CODE = "-1";
- public static final String ERROR_INFO = "消息错误";
- private static final String FAILURE_CODE = "-2";
- private static final String FAILUER_INFO = "消息失败";
- private String code;
- private String msg;
- private ResponseMessageData data;
- private ResponseMessage() {
- }
- public String getCode() {
- return code;
- }
- public String getMsg() {
- return msg;
- }
- public ResponseMessageData getData() {
- return data;
- }
- public static boolean isSuccess(ResponseMessage message) {
- return message.getCode() == null ? false : message.getCode().equals(SUCCESS_CODE) ? true : false;
- }
- public static ResponseMessage transResponseMessage(Object object) {
- if (object instanceof Message) {
- Message message = (Message) object;
- return new Builder(message.getCode(), message.getMsg(), Object.class).build();
- } else if (object instanceof Result) {
- Result result = (Result) object;
- return new Builder(result.getCode(), result.getMsg(), result.getData(), Object.class).build();
- } else {
- return null;
- }
- }
- public static Builder builder() {
- return new Builder();
- }
- public static Builder builder(String code, String msg) {
- return new Builder(code, msg);
- }
- public static Builder builder(String code, String msg, ResponseMessageData responseMessageData) {
- return new Builder(code, msg, responseMessageData);
- }
- public static Builder builder(String code, String msg, List list) {
- return new Builder(code, msg, list);
- }
- public static <T> Builder builder(T t, Class<T> type) {
- return new Builder(SUCCESS_CODE, SUCCESS_INFO, t, type);
- }
- public static <T> Builder builder(String msg, T t, Class<T> type) {
- return new Builder(SUCCESS_CODE, msg, t, type);
- }
- public static <T> Builder builder(String code, String msg, T t, Class<T> type) {
- return new Builder(code, msg, t, type);
- }
- //---------- 直接返回结果 ----------
- //成功
- public static ResponseMessage success() {
- return new Builder().build();
- }
- public static ResponseMessage success(String code, String msg) {
- return new Builder(code, msg).build();
- }
- public static ResponseMessage success(String msg) {
- return new Builder(SUCCESS_CODE, msg).build();
- }
- public static ResponseMessage success(Map map) {
- return new Builder(SUCCESS_CODE, SUCCESS_INFO, map).build();
- }
- public static ResponseMessage success(String msg, Map map) {
- return new Builder(SUCCESS_CODE, msg, map).build();
- }
- public static ResponseMessage success(List rows) {
- return new Builder(SUCCESS_CODE, SUCCESS_INFO, rows).build();
- }
- public static ResponseMessage success(String msg, List rows) {
- return new Builder(SUCCESS_CODE, msg, rows).build();
- }
- public static ResponseMessage success(String code, String msg, List rows) {
- return new Builder(code, msg, rows).build();
- }
- public static <T> ResponseMessage success(T t, Class<T> type) {
- return new Builder(SUCCESS_CODE, SUCCESS_INFO, t, type).build();
- }
- public static <T> ResponseMessage success(String msg, T t, Class<T> type) {
- return new Builder(SUCCESS_CODE, msg, t, type).build();
- }
- public static <T> ResponseMessage success(String code, String msg, T t, Class<T> type) {
- return new Builder(code, msg, t, type).build();
- }
- //错误
- public static ResponseMessage error() {
- return new Builder(ERROR_CODE, ERROR_INFO).build();
- }
- public static ResponseMessage error(String code, String msg) {
- return new Builder(code, msg).build();
- }
- public static ResponseMessage error(String msg) {
- return new Builder(ERROR_CODE, msg).build();
- }
- public static ResponseMessage error(Map map) {
- return new Builder(ERROR_CODE, ERROR_INFO, map).build();
- }
- public static ResponseMessage error(String msg, Map map) {
- return new Builder(ERROR_CODE, msg, map).build();
- }
- public static ResponseMessage error(List rows) {
- return new Builder(ERROR_CODE, ERROR_INFO, rows).build();
- }
- public static ResponseMessage error(String msg, List rows) {
- return new Builder(ERROR_CODE, msg, rows).build();
- }
- public static ResponseMessage error(String code, String msg, List rows) {
- return new Builder(code, msg, rows).build();
- }
- //失败
- public static ResponseMessage failed() {
- return new Builder(FAILURE_CODE, FAILUER_INFO).build();
- }
- public static ResponseMessage failed(String code, String msg) {
- return new Builder(code, msg).build();
- }
- public static ResponseMessage failed(String msg) {
- return new Builder(FAILURE_CODE, msg).build();
- }
- public static ResponseMessage failed(Map map) {
- return new Builder(ERROR_CODE, ERROR_INFO, map).build();
- }
- public static ResponseMessage failed(String msg, Map map) {
- return new Builder(ERROR_CODE, msg, map).build();
- }
- public static ResponseMessage failed(List rows) {
- return new Builder(FAILURE_CODE, FAILUER_INFO, rows).build();
- }
- public static ResponseMessage failed(String msg, List rows) {
- return new Builder(ERROR_CODE, msg, rows).build();
- }
- public static ResponseMessage failed(String code, String msg, List rows) {
- return new Builder(code, msg, rows).build();
- }
- /**
- * 消息构建器类
- */
- public static class Builder {
- private String code;
- private String msg;
- private ResponseMessageData data;
- public Builder() {
- this.code = SUCCESS_CODE;
- this.msg = SUCCESS_INFO;
- this.data = ResponseMessageData.builder().build();
- }
- public Builder(String code, String msg) {
- this.code = Strings.isNullOrEmpty(code) ? SUCCESS_CODE : code;
- this.msg = Strings.isNullOrEmpty(msg) ? SUCCESS_INFO : msg;
- this.data = ResponseMessageData.builder().build();
- }
- public Builder(String code, String msg, ResponseMessageData responseMessageData) {
- this.code = Strings.isNullOrEmpty(code) ? SUCCESS_CODE : code;
- this.msg = Strings.isNullOrEmpty(msg) ? SUCCESS_INFO : msg;
- this.data = responseMessageData;
- }
- public Builder(String code, String msg, Map map) {
- this.code = Strings.isNullOrEmpty(code) ? SUCCESS_CODE : code;
- this.msg = Strings.isNullOrEmpty(msg) ? SUCCESS_INFO : msg;
- this.data = ResponseMessageData.builder(map, Map.class).build();
- }
- public Builder(String code, String msg, List rows) {
- this.code = Strings.isNullOrEmpty(code) ? SUCCESS_CODE : code;
- this.msg = Strings.isNullOrEmpty(msg) ? SUCCESS_INFO : msg;
- this.data = ResponseMessageData.builder(rows).build();
- }
- public <T> Builder(T t, Class<T> type) {
- this.code = SUCCESS_CODE;
- this.msg = SUCCESS_INFO;
- this.data = ResponseMessageData.builder(t, type).build();
- }
- public <T> Builder(String msg, T t, Class<T> type) {
- this.code = SUCCESS_CODE;
- this.msg = Strings.isNullOrEmpty(msg) ? SUCCESS_INFO : msg;
- this.data = ResponseMessageData.builder(t, type).build();
- }
- public <T> Builder(String code, String msg, T t, Class<T> type) {
- this.code = Strings.isNullOrEmpty(code) ? SUCCESS_CODE : code;
- this.msg = Strings.isNullOrEmpty(msg) ? SUCCESS_INFO : msg;
- this.data = ResponseMessageData.builder(t, type).build();
- }
- public Builder setCode(String code) {
- this.code = code;
- return this;
- }
- public Builder setMsg(String msg) {
- this.msg = msg;
- return this;
- }
- public Builder setData(ResponseMessageData data) {
- this.data = data;
- return this;
- }
- public ResponseMessage build() {
- ResponseMessage message = new ResponseMessage();
- message.code = this.code;
- message.msg = this.msg;
- message.data = this.data;
- return message;
- }
- }
- }
|