package com.izouma.awesomeadmin.frame.gen;

import com.izouma.awesomeadmin.frame.util.StringUtil;
import com.izouma.awesomeadmin.util.PropertiesFileLoader;
import org.apache.commons.lang.StringUtils;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import java.util.Date;


public class GenEntity {

    private static boolean f_util = false; // 是否需要导入包java.util.*
    private static boolean f_sql = false; // 是否需要导入包java.sql.*
    private static boolean f_math = false;//是否需要导入包 java.math.BigDecimal


    public static void GenAll(TableParams tableParams) {

        String[] colnames; // 列名数组
        String[] colTypes; //列名类型数组
        String[] colComments;//列名注释数组mybatis-generator-core

        int[] colSizes; //列名大小数组

        String PK_NAME = "id";//主键名
        Connection con = null;

        //查要生成实体类的表
        String sql = "select * from " + tableParams.getTabelName();
        PreparedStatement pStemt = null;
        try {

            PropertiesFileLoader propertiesFileLoader = new PropertiesFileLoader();
            String JDBC_URL = propertiesFileLoader.getProperties("properties/jdbc.properties", "jdbc.url");
            String JDBC_NAME = propertiesFileLoader.getProperties("properties/jdbc.properties", "jdbc.username");
            String JDBC_PASS = propertiesFileLoader.getProperties("properties/jdbc.properties", "jdbc.password");
            String JDBC_DRIVER = propertiesFileLoader.getProperties("properties/jdbc.properties", "jdbc.driverClassName");

            try {
                Class.forName(JDBC_DRIVER);
            } catch (ClassNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }


            //con = DriverManager.getConnection(JdbcConfig.URL, JdbcConfig.NAME, JdbcConfig.PASS);
            con = DriverManager.getConnection(JDBC_URL, JDBC_NAME, JDBC_PASS);

            pStemt = con.prepareStatement(sql);

            ResultSetMetaData rsmd = pStemt.getMetaData();

            ResultSet pkRSet = con.getMetaData().getPrimaryKeys(con.getCatalog().toUpperCase(),
                    null, tableParams.getTabelName());

            while (pkRSet.next()) {
                System.out.println(pkRSet.getString("COLUMN_NAME"));
                PK_NAME = pkRSet.getString("COLUMN_NAME");
            }


            int size = rsmd.getColumnCount();   //统计列
            colComments = new String[size];
            colnames = new String[size];
            colTypes = new String[size];
            colSizes = new int[size];
            for (int i = 0; i < size; i++) {
                colnames[i] = rsmd.getColumnName(i + 1);
                colTypes[i] = rsmd.getColumnTypeName(i + 1);

                if (colTypes[i].equalsIgnoreCase("datetime") || colTypes[i].equalsIgnoreCase("timestamp")) {
                    f_util = true;
                }
                if (colTypes[i].equalsIgnoreCase("image") || colTypes[i].equalsIgnoreCase("text")) {
                    f_sql = true;
                }
                if (colTypes[i].equalsIgnoreCase("decimal") || colTypes[i].equalsIgnoreCase("numeric")
                        || colTypes[i].equalsIgnoreCase("real") || colTypes[i].equalsIgnoreCase("money")
                        || colTypes[i].equalsIgnoreCase("smallmoney")) {
                    f_math = true;
                }
                colSizes[i] = rsmd.getColumnDisplaySize(i + 1);
            }


            //获取注释
            ResultSet rs = pStemt.executeQuery("show full columns from " + tableParams.getTabelName());
            int j = 0;
            while (rs.next()) {
                colComments[j] = (rs.getString("Comment"));
                j++;
            }


            //如果生成实体
            if (tableParams.isModelFlag()) {

                createModelFile(tableParams, colnames, colTypes, colComments);
            }

            //如果生成数据层
            if (tableParams.isDaoFlag()) {

                createDaoClassFile(tableParams);
                createDaoXmlFile(tableParams, colnames, colTypes, PK_NAME);
            }

            //如果生成服务层
            if (tableParams.isServiceFlag()) {

                createServiceFile(tableParams, PK_NAME);
                createServiceImplFile(tableParams, PK_NAME);
            }

            //如果生成控制层
            if (tableParams.isControllerFlag()) {

                createControllerFile(tableParams, PK_NAME);
            }


            //如果生成JSP
            if (tableParams.isJspFlag()) {
                createVueByPageFile(tableParams, PK_NAME, colnames, colComments);
                createVueEditFile(tableParams, PK_NAME, colnames, colComments);
            }

            //关闭链接
            pStemt.close();
            pkRSet.close();
            con.close();

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {


        }
    }

    private static void createJspEditFile(TableParams tableParams, String pk_name, String[] colnames) {
        String jspEditContent = parseJspEdit(tableParams.getTabelName(), pk_name, colnames);

        try {
            File directory = new File("");

            String fileName = directory.getAbsolutePath() + "/" + changeToFolder(tableParams.getJspTargetPackage()) + StringUtil.underlineToCamel(tableParams.getTabelName()) + ".jsp";
            System.out.println("文件路径：" + fileName);
            FileWriter fw = new FileWriter(fileName);
            PrintWriter pw = new PrintWriter(fw);
            pw.println(jspEditContent);
            pw.flush();
            pw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static String parseJspEdit(String tabelName, String pkName, String[] colnames) {

        StringBuffer sb = new StringBuffer();


        String upTableName = StringUtil.initcap(tabelName);
        String downTableName = StringUtil.underlineToCamel(tabelName);


        sb.append("<%@ page contentType=\"text/html;charset=UTF-8\" language=\"java\" %>\r\n");
        sb.append("<html>\r\n");

        sb.append("<head>\r\n");
        sb.append("\t<jsp:include page=\"head.jsp\"/>\r\n");


        sb.append("\t<title>com.izouma.awesomeadmin.frame</title>\r\n");

        sb.append("</head>\r\n");

        sb.append("<body>\r\n");

        sb.append("<jsp:include page=\"contentBefore.jsp\"/>\r\n");
        sb.append("<template>\r\n");

        sb.append("\t<el-form ref=\"form\" :model=\"row_info\" label-width=\"80px\">\r\n");


        for (int i = 0; i < colnames.length; i++) {

            if (!pkName.equalsIgnoreCase(colnames[i])) {

                sb.append("\t\t<el-form-item label=\"" + StringUtil.underlineToCamel(colnames[i]) + "\">\r\n");
                sb.append("\t\t\t<el-input v-model=\"row_info." + StringUtil.underlineToCamel(colnames[i]) + "\"></el-input>\r\n");
                sb.append("\t\t</el-form-item>\r\n");
            }

        }


        sb.append("\t\t<el-form-item>\r\n");
        sb.append("\t\t\t<el-button type=\"primary\" @click=\"onSubmit\">{{edit?'保存':'立即创建'}}</el-button>\r\n");
        sb.append("\t\t\t<el-button>取消</el-button>\r\n");
        sb.append("\t</el-form-item>\r\n");
        sb.append("\t</el-form>\r\n");
        sb.append("</template>\r\n");


        sb.append("<jsp:include page=\"contentAfter.jsp\"/>\r\n");
        sb.append("</body>\r\n");


        sb.append("<script>\r\n");

        sb.append("\tfunction getQueryString(name) {\r\n");
        sb.append("\t\tvar reg = new RegExp(\"(^|&)\" + name + \"=([^&]*)(&|$)\", \"i\");\r\n");
        sb.append("\t\tvar r = window.location.search.substr(1).match(reg);\r\n");
        sb.append("\t\tif (r != null) return unescape(r[2]);\r\n");
        sb.append("\t\treturn null;\r\n");
        sb.append("\t}\r\n");

        sb.append("\tnew Vue({\r\n");
        sb.append("\t\tel: '#app',\r\n");
        sb.append("\t\tcreated: function () {\r\n");
        sb.append("\t\t\tvar " + StringUtil.underlineToCamel(pkName) + " = getQueryString('" + StringUtil.underlineToCamel(pkName) + "');\r\n");
        sb.append("\t\t\tif (" + StringUtil.underlineToCamel(pkName) + ") {\r\n");
        sb.append("\t\t\t\t$.get({\r\n");
        sb.append("\t\t\t\t\turl: '../" + downTableName + "/get" + upTableName + "',\r\n");
        sb.append("\t\t\t\t\tdata: {\r\n");
        sb.append("\t\t\t\t\t\t" + StringUtil.underlineToCamel(pkName) + ": " + StringUtil.underlineToCamel(pkName) + "\r\n");
        sb.append("\t\t\t\t\t}\r\n");
        sb.append("\t\t\t\t}).then(function (res) {\r\n");
        sb.append("\t\t\t\t\tif (res.success) {\r\n");
        sb.append("\t\t\t\t\t\tthis.edit = true;\r\n");
        sb.append("\t\t\t\t\t\tthis.row_info = res.data;\r\n");
        sb.append("\t\t\t\t\t}\r\n");
        sb.append("\t\t\t\t}.bind(this));\r\n");
        sb.append("\t\t\t}\r\n");
        sb.append("\t\t},\r\n");

        sb.append("\t\tdata: function () {\r\n");
        sb.append("\t\t\treturn {\r\n");
        sb.append("\t\t\t\tmenu: '0-0',\r\n");
        sb.append("\t\t\t\tuser: {\r\n");
        sb.append("\t\t\t\t\tid: '',\n\r\n");
        sb.append("\t\t\t\t\tusername: 'admin',\r\n");
        sb.append("\t\t\t\t\tavatar: ''\r\n");
        sb.append("\t\t\t\t},\r\n");
        sb.append("\t\t\t\tloading: false,\r\n");
        sb.append("\t\t\t\tedit: false,\r\n");
        sb.append("\t\t\t\ttab: 1,\r\n");
        sb.append("\t\t\t\trow_info : {},\r\n");
        sb.append("\t\t\t};\r\n");
        sb.append("\t\t},\r\n");


        sb.append("\t\tmethods: {\r\n");
        sb.append("\t\t\tlogout: function () {\r\n");
        sb.append("\t\t\t\tthis.$confirm('确定要注销吗?', '提示', {\r\n");
        sb.append("\t\t\t\t\tconfirmButtonText: '确定',\r\n");
        sb.append("\t\t\t\t\tcancelButtonText: '取消',\r\n");
        sb.append("\t\t\t\t\ttype: 'info'\r\n");
        sb.append("\t\t\t\t}).then(function () {\r\n");
        sb.append("\t\t\t\t\tlocalStorage.removeItem('user');\r\n");
        sb.append("\t\t\t\t\tthis.$router.push({path: '/login'});\r\n");
        sb.append("\t\t\t\t}.bind(this)).catch(function (e) {\r\n");
        sb.append("\r\n");
        sb.append("\t\t\t\t});\r\n");
        sb.append("\t\t\t},\r\n");

        sb.append("\t\t\tonSubmit: function () {\r\n");
        sb.append("\t\t\t\tvar data = JSON.parse(JSON.stringify(this.row_info));\r\n");
        sb.append("\t\t\t\t$.post({\r\n");
        sb.append("\t\t\t\t\turl: this.edit ? '../" + downTableName + "/update' : '../" + downTableName + "/save',\r\n");
        sb.append("\t\t\t\t\tdata: data\r\n");
        sb.append("\t\t\t\t}).then(function (res) {\r\n");
        sb.append("\t\t\t\t\tif (res.success) {\r\n");
        sb.append("\t\t\t\t\t\tif (!this.edit) {\r\n");
        sb.append("\t\t\t\t\t\t\tthis.row_info." + StringUtil.underlineToCamel(pkName) + " = res.data;\r\n");
        sb.append("\t\t\t\t\t\t}\r\n");
        sb.append("\t\t\t\t\t\tthis.edit = true;\r\n");
        sb.append("\t\t\t\t\t\tthis.$message.success(this.edit ? '保存成功' : '创建成功');\r\n");
        sb.append("\t\t\t\t\t} else {\r\n");
        sb.append("\t\t\t\t\t\tthis.$message.error(this.edit ? '保存失败' : '创建失败');\r\n");
        sb.append("\t\t\t\t\t}\r\n");
        sb.append("\t\t\t\t}.bind(this))\r\n");
        sb.append("\t\t\t},\r\n");

        sb.append("\t\t}\r\n");
        sb.append("\t})\r\n");
        sb.append("</script>\r\n");
        sb.append("</html>\r\n");


        return sb.toString();


    }

    private static void createVueEditFile(TableParams tableParams, String pk_name, String[] colnames, String[] colComments) {
        String jspEditContent = parseVueEdit(tableParams.getTabelName(), pk_name, colnames, colComments);

        try {
            File directory = new File("");

            String fileName = directory.getAbsolutePath() + "/" + changeToFolder(tableParams.getJspTargetPackage()) + StringUtil.underlineToCamel(tableParams.getTabelName()) + ".vue";
            System.out.println("文件路径：" + fileName);
            FileWriter fw = new FileWriter(fileName);
            PrintWriter pw = new PrintWriter(fw);
            pw.println(jspEditContent);
            pw.flush();
            pw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 生成VUE 编辑页面
     *
     * @param tabelName
     * @param pkName
     * @param colnames
     * @param colComments
     * @return
     */
    private static String parseVueEdit(String tabelName, String pkName, String[] colnames, String[] colComments) {

        StringBuffer sb = new StringBuffer();


        String upTableName = StringUtil.initcap(tabelName);
        String downTableName = StringUtil.underlineToCamel(tabelName);


        sb.append("<template>\r\n");

        sb.append("\t<div>\r\n");
        sb.append("\t\t<el-form :model=\"formData\" :rules=\"rules\" ref=\"form\" label-width=\"80px\" label-position=\"right\" size=\"small\"\r\n");
        sb.append("\t\t\t\tstyle=\"max-width: 500px;\">\r\n");


        sb.append("\t\t\r\n");
        sb.append("\t\t\r\n");


        for (int i = 0; i < colnames.length; i++) {

            if (!pkName.equalsIgnoreCase(colnames[i])) {

                sb.append("\t\t\t<el-form-item prop=\"" + StringUtil.underlineToCamel(colnames[i]) +
                        "\" label=\"" + (StringUtils.isNotEmpty(colComments[i]) ? colComments[i].split(":")[0] : colnames[i]) + "\">\r\n");
                sb.append("\t\t\t\t<el-input v-model=\"formData." + StringUtil.underlineToCamel(colnames[i]) + "\"></el-input>\r\n");
                sb.append("\t\t\t</el-form-item>\r\n");
            }

        }


        sb.append("\t\t\t<el-form-item>\r\n");
        sb.append("\t\t\t\t<el-button @click=\"onSave\" :loading=\"$store.state.fetchingData\" type=\"primary\">保存</el-button>\r\n");
        sb.append("\t\t\t\t<el-button @click=\"$router.go(-1)\">取消</el-button>\r\n");
        sb.append("\t\t\t</el-form-item>\r\n");
        sb.append("\t\t</el-form>\r\n");
        sb.append("\t</div>\r\n");
        sb.append("</template>\r\n");


        /**
         * script
         */
        sb.append("<script>\r\n");

        sb.append("\texport default {\r\n");

        /**
         * created
         */
        sb.append("\t\tcreated() {\r\n");
        sb.append("\t\t\tif (this.$route.query.id) {\r\n");
        sb.append("\t\t\t\tthis.$http.get({\r\n");
        sb.append("\t\t\t\t\turl: '/" + downTableName + "/get" + upTableName + "',\r\n");
        sb.append("\t\t\t\t\tdata: {\r\n");
        sb.append("\t\t\t\t\t\tid: this.$route.query.id\r\n");
        sb.append("\t\t\t\t\t}\r\n");
        sb.append("\t\t\t\t}).then(res => {\r\n");
        sb.append("\t\t\t\t\tif (res.success) {\r\n");
        sb.append("\t\t\t\t\t\tthis.formData = res.data;\r\n");
        sb.append("\t\t\t\t\t}\r\n");
        sb.append("\t\t\t\t})\r\n");
        sb.append("\t\t\t}\r\n");
        sb.append("\t\t},\r\n");


        /**
         * data
         */
        sb.append("\t\tdata() {\r\n");
        sb.append("\t\t\treturn {\r\n");
        sb.append("\t\t\t\tsaving: false,\r\n");
        sb.append("\t\t\t\tformData: {},\r\n");

        sb.append("\t\t\t\trules: {\r\n");
        for (int i = 0; i < colnames.length; i++) {

            if (!pkName.equalsIgnoreCase(colnames[i])) {

                sb.append("\t\t\t\t\t" + StringUtil.underlineToCamel(colnames[i]) + ": [\r\n");
                sb.append("\t\t\t\t\t\t{required: true, message: '请输入" + (StringUtils.isNotEmpty(colComments[i]) ? colComments[i].split(":")[0] : colnames[i]) + "', trigger: 'blur'},\r\n");
                sb.append("\t\t\t\t\t],\r\n");
            }

        }
        sb.append("\t\t\t\t},\r\n");

        sb.append("\t\t\t}\r\n");
        sb.append("\t\t},\r\n");


        /**
         * methods
         */
        sb.append("\tmethods: {\r\n");

        /**
         * onSave
         */
        sb.append("\t\t\tonSave() {\r\n");
        sb.append("\t\t\t\tthis.$refs.form.validate((valid) => {\r\n");
        sb.append("\t\t\t\t\tif (valid) {\r\n");
        sb.append("\t\t\t\t\t\tthis.submit();\r\n");
        sb.append("\t\t\t\t\t} else {\r\n");
        sb.append("\t\t\t\t\t\treturn false;\r\n");
        sb.append("\t\t\t\t\t}\r\n");
        sb.append("\t\t\t\t});\r\n");
        sb.append("\t\t\t},\r\n");

        /**
         * submit
         */
        sb.append("\t\t\tsubmit() {\r\n");
        sb.append("\t\t\t\tthis.$http.post({\r\n");
        sb.append("\t\t\t\t\turl: this.formData.id ? '/" + downTableName + "/update' : '/" + downTableName + "/save',\r\n");
        sb.append("\t\t\t\t\tdata: this.formData\r\n");
        sb.append("\t\t\t\t}).then(res => {\r\n");
        sb.append("\t\t\t\t\tif (res.success) {\r\n");
        sb.append("\t\t\t\t\t\tthis.$message.success('成功');\r\n");
        sb.append("\t\t\t\t\t\tthis.$router.go(-1);\r\n");
        sb.append("\t\t\t\t\t} else {\r\n");
        sb.append("\t\t\t\t\t\tthis.$message.warning('失败')\r\n");
        sb.append("\t\t\t\t\t}\r\n");
        sb.append("\t\t\t\t});\r\n");
        sb.append("\t\t\t}\r\n");


        sb.append("\t\t}\r\n");
        sb.append("\t}\r\n");
        sb.append("</script>\r\n");


        sb.append("<style lang=\"less\" scoped>\r\n");
        sb.append("\t\r\n");
        sb.append("</style>\r\n");
        sb.append("\r\n");


        return sb.toString();


    }


    private static void createJspByPageFile(TableParams tableParams, String pkName, String[] colnames) {
        String jspByPageContent = parseJspByPage(tableParams.getTabelName(), pkName, colnames);

        try {
            File directory = new File("");

            String fileName = directory.getAbsolutePath() + "/" + changeToFolder(tableParams.getJspTargetPackage()) + StringUtil.underlineToCamel(tableParams.getTabelName()) + "s.jsp";
            System.out.println("文件路径：" + fileName);
            FileWriter fw = new FileWriter(fileName);
            PrintWriter pw = new PrintWriter(fw);
            pw.println(jspByPageContent);
            pw.flush();
            pw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static String parseJspByPage(String tabelName, String pkName, String[] colnames) {

        StringBuffer sb = new StringBuffer();


        String downTableName = StringUtil.underlineToCamel(tabelName);


        sb.append("<%@ page contentType=\"text/html;charset=UTF-8\" language=\"java\" %>\r\n");
        sb.append("<html>\r\n");

        sb.append("<head>\r\n");
        sb.append("\t<jsp:include page=\"head.jsp\"/>\r\n");


        sb.append("\t<title>com.izouma.awesomeadmin.frame</title>\r\n");

        sb.append("</head>\r\n");

        sb.append("<body>\r\n");

        sb.append("<jsp:include page=\"contentBefore.jsp\"/>\r\n");
        sb.append("<template>\r\n");

        sb.append("\t<div class=\"filters\">\r\n");
        sb.append("\t\t<el-button type=\"primary\" @click=\"create()\">创建</el-button>\r\n");
        sb.append("\t\t<div class=\"num\"> 共{{totalNumber}}标签</div>\r\n");
        sb.append("\t</div>\r\n");
        sb.append("\r\n");

        sb.append("\t<el-table :data=\"rows\"\r\n");
        sb.append("\t\t\tref=\"table\"\r\n");
        sb.append("\t\t\tstyle=\"width: 100%\"\r\n");
        sb.append("\t\t\telement-loading-text=\"拼命加载中\"\r\n");
        sb.append("\t\t\tstripe\r\n");
        sb.append("\t\t\tv-loading=\"loading\">\r\n");

        sb.append("\t\t\t<el-table-column\r\n");
        sb.append("\t\t\t\ttype=\"index\"\r\n");
        sb.append("\t\t\t\twidth=\"70\"\r\n");
        sb.append("\t\t\t\talign=\"center\">\r\n");
        sb.append("\t\t\t</el-table-column>\r\n");

        for (int i = 0; i < colnames.length; i++) {

            if (!pkName.equalsIgnoreCase(colnames[i])) {

                sb.append("\t\t<el-table-column\r\n");
                sb.append("\t\t\t\tprop=\"" + StringUtil.underlineToCamel(colnames[i]) + "\"\r\n");
                sb.append("\t\t\t\tlabel=\"" + StringUtil.underlineToCamel(colnames[i]) + "\"\r\n");
                sb.append("\t\t\t\talign=\"center\">\r\n");
                sb.append("\t\t</el-table-column>\r\n");
            }

        }


        sb.append("\t\t<el-table-column\r\n");
        sb.append("\t\t\t\t:context=\"_self\"\r\n");
        sb.append("\t\t\t\twidth=\"150\"\r\n");
        sb.append("\t\t\t\tinline-template\r\n");
        sb.append("\t\t\t\tlabel=\"操作\"\r\n");
        sb.append("\t\t\t\talign=\"center\">\r\n");
        sb.append("\t\t\t<div>\r\n");
        sb.append("\t\t\t\t<el-button size=\"small\" @click=\"editRow(row)\">编辑</el-button>\r\n");
        sb.append("\t\t\t\t<el-button size=\"small\" type=\"danger\" @click=\"deleteRow(row)\">删除</el-button>\r\n");
        sb.append("\t\t\t</div>\r\n");
        sb.append("\t\t</el-table-column>\r\n");

        sb.append("\t</el-table>\r\n");

        sb.append("\t<div class=\"pagination-wrapper\" v-show=\"!loading\">\r\n");
        sb.append("\t\t<el-pagination layout=\"sizes, prev, pager, next\" :page-size=\"pageSize\" :total=\"totalNumber\"\r\n");
        sb.append("\t\t\t\t\t:page-size=\"pageSize\"\r\n");
        sb.append("\t\t\t\t\t:current-page=\"currentPage\" @current-change=\"pageChange\" @size-change=\"sizeChange\">\r\n");
        sb.append("\t\t</el-pagination>\r\n");
        sb.append("\t</div>\r\n");

        sb.append("</template>\r\n");


        sb.append("<jsp:include page=\"contentAfter.jsp\"/>\r\n");
        sb.append("</body>\r\n");


        sb.append("<script>\r\n");

        sb.append("\tfunction getQueryString(name) {\r\n");
        sb.append("\t\tvar reg = new RegExp(\"(^|&)\" + name + \"=([^&]*)(&|$)\", \"i\");\r\n");
        sb.append("\t\tvar r = window.location.search.substr(1).match(reg);\r\n");
        sb.append("\t\tif (r != null) return unescape(r[2]);\r\n");
        sb.append("\t\treturn null;\r\n");
        sb.append("\t}\r\n");

        sb.append("\tnew Vue({\r\n");
        sb.append("\t\tel: '#app',\r\n");
        sb.append("\t\tcreated: function () {\r\n");
        sb.append("\t\t\tthis.getRows();\r\n");
        sb.append("\t\t},\r\n");

        sb.append("\t\tdata: function () {\r\n");
        sb.append("\t\t\treturn {\r\n");
        sb.append("\t\t\t\tmenu: '0-0',\r\n");
        sb.append("\t\t\t\tuser: {\r\n");
        sb.append("\t\t\t\t\tid: '',\n\r\n");
        sb.append("\t\t\t\t\tusername: 'admin',\r\n");
        sb.append("\t\t\t\t\tavatar: ''\r\n");
        sb.append("\t\t\t\t},\r\n");
        sb.append("\t\t\t\tloading: false,\r\n");
        sb.append("\t\t\t\ttotalNumber: 0,\r\n");
        sb.append("\t\t\t\ttotalPage: 0,\r\n");
        sb.append("\t\t\t\tcurrentPage: 1,\r\n");
        sb.append("\t\t\t\tpageSize: 20,\r\n");
        sb.append("\t\t\t\trows: []\r\n");
        sb.append("\t\t\t};\r\n");
        sb.append("\t\t},\r\n");


        sb.append("\t\tmethods: {\r\n");
        sb.append("\t\t\tlogout: function () {\r\n");
        sb.append("\t\t\t\tthis.$confirm('确定要注销吗?', '提示', {\r\n");
        sb.append("\t\t\t\t\tconfirmButtonText: '确定',\r\n");
        sb.append("\t\t\t\t\tcancelButtonText: '取消',\r\n");
        sb.append("\t\t\t\t\ttype: 'info'\r\n");
        sb.append("\t\t\t\t}).then(function () {\r\n");
        sb.append("\t\t\t\t\tlocalStorage.removeItem('user');\r\n");
        sb.append("\t\t\t\t\tthis.$router.push({path: '/login'});\r\n");
        sb.append("\t\t\t\t}.bind(this)).catch(function (e) {\r\n");
        sb.append("\r\n");
        sb.append("\t\t\t\t});\r\n");
        sb.append("\t\t\t},\r\n");

        sb.append("\t\t\tgetRows: function () {\r\n");
        sb.append("\t\t\t\t$.get({\r\n");
        sb.append("\t\t\t\t\turl: '../" + downTableName + "/page',\r\n");
        sb.append("\t\t\t\t\tdata: {\r\n");
        sb.append("\t\t\t\t\t\tcurrentPage: this.currentPage,\r\n");
        sb.append("\t\t\t\t\t\tpageNumber: this.pageSize\r\n");
        sb.append("\t\t\t\t\t}\r\n");
        sb.append("\t\t\t\t}).then(function (res) {\r\n");
        sb.append("\t\t\t\t\tif (res.success) {\r\n");
        sb.append("\t\t\t\t\t\tthis.totalNumber = res.data.page.totalNumber;\r\n");
        sb.append("\t\t\t\t\t\tthis.rows = res.data.pp;\r\n");
        sb.append("\t\t\t\t\t}\r\n");
        sb.append("\t\t\t\t}.bind(this))\r\n");
        sb.append("\t\t\t},\r\n");


        sb.append("\t\t\tpageChange: function (page) {\r\n");
        sb.append("\t\t\t\tthis.currentPage = page;\r\n");
        sb.append("\t\t\t\tthis.getRows();\r\n");
        sb.append("\t\t\t},\r\n");

        sb.append("\t\t\tsizeChange: function (size) {\r\n");
        sb.append("\t\t\t\tthis.pageSize = size;\r\n");
        sb.append("\t\t\t\tthis.getRows();\r\n");
        sb.append("\t\t\t},\r\n");

        sb.append("\t\t\teditRow: function (row) {\r\n");
        sb.append("\t\t\t\twindow.location = '" + downTableName + "?" + StringUtil.underlineToCamel(pkName) + "=' + row." + StringUtil.underlineToCamel(pkName) + ";\r\n");
        sb.append("\t\t\t},\r\n");


        sb.append("\t\t\tdeleteRow: function (row) {\r\n");
        sb.append("\t\t\t\tthis.$confirm('确定要删除吗?', '提示', {\r\n");
        sb.append("\t\t\t\t\tconfirmButtonText: '删除',\r\n");
        sb.append("\t\t\t\t\tcancelButtonText: '取消',\r\n");
        sb.append("\t\t\t\t\ttype: 'warning'\r\n");
        sb.append("\t\t\t\t}).then(function () {\r\n");
        sb.append("\t\t\t\t\treturn $.post({\r\n");
        sb.append("\t\t\t\t\t\turl: '../" + downTableName + "/del',\r\n");
        sb.append("\t\t\t\t\t\tdata: {\r\n");
        sb.append("\t\t\t\t\t\t\t" + StringUtil.underlineToCamel(pkName) + ": row." + StringUtil.underlineToCamel(pkName) + "\r\n");
        sb.append("\t\t\t\t\t\t}\r\n");
        sb.append("\t\t\t\t\t})\r\n");
        sb.append("\t\t\t\t}.bind(this)).then(function (res) {\r\n");
        sb.append("\t\t\t\t\tif (res.success) {\r\n");
        sb.append("\t\t\t\t\t\tthis.$message.success('删除成功');\r\n");
        sb.append("\t\t\t\t\t\tthis.getRows();\r\n");
        sb.append("\t\t\t\t\t} else {\r\n");
        sb.append("\t\t\t\t\t\tthis.$message.error('删除失败');\r\n");
        sb.append("\t\t\t\t\t}\r\n");
        sb.append("\t\t\t\t}.bind(this)).catch(function () {\r\n");
        sb.append("\r\n");
        sb.append("\t\t\t\t});\r\n");
        sb.append("\t\t\t},\r\n");


        sb.append("\t\t\tcreate: function () {\r\n");
        sb.append("\t\t\t\twindow.location = '" + downTableName + "';\r\n");
        sb.append("\t\t\t}\r\n");


        sb.append("\t\t}\r\n");
        sb.append("\t})\r\n");
        sb.append("</script>\r\n");
        sb.append("</html>\r\n");


        return sb.toString();

    }

    private static void createVueByPageFile(TableParams tableParams, String pkName, String[] colnames, String[] colComments) {
        String jspByPageContent = parseVueByPage(tableParams.getTabelName(), pkName, colnames, colComments);

        try {
            File directory = new File("");

            String fileName = directory.getAbsolutePath() + "/" + changeToFolder(tableParams.getJspTargetPackage()) + StringUtil.underlineToCamel(tableParams.getTabelName()) + "s.vue";
            System.out.println("文件路径：" + fileName);
            FileWriter fw = new FileWriter(fileName);
            PrintWriter pw = new PrintWriter(fw);
            pw.println(jspByPageContent);
            pw.flush();
            pw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static String parseVueByPage(String tabelName, String pkName, String[] colnames, String[] colComments) {

        StringBuffer sb = new StringBuffer();


        String downTableName = StringUtil.underlineToCamel(tabelName);


        sb.append("<template>\r\n");
        sb.append("\t<div>\r\n");

        /**
         * 筛选部分
         */
        sb.append("\t\t<div class=\"filters-container\">\r\n");

        /**
         * 关键字筛选
         */
        sb.append("\t\t\t<el-input placeholder=\"关键字\" size=\"small\" v-model=\"filter1\" clearable class=\"filter-item\"></el-input>\r\n");

        /**
         * 下拉框筛选
         */
        sb.append("\t\t\t<el-select placeholder=\"选择框\" size=\"small\" v-model=\"filter2\" clearable class=\"filter-item\">\r\n");
        sb.append("\t\t\t\t<el-option\r\n");
        sb.append("\t\t\t\t\tlabel=\"值1\"\r\n");
        sb.append("\t\t\t\t\tvalue=\"item1\">\r\n");
        sb.append("\t\t\t\t</el-option>\r\n");
        sb.append("\t\t\t\t<el-option\r\n");
        sb.append("\t\t\t\t\tlabel=\"值2\"\r\n");
        sb.append("\t\t\t\t\tvalue=\"item1\">\r\n");
        sb.append("\t\t\t\t</el-option>\r\n");
        sb.append("\t\t\t</el-select>\r\n");


        sb.append("\t\t\t<el-button @click=\"getData\" type=\"primary\" size=\"small\" icon=\"el-icon-search\" class=\"filter-item\">搜索\r\n");
        sb.append("\t\t\t</el-button>\r\n");
        sb.append("\t\t\t<el-button @click=\"$router.push('/" + downTableName + "')\" type=\"primary\" size=\"small\" icon=\"el-icon-edit\"\r\n");
        sb.append("\t\t\t\t\t\tclass=\"filter-item\">添加\r\n");
        sb.append("\t\t\t</el-button>\r\n");

        sb.append("\t\t\t<el-dropdown trigger=\"click\" size=\"medium\" class=\"table-column-filter\">\r\n");
        sb.append("\t\t\t\t<span>\r\n");
        sb.append("\t\t\t\t筛选数据<i class=\"el-icon-arrow-down el-icon--right\"></i>\r\n");
        sb.append("\t\t\t\t</span>\r\n");
        sb.append("\t\t\t\t<el-dropdown-menu slot=\"dropdown\" class=\"table-column-filter-wrapper\">\r\n");
        sb.append("\t\t\t\t\t<el-checkbox v-for=\"item in tableColumns\" :key=\"item.value\" v-model=\"item.show\">{{item.label}}\r\n");
        sb.append("\t\t\t\t\t</el-checkbox>\r\n");
        sb.append("\t\t\t\t</el-dropdown-menu>\r\n");
        sb.append("\t\t\t</el-dropdown>\r\n");

        sb.append("\t\t</div>\r\n");


/**
 * table
 *
 */
        sb.append("\t\t<el-table\r\n");
        sb.append("\t\t\t:data=\"tableData\"\r\n");
        sb.append("\t\t\t:height=\"tableHeight\"\r\n");
        sb.append("\t\t\tref=\"table\">\r\n");
        sb.append("\t\t\t<el-table-column\r\n");
        sb.append("\t\t\t\tv-if=\"multipleMode\"\r\n");
        sb.append("\t\t\t\talign=\"center\"\r\n");
        sb.append("\t\t\t\ttype=\"selection\"\r\n");
        sb.append("\t\t\t\twidth=\"50\">\r\n");
        sb.append("\t\t\t</el-table-column>\r\n");

        sb.append("\t\t\t<el-table-column\r\n");
        sb.append("\t\t\t\ttype=\"index\"\r\n");
        sb.append("\t\t\t\tmin-width=\"50\"\r\n");
        sb.append("\t\t\t\talign=\"center\">\r\n");
        sb.append("\t\t\t</el-table-column>\r\n");


        for (int i = 0; i < colnames.length; i++) {

            if (!pkName.equalsIgnoreCase(colnames[i])) {

                sb.append("\t\t\t<el-table-column\r\n");
                sb.append("\t\t\t\tv-if=\"isColumnShow('" + StringUtil.underlineToCamel(colnames[i]) + "')\"\r\n");
                sb.append("\t\t\t\tprop=\"" + StringUtil.underlineToCamel(colnames[i]) + "\"\r\n");
                sb.append("\t\t\t\tlabel=\"" + (StringUtils.isNotEmpty(colComments[i]) ? colComments[i].split(":")[0] : colnames[i]) + "\"\r\n");
                sb.append("\t\t\t\tmin-width=\"300\">\r\n");
                sb.append("\t\t</el-table-column>\r\n");
            }

        }


        sb.append("\t\t\t<el-table-column\r\n");
        sb.append("\t\t\t\tlabel=\"操作\"\r\n");
        sb.append("\t\t\t\talign=\"center\"\r\n");
        sb.append("\t\t\t\tfixed=\"right\">\r\n");
        sb.append("\t\t\t\t<template slot-scope=\"scope\">\r\n");
        sb.append("\t\t\t\t\t<el-button @click=\"editRow(scope.row)\" type=\"primary\" size=\"mini\" plain>编辑</el-button>\r\n");
        sb.append("\t\t\t\t</template>\r\n");
        sb.append("\t\t\t</el-table-column>\r\n");

        sb.append("\t\t</el-table>\r\n");


        /**
         * 翻页和批量操作
         */


        sb.append("\t\t<div class=\"pagination-wrapper\">\r\n");

        sb.append("\t\t\t<div class=\"multiple-mode-wrapper\">\r\n");
        sb.append("\t\t\t\t<el-button size=\"small\" v-if=\"!multipleMode\" @click=\"toggleMultipleMode(true)\">批量编辑</el-button>\r\n");
        sb.append("\t\t\t\t<el-button-group v-else>\r\n");
        sb.append("\t\t\t\t\t<el-button size=\"small\" @click=\"operation1\">批量操作1</el-button>\r\n");
        sb.append("\t\t\t\t\t<el-button size=\"small\" @click=\"operation2\">批量操作2</el-button>\r\n");
        sb.append("\t\t\t\t\t<el-button size=\"small\" @click=\"toggleMultipleMode(false)\">取消</el-button>\r\n");
        sb.append("\t\t\t\t</el-button-group>\r\n");
        sb.append("\t\t\t</div>\r\n");

        sb.append("\t\t\t<el-pagination\r\n");
        sb.append("\t\t\t\tbackground\r\n");
        sb.append("\t\t\t\t@size-change=\"pageSizeChange\"\r\n");
        sb.append("\t\t\t\t@current-change=\"currentPageChange\"\r\n");
        sb.append("\t\t\t\t:current-page=\"currentPage\"\r\n");
        sb.append("\t\t\t\t:page-sizes=\"[10, 20, 30, 40, 50]\"\r\n");
        sb.append("\t\t\t\t:page-size=\"pageSize\"\r\n");
        sb.append("\t\t\t\tlayout=\"total, sizes, prev, pager, next, jumper\"\r\n");
        sb.append("\t\t\t\t:total=\"totalNumber\">\r\n");
        sb.append("\t\t\t</el-pagination>\r\n");

        sb.append("\t\t</div>\r\n");
        sb.append("\t</div>\r\n");
        sb.append("</template>\r\n");


        /**
         * script
         */
        sb.append("<script>\r\n");

        sb.append("\timport {mapState} from 'vuex'\r\n");

        sb.append("\r\n");
        sb.append("\texport default {\r\n");

        /**
         * created
         */
        sb.append("\t\tcreated() {\r\n");
        sb.append("\t\t\tthis.getData();\r\n");
        sb.append("\t\t},\r\n");

        /**
         * data
         */
        sb.append("\t\tdata() {\r\n");
        sb.append("\t\t\treturn {\r\n");
        sb.append("\t\t\t\ttotalNumber: 0,\r\n");
        sb.append("\t\t\t\ttotalPage: 0,\r\n");
        sb.append("\t\t\t\tcurrentPage: 1,\r\n");
        sb.append("\t\t\t\tpageSize: 20,\r\n");
        sb.append("\t\t\t\ttableData: [],\r\n");
        sb.append("\t\t\t\tfilter1: '',\r\n");
        sb.append("\t\t\t\tfilter2: '',\r\n");

        sb.append("\t\t\t\ttableColumns: [\r\n");

        for (int i = 0; i < colnames.length; i++) {

            if (!pkName.equalsIgnoreCase(colnames[i])) {

                sb.append("\t\t\t\t{\r\n");
                sb.append("\t\t\t\t\tlabel: '" + (StringUtils.isNotEmpty(colComments[i]) ? colComments[i].split(":")[0] : colnames[i]) + "',\r\n");
                sb.append("\t\t\t\t\tvalue: '" + StringUtil.underlineToCamel(colnames[i]) + "',\r\n");
                sb.append("\t\t\t\t\tshow: true\r\n");
                sb.append("\t\t\t\t},\r\n");
            }

        }

        sb.append("\t\t\t\t],\r\n");
        sb.append("\t\t\t\tmultipleMode: false\r\n");
        sb.append("\t\t\t}\r\n");
        sb.append("\t\t},\r\n");


        /**
         * computed
         */
        sb.append("\t\tcomputed: {\r\n");
        sb.append("\t\t\t...mapState(['tableHeight']),\r\n");
        sb.append("\t\t\tselection() {\r\n");
        sb.append("\t\t\t\treturn this.$refs.table.selection.map(i => i.id);\r\n");
        sb.append("\t\t\t}\r\n");
        sb.append("\t\t},\r\n");

        /**
         * methods
         */
        sb.append("\t\tmethods: {\r\n");

        /**
         * pageSizeChange
         */
        sb.append("\t\t\tpageSizeChange(size) {\r\n");
        sb.append("\t\t\t\tthis.pageSize = size;\r\n");
        sb.append("\t\t\t\tthis.getData();\r\n");
        sb.append("\t\t\t},\r\n");


        /**
         * currentPageChange
         */
        sb.append("\t\t\tcurrentPageChange(page) {\r\n");
        sb.append("\t\t\t\tthis.currentPage = page;\r\n");
        sb.append("\t\t\t\tthis.getData();\r\n");
        sb.append("\t\t\t},\r\n");

        /**
         * getData
         */
        sb.append("\t\t\tgetData() {\r\n");
        sb.append("\t\t\t\tthis.$http.get({\r\n");
        sb.append("\t\t\t\t\turl: '/" + downTableName + "/page',\r\n");
        sb.append("\t\t\t\t\tdata: {\r\n");
        sb.append("\t\t\t\t\t\tcurrentPage: this.currentPage,\r\n");
        sb.append("\t\t\t\t\t\tpageNumber: this.pageSize\r\n");
        sb.append("\t\t\t\t\t}\r\n");
        sb.append("\t\t\t\t}).then(res => {\r\n");
        sb.append("\t\t\t\t\tif (res.success) {\r\n");
        sb.append("\t\t\t\t\t\tthis.totalNumber = res.data.page.totalNumber;\r\n");
        sb.append("\t\t\t\t\t\tthis.tableData = res.data.pp;\r\n");
        sb.append("\t\t\t\t\t}\r\n");
        sb.append("\t\t\t\t})\r\n");
        sb.append("\t\t\t},\r\n");


        /**
         * isColumnShow
         */
        sb.append("\t\t\tisColumnShow(column) {\r\n");
        sb.append("\t\t\t\tvar row = this.tableColumns.find(i => i.value === column);\r\n");
        sb.append("\t\t\t\treturn row ? row.show : false;\r\n");
        sb.append("\t\t\t},\r\n");

        /**
         * toggleMultipleMode
         */
        sb.append("\t\t\ttoggleMultipleMode(multipleMode) {\r\n");
        sb.append("\t\t\t\tthis.multipleMode = multipleMode;\r\n");
        sb.append("\t\t\t\tif (!multipleMode) {\r\n");
        sb.append("\t\t\t\t\tthis.$refs.table.clearSelection();\r\n");
        sb.append("\t\t\t\t}\r\n");
        sb.append("\t\t\t},\r\n");


        /**
         * editRow
         */
        sb.append("\t\t\teditRow(row) {\r\n");
        sb.append("\t\t\t\tthis.$router.push({\r\n");
        sb.append("\t\t\t\t\tpath: '/" + downTableName + "',\r\n");
        sb.append("\t\t\t\t\tquery: {\r\n");
        sb.append("\t\t\t\t\t\tid: row.id\r\n");
        sb.append("\t\t\t\t\t}\r\n");
        sb.append("\t\t\t\t})\r\n");
        sb.append("\t\t\t},\r\n");


        /**
         * operation1
         */
        sb.append("\t\t\toperation1() {\r\n");
        sb.append("\t\t\t\tthis.$notify({\r\n");
        sb.append("\t\t\t\t\ttitle: '提示',\r\n");
        sb.append("\t\t\t\t\tmessage: this.selection\r\n");
        sb.append("\t\t\t\t});\r\n");
        sb.append("\t\t\t},\r\n");


        /**
         * operation2
         */
        sb.append("\t\t\toperation2() {\r\n");
        sb.append("\t\t\t\tthis.$message('操作2');\r\n");
        sb.append("\t\t\t}\r\n");


        sb.append("\t\t}\r\n");
        sb.append("\t}\r\n");


        sb.append("</script>\r\n");
        sb.append("<style lang=\"less\" scoped>\r\n");
        sb.append("\r\n");
        sb.append("</style>\r\n");

        sb.append("\r\n");

        return sb.toString();

    }

    private static void createDaoClassFile(TableParams tableParams) {

        String daoClassContent = parseDaoClass(tableParams);

        try {
            File directory = new File("");

            String fileName = directory.getAbsolutePath() + "/src/main/java/" + changeToFolder(tableParams.getDaoTargetPackage()) + StringUtil.initcap(tableParams.getTabelName()) + "Mapper.java";
            System.out.println("文件路径：" + fileName);
            FileWriter fw = new FileWriter(fileName);
            PrintWriter pw = new PrintWriter(fw);
            pw.println(daoClassContent);
            pw.flush();
            pw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static String parseDaoClass(TableParams tableParams) {

        StringBuffer sb = new StringBuffer();


        sb.append("package " + tableParams.getDaoTargetPackage() + ";\r\n");
        sb.append("\r\n");

        sb.append("import java.util.List;\r\n");
        sb.append("import java.util.Map;\r\n");


        sb.append("\r\n");

        sb.append("import org.springframework.stereotype.Repository;\r\n");

        sb.append("\r\n");

        sb.append("import " + tableParams.getModelTargetPackage() + "." + StringUtil.initcap(tableParams.getTabelName()) + ";\r\n");
        sb.append("\r\n");
        //注释部分
        sb.append("/**\r\n");
        sb.append(" * " + tableParams.getTabelName() + " Dao接口\r\n");
        sb.append(" * " + new Date() + "  Suo Chen Cheng\r\n");
        sb.append(" */ \r\n");

        //注解
        sb.append("@Repository(\"" + tableParams.getDaoTargetPackage() + "." + StringUtil.initcap(tableParams.getTabelName()) + "Mapper\")\r\n");

        //实体部分
        sb.append("public interface " + StringUtil.initcap(tableParams.getTabelName()) + "Mapper {\r\n");

        sb.append("\r\n");


        sb.append("\tint deleteByPrimaryKey(Integer id);\r\n");
        sb.append("\r\n");
        sb.append("\tint insertSelective(" + StringUtil.initcap(tableParams.getTabelName()) + " record);\r\n");
        sb.append("\r\n");
        sb.append("\t" + StringUtil.initcap(tableParams.getTabelName()) + " selectByPrimaryKey(Integer id);\r\n");
        sb.append("\r\n");
        sb.append("\tint updateByPrimaryKeySelective(" + StringUtil.initcap(tableParams.getTabelName()) + " record);\r\n");
        sb.append("\r\n");
        sb.append("\tList <" + StringUtil.initcap(tableParams.getTabelName()) + "> queryAll" + StringUtil.initcap(tableParams.getTabelName()) + "(" + StringUtil.initcap(tableParams.getTabelName()) + " record);\r\n");
        sb.append("\r\n");
        sb.append("\tList <" + StringUtil.initcap(tableParams.getTabelName()) + "> query" + StringUtil.initcap(tableParams.getTabelName()) + "sByPage(Map <String, Object> parameter);\r\n");
        sb.append("\r\n");
        sb.append("\tint delete(String id);\r\n");
        sb.append("\r\n");
        sb.append("\t" + StringUtil.initcap(tableParams.getTabelName()) + " query" + StringUtil.initcap(tableParams.getTabelName()) + "(" + StringUtil.initcap(tableParams.getTabelName()) + " record);\r\n");


        sb.append("}\r\n");

        //System.out.println(sb.toString());
        return sb.toString();
    }

    /**
     * 生成 dao XML
     *
     * @param tableParams
     * @param colnames
     * @param colTypes
     * @param pkName
     */
    private static void createDaoXmlFile(TableParams tableParams, String[] colnames, String[] colTypes, String pkName) {

        String daoXmlContent = parseDaoXml(colnames, colTypes, tableParams, pkName);

        try {
            File directory = new File("");

            String fileName = directory.getAbsolutePath() + "/src/main/java/" + changeToFolder(tableParams.getDaoTargetPackage()) + StringUtil.initcap(tableParams.getTabelName()) + "Mapper.xml";
            System.out.println("文件路径：" + fileName);
            FileWriter fw = new FileWriter(fileName);
            PrintWriter pw = new PrintWriter(fw);
            pw.println(daoXmlContent);
            pw.flush();
            pw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 解析 数据层 xml
     *
     * @param colnames
     * @param colTypes
     * @param tableParams
     * @param pkName
     * @return
     */
    private static String parseDaoXml(String[] colnames, String[] colTypes, TableParams tableParams, String pkName) {
        StringBuffer sb = new StringBuffer();


        sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\r\n");
        sb.append("<!DOCTYPE mapper PUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\" \"http://mybatis.org/dtd/mybatis-3-mapper.dtd\" >\r\n");

        sb.append("<mapper namespace=\"" + tableParams.getDaoTargetPackage() + "." + StringUtil.initcap(tableParams.getTabelName()) + "Mapper\" >\r\n");


        parseDaoBaseResultMap(sb, colnames, colTypes, pkName, tableParams);
        parseDaoBaseColumnList(sb, colnames);
        parseDaoSelectByPrimaryKey(sb, pkName, tableParams.getTabelName());
        parseDaoDeleteByPrimaryKey(sb, pkName, tableParams.getTabelName());
        parseDaoInsertSelective(sb, pkName, tableParams, colnames, colTypes);
        parseDaoUpdateByPrimaryKeySelective(sb, pkName, tableParams, colnames, colTypes);
        parseDaoQueryByPage(sb, pkName, tableParams, colnames);
        parseDaoQueryAll(sb, pkName, tableParams, colnames);
        parseDaoQueryOne(sb, tableParams, colnames);
        parseDaoDelete(sb, tableParams.getTabelName(), pkName);

        sb.append("</mapper>\r\n");
        return sb.toString();
    }

    /**
     * 逻辑删除
     *
     * @param sb
     * @param tabelName
     * @param pkName
     */
    private static void parseDaoDelete(StringBuffer sb, String tabelName, String pkName) {
        sb.append("\t<update id=\"delete\">\r\n");
        sb.append("\t\tUPDATE " + tabelName + " SET del_flag = 'Y'\r\n");
        sb.append("\t\t<where>\r\n");
        sb.append("\t\t\tAND " + pkName + " = #{" + StringUtil.underlineToCamel(pkName) + "}\r\n");
        sb.append("\t\t</where>\r\n");
        sb.append("\t</update>\r\n");

    }

    /**
     * 解析，查询一个
     *
     * @param sb
     * @param tableParams
     * @param colnames
     */
    private static void parseDaoQueryOne(StringBuffer sb, TableParams tableParams, String[] colnames) {
        sb.append("\t<select id=\"query" + StringUtil.initcap(tableParams.getTabelName()) + "\" parameterType=\"java.util.Map\" resultType=\"" + tableParams.getModelTargetPackage() + "." + StringUtil.initcap(tableParams.getTabelName()) + "\">\r\n");
        sb.append("\t\tselect <include refid=\"Base_Column_List\"/> from " + tableParams.getTabelName() + "\r\n");
        sb.append("\t\t<where>\r\n");
        sb.append("\t\t\tand del_flag = 'N'\r\n");


        for (int i = 0; i < colnames.length; i++) {


            sb.append("\t\t\t<if test=\"" + StringUtil.underlineToCamel(colnames[i]) + " != null and !&quot;&quot;.equals(" + StringUtil.underlineToCamel(colnames[i]) + ")\">\r\n");
            sb.append("\t\t\t\tand " + colnames[i] + " = #{" + StringUtil.underlineToCamel(colnames[i]) + "}\t\n");
            sb.append("\t\t\t</if>\r\n");

        }


        sb.append("\t\t</where>\r\n");
        sb.append("\t\tLIMIT 1\r\n");
        sb.append("\t</select>\r\n");
    }

    /**
     * 解析，查询所有
     *
     * @param sb
     * @param pkName
     * @param tableParams
     * @param colnames
     */
    private static void parseDaoQueryAll(StringBuffer sb, String pkName, TableParams tableParams, String[] colnames) {

        sb.append("\t<select id=\"queryAll" + StringUtil.initcap(tableParams.getTabelName()) + "\" parameterType=\"java.util.Map\" resultType=\"" + tableParams.getModelTargetPackage() + "." + StringUtil.initcap(tableParams.getTabelName()) + "\">\r\n");
        sb.append("\t\tselect <include refid=\"Base_Column_List\"/> from " + tableParams.getTabelName() + "\r\n");
        sb.append("\t\t<where>\r\n");
        sb.append("\t\t\tand del_flag = 'N'\r\n");


        for (int i = 0; i < colnames.length; i++) {


            sb.append("\t\t\t<if test=\"" + StringUtil.underlineToCamel(colnames[i]) + " != null and !&quot;&quot;.equals(" + StringUtil.underlineToCamel(colnames[i]) + ")\">\r\n");
            sb.append("\t\t\t\tand " + colnames[i] + " = #{" + StringUtil.underlineToCamel(colnames[i]) + "}\t\n");
            sb.append("\t\t\t</if>\r\n");

        }


        sb.append("\t\t</where>\r\n");
        sb.append("\t\torder by " + pkName + " desc\r\n");
        sb.append("\t</select>\r\n");
    }

    /**
     * 解析，翻页查询
     *
     * @param sb
     * @param pkName
     * @param tableParams
     * @param colnames
     */
    private static void parseDaoQueryByPage(StringBuffer sb, String pkName, TableParams tableParams, String[] colnames) {

        sb.append("\t<select id=\"query" + StringUtil.initcap(tableParams.getTabelName()) + "sByPage\" parameterType=\"java.util.Map\" resultType=\"" + tableParams.getModelTargetPackage() + "." + StringUtil.initcap(tableParams.getTabelName()) + "\">\r\n");
        sb.append("\t\tselect <include refid=\"Base_Column_List\"/> from " + tableParams.getTabelName() + "\r\n");
        sb.append("\t\t<where>\r\n");
        sb.append("\t\t\tand del_flag = 'N'\r\n");


        for (int i = 0; i < colnames.length; i++) {


            sb.append("\t\t\t<if test=\"record." + StringUtil.underlineToCamel(colnames[i]) + " != null and !&quot;&quot;.equals(record." + StringUtil.underlineToCamel(colnames[i]) + ")\">\r\n");
            sb.append("\t\t\t\tand " + colnames[i] + " = #{record." + StringUtil.underlineToCamel(colnames[i]) + "}\t\n");
            sb.append("\t\t\t</if>\r\n");

        }


        sb.append("\t\t</where>\r\n");
        sb.append("\t\torder by " + pkName + " desc\r\n");
        sb.append("\t</select>\r\n");

    }

    /**
     * 解析，根据主键更新字段
     *
     * @param sb
     * @param pkName
     * @param tableParams
     * @param colnames
     * @param colTypes
     */
    private static void parseDaoUpdateByPrimaryKeySelective(StringBuffer sb, String pkName, TableParams tableParams, String[] colnames, String[] colTypes) {
        sb.append("\t<update id=\"updateByPrimaryKeySelective\" parameterType=\"" + tableParams.getModelTargetPackage() + "." + StringUtil.initcap(tableParams.getTabelName()) + "\" >\r\n");
        sb.append("\t\tupdate " + tableParams.getTabelName() + "\r\n");
        sb.append("\t\t<set >\r\n");


        for (int i = 0; i < colnames.length; i++) {

            if (!pkName.equalsIgnoreCase(colnames[i])) {

                sb.append("\t\t\t<if test=\"" + StringUtil.underlineToCamel(colnames[i]) + " != null\" >\r\n");
                sb.append("\t\t\t\t" + colnames[i] + " = #{" + StringUtil.underlineToCamel(colnames[i]) + ",jdbcType=" + StringUtil.sqlType2JdbcType(colTypes[i]) + "},\t\n");
                sb.append("\t\t\t</if>\r\n");
            }

        }

        sb.append("\t\t</set>\r\n");
        sb.append("\t\twhere " + pkName + " = #{" + StringUtil.underlineToCamel(pkName) + ",jdbcType=INTEGER}\r\n");
        sb.append("\t</update>\r\n");
    }

    /**
     * 解析，插入数据
     *
     * @param sb
     * @param pkName
     * @param tableParams
     * @param colnames
     * @param colTypes
     */
    private static void parseDaoInsertSelective(StringBuffer sb, String pkName, TableParams tableParams, String[] colnames, String[] colTypes) {
        sb.append("\t<insert id=\"insertSelective\" parameterType=\"" + tableParams.getModelTargetPackage() + "." + StringUtil.initcap(tableParams.getTabelName()) + "\" useGeneratedKeys=\"true\" keyProperty=\"" + StringUtil.underlineToCamel(pkName) + "\">\r\n");
        sb.append("\t\tinsert into " + tableParams.getTabelName() + "\r\n");
        sb.append("\t\t<trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\" >\r\n");


        for (int i = 0; i < colnames.length; i++) {

            sb.append("\t\t\t<if test=\"" + StringUtil.underlineToCamel(colnames[i]) + " != null\" >\r\n");
            sb.append("\t\t\t\t" + colnames[i] + ",\t\n");
            sb.append("\t\t\t</if>\r\n");

        }

        sb.append("\t\t</trim>\r\n");
        sb.append("\t\t<trim prefix=\"values (\" suffix=\")\" suffixOverrides=\",\" >\r\n");
        for (int i = 0; i < colnames.length; i++) {

            sb.append("\t\t\t<if test=\"" + StringUtil.underlineToCamel(colnames[i]) + " != null\" >\r\n");
            sb.append("\t\t\t\t#{" + StringUtil.underlineToCamel(colnames[i]) + ",jdbcType=" + StringUtil.sqlType2JdbcType(colTypes[i]) + "},\t\n");
            sb.append("\t\t\t</if>\r\n");

        }

        sb.append("\t\t</trim>\r\n");
        sb.append("\t</insert>\r\n");

    }

    /**
     * 解析，根据主键删除
     *
     * @param sb
     * @param pkName
     * @param tabelName
     */
    private static void parseDaoDeleteByPrimaryKey(StringBuffer sb, String pkName, String tabelName) {

        sb.append("\t<delete id=\"deleteByPrimaryKey\" parameterType=\"java.lang.Integer\" >\r\n");
        sb.append("\t\tdelete from " + tabelName + "\r\n");
        sb.append("\t\twhere " + pkName + " = #{" + StringUtil.underlineToCamel(pkName) + ",jdbcType=INTEGER}\r\n");
        sb.append("\t</delete>\r\n");
    }

    /**
     * 解析根据主键查询
     *
     * @param sb
     * @param pkName
     * @param tabelName
     */
    private static void parseDaoSelectByPrimaryKey(StringBuffer sb, String pkName, String tabelName) {
        sb.append("\t<select id=\"selectByPrimaryKey\" resultMap=\"BaseResultMap\" parameterType=\"java.lang.Integer\" >\r\n");
        sb.append("\t\tselect\r\n");
        sb.append("\t\t<include refid=\"Base_Column_List\" />\r\n");
        sb.append("\t\tfrom " + tabelName + "\r\n");
        sb.append("\t\twhere " + pkName + " = #{" + StringUtil.underlineToCamel(pkName) + ",jdbcType=INTEGER}\r\n");
        sb.append("\t</select>\r\n");

    }

    /**
     * 解析baseColumnList
     *
     * @param sb
     * @param colnames
     */
    private static void parseDaoBaseColumnList(StringBuffer sb, String[] colnames) {

        sb.append("\t<sql id=\"Base_Column_List\" >\r\n");
        sb.append("\t\t");
        for (int i = 0; i < colnames.length; i++) {


            if (i == colnames.length - 1) {

                sb.append(" " + colnames[i] + "\r\n");
            } else {

                sb.append(" " + colnames[i] + ",");
            }

        }

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

    }

    /**
     * 解析BaseResultMap
     *
     * @param sb
     * @param colnames
     * @param colTypes
     * @param pkName
     * @param tableParams
     */
    private static void parseDaoBaseResultMap(StringBuffer sb, String[] colnames, String[] colTypes, String pkName, TableParams tableParams) {
        sb.append("\t<resultMap id=\"BaseResultMap\" type=\"" + tableParams.getModelTargetPackage() + "." + StringUtil.initcap(tableParams.getTabelName()) + "\" >\r\n");
        sb.append("\t\t<id column=\"" + pkName + "\" property=\"" + StringUtil.underlineToCamel(pkName) + "\" jdbcType=\"INTEGER\" />\r\n");
        for (int i = 0; i < colnames.length; i++) {

            if (!pkName.equalsIgnoreCase(colnames[i])) {

                sb.append("\t\t<result column=\"" + colnames[i] + "\" property=\"" + StringUtil.underlineToCamel(colnames[i]) + "\" jdbcType=\"" + StringUtil.sqlType2JdbcType(colTypes[i]) + "\" />\r\n");
            }

        }

        sb.append("\t</resultMap>\r\n");
    }

    private static void createServiceFile(TableParams tableParams, String pkName) {
        String serviceContent = parseService(tableParams, pkName);

        try {
            File directory = new File("");

            String fileName = directory.getAbsolutePath() + "/src/main/java/" + changeToFolder(tableParams.getServiceTargetPackage()) + StringUtil.initcap(tableParams.getTabelName()) + "Service.java";
            System.out.println("文件路径：" + fileName);
            FileWriter fw = new FileWriter(fileName);
            PrintWriter pw = new PrintWriter(fw);
            pw.println(serviceContent);
            pw.flush();
            pw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 生成service interface
     *
     * @param tableParams
     * @param pkName
     * @return
     */
    private static String parseService(TableParams tableParams, String pkName) {

        StringBuffer sb = new StringBuffer();


        sb.append("package " + tableParams.getServiceTargetPackage() + ";\r\n");
        sb.append("\r\n");

        sb.append("import java.util.List;\r\n");

        sb.append("\r\n");

        sb.append("import " + tableParams.getBasePackage() + ".dto.Page;\r\n");
        sb.append("import " + tableParams.getModelTargetPackage() + "." + StringUtil.initcap(tableParams.getTabelName()) + ";\r\n");

        sb.append("\r\n");

        //注释部分
        sb.append("/**\r\n");
        sb.append(" * " + tableParams.getTabelName() + " service接口类\r\n");
        sb.append(" * " + new Date() + "  Suo Chen Cheng\r\n");
        sb.append(" */ \r\n");

        //实体部分
        sb.append("public interface " + StringUtil.initcap(tableParams.getTabelName()) + "Service {\r\n");
        sb.append("\r\n");


        sb.append("\tList <" + StringUtil.initcap(tableParams.getTabelName()) + "> get" + StringUtil.initcap(tableParams.getTabelName()) + "List(" + StringUtil.initcap(tableParams.getTabelName()) + " record);\r\n");
        sb.append("\r\n");

        sb.append("\tList <" + StringUtil.initcap(tableParams.getTabelName()) + "> get" + StringUtil.initcap(tableParams.getTabelName()) + "ByPage(Page page, " + StringUtil.initcap(tableParams.getTabelName()) + " record);\r\n");
        sb.append("\r\n");

        sb.append("\t" + StringUtil.initcap(tableParams.getTabelName()) + " get" + StringUtil.initcap(tableParams.getTabelName()) + "ById(String " + StringUtil.underlineToCamel(pkName) + ");\r\n");
        sb.append("\r\n");

        sb.append("\t" + StringUtil.initcap(tableParams.getTabelName()) + " get" + StringUtil.initcap(tableParams.getTabelName()) + "(" + StringUtil.initcap(tableParams.getTabelName()) + " record);\r\n");
        sb.append("\r\n");

        sb.append("\tboolean create" + StringUtil.initcap(tableParams.getTabelName()) + "(" + StringUtil.initcap(tableParams.getTabelName()) + " record);\r\n");
        sb.append("\r\n");

        sb.append("\tboolean delete" + StringUtil.initcap(tableParams.getTabelName()) + "(String " + StringUtil.underlineToCamel(pkName) + ");\r\n");
        sb.append("\r\n");

        sb.append("\tboolean update" + StringUtil.initcap(tableParams.getTabelName()) + "(" + StringUtil.initcap(tableParams.getTabelName()) + " record);\r\n");
        sb.append("\r\n");

        sb.append("}\r\n");

        return sb.toString();

    }

    private static void createServiceImplFile(TableParams tableParams, String pkName) {
        String serviceImplContent = parseServiceImpl(tableParams, pkName);

        try {
            File directory = new File("");

            String fileName = directory.getAbsolutePath() + "/src/main/java/" + changeToFolder(tableParams.getImplTargetPackage()) + StringUtil.initcap(tableParams.getTabelName()) + "ServiceImpl.java";
            System.out.println("文件路径：" + fileName);
            FileWriter fw = new FileWriter(fileName);
            PrintWriter pw = new PrintWriter(fw);
            pw.println(serviceImplContent);
            pw.flush();
            pw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static String parseServiceImpl(TableParams tableParams, String pkName) {
        StringBuffer sb = new StringBuffer();

        String upTableName = StringUtil.initcap(tableParams.getTabelName());
        String downTableName = StringUtil.underlineToCamel(tableParams.getTabelName());

        sb.append("package " + tableParams.getImplTargetPackage() + ";\r\n");
        sb.append("\r\n");

        sb.append("import java.util.HashMap;\r\n");
        sb.append("import java.util.List;\r\n");
        sb.append("import java.util.Map;\r\n");

        sb.append("\r\n");

        sb.append("import org.apache.log4j.Logger;\r\n");
        sb.append("import org.springframework.beans.factory.annotation.Autowired;\r\n");
        sb.append("import org.springframework.stereotype.Service;\r\n");
        sb.append("\r\n");


        sb.append("import " + tableParams.getBasePackage() + ".constant.AppConstant;\r\n");
        sb.append("import " + tableParams.getDaoTargetPackage() + "." + upTableName + "Mapper;\r\n");
        sb.append("import " + tableParams.getBasePackage() + ".dto.Page;\r\n");
        sb.append("import " + tableParams.getModelTargetPackage() + "." + upTableName + ";\r\n");
        sb.append("import " + tableParams.getServiceTargetPackage() + "." + upTableName + "Service;\r\n");

        sb.append("\r\n");

        //注释部分
        sb.append("/**\r\n");
        sb.append(" * " + tableParams.getTabelName() + " service接口实现类\r\n");
        sb.append(" * " + new Date() + "  Suo Chen Cheng\r\n");
        sb.append(" */ \r\n");

        //实体部分
        sb.append("@Service\r\n");
        sb.append("public class " + upTableName + "ServiceImpl implements " + upTableName + "Service {\r\n");
        sb.append("\r\n");


        //日志部分
        sb.append("\tprivate static Logger   logger = Logger.getLogger(" + upTableName + "ServiceImpl.class);\r\n");
        sb.append("\r\n");


        //引入mapper
        sb.append("\t@Autowired\r\n");
        sb.append("\tprivate " + upTableName + "Mapper " + downTableName + "Mapper;\r\n");
        sb.append("\r\n");


        //get ALL
        sb.append("\t@Override\r\n");
        sb.append("\tpublic List <" + upTableName + "> get" + upTableName + "List(" + upTableName + " record) {\r\n");
        sb.append("\r\n");
        sb.append("\t\tlogger.info(\"get" + upTableName + "List\");\r\n");
        sb.append("\t\ttry {\r\n");
        sb.append("\r\n");
        sb.append("\t\t\treturn " + downTableName + "Mapper.queryAll" + upTableName + "(record);\r\n");
        sb.append("\t\t} catch (Exception e) {\r\n");
        sb.append("\t\t\tlogger.error(\"get" + upTableName + "List\", e);\r\n");
        sb.append("\t\t}\r\n");
        sb.append("\r\n");
        sb.append("\t\treturn null;\r\n");
        sb.append("\t}\r\n");
        sb.append("\r\n");

        //ByPage
        sb.append("\t@Override\r\n");
        sb.append("\tpublic List <" + upTableName + "> get" + upTableName + "ByPage(Page page, " + upTableName + " record) {\r\n");
        sb.append("\r\n");
        sb.append("\t\tlogger.info(\"get" + upTableName + "ByPage\");\r\n");
        sb.append("\t\ttry {\r\n");
        sb.append("\r\n");
        sb.append("\t\t\tMap <String, Object> parameter = new HashMap <String, Object>();\r\n");
        sb.append("\t\t\tparameter.put(\"record\", record);\r\n");
        sb.append("\t\t\tparameter.put(AppConstant.PAGE, page);\r\n");
        sb.append("\r\n");
        sb.append("\t\t\treturn " + downTableName + "Mapper.query" + upTableName + "sByPage(parameter);\r\n");
        sb.append("\t\t} catch (Exception e) {\r\n");
        sb.append("\t\t\tlogger.error(\"get" + upTableName + "ByPage\", e);\r\n");
        sb.append("\t\t}\r\n");
        sb.append("\r\n");
        sb.append("\t\treturn null;\r\n");
        sb.append("\t}\r\n");
        sb.append("\r\n");


        //selectByPrimaryKey
        sb.append("\t@Override\r\n");
        sb.append("\tpublic " + upTableName + " get" + upTableName + "ById(String " + StringUtil.underlineToCamel(pkName) + ") {\r\n");
        sb.append("\r\n");
        sb.append("\t\tlogger.info(\"get" + upTableName + "ById\");\r\n");
        sb.append("\t\ttry {\r\n");
        sb.append("\r\n");
        sb.append("\t\t\treturn " + downTableName + "Mapper.selectByPrimaryKey(Integer.valueOf(" + StringUtil.underlineToCamel(pkName) + "));\r\n");
        sb.append("\t\t} catch (Exception e) {\r\n");
        sb.append("\t\t\tlogger.error(\"get" + upTableName + "ById\", e);\r\n");
        sb.append("\t\t}\r\n");
        sb.append("\r\n");
        sb.append("\t\treturn null;\r\n");
        sb.append("\t}\r\n");
        sb.append("\r\n");


        //getOne
        sb.append("\t@Override\r\n");
        sb.append("\tpublic " + upTableName + " get" + upTableName + "(" + upTableName + " record) {\r\n");
        sb.append("\r\n");
        sb.append("\t\tlogger.info(\"get" + upTableName + "\");\r\n");
        sb.append("\t\ttry {\r\n");
        sb.append("\r\n");
        sb.append("\t\t\treturn " + downTableName + "Mapper.query" + upTableName + "(record);\r\n");
        sb.append("\t\t} catch (Exception e) {\r\n");
        sb.append("\t\t\tlogger.error(\"get" + upTableName + "\", e);\r\n");
        sb.append("\t\t}\r\n");
        sb.append("\r\n");
        sb.append("\t\treturn null;\r\n");
        sb.append("\t}\r\n");
        sb.append("\r\n");


        //create
        sb.append("\t@Override\r\n");
        sb.append("\tpublic boolean create" + upTableName + "(" + upTableName + " record) {\r\n");
        sb.append("\r\n");
        sb.append("\t\tlogger.info(\"create" + upTableName + "\");\r\n");
        sb.append("\t\ttry {\r\n");
        sb.append("\r\n");
        sb.append("\t\t\tint updates = " + downTableName + "Mapper.insertSelective(record);\r\n");
        sb.append("\r\n");
        sb.append("\t\t\tif (updates > 0) {\r\n");
        sb.append("\t\t\t\treturn true;\r\n");
        sb.append("\t\t\t}\r\n");
        sb.append("\t\t} catch (Exception e) {\r\n");
        sb.append("\t\t\tlogger.error(\"create" + upTableName + "\", e);\r\n");
        sb.append("\t\t}\r\n");
        sb.append("\r\n");
        sb.append("\t\treturn false;\r\n");
        sb.append("\t}\r\n");
        sb.append("\r\n");


        //delete
        sb.append("\t@Override\r\n");
        sb.append("\tpublic boolean delete" + upTableName + "(String " + StringUtil.underlineToCamel(pkName) + ") {\r\n");
        sb.append("\r\n");
        sb.append("\t\tlogger.info(\"delete" + upTableName + "\");\r\n");
        sb.append("\t\ttry {\r\n");
        sb.append("\r\n");
        sb.append("\t\t\tint updates = " + downTableName + "Mapper.delete(" + StringUtil.underlineToCamel(pkName) + ");\r\n");
        sb.append("\r\n");
        sb.append("\t\t\tif (updates > 0) {\r\n");
        sb.append("\t\t\t\treturn true;\r\n");
        sb.append("\t\t\t}\r\n");
        sb.append("\t\t} catch (Exception e) {\r\n");
        sb.append("\t\t\tlogger.error(\"delete" + upTableName + "\", e);\r\n");
        sb.append("\t\t}\r\n");
        sb.append("\r\n");
        sb.append("\t\treturn false;\r\n");
        sb.append("\t}\r\n");
        sb.append("\r\n");


        //update
        sb.append("\t@Override\r\n");
        sb.append("\tpublic boolean update" + upTableName + "(" + upTableName + " record) {\r\n");
        sb.append("\r\n");
        sb.append("\t\tlogger.info(\"update" + upTableName + "\");\r\n");
        sb.append("\t\ttry {\r\n");
        sb.append("\r\n");
        sb.append("\t\t\tint updates = " + downTableName + "Mapper.updateByPrimaryKeySelective(record);\r\n");
        sb.append("\r\n");
        sb.append("\t\t\tif (updates > 0) {\r\n");
        sb.append("\t\t\t\treturn true;\r\n");
        sb.append("\t\t\t}\r\n");
        sb.append("\t\t} catch (Exception e) {\r\n");
        sb.append("\t\t\tlogger.error(\"update" + upTableName + "\", e);\r\n");
        sb.append("\t\t}\r\n");
        sb.append("\r\n");
        sb.append("\t\treturn false;\r\n");
        sb.append("\t}\r\n");
        sb.append("\r\n");

        sb.append("}\r\n");

        return sb.toString();
    }

    private static void createControllerFile(TableParams tableParams, String pkName) {
        String controllerContent = parseController(tableParams, pkName);

        try {
            File directory = new File("");

            String fileName = directory.getAbsolutePath() + "/src/main/java/" + changeToFolder(tableParams.getControllerTargetPackage()) + StringUtil.initcap(tableParams.getTabelName()) + "Controller.java";
            System.out.println("文件路径：" + fileName);
            FileWriter fw = new FileWriter(fileName);
            PrintWriter pw = new PrintWriter(fw);
            pw.println(controllerContent);
            pw.flush();
            pw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 控制类
     *
     * @param tableParams
     * @param pkName
     * @return
     */
    private static String parseController(TableParams tableParams, String pkName) {

        StringBuffer sb = new StringBuffer();

        String upTableName = StringUtil.initcap(tableParams.getTabelName());
        String downTableName = StringUtil.underlineToCamel(tableParams.getTabelName());

        sb.append("package " + tableParams.getControllerTargetPackage() + ";\r\n");
        sb.append("\r\n");

        sb.append("import java.util.HashMap;\r\n");
        sb.append("import java.util.List;\r\n");
        sb.append("import java.util.Map;\r\n");

        sb.append("\r\n");

        sb.append("import org.springframework.beans.factory.annotation.Autowired;\r\n");
        sb.append("import org.springframework.stereotype.Controller;\r\n");
        sb.append("import org.springframework.web.bind.annotation.RequestMapping;\r\n");
        sb.append("import org.springframework.web.bind.annotation.RequestMethod;\r\n");
        sb.append("import org.springframework.web.bind.annotation.RequestParam;\r\n");
        sb.append("import org.springframework.web.bind.annotation.ResponseBody;\r\n");
        sb.append("\r\n");


        sb.append("import " + tableParams.getBasePackage() + ".constant.AppConstant;\r\n");
        sb.append("import " + tableParams.getBasePackage() + ".dto.Page;\r\n");
        sb.append("import " + tableParams.getBasePackage() + ".dto.Result;\r\n");
        sb.append("import " + tableParams.getModelTargetPackage() + "." + upTableName + ";\r\n");
        sb.append("import " + tableParams.getServiceTargetPackage() + "." + upTableName + "Service;\r\n");

        sb.append("\r\n");

        //注释部分
        sb.append("/**\r\n");
        sb.append(" * " + tableParams.getTabelName() + " controller类\r\n");
        sb.append(" * " + new Date() + "  Suo Chen Cheng\r\n");
        sb.append(" */ \r\n");

        //实体部分
        sb.append("@Controller\r\n");
        sb.append("@RequestMapping(\"/" + downTableName + "\")\r\n");
        sb.append("public class " + upTableName + "Controller extends BaseController  {\r\n");
        sb.append("\r\n");


        //引入
        sb.append("\t@Autowired\r\n");
        sb.append("\tprivate " + upTableName + "Service " + downTableName + "Service;\r\n");
        sb.append("\r\n");


        //get ALL
        sb.append("\t/**\r\n");
        sb.append("\t * <p>获取全部记录。</p>\r\n");
        sb.append("\t */ \r\n");
        sb.append("\t@RequestMapping(value = \"/all\", method = RequestMethod.GET)\r\n");
        sb.append("\t@ResponseBody\r\n");
        sb.append("\tpublic Result all(" + upTableName + " record) {\r\n");
        sb.append("\r\n");
        sb.append("\t\tList <" + upTableName + "> pp = " + downTableName + "Service.get" + upTableName + "List(record);\r\n");
        sb.append("\t\treturn new Result(true, pp);\r\n");
        sb.append("\t}\r\n");
        sb.append("\r\n");


        //get By id
        sb.append("\t/**\r\n");
        sb.append("\t * <p>根据Id。</p>\r\n");
        sb.append("\t */ \r\n");
        sb.append("\t@RequestMapping(value = \"/get" + upTableName + "\", method = RequestMethod.GET)\r\n");
        sb.append("\t@ResponseBody\r\n");
        sb.append("\tpublic Result get" + upTableName + "(@RequestParam(required = false, value = \"" + StringUtil.underlineToCamel(pkName) + "\") String " + StringUtil.underlineToCamel(pkName) + ") {\r\n");
        sb.append("\r\n");
        sb.append("\t\t" + upTableName + " data = " + downTableName + "Service.get" + upTableName + "ById(" + StringUtil.underlineToCamel(pkName) + ");\r\n");
        sb.append("\t\treturn new Result(true, data);\r\n");
        sb.append("\t}\r\n");
        sb.append("\r\n");


        //getOne
        sb.append("\t/**\r\n");
        sb.append("\t * <p>根据条件获取。</p>\r\n");
        sb.append("\t */ \r\n");
        sb.append("\t@RequestMapping(value = \"/getOne\", method = RequestMethod.GET)\r\n");
        sb.append("\t@ResponseBody\r\n");
        sb.append("\tpublic Result getOne(" + upTableName + " record) {\r\n");
        sb.append("\r\n");
        sb.append("\t\t" + upTableName + " data = " + downTableName + "Service.get" + upTableName + "(record);\r\n");
        sb.append("\t\treturn new Result(true, data);\r\n");
        sb.append("\t}\r\n");
        sb.append("\r\n");


        //page
        sb.append("\t/**\r\n");
        sb.append("\t * <p>分页查询。</p>\r\n");
        sb.append("\t */ \r\n");
        sb.append("\t@RequestMapping(value = \"/page\", method = RequestMethod.GET)\r\n");
        sb.append("\t@ResponseBody\r\n");
        sb.append("\tpublic Result page(Page page, " + upTableName + " record) {\r\n");
        sb.append("\r\n");
        sb.append("\t\tMap <String, Object> result = new HashMap <>();\r\n");
        sb.append("\r\n");
        sb.append("\t\tList <" + upTableName + "> pp = " + downTableName + "Service.get" + upTableName + "ByPage(page, record);\r\n");
        sb.append("\r\n");
        sb.append("\t\tresult.put(AppConstant.PAGE, page);\r\n");
        sb.append("\t\tresult.put(\"pp\", pp);\r\n");
        sb.append("\t\treturn new Result(true, result);\r\n");
        sb.append("\t}\r\n");
        sb.append("\r\n");


        //save
        sb.append("\t/**\r\n");
        sb.append("\t * <p>保存。</p>\r\n");
        sb.append("\t */ \r\n");
        sb.append("\t@RequestMapping(value = \"/save\", method = RequestMethod.POST)\r\n");
        sb.append("\t@ResponseBody\r\n");
        sb.append("\tpublic Result save(" + upTableName + " record) {\r\n");
        sb.append("\r\n");
        sb.append("\t\tboolean num = " + downTableName + "Service.create" + upTableName + "(record);\r\n");
        sb.append("\t\tif (num) {\r\n");
        sb.append("\t\t\treturn new Result(true, record.get" + StringUtil.initcap(pkName) + "());\r\n");
        sb.append("\t\t}\r\n");
        sb.append("\t\treturn new Result(false, \"保存异常\");\r\n");
        sb.append("\t}\r\n");
        sb.append("\r\n");

        //update
        sb.append("\t/**\r\n");
        sb.append("\t * <p>更新信息。</p>\r\n");
        sb.append("\t */ \r\n");
        sb.append("\t@RequestMapping(value = \"/update\", method = RequestMethod.POST)\r\n");
        sb.append("\t@ResponseBody\r\n");
        sb.append("\tpublic Result update" + upTableName + "(" + upTableName + " record) {\r\n");
        sb.append("\r\n");
        sb.append("\t\tboolean num = " + downTableName + "Service.update" + upTableName + "(record);\r\n");
        sb.append("\t\tif (num) {\r\n");
        sb.append("\t\t\treturn new Result(true, \"保存成功\");\r\n");
        sb.append("\t\t}\r\n");
        sb.append("\t\treturn new Result(false, \"保存异常\");\r\n");
        sb.append("\t}\r\n");
        sb.append("\r\n");


        //del
        sb.append("\t/**\r\n");
        sb.append("\t * <p>删除。</p>\r\n");
        sb.append("\t */ \r\n");
        sb.append("\t@RequestMapping(value = \"/del\", method = RequestMethod.POST)\r\n");
        sb.append("\t@ResponseBody\r\n");
        sb.append("\tpublic Result delete" + upTableName + "(@RequestParam(required = true, value = \"" + StringUtil.underlineToCamel(pkName) + "\") String " + StringUtil.underlineToCamel(pkName) + ") {\r\n");
        sb.append("\r\n");
        sb.append("\t\tboolean num = " + downTableName + "Service.delete" + upTableName + "(" + StringUtil.underlineToCamel(pkName) + ");\r\n");
        sb.append("\t\tif (num) {\r\n");
        sb.append("\t\t\treturn new Result(true, \"删除成功\");\r\n");
        sb.append("\t\t}\r\n");
        sb.append("\t\treturn new Result(false, \"删除异常\");\r\n");
        sb.append("\t}\r\n");
        sb.append("\r\n");


        sb.append("}\r\n");

        return sb.toString();


    }

    /**
     * 生成对象model文件
     *
     * @param tableParams 表
     * @param colnames    字段名
     * @param colTypes    字段类型
     * @param colComments 字段注释
     */
    private static void createModelFile(TableParams tableParams, String[] colnames, String[] colTypes, String[] colComments) {
        String modelContent = parseModel(colnames, colTypes, tableParams, colComments);

        try {
            File directory = new File("");

            String fileName = directory.getAbsolutePath() + "/src/main/java/" + changeToFolder(tableParams.getModelTargetPackage()) + StringUtil.initcap(tableParams.getTabelName()) + ".java";
            System.out.println("文件路径：" + fileName);
            FileWriter fw = new FileWriter(fileName);
            PrintWriter pw = new PrintWriter(fw);
            pw.println(modelContent);
            pw.flush();
            pw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String changeToFolder(String packageName) {

        return packageName.replaceAll("\\.", "\\/") + "/";
    }

    /**
     * 功能：生成实体类主体代码
     *
     * @return
     */
    public static String parseModel(String[] colnames, String[] colTypes, TableParams tableParams, String[] colComments) {
        StringBuffer sb = new StringBuffer();


        sb.append("package " + tableParams.getModelTargetPackage() + ";\r\n");
        sb.append("\r\n");

        //判断是否导入工具包
        if (f_util) {
            sb.append("import java.util.*;\r\n");
        }
        if (f_sql) {
            sb.append("import java.sql.*;\r\n");
        }

        if (f_math) {
            sb.append("import java.math.*;\r\n");
        }

        sb.append("\r\n");

        sb.append("import com.fasterxml.jackson.annotation.JsonAutoDetect;\r\n");
        sb.append("import com.fasterxml.jackson.annotation.JsonInclude;\r\n");

        sb.append("\r\n");

        //注释部分
        sb.append("/**\r\n");
        sb.append(" * " + tableParams.getTabelName() + " 实体类\r\n");
        sb.append(" * " + new Date() + "  Suo Chen Cheng\r\n");
        sb.append(" */ \r\n");

        //注解
        sb.append("@JsonAutoDetect\r\n");
        sb.append("@JsonInclude(JsonInclude.Include.NON_NULL)\r\n");

        //实体部分
        sb.append("public class " + StringUtil.initcap(tableParams.getTabelName()) + "{\r\n");
        sb.append("\r\n");

        processModelAllAttrs(sb, colnames, colTypes, colComments);//属性
        processModelAllMethod(sb, colnames, colTypes);//get set方法
        sb.append("}\r\n");

        //System.out.println(sb.toString());
        return sb.toString();
    }

    /**
     * 功能：生成所有属性
     *
     * @param sb
     */
    public static void processModelAllAttrs(StringBuffer sb, String[] colnames, String[] colTypes, String[] colComments) {

        for (int i = 0; i < colnames.length; i++) {
/**
 *
 */
            sb.append("/**\r\n");
            //获取注释第一个为列名，分割符 __
            sb.append("* \t " + (StringUtils.isNotEmpty(colComments[i]) ? colComments[i] : colnames[i]) + "\r\n");
            sb.append("*/\r\n");
            sb.append("\tprivate " + StringUtil.sqlType2JavaType(colTypes[i]) + " " + StringUtil.underlineToCamel(colnames[i]) + ";\r\n\r\n");
        }

    }

    /**
     * 功能：生成所有方法
     *
     * @param sb
     */
    public static void processModelAllMethod(StringBuffer sb, String[] colnames, String[] colTypes) {

        for (int i = 0; i < colnames.length; i++) {
            sb.append("\tpublic void set" + StringUtil.initcap(colnames[i]) + "(" + StringUtil.sqlType2JavaType(colTypes[i]) + " " +
                    StringUtil.underlineToCamel(colnames[i]) + "){\r\n");
            sb.append("\t\tthis." + StringUtil.underlineToCamel(colnames[i]) + "=" + StringUtil.underlineToCamel(colnames[i]) + ";\r\n");
            sb.append("\t}\r\n\r\n");
            sb.append("\tpublic " + StringUtil.sqlType2JavaType(colTypes[i]) + " get" + StringUtil.initcap(colnames[i]) + "(){\r\n");
            sb.append("\t\treturn " + StringUtil.underlineToCamel(colnames[i]) + ";\r\n");
            sb.append("\t}\r\n\r\n");
        }

    }


}
