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/4 10:23.
 */
public class ControllerKit extends ICreator {

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

    @Override
    public File getFile(String clazz) {
        return new File(filePath(pkg) + clazz + "Controller.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")+""));
        });

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

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

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