package com.lanyuanxiaoyao.service.common.utils;

import com.lanyuanxiaoyao.service.common.entity.SyncState;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

/**
 * @author ZhangJiacheng
 * @date 2023-04-24
 */
public class SyncStateHelper {
    public static String allSyncStateSql(String database) {
        // language=MySQL
        return "select info.flink_job_id,\n" +
                "       info.alias,\n" +
                "       state.message_id,\n" +
                "       state.source_start_time,\n" +
                "       state.source_checkpoint_time,\n" +
                "       state.source_publish_time,\n" +
                "       state.source_op_time,\n" +
                "       state.compaction_start_time,\n" +
                "       state.compaction_finish_time,\n" +
                "       state.compaction_application_id,\n" +
                "       state.compaction_status,\n" +
                "       state.compaction_status_time,\n" +
                "       state.compaction_latest_op_ts\n" +
                "from " + database + ".tb_app_hudi_sync_state state,\n" +
                "     " + database + ".tb_app_collect_table_info info\n" +
                "where state.id = concat(info.flink_job_id, '-', info.alias)\n" +
                "  and info.status = 'y'";
    }

    public static String syncStateSql(String database) {
        // language=MySQL
        return "select info.flink_job_id,\n" +
                "       info.alias,\n" +
                "       state.message_id,\n" +
                "       state.source_start_time,\n" +
                "       state.source_checkpoint_time,\n" +
                "       state.source_publish_time,\n" +
                "       state.source_op_time,\n" +
                "       state.compaction_start_time,\n" +
                "       state.compaction_finish_time,\n" +
                "       state.compaction_application_id,\n" +
                "       state.compaction_status,\n" +
                "       state.compaction_status_time,\n" +
                "       state.compaction_latest_op_ts\n" +
                "from " + database + ".tb_app_hudi_sync_state state,\n" +
                "     " + database + ".tb_app_collect_table_info info\n" +
                "where state.id = concat(info.flink_job_id, '-', info.alias)\n" +
                "  and info.flink_job_id = ?\n" +
                "  and info.alias = ?\n" +
                "  and info.status = 'y'";
    }

    public static List<SyncState> from(ResultSet rs) throws SQLException {
        List<SyncState> results = new ArrayList<>();
        Function<Timestamp, Long> dateConvertor = timestamp -> timestamp == null ? 0 : timestamp.getTime();
        while (rs.next()) {
            results.add(SyncState.builder()
                    .flinkJobId(rs.getLong(1))
                    .alias(rs.getString(2))
                    .messageId(rs.getString(3))
                    .sourceStartTime(dateConvertor.apply(rs.getTimestamp(4)))
                    .sourceCheckpointTime(dateConvertor.apply(rs.getTimestamp(5)))
                    .sourcePublishTime(dateConvertor.apply(rs.getTimestamp(6)))
                    .sourceOperationTime(dateConvertor.apply(rs.getTimestamp(7)))
                    .compactionStartTime(dateConvertor.apply(rs.getTimestamp(8)))
                    .compactionFinishTime(dateConvertor.apply(rs.getTimestamp(9)))
                    .compactionApplicationId(rs.getString(10))
                    .compactionStatus(rs.getString(11))
                    .compactionStatusTime(dateConvertor.apply(rs.getTimestamp(12)))
                    .compactionLatestOperationTime(dateConvertor.apply(rs.getTimestamp(13)))
                    .build());
        }
        return results;
    }
}
