package net.tccn.service;

import net.tccn.base.JBean;
import net.tccn.base.Kv;
import net.tccn.base.MetaKit;
import net.tccn.dbq.table.Table;
import net.tccn.meta.MetaTable;
import org.redkale.net.http.RestMapping;
import org.redkale.net.http.RestParam;
import org.redkale.net.http.RestService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 元数据实体管理
 * 1、实体导入，
 * 2、实体信息维护
 * 3、数据库表管理
 */
@RestService(automapping = true, comment = "元数据实体管理")
public class _TableService extends BaseService {

    @Resource
    private _FileService fileService;
    @Resource
    private _DbService dbService;


    @RestMapping(name = "sheets", comment = "导入选择列表数据准备")
    public JBean sheets(String cate, //类型
                        //excel {文件地址}
                        String filePath,

                        //mysql {数据库连接账号、数据源id、数据库database数组}
                        String dbPlatId, String catalog,
                        @RestParam(name = "platToken") String token) {

        JBean jBean = new JBean();

        if ("excel".equals(cate)) {
            jBean.setBody(fileService.data(filePath, token));

        } else if ("mysql".equals(cate)){
            List<Table> list = dbService.tableList(dbPlatId, catalog, null);

            String[] tableArr = list.stream().map(Table::getName).toArray(String[]::new);
            List<String> hv = MetaKit.tableExist(tableArr, token);

            List<MetaTable> sheets = new ArrayList<>();
            list.forEach(x -> {
                MetaTable bean = MetaTable.toAs(x);
                bean.setHv(hv.contains(x.getName()) ? 1 : 0);
                sheets.add(bean);
            });

            //对数据分组后返回
            Kv<String, MetaTable> data = Kv.of();
            sheets.forEach(x -> data.set(x.getName(), x));

            jBean.setBody(data);
        }

        return jBean;
    }


    /*public JBean tableInfo(DbAccount dbAccount,
                           String dbPlatId, String catalog, String tableName) {

        dbService.tableInfo(dbAccount, dbPlatId, catalog, tableName)

        return null;
    }*/

    @RestMapping(name = "table_save", comment = "保存数据源实体到元数据实体表")
    public void tableSave(String dbPlatId,
                          String catalog,
                          String[] tableArr,
                          @RestParam(name = "platToken") String token) {

        List<String> hv = MetaKit.tableExist(tableArr, token);
        List<Table> tables = dbService.tableInfoList(dbPlatId, catalog, tableArr);

        MetaTable[] metaTables = tables.stream()
                .filter(t -> !hv.contains(t.getName())) // 去除同名
                .map(t -> {
                    MetaTable metaTable = MetaTable.toAs(t);
                    metaTable.setCatalog(catalog);
                    metaTable.setDbPlatId(dbPlatId);
                    metaTable.setAlias(MetaKit.nextAlias());// 表别名
                    metaTable.setSysPlatId(platId(token));
                    return metaTable;
                }).toArray(MetaTable[]::new);

        MetaKit.save(metaTables);
    }

    /*@Comment("查询元数据中存在的表")
    private CompletableFuture<List<String>> tableExist(String[] tableArr, String token) {
        return CompletableFuture.supplyAsync(() -> {
            StringBuffer buf = new StringBuffer();
            buf.append("for d in MetaTable\n" +
                    "    filter d.name in [");
            for (String x : tableArr) {
                buf.append("'").append(x).append("',");
            }
            buf.deleteCharAt(buf.length() - 1);
            buf.append("] and d.sysPlatId=='" + platId(token) + "'\n" +
                    " return d.name");
            List<String> hv = MetaTable.dao.find(buf.toString(), String.class); //在元数据中已经存在的sheet
            return hv;
        });
    }*/
}
