package chanedi.generator;

import chanedi.generator.model.Bean;
import chanedi.generator.model.Property;
import chanedi.generator.model.PropertyType;
import chanedi.util.StringUtils;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by jijingyu625 on 2016/4/18.
 */
public class CodeGenerator {

    private static ResourceLoader resourceLoader = new PathMatchingResourcePatternResolver();

    public static void generateSetter(File file, String className) throws IOException {
        String variableName = StringUtils.uncapitalize(className);
        System.out.println(className + " " + variableName + " = new " + className + "();");
        BufferedReader fileReader = new BufferedReader(new FileReader(file));

        while (true) {
            String line = fileReader.readLine();
            if (line == null) {
                break;
            }
            if (!line.contains(";")) {
                continue;
            }

            String[] splits = line.split(";")[0].split(" ");
            String attrName = splits[splits.length - 1];
            System.out.println(variableName + ".set" + StringUtils.capitalize(attrName) + "(" + attrName + ");");
        }
    }

    public static void generateDaoGetMethod(boolean isReturnList, String tableName, String attrs) throws IOException, TemplateException {
        File dir = resourceLoader.getResource("classpath:/tmpl/code").getFile();

        Bean bean = new Bean();
        bean.setTableName(tableName);

        String[] attrArray = attrs.replace(", ", ",").split(",");
        for (String attr : attrArray) {
            try {
                String[] splits = attr.split(" ");
                Property property = new Property();
                property.setType(new PropertyType(splits[0]));
                property.setName(splits[1]);
                bean.addProperty(property);
            } catch (Exception e) {
                throw new RuntimeException("attr的格式应为$Javatype$ $attrName$");
            }
        }

        Configuration cfg = new Configuration();
        cfg.setDirectoryForTemplateLoading(dir);
        cfg.setObjectWrapper(new DefaultObjectWrapper());
        Template temp = cfg.getTemplate(isReturnList ? "daoGetForListMethodTmpl.ftl" : "daoGetForObjMethodTmpl.ftl");
        Map dataMap = new HashMap();
        dataMap.put("bean", bean);
        dataMap.put("beanCapNameRemoveDTO", bean.getCapitalizeName().replace("DTO", ""));
        dataMap.put("beanNameRemoveDTO", bean.getName().replace("DTO", ""));
        Writer out = new OutputStreamWriter(System.out);
        temp.process(dataMap, out);
        out.flush();
        out.close();
    }

}

@@ -548,16 +548,80 @@
 til.Map;
+%0Aimport java.util.regex.Matcher;%0Aimport java.util.regex.Pattern;
 %0A%0A/**%0A *
@@ -1648,16 +1648,38 @@
 urnList,
+ String beanNameRegex,
  String 
@@ -2772,167 +2772,381 @@
     
-dataMap.put(%22
+if (
 bean
-Cap
 NameRe
-moveDTO%22, bean.getCapitalizeName().replace(%22DTO%22, %22%22));%0A        dataMap.put(%22beanNameRemoveDTO%22,
+gex != null) %7B%0A            // %E6%A0%B9%E6%8D%AE%E7%94%A8%E6%88%B7%E8%AE%BE%E7%BD%AE%E4%BF%AE%E6%AD%A3beanName%0A            Pattern pattern = Pattern.compile(beanNameRegex);%0A            Matcher matcher = pattern.matcher(bean.getTableName());%0A            matcher.find();%0A            String group = matcher.group(matcher.groupCount());%0A           
  bean.
-g
+s
 etName(
-).replace(%22DTO%22
+StringUtils.uncapitalizeCamelBySeparator(group
 , %22
+_
 %22));%0A
+        %7D%0A
     

Update CodeGenerator.java