package com.test.xxx;

import com.heyu.api.data.utils.StringUtils;
import com.test.xxx.mysql.*;
import org.junit.Test;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
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_insert {

    public static String package_name = "com.api.model.entity";

    public static String save_path = "/Users/zhy/logs";

    public static String bean_path = "";
    public static String mapper_path = "";
    public static String mapper_xml_path = "";
    public static String service_path = "";
    public static String service_impl_path = "";


    public static String bean_package = "";
    public static String dao_package = "";
    public static String service_package = "";
    public static String service_impl_package = "";


    public static String mysql_url = "jdbc:mysql://121.40.41.240:3306/heyu_api?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8";
    //public static String mysql_url = "jdbc:mysql://121.40.41.240:3306/heyu_admin?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8";
    public static String pre = "";

    //public static String mysql_dbname = "heyu_admin";
    public static String mysql_dbname = "heyu_api";
    public static String mysql_username = "heyu_api";

    public static String mysql_password = "Ro_d39PlM^#8^f!2";

    public static void initApi(String package_name) throws Exception {

        String path = ResourceUtils.getURL("classpath:").getPath();
        System.out.println("=========" + path);
        String[] a = path.split("eb-service-api");
        System.out.println("==============" + a[0]);

        MysqlMain_insert.bean_package = "com.heyu.api.data.entity." + package_name;
        MysqlMain_insert.dao_package = "com.heyu.api.data.dao." + package_name;
        MysqlMain_insert.service_package = "com.heyu.api.data.service." + package_name;
        MysqlMain_insert.service_impl_package = "com.heyu.api.data.service.impl." + package_name;


        MysqlMain_insert.bean_path = a[0] + "eb-service-api/api-mapper/src/main/java/com/heyu/api/data/entity/" + package_name;
        MysqlMain_insert.mapper_path = a[0] + "eb-service-api/api-mapper/src/main/java/com/heyu/api/data/dao/" + package_name;
        MysqlMain_insert.mapper_xml_path = a[0] + "eb-service-api/api-mapper/src/main/resources/mapper/" + package_name;
        MysqlMain_insert.service_path = a[0] + "eb-service-api/api-mapper/src/main/java/com/heyu/api/data/service/" + package_name;
        MysqlMain_insert.service_impl_path = a[0] + "eb-service-api/api-mapper/src/main/java/com/heyu/api/data/service/impl/" + package_name;


        System.out.println("==============" + MysqlMain_insert.bean_path);
        for (String file : Arrays.asList(MysqlMain_insert.bean_path, MysqlMain_insert.mapper_path, MysqlMain_insert.mapper_xml_path, MysqlMain_insert.service_path, MysqlMain_insert.service_impl_path)) {
            File file1 = new File(file);
            if (!file1.exists()) {
                file1.mkdirs();
            }
        }
    }

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

        String packageName = "vv";

        initApi(packageName);

        System.out.println(MysqlMain_insert.save_path);

        List<TablesBean> list = new ArrayList<TablesBean>();

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




        List<TablesBean> list2 = new ArrayList<TablesBean>();
        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();

            List<FieldBean> itemList = MysqlUtil2ShowCreateTable.readTableDetail(tableName);
            obj.setFieldList(itemList);
            obj.setComment(map.get(tableName));
            list2.add(obj);
        }

        for (int i = 0; i < list2.size(); i++) {
            MysqlUtilTable2Bean.printEntity(list2.get(i));
        }

        for (int i = 0; i < list2.size(); i++) {
            MysqlUtilTable2Mapper.printDao(list2.get(i));
        }

        for (int i = 0; i < list2.size(); i++) {
            MysqlUtilTable2Service.printService(list2.get(i));
            MysqlUtilTable2Service.printServiceImpl(list2.get(i));
        }

        for (int i = 0; i < list2.size(); i++) {
            MysqlUtilTable2XML.printXMLForMap(list2.get(i));
        }
    }

    public static <T> T getAnnotationValueByTypeName(Class type, String name) {
        Annotation[] annotations = type.getAnnotations();
        if (annotations != null && annotations.length > 0) {
            for (Annotation annotation : annotations) {
                if (name.equals(getAnnotationName(annotation))) {
                    return getAnnotationValue(annotation);
                }
            }
        }
        return null;
    }


    public static String getAnnotationName(Annotation annotation) {
        String annotionStr = annotation.toString();
        int a = annotionStr.indexOf("(", 0);
        if (a != -1) {
            annotionStr = annotionStr.substring(0, a);
            String strs[] = annotionStr.split("\\.");
            if (strs != null && strs.length > 0) {
                return strs[strs.length - 1];
            }
        }
        return annotionStr;
    }


    public static <T> T getAnnotationValue(Annotation annotation) {
        try {
            Method method = annotation.getClass().getMethod("value");
            if (method != null) {
                T paramName = (T) method.invoke(annotation);
                return paramName;
            }
        } catch (NoSuchMethodException e) {

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static List<String> ergodic(File file, List<String> resultFileName) {
        File[] files = file.listFiles();
        if (files == null) return resultFileName;// 判断目录下是不是空的
        for (File f : files) {
            if (f.isDirectory()) {// 判断是否文件夹
                ergodic(f, resultFileName);// 调用自身,查找子目录
            } else {
                String path = f.getPath();
                String paths[] = path.split("/src/main/java/");
                String className = paths[1].replaceAll("/", ".");
                resultFileName.add(className);
            }
        }
        return resultFileName;
    }


}
