/**
 * 
 */
package org.springblade.etl.util;

import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import org.springblade.datasource.constants.CubeConstants;
import org.springblade.etl.connector.source.enums.FieldType;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;

/**
 * @author wookvn
 *
 */
public class CubeUtils {
	
	final static Pattern CHINESE_PATTERN = Pattern.compile("[\u4e00-\u9fa5]");
	//(?:--)|
	final static Pattern SQL_INJECT_PATTERN = Pattern.compile("(/\\*(?:.|[\\n\\r])*?\\*/)|(\\b(update|delete|drop|insert|alter|set|truncate|declare|exec|master|execute)\\b)", Pattern.CASE_INSENSITIVE);
	//计算字段全局变量参数匹配
	final static Pattern VIRTUAL_COLUMN_PARAM_PATTERN = Pattern.compile("\\$\\{.*?\\}");
	
	/**
	 * 智能识别Excel列的类型
	 * 
	 * @param excelColumnIndex - 第几列
	 * @param dataList         - 数据集列表(一般Excel前10行)
	 * @return
	 */
	public static FieldType resolveDataType(Integer excelColumnIndex, List<Map<Integer, String>> dataList) {
		// 迭代
		int integerIndex = 0, datetimeIndex = 0, mobileIndex = 0, floatIndex = 0;
		for (Map<Integer, String> dataMap : dataList) {
			String dataValue = dataMap.get(excelColumnIndex);
			if(StrUtil.isBlank(dataValue) || _isInteger(dataValue.trim())) {
				integerIndex++;
			}else if(StrUtil.isBlank(dataValue) || _isFloatPoint(dataValue.trim())){
				floatIndex++;
			}
//			if(StrUtil.isBlank(dataValue) || _isDateTime(dataValue.trim())) {
//				datetimeIndex++;
//			}
			if(StrUtil.isBlank(dataValue) || _isMobile(dataValue.trim())) {
				mobileIndex++;
			}
		}
		if(mobileIndex == dataList.size()) {
			return FieldType.STRING;
		}else if(integerIndex == dataList.size()) {
			return FieldType.INT;
		}else if(floatIndex == dataList.size()) {
			return FieldType.DOUBLE;
		}else if(datetimeIndex == dataList.size()) {
			return FieldType.TIMESTAMP;
		}
		return FieldType.STRING;
	}
	/**
	 * 是否包含中文字符串
	 * @param str
	 * @return
	 */
	public static boolean isContainsChinese(String str) {
		return CHINESE_PATTERN.matcher(str).find();
	}
	
	/**
	 * 校验是否Sql注入
	 * @param sql
	 * @return
	 */
	public static boolean checkSqlInject(String sql) {
		return SQL_INJECT_PATTERN.matcher(sql).find();
	}
	
	/**
	 * 是否数字 支持格式:[1-带空格的纯数值/2-带,千位符的纯数值/3-带%的百分数/4-货币]
	 * 
	 * @param value
	 * @return
	 */
	static boolean _isFloatPoint(String value) {
		try {
			// 货币字符串替换掉¥/$/%,以及逗号
			value = value.replaceAll(CubeConstants.NUMBER_FORAMT_PATTERNS, "");
			new Double(value);
			return value.contains(".");
		} catch (Exception e) {
		}
		return false;
	}
	
	static boolean _isInteger(String value) {
		try {
			// 货币字符串替换掉¥/$/%,以及逗号
			value = value.replaceAll(CubeConstants.NUMBER_FORAMT_PATTERNS, "");
			new Integer(value);
			//建表时，数字的最大字段长度30
			return true;
		} catch (Exception e) {
		}
		return false;
	}
	
	/**
	 * 是否日期类型
	 * 
	 * @param value
	 * @return
	 */
	static boolean _isDateTime(String value) {
		try {
			DateUtil.parse(value, CubeConstants.DATE_FORMAT_PATTERNS);
			return true;
		} catch (Exception e) {
		}
		return false;
	}
	
	/**
	 * 是否手机号
	 * @param value
	 * @return
	 */
	static boolean _isMobile(String value) {
		return value.startsWith("1") && value.length() == 11;
	}
	
}