package cc.smtweb.system.canal.file.impl;

import cc.smtweb.framework.core.common.CanalVO;
import cc.smtweb.framework.core.common.SwEnum;
import cc.smtweb.system.canal.file.common.AbstractFileWork;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

/**
 * @Author yaoq
 * @Date 2022年09月20日 10:29
 * @Description
 */
@Slf4j
public class KbSqlFileWork extends AbstractFileWork {

    @Override
    protected String getDbType() {
        return SwEnum.DbType.KBSQL.value;
    }

    @Override
    protected void doCreateSql(CanalVO canalVO) {
        Map<String, String> sqlMap = decodeCreateSql(canalVO.getSql());
        StringBuilder sql = new StringBuilder();
        sql.append("create table public.").append(canalVO.getTableName()).append(" ( ");
        sqlMap.forEach((k, v) -> {
            if (idField.equals(k)) return;
            sql.append(k).append(" ").append(convertFieldType(v));
            if (k.equals(sqlMap.get(idField))) {
                sql.append(" ").append("NOT NULL DEFAULT -1");
            } else {
                sql.append(" ").append("NULL");
            }
            sql.append(",");
        });
        sql.append(" CONSTRAINT pk_").append(canalVO.getTableName()).append(" PRIMARY KEY (").append(sqlMap.get(idField)).append(") ENABLE VALIDATE ) ");
        log.debug("sql:::" + sql);
        getDbEngine().update(sql.toString());
    }

    @Override
    protected void doAlterSql(CanalVO canalVO) {
        String sql = canalVO.getSql().replaceAll("`", "").replace("\r\n", "");
        StringBuilder sb = new StringBuilder();
        //添加sql
        if (sql.contains("ADD")) {
            sb.append("ALTER TABLE public.").append(canalVO.getTableName());
            sql = sql.substring(sql.indexOf("ADD COLUMN"));
            String[] sqls = sql.split(",");
            for (String str : sqls) {
                String[] ss = str.split(" ");
                sb.append(" ADD COLUMN ").append(ss[2]).append(" ").append(convertFieldType(ss[3])).append(" NULL ").append(",");
            }
            sb.deleteCharAt(sb.length() - 1);
        } else if (sql.contains("DROP")) {
            sb.append("ALTER TABLE public.").append(canalVO.getTableName());
            sql = sql.substring(sql.indexOf("DROP COLUMN"));
            String[] sqls = sql.split(",");
            for (String str : sqls) {
                String[] ss = str.split(" ");
                sb.append(" DROP COLUMN ").append(ss[2]).append(",");
            }
            sb.deleteCharAt(sb.length() - 1);

        } else if (sql.contains("CHANGE")) {
            sql = sql.substring(sql.indexOf("CHANGE COLUMN"));
            String[] sqls = sql.split(",");
            for (String str : sqls) {
                String[] ss = str.split(" ");
                sb.append("ALTER TABLE public.").append(canalVO.getTableName()).append(" RENAME COLUMN ").append(ss[2]).append(" TO ").append(ss[3]).append(";");
            }
            sb.deleteCharAt(sb.length() - 1);
        } else if (sql.contains("MODIFY")) {
            sb.append("ALTER TABLE public.").append(canalVO.getTableName());
            sql = sql.substring(sql.indexOf("MODIFY COLUMN"));
            String[] sqls = sql.split(",");
            for (String str : sqls) {
                String[] ss = str.split(" ");
                sb.append(" ALTER COLUMN ").append(ss[2]).append(" TYPE ").append(convertFieldType(ss[3])).append(",");
            }
            sb.deleteCharAt(sb.length() - 1);
        }
        log.debug("sql:::" + sb.toString());
        getDbEngine().update(sb.toString());
    }


    private String convertFieldType(String type) {
        if (fieldMapping == null) return type;
        String[] types = type.replace("(", "-").replace(")", "").split("-");
        for (Map<String, String> field : fieldMapping.getFields()) {
            String key = types[0].toUpperCase();
            if (field.containsKey(key)) return field.get(key);
        }
        return type;
    }
}
