/**
 * 
 */
package org.springblade.datasource.constants;

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

import org.springblade.core.secure.BladeUser;
import org.springblade.datasource.enums.DateFormatEnum;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.date.DateUtil;

/**
 * @author wookvn
 *
 */
public class CubeConstants {
	
	public final static int DEFAULT_PREVIEW_DATA_SIZE = 100;
	
	public final static String DEFAULT_SHOW_CHAR = "'-'";

	public final static String COLUMN_NAME_PREFIX = "COL_";
	
	public final static String COLUMN_ALIAS_NAME_PREFIX = "COL_ALIAS_";
	
	public final static String COLUMN_USER_ID = "JHBI_USER_ID";
	
	public final static String COLUMN_PRIMARY_KEY = "ID";
	
	public final static String[] SUPPORTS_FILE = {"xls", "xlsx", "csv", "txt"};
	
	public final static int DEFAULT_READ_EXCEL_SIZE = 100;
	
	public final static String INSERT_INTO_SQL_FORMAT = "insert into %s %s values %s";
	
	public final static String SELECT_SQL_FORMAT = "select %s from %s where %s %s";
	
	public final static Pattern COLUMN_FORMULA_PATTERN = Pattern.compile("\\[.+?\\]");
	
	public final static String[] AGGREGATION_FUNCTIONS = {"sum(", "count(", "min(", "max(", "avg(", "stddev(", "median("};
	
	/**
	 * 支持数字格式
	 */
	public static String NUMBER_FORAMT_PATTERNS = "￥|¥|$|%|,|\\s*";
	
	/**
	 * 支持的日期格式
	 */
	public static String[] DATE_FORMAT_PATTERNS = {
            "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM", 
            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
            "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM",
            "yyyy年MM月dd日", "yyyy年MM月dd日 HH点mm分ss秒", "yyyy年MM月dd日 HH点mm分", "yyyy年MM月",
            "MM-dd", "MM/dd", "MM月dd日"};
	
	/**
	 * 创建或更新视图语句
	 */
	public final static String CREATE_REPLACE_VIEW_SQL_FORMAT = "create or replace view %s AS %s";
	
	/**
	 * 数据集全局参数变量
	 */
	public final static String CUBE_GLOBAL_SYS_USER_PARAM_KEY = "jhbiSysUser";
	public final static String CUBE_GLOBAL_SYS_CURRENT_TIME_PARAM_KEY = "jhbiSysCurrentTime";
	
	public static Map<String, List<Object>> mergeSysUserParams(BladeUser bladeUser, Map<String, List<Object>> extraParams){
		if(bladeUser != null) {
			extraParams.put(CUBE_GLOBAL_SYS_USER_PARAM_KEY, ListUtil.toList(bladeUser.getUserId()));
		}
		return extraParams;
	}
	
	public static Map<String, List<Object>> mergeSysCurrentTimeParams(DateFormatEnum dateFormat, Map<String, List<Object>> extraParams){
		if(dateFormat != null) {
			extraParams.put(CUBE_GLOBAL_SYS_CURRENT_TIME_PARAM_KEY, ListUtil.toList(DateUtil.format(LocalDateTime.now(), dateFormat.getFormat())));
		}
		return extraParams;
	}
	
	public static Map<String, List<Object>> mergeAllSysParams(BladeUser bladeUser, DateFormatEnum dateFormat, Map<String, List<Object>> extraParams){
		return mergeSysCurrentTimeParams(dateFormat, mergeSysUserParams(bladeUser, extraParams));
	}
}
