package com.cpop.generator.template;

import com.mybatisflex.codegen.config.EntityConfig;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.mybatisflex.codegen.config.PackageConfig;
import com.mybatisflex.codegen.entity.Table;
import com.mybatisflex.codegen.generator.IGenerator;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: DB
 * @Date: 2023/08/04/17:07
 * @Description:
 */
public class DtoGenerator implements IGenerator {

    private String templatePath = System.getProperty("user.dir") + "/Cpop-Generator/src/main/resources/template/enjoy/dto.tpl";

    @Override
    public String getTemplatePath() {
        return templatePath;
    }

    @Override
    public void setTemplatePath(String templatePath) {
        this.templatePath = templatePath;
    }

    @Override
    public void generate(Table table, GlobalConfig globalConfig) {

        if (!globalConfig.isEntityGenerateEnable()) {
            return;
        }

        PackageConfig packageConfig = globalConfig.getPackageConfig();
        EntityConfig entityConfig = globalConfig.getEntityConfig();

        //获取路径
        String dtoPackagePath = packageConfig.getBasePackage().replace(".", "/") + "/dto";
        File dtoJavaFile = new File(packageConfig.getSourceDir(), dtoPackagePath + "/" +
                table.buildEntityClassName() + "Dto.java");

        if (dtoJavaFile.exists() && !entityConfig.isOverwriteEnable()) {
            return;
        }

        Map<String, Object> params = new HashMap<>(4);
        params.put("table", table);
        params.put("entityConfig", entityConfig);
        params.put("packageConfig", packageConfig);
        params.put("javadocConfig", globalConfig.getJavadocConfig());

        globalConfig.getTemplateConfig().getTemplate().generate(params, templatePath, dtoJavaFile);
        System.out.println("Dto ---> " + dtoJavaFile);
    }
}
