123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- package com.lqbz.common.result;
- import lombok.Data;
- import java.io.Serializable;
- import java.util.UUID;
- /**
- * 通用返回结果
- *
- * @param <T>
- */
- @Data
- public class CommonResult<T> extends BaseResult<T> implements Serializable {
- private static final long serialVersionUID = -7268040542410707954L;
- public CommonResult() {
- }
- public CommonResult(boolean success, String message) {
- this.setSuccess(success);
- this.setMsg(message);
- }
- public CommonResult(boolean success) {
- this.setSuccess(success);
- }
- public CommonResult(String code, String message) {
- this.setCode(code);
- this.setMsg(message);
- }
- public CommonResult(boolean success, String message, T data) {
- this.setSuccess(success);
- this.setMsg(message);
- this.setData(data);
- }
- public static CommonResult ok() {
- return ok(BaseErrorCode.Common.SUCCESS);
- }
- public static CommonResult fail(String message) {
- return fail(BaseErrorCode.Common.UNKNOWN_ERROR.getCode(), message);
- }
- public static <T> CommonResult<T> ok(IMessage message) {
- return baseCreate(message.getCode(), message.getMessage(), true);
- }
- public static CommonResult fail() {
- return fail(BaseErrorCode.Common.UNKNOWN_ERROR);
- }
- public static CommonResult fail(IMessage message) {
- return fail(message.getCode(), message.getMessage());
- }
- public static CommonResult fail(String code, String message) {
- return baseCreate(code, message, false);
- }
- private static <T> CommonResult<T> baseCreate(String code, String msg, boolean success) {
- CommonResult result = new CommonResult();
- result.setCode(code);
- result.setSuccess(success);
- result.setMsg(msg);
- result.setTimestamp(System.currentTimeMillis());
- result.setTraceId(UUID.randomUUID().toString());
- return result;
- }
- public CommonResult<T> addTraceId(String traceId) {
- this.setTraceId(traceId);
- return this;
- }
- public CommonResult<T> setResult(T data) {
- this.setData(data);
- return this;
- }
- public CommonResult<T> addData(T data) {
- this.setData(data);
- return this;
- }
- @Override
- public T getData() {
- return super.getData();
- }
- }
|