package com.kym.admin.utils;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.kym.entity.BaseEntity;
import com.kym.mapper.mybatisplus.MyBaseMapper;
import com.kym.service.mybatisplus.MyBaseService;
import com.kym.service.mybatisplus.MyBaseServiceImpl;
import org.apache.poi.ss.formula.functions.T;

import java.sql.Types;
import java.util.Map;

/**
 * mybatis-plus-generator代码生成
 *
 * @author skyline
 */
public class MybatisPlusGeneratorForAdmin {

    public static void main(String[] args) {
        // 指定数据源
        FastAutoGenerator.create("jdbc:mysql://121.40.98.15:3307/charge_admin", "root", "KuaiyuMan/*-")
                .globalConfig(builder -> {
                    builder.author("skyline")
                            // 指定输出目录
                            .disableOpenDir();
                })
                .dataSourceConfig(builder ->
                        builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                            int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                            if (typeCode == Types.TINYINT) {
                                // 自定义类型转换
                                return DbColumnType.INTEGER;
                            }
                            return typeRegistry.getColumnType(metaInfo);
                        })
                )
                .packageConfig(builder -> {
                    // 设置父包名
                    builder
                            // 设置父包模块名
                            .parent("com.kym")
                            .entity("entity.admin")
                            .service("service.admin")
                            .serviceImpl("service.admin.impl")
                            .mapper("mapper.admin")
                            .xml("mapper.xml")
                            .controller("admin.controller")
                            // 设置mapperXml生成路径
                            .pathInfo(Map.of(
                                    OutputFile.entity, "D://快与慢充电桩/code/charge-java/entity/src/main/java/com/kym/entity/admin",
                                    OutputFile.service, "D://快与慢充电桩/code/charge-java/service/src/main/java/com/kym/service/admin",
                                    OutputFile.serviceImpl, "D://快与慢充电桩/code/charge-java/service/src/main/java/com/kym/service/admin/impl",
                                    OutputFile.mapper, "D://快与慢充电桩/code/charge-java/mapper/src/main/java/com/kym/mapper/admin",
                                    OutputFile.controller, "D://快与慢充电桩/code/charge-java/admin/src/main/java/com/kym/admin/controller",
                                    OutputFile.xml, "D://快与慢充电桩/code/charge-java/mapper/src/main/resources/mappers/admin"

                            ));
                })

                .strategyConfig(builder -> {
                    // 设置需要生成的表名
                    builder.addInclude(
//                            "t_equipment_info",
//                            "t_connector_info"
//                            "t_invoice_detail"
//                            "t_coupon",
//                            "t_system_notice",
//                            "t_station_notice"
                            )
                            // 设置过滤表前缀
                            .addTablePrefix("t_");

                    // entity配置
                    builder.entityBuilder()
                            .superClass(BaseEntity.class)
                            .addIgnoreColumns("id","create_time","update_time")
                            .enableFileOverride()
                            .enableLombok();

                    // controller配置
                    builder.controllerBuilder()
                            .enableFileOverride()
                            .enableRestStyle()
                            .enableHyphenStyle()
//                            .superClass(BaseController.class)
                            .formatFileName("%sController");

                    // service配置
                    builder.serviceBuilder()
                            .superServiceClass(MyBaseService.class)
                            .superServiceImplClass(MyBaseServiceImpl.class)
                            .enableFileOverride()
                            .formatServiceFileName("%sService")
                            .formatServiceImplFileName("%sServiceImpl");

                    // mapper配置
                    builder.mapperBuilder()
                            .superClass(MyBaseMapper.class)
                            .enableFileOverride()
                            .enableBaseResultMap()
                            .enableBaseColumnList()
                            .formatMapperFileName("%sMapper")
                            .formatXmlFileName("%sMapper")
                            .build();


                    builder.build();

                })

                // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();
    }

}
