package com.eversec.kit.creator.impl;

import java.io.File;
import java.util.ArrayList;
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 XmlESKit extends XmlKit {


    @Override
    public File getFile(String clazz) {
        return new File(dirPath + "esmapper/" + clazz + "Mapper.xml");
    }

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

        String esIndex = clazz.substring(0,2).toLowerCase()+ "_" +clazz.substring(2).toLowerCase()+ "_all";
        String esType = clazz.substring(0,2).toLowerCase()+ "_" +clazz.substring(2).toLowerCase();

        //head
        buf.append(
            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
            "<mapper namespace=\""+ clazz +"Mapper\">\n"
        );

        //=========================temple==================================
        buf.append(insertStr(clazz, fieldList, esType, esIndex));
        buf.append(findStr(clazz, fieldList, esType));

        buf.append(updateStr(clazz, fieldList, esType));


        buf.append(
            "\n" +
            "</mapper>"
        );

        return buf.toString();
    }

    private StringBuffer updateStr(String clazz, List<Map> filed, String esType) {
        StringBuffer buf = new StringBuffer();
        buf.append(
            "\t<sql id=\"update\">\n" +
            "\t\tupdate "+ esType +" set\n"
        );

        List<String> arr = asList("status", "update_user_name", "update_user_id", "update_time");

        for (int i = 0; i < arr.size(); i++) {
            buf.append(
                "\t\t<isNotEmpty property=\""+ arr.get(i) +"\""+("status".equals(arr.get(i)) ? "" : " type=\"String\"")+">\n" +
                "\t\t\t"+ arr.get(i) +"=#{"+  arr.get(i) +"}"+ (i == arr.size()-1 ? "":",") + "\n" +
                "\t\t</isNotEmpty>\n"
            );
        }
        buf.append(
            "\t\t where\n" +
            "\t\t<isNotEmpty property=\"uid\">\n" +
            "\t\t\t uid=#{uid}\n" +
            "\t\t</isNotEmpty>\n" +
            "\t</sql>\n\n"
        );

        return buf;
    }

    private StringBuffer insertStr(String clazz, List<Map> filed, String esType, String esIndex) {
        StringBuffer buf = new StringBuffer();
        buf.append(
                "\t<sql id=\"insert\">\n" +
                "\t\tinsert into\n" +
                "\t\t"+esIndex+"."+ esType +" (\n"
        );
        int tag = 0;
        //body- k
        for (Map map : filed) {//"field","cate", "must","remark1", "remark2"
            String field = (map.get("field")+"").toLowerCase();
            String type = getFiledType(map.get("cate") + "");

            buf.append(
                    isMust(map) ?
                    "\t\t"+ (tag++  == 0 ? "" : ",") + field +"\n" :
                    "\t\t<isNotEmpty property=\""+ field +"\"" +
                    ("String".equals(type) ? " type=\"String\">\n" : ">\n") +
                    "\t\t\t"+ (tag++ == 0 ? "" : ",") + field +"\n" +
                    "\t\t</isNotEmpty>\n"
            );
        }
        buf.append(
                "\t\t)\n" +
                        "\t\tvalues (\n"
        );
        tag = 0;
        //body- v
        for (Map map : filed) {
            String field = (map.get("field")+"").toLowerCase();
            String type = getFiledType(map.get("cate") + "");
            buf.append(
                    "\t\t<isNotEmpty property=\""+ field +"\"" +
                    ("String".equals(type) ? " type=\"String\">\n" : ">\n") +
                    "\t\t\t"+ (tag++ == 0 ? "" : ",") +"#{"+ field +"}\n" +
                    "\t\t</isNotEmpty>\n"
            );
        }
        buf.append(
                "\t\t)\n" +
                "\t</sql>\n\n"
        );
        return buf;
    }

    private StringBuffer findStr(String clazz, List<Map> filed, String esType) {
        StringBuffer buf = new StringBuffer();
        List<String> filter = asList("uid", "insert_user_id", "tru", "status");

        List<String> conditions = asList("ip", "ip_md5", "url", "md5", "name", "domain", "organization", "version", "information", "interpolator"
                , "beforetitle", "aftertitle", "faketarget");

        //条件模糊查询
        StringBuffer condBuf = new StringBuffer();
        List<String> _conditions = new ArrayList();
        filed.forEach(fMap->{
            if (conditions.contains((fMap.get("field")+"").toLowerCase())){
                _conditions.add((fMap.get("field")+"").toLowerCase());
            }
        });
        if (_conditions.size() > 0){
            condBuf.append(
                    "\t\t<isNotEmpty property=\"condition\" type=\"String\">\n" +
                            "\t\t\tand ("
            );
            for (int i = 0; i < _conditions.size(); i++) {
                condBuf.append((i==0? "":" or ") + _conditions.get(i) +" like #{condition}");
            }
            condBuf.append(
                    ")\n" +
                "\t\t</isNotEmpty>\n"
            );
        }

        buf.append(
                "\t<sql id=\"findCount\">\n" +
                "\t\tselect count(1) from "+ esType +" where 1=1\n"
        );
        //查询条件
        buf.append(
                "\n"
        );

        buf.append(condBuf);//模糊查询

        filter.forEach(x->{
            buf.append(
                    "\t\t<isNotEmpty property=\""+ x +"\""+ ("status".equals(x) || "uid".equals(x) ? "" : " type=\"String\"") +">\n" +
                    "\t\t\t and "+ x +"=#{"+ x +"}\n" +
                    "\t\t</isNotEmpty>\n"
            );
        });
        buf.append(
                "\t\t<isNotEmpty property=\"startDate\" type=\"String\">\n" +
                "\t\t\t and datatime &gt; #{startDate}\n" +
                "\t\t</isNotEmpty>\n"+
                "\t\t<isNotEmpty property=\"endDate\" type=\"String\">\n" +
                "\t\t\t and datatime &lt; #{endDate}\n" +
                "\t\t</isNotEmpty>\n"
        );


        buf.append(
                "\n"
        );
        buf.append(
                "\t</sql>\n\n"
        );

        buf.append(
                "\t<sql id=\"findList\">\n" +
                "\t\tselect * from "+ esType +" where 1=1\n"
        );
        //查询条件
        buf.append(
                "\n"
        );

        buf.append(condBuf);//模糊查询

        filter.forEach(x->{
            buf.append(
                    "\t\t<isNotEmpty property=\""+ x +"\""+ ("status".equals(x) || "uid".equals(x) ? "" : " type=\"String\"") +">\n" +
                    "\t\t\t and "+ x +"=#{"+ x +"}\n" +
                    "\t\t</isNotEmpty>\n"
            );
        });
        buf.append(
                "\t\t<isNotEmpty property=\"startDate\" type=\"String\">\n" +
                "\t\t\t and datatime &gt; #{startDate}\n" +
                "\t\t</isNotEmpty>\n"+
                "\t\t<isNotEmpty property=\"endDate\" type=\"String\">\n" +
                "\t\t\t and datatime &lt; #{endDate}\n" +
                "\t\t</isNotEmpty>\n"
        );
        buf.append(
                "\t\t<isNotEmpty property=\"maxResult\">\n" +
                        "\t\t\tlimit #{maxResult}\n" +
                        "\t\t</isNotEmpty>\n" +
                        "\t\t<isNotEmpty property=\"startPosition\">\n" +
                        "\t\t\toffset #{startPosition}\n" +
                        "\t\t</isNotEmpty>\n"
        );
        buf.append(
                "\t</sql>\n\n"
        );
        return buf;
    }


}
