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: 字段名表管理EJB服务</p>
 * <p> public FunctionMessage  AddTC(BPIP_FIELD TC)         //增加字段名信息</p>
 * <p> public FunctionMessage  EditTC(BPIP_FIELD TC)        //编辑字段名信息</p>
 * <p> public FunctionMessage  DeleteTC(String ID)          //删除字段名信息</p>
 * <p> public BPIP_FIELD[]  GetBpipFieldList(String strwhere)//获取字段名列表</p>
 * <p> public BPIP_FIELD getFieldId(String FIELDID)         //获取字段名编号</p>
 * <p> public String GetMaxFIELDID()                        //获取字段名最大编号</p>
 * <p> public String GetMinTABLEID(String strok)            //获取字段名最小编号</p>
 * <p> public BPIP_FIELD[]  GetCHINESENAME(String strWhere) //根据表编号获取字段中文名称</p>
 * <p> public BPIP_FIELD[]  GetFIELDNAME(String strWhere2)  //根据表编号获取字段名称</p>
 * <p> public String  GetTableName(String TableId)          //根据表编号获取表名称</p>
 * <p> public String  GetTableCHINESENAME(String TableId)   //根据表编号获取表中文名称</p>
 * <p> public String  GetFieldCHINESENAME(String fieldId)   //根据字段编号获取字段中文名称</p>
 * <p> public String SumTable(String ID)                    //根据表编号获取字段记录</p>
 * <p> public String  GetTaNAME(String TableId)             //根据表编号和字段编号获取表中文名称和字段中文名称

 * @author NFZR
 * 
 */
public class FieldService
 {
 static private Log log;                            //日志
 dbEngine dbengine;                  //数据库引擎
 static private int clients=0;

 public FieldService()
 {
   dbengine = new dbEngine(SysPreperty.getProperty().MainDataSource,SysPreperty.getProperty().IsConvert);
   dbengine.initialize();
   if (clients<1)
   {
     log = new Log();
     log.SetLogForClass("FieldServiceBean");
     log.SetLogFile("FieldServiceBean.log");
   }
   clients++;
 }

/**
 * 增加字段名信息
 * @param TC BPIP_FIELD   新字段名表实体
 * @return FunctionMessage  保存是否成功,成功或是失败消息
 * 
 */

public FunctionMessage  AddTC(BPIP_FIELD TC)
 {
  String  strSql = "";
  dbSet mdbset;
  FunctionMessage fm = new FunctionMessage(1);
   try
   {
     strSql = "Select * From BPIP_FIELD Where FIELDID='"+TC.getFIELDID()+"'";
      mdbset = dbengine.QuerySQL(strSql);
       if (mdbset != null) {
         fm.setResult(false);
         fm.setMessage("字段名【"+TC.getFIELDID()+"】已经存在");
         mdbset = null;
       }
       else {
         String strMaxNo = GetMaxFIELDID();
             TC.setFIELDID(strMaxNo);//设置FIELDID
          if (dbengine.ExecuteInsert(TC.getData())) {
           fm.setMessage("录入成功");
           fm.setResult(true);
         }
       }
}
    catch(Exception e)
  {
      fm.setResult(false);
      fm.setMessage("调用方法AddTC出现异常"+e.toString());
      return fm;
  }
  return fm;
 }
 /**
      * 编辑字段名表
      * @param TC BPIP_FIELD 编辑的实体
      * @return FunctionMessage
      * 
      */

    public FunctionMessage  EditTC(BPIP_FIELD TC)
    {
    boolean isOk=false;
    FunctionMessage fm = new FunctionMessage(1);
     try
     {
      dbRow row =TC.getData();
      isOk = dbengine.ExecuteEdit(row,"FIELDID='"+ TC.getFIELDID()+"'");
      if(isOk)
      {
        fm.setResult(true);
        fm.setMessage("字段名修改成功");

      }
      else
      {
        fm.setResult(false);
        fm.setMessage("字段名【"+TC.getTABLEID()+"】不存在");
      }
     }
      catch(Exception e)
     {
       fm.setResult(false);
       fm.setMessage("调用方法EditTC出现异常"+e.toString());
       return fm;
     }
    return fm;
   }


   /**
      * 删除字段名
      * @return FunctionMessage
      * 
      */

   public FunctionMessage  DeleteTC(String ID)
    {
      boolean isOk=false;
      FunctionMessage fm = new FunctionMessage(1);
       try
      {
        isOk = dbengine.ExecuteDelete("BPIP_FIELD","FIELDID='"+ID+"'");
        if(isOk)
        {
          fm.setResult(true);
          fm.setMessage("字段名【"+ID+"】已经删除");
        }
        else
        {
          fm.setMessage("删除不成功");
          fm.setResult(false);
        }
      }
      catch(Exception e)
      {
        fm.setResult(false);
        fm.setMessage("调用方法DeleteTC出现异常"+e.toString());
        return fm;
      }
    return fm;
  }



  /**
   * 获取字段名列表
   * @param BPIP_FIELD String
   * @return String[][]
  */
 public BPIP_FIELD[]  GetBpipFieldList(String condition) {
  BPIP_FIELD bgs[] = null;
  String strSQL = "";
    try {
      if (condition.trim().length() == 0) {
        strSQL = "Select * from BPIP_FIELD order by FIELDID";
        }
      else {
        strSQL = "Select * from BPIP_FIELD where TABLEID ='"+condition+"' order by FIELDID";
      }
    dbSet mdbset = dbengine.QuerySQL(strSQL);
    bgs = new BPIP_FIELD[mdbset.RowCount()];
    if (mdbset.RowCount() == 0) {
      return null;
    }
    else {
      for (int i = 0; i < mdbset.RowCount(); i++) {
        bgs[i] = new BPIP_FIELD();
        bgs[i].fullData(mdbset.Row(i));
      }
      mdbset = null;
    }
  }
  catch (Exception e) {
    return null;
  }
  return bgs;
}
/**
   * 获取字段名编号
   * @param BPIP_FIELD String
   * @return String[][]
  */
public BPIP_FIELD getFieldId(String FIELDID) {
     BPIP_FIELD bp = new BPIP_FIELD();
     String strSQL = "";
      try {
       strSQL = "Select * From BPIP_FIELD Where FIELDID='"+FIELDID.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;
}


   /**
      * 功能或作用：取出最大FIELDID
      * @Return String 执行后返回一个最大FIELDID
      */
     private String GetMaxFIELDID() {
       dbSet ds = null;
       String Sql = "", MaxNo = "";
       int LenMaxNo = 0;
       Sql = "SELECT MAX(FIELDID) AS MaxNo FROM BPIP_FIELD";
       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(17 + LenMaxNo);
       }
       catch (Exception ex) {
         System.err.println(ex.getMessage());
       }
       return MaxNo;
  }
  //查询最小TABLEID
 public String GetMinTABLEID(String strok) {
   String result="";
   String strSql = strok;
   try {
    dbSet mdbset = dbengine.QuerySQL(strSql);
    result=mdbset.Row(0).Column("TABLEID1").getString();
    mdbset = null;
  }
  catch (Exception e) {
    return result;
  }
  return result;
 }

 //根据表编号获取字段中文名称
 public BPIP_FIELD[]  GetCHINESENAME(String strWhere) {
   BPIP_FIELD[] resultValue = null;
   String  strSQL = strWhere;
   dbSet set = dbengine.QuerySQL(strSQL, "BPIP_FIELD");
   if (set != null) {
    if (set.RowCount() > 0) {
      resultValue = new BPIP_FIELD[set.RowCount()];
      for (int i = 0; i < set.RowCount(); i++) {
        resultValue[i] = new BPIP_FIELD();
        resultValue[i].fullData(set.Row(i));
      }
    }
    set = null;
  }
  return resultValue;
 }
 //根据表编号获取字段中文名称1
public BPIP_FIELD[]  GetCHINESENAME1(String strtableid) {
  BPIP_FIELD[] resultValue = null;
  String strSQL = "";
  if (strtableid.trim().length() == 0) {
     strSQL = "Select CHINESENAME from BPIP_FIELD where TABLEID in (Select min(TABLEID) as TABLEID1 from BPIP_TABLE where TABLETYPE = '1')  order by FIELDID";
  }
  else{
    strSQL = "Select CHINESENAME from BPIP_FIELD where TABLEID ='"+strtableid+"'  order by FIELDID";
  }
    dbSet set = dbengine.QuerySQL(strSQL);
  if (set != null) {
   if (set.RowCount() > 0) {
     resultValue = new BPIP_FIELD[set.RowCount()];
     for (int i = 0; i < set.RowCount(); i++) {
       resultValue[i] = new BPIP_FIELD();
       resultValue[i].fullData(set.Row(i));
     }
   }
   set = null;
 }
 return resultValue;
}


 //根据表编号获取字段名称
 public BPIP_FIELD[]  GetFIELDNAME(String strWhere2) {
   BPIP_FIELD[] resultValue = null;
   String strSQL;
   strSQL = strWhere2;
   dbSet set = dbengine.QuerySQL(strSQL, "BPIP_FIELD");
   if (set != null) {
    if (set.RowCount() > 0) {
      resultValue = new BPIP_FIELD[set.RowCount()];
      for (int i = 0; i < set.RowCount(); i++) {
        resultValue[i] = new BPIP_FIELD();
        resultValue[i].fullData(set.Row(i));
      }
    }
    set = null;
  }
  return resultValue;
 }
 //根据表编号获取字段名称1
 public BPIP_FIELD[]  GetFIELDNAME1(String strtableid) {
   BPIP_FIELD[] resultValue = null;
   String strSQL = "";
   if (strtableid.trim().length() == 0) {
       strSQL = "Select FIELDNAME from BPIP_FIELD where TABLEID in (Select min(TABLEID) as TABLEID1 from BPIP_TABLE where TABLETYPE = '1')  order by FIELDID";
   }
   else{
       strSQL = "Select FIELDNAME from BPIP_FIELD where TABLEID ='"+strtableid+"'  order by FIELDID";
   }
   dbSet set = dbengine.QuerySQL(strSQL);
   if (set != null) {
    if (set.RowCount() > 0) {
      resultValue = new BPIP_FIELD[set.RowCount()];
      for (int i = 0; i < set.RowCount(); i++) {
        resultValue[i] = new BPIP_FIELD();
        resultValue[i].fullData(set.Row(i));
      }
    }
    set = null;
  }
  return resultValue;
 }

 /**
   * 根据表编号获取表名称
   * @param UnitID String   单位编号
   * @return String
   */
  public String  GetTableName(String TableId) {
   String returnValue = "";
   String strSQL = "";
   strSQL = "Select TABLENAME  From BPIP_TABLE Where TABLEID = '"+TableId+"'";
   dbSet dbset = dbengine.QuerySQL(strSQL);
   if(dbset!=null&&dbset.RowCount()>0){
       returnValue = dbset.Row(0).Column("TABLENAME").getString();
       dbset = null;
   }
    return returnValue;
 }
 /**
   * 根据表编号获取表中文名称
   * @param UnitID String   编号
   * @return String
   */
  public String  GetTableCHINESENAME(String TableId) {
   String returnValue = "";
   String strSQL = "";
   strSQL = "Select CHINESENAME  From BPIP_TABLE Where TABLEID = '"+TableId+"'";
   dbSet dbset = dbengine.QuerySQL(strSQL);
   if(dbset!=null&&dbset.RowCount()>0){
       returnValue = dbset.Row(0).Column("CHINESENAME").getString();
       dbset = null;
   }
    return returnValue;
 }
 /**
    * 根据字段编号获取字段中文名称
    * @param ID String   编号
    * @return String
    */
   public String  GetFieldCHINESENAME(String fieldId) {
    String returnValue = "";
    String strSQL = "";
    strSQL = "Select CHINESENAME  From BPIP_FIELD Where FIELDID = '"+fieldId+"'";
    dbSet dbset = dbengine.QuerySQL(strSQL);
    if(dbset!=null&&dbset.RowCount()>0){
        returnValue = dbset.Row(0).Column("CHINESENAME").getString();
        dbset = null;
    }
     return returnValue;
 }
 /**
      * 功能或作用：根据表编号获取字段记录计算字段记录
      * @Return String 执行后返回一个最大字符
      */
  public String SumTable(String ID) {
       String sum = "0";
       dbSet mdbset;
       String strSQL = "";
       strSQL = "select count(*) as count from BPIP_FIELD where TABLEID='"+ID+"'";
       mdbset = dbengine.QuerySQL(strSQL);
       if (mdbset != null) {
         sum = String.valueOf(mdbset.Row(0).Column("count").getInteger());
         mdbset = null;
       }
     return sum;
 }

 /**
   * 根据表编号和字段编号获取表中文名称和字段中文名称
   * @param TableId String   表编号和字段编号
   * @return 格式：表名称.字段名称
   */
  public String  GetTaNAME(String TableId) {

    String returnValue = "";
    dbSet dbset,dbset1,dbset2;
    String strSQL0,strSQL1,strSQL2,tableIdStr,fieldIdStr,fieldIdStr1;
     int i=0,j=4,k=12;
     while(k<=TableId.length()){
         tableIdStr = TableId.substring(i, j);
         fieldIdStr = TableId.substring(i+4, j+8);
         fieldIdStr1 = TableId.substring(k, k+8);
         i = j+16;
         j += 20;
         k += 20;

         strSQL0 = "Select CHINESENAME  From BPIP_TABLE Where TABLEID = '"+tableIdStr+"'";
         strSQL1 = "Select CHINESENAME  From BPIP_FIELD Where FIELDID = '"+fieldIdStr+"'";
         strSQL2 = "Select CHINESENAME  From BPIP_FIELD Where FIELDID = '"+fieldIdStr1+"'";

         dbset = dbengine.QuerySQL(strSQL0);
         dbset1 = dbengine.QuerySQL(strSQL1);
         dbset2 = dbengine.QuerySQL(strSQL2);
         if (dbset != null&& dbset.RowCount() > 0||dbset1 != null&& dbset1.RowCount() > 0||dbset2 != null&& dbset2.RowCount() > 0 ) {
           returnValue = dbset.Row(0).Column("CHINESENAME").getString()+"."+
                     dbset1.Row(0).Column("CHINESENAME").getString()+"&"+
                     dbset2.Row(0).Column("CHINESENAME").getString()+"&nbsp;&nbsp;";
         }
         dbset = null;
         dbset1 = null;
         dbset2 = null;
        }

      return returnValue.toString();
   }

   /**
      * 获取表列表
      * @param TableName String
      * @return String[][]
      */
     public String[][] GetFieldList(String TableId) {
       String[][] resultValue = null;
       String strSQL = "";
       strSQL = "Select FIELDID,CHINESENAME From BPIP_FIELD where TABLEID = '"+TableId+"' Order by FIELDID";
       dbSet set = dbengine.QuerySQL(strSQL);
       if (set != null) {
         if (set.RowCount() > 0) {
           resultValue = new String[set.RowCount()][2];
           for (int i = 0; i < set.RowCount(); i++) {
             resultValue[i][0] = set.Row(i).Column("FIELDID").getString();
             resultValue[i][1] = set.Row(i).Column("CHINESENAME").getString();
           }
         }
         set = null;
       }
         return resultValue;

     }


     /**
      * 生成带表的下拉列表
      * @param Table String  表名称
      * @param strID String    选中的表名称
      * @return String           下拉列表的HTML代码
      */
     public String ShowFieldSelect(String TableId, String strID) {
       return ShowFieldSelect(GetFieldList(TableId), strID);
     }

     /**
      * 生成用二维数组填充的下拉列表
      * @param Dictlist String[][]  二维字符串
      * @param strCode String       选中的代码值（可以为空）
      * @return String              下拉列表的HTML代码
      */
     public String ShowFieldSelect(String[][] Tablelist, String strID) {
       String returnValue = "";
       returnValue = "<option></option>\n";
       if (Tablelist != null) {
         if (Tablelist.length > 0) {
           for (int i = 0; i < Tablelist.length; i++) {
             if (strID != null) {
               if (strID.equals(Tablelist[i][0])) {
                 returnValue = returnValue + "<option value=\""+Tablelist[i][0]+
                     "\" selected>"+
                     Tablelist[i][1]+
                     "</option>\n";
               }
               else {
                 returnValue = returnValue +"<option value=\""+Tablelist[i][0]+"\">"+
                     Tablelist[i][1]+
                     "</option>\n";
               }
             }
             else {
               returnValue = returnValue +"<option value=\""+Tablelist[i][0]+"\">"+
                   Tablelist[i][1]+
                   "</option>\n";
             }

           }
         }

       }
       return returnValue.toString();
  }
  /**
    * 功能或作用：根据编号查询表,字段中文名称（通用）
    * String TableOrFieldchinesename  表和字段名
    * String TableOrFieldTablename    表或字段的中文名
    * String TableOrFieldId           表或字段所在的表的条件字段
    * String TableOrFieldIdCondition  表或字段所在的表的条件字段的查询值
    * @Return String 执行后返回所要的名称
*/
public String  GetAllTableFieldName(String TableOrFieldchinesename,
                                       String TableOrFieldTablename,
                                       String TableOrFieldId,
                                       String TableOrFieldIdCondition) {
    String returnValue = "";

    if (TableOrFieldchinesename==null)
    {TableOrFieldchinesename="";}
    TableOrFieldchinesename = TableOrFieldchinesename.trim();

    if (TableOrFieldchinesename.length()>0)
    {
      String strSQL = "";
      strSQL = "Select "+TableOrFieldchinesename+"  From "+TableOrFieldTablename+" Where "+TableOrFieldId+" = '"+TableOrFieldIdCondition+"'";
      dbSet dbset = dbengine.QuerySQL(strSQL);
      if(dbset!=null&&dbset.RowCount()>0){
         returnValue = dbset.Row(0).Column(""+TableOrFieldchinesename+"").getString();
         dbset = null;
      }
    }

    return returnValue;
 }

}
