package com.nokia.hb;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Mapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Collections;

/**
 * mybatis plus生成器
 */
@Slf4j
@SpringBootTest
class MybatisPlusGenerator {
    @Autowired
    private DataSourceProperties dataSourceProperties;

    @Test
    void generate() {
        log.info("dataSourceProperties: {}, {}, {}", dataSourceProperties.getUrl(), dataSourceProperties.getUsername(),
                dataSourceProperties.getPassword());
        FastAutoGenerator g1 = create();
        g1.strategyConfig(builder -> {
            builder.addInclude("user_area", "indicator_template_item")
                    .entityBuilder()
//                    .enableFileOverride()
                    .enableLombok()
                    .enableChainModel()
                    .mapperBuilder()
                    .mapperAnnotation(Mapper.class);
        }).execute();
        FastAutoGenerator g2 = create();
        g2.strategyConfig(builder -> {
            builder.addInclude("user", "indicator_template", "per_cfg_indicator", "per_cfg_cell", "per_cfg_area")
                    .entityBuilder()
//                    .enableFileOverride()
                    .enableLombok()
                    .enableChainModel()
                    .enableTableFieldAnnotation()
                    .mapperBuilder()
                    .mapperAnnotation(Mapper.class);
        }).execute();
    }

    FastAutoGenerator create() {
        return FastAutoGenerator.create(new DataSourceConfig
                        .Builder(dataSourceProperties.getUrl(), dataSourceProperties.getUsername(),
                        dataSourceProperties.getPassword())
                )
                .globalConfig(builder -> {
                    builder.disableOpenDir()
                            .outputDir("src/main/java");
                })
                .packageConfig(builder -> {
                    builder.parent("com.nokia.hb.dao")
                            .pathInfo(Collections.singletonMap(OutputFile.xml, "src/main/resources/mapper"));

                })
                .templateConfig(builder -> {
//                    builder.mapper("");
                    builder.controller("");
                    builder.xml("");
                    builder.service("");
                    builder.serviceImpl("");
                });
    }
}
