package net.northking.cctp.maven.tenant;

import net.northking.cctp.maven.gen.CCTPGenerator;
import net.northking.cctp.maven.plugin.AbsDatabaseLoadMojo;
import net.northking.cctp.maven.analyse.entity.TableRef;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;

/**
 * 数据库实体类Maven插件
 *
 * @author xiusong.xie
 * @since 1.0
 */
@Mojo(name = "saas-mybatis", threadSafe = true)
public class TenantMybatisMojo extends AbsDatabaseLoadMojo
{
  /**
   * Maven工程对象
   **/
  @Parameter(defaultValue = "${project}", readonly = true, required = true)
  private MavenProject project;


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

  /**
   * 生成数据库表的实体对象
   *
   * @throws MojoExecutionException 异常
   * @throws MojoFailureException   异常
   */
  public final void execute() throws MojoExecutionException, MojoFailureException
  {
    CCTPGenerator generator = new CCTPGenerator(project.getBasedir(), basePackage);
    for (TableRef tableRef : loadTables())
    {
      if (tableNames != null)
      {
        for (String tableName : tableNames)
        {
          if (tableRef.getTableName().equalsIgnoreCase(tableName))
          {
            generator.genTenantMybatisAll(tableRef);
            break;
          }
        }
      } else
      {
        generator.genTenantMybatisAll(tableRef);
      }
    }

  }

}
