package com.lanyuanxiaoyao.service.common.utils;

import com.lanyuanxiaoyao.service.common.entity.FlinkJob;
import com.lanyuanxiaoyao.service.common.entity.TableMeta;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * Flink Job 工具类
 *
 * @author ZhangJiacheng
 * @version 0.0.1
 * @date 2021-12-08
 */
public class FlinkJobHelper {
    public static String allFlinkJobSql(String database) {
        // language=MySQL
        return "select tafjc.id,\n" +
                "       tafjc.name,\n" +
                "       tafjc.run_mode,\n" +
                "       tayjc.job_manager_memory,\n" +
                "       tayjc.task_manager_memory\n" +
                "from `" + database + "`.tb_app_flink_job_config tafjc\n" +
                "         left join\n" +
                "     `" + database + "`.tb_app_yarn_job_config tayjc on tafjc.one_in_one_yarn_job_id = tayjc.id and tayjc.status = 'y'\n" +
                "where tafjc.status = 'y'";
    }

    public static String flinkJobSql(String database) {
        // language=MySQL
        return "select tafjc.id,\n" +
                "       tafjc.name,\n" +
                "       tafjc.run_mode,\n" +
                "       tayjc.job_manager_memory,\n" +
                "       tayjc.task_manager_memory\n" +
                "from `" + database + "`.tb_app_flink_job_config tafjc\n" +
                "         left join\n" +
                "     `" + database + "`.tb_app_yarn_job_config tayjc on tafjc.one_in_one_yarn_job_id = tayjc.id and tayjc.status = 'y'\n" +
                "where tafjc.id = ?\n" +
                "  and tafjc.status = 'y'";
    }

    public static List<FlinkJob> from(ResultSet rs) throws SQLException {
        List<FlinkJob> results = new ArrayList<>();
        while (rs.next()) {
            String runModeText = rs.getString(3);
            FlinkJob.RunMode mode;
            try {
                mode = FlinkJob.RunMode.valueOf(runModeText);
            } catch (IllegalArgumentException e) {
                mode = FlinkJob.RunMode.ALL_IN_ONE;
            }
            TableMeta.YarnMeta yarnMeta = TableMeta.YarnMeta.builder()
                    .jobManagerMemory(rs.getInt(4))
                    .taskManagerMemory(rs.getInt(5))
                    .build();
            results.add(
                    FlinkJob.builder()
                            .id(rs.getLong(1))
                            .name(rs.getString(2))
                            .runMode(mode)
                            .oneInOneSyncYarn(yarnMeta)
                            .build()
            );
        }
        return results;
    }

    public static Optional<FlinkJob> fromOne(ResultSet rs) throws SQLException {
        List<FlinkJob> results = from(rs);
        if (results.size() < 1) {
            return Optional.empty();
        } else if (results.size() > 1) {
            throw new SQLException("Found more than 1 records");
        } else {
            return Optional.of(results.get(0));
        }
    }

    public static boolean isOneInOneMode(FlinkJob job) {
        return FlinkJob.RunMode.ONE_IN_ONE.equals(job.getRunMode());
    }
}
