package com.qiuguo.iot.customer.http.api.mysql;





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

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

        boolean hasDate = false;

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

        StringBuffer bf = new StringBuffer();

        String realName = MysqlMain.pre + tableBean.getSpaceName() + "";
        String fileName = MysqlMain.save_path + "/" + realName + "Request.java";

        int i = 0;
        bf.append("  \n");
        //bf.append("    @ApiModelProperty(value = \"currPage\", name = \"当前页码\")\n");
        bf.append("    private int currPage = 1;\n");

        //bf.append("    @ApiModelProperty(value = \"pageSize\", name = \"每页返回条数\")\n");
        bf.append("    private int pageSize = 10;\n");

        //bf.append("    @ApiModelProperty(value = \"sort\", name = \"类型\")\n");
        bf.append("    private String sort;\n");

        //bf.append("    @ApiModelProperty(value = \"order\", name = \"排序\")\n");
        bf.append("    private String order;\n");

        // 定义声明
        for (FieldBean tb : list) {

            if (MysqlMain.req_exclude.contains("," + tb.getField() + ",")) {
                continue;
            }
            String temp = "";
            temp += "    //" + tb.getComment() + "\n";
            if (i == 0) {

            }else{
                //temp += "    @ApiModelProperty(value = \"" + tb.getComment() + "\", name = \"" + tb.getJavaCode() + "\")\n";
            }
            temp += "    private " + tb.getJavaType() + " " + tb.getJavaCode() + ";";
            if(tb.getJavaType().equals("Date")/*tb.getField().equals("modify_time") || tb.getField().equals("create_time")*/){
                temp += "\n    //" + tb.getComment() + "搜索开始\n";
                temp += "\n    private " + tb.getJavaType() + " " + tb.getJavaCode() + "Start;\n";
                temp += "\n    //" + tb.getComment() + "搜索结束\n";
                temp += "    private " + tb.getJavaType() + " " + tb.getJavaCode() + "End;";

            }

            i++;
            // System.out.println(temp);
            bf.append(temp).append("\n");


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






        StringBuilder sb = new StringBuilder();

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

        for (FieldBean tb : list) {

            if (MysqlMain.req_exclude.contains("," + tb.getField() + ",")) {
                continue;
            }
            String temp = "";
            sb.append("                \"," + tb.getJavaCode() + "=\" + " + tb.getJavaCode() + " +\n");
        }
        sb.append("                \"}\";\n");
        sb.append("    }\n");
        try {

            String content = "package " + MysqlMain.package_name + ";\n";
            //content += "import com.baomidou.mybatisplus.annotation.TableId;\n";
            //content += "import com.baomidou.mybatisplus.annotation.TableName;\n";
            //content += "import com.baomidou.mybatisplus.annotation.IdType;\n";
            //content += "import io.swagger.annotations.ApiModel;\n";
            //content += "import io.swagger.annotations.ApiModelProperty;\n";
            content += "import lombok.Data;\n";


            if (hasDate) {
                content += "import java.util.Date;" + "\n";
            }


            content += "/**\n";
            content += "* <p>\n";
            //content += "* 菜单权限表\n";
            //content += "* </p>";
            content += "*" + tableBean.getComment().replace("表", "") + "请求类\n";
            content += "* @author wulin\n";
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            content += "* @since " + format.format(new Date()) + "\n";
            content += "*/\n";

            content += "\n";
            content += "\n";



            content += "@Data\n";
            //content += "@ApiModel(value = \"" + tableBean.getComment() + "Req\")\n";
            content += "public class " + realName + "Request 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 + "Request.java" + "生成");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }




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

        boolean hasDate = false;

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

        StringBuffer bf = new StringBuffer();

        String realName = MysqlMain.pre + tableBean.getSpaceName() + "";
        String fileName = MysqlMain.save_path + "/" + realName + "Entity.java";

        int i = 0;
        // 定义声明
        for (FieldBean tb : list) {
            String temp = "";
            temp += "    @Comment(\"" +( tb.getComment().length() == 0 ? tb.getField() : tb.getComment()) + "\")\n    @Column(name = \"" + tb.getField() + "\"";
            if (tb.getField().equals("id")) {
                temp += ", length = 11, nullable = false, unique = true)\n";
               // temp += "    @TableId(value = \"" + tb.getField() + "\", type = IdType.AUTO)\n";
            }else{
                if(tb.getLenth() != null && tb.getLenth().length() > 0){
                    temp += ", length = " + tb.getLenth();
                    if(tb.getPrecision() != null && tb.getPrecision().length() > 0){
                        temp += ", precision = " + tb.getPrecision();
                    }
                }
                if(tb.getIsNullAble().equals("NO")){
                    temp += ", nullable = false";
                }
                temp += ")\n";
            }
            temp += "    private " + tb.getJavaType() + " " + tb.getJavaCode() + ";\n";
            i++;
            // System.out.println(temp);
            bf.append(temp).append("\n");


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




        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.package_name + ";\n";
            //content += "import com.baomidou.mybatisplus.annotation.TableId;\n";
            //content += "import com.baomidou.mybatisplus.annotation.TableName;\n";
            //content += "import com.baomidou.mybatisplus.annotation.IdType;\n";
            //content += "import io.swagger.annotations.ApiModel;\n";
            //content += "import io.swagger.annotations.ApiModelProperty;\n";
            content += "import org.hswebframework.ezorm.rdb.mapping.annotation.Comment;\n" +
                    "import org.hswebframework.web.crud.annotation.EnableEntityEvent;\n" +
                    "import org.hswebframework.web.api.crud.entity.GenericEntity;\n" +
                    "import javax.persistence.Column;\n" +
                    "import javax.persistence.Table;";
            content += "import lombok.Data;\n";


            if (hasDate) {
                content += "import java.util.Date;" + "\n";
            }


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

            content += "\n";
            content += "@Data\n@Comment(\"" + tableBean.getComment() + "\")\n";
            content += "@Table(name = \"" + tableBean.getTableName() + "\")\n@EnableEntityEvent\n";
            //content += "@ApiModel(value = \"" + tableBean.getComment() + "\")\n";
            content += "public class " + realName + "Entity extends GenericEntity<Long> {\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 + "Entity.java" + "生成");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void printDto(TablesBean tableBean) {

        boolean hasDate = false;

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

        StringBuffer bf = new StringBuffer();

        StringBuffer cp = new StringBuffer();


        String realName = MysqlMain.pre + tableBean.getSpaceName() + "";
        String fileName = MysqlMain.save_path + "/" + realName + "Resp.java";
        cp.append("    public " + realName + "Resp(){\n    }\n");
        cp.append("    public " + realName + "Resp(" + realName + "Entity entity){\n");
        //String[] exclude = MysqlMain.dto_exclude.split(",");
        //Arrays.stream(exclude).findFirst();
        int i = 0;
        // 定义声明
        for (FieldBean tb : list) {
            String temp = "";
            temp += "    //" + tb.getComment() + "\n";
            if (MysqlMain.dto_exclude.contains("," + tb.getField() + ",")) {
                continue;
            }/*else{
                temp += "    @ApiModelProperty(value = \"" + tb.getComment() + "\", name = \"" + tb.getJavaCode() + "\")\n";
            }*/
            temp += "    private " + tb.getJavaType() + " " + tb.getJavaCode() + ";";
            i++;
            // System.out.println(temp);
            bf.append(temp).append("\n");
            cp.append("        " + tb.getJavaCode() + " = entity.get" + MysqlUtil.getFirstToUpperCase(tb.getJavaCode()) + "();\n");


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




        StringBuilder sb = new StringBuilder();

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

        for (FieldBean tb : list) {
            if (MysqlMain.dto_exclude.contains("," + tb.getField() + ",")) {
                continue;
            }
            String temp = "";
            sb.append("                \"," + tb.getJavaCode() + "=\" + " + tb.getJavaCode() + " +\n");
        }
        sb.append("                \"}\";\n");
        sb.append("    }\n");
        try {

            String content = "package " + MysqlMain.package_name + ";\n";
            //content += "import io.swagger.annotations.ApiModel;\n";
            //content += "import io.swagger.annotations.ApiModelProperty;\n";
            content += "import lombok.Data;\n";


            if (hasDate) {
                content += "import java.util.Date;" + "\n";
            }


            content += "/**\n";
            content += "* <p>\n";
            content += "* </p>";
            content += "*" + tableBean.getComment().replace("表", "") + "返回类\n";
            content += "* @author wulin\n";
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            content += "* @since " + format.format(new Date()) + "\n";
            content += "*/\n";

            content += "\n";
            content += "@Data\n";
            //content += "@ApiModel(value = \"" + tableBean.getComment() + "Dto\")\n";
            content += "public class " + realName + "Resp {\n" + cp.toString() + "\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 + "Resp.java" + "生成");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
