Commit 0b3d3960943fe040dac0f620ba80a7d0b8531807
0 parents
First commit
Showing
41 changed files
with
5092 additions
and
0 deletions
Too many changes to show.
To preserve performance only 39 of 41 files are displayed.
.gitignore
0 → 100644
README.md
0 → 100644
1 | +++ a/README.md | ... | ... |
fw-rp-common/pom.xml
0 → 100644
1 | +++ a/fw-rp-common/pom.xml | |
1 | +<?xml version="1.0" encoding="UTF-8"?> | |
2 | +<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" | |
3 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | |
4 | + <modelVersion>4.0.0</modelVersion> | |
5 | + | |
6 | + <parent> | |
7 | + <groupId>cn.fw</groupId> | |
8 | + <artifactId>fw-rp</artifactId> | |
9 | + <version>1.0</version> | |
10 | + </parent> | |
11 | + | |
12 | + <artifactId>fw-rp-common</artifactId> | |
13 | + <packaging>jar</packaging> | |
14 | + <name>fw-rp-common</name> | |
15 | + | |
16 | + <dependencies> | |
17 | + | |
18 | + <dependency> | |
19 | + <groupId>org.slf4j</groupId> | |
20 | + <artifactId>slf4j-api</artifactId> | |
21 | + <version>1.6.4</version> | |
22 | + </dependency> | |
23 | + | |
24 | + <!--ZxingQrCode Libary start--> | |
25 | + <dependency> | |
26 | + <groupId>com.google.zxing</groupId> | |
27 | + <artifactId>core</artifactId> | |
28 | + <version>2.3.0</version> | |
29 | + </dependency> | |
30 | + <!-- ZxingQrCode Libary end --> | |
31 | + | |
32 | + <dependency> | |
33 | + <groupId>net.sf.json-lib</groupId> | |
34 | + <artifactId>json-lib</artifactId> | |
35 | + <classifier>jdk15</classifier> | |
36 | + </dependency> | |
37 | + <dependency> | |
38 | + <groupId>org.mybatis.spring.boot</groupId> | |
39 | + <artifactId>mybatis-spring-boot-starter</artifactId> | |
40 | + <version>1.3.0</version> | |
41 | + </dependency> | |
42 | + <dependency> | |
43 | + <groupId>net.sf.json-lib</groupId> | |
44 | + <artifactId>json-lib</artifactId> | |
45 | + <classifier>jdk15</classifier> | |
46 | + <version>2.3</version> | |
47 | + </dependency> | |
48 | + <dependency> | |
49 | + <groupId>com.fasterxml.jackson.core</groupId> | |
50 | + <artifactId>jackson-databind</artifactId> | |
51 | + </dependency> | |
52 | + <dependency> | |
53 | + <groupId>commons-codec</groupId> | |
54 | + <artifactId>commons-codec</artifactId> | |
55 | + </dependency> | |
56 | + </dependencies> | |
57 | +</project> | |
0 | 58 | \ No newline at end of file | ... | ... |
fw-rp-common/src/main/java/cn/fw/rp/common/constant/Constant.java
0 → 100644
1 | +++ a/fw-rp-common/src/main/java/cn/fw/rp/common/constant/Constant.java | |
1 | +package cn.fw.rp.common.constant; | |
2 | + | |
3 | +/** | |
4 | + * @author 张志伟 | |
5 | + * @date 2018-01-27 14:19 | |
6 | + * @description | |
7 | + */ | |
8 | +public class Constant { | |
9 | + | |
10 | + /** | |
11 | + * 产品名称:云通信隐私保护产品,开发者无需替换 | |
12 | + */ | |
13 | + public static final String PRODUCT = "Dyplsapi"; | |
14 | + /** | |
15 | + * 营销活动消息通知 topic | |
16 | + */ | |
17 | + public static final String MKT_PUBLISH_TITLE = "MKT_WS_MESSAGE"; | |
18 | + /** | |
19 | + * 产品域名,开发者无需替换 | |
20 | + */ | |
21 | + public static final String DOMAIN = "dyplsapi.aliyuncs.com"; | |
22 | + /** | |
23 | + * 访问ID | |
24 | + */ | |
25 | + public static final String ACCESS_KEY_ID = "LTAIU865lo41xCTI"; | |
26 | + /** | |
27 | + * 访问ID对应的KEY | |
28 | + */ | |
29 | + public static final String ACCESS_KEY_SECRET = "C5ilXwesfPV9KtF1V74IQsaXMgR4wC"; | |
30 | + /** | |
31 | + * 更新类型 | |
32 | + */ | |
33 | + public static final String A = "updateNoA"; | |
34 | + public static final String B = "updateNoB"; | |
35 | + public static final String T = "updateExpire"; | |
36 | + /** | |
37 | + * 此处替换成您所需的消息类型名称 | |
38 | + */ | |
39 | + public static final String MESSAGE_TYPE = "SecretReport"; | |
40 | + /** | |
41 | + * 在云通信页面开通相应业务消息后,就能在页面上获得对应的queueName | |
42 | + */ | |
43 | + public static final String QUEUE_NAME = "Alicom-Queue-1294201731075657-SecretReport"; | |
44 | + /** | |
45 | + * 成功的返回码 | |
46 | + */ | |
47 | + public static final String SUCCESS = "OK"; | |
48 | + | |
49 | + /** | |
50 | + * websocket config | |
51 | + */ | |
52 | + public static class WebSocketConfig { | |
53 | + /** | |
54 | + * ws endpoint | |
55 | + */ | |
56 | + public static final String WS_ENDPOINT = "marketing"; | |
57 | + /** | |
58 | + * app destination prefix | |
59 | + */ | |
60 | + public static final String APP_DESTINATION_PREFIX = "/app"; | |
61 | + /** | |
62 | + * simple broker destination prefix | |
63 | + */ | |
64 | + public static final String BROKER_DESTINATION_PREFIX = "/activity"; | |
65 | + | |
66 | + } | |
67 | +} | ... | ... |
fw-rp-common/src/main/java/cn/fw/rp/common/enums/FileTypeEnums.java
0 → 100644
1 | +++ a/fw-rp-common/src/main/java/cn/fw/rp/common/enums/FileTypeEnums.java | |
1 | +package cn.fw.rp.common.enums; | |
2 | + | |
3 | +/** | |
4 | + * @author suchu | |
5 | + * @since 2018/5/9 13:24 | |
6 | + */ | |
7 | +public enum FileTypeEnums { | |
8 | + /** | |
9 | + * 图片 | |
10 | + */ | |
11 | + IMAGE(1, "图片"), | |
12 | + | |
13 | + /** | |
14 | + * 视频 | |
15 | + */ | |
16 | + VIDEO(2, "视频"), | |
17 | + | |
18 | + /** | |
19 | + * 文档 | |
20 | + */ | |
21 | + FILE(3, "附件"); | |
22 | + | |
23 | + private int type; | |
24 | + private String desc; | |
25 | + | |
26 | + FileTypeEnums(int type, String desc) { | |
27 | + this.type = type; | |
28 | + this.desc = desc; | |
29 | + } | |
30 | + | |
31 | + public int getType() { | |
32 | + return type; | |
33 | + } | |
34 | + | |
35 | + public void setType(int type) { | |
36 | + this.type = type; | |
37 | + } | |
38 | + | |
39 | + public String getDesc() { | |
40 | + return desc; | |
41 | + } | |
42 | + | |
43 | + public void setDesc(String desc) { | |
44 | + this.desc = desc; | |
45 | + } | |
46 | +} | ... | ... |
fw-rp-common/src/main/java/cn/fw/rp/common/util/AesUtil.java
0 → 100644
1 | +++ a/fw-rp-common/src/main/java/cn/fw/rp/common/util/AesUtil.java | |
1 | +package cn.fw.rp.common.util; | |
2 | + | |
3 | +import sun.misc.BASE64Decoder; | |
4 | +import sun.misc.BASE64Encoder; | |
5 | + | |
6 | +import javax.crypto.*; | |
7 | +import javax.crypto.spec.SecretKeySpec; | |
8 | +import java.io.IOException; | |
9 | +import java.io.UnsupportedEncodingException; | |
10 | +import java.security.InvalidKeyException; | |
11 | +import java.security.NoSuchAlgorithmException; | |
12 | +import java.security.SecureRandom; | |
13 | + | |
14 | +/** | |
15 | + * Created with IntelliJ IDEA. | |
16 | + * Created By Devin | |
17 | + * Date: 2018/4/10 | |
18 | + * Time: 17:14 | |
19 | + * Description: | |
20 | + */ | |
21 | +public class AesUtil { | |
22 | + | |
23 | + private final static String KEY = ")O[xfrs]6,YF}+efcaj{+oESb9d8>Zhy"; | |
24 | + private final static String encoding = "UTF-8"; | |
25 | + | |
26 | + public static String aesDecrypt(String content) { | |
27 | + return decrypt(content, KEY); | |
28 | + } | |
29 | + | |
30 | + public static String aesEncrypt(String content) { | |
31 | + return encryptAES(content, KEY); | |
32 | + } | |
33 | + | |
34 | + /** | |
35 | + * AES加密 | |
36 | + * | |
37 | + * @param content | |
38 | + * @param password | |
39 | + * @return | |
40 | + */ | |
41 | + public static String encryptAES(String content, String password) { | |
42 | + byte[] encryptResult = encrypt(content, password); | |
43 | + String encryptResultStr = parseByte2HexStr(encryptResult); | |
44 | + // BASE64位加密 | |
45 | + encryptResultStr = ebotongEncrypto(encryptResultStr); | |
46 | + return encryptResultStr; | |
47 | + } | |
48 | + | |
49 | + /** | |
50 | + * AES解密 | |
51 | + * | |
52 | + * @param encryptResultStr | |
53 | + * @param password | |
54 | + * @return | |
55 | + */ | |
56 | + public static String decrypt(String encryptResultStr, String password) { | |
57 | + // BASE64位解密 | |
58 | + String decrpt = ebotongDecrypto(encryptResultStr); | |
59 | + byte[] decryptFrom = parseHexStr2Byte(decrpt); | |
60 | + byte[] decryptResult = decrypt(decryptFrom, password); | |
61 | + return new String(decryptResult); | |
62 | + } | |
63 | + | |
64 | + /** | |
65 | + * 加密字符串 | |
66 | + */ | |
67 | + public static String ebotongEncrypto(String str) { | |
68 | + BASE64Encoder base64encoder = new BASE64Encoder(); | |
69 | + String result = str; | |
70 | + if (str != null && str.length() > 0) { | |
71 | + try { | |
72 | + byte[] encodeByte = str.getBytes(encoding); | |
73 | + result = base64encoder.encode(encodeByte); | |
74 | + } catch (Exception e) { | |
75 | + e.printStackTrace(); | |
76 | + } | |
77 | + } | |
78 | + //base64加密超过一定长度会自动换行 需要去除换行符 | |
79 | + return result.replaceAll("\r\n", "").replaceAll("\r", "").replaceAll("\n", ""); | |
80 | + } | |
81 | + | |
82 | + /** | |
83 | + * 解密字符串 | |
84 | + */ | |
85 | + public static String ebotongDecrypto(String str) { | |
86 | + BASE64Decoder base64decoder = new BASE64Decoder(); | |
87 | + try { | |
88 | + byte[] encodeByte = base64decoder.decodeBuffer(str); | |
89 | + return new String(encodeByte); | |
90 | + } catch (IOException e) { | |
91 | + e.printStackTrace(); | |
92 | + return str; | |
93 | + } | |
94 | + } | |
95 | + | |
96 | + /** | |
97 | + * 加密 | |
98 | + * | |
99 | + * @param content 需要加密的内容 | |
100 | + * @param password 加密密码 | |
101 | + * @return | |
102 | + */ | |
103 | + private static byte[] encrypt(String content, String password) { | |
104 | + try { | |
105 | + KeyGenerator kgen = KeyGenerator.getInstance("AES"); | |
106 | + //防止linux下 随机生成key | |
107 | + SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG"); | |
108 | + secureRandom.setSeed(password.getBytes()); | |
109 | + kgen.init(128, secureRandom); | |
110 | + //kgen.init(128, new SecureRandom(password.getBytes())); | |
111 | + SecretKey secretKey = kgen.generateKey(); | |
112 | + byte[] enCodeFormat = secretKey.getEncoded(); | |
113 | + SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES"); | |
114 | + Cipher cipher = Cipher.getInstance("AES");// 创建密码器 | |
115 | + byte[] byteContent = content.getBytes("utf-8"); | |
116 | + cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化 | |
117 | + byte[] result = cipher.doFinal(byteContent); | |
118 | + return result; // 加密 | |
119 | + } catch (NoSuchAlgorithmException e) { | |
120 | + e.printStackTrace(); | |
121 | + } catch (NoSuchPaddingException e) { | |
122 | + e.printStackTrace(); | |
123 | + } catch (InvalidKeyException e) { | |
124 | + e.printStackTrace(); | |
125 | + } catch (UnsupportedEncodingException e) { | |
126 | + e.printStackTrace(); | |
127 | + } catch (IllegalBlockSizeException e) { | |
128 | + e.printStackTrace(); | |
129 | + } catch (BadPaddingException e) { | |
130 | + e.printStackTrace(); | |
131 | + } | |
132 | + return null; | |
133 | + } | |
134 | + | |
135 | + | |
136 | + /** | |
137 | + * 解密 | |
138 | + * | |
139 | + * @param content 待解密内容 | |
140 | + * @param password 解密密钥 | |
141 | + * @return | |
142 | + */ | |
143 | + private static byte[] decrypt(byte[] content, String password) { | |
144 | + try { | |
145 | + KeyGenerator kgen = KeyGenerator.getInstance("AES"); | |
146 | + //防止linux下 随机生成key | |
147 | + SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG"); | |
148 | + secureRandom.setSeed(password.getBytes()); | |
149 | + kgen.init(128, secureRandom); | |
150 | + //kgen.init(128, new SecureRandom(password.getBytes())); | |
151 | + SecretKey secretKey = kgen.generateKey(); | |
152 | + byte[] enCodeFormat = secretKey.getEncoded(); | |
153 | + SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES"); | |
154 | + Cipher cipher = Cipher.getInstance("AES");// 创建密码器 | |
155 | + cipher.init(Cipher.DECRYPT_MODE, key);// 初始化 | |
156 | + byte[] result = cipher.doFinal(content); | |
157 | + return result; // 加密 | |
158 | + } catch (NoSuchAlgorithmException e) { | |
159 | + e.printStackTrace(); | |
160 | + } catch (NoSuchPaddingException e) { | |
161 | + e.printStackTrace(); | |
162 | + } catch (InvalidKeyException e) { | |
163 | + e.printStackTrace(); | |
164 | + } catch (IllegalBlockSizeException e) { | |
165 | + e.printStackTrace(); | |
166 | + } catch (BadPaddingException e) { | |
167 | + e.printStackTrace(); | |
168 | + } | |
169 | + return null; | |
170 | + } | |
171 | + | |
172 | + /** | |
173 | + * 将二进制转换成16进制 | |
174 | + * | |
175 | + * @param buf | |
176 | + * @return | |
177 | + */ | |
178 | + public static String parseByte2HexStr(byte buf[]) { | |
179 | + StringBuffer sb = new StringBuffer(); | |
180 | + for (int i = 0; i < buf.length; i++) { | |
181 | + String hex = Integer.toHexString(buf[i] & 0xFF); | |
182 | + if (hex.length() == 1) { | |
183 | + hex = '0' + hex; | |
184 | + } | |
185 | + sb.append(hex.toUpperCase()); | |
186 | + } | |
187 | + return sb.toString(); | |
188 | + } | |
189 | + | |
190 | + | |
191 | + /** | |
192 | + * 将16进制转换为二进制 | |
193 | + * | |
194 | + * @param hexStr | |
195 | + * @return | |
196 | + */ | |
197 | + public static byte[] parseHexStr2Byte(String hexStr) { | |
198 | + if (hexStr.length() < 1) | |
199 | + return null; | |
200 | + byte[] result = new byte[hexStr.length() / 2]; | |
201 | + for (int i = 0; i < hexStr.length() / 2; i++) { | |
202 | + int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16); | |
203 | + int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16); | |
204 | + result[i] = (byte) (high * 16 + low); | |
205 | + } | |
206 | + return result; | |
207 | + } | |
208 | +} | ... | ... |
fw-rp-common/src/main/java/cn/fw/rp/common/util/DateUtils.java
0 → 100644
1 | +++ a/fw-rp-common/src/main/java/cn/fw/rp/common/util/DateUtils.java | |
1 | +package cn.fw.rp.common.util; | |
2 | + | |
3 | +import java.sql.Timestamp; | |
4 | +import java.text.DateFormat; | |
5 | +import java.text.ParseException; | |
6 | +import java.text.ParsePosition; | |
7 | +import java.text.SimpleDateFormat; | |
8 | +import java.util.Calendar; | |
9 | +import java.util.Date; | |
10 | +import java.util.HashMap; | |
11 | +import java.util.Map; | |
12 | + | |
13 | +/** | |
14 | + * 日期工具类 | |
15 | + * | |
16 | + * @author luox | |
17 | + * @version 1.0 | |
18 | + * @since 2018-04-16 | |
19 | + */ | |
20 | +public final class DateUtils { | |
21 | + | |
22 | + public static final long SECOND = 1000; | |
23 | + | |
24 | + public static final long MINUTE = SECOND * 60; | |
25 | + | |
26 | + public static final long HOUR = MINUTE * 60; | |
27 | + | |
28 | + public static final long DAY = HOUR * 24; | |
29 | + | |
30 | + public static final long WEEK = DAY * 7; | |
31 | + | |
32 | + public static final long YEAR = DAY * 365; | |
33 | + | |
34 | + public static final String FOMTER_TIMES = "yyyy-MM-dd HH:mm:ss"; | |
35 | + | |
36 | + private static final Map<Integer, String> WEEK_DAY = new HashMap<Integer, String>(); | |
37 | + private static final Map<String, Integer> DATE_FIELD = new HashMap<String, Integer>(); | |
38 | + | |
39 | + static { | |
40 | + WEEK_DAY.put(7, "星期六"); | |
41 | + WEEK_DAY.put(1, "星期天"); | |
42 | + WEEK_DAY.put(2, "星期一"); | |
43 | + WEEK_DAY.put(3, "星期二"); | |
44 | + WEEK_DAY.put(4, "星期三"); | |
45 | + WEEK_DAY.put(5, "星期四"); | |
46 | + WEEK_DAY.put(6, "星期五"); | |
47 | + } | |
48 | + | |
49 | + static { | |
50 | + DATE_FIELD.put("y", Calendar.YEAR); | |
51 | + DATE_FIELD.put("M", Calendar.MONTH); | |
52 | + DATE_FIELD.put("d", Calendar.DATE); | |
53 | + DATE_FIELD.put("H", Calendar.HOUR); | |
54 | + DATE_FIELD.put("m", Calendar.MINUTE); | |
55 | + DATE_FIELD.put("s", Calendar.SECOND); | |
56 | + DATE_FIELD.put("w", Calendar.WEDNESDAY); | |
57 | + } | |
58 | + | |
59 | + /** | |
60 | + * 解析日期 | |
61 | + * | |
62 | + * @param date 日期字符串 | |
63 | + * @param pattern 日期格式 | |
64 | + * @return | |
65 | + */ | |
66 | + public static Date parse(String date, String pattern) { | |
67 | + Date resultDate = null; | |
68 | + try { | |
69 | + resultDate = new SimpleDateFormat(pattern).parse(date); | |
70 | + } catch (ParseException e) { | |
71 | + e.printStackTrace(); | |
72 | + } | |
73 | + return resultDate; | |
74 | + } | |
75 | + | |
76 | + /** | |
77 | + * 解析日期 yyyy-MM-dd | |
78 | + * | |
79 | + * @param date 日期字符串 | |
80 | + * @return | |
81 | + */ | |
82 | + public static Timestamp parseSimple(String date) { | |
83 | + Date result = null; | |
84 | + try { | |
85 | + DateFormat yyyyMMdd = new SimpleDateFormat("yyyy-MM-dd"); | |
86 | + result = yyyyMMdd.parse(date); | |
87 | + } catch (ParseException e) { | |
88 | + e.printStackTrace(); | |
89 | + } | |
90 | + return result != null ? new Timestamp(result.getTime()) : null; | |
91 | + } | |
92 | + | |
93 | + /** | |
94 | + * 解析日期字符串 | |
95 | + * | |
96 | + * @param date | |
97 | + * @return | |
98 | + */ | |
99 | + public static Timestamp parseFull(String date) { | |
100 | + Date result = null; | |
101 | + try { | |
102 | + DateFormat yyyyMMddHHmmss = new SimpleDateFormat( | |
103 | + "yyyy-MM-dd HH:mm:ss"); | |
104 | + result = yyyyMMddHHmmss.parse(date); | |
105 | + } catch (ParseException e) { | |
106 | + e.printStackTrace(); | |
107 | + } | |
108 | + return result != null ? new Timestamp(result.getTime()) : null; | |
109 | + } | |
110 | + | |
111 | + /** | |
112 | + * 解析日期 yyyy-MM-dd | |
113 | + * | |
114 | + * @return | |
115 | + */ | |
116 | + public static Timestamp parse(Object object) { | |
117 | + if (object instanceof Date) { | |
118 | + return new Timestamp(((Date) object).getTime()); | |
119 | + } | |
120 | + if (StringUtils.isEmpty(object)) | |
121 | + return null; | |
122 | + String date = StringUtils.asString(object); | |
123 | + try { | |
124 | + if (date.length() == 10) { | |
125 | + return parseSimple(date); | |
126 | + } else if (date.length() == 8) { | |
127 | + DateFormat yyyyMMdd = new SimpleDateFormat("yyyyMMdd"); | |
128 | + Date d = yyyyMMdd.parse(date); | |
129 | + return new Timestamp(d.getTime()); | |
130 | + } else if (date.length() == 9) { | |
131 | + if (date.matches("\\d{4}-\\d-\\d{2}")) {//yyyy-M-dd | |
132 | + DateFormat yyyyMdd = new SimpleDateFormat("yyyy-M-dd"); | |
133 | + Date d = yyyyMdd.parse(date); | |
134 | + return new Timestamp(d.getTime()); | |
135 | + } else if (date.matches("\\d{4}-\\d{2}-\\d")) {//yyyy-MM-d | |
136 | + DateFormat yyyyMdd = new SimpleDateFormat("yyyy-MM-d"); | |
137 | + Date d = yyyyMdd.parse(date); | |
138 | + return new Timestamp(d.getTime()); | |
139 | + } | |
140 | + } else if (date.length() == 16) {//yyyy-MM-dd HH:mm | |
141 | + DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm"); | |
142 | + Date d = format.parse(date); | |
143 | + return new Timestamp(d.getTime()); | |
144 | + } else if (date.length() == 18) {//yyyy-MM-ddHH:mm:ss 医保返回日期可能出现 | |
145 | + DateFormat format = new SimpleDateFormat("yyyy-MM-ddHH:mm:ss"); | |
146 | + Date d = format.parse(date); | |
147 | + return new Timestamp(d.getTime()); | |
148 | + } else if (date.length() == 19) {//yyyy-MM-dd HH:mm:ss | |
149 | + return parseFull(date); | |
150 | + } else if (date.length() >= 20 && date.length() <= 23) {//yyyy-MM-dd HH:mm:ss.SSS | |
151 | + if (date.matches("\\d{4}-\\d{2}-\\d{2}\\s\\d{2}:\\d{2}:\\d{2}\\.\\d{1,3}")) { | |
152 | + DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); | |
153 | + Date d = format.parse(date); | |
154 | + return new Timestamp(d.getTime()); | |
155 | + } | |
156 | + } else { | |
157 | + return Timestamp.valueOf(object.toString()); | |
158 | + } | |
159 | + } catch (Exception e) { | |
160 | + e.printStackTrace(); | |
161 | + } | |
162 | + return null; | |
163 | + } | |
164 | + | |
165 | + /** | |
166 | + * 格式化日期字符串 | |
167 | + * | |
168 | + * @param date 日期 | |
169 | + * @param pattern 日期格式 | |
170 | + * @return | |
171 | + */ | |
172 | + public static String format(Date date, String pattern) { | |
173 | + if (date == null) { | |
174 | + return null; | |
175 | + } | |
176 | + DateFormat format = new SimpleDateFormat(pattern); | |
177 | + return format.format(date); | |
178 | + } | |
179 | + | |
180 | + /** | |
181 | + * 格式化日期 | |
182 | + * | |
183 | + * @param date | |
184 | + * @return yyyy年MM月dd日 | |
185 | + */ | |
186 | + public static String formatCHS(Date date) { | |
187 | + if (date == null) { | |
188 | + return null; | |
189 | + } | |
190 | + DateFormat format = new SimpleDateFormat("yyyy年MM月dd日"); | |
191 | + return format.format(date); | |
192 | + } | |
193 | + | |
194 | + /** | |
195 | + * 格式化日期字符串 | |
196 | + * | |
197 | + * @param date 日期 | |
198 | + * @return | |
199 | + */ | |
200 | + public static String format(Date date) { | |
201 | + if (date == null) { | |
202 | + return null; | |
203 | + } | |
204 | + DateFormat YYYY_MM_DD = new SimpleDateFormat("yyyy-MM-dd"); | |
205 | + return YYYY_MM_DD.format(date); | |
206 | + } | |
207 | + | |
208 | + /** | |
209 | + * 格式化日期 | |
210 | + * | |
211 | + * @param date | |
212 | + * @return | |
213 | + */ | |
214 | + public static String formatFull(Date date) { | |
215 | + DateFormat YYYY_MM_DD_HH_MM_SS = new SimpleDateFormat( | |
216 | + "yyyy-MM-dd HH:mm:ss"); | |
217 | + return YYYY_MM_DD_HH_MM_SS.format(date); | |
218 | + } | |
219 | + | |
220 | + /** | |
221 | + * 取得当前日期 | |
222 | + * | |
223 | + * @return | |
224 | + */ | |
225 | + public static Timestamp getNow() { | |
226 | + return new Timestamp(System.currentTimeMillis()); | |
227 | + } | |
228 | + | |
229 | + /** | |
230 | + * 取得当前日期 | |
231 | + * | |
232 | + * @return | |
233 | + */ | |
234 | + public static Integer getNowYear() { | |
235 | + return getYear(getNow()); | |
236 | + } | |
237 | + | |
238 | + /** | |
239 | + * 取得当前月份 | |
240 | + * | |
241 | + * @return | |
242 | + */ | |
243 | + public static Integer getNowMonth() { | |
244 | + return getMonth(getNow()); | |
245 | + } | |
246 | + | |
247 | + /** | |
248 | + * 取得年度 | |
249 | + * | |
250 | + * @param value | |
251 | + * @return | |
252 | + */ | |
253 | + public static Integer getYear(Object value) { | |
254 | + Calendar c = Calendar.getInstance(); | |
255 | + Date date = getDate(value); | |
256 | + c.setTime(date); | |
257 | + return c.get(Calendar.YEAR); | |
258 | + } | |
259 | + | |
260 | + /** | |
261 | + * 取得月份 | |
262 | + * | |
263 | + * @param value | |
264 | + * @return | |
265 | + */ | |
266 | + public static Integer getMonth(Object value) { | |
267 | + Calendar c = Calendar.getInstance(); | |
268 | + Date date = getDate(value); | |
269 | + c.setTime(date); | |
270 | + return c.get(Calendar.MONTH) + 1; | |
271 | + } | |
272 | + | |
273 | + /** | |
274 | + * 取得日 | |
275 | + * | |
276 | + * @param value | |
277 | + * @return | |
278 | + */ | |
279 | + public static Integer getDay(Object value) { | |
280 | + Calendar c = Calendar.getInstance(); | |
281 | + Date date = getDate(value); | |
282 | + c.setTime(date); | |
283 | + return c.get(Calendar.DAY_OF_MONTH); | |
284 | + } | |
285 | + | |
286 | + /** | |
287 | + * 取得日期对象 | |
288 | + * | |
289 | + * @param value | |
290 | + * @return | |
291 | + */ | |
292 | + private static Date getDate(Object value) { | |
293 | + Date date = null; | |
294 | + if (value instanceof Date) { | |
295 | + date = (Date) value; | |
296 | + } else { | |
297 | + date = parse((String) value); | |
298 | + } | |
299 | + if (date == null) { | |
300 | + throw new RuntimeException("日期格式解析错误!date=" + value); | |
301 | + } | |
302 | + return date; | |
303 | + } | |
304 | + | |
305 | + /** | |
306 | + * 取得年龄 | |
307 | + * | |
308 | + * @param value | |
309 | + * @return | |
310 | + */ | |
311 | + @SuppressWarnings("all") | |
312 | + public static Integer getAge(Object value) { | |
313 | + Timestamp date = DateUtils.parse(value); | |
314 | + if (date == null) { | |
315 | + return null; | |
316 | + } | |
317 | + Timestamp now = DateUtils.getNow(); | |
318 | + int year1 = date.getYear(), year2 = now.getYear(); | |
319 | + int month1 = date.getMonth(), month2 = now.getMonth(); | |
320 | + int date1 = date.getDate(), date2 = now.getDate(); | |
321 | + int months = (year2 - year1) * 12 + month2 - month1; | |
322 | + if (date1 > date2) { | |
323 | + months = months - 1; | |
324 | + } | |
325 | + int age = months / 12; | |
326 | + return age; | |
327 | + } | |
328 | + | |
329 | + /** | |
330 | + * @param offsetYear | |
331 | + * @return 当前时间 + offsetYear | |
332 | + */ | |
333 | + public static Timestamp getNowExpiredYear(int offsetYear) { | |
334 | + Calendar now = Calendar.getInstance(); | |
335 | + now.add(Calendar.YEAR, offsetYear); | |
336 | + return new Timestamp(now.getTime().getTime()); | |
337 | + } | |
338 | + | |
339 | + /** | |
340 | + * @param offset | |
341 | + * @return 当前时间 + offsetMonth | |
342 | + */ | |
343 | + public static Timestamp getNowExpiredMonth(int offset) { | |
344 | + Calendar now = Calendar.getInstance(); | |
345 | + now.add(Calendar.MONTH, offset); | |
346 | + return new Timestamp(now.getTime().getTime()); | |
347 | + } | |
348 | + | |
349 | + /** | |
350 | + * @param offset | |
351 | + * @return 当前时间 + offsetDay | |
352 | + */ | |
353 | + public static Timestamp getNowExpiredDay(int offset) { | |
354 | + Calendar now = Calendar.getInstance(); | |
355 | + now.add(Calendar.DATE, offset); | |
356 | + return new Timestamp(now.getTime().getTime()); | |
357 | + } | |
358 | + | |
359 | + /** | |
360 | + * @param offset | |
361 | + * @return 当前时间 + offsetDay | |
362 | + */ | |
363 | + public static Timestamp getNowExpiredHour(int offset) { | |
364 | + Calendar now = Calendar.getInstance(); | |
365 | + now.add(Calendar.HOUR, offset); | |
366 | + return new Timestamp(now.getTime().getTime()); | |
367 | + } | |
368 | + | |
369 | + /** | |
370 | + * @param offsetSecond | |
371 | + * @return 当前时间 + offsetSecond | |
372 | + */ | |
373 | + public static Timestamp getNowExpiredSecond(int offsetSecond) { | |
374 | + Calendar now = Calendar.getInstance(); | |
375 | + now.add(Calendar.SECOND, offsetSecond); | |
376 | + return new Timestamp(now.getTime().getTime()); | |
377 | + } | |
378 | + | |
379 | + /** | |
380 | + * @param offset | |
381 | + * @return 当前时间 + offset | |
382 | + */ | |
383 | + public static Timestamp getNowExpiredMinute(int offset) { | |
384 | + Calendar now = Calendar.getInstance(); | |
385 | + now.add(Calendar.MINUTE, offset); | |
386 | + return new Timestamp(now.getTime().getTime()); | |
387 | + } | |
388 | + | |
389 | + /** | |
390 | + * @param offset | |
391 | + * @return 指定时间 + offsetDay | |
392 | + */ | |
393 | + public static Timestamp getExpiredDay(Date givenDate, int offset) { | |
394 | + Calendar date = Calendar.getInstance(); | |
395 | + date.setTime(givenDate); | |
396 | + date.add(Calendar.DATE, offset); | |
397 | + return new Timestamp(date.getTime().getTime()); | |
398 | + } | |
399 | + | |
400 | + /** | |
401 | + * 实现ORACLE中ADD_MONTHS函数功能 | |
402 | + * | |
403 | + * @param offset | |
404 | + * @return 指定时间 + offsetMonth | |
405 | + */ | |
406 | + public static Timestamp getExpiredMonth(Date givenDate, int offset) { | |
407 | + Calendar date = Calendar.getInstance(); | |
408 | + date.setTime(givenDate); | |
409 | + date.add(Calendar.MONTH, offset); | |
410 | + return new Timestamp(date.getTime().getTime()); | |
411 | + } | |
412 | + | |
413 | + /** | |
414 | + * @param offsetSecond | |
415 | + * @return 指定时间 + offsetSecond | |
416 | + */ | |
417 | + public static Timestamp getExpiredYear(Date givenDate, int offsetHour) { | |
418 | + Calendar date = Calendar.getInstance(); | |
419 | + date.setTime(givenDate); | |
420 | + date.add(Calendar.YEAR, offsetHour); | |
421 | + return new Timestamp(date.getTime().getTime()); | |
422 | + } | |
423 | + | |
424 | + /** | |
425 | + * @param second | |
426 | + * @return 指定时间 + offsetSecond | |
427 | + */ | |
428 | + public static Timestamp getExpiredSecond(Date givenDate, int second) { | |
429 | + Calendar date = Calendar.getInstance(); | |
430 | + date.setTime(givenDate); | |
431 | + date.add(Calendar.SECOND, second); | |
432 | + return new Timestamp(date.getTime().getTime()); | |
433 | + } | |
434 | + | |
435 | + /** | |
436 | + * 计算时间差 | |
437 | + * | |
438 | + * @param givenDate 日期 | |
439 | + * @param offset 2 | |
440 | + * @param type 日期字段类型 | |
441 | + * @return | |
442 | + */ | |
443 | + public static Timestamp getExpired(Date givenDate, int offset, Integer type) { | |
444 | + Calendar date = Calendar.getInstance(); | |
445 | + date.setTime(givenDate); | |
446 | + date.add(type, offset); | |
447 | + return new Timestamp(date.getTime().getTime()); | |
448 | + } | |
449 | + | |
450 | + /** | |
451 | + * 根据日期取得日历 | |
452 | + * | |
453 | + * @param date | |
454 | + * @return | |
455 | + */ | |
456 | + public static Calendar getCalendar(Date date) { | |
457 | + Calendar c = Calendar.getInstance(); | |
458 | + c.setTime(date); | |
459 | + return c; | |
460 | + } | |
461 | + | |
462 | + /** | |
463 | + * @param offsetSecond | |
464 | + * @return 指定时间 + offsetSecond | |
465 | + */ | |
466 | + public static Timestamp getExpiredHour(Date givenDate, int offsetHour) { | |
467 | + Calendar date = Calendar.getInstance(); | |
468 | + date.setTime(givenDate); | |
469 | + date.add(Calendar.HOUR, offsetHour); | |
470 | + return new Timestamp(date.getTime().getTime()); | |
471 | + } | |
472 | + | |
473 | + /** | |
474 | + * @return 给出指定日期的月份的第一天 | |
475 | + */ | |
476 | + public static Date getMonthFirstDay(Date givenDate) { | |
477 | + Date date = DateUtils.parse(DateUtils.format(givenDate, "yyyy-MM"), | |
478 | + "yyyy-MM"); | |
479 | + return date; | |
480 | + } | |
481 | + | |
482 | + /** | |
483 | + * 取得当前是周几? | |
484 | + * | |
485 | + * @param givenDate | |
486 | + * @return | |
487 | + */ | |
488 | + public static int getDayOfWeek(Date givenDate) { | |
489 | + Calendar c = Calendar.getInstance(); | |
490 | + c.setTime(givenDate); | |
491 | + int day = c.get(Calendar.DAY_OF_WEEK); | |
492 | + return day; | |
493 | + } | |
494 | + | |
495 | + /** | |
496 | + * 取得中文星期? | |
497 | + * | |
498 | + * @param dayOfWeek | |
499 | + * @return | |
500 | + */ | |
501 | + public static String getChineseDayOfWeek(int dayOfWeek) { | |
502 | + return WEEK_DAY.get(dayOfWeek); | |
503 | + } | |
504 | + | |
505 | + /** | |
506 | + * 给定日期是否在范围内 | |
507 | + * | |
508 | + * @param date 给定日期 | |
509 | + * @param begin 开始日期 | |
510 | + * @param end 结束日期 | |
511 | + * @return true 在指定范围内 | |
512 | + */ | |
513 | + public static Boolean between(Date date, Date begin, Date end) { | |
514 | + if (date == null || begin == null || end == null) { | |
515 | + return true; | |
516 | + } | |
517 | + return date.after(begin) && date.before(end); | |
518 | + } | |
519 | + | |
520 | + /** | |
521 | + * 当前日期是否在范围内 | |
522 | + * | |
523 | + * @param begin 开始日期 | |
524 | + * @param end 结束日期 | |
525 | + * @return true 在指定范围内 | |
526 | + */ | |
527 | + public static Boolean between(Date begin, Date end) { | |
528 | + Date now = getNow(); | |
529 | + return between(now, begin, end); | |
530 | + } | |
531 | + | |
532 | + /** | |
533 | + * 取得今天零点日期 | |
534 | + * | |
535 | + * @return | |
536 | + */ | |
537 | + public static Calendar getTodayZero() { | |
538 | + Calendar c = Calendar.getInstance(); | |
539 | + c.set(Calendar.HOUR_OF_DAY, 0); | |
540 | + c.set(Calendar.MINUTE, 0); | |
541 | + c.set(Calendar.SECOND, 0); | |
542 | + c.set(Calendar.MILLISECOND, 0); | |
543 | + return c; | |
544 | + } | |
545 | + | |
546 | + /** | |
547 | + * 是否是日期格式 | |
548 | + * | |
549 | + * @param value | |
550 | + * @return | |
551 | + */ | |
552 | + public static boolean isDate(String value) { | |
553 | + return parse(value) != null; | |
554 | + } | |
555 | + | |
556 | + /** | |
557 | + * <p> | |
558 | + * Parses a string representing a date by trying a variety of different | |
559 | + * parsers. | |
560 | + * </p> | |
561 | + * <p> | |
562 | + * <p> | |
563 | + * The parse will try each parse pattern in turn. A parse is only deemed | |
564 | + * sucessful if it parses the whole of the input string. If no parse | |
565 | + * patterns match, a ParseException is thrown. | |
566 | + * </p> | |
567 | + * | |
568 | + * @param str the date to parse, not null | |
569 | + * @param parsePatterns the date format patterns to use, see SimpleDateFormat, not | |
570 | + * null | |
571 | + * @return the parsed date | |
572 | + * @throws IllegalArgumentException if the date string or pattern array is null | |
573 | + * @throws ParseException if none of the date patterns were suitable | |
574 | + */ | |
575 | + public static Date parseDate(String str, String[] parsePatterns) | |
576 | + throws ParseException { | |
577 | + if (str == null || parsePatterns == null) { | |
578 | + throw new IllegalArgumentException( | |
579 | + "Date and Patterns must not be null"); | |
580 | + } | |
581 | + SimpleDateFormat parser = null; | |
582 | + ParsePosition pos = new ParsePosition(0); | |
583 | + for (int i = 0; i < parsePatterns.length; i++) { | |
584 | + if (i == 0) { | |
585 | + parser = new SimpleDateFormat(parsePatterns[0]); | |
586 | + } else { | |
587 | + parser.applyPattern(parsePatterns[i]); | |
588 | + } | |
589 | + pos.setIndex(0); | |
590 | + Date date = parser.parse(str, pos); | |
591 | + if (date != null && pos.getIndex() == str.length()) { | |
592 | + return date; | |
593 | + } | |
594 | + } | |
595 | + throw new ParseException("Unable to parse the date: " + str, -1); | |
596 | + } | |
597 | + | |
598 | + /** | |
599 | + * 取得java.sql.Date | |
600 | + * | |
601 | + * @param value | |
602 | + * @return | |
603 | + */ | |
604 | + public static java.sql.Date getSqlDate(Object value) { | |
605 | + if (value == null) { | |
606 | + return null; | |
607 | + } | |
608 | + if (value instanceof Date) { | |
609 | + return new java.sql.Date(((Date) value).getTime()); | |
610 | + } else if (value instanceof String) { | |
611 | + Timestamp date = parse((String) value); | |
612 | + return date != null ? new java.sql.Date(date.getTime()) : null; | |
613 | + } | |
614 | + return null; | |
615 | + } | |
616 | + | |
617 | + | |
618 | + /** | |
619 | + * 计算日期表达式 | |
620 | + * | |
621 | + * @return | |
622 | + */ | |
623 | + public static Date getExpired(Object givenDate, String off) { | |
624 | + Date date = null; | |
625 | + if (givenDate instanceof Date) { | |
626 | + date = (Date) givenDate; | |
627 | + } else { | |
628 | + date = parse(StringUtils.asString(givenDate)); | |
629 | + } | |
630 | + String op = String.valueOf(off.charAt(0)); | |
631 | + int offset = Integer.parseInt(String.valueOf(off.charAt(1))); | |
632 | + String type = String.valueOf(off.charAt(2)); | |
633 | + Date result = null; | |
634 | + int dateField = DATE_FIELD.get(type); | |
635 | + if (op.equals("+")) { | |
636 | + result = DateUtils.getExpired(date, +offset, dateField); | |
637 | + } else if (op.equals("-")) { | |
638 | + result = DateUtils.getExpired(date, -offset, dateField); | |
639 | + } | |
640 | + return result; | |
641 | + } | |
642 | + | |
643 | + /** | |
644 | + * 计算日期表达式 | |
645 | + * | |
646 | + * @param expr today + 1d 或 2014-06-29 + 1d | |
647 | + * @return | |
648 | + */ | |
649 | + public static Date eval(String expr) { | |
650 | + expr = expr.trim(); | |
651 | + if (expr.equals("today") || expr.equals("now")) { | |
652 | + return getNow(); | |
653 | + } | |
654 | + String EXPR = "(.+)\\s*(\\+|\\-)\\s*(\\w+)"; | |
655 | + String p1 = expr.replaceAll(EXPR, "$1").trim(); | |
656 | + String p2 = expr.replaceAll(EXPR, "$2").trim(); | |
657 | + String p3 = expr.replaceAll(EXPR, "$3").trim(); | |
658 | + Date date = null; | |
659 | + if (p1.equals("today")) { | |
660 | + date = getNow(); | |
661 | + } else { | |
662 | + p1 = p1.replace("'", "").trim(); | |
663 | + date = parse(p1); | |
664 | + } | |
665 | + Date result = null; | |
666 | + result = getExpired(date, p2 + p3); | |
667 | + return result; | |
668 | + } | |
669 | + | |
670 | + /** | |
671 | + * 计算日期差 | |
672 | + * | |
673 | + * @param a 日期A | |
674 | + * @param b 日期B | |
675 | + * @param type 类型 | |
676 | + * @return 计算结果 | |
677 | + */ | |
678 | + public static Integer sub(Object a, Object b, String type) { | |
679 | + Date d1 = parse(a);//开始日期 | |
680 | + Date d2 = parse(b);//截止日期 | |
681 | + if (d1 == null || d2 == null) { | |
682 | + return null; | |
683 | + } | |
684 | + Long result = null; | |
685 | + long DAY = 24 * 60 * 60 * 1000; | |
686 | + long sub = d1.getTime() - d2.getTime(); | |
687 | + long daysub = (sub / DAY); | |
688 | + long y1 = d1.getYear(), y2 = d2.getYear(); | |
689 | + long m1 = d1.getMonth(), m2 = d2.getMonth(); | |
690 | + long monthsub = (y1 - y2) * 12 + (m1 - m2); | |
691 | + if (type.equals("m")) {//月 | |
692 | + result = monthsub; | |
693 | + } else if (type == "y") {//年 | |
694 | + result = monthsub / 12; | |
695 | + } else if (type.equals("d")) {//天 | |
696 | + result = daysub; | |
697 | + } | |
698 | + return Integer.valueOf(result.intValue()); | |
699 | + } | |
700 | + | |
701 | + public static void main(String[] args) { | |
702 | + System.out.println(getYear(new Date())); | |
703 | + System.out.println(getMonth(new Date())); | |
704 | + System.out.println(eval("today + 1d")); | |
705 | + System.out.println(eval("today - 1d")); | |
706 | + System.out.println(eval("today + 1M")); | |
707 | + System.out.println(eval("today - 1M")); | |
708 | + System.out.println(eval("today + 1y")); | |
709 | + System.out.println(eval("today - 1y")); | |
710 | + | |
711 | + System.out.println(eval("2014-06-29 + 1d")); | |
712 | + System.out.println(eval("2014-06-29 - 1d")); | |
713 | + System.out.println(eval("2014-06-29 + 1M")); | |
714 | + System.out.println(eval("2014-06-29 - 1M")); | |
715 | + System.out.println(eval("2014-06-29 + 1y")); | |
716 | + System.out.println(eval("2014-06-29 - 1y")); | |
717 | + System.out.println(eval("2014-06-29 - 1w")); | |
718 | + System.out.println(getExpired(new Date(), "-1M")); | |
719 | + String now = DateUtils.getNow().toString(); | |
720 | + System.out.println(now + "=" + now + " parse=" + parse(now) + ""); | |
721 | + System.out.println(sub("2014-07-30", "2013-06-29", "d")); | |
722 | + System.out.println(getAge("1982-09-29")); | |
723 | + System.out.println(parse("2015-03-2519:35:21")); | |
724 | + } | |
725 | +} | |
0 | 726 | \ No newline at end of file | ... | ... |
fw-rp-common/src/main/java/cn/fw/rp/common/util/PublicUtil.java
0 → 100644
1 | +++ a/fw-rp-common/src/main/java/cn/fw/rp/common/util/PublicUtil.java | |
1 | +package cn.fw.rp.common.util; | |
2 | + | |
3 | +import org.slf4j.Logger; | |
4 | +import org.slf4j.LoggerFactory; | |
5 | + | |
6 | +import java.io.UnsupportedEncodingException; | |
7 | +import java.lang.reflect.Array; | |
8 | +import java.math.BigDecimal; | |
9 | +import java.net.URLEncoder; | |
10 | +import java.security.MessageDigest; | |
11 | +import java.security.NoSuchAlgorithmException; | |
12 | +import java.text.DecimalFormat; | |
13 | +import java.text.SimpleDateFormat; | |
14 | +import java.time.ZonedDateTime; | |
15 | +import java.time.format.DateTimeFormatter; | |
16 | +import java.util.*; | |
17 | +import java.util.regex.Matcher; | |
18 | +import java.util.regex.Pattern; | |
19 | + | |
20 | +/** | |
21 | + * Created with IntelliJ IDEA | |
22 | + * Created By Devin | |
23 | + * Date: 2018/4/3 | |
24 | + * Time: 17:22 | |
25 | + * Description: 通用工具类 | |
26 | + */ | |
27 | +public class PublicUtil { | |
28 | + public static final String DATA_FORMAT = "yyyy-MM-dd"; | |
29 | + public static final String DATA_FORMAT_CLEAR = "yyyyMMdd"; | |
30 | + public static final String TIME_FORMAT = "yyyy-MM-dd HH:mm:ss"; | |
31 | + public static final String TIME_FORMAT_S = "yyyy-MM-dd HH:mm:ss.sss"; | |
32 | + protected static Logger logger = LoggerFactory.getLogger(PublicUtil.class); | |
33 | + | |
34 | + public PublicUtil() { | |
35 | + } | |
36 | + | |
37 | + public static String toShowHtml(String str) { | |
38 | + if (str == null) { | |
39 | + return null; | |
40 | + } else { | |
41 | + String html = new String(str); | |
42 | + html = replace(html, "&", "&"); | |
43 | + html = replace(html, "<", "<"); | |
44 | + html = replace(html, ">", ">"); | |
45 | + html = replace(html, """, "\""); | |
46 | + html = replace(html, "<br>", "\n"); | |
47 | + return html; | |
48 | + } | |
49 | + } | |
50 | + | |
51 | + public static Boolean containsStr(String source, String str) { | |
52 | + return Boolean.valueOf(isNotEmpty(source) && source.contains(str)); | |
53 | + } | |
54 | + | |
55 | + public static int getLength(String text) { | |
56 | + int textLength = text.length(); | |
57 | + int length = textLength; | |
58 | + | |
59 | + for (int i = 0; i < textLength; ++i) { | |
60 | + if (String.valueOf(text.charAt(i)).getBytes().length > 1) { | |
61 | + ++length; | |
62 | + } | |
63 | + } | |
64 | + | |
65 | + return length % 2 == 0 ? length / 2 : length / 2 + 1; | |
66 | + } | |
67 | + | |
68 | + public static String subMaxString(String str, Integer length) { | |
69 | + if (isNotEmpty(str) && str.length() > length.intValue()) { | |
70 | + Integer realLength = Integer.valueOf(0); | |
71 | + Integer len = Integer.valueOf(str.length()); | |
72 | + | |
73 | + for (int i = 0; i < len.intValue(); ++i) { | |
74 | + if (String.valueOf(str.charAt(i)).getBytes().length > 1) { | |
75 | + realLength = Integer.valueOf(realLength.intValue() + 2); | |
76 | + } else { | |
77 | + realLength = Integer.valueOf(realLength.intValue() + 1); | |
78 | + } | |
79 | + | |
80 | + if (realLength.intValue() > length.intValue()) { | |
81 | + str = toAppendStr(new Object[]{replaceBlank(str).substring(0, i), "..."}); | |
82 | + break; | |
83 | + } | |
84 | + } | |
85 | + } | |
86 | + | |
87 | + return str; | |
88 | + } | |
89 | + | |
90 | + public static String replaceBlank(String str) { | |
91 | + String dest = ""; | |
92 | + if (isNotEmpty(str)) { | |
93 | + Pattern p = Pattern.compile("\\s*|\t|\r|\n"); | |
94 | + Matcher m = p.matcher(str); | |
95 | + dest = m.replaceAll(""); | |
96 | + } | |
97 | + | |
98 | + return dest; | |
99 | + } | |
100 | + | |
101 | + public static String replace(String source, String oldString, String newString) { | |
102 | + StringBuffer output = new StringBuffer(); | |
103 | + if (!source.equals("") && source != null) { | |
104 | + int lengthOfSource = source.length(); | |
105 | + int lengthOfOld = oldString.length(); | |
106 | + | |
107 | + int posStart; | |
108 | + int pos; | |
109 | + for (posStart = 0; (pos = source.indexOf(oldString, posStart)) >= 0; posStart = pos + lengthOfOld) { | |
110 | + output.append(source.substring(posStart, pos)); | |
111 | + output.append(newString); | |
112 | + } | |
113 | + | |
114 | + if (posStart < lengthOfSource) { | |
115 | + output.append(source.substring(posStart)); | |
116 | + } | |
117 | + | |
118 | + return output.toString(); | |
119 | + } else { | |
120 | + return ""; | |
121 | + } | |
122 | + } | |
123 | + | |
124 | + public static String getAuthorizeURLBase(String url, String appId, String componentAppId) { | |
125 | +// url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appId | |
126 | +// + "&redirect_uri=" + url | |
127 | +// + "&response_type=code" | |
128 | +// + "&scope=snsapi_userinfo" | |
129 | +// + "&component_appid=" + componentAppId | |
130 | +// + "&state=123#wechat_redirect"; | |
131 | + url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appId | |
132 | + + "&redirect_uri=" + url | |
133 | + + "&response_type=code" | |
134 | + + "&scope=snsapi_userinfo" | |
135 | + + "&state=123" | |
136 | + + "&component_appid=" + componentAppId | |
137 | + + "&connect_redirect=1#wechat_redirect"; | |
138 | + return url; | |
139 | + } | |
140 | + | |
141 | + public static String getUrlEncode(String url) { | |
142 | + try { | |
143 | + url = URLEncoder.encode(url, "utf-8"); | |
144 | + } catch (UnsupportedEncodingException e) { | |
145 | + e.printStackTrace(); | |
146 | + } | |
147 | + return url; | |
148 | + } | |
149 | + | |
150 | + public static String replaceAll(String str, String oldStr, String newStr) { | |
151 | + int length = str.indexOf(oldStr); | |
152 | + int j = 0; | |
153 | + | |
154 | + for (String temp = ""; length != -1; length = str.indexOf(oldStr)) { | |
155 | + ++j; | |
156 | + if (j == 10) { | |
157 | + break; | |
158 | + } | |
159 | + | |
160 | + temp = str.substring(length + 1); | |
161 | + str = str.substring(0, length); | |
162 | + str = str + newStr + temp; | |
163 | + } | |
164 | + | |
165 | + return str; | |
166 | + } | |
167 | + | |
168 | + public static int parseInt(Object value, Integer defaultValue) { | |
169 | + try { | |
170 | + return Integer.parseInt(String.valueOf(value)); | |
171 | + } catch (Exception var3) { | |
172 | + return defaultValue.intValue(); | |
173 | + } | |
174 | + } | |
175 | + | |
176 | + public static long parseLong(Object value, Long defaultValue) { | |
177 | + try { | |
178 | + return Long.parseLong(String.valueOf(value)); | |
179 | + } catch (Exception var3) { | |
180 | + return defaultValue.longValue(); | |
181 | + } | |
182 | + } | |
183 | + | |
184 | + public static Date parseDate(Object date) { | |
185 | + return parseDate(String.valueOf(date)); | |
186 | + } | |
187 | + | |
188 | + public static Date parseDate(String date) { | |
189 | + try { | |
190 | + SimpleDateFormat ex = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); | |
191 | + return ex.parse(date); | |
192 | + } catch (Exception var2) { | |
193 | + return null; | |
194 | + } | |
195 | + } | |
196 | + | |
197 | + public static Date parseDate(String date, String format) { | |
198 | + try { | |
199 | + SimpleDateFormat ex = new SimpleDateFormat(format); | |
200 | + return ex.parse(date); | |
201 | + } catch (Exception var3) { | |
202 | + return null; | |
203 | + } | |
204 | + } | |
205 | + | |
206 | + public static String parseString(Object value, String defaultValue) { | |
207 | + return null != value && !"null".equals(String.valueOf(value).toLowerCase()) ? String.valueOf(value) : defaultValue; | |
208 | + } | |
209 | + | |
210 | + public static String getCurrentTime() { | |
211 | + return getCurrentTime("yyyy-MM-dd HH:mm:ss"); | |
212 | + } | |
213 | + | |
214 | + public static Date getCurrentDate() { | |
215 | + return new Date(); | |
216 | + } | |
217 | + | |
218 | + public static String getCurrentTime(String format) { | |
219 | + Date nowTime = new Date(); | |
220 | + SimpleDateFormat fmt = new SimpleDateFormat(format); | |
221 | + return fmt.format(nowTime); | |
222 | + } | |
223 | + | |
224 | + public static String getMySQLDateTimeFormat() { | |
225 | + return "yyyy-MM-dd HH:mm:ss"; | |
226 | + } | |
227 | + | |
228 | + public static String fmtDate(Date date, String fmt) { | |
229 | + if (null == date) { | |
230 | + return ""; | |
231 | + } else { | |
232 | + SimpleDateFormat f = new SimpleDateFormat(fmt); | |
233 | + return f.format(date); | |
234 | + } | |
235 | + } | |
236 | + | |
237 | + public static String fmtDate(Date date) { | |
238 | + if (null == date) { | |
239 | + return ""; | |
240 | + } else { | |
241 | + SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); | |
242 | + return f.format(date); | |
243 | + } | |
244 | + } | |
245 | + | |
246 | + /** | |
247 | + * 转换为时间(天,时:分:秒.毫秒) | |
248 | + * | |
249 | + * @param timeMillis | |
250 | + * @return | |
251 | + */ | |
252 | + public static String formatDateTime(long timeMillis) { | |
253 | + long day = timeMillis / (24 * 60 * 60 * 1000); | |
254 | + long hour = (timeMillis / (60 * 60 * 1000) - day * 24); | |
255 | + long min = ((timeMillis / (60 * 1000)) - day * 24 * 60 - hour * 60); | |
256 | + long s = (timeMillis / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60); | |
257 | + long sss = (timeMillis - day * 24 * 60 * 60 * 1000 - hour * 60 * 60 * 1000 - min * 60 * 1000 - s * 1000); | |
258 | + return (day > 0 ? day + "," : "") + hour + ":" + min + ":" + s + "." + sss; | |
259 | + } | |
260 | + | |
261 | + public static String nextId() { | |
262 | + return UUID.randomUUID().toString().replaceAll("-", ""); | |
263 | + } | |
264 | + | |
265 | + public static String getUUID() { | |
266 | + String uid = "0"; | |
267 | + SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmssSSS"); | |
268 | + String tempId = sf.format(new Date()); | |
269 | + if (Long.parseLong(uid) >= Long.parseLong(tempId)) { | |
270 | + uid = Long.parseLong(uid) + 1L + ""; | |
271 | + } else { | |
272 | + uid = tempId; | |
273 | + } | |
274 | + | |
275 | + return uid + getRandomString(10); | |
276 | + } | |
277 | + | |
278 | + public static String getRandomString(int size) { | |
279 | + char[] c = new char[]{'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'z', 'x', 'c', 'v', 'b', 'n', 'm'}; | |
280 | + Random random = new Random(); | |
281 | + StringBuffer sb = new StringBuffer(); | |
282 | + | |
283 | + for (int i = 0; i < size; ++i) { | |
284 | + sb.append(c[Math.abs(random.nextInt()) % c.length]); | |
285 | + } | |
286 | + | |
287 | + return sb.toString(); | |
288 | + } | |
289 | + | |
290 | + public static String getRandomNumber(int size) { | |
291 | + char[] c = new char[]{'1', '2', '3', '4', '5', '6', '7', '8', '9', '0'}; | |
292 | + Random random = new Random(); | |
293 | + StringBuffer sb = new StringBuffer(); | |
294 | + | |
295 | + for (int i = 0; i < size; ++i) { | |
296 | + sb.append(c[Math.abs(random.nextInt()) % c.length]); | |
297 | + } | |
298 | + | |
299 | + return sb.toString(); | |
300 | + } | |
301 | + | |
302 | + public static boolean isNotEmpty(Object obj) { | |
303 | + return !isEmpty(obj); | |
304 | + } | |
305 | + | |
306 | + public static boolean isEmpty(Object obj) { | |
307 | + if (obj == null) { | |
308 | + return true; | |
309 | + } else if (obj instanceof String) { | |
310 | + return obj.equals(""); | |
311 | + } else if (obj instanceof Collection) { | |
312 | + Collection map1 = (Collection) obj; | |
313 | + return map1.size() == 0; | |
314 | + } else if (obj instanceof Map) { | |
315 | + Map map = (Map) obj; | |
316 | + return map.size() == 0; | |
317 | + } else { | |
318 | + return obj.getClass().isArray() ? Array.getLength(obj) == 0 : false; | |
319 | + } | |
320 | + } | |
321 | + | |
322 | + // 获取随机字符串 | |
323 | + public static String getNonceStr() { | |
324 | + String chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; | |
325 | + String res = ""; | |
326 | + for (int i = 0; i < 16; i++) { | |
327 | + Random rd = new Random(); | |
328 | + res += chars.charAt(rd.nextInt(chars.length() - 1)); | |
329 | + } | |
330 | + return res; | |
331 | + } | |
332 | + | |
333 | + public static boolean isDigitalString(String str) { | |
334 | + if (isEmpty(str)) { | |
335 | + return false; | |
336 | + } else { | |
337 | + try { | |
338 | + Double.parseDouble(str); | |
339 | + return true; | |
340 | + } catch (Exception var2) { | |
341 | + return false; | |
342 | + } | |
343 | + } | |
344 | + } | |
345 | + | |
346 | + public static boolean equalsFlag(Object str) { | |
347 | + try { | |
348 | + if (str != null && "true".equals(str)) { | |
349 | + return true; | |
350 | + } | |
351 | + } catch (Exception var2) { | |
352 | + var2.printStackTrace(); | |
353 | + } | |
354 | + | |
355 | + return false; | |
356 | + } | |
357 | + | |
358 | + public static boolean isNumeric(String str) { | |
359 | + try { | |
360 | + return str != null && str.matches("\\d*.?\\d*"); | |
361 | + } catch (Exception var2) { | |
362 | + var2.printStackTrace(); | |
363 | + return false; | |
364 | + } | |
365 | + } | |
366 | + | |
367 | + public static int arrayIndexOf(Object array, Object value) { | |
368 | + if (array != null && value != null) { | |
369 | + int len = Array.getLength(array); | |
370 | + | |
371 | + for (int i = 0; i < len; ++i) { | |
372 | + if (value.equals(Array.get(array, i))) { | |
373 | + return i; | |
374 | + } | |
375 | + } | |
376 | + | |
377 | + return -1; | |
378 | + } else { | |
379 | + return -1; | |
380 | + } | |
381 | + } | |
382 | + | |
383 | + public static String toUtf8String(String s) { | |
384 | + StringBuffer sb = new StringBuffer(); | |
385 | + | |
386 | + for (int i = 0; i < s.length(); ++i) { | |
387 | + char c = s.charAt(i); | |
388 | + if (c >= 0 && c <= 255) { | |
389 | + sb.append(c); | |
390 | + } else { | |
391 | + byte[] b; | |
392 | + try { | |
393 | + b = Character.toString(c).getBytes("utf-8"); | |
394 | + } catch (Exception var7) { | |
395 | + var7.printStackTrace(); | |
396 | + b = new byte[0]; | |
397 | + } | |
398 | + | |
399 | + for (int j = 0; j < b.length; ++j) { | |
400 | + int k = b[j]; | |
401 | + if (k < 0) { | |
402 | + k += 256; | |
403 | + } | |
404 | + | |
405 | + sb.append("%" + Integer.toHexString(k).toUpperCase()); | |
406 | + } | |
407 | + } | |
408 | + } | |
409 | + | |
410 | + return sb.toString(); | |
411 | + } | |
412 | + | |
413 | + public static String sicenToComm(Object value) { | |
414 | + return value == null ? null : (value instanceof Double ? sicenToCommDouble(((Double) value).doubleValue()) : String.valueOf(value)); | |
415 | + } | |
416 | + | |
417 | + public static String sicenToCommDouble(double value) { | |
418 | + String retValue = null; | |
419 | + DecimalFormat df = new DecimalFormat(); | |
420 | + df.setMinimumFractionDigits(0); | |
421 | + df.setMaximumFractionDigits(5); | |
422 | + retValue = df.format(value); | |
423 | + retValue = retValue.replaceAll(",", ""); | |
424 | + return retValue; | |
425 | + } | |
426 | + | |
427 | + public static String roundStr(Double value) { | |
428 | + DecimalFormat df = new DecimalFormat("#.00"); | |
429 | + return df.format(value); | |
430 | + } | |
431 | + | |
432 | + public static Double round(Double value, int count) { | |
433 | + try { | |
434 | + BigDecimal e = new BigDecimal(value.doubleValue()); | |
435 | + return Double.valueOf(e.setScale(count, 4).doubleValue()); | |
436 | + } catch (Exception var3) { | |
437 | + logger.warn("round double" + value); | |
438 | + return Double.valueOf(0.0D); | |
439 | + } | |
440 | + } | |
441 | + | |
442 | + public static Double round(Object value, int count) { | |
443 | + return round(parseDouble(value), count); | |
444 | + } | |
445 | + | |
446 | + public static Double parseDouble(Object value) { | |
447 | + return value == null ? Double.valueOf(0.0D) : parseDouble(String.valueOf(value)); | |
448 | + } | |
449 | + | |
450 | + public static Double parseDouble(String value) { | |
451 | + return parseDouble(value, Double.valueOf(0.0D)); | |
452 | + } | |
453 | + | |
454 | + public static Double parseDouble(String value, Double dafualtVal) { | |
455 | + Double r = dafualtVal; | |
456 | + | |
457 | + try { | |
458 | + if (value != null) { | |
459 | + value = value.trim(); | |
460 | + if (value.endsWith("%")) { | |
461 | + value = value.substring(0, value.length() - 1); | |
462 | + } | |
463 | + | |
464 | + r = Double.valueOf(Double.parseDouble(value)); | |
465 | + } | |
466 | + } catch (Exception var4) { | |
467 | + logger.warn("转换失败:" + var4.getMessage()); | |
468 | + } | |
469 | + | |
470 | + return r; | |
471 | + } | |
472 | + | |
473 | + public static String toLowerCaseFirstOne(String s) { | |
474 | + return Character.isLowerCase(s.charAt(0)) ? s : Character.toLowerCase(s.charAt(0)) + s.substring(1); | |
475 | + } | |
476 | + | |
477 | + public static String toUpperCaseFirstOne(String s) { | |
478 | + return Character.isUpperCase(s.charAt(0)) ? s : Character.toUpperCase(s.charAt(0)) + s.substring(1); | |
479 | + } | |
480 | + | |
481 | + public static String toAppendStr(Object... strs) { | |
482 | + StringBuffer sb = new StringBuffer(); | |
483 | + Object[] var2 = strs; | |
484 | + int var3 = strs.length; | |
485 | + | |
486 | + for (int var4 = 0; var4 < var3; ++var4) { | |
487 | + Object str = var2[var4]; | |
488 | + if (isNotEmpty(str)) { | |
489 | + sb.append(str); | |
490 | + } | |
491 | + } | |
492 | + | |
493 | + return sb.toString(); | |
494 | + } | |
495 | + | |
496 | + public static boolean match(String pattern, String str) { | |
497 | + Pattern p = Pattern.compile(pattern); | |
498 | + Matcher m = p.matcher(str); | |
499 | + return m.find(); | |
500 | + } | |
501 | + | |
502 | + public static String toStrString(Object obj) { | |
503 | + return isNotEmpty(obj) ? String.valueOf(obj) : ""; | |
504 | + } | |
505 | + | |
506 | + public static String toStrStringNull(Object obj) { | |
507 | + return isNotEmpty(obj) ? String.valueOf(obj) : null; | |
508 | + } | |
509 | + | |
510 | + public static Object fmtDate(ZonedDateTime val, String format) { | |
511 | + return val == null ? null : val.format(DateTimeFormatter.ofPattern(format)); | |
512 | + } | |
513 | + | |
514 | + public static Object fmtDate(ZonedDateTime val) { | |
515 | + return fmtDate(val, "yyyy-MM-dd HH:mm:ss"); | |
516 | + } | |
517 | + | |
518 | + public static String getRandomBsString(int length) { | |
519 | + String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; | |
520 | + Random random = new Random(); | |
521 | + StringBuffer sb = new StringBuffer(); | |
522 | + for (int i = 0; i < length; i++) { | |
523 | + int number = random.nextInt(62); | |
524 | + sb.append(str.charAt(number)); | |
525 | + } | |
526 | + return sb.toString(); | |
527 | + } | |
528 | + | |
529 | + /** | |
530 | + * 将字符串进行sha1加密 | |
531 | + * | |
532 | + * @param str 需要加密的字符串 | |
533 | + * @return 加密后的内容 | |
534 | + */ | |
535 | + public static String sha1(String str) { | |
536 | + try { | |
537 | + MessageDigest digest = MessageDigest.getInstance("SHA-1"); | |
538 | + digest.update(str.getBytes()); | |
539 | + byte messageDigest[] = digest.digest(); | |
540 | + // Create Hex String | |
541 | + StringBuffer hexString = new StringBuffer(); | |
542 | + // 字节数组转换为 十六进制 数 | |
543 | + for (int i = 0; i < messageDigest.length; i++) { | |
544 | + String shaHex = Integer.toHexString(messageDigest[i] & 0xFF); | |
545 | + if (shaHex.length() < 2) { | |
546 | + hexString.append(0); | |
547 | + } | |
548 | + hexString.append(shaHex); | |
549 | + } | |
550 | + return hexString.toString(); | |
551 | + | |
552 | + } catch (NoSuchAlgorithmException e) { | |
553 | + e.printStackTrace(); | |
554 | + } | |
555 | + return ""; | |
556 | + } | |
557 | + | |
558 | + public static boolean isBlank(CharSequence cs) { | |
559 | + int strLen; | |
560 | + if (cs != null && (strLen = cs.length()) != 0) { | |
561 | + for (int i = 0; i < strLen; ++i) { | |
562 | + if (!Character.isWhitespace(cs.charAt(i))) { | |
563 | + return false; | |
564 | + } | |
565 | + } | |
566 | + | |
567 | + return true; | |
568 | + } else { | |
569 | + return true; | |
570 | + } | |
571 | + } | |
572 | + | |
573 | + public static boolean isNotBlank(CharSequence cs) { | |
574 | + return !isBlank(cs); | |
575 | + } | |
576 | + | |
577 | + /** | |
578 | + * 替换掉HTML标签方法 | |
579 | + */ | |
580 | + public static String replaceHtml(String html) { | |
581 | + if (isBlank(html)) { | |
582 | + return ""; | |
583 | + } | |
584 | + String regEx = "<.+?>"; | |
585 | + Pattern p = Pattern.compile(regEx); | |
586 | + Matcher m = p.matcher(html); | |
587 | + String s = m.replaceAll(""); | |
588 | + return s; | |
589 | + } | |
590 | +} | |
591 | + | ... | ... |
fw-rp-common/src/main/java/cn/fw/rp/common/util/QRCodeKit.java
0 → 100644
1 | +++ a/fw-rp-common/src/main/java/cn/fw/rp/common/util/QRCodeKit.java | |
1 | +package cn.fw.rp.common.util; | |
2 | + | |
3 | +import com.google.zxing.*; | |
4 | +import com.google.zxing.common.BitMatrix; | |
5 | +import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; | |
6 | +import org.apache.commons.codec.binary.Base64; | |
7 | +import org.apache.commons.codec.binary.Base64OutputStream; | |
8 | + | |
9 | +import javax.imageio.ImageIO; | |
10 | +import java.awt.*; | |
11 | +import java.awt.image.BufferedImage; | |
12 | +import java.io.*; | |
13 | +import java.util.HashMap; | |
14 | +import java.util.Map; | |
15 | + | |
16 | +/** | |
17 | + * QrCodeKit | |
18 | + * | |
19 | + * @author <link>https://www.jianshu.com/p/39c5bca32e3e</link> | |
20 | + */ | |
21 | +public class QRCodeKit { | |
22 | + | |
23 | + public static final String QRCODE_DEFAULT_CHARSET = "UTF-8"; | |
24 | + | |
25 | + public static final int QRCODE_DEFAULT_HEIGHT = 150; | |
26 | + | |
27 | + public static final int QRCODE_DEFAULT_WIDTH = 150; | |
28 | + | |
29 | + private static final int BLACK = 0xFF000000; | |
30 | + private static final int WHITE = 0xFFFFFFFF; | |
31 | + | |
32 | + public static void main(String[] args) throws IOException, NotFoundException { | |
33 | + String data = "当然是测试的二维码啊"; | |
34 | + File logoFile = new File("C:\\Users\\suchu\\Pictures\\erha_hd.jpg"); | |
35 | + BufferedImage image = QRCodeKit.createQRCodeWithLogo(data, logoFile); | |
36 | + String base64Str = QRCodeKit.getImageBase64String(image); | |
37 | + System.out.println(base64Str); | |
38 | + ImageIO.write(image, "png", new File("result_with_logo.png")); | |
39 | + System.out.println("done"); | |
40 | + } | |
41 | + | |
42 | + /** | |
43 | + * Create qrcode with default settings | |
44 | + * | |
45 | + * @param data | |
46 | + * @return | |
47 | + * @author stefli | |
48 | + */ | |
49 | + public static BufferedImage createQRCode(String data) { | |
50 | + return createQRCode(data, QRCODE_DEFAULT_WIDTH, QRCODE_DEFAULT_HEIGHT); | |
51 | + } | |
52 | + | |
53 | + /** | |
54 | + * Create qrcode with default charset | |
55 | + * | |
56 | + * @param data | |
57 | + * @param width | |
58 | + * @param height | |
59 | + * @return | |
60 | + * @author stefli | |
61 | + */ | |
62 | + public static BufferedImage createQRCode(String data, int width, int height) { | |
63 | + return createQRCode(data, QRCODE_DEFAULT_CHARSET, width, height); | |
64 | + } | |
65 | + | |
66 | + /** | |
67 | + * Create qrcode with specified charset | |
68 | + * | |
69 | + * @param data | |
70 | + * @param charset | |
71 | + * @param width | |
72 | + * @param height | |
73 | + * @return | |
74 | + * @author stefli | |
75 | + */ | |
76 | + @SuppressWarnings({"unchecked", "rawtypes"}) | |
77 | + public static BufferedImage createQRCode(String data, String charset, int width, int height) { | |
78 | + Map hint = new HashMap(); | |
79 | + hint.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); | |
80 | + hint.put(EncodeHintType.CHARACTER_SET, charset); | |
81 | + | |
82 | + return createQRCode(data, charset, hint, width, height); | |
83 | + } | |
84 | + | |
85 | + /** | |
86 | + * Create qrcode with specified hint | |
87 | + * | |
88 | + * @param data | |
89 | + * @param charset | |
90 | + * @param hint | |
91 | + * @param width | |
92 | + * @param height | |
93 | + * @return | |
94 | + * @author stefli | |
95 | + */ | |
96 | + public static BufferedImage createQRCode(String data, String charset, Map<EncodeHintType, ?> hint, int width, | |
97 | + int height) { | |
98 | + BitMatrix matrix; | |
99 | + try { | |
100 | + matrix = new MultiFormatWriter().encode(new String(data.getBytes(charset), charset), BarcodeFormat.QR_CODE, | |
101 | + width, height, hint); | |
102 | + return toBufferedImage(matrix); | |
103 | + } catch (WriterException e) { | |
104 | + throw new RuntimeException(e.getMessage(), e); | |
105 | + } catch (Exception e) { | |
106 | + throw new RuntimeException(e.getMessage(), e); | |
107 | + } | |
108 | + } | |
109 | + | |
110 | + | |
111 | + public static BufferedImage toBufferedImage(BitMatrix matrix) { | |
112 | + int width = matrix.getWidth(); | |
113 | + int height = matrix.getHeight(); | |
114 | + BufferedImage image = new BufferedImage(width, height, | |
115 | + BufferedImage.TYPE_INT_RGB); | |
116 | + for (int x = 0; x < width; x++) { | |
117 | + for (int y = 0; y < height; y++) { | |
118 | + image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE); | |
119 | + } | |
120 | + } | |
121 | + return image; | |
122 | + } | |
123 | + | |
124 | + /** | |
125 | + * Create qrcode with default settings and logo | |
126 | + * | |
127 | + * @param data | |
128 | + * @param logoFile | |
129 | + * @return | |
130 | + * @author stefli | |
131 | + */ | |
132 | + public static BufferedImage createQRCodeWithLogo(String data, File logoFile) { | |
133 | + return createQRCodeWithLogo(data, QRCODE_DEFAULT_WIDTH, QRCODE_DEFAULT_HEIGHT, logoFile); | |
134 | + } | |
135 | + | |
136 | + /** | |
137 | + * Create qrcode with default charset and logo | |
138 | + * | |
139 | + * @param data | |
140 | + * @param width | |
141 | + * @param height | |
142 | + * @param logoFile | |
143 | + * @return | |
144 | + * @author stefli | |
145 | + */ | |
146 | + public static BufferedImage createQRCodeWithLogo(String data, int width, int height, File logoFile) { | |
147 | + return createQRCodeWithLogo(data, QRCODE_DEFAULT_CHARSET, width, height, logoFile); | |
148 | + } | |
149 | + | |
150 | + /** | |
151 | + * Create qrcode with specified charset and logo | |
152 | + * | |
153 | + * @param data | |
154 | + * @param charset | |
155 | + * @param width | |
156 | + * @param height | |
157 | + * @param logoFile | |
158 | + * @return | |
159 | + * @author stefli | |
160 | + */ | |
161 | + @SuppressWarnings({"unchecked", "rawtypes"}) | |
162 | + public static BufferedImage createQRCodeWithLogo(String data, String charset, int width, int height, File logoFile) { | |
163 | + Map hint = new HashMap(); | |
164 | + hint.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); | |
165 | + hint.put(EncodeHintType.CHARACTER_SET, charset); | |
166 | + | |
167 | + return createQRCodeWithLogo(data, charset, hint, width, height, logoFile); | |
168 | + } | |
169 | + | |
170 | + /** | |
171 | + * Create qrcode with specified hint and logo | |
172 | + * | |
173 | + * @param data | |
174 | + * @param charset | |
175 | + * @param hint | |
176 | + * @param width | |
177 | + * @param height | |
178 | + * @param logoFile | |
179 | + * @return | |
180 | + * @author stefli | |
181 | + */ | |
182 | + public static BufferedImage createQRCodeWithLogo(String data, String charset, Map<EncodeHintType, ?> hint, | |
183 | + int width, int height, File logoFile) { | |
184 | + try { | |
185 | + BufferedImage qrcode = createQRCode(data, charset, hint, width, height); | |
186 | + | |
187 | + Graphics2D g = qrcode.createGraphics(); | |
188 | + BufferedImage logo = ImageIO.read(logoFile); | |
189 | + //logo最大宽度为二维码宽度的20% | |
190 | + int widthLogo = logo.getWidth(null) > qrcode.getWidth() * 2 / 10 ? | |
191 | + (qrcode.getWidth() * 2 / 10) : logo.getWidth(null); | |
192 | + int heightLogo = logo.getHeight(null) > qrcode.getHeight() * 2 / 10 ? | |
193 | + (qrcode.getHeight() * 2 / 10) : logo.getHeight(null); | |
194 | + int x = (qrcode.getWidth() - widthLogo) / 2; | |
195 | + int y = (qrcode.getHeight() - heightLogo) / 2; | |
196 | + | |
197 | + // 开始绘制图片 | |
198 | + g.drawImage(logo, x, y, widthLogo, heightLogo, null); | |
199 | + g.drawRoundRect(x, y, widthLogo, heightLogo, 15, 15); | |
200 | + //边框宽度 | |
201 | + g.setStroke(new BasicStroke(2)); | |
202 | + //边框颜色 | |
203 | + g.setColor(Color.WHITE); | |
204 | + g.drawRect(x, y, widthLogo, heightLogo); | |
205 | + g.dispose(); | |
206 | + logo.flush(); | |
207 | + qrcode.flush(); | |
208 | + return qrcode; | |
209 | + /* BufferedImage logo = ImageIO.read(logoFile); | |
210 | + int deltaHeight = height - logo.getHeight(); | |
211 | + int deltaWidth = width - logo.getWidth(); | |
212 | + BufferedImage combined = new BufferedImage(height, width, BufferedImage.TYPE_INT_ARGB); | |
213 | + Graphics2D g = (Graphics2D) combined.getGraphics(); | |
214 | + g.drawImage(qrcode, 0, 0, null); | |
215 | + g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f)); | |
216 | + g.drawImage(logo, (int) Math.round(deltaWidth / 2), (int) Math.round(deltaHeight / 2), null); | |
217 | + | |
218 | + return combined;*/ | |
219 | + } catch (IOException e) { | |
220 | + throw new RuntimeException(e.getMessage(), e); | |
221 | + } catch (Exception e) { | |
222 | + throw new RuntimeException(e.getMessage(), e); | |
223 | + } | |
224 | + } | |
225 | + | |
226 | + /** | |
227 | + * Return base64 for image | |
228 | + * | |
229 | + * @param image | |
230 | + * @return | |
231 | + * @author stefli | |
232 | + */ | |
233 | + public static String getImageBase64String(BufferedImage image) { | |
234 | + String result = null; | |
235 | + try { | |
236 | + ByteArrayOutputStream os = new ByteArrayOutputStream(); | |
237 | + OutputStream b64 = new Base64OutputStream(os); | |
238 | + ImageIO.write(image, "png", b64); | |
239 | + result = "data:image/png;base64," + os.toString("UTF-8"); | |
240 | + result = result.replaceAll("\r\n", ""); | |
241 | + } catch (UnsupportedEncodingException e) { | |
242 | + throw new RuntimeException(e.getMessage(), e); | |
243 | + } catch (IOException e) { | |
244 | + throw new RuntimeException(e.getMessage(), e); | |
245 | + } | |
246 | + return result; | |
247 | + } | |
248 | + | |
249 | + | |
250 | + public static String getPureImageBase64String(BufferedImage image) { | |
251 | + String result = null; | |
252 | + try { | |
253 | + ByteArrayOutputStream os = new ByteArrayOutputStream(); | |
254 | + OutputStream b64 = new Base64OutputStream(os); | |
255 | + ImageIO.write(image, "jpg", b64); | |
256 | + result = os.toString("UTF-8"); | |
257 | + } catch (UnsupportedEncodingException e) { | |
258 | + throw new RuntimeException(e.getMessage(), e); | |
259 | + } catch (IOException e) { | |
260 | + throw new RuntimeException(e.getMessage(), e); | |
261 | + } | |
262 | + return result; | |
263 | + } | |
264 | + | |
265 | + /** | |
266 | + * Decode the base64Image data to image | |
267 | + * | |
268 | + * @param base64ImageString | |
269 | + * @param file | |
270 | + * @author stefli | |
271 | + */ | |
272 | + public static void convertBase64StringToImage(String base64ImageString, File file) { | |
273 | + FileOutputStream os; | |
274 | + try { | |
275 | + Base64 d = new Base64(); | |
276 | + byte[] bs = d.decode(base64ImageString); | |
277 | + os = new FileOutputStream(file.getAbsolutePath()); | |
278 | + os.write(bs); | |
279 | + os.close(); | |
280 | + } catch (FileNotFoundException e) { | |
281 | + throw new RuntimeException(e.getMessage(), e); | |
282 | + } catch (IOException e) { | |
283 | + throw new RuntimeException(e.getMessage(), e); | |
284 | + } catch (Exception e) { | |
285 | + throw new RuntimeException(e.getMessage(), e); | |
286 | + } | |
287 | + } | |
288 | + | |
289 | + | |
290 | +} | ... | ... |
fw-rp-common/src/main/java/cn/fw/rp/common/util/StringUtils.java
0 → 100644
1 | +++ a/fw-rp-common/src/main/java/cn/fw/rp/common/util/StringUtils.java | |
1 | +/* | |
2 | + * 创建日期 2011-3-16 | |
3 | + * | |
4 | + * 成都天和软件公司 | |
5 | + * 电话:028-85425861 | |
6 | + * 传真:028-85425861-8008 | |
7 | + * 邮编:610041 | |
8 | + * 地址:成都市武侯区航空路6号丰德万瑞中心B座1001 | |
9 | + * 版权所有 | |
10 | + */ | |
11 | +package cn.fw.rp.common.util; | |
12 | + | |
13 | +import java.io.IOException; | |
14 | +import java.io.UnsupportedEncodingException; | |
15 | +import java.util.ArrayList; | |
16 | +import java.util.Arrays; | |
17 | +import java.util.List; | |
18 | +import java.util.UUID; | |
19 | +import java.util.regex.Matcher; | |
20 | +import java.util.regex.Pattern; | |
21 | + | |
22 | +/** | |
23 | + * 字符串工具 | |
24 | + * | |
25 | + * @author: luox | |
26 | + * @version: 1.0 | |
27 | + * @since 2018-01-15 | |
28 | + */ | |
29 | +public final class StringUtils { | |
30 | + | |
31 | + /** | |
32 | + * 空字符串 | |
33 | + */ | |
34 | + public static final String EMPTY = ""; | |
35 | + | |
36 | + private StringUtils() { | |
37 | + } | |
38 | + | |
39 | + /** | |
40 | + * 首字母小写 | |
41 | + * | |
42 | + * @param s String | |
43 | + * @return String | |
44 | + */ | |
45 | + public static String firstCharLowerCase(String s) { | |
46 | + if (isValid(s)) { | |
47 | + return s.substring(0, 1).toLowerCase() + s.substring(1); | |
48 | + } | |
49 | + return s; | |
50 | + } | |
51 | + | |
52 | + /** | |
53 | + * 删除前缀 | |
54 | + * | |
55 | + * @param s | |
56 | + * @param prefix | |
57 | + * @return | |
58 | + */ | |
59 | + public static String removePrefix(String s, String prefix) { | |
60 | + int index = s.indexOf(prefix); | |
61 | + return index == 0 ? s.substring(prefix.length()) : s; | |
62 | + } | |
63 | + | |
64 | + /** | |
65 | + * 删除后缀 | |
66 | + * | |
67 | + * @param s | |
68 | + * @param suffix | |
69 | + * @return | |
70 | + */ | |
71 | + public static String removeSuffix(String s, String suffix) { | |
72 | + return s.endsWith(suffix) ? s.substring(0, s.length() - suffix.length()) : s; | |
73 | + } | |
74 | + | |
75 | + /** | |
76 | + * 首字母大写 | |
77 | + * | |
78 | + * @param s String | |
79 | + * @return String | |
80 | + */ | |
81 | + public static String firstCharUpperCase(String s) { | |
82 | + if (isValid(s)) { | |
83 | + return s.substring(0, 1).toUpperCase() + s.substring(1); | |
84 | + } | |
85 | + return s; | |
86 | + } | |
87 | + | |
88 | + /** | |
89 | + * 检查对象是否有效 obj != null && obj.toString().length() > 0 | |
90 | + * | |
91 | + * @param obj | |
92 | + * @return boolean | |
93 | + */ | |
94 | + public static boolean isValid(Object obj) { | |
95 | + return obj != null && obj.toString().length() > 0; | |
96 | + } | |
97 | + | |
98 | + /** | |
99 | + * 是否是空的 | |
100 | + * | |
101 | + * @param obj | |
102 | + * @return | |
103 | + */ | |
104 | + public static boolean isEmpty(Object obj) { | |
105 | + return obj == null || obj.toString().length() == 0; | |
106 | + } | |
107 | + | |
108 | + /** | |
109 | + * 转化为String对象 | |
110 | + * | |
111 | + * @param obj | |
112 | + * @return boolean | |
113 | + */ | |
114 | + public static String asString(Object obj) { | |
115 | + return obj != null ? obj.toString() : ""; | |
116 | + } | |
117 | + | |
118 | + /** | |
119 | + * 返回其中一个有效的对象 value != null && value.toString().length() > 0 | |
120 | + * | |
121 | + * @param values | |
122 | + */ | |
123 | + public static String tryThese(Object... values) { | |
124 | + for (int i = 0; i < values.length; i++) { | |
125 | + String value = StringUtils.asString(values[i]); | |
126 | + if (!value.isEmpty()) { | |
127 | + return value; | |
128 | + } | |
129 | + } | |
130 | + return ""; | |
131 | + } | |
132 | + | |
133 | + /** | |
134 | + * EL表达式提供的定义方法 | |
135 | + * | |
136 | + * @param v1 | |
137 | + * @param v2 | |
138 | + * @return | |
139 | + */ | |
140 | + public static String tryThese(String v1, String v2) { | |
141 | + return tryThese(new Object[]{v1, v2}); | |
142 | + } | |
143 | + | |
144 | + /** | |
145 | + * 连接字符串 | |
146 | + * | |
147 | + * @param list | |
148 | + * @param split | |
149 | + * @return 字符串 | |
150 | + */ | |
151 | + public static <T> String join(T[] list, String split) { | |
152 | + return join(list, split, ""); | |
153 | + } | |
154 | + | |
155 | + /** | |
156 | + * 连接字符串 | |
157 | + * | |
158 | + * @param list | |
159 | + * @param split | |
160 | + * @return 字符串 | |
161 | + */ | |
162 | + public static <T> String join(T[] list, String split, String wrap) { | |
163 | + if (list == null) | |
164 | + return null; | |
165 | + StringBuilder s = new StringBuilder(128); | |
166 | + for (int i = 0; i < list.length; i++) { | |
167 | + if (i > 0) { | |
168 | + s.append(split); | |
169 | + } | |
170 | + s.append(wrap + list[i] + wrap); | |
171 | + } | |
172 | + return s.toString(); | |
173 | + } | |
174 | + | |
175 | + /** | |
176 | + * 连接 | |
177 | + * | |
178 | + * @param list | |
179 | + * @param split | |
180 | + * @param wrap | |
181 | + * @return | |
182 | + */ | |
183 | + public static <T> String join(List<T> list, String split, String wrap) { | |
184 | + return join(list.toArray(), split, wrap); | |
185 | + } | |
186 | + | |
187 | + /** | |
188 | + * 连接字符串 | |
189 | + * | |
190 | + * @param list | |
191 | + * @param split | |
192 | + * @return 字符串 | |
193 | + */ | |
194 | + public static String join(List<?> list, String split) { | |
195 | + return join(list.toArray(), split); | |
196 | + } | |
197 | + | |
198 | + /** | |
199 | + * 包裹字符串 id:12, {, } 输出 {id:12} | |
200 | + * | |
201 | + * @param input 输入串 | |
202 | + * @param begin { | |
203 | + * @param end } | |
204 | + * @return String | |
205 | + */ | |
206 | + public static String wrap(String begin, String input, String end) { | |
207 | + if (!input.startsWith(begin)) { | |
208 | + input = begin + input; | |
209 | + } | |
210 | + if (!input.endsWith(end)) { | |
211 | + input = input + end; | |
212 | + } | |
213 | + return input; | |
214 | + } | |
215 | + | |
216 | + /** | |
217 | + * 取得匹配的字符串 | |
218 | + * | |
219 | + * @param input | |
220 | + * @param regex | |
221 | + * @return | |
222 | + */ | |
223 | + public static List<String> matchs(String input, String regex) { | |
224 | + return matchs(input, regex, 0); | |
225 | + } | |
226 | + | |
227 | + /** | |
228 | + * 取得匹配的字符串 | |
229 | + * | |
230 | + * @param input | |
231 | + * @param regex | |
232 | + * @return | |
233 | + */ | |
234 | + public static List<String> matchs(String input, String regex, int group) { | |
235 | + Pattern pattern = Pattern.compile(regex); | |
236 | + Matcher match = pattern.matcher(input); | |
237 | + List<String> matches = new ArrayList<String>(); | |
238 | + while (match.find()) { | |
239 | + matches.add(match.group(group)); | |
240 | + } | |
241 | + return matches; | |
242 | + } | |
243 | + | |
244 | + /** | |
245 | + * 找到匹配的第一个字符串 | |
246 | + * | |
247 | + * @param input | |
248 | + * @param regex | |
249 | + * @param group | |
250 | + * @return | |
251 | + */ | |
252 | + public static String matchFirst(String input, String regex, int group) { | |
253 | + List<String> matches = matchs(input, regex, group); | |
254 | + return matches.isEmpty() ? null : matches.get(0); | |
255 | + } | |
256 | + | |
257 | + /** | |
258 | + * 截取指定长度字符串 | |
259 | + * | |
260 | + * @return | |
261 | + */ | |
262 | + public static String getShorterString(String str, int maxLength) { | |
263 | + return getShorterString(str, "...", maxLength); | |
264 | + } | |
265 | + | |
266 | + /** | |
267 | + * 截取指定长度字符串 | |
268 | + * | |
269 | + * @param input | |
270 | + * @param tail | |
271 | + * @param length | |
272 | + * @return | |
273 | + */ | |
274 | + public static String getShorterString(String input, String tail, int length) { | |
275 | + tail = isValid(tail) ? tail : ""; | |
276 | + StringBuffer buffer = new StringBuffer(512); | |
277 | + try { | |
278 | + int len = input.getBytes("GBK").length; | |
279 | + if (len > length) { | |
280 | + int ln = 0; | |
281 | + for (int i = 0; ln < length; i++) { | |
282 | + String temp = input.substring(i, i + 1); | |
283 | + if (temp.getBytes("GBK").length == 2) | |
284 | + ln += 2; | |
285 | + else | |
286 | + ln++; | |
287 | + | |
288 | + if (ln <= length) | |
289 | + buffer.append(temp); | |
290 | + } | |
291 | + } else { | |
292 | + return input; | |
293 | + } | |
294 | + buffer.append(tail); | |
295 | + } catch (UnsupportedEncodingException e) { | |
296 | + e.printStackTrace(); | |
297 | + } | |
298 | + return buffer.toString(); | |
299 | + } | |
300 | + | |
301 | + /** | |
302 | + * 取得GBK编码 | |
303 | + * | |
304 | + * @return | |
305 | + */ | |
306 | + public static String getBytesString(String input, String code) { | |
307 | + try { | |
308 | + byte[] b = input.getBytes(code); | |
309 | + return Arrays.toString(b); | |
310 | + } catch (UnsupportedEncodingException e) { | |
311 | + return String.valueOf(code.hashCode()); | |
312 | + } | |
313 | + } | |
314 | + | |
315 | + /** | |
316 | + * 转换格式 CUST_INFO_ID - > custInfoId | |
317 | + * | |
318 | + * @param input | |
319 | + * @return | |
320 | + */ | |
321 | + public static String getFieldString(String input) { | |
322 | + if (input == null) { | |
323 | + return null; | |
324 | + } | |
325 | + String field = input.toLowerCase(); | |
326 | + String[] values = field.split("\\_"); | |
327 | + StringBuffer b = new StringBuffer(input.length()); | |
328 | + for (int i = 0; i < values.length; i++) { | |
329 | + if (i == 0) | |
330 | + b.append(values[i]); | |
331 | + else | |
332 | + b.append(firstCharUpperCase(values[i])); | |
333 | + } | |
334 | + return b.toString(); | |
335 | + } | |
336 | + | |
337 | + /** | |
338 | + * 转换格式 CUST_INFO_ID - > custInfoId | |
339 | + * | |
340 | + * @param columnName | |
341 | + * @return | |
342 | + */ | |
343 | + public static String toFieldName(String columnName) { | |
344 | + return getFieldString(columnName); | |
345 | + } | |
346 | + | |
347 | + /** | |
348 | + * 转换格式 custInfoId - > CUST_INFO_ID | |
349 | + * | |
350 | + * @param field | |
351 | + * @return | |
352 | + */ | |
353 | + public static String toColumnName(String field) { | |
354 | + if (field == null) { | |
355 | + return null; | |
356 | + } | |
357 | + StringBuffer b = new StringBuffer(field.length() + 3); | |
358 | + for (int i = 0; i < field.length(); i++) { | |
359 | + Character char1 = field.charAt(i); | |
360 | + if (Character.isUpperCase(char1) && i != 0) { | |
361 | + b.append("_"); | |
362 | + } | |
363 | + b.append(char1); | |
364 | + } | |
365 | + return b.toString(); | |
366 | + } | |
367 | + | |
368 | + /** | |
369 | + * 转化为JSON值 | |
370 | + * | |
371 | + * @param value | |
372 | + * @return | |
373 | + * @throws IOException | |
374 | + */ | |
375 | + public static String toJsonValue(Object value) throws IOException { | |
376 | + if (value instanceof Number) { | |
377 | + return value.toString(); | |
378 | + } else { | |
379 | + return "'" + value.toString() + "'"; | |
380 | + } | |
381 | + } | |
382 | + | |
383 | + /** | |
384 | + * 字符串转化为UUID | |
385 | + * | |
386 | + * @param value | |
387 | + * @return | |
388 | + */ | |
389 | + public static String toUUID(String value) { | |
390 | + if (value == null) | |
391 | + throw new RuntimeException("value is null!"); | |
392 | + return UUID.nameUUIDFromBytes(value.getBytes()).toString(); | |
393 | + } | |
394 | + | |
395 | + /** | |
396 | + * 获取Style样式中样式的值 | |
397 | + * | |
398 | + * @param styleString | |
399 | + * @param styleName | |
400 | + * @return 相应的值 | |
401 | + */ | |
402 | + public static String getStyleValue(String styleString, String styleName) { | |
403 | + String[] styles = styleString.split(";"); | |
404 | + for (int i = 0; i < styles.length; i++) { | |
405 | + String tempValue = styles[i].trim(); | |
406 | + if (tempValue.startsWith(styleName)) { | |
407 | + String[] style = tempValue.split(":"); | |
408 | + return style[1]; | |
409 | + } | |
410 | + } | |
411 | + return ""; | |
412 | + } | |
413 | + | |
414 | + /** | |
415 | + * 生成重复次字符 | |
416 | + * | |
417 | + * @param charactor | |
418 | + * @param repeat | |
419 | + * @return | |
420 | + */ | |
421 | + public static String getRepeat(String charactor, int repeat) { | |
422 | + return repeat(charactor, repeat, ""); | |
423 | + } | |
424 | + | |
425 | + /** | |
426 | + * 生成重复次字符 | |
427 | + * | |
428 | + * @param charactor | |
429 | + * @param repeat | |
430 | + * @return | |
431 | + */ | |
432 | + public static String repeat(String charactor, int repeat, String split) { | |
433 | + StringBuilder s = new StringBuilder(charactor.length() * repeat); | |
434 | + for (int i = 0; i < repeat; i++) { | |
435 | + if (i != 0) { | |
436 | + s.append(split != null ? split : ""); | |
437 | + } | |
438 | + s.append(charactor); | |
439 | + } | |
440 | + return s.toString(); | |
441 | + } | |
442 | + | |
443 | + /** | |
444 | + * 取得长度 | |
445 | + * | |
446 | + * @param text | |
447 | + * @return | |
448 | + */ | |
449 | + public static int length(String text) { | |
450 | + int len = text.length(); | |
451 | + try { | |
452 | + len = text.getBytes("GBK").length;//SQLServer数据库用的GBK编码 | |
453 | + } catch (UnsupportedEncodingException e) { | |
454 | + e.printStackTrace(); | |
455 | + } | |
456 | + return len; | |
457 | + } | |
458 | + | |
459 | + /** | |
460 | + * 字符串替换函数 | |
461 | + * | |
462 | + * @param data 字符串 | |
463 | + * @param data from 旧值 | |
464 | + * @param to from 新值 | |
465 | + */ | |
466 | + public static String replaceString(String data, String from, String to) { | |
467 | + StringBuffer buf = new StringBuffer(data.length()); | |
468 | + int pos = -1; | |
469 | + int i = 0; | |
470 | + while ((pos = data.indexOf(from, i)) != -1) { | |
471 | + buf.append(data.substring(i, pos)).append(to); | |
472 | + i = pos + from.length(); | |
473 | + } | |
474 | + buf.append(data.substring(i)); | |
475 | + return buf.toString(); | |
476 | + } | |
477 | + | |
478 | + | |
479 | + public static void main(String[] args) { | |
480 | + System.out.println(toUUID("1")); | |
481 | + System.out.println(removePrefix("abcd123", "ab")); | |
482 | + System.out.println(removeSuffix("abcd123", "123")); | |
483 | + System.out.println(toColumnName("usernameid")); | |
484 | + System.out.println(getFieldString(toColumnName("userNameId"))); | |
485 | + System.out.println(repeat("?", 10, ",")); | |
486 | + length("AAA中国()111222bb"); | |
487 | + } | |
488 | +} | |
0 | 489 | \ No newline at end of file | ... | ... |
fw-rp-dao/pom.xml
0 → 100644
1 | +++ a/fw-rp-dao/pom.xml | |
1 | +<?xml version="1.0" encoding="UTF-8"?> | |
2 | +<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
3 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" | |
4 | + xmlns="http://maven.apache.org/POM/4.0.0"> | |
5 | + <modelVersion>4.0.0</modelVersion> | |
6 | + <parent> | |
7 | + <groupId>cn.fw</groupId> | |
8 | + <artifactId>fw-rp</artifactId> | |
9 | + <version>1.0</version> | |
10 | + </parent> | |
11 | + | |
12 | + <artifactId>fw-rp-dao</artifactId> | |
13 | + <packaging>jar</packaging> | |
14 | + <name>fw-rp-dao</name> | |
15 | + | |
16 | + <dependencies> | |
17 | + <dependency> | |
18 | + <groupId>org.mybatis.spring.boot</groupId> | |
19 | + <artifactId>mybatis-spring-boot-starter</artifactId> | |
20 | + </dependency> | |
21 | + <dependency> | |
22 | + <groupId>cn.fw</groupId> | |
23 | + <artifactId>fw-rp-domain</artifactId> | |
24 | + <version>${project.parent.version}</version> | |
25 | + </dependency> | |
26 | + <dependency> | |
27 | + <groupId>cn.fw</groupId> | |
28 | + <artifactId>fw-rp-common</artifactId> | |
29 | + <version>${project.parent.version}</version> | |
30 | + </dependency> | |
31 | + <dependency> | |
32 | + <groupId>cn.fw</groupId> | |
33 | + <artifactId>fw-data-base</artifactId> | |
34 | + </dependency> | |
35 | + </dependencies> | |
36 | + | |
37 | +</project> | ... | ... |
fw-rp-dao/src/main/java/cn/fw/rp/dao/FileListDao.java
0 → 100644
1 | +++ a/fw-rp-dao/src/main/java/cn/fw/rp/dao/FileListDao.java | |
1 | +package cn.fw.rp.dao; | |
2 | + | |
3 | +import cn.fw.data.base.db.dao.BaseDao; | |
4 | +import cn.fw.rp.db.FileList; | |
5 | +import org.apache.ibatis.annotations.Mapper; | |
6 | + | |
7 | +/** | |
8 | + * 2018年5月8日 15点26分 | |
9 | + * | |
10 | + * @author suchu | |
11 | + */ | |
12 | +@Mapper | |
13 | +public interface FileListDao extends BaseDao<FileList, Integer> { | |
14 | + FileList selectByFileId(String fileId); | |
15 | +} | ... | ... |
fw-rp-dao/src/main/java/cn/fw/rp/dao/ProblemFileDao.java
0 → 100644
1 | +++ a/fw-rp-dao/src/main/java/cn/fw/rp/dao/ProblemFileDao.java | |
1 | +package cn.fw.rp.dao; | |
2 | + | |
3 | +import cn.fw.data.base.db.dao.BaseDao; | |
4 | +import cn.fw.rp.db.ProblemFile; | |
5 | +import org.apache.ibatis.annotations.Mapper; | |
6 | + | |
7 | +/** | |
8 | + * 2018年5月8日 15点26分 | |
9 | + * | |
10 | + * @author suchu | |
11 | + */ | |
12 | +@Mapper | |
13 | +public interface ProblemFileDao extends BaseDao<ProblemFile, Integer> { | |
14 | +} | ... | ... |
fw-rp-dao/src/main/java/cn/fw/rp/dao/ProblemListDao.java
0 → 100644
1 | +++ a/fw-rp-dao/src/main/java/cn/fw/rp/dao/ProblemListDao.java | |
1 | +package cn.fw.rp.dao; | |
2 | + | |
3 | +import cn.fw.data.base.db.dao.BaseDao; | |
4 | +import cn.fw.rp.db.ProblemFile; | |
5 | +import cn.fw.rp.db.ProblemList; | |
6 | +import org.apache.ibatis.annotations.Mapper; | |
7 | +import org.apache.ibatis.annotations.Param; | |
8 | + | |
9 | +import java.util.List; | |
10 | +import java.util.Map; | |
11 | + | |
12 | +/** | |
13 | + * @author suchu | |
14 | + * @since 2018/5/8 15:23 | |
15 | + */ | |
16 | +@Mapper | |
17 | +public interface ProblemListDao extends BaseDao<ProblemList, Integer> { | |
18 | + List<ProblemFile> getFiles(@Param("flag") Integer flag, @Param("problemId") Integer problemId); | |
19 | + | |
20 | + List<Map> fileCount(Integer problemId); | |
21 | +} | ... | ... |
fw-rp-dao/src/main/resources/mapper/FileList.xml
0 → 100644
1 | +++ a/fw-rp-dao/src/main/resources/mapper/FileList.xml | |
1 | +<?xml version="1.0" encoding="UTF-8" ?> | |
2 | +<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" | |
3 | + "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> | |
4 | +<mapper namespace="cn.fw.rp.dao.FileListDao"> | |
5 | + | |
6 | + <!-- 查询数量 --> | |
7 | + <select id="selectCount" parameterType="QueryCriterion" resultType="java.lang.Integer"> | |
8 | + select count(1) from file_list | |
9 | + <include refid="core.where_condition"/> | |
10 | + </select> | |
11 | + | |
12 | + <!-- 查询,根据主键 --> | |
13 | + <select id="selectById" parameterType="Long" resultMap="fileListMap"> | |
14 | + select * | |
15 | + from file_list | |
16 | + where id = #{id} | |
17 | + </select> | |
18 | + | |
19 | + <select id="selectByFileId" parameterType="String" resultMap="fileListMap"> | |
20 | + select * | |
21 | + from file_list | |
22 | + where file_id = #{fileId} | |
23 | + </select> | |
24 | + | |
25 | + <!-- 查询列表,根据条件 --> | |
26 | + <select id="selectList" parameterType="QueryCriterion" resultMap="fileListMap"> | |
27 | + select * from file_list | |
28 | + <include refid="core.where_condition"/> | |
29 | + <include refid="core.page_condition"/> | |
30 | + </select> | |
31 | + | |
32 | + <!-- 更新,根据主键 --> | |
33 | + <update id="updateById" parameterType="FileList"> | |
34 | + update file_list | |
35 | + <include refid="update_columns"/> | |
36 | + where id = #{t.id} | |
37 | + </update> | |
38 | + | |
39 | + <!-- 更新列表,根据条件 --> | |
40 | + <update id="updateList"> | |
41 | + update marketing_comment | |
42 | + <include refid="update_columns"/> | |
43 | + <include refid="core.where_condition_pre_con"/> | |
44 | + </update> | |
45 | + | |
46 | + <!-- 新增 --> | |
47 | + <insert id="insert" useGeneratedKeys="true" keyProperty="id" parameterType="FileList"> | |
48 | + insert into file_list | |
49 | + (file_id, | |
50 | + file_name, | |
51 | + content_type, | |
52 | + upload_time, | |
53 | + upload_user, | |
54 | + file_path, | |
55 | + flag, | |
56 | + file_length) | |
57 | + values (#{fileId}, | |
58 | + #{fileName}, | |
59 | + #{contentType}, | |
60 | + now(), | |
61 | + #{uploadUser}, | |
62 | + #{filePath}, | |
63 | + #{flag}, | |
64 | + #{fileLength}) | |
65 | + </insert> | |
66 | + | |
67 | + <!-- 删除,根据主键 --> | |
68 | + <delete id="deleteById" parameterType="Long"> | |
69 | + delete | |
70 | + from file_list | |
71 | + where id = #{id} | |
72 | + </delete> | |
73 | + | |
74 | + <!-- 删除列表,根据条件 --> | |
75 | + <delete id="deleteList" parameterType="QueryCriterion"> | |
76 | + delete from file_list | |
77 | + <include refid="core.where_condition"/> | |
78 | + </delete> | |
79 | + | |
80 | + <!-- 查询结果集 --> | |
81 | + <resultMap id="fileListMap" type="FileList"> | |
82 | + <id column="id" jdbcType="BIGINT" property="id"/> | |
83 | + <result column="file_id" jdbcType="INTEGER" property="fileId"/> | |
84 | + <result column="file_name" jdbcType="VARCHAR" property="fileName"/> | |
85 | + <result column="content_type" jdbcType="VARCHAR" property="contentType"/> | |
86 | + <result column="upload_time" jdbcType="VARCHAR" property="uploadTime"/> | |
87 | + <result column="upload_user" jdbcType="VARCHAR" property="uploadUser"/> | |
88 | + <result column="file_path" jdbcType="VARCHAR" property="filePath"/> | |
89 | + <result column="flag" jdbcType="INTEGER" property="flag"/> | |
90 | + <result column="file_length" jdbcType="INTEGER" property="fileLength"/> | |
91 | + </resultMap> | |
92 | + | |
93 | + <!-- 更新字段列表 --> | |
94 | + <sql id="update_columns"> | |
95 | + <set> | |
96 | + <if test="t.problemId != null"><![CDATA[problem_id = #{t.problemId},]]></if> | |
97 | + <if test="t.fileId != null"><![CDATA[file_id = #{t.fileId},]]></if> | |
98 | + </set> | |
99 | + </sql> | |
100 | +</mapper> | |
0 | 101 | \ No newline at end of file | ... | ... |
fw-rp-dao/src/main/resources/mapper/ProblemFile.xml
0 → 100644
1 | +++ a/fw-rp-dao/src/main/resources/mapper/ProblemFile.xml | |
1 | +<?xml version="1.0" encoding="UTF-8" ?> | |
2 | +<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" | |
3 | + "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> | |
4 | +<mapper namespace="cn.fw.rp.dao.ProblemFileDao"> | |
5 | + | |
6 | + <!-- 查询数量 --> | |
7 | + <select id="selectCount" parameterType="QueryCriterion" resultType="java.lang.Integer"> | |
8 | + select count(1) from problem_file | |
9 | + <include refid="core.where_condition"/> | |
10 | + </select> | |
11 | + | |
12 | + <!-- 查询,根据主键 --> | |
13 | + <select id="selectById" parameterType="Long" resultMap="problemFileMap"> | |
14 | + select * | |
15 | + from problem_file | |
16 | + where id = #{id} | |
17 | + </select> | |
18 | + | |
19 | + | |
20 | + <!-- 查询列表,根据条件 --> | |
21 | + <select id="selectList" parameterType="QueryCriterion" resultMap="problemFileMap"> | |
22 | + select * from problem_file | |
23 | + <include refid="core.where_condition"/> | |
24 | + <include refid="core.page_condition"/> | |
25 | + </select> | |
26 | + | |
27 | + <!-- 更新,根据主键 --> | |
28 | + <update id="updateById" parameterType="ProblemFile"> | |
29 | + update problem_file | |
30 | + <include refid="update_columns"/> | |
31 | + where id = #{t.id} | |
32 | + </update> | |
33 | + | |
34 | + <!-- 更新列表,根据条件 --> | |
35 | + <update id="updateList"> | |
36 | + update marketing_comment | |
37 | + <include refid="update_columns"/> | |
38 | + <include refid="core.where_condition_pre_con"/> | |
39 | + </update> | |
40 | + | |
41 | + <!-- 新增 --> | |
42 | + <insert id="insert" useGeneratedKeys="true" keyProperty="id" parameterType="ProblemFile"> | |
43 | + insert into problem_file | |
44 | + (problem_id, | |
45 | + file_id) | |
46 | + values (#{problemId}, | |
47 | + #{fileId}) | |
48 | + </insert> | |
49 | + | |
50 | + <!-- 删除,根据主键 --> | |
51 | + <delete id="deleteById" parameterType="Long"> | |
52 | + delete | |
53 | + from problem_file | |
54 | + where id = #{id} | |
55 | + </delete> | |
56 | + | |
57 | + <!-- 删除列表,根据条件 --> | |
58 | + <delete id="deleteList" parameterType="QueryCriterion"> | |
59 | + delete from problem_file | |
60 | + <include refid="core.where_condition"/> | |
61 | + </delete> | |
62 | + | |
63 | + <!-- 查询结果集 --> | |
64 | + <resultMap id="problemFileMap" type="ProblemFile"> | |
65 | + <id column="id" jdbcType="BIGINT" property="id"/> | |
66 | + <result column="problem_id" jdbcType="VARCHAR" property="problemId"/> | |
67 | + <result column="file_id" jdbcType="VARCHAR" property="fileId"/> | |
68 | + </resultMap> | |
69 | + | |
70 | + <!-- 更新字段列表 --> | |
71 | + <sql id="update_columns"> | |
72 | + <set> | |
73 | + <if test="t.problemId != null"><![CDATA[problem_id = #{t.problemId},]]></if> | |
74 | + <if test="t.fileId != null"><![CDATA[file_id = #{t.fileId},]]></if> | |
75 | + </set> | |
76 | + </sql> | |
77 | +</mapper> | |
0 | 78 | \ No newline at end of file | ... | ... |
fw-rp-dao/src/main/resources/mapper/ProblemList.xml
0 → 100644
1 | +++ a/fw-rp-dao/src/main/resources/mapper/ProblemList.xml | |
1 | +<?xml version="1.0" encoding="UTF-8" ?> | |
2 | +<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" | |
3 | + "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> | |
4 | +<mapper namespace="cn.fw.rp.dao.ProblemListDao"> | |
5 | + | |
6 | + <!-- 查询数量 --> | |
7 | + <select id="selectCount" parameterType="QueryCriterion" resultType="java.lang.Integer"> | |
8 | + select count(1) from problem_list | |
9 | + <include refid="core.where_condition"/> | |
10 | + </select> | |
11 | + | |
12 | + <!-- 查询,根据主键 --> | |
13 | + <select id="selectById" parameterType="Integer" resultMap="problemListMap"> | |
14 | + select * | |
15 | + from problem_list | |
16 | + where id = #{id} | |
17 | + </select> | |
18 | + | |
19 | + <select id="getFiles" resultMap="fileListMap"> | |
20 | + SELECT | |
21 | + b.* | |
22 | + FROM | |
23 | + problem_file a | |
24 | + JOIN file_list b ON a.file_id = b.file_id | |
25 | + WHERE | |
26 | + 1 = 1 | |
27 | + <if test="flag !=null"> | |
28 | + AND b.flag = #{flag} | |
29 | + </if> | |
30 | + <if test="problemId !=null"> | |
31 | + AND a.problem_id = #{problemId} | |
32 | + </if> | |
33 | + order by b.upload_time desc | |
34 | + </select> | |
35 | + | |
36 | + <select id="fileCount" parameterType="Integer" resultType="java.util.Map"> | |
37 | + select flag, | |
38 | + count(b.file_id) as count | |
39 | + from problem_file a | |
40 | + join file_list b on a.file_id = b.file_id | |
41 | + where problem_id = #{problemId} | |
42 | + GROUP BY flag | |
43 | + </select> | |
44 | + | |
45 | + | |
46 | + <!-- 查询列表,根据条件 --> | |
47 | + <select id="selectList" parameterType="QueryCriterion" resultMap="problemListMap"> | |
48 | + select * from problem_list | |
49 | + <include refid="core.where_condition"/> | |
50 | + <include refid="core.page_condition"/> | |
51 | + </select> | |
52 | + | |
53 | + <!-- 更新,根据主键 --> | |
54 | + <update id="updateById" parameterType="ProblemList"> | |
55 | + update problem_list | |
56 | + <include refid="update_columns"/> | |
57 | + where id = #{t.id} | |
58 | + </update> | |
59 | + | |
60 | + <!-- 更新列表,根据条件 --> | |
61 | + <update id="updateList"> | |
62 | + update marketing_comment | |
63 | + <include refid="update_columns"/> | |
64 | + <include refid="core.where_condition_pre_con"/> | |
65 | + </update> | |
66 | + | |
67 | + <!-- 新增 --> | |
68 | + <insert id="insert" useGeneratedKeys="true" keyProperty="id" parameterType="ProblemList"> | |
69 | + insert into problem_list | |
70 | + (system_ref, | |
71 | + module_ref, | |
72 | + title, | |
73 | + content, | |
74 | + purpose, | |
75 | + question, | |
76 | + scenarios, | |
77 | + logic, | |
78 | + status, | |
79 | + create_time, | |
80 | + update_time, | |
81 | + create_user, | |
82 | + update_user) | |
83 | + values (#{systemRef}, | |
84 | + #{moduleRef}, | |
85 | + #{title}, | |
86 | + #{content}, | |
87 | + #{purpose}, | |
88 | + #{question}, | |
89 | + #{scenarios}, | |
90 | + #{logic}, | |
91 | + #{status}, | |
92 | + now(), | |
93 | + #{updateTime}, | |
94 | + #{createUser}, | |
95 | + #{updateUser}) | |
96 | + </insert> | |
97 | + | |
98 | + <!-- 删除,根据主键 --> | |
99 | + <delete id="deleteById" parameterType="Long"> | |
100 | + delete | |
101 | + from problem_list | |
102 | + where id = #{id} | |
103 | + </delete> | |
104 | + | |
105 | + <!-- 删除列表,根据条件 --> | |
106 | + <delete id="deleteList" parameterType="QueryCriterion"> | |
107 | + delete from problem_list | |
108 | + <include refid="core.where_condition"/> | |
109 | + </delete> | |
110 | + | |
111 | + <!-- 查询结果集 --> | |
112 | + <resultMap id="problemListMap" type="ProblemList"> | |
113 | + <id column="id" jdbcType="BIGINT" property="id"/> | |
114 | + <result column="system_ref" jdbcType="VARCHAR" property="systemRef"/> | |
115 | + <result column="module_ref" jdbcType="VARCHAR" property="moduleRef"/> | |
116 | + <result column="title" jdbcType="VARCHAR" property="title"/> | |
117 | + <result column="content" jdbcType="VARCHAR" property="content"/> | |
118 | + <result column="purpose" jdbcType="VARCHAR" property="purpose"/> | |
119 | + <result column="question" jdbcType="VARCHAR" property="question"/> | |
120 | + <result column="scenarios" jdbcType="VARCHAR" property="scenarios"/> | |
121 | + <result column="logic" jdbcType="VARCHAR" property="logic"/> | |
122 | + <result column="status" jdbcType="INTEGER" property="status"/> | |
123 | + <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/> | |
124 | + <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/> | |
125 | + <result column="create_user" jdbcType="VARCHAR" property="createUser"/> | |
126 | + <result column="update_user" jdbcType="VARCHAR" property="updateUser"/> | |
127 | + </resultMap> | |
128 | + | |
129 | + <!-- 查询结果集 --> | |
130 | + <resultMap id="fileListMap" type="FileList"> | |
131 | + <id column="id" jdbcType="BIGINT" property="id"/> | |
132 | + <result column="file_id" jdbcType="INTEGER" property="fileId"/> | |
133 | + <result column="file_name" jdbcType="VARCHAR" property="fileName"/> | |
134 | + <result column="content_type" jdbcType="VARCHAR" property="contentType"/> | |
135 | + <result column="upload_time" jdbcType="VARCHAR" property="uploadTime"/> | |
136 | + <result column="upload_user" jdbcType="VARCHAR" property="uploadUser"/> | |
137 | + <result column="file_path" jdbcType="VARCHAR" property="filePath"/> | |
138 | + <result column="flag" jdbcType="INTEGER" property="flag"/> | |
139 | + <result column="file_length" jdbcType="INTEGER" property="fileLength"/> | |
140 | + </resultMap> | |
141 | + | |
142 | + | |
143 | + <!-- 更新字段列表 --> | |
144 | + <sql id="update_columns"> | |
145 | + <set> | |
146 | + <if test="t.systemRef != null"><![CDATA[system_ref = #{t.systemRef},]]></if> | |
147 | + <if test="t.moduleRef != null"><![CDATA[module_ref = #{t.moduleRef},]]></if> | |
148 | + <if test="t.title != null"><![CDATA[title=#{t.title},]]></if> | |
149 | + <if test="t.content != null"><![CDATA[content = #{t.content},]]></if> | |
150 | + <if test="t.purpose != null"><![CDATA[purpose = #{t.purpose},]]></if> | |
151 | + <if test="t.question != null"><![CDATA[question = #{t.question},]]></if> | |
152 | + <if test="t.scenarios != null"><![CDATA[scenarios = #{t.scenarios},]]></if> | |
153 | + <if test="t.logic != null"><![CDATA[logic = #{t.logic},]]></if> | |
154 | + <if test="t.status != null"><![CDATA[status = #{t.status},]]></if> | |
155 | + <if test="t.createTime != null"><![CDATA[create_time = #{t.createTime},]]></if> | |
156 | + <if test="t.updateTime != null"><![CDATA[update_time = #{t.updateTime},]]></if> | |
157 | + <if test="t.createUser != null"><![CDATA[create_user = #{t.createUser},]]></if> | |
158 | + <if test="t.updateUser != null"><![CDATA[update_user = #{t.updateUser},]]></if> | |
159 | + </set> | |
160 | + </sql> | |
161 | +</mapper> | |
0 | 162 | \ No newline at end of file | ... | ... |
fw-rp-dao/src/main/resources/mapper/core.xml
0 → 100644
1 | +++ a/fw-rp-dao/src/main/resources/mapper/core.xml | |
1 | +<?xml version="1.0" encoding="UTF-8" ?> | |
2 | +<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" | |
3 | + "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> | |
4 | +<mapper namespace="core"> | |
5 | + <!-- 排序分页 --> | |
6 | + <sql id="page_condition"> | |
7 | + <if test="orderbyField != null"> | |
8 | + <![CDATA[order by ]]> | |
9 | + <foreach collection="orderbyField" index="col" item="val" separator=","> | |
10 | + <![CDATA[${col} ${val}]]> | |
11 | + </foreach> | |
12 | + </if> | |
13 | + <if test="startIdx != null and pageSize != null"> | |
14 | + <![CDATA[limit #{startIdx},#{pageSize} ]]> | |
15 | + </if> | |
16 | + </sql> | |
17 | + | |
18 | + <!-- 查询条件 --> | |
19 | + <sql id="where_condition"> | |
20 | + <where> | |
21 | + 1=1 | |
22 | + <if test="gtField != null"> | |
23 | + <foreach collection="gtField" index="col" item="val"> | |
24 | + <if test="val != null"><![CDATA[ and ${col} > #{val}]]></if> | |
25 | + </foreach> | |
26 | + </if> | |
27 | + <if test="gteField != null"> | |
28 | + <foreach collection="gteField" index="col" item="val"> | |
29 | + <if test="val != null"><![CDATA[ and ${col} >= #{val}]]></if> | |
30 | + </foreach> | |
31 | + </if> | |
32 | + <if test="ltField != null"> | |
33 | + <foreach collection="ltField" index="col" item="val"> | |
34 | + <if test="val != null"><![CDATA[ and ${col} < #{val}]]></if> | |
35 | + </foreach> | |
36 | + </if> | |
37 | + <if test="lteField != null"> | |
38 | + <foreach collection="lteField" index="col" item="val"> | |
39 | + <if test="val != null"><![CDATA[ and ${col} <= #{val}]]></if> | |
40 | + </foreach> | |
41 | + </if> | |
42 | + <if test="eqField != null"> | |
43 | + <foreach collection="eqField" index="col" item="val"> | |
44 | + <if test="val != null"><![CDATA[ and ${col} = #{val}]]></if> | |
45 | + </foreach> | |
46 | + </if> | |
47 | + <if test="neqField != null"> | |
48 | + <foreach collection="neqField" index="col" item="val"> | |
49 | + <if test="val != null"><![CDATA[ and ${col} != #{val}]]></if> | |
50 | + </foreach> | |
51 | + </if> | |
52 | + <if test="nullField != null"> | |
53 | + <foreach collection="nullField" item="val"><![CDATA[ and ${val} is null]]></foreach> | |
54 | + </if> | |
55 | + <if test="notNullField != null"> | |
56 | + <foreach collection="notNullField" item="val"><![CDATA[ and ${val} is not null]]></foreach> | |
57 | + </if> | |
58 | + <if test="likeField != null"> | |
59 | + <foreach collection="likeField" index="col" item="val"> | |
60 | + <if test="val != null"><![CDATA[ and ${col} like CONCAT('%','${val}','%' )]]></if> | |
61 | + </foreach> | |
62 | + </if> | |
63 | + <if test="notLikeField != null"> | |
64 | + <foreach collection="notLikeField" index="col" item="val"> | |
65 | + <if test="val != null"><![CDATA[ and ${col} not like #{val}]]></if> | |
66 | + </foreach> | |
67 | + </if> | |
68 | + <if test="betweenField != null"> | |
69 | + <foreach collection="betweenField" index="col" item="val"> | |
70 | + <if test="val.minVal != '' and val.maxVal != ''"> | |
71 | + <![CDATA[ and ${col} between #{val.minVal} and #{val.maxVal}]]> | |
72 | + </if> | |
73 | + </foreach> | |
74 | + </if> | |
75 | + <if test="notBetweenField != null"> | |
76 | + <foreach collection="notBetweenField" index="col" item="val"> | |
77 | + <if test="val.minVal != '' and val.maxVal != ''"> | |
78 | + <![CDATA[ and ${col} not between #{val.minVal} and #{val.maxVal}]]> | |
79 | + </if> | |
80 | + </foreach> | |
81 | + </if> | |
82 | + <if test="inField != null"> | |
83 | + <foreach collection="inField" index="col" item="valList"> | |
84 | + <![CDATA[ and ${col} in ]]> | |
85 | + <foreach collection="valList" item="val" separator="," open="(" close=")"> | |
86 | + <if test="val != null">#{val}</if> | |
87 | + </foreach> | |
88 | + </foreach> | |
89 | + </if> | |
90 | + <if test="notInField != null"> | |
91 | + <foreach collection="notInField" index="col" item="valList"> | |
92 | + <![CDATA[ and ${col} not in ]]> | |
93 | + <foreach collection="valList" item="val" separator="," open="(" close=")"> | |
94 | + <if test="val != null">#{val}</if> | |
95 | + </foreach> | |
96 | + </foreach> | |
97 | + </if> | |
98 | + <if test="groups != null"> | |
99 | + <foreach collection="groups" item="group"> | |
100 | + <![CDATA[ and (]]> | |
101 | + <include refid="core.where_group"></include> | |
102 | + <![CDATA[ )]]> | |
103 | + </foreach> | |
104 | + </if> | |
105 | + </where> | |
106 | + </sql> | |
107 | + | |
108 | + <!-- 分组条件 --> | |
109 | + <sql id="where_group"> | |
110 | + <![CDATA[1 = 1]]> | |
111 | + <if test="group.gtField != null"> | |
112 | + <foreach collection="group.gtField" index="col" item="val"> | |
113 | + <if test="val != null"><![CDATA[ and ${col} > #{val}]]></if> | |
114 | + </foreach> | |
115 | + </if> | |
116 | + <if test="group.gteField != null"> | |
117 | + <foreach collection="group.gteField" index="col" item="val"> | |
118 | + <if test="val != null"><![CDATA[ and ${col} >= #{val}]]></if> | |
119 | + </foreach> | |
120 | + </if> | |
121 | + <if test="group.ltField != null"> | |
122 | + <foreach collection="group.ltField" index="col" item="val"> | |
123 | + <if test="val != null"><![CDATA[ and ${col} < #{val}]]></if> | |
124 | + </foreach> | |
125 | + </if> | |
126 | + <if test="group.lteField != null"> | |
127 | + <foreach collection="group.lteField" index="col" item="val"> | |
128 | + <if test="val != null"><![CDATA[ and ${col} <= #{val}]]></if> | |
129 | + </foreach> | |
130 | + </if> | |
131 | + <if test="group.eqField != null"> | |
132 | + <foreach collection="group.eqField" index="col" item="val"> | |
133 | + <if test="val != null"><![CDATA[ and ${col} = #{val}]]></if> | |
134 | + </foreach> | |
135 | + </if> | |
136 | + <if test="group.neqField != null"> | |
137 | + <foreach collection="group.neqField" index="col" item="val"> | |
138 | + <if test="val != null"><![CDATA[ and ${col} != #{val}]]></if> | |
139 | + </foreach> | |
140 | + </if> | |
141 | + <if test="group.nullField != null"> | |
142 | + <foreach collection="group.nullField" item="val"><![CDATA[ and ${val} is null]]></foreach> | |
143 | + </if> | |
144 | + <if test="group.notNullField != null"> | |
145 | + <foreach collection="group.notNullField" item="val"><![CDATA[ and ${val} is not null]]></foreach> | |
146 | + </if> | |
147 | + <if test="group.likeField != null"> | |
148 | + <foreach collection="group.likeField" index="col" item="val"> | |
149 | + <if test="val != null"><![CDATA[ and ${col} like #{val}]]></if> | |
150 | + </foreach> | |
151 | + </if> | |
152 | + <if test="group.notLikeField != null"> | |
153 | + <foreach collection="group.notLikeField" index="col" item="val"> | |
154 | + <if test="val != null"><![CDATA[ and ${col} not like #{val}]]></if> | |
155 | + </foreach> | |
156 | + </if> | |
157 | + <if test="group.betweenField != null"> | |
158 | + <foreach collection="group.betweenField" index="col" item="val"> | |
159 | + <if test="(val.minVal == 0 or val.minVal != '') and (val.maxVal == 0 or val.maxVal != '')"> | |
160 | + <![CDATA[ and ${col} between #{val.minVal} and #{val.maxVal}]]> | |
161 | + </if> | |
162 | + </foreach> | |
163 | + </if> | |
164 | + <if test="group.notBetweenField != null"> | |
165 | + <foreach collection="group.notBetweenField" index="col" item="val"> | |
166 | + <if test="(val.minVal == 0 or val.minVal != '') and (val.maxVal == 0 or val.maxVal != '')"> | |
167 | + <![CDATA[ and ${col} not between #{val.minVal} and #{val.maxVal}]]> | |
168 | + </if> | |
169 | + </foreach> | |
170 | + </if> | |
171 | + <if test="group.inField != null"> | |
172 | + <foreach collection="group.inField" index="col" item="valList"> | |
173 | + <![CDATA[ and ${col} in ]]> | |
174 | + <foreach collection="valList" item="val" separator="," open="(" close=")"> | |
175 | + <if test="val != null">#{val}</if> | |
176 | + </foreach> | |
177 | + </foreach> | |
178 | + </if> | |
179 | + <if test="group.notInField != null"> | |
180 | + <foreach collection="group.notInField" index="col" item="valList"> | |
181 | + <![CDATA[ and ${col} not in ]]> | |
182 | + <foreach collection="valList" item="val" separator="," open="(" close=")"> | |
183 | + <if test="val != null">#{val}</if> | |
184 | + </foreach> | |
185 | + </foreach> | |
186 | + </if> | |
187 | + </sql> | |
188 | + | |
189 | + <!-- 查询条件,参数名为:con --> | |
190 | + <sql id="where_condition_pre_con"> | |
191 | + <where> | |
192 | + <if test="con.gtField != null"> | |
193 | + <foreach collection="con.gtField" index="col" item="val"> | |
194 | + <if test="val != null"><![CDATA[ and ${col} > #{val}]]></if> | |
195 | + </foreach> | |
196 | + </if> | |
197 | + <if test="con.gteField != null"> | |
198 | + <foreach collection="con.gteField" index="col" item="val"> | |
199 | + <if test="val != null"><![CDATA[ and ${col} >= #{val}]]></if> | |
200 | + </foreach> | |
201 | + </if> | |
202 | + <if test="con.ltField != null"> | |
203 | + <foreach collection="con.ltField" index="col" item="val"> | |
204 | + <if test="val != null"><![CDATA[ and ${col} < #{val}]]></if> | |
205 | + </foreach> | |
206 | + </if> | |
207 | + <if test="con.lteField != null"> | |
208 | + <foreach collection="con.lteField" index="col" item="val"> | |
209 | + <if test="val != null"><![CDATA[ and ${col} <= #{val}]]></if> | |
210 | + </foreach> | |
211 | + </if> | |
212 | + <if test="con.eqField != null"> | |
213 | + <foreach collection="con.eqField" index="col" item="val"> | |
214 | + <if test="val != null"><![CDATA[ and ${col} = #{val}]]></if> | |
215 | + </foreach> | |
216 | + </if> | |
217 | + <if test="con.neqField != null"> | |
218 | + <foreach collection="con.neqField" index="col" item="val"> | |
219 | + <if test="val != null"><![CDATA[ and ${col} != #{val}]]></if> | |
220 | + </foreach> | |
221 | + </if> | |
222 | + <if test="con.nullField != null"> | |
223 | + <foreach collection="con.nullField" item="val"><![CDATA[ and ${val} is null]]></foreach> | |
224 | + </if> | |
225 | + <if test="con.notNullField != null"> | |
226 | + <foreach collection="con.notNullField" item="val"><![CDATA[ and ${val} is not null]]></foreach> | |
227 | + </if> | |
228 | + <if test="con.likeField != null"> | |
229 | + <foreach collection="con.likeField" index="col" item="val"> | |
230 | + <if test="val != null"><![CDATA[ and ${col} like #{val}]]></if> | |
231 | + </foreach> | |
232 | + </if> | |
233 | + <if test="con.notLikeField != null"> | |
234 | + <foreach collection="con.notLikeField" index="col" item="val"> | |
235 | + <if test="val != null"><![CDATA[ and ${col} not like #{val}]]></if> | |
236 | + </foreach> | |
237 | + </if> | |
238 | + <if test="con.betweenField != null"> | |
239 | + <foreach collection="con.betweenField" index="col" item="val"> | |
240 | + <if test="(val.minVal == 0 or val.minVal != '') and (val.maxVal == 0 or val.maxVal != '')"> | |
241 | + <![CDATA[ and ${col} between #{val.minVal} and #{val.maxVal}]]> | |
242 | + </if> | |
243 | + </foreach> | |
244 | + </if> | |
245 | + <if test="con.notBetweenField != null"> | |
246 | + <foreach collection="con.notBetweenField" index="col" item="val"> | |
247 | + <if test="(val.minVal == 0 or val.minVal != '') and (val.maxVal == 0 or val.maxVal != '')"> | |
248 | + <![CDATA[ and ${col} not between #{val.minVal} and #{val.maxVal}]]> | |
249 | + </if> | |
250 | + </foreach> | |
251 | + </if> | |
252 | + <if test="con.inField != null"> | |
253 | + <foreach collection="con.inField" index="col" item="valList"> | |
254 | + <![CDATA[ and ${col} in ]]> | |
255 | + <foreach collection="valList" item="val" separator="," open="(" close=")"> | |
256 | + <if test="val != null">#{val}</if> | |
257 | + </foreach> | |
258 | + </foreach> | |
259 | + </if> | |
260 | + <if test="con.notInField != null"> | |
261 | + <foreach collection="con.notInField" index="col" item="valList"> | |
262 | + <![CDATA[ and ${col} not in ]]> | |
263 | + <foreach collection="valList" item="val" separator="," open="(" close=")"> | |
264 | + <if test="val != null">#{val}</if> | |
265 | + </foreach> | |
266 | + </foreach> | |
267 | + </if> | |
268 | + <if test="con.groups != null"> | |
269 | + <foreach collection="con.groups" item="group"> | |
270 | + <![CDATA[ and (]]> | |
271 | + <include refid="core.where_group_pre_con"></include> | |
272 | + <![CDATA[ )]]> | |
273 | + </foreach> | |
274 | + </if> | |
275 | + </where> | |
276 | + </sql> | |
277 | + | |
278 | + <!-- 分组条件,参数名为:con --> | |
279 | + <sql id="where_group_pre_con"> | |
280 | + <![CDATA[1 = 1]]> | |
281 | + <if test="con.group.gtField != null"> | |
282 | + <foreach collection="con.group.gtField" index="col" item="val"> | |
283 | + <if test="val != null"><![CDATA[ and ${col} > #{val}]]></if> | |
284 | + </foreach> | |
285 | + </if> | |
286 | + <if test="con.group.gteField != null"> | |
287 | + <foreach collection="con.group.gteField" index="col" item="val"> | |
288 | + <if test="val != null"><![CDATA[ and ${col} >= #{val}]]></if> | |
289 | + </foreach> | |
290 | + </if> | |
291 | + <if test="con.group.ltField != null"> | |
292 | + <foreach collection="con.group.ltField" index="col" item="val"> | |
293 | + <if test="val != null"><![CDATA[ and ${col} < #{val}]]></if> | |
294 | + </foreach> | |
295 | + </if> | |
296 | + <if test="con.group.lteField != null"> | |
297 | + <foreach collection="con.group.lteField" index="col" item="val"> | |
298 | + <if test="val != null"><![CDATA[ and ${col} <= #{val}]]></if> | |
299 | + </foreach> | |
300 | + </if> | |
301 | + <if test="con.group.eqField != null"> | |
302 | + <foreach collection="con.group.eqField" index="col" item="val"> | |
303 | + <if test="val != null"><![CDATA[ and ${col} = #{val}]]></if> | |
304 | + </foreach> | |
305 | + </if> | |
306 | + <if test="con.group.neqField != null"> | |
307 | + <foreach collection="con.group.neqField" index="col" item="val"> | |
308 | + <if test="val != null"><![CDATA[ and ${col} != #{val}]]></if> | |
309 | + </foreach> | |
310 | + </if> | |
311 | + <if test="con.group.nullField != null"> | |
312 | + <foreach collection="con.group.nullField" item="val"><![CDATA[ and ${val} is null]]></foreach> | |
313 | + </if> | |
314 | + <if test="con.group.notNullField != null"> | |
315 | + <foreach collection="con.group.notNullField" item="val"><![CDATA[ and ${val} is not null]]></foreach> | |
316 | + </if> | |
317 | + <if test="con.group.likeField != null"> | |
318 | + <foreach collection="con.group.likeField" index="col" item="val"> | |
319 | + <if test="val != null"><![CDATA[ and ${col} like #{val}]]></if> | |
320 | + </foreach> | |
321 | + </if> | |
322 | + <if test="con.group.notLikeField != null"> | |
323 | + <foreach collection="con.group.notLikeField" index="col" item="val"> | |
324 | + <if test="val != null"><![CDATA[ and ${col} not like #{val}]]></if> | |
325 | + </foreach> | |
326 | + </if> | |
327 | + <if test="con.group.betweenField != null"> | |
328 | + <foreach collection="con.group.betweenField" index="col" item="val"> | |
329 | + <if test="(val.minVal == 0 or val.minVal != '') and (val.maxVal == 0 or val.maxVal != '')"> | |
330 | + <![CDATA[ and ${col} between #{val.minVal} and #{val.maxVal}]]> | |
331 | + </if> | |
332 | + </foreach> | |
333 | + </if> | |
334 | + <if test="group.con.notBetweenField != null"> | |
335 | + <foreach collection="group.con.notBetweenField" index="col" item="val"> | |
336 | + <if test="(val.minVal == 0 or val.minVal != '') and (val.maxVal == 0 or val.maxVal != '')"> | |
337 | + <![CDATA[ and ${col} not between #{val.minVal} and #{val.maxVal}]]> | |
338 | + </if> | |
339 | + </foreach> | |
340 | + </if> | |
341 | + <if test="con.group.inField != null"> | |
342 | + <foreach collection="con.group.inField" index="col" item="valList"> | |
343 | + <![CDATA[ and ${col} in ]]> | |
344 | + <foreach collection="valList" item="val" separator="," open="(" close=")"> | |
345 | + <if test="val != null">#{val}</if> | |
346 | + </foreach> | |
347 | + </foreach> | |
348 | + </if> | |
349 | + <if test="con.group.notInField != null"> | |
350 | + <foreach collection="con.group.notInField" index="col" item="valList"> | |
351 | + <![CDATA[ and ${col} not in ]]> | |
352 | + <foreach collection="valList" item="val" separator="," open="(" close=")"> | |
353 | + <if test="val != null">#{val}</if> | |
354 | + </foreach> | |
355 | + </foreach> | |
356 | + </if> | |
357 | + </sql> | |
358 | + | |
359 | +</mapper> | |
0 | 360 | \ No newline at end of file | ... | ... |
fw-rp-domain/pom.xml
0 → 100644
1 | +++ a/fw-rp-domain/pom.xml | |
1 | +<?xml version="1.0" encoding="UTF-8"?> | |
2 | +<project | |
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 | + xmlns="http://maven.apache.org/POM/4.0.0"> | |
6 | + <modelVersion>4.0.0</modelVersion> | |
7 | + <parent> | |
8 | + <groupId>cn.fw</groupId> | |
9 | + <artifactId>fw-rp</artifactId> | |
10 | + <version>1.0</version> | |
11 | + </parent> | |
12 | + <artifactId>fw-rp-domain</artifactId> | |
13 | + <name>fw-rp-domain</name> | |
14 | + <packaging>jar</packaging> | |
15 | + | |
16 | + <dependencies> | |
17 | + <dependency> | |
18 | + <groupId>cn.fw</groupId> | |
19 | + <artifactId>fw-rp-common</artifactId> | |
20 | + <version>${project.parent.version}</version> | |
21 | + </dependency> | |
22 | + <dependency> | |
23 | + <groupId>org.springframework.boot</groupId> | |
24 | + <artifactId>spring-boot-starter-validation</artifactId> | |
25 | + </dependency> | |
26 | + <dependency> | |
27 | + <groupId>cn.fw</groupId> | |
28 | + <artifactId>fw-data-base</artifactId> | |
29 | + </dependency> | |
30 | + <dependency> | |
31 | + <groupId>io.springfox</groupId> | |
32 | + <artifactId>springfox-swagger2</artifactId> | |
33 | + <version>2.8.0</version> | |
34 | + <exclusions> | |
35 | + <exclusion> | |
36 | + <groupId>com.google.guava</groupId> | |
37 | + <artifactId>guava</artifactId> | |
38 | + </exclusion> | |
39 | + </exclusions> | |
40 | + </dependency> | |
41 | + <dependency> | |
42 | + <groupId>io.springfox</groupId> | |
43 | + <artifactId>springfox-swagger-ui</artifactId> | |
44 | + <version>2.6.1</version> | |
45 | + </dependency> | |
46 | + <dependency> | |
47 | + <groupId>com.google.guava</groupId> | |
48 | + <artifactId>guava</artifactId> | |
49 | + </dependency> | |
50 | + <!-- fastjson --> | |
51 | + <dependency> | |
52 | + <groupId>com.alibaba</groupId> | |
53 | + <artifactId>fastjson</artifactId> | |
54 | + </dependency> | |
55 | + <!--lombok--> | |
56 | + <dependency> | |
57 | + <groupId>org.projectlombok</groupId> | |
58 | + <artifactId>lombok</artifactId> | |
59 | + </dependency> | |
60 | + <dependency> | |
61 | + <groupId>cn.fw</groupId> | |
62 | + <artifactId>fw-rp-sdk</artifactId> | |
63 | + <version>${project.parent.version}</version> | |
64 | + </dependency> | |
65 | + | |
66 | + </dependencies> | |
67 | +</project> | ... | ... |
fw-rp-domain/src/main/java/cn/fw/rp/annotation/EnumValid.java
0 → 100644
1 | +++ a/fw-rp-domain/src/main/java/cn/fw/rp/annotation/EnumValid.java | |
1 | +package cn.fw.rp.annotation; | |
2 | + | |
3 | +import javax.validation.Constraint; | |
4 | +import javax.validation.ConstraintValidator; | |
5 | +import javax.validation.ConstraintValidatorContext; | |
6 | +import javax.validation.Payload; | |
7 | +import java.lang.annotation.ElementType; | |
8 | +import java.lang.annotation.Retention; | |
9 | +import java.lang.annotation.RetentionPolicy; | |
10 | +import java.lang.annotation.Target; | |
11 | +import java.lang.reflect.InvocationTargetException; | |
12 | +import java.lang.reflect.Method; | |
13 | +import java.lang.reflect.Modifier; | |
14 | + | |
15 | +/** | |
16 | + * @author Devin | |
17 | + * @date: 2018/4/18 20:44 | |
18 | + */ | |
19 | +@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE}) | |
20 | +@Retention(RetentionPolicy.RUNTIME) | |
21 | +@Constraint(validatedBy = EnumValid.Validator.class) | |
22 | +public @interface EnumValid { | |
23 | + | |
24 | + String message() default "枚举类型不符合"; | |
25 | + | |
26 | + Class<?>[] groups() default {}; | |
27 | + | |
28 | + Class<? extends Payload>[] payload() default {}; | |
29 | + | |
30 | + Class<? extends Enum<?>> enumClass(); | |
31 | + | |
32 | + String enumMethod(); | |
33 | + | |
34 | + class Validator implements ConstraintValidator<EnumValid, Object> { | |
35 | + | |
36 | + private Class<? extends Enum<?>> enumClass; | |
37 | + private String enumMethod; | |
38 | + | |
39 | + @Override | |
40 | + public void initialize(EnumValid enumValid) { | |
41 | + enumMethod = enumValid.enumMethod(); | |
42 | + enumClass = enumValid.enumClass(); | |
43 | + } | |
44 | + | |
45 | + @Override | |
46 | + public boolean isValid(Object value, ConstraintValidatorContext constraintValidatorContext) { | |
47 | + if (value == null) { | |
48 | + return Boolean.FALSE; | |
49 | + } | |
50 | + if (enumClass == null || enumMethod == null) { | |
51 | + return Boolean.FALSE; | |
52 | + } | |
53 | + Class<?> valueClass = value.getClass(); | |
54 | + try { | |
55 | + Method method = enumClass.getMethod(enumMethod, valueClass); | |
56 | + if (!Boolean.TYPE.equals(method.getReturnType()) && !Boolean.class.equals(method.getReturnType())) { | |
57 | + throw new RuntimeException(String.format("%s method return is not boolean type in the %s class", enumMethod, enumClass)); | |
58 | + } | |
59 | + if (!Modifier.isStatic(method.getModifiers())) { | |
60 | + throw new RuntimeException(String.format("%s method is not static method in the %s class", enumMethod, enumClass)); | |
61 | + } | |
62 | + Boolean result = (Boolean) method.invoke(null, value); | |
63 | + return result == null ? false : result; | |
64 | + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { | |
65 | + throw new RuntimeException(e); | |
66 | + } catch (NoSuchMethodException | SecurityException e) { | |
67 | + throw new RuntimeException(String.format("This %s(%s) method does not exist in the %s", enumMethod, valueClass, enumClass), e); | |
68 | + } | |
69 | + } | |
70 | + | |
71 | + } | |
72 | +} | ... | ... |
fw-rp-domain/src/main/java/cn/fw/rp/db/FileList.java
0 → 100644
1 | +++ a/fw-rp-domain/src/main/java/cn/fw/rp/db/FileList.java | |
1 | +package cn.fw.rp.db; | |
2 | + | |
3 | +import cn.fw.data.base.db.annotation.DbTable; | |
4 | +import lombok.Data; | |
5 | + | |
6 | +import java.util.Date; | |
7 | + | |
8 | +/** | |
9 | + * @author suchu | |
10 | + * @since 2018/5/8 15:22 | |
11 | + */ | |
12 | +@Data | |
13 | +@DbTable("file_list") | |
14 | +public class FileList { | |
15 | + | |
16 | + Integer id; | |
17 | + | |
18 | + String fileName; | |
19 | + | |
20 | + String contentType; | |
21 | + | |
22 | + Date uploadTime; | |
23 | + | |
24 | + String uploadUser; | |
25 | + | |
26 | + String filePath; | |
27 | + | |
28 | + Integer flag; | |
29 | + | |
30 | + String fileId; | |
31 | + | |
32 | + Long fileLength; | |
33 | +} | ... | ... |
fw-rp-domain/src/main/java/cn/fw/rp/db/ProblemFile.java
0 → 100644
1 | +++ a/fw-rp-domain/src/main/java/cn/fw/rp/db/ProblemFile.java | |
1 | +package cn.fw.rp.db; | |
2 | + | |
3 | +import lombok.Data; | |
4 | + | |
5 | +/** | |
6 | + * @author suchu | |
7 | + * @since 2018/5/8 15:21 | |
8 | + */ | |
9 | +@Data | |
10 | +public class ProblemFile { | |
11 | + private Integer id; | |
12 | + | |
13 | + private String problemId; | |
14 | + | |
15 | + private String fileId; | |
16 | + | |
17 | +} | ... | ... |
fw-rp-domain/src/main/java/cn/fw/rp/db/ProblemList.java
0 → 100644
1 | +++ a/fw-rp-domain/src/main/java/cn/fw/rp/db/ProblemList.java | |
1 | +package cn.fw.rp.db; | |
2 | + | |
3 | +import cn.fw.data.base.db.annotation.DbTable; | |
4 | +import lombok.Data; | |
5 | + | |
6 | +import java.util.Date; | |
7 | + | |
8 | +/** | |
9 | + * @author suchu | |
10 | + * @since 2018/5/8 15:18 | |
11 | + */ | |
12 | +@DbTable("problem_list") | |
13 | +@Data | |
14 | +public class ProblemList { | |
15 | + | |
16 | + private Integer id; | |
17 | + | |
18 | + private String systemRef; | |
19 | + | |
20 | + private String moduleRef; | |
21 | + | |
22 | + private String title; | |
23 | + private String content; | |
24 | + | |
25 | + private String purpose; | |
26 | + | |
27 | + private String question; | |
28 | + | |
29 | + private String scenarios; | |
30 | + | |
31 | + private String logic; | |
32 | + | |
33 | + private Integer status; | |
34 | + | |
35 | + private Date createTime; | |
36 | + | |
37 | + private Date updateTime; | |
38 | + | |
39 | + private String createUser; | |
40 | + | |
41 | + private String updateUser; | |
42 | +} | ... | ... |
fw-rp-domain/src/main/java/cn/fw/rp/man/ProblemDto.java
0 → 100644
1 | +++ a/fw-rp-domain/src/main/java/cn/fw/rp/man/ProblemDto.java | |
1 | +package cn.fw.rp.man; | |
2 | + | |
3 | +import lombok.Data; | |
4 | + | |
5 | +import java.util.Date; | |
6 | + | |
7 | +/** | |
8 | + * @author suchu | |
9 | + * @since 2018/5/8 16:08 | |
10 | + */ | |
11 | +@Data | |
12 | +public class ProblemDto { | |
13 | + private Integer id; | |
14 | + | |
15 | + private String systemRef; | |
16 | + | |
17 | + private String moduleRef; | |
18 | + | |
19 | + private String title; | |
20 | + private String content; | |
21 | + | |
22 | + private String purpose; | |
23 | + | |
24 | + private String question; | |
25 | + | |
26 | + private String scenarios; | |
27 | + | |
28 | + private String logic; | |
29 | + | |
30 | + private Integer status; | |
31 | + | |
32 | + private Date createTime; | |
33 | + | |
34 | + private Date updateTime; | |
35 | + | |
36 | + private String createUser; | |
37 | + | |
38 | + private String updateUser; | |
39 | +} | ... | ... |
fw-rp-domain/src/main/java/cn/fw/rp/man/ProblemQueryParam.java
0 → 100644
1 | +++ a/fw-rp-domain/src/main/java/cn/fw/rp/man/ProblemQueryParam.java | |
1 | +package cn.fw.rp.man; | |
2 | + | |
3 | +import lombok.Data; | |
4 | + | |
5 | +/** | |
6 | + * @author suchu | |
7 | + * @since 2018/5/9 19:10 | |
8 | + */ | |
9 | +@Data | |
10 | +public class ProblemQueryParam { | |
11 | + | |
12 | + private String title; | |
13 | + private String systemRef; | |
14 | + private String moduleRef; | |
15 | + private String createUser; | |
16 | + private String beginDate; | |
17 | + private String endDate; | |
18 | + private Integer status; | |
19 | +} | ... | ... |
fw-rp-man/pom.xml
0 → 100644
1 | +++ a/fw-rp-man/pom.xml | |
1 | +<?xml version="1.0" encoding="UTF-8"?> | |
2 | +<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
3 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" | |
4 | + xmlns="http://maven.apache.org/POM/4.0.0"> | |
5 | + <modelVersion>4.0.0</modelVersion> | |
6 | + <parent> | |
7 | + <groupId>cn.fw</groupId> | |
8 | + <artifactId>fw-rp</artifactId> | |
9 | + <version>1.0</version> | |
10 | + </parent> | |
11 | + | |
12 | + <artifactId>fw-rp-man</artifactId> | |
13 | + <packaging>jar</packaging> | |
14 | + <name>fw-rp-man</name> | |
15 | + | |
16 | + <dependencies> | |
17 | + <dependency> | |
18 | + <groupId>org.springframework.boot</groupId> | |
19 | + <artifactId>spring-boot-devtools</artifactId> | |
20 | + </dependency> | |
21 | + <dependency> | |
22 | + <groupId>org.springframework.boot</groupId> | |
23 | + <artifactId>spring-boot-starter-web</artifactId> | |
24 | + </dependency> | |
25 | + <dependency> | |
26 | + <groupId>org.mybatis.spring.boot</groupId> | |
27 | + <artifactId>mybatis-spring-boot-starter</artifactId> | |
28 | + </dependency> | |
29 | + <dependency> | |
30 | + <groupId>org.springframework.boot</groupId> | |
31 | + <artifactId>spring-boot-starter-validation</artifactId> | |
32 | + </dependency> | |
33 | + <dependency> | |
34 | + <groupId>org.springframework.boot</groupId> | |
35 | + <artifactId>spring-boot-starter-actuator</artifactId> | |
36 | + </dependency> | |
37 | + <dependency> | |
38 | + <groupId>com.alibaba</groupId> | |
39 | + <artifactId>druid-spring-boot-starter</artifactId> | |
40 | + <version>1.1.7</version> | |
41 | + </dependency> | |
42 | + <!--单元测试--> | |
43 | + <dependency> | |
44 | + <groupId>junit</groupId> | |
45 | + <artifactId>junit</artifactId> | |
46 | + <scope>compile</scope> | |
47 | + </dependency> | |
48 | + <dependency> | |
49 | + <groupId>org.springframework</groupId> | |
50 | + <artifactId>spring-test</artifactId> | |
51 | + </dependency> | |
52 | + <dependency> | |
53 | + <groupId>org.springframework.boot</groupId> | |
54 | + <artifactId>spring-boot-starter-test</artifactId> | |
55 | + <scope>test</scope> | |
56 | + </dependency> | |
57 | + | |
58 | + | |
59 | + <dependency> | |
60 | + <groupId>cn.fw</groupId> | |
61 | + <artifactId>fw-rp-common</artifactId> | |
62 | + <version>${project.parent.version}</version> | |
63 | + </dependency> | |
64 | + <dependency> | |
65 | + <groupId>cn.fw</groupId> | |
66 | + <artifactId>fw-rp-domain</artifactId> | |
67 | + <version>${project.parent.version}</version> | |
68 | + </dependency> | |
69 | + <dependency> | |
70 | + <groupId>cn.fw</groupId> | |
71 | + <artifactId>fw-rp-service</artifactId> | |
72 | + <version>${project.parent.version}</version> | |
73 | + </dependency> | |
74 | + <dependency> | |
75 | + <groupId>cn.fw</groupId> | |
76 | + <artifactId>fw-base-config-sdk</artifactId> | |
77 | + </dependency> | |
78 | + <dependency> | |
79 | + <groupId>cn.fw</groupId> | |
80 | + <artifactId>fw-data-base</artifactId> | |
81 | + </dependency> | |
82 | + <dependency> | |
83 | + <groupId>cn.fw</groupId> | |
84 | + <artifactId>fw-personnel-sdk</artifactId> | |
85 | + </dependency> | |
86 | + <dependency> | |
87 | + <groupId>cn.fw</groupId> | |
88 | + <artifactId>fw-potential-sdk</artifactId> | |
89 | + </dependency> | |
90 | + <dependency> | |
91 | + <groupId>cn.fw.third</groupId> | |
92 | + <artifactId>fw-wechat-sdk</artifactId> | |
93 | + </dependency> | |
94 | + <dependency> | |
95 | + <groupId>mysql</groupId> | |
96 | + <artifactId>mysql-connector-java</artifactId> | |
97 | + </dependency> | |
98 | + <dependency> | |
99 | + <groupId>cn.fw</groupId> | |
100 | + <artifactId>fw-auth-client</artifactId> | |
101 | + </dependency> | |
102 | + <dependency> | |
103 | + <groupId>cn.fw</groupId> | |
104 | + <artifactId>fw-rp-sdk</artifactId> | |
105 | + <version>${project.parent.version}</version> | |
106 | + </dependency> | |
107 | + <dependency> | |
108 | + <groupId>org.apache.commons</groupId> | |
109 | + <artifactId>commons-lang3</artifactId> | |
110 | + </dependency> | |
111 | + <dependency> | |
112 | + <groupId>org.springframework.cloud</groupId> | |
113 | + <artifactId>spring-cloud-starter-eureka</artifactId> | |
114 | + </dependency> | |
115 | + <dependency> | |
116 | + <groupId>net.coobird</groupId> | |
117 | + <artifactId>thumbnailator</artifactId> | |
118 | + <version>0.4.8</version> | |
119 | + </dependency> | |
120 | + </dependencies> | |
121 | + | |
122 | + <build> | |
123 | + <finalName>fw-rp-man</finalName> | |
124 | + <resources> | |
125 | + <resource> | |
126 | + <directory>src/main/resources</directory> | |
127 | + <filtering>true</filtering> | |
128 | + </resource> | |
129 | + </resources> | |
130 | + <plugins> | |
131 | + <plugin> | |
132 | + <groupId>org.springframework.boot</groupId> | |
133 | + <artifactId>spring-boot-maven-plugin</artifactId> | |
134 | + <configuration> | |
135 | + <fork>true</fork> | |
136 | + <executable>true</executable> | |
137 | + </configuration> | |
138 | + </plugin> | |
139 | + </plugins> | |
140 | + </build> | |
141 | + | |
142 | + <profiles> | |
143 | + <profile> | |
144 | + <id>dev</id> | |
145 | + <properties> | |
146 | + <activatedProperties>dev</activatedProperties> | |
147 | + </properties> | |
148 | + <activation> | |
149 | + <activeByDefault>true</activeByDefault> | |
150 | + </activation> | |
151 | + </profile> | |
152 | + <profile> | |
153 | + <id>test</id> | |
154 | + <properties> | |
155 | + <activatedProperties>test</activatedProperties> | |
156 | + </properties> | |
157 | + </profile> | |
158 | + <profile> | |
159 | + <id>local</id> | |
160 | + <properties> | |
161 | + <activatedProperties>local</activatedProperties> | |
162 | + </properties> | |
163 | + </profile> | |
164 | + <profile> | |
165 | + <id>prd</id> | |
166 | + <properties> | |
167 | + <activatedProperties>prd</activatedProperties> | |
168 | + </properties> | |
169 | + </profile> | |
170 | + </profiles> | |
171 | +</project> | ... | ... |
fw-rp-man/src/main/java/cn/fw/rp/web/RpManServer.java
0 → 100644
1 | +++ a/fw-rp-man/src/main/java/cn/fw/rp/web/RpManServer.java | |
1 | +package cn.fw.rp.web; | |
2 | + | |
3 | +import org.mybatis.spring.annotation.MapperScan; | |
4 | +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | |
5 | +import org.springframework.boot.autoconfigure.SpringBootApplication; | |
6 | +import org.springframework.boot.builder.SpringApplicationBuilder; | |
7 | +import org.springframework.context.annotation.ComponentScan; | |
8 | +import org.springframework.transaction.annotation.EnableTransactionManagement; | |
9 | + | |
10 | +/** | |
11 | + * man端服务启动类 | |
12 | + * | |
13 | + * @author luoxin | |
14 | + * @create 2018-02-24 | |
15 | + */ | |
16 | +@SpringBootApplication | |
17 | +@EnableTransactionManagement | |
18 | +@EnableAutoConfiguration | |
19 | +@MapperScan(basePackages = "cn.fw.rp.dao") | |
20 | +@ComponentScan({"cn.fw.rp.service", "cn.fw.rp.web"}) | |
21 | +public class RpManServer { | |
22 | + public static void main(String[] args) { | |
23 | + new SpringApplicationBuilder(RpManServer.class).web(true).run(args); | |
24 | + } | |
25 | +} | ... | ... |
fw-rp-man/src/main/java/cn/fw/rp/web/controller/FileController.java
0 → 100644
1 | +++ a/fw-rp-man/src/main/java/cn/fw/rp/web/controller/FileController.java | |
1 | +package cn.fw.rp.web.controller; | |
2 | + | |
3 | +import cn.fw.data.base.domain.common.Message; | |
4 | +import cn.fw.rp.common.enums.FileTypeEnums; | |
5 | +import cn.fw.rp.common.util.DateUtils; | |
6 | +import cn.fw.rp.dao.FileListDao; | |
7 | +import cn.fw.rp.dao.ProblemFileDao; | |
8 | +import cn.fw.rp.db.FileList; | |
9 | +import cn.fw.rp.db.ProblemFile; | |
10 | +import net.coobird.thumbnailator.Thumbnails; | |
11 | +import org.apache.catalina.connector.ClientAbortException; | |
12 | +import org.apache.tomcat.util.http.fileupload.IOUtils; | |
13 | +import org.springframework.beans.factory.annotation.Autowired; | |
14 | +import org.springframework.beans.factory.annotation.Value; | |
15 | +import org.springframework.stereotype.Controller; | |
16 | +import org.springframework.transaction.annotation.Transactional; | |
17 | +import org.springframework.util.Assert; | |
18 | +import org.springframework.web.bind.annotation.*; | |
19 | +import org.springframework.web.multipart.MultipartFile; | |
20 | + | |
21 | +import javax.servlet.ServletOutputStream; | |
22 | +import javax.servlet.http.HttpServletRequest; | |
23 | +import javax.servlet.http.HttpServletResponse; | |
24 | +import java.io.*; | |
25 | +import java.text.SimpleDateFormat; | |
26 | +import java.util.UUID; | |
27 | + | |
28 | +/** | |
29 | + * @author suchu | |
30 | + * @since 2018/5/9 10:59 | |
31 | + */ | |
32 | +@Controller | |
33 | +@RequestMapping("/file") | |
34 | +public class FileController { | |
35 | + | |
36 | + @Value("${upload.path}") | |
37 | + String uploadPath; | |
38 | + | |
39 | + @Autowired | |
40 | + FileListDao dao; | |
41 | + | |
42 | + @Autowired | |
43 | + ProblemFileDao problemFileDao; | |
44 | + | |
45 | + public static void main(String[] args) throws IOException { | |
46 | + Thumbnails.of("F:\\RP_UPLOAD\\2018\\05\\09\\1db414949be24c3d9de479d2cbfa4c81.jpg") | |
47 | + .size(100, 100).toFile(new File("F:\\\\RP_UPLOAD\\test1.jpg")); | |
48 | + } | |
49 | + | |
50 | + public String datePath() { | |
51 | + SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd"); | |
52 | + return sdf.format(DateUtils.getNow()); | |
53 | + } | |
54 | + | |
55 | + @RequestMapping("/video/{fileId}") | |
56 | + public void videoView(@PathVariable String fileId, HttpServletResponse response, HttpServletRequest request, @RequestHeader(value = "range", required = false) String range) { | |
57 | + FileList fileList = dao.selectByFileId(fileId); | |
58 | + File file = null; | |
59 | + if (null != fileList) { | |
60 | + file = new File(fileList.getFilePath()); | |
61 | + if (!file.exists()) { | |
62 | + response.setStatus(204);//NO content | |
63 | + return; | |
64 | + } | |
65 | + } | |
66 | + | |
67 | + //文件目录 | |
68 | +// File music = new File("E:\\CloudMusic\\林子祥 - 街头霸王榜.mp3"); | |
69 | + | |
70 | + | |
71 | + //开始下载位置 | |
72 | + long startByte = 0; | |
73 | + //结束下载位置 | |
74 | + long endByte = file.length() - 1; | |
75 | + | |
76 | + //有range的话 | |
77 | + if (range != null && range.contains("bytes=") && range.contains("-")) { | |
78 | + range = range.substring(range.lastIndexOf("=") + 1).trim(); | |
79 | + String ranges[] = range.split("-"); | |
80 | + try { | |
81 | + //判断range的类型 | |
82 | + if (ranges.length == 1) { | |
83 | + //类型一:bytes=-2343 | |
84 | + if (range.startsWith("-")) { | |
85 | + endByte = Long.parseLong(ranges[0]); | |
86 | + } | |
87 | + //类型二:bytes=2343- | |
88 | + else if (range.endsWith("-")) { | |
89 | + startByte = Long.parseLong(ranges[0]); | |
90 | + } | |
91 | + } | |
92 | + //类型三:bytes=22-2343 | |
93 | + else if (ranges.length == 2) { | |
94 | + startByte = Long.parseLong(ranges[0]); | |
95 | + endByte = Long.parseLong(ranges[1]); | |
96 | + } | |
97 | + | |
98 | + } catch (NumberFormatException e) { | |
99 | + startByte = 0; | |
100 | + endByte = file.length() - 1; | |
101 | + } | |
102 | + } | |
103 | + | |
104 | + //要下载的长度(为啥要加一问小学数学老师去) | |
105 | + long contentLength = endByte - startByte + 1; | |
106 | + //文件名 | |
107 | + String fileName = file.getName(); | |
108 | + //文件类型 | |
109 | + String contentType = request.getServletContext().getMimeType(fileName); | |
110 | + | |
111 | + | |
112 | + //各种响应头设置 | |
113 | + //参考资料:https://www.ibm.com/developerworks/cn/java/joy-down/index.html | |
114 | + //坑爹地方一:看代码 | |
115 | + response.setHeader("Accept-Ranges", "bytes"); | |
116 | + //坑爹地方二:http状态码要为206 | |
117 | + response.setStatus(response.SC_PARTIAL_CONTENT); | |
118 | + response.setContentType(contentType); | |
119 | + response.setHeader("Content-Type", contentType); | |
120 | + //这里文件名换你想要的,inline表示浏览器直接实用(我方便测试用的) | |
121 | + //参考资料:http://hw1287789687.iteye.com/blog/2188500 | |
122 | + response.setHeader("Content-Disposition", "inline;filename=test.mp3"); | |
123 | + response.setHeader("Content-Length", String.valueOf(contentLength)); | |
124 | + //坑爹地方三:Content-Range,格式为 | |
125 | + // [要下载的开始位置]-[结束位置]/[文件总大小] | |
126 | + response.setHeader("Content-Range", "bytes " + startByte + "-" + endByte + "/" + file.length()); | |
127 | + | |
128 | + | |
129 | + BufferedOutputStream outputStream = null; | |
130 | + RandomAccessFile randomAccessFile = null; | |
131 | + //已传送数据大小 | |
132 | + long transmitted = 0; | |
133 | + try { | |
134 | + randomAccessFile = new RandomAccessFile(file, "r"); | |
135 | + outputStream = new BufferedOutputStream(response.getOutputStream()); | |
136 | + byte[] buff = new byte[4096]; | |
137 | + int len = 0; | |
138 | + randomAccessFile.seek(startByte); | |
139 | + //坑爹地方四:判断是否到了最后不足4096(buff的length)个byte这个逻辑((transmitted + len) <= contentLength)要放前面!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | |
140 | + //不然会会先读取randomAccessFile,造成后面读取位置出错,找了一天才发现问题所在 | |
141 | + while ((transmitted + len) <= contentLength && (len = randomAccessFile.read(buff)) != -1) { | |
142 | + outputStream.write(buff, 0, len); | |
143 | + transmitted += len; | |
144 | + //停一下,方便测试,用的时候删了就行了 | |
145 | + // Thread.sleep(10); | |
146 | + } | |
147 | + //处理不足buff.length部分 | |
148 | + if (transmitted < contentLength) { | |
149 | + len = randomAccessFile.read(buff, 0, (int) (contentLength - transmitted)); | |
150 | + outputStream.write(buff, 0, len); | |
151 | + transmitted += len; | |
152 | + } | |
153 | + | |
154 | + outputStream.flush(); | |
155 | + response.flushBuffer(); | |
156 | + randomAccessFile.close(); | |
157 | + System.out.println("下载完毕:" + startByte + "-" + endByte + ":" + transmitted); | |
158 | + | |
159 | + } catch (ClientAbortException e) { | |
160 | + System.out.println("用户停止下载:" + startByte + "-" + endByte + ":" + transmitted); | |
161 | + //捕获此异常表示拥护停止下载 | |
162 | + } catch (IOException e) { | |
163 | + e.printStackTrace(); | |
164 | + } finally { | |
165 | + try { | |
166 | + if (randomAccessFile != null) { | |
167 | + randomAccessFile.close(); | |
168 | + } | |
169 | + } catch (IOException e) { | |
170 | + e.printStackTrace(); | |
171 | + } | |
172 | + } | |
173 | + | |
174 | + } | |
175 | + | |
176 | + @RequestMapping("/view/{fileId}") | |
177 | + public void view(@PathVariable String fileId, HttpServletResponse response, HttpServletRequest request) throws IOException { | |
178 | + | |
179 | + FileList fileList = dao.selectByFileId(fileId); | |
180 | + if (null != fileList) { | |
181 | + File file = new File(fileList.getFilePath()); | |
182 | + if (!file.exists()) { | |
183 | + response.setStatus(404); | |
184 | + return; | |
185 | + } | |
186 | + FileInputStream fileInputStream = new FileInputStream(file); | |
187 | + response.setContentType(fileList.getContentType()); | |
188 | + response.setContentLength(fileInputStream.available()); | |
189 | + byte buffer[] = new byte[4096]; | |
190 | + ServletOutputStream outputStream = response.getOutputStream(); | |
191 | + int length = fileInputStream.read(buffer); | |
192 | + while (length != -1) { | |
193 | + outputStream.write(buffer, 0, length); | |
194 | + length = fileInputStream.read(buffer); | |
195 | + } | |
196 | + outputStream.close(); | |
197 | + } else { | |
198 | + response.setStatus(404); | |
199 | + System.out.println("无法下载..."); | |
200 | + } | |
201 | + } | |
202 | + | |
203 | + public int getFlag(String contentType) { | |
204 | + if (contentType.contains("image")) { | |
205 | + return FileTypeEnums.IMAGE.getType(); | |
206 | + } else if (contentType.contains("video")) | |
207 | + return FileTypeEnums.VIDEO.getType(); | |
208 | + else return FileTypeEnums.FILE.getType(); | |
209 | + } | |
210 | + | |
211 | + @RequestMapping("/image/thumb") | |
212 | + public void thumbnails(HttpServletResponse response, @RequestParam String fileId, @RequestParam(defaultValue = "200") Integer width, @RequestParam(defaultValue = "200") Integer height) throws IOException { | |
213 | + FileList fileList = dao.selectByFileId(fileId); | |
214 | + if (width > 2000) | |
215 | + width = 150; | |
216 | + if (height > 2000) { | |
217 | + height = 150; | |
218 | + } | |
219 | + if (null != fileList) { | |
220 | + File file = new File(fileList.getFilePath()); | |
221 | + if (!file.exists()) { | |
222 | + response.setStatus(404); | |
223 | + } else { | |
224 | + response.setContentType(fileList.getContentType()); | |
225 | + ServletOutputStream outputStream = response.getOutputStream(); | |
226 | + Thumbnails.of(file).size(width, height).toOutputStream(outputStream); | |
227 | + outputStream.close(); | |
228 | + } | |
229 | + } else { | |
230 | + response.setStatus(404); | |
231 | + } | |
232 | + } | |
233 | + | |
234 | + @Transactional(rollbackFor = Exception.class) | |
235 | + @RequestMapping("/relate") | |
236 | + @ResponseBody | |
237 | + public Message relate(String fileId, String problemId) { | |
238 | + Assert.notNull(fileId, "file id must not be null"); | |
239 | + Assert.notNull(problemId, "problem id must not be null"); | |
240 | + ProblemFile problemFile = new ProblemFile(); | |
241 | + problemFile.setFileId(fileId); | |
242 | + problemFile.setProblemId(problemId); | |
243 | + problemFileDao.insert(problemFile); | |
244 | + return Message.success("关联成功"); | |
245 | + } | |
246 | + | |
247 | + @Transactional(rollbackFor = Exception.class) | |
248 | + @PostMapping("/upload") | |
249 | + @ResponseBody | |
250 | + public Message upload(@RequestParam("file") MultipartFile file, @RequestParam(name = "flag", defaultValue = "3") Integer flag, HttpServletRequest request) throws IOException { | |
251 | + String contentType = file.getContentType(); | |
252 | + String uuid = UUID.randomUUID().toString().replaceAll("-", ""); | |
253 | + Long fileLength = file.getSize(); | |
254 | + //int flag = getFlag(contentType); | |
255 | + String fileName = file.getOriginalFilename(); | |
256 | + String fileSuffix = fileName.substring(fileName.lastIndexOf(".")); | |
257 | + FileList fileList = new FileList(); | |
258 | + fileList.setContentType(contentType); | |
259 | + fileList.setFileId(uuid); | |
260 | + fileList.setUploadTime(DateUtils.getNow()); | |
261 | + fileList.setFileName(fileName); | |
262 | + fileList.setFileLength(fileLength); | |
263 | + fileList.setFlag(flag); | |
264 | + // IOUtils. | |
265 | + File rootPath = new File(uploadPath); | |
266 | + if (!rootPath.exists()) { | |
267 | + rootPath.mkdir(); | |
268 | + } | |
269 | + String datePath = datePath(); | |
270 | + | |
271 | + File outPath = new File(rootPath, datePath); | |
272 | + if (!outPath.exists()) { | |
273 | + outPath.mkdirs(); | |
274 | + } | |
275 | + File outFile = new File(outPath, uuid + fileSuffix); | |
276 | + fileList.setFilePath(outFile.getAbsolutePath()); | |
277 | + FileOutputStream fos = new FileOutputStream(outFile); | |
278 | + IOUtils.copy(file.getInputStream(), fos); | |
279 | + fos.flush(); | |
280 | + fos.close(); | |
281 | + dao.insert(fileList); | |
282 | + Message message = Message.success(); | |
283 | + message.setData(fileList); | |
284 | + return message; | |
285 | + | |
286 | + } | |
287 | +} | ... | ... |
fw-rp-man/src/main/java/cn/fw/rp/web/controller/LoginController.java
0 → 100644
1 | +++ a/fw-rp-man/src/main/java/cn/fw/rp/web/controller/LoginController.java | |
1 | +package cn.fw.rp.web.controller; | |
2 | + | |
3 | +import cn.fw.data.base.domain.common.Message; | |
4 | +import org.springframework.web.bind.annotation.RequestMapping; | |
5 | +import org.springframework.web.bind.annotation.RestController; | |
6 | + | |
7 | +/** | |
8 | + * @author suchu | |
9 | + * @since 2018/5/8 15:16 | |
10 | + */ | |
11 | +@RestController | |
12 | +public class LoginController { | |
13 | + | |
14 | + @RequestMapping("") | |
15 | + Message login(String username, String password) { | |
16 | + | |
17 | + return Message.success(); | |
18 | + } | |
19 | +} | ... | ... |
fw-rp-man/src/main/java/cn/fw/rp/web/controller/ProblemController.java
0 → 100644
1 | +++ a/fw-rp-man/src/main/java/cn/fw/rp/web/controller/ProblemController.java | |
1 | +package cn.fw.rp.web.controller; | |
2 | + | |
3 | +import cn.fw.data.base.db.query.Condition; | |
4 | +import cn.fw.data.base.db.query.QueryCriterion; | |
5 | +import cn.fw.data.base.domain.common.Message; | |
6 | +import cn.fw.rp.common.util.DateUtils; | |
7 | +import cn.fw.rp.common.util.StringUtils; | |
8 | +import cn.fw.rp.dao.ProblemListDao; | |
9 | +import cn.fw.rp.db.ProblemList; | |
10 | +import cn.fw.rp.man.ProblemDto; | |
11 | +import cn.fw.rp.man.ProblemQueryParam; | |
12 | +import org.springframework.beans.BeanUtils; | |
13 | +import org.springframework.beans.factory.annotation.Autowired; | |
14 | +import org.springframework.transaction.annotation.Transactional; | |
15 | +import org.springframework.util.Assert; | |
16 | +import org.springframework.web.bind.annotation.*; | |
17 | + | |
18 | +import java.util.List; | |
19 | + | |
20 | +/** | |
21 | + * 需求controller | |
22 | + * | |
23 | + * @author suchu | |
24 | + * @since 2018/5/8 16:05 | |
25 | + */ | |
26 | +@RestController() | |
27 | +@RequestMapping("/problem") | |
28 | +public class ProblemController { | |
29 | + @Autowired | |
30 | + ProblemListDao problemListDao; | |
31 | + | |
32 | + /** | |
33 | + * 查询problem 列表 | |
34 | + * | |
35 | + * @param queryParam | |
36 | + * @return | |
37 | + */ | |
38 | + @RequestMapping("/list") | |
39 | + public Message query(@RequestBody ProblemQueryParam queryParam) { | |
40 | + Message message = Message.success(); | |
41 | + QueryCriterion condition = Condition.open(ProblemList.class); | |
42 | + if (StringUtils.isValid(queryParam.getBeginDate())) { | |
43 | + condition.gte("createTime", queryParam.getBeginDate()); | |
44 | + } | |
45 | + if (StringUtils.isValid(queryParam.getEndDate())) { | |
46 | + condition.lte("createTime", queryParam.getEndDate()); | |
47 | + } | |
48 | + if (StringUtils.isValid(queryParam.getStatus())) { | |
49 | + condition.eq("status", queryParam.getStatus()); | |
50 | + } | |
51 | + if (StringUtils.isValid(queryParam.getSystemRef())) { | |
52 | + condition.eq("systemRef", queryParam.getSystemRef()); | |
53 | + } | |
54 | + if (StringUtils.isValid(queryParam.getModuleRef())) { | |
55 | + condition.eq("moduleRef", queryParam.getModuleRef()); | |
56 | + } | |
57 | + | |
58 | + if (StringUtils.isValid(queryParam.getTitle())) { | |
59 | + condition.like("title", queryParam.getTitle()); | |
60 | + } | |
61 | + condition.orderbyDesc("createTime"); | |
62 | + List list = problemListDao.selectList(condition); | |
63 | + message.setData(list); | |
64 | + return message; | |
65 | + } | |
66 | + | |
67 | + @RequestMapping("/getFiles") | |
68 | + public Message getFiles(Integer flag, Integer problemId) { | |
69 | + Assert.notNull(problemId, "problemId must not be null"); | |
70 | + List result = problemListDao.getFiles(flag, problemId); | |
71 | + Message message = Message.success(); | |
72 | + message.setData(result); | |
73 | + return message; | |
74 | + } | |
75 | + | |
76 | + /** | |
77 | + * 查询problem 通过id | |
78 | + * | |
79 | + * @param problemId | |
80 | + * @return | |
81 | + */ | |
82 | + @GetMapping("/get/{problemId}") | |
83 | + public Message get(@PathVariable Integer problemId) { | |
84 | + Assert.notNull(problemId, "problemId must not be null"); | |
85 | + ProblemList problemList = problemListDao.selectById(problemId); | |
86 | + if (problemList == null) { | |
87 | + return Message.failure("没查询到相关数据"); | |
88 | + } | |
89 | + Message message = Message.success(); | |
90 | + message.setData(problemList); | |
91 | + return message; | |
92 | + } | |
93 | + | |
94 | + /** | |
95 | + * 统计problem上传的文件量 | |
96 | + * | |
97 | + * @param problemId | |
98 | + * @return | |
99 | + */ | |
100 | + @RequestMapping("fileCount") | |
101 | + public Message fileCount(Integer problemId) { | |
102 | + Assert.notNull(problemId, "problemId must not be null"); | |
103 | + List data = problemListDao.fileCount(problemId); | |
104 | + Message message = Message.success(); | |
105 | + message.setData(data); | |
106 | + return message; | |
107 | + } | |
108 | + | |
109 | + | |
110 | + /** | |
111 | + * 保存接口 | |
112 | + * | |
113 | + * @param problemDto | |
114 | + * @return | |
115 | + */ | |
116 | + @Transactional(rollbackFor = Exception.class) | |
117 | + @RequestMapping("/save") | |
118 | + public Message save(@RequestBody ProblemDto problemDto) { | |
119 | + if (problemDto.getId() != null) { | |
120 | + System.out.println(problemDto.toString()); | |
121 | + ProblemList problemList = new ProblemList(); | |
122 | + BeanUtils.copyProperties(problemDto, problemList); | |
123 | + problemList.setUpdateTime(DateUtils.getNow()); | |
124 | + int i = problemListDao.updateById(problemList); | |
125 | + return Message.success(); | |
126 | + } else { | |
127 | + Message message = Message.success(); | |
128 | + ProblemList problemList = new ProblemList(); | |
129 | + BeanUtils.copyProperties(problemDto, problemList); | |
130 | + problemListDao.insert(problemList); | |
131 | + message.setCode(10); | |
132 | + message.setData(problemList.getId()); | |
133 | + return message; | |
134 | + } | |
135 | + } | |
136 | +} | ... | ... |
fw-rp-man/src/main/resources/application-dev.yml
0 → 100644
1 | +++ a/fw-rp-man/src/main/resources/application-dev.yml | |
1 | +server: | |
2 | + port: 8030 | |
3 | + tomcat.max-threads: 50 | |
4 | + tomcat.uri-encoding: UTF-8 | |
5 | + | |
6 | +eureka: | |
7 | + instance: | |
8 | + hostname: fw-marketing-man # 设置当前实例的主机名称 | |
9 | + prefer-ip-address: true #访问路径变更为IP | |
10 | + lease-renewal-interval-in-seconds: 5 #心跳间隔 | |
11 | + lease-expiration-duration-in-seconds: 5 #如果现在超过了5秒的间隔 | |
12 | + client: | |
13 | + serviceUrl: | |
14 | + defaultZone: http://feewee:1234qwer@deveureka.feewee.cn/eureka/ | |
15 | + fetch-registry: true | |
16 | + | |
17 | +spring: | |
18 | + application: | |
19 | + name: fw-marketing-man | |
20 | + datasource: | |
21 | + url: jdbc:mysql://192.168.0.8:3306/fw_rp?characterEncoding=UTF-8 | |
22 | + # 只有下面三个是必填项(使用内嵌数据库的话这三个也可以不用填,会使用默认配置),其他配置不是必须的 | |
23 | + username: root | |
24 | + password: mysql@pwd123 | |
25 | + driver-class-name: com.mysql.jdbc.Driver | |
26 | + initial-size: 2 | |
27 | + max-active: 30 | |
28 | + min-idle: 2 | |
29 | + max-wait: 1234 | |
30 | + pool-prepared-statements: true | |
31 | + max-pool-prepared-statement-per-connection-size: 5 | |
32 | + validation-query: SELECT 1 FROM DUAL | |
33 | + validation-query-timeout: 1 | |
34 | + test-on-borrow: true | |
35 | + test-on-return: true | |
36 | + test-while-idle: true | |
37 | + time-between-eviction-runs-millis: 10000 | |
38 | + min-evictable-idle-time-millis: 30001 | |
39 | + async-close-connection-enable: true | |
40 | + aop-patterns: cn.fw.third.pstn.dao.* | |
41 | + # 自定义StatFilter 配置 其他 Filter 不再演示 | |
42 | + filter: | |
43 | + stat: | |
44 | + db-type: mysql | |
45 | + log-slow-sql: true | |
46 | + slow-sql-millis: 2000 | |
47 | + redis: | |
48 | + host: 192.168.0.8 | |
49 | + http: | |
50 | + multipart: | |
51 | + max-file-size: 100Mb | |
52 | + max-request-size: 100Mb | |
53 | + | |
54 | + | |
55 | +mybatis: | |
56 | + type-aliases-package: cn.fw.rp.db cn.fw.data.base.db.query | |
57 | + mapper-locations: classpath:mapper/*.xml | |
58 | + configuration: | |
59 | + log-impl: org.apache.ibatis.logging.stdout.StdOutImpl | |
60 | + | |
61 | +jedis: | |
62 | + pool: | |
63 | + host: 192.168.0.8 | |
64 | + # host: 127.0.0.1 | |
65 | + port: 6379 | |
66 | + config: | |
67 | + maxActive: 50 | |
68 | + maxIdle: 20 | |
69 | + minIdle: 3 | |
70 | + maxWaitMillis: 1000 | |
71 | + testOnBorrow: false | |
72 | + testOnReturn: false | |
73 | + testWhileIdle: true | |
74 | + | |
75 | +auth: | |
76 | + aes: | |
77 | + key: bN7DVjFMJPcoWDcR | |
78 | + ignore: | |
79 | + start-with: /user/*,/role/*,/user/login,/v2/api-docs,/swagger-resources/** | |
80 | + | |
81 | +logging: | |
82 | + # path: ${dynamic.log.path} | |
83 | + level: | |
84 | + root: info | |
85 | + | |
86 | + | |
87 | +upload: | |
88 | + path: F:/RP_UPLOAD | |
0 | 89 | \ No newline at end of file | ... | ... |
fw-rp-man/src/main/resources/application-local.yml
0 → 100644
1 | +++ a/fw-rp-man/src/main/resources/application-local.yml | |
1 | +spring: | |
2 | + datasource: | |
3 | + url: jdbc:mysql://192.168.1.47:3306/fw_erp?characterEncoding=UTF-8 | |
4 | + # 只有下面三个是必填项(使用内嵌数据库的话这三个也可以不用填,会使用默认配置),其他配置不是必须的 | |
5 | + username: root | |
6 | + password: '07141105' | |
7 | + driver-class-name: com.mysql.jdbc.Driver | |
8 | + initial-size: 2 | |
9 | + max-active: 30 | |
10 | + min-idle: 2 | |
11 | + max-wait: 1234 | |
12 | + pool-prepared-statements: true | |
13 | + max-pool-prepared-statement-per-connection-size: 5 | |
14 | + validation-query: SELECT 1 FROM DUAL | |
15 | + validation-query-timeout: 1 | |
16 | + test-on-borrow: true | |
17 | + test-on-return: true | |
18 | + test-while-idle: true | |
19 | + time-between-eviction-runs-millis: 10000 | |
20 | + min-evictable-idle-time-millis: 30001 | |
21 | + async-close-connection-enable: true | |
22 | + aop-patterns: cn.fw.erp.dao.* | |
23 | + | |
24 | + # 自定义StatFilter 配置 其他 Filter 不再演示 | |
25 | + filter: | |
26 | + stat: | |
27 | + db-type: mysql | |
28 | + log-slow-sql: true | |
29 | + slow-sql-millis: 2000 | |
30 | + | |
31 | +mybatis: | |
32 | + type-aliases-package: cn.fw.erp.domain cn.fw.data.base.db.query | |
33 | + mapper-locations: classpath:mapper/*.xml | |
34 | +security: | |
35 | + aes: | |
36 | + key: bN7DVjFMJPcoWDcR | |
0 | 37 | \ No newline at end of file | ... | ... |
fw-rp-man/src/main/resources/application-prd.yml
0 → 100644
1 | +++ a/fw-rp-man/src/main/resources/application-prd.yml | |
1 | +server: | |
2 | + port: 8082 | |
3 | + tomcat.max-threads: 50 | |
4 | + tomcat.uri-encoding: UTF-8 | |
5 | + | |
6 | +eureka: | |
7 | + instance: | |
8 | + hostname: fw-marketing-man # 设置当前实例的主机名称 | |
9 | + prefer-ip-address: true #访问路径变更为IP | |
10 | + lease-renewal-interval-in-seconds: 5 #心跳间隔 | |
11 | + lease-expiration-duration-in-seconds: 5 #如果现在超过了5秒的间隔 | |
12 | + client: | |
13 | + serviceUrl: | |
14 | + defaultZone: http://feewee:1234qwer@eureka.feewee.cn/eureka/ | |
15 | + fetch-registry: true | |
16 | + | |
17 | +spring: | |
18 | + application: | |
19 | + name: fw-marketing-man | |
20 | + datasource: | |
21 | + url: jdbc:mysql://localhost:3306/fw_rp?characterEncoding=UTF-8 | |
22 | + # 只有下面三个是必填项(使用内嵌数据库的话这三个也可以不用填,会使用默认配置),其他配置不是必须的 | |
23 | + username: root | |
24 | + password: fw123456 | |
25 | + driver-class-name: com.mysql.jdbc.Driver | |
26 | + initial-size: 2 | |
27 | + max-active: 30 | |
28 | + min-idle: 2 | |
29 | + max-wait: 1234 | |
30 | + pool-prepared-statements: true | |
31 | + max-pool-prepared-statement-per-connection-size: 5 | |
32 | + validation-query: SELECT 1 FROM DUAL | |
33 | + validation-query-timeout: 1 | |
34 | + test-on-borrow: true | |
35 | + test-on-return: true | |
36 | + test-while-idle: true | |
37 | + time-between-eviction-runs-millis: 10000 | |
38 | + min-evictable-idle-time-millis: 30001 | |
39 | + async-close-connection-enable: true | |
40 | + aop-patterns: cn.fw.third.pstn.dao.* | |
41 | + # 自定义StatFilter 配置 其他 Filter 不再演示 | |
42 | + filter: | |
43 | + stat: | |
44 | + db-type: mysql | |
45 | + log-slow-sql: true | |
46 | + slow-sql-millis: 2000 | |
47 | + redis: | |
48 | + host: 172.26.154.159 | |
49 | + http: | |
50 | + multipart: | |
51 | + max-file-size: 100Mb | |
52 | + max-request-size: 100Mb | |
53 | +mybatis: | |
54 | + type-aliases-package: cn.fw.rp.db cn.fw.data.base.db.query | |
55 | + mapper-locations: classpath:mapper/*.xml | |
56 | + | |
57 | +jedis: | |
58 | + pool: | |
59 | + host: 172.26.154.159 | |
60 | + # host: 127.0.0.1 | |
61 | + port: 6379 | |
62 | + config: | |
63 | + maxActive: 50 | |
64 | + maxIdle: 20 | |
65 | + minIdle: 3 | |
66 | + maxWaitMillis: 1000 | |
67 | + testOnBorrow: false | |
68 | + testOnReturn: false | |
69 | + testWhileIdle: true | |
70 | + | |
71 | +auth: | |
72 | + aes: | |
73 | + key: bN7DVjFMJPcoWDcR | |
74 | + ignore: | |
75 | + start-with: /user/*,/role/*,/user/login,/v2/api-docs,/swagger-resources/** | |
76 | + | |
77 | +logging: | |
78 | + # path: ${dynamic.log.path} | |
79 | + level: | |
80 | + root: info | |
81 | + | |
82 | +upload: | |
83 | + path: E:/RP_UPLOAD | |
0 | 84 | \ No newline at end of file | ... | ... |
fw-rp-man/src/main/resources/application-test.yml
0 → 100644
1 | +++ a/fw-rp-man/src/main/resources/application-test.yml | |
1 | +spring: | |
2 | + datasource: | |
3 | + url: jdbc:mysql://192.168.1.47:3306/fw_erp?characterEncoding=UTF-8 | |
4 | + # 只有下面三个是必填项(使用内嵌数据库的话这三个也可以不用填,会使用默认配置),其他配置不是必须的 | |
5 | + username: root | |
6 | + password: '07141105' | |
7 | + driver-class-name: com.mysql.jdbc.Driver | |
8 | + initial-size: 2 | |
9 | + max-active: 30 | |
10 | + min-idle: 2 | |
11 | + max-wait: 1234 | |
12 | + pool-prepared-statements: true | |
13 | + max-pool-prepared-statement-per-connection-size: 5 | |
14 | + validation-query: SELECT 1 FROM DUAL | |
15 | + validation-query-timeout: 1 | |
16 | + test-on-borrow: true | |
17 | + test-on-return: true | |
18 | + test-while-idle: true | |
19 | + time-between-eviction-runs-millis: 10000 | |
20 | + min-evictable-idle-time-millis: 30001 | |
21 | + async-close-connection-enable: true | |
22 | + aop-patterns: cn.fw.erp.dao.* | |
23 | + | |
24 | + # 自定义StatFilter 配置 其他 Filter 不再演示 | |
25 | + filter: | |
26 | + stat: | |
27 | + db-type: mysql | |
28 | + log-slow-sql: true | |
29 | + slow-sql-millis: 2000 | |
30 | + | |
31 | +mybatis: | |
32 | + type-aliases-package: cn.fw.erp.domain cn.fw.data.base.db.query | |
33 | + mapper-locations: classpath:mapper/*.xml | |
34 | + | |
35 | +auth: | |
36 | + aes: | |
37 | + key: bN7DVjFMJPcoWDcR | |
38 | + ignore: | |
39 | + start-with: /user/*,/role/*,/user/login,/v2/api-docs,/swagger-resources/** | |
40 | + | |
41 | +logging: | |
42 | + path: ${dynamic.log.path} | |
43 | + level: | |
44 | + root: info | |
0 | 45 | \ No newline at end of file | ... | ... |
fw-rp-man/src/main/resources/application.yml
0 → 100644
fw-rp-man/src/main/resources/banner.txt
0 → 100644
1 | +++ a/fw-rp-man/src/main/resources/banner.txt | |
1 | + _____ ___________ _____ _ _ _____ _ _______ _____ | |
2 | +|_ _| _ | ___ \ ___| \ | |_ _| | | | ___ \ ___| | |
3 | + | | | | | | |_/ / |__ | \| | | | | | | | |_/ / |__ | |
4 | + | | | | | | ___ \ __|| . ` | | | | | | | ___ \ __| | |
5 | + | | \ \_/ / |_/ / |___| |\ |_| |_| |_| | |_/ / |___ | |
6 | + \_/ \___/\____/\____/\_| \_/\___/ \___/\____/\____/ | |
0 | 7 | \ No newline at end of file | ... | ... |
fw-rp-man/src/main/resources/logback-spring.xml
0 → 100644
1 | +++ a/fw-rp-man/src/main/resources/logback-spring.xml | |
1 | +<?xml version="1.0" encoding="UTF-8"?> | |
2 | +<configuration scan="true" scanPeriod="60 seconds" debug="false"> | |
3 | + <property name="charSet" value="UTF-8"/> | |
4 | + | |
5 | + <!-- log output to console --> | |
6 | + <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> | |
7 | + <encoder> | |
8 | + <pattern>[%-5p] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] [%c] - %m%n</pattern> | |
9 | + <charset>${charSet}</charset> | |
10 | + </encoder> | |
11 | + </appender> | |
12 | + | |
13 | + <!-- full log output to file --> | |
14 | + <appender name="DEFAULT_FILE" | |
15 | + class="ch.qos.logback.core.rolling.RollingFileAppender"> | |
16 | + <file>${dynamic.log.path}/fw_mkt_man_all.log</file> | |
17 | + <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> | |
18 | + <fileNamePattern>${dynamic.log.path}/fw_mkt_man_all.log.%d{yyyy-MM-dd}</fileNamePattern> | |
19 | + <maxHistory>7</maxHistory> | |
20 | + <totalSizeCap>1GB</totalSizeCap> | |
21 | + </rollingPolicy> | |
22 | + <encoder> | |
23 | + <pattern>[%-5p] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] [%c] - %m%n</pattern> | |
24 | + <charset>${charSet}</charset> | |
25 | + </encoder> | |
26 | + </appender> | |
27 | + | |
28 | + <appender name="LOG_ERROR_FILE" | |
29 | + class="ch.qos.logback.core.rolling.RollingFileAppender"> | |
30 | + <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> | |
31 | + <level>ERROR</level> | |
32 | + </filter> | |
33 | + <file>${dynamic.log.path}/fw_mkt_man_error.log</file> | |
34 | + <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> | |
35 | + <fileNamePattern>${dynamic.log.path}/fw_mkt_man_error.log.%d{yyyy-MM-dd}</fileNamePattern> | |
36 | + <maxHistory>7</maxHistory> | |
37 | + <totalSizeCap>1GB</totalSizeCap> | |
38 | + </rollingPolicy> | |
39 | + <encoder> | |
40 | + <pattern>[%-5p] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] [%c] - %m%n</pattern> | |
41 | + <charset>${charSet}</charset> | |
42 | + </encoder> | |
43 | + </appender> | |
44 | + | |
45 | + <root level="INFO"> | |
46 | + <!--<appender-ref ref="CONSOLE"/>--> | |
47 | + <appender-ref ref="DEFAULT_FILE"/> | |
48 | + <appender-ref ref="LOG_ERROR_FILE"/> | |
49 | + </root> | |
50 | +</configuration> | |
0 | 51 | \ No newline at end of file | ... | ... |
fw-rp-man/src/test/java/cn/fw/rp/web/MockMvcRequestBuilderUtils.java
0 → 100644
1 | +++ a/fw-rp-man/src/test/java/cn/fw/rp/web/MockMvcRequestBuilderUtils.java | |
1 | +package cn.fw.rp.web; | |
2 | + | |
3 | +import com.alibaba.fastjson.JSON; | |
4 | +import com.alibaba.fastjson.JSONObject; | |
5 | +import org.apache.commons.lang3.StringUtils; | |
6 | +import org.apache.commons.lang3.reflect.FieldUtils; | |
7 | +import org.slf4j.Logger; | |
8 | +import org.slf4j.LoggerFactory; | |
9 | +import org.springframework.http.MediaType; | |
10 | +import org.springframework.test.util.ReflectionTestUtils; | |
11 | +import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; | |
12 | +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; | |
13 | +import org.springframework.util.ClassUtils; | |
14 | +import org.springframework.util.CollectionUtils; | |
15 | + | |
16 | +import java.beans.PropertyEditor; | |
17 | +import java.lang.reflect.Field; | |
18 | +import java.lang.reflect.ParameterizedType; | |
19 | +import java.lang.reflect.Type; | |
20 | +import java.time.temporal.Temporal; | |
21 | +import java.util.*; | |
22 | + | |
23 | +/** | |
24 | + * Custom MockMvcRequestBuilder to post an entire form to a given url. | |
25 | + * Useful to test Spring MVC form validation. | |
26 | + * | |
27 | + * @author Florian Lopes | |
28 | + * @see org.springframework.test.web.servlet.request.MockMvcRequestBuilders | |
29 | + */ | |
30 | +public class MockMvcRequestBuilderUtils { | |
31 | + | |
32 | + private static final Map<Class, PropertyEditor> propertyEditors = new HashMap<>(); | |
33 | + | |
34 | + private static final Logger LOGGER = LoggerFactory.getLogger(MockMvcRequestBuilderUtils.class); | |
35 | + | |
36 | + private MockMvcRequestBuilderUtils() { | |
37 | + } | |
38 | + | |
39 | + /** | |
40 | + * Register custom property editor for a given type. | |
41 | + * | |
42 | + * @param type type of the property | |
43 | + * @param propertyEditor {@link PropertyEditor} to register | |
44 | + */ | |
45 | + public static void registerPropertyEditor(Class type, PropertyEditor propertyEditor) { | |
46 | + propertyEditors.put(type, propertyEditor); | |
47 | + } | |
48 | + | |
49 | + /** | |
50 | + * Unregister custom property editor for a given type | |
51 | + * | |
52 | + * @param type type of the property | |
53 | + */ | |
54 | + public static void unregisterPropertyEditor(Class type) { | |
55 | + propertyEditors.remove(type); | |
56 | + } | |
57 | + | |
58 | + /** | |
59 | + * Unregister all previously registered property editors | |
60 | + */ | |
61 | + public static void unregisterPropertyEditors() { | |
62 | + propertyEditors.clear(); | |
63 | + } | |
64 | + | |
65 | + /** | |
66 | + * Post a form to the given url. | |
67 | + * All non-null form fields will be added as HTTP request parameters using POST method | |
68 | + * | |
69 | + * @param url the URL to post the form to | |
70 | + * @param form form object to send using POST method | |
71 | + * @return mockHttpServletRequestBuilder wrapped mockHttpServletRequestBuilder | |
72 | + */ | |
73 | + public static MockHttpServletRequestBuilder postForm(String url, Object form) { | |
74 | + final MockHttpServletRequestBuilder mockHttpServletRequestBuilder = MockMvcRequestBuilders.post(url) | |
75 | + .contentType(MediaType.APPLICATION_FORM_URLENCODED); | |
76 | + final Map<String, String> formFields = getFormFields(form, new TreeMap<>(), StringUtils.EMPTY); | |
77 | + formFields.forEach((path, value) -> { | |
78 | + LOGGER.debug(String.format("Adding form field (%s=%s) to HTTP request parameters", path, value)); | |
79 | + mockHttpServletRequestBuilder.param(path, value); | |
80 | + }); | |
81 | + | |
82 | + return mockHttpServletRequestBuilder; | |
83 | + } | |
84 | + | |
85 | + public static MockHttpServletRequestBuilder postForm(String url, JSONObject object) { | |
86 | + final MockHttpServletRequestBuilder mockHttpServletRequestBuilder = MockMvcRequestBuilders.post(url) | |
87 | + .contentType(MediaType.APPLICATION_FORM_URLENCODED); | |
88 | + object.forEach((path, value) -> { | |
89 | + LOGGER.debug(String.format("Adding form field (%s=%s) to HTTP request parameters", path, value)); | |
90 | + | |
91 | + if (object.get(path) instanceof String) { | |
92 | + LOGGER.info("{}:{}", path, object.get(path)); | |
93 | + mockHttpServletRequestBuilder.param(path, (String) object.get(path)); | |
94 | + } else { | |
95 | + LOGGER.info("{}:{}", path, JSON.toJSONString(object.get(path))); | |
96 | + mockHttpServletRequestBuilder.param(path, JSON.toJSONString(object.get(path))); | |
97 | + } | |
98 | + | |
99 | + }); | |
100 | + | |
101 | + return mockHttpServletRequestBuilder; | |
102 | + } | |
103 | + | |
104 | + private static Map<String, String> getFormFields(Object form, Map<String, String> formFields, String path) { | |
105 | + final List<Field> fields = form != null ? Arrays.asList(FieldUtils.getAllFields(form.getClass())) : new ArrayList<>(); | |
106 | + for (Field field : fields) { | |
107 | + final Class<?> fieldType = field.getType(); | |
108 | + final Object fieldValue = getFieldValue(form, field); | |
109 | + if (isIterable(fieldType)) { | |
110 | + final Iterable<?> iterableObject = getIterable(fieldValue, fieldType); | |
111 | + if (iterableObject != null) { | |
112 | + if (isComplexField(field)) { | |
113 | + int i = 0; | |
114 | + for (Object object : iterableObject) { | |
115 | + final String nestedPath = getPositionedField(path, field, i) + "."; | |
116 | + formFields.putAll(getFormFields(object, formFields, nestedPath)); | |
117 | + i++; | |
118 | + } | |
119 | + } else { | |
120 | + formFields.putAll(getCollectionFields(iterableObject, path, field)); | |
121 | + } | |
122 | + } | |
123 | + } else if (isMap(fieldType)) { | |
124 | + final Map<?, ?> map = getMap(fieldValue, fieldType); | |
125 | + if (map != null) { | |
126 | + map.forEach((key, value) -> formFields.put(getPositionedField(path, field, getStringValue(key)), getStringValue(value))); | |
127 | + } | |
128 | + } else { | |
129 | + if (isComplexField(field)) { | |
130 | + final String nestedPath = getNestedPath(field); | |
131 | + formFields.putAll(getFormFields(ReflectionTestUtils.getField(form, field.getName()), formFields, nestedPath)); | |
132 | + } else { | |
133 | + formFields.put(path + field.getName(), getFieldStringValue(form, field)); | |
134 | + } | |
135 | + } | |
136 | + } | |
137 | + return formFields; | |
138 | + } | |
139 | + | |
140 | + private static Map<?, ?> getMap(Object fieldValue, Class<?> type) { | |
141 | + return Map.class.isAssignableFrom(type) ? (Map) fieldValue : null; | |
142 | + } | |
143 | + | |
144 | + private static Iterable<?> getIterable(Object fieldValue, Class<?> type) { | |
145 | + return Iterable.class.isAssignableFrom(type) ? (Iterable<?>) fieldValue : CollectionUtils.arrayToList(fieldValue); | |
146 | + } | |
147 | + | |
148 | + private static Map<String, String> getCollectionFields(Iterable<?> iterable, String path, Field field) { | |
149 | + final Map<String, String> fields = new TreeMap<>(); | |
150 | + int i = 0; | |
151 | + for (Object object : iterable) { | |
152 | + fields.put(getPositionedField(path, field, i), getStringValue(object)); | |
153 | + i++; | |
154 | + } | |
155 | + return fields; | |
156 | + } | |
157 | + | |
158 | + private static String getPositionedField(String path, Field field, int position) { | |
159 | + return getPositionedField(path, field, String.valueOf(position)); | |
160 | + } | |
161 | + | |
162 | + private static String getPositionedField(String path, Field field, String positionOrKey) { | |
163 | + return String.format("%s%s[%s]", path, field.getName(), positionOrKey); | |
164 | + } | |
165 | + | |
166 | + private static String getNestedPath(Field field) { | |
167 | + return field.getName() + "."; | |
168 | + } | |
169 | + | |
170 | + private static Object getFieldValue(Object form, Field field) { | |
171 | + return ReflectionTestUtils.getField(form, field.getName()); | |
172 | + } | |
173 | + | |
174 | + private static String getFieldStringValue(Object object, Field field) { | |
175 | + return getStringValue(getFieldValue(object, field)); | |
176 | + } | |
177 | + | |
178 | + private static String getStringValue(Object object) { | |
179 | + if (object != null) { | |
180 | +// final PropertyEditor propertyEditor = getPropertyEditorFor(object); | |
181 | +// if (propertyEditor != null) { | |
182 | +// propertyEditor.setValue(object); | |
183 | +// return propertyEditor.getAsText(); | |
184 | +// } | |
185 | + // Default strategy | |
186 | + return JSONObject.toJSONString(object); | |
187 | + } | |
188 | + return StringUtils.EMPTY; | |
189 | + } | |
190 | + | |
191 | + private static PropertyEditor getPropertyEditorFor(Object object) { | |
192 | + final Optional<Map.Entry<Class, PropertyEditor>> propertyEditorEntry = propertyEditors.entrySet().stream() | |
193 | + .filter(entry -> entry.getKey().equals(object.getClass())) | |
194 | + .findFirst(); | |
195 | + return propertyEditorEntry.map(Map.Entry::getValue).orElse(null); | |
196 | + } | |
197 | + | |
198 | + private static boolean isComplexField(Field field) { | |
199 | + if (isGeneric(field)) { | |
200 | + final ParameterizedType genericType = (ParameterizedType) field.getGenericType(); | |
201 | + final Type[] actualTypeArguments = genericType.getActualTypeArguments(); | |
202 | + return isComplexType((Class<?>) actualTypeArguments[0]); | |
203 | + } else { | |
204 | + return isComplexType(field.getType()); | |
205 | + } | |
206 | + } | |
207 | + | |
208 | + private static boolean isGeneric(Field field) { | |
209 | + return !(field.getGenericType() instanceof Class); | |
210 | + } | |
211 | + | |
212 | + private static boolean isComplexType(Class<?> type) { | |
213 | + if (type.getComponentType() != null) { | |
214 | + return isComplexType(type.getComponentType()); | |
215 | + } | |
216 | + return !ClassUtils.isPrimitiveOrWrapper(type) | |
217 | + && !String.class.isAssignableFrom(type) | |
218 | + && !Date.class.isAssignableFrom(type) | |
219 | + && !Temporal.class.isAssignableFrom(type) | |
220 | + && type.getSuperclass() != null | |
221 | + && !Enum.class.isAssignableFrom(type.getSuperclass()); | |
222 | + } | |
223 | + | |
224 | + private static boolean isIterable(Class fieldClass) { | |
225 | + return Iterable.class.isAssignableFrom(fieldClass) || Object[].class.isAssignableFrom(fieldClass); | |
226 | + } | |
227 | + | |
228 | + private static boolean isMap(Class<?> type) { | |
229 | + return Map.class.isAssignableFrom(type); | |
230 | + } | |
231 | +} | |
0 | 232 | \ No newline at end of file | ... | ... |
fw-rp-sdk/pom.xml
0 → 100644
1 | +++ a/fw-rp-sdk/pom.xml | |
1 | +<?xml version="1.0" encoding="UTF-8"?> | |
2 | +<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
3 | + xmlns="http://maven.apache.org/POM/4.0.0" | |
4 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
5 | + <modelVersion>4.0.0</modelVersion> | |
6 | + <groupId>cn.fw</groupId> | |
7 | + <artifactId>fw-rp-sdk</artifactId> | |
8 | + <packaging>jar</packaging> | |
9 | + <version>1.0</version> | |
10 | + <name>fw-rp-sdk</name> | |
11 | + | |
12 | + <properties> | |
13 | + <jdk.version>1.8</jdk.version> | |
14 | + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
15 | + <maven.compiler.encoding>UTF-8</maven.compiler.encoding> | |
16 | + </properties> | |
17 | + | |
18 | + <dependencyManagement> | |
19 | + <dependencies> | |
20 | + <dependency> | |
21 | + <groupId>org.springframework.cloud</groupId> | |
22 | + <artifactId>spring-cloud-dependencies</artifactId> | |
23 | + <version>Dalston.SR1</version> | |
24 | + <type>pom</type> | |
25 | + <scope>import</scope> | |
26 | + </dependency> | |
27 | + </dependencies> | |
28 | + </dependencyManagement> | |
29 | + <dependencies> | |
30 | + <dependency> | |
31 | + <groupId>org.springframework.cloud</groupId> | |
32 | + <artifactId>spring-cloud-starter-feign</artifactId> | |
33 | + </dependency> | |
34 | + </dependencies> | |
35 | + | |
36 | + <distributionManagement> | |
37 | + <repository> | |
38 | + <id>releases</id> | |
39 | + <name>Nexus Release Repository</name> | |
40 | + <url>http://nexus.feewee.cn/nexus/content/repositories/releases/</url> | |
41 | + </repository> | |
42 | + <snapshotRepository> | |
43 | + <id>snapshots</id> | |
44 | + <name>Nexus Snapshot Repository</name> | |
45 | + <url>http://nexus.feewee.cn/nexus/content/repositories/snapshots/</url> | |
46 | + <uniqueVersion>true</uniqueVersion> | |
47 | + </snapshotRepository> | |
48 | + </distributionManagement> | |
49 | + | |
50 | + <build> | |
51 | + <pluginManagement> | |
52 | + <plugins> | |
53 | + <plugin> | |
54 | + <groupId>org.apache.maven.plugins</groupId> | |
55 | + <artifactId>maven-compiler-plugin</artifactId> | |
56 | + <version>3.6.1</version> | |
57 | + <configuration> | |
58 | + <source>1.8</source> | |
59 | + <target>1.8</target> | |
60 | + </configuration> | |
61 | + </plugin> | |
62 | + </plugins> | |
63 | + </pluginManagement> | |
64 | + </build> | |
65 | +</project> | |
0 | 66 | \ No newline at end of file | ... | ... |