From 73110d1170a1b0907cb8c5e50fdefcd6db85262c Mon Sep 17 00:00:00 2001 From: jishenghua <752718920@qq.com> Date: Sat, 22 Feb 2025 15:33:29 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E7=A7=9F=E6=88=B7=E7=9A=84?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2=E5=88=86=E9=A1=B5=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jshERP-boot/pom.xml | 5 + .../java/com/jsh/erp/base/AjaxResult.java | 228 ++++++++ .../java/com/jsh/erp/base/BaseController.java | 158 ++++++ .../java/com/jsh/erp/base/PageDomain.java | 87 +++ .../java/com/jsh/erp/base/TableDataInfo.java | 70 +++ .../java/com/jsh/erp/base/TableSupport.java | 56 ++ .../jsh/erp/controller/TenantController.java | 28 +- .../datasource/mappers/TenantMapperEx.java | 3 +- .../com/jsh/erp/exception/UtilException.java | 26 + .../jsh/erp/service/tenant/TenantService.java | 16 +- .../main/java/com/jsh/erp/utils/Convert.java | 536 ++++++++++++++++++ .../java/com/jsh/erp/utils/PageUtils.java | 34 ++ .../java/com/jsh/erp/utils/ServletUtils.java | 123 ++++ .../main/java/com/jsh/erp/utils/SqlUtil.java | 50 ++ .../java/com/jsh/erp/utils/StringUtil.java | 75 +++ 15 files changed, 1465 insertions(+), 30 deletions(-) create mode 100644 jshERP-boot/src/main/java/com/jsh/erp/base/AjaxResult.java create mode 100644 jshERP-boot/src/main/java/com/jsh/erp/base/BaseController.java create mode 100644 jshERP-boot/src/main/java/com/jsh/erp/base/PageDomain.java create mode 100644 jshERP-boot/src/main/java/com/jsh/erp/base/TableDataInfo.java create mode 100644 jshERP-boot/src/main/java/com/jsh/erp/base/TableSupport.java create mode 100644 jshERP-boot/src/main/java/com/jsh/erp/exception/UtilException.java create mode 100644 jshERP-boot/src/main/java/com/jsh/erp/utils/Convert.java create mode 100644 jshERP-boot/src/main/java/com/jsh/erp/utils/PageUtils.java create mode 100644 jshERP-boot/src/main/java/com/jsh/erp/utils/ServletUtils.java create mode 100644 jshERP-boot/src/main/java/com/jsh/erp/utils/SqlUtil.java diff --git a/jshERP-boot/pom.xml b/jshERP-boot/pom.xml index 61ce851e5..68cdf4ea1 100644 --- a/jshERP-boot/pom.xml +++ b/jshERP-boot/pom.xml @@ -123,6 +123,11 @@ pinyin4j 2.5.1 + + com.github.pagehelper + pagehelper-spring-boot-starter + 1.2.13 + diff --git a/jshERP-boot/src/main/java/com/jsh/erp/base/AjaxResult.java b/jshERP-boot/src/main/java/com/jsh/erp/base/AjaxResult.java new file mode 100644 index 000000000..491ad943c --- /dev/null +++ b/jshERP-boot/src/main/java/com/jsh/erp/base/AjaxResult.java @@ -0,0 +1,228 @@ +package com.jsh.erp.base; + +import com.jsh.erp.utils.StringUtil; + +import java.util.HashMap; +import java.util.Objects; + +/** + * 操作消息提醒 + * + * @author ji-sheng-hua + */ +public class AjaxResult extends HashMap +{ + private static final long serialVersionUID = 1L; + + /** 状态码 */ + public static final String CODE_TAG = "code"; + + /** 返回内容 */ + public static final String MSG_TAG = "msg"; + + /** 数据对象 */ + public static final String DATA_TAG = "data"; + + /** + * 状态类型 + */ + public enum Type + { + /** 成功 */ + SUCCESS(0), + /** 警告 */ + WARN(301), + /** 错误 */ + ERROR(500); + private final int value; + + Type(int value) + { + this.value = value; + } + + public int value() + { + return this.value; + } + } + + /** + * 初始化一个新创建的 AjaxResult 对象,使其表示一个空消息。 + */ + public AjaxResult() + { + } + + /** + * 初始化一个新创建的 AjaxResult 对象 + * + * @param type 状态类型 + * @param msg 返回内容 + */ + public AjaxResult(Type type, String msg) + { + super.put(CODE_TAG, type.value); + super.put(MSG_TAG, msg); + } + + /** + * 初始化一个新创建的 AjaxResult 对象 + * + * @param type 状态类型 + * @param msg 返回内容 + * @param data 数据对象 + */ + public AjaxResult(Type type, String msg, Object data) + { + super.put(CODE_TAG, type.value); + super.put(MSG_TAG, msg); + if (StringUtil.isNotNull(data)) + { + super.put(DATA_TAG, data); + } + } + + /** + * 返回成功消息 + * + * @return 成功消息 + */ + public static AjaxResult success() + { + return AjaxResult.success("操作成功"); + } + + /** + * 返回成功数据 + * + * @return 成功消息 + */ + public static AjaxResult success(Object data) + { + return AjaxResult.success("操作成功", data); + } + + /** + * 返回成功消息 + * + * @param msg 返回内容 + * @return 成功消息 + */ + public static AjaxResult success(String msg) + { + return AjaxResult.success(msg, null); + } + + /** + * 返回成功消息 + * + * @param msg 返回内容 + * @param data 数据对象 + * @return 成功消息 + */ + public static AjaxResult success(String msg, Object data) + { + return new AjaxResult(Type.SUCCESS, msg, data); + } + + /** + * 返回警告消息 + * + * @param msg 返回内容 + * @return 警告消息 + */ + public static AjaxResult warn(String msg) + { + return AjaxResult.warn(msg, null); + } + + /** + * 返回警告消息 + * + * @param msg 返回内容 + * @param data 数据对象 + * @return 警告消息 + */ + public static AjaxResult warn(String msg, Object data) + { + return new AjaxResult(Type.WARN, msg, data); + } + + /** + * 返回错误消息 + * + * @return + */ + public static AjaxResult error() + { + return AjaxResult.error("操作失败"); + } + + /** + * 返回错误消息 + * + * @param msg 返回内容 + * @return 警告消息 + */ + public static AjaxResult error(String msg) + { + return AjaxResult.error(msg, null); + } + + /** + * 返回错误消息 + * + * @param msg 返回内容 + * @param data 数据对象 + * @return 警告消息 + */ + public static AjaxResult error(String msg, Object data) + { + return new AjaxResult(Type.ERROR, msg, data); + } + + /** + * 是否为成功消息 + * + * @return 结果 + */ + public boolean isSuccess() + { + return Objects.equals(Type.SUCCESS.value, this.get(CODE_TAG)); + } + + /** + * 是否为警告消息 + * + * @return 结果 + */ + public boolean isWarn() + { + return Objects.equals(Type.WARN.value, this.get(CODE_TAG)); + } + + /** + * 是否为错误消息 + * + * @return 结果 + */ + public boolean isError() + { + return Objects.equals(Type.ERROR.value, this.get(CODE_TAG)); + } + + /** + * 方便链式调用 + * + * @param key 键 + * @param value 值 + * @return 数据对象 + */ + @Override + public AjaxResult put(String key, Object value) + { + super.put(key, value); + return this; + } +} diff --git a/jshERP-boot/src/main/java/com/jsh/erp/base/BaseController.java b/jshERP-boot/src/main/java/com/jsh/erp/base/BaseController.java new file mode 100644 index 000000000..bf9f01ce3 --- /dev/null +++ b/jshERP-boot/src/main/java/com/jsh/erp/base/BaseController.java @@ -0,0 +1,158 @@ +package com.jsh.erp.base; + +import com.baomidou.mybatisplus.core.toolkit.StringUtils; +import com.github.pagehelper.PageHelper; +import com.github.pagehelper.PageInfo; +import com.jsh.erp.utils.PageUtils; +import com.jsh.erp.utils.ServletUtils; +import com.jsh.erp.utils.SqlUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * web层通用数据处理 + * + * @author ji-sheng-hua + */ +public class BaseController +{ + protected final Logger logger = LoggerFactory.getLogger(this.getClass()); + + /** + * 设置请求分页数据 + */ + protected void startPage() + { + PageUtils.startPage(); + } + + /** + * 设置请求排序数据 + */ + protected void startOrderBy() + { + PageDomain pageDomain = TableSupport.buildPageRequest(); + if (StringUtils.isNotEmpty(pageDomain.getOrderBy())) + { + String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy()); + PageHelper.orderBy(orderBy); + } + } + + /** + * 清理分页的线程变量 + */ + protected void clearPage() + { + PageUtils.clearPage(); + } + + /** + * 获取request + */ + public HttpServletRequest getRequest() + { + return ServletUtils.getRequest(); + } + + /** + * 获取response + */ + public HttpServletResponse getResponse() + { + return ServletUtils.getResponse(); + } + + /** + * 获取session + */ + public HttpSession getSession() + { + return getRequest().getSession(); + } + + /** + * 响应请求分页数据 + */ + @SuppressWarnings({ "rawtypes", "unchecked" }) + protected TableDataInfo getDataTable(List list) + { + Map data = new HashMap<>(); + TableDataInfo rspData = new TableDataInfo(); + rspData.setCode(200); + data.put("rows", list); + data.put("total", new PageInfo(list).getTotal()); + rspData.setData(data); + return rspData; + } + + /** + * 响应返回结果 + * + * @param rows 影响行数 + * @return 操作结果 + */ + protected AjaxResult toAjax(int rows) + { + return rows > 0 ? success() : error(); + } + + /** + * 响应返回结果 + * + * @param result 结果 + * @return 操作结果 + */ + protected AjaxResult toAjax(boolean result) + { + return result ? success() : error(); + } + + /** + * 返回成功 + */ + public AjaxResult success() + { + return AjaxResult.success(); + } + + /** + * 返回失败消息 + */ + public AjaxResult error() + { + return AjaxResult.error(); + } + + /** + * 返回成功消息 + */ + public AjaxResult success(String message) + { + return AjaxResult.success(message); + } + + /** + * 返回成功数据 + */ + public static AjaxResult success(Object data) + { + return AjaxResult.success("操作成功", data); + } + + /** + * 返回失败消息 + */ + public AjaxResult error(String message) + { + return AjaxResult.error(message); + } + +} diff --git a/jshERP-boot/src/main/java/com/jsh/erp/base/PageDomain.java b/jshERP-boot/src/main/java/com/jsh/erp/base/PageDomain.java new file mode 100644 index 000000000..5c8de689f --- /dev/null +++ b/jshERP-boot/src/main/java/com/jsh/erp/base/PageDomain.java @@ -0,0 +1,87 @@ +package com.jsh.erp.base; + +import com.jsh.erp.utils.StringUtil; + +/** + * 分页数据 + * + * @author ji-sheng-hua + */ +public class PageDomain +{ + /** 当前记录起始索引 */ + private Integer currentPage; + + /** 每页显示记录数 */ + private Integer pageSize; + + /** 排序列 */ + private String orderByColumn; + + /** 排序的方向desc或者asc */ + private String isAsc = "asc"; + + /** 分页参数合理化 */ + private Boolean reasonable = true; + + public String getOrderBy() + { + if (StringUtil.isEmpty(orderByColumn)) + { + return ""; + } + return StringUtil.toUnderScoreCase(orderByColumn) + " " + isAsc; + } + + public Integer getCurrentPage() { + return currentPage; + } + + public void setCurrentPage(Integer currentPage) { + this.currentPage = currentPage; + } + + public Integer getPageSize() + { + return pageSize; + } + + public void setPageSize(Integer pageSize) + { + this.pageSize = pageSize; + } + + public String getOrderByColumn() + { + return orderByColumn; + } + + public void setOrderByColumn(String orderByColumn) + { + this.orderByColumn = orderByColumn; + } + + public String getIsAsc() + { + return isAsc; + } + + public void setIsAsc(String isAsc) + { + this.isAsc = isAsc; + } + + public Boolean getReasonable() + { + if (StringUtil.isNull(reasonable)) + { + return Boolean.TRUE; + } + return reasonable; + } + + public void setReasonable(Boolean reasonable) + { + this.reasonable = reasonable; + } +} diff --git a/jshERP-boot/src/main/java/com/jsh/erp/base/TableDataInfo.java b/jshERP-boot/src/main/java/com/jsh/erp/base/TableDataInfo.java new file mode 100644 index 000000000..54de48cbe --- /dev/null +++ b/jshERP-boot/src/main/java/com/jsh/erp/base/TableDataInfo.java @@ -0,0 +1,70 @@ +package com.jsh.erp.base; + +import java.io.Serializable; + +/** + * 表格分页数据对象 + * + * @author ji-sheng-hua + */ +public class TableDataInfo implements Serializable +{ + private static final long serialVersionUID = 1L; + + /** 总记录数 */ + private long total; + + /** 消息状态码 */ + private int code; + + /** 消息状态码 */ + private Object data; + + /** 消息内容 */ + private String msg; + + /** + * 表格数据对象 + */ + public TableDataInfo() + { + } + + public long getTotal() + { + return total; + } + + public void setTotal(long total) + { + this.total = total; + } + + public int getCode() + { + return code; + } + + public void setCode(int code) + { + this.code = code; + } + + public Object getData() { + return data; + } + + public void setData(Object data) { + this.data = data; + } + + public String getMsg() + { + return msg; + } + + public void setMsg(String msg) + { + this.msg = msg; + } +} \ No newline at end of file diff --git a/jshERP-boot/src/main/java/com/jsh/erp/base/TableSupport.java b/jshERP-boot/src/main/java/com/jsh/erp/base/TableSupport.java new file mode 100644 index 000000000..03c986aa2 --- /dev/null +++ b/jshERP-boot/src/main/java/com/jsh/erp/base/TableSupport.java @@ -0,0 +1,56 @@ +package com.jsh.erp.base; + +import com.jsh.erp.utils.Convert; +import com.jsh.erp.utils.ServletUtils; + +/** + * 表格数据处理 + * + * @author ji-sheng-hua + */ +public class TableSupport +{ + /** + * 当前记录起始索引 + */ + public static final String CURRENT_PAGE = "currentPage"; + + /** + * 每页显示记录数 + */ + public static final String PAGE_SIZE = "pageSize"; + + /** + * 排序列 + */ + public static final String ORDER_BY_COLUMN = "orderByColumn"; + + /** + * 排序的方向 "desc" 或者 "asc". + */ + public static final String IS_ASC = "isAsc"; + + /** + * 分页参数合理化 + */ + public static final String REASONABLE = "reasonable"; + + /** + * 封装分页对象 + */ + public static PageDomain getPageDomain() + { + PageDomain pageDomain = new PageDomain(); + pageDomain.setCurrentPage(Convert.toInt(ServletUtils.getParameter(CURRENT_PAGE), 1)); + pageDomain.setPageSize(Convert.toInt(ServletUtils.getParameter(PAGE_SIZE), 10)); + pageDomain.setOrderByColumn(ServletUtils.getParameter(ORDER_BY_COLUMN)); + pageDomain.setIsAsc(ServletUtils.getParameter(IS_ASC)); + pageDomain.setReasonable(ServletUtils.getParameterToBool(REASONABLE)); + return pageDomain; + } + + public static PageDomain buildPageRequest() + { + return getPageDomain(); + } +} diff --git a/jshERP-boot/src/main/java/com/jsh/erp/controller/TenantController.java b/jshERP-boot/src/main/java/com/jsh/erp/controller/TenantController.java index dd0d8aeb7..9f2dfccae 100644 --- a/jshERP-boot/src/main/java/com/jsh/erp/controller/TenantController.java +++ b/jshERP-boot/src/main/java/com/jsh/erp/controller/TenantController.java @@ -3,6 +3,8 @@ package com.jsh.erp.controller; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.jsh.erp.base.BaseController; +import com.jsh.erp.base.TableDataInfo; import com.jsh.erp.constants.BusinessConstants; import com.jsh.erp.datasource.entities.Tenant; import com.jsh.erp.datasource.entities.TenantEx; @@ -18,6 +20,7 @@ import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; import static com.jsh.erp.utils.ResponseJsonUtil.returnJson; @@ -29,7 +32,7 @@ import static com.jsh.erp.utils.ResponseJsonUtil.returnStr; @RestController @RequestMapping(value = "/tenant") @Api(tags = {"租户管理"}) -public class TenantController { +public class TenantController extends BaseController { @Resource private TenantService tenantService; @@ -50,31 +53,14 @@ public class TenantController { @GetMapping(value = "/list") @ApiOperation(value = "获取信息列表") - public String getList(@RequestParam(value = Constants.PAGE_SIZE, required = false) Integer pageSize, - @RequestParam(value = Constants.CURRENT_PAGE, required = false) Integer currentPage, - @RequestParam(value = Constants.SEARCH, required = false) String search, + public TableDataInfo getList(@RequestParam(value = Constants.SEARCH, required = false) String search, HttpServletRequest request)throws Exception { - Map objectMap = new HashMap<>(); - if (pageSize != null && pageSize <= 0) { - pageSize = 10; - } String loginName = StringUtil.getInfo(search, "loginName"); String type = StringUtil.getInfo(search, "type"); String enabled = StringUtil.getInfo(search, "enabled"); String remark = StringUtil.getInfo(search, "remark"); - IPage page = new Page<>(); - page.setCurrent(currentPage); - page.setSize(pageSize); - IPage list = tenantService.select(page, loginName, type, enabled, remark); - if (list != null) { - objectMap.put("rows", list.getRecords()); - objectMap.put("total", list.getTotal()); - return returnJson(objectMap, ErpInfo.OK.name, ErpInfo.OK.code); - } else { - objectMap.put("rows", new ArrayList()); - objectMap.put("total", BusinessConstants.DEFAULT_LIST_NULL_NUMBER); - return returnJson(objectMap, "查找不到数据", ErpInfo.OK.code); - } + List list = tenantService.select(loginName, type, enabled, remark); + return getDataTable(list); } @PostMapping(value = "/add") diff --git a/jshERP-boot/src/main/java/com/jsh/erp/datasource/mappers/TenantMapperEx.java b/jshERP-boot/src/main/java/com/jsh/erp/datasource/mappers/TenantMapperEx.java index 5d025df14..430203c9d 100644 --- a/jshERP-boot/src/main/java/com/jsh/erp/datasource/mappers/TenantMapperEx.java +++ b/jshERP-boot/src/main/java/com/jsh/erp/datasource/mappers/TenantMapperEx.java @@ -8,8 +8,7 @@ import java.util.List; public interface TenantMapperEx { - IPage selectByConditionTenant( - IPage page, + List selectByConditionTenant( @Param("loginName") String loginName, @Param("type") String type, @Param("enabled") String enabled, diff --git a/jshERP-boot/src/main/java/com/jsh/erp/exception/UtilException.java b/jshERP-boot/src/main/java/com/jsh/erp/exception/UtilException.java new file mode 100644 index 000000000..63a4cd0bd --- /dev/null +++ b/jshERP-boot/src/main/java/com/jsh/erp/exception/UtilException.java @@ -0,0 +1,26 @@ +package com.jsh.erp.exception; + +/** + * 工具类异常 + * + * @author ji-sheng-hua + */ +public class UtilException extends RuntimeException +{ + private static final long serialVersionUID = 8247610319171014183L; + + public UtilException(Throwable e) + { + super(e.getMessage(), e); + } + + public UtilException(String message) + { + super(message); + } + + public UtilException(String message, Throwable throwable) + { + super(message, throwable); + } +} diff --git a/jshERP-boot/src/main/java/com/jsh/erp/service/tenant/TenantService.java b/jshERP-boot/src/main/java/com/jsh/erp/service/tenant/TenantService.java index efc97d663..fa2c1b388 100644 --- a/jshERP-boot/src/main/java/com/jsh/erp/service/tenant/TenantService.java +++ b/jshERP-boot/src/main/java/com/jsh/erp/service/tenant/TenantService.java @@ -14,6 +14,7 @@ import com.jsh.erp.datasource.mappers.UserMapperEx; import com.jsh.erp.exception.JshException; import com.jsh.erp.service.log.LogService; import com.jsh.erp.service.user.UserService; +import com.jsh.erp.utils.PageUtils; import com.jsh.erp.utils.StringUtil; import com.jsh.erp.utils.Tools; import org.slf4j.Logger; @@ -75,22 +76,23 @@ public class TenantService { return list; } - public IPage select(IPage page, String loginName, String type, String enabled, String remark)throws Exception { - IPage iPage = null; + public List select(String loginName, String type, String enabled, String remark)throws Exception { + List list = null; try{ if(BusinessConstants.DEFAULT_MANAGER.equals(userService.getCurrentUser().getLoginName())) { - iPage = tenantMapperEx.selectByConditionTenant(page, loginName, type, enabled, remark); - if (null != iPage.getRecords()) { - for (TenantEx tenantEx : iPage.getRecords()) { + PageUtils.startPage(); + list = tenantMapperEx.selectByConditionTenant(loginName, type, enabled, remark); + if (null != list) { + for (TenantEx tenantEx : list) { tenantEx.setCreateTimeStr(Tools.getCenternTime(tenantEx.getCreateTime())); tenantEx.setExpireTimeStr(Tools.getCenternTime(tenantEx.getExpireTime())); } } } - }catch(Exception e){ + } catch(Exception e){ JshException.readFail(logger, e); } - return iPage; + return list; } @Transactional(value = "transactionManager", rollbackFor = Exception.class) diff --git a/jshERP-boot/src/main/java/com/jsh/erp/utils/Convert.java b/jshERP-boot/src/main/java/com/jsh/erp/utils/Convert.java new file mode 100644 index 000000000..cebb6c2a3 --- /dev/null +++ b/jshERP-boot/src/main/java/com/jsh/erp/utils/Convert.java @@ -0,0 +1,536 @@ +package com.jsh.erp.utils; + +import com.jsh.erp.utils.StringUtil; + +import java.math.BigDecimal; +import java.text.NumberFormat; + +/** + * 类型转换器 + * + * @author ji-sheng-hua + */ +public class Convert +{ + /** + * 转换为字符串
+ * 如果给定的值为null,或者转换失败,返回默认值
+ * 转换失败不会报错 + * + * @param value 被转换的值 + * @param defaultValue 转换错误时的默认值 + * @return 结果 + */ + public static String toStr(Object value, String defaultValue) + { + if (null == value) + { + return defaultValue; + } + if (value instanceof String) + { + return (String) value; + } + return value.toString(); + } + + /** + * 转换为字符串
+ * 如果给定的值为null,或者转换失败,返回默认值null
+ * 转换失败不会报错 + * + * @param value 被转换的值 + * @return 结果 + */ + public static String toStr(Object value) + { + return toStr(value, null); + } + + /** + * 转换为字符
+ * 如果给定的值为null,或者转换失败,返回默认值
+ * 转换失败不会报错 + * + * @param value 被转换的值 + * @param defaultValue 转换错误时的默认值 + * @return 结果 + */ + public static Character toChar(Object value, Character defaultValue) + { + if (null == value) + { + return defaultValue; + } + if (value instanceof Character) + { + return (Character) value; + } + + final String valueStr = toStr(value, null); + return StringUtil.isEmpty(valueStr) ? defaultValue : valueStr.charAt(0); + } + + /** + * 转换为字符
+ * 如果给定的值为null,或者转换失败,返回默认值null
+ * 转换失败不会报错 + * + * @param value 被转换的值 + * @return 结果 + */ + public static Character toChar(Object value) + { + return toChar(value, null); + } + + /** + * 转换为byte
+ * 如果给定的值为null,或者转换失败,返回默认值
+ * 转换失败不会报错 + * + * @param value 被转换的值 + * @param defaultValue 转换错误时的默认值 + * @return 结果 + */ + public static Byte toByte(Object value, Byte defaultValue) + { + if (value == null) + { + return defaultValue; + } + if (value instanceof Byte) + { + return (Byte) value; + } + if (value instanceof Number) + { + return ((Number) value).byteValue(); + } + final String valueStr = toStr(value, null); + if (StringUtil.isEmpty(valueStr)) + { + return defaultValue; + } + try + { + return Byte.parseByte(valueStr); + } + catch (Exception e) + { + return defaultValue; + } + } + + /** + * 转换为byte
+ * 如果给定的值为null,或者转换失败,返回默认值null
+ * 转换失败不会报错 + * + * @param value 被转换的值 + * @return 结果 + */ + public static Byte toByte(Object value) + { + return toByte(value, null); + } + + /** + * 转换为Short
+ * 如果给定的值为null,或者转换失败,返回默认值
+ * 转换失败不会报错 + * + * @param value 被转换的值 + * @param defaultValue 转换错误时的默认值 + * @return 结果 + */ + public static Short toShort(Object value, Short defaultValue) + { + if (value == null) + { + return defaultValue; + } + if (value instanceof Short) + { + return (Short) value; + } + if (value instanceof Number) + { + return ((Number) value).shortValue(); + } + final String valueStr = toStr(value, null); + if (StringUtil.isEmpty(valueStr)) + { + return defaultValue; + } + try + { + return Short.parseShort(valueStr.trim()); + } + catch (Exception e) + { + return defaultValue; + } + } + + /** + * 转换为Short
+ * 如果给定的值为null,或者转换失败,返回默认值null
+ * 转换失败不会报错 + * + * @param value 被转换的值 + * @return 结果 + */ + public static Short toShort(Object value) + { + return toShort(value, null); + } + + /** + * 转换为Number
+ * 如果给定的值为空,或者转换失败,返回默认值
+ * 转换失败不会报错 + * + * @param value 被转换的值 + * @param defaultValue 转换错误时的默认值 + * @return 结果 + */ + public static Number toNumber(Object value, Number defaultValue) + { + if (value == null) + { + return defaultValue; + } + if (value instanceof Number) + { + return (Number) value; + } + final String valueStr = toStr(value, null); + if (StringUtil.isEmpty(valueStr)) + { + return defaultValue; + } + try + { + return NumberFormat.getInstance().parse(valueStr); + } + catch (Exception e) + { + return defaultValue; + } + } + + /** + * 转换为Number
+ * 如果给定的值为空,或者转换失败,返回默认值null
+ * 转换失败不会报错 + * + * @param value 被转换的值 + * @return 结果 + */ + public static Number toNumber(Object value) + { + return toNumber(value, null); + } + + /** + * 转换为int
+ * 如果给定的值为空,或者转换失败,返回默认值
+ * 转换失败不会报错 + * + * @param value 被转换的值 + * @param defaultValue 转换错误时的默认值 + * @return 结果 + */ + public static Integer toInt(Object value, Integer defaultValue) + { + if (value == null) + { + return defaultValue; + } + if (value instanceof Integer) + { + return (Integer) value; + } + if (value instanceof Number) + { + return ((Number) value).intValue(); + } + final String valueStr = toStr(value, null); + if (StringUtil.isEmpty(valueStr)) + { + return defaultValue; + } + try + { + return Integer.parseInt(valueStr.trim()); + } + catch (Exception e) + { + return defaultValue; + } + } + + /** + * 转换为int
+ * 如果给定的值为null,或者转换失败,返回默认值null
+ * 转换失败不会报错 + * + * @param value 被转换的值 + * @return 结果 + */ + public static Integer toInt(Object value) + { + return toInt(value, null); + } + + /** + * 转换为Integer数组
+ * + * @param str 被转换的值 + * @return 结果 + */ + public static Integer[] toIntArray(String str) + { + return toIntArray(",", str); + } + + /** + * 转换为Long数组
+ * + * @param str 被转换的值 + * @return 结果 + */ + public static Long[] toLongArray(String str) + { + return toLongArray(",", str); + } + + /** + * 转换为Integer数组
+ * + * @param split 分隔符 + * @param split 被转换的值 + * @return 结果 + */ + public static Integer[] toIntArray(String split, String str) + { + if (StringUtil.isEmpty(str)) + { + return new Integer[] {}; + } + String[] arr = str.split(split); + final Integer[] ints = new Integer[arr.length]; + for (int i = 0; i < arr.length; i++) + { + final Integer v = toInt(arr[i], 0); + ints[i] = v; + } + return ints; + } + + /** + * 转换为Long数组
+ * + * @param split 分隔符 + * @param str 被转换的值 + * @return 结果 + */ + public static Long[] toLongArray(String split, String str) + { + if (StringUtil.isEmpty(str)) + { + return new Long[] {}; + } + String[] arr = str.split(split); + final Long[] longs = new Long[arr.length]; + for (int i = 0; i < arr.length; i++) + { + final Long v = toLong(arr[i], null); + longs[i] = v; + } + return longs; + } + + /** + * 转换为String数组
+ * + * @param str 被转换的值 + * @return 结果 + */ + public static String[] toStrArray(String str) + { + return toStrArray(",", str); + } + + /** + * 转换为String数组
+ * + * @param split 分隔符 + * @param split 被转换的值 + * @return 结果 + */ + public static String[] toStrArray(String split, String str) + { + return str.split(split); + } + + /** + * 转换为long
+ * 如果给定的值为空,或者转换失败,返回默认值
+ * 转换失败不会报错 + * + * @param value 被转换的值 + * @param defaultValue 转换错误时的默认值 + * @return 结果 + */ + public static Long toLong(Object value, Long defaultValue) + { + if (value == null) + { + return defaultValue; + } + if (value instanceof Long) + { + return (Long) value; + } + if (value instanceof Number) + { + return ((Number) value).longValue(); + } + final String valueStr = toStr(value, null); + if (StringUtil.isEmpty(valueStr)) + { + return defaultValue; + } + try + { + // 支持科学计数法 + return new BigDecimal(valueStr.trim()).longValue(); + } + catch (Exception e) + { + return defaultValue; + } + } + + /** + * 转换为long
+ * 如果给定的值为null,或者转换失败,返回默认值null
+ * 转换失败不会报错 + * + * @param value 被转换的值 + * @return 结果 + */ + public static Long toLong(Object value) + { + return toLong(value, null); + } + + /** + * 转换为double
+ * 如果给定的值为空,或者转换失败,返回默认值
+ * 转换失败不会报错 + * + * @param value 被转换的值 + * @param defaultValue 转换错误时的默认值 + * @return 结果 + */ + public static Double toDouble(Object value, Double defaultValue) + { + if (value == null) + { + return defaultValue; + } + if (value instanceof Double) + { + return (Double) value; + } + if (value instanceof Number) + { + return ((Number) value).doubleValue(); + } + final String valueStr = toStr(value, null); + if (StringUtil.isEmpty(valueStr)) + { + return defaultValue; + } + try + { + // 支持科学计数法 + return new BigDecimal(valueStr.trim()).doubleValue(); + } + catch (Exception e) + { + return defaultValue; + } + } + + /** + * 转换为double
+ * 如果给定的值为空,或者转换失败,返回默认值null
+ * 转换失败不会报错 + * + * @param value 被转换的值 + * @return 结果 + */ + public static Double toDouble(Object value) + { + return toDouble(value, null); + } + + /** + * 转换为boolean
+ * String支持的值为:true、false、yes、ok、no,1,0 如果给定的值为空,或者转换失败,返回默认值
+ * 转换失败不会报错 + * + * @param value 被转换的值 + * @param defaultValue 转换错误时的默认值 + * @return 结果 + */ + public static Boolean toBool(Object value, Boolean defaultValue) + { + if (value == null) + { + return defaultValue; + } + if (value instanceof Boolean) + { + return (Boolean) value; + } + String valueStr = toStr(value, null); + if (StringUtil.isEmpty(valueStr)) + { + return defaultValue; + } + valueStr = valueStr.trim().toLowerCase(); + switch (valueStr) + { + case "true": + case "yes": + case "ok": + case "1": + return true; + case "false": + case "no": + case "0": + return false; + default: + return defaultValue; + } + } + + /** + * 转换为boolean
+ * 如果给定的值为空,或者转换失败,返回默认值null
+ * 转换失败不会报错 + * + * @param value 被转换的值 + * @return 结果 + */ + public static Boolean toBool(Object value) + { + return toBool(value, null); + } + +} diff --git a/jshERP-boot/src/main/java/com/jsh/erp/utils/PageUtils.java b/jshERP-boot/src/main/java/com/jsh/erp/utils/PageUtils.java new file mode 100644 index 000000000..896646f28 --- /dev/null +++ b/jshERP-boot/src/main/java/com/jsh/erp/utils/PageUtils.java @@ -0,0 +1,34 @@ +package com.jsh.erp.utils; + +import com.github.pagehelper.PageHelper; +import com.jsh.erp.base.PageDomain; +import com.jsh.erp.base.TableSupport; + +/** + * 分页工具类 + * + * @author ji-sheng-hua + */ +public class PageUtils extends PageHelper +{ + /** + * 设置请求分页数据 + */ + public static void startPage() + { + PageDomain pageDomain = TableSupport.buildPageRequest(); + Integer currentPage = pageDomain.getCurrentPage(); + Integer pageSize = pageDomain.getPageSize(); + String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy()); + Boolean reasonable = pageDomain.getReasonable(); + PageHelper.startPage(currentPage, pageSize, orderBy).setReasonable(reasonable); + } + + /** + * 清理分页的线程变量 + */ + public static void clearPage() + { + PageHelper.clearPage(); + } +} diff --git a/jshERP-boot/src/main/java/com/jsh/erp/utils/ServletUtils.java b/jshERP-boot/src/main/java/com/jsh/erp/utils/ServletUtils.java new file mode 100644 index 000000000..bf7a02150 --- /dev/null +++ b/jshERP-boot/src/main/java/com/jsh/erp/utils/ServletUtils.java @@ -0,0 +1,123 @@ +package com.jsh.erp.utils; + +import org.springframework.web.context.request.RequestAttributes; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; +import java.io.IOException; + +/** + * 客户端工具类 + * + * @author ji-sheng-hua + */ +public class ServletUtils +{ + /** + * 定义移动端请求的所有可能类型 + */ + private final static String[] agent = { "Android", "iPhone", "iPod", "iPad", "Windows Phone", "MQQBrowser" }; + + /** + * 获取String参数 + */ + public static String getParameter(String name) + { + return getRequest().getParameter(name); + } + + /** + * 获取String参数 + */ + public static String getParameter(String name, String defaultValue) + { + return Convert.toStr(getRequest().getParameter(name), defaultValue); + } + + /** + * 获取Integer参数 + */ + public static Integer getParameterToInt(String name) + { + return Convert.toInt(getRequest().getParameter(name)); + } + + /** + * 获取Integer参数 + */ + public static Integer getParameterToInt(String name, Integer defaultValue) + { + return Convert.toInt(getRequest().getParameter(name), defaultValue); + } + + /** + * 获取Boolean参数 + */ + public static Boolean getParameterToBool(String name) + { + return Convert.toBool(getRequest().getParameter(name)); + } + + /** + * 获取Boolean参数 + */ + public static Boolean getParameterToBool(String name, Boolean defaultValue) + { + return Convert.toBool(getRequest().getParameter(name), defaultValue); + } + + /** + * 获取request + */ + public static HttpServletRequest getRequest() + { + return getRequestAttributes().getRequest(); + } + + /** + * 获取response + */ + public static HttpServletResponse getResponse() + { + return getRequestAttributes().getResponse(); + } + + /** + * 获取session + */ + public static HttpSession getSession() + { + return getRequest().getSession(); + } + + public static ServletRequestAttributes getRequestAttributes() + { + RequestAttributes attributes = RequestContextHolder.getRequestAttributes(); + return (ServletRequestAttributes) attributes; + } + + /** + * 将字符串渲染到客户端 + * + * @param response 渲染对象 + * @param string 待渲染的字符串 + * @return null + */ + public static String renderString(HttpServletResponse response, String string) + { + try + { + response.setContentType("application/json"); + response.setCharacterEncoding("utf-8"); + response.getWriter().print(string); + } + catch (IOException e) + { + e.printStackTrace(); + } + return null; + } +} diff --git a/jshERP-boot/src/main/java/com/jsh/erp/utils/SqlUtil.java b/jshERP-boot/src/main/java/com/jsh/erp/utils/SqlUtil.java new file mode 100644 index 000000000..6de75a157 --- /dev/null +++ b/jshERP-boot/src/main/java/com/jsh/erp/utils/SqlUtil.java @@ -0,0 +1,50 @@ +package com.jsh.erp.utils; + +import com.jsh.erp.exception.UtilException; + +/** + * sql操作工具类 + * + * @author ji-sheng-hua + */ +public class SqlUtil +{ + /** + * 定义常用的 sql关键字 + */ + public static String SQL_REGEX = "and |extractvalue|updatexml|exec |insert |select |delete |update |drop |count |chr |mid |master |truncate |char |declare |or |+|user()"; + + /** + * 仅支持字母、数字、下划线、空格、逗号、小数点(支持多个字段排序) + */ + public static String SQL_PATTERN = "[a-zA-Z0-9_\\ \\,\\.]+"; + + /** + * 限制orderBy最大长度 + */ + private static final int ORDER_BY_MAX_LENGTH = 500; + + /** + * 检查字符,防止注入绕过 + */ + public static String escapeOrderBySql(String value) + { + if (StringUtil.isNotEmpty(value) && !isValidOrderBySql(value)) + { + throw new UtilException("参数不符合规范,不能进行查询"); + } + if (StringUtil.length(value) > ORDER_BY_MAX_LENGTH) + { + throw new UtilException("参数已超过最大限制,不能进行查询"); + } + return value; + } + + /** + * 验证 order by 语法是否符合规范 + */ + public static boolean isValidOrderBySql(String value) + { + return value.matches(SQL_PATTERN); + } +} diff --git a/jshERP-boot/src/main/java/com/jsh/erp/utils/StringUtil.java b/jshERP-boot/src/main/java/com/jsh/erp/utils/StringUtil.java index d935d8bde..d54fd0b07 100644 --- a/jshERP-boot/src/main/java/com/jsh/erp/utils/StringUtil.java +++ b/jshERP-boot/src/main/java/com/jsh/erp/utils/StringUtil.java @@ -47,6 +47,81 @@ public class StringUtil { } } + public static int length(CharSequence cs) { + return cs == null ? 0 : cs.length(); + } + + /** + * 驼峰转下划线命名 + */ + public static String toUnderScoreCase(String str) + { + if (str == null) + { + return null; + } + StringBuilder sb = new StringBuilder(); + // 前置字符是否大写 + boolean preCharIsUpperCase = true; + // 当前字符是否大写 + boolean curreCharIsUpperCase = true; + // 下一字符是否大写 + boolean nexteCharIsUpperCase = true; + for (int i = 0; i < str.length(); i++) + { + char c = str.charAt(i); + if (i > 0) + { + preCharIsUpperCase = Character.isUpperCase(str.charAt(i - 1)); + } + else + { + preCharIsUpperCase = false; + } + + curreCharIsUpperCase = Character.isUpperCase(c); + + if (i < (str.length() - 1)) + { + nexteCharIsUpperCase = Character.isUpperCase(str.charAt(i + 1)); + } + + if (preCharIsUpperCase && curreCharIsUpperCase && !nexteCharIsUpperCase) + { + sb.append(SEPARATOR); + } + else if ((i != 0 && !preCharIsUpperCase) && curreCharIsUpperCase) + { + sb.append(SEPARATOR); + } + sb.append(Character.toLowerCase(c)); + } + + return sb.toString(); + } + + /** + * * 判断一个对象是否为空 + * + * @param object Object + * @return true:为空 false:非空 + */ + public static boolean isNull(Object object) + { + return object == null; + } + + /** + * * 判断一个对象是否非空 + * + * @param object Object + * @return true:非空 false:空 + */ + public static boolean isNotNull(Object object) + { + return !isNull(object); + } + public static boolean isEmpty(String str) { return str == null || "".equals(str.trim()); }