package com.eversec.kit.creator.impl;

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

/**
 * Created by liangxianyou at 2019/1/22 20:29.
 */
public class EsSqlKit extends XmlKit{

    private String pkg = "resources.es_sql";

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

    @Override
    public String createStr(String clazz, List<Map> fieldList, Map para) {
        StringBuilder buf = new StringBuilder();

        buf.append("# ").append(para.get("remark")).append("\n");
        buf.append("# 列表查询\n");
        buf.append("#sql("+ para.get("table") +".list)\n");

        buf.append(" select * from " + para.get("table") + "_all\n");
        buf.append("    where 1=1\n");

        fieldList.forEach(m -> {
            buf.append("    #if("+ m.get("field") +")\n");
            buf.append("        and `"+ m.get("field") +"`=").append("#("+ m.get("field") +")\n");
            buf.append("    #end\n");
        });
        buf.append("    and `status` != 9\n");
        buf.append("    order by `id` desc\n");

        buf.append("    #if(maxResult)\n");
        buf.append("        limit #(startPosition),#(maxResult)\n");
        buf.append("    #end\n");

        buf.append("#end\n");
        return buf.toString();
    }
}
