package net.northking.cctp.maven.plugin;

import net.northking.cctp.maven.analyse.JDBCConfig;
import net.northking.cctp.maven.analyse.dialects.DBDialects;
import net.northking.cctp.maven.analyse.dialects.Dialect;
import net.northking.cctp.maven.analyse.entity.TableRef;
import net.northking.cctp.maven.analyse.util.DBUtil;
import net.northking.cctp.maven.gen.CCTPGenerator;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;

import java.util.List;

/**
 * 基于 spring boot 的代码生成器 Maven插件
 *
 * @author xiusong.xie
 * @since 1.0
 */
//@Mojo(name = "main", threadSafe = true)
public class MainMojo extends AbstractMojo
{
  /**
   * Maven工程对象
   **/
  @Parameter(defaultValue = "${project}", readonly = true, required = true)
  private MavenProject project;

  /**
   * jdbcDriver
   */
  @Parameter(required = true)
  private String jdbcDriver;

  /**
   * jdbcUrl
   */
  @Parameter(required = true)
  private String jdbcUrl;

  /**
   * jdbcUsername
   */
  @Parameter(required = true)
  private String jdbcUsername;

  /**
   * jdbcPassword
   */
  @Parameter(required = true)
  private String jdbcPassword;

  /**
   * database
   */
  @Parameter(required = true)
  private String database;

  /**
   * schema
   */
  @Parameter(required = true)
  private String schema;

  /**
   * basePackage
   */
  @Parameter(required = true)
  private String basePackage;

  /**
   * 包含的表名
   */
  @Parameter(required = false)
  private String[] tableNames;

  /**
   * 生成数据库表的实体对象
   *
   * @throws MojoExecutionException 异常
   * @throws MojoFailureException   异常
   */
  public final void execute() throws MojoExecutionException, MojoFailureException
  {
    init();
    List<TableRef> tableRefs;
    Dialect dialect = DBDialects.getInstance().getDialect();
    try
    {
      // 获取所有的表
      tableRefs = dialect.getTables(schema, tableNames);
    } catch (Exception e)
    {
      throw new MojoExecutionException("", e);
    } finally
    {
      DBUtil.getInstance().close();
    }

    CCTPGenerator generator = new CCTPGenerator(project.getBasedir(), basePackage);
    for (TableRef tableRef : tableRefs)
    {
      if (isMyTable(tableRef))
      {
        generator.genMainCode(tableRef);
      }
    }

  }

  /**
   * 判断是否在名单中
   *
   * @param tableRef 数据库中的表
   * @return 是否在名单中
   */
  private boolean isMyTable(TableRef tableRef)
  {
    if (tableNames == null || tableNames.length == 0)
    {
      return true;
    }
    for (String tableName : tableNames)
    {
      if (tableRef.getTableName().equalsIgnoreCase(tableName))
      {
        return true;
      }
    }
    return false;
  }

  /**
   * 初始化
   */
  private void init()
  {
    JDBCConfig jdbcConfig = JDBCConfig.getInstance();
    jdbcConfig.setDriverClass(jdbcDriver);
    jdbcConfig.setUrl(jdbcUrl);
    jdbcConfig.setCatalog(database);
    jdbcConfig.setUserId(jdbcUsername);
    jdbcConfig.setPassword(jdbcPassword);
  }
}
