package com.yswl.leader.util;
import com.yswl.common.core.util.R;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.web.multipart.MultipartFile;
import java.text.ParseException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
public class Kit {
public final int CHINA_ID_MIN_LENGTH = 15;
public final int CHINA_ID_MAX_LENGTH = 18;
public static int getAgeByIdCard(String idCard) {
int iAge = 0;
Calendar cal = Calendar.getInstance();
String year = idCard.substring(6, 10);
int iCurrYear = cal.get(Calendar.YEAR);
iAge = iCurrYear - Integer.valueOf(year);
return iAge;
}
public static String getBirthByIdCard(String idCard) {
return idCard.substring(6, 14);
}
public static Short getYearByIdCard(String idCard) {
return Short.valueOf(idCard.substring(6, 10));
}
public static Short getMonthByIdCard(String idCard) {
return Short.valueOf(idCard.substring(10, 12));
}
public static Short getDateByIdCard(String idCard) {
return Short.valueOf(idCard.substring(12, 14));
}
public static String getGenderByIdCard(String idCard) {
String sGender = "未知";
String sCardNum = idCard.substring(16, 17);
if (Integer.parseInt(sCardNum) % 2 != 0) {
sGender = "1";
} else {
sGender = "2";
}
return sGender;
}
public static String[] getNullPropertyNames(Object source) {
final BeanWrapper src = new BeanWrapperImpl(source);
java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();
Set<String> emptyNames = new HashSet<String>();
for (java.beans.PropertyDescriptor pd : pds) {
Object srcValue = src.getPropertyValue(pd.getName());
if (srcValue != null) {
emptyNames.add(pd.getName());
}
}
String[] result = new String[emptyNames.size()];
return emptyNames.toArray(result);
}
public static boolean isEmpty(Object obj) {
if (obj != null && !"".equals(obj.toString())) {
return false;
}
return true;
}
public static boolean isNotEmpty(Object obj) {
if (obj != null && !"".equals(obj.toString())) {
return true;
}
return false;
}
public static R verifyFileLegal(String[] fileType, MultipartFile file) {
if (isEmpty(file)) {
return R.fail("未获取到文件");
} else if (isNotEmpty(file)) {
String fileName = file.getResource().getFilename();
if (isNotEmpty(fileName) && isNotEmpty(fileType)) {
String[] fileNames = fileName.split("\\.");
if (isNotEmpty(fileNames) && fileNames.length > 0) {
if (!Arrays.asList(fileType).contains(fileNames[1])) {
return R.fail("文件类型不匹配");
}
}
}
}
return R.ok();
}
public static LocalDateTime getStringToLocalDateTime(String timeString) {
if (isNotEmpty(timeString)) {
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
return LocalDateTime.parse(timeString, df);
}
return null;
}
private static String[] parsePatterns = {"yyyy-MM-dd", "yyyy年MM月dd日",
"yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy/MM/dd",
"yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyyMMdd",
"yyyy.MM.dd", "yyyy.MM.dd HH:mm", "yyyy.MM.dd HH:mm:ss"};
public static boolean verifyDate(String str) {
boolean convertSuccess = true;
try {
DateUtils.parseDate(str, parsePatterns);
} catch (ParseException e) {
convertSuccess = false;
}
return convertSuccess;
}
public static List<String> subList2(List<String> list1, List<String> list2) {
Map<String, String> tempMap = list2.parallelStream().collect(Collectors.toMap(Function.identity(), Function.identity(), (oldData, newData) -> newData));
return list1.parallelStream().filter(str -> {
return !tempMap.containsKey(str);
}).collect(Collectors.toList());
}
}
|