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

import com.yifu.cloud.plus.v1.yifu.common.core.constant.CommonConstants;
import com.yifu.cloud.plus.v1.yifu.common.core.constant.ExcelAttribute;
import com.yifu.cloud.plus.v1.yifu.common.core.constant.ValidityConstants;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * @Author fxj
 * @Date 2022/6/23
 * @Description
 * @Version 1.0
 */
@Data
@Slf4j
public class ExcelUtil <T> implements Serializable {
	private Class<T> clazz;
	/**
	 * 用于返回错误信息
	 */
	private List<ErrorMessage> errorInfo;

	private HashMap<Integer, ErrorMessage> errorMessageHashMap;

	public ExcelUtil(Class<T> clazz) {
		this.clazz = clazz;
		this.fields =clazz.getDeclaredFields();
	}
	private Field[] fields;

	public void convertEntity(T vo, String[] exportFields, Map<String,String> diyDicMap, String dateFormat) {
		String provinceTemp;
		String cityTemp;
		// 得到所有定义字段
		Field[] allFields = clazz.getDeclaredFields();
		List<Field> fields = new ArrayList<>();
		Class<?> superClazz = clazz.getSuperclass();
		// 将需要字典表头存起来备用，如果没传，则全部翻译
		List<String> allField = new ArrayList<>();
		ExcelAttribute annotation;
		for (Field field : allFields) {
			if (field.isAnnotationPresent(ExcelAttribute.class)) {
				fields.add(field);
				annotation = field.getAnnotation(ExcelAttribute.class);
				if (annotation != null && Common.isNotNull(annotation.name())) {
					allField.add(annotation.name());
				}
			}
		}
		if (Common.isEmpty(exportFields) && !allField.isEmpty()) {
			exportFields = new String[allField.size()];
			for (int i = 0; i < allField.size(); i++) {
				exportFields[i] = allField.get(i);
			}
		}
		if (superClazz != null) {
			Field[] superFields = superClazz.getDeclaredFields();
			for (Field field : superFields) {
				if (field.isAnnotationPresent(ExcelAttribute.class)) {
					fields.add(field);
				}
			}
		}

		fields = getFiledsByParamFiled(fields, exportFields);
		ExcelAttribute attr;
		Field field;
		//分割符
		String divider;
		for (int j = 0; j < fields.size(); j++) {
			// 获得field
			field = fields.get(j);
			// 设置实体类私有属性可访问
			field.setAccessible(true);
			attr = field.getAnnotation(ExcelAttribute.class);
			if (attr.isExport()) {
				// 如果数据存在就填入,不存在填入空格
				Class<?> classType = field.getType();
				String value = null;
				Date date;
				try {
					if (field.get(vo) != null && (classType.isAssignableFrom(Date.class) || classType.isAssignableFrom(LocalDateTime.class) || classType.isAssignableFrom(LocalDate.class))) {
						if (Common.isNotNull(attr.dateFormatExport())) {
							date = DateUtil.stringToDate2(String.valueOf(field.get(vo)), attr.dateFormatExport());
							if (classType.isAssignableFrom(LocalDateTime.class)){
								value = LocalDateTimeUtils.formatTime((LocalDateTime)field.get(vo),attr.dateFormatExport());
							}else {
								value = DateUtil.dateToString(date, attr.dateFormatExport());
							}
						} else {
							date = DateUtil.stringToDate2(String.valueOf(field.get(vo)), !Common.isNotNull(dateFormat) ? DateUtil.ISO_EXPANDED_DATE_FORMAT : dateFormat);
							if (classType.isAssignableFrom(LocalDateTime.class)){
								value = LocalDateTimeUtils.formatTime((LocalDateTime)field.get(vo),!Common.isNotNull(dateFormat) ? DateUtil.ISO_EXPANDED_DATE_FORMAT : dateFormat);
							}else {
								value = cn.hutool.core.date.DateUtil.format(date, !Common.isNotNull(dateFormat) ? DateUtil.ISO_EXPANDED_DATE_FORMAT : dateFormat);
							}
						}
					}
					//如果有默认字典数据直接取值
					if (Common.isNotNull(attr.readConverterExp())) {
						value = convertByExp(field.get(vo) == null ? "" : String.valueOf(field.get(vo)), attr.readConverterExp());
					} else if (attr.isDataId()) {
						//加入分割符逻辑
						divider = attr.divider();
						if (!"".equals(divider) && field.get(vo) != null){
							try {
								value = "";
								for(String key : String.valueOf(field.get(vo)).split(divider)){
									value += getLableById(attr, diyDicMap, key) + divider;
								}
								if(Common.isNotNull(value)){
									value = value.substring(0,value.length()-1);
								}
							}catch (Exception e){
								log.error("excel数据导出字符切割错误",e);
								value = "字符切割错误";
							}
						} else {
							//如果是字典数据ID或区域ID 去dicMap（包含了区域） 取值 取不到返回空
							value = getLableById(attr, diyDicMap, field.get(vo) == null ? "" : String.valueOf(field.get(vo)));
						}
					} else if (attr.isArea()){
						//区域字段处理 TODO
						value = getAreaLabel("area_"+field.get(vo));
					}
					field.set(vo, Common.isNotNull(value)?value:field.get(vo));
				}catch (Exception e){
					log.error("字典数据解析异常");
				}
			}
		}
	}
	/**
	 * 通过标志_字典ID的方式去dicMap 取值
	 *
	 * @param attr
	 * @param dicMap 字典值  key: 类型标识_ID  value：实际要导出的内容
	 * @param value  对应字段的ID值
	 * @return
	 * @Author fxj
	 * @Date 2019-08-06
	 **/
	private String getLableById(ExcelAttribute attr, Map<String, String> dicMap, String value) {
		String tempValue = null;
		if (null != dicMap && dicMap.size() > 0 && Common.isNotNull(value)) {
			if (Common.isNotNull(attr.dataType())) {
				tempValue =  dicMap.get(attr.dataType() + CommonConstants.DOWN_LINE_STRING + value);
				if (Common.isEmpty(tempValue)){
					tempValue  = dicMap.get(attr.dataType() + CommonConstants.DOWN_LINE_STRING +"id"+CommonConstants.DOWN_LINE_STRING +  value);
				}
			} else {
				tempValue = dicMap.get(value);
			}
		}
		// 默认字典获取不到 去 缓存获取
		if (Common.isEmpty(tempValue)){
			Map<String,String> dicObj = (Map<String, String>) RedisUtil.redis.opsForValue().get(attr.dataType());
			boolean flag = true;
			for (Map.Entry<String,String> entry:dicObj.entrySet()){
				if (Common.isNotNull(entry.getKey()) && entry.getKey().equals(value.trim())){
					tempValue = entry.getValue();
					break;
				}
			}
		}
		return tempValue;
	}
	/**
	 * 返回传参对应的fileds
	 *
	 * @param fields
	 * @param paramFields
	 * @return
	 * @Author fxj
	 * @Date 2019-08-05
	 **/
	private List<Field> getFiledsByParamFiled(List<Field> fields, String[] paramFields) {
		List<Field> fieldsTemp = new ArrayList<Field>();
		List<String> attrName = new ArrayList();//存中文名 wangan修改
		if (null != paramFields && paramFields.length > 0) {
			ExcelAttribute attr;
			Field field;
			for (int j = 0; j < paramFields.length; j++) {
				for (int i = 0; i < fields.size(); i++) {
					field = fields.get(i);
					attr = field.getAnnotation(ExcelAttribute.class);
					//如果有同样的中文名数据字段，后面不再添加。  wangan修改
					if (!attrName.contains(attr.name())) {
						if (attr.name().equals(paramFields[j])) {
							fieldsTemp.add(field);
							attrName.add(attr.name());
							break;
						}
					}
				}
			}
		} else {
			return fields;
		}
		return fieldsTemp;
	}
	public ErrorMessage checkEntity(T data , Integer rowNum) {
		if (!Common.isNotNull(fields)){
			return null;
		}
		//存储错误数据
		Class<?> fieldType;
		String error = "";
		String c = null;
		//临时存储单元格数据
		String tempStr = "";
		ErrorMessage errorTemp = null;
		errorMessageHashMap = new HashMap<>();
		Map<String,Field> mapArea = new HashMap<>();
		// 参数校验
		ExcelAttribute attr = null;
		try{

			for (Field field : fields) {
				if (field == null) {
					continue;
				}
				tempStr=null;
				// 设置实体类私有属性可访问
				field.setAccessible(true);
				attr = field.getAnnotation(ExcelAttribute.class);
				if (Common.isEmpty(attr)){
					continue;
				}
				// 单元格中的内容.
				c = getStringValByObject(field.get(data));
				if (c != null) {
					c = c.trim();
				}
				//校验字段是否符合要求 返回错误信息
				error = validateUtil(c, attr, rowNum);
				if (null != error) {
					errorMessageHashMap = initErrorMessage(errorMessageHashMap, new ErrorMessage(rowNum, error), errorTemp);
					continue;
				}
				//单元格数据为空不需要处理了
				if (!StringUtils.isNotBlank(c)) {
					continue;
				}
				//如果是需要从字典表取值数据的话在这里处理即可

				if (Common.isNotNull(attr.readConverterExp())) {
					 tempStr= reverseByExp( c, attr.readConverterExp());
					if (Common.isEmpty(tempStr)){
						errorMessageHashMap = initErrorMessage(errorMessageHashMap, new ErrorMessage(rowNum, "未找到:" + c + "的字典数据"), errorTemp);
						continue;
					}else {
						c = tempStr;
					}
				} else if (attr.isDataId()) {
					if (Common.isNotNull(attr.dataType())) {
						//非区域字段处理 TODO
						tempStr = getDicValue(c, tempStr, attr,rowNum,errorTemp);
					/*if (Common.isNotNull(attr.parentField())){
						tempStr = dicMap.get(attr.dataType() + "_" + c.trim() + "_" + getFieldValueByName(attr.parentField(), entity, DateUtil.ISO_EXPANDED_DATE_FORMAT));
					}else {
						tempStr = dicMap.get(attr.dataType() + "_" + c.trim());
					}*/

					}
				}else {
					if (attr.isArea()) {
						//区域字段处理 TODO
						if (!Common.isNotNull(attr.parentField())) {
							tempStr = getAreaValue("area_"+c.trim(), tempStr, attr,rowNum,errorTemp);
						} else {
							// TODO
							tempStr = getAreaValue("area_"+c.trim()+ "_" + getFieldValueByName(attr.parentField(), data, DateUtil.ISO_EXPANDED_DATE_FORMAT), tempStr, attr,rowNum,errorTemp);
						}
					}
				}
				if (StringUtils.isEmpty(tempStr) && (attr.isArea() || attr.isDataId())) {
					if (attr.isOrgan()) {
						errorMessageHashMap = initErrorMessage(errorMessageHashMap, new ErrorMessage(rowNum, "未找到:" + c + "的单位或结算主体数据，请确认存在结算主体权限"), errorTemp);
					}
					if (attr.isArea()) {
						errorMessageHashMap = initErrorMessage(errorMessageHashMap, new ErrorMessage(rowNum, "未找到:" + c + "的区域数据"), errorTemp);
					} else {
						if (Common.isNotNull(attr.errorInfoImport())) {
							errorMessageHashMap = initErrorMessage(errorMessageHashMap, new ErrorMessage(rowNum, "数据'" + c + "':" + attr.errorInfoImport()), errorTemp);
						} else {
							errorMessageHashMap = initErrorMessage(errorMessageHashMap, new ErrorMessage(rowNum, "未找到:" + c + "的字典数据"), errorTemp);
						}
					}
					continue;
				}
				if (Common.isNotNull(tempStr)){
					c = tempStr;
					mapArea.put(tempStr,field);
				}
				// 取得类型,并根据对象类型设置值.
				fieldType = field.getType();
				if (fieldType == null) {
					continue;
				}
				if (attr.isArea()){
					// 区域字段单独处理
					continue;
				}else if (String.class == fieldType) {
					field.set(data, c);
				} else if (BigDecimal.class == fieldType) {
					c = c.indexOf('%') != -1 ? c.replace("%", "") : c;
					field.set(data, BigDecimal.valueOf(Double.valueOf(c)));
				} else if (Date.class == fieldType && Common.isNotNull(c.trim())) {
					field.set(data, DateUtil.parseDate(c.trim(), Common.isEmpty(attr.dateFormat()) ? DateUtil.ISO_EXPANDED_DATE_FORMAT : attr.dateFormat()));
				} else if ((Integer.TYPE == fieldType) || (Integer.class == fieldType)) {
					field.set(data, Integer.parseInt(c));
				} else if ((Long.TYPE == fieldType) || (Long.class == fieldType)) {
					field.set(data, Long.valueOf(c));
				} else if ((Float.TYPE == fieldType) || (Float.class == fieldType)) {
					field.set(data, Float.valueOf(c));
				} else if ((Short.TYPE == fieldType) || (Short.class == fieldType)) {
					field.set(data, Short.valueOf(c));
				} else if ((Double.TYPE == fieldType) || (Double.class == fieldType)) {
					field.set(data, Double.valueOf(c));
				} else if (Character.TYPE == fieldType) {
					if ((c != null) && (c.length() > 0)) {
						field.set(data, Character.valueOf(c.charAt(0)));
					}
				} else if (LocalDateTime.class == fieldType && Common.isNotNull(c.trim())) {
					String dc = c.trim().replace(CommonConstants.CENTER_SPLIT_LINE_STRING, "")
							.replace(CommonConstants.SLASH_SPLIT_LINE_STRING, "");
					try {
						if (dc.length() > CommonConstants.EIGHT_INT) {
							field.set(data, LocalDateTimeUtils.convertDateToLDT(DateUtil.stringToDate(dc, DateUtil.DATETIME_PATTERN_CONTAINS)));
						}else {
							field.set(data, LocalDateTimeUtils.convertDateToLDT(DateUtil.stringToDate(dc, DateUtil.ISO_DATE_FORMAT)));
						}
					} catch (Exception e) {
						if (dc.length() > CommonConstants.EIGHT_INT) {
							errorMessageHashMap = initErrorMessage(errorMessageHashMap,
									new ErrorMessage(rowNum, attr.name() + "：" +
											c.trim() + "应满足时间格式：" + DateUtil.DATETIME_PATTERN_SECOND), errorTemp);
							continue;
						} else {
							errorMessageHashMap = initErrorMessage(errorMessageHashMap,
									new ErrorMessage(rowNum, attr.name() + "：" +
											c.trim() + "应满足时间格式：" + DateUtil.ISO_DATE_FORMAT), errorTemp);
							continue;
						}
					}
				} else if (LocalDate.class == fieldType && Common.isNotNull(c.trim())) {
					try {
						field.set(data, LocalDate.parse(c.trim().replace(CommonConstants.CENTER_SPLIT_LINE_STRING, ""), DateTimeFormatter.ofPattern(DateUtil.ISO_DATE_FORMAT)));

					} catch (Exception e) {
						errorMessageHashMap = initErrorMessage(errorMessageHashMap, new ErrorMessage(rowNum, attr.name() + "：" + c.trim() + "应满足时间格式：" + DateUtil.ISO_DATE_FORMAT), errorTemp);
						continue;
					}
				}
			}
			if (Common.isNotNull(mapArea)){
				for (Map.Entry<String, Field> entry: mapArea.entrySet()){
					entry.getValue().set(data,entry.getKey());
				}
			}
		}catch (Exception e){
			errorMessageHashMap = initErrorMessage(errorMessageHashMap, new ErrorMessage(rowNum, attr.name() + "：" + c.trim() + "校验异常，请联系管理人员"), errorTemp);
		}finally {

		}
		return errorMessageHashMap.get(rowNum);
	}
	/**
	 * 例如：解析值 0=男,1=女,2=未知  传参 男解析为 0
	 *
	 * @param propertyValue 参数值
	 * @param converterExp  翻译注解
	 * @return 解析后值
	 * @throws Exception
	 */
	public static String reverseByExp(String propertyValue, String converterExp) throws Exception {
		try {
			String[] convertSource = converterExp.split(",");
			for (String item : convertSource) {
				String[] itemArray = item.split("=");
				if (itemArray[1].equals(propertyValue)) {
					return itemArray[0];
				}
			}
		} catch (Exception e) {
			throw e;
		}
		return null;
	}

	/**
	 * 例如：解析值 0=男,1=女,2=未知  传参 0解析为 男
	 *
	 * @param propertyValue 参数值
	 * @param converterExp  翻译注解
	 * @return 解析后值
	 * @throws Exception
	 */
	public static String convertByExp(String propertyValue, String converterExp) throws Exception {
		try {
			String[] convertSource = converterExp.split(",");
			if(propertyValue.contains(",")){
				String[] valueSource = propertyValue.split(",");
				propertyValue = "";
				for(String value:valueSource){
					for (String item : convertSource) {
						String[] itemArray = item.split("=");
						if (itemArray[0].equals(value)) {
							propertyValue += itemArray[1] + ",";
							break;
						}
					}
				}
				if(propertyValue.endsWith(",")){
					propertyValue = propertyValue.substring(CommonConstants.dingleDigitIntArray[0],propertyValue.length()-1);
				}
			}else{
				for (String item : convertSource) {
					String[] itemArray = item.split("=");
					if (itemArray[0].equals(propertyValue)) {
						return itemArray[1];
					}
				}
			}
		} catch (Exception e) {
			throw e;
		}
		return propertyValue;
	}
	private String getAreaLabel(String c) {
		return (String)RedisUtil.redis.opsForValue().get(c);
	}

	private String getAreaValue(String c, String tempStr, ExcelAttribute attr,Integer rowNum, ErrorMessage errorTemp) {
		tempStr =  String.valueOf(RedisUtil.redis.opsForValue().get(c));
		if (Common.isEmpty(tempStr)){
			errorMessageHashMap = initErrorMessage(errorMessageHashMap, new ErrorMessage(rowNum, attr.name() + "：" + c.trim() + "校验异常，请联系管理人员"), errorTemp);
			return CommonConstants.EMPTY_STRING;
		}
		return tempStr;
	}
	private String getDicValue(String c, String tempStr, ExcelAttribute attr,Integer rowNum, ErrorMessage errorTemp) {
		Map<String,String> dicObj = (Map<String, String>) RedisUtil.redis.opsForValue().get(attr.dataType());
		boolean flag = true;
		for (Map.Entry<String,String> entry:dicObj.entrySet()){
			if (Common.isNotNull(entry.getValue()) && entry.getValue().equals(c.trim())){
				tempStr = entry.getKey();
				flag = false;
				break;
			}
		}
		if (flag){
			errorMessageHashMap = initErrorMessage(errorMessageHashMap, new ErrorMessage(rowNum, attr.name() + "：" + c.trim() + "校验异常，请联系管理人员"), errorTemp);
		return CommonConstants.EMPTY_STRING;
		}
		return tempStr;
	}

	/**
	 * @param fieldName 字段名
	 * @param o         对象
	 * @return 字段值
	 * @MethodName : getFieldValueByName
	 * @Description : 根据字段名获取字段值
	 */
	public static Object getFieldValueByName(String fieldName, Object o, String dateFormat) {

		Object value = null;
		Field field = null;
		if (o == null) {
			return "";
		}
		field = getFieldByName(fieldName, o.getClass());
		String type = getFieldType(fieldName, o.getClass());
		if (field != null) {
			field.setAccessible(true);
			try {
				if (null != type && type.equals("Date")) {
					Date date = (Date) field.get(o);
					return DateUtil.dateToString(date, dateFormat == null ? "yyyy-MM-dd" : dateFormat);
				} else {
					value = field.get(o);
				}
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		} else {
			log.info(o.getClass().getSimpleName() + "类不存在字段名 " + fieldName);
		}

		return value;
	}

	/**
	 * @param fieldName 字段名
	 * @param clazz     包含该字段的类
	 * @return 字段
	 * @MethodName : getFieldByName
	 * @Description : 根据字段名获取字段
	 */
	private static Field getFieldByName(String fieldName, Class<?> clazz) {
		// 拿到本类的所有字段
		Field[] selfFields = clazz.getDeclaredFields();

		// 如果本类中存在该字段，则返回
		for (Field field : selfFields) {
			if (field.getName().equals(fieldName)) {
				return field;
			}
		}
		// 否则，查看父类中是否存在此字段，如果有则返回
		Class<?> superClazz = clazz.getSuperclass();
		if (superClazz != null && superClazz != Object.class) {
			return getFieldByName(fieldName, superClazz);
		}

		// 如果本类和父类都没有，则返回空
		return null;
	}
	/**
	 * @param fieldName 字段名
	 * @param clazz     包含该字段的类
	 * @return 字段
	 * @MethodName : getFieldType
	 * @Description : 根据字段名获取字段类型
	 */
	private static String getFieldType(String fieldName, Class<?> clazz) {
		// 拿到本类的所有字段
		Field[] selfFields = clazz.getDeclaredFields();

		// 如果本类中存在该字段，则返回
		for (Field field : selfFields) {
			if (field.getName().equals(fieldName)) {
				//如果type是类类型，则前面包含"class "，后面跟类名
				String type = field.getGenericType().toString();
				if (type.equals("class java.lang.String")) {
					return "String";
				} else if (type.equals("class java.lang.Integer")) {
					return "Integer";
				} else if (type.equals("class java.lang.Short")) {
					return "Short";
				} else if (type.equals("class java.lang.Double")) {
					return "Double";
				} else if (type.equals("class java.lang.Boolean")) {
					return "Boolean";
				} else if (type.equals("class java.util.Date")) {
					return "Date";
				} else {
					return null;
				}
			}
		}
		// 否则，查看父类中是否存在此字段，如果有则返回
		Class<?> superClazz = clazz.getSuperclass();
		if (superClazz != null && superClazz != Object.class) {
			return getFieldType(fieldName, superClazz);
		}
		// 如果本类和父类都没有，则返回空
		return null;
	}
	/**
	 * 输出错误数据
	 *
	 * @param attr
	 * @param msg
	 * @param i
	 * @return
	 */
	private String errorInfo(ExcelAttribute attr, String msg, Integer i) {
		return (Common.isEmpty(msg) ? (Common.isEmpty(attr.errorInfo()) ? "" : attr.errorInfo()) : (attr.name() + msg));
	}
	/**
	 * 正则匹配返回
	 *
	 * @param c
	 * @param attr
	 * @param pattern
	 * @return
	 * @Author fxj
	 * @Date 2019-08-06
	 **/
	private boolean getMatchRes(String c, ExcelAttribute attr, String pattern) {
		return !c.matches(Common.isEmpty(attr.pattern()) ? pattern : attr.pattern());
	}
	/**
	 * 实现各种校验方式的错误返回新
	 *
	 * @param c
	 * @param attr
	 * @param i
	 * @return
	 */
	private String validateUtil(String c, ExcelAttribute attr, int i) {
		//非空校验
		if (attr.isNotEmpty() && !Common.isNotNull(c)) {
			return errorInfo(attr, "_字段不可为空", i);
		}
		if (Common.isNotNull(c)) {
			//日期格式校验
			if (attr.isDate()) {
				DateUtil.stringToDate(c.trim(), attr.dateFormat());
			}
			//手机号码校验
			if (attr.isPhone() && getMatchRes(c.trim(), attr, ValidityConstants.MOBILE_PATTERN)) {
				return errorInfo(attr, "_手机号码有误", i);
			}
			//身份证校验
			if (attr.isIdCard() && getMatchRes(c.trim(), attr, ValidityConstants.IDCARD_PATTERN)) {
				return errorInfo(attr, "_身份证格式有误", i);
			}
			//邮箱验证
			if (attr.isEmail() && getMatchRes(c.trim(), attr, ValidityConstants.EMAIL_PATTERN)) {
				return errorInfo(attr, "_邮箱格式有误", i);
			}
			//integer 验证
			if (attr.isInteger() && getMatchRes(c.trim(), attr, ValidityConstants.INTEGER_PATTERN)) {
				return errorInfo(attr, "_整数格式有误", i);
			}
			//float、double 验证
			if (attr.isFloat() && getMatchRes(c.trim(), attr, ValidityConstants.FLOAT_PATTERN)) {
				return errorInfo(attr, "_数字格式有误", i);
			}
			if (attr.isDouble() && getMatchRes(c.trim(), attr, ValidityConstants.FLOAT_PATTERN)) {
				return errorInfo(attr, "_数字格式有误", i);
			}
			//最大长度校验
			if (attr.maxLength() > 0 && c.length() > attr.maxLength()) {
				return errorInfo(attr, "_超出最大长度", i);
			}
			//最大值校验
			if (attr.max() > 0) {
				if (Common.isNumber(c)) {
					if (attr.isDouble()){
						if (Double.doubleToLongBits(Double.valueOf(c).doubleValue()) > Double.doubleToLongBits(attr.max())) {
							return errorInfo(attr, "_超出最大值", i);
						}
					}else{
						if (Float.valueOf(c).intValue() > attr.max()) {
							return errorInfo(attr, "_超出最大值", i);
						}
					}
				} else {
					return errorInfo(attr, "_必须为数字且最多两位小数", i);
				}
			}
			//最小值校验
			if (attr.min() > 0) {
				if (Common.isNumber(c)) {
					if (attr.isFloat()){
						if (Float.valueOf(c).compareTo(attr.max()) > CommonConstants.ZERO_INT) {
							return errorInfo(attr, "_超出最大值", i);
						}
					}else{
						if (Float.valueOf(c).intValue() < attr.min()) {
							return errorInfo(attr, "_小于最小值", i);
						}
					}
				} else {
					return errorInfo(attr, "_必须为数字且最多两位小数", i);
				}
			}
		}
		return null;
	}

	/**
	 * 根据属性名获取属性值
	 *
	 * @param fieldName
	 * @param hashMap
	 * @return
	 */
	private static Object getFieldValueByName(String fieldName, HashMap hashMap) {
		try {
			return hashMap.get(fieldName);
		} catch (Exception e) {
			return null;
		}
	}
	/**
	 * 按类型统一返回String内容
	 *
	 * @param param
	 * @return
	 */
	private 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.dateToString((Date) param);
		} else if (param instanceof LocalDateTime) {
			return LocalDateTimeUtils.formatTime((LocalDateTime) param, DateUtil.DATETIME_PATTERN_SECOND);
		} else {
			return param.toString();
		}
	}

	private HashMap<Integer, ErrorMessage> initErrorMessage(HashMap<Integer, ErrorMessage> errorMessageHashMap, ErrorMessage errorMessage, ErrorMessage temp) {
		if (null != errorMessageHashMap && null != errorMessage) {
			temp = errorMessageHashMap.get(errorMessage.getLineNum());
			if (null != temp) {
				temp.setMessage(temp.getMessage() + CommonConstants.DOWN_LINE_STRING + errorMessage.getMessage());
				errorMessageHashMap.put(temp.getLineNum(), temp);
			} else {
				errorMessageHashMap.put(errorMessage.getLineNum(), errorMessage);
			}
		}
		return errorMessageHashMap;
	}
}
