ResponseMessage.java 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. package com.kmall.admin.haikong.utils;
  2. import com.google.common.base.Strings;
  3. import com.kmall.admin.utils.data.response.ResponseMessageData;
  4. import com.kmall.admin.utils.oms.result.Result;
  5. import java.util.List;
  6. import java.util.Map;
  7. /**
  8. * @author Scott Chen
  9. * @date 2017/4/20
  10. */
  11. public class ResponseMessage {
  12. private static final String SUCCESS_CODE = "0";
  13. private static final String SUCCESS_INFO = "成功";
  14. public static final String ERROR_CODE = "-1";
  15. public static final String ERROR_INFO = "消息错误";
  16. private static final String FAILURE_CODE = "-2";
  17. private static final String FAILUER_INFO = "消息失败";
  18. private String code;
  19. private String msg;
  20. private ResponseMessageData data;
  21. private ResponseMessage() {
  22. }
  23. public String getCode() {
  24. return code;
  25. }
  26. public String getMsg() {
  27. return msg;
  28. }
  29. public ResponseMessageData getData() {
  30. return data;
  31. }
  32. public static boolean isSuccess(ResponseMessage message) {
  33. return message.getCode() == null ? false : message.getCode().equals(SUCCESS_CODE) ? true : false;
  34. }
  35. public static ResponseMessage transResponseMessage(Object object) {
  36. if (object instanceof Message) {
  37. Message message = (Message) object;
  38. return new Builder(message.getCode(), message.getMsg(), Object.class).build();
  39. } else if (object instanceof Result) {
  40. Result result = (Result) object;
  41. return new Builder(result.getCode(), result.getMsg(), result.getData(), Object.class).build();
  42. } else {
  43. return null;
  44. }
  45. }
  46. public static Builder builder() {
  47. return new Builder();
  48. }
  49. public static Builder builder(String code, String msg) {
  50. return new Builder(code, msg);
  51. }
  52. public static Builder builder(String code, String msg, ResponseMessageData responseMessageData) {
  53. return new Builder(code, msg, responseMessageData);
  54. }
  55. public static Builder builder(String code, String msg, List list) {
  56. return new Builder(code, msg, list);
  57. }
  58. public static <T> Builder builder(T t, Class<T> type) {
  59. return new Builder(SUCCESS_CODE, SUCCESS_INFO, t, type);
  60. }
  61. public static <T> Builder builder(String msg, T t, Class<T> type) {
  62. return new Builder(SUCCESS_CODE, msg, t, type);
  63. }
  64. public static <T> Builder builder(String code, String msg, T t, Class<T> type) {
  65. return new Builder(code, msg, t, type);
  66. }
  67. //---------- 直接返回结果 ----------
  68. //成功
  69. public static ResponseMessage success() {
  70. return new Builder().build();
  71. }
  72. public static ResponseMessage success(String code, String msg) {
  73. return new Builder(code, msg).build();
  74. }
  75. public static ResponseMessage success(String msg) {
  76. return new Builder(SUCCESS_CODE, msg).build();
  77. }
  78. public static ResponseMessage success(Map map) {
  79. return new Builder(SUCCESS_CODE, SUCCESS_INFO, map).build();
  80. }
  81. public static ResponseMessage success(String msg, Map map) {
  82. return new Builder(SUCCESS_CODE, msg, map).build();
  83. }
  84. public static ResponseMessage success(List rows) {
  85. return new Builder(SUCCESS_CODE, SUCCESS_INFO, rows).build();
  86. }
  87. public static ResponseMessage success(String msg, List rows) {
  88. return new Builder(SUCCESS_CODE, msg, rows).build();
  89. }
  90. public static ResponseMessage success(String code, String msg, List rows) {
  91. return new Builder(code, msg, rows).build();
  92. }
  93. public static <T> ResponseMessage success(T t, Class<T> type) {
  94. return new Builder(SUCCESS_CODE, SUCCESS_INFO, t, type).build();
  95. }
  96. public static <T> ResponseMessage success(String msg, T t, Class<T> type) {
  97. return new Builder(SUCCESS_CODE, msg, t, type).build();
  98. }
  99. public static <T> ResponseMessage success(String code, String msg, T t, Class<T> type) {
  100. return new Builder(code, msg, t, type).build();
  101. }
  102. //错误
  103. public static ResponseMessage error() {
  104. return new Builder(ERROR_CODE, ERROR_INFO).build();
  105. }
  106. public static ResponseMessage error(String code, String msg) {
  107. return new Builder(code, msg).build();
  108. }
  109. public static ResponseMessage error(String msg) {
  110. return new Builder(ERROR_CODE, msg).build();
  111. }
  112. public static ResponseMessage error(Map map) {
  113. return new Builder(ERROR_CODE, ERROR_INFO, map).build();
  114. }
  115. public static ResponseMessage error(String msg, Map map) {
  116. return new Builder(ERROR_CODE, msg, map).build();
  117. }
  118. public static ResponseMessage error(List rows) {
  119. return new Builder(ERROR_CODE, ERROR_INFO, rows).build();
  120. }
  121. public static ResponseMessage error(String msg, List rows) {
  122. return new Builder(ERROR_CODE, msg, rows).build();
  123. }
  124. public static ResponseMessage error(String code, String msg, List rows) {
  125. return new Builder(code, msg, rows).build();
  126. }
  127. //失败
  128. public static ResponseMessage failed() {
  129. return new Builder(FAILURE_CODE, FAILUER_INFO).build();
  130. }
  131. public static ResponseMessage failed(String code, String msg) {
  132. return new Builder(code, msg).build();
  133. }
  134. public static ResponseMessage failed(String msg) {
  135. return new Builder(FAILURE_CODE, msg).build();
  136. }
  137. public static ResponseMessage failed(Map map) {
  138. return new Builder(ERROR_CODE, ERROR_INFO, map).build();
  139. }
  140. public static ResponseMessage failed(String msg, Map map) {
  141. return new Builder(ERROR_CODE, msg, map).build();
  142. }
  143. public static ResponseMessage failed(List rows) {
  144. return new Builder(FAILURE_CODE, FAILUER_INFO, rows).build();
  145. }
  146. public static ResponseMessage failed(String msg, List rows) {
  147. return new Builder(ERROR_CODE, msg, rows).build();
  148. }
  149. public static ResponseMessage failed(String code, String msg, List rows) {
  150. return new Builder(code, msg, rows).build();
  151. }
  152. /**
  153. * 消息构建器类
  154. */
  155. public static class Builder {
  156. private String code;
  157. private String msg;
  158. private ResponseMessageData data;
  159. public Builder() {
  160. this.code = SUCCESS_CODE;
  161. this.msg = SUCCESS_INFO;
  162. this.data = ResponseMessageData.builder().build();
  163. }
  164. public Builder(String code, String msg) {
  165. this.code = Strings.isNullOrEmpty(code) ? SUCCESS_CODE : code;
  166. this.msg = Strings.isNullOrEmpty(msg) ? SUCCESS_INFO : msg;
  167. this.data = ResponseMessageData.builder().build();
  168. }
  169. public Builder(String code, String msg, ResponseMessageData responseMessageData) {
  170. this.code = Strings.isNullOrEmpty(code) ? SUCCESS_CODE : code;
  171. this.msg = Strings.isNullOrEmpty(msg) ? SUCCESS_INFO : msg;
  172. this.data = responseMessageData;
  173. }
  174. public Builder(String code, String msg, Map map) {
  175. this.code = Strings.isNullOrEmpty(code) ? SUCCESS_CODE : code;
  176. this.msg = Strings.isNullOrEmpty(msg) ? SUCCESS_INFO : msg;
  177. this.data = ResponseMessageData.builder(map, Map.class).build();
  178. }
  179. public Builder(String code, String msg, List rows) {
  180. this.code = Strings.isNullOrEmpty(code) ? SUCCESS_CODE : code;
  181. this.msg = Strings.isNullOrEmpty(msg) ? SUCCESS_INFO : msg;
  182. this.data = ResponseMessageData.builder(rows).build();
  183. }
  184. public <T> Builder(T t, Class<T> type) {
  185. this.code = SUCCESS_CODE;
  186. this.msg = SUCCESS_INFO;
  187. this.data = ResponseMessageData.builder(t, type).build();
  188. }
  189. public <T> Builder(String msg, T t, Class<T> type) {
  190. this.code = SUCCESS_CODE;
  191. this.msg = Strings.isNullOrEmpty(msg) ? SUCCESS_INFO : msg;
  192. this.data = ResponseMessageData.builder(t, type).build();
  193. }
  194. public <T> Builder(String code, String msg, T t, Class<T> type) {
  195. this.code = Strings.isNullOrEmpty(code) ? SUCCESS_CODE : code;
  196. this.msg = Strings.isNullOrEmpty(msg) ? SUCCESS_INFO : msg;
  197. this.data = ResponseMessageData.builder(t, type).build();
  198. }
  199. public Builder setCode(String code) {
  200. this.code = code;
  201. return this;
  202. }
  203. public Builder setMsg(String msg) {
  204. this.msg = msg;
  205. return this;
  206. }
  207. public Builder setData(ResponseMessageData data) {
  208. this.data = data;
  209. return this;
  210. }
  211. public ResponseMessage build() {
  212. ResponseMessage message = new ResponseMessage();
  213. message.code = this.code;
  214. message.msg = this.msg;
  215. message.data = this.data;
  216. return message;
  217. }
  218. }
  219. }