package com.test;

import club.kingon.sql.builder.SelectSqlBuilder;
import club.kingon.sql.builder.SqlBuilder;
import club.kingon.sql.builder.entry.Alias;
import club.kingon.sql.builder.entry.Column;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.db.sql.SqlUtil;
import com.lanyuanxiaoyao.service.common.SQLConstants;

import static com.lanyuanxiaoyao.service.common.SQLConstants.HudiCollectBuild.*;

/**
 * @author lanyuanxiaoyao
 * @date 2023-06-07
 */
public class SqlBuilderTests {
    private static Long flinkJobId = 100086L;
    private static String alias = "hello_world";
    private static String STATUS_Y = "y";
    private static String STATUS_N = "n";

    private static SqlBuilder generateTableMetaList(SelectSqlBuilder builder, Long flinkJobId, String aliasText) {
        return builder
                .from(
                        SQLConstants.IapDatahub.DataSource._alias_,
                        SQLConstants.IapDatahub.DataSourceTable._alias_,
                        SQLConstants.IapDatahub.DataSourceTableField._alias_,
                        TbAppFlinkJobConfig._alias_,
                        TbAppHudiJobConfig._alias_,
                        Alias.of(TbAppYarnJobConfig._origin_, "tayjc_sync"),
                        Alias.of(TbAppYarnJobConfig._origin_, "tayjc_compaction"),
                        TbAppGlobalConfig._alias_,
                        TbAppCollectTableInfo._alias_,
                        TbAppHudiSyncState._alias_
                )
                .whereEq(SQLConstants.IapDatahub.DataSource.DS_ROLE_A, "src")
                .andEq(SQLConstants.IapDatahub.DataSource.DS_STATE_A, STATUS_Y)
                .andEq(SQLConstants.IapDatahub.DataSource.RECORD_STATE_A, STATUS_Y)
                .andEq(SQLConstants.IapDatahub.DataSourceTable.DS_ID_A, Column.as(SQLConstants.IapDatahub.DataSource.DS_ID_A))
                .andEq(SQLConstants.IapDatahub.DataSourceTable.RECORD_STATE_A, STATUS_Y)
                .andEq(SQLConstants.IapDatahub.DataSourceTableField.TABLE_ID_A, Column.as(SQLConstants.IapDatahub.DataSourceTable.TABLE_ID_A))
                .andEq(SQLConstants.IapDatahub.DataSourceTableField.RECORD_STATE_A, STATUS_Y)
                .andIn(SQLConstants.IapDatahub.DataSource.DS_TYPE_A, "udal", "telepg")
                .andEq(SQLConstants.IapDatahub.DataSource.DS_NAME_A, Column.as(TbAppCollectTableInfo.SRC_DB_A))
                .andEq(SQLConstants.IapDatahub.DataSource.SCHEMA_NAME_A, Column.as(TbAppCollectTableInfo.SRC_SCHEMA_A))
                .andEq(SQLConstants.IapDatahub.DataSourceTable.TABLE_NAME_A, Column.as(TbAppCollectTableInfo.SRC_TABLE_A))
                .andEq(TbAppCollectTableInfo.FLINK_JOB_ID_A, Column.as(TbAppFlinkJobConfig.ID_A))
                .andEq(TbAppCollectTableInfo.HUDI_JOB_ID_A, Column.as(TbAppHudiJobConfig.ID_A))
                .andEq(TbAppCollectTableInfo.SYNC_YARN_JOB_ID_A, Column.as("tayjc_sync.id"))
                .andEq(TbAppCollectTableInfo.COMPACTION_YARN_JOB_ID_A, Column.as("tayjc_compaction.id"))
                .andEq(TbAppCollectTableInfo.CONFIG_ID_A, Column.as(TbAppGlobalConfig.ID_A))
                .andEq(TbAppHudiSyncState.ID_A, Column.as(StrUtil.format("concat({}, '-', {})", TbAppFlinkJobConfig.ID_A, TbAppCollectTableInfo.ALIAS_A)))
                .andEq(ObjectUtil.isNotNull(flinkJobId), TbAppFlinkJobConfig.ID_A, flinkJobId)
                .andEq(StrUtil.isNotBlank(aliasText), TbAppCollectTableInfo.ALIAS_A, aliasText)
                .andEq(TbAppCollectTableInfo.STATUS_A, STATUS_Y)
                .andEq(TbAppFlinkJobConfig.STATUS_A, STATUS_Y)
                .andEq(TbAppHudiJobConfig.STATUS_A, STATUS_Y)
                .andEq("tayjc_sync.status", STATUS_Y)
                .andEq("tayjc_compaction.status", STATUS_Y)
                .orderBy(SQLConstants.IapDatahub.DataSourceTableField.FIELD_SEQ_A);
    }

    public static void main(String[] args) {
        System.out.println(SqlUtil.formatSql(
                generateTableMetaList(
                        SqlBuilder.select(
                                TbAppFlinkJobConfig.ID_A,
                                TbAppCollectTableInfo.ALIAS_A,
                                TbAppCollectTableInfo.TGT_HDFS_PATH_A,
                                TbAppCollectTableInfo.SRC_PULSAR_ADDR_A,
                                TbAppCollectTableInfo.SRC_TOPIC_A
                        ),
                        null,
                        null
                ).build()
        ));
    }
}
