You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
339 lines
9.2 KiB
339 lines
9.2 KiB
2 years ago
|
package com.ynxbd.common.helper.common;
|
||
|
|
||
|
import lombok.extern.slf4j.Slf4j;
|
||
|
import org.apache.commons.lang3.ObjectUtils;
|
||
|
|
||
|
import java.text.ParseException;
|
||
|
import java.text.SimpleDateFormat;
|
||
|
import java.util.ArrayList;
|
||
|
import java.util.Collections;
|
||
|
import java.util.Date;
|
||
|
import java.util.List;
|
||
|
|
||
|
/**
|
||
|
* 参数处理
|
||
|
*/
|
||
|
@Slf4j
|
||
|
public class ParamHelper {
|
||
|
|
||
|
/**
|
||
|
* 数据空格处理
|
||
|
*
|
||
|
* @param data 被处理数据
|
||
|
* @return 处理后的数据
|
||
|
*/
|
||
|
public static String spaceFormat(String data) {
|
||
|
if (ObjectUtils.isEmpty(data)) {
|
||
|
return null;
|
||
|
}
|
||
|
data = data.trim();
|
||
|
if ("".equals(data)) {
|
||
|
return null;
|
||
|
}
|
||
|
return data;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* ids转换处理
|
||
|
*
|
||
|
* @param ids id集合
|
||
|
* @return id集
|
||
|
*/
|
||
|
public static List<Long> splitIds(String ids) {
|
||
|
List<Long> rIds = new ArrayList<>();
|
||
|
if (ObjectUtils.isEmpty(ids)) {
|
||
|
return rIds;
|
||
|
}
|
||
|
|
||
|
ids = ids.replace(" ", "");
|
||
|
|
||
|
String[] idArr = ids.split(",");
|
||
|
long idLong;
|
||
|
for (String id : idArr) {
|
||
|
try {
|
||
|
idLong = Long.parseLong(id);
|
||
|
} catch (Exception e) {
|
||
|
continue;
|
||
|
}
|
||
|
if (!rIds.contains(idLong)) {
|
||
|
rIds.add(idLong);
|
||
|
}
|
||
|
}
|
||
|
Collections.sort(rIds);
|
||
|
return rIds;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* ids转换处理
|
||
|
*
|
||
|
* @param ids id集字符串
|
||
|
* @return id集
|
||
|
*/
|
||
|
public static String verifyIds(String ids) {
|
||
|
return verifyIds(splitIds(ids));
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* ids转换处理
|
||
|
*
|
||
|
* @param idList id集合
|
||
|
* @return id集
|
||
|
*/
|
||
|
public static String verifyIds(List<Long> idList) {
|
||
|
if (idList.size() == 0) {
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
List<Long> dataList = new ArrayList<>();
|
||
|
for (Long idItem : idList) {
|
||
|
if (!dataList.contains(idItem)) {
|
||
|
dataList.add(idItem);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
StringBuilder sb = new StringBuilder();
|
||
|
for (int i = 0; i < dataList.size(); i++) {
|
||
|
sb.append(dataList.get(i));
|
||
|
if ((i + 1) != dataList.size()) {
|
||
|
sb.append(",");
|
||
|
}
|
||
|
}
|
||
|
return sb.toString();
|
||
|
}
|
||
|
|
||
|
|
||
|
public static boolean verifyPassword(String password) {
|
||
|
return password.length() >= 6 && password.length() <= 34;
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* ids转换处理
|
||
|
*
|
||
|
* @param id id
|
||
|
*/
|
||
|
public static String idToStr(Integer id) {
|
||
|
if (id == null) {
|
||
|
return null;
|
||
|
}
|
||
|
try {
|
||
|
return Integer.toString(id);
|
||
|
} catch (Exception e) {
|
||
|
return null;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* ids转换处理
|
||
|
*
|
||
|
* @param id id
|
||
|
*/
|
||
|
public static String idToStr(Long id) {
|
||
|
if (id == null) {
|
||
|
return null;
|
||
|
}
|
||
|
try {
|
||
|
return Long.toString(id);
|
||
|
} catch (Exception e) {
|
||
|
return null;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* 身份证过过滤
|
||
|
*
|
||
|
* @param idCardNo 身份证
|
||
|
* @return idCardNo
|
||
|
*/
|
||
|
public static String idCardNoFilter(String idCardNo) {
|
||
|
if (ObjectUtils.isEmpty(idCardNo)) {
|
||
|
return null;
|
||
|
}
|
||
|
if (idCardNo.length() == 18) {
|
||
|
return idCardNo.substring(0, 4) + "**********" + idCardNo.substring(14);
|
||
|
}
|
||
|
if (idCardNo.length() == 15) {
|
||
|
return idCardNo.substring(0, 4) + "*******" + idCardNo.substring(11);
|
||
|
}
|
||
|
|
||
|
if (idCardNo.length() == 8) { // 台胞证
|
||
|
return idCardNo.substring(0, 2) + "****" + idCardNo.substring(6);
|
||
|
}
|
||
|
|
||
|
if (idCardNo.length() == 9) { // 港澳
|
||
|
return idCardNo.substring(0, 3) + "****" + idCardNo.substring(7);
|
||
|
}
|
||
|
return idCardNo;
|
||
|
}
|
||
|
|
||
|
|
||
|
public static String telFilter(String tel) {
|
||
|
if (ObjectUtils.isEmpty(tel)) {
|
||
|
return null;
|
||
|
}
|
||
|
if (tel.length() == 11) {
|
||
|
return tel.substring(0, 3) + "*****" + tel.substring(8);
|
||
|
}
|
||
|
if (tel.length() == 7) {
|
||
|
return tel.substring(0, 2) + "***" + tel.substring(5);
|
||
|
}
|
||
|
if (tel.length() > 11) {
|
||
|
StringBuilder sb = new StringBuilder();
|
||
|
for (int i = 0; i < (tel.length() - 6); i++) {
|
||
|
sb.append("*");
|
||
|
}
|
||
|
return tel.substring(0, 3) + sb + tel.substring(8);
|
||
|
}
|
||
|
return tel;
|
||
|
}
|
||
|
|
||
|
|
||
|
public static String nameFilter(String name) {
|
||
|
if (name == null) {
|
||
|
return null;
|
||
|
}
|
||
|
name = name.trim();
|
||
|
if ("".equals(name)) {
|
||
|
return null;
|
||
|
}
|
||
|
int length = name.length();
|
||
|
if (length == 2) {
|
||
|
name = name.substring(0, 1) + "*";
|
||
|
} else if (length > 2) {
|
||
|
StringBuilder x = new StringBuilder();
|
||
|
for (int i = 0; i < length - 2; i++) {
|
||
|
x.append("*");
|
||
|
}
|
||
|
name = name.substring(0, 1) + x + name.substring(length - 1, length);
|
||
|
}
|
||
|
return name;
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* 根据身份证号获取性别
|
||
|
*
|
||
|
* @param idNumber 身份证号码
|
||
|
* @return 性别
|
||
|
*/
|
||
|
public static String getSex(String idNumber) {
|
||
|
if (isValid(idNumber)) return null;
|
||
|
String sex;
|
||
|
if (idNumber.length() == 18) {
|
||
|
sex = Integer.parseInt(idNumber.substring(16, 17)) % 2 == 1 ? "男" : "女";
|
||
|
} else {
|
||
|
sex = Integer.parseInt(idNumber.substring(14, 15)) % 2 == 1 ? "男" : "女";
|
||
|
}
|
||
|
return sex;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 根据身份证号获取年龄
|
||
|
*
|
||
|
* @param idNumber 身份证号码
|
||
|
* @return 年龄
|
||
|
*/
|
||
|
public static Integer getAge(String idNumber) {
|
||
|
if (isValid(idNumber)) return null;
|
||
|
|
||
|
String year, month, day;
|
||
|
if (idNumber.length() == 18) {
|
||
|
year = idNumber.substring(6, 10);// 得到年份
|
||
|
month = idNumber.substring(10, 12);// 得到月份
|
||
|
day = idNumber.substring(12, 14);//得到日
|
||
|
} else {
|
||
|
year = "19" + idNumber.substring(6, 8);// 年份
|
||
|
month = idNumber.substring(8, 10);// 月份
|
||
|
day = idNumber.substring(10, 12);//日
|
||
|
}
|
||
|
Date date = new Date();// 得到当前的系统时间
|
||
|
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
|
||
|
String curYear = format.format(date).substring(0, 4);// 当前年份
|
||
|
String curMonth = format.format(date).substring(5, 7);// 月份
|
||
|
String curDay = format.format(date).substring(8, 10);//
|
||
|
|
||
|
int age = Integer.parseInt(curYear) - Integer.parseInt(year);
|
||
|
|
||
|
if (Integer.parseInt(month) > Integer.parseInt(curMonth)
|
||
|
//如果当前月份小于出生月份,说明生日还没过
|
||
|
|| (Integer.parseInt(month) == Integer.parseInt(curMonth) && Integer.parseInt(day) > Integer.parseInt(curDay))) {
|
||
|
age--;
|
||
|
}
|
||
|
return age;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 获取出生日期
|
||
|
*
|
||
|
* @param idNumber 身份证号码
|
||
|
* @return 出生日期 yyyy-mm-dd
|
||
|
*/
|
||
|
public static String getBirthday(String idNumber) {
|
||
|
if (isValid(idNumber)) return null;
|
||
|
|
||
|
String y, m, d;
|
||
|
if (idNumber.length() == 18) { //18位身份证号
|
||
|
y = idNumber.substring(6, 10);
|
||
|
m = idNumber.substring(10, 12);
|
||
|
d = idNumber.substring(12, 14);
|
||
|
} else {
|
||
|
// 身份证上的年份(15位身份证为1980年前的)
|
||
|
y = "19" + idNumber.substring(6, 8);
|
||
|
m = idNumber.substring(8, 10);
|
||
|
d = idNumber.substring(10, 12);
|
||
|
}
|
||
|
return y + "-" + m + "-" + d;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 身份证验证
|
||
|
*
|
||
|
* @param idNumber 身份证号码
|
||
|
* @return 是否有效
|
||
|
*/
|
||
|
public static boolean isValid(String idNumber) {
|
||
|
if (idNumber == null || "".equals(idNumber)) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
int len = idNumber.length();
|
||
|
if (len != 15 && len != 18) { // 校验长度只能为15或18
|
||
|
log.info("身份证位数异常");
|
||
|
return true;
|
||
|
}
|
||
|
// 校验生日
|
||
|
if (!validDate(idNumber)) {
|
||
|
log.info("生日异常");
|
||
|
return true;
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 校验生日
|
||
|
*
|
||
|
* @param idNumber 身份证号码
|
||
|
* @return 是否有效
|
||
|
*/
|
||
|
private static boolean validDate(String idNumber) {
|
||
|
try {
|
||
|
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
|
||
|
|
||
|
String birth = idNumber.length() == 15 ? "19" + idNumber.substring(6, 12) : idNumber.substring(6, 14);
|
||
|
if (new Date().compareTo(format.parse(birth)) < 0) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
Date birthDate = format.parse(birth);
|
||
|
if (!birth.equals(format.format(birthDate))) {
|
||
|
return false;
|
||
|
}
|
||
|
} catch (ParseException e) {
|
||
|
return false;
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
}
|