package xiaoqu.home.open.utils;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.TemplateConfig;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

public class CodeCreateAuto {

    public static void main(String[] args) {
        //选择数据源
        //1-xiaoqu_comples 2:xiaoqu_intellectual
        int dataSource = 1;
//		int dataSource =2;
//		int dataSource =3;
        // 3.策略配置
        StrategyConfig stConfig = new StrategyConfig();
//        stConfig.setLogicDeleteFieldName("soft_delete");//删除标识
        stConfig.setCapitalMode(true)// 全局大写命名
        .setEntityTableFieldAnnotationEnable(true)
		.setInclude("user") // 如果只生成一个表
//		.setInclude("authority","button","button_field","button_webpage_authority","field_webpage_authority","label_manage","matter",
//				"matter_type","button","button_field","button_webpage_authority","field_webpage_authority","label_manage","matter"
//				) // 如果只生成一个表
//                .setInclude("login_log") // 如果只生成一个表
                .setNaming(NamingStrategy.underline_to_camel);// 数据库表映射到实体的命名策略
        // 1. 全局配置
        GlobalConfig config = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        // 是否支持AR模式
        config.setActiveRecord(true).setAuthor("wh") // 作者
                .setOutputDir(projectPath+"\\src\\main\\java") // 生成路径
                .setFileOverride(true)// 是否文件覆盖，如果多次
                .setServiceName("%sService") // 设置生成的service接口名首字母是否为I
                .setIdType(IdType.AUTO) // 主键策略
                .setOpen(false)
                .setBaseResultMap(true)
                .setBaseColumnList(true)
                .setSwagger2(true);
        // 2. 数据源配置
        DataSourceConfig dsConfig = new DataSourceConfig();
        //选择的数据源
        switch (dataSource) {
            case 1:
                dsConfig.setUrl("jdbc:mysql://rm-2zerw72wwh56sj214xo.mysql.rds.aliyuncs.com:3306/xiaoqu_complex?useUnicode=true&useSSL=false&characterEncoding=utf8");
                break;
            case 2:
                dsConfig.setUrl("jdbc:mysql://192.168.1.181:3306/xiaoqu_intellectual_d?useUnicode=true&useSSL=false&characterEncoding=utf8");
                break;
//            case 3:
//                dsConfig.setUrl("jdbc:mysql://192.168.1.186:3306/xiaoqu_franchisee?useUnicode=true&useSSL=false&characterEncoding=utf8");
//                break;
            default:
                break;
        }
        // dsc.setSchemaName("public");
        dsConfig.setDriverName("com.mysql.cj.jdbc.Driver");
//        dsConfig.setUsername("root");
//        dsConfig.setPassword("kaixinjiuhao");
        dsConfig.setUsername("xiaoqu_yanfa");
        dsConfig.setPassword("Xdny7dvk");
        // 自定义数据库表字段类型转换【可选】
        dsConfig.setTypeConvert(new MySqlTypeConvert() {
            @Override
            public DbColumnType processTypeConvert(GlobalConfig globalConfig, String fieldType) {
                //将数据库中datetime转换成date
                if (fieldType.toLowerCase().contains("datetime")) {
                    return DbColumnType.DATE;
                }
                return (DbColumnType) super.processTypeConvert(globalConfig, fieldType);
            }
        });
       
        // 4.包名策略
        // 配置模板
        TemplateConfig templateConfig = new TemplateConfig();

        //控制 不生成 controller
        templateConfig.setController("");

        PackageConfig pkConfig = new PackageConfig();
        pkConfig.setParent("xiaoqu.home.open")// 父包名
                .setController("")
                .setEntity("model.db" + dataSource)
                .setService("service.db" + dataSource)
                .setServiceImpl("service.db" + dataSource + ".impl")
                .setMapper("mapper.db" + dataSource)
                .setXml("mapper.db" + dataSource);
        // 5.整合配置
        AutoGenerator ag = new AutoGenerator().setTemplate(templateConfig).setGlobalConfig(config).setDataSource(dsConfig).setStrategy(stConfig).setPackageInfo(pkConfig);

        ag.execute();
    }

}
