package com.eversec.kit.creator.impl;


import com.eversec.common.Kv;

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

import static java.util.Arrays.asList;

/**
 * Created by liangxianyou@eversec.cn at 2018/5/3 18:28.
 */
public class XmlSqlKit extends XmlKit {

    private String pkg = "resources.mapper";

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

    @Override
    public String createStr(String clazz, List<Map> fieldList, Map para) {
        Kv kv = createTplData(clazz);
        kv.set("remark", para.get("remark"));
        kv.set("verify", para.get("verify"));
        kv.set("condition", para.get("condition"));
        kv.set("table", getTableName(clazz));
        kv.set("fields", dealField(fieldList));
        kv.set("order", createOrder(clazz, fieldList));

        return engine.getTemplate(tplPath + "MapperTpl.xml").renderToString(kv);
    }

    private static Map ordMap =
            Kv.of(getClazzName("basic_platform"), "order by `keyPlat` desc,`id` desc")
            .set(getClazzName("basic_enterprise"), "order by `keyEnterprise` desc,`id` desc")
            .set(getClazzName("basic_ip"), "order by `status` desc,`id` desc");
    private String createOrder(String clazz, List<Map> fieldList) {
        return String.valueOf(ordMap.getOrDefault(clazz, "order by `id` desc"));
    }

    private static List<String> numbers = asList("Integer", "Long");
    private List<Map> dealField(List<Map> fieldList) {
        //处理数值型字段标记，--在mybites 的xml中 数值型0 ，判断 != ''  的问题

        fieldList.stream().forEach(f -> {
            String jType = getFiledType(f.get("cate")+"");
            if (jType != "Date" && jType != "String" && !numbers.contains(jType)) {
                System.out.println(jType);
            }

            f.put("isNumber", numbers.contains(jType));
        });

        return fieldList;
    }


}
