package net.tccn.qtask.impl;

import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.template.Engine;
import com.jfinal.template.Template;
import net.tccn.base.Kv;
import net.tccn.base.MetaKit;
import net.tccn.base.Utils;
import net.tccn.base.dbq.jdbc.api.DbKit;
import net.tccn.qtask.QTask;
import net.tccn.qtask.Task;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

public class QTaskMysql extends QTaskAbs implements QTask {

    public static Engine engine = Engine.create("sqlTpl");
    private static MysqlDialect dialect = new MysqlDialect();

    protected DbKit dbKit;

    static {
        engine.setDevMode(true);
    }

    public QTaskMysql(Task task) {
        super(task);
        this.dbKit = MetaKit.getDbKit(task.getDbid(), task.getCatalog());
    }

    @Override
    public Object execute() {
        Template tpl = engine.getTemplateByString(task.getContent());
        String sql = tpl.renderToString(getTask().getPara()).replaceAll("[\\s]+", " ");

        // 聚合统计返回统计结果 TODO 待完善
        if (sql.startsWith("UPDATE ") || sql.startsWith("update ")) {
            return dbKit.exetuteUpdate(sql);
        }
        String countSql = Utils.parserCountSql(sql);
        if (countSql == null || (sql.startsWith("SELECT COUNT") || sql.startsWith("select count") || sql.startsWith("SELECT count") || sql.startsWith("select COUNT"))) {
            return dbKit.find(sql, Kv.class);
        }

        /*
        // todo: 从sql分析，支持多种sql处理类别，
        if (sql.startsWith("select count")) {
            return dbKit.queryInt(sql);
        } else if (sql.startsWith("select ")) {
            String[] sqls = PageSqlKit.parsePageSql(sql);

            String findTotal = "select count(*) " + dialect.replaceOrderBy(sqls[1]);
            String findList = sqls[0] + " " + sqls[1];

            CompletableFuture<Integer> countFuture = CompletableFuture.supplyAsync(() -> dbKit.queryColumn(findTotal, int.class));
            CompletableFuture<List<Map>> listFuture = CompletableFuture.supplyAsync(() -> dbKit.findList(findList, Map.class));

            try {
                return PageBean.by(listFuture.get(), countFuture.get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        } else if (sql.startsWith("update ") || sql.startsWith("UPDATE ") ||
                sql.startsWith("delete ") || sql.startsWith("DELETE ") ||
                sql.startsWith("delete ") || sql.startsWith("DELETE ") ||
                sql.startsWith("insert ") || sql.startsWith("INSERT ")){
            dbKit.exetute(sql);
        }*/

        // sql 解析-检查：未设置分页设置默认分页
        sql = Utils.parserSql(sql);

        Kv kv = Kv.of();

        CompletableFuture<Long> countFuture = dbKit.findColumnAsync(countSql, long.class);
        CompletableFuture<List<Kv>> listFuture = dbKit.queryListAsync(sql, Kv.class);

        try {
            kv.set("count", countFuture.get());
            kv.set("list", listFuture.get());
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }
        return kv;
    }
}
