/**
 * 
 */
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.DatasourceDto;
import org.springblade.datasource.model.FileStructureDto;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

/**
 * @author wookvn
 *
 */
@Component
public class MysqlGmetryDialect extends AbstractGmetryDialect {
	
	{
		//数据类型
		columnTypeMap.put(ViewColumnTypeEnum.NUMBER, "decimal(%s, %s)");
		columnTypeMap.put(ViewColumnTypeEnum.STRING, "varchar(%s)");
		columnTypeMap.put(ViewColumnTypeEnum.DATETIME, "datetime");
		columnTypeMap.put(ViewColumnTypeEnum.BIGINT, "bigint");
		columnTypeMap.put(ViewColumnTypeEnum.INT, "int");
		columnTypeMap.put(ViewColumnTypeEnum.LARGE_STRING, "text");

		//函数方法
		functionMethodMap.put(FunctionMethodEnum.DATE_TO_STR, "DATE_FORMAT(%s, '%s')");
		functionMethodMap.put(FunctionMethodEnum.STR_TO_DATE, "STR_TO_DATE(%s, '%s')");
		functionMethodMap.put(FunctionMethodEnum.NUMBER_TO_DATE, "STR_TO_DATE(CONCAT(%s, ''), '%s')");
		functionMethodMap.put(FunctionMethodEnum.NUMBER_JOINT_PERCENT, "CONCAT(%s, '%%')");
		functionMethodMap.put(FunctionMethodEnum.EXTRACT_YEAR, "DATE_FORMAT(%s,'%%Y')");
		functionMethodMap.put(FunctionMethodEnum.EXTRACT_QUARTER, "CONCAT(YEAR(%s),'Q',QUARTER(%s))");
		functionMethodMap.put(FunctionMethodEnum.EXTRACT_MONTH, "DATE_FORMAT(%s,'%%Y%%m')");
		functionMethodMap.put(FunctionMethodEnum.EXTRACT_WEEK, "DATE_FORMAT(%s,'%%Y-%%v')");
		functionMethodMap.put(FunctionMethodEnum.EXTRACT_DAY, "DATE_FORMAT(%s,'%%Y%%m%%d')");
		functionMethodMap.put(FunctionMethodEnum.EXTRACT_HOUR, "DATE_FORMAT(%s,'%%H:00:00')");
		functionMethodMap.put(FunctionMethodEnum.EXTRACT_MINUTE, "DATE_FORMAT(%s,'%%H:%%i:00')");
		functionMethodMap.put(FunctionMethodEnum.EXTRACT_SECOND, "DATE_FORMAT(%s,'%%H:%%i:%%S')");
		functionMethodMap.put(FunctionMethodEnum.EXTRACT_FULL, "DATE_FORMAT(%s,'%%Y%%m%%d %%H:%%i:%%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 String quoteIdentifier(String name) {
		return "`" + name + "`";
	}

	@Override
	public String dbSchema(DatasourceDto datasource) {
		return datasource.getDsDb();
	}

	@Override
	public List<String> buildCreateTableSql(String tableName, String tableComment, List<FileStructureDto> structureList) {
		StringBuilder fieldSql = new StringBuilder();
		structureList.stream().forEach(f -> fieldSql.append(String.format("%s %s %s comment '%s',",
				f.getColumnName(), getColumnType(f.getColumnType(), f.getColumnSize(), f.getDecimalDigits()),
				f.isPrimaryKey() ? "primary key" : "", f.getComment())));
		fieldSql.deleteCharAt(fieldSql.length() - 1);
		return Arrays.asList(String.format("create table %s (%s) comment '%s'", tableName, fieldSql.toString(), tableComment));
	}
	
	@Override
	public DsTypeEnum dsType() {
		return DsTypeEnum.mysql;
	}

}
