package zr.zrpower.ejb.sys;

import zr.zrpower.common.util.*;
import zr.zrpower.common.db.*;
import zr.zrpower.entity.sys.BPIP_USER_IDEA;

/**
 *
 * <p>Title: ZRPower</p>
 * <p>Description: 意见设置</p>
 * <p>Copyright: Copyright (c) 2015</p>
 * <p>public FunctionMessage DelUserIdea(String ID)                      //删除意见信息</p>
 * <p>  public FunctionMessage EditUserIdea(BPIP_USER_IDEA mUserIdea)    //修改意见信息</p>
 * <p> public FunctionMessage AddUserIdea(BPIP_USER_IDEA mUserIdea)      //保存新意见信息</p>
 * <p> public BPIP_USER_IDEA[] GetIdeaList(String strWhere)              //根据指定条件查询所有的意见信息</p>
 */
public class UserIdeaService
  {
  static private Log log;
  dbEngine dbengine;
  static private int clients=0;

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

  /**
     * 保存新意见信息
     * @param BPIP_USER_IDEA mUserIdea   新意见信息实体
     * @return FunctionMessage  保存是否成功,成功或是失败消息
   */
public FunctionMessage AddUserIdea(BPIP_USER_IDEA mUserIdea) {
  String  strSql= "";
  dbSet mdbset;
  FunctionMessage fm = new FunctionMessage(1);
  try {
    strSql = "Select * From BPIP_USER_IDEA Where ID='"+
        mUserIdea.getID()+ "'";
    mdbset = dbengine.QuerySQL(strSql);
    if (mdbset != null) {
      fm.setResult(false);
      fm.setMessage("意见【" + mUserIdea.getID() + "】已经存在");
      mdbset = null;
    }
    else {
      String strMaxNo = GetMaxFieldNo("BPIP_USER_IDEA", "ID", 8);
      mUserIdea.setID(strMaxNo); //设置ID
      if (dbengine.ExecuteInsert(mUserIdea.getData())) {
        fm.setMessage("意见录入成功");
        fm.setResult(true);
      }
    }
  }
  catch (Exception e) {
    fm.setResult(false);
    fm.setMessage("调用方法AddUserIdea出现异常" + e.toString());
    return fm;
  }
  return fm;
}
  /**
    * 删除意见信息
    * @param ID String 意见的ID
    * @return FunctionMessage
    */
   public FunctionMessage DelUserIdea(String ID) {
     boolean isOk = false;
     FunctionMessage fm = new FunctionMessage(1);

     try {
       isOk = dbengine.ExecuteDelete("BPIP_USER_IDEA", "ID=" + ID );
       if (isOk) {
         fm.setResult(true);
       }
       else {
         fm.setMessage("删除不成功");
         fm.setResult(false);
       }
     }
     catch (Exception e) {
       fm.setResult(false);
       fm.setMessage("调用方法delUserIdea出现异常" + e.toString());
       return fm;
     }
     return fm;
   }

   /**
      * 修改意见信息
      * @param BPIP_USER_IDEA mUserIdea   新意见信息实体
     * @return FunctionMessage  修改是否成功,成功或是失败消息
    */
  public FunctionMessage  EditUserIdea(BPIP_USER_IDEA mUserIdea) {
    boolean isOk = false;
    FunctionMessage fm = new FunctionMessage(1);
    try {
    dbRow row = mUserIdea.getData();
    isOk = dbengine.ExecuteEdit(row, "ID=" + mUserIdea.getID());
    if (isOk) {
      fm.setResult(true);
      fm.setMessage("意见修改成功");

    }
    else {
      fm.setResult(false);
      fm.setMessage("意见【" + mUserIdea.getCONTENT() + "】不存在");
    }
  }

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

  /**
     * 根据指定条件查询所有的意见信息
     * @param strWhere String  条件
     * @return BPIP_USER_IDEA[]
     */
    public BPIP_USER_IDEA[] GetIdeaList(String strWhere) {
      BPIP_USER_IDEA ideas[] = null;
      String strSql = null;

      try {
        strSql = "Select * From BPIP_USER_IDEA ";
       if(strWhere.trim()!="")
       {
         strSql+="  where "+ strWhere;
       }
        dbSet mdbset = dbengine.QuerySQL(strSql);
        ideas = new  BPIP_USER_IDEA[mdbset.RowCount()];
        if (mdbset.RowCount() == 0) {
          return null;
        }
        else {
          for (int i = 0; i < mdbset.RowCount(); i++) {
            ideas[i] = new BPIP_USER_IDEA();
            ideas[i].fullData(mdbset.Row(i));
          }
          mdbset = null;
        }
      }
      catch (Exception e) {
        return null;
      }
      return ideas;
    }
    /**
        * 根据指定条件查询所有的意见信息
        * @param strWhere String  条件
        * @return BPIP_USER_IDEA[]
        */
 public BPIP_USER_IDEA[] GetIdeaList1(String StrUserID) {
         BPIP_USER_IDEA ideas[] = null;
         String strSql= "";
        try {
           strSql = "Select * From BPIP_USER_IDEA where USER_NO='"+StrUserID+"'";
           dbSet mdbset = dbengine.QuerySQL(strSql);
           ideas = new  BPIP_USER_IDEA[mdbset.RowCount()];
           if (mdbset.RowCount() == 0) {
             return null;
           }
           else {
             for (int i = 0; i < mdbset.RowCount(); i++) {
               ideas[i] = new BPIP_USER_IDEA();
               ideas[i].fullData(mdbset.Row(i));
             }
             mdbset = null;
           }
         }
         catch (Exception e) {
           return null;
         }
         return ideas;
    }
    /**
      * 功能或作用：取出最大的记录流水号
      * @param TableName 数据库表名
      * @param FieldName 数据库字段名称
      * @param FieldLen 数据库字段长度
      * @Return MaxNo 执行后返回一个MaxNo字符串
      */
     private  String GetMaxFieldNo(String TableName, String FieldName,
                                        int FieldLen) {
       String  MaxNo = "";
       String  strSQL= "";
       int LenMaxNo = 0;

       strSQL = "SELECT MAX("+FieldName+") AS MaxNo FROM "+TableName;
       try {
         dbSet dbset = dbengine.QuerySQL(strSQL);
         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();
               MaxNo = "0000000000000000000000000" + MaxNo;
             }
             else {
               MaxNo = "00000000000000000000000001";
               LenMaxNo = 1;
             }
           }
           dbset = null;
         }
         MaxNo = MaxNo.substring(25 - FieldLen + LenMaxNo);
       }
       catch (Exception ex) {
         System.err.println(ex.getMessage());
       }
       return MaxNo;
  }
}
