package com.fdkankan.modeling.generate;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class AutoGenerate {


    public static void main(String[] args) {

        System.out.println("相对路径指定到："+ System.getProperty("user.dir"));


        String path =System.getProperty("user.dir") + "\\4dkankan-center-modeling";

        generate(path,"modeling", getTables(new String[]{
                "t_build_log",
        }));

//        generate(path,"goods", getTables(new String[]{
//                        "t_camera","t_camera_detail","t_camera_out","t_camera_space","t_camera_version",
//                        "t_company","t_goods","t_goods_sku","t_cart","t_goods_spec",
//                        "t_goods_spec_value","t_goods_spu_spec","t_sn_code"
//        }));
//
//        generate(path,"order", getTables(new String[]{
//                        "t_increment_order","t_invoice","t_order","t_order_item",
//                        "t_pre_sale","t_space_sdk","t_trade_log","t_commerce_order","t_download_order","t_expansion_order"
//        }));
//        generate(path,"order", getTables(new String[]{
//                        "t_virtual_order"
//        }));
//
//        generate(path,"user", getTables(new String[]{
//                        "t_user","t_user_increment","t_manager","t_province","t_increment_type","t_intercom_message","t_receiver_info"
//        }));
    }

    public static List<String> getTables(String [] tableNames){
        return new ArrayList<>(Arrays.asList(tableNames));
    }


    public static void  generate(String path,String moduleName,  List<String> tables){
        FastAutoGenerator.create("jdbc:mysql://192.168.0.47:13306/4dkankan-center-modeling",
                "root","4dkk2020cuikuan%")
                .globalConfig(builder -> {
                    builder.author("")               //作者
                            .outputDir(path+"\\src\\main\\java")    //输出路径(写到java目录)
                            //.enableSwagger()           //开启swagger
                            .commentDate("yyyy-MM-dd")
                            .dateType(DateType.ONLY_DATE)
                            .fileOverride();            //开启覆盖之前生成的文件

                })
                .packageConfig(builder -> {
                    builder.parent("com.fdkankan")
                            .moduleName(moduleName)
                            .entity("entity")
                            .service("service")
                            .serviceImpl("service.impl")
                            .controller("controller")
                            .mapper("mapper")
                            .xml("test.mapper")
                            .pathInfo(Collections.singletonMap(OutputFile.mapperXml,path+"\\src\\main\\resources\\mapper\\"+moduleName));
                })
                .strategyConfig(builder -> {
                    builder.addInclude(tables)
                            .addTablePrefix("t_")

                            .serviceBuilder()
                            .formatServiceFileName("I%sService")
                            .formatServiceImplFileName("%sServiceImpl")

                            .entityBuilder()
                            .enableLombok()
                            .logicDeleteColumnName("tb_status")
                            .enableTableFieldAnnotation()
//                            .superClass(BaseEntity.class)

                            .controllerBuilder()
                            .formatFileName("%sController")
                            .enableRestStyle()

                            .mapperBuilder()
                            .superClass(BaseMapper.class)
                            .formatMapperFileName("I%sMapper")
                            .enableMapperAnnotation()
                            .formatXmlFileName("%sMapper");
                })
                // .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }
}
