diff --git a/codermantoolsutils/doc/CouponCalculateService.puml b/codermantoolsutils/doc/CouponCalculateService.puml new file mode 100644 index 0000000000000000000000000000000000000000..5d0b84dfe8bde186be932af3845ca90d8b2be4c9 --- /dev/null +++ b/codermantoolsutils/doc/CouponCalculateService.puml @@ -0,0 +1,12 @@ +@startuml + +interface "优惠计算处理接口-ICouponCalculateService" as ICouponCalculateService{ + 通用优惠处理能力:int calculate(List couponList,String orderCode) + 小额度优惠处理能力,直接计算:int calculateSmall(List couponList,Order order) + 大额度优惠处理:int calculateSmall(List globalCouponList,List shopCouponList,Order order) +} + +class "店铺优惠计算-ShopCalculateService" as ShopCalculateService{} +class "全局优惠计算-GlobalCalculateService" as GlobalCalculateService{} + +@enduml \ No newline at end of file diff --git a/codermantoolsutils/doc/KVPair.puml b/codermantoolsutils/doc/KVPair.puml new file mode 100644 index 0000000000000000000000000000000000000000..0e5b85300af920b3f21706778ef8c4edcf831328 --- /dev/null +++ b/codermantoolsutils/doc/KVPair.puml @@ -0,0 +1,41 @@ +@startuml +class "键值对" as KVPairBO { + 主键:Long id; + K k; + V v; + key属性分组键:String groupKey; + 键值对所属的对象:String relationObject; + 键值对所属对象的哪一条记录:Long relationId; + 键值对所属的父级KeyId:Long superId + //get/set/construct + +构建k-v实例:static KVPair build(K k, V v) + +判断KV对象是否是同一个对象:boolean equals(Object o) + +对value是json串的进行反序列化:Object parseObject() + +对value是数组列表的进行反序列化:List parseList() + +对value是数组列表的获取列表大小:Integer getValueListSize() + +对value进行特定值类型的转换:Long parseLong() + +对value进行特定值类型的转换:Integer parseInteger() + +判断给定值是否在当前value中:Boolean containsValue() +} + +class "键值对的业务操作服务类" as KVPairService{ + 查询所有:List getAll() + 根据ID返回:KVPairBO getById(Long id) + 插入并返回:KVPairBO insert(KVPairBO kVPairBO) + 根据key判断是否存在:boolean containsKey(String key) + 根据key+groupKey判断是否存在:boolean containsKey(String key,String groupKey) + 按groupKey查询所有:List getListByGroupKey(String groupKey) + 按groupKey查询:Map getAllMap(String groupKey) + + 按所属对象查询所有:List getListByRelationObj(String relationObject) + 按所属对象的记录查询所有:List getListByRelationId(String relationObject,Long relationId) + 修改:boolean update(KVPairBO kVPairBO) + 删除:boolean deleteById(Long id) + 根据所属对象ID删除:boolean deleteByRelation(String relationObject,Long relationId) + + 根据ID返回其下级所有k-v结构:List getListBySuperId(Long superId,boolean loadAllChild) + 判断某keyID下的子集是否存在某k-v:boolean containsKV(Long superId,KVPairBO) + 按父级ID删除:boolean deleteBySuperId(Long superId) +} + +@enduml \ No newline at end of file diff --git a/codermantoolsutils/doc/KVPairAbstract.puml b/codermantoolsutils/doc/KVPairAbstract.puml new file mode 100644 index 0000000000000000000000000000000000000000..23acd720258cb78042a9940a02214f3bf392b4a1 --- /dev/null +++ b/codermantoolsutils/doc/KVPairAbstract.puml @@ -0,0 +1,48 @@ +@startuml + +abstract class "抽象通用KV的业务操作服务类-AbstractKVPairService" as AbstractKVPairService{ + 查询所有:List getAll() + 根据ID返回:KVPairBO getById(Long id) + 插入并返回:KVPairBO insert(KVPairBO kVPairBO) + 根据key判断是否存在:boolean containsKey(String key) + +} + +abstract class "可持久化KV的业务操作服务类-PersistKVPairService" as PersistKVPairService{ + 查询所有:List getAll() + 根据ID返回:KVPairBO getById(Long id) + 插入并返回:KVPairBO insert(KVPairBO kVPairBO) + 根据key判断是否存在:boolean containsKey(String key) +} +PersistKVPairService --|> AbstractKVPairService + + +class "分组KV的业务操作服务类-GroupKVPairService" as GroupKVPairService{ + 根据key+groupKey判断是否存在:boolean containsKey(String key,String groupKey) + 按groupKey查询所有:List getListByGroupKey(String groupKey) + 按groupKey查询:Map getAllMap(String groupKey) +} + +GroupKVPairService --|> PersistKVPairService + +class "对象KV的业务操作服务类-ObjectKVPairService" as ObjectKVPairService{ + + 按所属对象查询所有:List getListByRelationObj(String relationObject) + 按所属对象的记录查询所有:List getListByRelationId(String relationObject,Long relationId) + 修改:boolean update(KVPairBO kVPairBO) + 删除:boolean deleteById(Long id) + 根据所属对象ID删除:boolean deleteByRelation(String relationObject,Long relationId) +} + +ObjectKVPairService --|> PersistKVPairService + + +class "树状KV的业务操作服务类-TreeKVPairService" as TreeKVPairService{ + 根据ID返回其下级所有k-v结构:List getListBySuperId(Long superId,boolean loadAllChild) + 判断某keyID下的子集是否存在某k-v:boolean containsKV(Long superId,KVPairBO) + 按父级ID删除:boolean deleteBySuperId(Long superId) +} + +TreeKVPairService --|> PersistKVPairService + +@enduml \ No newline at end of file diff --git a/codermantoolsutils/doc/KVPairInterface.puml b/codermantoolsutils/doc/KVPairInterface.puml new file mode 100644 index 0000000000000000000000000000000000000000..2235b430bb6d48087e09f8be1bac80a396a3db69 --- /dev/null +++ b/codermantoolsutils/doc/KVPairInterface.puml @@ -0,0 +1,66 @@ +@startuml + + +interface "通用KV的业务操作服务类-KVPairService" as KVPairService{ + 查询所有:List getAll() + 根据ID返回:KVPairBO getById(Long id) + 插入并返回:KVPairBO insert(KVPairBO kVPairBO) + 根据key判断是否存在:boolean containsKey(String key) + +} + +interface "分组KV的业务操作服务类-GroupKVPairService" as GroupKVPairService{ + 根据key+groupKey判断是否存在:boolean containsKey(String key,String groupKey) + 按groupKey查询所有:List getListByGroupKey(String groupKey) + 按groupKey查询:Map getAllMap(String groupKey) +} + + +interface "对象KV的业务操作服务类-ObjectKVPairService" as ObjectKVPairService{ + + 按所属对象查询所有:List getListByRelationObj(String relationObject) + 按所属对象的记录查询所有:List getListByRelationId(String relationObject,Long relationId) + 修改:boolean update(KVPairBO kVPairBO) + 删除:boolean deleteById(Long id) + 根据所属对象ID删除:boolean deleteByRelation(String relationObject,Long relationId) +} + + +interface "树状KV的业务操作服务类-TreeKVPairService" as TreeKVPairService{ + 根据ID返回其下级所有k-v结构:List getListBySuperId(Long superId,boolean loadAllChild) + 判断某keyID下的子集是否存在某k-v:boolean containsKV(Long superId,KVPairBO) + 按父级ID删除:boolean deleteBySuperId(Long superId) +} + +class "通用KV的业务操作服务实现-KVPairServiceImpl" as KVPairServiceImpl{ +} + + +class "可持久化KV的业务操作服务实现-PersistKVPairServiceImpl" as PersistKVPairServiceImpl{ +} + +class "分组KV的业务操作服务类-GroupKVPairServiceImpl" as GroupKVPairServiceImpl{ +} + +class "对象KV的业务操作服务类-ObjectKVPairServiceImpl" as ObjectKVPairServiceImpl{ +} + +class "树状KV的业务操作服务类-TreeKVPairServiceImpl" as TreeKVPairServiceImpl{ +} + +KVPairService <|.. KVPairServiceImpl +KVPairService <|.. PersistKVPairServiceImpl + + +GroupKVPairService --|> KVPairService +GroupKVPairService <|.. GroupKVPairServiceImpl + + +ObjectKVPairService --|> KVPairService +ObjectKVPairService <|.. ObjectKVPairServiceImpl + + +TreeKVPairService --|> KVPairService +TreeKVPairService <|.. TreeKVPairServiceImpl + +@enduml \ No newline at end of file diff --git a/codermantoolsutils/pom.xml b/codermantoolsutils/pom.xml index b3a8956c880031dc43d04a674695c47750b5f995..dca87d50d1a82286c843591fa491f792c7744a04 100644 --- a/codermantoolsutils/pom.xml +++ b/codermantoolsutils/pom.xml @@ -6,7 +6,7 @@ com.coderman.utils coderman-utils - 1.0.3-SNAPSHOT + 1.0.5-SNAPSHOT coderman-tools-utils diff --git a/codermantoolsutils/src/main/java/com/coderman/utils/Main.java b/codermantoolsutils/src/main/java/com/coderman/utils/Main.java new file mode 100644 index 0000000000000000000000000000000000000000..69ba37bcf8e1094a5fdb7523d5e27ff85c3149bf --- /dev/null +++ b/codermantoolsutils/src/main/java/com/coderman/utils/Main.java @@ -0,0 +1,14 @@ +package com.coderman.utils; + +/** + * Description: + * date: 2021/11/22 + * + * @author fanchunshuai + * @version 1.0.0 + * @since JDK 1.8 + */ +public class Main { + + +} diff --git a/codermantoolsutils/src/main/java/com/coderman/utils/bean/IConvert.java b/codermantoolsutils/src/main/java/com/coderman/utils/bean/IConvert.java index 3b78156f702120bebc3d3f7d301d4102d200a920..7f1897d8948aa9062740294aa1b151c80829ed9c 100644 --- a/codermantoolsutils/src/main/java/com/coderman/utils/bean/IConvert.java +++ b/codermantoolsutils/src/main/java/com/coderman/utils/bean/IConvert.java @@ -32,7 +32,4 @@ public interface IConvert { * @throws Exception */ T copyPropertity(Class clazz, Object orig) throws Exception; - - - } \ No newline at end of file diff --git a/codermantoolsutils/src/main/java/com/coderman/utils/clazz/ClassUtils.java b/codermantoolsutils/src/main/java/com/coderman/utils/clazz/ClassUtils.java index 6a01fa2ae55277dc5e256badfe1601ea006d63b8..a6dfa9da2d30478dc610578667ddfad1e1329df7 100644 --- a/codermantoolsutils/src/main/java/com/coderman/utils/clazz/ClassUtils.java +++ b/codermantoolsutils/src/main/java/com/coderman/utils/clazz/ClassUtils.java @@ -8,6 +8,7 @@ import javassist.bytecode.CodeAttribute; import javassist.bytecode.LocalVariableAttribute; import javassist.bytecode.MethodInfo; import org.apache.commons.lang3.StringUtils; +import sun.tools.jar.resources.jar; import java.io.File; import java.io.FileInputStream; @@ -15,19 +16,11 @@ import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Method; import java.lang.reflect.Modifier; -import java.net.JarURLConnection; -import java.net.URISyntaxException; -import java.net.URL; +import java.net.*; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Enumeration; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.jar.JarEntry; import java.util.jar.JarFile; import java.util.stream.Stream; @@ -308,4 +301,76 @@ public class ClassUtils { return raw; } + + public static void main(String[] args) throws IOException, ClassNotFoundException { + + String default_path ="/Users/dasouche/.m2/repository/cn/hutool/hutool-core/5.3.10/hutool-core-5.3.10.jar"; + URL[] urls = new URL[1]; + urls[0] = new URL("file:" + default_path); + + URLClassLoader classLoader = new URLClassLoader(urls); + Map> classMethodMap = new LinkedHashMap>(); + JarFile jarfile = new JarFile(default_path); + Enumeration iterator = jarfile.entries(); + while (iterator.hasMoreElements()) { + //这里拿到的一般的"aa/bb/.../cc.class"格式的Entry或 "包路径" + JarEntry jarEntry = iterator.nextElement(); + if (!jarEntry.isDirectory()) { + String name = jarEntry.getName(); + //对于拿到的文件,要去除末尾的.class + int lastDotClassIndex = name.lastIndexOf(".class"); + if(lastDotClassIndex != -1) { + int lastSlashIndex = name.lastIndexOf(SLASH_CHAR); + name = name.replace(SLASH_CHAR, DOT_CHAR); + + Class clazz = classLoader.loadClass(name.replace(".class","")); + System.out.println(clazz.getPackage().getName()+"."+clazz.getName()); + java.lang.reflect.Method[] methodArr = clazz.getDeclaredMethods(); + System.out.println(methodArr.length +"=========="); + } + } + } + + } + + private static void loadJar(String jarPath) throws MalformedURLException { + File jarFile = new File(jarPath); // 从URLClassLoader类中获取类所在文件夹的方法,jar也可以认为是一个文件夹 + + if (jarFile.exists() == false) { + System.out.println("jar file not found."); + return; + } + + //获取类加载器的addURL方法,准备动态调用 + Method method = null; + try { + method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class); + } catch (NoSuchMethodException | SecurityException e1) { + e1.printStackTrace(); + } + + // 获取方法的访问权限,保存原始值 + boolean accessible = method.isAccessible(); + try { + //修改访问权限为可写 + if (accessible == false) { + method.setAccessible(true); + } + + // 获取系统类加载器 + URLClassLoader classLoader = (URLClassLoader) ClassLoader.getSystemClassLoader(); + + //获取jar文件的url路径 + java.net.URL url = jarFile.toURI().toURL(); + + //jar路径加入到系统url路径里 + method.invoke(classLoader, url); + } catch (Exception e) { + e.printStackTrace(); + } finally { + //回写访问权限 + method.setAccessible(accessible); + } + + } } diff --git a/codermantoolsutils/src/main/java/com/coderman/utils/commonbo/PageBO.java b/codermantoolsutils/src/main/java/com/coderman/utils/commonbo/PageBO.java new file mode 100644 index 0000000000000000000000000000000000000000..8a79826a765aee8e9f3697a29c0df8fe02751d13 --- /dev/null +++ b/codermantoolsutils/src/main/java/com/coderman/utils/commonbo/PageBO.java @@ -0,0 +1,379 @@ +package com.coderman.utils.commonbo; + +import java.io.Serializable; +import java.util.List; +import java.util.Map; + +/** + * Description:参考Mybatis_PageHelper的Page对象封装的BO + * date: 2021/10/26 + * + * @author fanchunshuai + * @version 1.0.0 + * @since JDK 1.8 + */ +public class PageBO implements Serializable { + + private static final long serialVersionUID = -8281210709513240574L; + /** + * 页码,从1开始 + */ + private int currentPageNum; + + /** + * 当前页的下一页 + */ + private int nextPageNum; + + /** + * 当前页的上一页 + */ + private int prePageNum; + /** + * 页面大小 + */ + private int pageSize; + /** + * 起始行 + */ + private int startRow; + /** + * 末行 + */ + private int endRow; + /** + * 总数 + */ + private long totalRows; + /** + * 总页数 + */ + private int totalPages; + /** + * 包含count查询 + */ + private boolean count = true; + /** + * 分页合理化 + */ + private Boolean reasonable; + /** + * 当设置为true的时候,如果pagesize设置为0(或RowBounds的limit=0),就不执行分页,返回全部结果 + */ + private Boolean pageSizeZero; + /** + * 进行count查询的列名 + */ + private String countColumn; + /** + * 排序 + */ + private String orderBy; + /** + * 只增加排序 + */ + private boolean orderByOnly; + + private List Items; + + + /** + * 针对简单场景 参数的查询map处理 + */ + private Map queryMap; + + /** + * 针对复杂场景 参数的查询queryDTO对象处理 + */ + private Object queryDTO; + + + public Map getQueryMap() { + return queryMap; + } + + public void setQueryMap(Map queryMap) { + this.queryMap = queryMap; + } + + public Object getQueryDTO() { + return queryDTO; + } + + public void setQueryDTO(Object queryDTO) { + this.queryDTO = queryDTO; + } + + public List getItems() { + return Items; + } + + public PageBO setItems(List items) { + Items = items; + return this; + } + + public PageBO() { + super(); + } + + public PageBO(int pageNum, int pageSize) { + this(pageNum, pageSize, true, null); + } + + public PageBO(int pageNum, int pageSize, boolean count) { + this(pageNum, pageSize, count, null); + } + + private PageBO(int pageNum, int pageSize, boolean count, Boolean reasonable) { + + if (pageNum == 1 && pageSize == Integer.MAX_VALUE) { + pageSizeZero = true; + pageSize = 0; + } + this.currentPageNum = pageNum; + this.pageSize = pageSize; + this.count = count; + calculateStartAndEndRow(); + setReasonable(reasonable); + } + + public int getNextIndex() { + if (this.currentPageNum >= this.getTotalPages()) { + this.nextPageNum = this.currentPageNum; + } else { + this.nextPageNum = this.currentPageNum + 1; + } + + return this.nextPageNum; + } + + public int getPreIndex() { + if (this.currentPageNum <= 1) { + this.prePageNum = 0; + } else { + this.prePageNum = this.currentPageNum - 1; + } + + return this.prePageNum; + } + + + public int getTotalPages() { + return totalPages; + } + + public PageBO setPages(int totalPages) { + this.totalPages = totalPages; + return this; + } + + public int getEndRow() { + return endRow; + } + + public PageBO setEndRow(int endRow) { + this.endRow = endRow; + return this; + } + + public int getCurrentPageNum() { + return currentPageNum; + } + + public PageBO setPageNum(int currentPageNum) { + //分页合理化,针对不合理的页码自动处理 + this.currentPageNum = ((reasonable != null && reasonable) && currentPageNum <= 0) ? 1 : currentPageNum; + return this; + } + + public int getPageSize() { + return pageSize; + } + + public PageBO setPageSize(int pageSize) { + this.pageSize = pageSize; + return this; + } + + public int getStartRow() { + return startRow; + } + + public PageBO setStartRow(int startRow) { + this.startRow = startRow; + return this; + } + + public long getTotalRows() { + return totalRows; + } + + public void setTotal(long totalRows) { + this.totalRows = totalRows; + if (totalRows == -1) { + totalPages = 1; + return; + } + if (pageSize > 0) { + totalPages = (int) (totalRows / pageSize + ((totalRows % pageSize == 0) ? 0 : 1)); + } else { + totalPages = 0; + } + //分页合理化,针对不合理的页码自动处理 + if ((reasonable != null && reasonable) && currentPageNum > totalPages) { + if(totalPages!=0){ + currentPageNum = totalPages; + } + calculateStartAndEndRow(); + } + } + + public Boolean getReasonable() { + return reasonable; + } + + public PageBO setReasonable(Boolean reasonable) { + if (reasonable == null) { + return this; + } + this.reasonable = reasonable; + //分页合理化,针对不合理的页码自动处理 + if (this.reasonable && this.currentPageNum <= 0) { + this.currentPageNum = 1; + calculateStartAndEndRow(); + } + return this; + } + + public Boolean getPageSizeZero() { + return pageSizeZero; + } + + public PageBO setPageSizeZero(Boolean pageSizeZero) { + if (pageSizeZero != null) { + this.pageSizeZero = pageSizeZero; + } + return this; + } + public String getOrderBy() { + return orderBy; + } + + public PageBO setOrderBy(String orderBy) { + this.orderBy = orderBy; + return (PageBO) this; + } + + public boolean isOrderByOnly() { + return orderByOnly; + } + + public void setOrderByOnly(boolean orderByOnly) { + this.orderByOnly = orderByOnly; + } + + /** + * 计算起止行号 + */ + private void calculateStartAndEndRow() { + this.startRow = this.currentPageNum > 0 ? (this.currentPageNum - 1) * this.pageSize : 0; + this.endRow = this.startRow + this.pageSize * (this.currentPageNum > 0 ? 1 : 0); + } + + public boolean isCount() { + return this.count; + } + + public PageBO setCount(boolean count) { + this.count = count; + return this; + } + + /** + * 设置页码 + * + * @param pageNum + * @return + */ + public PageBO pageNum(int pageNum) { + //分页合理化,针对不合理的页码自动处理 + this.currentPageNum = ((reasonable != null && reasonable) && pageNum <= 0) ? 1 : pageNum; + return this; + } + + /** + * 设置页面大小 + * + * @param pageSize + * @return + */ + public PageBO pageSize(int pageSize) { + this.pageSize = pageSize; + calculateStartAndEndRow(); + return this; + } + + + /** + * 设置合理化 + * + * @param reasonable + * @return + */ + public PageBO reasonable(Boolean reasonable) { + setReasonable(reasonable); + return this; + } + + /** + * 当设置为true的时候,如果pagesize设置为0(或RowBounds的limit=0),就不执行分页,返回全部结果 + * + * @param pageSizeZero + * @return + */ + public PageBO pageSizeZero(Boolean pageSizeZero) { + setPageSizeZero(pageSizeZero); + return this; + } + + /** + * 指定 count 查询列 + * + * @param columnName + * @return + */ + public PageBO countColumn(String columnName) { + this.countColumn = columnName; + return this; + } + + + + public String getCountColumn() { + return countColumn; + } + + public void setCountColumn(String countColumn) { + this.countColumn = countColumn; + } + + @Override + public String toString() { + return "PageDTO{" + + "currentPageNum=" + currentPageNum + + ", pageSize=" + pageSize + + ", startRow=" + startRow + + ", endRow=" + endRow + + ", totalRows=" + totalRows + + ", totalPages=" + totalPages + + ", count=" + count + + ", reasonable=" + reasonable + + ", pageSizeZero=" + pageSizeZero + + ", countColumn='" + countColumn + '\'' + + ", orderBy='" + orderBy + '\'' + + ", orderByOnly=" + orderByOnly + + ", Items=" + Items + + '}'; + } +} diff --git a/codermantoolsutils/src/main/java/com/coderman/utils/error/CommonErrorEnum.java b/codermantoolsutils/src/main/java/com/coderman/utils/error/CommonErrorEnum.java new file mode 100644 index 0000000000000000000000000000000000000000..67a918de0bfdbbd94481dcd9e2f44a9071528346 --- /dev/null +++ b/codermantoolsutils/src/main/java/com/coderman/utils/error/CommonErrorEnum.java @@ -0,0 +1,36 @@ +package com.coderman.utils.error; + +/** + * Description:通用异常提醒 + * date: 2021/10/26 + * + * @author fanchunshuai + * @version 1.0.0 + * @since JDK 1.8 + */ +public enum CommonErrorEnum implements IErrorEnum { + + + DEFAULT_ERROR("E00000000", "系统异常"), + REQUEST_OBJECT_IS_NULL_ERROR("W00000001", "入参对象为空"), + PARAMS_CANNOT_BE_NULL_ERROR("W00000002", "参数不能为空"), + DAO_ERROR("E0000003", "数据库处理异常"), + ; + private String code; + private String msg; + CommonErrorEnum(String code,String msg){ + this.code = code; + this.msg = msg; + } + + + @Override + public String getErrorCode() { + return code; + } + + @Override + public String getErrorMsg() { + return msg; + } +} diff --git a/codermantoolsutils/src/main/java/com/coderman/utils/error/IErrorEnum.java b/codermantoolsutils/src/main/java/com/coderman/utils/error/IErrorEnum.java index 0f355615cec3576ba21913cc49ecdc8ded87f299..9c685fbb1c8ec401b5ba6f399412689bd6b66343 100644 --- a/codermantoolsutils/src/main/java/com/coderman/utils/error/IErrorEnum.java +++ b/codermantoolsutils/src/main/java/com/coderman/utils/error/IErrorEnum.java @@ -1,11 +1,15 @@ package com.coderman.utils.error; /** - * description: IErrorEnum
- * date: 2020/10/14 23:37
- * author: coderman
- * version: 1.0
+ * Description: + * date: 2020/10/14 + * + * @author fanchunshuai + * @version 1.0.0 + * @since JDK 1.8 */ public interface IErrorEnum { + String getErrorCode(); + String getErrorMsg(); } diff --git a/codermantoolsutils/src/main/java/com/coderman/utils/exceptions/ExceptionBase.java b/codermantoolsutils/src/main/java/com/coderman/utils/exceptions/ExceptionBase.java new file mode 100644 index 0000000000000000000000000000000000000000..355663fb1be390aec9c7002b3bd6e0eb8890a827 --- /dev/null +++ b/codermantoolsutils/src/main/java/com/coderman/utils/exceptions/ExceptionBase.java @@ -0,0 +1,93 @@ +package com.coderman.utils.exceptions; + +/** + * Description:异常基类 + * date: 2022/1/5 + * + * @author shenshuai + * @version 1.0.0 + * @since JDK 1.8 + */ +public class ExceptionBase extends RuntimeException { + public ExceptionBase(String msg) { + super(msg); + } + + + /** + * 异常对应的rpc码 + */ + private int rpcCode = 500; + + /** + * 错误码 + */ + private String errCode = Integer.toString(rpcCode); + + /** + * 是否异常型错误,true-表示系统故障或数据出现异常 false-表示正常逻辑的失败分支 + */ + private boolean fault; + + + /** + * 是否是通过Exceptions.fail或fault这种方式来的 + */ + private boolean fromExceptionsMethod; + + + public ExceptionBase(){ + + } + + public ExceptionBase(String errCode, String errorMsg) { + super(errorMsg); + this.errCode = errCode; + } + + public ExceptionBase(int httpCode, String errCode, String errorMsg, Throwable e) { + super(errorMsg, e); + this.rpcCode = httpCode; + this.errCode = errCode; + + } + + public ExceptionBase(int httpCode, String errCode, String errorMsg) { + super(errorMsg); + this.rpcCode = httpCode; + this.errCode = errCode; + } + + public int getRpcCode() { + return rpcCode; + } + + public void setRpcCode(int rpcCode) { + this.rpcCode = rpcCode; + } + + public String getErrCode() { + return errCode; + } + + public void setErrCode(String errCode) { + this.errCode = errCode; + } + + public boolean isFault() { + return fault; + } + + public void setFault(boolean isFault) { + this.fault = isFault; + } + + public boolean isFromExceptionsMethod() { + return fromExceptionsMethod; + } + + public void setFromExceptionsMethod(boolean fromExceptionsMethod) { + this.fromExceptionsMethod = fromExceptionsMethod; + } + +} diff --git a/codermantoolsutils/src/main/java/com/coderman/utils/exceptions/ExceptionUtils.java b/codermantoolsutils/src/main/java/com/coderman/utils/exceptions/ExceptionUtils.java new file mode 100644 index 0000000000000000000000000000000000000000..3dd21cd96bffd7d8725afdc3d97ebef21cd8fe83 --- /dev/null +++ b/codermantoolsutils/src/main/java/com/coderman/utils/exceptions/ExceptionUtils.java @@ -0,0 +1,56 @@ +package com.coderman.utils.exceptions; + + +import com.coderman.utils.error.IErrorEnum; + +import java.text.MessageFormat; + +/** + * Description: + * date: 2022/1/22 + * + * @author shenshuai + * @version 1.0.0 + * @since JDK 1.8 + */ +public class ExceptionUtils { + + /** + * @desc 对外统一暴漏一个打异常的接口 + * 适用于使用默认的IError实现类中的code,msg去打异常 + * @param error + * @return + * @throws ExceptionBase + */ + public static ExceptionBase error(IErrorEnum error) throws ExceptionBase { + return new ExceptionBase(error.getErrorCode(),error.getErrorMsg()); + } + + /** + * @desc 对外统一暴漏一个打异常的接口 + * 适用于使用默认的IError实现类中的code加上自定义的简短msg去打异常 + * @param error + * @param msg + * @return + * @throws ExceptionBase + */ + public static ExceptionBase error(IErrorEnum error, String msg) throws ExceptionBase { + return new ExceptionBase(error.getErrorCode(),msg); + } + + + + /** + * @desc 对外统一暴漏一个打异常的接口 + * 适用于使用默认的IError实现类中的code,带格式化的msg和传入的参数objects,进行消息格式化 + * @param error error + * @param objects objects + * @return return + * @throws ExceptionBase + */ + public static ExceptionBase errorFormat(IErrorEnum error, Object... objects) throws ExceptionBase { + return new ExceptionBase(error.getErrorCode(), MessageFormat.format(error.getErrorMsg(), objects)); + } + + +} diff --git a/codermantoolsutils/src/main/java/com/coderman/utils/exceptions/ParamCheckException.java b/codermantoolsutils/src/main/java/com/coderman/utils/exceptions/ParamCheckException.java new file mode 100644 index 0000000000000000000000000000000000000000..71d1102eb006c39d3b52200b3a55f4c6b74eba92 --- /dev/null +++ b/codermantoolsutils/src/main/java/com/coderman/utils/exceptions/ParamCheckException.java @@ -0,0 +1,16 @@ +package com.coderman.utils.exceptions; + +/** + * Description:参数校验异常 + * date: 2022/1/9 + * + * @author shenshuai + * @version 1.0.0 + * @since JDK 1.8 + */ +public class ParamCheckException extends ExceptionBase { + public ParamCheckException(String msg){ + super(msg); + } + +} diff --git a/codermantoolsutils/src/main/java/com/coderman/utils/page/PageDTO.java b/codermantoolsutils/src/main/java/com/coderman/utils/page/PageDTO.java new file mode 100644 index 0000000000000000000000000000000000000000..8e948a726ec554670eb1446cfd9cc28df3125fd0 --- /dev/null +++ b/codermantoolsutils/src/main/java/com/coderman/utils/page/PageDTO.java @@ -0,0 +1,379 @@ +package com.coderman.utils.page; + +import java.io.Serializable; +import java.util.List; +import java.util.Map; + +/** + * Description:参考Mybatis_PageHelper的Page对象封装的DTO + * date: 2021/10/26 + * + * @author fanchunshuai + * @version 1.0.0 + * @since JDK 1.8 + */ +public class PageDTO implements Serializable { + + private static final long serialVersionUID = -2470832822882514457L; + /** + * 页码,从1开始 + */ + private int currentPageNum; + + /** + * 当前页的下一页 + */ + private int nextPageNum; + + /** + * 当前页的上一页 + */ + private int prePageNum; + /** + * 页面大小 + */ + private int pageSize; + /** + * 起始行 + */ + private int startRow; + /** + * 末行 + */ + private int endRow; + /** + * 总数 + */ + private long totalRows; + /** + * 总页数 + */ + private int totalPages; + /** + * 包含count查询 + */ + private boolean count = true; + /** + * 分页合理化 + */ + private Boolean reasonable; + /** + * 当设置为true的时候,如果pagesize设置为0(或RowBounds的limit=0),就不执行分页,返回全部结果 + */ + private Boolean pageSizeZero; + /** + * 进行count查询的列名 + */ + private String countColumn; + /** + * 排序 + */ + private String orderBy; + /** + * 只增加排序 + */ + private boolean orderByOnly; + + private List Items; + + + /** + * 针对简单场景 参数的查询map处理 + */ + private Map queryMap; + + /** + * 针对复杂场景 参数的查询queryDTO对象处理 + */ + private Object queryDTO; + + + public Map getQueryMap() { + return queryMap; + } + + public void setQueryMap(Map queryMap) { + this.queryMap = queryMap; + } + + public Object getQueryDTO() { + return queryDTO; + } + + public void setQueryDTO(Object queryDTO) { + this.queryDTO = queryDTO; + } + + public List getItems() { + return Items; + } + + public PageDTO setItems(List items) { + Items = items; + return this; + } + + public PageDTO() { + super(); + } + + public PageDTO(int pageNum, int pageSize) { + this(pageNum, pageSize, true, null); + } + + public PageDTO(int pageNum, int pageSize, boolean count) { + this(pageNum, pageSize, count, null); + } + + private PageDTO(int pageNum, int pageSize, boolean count, Boolean reasonable) { + + if (pageNum == 1 && pageSize == Integer.MAX_VALUE) { + pageSizeZero = true; + pageSize = 0; + } + this.currentPageNum = pageNum; + this.pageSize = pageSize; + this.count = count; + calculateStartAndEndRow(); + setReasonable(reasonable); + } + + public int getNextIndex() { + if (this.currentPageNum >= this.getTotalPages()) { + this.nextPageNum = this.currentPageNum; + } else { + this.nextPageNum = this.currentPageNum + 1; + } + + return this.nextPageNum; + } + + public int getPreIndex() { + if (this.currentPageNum <= 1) { + this.prePageNum = 0; + } else { + this.prePageNum = this.currentPageNum - 1; + } + + return this.prePageNum; + } + + + public int getTotalPages() { + return totalPages; + } + + public PageDTO setPages(int totalPages) { + this.totalPages = totalPages; + return this; + } + + public int getEndRow() { + return endRow; + } + + public PageDTO setEndRow(int endRow) { + this.endRow = endRow; + return this; + } + + public int getCurrentPageNum() { + return currentPageNum; + } + + public PageDTO setPageNum(int currentPageNum) { + //分页合理化,针对不合理的页码自动处理 + this.currentPageNum = ((reasonable != null && reasonable) && currentPageNum <= 0) ? 1 : currentPageNum; + return this; + } + + public int getPageSize() { + return pageSize; + } + + public PageDTO setPageSize(int pageSize) { + this.pageSize = pageSize; + return this; + } + + public int getStartRow() { + return startRow; + } + + public PageDTO setStartRow(int startRow) { + this.startRow = startRow; + return this; + } + + public long getTotalRows() { + return totalRows; + } + + public void setTotal(long totalRows) { + this.totalRows = totalRows; + if (totalRows == -1) { + totalPages = 1; + return; + } + if (pageSize > 0) { + totalPages = (int) (totalRows / pageSize + ((totalRows % pageSize == 0) ? 0 : 1)); + } else { + totalPages = 0; + } + //分页合理化,针对不合理的页码自动处理 + if ((reasonable != null && reasonable) && currentPageNum > totalPages) { + if(totalPages!=0){ + currentPageNum = totalPages; + } + calculateStartAndEndRow(); + } + } + + public Boolean getReasonable() { + return reasonable; + } + + public PageDTO setReasonable(Boolean reasonable) { + if (reasonable == null) { + return this; + } + this.reasonable = reasonable; + //分页合理化,针对不合理的页码自动处理 + if (this.reasonable && this.currentPageNum <= 0) { + this.currentPageNum = 1; + calculateStartAndEndRow(); + } + return this; + } + + public Boolean getPageSizeZero() { + return pageSizeZero; + } + + public PageDTO setPageSizeZero(Boolean pageSizeZero) { + if (pageSizeZero != null) { + this.pageSizeZero = pageSizeZero; + } + return this; + } + public String getOrderBy() { + return orderBy; + } + + public PageDTO setOrderBy(String orderBy) { + this.orderBy = orderBy; + return (PageDTO) this; + } + + public boolean isOrderByOnly() { + return orderByOnly; + } + + public void setOrderByOnly(boolean orderByOnly) { + this.orderByOnly = orderByOnly; + } + + /** + * 计算起止行号 + */ + private void calculateStartAndEndRow() { + this.startRow = this.currentPageNum > 0 ? (this.currentPageNum - 1) * this.pageSize : 0; + this.endRow = this.startRow + this.pageSize * (this.currentPageNum > 0 ? 1 : 0); + } + + public boolean isCount() { + return this.count; + } + + public PageDTO setCount(boolean count) { + this.count = count; + return this; + } + + /** + * 设置页码 + * + * @param pageNum + * @return + */ + public PageDTO pageNum(int pageNum) { + //分页合理化,针对不合理的页码自动处理 + this.currentPageNum = ((reasonable != null && reasonable) && pageNum <= 0) ? 1 : pageNum; + return this; + } + + /** + * 设置页面大小 + * + * @param pageSize + * @return + */ + public PageDTO pageSize(int pageSize) { + this.pageSize = pageSize; + calculateStartAndEndRow(); + return this; + } + + + /** + * 设置合理化 + * + * @param reasonable + * @return + */ + public PageDTO reasonable(Boolean reasonable) { + setReasonable(reasonable); + return this; + } + + /** + * 当设置为true的时候,如果pagesize设置为0(或RowBounds的limit=0),就不执行分页,返回全部结果 + * + * @param pageSizeZero + * @return + */ + public PageDTO pageSizeZero(Boolean pageSizeZero) { + setPageSizeZero(pageSizeZero); + return this; + } + + /** + * 指定 count 查询列 + * + * @param columnName + * @return + */ + public PageDTO countColumn(String columnName) { + this.countColumn = columnName; + return this; + } + + + + public String getCountColumn() { + return countColumn; + } + + public void setCountColumn(String countColumn) { + this.countColumn = countColumn; + } + + @Override + public String toString() { + return "PageDTO{" + + "currentPageNum=" + currentPageNum + + ", pageSize=" + pageSize + + ", startRow=" + startRow + + ", endRow=" + endRow + + ", totalRows=" + totalRows + + ", totalPages=" + totalPages + + ", count=" + count + + ", reasonable=" + reasonable + + ", pageSizeZero=" + pageSizeZero + + ", countColumn='" + countColumn + '\'' + + ", orderBy='" + orderBy + '\'' + + ", orderByOnly=" + orderByOnly + + ", Items=" + Items + + '}'; + } +} diff --git a/codermantoolsutils/src/main/java/com/coderman/utils/page/PageVO.java b/codermantoolsutils/src/main/java/com/coderman/utils/page/PageVO.java new file mode 100644 index 0000000000000000000000000000000000000000..c6598a21907fc18ec9fe37f44acbdff282af2eb6 --- /dev/null +++ b/codermantoolsutils/src/main/java/com/coderman/utils/page/PageVO.java @@ -0,0 +1,379 @@ +package com.coderman.utils.page; + +import java.io.Serializable; +import java.util.List; +import java.util.Map; + +/** + * Description:参考Mybatis_PageHelper的Page对象封装的DTO + * date: 2021/10/26 + * + * @author fanchunshuai + * @version 1.0.0 + * @since JDK 1.8 + */ +public class PageVO implements Serializable { + + private static final long serialVersionUID = -2470832822882514457L; + /** + * 页码,从1开始 + */ + private int currentPageNum; + + /** + * 当前页的下一页 + */ + private int nextPageNum; + + /** + * 当前页的上一页 + */ + private int prePageNum; + /** + * 页面大小 + */ + private int pageSize; + /** + * 起始行 + */ + private int startRow; + /** + * 末行 + */ + private int endRow; + /** + * 总数 + */ + private long totalRows; + /** + * 总页数 + */ + private int totalPages; + /** + * 包含count查询 + */ + private boolean count = true; + /** + * 分页合理化 + */ + private Boolean reasonable; + /** + * 当设置为true的时候,如果pagesize设置为0(或RowBounds的limit=0),就不执行分页,返回全部结果 + */ + private Boolean pageSizeZero; + /** + * 进行count查询的列名 + */ + private String countColumn; + /** + * 排序 + */ + private String orderBy; + /** + * 只增加排序 + */ + private boolean orderByOnly; + + private List Items; + + + /** + * 针对简单场景 参数的查询map处理 + */ + private Map queryMap; + + /** + * 针对复杂场景 参数的查询queryDTO对象处理 + */ + private Object queryDTO; + + + public Map getQueryMap() { + return queryMap; + } + + public void setQueryMap(Map queryMap) { + this.queryMap = queryMap; + } + + public Object getQueryDTO() { + return queryDTO; + } + + public void setQueryDTO(Object queryDTO) { + this.queryDTO = queryDTO; + } + + public List getItems() { + return Items; + } + + public PageVO setItems(List items) { + Items = items; + return this; + } + + public PageVO() { + super(); + } + + public PageVO(int pageNum, int pageSize) { + this(pageNum, pageSize, true, null); + } + + public PageVO(int pageNum, int pageSize, boolean count) { + this(pageNum, pageSize, count, null); + } + + private PageVO(int pageNum, int pageSize, boolean count, Boolean reasonable) { + + if (pageNum == 1 && pageSize == Integer.MAX_VALUE) { + pageSizeZero = true; + pageSize = 0; + } + this.currentPageNum = pageNum; + this.pageSize = pageSize; + this.count = count; + calculateStartAndEndRow(); + setReasonable(reasonable); + } + + public int getNextIndex() { + if (this.currentPageNum >= this.getTotalPages()) { + this.nextPageNum = this.currentPageNum; + } else { + this.nextPageNum = this.currentPageNum + 1; + } + + return this.nextPageNum; + } + + public int getPreIndex() { + if (this.currentPageNum <= 1) { + this.prePageNum = 0; + } else { + this.prePageNum = this.currentPageNum - 1; + } + + return this.prePageNum; + } + + + public int getTotalPages() { + return totalPages; + } + + public PageVO setPages(int totalPages) { + this.totalPages = totalPages; + return this; + } + + public int getEndRow() { + return endRow; + } + + public PageVO setEndRow(int endRow) { + this.endRow = endRow; + return this; + } + + public int getCurrentPageNum() { + return currentPageNum; + } + + public PageVO setPageNum(int currentPageNum) { + //分页合理化,针对不合理的页码自动处理 + this.currentPageNum = ((reasonable != null && reasonable) && currentPageNum <= 0) ? 1 : currentPageNum; + return this; + } + + public int getPageSize() { + return pageSize; + } + + public PageVO setPageSize(int pageSize) { + this.pageSize = pageSize; + return this; + } + + public int getStartRow() { + return startRow; + } + + public PageVO setStartRow(int startRow) { + this.startRow = startRow; + return this; + } + + public long getTotalRows() { + return totalRows; + } + + public void setTotal(long totalRows) { + this.totalRows = totalRows; + if (totalRows == -1) { + totalPages = 1; + return; + } + if (pageSize > 0) { + totalPages = (int) (totalRows / pageSize + ((totalRows % pageSize == 0) ? 0 : 1)); + } else { + totalPages = 0; + } + //分页合理化,针对不合理的页码自动处理 + if ((reasonable != null && reasonable) && currentPageNum > totalPages) { + if(totalPages!=0){ + currentPageNum = totalPages; + } + calculateStartAndEndRow(); + } + } + + public Boolean getReasonable() { + return reasonable; + } + + public PageVO setReasonable(Boolean reasonable) { + if (reasonable == null) { + return this; + } + this.reasonable = reasonable; + //分页合理化,针对不合理的页码自动处理 + if (this.reasonable && this.currentPageNum <= 0) { + this.currentPageNum = 1; + calculateStartAndEndRow(); + } + return this; + } + + public Boolean getPageSizeZero() { + return pageSizeZero; + } + + public PageVO setPageSizeZero(Boolean pageSizeZero) { + if (pageSizeZero != null) { + this.pageSizeZero = pageSizeZero; + } + return this; + } + public String getOrderBy() { + return orderBy; + } + + public PageVO setOrderBy(String orderBy) { + this.orderBy = orderBy; + return (PageVO) this; + } + + public boolean isOrderByOnly() { + return orderByOnly; + } + + public void setOrderByOnly(boolean orderByOnly) { + this.orderByOnly = orderByOnly; + } + + /** + * 计算起止行号 + */ + private void calculateStartAndEndRow() { + this.startRow = this.currentPageNum > 0 ? (this.currentPageNum - 1) * this.pageSize : 0; + this.endRow = this.startRow + this.pageSize * (this.currentPageNum > 0 ? 1 : 0); + } + + public boolean isCount() { + return this.count; + } + + public PageVO setCount(boolean count) { + this.count = count; + return this; + } + + /** + * 设置页码 + * + * @param pageNum + * @return + */ + public PageVO pageNum(int pageNum) { + //分页合理化,针对不合理的页码自动处理 + this.currentPageNum = ((reasonable != null && reasonable) && pageNum <= 0) ? 1 : pageNum; + return this; + } + + /** + * 设置页面大小 + * + * @param pageSize + * @return + */ + public PageVO pageSize(int pageSize) { + this.pageSize = pageSize; + calculateStartAndEndRow(); + return this; + } + + + /** + * 设置合理化 + * + * @param reasonable + * @return + */ + public PageVO reasonable(Boolean reasonable) { + setReasonable(reasonable); + return this; + } + + /** + * 当设置为true的时候,如果pagesize设置为0(或RowBounds的limit=0),就不执行分页,返回全部结果 + * + * @param pageSizeZero + * @return + */ + public PageVO pageSizeZero(Boolean pageSizeZero) { + setPageSizeZero(pageSizeZero); + return this; + } + + /** + * 指定 count 查询列 + * + * @param columnName + * @return + */ + public PageVO countColumn(String columnName) { + this.countColumn = columnName; + return this; + } + + + + public String getCountColumn() { + return countColumn; + } + + public void setCountColumn(String countColumn) { + this.countColumn = countColumn; + } + + @Override + public String toString() { + return "PageDTO{" + + "currentPageNum=" + currentPageNum + + ", pageSize=" + pageSize + + ", startRow=" + startRow + + ", endRow=" + endRow + + ", totalRows=" + totalRows + + ", totalPages=" + totalPages + + ", count=" + count + + ", reasonable=" + reasonable + + ", pageSizeZero=" + pageSizeZero + + ", countColumn='" + countColumn + '\'' + + ", orderBy='" + orderBy + '\'' + + ", orderByOnly=" + orderByOnly + + ", Items=" + Items + + '}'; + } +} diff --git a/codermantoolsutils/src/main/java/com/coderman/utils/paramcheck/ParamCheckUtil.java b/codermantoolsutils/src/main/java/com/coderman/utils/paramcheck/ParamCheckUtil.java new file mode 100644 index 0000000000000000000000000000000000000000..a0d3b49ae4072c10b8e21bbb234e1364a7dc2d92 --- /dev/null +++ b/codermantoolsutils/src/main/java/com/coderman/utils/paramcheck/ParamCheckUtil.java @@ -0,0 +1,59 @@ +package com.coderman.utils.paramcheck; + +import com.coderman.utils.error.CommonErrorEnum; +import com.coderman.utils.exceptions.ExceptionUtils; +import org.apache.commons.lang3.StringUtils; + +import java.lang.reflect.Method; +import java.lang.reflect.Type; +import java.util.Collection; +import java.util.Map; + +/** + * @author shenshuai + * @since 2020/1/7 5:55 下午 + */ +public class ParamCheckUtil { + + /** + * 校验请求参数是否为空 + * + * @param requestParams 请求入参 + * @param keys 属性值数组 + */ + public static void checkParams(Object requestParams, String... keys) throws Exception { + if (null == requestParams) { + throw ExceptionUtils.error(CommonErrorEnum.REQUEST_OBJECT_IS_NULL_ERROR); + } + StringBuilder sb = new StringBuilder(); + for (String fieldName : keys) { + Object value = null; + Type type = null; + try { + String firstLetter = fieldName.substring(0, 1).toUpperCase(); + String getter = "get" + firstLetter + fieldName.substring(1); + Method method = requestParams.getClass().getMethod(getter); + value = method.invoke(requestParams); + type = method.getReturnType(); + } catch (Exception e) { + throw ExceptionUtils.error(CommonErrorEnum.PARAMS_CANNOT_BE_NULL_ERROR,"获取属性值出错"); + } finally { + // 判空标志 String/Collection/Map特殊处理 + boolean isEmpty = + (null == value) || (String.class == type && StringUtils.isEmpty((String) value)) + || (value instanceof Collection && ((Collection) value).isEmpty()) + || (Map.class == type && ((Map) value).isEmpty()); + if (isEmpty) { + if (sb.length() != 0) { + sb.append(","); + } + sb.append(fieldName); + } + } + } + + if (sb.length() > 0) { + throw ExceptionUtils.error(CommonErrorEnum.PARAMS_CANNOT_BE_NULL_ERROR); + } + } +} diff --git a/codermantoolsutils/src/main/java/com/coderman/utils/response/ResultDataDto.java b/codermantoolsutils/src/main/java/com/coderman/utils/response/ResultDataDto.java index d2f4207ac9b8498cfd02874f968e887586166680..736a7968881e36fdd31d1a4616905a0739c6e5c3 100644 --- a/codermantoolsutils/src/main/java/com/coderman/utils/response/ResultDataDto.java +++ b/codermantoolsutils/src/main/java/com/coderman/utils/response/ResultDataDto.java @@ -7,19 +7,24 @@ package com.coderman.utils.response; * version: 1.0
*/ public class ResultDataDto { - private int code; + private String code; private String msg; private T data; public ResultDataDto(){} public boolean isSuccess(){ - return this.code == CodeEnum.SUCCESS.getCode(); + return this.code == CodeEnum.SUCCESS.getCode()+""; } - public ResultDataDto(int code,String msg){ + public ResultDataDto(String code,String msg){ this.code = code; this.msg = msg; } + public ResultDataDto setErrorCodeMsg(String msg){ + this.code = CodeEnum.ERROR.getCode()+""; + this.msg = msg; + return this; + } public ResultDataDto setData(T data){ if(data == null){ @@ -27,66 +32,50 @@ public class ResultDataDto { this.msg = "获取数据为空"; } this.data = data; - this.code = CodeEnum.SUCCESS.getCode(); + this.code = CodeEnum.SUCCESS.getCode()+""; this.msg = "成功"; return this; } - public static ResultDataDto setNullErrorMsg(String msg){ - String newMsg = "调用参数/结果为空" ; - if(msg != null){ - newMsg = newMsg + msg; - } - return new ResultDataDto(CodeEnum.NULL_ERROR.getCode(),newMsg); + public static ResultDataDto success(){ + return new ResultDataDto(CodeEnum.SUCCESS.getCode()+"","成功"); } - public static ResultDataDto setErrorCodeMsg(String msg){ - return new ResultDataDto(ResultDataDto.CodeEnum.ERROR.getCode(),msg); + public static ResultDataDto success(T data){ + ResultDataDto resultDataDto = new ResultDataDto(CodeEnum.SUCCESS.getCode()+"","成功"); + return resultDataDto.setData(data); } - public static ResultDataDto setErrorCodeMsg(int code, String msg){ - return new ResultDataDto(code,msg); + public static ResultDataDto fail(String code,String msg,T data){ + ResultDataDto resultDataDto = new ResultDataDto(code,msg); + return resultDataDto.setData(data); } - public static ResultDataDto setParamErrorMsg(String msg){ - String newMsg = "调用参数/结果为空" ; - if(msg != null){ - newMsg = newMsg + ":" + msg; - } - return new ResultDataDto(CodeEnum.PARAM_ERROR.getCode(),newMsg); + public static ResultDataDto fail(String code,String msg){ + ResultDataDto resultDataDto = new ResultDataDto(code,msg); + return resultDataDto; } - public ResultDataDto setInvokeErrorMsg(String msg){ - String newMsg = "调用方法异常" ; - if(msg != null){ - newMsg = newMsg + ":" + msg; - } - this.code = CodeEnum.INVOKE_ERROR.getCode(); - this.msg = newMsg; - return this; - } + enum CodeEnum{ - SUCCESS(0), - ERROR(-1), - NULL_ERROR(-2), - PARAM_ERROR(-3), - INVOKE_ERROR(-4), + SUCCESS("200"), + ERROR("500"), ; - int code; - CodeEnum(int code){this.code = code;} + String code; + CodeEnum(String code){this.code = code;} - public int getCode() { + public String getCode() { return code; } } - public int getCode() { + public String getCode() { return code; } - public void setCode(int code) { + public void setCode(String code) { this.code = code; } diff --git a/codermantoolsutils/src/main/java/com/coderman/utils/response/ResultDto.java b/codermantoolsutils/src/main/java/com/coderman/utils/response/ResultDto.java index f4cf86ea5e9feaa60f438617fb79fb9c3b5c6888..9bd4cc2871082e04455927e7fbf91f5c8ffc5f9d 100644 --- a/codermantoolsutils/src/main/java/com/coderman/utils/response/ResultDto.java +++ b/codermantoolsutils/src/main/java/com/coderman/utils/response/ResultDto.java @@ -7,7 +7,7 @@ package com.coderman.utils.response; * version: 1.0
*/ public class ResultDto { - private int code; + private String code; private String msg; @@ -16,56 +16,27 @@ public class ResultDto { } public ResultDto(){} - public ResultDto(int code,String msg){ + public ResultDto(String code,String msg){ this.code = code; this.msg = msg; } - public ResultDto setErrorMsg(String msg){ - this.code = ResultDataDto.CodeEnum.ERROR.getCode(); - this.msg = msg; - return this; - } - - public static ResultDto setErrorCodeMsg(String msg){ - return new ResultDto(ResultDataDto.CodeEnum.ERROR.getCode(),msg); - } - public static ResultDto setErrorCodeMsg(int code, String msg){ - return new ResultDto(code,msg); - } - - public static ResultDto setNullErrorMsg(String msg){ - String newMsg = "调用参数/结果为空" ; - if(msg != null){ - newMsg = newMsg + msg; - } - return new ResultDto(ResultDataDto.CodeEnum.NULL_ERROR.getCode(),newMsg); - } - public static ResultDto setParamErrorMsg(String msg){ - String newMsg = "调用参数异常" ; - if(msg != null){ - newMsg = newMsg + ":" + msg; - } - return new ResultDto(ResultDataDto.CodeEnum.PARAM_ERROR.getCode(),newMsg); + public static ResultDto success(){ + return new ResultDto(ResultDataDto.CodeEnum.SUCCESS.getCode()+"","成功"); } - public ResultDto setInvokeErrorMsg(String msg){ - String newMsg = "调用方法异常" ; - if(msg != null){ - newMsg = newMsg + ":" + msg; - } - this.code = ResultDataDto.CodeEnum.INVOKE_ERROR.getCode(); - this.msg = newMsg; - return this; + public static ResultDto fail(String code,String msg){ + ResultDto resultDto = new ResultDto(code,msg); + return resultDto; } - public int getCode() { + public String getCode() { return code; } - public void setCode(int code) { + public void setCode(String code) { this.code = code; } diff --git a/common-starter/springboot-commonstarter-testdemo/src/main/java/com/coderman/common/starter/controller/ConfigController.java b/common-starter/springboot-commonstarter-testdemo/src/main/java/com/coderman/common/starter/controller/ConfigController.java index adff74c1a0146f8cbeab25f9295993549ce0bf8b..0c5a2b0bc238afc953567fa90cf3369518328477 100644 --- a/common-starter/springboot-commonstarter-testdemo/src/main/java/com/coderman/common/starter/controller/ConfigController.java +++ b/common-starter/springboot-commonstarter-testdemo/src/main/java/com/coderman/common/starter/controller/ConfigController.java @@ -40,4 +40,5 @@ public class ConfigController { feeCodeConfigEntity = (FeeCodeConfigEntity)dataJsonService.getByQueryKey(feeCodeConfigEntity); return JSON.toJSONString(feeCodeConfigEntity); } + }