package com.test.xxx.mysql;


import com.heyu.api.data.utils.StringUtils;
import com.lz.mybatis.plugin.utils.OsUtil;
import com.test.xxx.MysqlMain_insert;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class MysqlUtilTable2Bean {
    /**
     * 打印entity的信息
     */
    public static void printEntity(TablesBean tableBean) {

        boolean hasDate = false;

        List<FieldBean> list = tableBean.getFieldList();

        StringBuffer bf = new StringBuffer();

        String realName = MysqlMain_insert.pre + tableBean.getSpaceName() + "";


        String fileName = "";

        realName = realName + "Entity";
        if (OsUtil.isWindows()) {
            if (StringUtils.isNotEmpty(MysqlMain_insert.bean_path)) {
                fileName = MysqlMain_insert.bean_path + "/" + realName + ".java";
            } else {
                //fileName = WindowMysqlMain.save_path + "/" + realName + ".java";
                fileName = MysqlMain_insert.save_path + "/" + realName + ".java";
            }
        } else {
            fileName = MysqlMain_insert.bean_path + "/" + realName + ".java";
        }

        bf.append("private static final long serialVersionUID = 1L;\n\n");
        bf.append("    public final static String CLASS_NAME =" + "\"" + MysqlMain_insert.bean_package + "." + realName +":\";\n\n");
        bf.append("    public final static String all = CLASS_NAME + " + "\"*\";\n");
        int maxLength = 0 ;
        // 定义声明
        for (FieldBean tb : list) {
            String xx = "    public final static String " + append_(tb.getField()) + " = CLASS_NAME + \"" + tb.getField() + "\";";
            if (xx.length() > maxLength) {
                maxLength = xx.length();
            }
        }
        // 定义声明
        for (FieldBean tb : list) {
            String temp = "";
            String xx = "    public final static String " + append_(tb.getField()) + " = CLASS_NAME + \"" + tb.getField() + "\";";
            temp += xx + getSpace(xx.length(), maxLength + 4) + "// " + tb.getComment() + "\n";
            bf.append(temp);
        }

        int i = 0;
        // 定义声明
        for (FieldBean tb : list) {
            String temp = "";
            temp += "    //" + tb.getComment() + "\n";
            if (i == 0) {
                temp += "    @TableId(value = \"" + tb.getField() + "\", type = IdType.AUTO)\n";
            }
            temp += "    private " + tb.getJavaType() + " " + tb.getJavaCode() + ";";
            i++;
            // System.out.println(temp);
            bf.append(temp).append("\n");


            if (!hasDate && "Date".equals(tb.getJavaType())) {
                hasDate = true;
            }
        }


        // 定义get set方法
        for (FieldBean tb : list) {
            String temp = "";
            temp += "    /**\n";
            temp += "     * " + tb.getComment() + " \n";
            temp += "     * @return\n";
            temp += "     */\n";
            temp += "    public " + tb.getJavaType() + " "
                    + tb.getJavaCodeForGet() + "() {\n";
            temp += "        return " + tb.getJavaCode() + ";\n";
            temp += "    }";
            // System.out.println(temp);

            bf.append(temp).append("\n");

            temp = "";
            temp += "    /**\n";
            temp += "     * " + tb.getComment() + " \n";
            temp += "     * @param " + tb.getJavaCode() + "\n";
            temp += "     */\n";
            temp += "    public void " + tb.getJavaCodeForSet() + "("
                    + tb.getJavaType() + " " + tb.getJavaCode() + ") {\n";
            temp += "        this." + tb.getJavaCode() + " = " + tb.getJavaCode()
                    + ";\n";
            temp += "    }\n";
            // System.out.println(temp);
            bf.append(temp).append("\n");
        }


        // 定义get set方法
        for (FieldBean tb : list) {
            if (!tb.getComment().startsWith("枚举")) {
                continue;
            }
            String mo = "Integer";

            bf.append("/** \n" +
                    "* " + tb.getComment() +
                    "*/ \n" +
                    "    public enum " + tb.getField().toUpperCase() + "_ENUM {\n");
            String[] comments = tb.getComment().split(":");
            boolean isStr = false;
            for (int j = 1; j < comments.length; j++) {
                String bs[] = comments[j].split(",");
                if (!StringUtils.checkStrIsNum(bs[1].trim())) {
                    isStr = true;
                }
            }
            for (int j = 1; j < comments.length; j++) {
                String bs[] = comments[j].split(",");
                if (isStr) {
                    if (j == comments.length - 1) {
                        bf.append("        ").append(bs[0].trim() + "(\"" + bs[1].trim() + "\", \"" + bs[2].trim() + "\");\n");
                    } else {
                        bf.append("        ").append(bs[0].trim() + "(\"" + bs[1].trim() + "\", \"" + bs[2].trim() + "\"),\n");
                    }
                } else {
                    if (j == comments.length - 1) {
                        bf.append("        ").append(bs[0].trim() + "(" + bs[1].trim() + ", \"" + bs[2].trim() + "\");\n");
                    } else {
                        bf.append("        ").append(bs[0].trim() + "(" + bs[1].trim() + ", \"" + bs[2].trim() + "\"),\n");
                    }
                }
            }
            if (isStr) {
                mo = "String";
            }

            bf.append("\n");
            bf.append("\n" +
                    "        private " + mo + " code;\n" +
                    "\n" +
                    "        private String desc;\n" +
                    "\n" +
                    "        " + tb.getField().toUpperCase() + "_ENUM(" + mo + " code, String desc) {\n" +
                    "            this.code = code;\n" +
                    "            this.desc = desc;\n" +
                    "        }\n" +
                    "\n" +
                    "        public " + mo + " getCode() {\n" +
                    "            return code;\n" +
                    "        }\n" +
                    "\n" +
                    "        public void setCode(" + mo + " code) {\n" +
                    "            this.code = code;\n" +
                    "        }\n" +
                    "\n" +
                    "        public String getDesc() {\n" +
                    "            return desc;\n" +
                    "        }\n" +
                    "\n" +
                    "        public void setDesc(String desc) {\n" +
                    "            this.desc = desc;\n" +
                    "        }\n" +
                    "    }\n");

        }





        StringBuilder sb = new StringBuilder();

        sb.append("    @Override\n");
        sb.append("    public String toString() {\n");
        sb.append("        return \"" + realName + "{\" +\n");

        for (FieldBean tb : list) {
            String temp = "";
            sb.append("                \"," + tb.getJavaCode() + "=\" + " + tb.getJavaCode() + " +\n");
        }
        sb.append("                \"}\";\n");
        sb.append("    }\n");
        try {

            String content = "package " + MysqlMain_insert.bean_package + ";\n";
            content += "import com.baomidou.mybatisplus.annotation.IdType;\n" +
                    "import com.baomidou.mybatisplus.annotation.TableId;\n" +
                    "import com.baomidou.mybatisplus.annotation.TableName;\n" +
                    "import lombok.Data;\n" +
                    "import com.lz.mybatis.plugin.annotations.AS;\n" +
                    "\n" +
                    "import java.math.BigDecimal;\n" +
                    "import java.util.Date;";
            if (hasDate) {
                content += "import java.util.Date;" + "\n";
            }


            content += "/**\n";
            content += "*" + tableBean.getComment() + "\n";
            content += "* @author quyixiao\n";
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            content += "* @since " + format.format(new Date()) + "\n";
            content += "*/\n";

            content += "\n";
            content += "@Data\n";
            content += "@TableName(" + "\"" + tableBean.getTableName() + "\")\n";
            content += "public class " + realName + " implements java.io.Serializable {\n" + bf.toString();
            content += sb.toString();
            content += "}";

            FileOutputStream fos = new FileOutputStream(fileName);

            Writer out = new OutputStreamWriter(fos, "UTF-8");
            out.write(content);
            out.close();
            fos.close();
            System.out.println("===" + realName + ".java" + "生成");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String getSpace(int current , int max ){
        StringBuilder sb = new StringBuilder();
        for(int i = 0 ;i < max - current  ; i++){
            sb.append(" ");
        }
        return sb.toString();
    }


    public static String getAs(String value) {
        StringBuilder sb = new StringBuilder();
        char values[] = value.toCharArray();
        sb.append(values[0]);
        boolean flag = false;
        for (int i = 1; i < value.length(); i++) {
            char c = values[i];
            if (flag) {
                flag = false;
                sb.append(c);
            }

            if (c == '_') {
                flag = true;
            }
        }
        return sb.toString();
    }



    public static String append_(String value){
        if(!value.contains("_")){
            return value + "_";
        }
        return value;
    }
}
