package net.tccn.plat;

import net.tccn.base.BaseService;
import net.tccn.base.JBean;
import net.tccn.base.Kv;
import net.tccn.base.MetaKit;
import net.tccn.base.dbq.table.Table;
import net.tccn.file._FileService;
import net.tccn.meta.MTable;
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|mysql
                        // excel
                        String filePath,
                        //mysql {数据库连接账号、数据源id、数据库database数组}
                        int dbid, String catalog,
                        @RestParam(name = "plattoken") String token) {

        JBean jBean = new JBean();

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

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

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

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

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

            jBean.setBody(data);
        }

        return jBean;
    }

    @RestMapping(name = "sheet_info", comment = "sheet详情")
    public JBean sheetInfo(String cate, @RestParam(name = "plattoken") String token,
                           // excel
                           String filePath, String sheetName,
                           // mysql
                           int dbid, String catalog, String tableName) {
        if ("excel".equals(cate)) {
            return fileService.sheetData(filePath, sheetName, token);
        } else if ("mysql".equals(cate)) {
            return new JBean().setBody(dbService.tableInfo(dbid, catalog, tableName));
        }
        return null;
    }


    @RestMapping(name = "table_save", comment = "保存数据源实体到元数据实体表")
    public JBean tableSave(String cate, @RestParam(name = "plattoken") String token,
                           String filePath, String[] sheetNames,
                           int dbid, String catalog, String[] tableArr) {

        if ("excel".equals(cate)) {
            return fileService.saveSheet(sheetNames, filePath, token);
        } else if ("mysql".equals(cate)) {
            List<String> hv = MetaKit.tableExist(tableArr, token);
            List<Table> tables = dbService.tableInfoList(dbid, catalog, tableArr);

            MTable[] metaTables = tables.stream()
                    .filter(t -> !hv.contains(t.getName())) // 去除同名
                    .map(t -> {
                        MTable metaTable = MTable.toAs(t);
                        metaTable.setCatalog(catalog);
                        metaTable.setDbid(dbid);
                        metaTable.setAlias(metaKit.nextAlias());// 表别名
                        metaTable.setPlatid(platId(token));
                        return metaTable;
                    }).toArray(MTable[]::new);
            for (MTable table : metaTables) {
                metaKit.save(table);
            }

            // 已经有的表 更新
            MTable[] metaTables2 = tables.stream()
                    .filter(t -> hv.contains(t.getName())) // 去除同名
                    .map(t -> {
                        MTable table = MetaKit.getMetaTable(t.getName(), token);

                        MTable metaTable = MTable.toAs(t);

                        table.setCatalog(catalog);
                        table.setDbid(dbid);
                        table.setAlias(table.getAlias());// 表别名
                        table.setPlatid(platId(token));

                        table.setItems(metaTable.getItems());
                        table.setComment(metaTable.getComment());
                        return table;
                    }).toArray(MTable[]::new);
            for (MTable table : metaTables2) {
                metaKit.save(table);
            }
        }
        return JBean.OK;
    }
}
