add:增加mapper生成
This commit is contained in:
@@ -0,0 +1,141 @@
|
||||
package org.nl.common.api;
|
||||
|
||||
/**
|
||||
* 通用返回对象
|
||||
*
|
||||
* @author gbx
|
||||
* @date 2023-03-02
|
||||
*/
|
||||
public class CommonResult<T> {
|
||||
private long status;
|
||||
private String message;
|
||||
private T data;
|
||||
|
||||
public CommonResult() {
|
||||
}
|
||||
|
||||
public CommonResult(T data) {
|
||||
this.data = data;
|
||||
this.message = ResultCode.SUCCESS.getDesc();
|
||||
this.status = ResultCode.SUCCESS.getCode();
|
||||
}
|
||||
|
||||
|
||||
public CommonResult(long status, String message, T data) {
|
||||
this.status = status;
|
||||
this.message = message;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 成功返回结果
|
||||
|
||||
*/
|
||||
public static <T> CommonResult<T> success() {
|
||||
return new CommonResult<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getDesc(), null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 成功返回结果
|
||||
*
|
||||
* @param data 获取的数据
|
||||
*/
|
||||
public static <T> CommonResult<T> success(T data) {
|
||||
return new CommonResult<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getDesc(), data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 成功返回结果
|
||||
*
|
||||
* @param data 获取的数据
|
||||
* @param message 提示信息
|
||||
*/
|
||||
public static <T> CommonResult<T> success(T data, String message) {
|
||||
return new CommonResult<>(ResultCode.SUCCESS.getCode(), message, data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 失败返回结果
|
||||
* @param errorCode 错误码
|
||||
*/
|
||||
public static <T> CommonResult<T> failed(IErrorCode errorCode) {
|
||||
return new CommonResult<>(errorCode.getCode(), errorCode.getDesc(), null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 失败返回结果
|
||||
* @param errorCode 错误码
|
||||
* @param message 错误信息
|
||||
*/
|
||||
public static <T> CommonResult<T> failed(IErrorCode errorCode,String message) {
|
||||
return new CommonResult<>(errorCode.getCode(), message, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 失败返回结果
|
||||
* @param message 提示信息
|
||||
*/
|
||||
public static <T> CommonResult<T> failed(String message) {
|
||||
return new CommonResult<>(ResultCode.FAILED.getCode(), message, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 失败返回结果
|
||||
*/
|
||||
public static <T> CommonResult<T> failed() {
|
||||
return failed(ResultCode.FAILED);
|
||||
}
|
||||
|
||||
/**
|
||||
* 参数验证失败返回结果
|
||||
*/
|
||||
public static <T> CommonResult<T> validateFailed() {
|
||||
return failed(ResultCode.VALIDATE_FAILED);
|
||||
}
|
||||
|
||||
/**
|
||||
* 参数验证失败返回结果
|
||||
* @param message 提示信息
|
||||
*/
|
||||
public static <T> CommonResult<T> validateFailed(String message) {
|
||||
return new CommonResult<>(ResultCode.VALIDATE_FAILED.getCode(), message, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 未登录返回结果
|
||||
*/
|
||||
public static <T> CommonResult<T> unauthorized(T data) {
|
||||
return new CommonResult<>(ResultCode.UNAUTHORIZED.getCode(), ResultCode.UNAUTHORIZED.getDesc(), data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 未授权返回结果
|
||||
*/
|
||||
public static <T> CommonResult<T> forbidden(T data) {
|
||||
return new CommonResult<>(ResultCode.FORBIDDEN.getCode(), ResultCode.FORBIDDEN.getDesc(), data);
|
||||
}
|
||||
|
||||
public long getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(long status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public T getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public void setData(T data) {
|
||||
this.data = data;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package org.nl.common.api;
|
||||
|
||||
/**
|
||||
* 封装API的错误码
|
||||
*
|
||||
* @author gbx
|
||||
* @date 2023-03-02
|
||||
*/
|
||||
public interface IErrorCode{
|
||||
/**
|
||||
* 返回状态码
|
||||
*/
|
||||
Integer getCode();
|
||||
|
||||
/**
|
||||
* 返回提示
|
||||
*/
|
||||
String getDesc();
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
package org.nl.common.api;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.nl.modules.common.exception.BadRequestException;
|
||||
import org.nl.modules.common.exception.BizCoreException;
|
||||
|
||||
/**
|
||||
* 回调
|
||||
*
|
||||
* @author gbx
|
||||
* @date 2023-03-02
|
||||
*/
|
||||
@Slf4j
|
||||
public class RestBusinessTemplate{
|
||||
public static <T> CommonResult<T> execute(Callback<T> callback) {
|
||||
CommonResult<T> result = new CommonResult<>();
|
||||
try {
|
||||
result.setStatus(ResultCode.SUCCESS.getCode());
|
||||
result.setMessage(ResultCode.SUCCESS.getDesc());
|
||||
T object = callback.doExecute();
|
||||
if(object != null) {
|
||||
result.setData(object);
|
||||
}
|
||||
}
|
||||
catch(BizCoreException e) {
|
||||
String errorMsg;
|
||||
if(StringUtils.isNotBlank(e.getErrorMsg())) {
|
||||
errorMsg = e.getErrorMsg();
|
||||
}
|
||||
else if(e.getCode() != null) {
|
||||
errorMsg = e.getCode().getDesc();
|
||||
}
|
||||
else{
|
||||
errorMsg = e.getMessage();
|
||||
}
|
||||
log.error(e.getErrorMsg());
|
||||
ResultCode code = e.getCode() == null ? ResultCode.FAILED : e.getCode();
|
||||
result.setStatus(code.getCode());
|
||||
result.setMessage(errorMsg);
|
||||
}
|
||||
catch(Exception e) {
|
||||
log.error("execute error", e);
|
||||
result.setStatus(ResultCode.FAILED.getCode());
|
||||
result.setMessage(ResultCode.FAILED.getDesc() + ",原因是:" + e.getMessage());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static <T> CommonResult<T> execute(T object) {
|
||||
CommonResult<T> result = new CommonResult<>();
|
||||
try {
|
||||
result.setStatus(ResultCode.SUCCESS.getCode());
|
||||
result.setMessage(ResultCode.SUCCESS.getDesc());
|
||||
if(object != null) {
|
||||
result.setData(object);
|
||||
}
|
||||
}
|
||||
catch(BizCoreException e) {
|
||||
String errorMsg;
|
||||
if(StringUtils.isNotBlank(e.getErrorMsg())) {
|
||||
errorMsg = e.getErrorMsg();
|
||||
}
|
||||
else if(e.getCode() != null) {
|
||||
errorMsg = e.getCode().getDesc();
|
||||
}
|
||||
else{
|
||||
errorMsg = e.getMessage();
|
||||
}
|
||||
log.error(e.getErrorMsg());
|
||||
ResultCode code = e.getCode() == null ? ResultCode.FAILED : e.getCode();
|
||||
result.setStatus(code.getCode());
|
||||
result.setMessage(errorMsg);
|
||||
}
|
||||
catch(Exception e) {
|
||||
log.error("execute error", e);
|
||||
result.setStatus(ResultCode.FAILED.getCode());
|
||||
result.setMessage(ResultCode.FAILED.getDesc() + ",原因是:" + e);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static CommonResult<Void> execute(VoidCallback callback) {
|
||||
CommonResult<Void> result = new CommonResult<>();
|
||||
try {
|
||||
callback.execute();
|
||||
result.setStatus(ResultCode.SUCCESS.getCode());
|
||||
result.setMessage(ResultCode.SUCCESS.getDesc());
|
||||
}
|
||||
catch(BizCoreException e) {
|
||||
log.error("", e);
|
||||
ResultCode code = e.getCode() == null ? ResultCode.FAILED : e.getCode();
|
||||
result.setStatus(code.getCode());
|
||||
result.setMessage(StringUtils.isBlank(e.getMessage()) ? code.getDesc() : e.getMessage());
|
||||
}
|
||||
catch(Exception e) {
|
||||
log.error("execute error", e);
|
||||
result.setStatus(ResultCode.FAILED.getCode());
|
||||
result.setMessage(ResultCode.FAILED.getDesc());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行回调
|
||||
*
|
||||
* @param <T>
|
||||
*/
|
||||
public interface Callback<T>{
|
||||
T doExecute();
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行回调
|
||||
*/
|
||||
public interface VoidCallback{
|
||||
void execute();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package org.nl.common.api;
|
||||
|
||||
/**
|
||||
* 枚举了一些常用API操作码
|
||||
*
|
||||
* @author gbx
|
||||
* @date 2023-03-02
|
||||
*/
|
||||
public enum ResultCode implements IErrorCode{
|
||||
SUCCESS(200, "操作成功"),
|
||||
FAILED(400, "操作失败"),
|
||||
UNAUTHORIZED(401, "暂未登录或token已经过期"),
|
||||
INVALID_PARAMETER(402, "无效参数"),
|
||||
FORBIDDEN(403, "没有相关权限"),
|
||||
VALIDATE_FAILED(404, "参数检验失败"),
|
||||
INVALID_CAPTCHA(503, "验证码已失效或验证码错误"),
|
||||
/**
|
||||
* 员工相关异常 1000 ~ 1199
|
||||
*/
|
||||
STAFF_NOT_FOUND(1000, "员工不存在"),
|
||||
STAFF_EXISTED(1001, "员工已存在"),
|
||||
/**
|
||||
* 部门相关异常 1200 ~ 1399
|
||||
*/
|
||||
DEPT_EXIST(1200, "部门已存在"),
|
||||
PARENT_DEPT_STATUS_EXCEPTION(1201, "父部门状态异常"),
|
||||
DEPT_NOT_EXIST(1202, "部门不存在"),
|
||||
/**
|
||||
* 岗位相关异常 1400 ~ 1599
|
||||
*/
|
||||
POST_EXIST(1400, "岗位已存在"),
|
||||
POST_NOT_EXIST(1401, "岗位不存在"),
|
||||
/**
|
||||
* 菜单相关异常 1600 ~ 1799
|
||||
*/
|
||||
MENU_EXIST(1600, "菜单已存在"),
|
||||
MENU_NOT_EXIST(1601, "菜单不存在"),
|
||||
MENU_STATUS_EXCEPTION(1602, "菜单状态异常"),
|
||||
/**
|
||||
* 角色相关异常 1800 ~ 1999
|
||||
*/
|
||||
ROLE_EXIST(1800, "角色已存在"),
|
||||
ROLE_NOT_EXIST(1801, "角色不存在"),
|
||||
/**
|
||||
* 账户相关异常 2000 ~ 2099
|
||||
*/
|
||||
ACCOUNT_EXCEPTION(2000, "账户异常"),
|
||||
INVALID_RECHARGE_AMOUNT(2001, "无效金额"),
|
||||
BALANCE_NOT_ENOUGH(2002, "余额不足"),
|
||||
ACCOUNT_NOT_EXIST(2003, "账户不存在"),
|
||||
STORAGE_NOT_ENOUGH(2004, "库存不足"),
|
||||
IS_EXCHANGED(2005, "已有兑换"),
|
||||
/**
|
||||
* 短信相关 2100 ~ 2199
|
||||
*/
|
||||
ERR_MESSAGE(2100, "短信发送失败"),
|
||||
ERR_SEND_LIMIT(2101, "短信发送上限"),
|
||||
ERR_PHONE(2102, "短信号码不正确"),
|
||||
ERR_Content(2103, "短信内容不能为空");
|
||||
private final Integer code;
|
||||
private final String desc;
|
||||
|
||||
ResultCode(Integer code, String desc) {
|
||||
this.code = code;
|
||||
this.desc = desc;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDesc() {
|
||||
return desc;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user