package com.eversec.kit.creator.impl;

import com.eversec.common.Kv;
import com.eversec.kit.creator.ICreator;

import java.io.File;
import java.util.List;
import java.util.Map;

/**
 * Created by liangxianyou at 2018/5/22 11:24.
 */
public class SqlKit extends ICreator {

    private static String pkg = "resources.ddl";

    public static File getFile(){
        return new File(dirPath + pkg.replace('.', '/') + "/" + "ddl.sql");
    }

    @Override
    public File getFile(String clazz) {
        return null;
    }

    @Override
    public String createStr(String clazz, List<Map> fieldList, Map para) {
        fieldList.forEach(x->{
            x.put("field", (x.get("field") + "").trim());
            x.put("isMust", isMust(x));
            //x.put("fieldType", this.getFieldType(x.get("cate")+""));
            x.put("fieldType", x.get("cate"));
            x.put("remark", x.get("remark1"));
        });

        Kv kv = createTplData(clazz);
        kv.set("remark", para.get("remark"));
        kv.set("fieldList", fieldList);
        kv.set("tableName", getTableName(clazz));

        return engine.getTemplate(tplPath + "SqlTpl.sql").renderToString(kv);
    }

    private Kv fieldType =
            Kv.of("string", "varchar(64)")
            .set("int", "int(11)")
            .set("long", "bigint(11)")
            .set("date", "datetime");
    private String getFieldType(String cate) {
        return (String) fieldType.getOrDefault(cate.trim().toLowerCase(),"varchar(64)");
    }
}
