package zr.zrpower.dbmanage.service;

import zr.zrpower.common.util.Log;
import zr.zrpower.common.db.dbSet;
import zr.zrpower.common.db.dbEngine;
import zr.zrpower.dbmanage.*;
import zr.zrpower.common.util.SysPreperty;
import zr.zrpower.common.util.FunctionMessage;
import zr.zrpower.common.db.*;

/**
 * <p>Title: zrpower平台</p>
 * <p>Description: 表分类管理</p>
 */
public class TableSpaceService
{
  static private Log log; //日志
  dbEngine dbengine; //数据库引擎
  static private int clients=0;

  public  TableSpaceService()
  {
    dbengine = new dbEngine(SysPreperty.getProperty().MainDataSource,
                             SysPreperty.getProperty().IsConvert);
    dbengine.initialize();

    if (clients<1)
    {
     log = new Log();
     log.SetLogForClass("TableSpaceServiceBean");
     log.SetLogFile("TableSpace.log");
    }
    clients++;
  }

  /**
   * 增加表空间信息
   * @param SP BPIP_TABLESPACE   新表空间实体
   * @return FunctionMessage  保存是否成功,成功或是失败消息
    * 
   */

  public FunctionMessage AddSP(BPIP_TABLESPACE SP) {
    String  strSql= "";
    dbSet mdbset;
    FunctionMessage fm = new FunctionMessage(1);
    try {

      strSql = "Select * From BPIP_TABLESPACE Where ID='"+ SP.getID() + "'";
      mdbset = dbengine.QuerySQL(strSql);
      if (mdbset != null) {
        fm.setResult(false);
        fm.setMessage("表分类【"+SP.getID()+"】已经存在");
        mdbset = null;
      }
      else {

          String strMaxNo = GetMaxSPACEID();
          SP.setID(strMaxNo);//设置ID
          if (dbengine.ExecuteInsert(SP.getData())) {
            mdbset = dbengine.QuerySQL(strSql);
            fm.setMessage("录入成功");
            fm.setResult(true);
          }
          fm.setResult(true);

      }
    }

    catch (Exception e) {
      fm.setResult(false);
      fm.setMessage("调用方法AddSP出现异常" + e.toString());
      return fm;
    }
     return fm;
  }

  /**
   * 编辑表空间
   * @param SP BPIP_TABLESPACE 编辑的实体
   * @return FunctionMessage
   * 
   */

  public FunctionMessage EditSP(BPIP_TABLESPACE SP) {
    boolean isOk = false;
    FunctionMessage fm = new FunctionMessage(1);
     try {
      dbRow row = SP.getData();
      isOk = dbengine.ExecuteEdit(row,"ID="+SP.getID());

      if (isOk) {
        fm.setResult(true);
        fm.setMessage("表分类修改成功");
      }
      else {
        fm.setResult(false);
        fm.setMessage("表分类【"+SP.getCHINESENAME()+"】不存在");
      }

    }
    catch (Exception e) {
      fm.setResult(false);
      fm.setMessage("调用方法EditSP出现异常" + e.toString());
      return fm;
    }

   return fm;
  }

  /**
   * 删除表空间
   * @return FunctionMessage
   * 
   */

  public FunctionMessage DeleteSP(String ID) {
    boolean isOk = false;
    int getNum = 0;
    FunctionMessage fm = new FunctionMessage(1);
    String strSql = "select count(TABLEID) as num from BPIP_TABLE where TABLESPACE = '"+ID+"'";
    dbSet mdbset = dbengine.QuerySQL(strSql.toString());
    if (mdbset!=null && mdbset.RowCount()>0)
    {
       getNum = mdbset.Row(0).Column("num").getInteger();
    }
    if (getNum>0)
    {
       fm.setResult(false);
       fm.setMessage("此表分类下存在表，不能删除！");
       return fm;

    }
    else
    {
    try {
      isOk = dbengine.ExecuteDelete("BPIP_TABLESPACE","ID="+ID);
      if (isOk) {
        fm.setResult(true);
        fm.setMessage("表分类【"+ID+"】已经删除");
      }
      else {
        fm.setMessage("删除不成功");
        fm.setResult(false);
      }
    }
    catch (Exception e) {
      fm.setResult(false);
      fm.setMessage("调用方法DeleteSP出现异常" + e.toString());
      return fm;
    }
    }
    return fm;
  }

  /**
   * 获取表分类列表
   * @param BPIP_TABLESPACE String
   * @return String[][]
   */
  public BPIP_TABLESPACE[] GetBpipTableSpaceList(String condition) {
    BPIP_TABLESPACE bgs[] = null;
    String  strSql= "";
    try {
      if (condition.trim().length() == 0) {
        strSql = "Select * from BPIP_TABLESPACE order by ID";
      }
      else {
        strSql = "Select * from BPIP_TABLESPACE Where ID = '"+condition+"'order by ID";
      }
      dbSet mdbset = dbengine.QuerySQL(strSql);
      bgs = new BPIP_TABLESPACE[mdbset.RowCount()];
      if (mdbset.RowCount() == 0) {
        return null;
      }
      else {
        for (int i = 0; i < mdbset.RowCount(); i++) {
          bgs[i] = new BPIP_TABLESPACE();
          bgs[i].fullData(mdbset.Row(i));
        }
        mdbset = null;
      }
    }
    catch (Exception e) {
      return null;
    }
    return bgs;
  }

  /**
   * 获取表分类
   * @param BPIP_TABLESPACE String
   * @return String[][]
   */
  public BPIP_TABLESPACE getSPACEID(String ID) {
    BPIP_TABLESPACE bp = new BPIP_TABLESPACE();
    String strSql= "";
      try {
      strSql = "Select * From BPIP_TABLESPACE Where ID='" +ID.trim() +"'";
      dbSet mdbset = dbengine.QuerySQL(strSql);
      if (mdbset == null) {
        return null;
      }
      else {
        if (mdbset.RowCount() > 0) {
          bp.fullData(mdbset.Row(0));
        }
        mdbset = null;
      }

    }
    catch (Exception e) {
      return null;
    }
    return bp;
  }

  /**
   * 得到表分类数
   */
  public String SumTableSpace() {
    String sum = "0";
    String strSql;
    dbSet mdbset;
    strSql = "select count(*) as count from BPIP_TABLESPACE";
    mdbset = dbengine.QuerySQL(strSql);
    if (mdbset != null) {
      sum = String.valueOf(mdbset.Row(0).Column("count").getInteger());
      mdbset = null;
    }
    return sum;
  }

  /**
   * 功能或作用：取出最大ID
   * @Return String 执行后返回一个最大ID
   */
  private String GetMaxSPACEID() {
    dbSet ds = null;
    String Sql = "", MaxNo = "";
    int LenMaxNo = 0;
    Sql = "SELECT MAX(ID) AS MaxNo FROM BPIP_TABLESPACE";
    try {
      ds = dbengine.QuerySQL(Sql);
      if (ds != null) {
        if (ds.Row(0).Column("MaxNo").getString().trim().length() == 0) {
          MaxNo = "0000000000000000000000001";
        }
        else {
          MaxNo = ds.Row(0).Column("MaxNo").getString().trim();
          MaxNo = String.valueOf(Integer.parseInt(MaxNo) + 1);
          LenMaxNo = MaxNo.length();
          MaxNo = "0000000000000000000000000" + MaxNo;
        }
        ds = null;
      }

      MaxNo = MaxNo.substring(23 + LenMaxNo);
    }
    catch (Exception ex) {
      System.err.println(ex.getMessage());
    }
    return MaxNo;
  }

  /**
    * 根据表空间编号获取表空间中文名称
    * @param UnitID String   表空间编号
    * @return String
    */
   public String GetSpaceCHINESENAME(String ID) {
     String returnValue = "";
     String  strSQL= "";
     strSQL = "Select CHINESENAME  From BPIP_TABLESPACE Where ID = '" + ID + "'";
     dbSet dbset = dbengine.QuerySQL(strSQL.toString());
     if (dbset != null && dbset.RowCount() > 0) {
       returnValue = dbset.Row(0).Column("CHINESENAME").getString();
       dbset = null;
     }
     return returnValue;
  }
  /**
   * 根据表编号获取表空间
   * @param UnitID String   表编号
   * @return String
   */
  public String GetSpaceNo(String ID) {
    String returnValue = "";
    String  strSQL= "";
    strSQL = "Select TABLESPACE  From BPIP_TABLE Where TABLEID = '" + ID + "'";
    dbSet dbset = dbengine.QuerySQL(strSQL);
    if (dbset != null && dbset.RowCount() > 0) {
      returnValue = dbset.Row(0).Column("TABLESPACE").getString();
      dbset = null;
    }
    return returnValue;
  }

  /**
     * 功能或作用：取出最大ID
     * @Return String 执行后返回一个最大ID
     */
  public String  GetSpaceId() {
    String returnValue = "";
    String strSQL;
    strSQL = "Select MAX(ID) AS MaxNo From BPIP_TABLESPACE";
    dbSet dbset = dbengine.QuerySQL(strSQL);
    if(dbset!=null&&dbset.RowCount()>0){
        returnValue = dbset.Row(0).Column("MaxNo").getString();
        dbset = null;
    }
     return returnValue;
 }

}
