package com.kym.miniapp.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 java.sql.Types;
import java.util.Map;

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

    public static void main(String[] args) {
        // 指定数据源
        FastAutoGenerator.create("jdbc:mysql://121.40.98.15:3307/car-wash", "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.miniapp")
                            .service("service.miniapp")
                            .serviceImpl("service.miniapp.impl")
                            .mapper("mapper.miniapp")
                            .xml("mapper.xml")
                            .controller("miniapp.controller")
                            // 设置mapperXml生成路径
                            .pathInfo(Map.of(
                                    OutputFile.entity, "D://快与慢充电桩/code/car-wash-java/car-wash-entity/src/main/java/com/kym/entity/miniapp",
                                    OutputFile.service, "D://快与慢充电桩/code/car-wash-java/car-wash-service/src/main/java/com/kym/service/miniapp",
                                    OutputFile.serviceImpl, "D://快与慢充电桩/code/car-wash-java/car-wash-service/src/main/java/com/kym/service/miniapp/impl",
                                    OutputFile.mapper, "D://快与慢充电桩/code/car-wash-java/car-wash-mapper/src/main/java/com/kym/mapper/miniapp",
                                    OutputFile.controller, "D://快与慢充电桩/code/car-wash-java/car-wash-miniapp/src/main/java/com/kym/miniapp/controller",
                                    OutputFile.xml, "D://快与慢充电桩/code/car-wash-java/car-wash-mapper/src/main/resources/mappers/miniapp"

                            ));
                })

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

                    // entity配置
                    builder.entityBuilder()
                            .superClass(BaseEntity.class)
                            .addIgnoreColumns("id", "company_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();
    }

}
