package com.greenorange.promotion.generator;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.builder.CustomFile;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;


import java.util.*;

/**
 * @author chenxinzhi
 * @date 2025/3/30
 **/
public class Generator {

    // 数据源配置
    private static final String DATASOURCE_URL = "jdbc:mysql://160.202.242.36:3306/qingcheng_dev?serverTimezone=Asia/Shanghai";
    private static final String USERNAME = "qingcheng";
    private static final String PASSWORD = "Qc@8ls2jf";

    // 输出路径
    private static final String OUTPUT_PATH = System.getProperty("user.dir");
    // 根路径
    private static final String ROOT_PATH = "/src/main/java";

    // 作者
    private static final String AUTHOR = "chenxinzhi";
    // 表注释
    private static final String TABLE_COMMENT = "用户绩效汇总";
    // 实体类名
    private static final String ENTITY_NAME = "UserPerformanceSummary";
    // 表名
    private static final String TABLE_NAME = "user_performance_summary";
    // 实体类属性名
    private static final String ENTITY_NAME_LOWER = "userPerformanceSummary";

    // 父包名
    private static final String PARENT_PATH = "com.greenorange.promotion";
    // 子包名
    private static final String CONTROLLER_PACKAGE = "controller." + ENTITY_NAME_LOWER;
    private static final String DTO_PACKAGE = "model.dto." + ENTITY_NAME_LOWER;
    private static final String VO_PACKAGE = "model.vo." + ENTITY_NAME_LOWER;

    // 生成的文件后缀名
    private static final String DTO_ADD_REQUEST = "AddRequest.java";
    private static final String DTO_UPDATE_REQUEST = "UpdateRequest.java";
    private static final String DTO_QUERY_REQUEST = "QueryRequest.java";
    private static final String VO = "VO.java";

    // 模版文件路径
    private static final String CONTROLLER_TEMPLATE = "/templates/controller.java";
    private static final String DTO_ADD_REQUEST_TEMPLATE = "/templates/dto/AddRequest.java.vm";
    private static final String DTO_UPDATE_REQUEST_TEMPLATE = "/templates/dto/UpdateRequest.java.vm";
    private static final String DTO_QUERY_REQUEST_TEMPLATE = "/templates/dto/QueryRequest.java.vm";
    private static final String VO_TEMPLATE = "/templates/vo/VO.java.vm";




    public static void main(String[] args) {
        //1、配置数据源
        FastAutoGenerator.create(DATASOURCE_URL, USERNAME, PASSWORD)
        //2、全局配置
        .globalConfig(builder -> {
            builder.disableOpenDir() // 禁止打开输出目录 默认 true
            .outputDir(OUTPUT_PATH + ROOT_PATH)   // 设置输出路径：项目的 java 目录下
            .author(AUTHOR) // 设置作者名
            .dateType(DateType.TIME_PACK)   // 定义生成的实体类中日期的类型 TIME_PACK=LocalDateTime;ONLY_DATE=Date;
            .commentDate("yyyy/MM/dd"); // 注释日期 默认值 yyyy-MM-dd
        })
        //3、包配置
        .packageConfig(builder -> {
            builder.parent(PARENT_PATH) // 父包名 默认值 com.baomidou
            .controller(CONTROLLER_PACKAGE); // Controller 包名 默认值 controller
        })
        //4、模版配置
//        .templateConfig(builder -> builder
//                .controller(CONTROLLER_TEMPLATE))
                //5、策略配置
                .strategyConfig(builder -> {
                    builder.addInclude(TABLE_NAME) // 设置需要生成的数据表名
                            .controllerBuilder()
//                            .enableFileOverride() // 覆盖controller
                            .enableRestStyle()  // 开启生成 @RestController 控制器
                            .formatFileName("%sController"); // 格式化 Controller 类文件名称，%s进行匹配表名，如 UserController
                    builder.entityBuilder().disable(); // 禁止生成 Entity
                    builder.serviceBuilder().disable(); // 禁止生成 Service
                    builder.mapperBuilder().disable(); // 禁止生成 Mapper
                })
                //6、自定义配置
                .injectionConfig(consumer -> {
                    Map<String, Object> customMap = new HashMap<>();
                    customMap.put("entityName", ENTITY_NAME); // 示例值
                    customMap.put("entityNameLower", ENTITY_NAME_LOWER);
                    customMap.put("entityComment", TABLE_COMMENT); // 示例值
                    customMap.put("parentPackage", PARENT_PATH);
                    customMap.put("controllerPackage", CONTROLLER_PACKAGE);
                    customMap.put("dtoPackage", DTO_PACKAGE);
                    customMap.put("voPackage", VO_PACKAGE);

                    consumer.customMap(customMap);
                    // DTO
                    List<CustomFile> customFiles = new ArrayList<>();
                    customFiles.add(new CustomFile.Builder().packageName(DTO_PACKAGE).fileName(DTO_ADD_REQUEST)
                            .templatePath(DTO_ADD_REQUEST_TEMPLATE).build());
                    customFiles.add(new CustomFile.Builder().packageName(DTO_PACKAGE).fileName(DTO_UPDATE_REQUEST)
                            .templatePath(DTO_UPDATE_REQUEST_TEMPLATE).build());
                    customFiles.add(new CustomFile.Builder().packageName(DTO_PACKAGE).fileName(DTO_QUERY_REQUEST)
                            .templatePath(DTO_QUERY_REQUEST_TEMPLATE).build());
                    customFiles.add(new CustomFile.Builder().packageName(VO_PACKAGE).fileName(VO)
                            .templatePath(VO_TEMPLATE).build());
                    consumer.customFile(customFiles);
                })
                //7、模板
                .templateEngine(new VelocityTemplateEngine())

                //8、执行
                .execute();
    }
}
