Commit 84af5316d6049587b225d43bae1c5ea040caa05c

Authored by youxiw2000
1 parent 111dae4c

111

Too many changes to show.

To preserve performance only 17 of 139 files are displayed.

trash-admin/pom.xml
1   -<?xml version="1.0" encoding="UTF-8"?>
2   -<project xmlns="http://maven.apache.org/POM/4.0.0"
3   - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4   - xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5   - <parent>
6   - <artifactId>trash</artifactId>
7   - <groupId>com.trash</groupId>
8   - <version>3.2.0</version>
9   - </parent>
10   - <modelVersion>4.0.0</modelVersion>
11   - <packaging>jar</packaging>
12   - <artifactId>trash-admin</artifactId>
13   -
14   - <description>
15   - web服务入口
16   - </description>
17   -
18   - <dependencies>
19   -
20   - <!-- spring-boot-devtools -->
21   - <dependency>
22   - <groupId>org.springframework.boot</groupId>
23   - <artifactId>spring-boot-devtools</artifactId>
24   - <optional>true</optional> <!-- 表示依赖不会传递 -->
25   - </dependency>
26   -
27   - <!-- swagger2-->
28   - <dependency>
29   - <groupId>io.springfox</groupId>
30   - <artifactId>springfox-swagger2</artifactId>
31   - </dependency>
32   -
33   - <!--防止进入swagger页面报类型转换错误,排除2.9.2中的引用,手动增加1.5.21版本-->
34   - <dependency>
35   - <groupId>io.swagger</groupId>
36   - <artifactId>swagger-annotations</artifactId>
37   - <version>1.5.21</version>
38   - </dependency>
39   -
40   - <dependency>
41   - <groupId>io.swagger</groupId>
42   - <artifactId>swagger-models</artifactId>
43   - <version>1.5.21</version>
44   - </dependency>
45   -
46   - <!-- swagger2-UI-->
47   - <dependency>
48   - <groupId>io.springfox</groupId>
49   - <artifactId>springfox-swagger-ui</artifactId>
50   - </dependency>
51   -
52   - <!-- Mysql驱动包 -->
53   - <dependency>
54   - <groupId>mysql</groupId>
55   - <artifactId>mysql-connector-java</artifactId>
56   - </dependency>
57   -
58   - <!-- 核心模块-->
59   - <dependency>
60   - <groupId>com.trash</groupId>
61   - <artifactId>trash-framework</artifactId>
62   - </dependency>
63   -
64   - <!-- 定时任务-->
65   - <dependency>
66   - <groupId>com.trash</groupId>
67   - <artifactId>trash-quartz</artifactId>
68   - </dependency>
69   -
70   - <!-- 代码生成-->
71   - <dependency>
72   - <groupId>com.trash</groupId>
73   - <artifactId>trash-generator</artifactId>
74   - </dependency>
75   - <!-- activiti模块-->
76   - <dependency>
77   - <groupId>com.trash</groupId>
78   - <artifactId>trash-activiti</artifactId>
79   - </dependency>
80   - <!-- 工作流模块-->
81   - <dependency>
82   - <groupId>com.trash</groupId>
83   - <artifactId>trash-workflow</artifactId>
84   - </dependency>
85   - <dependency>
86   - <groupId>com.trash</groupId>
87   - <artifactId>trash-daily</artifactId>
88   - </dependency>
89   - <dependency>
90   - <groupId>com.trash</groupId>
91   - <artifactId>trash-garbage</artifactId>
92   - </dependency>
93   - </dependencies>
94   -
95   - <build>
96   - <plugins>
97   - <plugin>
98   - <groupId>org.springframework.boot</groupId>
99   - <artifactId>spring-boot-maven-plugin</artifactId>
100   - <version>2.1.1.RELEASE</version>
101   - <configuration>
102   - <fork>true</fork> <!-- 如果没有该配置,devtools不会生效 -->
103   - </configuration>
104   - <executions>
105   - <execution>
106   - <goals>
107   - <goal>repackage</goal>
108   - </goals>
109   - </execution>
110   - </executions>
111   - </plugin>
112   - <plugin>
113   - <groupId>org.apache.maven.plugins</groupId>
114   - <artifactId>maven-war-plugin</artifactId>
115   - <version>3.1.0</version>
116   - <configuration>
117   - <failOnMissingWebXml>false</failOnMissingWebXml>
118   - <warName>${project.artifactId}</warName>
119   - </configuration>
120   - </plugin>
121   - </plugins>
122   - <finalName>${project.artifactId}</finalName>
123   - </build>
124   -
  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +<project xmlns="http://maven.apache.org/POM/4.0.0"
  3 + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5 + <parent>
  6 + <artifactId>trash</artifactId>
  7 + <groupId>com.trash</groupId>
  8 + <version>3.2.0</version>
  9 + </parent>
  10 + <modelVersion>4.0.0</modelVersion>
  11 + <packaging>jar</packaging>
  12 + <artifactId>trash-admin</artifactId>
  13 +
  14 + <description>
  15 + web服务入口
  16 + </description>
  17 +
  18 + <dependencies>
  19 +
  20 + <!-- spring-boot-devtools -->
  21 + <dependency>
  22 + <groupId>org.springframework.boot</groupId>
  23 + <artifactId>spring-boot-devtools</artifactId>
  24 + <optional>true</optional> <!-- 表示依赖不会传递 -->
  25 + </dependency>
  26 +
  27 + <!-- swagger2-->
  28 + <dependency>
  29 + <groupId>io.springfox</groupId>
  30 + <artifactId>springfox-swagger2</artifactId>
  31 + </dependency>
  32 +
  33 + <!--防止进入swagger页面报类型转换错误,排除2.9.2中的引用,手动增加1.5.21版本-->
  34 + <dependency>
  35 + <groupId>io.swagger</groupId>
  36 + <artifactId>swagger-annotations</artifactId>
  37 + <version>1.5.21</version>
  38 + </dependency>
  39 +
  40 + <dependency>
  41 + <groupId>io.swagger</groupId>
  42 + <artifactId>swagger-models</artifactId>
  43 + <version>1.5.21</version>
  44 + </dependency>
  45 +
  46 + <!-- swagger2-UI-->
  47 + <dependency>
  48 + <groupId>io.springfox</groupId>
  49 + <artifactId>springfox-swagger-ui</artifactId>
  50 + </dependency>
  51 +
  52 + <!-- Mysql驱动包 -->
  53 + <dependency>
  54 + <groupId>mysql</groupId>
  55 + <artifactId>mysql-connector-java</artifactId>
  56 + </dependency>
  57 +
  58 + <!-- 核心模块-->
  59 + <dependency>
  60 + <groupId>com.trash</groupId>
  61 + <artifactId>trash-framework</artifactId>
  62 + </dependency>
  63 +
  64 + <!-- 定时任务-->
  65 + <dependency>
  66 + <groupId>com.trash</groupId>
  67 + <artifactId>trash-quartz</artifactId>
  68 + </dependency>
  69 +
  70 + <!-- 代码生成-->
  71 + <dependency>
  72 + <groupId>com.trash</groupId>
  73 + <artifactId>trash-generator</artifactId>
  74 + </dependency>
  75 + <!-- activiti模块-->
  76 + <dependency>
  77 + <groupId>com.trash</groupId>
  78 + <artifactId>trash-activiti</artifactId>
  79 + </dependency>
  80 + <!-- 工作流模块-->
  81 + <dependency>
  82 + <groupId>com.trash</groupId>
  83 + <artifactId>trash-workflow</artifactId>
  84 + </dependency>
  85 + <dependency>
  86 + <groupId>com.trash</groupId>
  87 + <artifactId>trash-daily</artifactId>
  88 + </dependency>
  89 + <dependency>
  90 + <groupId>com.trash</groupId>
  91 + <artifactId>trash-garbage</artifactId>
  92 + </dependency>
  93 + </dependencies>
  94 +
  95 + <build>
  96 + <plugins>
  97 + <plugin>
  98 + <groupId>org.springframework.boot</groupId>
  99 + <artifactId>spring-boot-maven-plugin</artifactId>
  100 + <version>2.1.1.RELEASE</version>
  101 + <configuration>
  102 + <fork>true</fork> <!-- 如果没有该配置,devtools不会生效 -->
  103 + </configuration>
  104 + <executions>
  105 + <execution>
  106 + <goals>
  107 + <goal>repackage</goal>
  108 + </goals>
  109 + </execution>
  110 + </executions>
  111 + </plugin>
  112 + <plugin>
  113 + <groupId>org.apache.maven.plugins</groupId>
  114 + <artifactId>maven-war-plugin</artifactId>
  115 + <version>3.1.0</version>
  116 + <configuration>
  117 + <failOnMissingWebXml>false</failOnMissingWebXml>
  118 + <warName>${project.artifactId}</warName>
  119 + </configuration>
  120 + </plugin>
  121 + </plugins>
  122 + <finalName>${project.artifactId}</finalName>
  123 + </build>
  124 +
125 125 </project>
126 126 \ No newline at end of file
... ...
trash-admin/src/main/java/com/trash/web/controller/system/SysDeptController.java
1   -package com.trash.web.controller.system;
2   -
3   -import java.util.Iterator;
4   -import java.util.List;
5   -import org.apache.commons.lang3.ArrayUtils;
6   -import org.springframework.beans.factory.annotation.Autowired;
7   -import org.springframework.security.access.prepost.PreAuthorize;
8   -import org.springframework.validation.annotation.Validated;
9   -import org.springframework.web.bind.annotation.DeleteMapping;
10   -import org.springframework.web.bind.annotation.GetMapping;
11   -import org.springframework.web.bind.annotation.PathVariable;
12   -import org.springframework.web.bind.annotation.PostMapping;
13   -import org.springframework.web.bind.annotation.PutMapping;
14   -import org.springframework.web.bind.annotation.RequestBody;
15   -import org.springframework.web.bind.annotation.RequestMapping;
16   -import org.springframework.web.bind.annotation.RestController;
17   -
18   -import com.trash.common.annotation.Log;
19   -import com.trash.common.constant.UserConstants;
20   -import com.trash.common.core.controller.BaseController;
21   -import com.trash.common.core.domain.AjaxResult;
22   -import com.trash.common.core.domain.entity.SysDept;
23   -import com.trash.common.enums.BusinessType;
24   -import com.trash.common.utils.SecurityUtils;
25   -import com.trash.common.utils.StringUtils;
26   -import com.trash.system.service.ISysDeptService;
27   -
28   -/**
29   - * 部门信息
30   - *
31   - * @author trash
32   - */
33   -@RestController
34   -@RequestMapping("/system/dept")
35   -public class SysDeptController extends BaseController
36   -{
37   - @Autowired
38   - private ISysDeptService deptService;
39   -
40   - /**
41   - * 获取部门列表
42   - */
43   - @PreAuthorize("@ss.hasPermi('system:dept:list')")
44   - @GetMapping("/list")
45   - public AjaxResult list(SysDept dept)
46   - {
47   - List<SysDept> depts = deptService.selectDeptList(dept);
48   - return AjaxResult.success(depts);
49   - }
50   -
51   - /**
52   - * 查询部门列表(排除节点)
53   - */
54   - @PreAuthorize("@ss.hasPermi('system:dept:list')")
55   - @GetMapping("/list/exclude/{deptId}")
56   - public AjaxResult excludeChild(@PathVariable(value = "deptId", required = false) Long deptId)
57   - {
58   - List<SysDept> depts = deptService.selectDeptList(new SysDept());
59   - Iterator<SysDept> it = depts.iterator();
60   - while (it.hasNext())
61   - {
62   - SysDept d = it.next();
63   - if (d.getDeptId().intValue() == deptId
64   - || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), deptId + ""))
65   - {
66   - it.remove();
67   - }
68   - }
69   - return AjaxResult.success(depts);
70   - }
71   -
72   - /**
73   - * 根据部门编号获取详细信息
74   - */
75   - @PreAuthorize("@ss.hasPermi('system:dept:query')")
76   - @GetMapping(value = "/{deptId}")
77   - public AjaxResult getInfo(@PathVariable Long deptId)
78   - {
79   - return AjaxResult.success(deptService.selectDeptById(deptId));
80   - }
81   -
82   - /**
83   - * 获取部门下拉树列表
84   - */
85   - @GetMapping("/treeselect")
86   - public AjaxResult treeselect(SysDept dept)
87   - {
88   - List<SysDept> depts = deptService.selectDeptList(dept);
89   - return AjaxResult.success(deptService.buildDeptTreeSelect(depts));
90   - }
91   -
92   - /**
93   - * 加载对应角色部门列表树
94   - */
95   - @GetMapping(value = "/roleDeptTreeselect/{roleId}")
96   - public AjaxResult roleDeptTreeselect(@PathVariable("roleId") Long roleId)
97   - {
98   - List<SysDept> depts = deptService.selectDeptList(new SysDept());
99   - AjaxResult ajax = AjaxResult.success();
100   - ajax.put("checkedKeys", deptService.selectDeptListByRoleId(roleId));
101   - ajax.put("depts", deptService.buildDeptTreeSelect(depts));
102   - return ajax;
103   - }
104   -
105   - /**
106   - * 新增部门
107   - */
108   - @PreAuthorize("@ss.hasPermi('system:dept:add')")
109   - @Log(title = "部门管理", businessType = BusinessType.INSERT)
110   - @PostMapping
111   - public AjaxResult add(@Validated @RequestBody SysDept dept)
112   - {
113   - if (UserConstants.NOT_UNIQUE.equals(deptService.checkDeptNameUnique(dept)))
114   - {
115   - return AjaxResult.error("新增部门'" + dept.getDeptName() + "'失败,部门名称已存在");
116   - }
117   - dept.setCreateBy(SecurityUtils.getUsername());
118   - return toAjax(deptService.insertDept(dept));
119   - }
120   -
121   - /**
122   - * 修改部门
123   - */
124   - @PreAuthorize("@ss.hasPermi('system:dept:edit')")
125   - @Log(title = "部门管理", businessType = BusinessType.UPDATE)
126   - @PutMapping
127   - public AjaxResult edit(@Validated @RequestBody SysDept dept)
128   - {
129   - if (UserConstants.NOT_UNIQUE.equals(deptService.checkDeptNameUnique(dept)))
130   - {
131   - return AjaxResult.error("修改部门'" + dept.getDeptName() + "'失败,部门名称已存在");
132   - }
133   - else if (dept.getParentId().equals(dept.getDeptId()))
134   - {
135   - return AjaxResult.error("修改部门'" + dept.getDeptName() + "'失败,上级部门不能是自己");
136   - }
137   - else if (StringUtils.equals(UserConstants.DEPT_DISABLE, dept.getStatus())
138   - && deptService.selectNormalChildrenDeptById(dept.getDeptId()) > 0)
139   - {
140   - return AjaxResult.error("该部门包含未停用的子部门!");
141   - }
142   - dept.setUpdateBy(SecurityUtils.getUsername());
143   - return toAjax(deptService.updateDept(dept));
144   - }
145   -
146   - /**
147   - * 删除部门
148   - */
149   - @PreAuthorize("@ss.hasPermi('system:dept:remove')")
150   - @Log(title = "部门管理", businessType = BusinessType.DELETE)
151   - @DeleteMapping("/{deptId}")
152   - public AjaxResult remove(@PathVariable Long deptId)
153   - {
154   - if (deptService.hasChildByDeptId(deptId))
155   - {
156   - return AjaxResult.error("存在下级部门,不允许删除");
157   - }
158   - if (deptService.checkDeptExistUser(deptId))
159   - {
160   - return AjaxResult.error("部门存在用户,不允许删除");
161   - }
162   - return toAjax(deptService.deleteDeptById(deptId));
163   - }
164   -}
  1 +package com.trash.web.controller.system;
  2 +
  3 +import java.util.Iterator;
  4 +import java.util.List;
  5 +import org.apache.commons.lang3.ArrayUtils;
  6 +import org.springframework.beans.factory.annotation.Autowired;
  7 +import org.springframework.security.access.prepost.PreAuthorize;
  8 +import org.springframework.validation.annotation.Validated;
  9 +import org.springframework.web.bind.annotation.DeleteMapping;
  10 +import org.springframework.web.bind.annotation.GetMapping;
  11 +import org.springframework.web.bind.annotation.PathVariable;
  12 +import org.springframework.web.bind.annotation.PostMapping;
  13 +import org.springframework.web.bind.annotation.PutMapping;
  14 +import org.springframework.web.bind.annotation.RequestBody;
  15 +import org.springframework.web.bind.annotation.RequestMapping;
  16 +import org.springframework.web.bind.annotation.RestController;
  17 +
  18 +import com.trash.common.annotation.Log;
  19 +import com.trash.common.constant.UserConstants;
  20 +import com.trash.common.core.controller.BaseController;
  21 +import com.trash.common.core.domain.AjaxResult;
  22 +import com.trash.common.core.domain.entity.SysDept;
  23 +import com.trash.common.enums.BusinessType;
  24 +import com.trash.common.utils.SecurityUtils;
  25 +import com.trash.common.utils.StringUtils;
  26 +import com.trash.system.service.ISysDeptService;
  27 +
  28 +/**
  29 + * 部门信息
  30 + *
  31 + * @author trash
  32 + */
  33 +@RestController
  34 +@RequestMapping("/system/dept")
  35 +public class SysDeptController extends BaseController
  36 +{
  37 + @Autowired
  38 + private ISysDeptService deptService;
  39 +
  40 + /**
  41 + * 获取部门列表
  42 + */
  43 + @PreAuthorize("@ss.hasPermi('system:dept:list')")
  44 + @GetMapping("/list")
  45 + public AjaxResult list(SysDept dept)
  46 + {
  47 + List<SysDept> depts = deptService.selectDeptList(dept);
  48 + return AjaxResult.success(depts);
  49 + }
  50 +
  51 + /**
  52 + * 查询部门列表(排除节点)
  53 + */
  54 + @PreAuthorize("@ss.hasPermi('system:dept:list')")
  55 + @GetMapping("/list/exclude/{deptId}")
  56 + public AjaxResult excludeChild(@PathVariable(value = "deptId", required = false) Long deptId)
  57 + {
  58 + List<SysDept> depts = deptService.selectDeptList(new SysDept());
  59 + Iterator<SysDept> it = depts.iterator();
  60 + while (it.hasNext())
  61 + {
  62 + SysDept d = it.next();
  63 + if (d.getDeptId().intValue() == deptId
  64 + || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), deptId + ""))
  65 + {
  66 + it.remove();
  67 + }
  68 + }
  69 + return AjaxResult.success(depts);
  70 + }
  71 +
  72 + /**
  73 + * 根据部门编号获取详细信息
  74 + */
  75 + @PreAuthorize("@ss.hasPermi('system:dept:query')")
  76 + @GetMapping(value = "/{deptId}")
  77 + public AjaxResult getInfo(@PathVariable Long deptId)
  78 + {
  79 + return AjaxResult.success(deptService.selectDeptById(deptId));
  80 + }
  81 +
  82 + /**
  83 + * 获取部门下拉树列表
  84 + */
  85 + @GetMapping("/treeselect")
  86 + public AjaxResult treeselect(SysDept dept)
  87 + {
  88 + List<SysDept> depts = deptService.selectDeptList(dept);
  89 + return AjaxResult.success(deptService.buildDeptTreeSelect(depts));
  90 + }
  91 +
  92 + /**
  93 + * 加载对应角色部门列表树
  94 + */
  95 + @GetMapping(value = "/roleDeptTreeselect/{roleId}")
  96 + public AjaxResult roleDeptTreeselect(@PathVariable("roleId") Long roleId)
  97 + {
  98 + List<SysDept> depts = deptService.selectDeptList(new SysDept());
  99 + AjaxResult ajax = AjaxResult.success();
  100 + ajax.put("checkedKeys", deptService.selectDeptListByRoleId(roleId));
  101 + ajax.put("depts", deptService.buildDeptTreeSelect(depts));
  102 + return ajax;
  103 + }
  104 +
  105 + /**
  106 + * 新增部门
  107 + */
  108 + @PreAuthorize("@ss.hasPermi('system:dept:add')")
  109 + @Log(title = "部门管理", businessType = BusinessType.INSERT)
  110 + @PostMapping
  111 + public AjaxResult add(@Validated @RequestBody SysDept dept)
  112 + {
  113 + if (UserConstants.NOT_UNIQUE.equals(deptService.checkDeptNameUnique(dept)))
  114 + {
  115 + return AjaxResult.error("新增部门'" + dept.getDeptName() + "'失败,部门名称已存在");
  116 + }
  117 + dept.setCreateBy(SecurityUtils.getUsername());
  118 + return toAjax(deptService.insertDept(dept));
  119 + }
  120 +
  121 + /**
  122 + * 修改部门
  123 + */
  124 + @PreAuthorize("@ss.hasPermi('system:dept:edit')")
  125 + @Log(title = "部门管理", businessType = BusinessType.UPDATE)
  126 + @PutMapping
  127 + public AjaxResult edit(@Validated @RequestBody SysDept dept)
  128 + {
  129 + if (UserConstants.NOT_UNIQUE.equals(deptService.checkDeptNameUnique(dept)))
  130 + {
  131 + return AjaxResult.error("修改部门'" + dept.getDeptName() + "'失败,部门名称已存在");
  132 + }
  133 + else if (dept.getParentId().equals(dept.getDeptId()))
  134 + {
  135 + return AjaxResult.error("修改部门'" + dept.getDeptName() + "'失败,上级部门不能是自己");
  136 + }
  137 + else if (StringUtils.equals(UserConstants.DEPT_DISABLE, dept.getStatus())
  138 + && deptService.selectNormalChildrenDeptById(dept.getDeptId()) > 0)
  139 + {
  140 + return AjaxResult.error("该部门包含未停用的子部门!");
  141 + }
  142 + dept.setUpdateBy(SecurityUtils.getUsername());
  143 + return toAjax(deptService.updateDept(dept));
  144 + }
  145 +
  146 + /**
  147 + * 删除部门
  148 + */
  149 + @PreAuthorize("@ss.hasPermi('system:dept:remove')")
  150 + @Log(title = "部门管理", businessType = BusinessType.DELETE)
  151 + @DeleteMapping("/{deptId}")
  152 + public AjaxResult remove(@PathVariable Long deptId)
  153 + {
  154 + if (deptService.hasChildByDeptId(deptId))
  155 + {
  156 + return AjaxResult.error("存在下级部门,不允许删除");
  157 + }
  158 + if (deptService.checkDeptExistUser(deptId))
  159 + {
  160 + return AjaxResult.error("部门存在用户,不允许删除");
  161 + }
  162 + return toAjax(deptService.deleteDeptById(deptId));
  163 + }
  164 +}
... ...
trash-common/pom.xml
1   -<?xml version="1.0" encoding="UTF-8"?>
2   -<project xmlns="http://maven.apache.org/POM/4.0.0"
3   - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4   - xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5   - <parent>
6   - <artifactId>trash</artifactId>
7   - <groupId>com.trash</groupId>
8   - <version>3.2.0</version>
9   - </parent>
10   - <modelVersion>4.0.0</modelVersion>
11   -
12   - <artifactId>trash-common</artifactId>
13   -
14   - <description>
15   - common通用工具
16   - </description>
17   -
18   - <dependencies>
19   -
20   - <dependency>
21   - <groupId>com.aliyun</groupId>
22   - <artifactId>aliyun-java-sdk-dysmsapi</artifactId>
23   - <version>2.1.0</version>
24   - </dependency>
25   - <!-- Spring框架基本的核心工具 -->
26   - <dependency>
27   - <groupId>org.springframework</groupId>
28   - <artifactId>spring-context-support</artifactId>
29   - </dependency>
30   - <dependency>
31   - <groupId>com.aliyun</groupId>
32   - <artifactId>aliyun-java-sdk-core</artifactId>
33   - <version>4.5.16</version>
34   - </dependency>
35   - <!-- SpringWeb模块 -->
36   - <dependency>
37   - <groupId>org.springframework</groupId>
38   - <artifactId>spring-web</artifactId>
39   - </dependency>
40   -
41   - <!-- spring security 安全认证 -->
42   - <dependency>
43   - <groupId>org.springframework.boot</groupId>
44   - <artifactId>spring-boot-starter-security</artifactId>
45   - </dependency>
46   -
47   - <!-- pagehelper 分页插件 -->
48   - <dependency>
49   - <groupId>com.github.pagehelper</groupId>
50   - <artifactId>pagehelper-spring-boot-starter</artifactId>
51   - </dependency>
52   -
53   - <!-- 自定义验证注解 -->
54   - <dependency>
55   - <groupId>javax.validation</groupId>
56   - <artifactId>validation-api</artifactId>
57   - </dependency>
58   -
59   - <!--常用工具类 -->
60   - <dependency>
61   - <groupId>org.apache.commons</groupId>
62   - <artifactId>commons-lang3</artifactId>
63   - </dependency>
64   -
65   - <!-- JSON工具类 -->
66   - <dependency>
67   - <groupId>com.fasterxml.jackson.core</groupId>
68   - <artifactId>jackson-databind</artifactId>
69   - </dependency>
70   -
71   - <!-- 阿里JSON解析器 -->
72   - <dependency>
73   - <groupId>com.alibaba</groupId>
74   - <artifactId>fastjson</artifactId>
75   - </dependency>
76   -
77   - <!-- io常用工具类 -->
78   - <dependency>
79   - <groupId>commons-io</groupId>
80   - <artifactId>commons-io</artifactId>
81   - </dependency>
82   -
83   - <!-- 文件上传工具类 -->
84   - <dependency>
85   - <groupId>commons-fileupload</groupId>
86   - <artifactId>commons-fileupload</artifactId>
87   - </dependency>
88   -
89   - <!-- excel工具 -->
90   - <dependency>
91   - <groupId>org.apache.poi</groupId>
92   - <artifactId>poi-ooxml</artifactId>
93   - </dependency>
94   -
95   - <!-- yml解析器 -->
96   - <dependency>
97   - <groupId>org.yaml</groupId>
98   - <artifactId>snakeyaml</artifactId>
99   - </dependency>
100   -
101   - <!--Token生成与解析-->
102   - <dependency>
103   - <groupId>io.jsonwebtoken</groupId>
104   - <artifactId>jjwt</artifactId>
105   - </dependency>
106   -
107   - <!-- redis 缓存操作 -->
108   - <dependency>
109   - <groupId>org.springframework.boot</groupId>
110   - <artifactId>spring-boot-starter-data-redis</artifactId>
111   - </dependency>
112   -
113   - <!-- pool 对象池 -->
114   - <dependency>
115   - <groupId>org.apache.commons</groupId>
116   - <artifactId>commons-pool2</artifactId>
117   - </dependency>
118   -
119   - <!-- 解析客户端操作系统、浏览器等 -->
120   - <dependency>
121   - <groupId>eu.bitwalker</groupId>
122   - <artifactId>UserAgentUtils</artifactId>
123   - </dependency>
124   -
125   - <!-- servlet包 -->
126   - <dependency>
127   - <groupId>javax.servlet</groupId>
128   - <artifactId>javax.servlet-api</artifactId>
129   - </dependency>
130   - <!-- 华为云OBS-->
131   - <dependency>
132   - <groupId>com.huaweicloud</groupId>
133   - <artifactId>esdk-obs-java</artifactId>
134   - <version>3.24.12</version>
135   - </dependency>
136   - <!-- mybatis-plus 增强CRUD -->
137   - <dependency>
138   - <groupId>com.baomidou</groupId>
139   - <artifactId>mybatis-plus-boot-starter</artifactId>
140   - <version>3.4.1</version>
141   - </dependency>
142   - </dependencies>
143   -
  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +<project xmlns="http://maven.apache.org/POM/4.0.0"
  3 + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5 + <parent>
  6 + <artifactId>trash</artifactId>
  7 + <groupId>com.trash</groupId>
  8 + <version>3.2.0</version>
  9 + </parent>
  10 + <modelVersion>4.0.0</modelVersion>
  11 +
  12 + <artifactId>trash-common</artifactId>
  13 +
  14 + <description>
  15 + common通用工具
  16 + </description>
  17 +
  18 + <dependencies>
  19 +
  20 + <dependency>
  21 + <groupId>com.aliyun</groupId>
  22 + <artifactId>aliyun-java-sdk-dysmsapi</artifactId>
  23 + <version>2.1.0</version>
  24 + </dependency>
  25 + <!-- Spring框架基本的核心工具 -->
  26 + <dependency>
  27 + <groupId>org.springframework</groupId>
  28 + <artifactId>spring-context-support</artifactId>
  29 + </dependency>
  30 + <dependency>
  31 + <groupId>com.aliyun</groupId>
  32 + <artifactId>aliyun-java-sdk-core</artifactId>
  33 + <version>4.5.16</version>
  34 + </dependency>
  35 + <!-- SpringWeb模块 -->
  36 + <dependency>
  37 + <groupId>org.springframework</groupId>
  38 + <artifactId>spring-web</artifactId>
  39 + </dependency>
  40 +
  41 + <!-- spring security 安全认证 -->
  42 + <dependency>
  43 + <groupId>org.springframework.boot</groupId>
  44 + <artifactId>spring-boot-starter-security</artifactId>
  45 + </dependency>
  46 +
  47 + <!-- pagehelper 分页插件 -->
  48 + <dependency>
  49 + <groupId>com.github.pagehelper</groupId>
  50 + <artifactId>pagehelper-spring-boot-starter</artifactId>
  51 + </dependency>
  52 +
  53 + <!-- 自定义验证注解 -->
  54 + <dependency>
  55 + <groupId>javax.validation</groupId>
  56 + <artifactId>validation-api</artifactId>
  57 + </dependency>
  58 +
  59 + <!--常用工具类 -->
  60 + <dependency>
  61 + <groupId>org.apache.commons</groupId>
  62 + <artifactId>commons-lang3</artifactId>
  63 + </dependency>
  64 +
  65 + <!-- JSON工具类 -->
  66 + <dependency>
  67 + <groupId>com.fasterxml.jackson.core</groupId>
  68 + <artifactId>jackson-databind</artifactId>
  69 + </dependency>
  70 +
  71 + <!-- 阿里JSON解析器 -->
  72 + <dependency>
  73 + <groupId>com.alibaba</groupId>
  74 + <artifactId>fastjson</artifactId>
  75 + </dependency>
  76 +
  77 + <!-- io常用工具类 -->
  78 + <dependency>
  79 + <groupId>commons-io</groupId>
  80 + <artifactId>commons-io</artifactId>
  81 + </dependency>
  82 +
  83 + <!-- 文件上传工具类 -->
  84 + <dependency>
  85 + <groupId>commons-fileupload</groupId>
  86 + <artifactId>commons-fileupload</artifactId>
  87 + </dependency>
  88 +
  89 + <!-- excel工具 -->
  90 + <dependency>
  91 + <groupId>org.apache.poi</groupId>
  92 + <artifactId>poi-ooxml</artifactId>
  93 + </dependency>
  94 +
  95 + <!-- yml解析器 -->
  96 + <dependency>
  97 + <groupId>org.yaml</groupId>
  98 + <artifactId>snakeyaml</artifactId>
  99 + </dependency>
  100 +
  101 + <!--Token生成与解析-->
  102 + <dependency>
  103 + <groupId>io.jsonwebtoken</groupId>
  104 + <artifactId>jjwt</artifactId>
  105 + </dependency>
  106 +
  107 + <!-- redis 缓存操作 -->
  108 + <dependency>
  109 + <groupId>org.springframework.boot</groupId>
  110 + <artifactId>spring-boot-starter-data-redis</artifactId>
  111 + </dependency>
  112 +
  113 + <!-- pool 对象池 -->
  114 + <dependency>
  115 + <groupId>org.apache.commons</groupId>
  116 + <artifactId>commons-pool2</artifactId>
  117 + </dependency>
  118 +
  119 + <!-- 解析客户端操作系统、浏览器等 -->
  120 + <dependency>
  121 + <groupId>eu.bitwalker</groupId>
  122 + <artifactId>UserAgentUtils</artifactId>
  123 + </dependency>
  124 +
  125 + <!-- servlet包 -->
  126 + <dependency>
  127 + <groupId>javax.servlet</groupId>
  128 + <artifactId>javax.servlet-api</artifactId>
  129 + </dependency>
  130 + <!-- 华为云OBS-->
  131 + <dependency>
  132 + <groupId>com.huaweicloud</groupId>
  133 + <artifactId>esdk-obs-java</artifactId>
  134 + <version>3.24.12</version>
  135 + </dependency>
  136 + <!-- mybatis-plus 增强CRUD -->
  137 + <dependency>
  138 + <groupId>com.baomidou</groupId>
  139 + <artifactId>mybatis-plus-boot-starter</artifactId>
  140 + <version>3.4.1</version>
  141 + </dependency>
  142 + </dependencies>
  143 +
144 144 </project>
145 145 \ No newline at end of file
... ...
trash-common/src/main/java/com/trash/common/annotation/DataScope.java
1   -package com.trash.common.annotation;
2   -
3   -import java.lang.annotation.Documented;
4   -import java.lang.annotation.ElementType;
5   -import java.lang.annotation.Retention;
6   -import java.lang.annotation.RetentionPolicy;
7   -import java.lang.annotation.Target;
8   -
9   -/**
10   - * 数据权限过滤注解
11   - *
12   - * @author trash
13   - */
14   -@Target(ElementType.METHOD)
15   -@Retention(RetentionPolicy.RUNTIME)
16   -@Documented
17   -public @interface DataScope
18   -{
19   - /**
20   - * 部门表的别名
21   - */
22   - String deptAlias() default "";
23   -
24   - /**
25   - * 用户表的别名
26   - */
27   - String userAlias() default "";
28   -}
  1 +package com.trash.common.annotation;
  2 +
  3 +import java.lang.annotation.Documented;
  4 +import java.lang.annotation.ElementType;
  5 +import java.lang.annotation.Retention;
  6 +import java.lang.annotation.RetentionPolicy;
  7 +import java.lang.annotation.Target;
  8 +
  9 +/**
  10 + * 数据权限过滤注解
  11 + *
  12 + * @author trash
  13 + */
  14 +@Target(ElementType.METHOD)
  15 +@Retention(RetentionPolicy.RUNTIME)
  16 +@Documented
  17 +public @interface DataScope
  18 +{
  19 + /**
  20 + * 部门表的别名
  21 + */
  22 + String deptAlias() default "";
  23 +
  24 + /**
  25 + * 用户表的别名
  26 + */
  27 + String userAlias() default "";
  28 +}
... ...
trash-common/src/main/java/com/trash/common/annotation/DataSource.java
1   -package com.trash.common.annotation;
2   -
3   -import java.lang.annotation.Documented;
4   -import java.lang.annotation.ElementType;
5   -import java.lang.annotation.Inherited;
6   -import java.lang.annotation.Retention;
7   -import java.lang.annotation.RetentionPolicy;
8   -import java.lang.annotation.Target;
9   -
10   -import com.trash.common.enums.DataSourceType;
11   -
12   -/**
13   - * 自定义多数据源切换注解
14   - *
15   - * 优先级:先方法,后类,如果方法覆盖了类上的数据源类型,以方法的为准,否则以类上的为准
16   - *
17   - * @author trash
18   - */
19   -@Target({ ElementType.METHOD, ElementType.TYPE })
20   -@Retention(RetentionPolicy.RUNTIME)
21   -@Documented
22   -@Inherited
23   -public @interface DataSource
24   -{
25   - /**
26   - * 切换数据源名称
27   - */
28   - DataSourceType value() default DataSourceType.MASTER;
29   -}
  1 +package com.trash.common.annotation;
  2 +
  3 +import java.lang.annotation.Documented;
  4 +import java.lang.annotation.ElementType;
  5 +import java.lang.annotation.Inherited;
  6 +import java.lang.annotation.Retention;
  7 +import java.lang.annotation.RetentionPolicy;
  8 +import java.lang.annotation.Target;
  9 +
  10 +import com.trash.common.enums.DataSourceType;
  11 +
  12 +/**
  13 + * 自定义多数据源切换注解
  14 + *
  15 + * 优先级:先方法,后类,如果方法覆盖了类上的数据源类型,以方法的为准,否则以类上的为准
  16 + *
  17 + * @author trash
  18 + */
  19 +@Target({ ElementType.METHOD, ElementType.TYPE })
  20 +@Retention(RetentionPolicy.RUNTIME)
  21 +@Documented
  22 +@Inherited
  23 +public @interface DataSource
  24 +{
  25 + /**
  26 + * 切换数据源名称
  27 + */
  28 + DataSourceType value() default DataSourceType.MASTER;
  29 +}
... ...
trash-common/src/main/java/com/trash/common/annotation/Excel.java
1   -package com.trash.common.annotation;
2   -
3   -import java.lang.annotation.ElementType;
4   -import java.lang.annotation.Retention;
5   -import java.lang.annotation.RetentionPolicy;
6   -import java.lang.annotation.Target;
7   -import java.math.BigDecimal;
8   -
9   -/**
10   - * 自定义导出Excel数据注解
11   - *
12   - * @author trash
13   - */
14   -@Retention(RetentionPolicy.RUNTIME)
15   -@Target(ElementType.FIELD)
16   -public @interface Excel
17   -{
18   - /**
19   - * 导出时在excel中排序
20   - */
21   - int sort() default Integer.MAX_VALUE;
22   -
23   - /**
24   - * 导出到Excel中的名字.
25   - */
26   - String name() default "";
27   -
28   - /**
29   - * 日期格式, 如: yyyy-MM-dd
30   - */
31   - String dateFormat() default "";
32   -
33   - /**
34   - * 如果是字典类型,请设置字典的type值 (如: sys_user_sex)
35   - */
36   - String dictType() default "";
37   -
38   - /**
39   - * 读取内容转表达式 (如: 0=男,1=女,2=未知)
40   - */
41   - String readConverterExp() default "";
42   -
43   - /**
44   - * 分隔符,读取字符串组内容
45   - */
46   - String separator() default ",";
47   -
48   - /**
49   - * BigDecimal 精度 默认:-1(默认不开启BigDecimal格式化)
50   - */
51   - int scale() default -1;
52   -
53   - /**
54   - * BigDecimal 舍入规则 默认:BigDecimal.ROUND_HALF_EVEN
55   - */
56   - int roundingMode() default BigDecimal.ROUND_HALF_EVEN;
57   -
58   - /**
59   - * 导出类型(0数字 1字符串)
60   - */
61   - ColumnType cellType() default ColumnType.STRING;
62   -
63   - /**
64   - * 导出时在excel中每个列的高度 单位为字符
65   - */
66   - double height() default 14;
67   -
68   - /**
69   - * 导出时在excel中每个列的宽 单位为字符
70   - */
71   - double width() default 16;
72   -
73   - /**
74   - * 文字后缀,如% 90 变成90%
75   - */
76   - String suffix() default "";
77   -
78   - /**
79   - * 当值为空时,字段的默认值
80   - */
81   - String defaultValue() default "";
82   -
83   - /**
84   - * 提示信息
85   - */
86   - String prompt() default "";
87   -
88   - /**
89   - * 设置只能选择不能输入的列内容.
90   - */
91   - String[] combo() default {};
92   -
93   - /**
94   - * 是否导出数据,应对需求:有时我们需要导出一份模板,这是标题需要但内容需要用户手工填写.
95   - */
96   - boolean isExport() default true;
97   -
98   - /**
99   - * 另一个类中的属性名称,支持多级获取,以小数点隔开
100   - */
101   - String targetAttr() default "";
102   -
103   - /**
104   - * 是否自动统计数据,在最后追加一行统计数据总和
105   - */
106   - boolean isStatistics() default false;
107   -
108   - /**
109   - * 字段类型(0:导出导入;1:仅导出;2:仅导入)
110   - */
111   - Type type() default Type.ALL;
112   -
113   - enum Type
114   - {
115   - ALL(0), EXPORT(1), IMPORT(2);
116   - private final int value;
117   -
118   - Type(int value)
119   - {
120   - this.value = value;
121   - }
122   -
123   - public int value()
124   - {
125   - return this.value;
126   - }
127   - }
128   -
129   - enum ColumnType
130   - {
131   - NUMERIC(0), STRING(1);
132   - private final int value;
133   -
134   - ColumnType(int value)
135   - {
136   - this.value = value;
137   - }
138   -
139   - public int value()
140   - {
141   - return this.value;
142   - }
143   - }
  1 +package com.trash.common.annotation;
  2 +
  3 +import java.lang.annotation.ElementType;
  4 +import java.lang.annotation.Retention;
  5 +import java.lang.annotation.RetentionPolicy;
  6 +import java.lang.annotation.Target;
  7 +import java.math.BigDecimal;
  8 +
  9 +/**
  10 + * 自定义导出Excel数据注解
  11 + *
  12 + * @author trash
  13 + */
  14 +@Retention(RetentionPolicy.RUNTIME)
  15 +@Target(ElementType.FIELD)
  16 +public @interface Excel
  17 +{
  18 + /**
  19 + * 导出时在excel中排序
  20 + */
  21 + int sort() default Integer.MAX_VALUE;
  22 +
  23 + /**
  24 + * 导出到Excel中的名字.
  25 + */
  26 + String name() default "";
  27 +
  28 + /**
  29 + * 日期格式, 如: yyyy-MM-dd
  30 + */
  31 + String dateFormat() default "";
  32 +
  33 + /**
  34 + * 如果是字典类型,请设置字典的type值 (如: sys_user_sex)
  35 + */
  36 + String dictType() default "";
  37 +
  38 + /**
  39 + * 读取内容转表达式 (如: 0=男,1=女,2=未知)
  40 + */
  41 + String readConverterExp() default "";
  42 +
  43 + /**
  44 + * 分隔符,读取字符串组内容
  45 + */
  46 + String separator() default ",";
  47 +
  48 + /**
  49 + * BigDecimal 精度 默认:-1(默认不开启BigDecimal格式化)
  50 + */
  51 + int scale() default -1;
  52 +
  53 + /**
  54 + * BigDecimal 舍入规则 默认:BigDecimal.ROUND_HALF_EVEN
  55 + */
  56 + int roundingMode() default BigDecimal.ROUND_HALF_EVEN;
  57 +
  58 + /**
  59 + * 导出类型(0数字 1字符串)
  60 + */
  61 + ColumnType cellType() default ColumnType.STRING;
  62 +
  63 + /**
  64 + * 导出时在excel中每个列的高度 单位为字符
  65 + */
  66 + double height() default 14;
  67 +
  68 + /**
  69 + * 导出时在excel中每个列的宽 单位为字符
  70 + */
  71 + double width() default 16;
  72 +
  73 + /**
  74 + * 文字后缀,如% 90 变成90%
  75 + */
  76 + String suffix() default "";
  77 +
  78 + /**
  79 + * 当值为空时,字段的默认值
  80 + */
  81 + String defaultValue() default "";
  82 +
  83 + /**
  84 + * 提示信息
  85 + */
  86 + String prompt() default "";
  87 +
  88 + /**
  89 + * 设置只能选择不能输入的列内容.
  90 + */
  91 + String[] combo() default {};
  92 +
  93 + /**
  94 + * 是否导出数据,应对需求:有时我们需要导出一份模板,这是标题需要但内容需要用户手工填写.
  95 + */
  96 + boolean isExport() default true;
  97 +
  98 + /**
  99 + * 另一个类中的属性名称,支持多级获取,以小数点隔开
  100 + */
  101 + String targetAttr() default "";
  102 +
  103 + /**
  104 + * 是否自动统计数据,在最后追加一行统计数据总和
  105 + */
  106 + boolean isStatistics() default false;
  107 +
  108 + /**
  109 + * 字段类型(0:导出导入;1:仅导出;2:仅导入)
  110 + */
  111 + Type type() default Type.ALL;
  112 +
  113 + enum Type
  114 + {
  115 + ALL(0), EXPORT(1), IMPORT(2);
  116 + private final int value;
  117 +
  118 + Type(int value)
  119 + {
  120 + this.value = value;
  121 + }
  122 +
  123 + public int value()
  124 + {
  125 + return this.value;
  126 + }
  127 + }
  128 +
  129 + enum ColumnType
  130 + {
  131 + NUMERIC(0), STRING(1);
  132 + private final int value;
  133 +
  134 + ColumnType(int value)
  135 + {
  136 + this.value = value;
  137 + }
  138 +
  139 + public int value()
  140 + {
  141 + return this.value;
  142 + }
  143 + }
144 144 }
145 145 \ No newline at end of file
... ...
trash-common/src/main/java/com/trash/common/annotation/Log.java
1   -package com.trash.common.annotation;
2   -
3   -import java.lang.annotation.Documented;
4   -import java.lang.annotation.ElementType;
5   -import java.lang.annotation.Retention;
6   -import java.lang.annotation.RetentionPolicy;
7   -import java.lang.annotation.Target;
8   -
9   -import com.trash.common.enums.BusinessType;
10   -import com.trash.common.enums.OperatorType;
11   -
12   -/**
13   - * 自定义操作日志记录注解
14   - *
15   - * @author trash
16   - *
17   - */
18   -@Target({ ElementType.PARAMETER, ElementType.METHOD })
19   -@Retention(RetentionPolicy.RUNTIME)
20   -@Documented
21   -public @interface Log
22   -{
23   - /**
24   - * 模块
25   - */
26   - String title() default "";
27   -
28   - /**
29   - * 功能
30   - */
31   - BusinessType businessType() default BusinessType.OTHER;
32   -
33   - /**
34   - * 操作人类别
35   - */
36   - OperatorType operatorType() default OperatorType.MANAGE;
37   -
38   - /**
39   - * 是否保存请求的参数
40   - */
41   - boolean isSaveRequestData() default true;
42   -}
  1 +package com.trash.common.annotation;
  2 +
  3 +import java.lang.annotation.Documented;
  4 +import java.lang.annotation.ElementType;
  5 +import java.lang.annotation.Retention;
  6 +import java.lang.annotation.RetentionPolicy;
  7 +import java.lang.annotation.Target;
  8 +
  9 +import com.trash.common.enums.BusinessType;
  10 +import com.trash.common.enums.OperatorType;
  11 +
  12 +/**
  13 + * 自定义操作日志记录注解
  14 + *
  15 + * @author trash
  16 + *
  17 + */
  18 +@Target({ ElementType.PARAMETER, ElementType.METHOD })
  19 +@Retention(RetentionPolicy.RUNTIME)
  20 +@Documented
  21 +public @interface Log
  22 +{
  23 + /**
  24 + * 模块
  25 + */
  26 + String title() default "";
  27 +
  28 + /**
  29 + * 功能
  30 + */
  31 + BusinessType businessType() default BusinessType.OTHER;
  32 +
  33 + /**
  34 + * 操作人类别
  35 + */
  36 + OperatorType operatorType() default OperatorType.MANAGE;
  37 +
  38 + /**
  39 + * 是否保存请求的参数
  40 + */
  41 + boolean isSaveRequestData() default true;
  42 +}
... ...
trash-common/src/main/java/com/trash/common/config/trashConfig.java
1   -package com.trash.common.config;
2   -
3   -import org.springframework.boot.context.properties.ConfigurationProperties;
4   -import org.springframework.stereotype.Component;
5   -
6   -/**
7   - * 读取项目相关配置
8   - *
9   - * @author 2c
10   - */
11   -@Component
12   -@ConfigurationProperties(prefix = "trash")
13   -public class trashConfig
14   -{
15   - /** 项目名称 */
16   - private String name;
17   -
18   - /** 版本 */
19   - private String version;
20   -
21   - /** 版权年份 */
22   - private String copyrightYear;
23   -
24   - /** 实例演示开关 */
25   - private boolean demoEnabled;
26   -
27   - /** 上传路径 */
28   - private static String profile;
29   -
30   - private String remotePath;
31   - private String token;
32   -
33   - /** 获取地址开关 */
34   - private static boolean addressEnabled;
35   -
36   -
37   -
38   - public String getToken() {
39   - return token;
40   - }
41   -
42   - public void setToken(String token) {
43   - this.token = token;
44   - }
45   -
46   - public String getName()
47   - {
48   - return name;
49   - }
50   -
51   - public void setName(String name)
52   - {
53   - this.name = name;
54   - }
55   -
56   - public String getVersion()
57   - {
58   - return version;
59   - }
60   -
61   - public void setVersion(String version)
62   - {
63   - this.version = version;
64   - }
65   -
66   - public String getCopyrightYear()
67   - {
68   - return copyrightYear;
69   - }
70   -
71   - public void setCopyrightYear(String copyrightYear)
72   - {
73   - this.copyrightYear = copyrightYear;
74   - }
75   -
76   - public boolean isDemoEnabled()
77   - {
78   - return demoEnabled;
79   - }
80   -
81   - public void setDemoEnabled(boolean demoEnabled)
82   - {
83   - this.demoEnabled = demoEnabled;
84   - }
85   -
86   - public static String getProfile()
87   - {
88   - return profile;
89   - }
90   -
91   - public void setProfile(String profile)
92   - {
93   - trashConfig.profile = profile;
94   - }
95   -
96   - public static boolean isAddressEnabled()
97   - {
98   - return addressEnabled;
99   - }
100   -
101   - public void setAddressEnabled(boolean addressEnabled)
102   - {
103   - trashConfig.addressEnabled = addressEnabled;
104   - }
105   -
106   -
107   -
108   - public String getRemotePath() {
109   - return remotePath;
110   - }
111   -
112   - public void setRemotePath(String remote) {
113   - this.remotePath = remote;
114   - }
115   -
116   - /**
117   - * 获取头像上传路径
118   - */
119   - public static String getAvatarPath()
120   - {
121   - return getProfile() + "/avatar";
122   - }
123   -
124   - /**
125   - * 获取下载路径
126   - */
127   - public static String getDownloadPath()
128   - {
129   - return getProfile() + "/download/";
130   - }
131   -
132   - /**
133   - * 获取上传路径
134   - */
135   - public static String getUploadPath()
136   - {
137   - return getProfile() + "/upload";
138   - }
139   -}
  1 +package com.trash.common.config;
  2 +
  3 +import org.springframework.boot.context.properties.ConfigurationProperties;
  4 +import org.springframework.stereotype.Component;
  5 +
  6 +/**
  7 + * 读取项目相关配置
  8 + *
  9 + * @author 2c
  10 + */
  11 +@Component
  12 +@ConfigurationProperties(prefix = "trash")
  13 +public class trashConfig
  14 +{
  15 + /** 项目名称 */
  16 + private String name;
  17 +
  18 + /** 版本 */
  19 + private String version;
  20 +
  21 + /** 版权年份 */
  22 + private String copyrightYear;
  23 +
  24 + /** 实例演示开关 */
  25 + private boolean demoEnabled;
  26 +
  27 + /** 上传路径 */
  28 + private static String profile;
  29 +
  30 + private String remotePath;
  31 + private String token;
  32 +
  33 + /** 获取地址开关 */
  34 + private static boolean addressEnabled;
  35 +
  36 +
  37 +
  38 + public String getToken() {
  39 + return token;
  40 + }
  41 +
  42 + public void setToken(String token) {
  43 + this.token = token;
  44 + }
  45 +
  46 + public String getName()
  47 + {
  48 + return name;
  49 + }
  50 +
  51 + public void setName(String name)
  52 + {
  53 + this.name = name;
  54 + }
  55 +
  56 + public String getVersion()
  57 + {
  58 + return version;
  59 + }
  60 +
  61 + public void setVersion(String version)
  62 + {
  63 + this.version = version;
  64 + }
  65 +
  66 + public String getCopyrightYear()
  67 + {
  68 + return copyrightYear;
  69 + }
  70 +
  71 + public void setCopyrightYear(String copyrightYear)
  72 + {
  73 + this.copyrightYear = copyrightYear;
  74 + }
  75 +
  76 + public boolean isDemoEnabled()
  77 + {
  78 + return demoEnabled;
  79 + }
  80 +
  81 + public void setDemoEnabled(boolean demoEnabled)
  82 + {
  83 + this.demoEnabled = demoEnabled;
  84 + }
  85 +
  86 + public static String getProfile()
  87 + {
  88 + return profile;
  89 + }
  90 +
  91 + public void setProfile(String profile)
  92 + {
  93 + trashConfig.profile = profile;
  94 + }
  95 +
  96 + public static boolean isAddressEnabled()
  97 + {
  98 + return addressEnabled;
  99 + }
  100 +
  101 + public void setAddressEnabled(boolean addressEnabled)
  102 + {
  103 + trashConfig.addressEnabled = addressEnabled;
  104 + }
  105 +
  106 +
  107 +
  108 + public String getRemotePath() {
  109 + return remotePath;
  110 + }
  111 +
  112 + public void setRemotePath(String remote) {
  113 + this.remotePath = remote;
  114 + }
  115 +
  116 + /**
  117 + * 获取头像上传路径
  118 + */
  119 + public static String getAvatarPath()
  120 + {
  121 + return getProfile() + "/avatar";
  122 + }
  123 +
  124 + /**
  125 + * 获取下载路径
  126 + */
  127 + public static String getDownloadPath()
  128 + {
  129 + return getProfile() + "/download/";
  130 + }
  131 +
  132 + /**
  133 + * 获取上传路径
  134 + */
  135 + public static String getUploadPath()
  136 + {
  137 + return getProfile() + "/upload";
  138 + }
  139 +}
... ...
trash-common/src/main/java/com/trash/common/constant/ScheduleConstants.java
1   -package com.trash.common.constant;
2   -
3   -/**
4   - * 任务调度通用常量
5   - *
6   - * @author trash
7   - */
8   -public class ScheduleConstants
9   -{
10   - public static final String TASK_CLASS_NAME = "TASK_CLASS_NAME";
11   -
12   - /** 执行目标key */
13   - public static final String TASK_PROPERTIES = "TASK_PROPERTIES";
14   -
15   - /** 默认 */
16   - public static final String MISFIRE_DEFAULT = "0";
17   -
18   - /** 立即触发执行 */
19   - public static final String MISFIRE_IGNORE_MISFIRES = "1";
20   -
21   - /** 触发一次执行 */
22   - public static final String MISFIRE_FIRE_AND_PROCEED = "2";
23   -
24   - /** 不触发立即执行 */
25   - public static final String MISFIRE_DO_NOTHING = "3";
26   -
27   - public enum Status
28   - {
29   - /**
30   - * 正常
31   - */
32   - NORMAL("0"),
33   - /**
34   - * 暂停
35   - */
36   - PAUSE("1");
37   -
38   - private final String value;
39   -
40   - Status(String value)
41   - {
42   - this.value = value;
43   - }
44   -
45   - public String getValue()
46   - {
47   - return value;
48   - }
49   - }
50   -}
  1 +package com.trash.common.constant;
  2 +
  3 +/**
  4 + * 任务调度通用常量
  5 + *
  6 + * @author trash
  7 + */
  8 +public class ScheduleConstants
  9 +{
  10 + public static final String TASK_CLASS_NAME = "TASK_CLASS_NAME";
  11 +
  12 + /** 执行目标key */
  13 + public static final String TASK_PROPERTIES = "TASK_PROPERTIES";
  14 +
  15 + /** 默认 */
  16 + public static final String MISFIRE_DEFAULT = "0";
  17 +
  18 + /** 立即触发执行 */
  19 + public static final String MISFIRE_IGNORE_MISFIRES = "1";
  20 +
  21 + /** 触发一次执行 */
  22 + public static final String MISFIRE_FIRE_AND_PROCEED = "2";
  23 +
  24 + /** 不触发立即执行 */
  25 + public static final String MISFIRE_DO_NOTHING = "3";
  26 +
  27 + public enum Status
  28 + {
  29 + /**
  30 + * 正常
  31 + */
  32 + NORMAL("0"),
  33 + /**
  34 + * 暂停
  35 + */
  36 + PAUSE("1");
  37 +
  38 + private final String value;
  39 +
  40 + Status(String value)
  41 + {
  42 + this.value = value;
  43 + }
  44 +
  45 + public String getValue()
  46 + {
  47 + return value;
  48 + }
  49 + }
  50 +}
... ...
trash-common/src/main/java/com/trash/common/core/domain/entity/SysDictData.java
1   -package com.trash.common.core.domain.entity;
2   -
3   -import javax.validation.constraints.NotBlank;
4   -import javax.validation.constraints.Size;
5   -import org.apache.commons.lang3.builder.ToStringBuilder;
6   -import org.apache.commons.lang3.builder.ToStringStyle;
7   -
8   -import com.trash.common.annotation.Excel;
9   -import com.trash.common.annotation.Excel.ColumnType;
10   -import com.trash.common.constant.UserConstants;
11   -import com.trash.common.core.domain.BaseEntity;
12   -
13   -/**
14   - * 字典数据表 sys_dict_data
15   - *
16   - * @author trash
17   - */
18   -public class SysDictData extends BaseEntity
19   -{
20   - private static final long serialVersionUID = 1L;
21   -
22   - /** 字典编码 */
23   - @Excel(name = "字典编码", cellType = ColumnType.NUMERIC)
24   - private Long dictCode;
25   -
26   - /** 字典排序 */
27   - @Excel(name = "字典排序", cellType = ColumnType.NUMERIC)
28   - private Long dictSort;
29   -
30   - /** 字典标签 */
31   - @Excel(name = "字典标签")
32   - private String dictLabel;
33   -
34   - /** 字典键值 */
35   - @Excel(name = "字典键值")
36   - private String dictValue;
37   -
38   - /** 字典类型 */
39   - @Excel(name = "字典类型")
40   - private String dictType;
41   -
42   - /** 样式属性(其他样式扩展) */
43   - private String cssClass;
44   -
45   - /** 表格字典样式 */
46   - private String listClass;
47   -
48   - /** 是否默认(Y是 N否) */
49   - @Excel(name = "是否默认", readConverterExp = "Y=是,N=否")
50   - private String isDefault;
51   -
52   - /** 状态(0正常 1停用) */
53   - @Excel(name = "状态", readConverterExp = "0=正常,1=停用")
54   - private String status;
55   -
56   - public Long getDictCode()
57   - {
58   - return dictCode;
59   - }
60   -
61   - public void setDictCode(Long dictCode)
62   - {
63   - this.dictCode = dictCode;
64   - }
65   -
66   - public Long getDictSort()
67   - {
68   - return dictSort;
69   - }
70   -
71   - public void setDictSort(Long dictSort)
72   - {
73   - this.dictSort = dictSort;
74   - }
75   -
76   - @NotBlank(message = "字典标签不能为空")
77   - @Size(min = 0, max = 100, message = "字典标签长度不能超过100个字符")
78   - public String getDictLabel()
79   - {
80   - return dictLabel;
81   - }
82   -
83   - public void setDictLabel(String dictLabel)
84   - {
85   - this.dictLabel = dictLabel;
86   - }
87   -
88   - @NotBlank(message = "字典键值不能为空")
89   - @Size(min = 0, max = 100, message = "字典键值长度不能超过100个字符")
90   - public String getDictValue()
91   - {
92   - return dictValue;
93   - }
94   -
95   - public void setDictValue(String dictValue)
96   - {
97   - this.dictValue = dictValue;
98   - }
99   -
100   - @NotBlank(message = "字典类型不能为空")
101   - @Size(min = 0, max = 100, message = "字典类型长度不能超过100个字符")
102   - public String getDictType()
103   - {
104   - return dictType;
105   - }
106   -
107   - public void setDictType(String dictType)
108   - {
109   - this.dictType = dictType;
110   - }
111   -
112   - @Size(min = 0, max = 100, message = "样式属性长度不能超过100个字符")
113   - public String getCssClass()
114   - {
115   - return cssClass;
116   - }
117   -
118   - public void setCssClass(String cssClass)
119   - {
120   - this.cssClass = cssClass;
121   - }
122   -
123   - public String getListClass()
124   - {
125   - return listClass;
126   - }
127   -
128   - public void setListClass(String listClass)
129   - {
130   - this.listClass = listClass;
131   - }
132   -
133   - public boolean getDefault()
134   - {
135   - return UserConstants.YES.equals(this.isDefault);
136   - }
137   -
138   - public String getIsDefault()
139   - {
140   - return isDefault;
141   - }
142   -
143   - public void setIsDefault(String isDefault)
144   - {
145   - this.isDefault = isDefault;
146   - }
147   -
148   - public String getStatus()
149   - {
150   - return status;
151   - }
152   -
153   - public void setStatus(String status)
154   - {
155   - this.status = status;
156   - }
157   -
158   - @Override
159   - public String toString() {
160   - return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
161   - .append("dictCode", getDictCode())
162   - .append("dictSort", getDictSort())
163   - .append("dictLabel", getDictLabel())
164   - .append("dictValue", getDictValue())
165   - .append("dictType", getDictType())
166   - .append("cssClass", getCssClass())
167   - .append("listClass", getListClass())
168   - .append("isDefault", getIsDefault())
169   - .append("status", getStatus())
170   - .append("createBy", getCreateBy())
171   - .append("createTime", getCreateTime())
172   - .append("updateBy", getUpdateBy())
173   - .append("updateTime", getUpdateTime())
174   - .append("remark", getRemark())
175   - .toString();
176   - }
177   -}
  1 +package com.trash.common.core.domain.entity;
  2 +
  3 +import javax.validation.constraints.NotBlank;
  4 +import javax.validation.constraints.Size;
  5 +import org.apache.commons.lang3.builder.ToStringBuilder;
  6 +import org.apache.commons.lang3.builder.ToStringStyle;
  7 +
  8 +import com.trash.common.annotation.Excel;
  9 +import com.trash.common.annotation.Excel.ColumnType;
  10 +import com.trash.common.constant.UserConstants;
  11 +import com.trash.common.core.domain.BaseEntity;
  12 +
  13 +/**
  14 + * 字典数据表 sys_dict_data
  15 + *
  16 + * @author trash
  17 + */
  18 +public class SysDictData extends BaseEntity
  19 +{
  20 + private static final long serialVersionUID = 1L;
  21 +
  22 + /** 字典编码 */
  23 + @Excel(name = "字典编码", cellType = ColumnType.NUMERIC)
  24 + private Long dictCode;
  25 +
  26 + /** 字典排序 */
  27 + @Excel(name = "字典排序", cellType = ColumnType.NUMERIC)
  28 + private Long dictSort;
  29 +
  30 + /** 字典标签 */
  31 + @Excel(name = "字典标签")
  32 + private String dictLabel;
  33 +
  34 + /** 字典键值 */
  35 + @Excel(name = "字典键值")
  36 + private String dictValue;
  37 +
  38 + /** 字典类型 */
  39 + @Excel(name = "字典类型")
  40 + private String dictType;
  41 +
  42 + /** 样式属性(其他样式扩展) */
  43 + private String cssClass;
  44 +
  45 + /** 表格字典样式 */
  46 + private String listClass;
  47 +
  48 + /** 是否默认(Y是 N否) */
  49 + @Excel(name = "是否默认", readConverterExp = "Y=是,N=否")
  50 + private String isDefault;
  51 +
  52 + /** 状态(0正常 1停用) */
  53 + @Excel(name = "状态", readConverterExp = "0=正常,1=停用")
  54 + private String status;
  55 +
  56 + public Long getDictCode()
  57 + {
  58 + return dictCode;
  59 + }
  60 +
  61 + public void setDictCode(Long dictCode)
  62 + {
  63 + this.dictCode = dictCode;
  64 + }
  65 +
  66 + public Long getDictSort()
  67 + {
  68 + return dictSort;
  69 + }
  70 +
  71 + public void setDictSort(Long dictSort)
  72 + {
  73 + this.dictSort = dictSort;
  74 + }
  75 +
  76 + @NotBlank(message = "字典标签不能为空")
  77 + @Size(min = 0, max = 100, message = "字典标签长度不能超过100个字符")
  78 + public String getDictLabel()
  79 + {
  80 + return dictLabel;
  81 + }
  82 +
  83 + public void setDictLabel(String dictLabel)
  84 + {
  85 + this.dictLabel = dictLabel;
  86 + }
  87 +
  88 + @NotBlank(message = "字典键值不能为空")
  89 + @Size(min = 0, max = 100, message = "字典键值长度不能超过100个字符")
  90 + public String getDictValue()
  91 + {
  92 + return dictValue;
  93 + }
  94 +
  95 + public void setDictValue(String dictValue)
  96 + {
  97 + this.dictValue = dictValue;
  98 + }
  99 +
  100 + @NotBlank(message = "字典类型不能为空")
  101 + @Size(min = 0, max = 100, message = "字典类型长度不能超过100个字符")
  102 + public String getDictType()
  103 + {
  104 + return dictType;
  105 + }
  106 +
  107 + public void setDictType(String dictType)
  108 + {
  109 + this.dictType = dictType;
  110 + }
  111 +
  112 + @Size(min = 0, max = 100, message = "样式属性长度不能超过100个字符")
  113 + public String getCssClass()
  114 + {
  115 + return cssClass;
  116 + }
  117 +
  118 + public void setCssClass(String cssClass)
  119 + {
  120 + this.cssClass = cssClass;
  121 + }
  122 +
  123 + public String getListClass()
  124 + {
  125 + return listClass;
  126 + }
  127 +
  128 + public void setListClass(String listClass)
  129 + {
  130 + this.listClass = listClass;
  131 + }
  132 +
  133 + public boolean getDefault()
  134 + {
  135 + return UserConstants.YES.equals(this.isDefault);
  136 + }
  137 +
  138 + public String getIsDefault()
  139 + {
  140 + return isDefault;
  141 + }
  142 +
  143 + public void setIsDefault(String isDefault)
  144 + {
  145 + this.isDefault = isDefault;
  146 + }
  147 +
  148 + public String getStatus()
  149 + {
  150 + return status;
  151 + }
  152 +
  153 + public void setStatus(String status)
  154 + {
  155 + this.status = status;
  156 + }
  157 +
  158 + @Override
  159 + public String toString() {
  160 + return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
  161 + .append("dictCode", getDictCode())
  162 + .append("dictSort", getDictSort())
  163 + .append("dictLabel", getDictLabel())
  164 + .append("dictValue", getDictValue())
  165 + .append("dictType", getDictType())
  166 + .append("cssClass", getCssClass())
  167 + .append("listClass", getListClass())
  168 + .append("isDefault", getIsDefault())
  169 + .append("status", getStatus())
  170 + .append("createBy", getCreateBy())
  171 + .append("createTime", getCreateTime())
  172 + .append("updateBy", getUpdateBy())
  173 + .append("updateTime", getUpdateTime())
  174 + .append("remark", getRemark())
  175 + .toString();
  176 + }
  177 +}
... ...
trash-common/src/main/java/com/trash/common/core/text/CharsetKit.java
1   -package com.trash.common.core.text;
2   -
3   -import java.nio.charset.Charset;
4   -import java.nio.charset.StandardCharsets;
5   -
6   -import com.trash.common.utils.StringUtils;
7   -
8   -/**
9   - * 字符集工具类
10   - *
11   - * @author trash
12   - */
13   -public class CharsetKit
14   -{
15   - /** ISO-8859-1 */
16   - public static final String ISO_8859_1 = "ISO-8859-1";
17   - /** UTF-8 */
18   - public static final String UTF_8 = "UTF-8";
19   - /** GBK */
20   - public static final String GBK = "GBK";
21   -
22   - /** ISO-8859-1 */
23   - public static final Charset CHARSET_ISO_8859_1 = StandardCharsets.ISO_8859_1;
24   - /** UTF-8 */
25   - public static final Charset CHARSET_UTF_8 = StandardCharsets.UTF_8;
26   - /** GBK */
27   - public static final Charset CHARSET_GBK = Charset.forName(GBK);
28   -
29   - /**
30   - * 转换为Charset对象
31   - *
32   - * @param charset 字符集,为空则返回默认字符集
33   - * @return Charset
34   - */
35   - public static Charset charset(String charset)
36   - {
37   - return StringUtils.isEmpty(charset) ? Charset.defaultCharset() : Charset.forName(charset);
38   - }
39   -
40   - /**
41   - * 转换字符串的字符集编码
42   - *
43   - * @param source 字符串
44   - * @param srcCharset 源字符集,默认ISO-8859-1
45   - * @param destCharset 目标字符集,默认UTF-8
46   - * @return 转换后的字符集
47   - */
48   - public static String convert(String source, String srcCharset, String destCharset)
49   - {
50   - return convert(source, Charset.forName(srcCharset), Charset.forName(destCharset));
51   - }
52   -
53   - /**
54   - * 转换字符串的字符集编码
55   - *
56   - * @param source 字符串
57   - * @param srcCharset 源字符集,默认ISO-8859-1
58   - * @param destCharset 目标字符集,默认UTF-8
59   - * @return 转换后的字符集
60   - */
61   - public static String convert(String source, Charset srcCharset, Charset destCharset)
62   - {
63   - if (null == srcCharset)
64   - {
65   - srcCharset = StandardCharsets.ISO_8859_1;
66   - }
67   -
68   - if (null == destCharset)
69   - {
70   - srcCharset = StandardCharsets.UTF_8;
71   - }
72   -
73   - if (StringUtils.isEmpty(source) || srcCharset.equals(destCharset))
74   - {
75   - return source;
76   - }
77   - return new String(source.getBytes(srcCharset), destCharset);
78   - }
79   -
80   - /**
81   - * @return 系统字符集编码
82   - */
83   - public static String systemCharset()
84   - {
85   - return Charset.defaultCharset().name();
86   - }
87   -}
  1 +package com.trash.common.core.text;
  2 +
  3 +import java.nio.charset.Charset;
  4 +import java.nio.charset.StandardCharsets;
  5 +
  6 +import com.trash.common.utils.StringUtils;
  7 +
  8 +/**
  9 + * 字符集工具类
  10 + *
  11 + * @author trash
  12 + */
  13 +public class CharsetKit
  14 +{
  15 + /** ISO-8859-1 */
  16 + public static final String ISO_8859_1 = "ISO-8859-1";
  17 + /** UTF-8 */
  18 + public static final String UTF_8 = "UTF-8";
  19 + /** GBK */
  20 + public static final String GBK = "GBK";
  21 +
  22 + /** ISO-8859-1 */
  23 + public static final Charset CHARSET_ISO_8859_1 = StandardCharsets.ISO_8859_1;
  24 + /** UTF-8 */
  25 + public static final Charset CHARSET_UTF_8 = StandardCharsets.UTF_8;
  26 + /** GBK */
  27 + public static final Charset CHARSET_GBK = Charset.forName(GBK);
  28 +
  29 + /**
  30 + * 转换为Charset对象
  31 + *
  32 + * @param charset 字符集,为空则返回默认字符集
  33 + * @return Charset
  34 + */
  35 + public static Charset charset(String charset)
  36 + {
  37 + return StringUtils.isEmpty(charset) ? Charset.defaultCharset() : Charset.forName(charset);
  38 + }
  39 +
  40 + /**
  41 + * 转换字符串的字符集编码
  42 + *
  43 + * @param source 字符串
  44 + * @param srcCharset 源字符集,默认ISO-8859-1
  45 + * @param destCharset 目标字符集,默认UTF-8
  46 + * @return 转换后的字符集
  47 + */
  48 + public static String convert(String source, String srcCharset, String destCharset)
  49 + {
  50 + return convert(source, Charset.forName(srcCharset), Charset.forName(destCharset));
  51 + }
  52 +
  53 + /**
  54 + * 转换字符串的字符集编码
  55 + *
  56 + * @param source 字符串
  57 + * @param srcCharset 源字符集,默认ISO-8859-1
  58 + * @param destCharset 目标字符集,默认UTF-8
  59 + * @return 转换后的字符集
  60 + */
  61 + public static String convert(String source, Charset srcCharset, Charset destCharset)
  62 + {
  63 + if (null == srcCharset)
  64 + {
  65 + srcCharset = StandardCharsets.ISO_8859_1;
  66 + }
  67 +
  68 + if (null == destCharset)
  69 + {
  70 + srcCharset = StandardCharsets.UTF_8;
  71 + }
  72 +
  73 + if (StringUtils.isEmpty(source) || srcCharset.equals(destCharset))
  74 + {
  75 + return source;
  76 + }
  77 + return new String(source.getBytes(srcCharset), destCharset);
  78 + }
  79 +
  80 + /**
  81 + * @return 系统字符集编码
  82 + */
  83 + public static String systemCharset()
  84 + {
  85 + return Charset.defaultCharset().name();
  86 + }
  87 +}
... ...
trash-common/src/main/java/com/trash/common/core/text/Convert.java
1   -package com.trash.common.core.text;
2   -
3   -import java.math.BigDecimal;
4   -import java.math.BigInteger;
5   -import java.nio.ByteBuffer;
6   -import java.nio.charset.Charset;
7   -import java.text.NumberFormat;
8   -import java.util.Set;
9   -
10   -import com.trash.common.utils.StringUtils;
11   -
12   -/**
13   - * 类型转换器
14   - *
15   - * @author trash
16   - */
17   -public class Convert
18   -{
19   - /**
20   - * 转换为字符串<br>
21   - * 如果给定的值为null,或者转换失败,返回默认值<br>
22   - * 转换失败不会报错
23   - *
24   - * @param value 被转换的值
25   - * @param defaultValue 转换错误时的默认值
26   - * @return 结果
27   - */
28   - public static String toStr(Object value, String defaultValue)
29   - {
30   - if (null == value)
31   - {
32   - return defaultValue;
33   - }
34   - if (value instanceof String)
35   - {
36   - return (String) value;
37   - }
38   - return value.toString();
39   - }
40   -
41   - /**
42   - * 转换为字符串<br>
43   - * 如果给定的值为<code>null</code>,或者转换失败,返回默认值<code>null</code><br>
44   - * 转换失败不会报错
45   - *
46   - * @param value 被转换的值
47   - * @return 结果
48   - */
49   - public static String toStr(Object value)
50   - {
51   - return toStr(value, null);
52   - }
53   -
54   - /**
55   - * 转换为字符<br>
56   - * 如果给定的值为null,或者转换失败,返回默认值<br>
57   - * 转换失败不会报错
58   - *
59   - * @param value 被转换的值
60   - * @param defaultValue 转换错误时的默认值
61   - * @return 结果
62   - */
63   - public static Character toChar(Object value, Character defaultValue)
64   - {
65   - if (null == value)
66   - {
67   - return defaultValue;
68   - }
69   - if (value instanceof Character)
70   - {
71   - return (Character) value;
72   - }
73   -
74   - final String valueStr = toStr(value, null);
75   - return StringUtils.isEmpty(valueStr) ? defaultValue : valueStr.charAt(0);
76   - }
77   -
78   - /**
79   - * 转换为字符<br>
80   - * 如果给定的值为<code>null</code>,或者转换失败,返回默认值<code>null</code><br>
81   - * 转换失败不会报错
82   - *
83   - * @param value 被转换的值
84   - * @return 结果
85   - */
86   - public static Character toChar(Object value)
87   - {
88   - return toChar(value, null);
89   - }
90   -
91   - /**
92   - * 转换为byte<br>
93   - * 如果给定的值为<code>null</code>,或者转换失败,返回默认值<br>
94   - * 转换失败不会报错
95   - *
96   - * @param value 被转换的值
97   - * @param defaultValue 转换错误时的默认值
98   - * @return 结果
99   - */
100   - public static Byte toByte(Object value, Byte defaultValue)
101   - {
102   - if (value == null)
103   - {
104   - return defaultValue;
105   - }
106   - if (value instanceof Byte)
107   - {
108   - return (Byte) value;
109   - }
110   - if (value instanceof Number)
111   - {
112   - return ((Number) value).byteValue();
113   - }
114   - final String valueStr = toStr(value, null);
115   - if (StringUtils.isEmpty(valueStr))
116   - {
117   - return defaultValue;
118   - }
119   - try
120   - {
121   - return Byte.parseByte(valueStr);
122   - }
123   - catch (Exception e)
124   - {
125   - return defaultValue;
126   - }
127   - }
128   -
129   - /**
130   - * 转换为byte<br>
131   - * 如果给定的值为<code>null</code>,或者转换失败,返回默认值<code>null</code><br>
132   - * 转换失败不会报错
133   - *
134   - * @param value 被转换的值
135   - * @return 结果
136   - */
137   - public static Byte toByte(Object value)
138   - {
139   - return toByte(value, null);
140   - }
141   -
142   - /**
143   - * 转换为Short<br>
144   - * 如果给定的值为<code>null</code>,或者转换失败,返回默认值<br>
145   - * 转换失败不会报错
146   - *
147   - * @param value 被转换的值
148   - * @param defaultValue 转换错误时的默认值
149   - * @return 结果
150   - */
151   - public static Short toShort(Object value, Short defaultValue)
152   - {
153   - if (value == null)
154   - {
155   - return defaultValue;
156   - }
157   - if (value instanceof Short)
158   - {
159   - return (Short) value;
160   - }
161   - if (value instanceof Number)
162   - {
163   - return ((Number) value).shortValue();
164   - }
165   - final String valueStr = toStr(value, null);
166   - if (StringUtils.isEmpty(valueStr))
167   - {
168   - return defaultValue;
169   - }
170   - try
171   - {
172   - return Short.parseShort(valueStr.trim());
173   - }
174   - catch (Exception e)
175   - {
176   - return defaultValue;
177   - }
178   - }
179   -
180   - /**
181   - * 转换为Short<br>
182   - * 如果给定的值为<code>null</code>,或者转换失败,返回默认值<code>null</code><br>
183   - * 转换失败不会报错
184   - *
185   - * @param value 被转换的值
186   - * @return 结果
187   - */
188   - public static Short toShort(Object value)
189   - {
190   - return toShort(value, null);
191   - }
192   -
193   - /**
194   - * 转换为Number<br>
195   - * 如果给定的值为空,或者转换失败,返回默认值<br>
196   - * 转换失败不会报错
197   - *
198   - * @param value 被转换的值
199   - * @param defaultValue 转换错误时的默认值
200   - * @return 结果
201   - */
202   - public static Number toNumber(Object value, Number defaultValue)
203   - {
204   - if (value == null)
205   - {
206   - return defaultValue;
207   - }
208   - if (value instanceof Number)
209   - {
210   - return (Number) value;
211   - }
212   - final String valueStr = toStr(value, null);
213   - if (StringUtils.isEmpty(valueStr))
214   - {
215   - return defaultValue;
216   - }
217   - try
218   - {
219   - return NumberFormat.getInstance().parse(valueStr);
220   - }
221   - catch (Exception e)
222   - {
223   - return defaultValue;
224   - }
225   - }
226   -
227   - /**
228   - * 转换为Number<br>
229   - * 如果给定的值为空,或者转换失败,返回默认值<code>null</code><br>
230   - * 转换失败不会报错
231   - *
232   - * @param value 被转换的值
233   - * @return 结果
234   - */
235   - public static Number toNumber(Object value)
236   - {
237   - return toNumber(value, null);
238   - }
239   -
240   - /**
241   - * 转换为int<br>
242   - * 如果给定的值为空,或者转换失败,返回默认值<br>
243   - * 转换失败不会报错
244   - *
245   - * @param value 被转换的值
246   - * @param defaultValue 转换错误时的默认值
247   - * @return 结果
248   - */
249   - public static Integer toInt(Object value, Integer defaultValue)
250   - {
251   - if (value == null)
252   - {
253   - return defaultValue;
254   - }
255   - if (value instanceof Integer)
256   - {
257   - return (Integer) value;
258   - }
259   - if (value instanceof Number)
260   - {
261   - return ((Number) value).intValue();
262   - }
263   - final String valueStr = toStr(value, null);
264   - if (StringUtils.isEmpty(valueStr))
265   - {
266   - return defaultValue;
267   - }
268   - try
269   - {
270   - return Integer.parseInt(valueStr.trim());
271   - }
272   - catch (Exception e)
273   - {
274   - return defaultValue;
275   - }
276   - }
277   -
278   - /**
279   - * 转换为int<br>
280   - * 如果给定的值为<code>null</code>,或者转换失败,返回默认值<code>null</code><br>
281   - * 转换失败不会报错
282   - *
283   - * @param value 被转换的值
284   - * @return 结果
285   - */
286   - public static Integer toInt(Object value)
287   - {
288   - return toInt(value, null);
289   - }
290   -
291   - /**
292   - * 转换为Integer数组<br>
293   - *
294   - * @param str 被转换的值
295   - * @return 结果
296   - */
297   - public static Integer[] toIntArray(String str)
298   - {
299   - return toIntArray(",", str);
300   - }
301   -
302   - /**
303   - * 转换为Long数组<br>
304   - *
305   - * @param str 被转换的值
306   - * @return 结果
307   - */
308   - public static Long[] toLongArray(String str)
309   - {
310   - return toLongArray(",", str);
311   - }
312   -
313   - /**
314   - * 转换为Integer数组<br>
315   - *
316   - * @param split 分隔符
317   - * @param split 被转换的值
318   - * @return 结果
319   - */
320   - public static Integer[] toIntArray(String split, String str)
321   - {
322   - if (StringUtils.isEmpty(str))
323   - {
324   - return new Integer[] {};
325   - }
326   - String[] arr = str.split(split);
327   - final Integer[] ints = new Integer[arr.length];
328   - for (int i = 0; i < arr.length; i++)
329   - {
330   - final Integer v = toInt(arr[i], 0);
331   - ints[i] = v;
332   - }
333   - return ints;
334   - }
335   -
336   - /**
337   - * 转换为Long数组<br>
338   - *
339   - * @param split 分隔符
340   - * @param str 被转换的值
341   - * @return 结果
342   - */
343   - public static Long[] toLongArray(String split, String str)
344   - {
345   - if (StringUtils.isEmpty(str))
346   - {
347   - return new Long[] {};
348   - }
349   - String[] arr = str.split(split);
350   - final Long[] longs = new Long[arr.length];
351   - for (int i = 0; i < arr.length; i++)
352   - {
353   - final Long v = toLong(arr[i], null);
354   - longs[i] = v;
355   - }
356   - return longs;
357   - }
358   -
359   - /**
360   - * 转换为String数组<br>
361   - *
362   - * @param str 被转换的值
363   - * @return 结果
364   - */
365   - public static String[] toStrArray(String str)
366   - {
367   - return toStrArray(",", str);
368   - }
369   -
370   - /**
371   - * 转换为String数组<br>
372   - *
373   - * @param split 分隔符
374   - * @param split 被转换的值
375   - * @return 结果
376   - */
377   - public static String[] toStrArray(String split, String str)
378   - {
379   - return str.split(split);
380   - }
381   -
382   - /**
383   - * 转换为long<br>
384   - * 如果给定的值为空,或者转换失败,返回默认值<br>
385   - * 转换失败不会报错
386   - *
387   - * @param value 被转换的值
388   - * @param defaultValue 转换错误时的默认值
389   - * @return 结果
390   - */
391   - public static Long toLong(Object value, Long defaultValue)
392   - {
393   - if (value == null)
394   - {
395   - return defaultValue;
396   - }
397   - if (value instanceof Long)
398   - {
399   - return (Long) value;
400   - }
401   - if (value instanceof Number)
402   - {
403   - return ((Number) value).longValue();
404   - }
405   - final String valueStr = toStr(value, null);
406   - if (StringUtils.isEmpty(valueStr))
407   - {
408   - return defaultValue;
409   - }
410   - try
411   - {
412   - // 支持科学计数法
413   - return new BigDecimal(valueStr.trim()).longValue();
414   - }
415   - catch (Exception e)
416   - {
417   - return defaultValue;
418   - }
419   - }
420   -
421   - /**
422   - * 转换为long<br>
423   - * 如果给定的值为<code>null</code>,或者转换失败,返回默认值<code>null</code><br>
424   - * 转换失败不会报错
425   - *
426   - * @param value 被转换的值
427   - * @return 结果
428   - */
429   - public static Long toLong(Object value)
430   - {
431   - return toLong(value, null);
432   - }
433   -
434   - /**
435   - * 转换为double<br>
436   - * 如果给定的值为空,或者转换失败,返回默认值<br>
437   - * 转换失败不会报错
438   - *
439   - * @param value 被转换的值
440   - * @param defaultValue 转换错误时的默认值
441   - * @return 结果
442   - */
443   - public static Double toDouble(Object value, Double defaultValue)
444   - {
445   - if (value == null)
446   - {
447   - return defaultValue;
448   - }
449   - if (value instanceof Double)
450   - {
451   - return (Double) value;
452   - }
453   - if (value instanceof Number)
454   - {
455   - return ((Number) value).doubleValue();
456   - }
457   - final String valueStr = toStr(value, null);
458   - if (StringUtils.isEmpty(valueStr))
459   - {
460   - return defaultValue;
461   - }
462   - try
463   - {
464   - // 支持科学计数法
465   - return new BigDecimal(valueStr.trim()).doubleValue();
466   - }
467   - catch (Exception e)
468   - {
469   - return defaultValue;
470   - }
471   - }
472   -
473   - /**
474   - * 转换为double<br>
475   - * 如果给定的值为空,或者转换失败,返回默认值<code>null</code><br>
476   - * 转换失败不会报错
477   - *
478   - * @param value 被转换的值
479   - * @return 结果
480   - */
481   - public static Double toDouble(Object value)
482   - {
483   - return toDouble(value, null);
484   - }
485   -
486   - /**
487   - * 转换为Float<br>
488   - * 如果给定的值为空,或者转换失败,返回默认值<br>
489   - * 转换失败不会报错
490   - *
491   - * @param value 被转换的值
492   - * @param defaultValue 转换错误时的默认值
493   - * @return 结果
494   - */
495   - public static Float toFloat(Object value, Float defaultValue)
496   - {
497   - if (value == null)
498   - {
499   - return defaultValue;
500   - }
501   - if (value instanceof Float)
502   - {
503   - return (Float) value;
504   - }
505   - if (value instanceof Number)
506   - {
507   - return ((Number) value).floatValue();
508   - }
509   - final String valueStr = toStr(value, null);
510   - if (StringUtils.isEmpty(valueStr))
511   - {
512   - return defaultValue;
513   - }
514   - try
515   - {
516   - return Float.parseFloat(valueStr.trim());
517   - }
518   - catch (Exception e)
519   - {
520   - return defaultValue;
521   - }
522   - }
523   -
524   - /**
525   - * 转换为Float<br>
526   - * 如果给定的值为空,或者转换失败,返回默认值<code>null</code><br>
527   - * 转换失败不会报错
528   - *
529   - * @param value 被转换的值
530   - * @return 结果
531   - */
532   - public static Float toFloat(Object value)
533   - {
534   - return toFloat(value, null);
535   - }
536   -
537   - /**
538   - * 转换为boolean<br>
539   - * String支持的值为:true、false、yes、ok、no,1,0 如果给定的值为空,或者转换失败,返回默认值<br>
540   - * 转换失败不会报错
541   - *
542   - * @param value 被转换的值
543   - * @param defaultValue 转换错误时的默认值
544   - * @return 结果
545   - */
546   - public static Boolean toBool(Object value, Boolean defaultValue)
547   - {
548   - if (value == null)
549   - {
550   - return defaultValue;
551   - }
552   - if (value instanceof Boolean)
553   - {
554   - return (Boolean) value;
555   - }
556   - String valueStr = toStr(value, null);
557   - if (StringUtils.isEmpty(valueStr))
558   - {
559   - return defaultValue;
560   - }
561   - valueStr = valueStr.trim().toLowerCase();
562   - switch (valueStr)
563   - {
564   - case "true":
565   - return true;
566   - case "false":
567   - return false;
568   - case "yes":
569   - return true;
570   - case "ok":
571   - return true;
572   - case "no":
573   - return false;
574   - case "1":
575   - return true;
576   - case "0":
577   - return false;
578   - default:
579   - return defaultValue;
580   - }
581   - }
582   -
583   - /**
584   - * 转换为boolean<br>
585   - * 如果给定的值为空,或者转换失败,返回默认值<code>null</code><br>
586   - * 转换失败不会报错
587   - *
588   - * @param value 被转换的值
589   - * @return 结果
590   - */
591   - public static Boolean toBool(Object value)
592   - {
593   - return toBool(value, null);
594   - }
595   -
596   - /**
597   - * 转换为Enum对象<br>
598   - * 如果给定的值为空,或者转换失败,返回默认值<br>
599   - *
600   - * @param clazz Enum的Class
601   - * @param value 值
602   - * @param defaultValue 默认值
603   - * @return Enum
604   - */
605   - public static <E extends Enum<E>> E toEnum(Class<E> clazz, Object value, E defaultValue)
606   - {
607   - if (value == null)
608   - {
609   - return defaultValue;
610   - }
611   - if (clazz.isAssignableFrom(value.getClass()))
612   - {
613   - @SuppressWarnings("unchecked")
614   - E myE = (E) value;
615   - return myE;
616   - }
617   - final String valueStr = toStr(value, null);
618   - if (StringUtils.isEmpty(valueStr))
619   - {
620   - return defaultValue;
621   - }
622   - try
623   - {
624   - return Enum.valueOf(clazz, valueStr);
625   - }
626   - catch (Exception e)
627   - {
628   - return defaultValue;
629   - }
630   - }
631   -
632   - /**
633   - * 转换为Enum对象<br>
634   - * 如果给定的值为空,或者转换失败,返回默认值<code>null</code><br>
635   - *
636   - * @param clazz Enum的Class
637   - * @param value 值
638   - * @return Enum
639   - */
640   - public static <E extends Enum<E>> E toEnum(Class<E> clazz, Object value)
641   - {
642   - return toEnum(clazz, value, null);
643   - }
644   -
645   - /**
646   - * 转换为BigInteger<br>
647   - * 如果给定的值为空,或者转换失败,返回默认值<br>
648   - * 转换失败不会报错
649   - *
650   - * @param value 被转换的值
651   - * @param defaultValue 转换错误时的默认值
652   - * @return 结果
653   - */
654   - public static BigInteger toBigInteger(Object value, BigInteger defaultValue)
655   - {
656   - if (value == null)
657   - {
658   - return defaultValue;
659   - }
660   - if (value instanceof BigInteger)
661   - {
662   - return (BigInteger) value;
663   - }
664   - if (value instanceof Long)
665   - {
666   - return BigInteger.valueOf((Long) value);
667   - }
668   - final String valueStr = toStr(value, null);
669   - if (StringUtils.isEmpty(valueStr))
670   - {
671   - return defaultValue;
672   - }
673   - try
674   - {
675   - return new BigInteger(valueStr);
676   - }
677   - catch (Exception e)
678   - {
679   - return defaultValue;
680   - }
681   - }
682   -
683   - /**
684   - * 转换为BigInteger<br>
685   - * 如果给定的值为空,或者转换失败,返回默认值<code>null</code><br>
686   - * 转换失败不会报错
687   - *
688   - * @param value 被转换的值
689   - * @return 结果
690   - */
691   - public static BigInteger toBigInteger(Object value)
692   - {
693   - return toBigInteger(value, null);
694   - }
695   -
696   - /**
697   - * 转换为BigDecimal<br>
698   - * 如果给定的值为空,或者转换失败,返回默认值<br>
699   - * 转换失败不会报错
700   - *
701   - * @param value 被转换的值
702   - * @param defaultValue 转换错误时的默认值
703   - * @return 结果
704   - */
705   - public static BigDecimal toBigDecimal(Object value, BigDecimal defaultValue)
706   - {
707   - if (value == null)
708   - {
709   - return defaultValue;
710   - }
711   - if (value instanceof BigDecimal)
712   - {
713   - return (BigDecimal) value;
714   - }
715   - if (value instanceof Long)
716   - {
717   - return new BigDecimal((Long) value);
718   - }
719   - if (value instanceof Double)
720   - {
721   - return new BigDecimal((Double) value);
722   - }
723   - if (value instanceof Integer)
724   - {
725   - return new BigDecimal((Integer) value);
726   - }
727   - final String valueStr = toStr(value, null);
728   - if (StringUtils.isEmpty(valueStr))
729   - {
730   - return defaultValue;
731   - }
732   - try
733   - {
734   - return new BigDecimal(valueStr);
735   - }
736   - catch (Exception e)
737   - {
738   - return defaultValue;
739   - }
740   - }
741   -
742   - /**
743   - * 转换为BigDecimal<br>
744   - * 如果给定的值为空,或者转换失败,返回默认值<br>
745   - * 转换失败不会报错
746   - *
747   - * @param value 被转换的值
748   - * @return 结果
749   - */
750   - public static BigDecimal toBigDecimal(Object value)
751   - {
752   - return toBigDecimal(value, null);
753   - }
754   -
755   - /**
756   - * 将对象转为字符串<br>
757   - * 1、Byte数组和ByteBuffer会被转换为对应字符串的数组 2、对象数组会调用Arrays.toString方法
758   - *
759   - * @param obj 对象
760   - * @return 字符串
761   - */
762   - public static String utf8Str(Object obj)
763   - {
764   - return str(obj, CharsetKit.CHARSET_UTF_8);
765   - }
766   -
767   - /**
768   - * 将对象转为字符串<br>
769   - * 1、Byte数组和ByteBuffer会被转换为对应字符串的数组 2、对象数组会调用Arrays.toString方法
770   - *
771   - * @param obj 对象
772   - * @param charsetName 字符集
773   - * @return 字符串
774   - */
775   - public static String str(Object obj, String charsetName)
776   - {
777   - return str(obj, Charset.forName(charsetName));
778   - }
779   -
780   - /**
781   - * 将对象转为字符串<br>
782   - * 1、Byte数组和ByteBuffer会被转换为对应字符串的数组 2、对象数组会调用Arrays.toString方法
783   - *
784   - * @param obj 对象
785   - * @param charset 字符集
786   - * @return 字符串
787   - */
788   - public static String str(Object obj, Charset charset)
789   - {
790   - if (null == obj)
791   - {
792   - return null;
793   - }
794   -
795   - if (obj instanceof String)
796   - {
797   - return (String) obj;
798   - }
799   - else if (obj instanceof byte[] || obj instanceof Byte[])
800   - {
801   - return str(obj, charset);
802   - }
803   - else if (obj instanceof ByteBuffer)
804   - {
805   - return str((ByteBuffer) obj, charset);
806   - }
807   - return obj.toString();
808   - }
809   -
810   - /**
811   - * 将byte数组转为字符串
812   - *
813   - * @param bytes byte数组
814   - * @param charset 字符集
815   - * @return 字符串
816   - */
817   - public static String str(byte[] bytes, String charset)
818   - {
819   - return str(bytes, StringUtils.isEmpty(charset) ? Charset.defaultCharset() : Charset.forName(charset));
820   - }
821   -
822   - /**
823   - * 解码字节码
824   - *
825   - * @param data 字符串
826   - * @param charset 字符集,如果此字段为空,则解码的结果取决于平台
827   - * @return 解码后的字符串
828   - */
829   - public static String str(byte[] data, Charset charset)
830   - {
831   - if (data == null)
832   - {
833   - return null;
834   - }
835   -
836   - if (null == charset)
837   - {
838   - return new String(data);
839   - }
840   - return new String(data, charset);
841   - }
842   -
843   - /**
844   - * 将编码的byteBuffer数据转换为字符串
845   - *
846   - * @param data 数据
847   - * @param charset 字符集,如果为空使用当前系统字符集
848   - * @return 字符串
849   - */
850   - public static String str(ByteBuffer data, String charset)
851   - {
852   - if (data == null)
853   - {
854   - return null;
855   - }
856   -
857   - return str(data, Charset.forName(charset));
858   - }
859   -
860   - /**
861   - * 将编码的byteBuffer数据转换为字符串
862   - *
863   - * @param data 数据
864   - * @param charset 字符集,如果为空使用当前系统字符集
865   - * @return 字符串
866   - */
867   - public static String str(ByteBuffer data, Charset charset)
868   - {
869   - if (null == charset)
870   - {
871   - charset = Charset.defaultCharset();
872   - }
873   - return charset.decode(data).toString();
874   - }
875   -
876   - // ----------------------------------------------------------------------- 全角半角转换
877   - /**
878   - * 半角转全角
879   - *
880   - * @param input String.
881   - * @return 全角字符串.
882   - */
883   - public static String toSBC(String input)
884   - {
885   - return toSBC(input, null);
886   - }
887   -
888   - /**
889   - * 半角转全角
890   - *
891   - * @param input String
892   - * @param notConvertSet 不替换的字符集合
893   - * @return 全角字符串.
894   - */
895   - public static String toSBC(String input, Set<Character> notConvertSet)
896   - {
897   - char[] c = input.toCharArray();
898   - for (int i = 0; i < c.length; i++)
899   - {
900   - if (null != notConvertSet && notConvertSet.contains(c[i]))
901   - {
902   - // 跳过不替换的字符
903   - continue;
904   - }
905   -
906   - if (c[i] == ' ')
907   - {
908   - c[i] = '\u3000';
909   - }
910   - else if (c[i] < '\177')
911   - {
912   - c[i] = (char) (c[i] + 65248);
913   -
914   - }
915   - }
916   - return new String(c);
917   - }
918   -
919   - /**
920   - * 全角转半角
921   - *
922   - * @param input String.
923   - * @return 半角字符串
924   - */
925   - public static String toDBC(String input)
926   - {
927   - return toDBC(input, null);
928   - }
929   -
930   - /**
931   - * 替换全角为半角
932   - *
933   - * @param text 文本
934   - * @param notConvertSet 不替换的字符集合
935   - * @return 替换后的字符
936   - */
937   - public static String toDBC(String text, Set<Character> notConvertSet)
938   - {
939   - char[] c = text.toCharArray();
940   - for (int i = 0; i < c.length; i++)
941   - {
942   - if (null != notConvertSet && notConvertSet.contains(c[i]))
943   - {
944   - // 跳过不替换的字符
945   - continue;
946   - }
947   -
948   - if (c[i] == '\u3000')
949   - {
950   - c[i] = ' ';
951   - }
952   - else if (c[i] > '\uFF00' && c[i] < '\uFF5F')
953   - {
954   - c[i] = (char) (c[i] - 65248);
955   - }
956   - }
957   - String returnString = new String(c);
958   -
959   - return returnString;
960   - }
961   -
962   - /**
963   - * 数字金额大写转换 先写个完整的然后将如零拾替换成零
964   - *
965   - * @param n 数字
966   - * @return 中文大写数字
967   - */
968   - public static String digitUppercase(double n)
969   - {
970   - String[] fraction = { "角", "分" };
971   - String[] digit = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
972   - String[][] unit = { { "元", "万", "亿" }, { "", "拾", "佰", "仟" } };
973   -
974   - String head = n < 0 ? "负" : "";
975   - n = Math.abs(n);
976   -
977   - String s = "";
978   - for (int i = 0; i < fraction.length; i++)
979   - {
980   - s += (digit[(int) (Math.floor(n * 10 * Math.pow(10, i)) % 10)] + fraction[i]).replaceAll("(零.)+", "");
981   - }
982   - if (s.length() < 1)
983   - {
984   - s = "整";
985   - }
986   - int integerPart = (int) Math.floor(n);
987   -
988   - for (int i = 0; i < unit[0].length && integerPart > 0; i++)
989   - {
990   - String p = "";
991   - for (int j = 0; j < unit[1].length && n > 0; j++)
992   - {
993   - p = digit[integerPart % 10] + unit[1][j] + p;
994   - integerPart = integerPart / 10;
995   - }
996   - s = p.replaceAll("(零.)*零$", "").replaceAll("^$", "零") + unit[0][i] + s;
997   - }
998   - return head + s.replaceAll("(零.)*零元", "元").replaceFirst("(零.)+", "").replaceAll("(零.)+", "零").replaceAll("^整$", "零元整");
999   - }
1000   -}
  1 +package com.trash.common.core.text;
  2 +
  3 +import java.math.BigDecimal;
  4 +import java.math.BigInteger;
  5 +import java.nio.ByteBuffer;
  6 +import java.nio.charset.Charset;
  7 +import java.text.NumberFormat;
  8 +import java.util.Set;
  9 +
  10 +import com.trash.common.utils.StringUtils;
  11 +
  12 +/**
  13 + * 类型转换器
  14 + *
  15 + * @author trash
  16 + */
  17 +public class Convert
  18 +{
  19 + /**
  20 + * 转换为字符串<br>
  21 + * 如果给定的值为null,或者转换失败,返回默认值<br>
  22 + * 转换失败不会报错
  23 + *
  24 + * @param value 被转换的值
  25 + * @param defaultValue 转换错误时的默认值
  26 + * @return 结果
  27 + */
  28 + public static String toStr(Object value, String defaultValue)
  29 + {
  30 + if (null == value)
  31 + {
  32 + return defaultValue;
  33 + }
  34 + if (value instanceof String)
  35 + {
  36 + return (String) value;
  37 + }
  38 + return value.toString();
  39 + }
  40 +
  41 + /**
  42 + * 转换为字符串<br>
  43 + * 如果给定的值为<code>null</code>,或者转换失败,返回默认值<code>null</code><br>
  44 + * 转换失败不会报错
  45 + *
  46 + * @param value 被转换的值
  47 + * @return 结果
  48 + */
  49 + public static String toStr(Object value)
  50 + {
  51 + return toStr(value, null);
  52 + }
  53 +
  54 + /**
  55 + * 转换为字符<br>
  56 + * 如果给定的值为null,或者转换失败,返回默认值<br>
  57 + * 转换失败不会报错
  58 + *
  59 + * @param value 被转换的值
  60 + * @param defaultValue 转换错误时的默认值
  61 + * @return 结果
  62 + */
  63 + public static Character toChar(Object value, Character defaultValue)
  64 + {
  65 + if (null == value)
  66 + {
  67 + return defaultValue;
  68 + }
  69 + if (value instanceof Character)
  70 + {
  71 + return (Character) value;
  72 + }
  73 +
  74 + final String valueStr = toStr(value, null);
  75 + return StringUtils.isEmpty(valueStr) ? defaultValue : valueStr.charAt(0);
  76 + }
  77 +
  78 + /**
  79 + * 转换为字符<br>
  80 + * 如果给定的值为<code>null</code>,或者转换失败,返回默认值<code>null</code><br>
  81 + * 转换失败不会报错
  82 + *
  83 + * @param value 被转换的值
  84 + * @return 结果
  85 + */
  86 + public static Character toChar(Object value)
  87 + {
  88 + return toChar(value, null);
  89 + }
  90 +
  91 + /**
  92 + * 转换为byte<br>
  93 + * 如果给定的值为<code>null</code>,或者转换失败,返回默认值<br>
  94 + * 转换失败不会报错
  95 + *
  96 + * @param value 被转换的值
  97 + * @param defaultValue 转换错误时的默认值
  98 + * @return 结果
  99 + */
  100 + public static Byte toByte(Object value, Byte defaultValue)
  101 + {
  102 + if (value == null)
  103 + {
  104 + return defaultValue;
  105 + }
  106 + if (value instanceof Byte)
  107 + {
  108 + return (Byte) value;
  109 + }
  110 + if (value instanceof Number)
  111 + {
  112 + return ((Number) value).byteValue();
  113 + }
  114 + final String valueStr = toStr(value, null);
  115 + if (StringUtils.isEmpty(valueStr))
  116 + {
  117 + return defaultValue;
  118 + }
  119 + try
  120 + {
  121 + return Byte.parseByte(valueStr);
  122 + }
  123 + catch (Exception e)
  124 + {
  125 + return defaultValue;
  126 + }
  127 + }
  128 +
  129 + /**
  130 + * 转换为byte<br>
  131 + * 如果给定的值为<code>null</code>,或者转换失败,返回默认值<code>null</code><br>
  132 + * 转换失败不会报错
  133 + *
  134 + * @param value 被转换的值
  135 + * @return 结果
  136 + */
  137 + public static Byte toByte(Object value)
  138 + {
  139 + return toByte(value, null);
  140 + }
  141 +
  142 + /**
  143 + * 转换为Short<br>
  144 + * 如果给定的值为<code>null</code>,或者转换失败,返回默认值<br>
  145 + * 转换失败不会报错
  146 + *
  147 + * @param value 被转换的值
  148 + * @param defaultValue 转换错误时的默认值
  149 + * @return 结果
  150 + */
  151 + public static Short toShort(Object value, Short defaultValue)
  152 + {
  153 + if (value == null)
  154 + {
  155 + return defaultValue;
  156 + }
  157 + if (value instanceof Short)
  158 + {
  159 + return (Short) value;
  160 + }
  161 + if (value instanceof Number)
  162 + {
  163 + return ((Number) value).shortValue();
  164 + }
  165 + final String valueStr = toStr(value, null);
  166 + if (StringUtils.isEmpty(valueStr))
  167 + {
  168 + return defaultValue;
  169 + }
  170 + try
  171 + {
  172 + return Short.parseShort(valueStr.trim());
  173 + }
  174 + catch (Exception e)
  175 + {
  176 + return defaultValue;
  177 + }
  178 + }
  179 +
  180 + /**
  181 + * 转换为Short<br>
  182 + * 如果给定的值为<code>null</code>,或者转换失败,返回默认值<code>null</code><br>
  183 + * 转换失败不会报错
  184 + *
  185 + * @param value 被转换的值
  186 + * @return 结果
  187 + */
  188 + public static Short toShort(Object value)
  189 + {
  190 + return toShort(value, null);
  191 + }
  192 +
  193 + /**
  194 + * 转换为Number<br>
  195 + * 如果给定的值为空,或者转换失败,返回默认值<br>
  196 + * 转换失败不会报错
  197 + *
  198 + * @param value 被转换的值
  199 + * @param defaultValue 转换错误时的默认值
  200 + * @return 结果
  201 + */
  202 + public static Number toNumber(Object value, Number defaultValue)
  203 + {
  204 + if (value == null)
  205 + {
  206 + return defaultValue;
  207 + }
  208 + if (value instanceof Number)
  209 + {
  210 + return (Number) value;
  211 + }
  212 + final String valueStr = toStr(value, null);
  213 + if (StringUtils.isEmpty(valueStr))
  214 + {
  215 + return defaultValue;
  216 + }
  217 + try
  218 + {
  219 + return NumberFormat.getInstance().parse(valueStr);
  220 + }
  221 + catch (Exception e)
  222 + {
  223 + return defaultValue;
  224 + }
  225 + }
  226 +
  227 + /**
  228 + * 转换为Number<br>
  229 + * 如果给定的值为空,或者转换失败,返回默认值<code>null</code><br>
  230 + * 转换失败不会报错
  231 + *
  232 + * @param value 被转换的值
  233 + * @return 结果
  234 + */
  235 + public static Number toNumber(Object value)
  236 + {
  237 + return toNumber(value, null);
  238 + }
  239 +
  240 + /**
  241 + * 转换为int<br>
  242 + * 如果给定的值为空,或者转换失败,返回默认值<br>
  243 + * 转换失败不会报错
  244 + *
  245 + * @param value 被转换的值
  246 + * @param defaultValue 转换错误时的默认值
  247 + * @return 结果
  248 + */
  249 + public static Integer toInt(Object value, Integer defaultValue)
  250 + {
  251 + if (value == null)
  252 + {
  253 + return defaultValue;
  254 + }
  255 + if (value instanceof Integer)
  256 + {
  257 + return (Integer) value;
  258 + }
  259 + if (value instanceof Number)
  260 + {
  261 + return ((Number) value).intValue();
  262 + }
  263 + final String valueStr = toStr(value, null);
  264 + if (StringUtils.isEmpty(valueStr))
  265 + {
  266 + return defaultValue;
  267 + }
  268 + try
  269 + {
  270 + return Integer.parseInt(valueStr.trim());
  271 + }
  272 + catch (Exception e)
  273 + {
  274 + return defaultValue;
  275 + }
  276 + }
  277 +
  278 + /**
  279 + * 转换为int<br>
  280 + * 如果给定的值为<code>null</code>,或者转换失败,返回默认值<code>null</code><br>
  281 + * 转换失败不会报错
  282 + *
  283 + * @param value 被转换的值
  284 + * @return 结果
  285 + */
  286 + public static Integer toInt(Object value)
  287 + {
  288 + return toInt(value, null);
  289 + }
  290 +
  291 + /**
  292 + * 转换为Integer数组<br>
  293 + *
  294 + * @param str 被转换的值
  295 + * @return 结果
  296 + */
  297 + public static Integer[] toIntArray(String str)
  298 + {
  299 + return toIntArray(",", str);
  300 + }
  301 +
  302 + /**
  303 + * 转换为Long数组<br>
  304 + *
  305 + * @param str 被转换的值
  306 + * @return 结果
  307 + */
  308 + public static Long[] toLongArray(String str)
  309 + {
  310 + return toLongArray(",", str);
  311 + }
  312 +
  313 + /**
  314 + * 转换为Integer数组<br>
  315 + *
  316 + * @param split 分隔符
  317 + * @param split 被转换的值
  318 + * @return 结果
  319 + */
  320 + public static Integer[] toIntArray(String split, String str)
  321 + {
  322 + if (StringUtils.isEmpty(str))
  323 + {
  324 + return new Integer[] {};
  325 + }
  326 + String[] arr = str.split(split);
  327 + final Integer[] ints = new Integer[arr.length];
  328 + for (int i = 0; i < arr.length; i++)
  329 + {
  330 + final Integer v = toInt(arr[i], 0);
  331 + ints[i] = v;
  332 + }
  333 + return ints;
  334 + }
  335 +
  336 + /**
  337 + * 转换为Long数组<br>
  338 + *
  339 + * @param split 分隔符
  340 + * @param str 被转换的值
  341 + * @return 结果
  342 + */
  343 + public static Long[] toLongArray(String split, String str)
  344 + {
  345 + if (StringUtils.isEmpty(str))
  346 + {
  347 + return new Long[] {};
  348 + }
  349 + String[] arr = str.split(split);
  350 + final Long[] longs = new Long[arr.length];
  351 + for (int i = 0; i < arr.length; i++)
  352 + {
  353 + final Long v = toLong(arr[i], null);
  354 + longs[i] = v;
  355 + }
  356 + return longs;
  357 + }
  358 +
  359 + /**
  360 + * 转换为String数组<br>
  361 + *
  362 + * @param str 被转换的值
  363 + * @return 结果
  364 + */
  365 + public static String[] toStrArray(String str)
  366 + {
  367 + return toStrArray(",", str);
  368 + }
  369 +
  370 + /**
  371 + * 转换为String数组<br>
  372 + *
  373 + * @param split 分隔符
  374 + * @param split 被转换的值
  375 + * @return 结果
  376 + */
  377 + public static String[] toStrArray(String split, String str)
  378 + {
  379 + return str.split(split);
  380 + }
  381 +
  382 + /**
  383 + * 转换为long<br>
  384 + * 如果给定的值为空,或者转换失败,返回默认值<br>
  385 + * 转换失败不会报错
  386 + *
  387 + * @param value 被转换的值
  388 + * @param defaultValue 转换错误时的默认值
  389 + * @return 结果
  390 + */
  391 + public static Long toLong(Object value, Long defaultValue)
  392 + {
  393 + if (value == null)
  394 + {
  395 + return defaultValue;
  396 + }
  397 + if (value instanceof Long)
  398 + {
  399 + return (Long) value;
  400 + }
  401 + if (value instanceof Number)
  402 + {
  403 + return ((Number) value).longValue();
  404 + }
  405 + final String valueStr = toStr(value, null);
  406 + if (StringUtils.isEmpty(valueStr))
  407 + {
  408 + return defaultValue;
  409 + }
  410 + try
  411 + {
  412 + // 支持科学计数法
  413 + return new BigDecimal(valueStr.trim()).longValue();
  414 + }
  415 + catch (Exception e)
  416 + {
  417 + return defaultValue;
  418 + }
  419 + }
  420 +
  421 + /**
  422 + * 转换为long<br>
  423 + * 如果给定的值为<code>null</code>,或者转换失败,返回默认值<code>null</code><br>
  424 + * 转换失败不会报错
  425 + *
  426 + * @param value 被转换的值
  427 + * @return 结果
  428 + */
  429 + public static Long toLong(Object value)
  430 + {
  431 + return toLong(value, null);
  432 + }
  433 +
  434 + /**
  435 + * 转换为double<br>
  436 + * 如果给定的值为空,或者转换失败,返回默认值<br>
  437 + * 转换失败不会报错
  438 + *
  439 + * @param value 被转换的值
  440 + * @param defaultValue 转换错误时的默认值
  441 + * @return 结果
  442 + */
  443 + public static Double toDouble(Object value, Double defaultValue)
  444 + {
  445 + if (value == null)
  446 + {
  447 + return defaultValue;
  448 + }
  449 + if (value instanceof Double)
  450 + {
  451 + return (Double) value;
  452 + }
  453 + if (value instanceof Number)
  454 + {
  455 + return ((Number) value).doubleValue();
  456 + }
  457 + final String valueStr = toStr(value, null);
  458 + if (StringUtils.isEmpty(valueStr))
  459 + {
  460 + return defaultValue;
  461 + }
  462 + try
  463 + {
  464 + // 支持科学计数法
  465 + return new BigDecimal(valueStr.trim()).doubleValue();
  466 + }
  467 + catch (Exception e)
  468 + {
  469 + return defaultValue;
  470 + }
  471 + }
  472 +
  473 + /**
  474 + * 转换为double<br>
  475 + * 如果给定的值为空,或者转换失败,返回默认值<code>null</code><br>
  476 + * 转换失败不会报错
  477 + *
  478 + * @param value 被转换的值
  479 + * @return 结果
  480 + */
  481 + public static Double toDouble(Object value)
  482 + {
  483 + return toDouble(value, null);
  484 + }
  485 +
  486 + /**
  487 + * 转换为Float<br>
  488 + * 如果给定的值为空,或者转换失败,返回默认值<br>
  489 + * 转换失败不会报错
  490 + *
  491 + * @param value 被转换的值
  492 + * @param defaultValue 转换错误时的默认值
  493 + * @return 结果
  494 + */
  495 + public static Float toFloat(Object value, Float defaultValue)
  496 + {
  497 + if (value == null)
  498 + {
  499 + return defaultValue;
  500 + }
  501 + if (value instanceof Float)
  502 + {
  503 + return (Float) value;
  504 + }
  505 + if (value instanceof Number)
  506 + {
  507 + return ((Number) value).floatValue();
  508 + }
  509 + final String valueStr = toStr(value, null);
  510 + if (StringUtils.isEmpty(valueStr))
  511 + {
  512 + return defaultValue;
  513 + }
  514 + try
  515 + {
  516 + return Float.parseFloat(valueStr.trim());
  517 + }
  518 + catch (Exception e)
  519 + {
  520 + return defaultValue;
  521 + }
  522 + }
  523 +
  524 + /**
  525 + * 转换为Float<br>
  526 + * 如果给定的值为空,或者转换失败,返回默认值<code>null</code><br>
  527 + * 转换失败不会报错
  528 + *
  529 + * @param value 被转换的值
  530 + * @return 结果
  531 + */
  532 + public static Float toFloat(Object value)
  533 + {
  534 + return toFloat(value, null);
  535 + }
  536 +
  537 + /**
  538 + * 转换为boolean<br>
  539 + * String支持的值为:true、false、yes、ok、no,1,0 如果给定的值为空,或者转换失败,返回默认值<br>
  540 + * 转换失败不会报错
  541 + *
  542 + * @param value 被转换的值
  543 + * @param defaultValue 转换错误时的默认值
  544 + * @return 结果
  545 + */
  546 + public static Boolean toBool(Object value, Boolean defaultValue)
  547 + {
  548 + if (value == null)
  549 + {
  550 + return defaultValue;
  551 + }
  552 + if (value instanceof Boolean)
  553 + {
  554 + return (Boolean) value;
  555 + }
  556 + String valueStr = toStr(value, null);
  557 + if (StringUtils.isEmpty(valueStr))
  558 + {
  559 + return defaultValue;
  560 + }
  561 + valueStr = valueStr.trim().toLowerCase();
  562 + switch (valueStr)
  563 + {
  564 + case "true":
  565 + return true;
  566 + case "false":
  567 + return false;
  568 + case "yes":
  569 + return true;
  570 + case "ok":
  571 + return true;
  572 + case "no":
  573 + return false;
  574 + case "1":
  575 + return true;
  576 + case "0":
  577 + return false;
  578 + default:
  579 + return defaultValue;
  580 + }
  581 + }
  582 +
  583 + /**
  584 + * 转换为boolean<br>
  585 + * 如果给定的值为空,或者转换失败,返回默认值<code>null</code><br>
  586 + * 转换失败不会报错
  587 + *
  588 + * @param value 被转换的值
  589 + * @return 结果
  590 + */
  591 + public static Boolean toBool(Object value)
  592 + {
  593 + return toBool(value, null);
  594 + }
  595 +
  596 + /**
  597 + * 转换为Enum对象<br>
  598 + * 如果给定的值为空,或者转换失败,返回默认值<br>
  599 + *
  600 + * @param clazz Enum的Class
  601 + * @param value 值
  602 + * @param defaultValue 默认值
  603 + * @return Enum
  604 + */
  605 + public static <E extends Enum<E>> E toEnum(Class<E> clazz, Object value, E defaultValue)
  606 + {
  607 + if (value == null)
  608 + {
  609 + return defaultValue;
  610 + }
  611 + if (clazz.isAssignableFrom(value.getClass()))
  612 + {
  613 + @SuppressWarnings("unchecked")
  614 + E myE = (E) value;
  615 + return myE;
  616 + }
  617 + final String valueStr = toStr(value, null);
  618 + if (StringUtils.isEmpty(valueStr))
  619 + {
  620 + return defaultValue;
  621 + }
  622 + try
  623 + {
  624 + return Enum.valueOf(clazz, valueStr);
  625 + }
  626 + catch (Exception e)
  627 + {
  628 + return defaultValue;
  629 + }
  630 + }
  631 +
  632 + /**
  633 + * 转换为Enum对象<br>
  634 + * 如果给定的值为空,或者转换失败,返回默认值<code>null</code><br>
  635 + *
  636 + * @param clazz Enum的Class
  637 + * @param value 值
  638 + * @return Enum
  639 + */
  640 + public static <E extends Enum<E>> E toEnum(Class<E> clazz, Object value)
  641 + {
  642 + return toEnum(clazz, value, null);
  643 + }
  644 +
  645 + /**
  646 + * 转换为BigInteger<br>
  647 + * 如果给定的值为空,或者转换失败,返回默认值<br>
  648 + * 转换失败不会报错
  649 + *
  650 + * @param value 被转换的值
  651 + * @param defaultValue 转换错误时的默认值
  652 + * @return 结果
  653 + */
  654 + public static BigInteger toBigInteger(Object value, BigInteger defaultValue)
  655 + {
  656 + if (value == null)
  657 + {
  658 + return defaultValue;
  659 + }
  660 + if (value instanceof BigInteger)
  661 + {
  662 + return (BigInteger) value;
  663 + }
  664 + if (value instanceof Long)
  665 + {
  666 + return BigInteger.valueOf((Long) value);
  667 + }
  668 + final String valueStr = toStr(value, null);
  669 + if (StringUtils.isEmpty(valueStr))
  670 + {
  671 + return defaultValue;
  672 + }
  673 + try
  674 + {
  675 + return new BigInteger(valueStr);
  676 + }
  677 + catch (Exception e)
  678 + {
  679 + return defaultValue;
  680 + }
  681 + }
  682 +
  683 + /**
  684 + * 转换为BigInteger<br>
  685 + * 如果给定的值为空,或者转换失败,返回默认值<code>null</code><br>
  686 + * 转换失败不会报错
  687 + *
  688 + * @param value 被转换的值
  689 + * @return 结果
  690 + */
  691 + public static BigInteger toBigInteger(Object value)
  692 + {
  693 + return toBigInteger(value, null);
  694 + }
  695 +
  696 + /**
  697 + * 转换为BigDecimal<br>
  698 + * 如果给定的值为空,或者转换失败,返回默认值<br>
  699 + * 转换失败不会报错
  700 + *
  701 + * @param value 被转换的值
  702 + * @param defaultValue 转换错误时的默认值
  703 + * @return 结果
  704 + */
  705 + public static BigDecimal toBigDecimal(Object value, BigDecimal defaultValue)
  706 + {
  707 + if (value == null)
  708 + {
  709 + return defaultValue;
  710 + }
  711 + if (value instanceof BigDecimal)
  712 + {
  713 + return (BigDecimal) value;
  714 + }
  715 + if (value instanceof Long)
  716 + {
  717 + return new BigDecimal((Long) value);
  718 + }
  719 + if (value instanceof Double)
  720 + {
  721 + return new BigDecimal((Double) value);
  722 + }
  723 + if (value instanceof Integer)
  724 + {
  725 + return new BigDecimal((Integer) value);
  726 + }
  727 + final String valueStr = toStr(value, null);
  728 + if (StringUtils.isEmpty(valueStr))
  729 + {
  730 + return defaultValue;
  731 + }
  732 + try
  733 + {
  734 + return new BigDecimal(valueStr);
  735 + }
  736 + catch (Exception e)
  737 + {
  738 + return defaultValue;
  739 + }
  740 + }
  741 +
  742 + /**
  743 + * 转换为BigDecimal<br>
  744 + * 如果给定的值为空,或者转换失败,返回默认值<br>
  745 + * 转换失败不会报错
  746 + *
  747 + * @param value 被转换的值
  748 + * @return 结果
  749 + */
  750 + public static BigDecimal toBigDecimal(Object value)
  751 + {
  752 + return toBigDecimal(value, null);
  753 + }
  754 +
  755 + /**
  756 + * 将对象转为字符串<br>
  757 + * 1、Byte数组和ByteBuffer会被转换为对应字符串的数组 2、对象数组会调用Arrays.toString方法
  758 + *
  759 + * @param obj 对象
  760 + * @return 字符串
  761 + */
  762 + public static String utf8Str(Object obj)
  763 + {
  764 + return str(obj, CharsetKit.CHARSET_UTF_8);
  765 + }
  766 +
  767 + /**
  768 + * 将对象转为字符串<br>
  769 + * 1、Byte数组和ByteBuffer会被转换为对应字符串的数组 2、对象数组会调用Arrays.toString方法
  770 + *
  771 + * @param obj 对象
  772 + * @param charsetName 字符集
  773 + * @return 字符串
  774 + */
  775 + public static String str(Object obj, String charsetName)
  776 + {
  777 + return str(obj, Charset.forName(charsetName));
  778 + }
  779 +
  780 + /**
  781 + * 将对象转为字符串<br>
  782 + * 1、Byte数组和ByteBuffer会被转换为对应字符串的数组 2、对象数组会调用Arrays.toString方法
  783 + *
  784 + * @param obj 对象
  785 + * @param charset 字符集
  786 + * @return 字符串
  787 + */
  788 + public static String str(Object obj, Charset charset)
  789 + {
  790 + if (null == obj)
  791 + {
  792 + return null;
  793 + }
  794 +
  795 + if (obj instanceof String)
  796 + {
  797 + return (String) obj;
  798 + }
  799 + else if (obj instanceof byte[] || obj instanceof Byte[])
  800 + {
  801 + return str(obj, charset);
  802 + }
  803 + else if (obj instanceof ByteBuffer)
  804 + {
  805 + return str((ByteBuffer) obj, charset);
  806 + }
  807 + return obj.toString();
  808 + }
  809 +
  810 + /**
  811 + * 将byte数组转为字符串
  812 + *
  813 + * @param bytes byte数组
  814 + * @param charset 字符集
  815 + * @return 字符串
  816 + */
  817 + public static String str(byte[] bytes, String charset)
  818 + {
  819 + return str(bytes, StringUtils.isEmpty(charset) ? Charset.defaultCharset() : Charset.forName(charset));
  820 + }
  821 +
  822 + /**
  823 + * 解码字节码
  824 + *
  825 + * @param data 字符串
  826 + * @param charset 字符集,如果此字段为空,则解码的结果取决于平台
  827 + * @return 解码后的字符串
  828 + */
  829 + public static String str(byte[] data, Charset charset)
  830 + {
  831 + if (data == null)
  832 + {
  833 + return null;
  834 + }
  835 +
  836 + if (null == charset)
  837 + {
  838 + return new String(data);
  839 + }
  840 + return new String(data, charset);
  841 + }
  842 +
  843 + /**
  844 + * 将编码的byteBuffer数据转换为字符串
  845 + *
  846 + * @param data 数据
  847 + * @param charset 字符集,如果为空使用当前系统字符集
  848 + * @return 字符串
  849 + */
  850 + public static String str(ByteBuffer data, String charset)
  851 + {
  852 + if (data == null)
  853 + {
  854 + return null;
  855 + }
  856 +
  857 + return str(data, Charset.forName(charset));
  858 + }
  859 +
  860 + /**
  861 + * 将编码的byteBuffer数据转换为字符串
  862 + *
  863 + * @param data 数据
  864 + * @param charset 字符集,如果为空使用当前系统字符集
  865 + * @return 字符串
  866 + */
  867 + public static String str(ByteBuffer data, Charset charset)
  868 + {
  869 + if (null == charset)
  870 + {
  871 + charset = Charset.defaultCharset();
  872 + }
  873 + return charset.decode(data).toString();
  874 + }
  875 +
  876 + // ----------------------------------------------------------------------- 全角半角转换
  877 + /**
  878 + * 半角转全角
  879 + *
  880 + * @param input String.
  881 + * @return 全角字符串.
  882 + */
  883 + public static String toSBC(String input)
  884 + {
  885 + return toSBC(input, null);
  886 + }
  887 +
  888 + /**
  889 + * 半角转全角
  890 + *
  891 + * @param input String
  892 + * @param notConvertSet 不替换的字符集合
  893 + * @return 全角字符串.
  894 + */
  895 + public static String toSBC(String input, Set<Character> notConvertSet)
  896 + {
  897 + char[] c = input.toCharArray();
  898 + for (int i = 0; i < c.length; i++)
  899 + {
  900 + if (null != notConvertSet && notConvertSet.contains(c[i]))
  901 + {
  902 + // 跳过不替换的字符
  903 + continue;
  904 + }
  905 +
  906 + if (c[i] == ' ')
  907 + {
  908 + c[i] = '\u3000';
  909 + }
  910 + else if (c[i] < '\177')
  911 + {
  912 + c[i] = (char) (c[i] + 65248);
  913 +
  914 + }
  915 + }
  916 + return new String(c);
  917 + }
  918 +
  919 + /**
  920 + * 全角转半角
  921 + *
  922 + * @param input String.
  923 + * @return 半角字符串
  924 + */
  925 + public static String toDBC(String input)
  926 + {
  927 + return toDBC(input, null);
  928 + }
  929 +
  930 + /**
  931 + * 替换全角为半角
  932 + *
  933 + * @param text 文本
  934 + * @param notConvertSet 不替换的字符集合
  935 + * @return 替换后的字符
  936 + */
  937 + public static String toDBC(String text, Set<Character> notConvertSet)
  938 + {
  939 + char[] c = text.toCharArray();
  940 + for (int i = 0; i < c.length; i++)
  941 + {
  942 + if (null != notConvertSet && notConvertSet.contains(c[i]))
  943 + {
  944 + // 跳过不替换的字符
  945 + continue;
  946 + }
  947 +
  948 + if (c[i] == '\u3000')
  949 + {
  950 + c[i] = ' ';
  951 + }
  952 + else if (c[i] > '\uFF00' && c[i] < '\uFF5F')
  953 + {
  954 + c[i] = (char) (c[i] - 65248);
  955 + }
  956 + }
  957 + String returnString = new String(c);
  958 +
  959 + return returnString;
  960 + }
  961 +
  962 + /**
  963 + * 数字金额大写转换 先写个完整的然后将如零拾替换成零
  964 + *
  965 + * @param n 数字
  966 + * @return 中文大写数字
  967 + */
  968 + public static String digitUppercase(double n)
  969 + {
  970 + String[] fraction = { "角", "分" };
  971 + String[] digit = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
  972 + String[][] unit = { { "元", "万", "亿" }, { "", "拾", "佰", "仟" } };
  973 +
  974 + String head = n < 0 ? "负" : "";
  975 + n = Math.abs(n);
  976 +
  977 + String s = "";
  978 + for (int i = 0; i < fraction.length; i++)
  979 + {
  980 + s += (digit[(int) (Math.floor(n * 10 * Math.pow(10, i)) % 10)] + fraction[i]).replaceAll("(零.)+", "");
  981 + }
  982 + if (s.length() < 1)
  983 + {
  984 + s = "整";
  985 + }
  986 + int integerPart = (int) Math.floor(n);
  987 +
  988 + for (int i = 0; i < unit[0].length && integerPart > 0; i++)
  989 + {
  990 + String p = "";
  991 + for (int j = 0; j < unit[1].length && n > 0; j++)
  992 + {
  993 + p = digit[integerPart % 10] + unit[1][j] + p;
  994 + integerPart = integerPart / 10;
  995 + }
  996 + s = p.replaceAll("(零.)*零$", "").replaceAll("^$", "零") + unit[0][i] + s;
  997 + }
  998 + return head + s.replaceAll("(零.)*零元", "元").replaceFirst("(零.)+", "").replaceAll("(零.)+", "零").replaceAll("^整$", "零元整");
  999 + }
  1000 +}
... ...
trash-common/src/main/java/com/trash/common/exception/BaseException.java
1   -package com.trash.common.exception;
2   -
3   -import com.trash.common.utils.MessageUtils;
4   -import com.trash.common.utils.StringUtils;
5   -
6   -/**
7   - * 基础异常
8   - *
9   - * @author trash
10   - */
11   -public class BaseException extends RuntimeException
12   -{
13   - private static final long serialVersionUID = 1L;
14   -
15   - /**
16   - * 所属模块
17   - */
18   - private final String module;
19   -
20   - /**
21   - * 错误码
22   - */
23   - private final String code;
24   -
25   - /**
26   - * 错误码对应的参数
27   - */
28   - private final Object[] args;
29   -
30   - /**
31   - * 错误消息
32   - */
33   - private final String defaultMessage;
34   -
35   - public BaseException(String module, String code, Object[] args, String defaultMessage)
36   - {
37   - this.module = module;
38   - this.code = code;
39   - this.args = args;
40   - this.defaultMessage = defaultMessage;
41   - }
42   -
43   - public BaseException(String module, String code, Object[] args)
44   - {
45   - this(module, code, args, null);
46   - }
47   -
48   - public BaseException(String module, String defaultMessage)
49   - {
50   - this(module, null, null, defaultMessage);
51   - }
52   -
53   - public BaseException(String code, Object[] args)
54   - {
55   - this(null, code, args, null);
56   - }
57   -
58   - public BaseException(String defaultMessage)
59   - {
60   - this(null, null, null, defaultMessage);
61   - }
62   -
63   - @Override
64   - public String getMessage()
65   - {
66   - String message = null;
67   - if (!StringUtils.isEmpty(code))
68   - {
69   - message = MessageUtils.message(code, args);
70   - }
71   - if (message == null)
72   - {
73   - message = defaultMessage;
74   - }
75   - return message;
76   - }
77   -
78   - public String getModule()
79   - {
80   - return module;
81   - }
82   -
83   - public String getCode()
84   - {
85   - return code;
86   - }
87   -
88   - public Object[] getArgs()
89   - {
90   - return args;
91   - }
92   -
93   - public String getDefaultMessage()
94   - {
95   - return defaultMessage;
96   - }
97   -}
  1 +package com.trash.common.exception;
  2 +
  3 +import com.trash.common.utils.MessageUtils;
  4 +import com.trash.common.utils.StringUtils;
  5 +
  6 +/**
  7 + * 基础异常
  8 + *
  9 + * @author trash
  10 + */
  11 +public class BaseException extends RuntimeException
  12 +{
  13 + private static final long serialVersionUID = 1L;
  14 +
  15 + /**
  16 + * 所属模块
  17 + */
  18 + private final String module;
  19 +
  20 + /**
  21 + * 错误码
  22 + */
  23 + private final String code;
  24 +
  25 + /**
  26 + * 错误码对应的参数
  27 + */
  28 + private final Object[] args;
  29 +
  30 + /**
  31 + * 错误消息
  32 + */
  33 + private final String defaultMessage;
  34 +
  35 + public BaseException(String module, String code, Object[] args, String defaultMessage)
  36 + {
  37 + this.module = module;
  38 + this.code = code;
  39 + this.args = args;
  40 + this.defaultMessage = defaultMessage;
  41 + }
  42 +
  43 + public BaseException(String module, String code, Object[] args)
  44 + {
  45 + this(module, code, args, null);
  46 + }
  47 +
  48 + public BaseException(String module, String defaultMessage)
  49 + {
  50 + this(module, null, null, defaultMessage);
  51 + }
  52 +
  53 + public BaseException(String code, Object[] args)
  54 + {
  55 + this(null, code, args, null);
  56 + }
  57 +
  58 + public BaseException(String defaultMessage)
  59 + {
  60 + this(null, null, null, defaultMessage);
  61 + }
  62 +
  63 + @Override
  64 + public String getMessage()
  65 + {
  66 + String message = null;
  67 + if (!StringUtils.isEmpty(code))
  68 + {
  69 + message = MessageUtils.message(code, args);
  70 + }
  71 + if (message == null)
  72 + {
  73 + message = defaultMessage;
  74 + }
  75 + return message;
  76 + }
  77 +
  78 + public String getModule()
  79 + {
  80 + return module;
  81 + }
  82 +
  83 + public String getCode()
  84 + {
  85 + return code;
  86 + }
  87 +
  88 + public Object[] getArgs()
  89 + {
  90 + return args;
  91 + }
  92 +
  93 + public String getDefaultMessage()
  94 + {
  95 + return defaultMessage;
  96 + }
  97 +}
... ...
trash-common/src/main/java/com/trash/common/exception/CustomException.java
1   -package com.trash.common.exception;
2   -
3   -/**
4   - * 自定义异常
5   - *
6   - * @author trash
7   - */
8   -public class CustomException extends RuntimeException
9   -{
10   - private static final long serialVersionUID = 1L;
11   -
12   - private Integer code;
13   -
14   - private final String message;
15   -
16   - public CustomException(String message)
17   - {
18   - this.message = message;
19   - }
20   -
21   - public CustomException(String message, Integer code)
22   - {
23   - this.message = message;
24   - this.code = code;
25   - }
26   -
27   - public CustomException(String message, Throwable e)
28   - {
29   - super(message, e);
30   - this.message = message;
31   - }
32   -
33   - @Override
34   - public String getMessage()
35   - {
36   - return message;
37   - }
38   -
39   - public Integer getCode()
40   - {
41   - return code;
42   - }
43   -}
  1 +package com.trash.common.exception;
  2 +
  3 +/**
  4 + * 自定义异常
  5 + *
  6 + * @author trash
  7 + */
  8 +public class CustomException extends RuntimeException
  9 +{
  10 + private static final long serialVersionUID = 1L;
  11 +
  12 + private Integer code;
  13 +
  14 + private final String message;
  15 +
  16 + public CustomException(String message)
  17 + {
  18 + this.message = message;
  19 + }
  20 +
  21 + public CustomException(String message, Integer code)
  22 + {
  23 + this.message = message;
  24 + this.code = code;
  25 + }
  26 +
  27 + public CustomException(String message, Throwable e)
  28 + {
  29 + super(message, e);
  30 + this.message = message;
  31 + }
  32 +
  33 + @Override
  34 + public String getMessage()
  35 + {
  36 + return message;
  37 + }
  38 +
  39 + public Integer getCode()
  40 + {
  41 + return code;
  42 + }
  43 +}
... ...
trash-common/src/main/java/com/trash/common/exception/file/InvalidExtensionException.java
1   -package com.trash.common.exception.file;
2   -
3   -import java.util.Arrays;
4   -import org.apache.commons.fileupload.FileUploadException;
5   -
6   -/**
7   - * 文件上传 误异常类
8   - *
9   - * @author trash
10   - */
11   -public class InvalidExtensionException extends FileUploadException
12   -{
13   - private static final long serialVersionUID = 1L;
14   -
15   - private final String[] allowedExtension;
16   - private final String extension;
17   - private final String filename;
18   -
19   - public InvalidExtensionException(String[] allowedExtension, String extension, String filename)
20   - {
21   - super("filename : [" + filename + "], extension : [" + extension + "], allowed extension : [" + Arrays.toString(allowedExtension) + "]");
22   - this.allowedExtension = allowedExtension;
23   - this.extension = extension;
24   - this.filename = filename;
25   - }
26   -
27   - public String[] getAllowedExtension()
28   - {
29   - return allowedExtension;
30   - }
31   -
32   - public String getExtension()
33   - {
34   - return extension;
35   - }
36   -
37   - public String getFilename()
38   - {
39   - return filename;
40   - }
41   -
42   - public static class InvalidImageExtensionException extends InvalidExtensionException
43   - {
44   - private static final long serialVersionUID = 1L;
45   -
46   - public InvalidImageExtensionException(String[] allowedExtension, String extension, String filename)
47   - {
48   - super(allowedExtension, extension, filename);
49   - }
50   - }
51   -
52   - public static class InvalidFlashExtensionException extends InvalidExtensionException
53   - {
54   - private static final long serialVersionUID = 1L;
55   -
56   - public InvalidFlashExtensionException(String[] allowedExtension, String extension, String filename)
57   - {
58   - super(allowedExtension, extension, filename);
59   - }
60   - }
61   -
62   - public static class InvalidMediaExtensionException extends InvalidExtensionException
63   - {
64   - private static final long serialVersionUID = 1L;
65   -
66   - public InvalidMediaExtensionException(String[] allowedExtension, String extension, String filename)
67   - {
68   - super(allowedExtension, extension, filename);
69   - }
70   - }
71   -}
  1 +package com.trash.common.exception.file;
  2 +
  3 +import java.util.Arrays;
  4 +import org.apache.commons.fileupload.FileUploadException;
  5 +
  6 +/**
  7 + * 文件上传 误异常类
  8 + *
  9 + * @author trash
  10 + */
  11 +public class InvalidExtensionException extends FileUploadException
  12 +{
  13 + private static final long serialVersionUID = 1L;
  14 +
  15 + private final String[] allowedExtension;
  16 + private final String extension;
  17 + private final String filename;
  18 +
  19 + public InvalidExtensionException(String[] allowedExtension, String extension, String filename)
  20 + {
  21 + super("filename : [" + filename + "], extension : [" + extension + "], allowed extension : [" + Arrays.toString(allowedExtension) + "]");
  22 + this.allowedExtension = allowedExtension;
  23 + this.extension = extension;
  24 + this.filename = filename;
  25 + }
  26 +
  27 + public String[] getAllowedExtension()
  28 + {
  29 + return allowedExtension;
  30 + }
  31 +
  32 + public String getExtension()
  33 + {
  34 + return extension;
  35 + }
  36 +
  37 + public String getFilename()
  38 + {
  39 + return filename;
  40 + }
  41 +
  42 + public static class InvalidImageExtensionException extends InvalidExtensionException
  43 + {
  44 + private static final long serialVersionUID = 1L;
  45 +
  46 + public InvalidImageExtensionException(String[] allowedExtension, String extension, String filename)
  47 + {
  48 + super(allowedExtension, extension, filename);
  49 + }
  50 + }
  51 +
  52 + public static class InvalidFlashExtensionException extends InvalidExtensionException
  53 + {
  54 + private static final long serialVersionUID = 1L;
  55 +
  56 + public InvalidFlashExtensionException(String[] allowedExtension, String extension, String filename)
  57 + {
  58 + super(allowedExtension, extension, filename);
  59 + }
  60 + }
  61 +
  62 + public static class InvalidMediaExtensionException extends InvalidExtensionException
  63 + {
  64 + private static final long serialVersionUID = 1L;
  65 +
  66 + public InvalidMediaExtensionException(String[] allowedExtension, String extension, String filename)
  67 + {
  68 + super(allowedExtension, extension, filename);
  69 + }
  70 + }
  71 +}
... ...
trash-common/src/main/java/com/trash/common/exception/job/TaskException.java
1   -package com.trash.common.exception.job;
2   -
3   -/**
4   - * 计划策略异常
5   - *
6   - * @author trash
7   - */
8   -public class TaskException extends Exception
9   -{
10   - private static final long serialVersionUID = 1L;
11   -
12   - private final Code code;
13   -
14   - public TaskException(String msg, Code code)
15   - {
16   - this(msg, code, null);
17   - }
18   -
19   - public TaskException(String msg, Code code, Exception nestedEx)
20   - {
21   - super(msg, nestedEx);
22   - this.code = code;
23   - }
24   -
25   - public Code getCode()
26   - {
27   - return code;
28   - }
29   -
30   - public enum Code
31   - {
32   - TASK_EXISTS, NO_TASK_EXISTS, TASK_ALREADY_STARTED, UNKNOWN, CONFIG_ERROR, TASK_NODE_NOT_AVAILABLE
33   - }
  1 +package com.trash.common.exception.job;
  2 +
  3 +/**
  4 + * 计划策略异常
  5 + *
  6 + * @author trash
  7 + */
  8 +public class TaskException extends Exception
  9 +{
  10 + private static final long serialVersionUID = 1L;
  11 +
  12 + private final Code code;
  13 +
  14 + public TaskException(String msg, Code code)
  15 + {
  16 + this(msg, code, null);
  17 + }
  18 +
  19 + public TaskException(String msg, Code code, Exception nestedEx)
  20 + {
  21 + super(msg, nestedEx);
  22 + this.code = code;
  23 + }
  24 +
  25 + public Code getCode()
  26 + {
  27 + return code;
  28 + }
  29 +
  30 + public enum Code
  31 + {
  32 + TASK_EXISTS, NO_TASK_EXISTS, TASK_ALREADY_STARTED, UNKNOWN, CONFIG_ERROR, TASK_NODE_NOT_AVAILABLE
  33 + }
34 34 }
35 35 \ No newline at end of file
... ...
trash-common/src/main/java/com/trash/common/filter/RepeatedlyRequestWrapper.java
1   -package com.trash.common.filter;
2   -
3   -import java.io.BufferedReader;
4   -import java.io.ByteArrayInputStream;
5   -import java.io.IOException;
6   -import java.io.InputStreamReader;
7   -import java.nio.charset.StandardCharsets;
8   -import javax.servlet.ReadListener;
9   -import javax.servlet.ServletInputStream;
10   -import javax.servlet.ServletResponse;
11   -import javax.servlet.http.HttpServletRequest;
12   -import javax.servlet.http.HttpServletRequestWrapper;
13   -
14   -import com.trash.common.utils.http.HttpHelper;
15   -
16   -/**
17   - * 构建可重复读取inputStream的request
18   - *
19   - * @author trash
20   - */
21   -public class RepeatedlyRequestWrapper extends HttpServletRequestWrapper
22   -{
23   - private final byte[] body;
24   -
25   - public RepeatedlyRequestWrapper(HttpServletRequest request, ServletResponse response) throws IOException
26   - {
27   - super(request);
28   - request.setCharacterEncoding("UTF-8");
29   - response.setCharacterEncoding("UTF-8");
30   -
31   - body = HttpHelper.getBodyString(request).getBytes(StandardCharsets.UTF_8);
32   - }
33   -
34   - @Override
35   - public BufferedReader getReader() throws IOException
36   - {
37   - return new BufferedReader(new InputStreamReader(getInputStream()));
38   - }
39   -
40   - @Override
41   - public ServletInputStream getInputStream() throws IOException
42   - {
43   -
44   - final ByteArrayInputStream bais = new ByteArrayInputStream(body);
45   -
46   - return new ServletInputStream()
47   - {
48   -
49   - @Override
50   - public int read() throws IOException
51   - {
52   - return bais.read();
53   - }
54   -
55   - @Override
56   - public boolean isFinished()
57   - {
58   - return false;
59   - }
60   -
61   - @Override
62   - public boolean isReady()
63   - {
64   - return false;
65   - }
66   -
67   - @Override
68   - public void setReadListener(ReadListener readListener)
69   - {
70   -
71   - }
72   - };
73   - }
74   -}
  1 +package com.trash.common.filter;
  2 +
  3 +import java.io.BufferedReader;
  4 +import java.io.ByteArrayInputStream;
  5 +import java.io.IOException;
  6 +import java.io.InputStreamReader;
  7 +import java.nio.charset.StandardCharsets;
  8 +import javax.servlet.ReadListener;
  9 +import javax.servlet.ServletInputStream;
  10 +import javax.servlet.ServletResponse;
  11 +import javax.servlet.http.HttpServletRequest;
  12 +import javax.servlet.http.HttpServletRequestWrapper;
  13 +
  14 +import com.trash.common.utils.http.HttpHelper;
  15 +
  16 +/**
  17 + * 构建可重复读取inputStream的request
  18 + *
  19 + * @author trash
  20 + */
  21 +public class RepeatedlyRequestWrapper extends HttpServletRequestWrapper
  22 +{
  23 + private final byte[] body;
  24 +
  25 + public RepeatedlyRequestWrapper(HttpServletRequest request, ServletResponse response) throws IOException
  26 + {
  27 + super(request);
  28 + request.setCharacterEncoding("UTF-8");
  29 + response.setCharacterEncoding("UTF-8");
  30 +
  31 + body = HttpHelper.getBodyString(request).getBytes(StandardCharsets.UTF_8);
  32 + }
  33 +
  34 + @Override
  35 + public BufferedReader getReader() throws IOException
  36 + {
  37 + return new BufferedReader(new InputStreamReader(getInputStream()));
  38 + }
  39 +
  40 + @Override
  41 + public ServletInputStream getInputStream() throws IOException
  42 + {
  43 +
  44 + final ByteArrayInputStream bais = new ByteArrayInputStream(body);
  45 +
  46 + return new ServletInputStream()
  47 + {
  48 +
  49 + @Override
  50 + public int read() throws IOException
  51 + {
  52 + return bais.read();
  53 + }
  54 +
  55 + @Override
  56 + public boolean isFinished()
  57 + {
  58 + return false;
  59 + }
  60 +
  61 + @Override
  62 + public boolean isReady()
  63 + {
  64 + return false;
  65 + }
  66 +
  67 + @Override
  68 + public void setReadListener(ReadListener readListener)
  69 + {
  70 +
  71 + }
  72 + };
  73 + }
  74 +}
... ...