ApiResponse.java 3.39 KB
package com.yoho.unions.vo;

import com.alibaba.fastjson.JSON;
import com.yoho.core.common.utils.MD5;
import com.yoho.error.GatewayError;
import com.yoho.service.model.union.BaseBO;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;

public class ApiResponse extends BaseBO {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5131027255966719148L;
	
	private static String DEFAULT_MSG = "操作成功";
	private static int DEFAULT_CODE = 200;
	private static final String MD5_SALT = "fd4ad5fcsa0de589af23234ks1923ks";
	
	private int code = 200;
	private String message = "success";
	private String md5 = "";
	private Object data;
	
	public ApiResponse() {

	}
	
	public ApiResponse(Object data) {
		this.data = data;
	}
	
	public ApiResponse(int code, String message) {
		this.code = code;
		this.message = message;
	}
	
	public ApiResponse(int code, String message, Object data) {
		this.code = code;
		this.message = message;
		this.data = data;
	}

	public int getCode() {
		return code;
	}

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

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getMd5() {
		return md5;
	}

	public void setMd5(String md5) {
		this.md5 = md5;
	}

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}

	/**
	 * 构造响应。 使用方式:
	 * <p/>
	 * 
	 * <pre>
	 * ApiResponse.ApiResponseBuilder builder = new ApiResponse.ApiResponseBuilder();
	 * ApiResponse apiResponse = builder.code(200).message(&quot;coupons total&quot;).data(new Total(&quot;0&quot;)).build();
	 * </pre>
	 */
	public static class ApiResponseBuilder {
		ApiResponse apiResponse;

		public ApiResponseBuilder() {
			apiResponse = new ApiResponse();
		}

		/**
		 * 设置错误码。默认200
		 *
		 * @param code
		 *            错误码
		 * @return ApiResponseBuilder
		 */
		public ApiResponseBuilder code(int code) {
			apiResponse.code = code;
			return this;
		}

		/**
		 * 设置消息。默认[操作成功]
		 *
		 * @param message
		 *            错误消息
		 * @return ApiResponseBuilder
		 */
		public ApiResponseBuilder message(String message) {
			apiResponse.message = message;
			return this;
		}

		/**
		 * 从Errorcode中设置错误码和错误消息
		 *
		 * @param gatewayError
		 * @return
		 */
		public ApiResponseBuilder code(GatewayError gatewayError) {
			apiResponse.code = gatewayError.getCode();
			apiResponse.message = gatewayError.getMessage();
			return this;
		}

		/**
		 * 设置响应的具体内容
		 *
		 * @param data
		 *            响应的具体内容
		 * @return 内容
		 */
		public ApiResponseBuilder data(Object data) {
			apiResponse.data = data;
			return this;
		}

		/**
		 * 构造响应
		 *
		 * @return 响应
		 */
		public ApiResponse build() {
			// 参数校验, 并且设置默认值
			if (this.apiResponse.code <= 0) {
				this.apiResponse.code = DEFAULT_CODE;
			}
			if (StringUtils.isEmpty(apiResponse.message)) {
				this.apiResponse.message = DEFAULT_MSG;
			}

			// 构造JSON
			apiResponse.md5 = getMd5();
			return apiResponse;
		}

		// 计算MD5
		private String getMd5() {
			if (this.apiResponse.data == null) {
				this.apiResponse.data = new ArrayList<>(0);
			}
			String json = JSON.toJSONString(this.apiResponse.data); // []
			return MD5.md5(MD5_SALT + ":" + json);
		}

	}

}