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

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 KingbaseGmetryDialect extends AbstractGmetryDialect {

	{
		//数据类型
		columnTypeMap.put(ColumnTypeEnum.NUMBER, "decimal(30, 4)");
		columnTypeMap.put(ColumnTypeEnum.STRING, "varchar(1000)");
		columnTypeMap.put(ColumnTypeEnum.DATETIME, "datetime");
		columnTypeMap.put(ColumnTypeEnum.BIGINT, "bigint");
		
		//函数方法
		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, "CONCAT(%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 datasource.getDbSchema();
	}
	
	@Override
	public DsTypeEnum dsType() {
		return DsTypeEnum.kingbasees;
	}
}
