package com.lanyuanxiaoyao.service.command.commands.jobs;

import cn.hutool.core.util.StrUtil;
import com.lanyuanxiaoyao.service.common.entity.FlinkJob;
import com.lanyuanxiaoyao.service.forest.service.InfoService;
import org.eclipse.collections.api.factory.Lists;
import org.eclipse.collections.api.list.ImmutableList;

/**
 * @date 2022-06-29
 */
public class AcctBigTableAdder extends BigTableAdder {
    public AcctBigTableAdder(InfoService infoService, String database, String type, String schema, String table, String pulsarAddress, String hdfs) {
        super(infoService, database, type, schema, table, pulsarAddress, hdfs);
    }

    protected ImmutableList<Table> getTables(String suffix) {
        return getTables()
                .tap(table -> {
                    table.alias = table.alias + "_" + suffix;
                    table.srcDb = table.srcDb + "_" + suffix;
                    table.srcSchema = table.srcSchema + "_" + suffix;
                    table.tgtDb = table.tgtDb + "_" + suffix;
                    table.srcTopic = StrUtil.format("persistent://odcp/{}/{}", table.srcSchema, table.srcTable);
                    if (StrUtil.endWith(table.tgtHdfsPath, "b")) {
                        table.tgtHdfsPath = StrUtil.format("{}/{}/{}_b", hdfs, table.tgtDb, table.tgtTable);
                    } else {
                        table.tgtHdfsPath = StrUtil.format("{}/{}/{}", hdfs, table.tgtDb, table.tgtTable);
                    }
                });
    }

    @Override
    protected ImmutableList<Job> getJobs() {
        return Lists.immutable.of(
                new Job(
                        snowflake.nextId(),
                        StrUtil.format("{} {}", schema, table),
                        FlinkJob.RunMode.ONE_IN_ONE.name(),
                        Lists.immutable.of(
                                getTables("gz"),
                                getTables("fs"),
                                getTables("sz"),
                                getTables("dg")
                        ).flatCollect(list -> list),
                        true
                )
        );
    }
}
