diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..9e527548 --- /dev/null +++ b/.gitignore @@ -0,0 +1,15 @@ +/weaver-hrm-organization.iml +/out/ +/.idea/ + +HELP.md +target/ + +### IntelliJ IDEA ### +.idea + +/src/test +/src/META-INF + +/log + diff --git a/src/com/api/organization/web/DemoController.java b/src/com/api/organization/web/DemoController.java new file mode 100644 index 00000000..f8e72094 --- /dev/null +++ b/src/com/api/organization/web/DemoController.java @@ -0,0 +1,14 @@ +package com.api.organization.web; + +import javax.ws.rs.Path; + +/** + * @Author weaver_cl + * @Description: TODO + * @Date 2022/4/26 + * @Version V1.0 + **/ +@Path("/bs/hrmorganization/demo") +public class DemoController extends com.engine.organization.web.DemoController { + +} diff --git a/src/com/engine/organization/entity/po/DemoUser.java b/src/com/engine/organization/entity/po/DemoUser.java new file mode 100644 index 00000000..4bda8b5c --- /dev/null +++ b/src/com/engine/organization/entity/po/DemoUser.java @@ -0,0 +1,24 @@ +package com.engine.organization.entity.po; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * @Author weaver_cl + * @Description: TODO + * @Date 2022/4/26 + * @Version V1.0 + **/ + +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class DemoUser { + + private String username; + + private Integer age; +} diff --git a/src/com/engine/organization/exception/OrganizationRunTimeException.java b/src/com/engine/organization/exception/OrganizationRunTimeException.java new file mode 100644 index 00000000..fba75ba0 --- /dev/null +++ b/src/com/engine/organization/exception/OrganizationRunTimeException.java @@ -0,0 +1,18 @@ +package com.engine.organization.exception; + +/** + * @Author weaver_cl + * @Description: TODO + * @Date 2022/4/26 + * @Version V1.0 + **/ +public class OrganizationRunTimeException extends RuntimeException{ + + public OrganizationRunTimeException(String message) { + super(message); + } + + public OrganizationRunTimeException(Throwable cause) { + super(cause); + } +} diff --git a/src/com/engine/organization/util/OrganizationAssert.java b/src/com/engine/organization/util/OrganizationAssert.java new file mode 100644 index 00000000..eb3e9368 --- /dev/null +++ b/src/com/engine/organization/util/OrganizationAssert.java @@ -0,0 +1,162 @@ +package com.engine.organization.util; + +import com.engine.organization.exception.OrganizationRunTimeException; +import org.springframework.util.CollectionUtils; +import org.springframework.util.ObjectUtils; + +import java.util.Collection; +import java.util.Map; + +/** + * @Author weaver_cl + * @Description: TODO + * @Date 2022/4/26 + * @Version V1.0 + **/ +public abstract class OrganizationAssert { + /** + * 判断入参不为null + * + * @param object 待检查参数 + * @param message 检查失败返回的异常信息 + */ + public static void notNull(Object object, String message) { + if (object == null) { + throw new OrganizationRunTimeException(message); + } + } + + /** + * 判断多个入参不为null + * + * @param message 检查失败返回的异常信息 + * @param objects 待检查参数 + */ + public static void notNull(String message, Object... objects) { + for (Object obj : objects) { + if (obj == null) { + throw new OrganizationRunTimeException(message); + } + } + } + + /** + * 判断入参为null + * + * @param object 待检查参数 + * @param message 检查失败返回的异常信息 + */ + public static void isNull(Object object, String message) { + if (object != null) { + throw new OrganizationRunTimeException(message); + } + } + + /** + * 判断集合是否为空 + * + * @param collection 待检查参数 + * @param message 检查失败返回的异常信息 + */ + public static void isEmpty(Collection collection, String message) { + if (!CollectionUtils.isEmpty(collection)) { + throw new OrganizationRunTimeException(message); + } + } + + /** + * 判断集合不为空 + * + * @param collection 待检查参数 + * @param message 检查失败返回的异常信息 + */ + public static void notEmpty(Collection collection, String message) { + if (CollectionUtils.isEmpty(collection)) { + throw new OrganizationRunTimeException(message); + } + } + + /** + * 判断数组是否为空 + * + * @param arr 待检查参数 + * @param message 检查失败返回的异常信息 + */ + public static void notEmpty(Object[] arr, String message) { + if (ObjectUtils.isEmpty(arr)) { + throw new OrganizationRunTimeException(message); + } + } + + /** + * 判断map是否为空 + * + * @param map 待检查参数 + * @param message 检查失败返回的异常信息 + */ + public static void notEmpty(Map map, String message) { + if (CollectionUtils.isEmpty(map)) { + throw new OrganizationRunTimeException(message); + } + } + + /** + * 判断数组元素是否为空 + * + * @param arr 待检查参数 + * @param message 检查失败返回的异常信息 + */ + public static void notNullElement(Object[] arr, String message) { + if (arr != null) { + for (Object obj : arr) { + if (obj == null) { + throw new OrganizationRunTimeException(message); + } + } + } + } + + /** + * 判断boolean + * + * @param expression 待检查参数 + * @param message 检查失败返回的异常信息 + */ + public static void isTrue(boolean expression, String message) { + if (!expression) { + throw new OrganizationRunTimeException(message); + } + } + + public static void isFalse(boolean expression, String message) { + if (expression) { + throw new OrganizationRunTimeException(message); + } + } + + public static void isBlank(CharSequence cs, String message) { + int strLen; + if (cs != null && (strLen = cs.length()) != 0) { + for (int i = 0; i < strLen; ++i) { + if (!Character.isWhitespace(cs.charAt(i))) { + throw new OrganizationRunTimeException(message); + } + } + } + } + + public static void notBlank(CharSequence cs, String message) { + int strLen; + if (cs != null && (strLen = cs.length()) != 0) { + for (int i = 0; i < strLen; ++i) { + if (Character.isWhitespace(cs.charAt(i))) { + throw new OrganizationRunTimeException(message); + } + } + } + if (cs == null || cs.length() == 0) { + throw new OrganizationRunTimeException(message); + } + + } +} diff --git a/src/com/engine/organization/util/response/ResultCode.java b/src/com/engine/organization/util/response/ResultCode.java new file mode 100644 index 00000000..92670362 --- /dev/null +++ b/src/com/engine/organization/util/response/ResultCode.java @@ -0,0 +1,38 @@ +package com.engine.organization.util.response; + +public enum ResultCode implements IErrorCode { + SUCCESS(200,"操作成功"), + FAILED(500,"操作失败"), + VALIDATE_FAILED(404,"参数检验失败"), + UNAUTHORIZED(401,"暂未登陆或相关token已经过期"), + FORBIDDEN(403,"没有相关权限"), + + + BODY_NOT_MATCH(400,"请求的数据格式不符!"), + INTERNAL_SERVER_ERROR(500, "服务器内部错误!"), + SERVER_BUSY(503,"服务器正忙,请稍后再试!"); + private long code; + private String msg; + + ResultCode(long code, String msg) { + this.code = code; + this.msg = msg; + } + + @Override + public long getCode() { + return code; + } + + @Override + public String getMessage() { + return msg; + } +} + +interface IErrorCode { + long getCode(); + + String getMessage(); +} + diff --git a/src/com/engine/organization/util/response/ReturnResult.java b/src/com/engine/organization/util/response/ReturnResult.java new file mode 100644 index 00000000..da3119e0 --- /dev/null +++ b/src/com/engine/organization/util/response/ReturnResult.java @@ -0,0 +1,180 @@ +package com.engine.organization.util.response; + +/** + * @Author weaver_cl + * @Description: TODO + * @Date 2022/4/26 + * @Version V1.0 + **/ +import java.io.Serializable; + +public class ReturnResult implements Serializable { + private static final long serialVersionUID = 1L; + + private long code; + private T data; + private String msg; + + public ReturnResult() { + this.code= ResultCode.SUCCESS.getCode(); + this.msg= ResultCode.SUCCESS.getMessage(); + } + + /** + * @param code + * @param msg + * @param data + */ + + public ReturnResult(long code, String msg, T data) { + this.code = code; + this.data = data; + this.msg = msg; + } + + /** + * 返回 code 和 data + * @param data + */ + public ReturnResult(T data) { + this.code = ResultCode.SUCCESS.getCode(); + this.data = data; + } + + /** + * 返回 code 和 msg + * @param code + * @param msg + */ + public ReturnResult(long code, String msg) { + this.code = code; + this.msg = msg; + } + + public long getCode() { + return code; + } + + public void setCode(long code) { + this.code = code; + } + + public T getData() { + return data; + } + + public void setData(T data) { + this.data = data; + } + + public String getMsg() { + return msg; + } + + public void setMsg(String msg) { + this.msg = msg; + } + + + public ReturnResult Msg(String msg){ + this.setMsg(msg); + return this; + } + + + public ReturnResult Code(long code){ + this.setCode(code); + return this; + } + + + public ReturnResult Data(T data){ + this.setData(data); + return this; + } + + /** + * 返回默认状态 + */ + public static ReturnResult successed() { + return new ReturnResult<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage()); + } + + /** + * 成功返回 + * + * @param data + * @return + */ + public static ReturnResult successed(T data) { + + return new ReturnResult<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), data); + } + + /** + * 失败返回结果 + * + * @param errorCode + * @param + * @return + */ + public static ReturnResult failed(IErrorCode errorCode) { + return new ReturnResult<>(errorCode.getCode(), errorCode.getMessage(), null); + } + + /** + * 返回失败状态 + * + * @param msg + * @return + */ + public static ReturnResult failed(String msg) { + return failed(ResultCode.FAILED); + } + + + /** + * 参数验证失败 + * + * @param + * @return + */ + public static ReturnResult validateFailed() { + return failed(ResultCode.VALIDATE_FAILED); + } + + /** + * 参数验证失败,返回信息 + * + * @param msg + * @param + * @return + */ + public static ReturnResult validateFailed(String msg) { + return new ReturnResult<>(ResultCode.VALIDATE_FAILED.getCode(), msg); + } + + /** + * 未登录返回结果 。 token过期.. + * + * @param data + * @param + * @return + */ + public static ReturnResult unauthorized(T data) { + return new ReturnResult<>(ResultCode.UNAUTHORIZED.getCode(), ResultCode.UNAUTHORIZED.getMessage(), data); + } + + /** + * 未授权返回结果 + * + * @param data + * @param + * @return + */ + public static ReturnResult forbiden(T data) { + return new ReturnResult<>(ResultCode.FORBIDDEN.getCode(), ResultCode.FORBIDDEN.getMessage(), data); + } + + +} diff --git a/src/com/engine/organization/web/DemoController.java b/src/com/engine/organization/web/DemoController.java new file mode 100644 index 00000000..807df48c --- /dev/null +++ b/src/com/engine/organization/web/DemoController.java @@ -0,0 +1,39 @@ +package com.engine.organization.web; + +import com.engine.common.util.ServiceUtil; +import com.engine.organization.util.response.ReturnResult; +import com.engine.organization.wrapper.DemoWrapper; +import weaver.hrm.HrmUserVarify; +import weaver.hrm.User; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.MediaType; + +/** + * @Author weaver_cl + * @Description: TODO + * @Date 2022/4/26 + * @Version V1.0 + **/ +public class DemoController { + + public DemoWrapper getDemoWrapper(User user){ + return ServiceUtil.getService(DemoWrapper.class,user); + } + + @GET + @Path("/test") + @Produces(MediaType.APPLICATION_JSON) + public ReturnResult testDemo(@Context HttpServletRequest request, @Context HttpServletResponse response, + @QueryParam("name") String name) { + User user = HrmUserVarify.getUser(request, response); + return getDemoWrapper(user).testDemo(name); + } + +} diff --git a/src/com/engine/organization/wrapper/DemoWrapper.java b/src/com/engine/organization/wrapper/DemoWrapper.java new file mode 100644 index 00000000..248d88ac --- /dev/null +++ b/src/com/engine/organization/wrapper/DemoWrapper.java @@ -0,0 +1,23 @@ +package com.engine.organization.wrapper; + +import com.engine.core.impl.Service; +import com.engine.organization.entity.po.DemoUser; +import com.engine.organization.util.OrganizationAssert; +import com.engine.organization.util.response.ReturnResult; + +/** + * @Author weaver_cl + * @Description: TODO + * @Date 2022/4/26 + * @Version V1.0 + **/ +public class DemoWrapper extends Service { + + + public ReturnResult testDemo(String name) { + + OrganizationAssert.notNull(name,"参数不能为空"); + DemoUser demoUser = DemoUser.builder().username("张三").age(12).build(); + return ReturnResult.successed(demoUser); + } +}