package com.eversec.kit.creator.impl.page;

import com.eversec.common.Kv;

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

import static java.util.Arrays.asList;

/**
 * 创建list.html
 * @author: liangxianyou at 2018/8/6 16:33.
 */
public class ListKit extends PageKit {
    @Override
    public File getFile(String clazz) {
        return new File(filePath(clazz) + "/list.html");
    }

    @Override
    public String createStr(String clazz, List<Map> fieldList, Map para) {
        Kv kv = createTplData(clazz);
        kv.set("remark", para.get("remark"));
        kv.set("condition", getCondition(fieldList, para));
        getFileDir(clazz);

        return engine.getTemplate(tplPath + "front/listTpl.html").renderToString(kv);
    }

    private String getCondition(List<Map> fieldList, Map para) {
        StringBuilder buf = new StringBuilder();
        if (para.get("condition") != null){
            List<String> list = asList((String[])para.get("condition"));
            fieldList.stream().filter(x -> list.contains(x.get("field") + "")).forEach(x->{
                buf.append(x.get("remark1")+"/");
            });
        }
        if (buf.length() > 0){
            buf.deleteCharAt(buf.length()-1);
        }
        return buf.toString();
    }
}
