package com.test.xxx.mysql;



import com.heyu.api.data.utils.StringUtils;
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;

public class MysqlUtilTable2Service {


    public static String TAB = "	";

    public static void printService(TablesBean tableBean) {
        String realName = MysqlMain_insert.pre + tableBean.getSpaceName();
        String realName2 = MysqlMain_insert.pre + tableBean.getSpaceName() + "";
        String fileName = MysqlMain_insert.save_path + "/" + realName + "Service.java";


        if (StringUtils.isNotEmpty(MysqlMain_insert.service_path)) {
            fileName = MysqlMain_insert.service_path + "/" + realName + "Service.java";
        }


        try {
            String content = "package " + MysqlMain_insert.service_package + ";\n";


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

            content += "import com.baomidou.mybatisplus.extension.service.IService;\n" +
                    "import " + MysqlMain_insert.bean_package + "." + tableBean.getSpaceName() + "Entity;\n";
            content += "public interface " + tableBean.getSpaceName() + "Service extends IService<" + tableBean.getSpaceName() +"Entity"+ "> {\n";
            content += "\n";
            content += "\n";
            content += "\n";


            content += TAB + realName +"Entity"+ " select" + realName + "ById(Long id);";
            content += "\n";
            content += "\n";
            content += "\n";

            content += TAB + "Long insert" + realName + "(" + realName+"Entity" + " " + tableBean.getJavaName() + ");";
            content += "\n";
            content += "\n";
            content += "\n";


            content += TAB + "Long insertOrUpdate" + realName + "(" + realName +"Entity"+ " " + tableBean.getJavaName() + ");";
            content += "\n";
            content += "\n";
            content += "\n";

            content += TAB + "int update" + realName + "ById(" + realName +"Entity"+ " " + tableBean.getJavaName() + ");";
            content += "\n";
            content += "\n";
            content += "\n";

            content += TAB + "int updateCover" + realName + "ById(" + realName +"Entity"+ " " + tableBean.getJavaName() + ");";
            content += "\n";
            content += "\n";
            content += "\n";

            content += TAB + "int delete" + tableBean.getSpaceName() + "ById(Long id);";
            content += "\n";
            content += "\n";
            content += "\n";


            content += "}";

            FileOutputStream fos = new FileOutputStream(fileName);
            Writer out = new OutputStreamWriter(fos, "UTF-8");
            out.write(content);
            out.close();
            fos.close();

            System.out.println("===" + realName + "service.java" + "生成");
            // 打开一个写文件器，构造函数中的第二个参数true表示以追加形式写文件
            // FileWriter writer = new FileWriter(fileName, false);
            // writer.write(content);
            // writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static String captureName(String name) {
        if (org.apache.commons.lang3.StringUtils.isNotBlank(name) && name.length() > 0) {
            name = name.substring(0, 1).toUpperCase() + name.substring(1);
        }
        return name;

    }

    public static void printServiceImpl(TablesBean tableBean) {
        String realName = MysqlMain_insert.pre + tableBean.getSpaceName();

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

        String javaName = "" + captureName(tableBean.getJavaName());
        String fileName = MysqlMain_insert.save_path + "/" + realName + "ServiceImpl.java";


        if (org.apache.commons.lang3.StringUtils.isNotBlank(MysqlMain_insert.service_impl_path)) {
            fileName = MysqlMain_insert.service_impl_path + "/" + realName + "ServiceImpl.java";
        }

        try {
            String content = "package " + MysqlMain_insert.service_impl_package + ";\n";


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

            content += "\n";
            content += "import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;\n" +
                    "import " + MysqlMain_insert.dao_package + "." + tableBean.getSpaceName() + "Dao;\n" +
                    "import " + MysqlMain_insert.bean_package + "." + tableBean.getSpaceName() + "Entity;\n" +
                    "import " + MysqlMain_insert.service_package + "." + tableBean.getSpaceName() + "Service;\n" +
                    "import org.springframework.beans.factory.annotation.Autowired;\n" +
                    "import org.springframework.stereotype.Service;\n";

            content += "@Service\n";
            content += "public class " + tableBean.getSpaceName() + "ServiceImpl extends ServiceImpl<" + tableBean.getSpaceName() + "Dao, " + tableBean.getSpaceName()  + "Entity> implements " + tableBean.getSpaceName() + "Service {\n";
            content += "\n";
            content += "\n";
            content += "    @Autowired\n";
            content += "\tprivate " + realName + "Dao " + MysqlUtil.getFirstToLower(javaName) + "Dao;" + "\n";
            content += "\n";
            content += "\n";
            content += "\n";

            content += TAB + "@Override\n";
            content += TAB + "public " + realName +"Entity" + " select" + realName + "ById(Long id){\n";
            content += TAB + TAB + "return " + tableBean.getJavaName() + "Dao." + "select" + realName + "ById(id);\n";
            content += TAB + "}\n";
            content += "\n";
            content += "\n";
            content += "\n";
            content += TAB + "@Override\n";
            content += TAB + "public Long insert" + realName + "(" + realName  +"Entity"+ " " + tableBean.getJavaName() + "){\n";
            content += TAB + TAB + "return " + tableBean.getJavaName() + "Dao." + "insert" + realName + "(" + tableBean.getJavaName() + ");\n";
            content += TAB + "}\n";
            content += "\n";
            content += "\n";
            content += "\n";


            content += TAB + "@Override\n";
            content += TAB + "public Long insertOrUpdate" + realName + "(" + realName +"Entity" + " " + tableBean.getJavaName() + "){\n";
            content += TAB + TAB + "return " + tableBean.getJavaName() + "Dao." + "insertOrUpdate" + realName + "(" + tableBean.getJavaName()  + ");\n";
            content += TAB + "}\n";
            content += "\n";
            content += "\n";
            content += "\n";


            content += TAB + "@Override\n";
            content += TAB + "public int update" + realName + "ById(" + realName  +"Entity"+ " " + tableBean.getJavaName() + "){\n";
            content += TAB + TAB + "return " + tableBean.getJavaName() + "Dao." + "update" + realName + "ById(" + tableBean.getJavaName() + ");\n";
            content += TAB + "}\n";

            content += "\n";
            content += "\n";
            content += "\n";
            content += TAB + "@Override\n";
            content += TAB + "public int updateCover" + realName + "ById("  + realName+"Entity" + " " + tableBean.getJavaName() + "){\n";
            content += TAB + TAB + "return " + tableBean.getJavaName() + "Dao." + "updateCover" + realName + "ById(" + tableBean.getJavaName() + ");\n";
            content += TAB + "}\n";
            content += "\n";
            content += "\n";
            content += "\n";
            content += TAB + "@Override\n";
            content += TAB + "public int delete" + tableBean.getSpaceName() + "ById(Long id){\n";
            content += TAB + TAB + "return " + tableBean.getJavaName() + "Dao." + "delete" + tableBean.getSpaceName() + "ById(id);\n";
            content += TAB + "}\n";
            content += "\n";
            content += "\n";
            content += "\n";


            content += "}" + "\n";
            FileOutputStream fos = new FileOutputStream(fileName);
            Writer out = new OutputStreamWriter(fos, "UTF-8");
            out.write(content);
            out.close();
            fos.close();

            System.out.println("===" + realName + "ServiceImpl.java" + "生成");
            // 打开一个写文件器，构造函数中的第二个参数true表示以追加形式写文件
            // FileWriter writer = new FileWriter(fileName, false);
            // writer.write(content);
            // writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
