package com.jiayue.ipfcst.common.data.strategy;

import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy;

/**
 * 命名策略
 *
 * @author zzy
 * @version 1.0
 * @since 2019/5/10 14:08
 */
public class IpfcstNamingStrategy extends SpringPhysicalNamingStrategy {
    private static final String PREFIX = "T_";
    private String tableName = "";

    @Override
    public Identifier toPhysicalCatalogName(Identifier identifier, JdbcEnvironment jdbcEnvironment) {
        String name;
        Identifier physicalCatalogName = super.toPhysicalCatalogName(identifier, jdbcEnvironment);
        if (physicalCatalogName != null) {
            name = PREFIX + physicalCatalogName.getText().toUpperCase();
            //判断是否为定时任务实体
            if (physicalCatalogName.getText().toUpperCase().startsWith("QRTZ_")) {
                name = physicalCatalogName.getText().toUpperCase();
            }
            return new Identifier(name, identifier.isQuoted());
        } else {
            return identifier;
        }
    }

    @Override
    public Identifier toPhysicalSchemaName(Identifier identifier, JdbcEnvironment jdbcEnvironment) {
        String name;
        Identifier physicalSchemaName = super.toPhysicalSchemaName(identifier, jdbcEnvironment);
        if (physicalSchemaName != null) {
            name = PREFIX + physicalSchemaName.getText().toUpperCase();
            //判断是否为定时任务实体
            if (physicalSchemaName.getText().toUpperCase().startsWith("QRTZ_")) {
                name = physicalSchemaName.getText().toUpperCase();
            }
            return new Identifier(name, identifier.isQuoted());
        } else {
            return identifier;
        }
    }

    @Override
    public Identifier toPhysicalTableName(Identifier identifier, JdbcEnvironment jdbcEnvironment) {
        String name;
        Identifier physicalTableName = super.toPhysicalTableName(identifier, jdbcEnvironment);
        if (physicalTableName != null) {
            tableName = physicalTableName.getText().toUpperCase();
            name = PREFIX + physicalTableName.getText().toUpperCase();
            //判断是否为定时任务实体
            if (physicalTableName.getText().toUpperCase().startsWith("QRTZ_")) {
                name = physicalTableName.getText().toUpperCase();
            }
            return new Identifier(name, identifier.isQuoted());
        } else {
            return identifier;
        }
    }

    @Override
    public Identifier toPhysicalSequenceName(Identifier identifier, JdbcEnvironment jdbcEnvironment) {
        String name;
        Identifier physicalSequenceName = super.toPhysicalSequenceName(identifier, jdbcEnvironment);
        if (physicalSequenceName != null) {
            name = PREFIX + physicalSequenceName.getText().toUpperCase();
            //判断是否为定时任务实体
            if (physicalSequenceName.getText().toUpperCase().startsWith("QRTZ_")) {
                name = physicalSequenceName.getText().toUpperCase();
            }
            return new Identifier(name, identifier.isQuoted());
        } else {
            return identifier;
        }
    }

    @Override
    public Identifier toPhysicalColumnName(Identifier identifier, JdbcEnvironment jdbcEnvironment) {
        String name;
        Identifier physicalColumnName = super.toPhysicalColumnName(identifier, jdbcEnvironment);
        if (physicalColumnName != null) {
            name = "C_" + physicalColumnName.getText().toUpperCase();
            if (tableName.startsWith("QRTZ_")) {
                name = physicalColumnName.getText().toUpperCase();
            }

            return new Identifier(name, identifier.isQuoted());
        } else {
            return identifier;
        }
    }
}
