package com.yifu.cloud.plus.v1.yifu.common.core.util;

import com.yifu.cloud.plus.v1.yifu.common.core.constant.CommonConstants;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static java.util.regex.Pattern.compile;


/**
 * @author: FANG
 * @createDate: 2017年7月24日 下午4:50:23
 * @Description 常用工具类
 */
@Slf4j
public class Common {
	/**
	 * 字符串不为空且不是空串不是字符"null"
	 *
	 * @param str
	 * @return
	 * @author: fxj
	 */
	public static boolean isEmpty(String str) {
		return null == str || "".equals(str.trim()) || "null".equals(str.trim()) || "undefinded".equals(str.trim());
	}

	/**
	 * 对象null
	 *
	 * @param obj
	 * @return
	 * @author: fxj
	 */
	public static boolean isEmpty(Object obj) {
		return null == obj;
	}

	public static boolean isNotNull(Object obj) {
		return null != obj;
	}

	public static boolean isNotEmpty(List obj) {
		return (null != obj && obj.size() > 0);
	}

	public static boolean isNotNull(String obj) {
		if (null != obj && !"".equals(obj) && !"undefined".equals(obj)) {
			return true;
		}
		return false;
	}

	// 金额验证
	public static boolean isNumber(String str) {
		// 判断小数点后2位的数字的正则表达式
		Pattern pattern = compile("^((-?[1-9]{1}\\d*)|(-?[0]{1}))(\\.(\\d){0,2})?$");
		Matcher match = pattern.matcher(str);
		return match.matches() != false;
	}

	/**
	 * 允许上传的文件类型
	 *
	 * @Author pwang
	 * @Date 2020-01-07 15:52
	 * @param null
	 * @return
	 **/
	private static final String suffixList = "gif|jpg|jpeg|png|bmp|xls|xlsx|csv|pdf|docx|doc|m4a|mp3|zip|rar|txt";

	/**
	 * 判断是否为允许的上传文件类型,true表示允许
	 */
	public static boolean checkFile(String fileName) {
		// 获取文件后缀
		String suffix = getSuffix(fileName);
		if (null != suffix) {
			if (suffixList.contains(suffix.trim().toLowerCase())) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 判断是否为允许的上传文件类型,true表示允许
	 */
	public static String getSuffix(String fileName) {

		if (isEmpty(fileName) || fileName.lastIndexOf(CommonConstants.SPOT_CHAR) < 0) {
			return null;
		}
		// 获取文件后缀
		return fileName.substring(fileName.lastIndexOf(CommonConstants.SPOT_CHAR)
				+ 1, fileName.length());

	}

	/**
	 * 将字符串根据分割附转换为字符集合
	 *
	 * @param str
	 * @param regex
	 * @return
	 * @author: pwang
	 */
	public static List<String> initStrToList(String str, String regex) {
		List<String> result = new ArrayList<String>();
		try {
			if (isNotNull(str) && isNotNull(regex)) {
				String[] strTemp = str.split(regex);
				if (isNotNull(strTemp)) {
					for (String temp : strTemp) {
						if (isNotNull(temp)) {
							result.add(temp);
						}
					}
				}
			}
		} catch (Exception e) {
			log.error("字符串转LIST错误");
			return null;
		}
		return result;
	}

	/**
	 * 将集合拼成串
	 *
	 * @param <T>
	 * @param str
	 * @param regex 分隔符默认，
	 * @return
	 */
	public static <T> String ListToStr(Collection<T> str, String regex) {
		String result = "";
		try {
			if (isNotNull(str)) {
				// 默认用，分割
				if (!isNotNull(regex)) {
					regex = ",";
				}
				for (Object strtemp : str) {
					if ("".equals(result)) {
						result = String.valueOf(strtemp);
					} else {
						result = result + regex + strtemp;
					}
				}
			}
		} catch (Exception e) {
			// 数据异常
		}
		return result;
	}

	public static <T> boolean isNotNull(Collection<T> obj) {
		return null != obj && obj.size() > 0;
	}

	public static List<String> getList(String ids) {
		List<String> idList = null;
		if (Common.isNotNull(ids)) {
			idList = Common.initStrToList(ids, CommonConstants.COMMA_STRING);
		}
		return idList;
	}

	/**
	 * 对象转string
	 *
	 * @param param
	 * @return
	 */
	public static String getStringValByObject(Object param) {
		if (null == param) {
			return null;
		} else if (param instanceof Integer) {
			return Integer.toString(((Integer) param).intValue());
		} else if (param instanceof String) {
			return (String) param;
		} else if (param instanceof Double) {
			return Double.toString(((Double) param).doubleValue());
		} else if (param instanceof Float) {
			return Float.toString(((Float) param).floatValue());
		} else if (param instanceof Long) {
			return Long.toString(((Long) param).longValue());
		} else if (param instanceof Boolean) {
			return Boolean.toString(((Boolean) param).booleanValue());
		} else if (param instanceof Date) {
			return DateUtil.formatDate((Date) param);
		} else {
			return param.toString();
		}
	}

	/**
	 * 根据属性名获取属性值
	 *
	 * @param fieldName
	 * @param o
	 * @return
	 */
	public static Object getFieldValueByName(String fieldName, Object o) {
		try {
			String firstLetter = fieldName.substring(0, 1).toUpperCase();
			String getter = "get" + firstLetter + fieldName.substring(1);
			Method method = o.getClass().getMethod(getter);
			Object value = method.invoke(o);
			return value;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

}
