package zr.zrpower.ejb.sys;

import zr.zrpower.entity.sys.BPIP_USER_REMOVE;
import zr.zrpower.common.db.dbSet;
import zr.zrpower.common.db.dbEngine;
import zr.zrpower.common.util.SysPreperty;
import zr.zrpower.common.util.FunctionMessage;
import zr.zrpower.common.util.Log;
import zr.zrpower.common.db.*;

/**
 * <p>Title: ZRPower</p>
 * <p>Description: 调动用户表管理服务</p>
 * <p>Copyright: Copyright (c) 2015</p>
 * <p> public FunctionMessage  AddID(BPIP_USER_REMOVE ID)             // 增加新调动用户信息
 * <p> public BPIP_USER_REMOVE[]  GetUserRemoveList(String userID)    // 获取调动用户列表
 * <p> public BPIP_USER_REMOVE  GetUserInfo(String userID)            // 获取调动用户编号
 * <p> public String  GetBEFOREUNITName(String BEFOREUNITID)          // 根据单位编号获取单位名称
 */

public class UserRemoveService
{
static private Log log;                            //日志
dbEngine dbengine;                  //数据库引擎
static private int clients=0;

public UserRemoveService()
{
  dbengine = new dbEngine(SysPreperty.getProperty().MainDataSource,SysPreperty.getProperty().IsConvert);
  dbengine.initialize();
  if (clients<1)
  {
   log = new Log();
   log.SetLogForClass("UserRemoveServiceBean");
   log.SetLogFile("DictEjb.log");

  }
  clients++;
}


/**
 * 增加新调动用户信息
 * @param 用户实体
 * @return FunctionMessage  保存是否成功,成功或是失败消息
 * @author NFZR
 * 
 */

public FunctionMessage  AddID(BPIP_USER_REMOVE ID)
 {
  String  strSql= "";
  dbSet mdbset;
  FunctionMessage fm = new FunctionMessage(1);
   try
   {
      strSql = "Select * From BPIP_USER_REMOVE Where ID="+ID.getID();
      mdbset = dbengine.QuerySQL(strSql);
       if (mdbset != null) {
         fm.setResult(false);
         fm.setMessage("调动用户【" + ID.getID() + "】已经存在");
         mdbset = null;
       }
       else {

          strSql = "Select max(ID)+1 as num From BPIP_USER_REMOVE";
          mdbset = dbengine.QuerySQL(strSql);
          if (mdbset != null) {
            if (mdbset.Row(0).Column("num").getInteger()==0)
            {
               ID.setID(1);
            }else
            {
               ID.setID(mdbset.Row(0).Column("num").getInteger());
            }
          }else
          {
            ID.setID(1);
          }

          if (dbengine.ExecuteInsert(ID.getData())) {

            strSql = "UPDATE BPIP_USER SET UNITID= (Select AFTERUNIT From BPIP_USER_REMOVE  Where ID=(SELECT  MAX(ID) From BPIP_USER_REMOVE Where USERID='"+ID.getUSERID()+"'))  where USERID='"+ID.getUSERID()+"'";
            dbengine.ExecuteSQL(strSql);
            strSql = "DELETE FROM BPIP_USER_ROLE WHERE  USERID='"+ID.getUSERID()+"'";
            dbengine.ExecuteSQL(strSql);
            fm.setMessage("调动成功");
              fm.setResult(true);
          }
       }
}
    catch(Exception e)
  {
      fm.setResult(false);
      fm.setMessage("调用方法AddID出现异常"+e.toString());
      return fm;
  }
  return fm;
 }
  /**
   * 获取调动用户列表
   * @param TableName String
   * @return String[][]
  */
 public BPIP_USER_REMOVE[]  GetUserRemoveList(String struserid) {
  BPIP_USER_REMOVE bgs[] = null;
  String strSql= "";
  try {
    strSql = "Select A.USERID,A.MOVETIME,B.UNITNAME AS BEFOREUNIT,C.UNITNAME AS AFTERUNIT,A.MOVEREASON from BPIP_USER_REMOVE A left join BPIP_UNIT B on A.BEFOREUNIT = B.UNITID left join BPIP_UNIT C on A.AFTERUNIT = C.UNITID Where A.USERID = '"+struserid+"' order by A.ID";
    dbSet mdbset = dbengine.QuerySQL(strSql);
    bgs = new BPIP_USER_REMOVE[mdbset.RowCount()];
    if (mdbset.RowCount() == 0) {
      return null;
    }
    else {
      for (int i = 0; i < mdbset.RowCount(); i++) {
        bgs[i] = new BPIP_USER_REMOVE();
        bgs[i].fullData(mdbset.Row(i));
      }
      mdbset = null;
    }
  }
  catch (Exception e) {
    return null;
  }
  return bgs;
}
/**
  * 获取调动用户编号
  * @param TableName String
  * @return String[][]
  */
 public BPIP_USER_REMOVE  GetUserInfo(String userID) {
    BPIP_USER_REMOVE returnValue = null;
    String strSQL= "";
    strSQL = "Select * From BPIP_USER_REMOVE Where UserID = '"+userID+"'";
    dbSet dbset = dbengine.QuerySQL(strSQL.toString());
    if (dbset != null) {
      if (dbset.RowCount() > 0) {
        returnValue = new BPIP_USER_REMOVE();
        returnValue.fullData(dbset.Row(0));
      }
      dbset = null;
    }
    return returnValue;
  }

  /**
    * 根据单位编号获取单位名称
    * @param UnitID String   单位编号
    * @return String
    */
   public String  GetBEFOREUNITName(String BEFOREUNITID) {
    String returnValue = null;
    String  strSQL= "";
    strSQL = "Select BEFOREUNIT  From BPIP_UNIT Where BEFOREUNIT = '"+BEFOREUNITID+"'";
    dbSet dbset = dbengine.QuerySQL(strSQL);
    if(dbset!=null&&dbset.RowCount()>0){
        returnValue = dbset.Row(0).Column("BEFOREUNIT").getString();
        dbset = null;
    }
     return returnValue;
 }
}
