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.*;
import java.io.*;
import java.lang.Object;

/**
 * <p>Title: 信息平台</p>
 * <p>Description: 表名表管理EJB服务</p>
* <p> public FunctionMessage AddTC(BPIP_TABLE TC)                   //增加表名表信息</p>
* <p> public FunctionMessage EditTC(BPIP_TABLE TC)                  //编辑表名表信息</p>
* <p> public FunctionMessage  DeleteTC(String ID)                   //删除表名表</p>
* <p> public BPIP_TABLE[]  GetBpipTableList(String condition)        //获取表名表列表</p>
* <p> public BPIP_TABLE getTableID(String TABLEID)                  //获取表名表编号</p>
* <p> private String GetMaxTABLEID()                                 //获取表名表最大编号</p>
* <p> public String GetMinTABLEID(String strok)                     //获取表名表最小编号</p>
* <p> public String SumTable(String ID)                             //计算表名表记录</p>
* <p> public String[][] GetTableList(String Table)                  //获取表列表</p>
* <p> public String ShowTableSelect(String Table, String strID)     //生成带表的下拉列表</p>
* <p> public String ShowTableSelect(String[][] Tablelist, String strID)//生成用二维数组填充的下拉列表</p>
*
*/
public class TableService
{
  static private Log log;                            //日志
  dbEngine dbengine;                  //数据库引擎
  static private int clients=0;
  String DataBaseType = SysPreperty.getProperty().DataBaseType;//数据库类型

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

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

  public FunctionMessage AddTC(BPIP_TABLE TC) {
    String  strSql= "";
    dbSet mdbset;
    FunctionMessage fm = new FunctionMessage(1);
     try {
      strSql = "Select * From BPIP_TABLE Where TABLEID='" + TC.getTABLEID() + "'";
      mdbset = dbengine.QuerySQL(strSql);

      if (mdbset != null) {
        fm.setResult(false);
        fm.setMessage("表名表【"+TC.getTABLEID()+"】已经存在");

        mdbset = null;
      }
      else {
        String strMaxNo = GetMaxTABLEID();
        TC.setTABLEID(strMaxNo);//设置TABLEID
        if (dbengine.ExecuteInsert(TC.getData())) {

          String strType = TC.getTABLETYPE();
          if (strType.equals("1"))//为字典表时建表且自动增加字段
          {
              String strTableName = TC.getTABLENAME();
              if (DataBaseType.equals("1"))//oracle
              {
                 strSql = "create table " + strTableName + " (CODE  VARCHAR2(6) not null,NAME VARCHAR2(100),SPELL VARCHAR2(50),ISSHOW VARCHAR2(1))";
                 dbengine.ExecuteSQL(strSql);
                 strSql = "alter table "+ strTableName +" add constraint PK_"+strTableName + " primary key (CODE)";
                 dbengine.ExecuteSQL(strSql);
              }
              if (DataBaseType.equals("2"))//mssql
              {
                 strSql = "CREATE TABLE ["+strTableName+"]([CODE] [varchar](6)  NOT NULL,[NAME] [varchar](100)  NULL,[SPELL] [varchar](50)  NULL,[ISSHOW] [varchar](1)  NULL  CONSTRAINT [PK_"+strTableName+"] PRIMARY KEY CLUSTERED ([CODE]))";
                 dbengine.ExecuteSQL(strSql);
              }
              if (DataBaseType.equals("3"))//mysql
              {
                strSql ="CREATE TABLE "+strTableName+" (CODE varchar(6) NOT NULL,NAME varchar(100) default NULL,SPELL varchar(50) default NULL,ISSHOW varchar(1) default NULL, PRIMARY KEY  (CODE))";
                dbengine.ExecuteSQL(strSql);
              }
              BPIP_FIELD FIELD = new BPIP_FIELD();
              strMaxNo = GetMaxFieldNo("BPIP_FIELD","FIELDID",8);
              FIELD.setFIELDID(strMaxNo);//设置FIELDID
              FIELD.setTABLEID(TC.getTABLEID());
              FIELD.setFIELDNAME("CODE");
              FIELD.setFIELDTAG("1");
              FIELD.setCHINESENAME("编号");
              FIELD.setFIELDTYPE("1");
              FIELD.setFIELDLENGTH(6);
              FIELD.setISNULL("0");
              dbengine.ExecuteInsert(FIELD.getData());
              strMaxNo = GetMaxFieldNo("BPIP_FIELD","FIELDID",8);
              FIELD.setFIELDID(strMaxNo);//设置FIELDID
              FIELD.setFIELDNAME("NAME");
              FIELD.setFIELDTAG("1");
              FIELD.setCHINESENAME("名称");
              FIELD.setFIELDTYPE("1");
              FIELD.setFIELDLENGTH(100);
              FIELD.setISNULL("1");
              dbengine.ExecuteInsert(FIELD.getData());
              strMaxNo = GetMaxFieldNo("BPIP_FIELD","FIELDID",8);
              FIELD.setFIELDID(strMaxNo);//设置FIELDID
              FIELD.setFIELDNAME("SPELL");
              FIELD.setFIELDTAG("1");
              FIELD.setCHINESENAME("拼音首字母");
              FIELD.setFIELDTYPE("1");
              FIELD.setFIELDLENGTH(50);
              FIELD.setISNULL("1");
              dbengine.ExecuteInsert(FIELD.getData());
              strMaxNo = GetMaxFieldNo("BPIP_FIELD","FIELDID",8);
              FIELD.setFIELDID(strMaxNo);//设置FIELDID
              FIELD.setFIELDNAME("ISSHOW");
              FIELD.setFIELDTAG("1");
              FIELD.setCHINESENAME("是否禁用");
              FIELD.setFIELDTYPE("1");
              FIELD.setFIELDLENGTH(1);
              FIELD.setISNULL("1");
              dbengine.ExecuteInsert(FIELD.getData());

          }else
          {
              String strPRIMARYKEY = TC.getPRIMARYKEY();
              if (strPRIMARYKEY==null)
              {
                 strPRIMARYKEY = "";
              }
              strPRIMARYKEY = strPRIMARYKEY.trim();
              if (strPRIMARYKEY.length()>0)
              {
                String strTableName = TC.getTABLENAME();
                if (DataBaseType.equals("1"))//oracle
                {
                   strSql = "create table " + strTableName + " ("+strPRIMARYKEY+"  VARCHAR2(18) not null)";
                   dbengine.ExecuteSQL(strSql);
                   strSql = "alter table "+ strTableName +" add constraint PK_"+strTableName + " primary key ("+strPRIMARYKEY+")";
                   dbengine.ExecuteSQL(strSql);
                }
                if (DataBaseType.equals("2"))//mssql
                {
                   strSql = "CREATE TABLE ["+strTableName+"](["+strPRIMARYKEY+"] [varchar](18)  NOT NULL  CONSTRAINT [PK_"+strTableName+"] PRIMARY KEY CLUSTERED (["+strPRIMARYKEY+"]))";
                   dbengine.ExecuteSQL(strSql);
                }
                if (DataBaseType.equals("3"))//mysql
                {
                  strSql ="CREATE TABLE "+strTableName+" ("+strPRIMARYKEY+" varchar(18) NOT NULL, PRIMARY KEY  ("+strPRIMARYKEY+"))";
                  dbengine.ExecuteSQL(strSql);
                }
                BPIP_FIELD FIELD = new BPIP_FIELD();
                strMaxNo = GetMaxFieldNo("BPIP_FIELD","FIELDID",8);
                FIELD.setFIELDID(strMaxNo);//设置FIELDID
                FIELD.setTABLEID(TC.getTABLEID());
                FIELD.setFIELDNAME(strPRIMARYKEY);
                FIELD.setFIELDTAG("5");
                FIELD.setCHINESENAME("编号");
                FIELD.setFIELDTYPE("1");
                FIELD.setFIELDLENGTH(18);
                FIELD.setISNULL("0");
                FIELD.setAUTO1("Z");
                FIELD.setISKEY("1");

                dbengine.ExecuteInsert(FIELD.getData());


              }
          }

          fm.setMessage("录入成功");
          fm.setResult(true);
        }
      }

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

  /**
   * 编辑表名表
   * @param TC BPIP_TABLE 编辑的实体
   * @return FunctionMessage
   *
   */

  public FunctionMessage EditTC(BPIP_TABLE TC) {
    boolean isOk = false;
    FunctionMessage fm = new FunctionMessage(1);
      try {
      dbRow row = TC.getData();
      isOk = dbengine.ExecuteEdit(row,"TABLEID="+TC.getTABLEID());
      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_TABLE","TABLEID="+ID);
      if (isOk) {

        dbengine.ExecuteSQL("delete from BPIP_FIELD where TABLEID ='"+ID+"'");

        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_TABLE String
   * @return String[][]
   */
  public BPIP_TABLE[] GetBpipTableList(String condition) {
    BPIP_TABLE bgs[] = null;
    String strSql = "";
    try {
       if (condition.trim().length() == 0) {
         strSql = "Select * from BPIP_TABLE order by TABLEID";
       }
       else {
         strSql = "Select * from BPIP_TABLE Where TABLESPACE = '"+condition+"' order by TABLEID";
       }
       dbSet mdbset = dbengine.QuerySQL(strSql);
       bgs = new BPIP_TABLE[mdbset.RowCount()];
       if (mdbset.RowCount() == 0) {
          return null;
       }
       else {
         for (int i = 0; i < mdbset.RowCount(); i++) {
           bgs[i] = new BPIP_TABLE();
           bgs[i].fullData(mdbset.Row(i));
         }
         mdbset = null;
      }
    }
    catch (Exception e) {
      return null;
    }
    return bgs;
  }
  /**
    * 获取表名表列表
    * @param BPIP_TABLE String
    * @return String[][]
    */
   public BPIP_TABLE[] GetBpipTableList1() {
     BPIP_TABLE bgs[] = null;
     String strSql = null;
     try {
        strSql = "Select * from BPIP_TABLE where TABLETYPE = '1' order by TABLEID";
        dbSet mdbset = dbengine.QuerySQL(strSql);
        bgs = new BPIP_TABLE[mdbset.RowCount()];
        if (mdbset.RowCount() == 0) {
          return null;
        }
        else {
          for (int i = 0; i < mdbset.RowCount(); i++) {
            bgs[i] = new BPIP_TABLE();
            bgs[i].fullData(mdbset.Row(i));
          }
          mdbset = null;
       }
     }
     catch (Exception e) {
       return null;
     }
     return bgs;
  }
  /**
   * 获取表名编号
   * @param BPIP_TABLE String
   * @return String[][]
   */
  public BPIP_TABLE getTableID(String TABLEID) {
    BPIP_TABLE bp = new BPIP_TABLE();
    String strSql = "";
    try {
      strSql = "Select * From BPIP_TABLE Where TABLEID='" + TABLEID.trim() + "'";
      dbSet mdbset = dbengine.QuerySQL(strSql.toString());
      if (mdbset == null) {
        return null;
      }
      else {
        if (mdbset.RowCount() > 0) {
          bp.fullData(mdbset.Row(0));
        }
        mdbset = null;
      }
    }
    catch (Exception e) {
      return null;
    }
    return bp;
  }

  /**
   * 功能或作用：取出最大TABLEID（类别）
   * @Return String 执行后返回一个最大TABLEID（类别）
   */
  private String GetMaxTABLEID() {
    dbSet ds = null;
    String Sql = "", MaxNo = "";
    int LenMaxNo = 0;
    Sql = "SELECT MAX(TABLEID) AS MaxNo FROM BPIP_TABLE";
    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(21 + 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;
  }
  //查询最小TABLEID
public String GetMinTABLEID1() {
  String result = "";
  String strSql = null;
    try {
    strSql = "Select min(TABLEID) as TABLEID1 from BPIP_TABLE where TABLETYPE = '1'";
    dbSet mdbset = dbengine.QuerySQL(strSql);
    result = mdbset.Row(0).Column("TABLEID1").getString();
    mdbset = null;
  }
  catch (Exception e) {
    return result;
  }
  return result;
}

  /**
     * 功能或作用：计算表记录
     * @Return String 执行后返回一个最大字符
     */
 public String SumTable(String ID) {
      String sum = "0";
      dbSet mdbset;
      String  strSql= "";
      strSql = "select count(*) as count from BPIP_TABLE where TABLESPACE='"+ID+"'";
      mdbset = dbengine.QuerySQL(strSql);
      if (mdbset != null) {
        sum = String.valueOf(mdbset.Row(0).Column("count").getInteger());
        mdbset = null;
      }
    return sum;
 }
 /**
     * 功能或作用：计算表记录
     * @Return String 执行后返回一个最大字符
     */

 public String SumTable1(String Tablename) {
     String sum = "0";
     if (IsExistTable(Tablename))
     {
      String  strSql= "";
      dbSet mdbset;
      strSql = "select count(*) as count from "+Tablename+"";
      mdbset = dbengine.QuerySQL(strSql);
      if (mdbset != null) {
        sum = String.valueOf(mdbset.Row(0).Column("count").getInteger());
        mdbset = null;
      }
     }
     return sum;
}
 /**
   * 获取表列表
   * @param TableName String
   * @return String[][]
   */
  public String[][] GetTableList(String Table) {
    String[][] resultValue = null;
    String  strSQL= "";
    strSQL = "Select TABLENAME,CHINESENAME From " + Table + " where TABLETYPE = '1' Order by CHINESENAME";
    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("TABLENAME").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 ShowTableSelect(String Table, String strID) {
    return ShowTableSelect(GetTableList(Table), strID);
  }

  /**
   * 生成用二维数组填充的下拉列表
   * @param Dictlist String[][]  二维字符串
   * @param strCode String       选中的代码值（可以为空）
   * @return String              下拉列表的HTML代码
   */
  public String ShowTableSelect(String[][] Tablelist, String strID) {
    String 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();
  }
  /**
       * 功能或作用：取出最大ID
       * @Return String 执行后返回一个最大ID
       */
    public String  GetTableId(String ID) {
      String returnValue = "";
      String strSQL= "";
      strSQL = "Select TABLEID from  bpip_table where TABLESPACE='"+ID+"'";
      dbSet dbset = dbengine.QuerySQL(strSQL);
      if(dbset!=null&&dbset.RowCount()>0){
          returnValue = dbset.Row(0).Column("TABLEID").getString();
          dbset = null;
      }
      return returnValue;
   }

   //判断表是否已经存在。
   private boolean IsExistTable(String tableName) {
      boolean result = false;
      String  strSql = "";
      strSql = "select count(TABLEID) as num from BPIP_FIELD where TABLEID in (select TABLEID from BPIP_TABLE where TABLENAME=upper('"+
          tableName+"') or TABLENAME='"+ tableName+"')";

      int rowCount  = 0;
      try {
        dbSet dbset = dbengine.QuerySQL(strSql);
        if (dbset != null) {
          if (dbset.RowCount() > 0) {
             rowCount = dbset.Row(0).Column("num").getInteger();
          }
        }

      }
      catch (Exception e) {
        rowCount = 0;
      }

      if (rowCount > 1) {
        result = true;
      }
      else {
        result = false;
      }
      return result;
  }


   /**
   * 功能或作用：得到实体生成上一次的路径设置
   * @Return 路径
   */
   public String GetEntityPath() {
          String returnValue = "";
          try{
             String filename ="c:/CreateEntity/setup.txt";
             FileInputStream uFile = new FileInputStream(filename);
             int i = uFile.available(); //得到文件大小
             byte data[] = new byte[i];
             uFile.read(data); //TODO 读数据
             uFile.close();
             returnValue = new String(data,0,data.length);
          }
          catch(Exception e)
          {
             log.WriteLog("得到实体生成上一次的路径设置出错："+e.toString());
             returnValue = "zr.zrpower.ejb.sys";
          }
          return returnValue;
   }



   /**
   * 功能或作用：生成实体
   */
   public void CreateEntity(String id,String path){
          StringBuffer  strSQL= new   StringBuffer();
          StringBuffer  strRes= new   StringBuffer();
          StringBuffer  strRes1= new   StringBuffer();
          String FIELDNAME ="";
          String CHINESENAME ="";
          String FIELDTYPE ="";
          String TABLENAME ="";
          String TABLECHINESENAME ="";
          String PRIMARYKEY ="";

          strRes.append("package "+path+";\r\n");
          strRes.append("import zr.zrpower.common.db.*;\r\n");
          //得到表的英文名、中文名、主键
          strSQL.append("Select TABLENAME,CHINESENAME,PRIMARYKEY From BPIP_TABLE where TABLEID='"+id+"'");
          dbSet set0 = dbengine.QuerySQL(strSQL.toString());
          if (set0 != null && set0.RowCount()>0) {
                TABLENAME = set0.Row(0).Column("TABLENAME").getString();
                TABLECHINESENAME = set0.Row(0).Column("CHINESENAME").getString();
                PRIMARYKEY = set0.Row(0).Column("PRIMARYKEY").getString();
                set0 = null;
          }

          strRes.append("/**\r\n");
          strRes.append("* <p>Title:ZRPower平台</p>\r\n");
          strRes.append("* <p>Description: 数据表实体映射包</p>\r\n");
          strRes.append("* <p>单元名称："+TABLENAME+".java</p>\r\n");
          strRes.append("* <p>中文解释："+TABLECHINESENAME+"数据实体类</p>\r\n");
          strRes.append("* <p>作用：将数据库表字段名表映射为Java类，作为数据传输的载体。</p>\r\n");
          strRes.append("*/\r\n");
          strRes.append("public class "+TABLENAME+" extends dbEntity {\r\n");
          strRes.append("//构造函数\r\n");
          strRes.append("public "+TABLENAME+"() {\r\n");
          strRes.append("   super();\r\n");
          strRes.append("   initialize();\r\n");
          strRes.append("}\r\n");
          strRes.append("//初始化函数\r\n");
          strRes.append("protected void  initialize(){\r\n");
          strRes.append("   this.dbrow.setTableName(\""+TABLENAME+"\");\r\n");


          strSQL.delete(0,strSQL.length());//清空
          strSQL.append("Select FIELDNAME,CHINESENAME,FIELDTYPE From BPIP_FIELD where TABLEID='"+id+"' Order by FIELDID");
          dbSet set = dbengine.QuerySQL(strSQL.toString());
          if (set != null && set.RowCount()>0) {
            for (int i = 0; i < set.RowCount(); i++) {
                FIELDNAME = set.Row(i).Column("FIELDNAME").getString();
                CHINESENAME = set.Row(i).Column("CHINESENAME").getString();
                FIELDTYPE = set.Row(i).Column("FIELDTYPE").getString();
                if (FIELDTYPE.equals("1"))//STRING 类型
                {
                    strRes.append("   this.dbrow.addColumn(\""+FIELDNAME+"\",null,dbType.STRING);//"+CHINESENAME+"\r\n");

                    strRes1.append("//获取["+CHINESENAME+"]\r\n");
                    strRes1.append("public String get"+FIELDNAME+"() {\r\n");
                    strRes1.append("   return this.getString(this.dbrow.Column(\""+FIELDNAME+"\").getString());\r\n");
                    strRes1.append("}\r\n");
                    strRes1.append("//设置["+CHINESENAME+"]\r\n");
                    strRes1.append("public void set"+FIELDNAME+"(String "+FIELDNAME+") {\r\n");
                    strRes1.append("   this.dbrow.Column(\""+FIELDNAME+"\").setValue("+FIELDNAME+");\r\n");
                    strRes1.append("}\r\n");

                }
                if (FIELDTYPE.equals("2"))//FLOAT 类型
                {
                    strRes.append("   this.dbrow.addColumn(\""+FIELDNAME+"\",null,dbType.FLOAT);//"+CHINESENAME+"\r\n");

                    strRes1.append("//获取["+CHINESENAME+"]\r\n");
                    strRes1.append("public float get"+FIELDNAME+"() {\r\n");
                    strRes1.append("   return this.getFloat(this.dbrow.Column(\""+FIELDNAME+"\").getFloat());\r\n");
                    strRes1.append("}\r\n");
                    strRes1.append("//设置["+CHINESENAME+"]\r\n");
                    strRes1.append("public void set"+FIELDNAME+"(float "+FIELDNAME+") {\r\n");
                    strRes1.append("   this.dbrow.Column(\""+FIELDNAME+"\").setValue("+FIELDNAME+");\r\n");
                    strRes1.append("}\r\n");

                }
                if (FIELDTYPE.equals("3"))//BLOB 类型
                {
                    strRes.append("   this.dbrow.addColumn(\""+FIELDNAME+"\",null,dbType.BLOB);//"+CHINESENAME+"\r\n");

                    strRes1.append("//获取["+CHINESENAME+"]\r\n");
                    strRes1.append("public byte[] get"+FIELDNAME+"() {\r\n");
                    strRes1.append("   return this.dbrow.Column(\""+FIELDNAME+"\").getBlob();\r\n");
                    strRes1.append("}\r\n");
                    strRes1.append("//设置["+CHINESENAME+"]\r\n");
                    strRes1.append("public void set"+FIELDNAME+"(byte[] "+FIELDNAME+") {\r\n");
                    strRes1.append("   this.dbrow.Column(\""+FIELDNAME+"\").setValue("+FIELDNAME+");\r\n");
                    strRes1.append("}\r\n");

                }
                if (FIELDTYPE.equals("4") || FIELDTYPE.equals("5"))//DATE 类型
                {
                    strRes.append("   this.dbrow.addColumn(\""+FIELDNAME+"\",null,dbType.DATE);//"+CHINESENAME+"\r\n");

                    strRes1.append("//获取["+CHINESENAME+"]\r\n");
                    strRes1.append("public java.util.Date get"+FIELDNAME+"() {\r\n");
                    strRes1.append("   return this.dbrow.Column(\""+FIELDNAME+"\").getDate();\r\n");
                    strRes1.append("}\r\n");
                    strRes1.append("//设置["+CHINESENAME+"]\r\n");
                    strRes1.append("public void set"+FIELDNAME+"(java.util.Date "+FIELDNAME+") {\r\n");
                    strRes1.append("   this.dbrow.Column(\""+FIELDNAME+"\").setValue("+FIELDNAME+");\r\n");
                    strRes1.append("}\r\n");

                }
                if (FIELDTYPE.equals("6"))//INTEGER 类型
                {
                    strRes.append("   this.dbrow.addColumn(\""+FIELDNAME+"\",null,dbType.LONG);//"+CHINESENAME+"\r\n");

                    strRes1.append("//获取["+CHINESENAME+"]\r\n");
                    strRes1.append("public int get"+FIELDNAME+"() {\r\n");
                    strRes1.append("   return this.dbrow.Column(\""+FIELDNAME+"\").getInteger();\r\n");
                    strRes1.append("}\r\n");
                    strRes1.append("//设置["+CHINESENAME+"]\r\n");
                    strRes1.append("public void set"+FIELDNAME+"(int "+FIELDNAME+") {\r\n");
                    strRes1.append("   this.dbrow.Column(\""+FIELDNAME+"\").setValue(Integer.toString("+FIELDNAME+"));\r\n");
                    strRes1.append("}\r\n");

                }
                if (FIELDTYPE.equals("7"))//CLOB 类型
                {
                    strRes.append("   this.dbrow.addColumn(\""+FIELDNAME+"\",null,dbType.CLOB);//"+CHINESENAME+"\r\n");

                    strRes1.append("//获取["+CHINESENAME+"]\r\n");
                    strRes1.append("public String get"+FIELDNAME+"() {\r\n");
                    strRes1.append("   return this.getString(this.dbrow.Column(\""+FIELDNAME+"\").getClob());\r\n");
                    strRes1.append("}\r\n");
                    strRes1.append("//设置["+CHINESENAME+"]\r\n");
                    strRes1.append("public void set"+FIELDNAME+"(String "+FIELDNAME+") {\r\n");
                    strRes1.append("   this.dbrow.Column(\""+FIELDNAME+"\").setValue("+FIELDNAME+");\r\n");
                    strRes1.append("}\r\n");

                }

            }
            set = null;
          }

          strRes.append("   this.dbrow.setPrimaryKey(\""+PRIMARYKEY+"\");\r\n");
          strRes.append("}\r\n");


          strRes.append(strRes1.toString());
          strRes.append("}\r\n");



          //-----重新设置路径文件-------------
          File   dirFile;
          File   tempFile;
          boolean bFile;
          dirFile = new File("c:/CreateEntity");
          bFile = dirFile.exists();
          if( bFile == false )
          {
              dirFile.mkdir();
          }
          tempFile = new File("c:/CreateEntity/setup.txt");
          tempFile.delete();
          try {
            bFile = tempFile.createNewFile();
            if (bFile) {
                FileWriter fw = new FileWriter("c:/CreateEntity/setup.txt");
                fw.write(path, 0, path.length());
                fw.flush();
                fw.close();
            }
          } catch (IOException ex) {
          }
          //--------------------


          //-----生成实体文件-------------
          tempFile = new File("c:/CreateEntity/"+TABLENAME+".java");
          tempFile.delete();
          try {
            bFile = tempFile.createNewFile();
            if (bFile) {
                FileWriter fw = new FileWriter("c:/CreateEntity/"+TABLENAME+".java");
                fw.write(strRes.toString(), 0, strRes.toString().length());
                fw.flush();
                fw.close();
            }
          } catch (IOException ex) {
          }
          //--------------------



   }
   //取最大ID
   public String GetMaxFieldNo(String TableName, String FieldName, int FieldLen) {
    String MaxNo = "";
    int LenMaxNo = 0;
    StringBuffer  addBuf = new   StringBuffer();
    addBuf.append("SELECT MAX("+FieldName+") AS MaxNo FROM "+TableName);

    try {
      dbSet dbset = dbengine.QuerySQL(addBuf.toString());
      if (dbset != null) {
        if (dbset.RowCount() > 0) {
          MaxNo = dbset.Row(0).Column("MaxNo").getString();

          if (MaxNo.length() > 0) {
            MaxNo = String.valueOf(Integer.parseInt(MaxNo) + 1);
            LenMaxNo = MaxNo.length();
            addBuf.delete(0,addBuf.length());//清空
            addBuf.append("0000000000000000000000000"+MaxNo);
            MaxNo = addBuf.toString();
          }
          else {
            MaxNo = "00000000000000000000000001";
            LenMaxNo = 1;
          }
        }
        else {
        }
      }
      else {
      }
      MaxNo = MaxNo.substring(25 - FieldLen + LenMaxNo);
    }
    catch (Exception ex) {
      System.err.println(ex.getMessage());
    }
    return MaxNo;
  }

//--------
}
