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

import cn.hutool.core.util.StrUtil;
import org.springblade.datasource.AbstractGmetryDialect;
import org.springblade.datasource.enums.ColumnTypeEnum;
import org.springblade.datasource.enums.DateFormatEnum;
import org.springblade.datasource.enums.DsTypeEnum;
import org.springblade.datasource.enums.FunctionMethodEnum;
import org.springblade.etl.dto.FileStructure;
import org.springblade.etl.entity.bi.ReportDatasource;
import org.springframework.stereotype.Component;

import java.util.LinkedList;
import java.util.List;

/**
 * @author wookvn
 *
 */
@Component
public class OracleGmetryDialect extends AbstractGmetryDialect {

	{
		//数据类型
		columnTypeMap.put(ColumnTypeEnum.NUMBER, "NUMBER(30, 4)");
		columnTypeMap.put(ColumnTypeEnum.STRING, "VARCHAR2(1000)");
		columnTypeMap.put(ColumnTypeEnum.DATETIME, "DATE");
		columnTypeMap.put(ColumnTypeEnum.BIGINT, "NUMBER");
		
		//函数方法
		functionMethodMap.put(FunctionMethodEnum.DATE_TO_STR, "TO_CHAR(%s, '%s')");
		functionMethodMap.put(FunctionMethodEnum.STR_TO_DATE, "TO_DATE(%s, '%s')");
		functionMethodMap.put(FunctionMethodEnum.NUMBER_TO_DATE, "TO_DATE(TO_CHAR(%s), '%s')");
		functionMethodMap.put(FunctionMethodEnum.NUMBER_JOINT_PERCENT, "TO_CHAR(%s, '%s') || '%%'");
		
		//日期格式化函数
		dateFormatMap.put(DateFormatEnum.yyyy, "yyyy");
		dateFormatMap.put(DateFormatEnum.yyyyMM, "yyyyMM");
		dateFormatMap.put(DateFormatEnum.yyyy_MM, "yyyy-MM");
		dateFormatMap.put(DateFormatEnum.yyyy$MM, "yyyy/MM");
		dateFormatMap.put(DateFormatEnum.yyyyMMdd, "yyyyMMdd");
		dateFormatMap.put(DateFormatEnum.yyyy_MM_dd, "yyyy-MM-dd");
		dateFormatMap.put(DateFormatEnum.yyyy$MM$dd, "yyyy/MM/dd");
		dateFormatMap.put(DateFormatEnum.yyyyMMddhh$mm$ss,"yyyyMMdd HH24:mi:ss");
		dateFormatMap.put(DateFormatEnum.yyyy_MM_ddhh$mm$ss,"yyyy-MM-dd HH24:mi:ss");
		dateFormatMap.put(DateFormatEnum.yyyy$MM$ddhh$mm$ss,"yyyy/MM/dd HH24:mi:ss");
		dateFormatMap.put(DateFormatEnum.hh,"HH24");
		dateFormatMap.put(DateFormatEnum.hh$mm,"HH24:mi");
		dateFormatMap.put(DateFormatEnum.hh$mm$ss,"HH24:mi:ss");
	}
	
	@Override
	public List<String> buildCreateTableSql(String tableName, String tableComment, List<FileStructure> structureList) {
		String createColumnCommentSql = "COMMENT ON column %s.%s IS '%s'",
			   createTableCommentSql = "COMMENT ON table %s IS '%s'",
			   createTableSql = "create table %s (%s)";
		List<String> sqlList = new LinkedList<String>();
		StringBuilder fieldSql = new StringBuilder();
		structureList.stream().forEach(f ->{
			fieldSql.append(String.format("%s %s %s,", 
					f.getColumnName(), getColumnType(ColumnTypeEnum.valueOf(f.getColumnType().name())),
					f.isPrimaryKey() ? "primary key" : ""));
			//添加字段注释SQL
			sqlList.add(String.format(createColumnCommentSql, tableName, f.getColumnName(), f.getComment()));
		});
		fieldSql.deleteCharAt(fieldSql.length() - 1);
		//创建表SQL
		sqlList.add(0, String.format(createTableSql, tableName, fieldSql.toString()));
		//添加表注释SQL
		sqlList.add(1, String.format(createTableCommentSql, tableName, tableComment));
		return sqlList;
	}

	@Override
	public String dbSchema(ReportDatasource datasource) {
		return StrUtil.isBlank(datasource.getDbSchema()) ? datasource.getDsUser() : datasource.getDbSchema();
	}
	
	@Override
	public DsTypeEnum dsType() {
		return DsTypeEnum.oracle;
	}
	
	/**
	 * @TODO oracle暂时还不能开启。
	 * 因为datax的配置文件中如果字段名或者表名带有双引号，会导致配置文件有问题.
	 * 具体参考：/datax/template.json文件的内容
	 */
//	@Override
//	public String dbEscape(String name) {
//		return "\"" + name + "\"";
//	}
}
