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

import org.springblade.datasource.AbstractGmetryDialect;
import org.springblade.datasource.enums.DateFormatEnum;
import org.springblade.datasource.enums.DsTypeEnum;
import org.springblade.datasource.enums.FunctionMethodEnum;
import org.springblade.datasource.enums.ViewColumnTypeEnum;
import org.springblade.datasource.model.FileStructureDto;
import org.springframework.stereotype.Component;

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

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

	{
		//数据类型
		columnTypeMap.put(ViewColumnTypeEnum.NUMBER, "NUMBER(%s, %s)");
		columnTypeMap.put(ViewColumnTypeEnum.STRING, "VARCHAR2(%s)");
		columnTypeMap.put(ViewColumnTypeEnum.DATETIME, "DATE");
		columnTypeMap.put(ViewColumnTypeEnum.BIGINT, "NUMBER(20)");
		columnTypeMap.put(ViewColumnTypeEnum.INT, "NUMBER(10)");
		columnTypeMap.put(ViewColumnTypeEnum.LARGE_STRING, "VARCHAR2(4000)");
		
		//函数方法
		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') || '%%'");
		functionMethodMap.put(FunctionMethodEnum.EXTRACT_YEAR, "TO_CHAR(%s,'yyyy')");
		functionMethodMap.put(FunctionMethodEnum.EXTRACT_QUARTER, "TO_CHAR(%s,'YYYY\"Q\"Q')");
		functionMethodMap.put(FunctionMethodEnum.EXTRACT_MONTH, "TO_CHAR(%s,'yyyyMM')");
		functionMethodMap.put(FunctionMethodEnum.EXTRACT_WEEK, "TO_CHAR(%s,'yyyy-WW')");
		functionMethodMap.put(FunctionMethodEnum.EXTRACT_DAY, "TO_CHAR(%s,'yyyyMMdd')");
		functionMethodMap.put(FunctionMethodEnum.EXTRACT_HOUR, "TO_CHAR(%s,'HH24:mi:ss')");
		functionMethodMap.put(FunctionMethodEnum.EXTRACT_MINUTE, "TO_CHAR(%s,'HH24:mi:ss')");
		functionMethodMap.put(FunctionMethodEnum.EXTRACT_SECOND, "TO_CHAR(%s,'HH24:mi:ss')");
		functionMethodMap.put(FunctionMethodEnum.EXTRACT_FULL, "TO_CHAR(%s,'yyyyMMdd HH24:mi:ss')");
		
		//日期格式化函数
		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<FileStructureDto> 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(f.getColumnType(), f.getColumnSize(), f.getDecimalDigits()),
					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 DsTypeEnum dsType() {
		return DsTypeEnum.oracle;
	}
	
}
