package generator.defined;

import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import java.util.List;


/**
 * @author zhangchenbiao
 * @FileName: MyPluginAdapter
 * @Description: 自定义生成器
 * @date 2021-11-18 14:11
 */
public class TKMyPluginAdapter extends PluginAdapter {

    @Override
    public boolean validate(List<String> list) {
        return true;
    }

    @Override
    public void initialized(IntrospectedTable introspectedTable) {
        String tableName = introspectedTable.getFullyQualifiedTableNameAtRuntime();
        // 将tableName转换为驼峰命名（这里假设你已经有了相应的方法）
        String camelCaseTableName = toCamelCase(tableName.replace("xfsg", ""));

        // 设置 domainObjectName
        introspectedTable.setIbatis2SqlMapFileName(camelCaseTableName + "DO");

        // 设置 mapperName，注意这个属性可能需要在生成XML后手动设置到XML文件中
        introspectedTable.setMyBatis3XmlMapperFileName(camelCaseTableName + "Mapper.xml");
    }

    public static String toCamelCase(String input) {
        if (input == null || input.isEmpty()) {
            return "";
        }

        StringBuilder camelCaseString = new StringBuilder(input.length());
        boolean capitalizeNext = false;

        for (int i = 0; i < input.length(); i++) {
            char ch = input.charAt(i);

            if (ch == '_') {
                capitalizeNext = true;
            } else if (capitalizeNext) {
                camelCaseString.append(Character.toUpperCase(ch));
                capitalizeNext = false;
            } else {
                camelCaseString.append(Character.toLowerCase(ch));
            }
        }

        return camelCaseString.toString();
    }

}
