ResultUtil.java 6.35 KB
Newer Older
yuquan.zhu committed
1 2 3 4 5 6 7 8 9 10 11 12
package cn.timer.api.utils;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.metadata.IPage;

/**
 * @author Exrick
 */
@Service("resultUtil")
public class ResultUtil<T> {

13 14 15 16 17 18 19 20 21 22 23
//	private static final String CODE_KEY = "code";
//	private static final String RESULT_KEY = "result";
//	private static final String MESSAGE_KEY = "message";
//	private static final String DATA_KEY = "data";
//
//	private static final String PAGE_KEY = "page";
//	private static final String NUMBER_KEY = "number";
//	private static final String SIZE_KEY = "size";
//	private static final String TOTALELEMENTS_KEY = "totalelements";
//	private static final String TOTALPAGES_KEY = "totalpages";
//	private static final String OFFSET_KEY = "offset";
yuquan.zhu committed
24 25 26 27

	private static final String MESSAGE_SUCCESS = "操作成功";
	private static final String MESSAGE_FAIL = "操作失败";
	private static final String MESSAGE_UNLOGIN = "会话超时,请重新登录";
28 29
//	private static final String MESSAGE_ERROR = "操作错误";
//	private static final String MESSAGE_KICKOUT = "当前账户已在其他地方登录,请重新登录";
30
	private static final String MESSAGE_PARAMERROR = "参数错误";
31 32 33
//	private static final String MESSAGE_UNAUTHORIZED = "授权错误";
//	private static final String MESSAGE_UNAUTHENTICATED = "认证错误";
//	private static final String MESSAGE_EXCEPTION = "服务器异常";
yuquan.zhu committed
34 35

	private static final String STATUS_CODE_SUCCESS = "200";
36 37
//	private static final String STATUS_CODE_FAIL = "201";
//	private static final String STATUS_CODE_ERROR = "202";
yuquan.zhu committed
38
	private static final String STATUS_CODE_UNLOGIN = "301";
39
//	private static final String STATUS_CODE_KICKOUT = "302";
40
	private static final String STATUS_CODE_UNPARAM = "400";
41 42
//	private static final String STATUS_CODE_UNAUTHORIZED = "401";
//	private static final String STATUS_CODE_UNAUTHENTICATED = "405";
yuquan.zhu committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56
	private static final String STATUS_CODE_EXCEPTION = "500";

	private Result<T> result;

	public ResultUtil() {
		result = new Result<>();
		result.setResult(true);
		result.setMessage("success");
		result.setCode(STATUS_CODE_SUCCESS);
	}

	public Result<T> setData(T t) {
		this.result.setData(t);
		this.result.setCode(STATUS_CODE_SUCCESS);
57
		this.result.setMessage(MESSAGE_SUCCESS);
yuquan.zhu committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
		return this.result;
	}

	public Result<T> setSuccessMsg(String msg) {
		this.result.setResult(true);
		this.result.setMessage(msg);
		this.result.setCode(STATUS_CODE_SUCCESS);
		this.result.setData(null);
		return this.result;
	}

	public Result<T> setPageData(T t1, Long t2, String msg) {
		this.result.setData(t1);
		this.result.setTotal(t2);
		this.result.setCode(STATUS_CODE_SUCCESS);
		this.result.setMessage(msg);
		return this.result;
	}

	public Result<T> setDatas(T t1, T t2, String msg) {
		this.result.setData(t1);
		this.result.setOther(t2);
		this.result.setCode(STATUS_CODE_SUCCESS);
		this.result.setMessage(msg);
		return this.result;
	}

85
	@SuppressWarnings("rawtypes")
yuquan.zhu committed
86 87 88 89 90 91 92 93 94 95 96
	public Result<T> setData(T t1, T t2, String msg) {

		this.result.setCurrent(((IPage) t1).getCurrent());
		this.result.setPages(((IPage) t1).getPages());
		this.result.setSize(((IPage) t1).getSize());
		this.result.setTotal(((IPage) t1).getTotal());
		this.result.setData(t2);
		this.result.setCode(STATUS_CODE_SUCCESS);
		this.result.setMessage(msg);
		return this.result;
	}
97 98 99 100 101 102 103 104 105 106 107 108 109
	
	@SuppressWarnings("rawtypes")
	public Result<T> setData(T t1, T t2) {
		
		this.result.setCurrent(((IPage) t1).getCurrent());
		this.result.setPages(((IPage) t1).getPages());
		this.result.setSize(((IPage) t1).getSize());
		this.result.setTotal(((IPage) t1).getTotal());
		this.result.setData(t2);
		this.result.setCode(STATUS_CODE_SUCCESS);
		this.result.setMessage(MESSAGE_SUCCESS);
		return this.result;
	}
yuquan.zhu committed
110

111
	@SuppressWarnings("rawtypes")
yuquan.zhu committed
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
	public Result<T> setData2(T t1, T t2, String msg) {

		this.result.setCurrent((long) ((com.github.pagehelper.Page) t1).getPageNum());
		this.result.setPages((long) ((com.github.pagehelper.Page) t1).getPages());
		this.result.setSize((long) ((com.github.pagehelper.Page) t1).getPageSize());
		this.result.setTotal(((com.github.pagehelper.Page) t1).getTotal());
		this.result.setData(t2);
		this.result.setCode(STATUS_CODE_SUCCESS);
		this.result.setMessage(msg);
		return this.result;
	}

	public Result<T> setData(T t, String msg) {
		this.result.setData(t);
		this.result.setCode(STATUS_CODE_SUCCESS);
		this.result.setMessage(msg);
		return this.result;
	}
130 131 132 133 134 135 136
	
	public Result<T> setErrorMsg(T t) {
		this.result.setData(t);
		this.result.setCode(STATUS_CODE_UNPARAM);
		this.result.setMessage(MESSAGE_PARAMERROR);
		return this.result;
	}
yuquan.zhu committed
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155

	public Result<T> setErrorMsg(String msg) {
		this.result.setResult(false);
		this.result.setMessage(msg);
		this.result.setCode(STATUS_CODE_EXCEPTION);
		return this.result;
	}

	public Result<T> setErrorMsg(String code, String msg) {
		this.result.setResult(false);
		this.result.setMessage(msg);
		this.result.setCode(code);
		return this.result;
	}

	public static <T> Result<T> data(T t) {
		return new ResultUtil<T>().setData(t);
	}

156
	public static <T> Result<T> data(T t1, T t2) {
yuquan.zhu committed
157

158 159 160 161 162
		return new ResultUtil<T>().setData(t1, t2);
	}
	
	public static <T> Result<T> data(T t1, T t2, String msg) {
		
yuquan.zhu committed
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
		return new ResultUtil<T>().setData(t1, t2, msg);
	}

	public static <T> Result<T> data2(T t1, T t2, String msg) {

		return new ResultUtil<T>().setData2(t1, t2, msg);
	}

	public static <T> Result<T> datas(T t1, T t2, String msg) {

		return new ResultUtil<T>().setDatas(t1, t2, msg);
	}

	public static <T> Result<T> data(T t, String msg) {
		return new ResultUtil<T>().setData(t, msg);
	}

	public static <T> Result<T> pageData(T t, Long t2, String msg) {
		return new ResultUtil<T>().setPageData(t, t2, msg);
	}

	public static <T> Result<T> success() {
		return new ResultUtil<T>().setSuccessMsg(MESSAGE_SUCCESS);
	}

	public static <T> Result<T> success(String msg) {
		return new ResultUtil<T>().setSuccessMsg(msg);
	}

	public static <T> Result<T> error() {
		return new ResultUtil<T>().setErrorMsg(MESSAGE_FAIL);
	}

	public static <T> Result<T> unlogin() {
		return new ResultUtil<T>().setErrorMsg(STATUS_CODE_UNLOGIN, MESSAGE_UNLOGIN);
	}

	public static <T> Result<T> error(String msg) {
		return new ResultUtil<T>().setErrorMsg(msg);
	}

	public static <T> Result<T> error(String code, String msg) {
		return new ResultUtil<T>().setErrorMsg(code, msg);
	}
207 208 209 210
	
	public static <T> Result<T> error(T t) {
		return new ResultUtil<T>().setErrorMsg(t);
	}
yuquan.zhu committed
211 212

}