package zr.zrpower.ejb.sys;

import zr.zrpower.common.util.Log;
import zr.zrpower.common.db.dbSet;
import zr.zrpower.common.db.dbEngine;
import zr.zrpower.entity.sys.CODE_YWXT;
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 CODE_YWXT[] GetCodeList(String strwhere1)    //获取字典列表</p>
 * <p> public CODE_YWXT getDcitId(String strcode,String strtableid)  //获取字典编号</p>
 * <p> public FunctionMessage  AddDC(CODE_YWXT DC,String strtableid) //增加字典信息</p>
 * <p> public FunctionMessage  EditDC(CODE_YWXT DC,String strtableid,String id) //编辑字典信息</p>
 * <p> public FunctionMessage  DeleteDC(String ID,String strtableid)  //删除字典信息</p>
 * <p> public CODE_YWXT[] GetCodeList1(String strwhere)               //获取字典列表</p>
 * @author NFZR
 *
 */
public class UdictService
  {
 static private Log log;                            //日志
 dbEngine dbengine;                  //数据库引擎
 static private int clients=0;

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

   if (clients<1)
   {
     log = new Log();
     log.SetLogForClass("FieldServiceBean");
     log.SetLogFile("DictEjb.log");

   }
   clients++;
 }

  /**
     * 获取字典列表
     * @param CODE_YWXT String
     * @return String[][]
    */
   public CODE_YWXT[] GetCodeList(String strwhere1) {
    CODE_YWXT bgs[] = null;
    String strTABLENAME = null;
    String  strSql= "";
    strSql = "Select TABLENAME from BPIP_TABLE where TABLEID = '"+strwhere1+"'";
    dbSet dbset = dbengine.QuerySQL(strSql);
    if (dbset != null) {
        strTABLENAME = dbset.Row(0).Column("TABLENAME").getString();
     }

     strSql = "Select * from "+strTABLENAME+" order by CODE";
     dbSet dbset1 = dbengine.QuerySQL(strSql);
     if (dbset1 != null) {
         bgs = new CODE_YWXT[dbset1.RowCount()];
         for (int i = 0; i < dbset1.RowCount(); i++) {
           bgs[i] = new CODE_YWXT();
           bgs[i].fullData(dbset1.Row(i));
         }
         dbset1 = null;
      }
    return bgs;
 }

  /**
     * 获取字典列表
     * @param CODE_YWXT String
     * @return String[][]
    */

   /**
     * 获取字段名编号
     * @param BPIP_FIELD String
     * @return String[][]
    */
  public CODE_YWXT getDcitId(String strcode,String strtableid) {
       CODE_YWXT bp = new CODE_YWXT();
       String strTABLENAME = null;
       String strSql= "";
      try {
         strSql = "Select TABLENAME from BPIP_TABLE where TABLEID = '"+strtableid+"'";
         dbSet dbset = dbengine.QuerySQL(strSql);
         if (dbset != null) {
             strTABLENAME = dbset.Row(0).Column("TABLENAME").getString();
             strSql = "Select * from "+strTABLENAME+" where CODE = '"+strcode+"'";
             dbSet dbset1 = dbengine.QuerySQL(strSql);
             if (dbset1.RowCount() > 0) {
               bp.fullData(dbset1.Row(0));
             }
             dbset = null;
         }
         else{
               return null;
         }
     }
     catch (Exception e) {
         return null;
     }
     return bp;
  }


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

  public FunctionMessage  AddDC(CODE_YWXT DC,String strtableid)
   {   String strTABLENAME = null;
        String strSql= "";
        String strCODE=null;
        String strNAME=null;
        String strSPELL=null;
        String strISSHOW = null;
    FunctionMessage fm = new FunctionMessage(1);
    try
     {
         dbRow row =DC.getData();
         strCODE = row.Column("CODE").getString().trim();
         strNAME =row.Column("NAME").getString().trim();
         strSPELL = row.Column("SPELL").getString().trim();
         strISSHOW = row.Column("ISSHOW").getString().trim();

         strSql = "Select TABLENAME from BPIP_TABLE where TABLEID = '"+strtableid+"'";
          dbSet dbset = dbengine.QuerySQL(strSql);
          if (dbset != null) {
              strTABLENAME = dbset.Row(0).Column("TABLENAME").getString();
              dbset = null;
          }

         strSql = "Insert into "+strTABLENAME+"(CODE,NAME,SPELL,ISSHOW) values ('"+strCODE+"','"+strNAME+"','"+strSPELL+"','"+strISSHOW+"')";
         dbengine.ExecuteSQL(strSql);
             fm.setMessage("录入成功");
             fm.setResult(true);
        }
      catch(Exception e)
    {
        fm.setResult(false);
        fm.setMessage("调用方法AddDC出现异常"+e.toString());
        return fm;
    }
    return fm;
  }
   /**
        * 编辑字段名表
        * @param TC BPIP_FIELD 编辑的实体
        * @return FunctionMessage
        * @author NFZR
        *
        */
       public FunctionMessage  EditDC(CODE_YWXT DC,String strtableid,String id)
        {
       String strSql= "";
       String strCODE=null;
       String strNAME=null;
       String strSPELL=null;
       String strTABLENAME = null;
       String strISSHOW = null;
       FunctionMessage fm = new FunctionMessage(1);
         try
         {
          dbRow row =DC.getData();
         strCODE = row.Column("CODE").getString().trim();
         strNAME =row.Column("NAME").getString().trim();
         strSPELL = row.Column("SPELL").getString().trim();
         strISSHOW = row.Column("ISSHOW").getString().trim();

         strSql = "Select TABLENAME from BPIP_TABLE where TABLEID = '"+strtableid+"'";
          dbSet dbset = dbengine.QuerySQL(strSql);
          if (dbset != null) {
              strTABLENAME = dbset.Row(0).Column("TABLENAME").getString();
              dbset = null;
          }

         strSql = "UPDATE "+strTABLENAME+" SET CODE ='"+strCODE+"',NAME='"+strNAME+"',SPELL='"+strSPELL+"',ISSHOW='"+strISSHOW+"' where CODE ='"+id+"'";
         dbengine.ExecuteSQL(strSql);
         fm.setMessage("修改成功");
         fm.setResult(true);
         }
          catch(Exception e)
         {
           fm.setResult(false);
           fm.setMessage("调用方法EditDC出现异常"+e.toString());
           return fm;
         }
        return fm;
       }

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

     public FunctionMessage  DeleteDC(String ID,String strtableid)
      {
        String  strSql= "";
        String strTABLENAME = null;
        FunctionMessage fm = new FunctionMessage(1);
         try
        {
         strSql = "Select TABLENAME from BPIP_TABLE where TABLEID = '"+strtableid+"'";
         dbSet dbset = dbengine.QuerySQL(strSql);
         if (dbset != null) {
             strTABLENAME = dbset.Row(0).Column("TABLENAME").getString();
             dbset = null;
         }

         strSql = "Delete From "+strTABLENAME+" where CODE='"+ID+"'";
          dbengine.ExecuteSQL(strSql);
          fm.setMessage("删除成功");
             fm.setResult(true);
        }
        catch(Exception e)
        {
          fm.setResult(false);
          fm.setMessage("调用方法DeleteDC出现异常"+e.toString());
          return fm;
        }
       return fm;
   }

   public CODE_YWXT[] GetCodeList1(String strwhere) {
     CODE_YWXT bgs[] = null;
     String strTABLENAME = null;
     String  strSql= strwhere;

     dbSet dbset = dbengine.QuerySQL(strSql);
     if (dbset != null) {
         strTABLENAME = dbset.Row(0).Column("TABLENAME").getString();
         dbset = null;
      }

      strSql = "Select * from "+strTABLENAME;
      dbSet dbset1 = dbengine.QuerySQL(strSql);

      if (dbset1 != null) {
         bgs = new CODE_YWXT[dbset1.RowCount()];
         for (int i = 0; i < dbset1.RowCount(); i++) {
           bgs[i] = new CODE_YWXT();
           bgs[i].fullData(dbset1.Row(i));
         }
         dbset1 = null;
       }
     return bgs;

}

    /**
     * 生成带字典的下拉列表
     * @param DictTable String  字典表名称
     * @param strCode String    选中的字典代码（可以为空）
     * @return String           下拉列表的HTML代码
     */
    public String ShowDictSelect(String DictTable, String strCode) {
      return ShowDictSelect(GetDictList(DictTable),strCode);
    }

    /**
  * 获取字典列表
  * @param TableName String
  * @return String[][]
  */
 public String[][]  GetDictList(String TableName) {
  String[][] resultValue = null;
  String strSQL = "";
  strSQL = "Select Code,Name From "+TableName+" Order by Code";
  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("Code").getString();
        resultValue[i][1] = set.Row(i).Column("Name").getString();
      }
    }
    set = null;
  }
  return resultValue;
 }

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

          }
        }

      }
      return returnValue.toString();
    }
    //获取字典中的中文名称
      //传入参数：TableName  字典表名
      //         DictCode   字典代码
      //返回值：String 中文名称
      public String  GetDictName(String TableName, String DictCode) {
        String resultValue = "";
        String strSQL = "";
        strSQL = "Select Name From "+TableName+" Where Code='"+DictCode+"'";
        dbSet set = dbengine.QuerySQL(strSQL);
        if (set != null) {
            if (set.RowCount() > 0) {
              resultValue = set.Row(0).Column("Name").getString();
            }
            set = null;
        }
        return resultValue;
  }
}
