/*
package com.tashow.cloud.canal.service;

import com.alibaba.otter.canal.client.CanalConnector;
import com.alibaba.otter.canal.client.CanalConnectors;
import com.alibaba.otter.canal.protocol.CanalEntry.*;
import com.alibaba.otter.canal.protocol.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.net.InetSocketAddress;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

@Service
public class CanalSyncServiceTest {

    private static final Queue<String> SQL_QUEUE = new ConcurrentLinkedQueue<>();
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private Canaldb canaldb;

    @PostConstruct
    public void start() {
        new Thread(this::runCanalClient).start();
    }

    private void runCanalClient() {
        CanalConnector connector = CanalConnectors.newSingleConnector(
                new InetSocketAddress("43.139.42.137", 11111),
                "example",
                "",
                ""
        );
        int batchSize = 1000;

        try {
            connector.connect();
            connector.subscribe("tashow-platform\\..*");
            connector.rollback();

            while (true) {
                Message message = connector.getWithoutAck(batchSize);
                System.out.println("Received message id: " + message.getId() + ", entries size: " + message.getEntries().size());
                long batchId = message.getId();
                int size = message.getEntries().size();

                if (batchId == -1 || size == 0) {
                    Thread.sleep(1000);
                    continue;
                }

                dataHandle(message.getEntries());
                connector.ack(batchId);

                if (SQL_QUEUE.size() > 0) {
                    executeQueueSql();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            connector.disconnect();
        }
    }

    private void dataHandle(List<Entry> entries) {
        for (Entry entry : entries) {
            if (entry.getEntryType() != EntryType.ROWDATA) continue;

            try {
                RowChange rowChange = RowChange.parseFrom(entry.getStoreValue());
                EventType eventType = rowChange.getEventType();
                String schemaName = entry.getHeader().getSchemaName();
                String tableName = entry.getHeader().getTableName();

                System.out.println("schema: " + schemaName + ", table: " + tableName + ", type: " + eventType);

                if (eventType == EventType.DELETE) {
                    saveDeleteSql(entry);
                } else if (eventType == EventType.UPDATE) {
                    saveUpdateSql(entry);
                } else if (eventType == EventType.INSERT) {
                    saveInsertSql(entry);
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private void saveInsertSql(Entry entry) throws Exception {
        RowChange rowChange = RowChange.parseFrom(entry.getStoreValue());
        for (RowData rowData : rowChange.getRowDatasList()) {
            List<Column> columns = rowData.getAfterColumnsList();
            StringBuilder sql = new StringBuilder("INSERT INTO ")
                    .append(entry.getHeader().getTableName()).append(" (")
                    .append(columns.stream().map(Column::getName).reduce((a, b) -> a + "," + b).orElse(""))
                    .append(") VALUES (")
                    .append(columns.stream().map(c -> "'" + c.getValue() + "'").reduce((a, b) -> a + "," + b).orElse(""))
                    .append(");");

            SQL_QUEUE.add(sql.toString());
        }
    }

    private void saveUpdateSql(Entry entry) throws Exception {
        RowChange rowChange = RowChange.parseFrom(entry.getStoreValue());
        for (RowData rowData : rowChange.getRowDatasList()) {
            List<Column> newColumns = rowData.getAfterColumnsList();
            List<Column> oldColumns = rowData.getBeforeColumnsList();

            StringBuilder setClause = new StringBuilder();
            for (Column col : newColumns) {
                setClause.append(col.getName()).append("='").append(col.getValue()).append("', ");
            }
            if (setClause.length() > 0) setClause.setLength(setClause.length() - 2);

            String whereClause = oldColumns.stream()
                    .filter(Column::getIsKey)
                    .map(c -> c.getName() + "='" + c.getValue() + "'")
                    .findFirst()
                    .orElseThrow(() -> new RuntimeException("未找到主键"));

            SQL_QUEUE.add("UPDATE " + entry.getHeader().getTableName() + " SET " + setClause + " WHERE " + whereClause + ";");
        }
    }

    private void saveDeleteSql(Entry entry) throws Exception {
        RowChange rowChange = RowChange.parseFrom(entry.getStoreValue());
        for (RowData rowData : rowChange.getRowDatasList()) {
            String whereClause = rowData.getBeforeColumnsList().stream()
                    .filter(Column::getIsKey)
                    .map(c -> c.getName() + "='" + c.getValue() + "'")
                    .findFirst()
                    .orElseThrow(() -> new RuntimeException("未找到主键"));

            SQL_QUEUE.add("DELETE FROM " + entry.getHeader().getTableName() + " WHERE " + whereClause + ";");
        }
    }


    private void executeQueueSql() {
        int size = SQL_QUEUE.size();
        for (int i = 0; i < size; i++) {
            String sql = SQL_QUEUE.poll();
            canaldb.execute(sql);
        }
    }
}
*/
