Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
top.cutexingluo.tools.common.StrResult Maven / Gradle / Ivy
package top.cutexingluo.tools.common;
import cn.hutool.core.bean.BeanUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import top.cutexingluo.tools.common.base.IResult;
import top.cutexingluo.tools.common.base.IResultData;
import top.cutexingluo.tools.common.base.XTIntCode;
import top.cutexingluo.tools.designtools.method.ClassMaker;
import java.util.List;
import java.util.Map;
/**
* 兼容 String 类型的 code
*
* @author XingTian
* @version 1.1.1
* @date 2023/7/13 23:13
*/
@EqualsAndHashCode(callSuper = true)
@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class StrResult extends CommonResult implements XTIntCode {
protected String code;
protected String msg;
protected Object data;
@Override
public int intCode() {
return Integer.parseInt(code);
}
//-----------convertor---------------
public StrResult(Result result) {
this.code = result.strCode();
this.msg = result.getMsg();
this.data = result.getData();
}
public StrResult(MSResult result) {
this.code = result.strCode();
this.msg = result.getMsg();
this.data = result.getData();
}
public StrResult(StrMSResult result) {
this.code = result.getCode();
this.msg = result.getMsg();
this.data = result.getData();
}
public Result toResult() {
return new Result(this);
}
public MSResult toMSResult() {
return new MSResult<>(this);
}
public MSResult toMSResult(Class dataType) {
return new MSResult<>(this);
}
// public StrResult toStrResult() {
// return new StrResult(this);
// }
public StrMSResult toStrMSResult() {
return new StrMSResult<>(this);
}
public StrMSResult toStrMSResult(Class dataType) {
return new StrMSResult<>(this);
}
//------------静态方法区-------------------------
//------------common-----------
public static StrResult put(String otherCode, String otherMsg, Object data) {
return new StrResult(otherCode, otherMsg, data);
}
public static StrResult put(Constants constants, Object data) {
return put(constants.getCode(), constants.getMsg(), data);
}
public static StrResult put(Constants constants) {
return put(constants, "");
}
/**
* @since 1.1.2
*/
public static StrResult put(HttpStatus httpStatus, Object data) {
return put(httpStatus.strCode(), httpStatus.getMsg(), data);
}
/**
* @since 1.1.2
*/
public static StrResult put(HttpStatus httpStatus) {
return put(httpStatus, "");
}
public static StrResult put(IResult resultData) {
return put(resultData, resultData.getData());
}
public static StrResult put(IResultData resultData, Object data) {
C code = resultData.getCode();
if (code instanceof Integer) {
return put(ICommonResult.strCode((Integer) code), resultData.getMsg(), data);
} else if (code instanceof String) {
return put((String) code, resultData.getMsg(), data);
}
return put(Constants.CODE_600.getCode(), resultData.getMsg(), data);
}
//------------own-------------------
public static StrResult ok() {
return put(Constants.CODE_200);
}
public static StrResult pass() {
return put(Constants.CODE_200.getCode(), "", null);
}
public static StrResult notPass() {
return put(Constants.CODE_500, null);
}
//---success---
public static StrResult success() {
return put(Constants.CODE_200, true);
}
public static StrResult success(Object data) {
return put(Constants.CODE_200, data);
}
public static StrResult success(String msg, Object data) {
return put(Constants.CODE_200.getCode(), msg, data);
}
//---error---
public static StrResult error() {
return put(Constants.CODE_500, false);
}
public static StrResult error(String msg) {
return put(Constants.CODE_500.getCode(), msg, false);
}
public static StrResult error(Object data) {
return put(Constants.CODE_500, data);
}
public static StrResult error(String otherCode, String msg) {
return put(otherCode, msg, false);
}
/**
* 其他错误
*/
public static StrResult error600(String msg, Object data) {
return put(Constants.CODE_600.getCode(), msg, data);
}
// 常用 StrResult.error(错误码)
public static StrResult error(Constants constants) {
return put(constants, false);
}
public static StrResult error(Constants constants, String msg) {
return error(constants.getCode(), msg);
}
/**
* @since 1.1.2
*/
public static StrResult error(HttpStatus httpStatus) {
return put(httpStatus, false);
}
/**
* @since 1.1.2
*/
public static StrResult error(HttpStatus httpStatus, String msg) {
return error(httpStatus.strCode(), msg);
}
//----------------ext------------
/**
* 扩展,添加 list
**/
public StrResult add(Object... items) {
ICommonResult commonResult = () -> (Object) this.data;
List list = commonResult.add(items);
setData(list);
return this;
}
/**
* 扩展,添加 map
*/
public StrResult putAll(Object... pairs) {
ICommonResult commonResult = () -> (Object) this.data;
Map map = commonResult.put(pairs);
setData(map);
return this;
}
//-----------convertor---------------
public StrResult(IResult result) {
if (result.getCode() == null) {
this.code = Constants.CODE_200.getCode();
} else if (result.getCode() instanceof Integer) {
this.code = String.valueOf(result.getCode());
} else {
try {
this.code = result.getCode().toString();
} catch (NumberFormatException e) {
throw new NumberFormatException("code transform eror ==> " + e.getMessage());
}
}
this.msg = result.getMsg();
this.data = result.getData();
}
/**
* 转到自定义类型
* fix bug
*
* @param clazz 自定义类型,需继承自IResult
* @return {@link P}
* @updateFrom 1.0.3
*/
public , O> P toCollect(Class
clazz) {
return ClassMaker.castTarClass(this, clazz);
}
/**
* 复制到自定义类型
*
* @param clazz 自定义类型,需继承自IResult
* @return {@link P}
*/
public
, O> P toCollectByCopy(Class
clazz) {
return BeanUtil.copyProperties(this, clazz);
}
}