好久没更新了 项目经理给了很多活 我还得996干活 做了一个小小的工具类 有用的就c走
package com.ruoyi.common.utils;
import com.ruoyi.common.exception.user.FormatException;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.apache.poi.ss.formula.functions.T;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URI;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Function;
import java.util.function.Predicate;
/**
* 我自己用的工具类
* What good will it be for a man if he gains the whole world, yet forfeits his soul?
* Or what can a man give in exchange for his soul?
* author 马亮
*/
public class ChijingUtils {
/**
* 判断
*
* @param t 任何类型对象
* @param msg 报错信息
*/
public static <T> void checkT(T t, Predicate<T> predicate, String msg) {
if (predicate.test(t)) {
throw new FormatException(msg);
}
}
/**
* 判断 t至少和t1有一项是相同的不然就报错
*
* @param t 任何类型对象
* @param t1 匹配的集合
* @param msg 报错信息
*/
public static void notIncludeExce(String msg, Integer t, Integer... t1) {
boolean b = true;
for (Integer temp : t1) {
if (temp.equals(t)) {
b = false;
break;
}
}
if (b) {
throw new FormatException(msg);
}
}
public static void notIncludeExce(String msg, Integer t, List<Integer> t1) {
boolean b = true;
for (Integer temp : t1) {
if (temp.equals(t)) {
b = false;
break;
}
}
if (b) {
throw new FormatException(msg);
}
}
/**
* 判断 t至少和t1有一项是相同的不然就报错
*
* @param t 任何类型对象
* @param t1 匹配的集合
* @param msg 报错信息
*/
public static void notIncludeExce(String msg, String t, String... t1) {
boolean b = true;
for (String temp : t1) {
if (temp.equals(t)) {
b = false;
break;
}
}
if (b) {
throw new FormatException(msg);
}
}
public static void notIncludeExce(String msg, String t, List<String> t1) {
boolean b = true;
for (String temp : t1) {
if (temp.equals(t)) {
b = false;
break;
}
}
if (b) {
throw new FormatException(msg);
}
}
/**
* 判断 t至少和t1有一项是相同的就报错
*
* @param t 任何类型对象
* @param t1 匹配的集合
* @param msg 报错信息
*/
public static void includeExce(String msg, Integer t, Integer... t1) {
for (Integer temp : t1) {
if (temp.equals(t)) {
throw new FormatException(msg);
}
}
}
public static void includeExce(String msg, Integer t, List<Integer> t1) {
for (Integer temp : t1) {
if (temp.equals(t)) {
throw new FormatException(msg);
}
}
}
/**
* 判断 t至少和t1有一项是相同的不然就报错
*
* @param t 任何类型对象
* @param t1 匹配的集合
* @param msg 报错信息
*/
public static void includeExce(String msg, String t, String... t1) {
for (String temp : t1) {
if (temp.equals(t)) {
throw new FormatException(msg);
}
}
}
public static void includeExce(String msg, String t, List<String> t1) {
for (String temp : t1) {
if (temp.equals(t)) {
throw new FormatException(msg);
}
}
}
/**
* 判断 t至少和t1有一项是相同的不然就报错
*
* @param t 任何类型对象
* @param t1 匹配的集合
*/
public static boolean isInclude(Integer t, Integer... t1) {
for (Integer temp : t1) {
if (temp.equals(t)) {
return true;
}
}
return false;
}
public static boolean isInclude(Integer t, List<Integer> t1) {
for (Integer temp : t1) {
if (temp.equals(t)) {
return true;
}
}
return false;
}
/**
* 判断 t至少和t1有一项是相同的不然就报错
*
* @param t 任何类型对象
* @param t1 匹配的集合
*/
public static boolean isInclude(String t, String... t1) {
boolean b = true;
for (String temp : t1) {
if (temp.equals(t)) {
return true;
}
}
return false;
}
public static boolean isInclude(String t, List<String> t1) {
for (String temp : t1) {
if (temp.equals(t)) {
return true;
}
}
return false;
}
/**
* 判断是否是空
*
* @param t 任何类型对象
* @param msg 报错信息
*/
public static <T> void isNullExce(T t, String msg) {
if (t == null) {
throw new FormatException(msg);
}
}
public static void isNullExce(String t, String msg) {
if (StringUtils.isBlank(t)) {
throw new FormatException(msg);
}
}
/**
* 判断是否不为空
*/
public static <T> void notNullExce(T t, String msg) {
if (t != null) {
throw new FormatException(msg);
}
}
/**
* 判断是否不为空
*/
public static void notNullExce(String t, String msg) {
if (StringUtils.isNotEmpty(t)) {
throw new FormatException(msg);
}
}
/**
* 对集合做处理
*
* @param t 参数
* @param predicate 判断
* @param function 处理
* @param <T> 泛型
* @return 新的集合
*/
public static <T> List<T> handle(List<T> t, Predicate<T> predicate, Function<T, T> function) {
List<T> listnew = new ArrayList<>();
for (T t1 : t) {
if (predicate.test(t1)) {
T apply = function.apply(t1);
listnew.add(apply);
} else {
listnew.add(t1);
}
}
return listnew;
}
public static T handle(T t, Predicate<T> predicate, Function<T, T> function) {
if (predicate.test(t)) {
return function.apply(t);
}
return t;
}
/**
* collect
* 获得的格式化后当前时间
*
* @return 当前时间
*/
public static String now() {
return LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
}
/**
* 获得的格式化后当前时间
*
* @return 当前时间
*/
public static String now(String format) {
return LocalDateTime.now().format(DateTimeFormatter.ofPattern(format));
}
/**
* @return 当月第一天
*/
public static String getFirstDayOfMonth() {
return LocalDateTime.now().with(TemporalAdjusters.firstDayOfMonth()).format(DateTimeFormatter.ofPattern("yyyy-MM-dd 00:00:00"));
}
/**
* @return 当月最后一天
*/
public static String getLastDayOfMonth() {
return LocalDateTime.now().with(TemporalAdjusters.lastDayOfMonth()).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
}
/**
* @return 当前季度第一天
*/
public static String getFirtstOfQuarter() {
int month = LocalDateTime.now().getMonthValue();
// 第四季度
if (month >= 10) {
return ChijingUtils.now("yyyy-10-01 00:00:00");
// 第三季度
} else if (month >= 7) {
return ChijingUtils.now("yyyy-07-01 00:00:00");
// 第二季度
} else if (month >= 4) {
return ChijingUtils.now("yyyy-04-01 00:00:00");
// 第一季度
} else {
return ChijingUtils.now("yyyy-01-01 00:00:00");
}
}
/**
* @return 当前季度第一天
*/
public static String getFirtstOfQuarter(int month) {
// 第四季度
if (month >= 10) {
return ChijingUtils.now("yyyy-10-01 00:00:00");
// 第三季度
} else if (month >= 7) {
return ChijingUtils.now("yyyy-07-01 00:00:00");
// 第二季度
} else if (month >= 4) {
return ChijingUtils.now("yyyy-04-01 00:00:00");
// 第一季度
} else {
return ChijingUtils.now("yyyy-01-01 00:00:00");
}
}
/**
* @return 当前月份 季度第一天
*/
public static String getLastOfQuarter(int month) {
// 第四季度
if (month >= 10) {
return ChijingUtils.now("yyyy-12-01 00:00:00");
// 第三季度
} else if (month >= 7) {
return ChijingUtils.now("yyyy-09-01 00:00:00");
// 第二季度
} else if (month >= 4) {
return ChijingUtils.now("yyyy-06-01 00:00:00");
// 第一季度
} else {
return ChijingUtils.now("yyyy-03-01 00:00:00");
}
}
/**
* @return 当前季度第一天
*/
public static String getLastOfQuarter() {
int month = LocalDateTime.now().getMonthValue();
// 第四季度
if (month >= 10) {
return ChijingUtils.now("yyyy-12-01 00:00:00");
// 第三季度
} else if (month >= 7) {
return ChijingUtils.now("yyyy-09-01 00:00:00");
// 第二季度
} else if (month >= 4) {
return ChijingUtils.now("yyyy-06-01 00:00:00");
// 第一季度
} else {
return ChijingUtils.now("yyyy-03-01 00:00:00");
}
}
/**
* 对date类型时间格式化后返回字符窜时间
*
* @param date 时间参数
* @param format 格式
* @return 字符串时间
*/
public static String dateToString(Date date, String format) {
return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).format(DateTimeFormatter.ofPattern(format));
}
/**
* 把eneity包装成map
*
* @param obj 数据
* @return map
*/
public static Map<String, Object> createMapEntity(String key, Object obj) {
Map<String, Object> map = new HashMap<>();
map.put(key, obj);
return map;
}
/**
* 判断多项是否都是空 多项都不是空返回true,有一项是空返回false
*/
public static boolean anyNotBlank(String... strings) {
for (String string : strings) {
if (StringUtils.isBlank(string)) {
return false;
}
}
return true;
}
/**
* 判断多项是否都是空
*/
public static boolean anyNotBlank(Object... objects) {
for (Object o : objects) {
if (o == null) {
return false;
}
}
return true;
}
/**
* 校验是否每一个事务都成功
*
* @param msg
* @param ids
*/
public static void checkAnySuccess(String msg, Integer... ids) {
for (Integer id : ids) {
if (id == 0) {
throw new FormatException(msg);
}
}
}
public static String doGet(String url, Map<String, String> param) {
// 创建Httpclient对象
CloseableHttpClient httpclient = HttpClients.createDefault();
String resultString = "";
CloseableHttpResponse response = null;
try {
// 创建uri
URIBuilder builder = new URIBuilder(url);
if (param != null) {
for (String key : param.keySet()) {
builder.addParameter(key, param.get(key));
}
}
URI uri = builder.build();
// 创建http GET请求
HttpGet httpGet = new HttpGet(uri);
// 执行请求
response = httpclient.execute(httpGet);
// 判断返回状态是否为200
if (response.getStatusLine().getStatusCode() == 200) {
resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (response != null) {
response.close();
}
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return resultString;
}
public static String doGet(String url) {
return doGet(url, null);
}
public static String doPost(String url, Map<String, String> param) {
// 创建Httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String resultString = "";
try {
// 创建Http Post请求
HttpPost httpPost = new HttpPost(url);
// 创建参数列表
if (param != null) {
List<NameValuePair> paramList = new ArrayList<>();
for (String key : param.keySet()) {
paramList.add(new BasicNameValuePair(key, param.get(key)));
}
// 模拟表单
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList);
httpPost.setEntity(entity);
}
// 执行http请求
response = httpClient.execute(httpPost);
resultString = EntityUtils.toString(response.getEntity(), "utf-8");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return resultString;
}
public static String doPost(String url) {
return doPost(url, null);
}
public static String doPostJson(String url, String json) {
// 创建Httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String resultString = "";
try {
// 创建Http Post请求
HttpPost httpPost = new HttpPost(url);
// 创建请求内容
StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
httpPost.setEntity(entity);
// 执行http请求
response = httpClient.execute(httpPost);
resultString = EntityUtils.toString(response.getEntity(), "utf-8");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return resultString;
}
/**
* 判断入参的每一个参数是否都是空
*
* @param t 任何类型
* @return 是否全是null
*/
public static boolean allFieldAreNull(Object t) {
if (t == null) {
return true;
}
List<Object> list = new ArrayList<>();
Class<? extends Object> classLoader = t.getClass();
Method[] methods = classLoader.getMethods();
for (Method method : methods) {
if (method.getName().contains("get")
&& (method.getName().length() > 3 && !"getClass".equals(method.getName())) && !"getParams".equals(method.getName())) {
try {
Object invoke = method.invoke(t);
list.add(invoke);
} catch (InvocationTargetException e) {
list.add(null);
e.getTargetException();
} catch (IllegalArgumentException e) {
list.add(null);
e.getMessage();
} catch (IllegalAccessException e) {
list.add(null);
e.getMessage();
}
}
}
AtomicBoolean b = new AtomicBoolean(true);
list.forEach(e -> {
if (e != null) {
b.set(false);
}
});
return b.get();
}
}