/**
 * 
 */
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 DamengGmetryDialect 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, "DATE_FORMAT(%s, '%s')");
		functionMethodMap.put(FunctionMethodEnum.STR_TO_DATE, "CONVERT(datetime, '%s')");
		functionMethodMap.put(FunctionMethodEnum.NUMBER_TO_DATE, "CONVERT(datetime, CONCAT(%s, ''))");
		functionMethodMap.put(FunctionMethodEnum.NUMBER_JOINT_PERCENT, "CONCAT(%s, '%%')");
		
		//日期格式化函数
		dateFormatMap.put(DateFormatEnum.yyyy, "%Y");
		dateFormatMap.put(DateFormatEnum.yyyyMM, "%Y%m");
		dateFormatMap.put(DateFormatEnum.yyyy_MM, "%Y-%m");
		dateFormatMap.put(DateFormatEnum.yyyy$MM, "%Y/%m");
		dateFormatMap.put(DateFormatEnum.yyyyMMdd, "%Y%m%d");
		dateFormatMap.put(DateFormatEnum.yyyy_MM_dd, "%Y-%m-%d");
		dateFormatMap.put(DateFormatEnum.yyyy$MM$dd, "%Y/%m/%d");
		dateFormatMap.put(DateFormatEnum.yyyyMMddhh$mm$ss,"%Y%m%d %H:%i:%S");
		dateFormatMap.put(DateFormatEnum.yyyy_MM_ddhh$mm$ss,"%Y-%m-%d %H:%i:%S");
		dateFormatMap.put(DateFormatEnum.yyyy$MM$ddhh$mm$ss,"%Y/%m/%d %H:%i:%S");
		dateFormatMap.put(DateFormatEnum.hh,"%H");
		dateFormatMap.put(DateFormatEnum.hh$mm,"%H:%i");
		dateFormatMap.put(DateFormatEnum.hh$mm$ss,"%H:%i:%S");
	}
	
	@Override
	public List<String> buildCreateTableSql(String tableName, String tableComment, List<FileStructure> structureList) {
		String tableCommentSql = "COMMENT ON table %s IS '%s'";
		List<String> sqlList = new LinkedList<String>();
		StringBuilder fieldSql = new StringBuilder();
		structureList.stream().forEach(f ->{
			fieldSql.append(String.format("%s %s %s comment '%s',", 
					f.getColumnName(), getColumnType(ColumnTypeEnum.valueOf(f.getColumnType().name())), 
					f.isPrimaryKey() ? "primary key" : "", f.getComment()));
		});
		fieldSql.deleteCharAt(fieldSql.length() - 1);
		//创建表语句【不带表注释】
		sqlList.add(String.format("create table %s (%s)", tableName, fieldSql.toString()));
		//表注释语句
		sqlList.add(String.format(tableCommentSql, 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.dameng;
	}
}
