package com.trade.service;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;
import com.baomidou.mybatisplus.generator.keywords.MySqlKeyWordsHandler;
import com.trade.common.core.domain.BaseEntity;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.io.IOException;
import java.util.Collections;

/**
 * 代码生成器
 */
public class MysqlGenerator {

    static String packagePath = "com.trade.client";


    /**
     * run this main function
     */
    public static void main(String[] args) {
        Resource resource = new ClassPathResource("");
        String absolutePath = null;
        try {
            absolutePath = resource.getFile().getAbsolutePath();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(absolutePath);
        String rootPath = absolutePath.replace("\\target\\classes", "");
        // 数据源配置
        String url = "jdbc:mysql://localhost:3306/trade-service?useUnicode=true&characterEncoding=utf-8&useSSL=false";
        String username = "root";
        String password = "Root@123";
        DataSourceConfig dsc = new DataSourceConfig.Builder(url, username, password).keyWordsHandler(
                new MySqlKeyWordsHandler()).build();
        // 全局配置
        GlobalConfig gc = new GlobalConfig.Builder().outputDir(rootPath + "\\src\\main\\java").disableOpenDir()
                .fileOverride().enableSwagger().commentDate("yyyy-MM-dd").dateType(DateType.TIME_PACK).build();
        // 包配置
        PackageConfig pc = new PackageConfig.Builder().parent(packagePath).moduleName("companyRelated").xml("mapper.xml")
                .entity("domain").controller("controller").mapper("mapper").service("service")
                .serviceImpl("service.impl")
                .pathInfo(Collections.singletonMap(OutputFile.xml, rootPath + "\\src\\main\\resources\\mapper\\"))
                .build();

        // 策略配置
        StrategyConfig strategy = new StrategyConfig.Builder()
                //指定表
                .addInclude("biz_entity_related")
                .addInclude("biz_entity_related_person")
                //entity策略
                .entityBuilder().superClass(BaseEntity.class).naming(NamingStrategy.underline_to_camel)
                .columnNaming(NamingStrategy.underline_to_camel).enableLombok().enableTableFieldAnnotation()
                .logicDeleteColumnName("deleted")
                .addSuperEntityColumns("gmt_create", "gmt_modified", "deleted")
                .addTableFills(new Column("create_by", FieldFill.INSERT))
                .addTableFills(new Column("update_by", FieldFill.INSERT_UPDATE)).idType(IdType.AUTO)
                //controller策略
                .controllerBuilder()
                //.superClass("com.trade.common.core.controller.BaseController")
                .enableHyphenStyle()
                .enableRestStyle().formatFileName("%sController")
                //service策略
                .serviceBuilder()
                .superServiceImplClass(ServiceImpl.class).formatServiceFileName("%sService")
                .formatServiceImplFileName("%sServiceImp")
                //mapper策略
                .mapperBuilder().superClass(BaseMapper.class).enableMapperAnnotation().enableBaseResultMap()
                .enableBaseColumnList().formatMapperFileName("%sMapper").formatXmlFileName("%sMapper").build();
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator(dsc).global(gc).packageInfo(pc).strategy(strategy);
        mpg.execute();
    }
}
