package com.test.xxx;

import com.heyu.api.data.utils.StringUtils;
import com.test.xxx.mysql.FieldBean;
import com.test.xxx.mysql.MysqlUtil2ShowCreateTable;
import com.test.xxx.mysql.MysqlUtilTable2Bean;
import com.test.xxx.mysql.TablesBean;
import org.junit.Test;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/***
 * 预发布环境回调接口地址：
 https://pre-callback.sinawallent.com/platform/callback
 https://pre-callback.sinawallent.com/platformweb/query
 */
public class MysqlMain_update {

    @Test
    public void testUpdateEntity() throws Exception {
        String path = ResourceUtils.getURL("classpath:").getPath();
        System.out.println(path);
        String dir = null;
        if (StringUtils.isNotEmpty(path)) {
            dir = path.split("target")[0];
        }
        String old = dir + "src/test/tmp";
        MysqlMain_insert.save_path = dir + "src/test/tmp";
        System.out.println(MysqlMain_insert.save_path);

        String packageName = "vv";

        MysqlMain_insert.initApi(packageName);

        String entityPath = dir + "/src/main/java/com/heyu/api/data/entity";
        List<String> resultFileName = new ArrayList<>();
        File file = new File(entityPath);
        MysqlMain_insert.ergodic(file, resultFileName);

        Map<String, String> fileNameMap = new HashMap<>();
        for (String fileName : resultFileName) {
            System.out.println(fileName);
        }
        List<TablesBean> list = new ArrayList<TablesBean>();

        list.add(new TablesBean("vv_promoter"));

        Map<String, String> map = MysqlUtil2ShowCreateTable.getComments();
        for (int i = 0; i < list.size(); i++) {
            TablesBean obj = list.get(i);
            String tableName = list.get(i).getTableName();
            System.out.println("---------" + tableName);
            List<FieldBean> itemList = MysqlUtil2ShowCreateTable.readTableDetail(tableName);
            obj.setFieldList(itemList);
            obj.setComment(map.get(tableName));
            for (String fileName : resultFileName) {
                String className = fileName.replaceAll("\\.java", "");
                try {
                    System.out.println("xxxxxxxxxxxxxxxxxxxxxxx:" + className);
                    Class clazz = Class.forName(className);
                    String annTableName = MysqlMain_insert.getAnnotationValueByTypeName(clazz, "TableName");
                    if (StringUtils.isNotEmpty(annTableName) && tableName.equals(annTableName)) {
                        System.out.println("annotationName is = " + annTableName);
                        String fileNames[] = className.split("\\.");
                        String xx = fileNames[fileNames.length - 1];
                        String paxx = className.replace("." + xx, "");
                        System.out.println(paxx);
                        MysqlMain_insert.package_name = paxx;
                        MysqlMain_insert.save_path = dir + "src/main/java/" + paxx.replaceAll("\\.", "/");
                        System.out.println("----------" + MysqlMain_insert.save_path);
                        break;
                    } else {
                        MysqlMain_insert.save_path = old;
                    }
                } catch (ClassNotFoundException e) {
                    System.out.println("=============" + className);
                    e.printStackTrace();

                }
            }
            MysqlUtilTable2Bean.printEntity(obj);
        }

    }


}
