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@eversec.cn at 2018/5/3 18:29.
 */
public class JavaBeanKit extends ICreator {

    private String pkg = ICreator.pkg + ".domain";

    @Override
    public File getFile(String clazz) {
        return new File(filePath(pkg) + clazz + ".java");
    }

    @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", getFiledType(x.get("cate")+""));
            x.put("remark", x.get("remark1") + " ("+ x.get("remark2") +")" );

        });

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

        return engine.getTemplate(tplPath + "BeanTpl.java").renderToString(kv);
    }

    protected boolean hasDate(List<Map> fieldList) {
        return fieldList.stream().filter(x->"Date".equalsIgnoreCase(String.valueOf(x.get("fieldType"))) ).count() > 0;
    }

}
