using System;
using System.Data;
using System.Collections;
using Core.Mes.ServerFrameWork;
using Core.Mes.IBaseInterface;

namespace Core.Mes.PurviewManager
{
  public class UAM_USER : IComponent
  {
    public UAM_USER() { }

    private System.String _USERID;
    private System.String _DEPARTMENTID;
    private System.String _NAME;
    private System.String _SEX;
    private System.Single _AGE;
    private System.String _COMPUTERIP;
    private System.String _HOMEPHONE;
    private System.String _WORKPOS;
    private System.String _WORKJOB;
    private System.String _MEMO;


    public System.String USERID
    {
      get { return _USERID; }
      set { _USERID = value; }
    }

    public System.String DEPARTMENTID
    {
      get { return _DEPARTMENTID; }
      set { _DEPARTMENTID = value; }
    }

    public System.String NAME
    {
      get { return _NAME; }
      set { _NAME = value; }
    }

    public System.String SEX
    {
      get { return _SEX; }
      set { _SEX = value; }
    }

    public System.Single AGE
    {
      get { return _AGE; }
      set { _AGE = value; }
    }

    public System.String COMPUTERIP
    {
      get { return _COMPUTERIP; }
      set { _COMPUTERIP = value; }
    }

    public System.String HOMEPHONE
    {
      get { return _HOMEPHONE; }
      set { _HOMEPHONE = value; }
    }

    public System.String WORKPOS
    {
      get { return _WORKPOS; }
      set { _WORKPOS = value; }
    }

    public System.String WORKJOB
    {
      get { return _WORKJOB; }
      set { _WORKJOB = value; }
    }

    public System.String MEMO
    {
      get { return _MEMO; }
      set { _MEMO = value; }
    }


    public ReturnObject SelectUAM_USER(string strWhere)
    {
      string sqlstr = "SELECT USERID"
           + ", DEPARTMENTID"
   + ", NAME"
   + ", SEX"
   + ", AGE"
   + ", COMPUTERIP"
   + ", HOMEPHONE"
   + ", WORKPOS"
   + ", WORKJOB"
   + ", MEMO"
           + ", EMAIL"
           + ", (SELECT DEPARTMENTPID FROM CXUSER.UAM_DEPARTMENT WHERE CXUSER.UAM_DEPARTMENT.DEPARTMENTID=CXUSER.UAM_USER.DEPARTMENTID) PD"
   + " FROM CXUSER.UAM_USER " + strWhere;
      string err = "";
      DataSet ds = DBManager.ExecuteQuery(sqlstr, out err);
      if (err == "") err = sqlstr;
      return new ReturnObject(ds, err);
    }
    public ReturnObject GetUAM_USER(string where)
    {
      try
      {
        string sql = "SELECT USERID"
                   + ", DEPARTMENTID"
                   + ", NAME"
                   + ", SEX"
                   + ", AGE"
                   + ", COMPUTERIP"
                   + ", HOMEPHONE"
                   + ", WORKPOS"
                   + ", WORKJOB"
                   + ", MEMO"
                   + ", EMAIL"
                   + ", (SELECT DEPARTMENTPID FROM CXUSER.UAM_DEPARTMENT WHERE CXUSER.UAM_DEPARTMENT.DEPARTMENTID=CXUSER.UAM_USER.DEPARTMENTID) PD"
                   + " FROM CXUSER.UAM_USER "
                   + where;
        string err = "";
        System.Data.DataSet dst = this.DBManager.ExecuteQuery(sql, out err);
        if (err == "") err = err + "%" + sql;
        return new ReturnObject(dst, err);
      }
      catch (System.Exception ex)
      {
        System.Diagnostics.Debug.WriteLine(ex.ToString());
        return new ReturnObject(null, ex.ToString());
      }
    }
    public ReturnObject SelectAndCopy(string strWhere)
    {
      string sqlstr = "SELECT USERID"
         + ", DEPARTMENTID"
         + ", NAME"
         + ", SEX"
         + ", AGE"
         + ", COMPUTERIP"
         + ", HOMEPHONE"
         + ", WORKPOS"
         + ", WORKJOB"
         + ", MEMO"
         + " FROM CXUSER.UAM_USER " + strWhere;
      string err = "";
      DataSet ds = DBManager.ExecuteQuery(sqlstr, out err);
      if (err == "" && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
      {
        DataRow dr = ds.Tables[0].Rows[0];
        Type type = this.GetType();
        foreach (System.Reflection.PropertyInfo pro in type.GetProperties())
        {
          if (pro.GetType().ToString() == "System.Single")
          {
            pro.SetValue(this, (Single)dr[pro.Name], null);
            continue;
          }
          pro.SetValue(this, dr[pro.Name], null);
        }
      }
      if (err == "") err = sqlstr;
      return new ReturnObject(ds, err);
    }

    public ReturnObject InsertUAM_USER(ArrayList ar)
    {
      string sqlstr = "INSERT INTO CXUSER.UAM_USER"
         + " (USERID"
         + ", DEPARTMENTID"
         + ", NAME"
         + ", SEX"
         + ", AGE"
         + ", COMPUTERIP"
         + ", HOMEPHONE"
         + ", WORKPOS"
         + ", WORKJOB"
         + ", MEMO"
                 + ", EMAIL"
         + ") VALUES ('" + ar[0]
         + "', '" + ar[1]
         + "', '" + ar[2]
         + "', '" + ar[3]
         + "', " + ar[4]
         + ", '" + ar[5]
         + "', '" + ar[6]
         + "', '" + ar[7]
         + "', '" + ar[8]
         + "', '" + ar[9]
                 + "', '" + ar[10] + "')";
      string err = "";
      int i = DBManager.ExecuteNonQuery(sqlstr, out err);
      return new ReturnObject(i, err);
    }

    public ReturnObject UpdateUAM_USER(ArrayList ar)
    {
      string sqlstr = "UPDATE CXUSER.UAM_USER SET "
         + " DEPARTMENTID = '" + ar[1]
         + "', NAME = '" + ar[2]
         + "', SEX = '" + ar[3]
         + "', AGE = " + ar[4]
         + ", COMPUTERIP = '" + ar[5]
         + "', HOMEPHONE = '" + ar[6]
         + "', WORKPOS = '" + ar[7]
         + "', WORKJOB = '" + ar[8]
         + "', MEMO = '" + ar[9]
                 + "', EMAIL = '" + ar[10]
         + "' WHERE USERID = '" + ar[0] + "'";
      string err = "";
      int i = DBManager.ExecuteNonQuery(sqlstr, out err);
      return new ReturnObject(i, err);
    }

    public ReturnObject DeleteUAM_USER(string sUserID)
    {
      string err = "";

      Guid transGuid = DBManager.BeginTransaction(out err);
      try
      {
        if (err == "")
        {
          string sqlstr = "DELETE FROM CXUSER.UAM_AUTHORIZATION WHERE USERID='" + sUserID + "'";
          DBManager.ExecuteNonQuery(sqlstr, transGuid, out err);
          if (err == "")
          {
            sqlstr = "DELETE FROM CXUSER.UAM_USER WHERE USERID='" + sUserID + "'";
            DBManager.ExecuteNonQuery(sqlstr, transGuid, out err);
            bool succ = DBManager.Commit(transGuid, out err);
            if (succ)
              return new ReturnObject(1, "");
            else
              return new ReturnObject(0, err);
          }
          else
          {
            DBManager.Rollback(transGuid);
            return new ReturnObject(0, err);
          }
        }
        else
        {
          return new ReturnObject(0, err);
        }
      }
      catch (Exception ee)
      {
        DBManager.Rollback(transGuid);
        return new ReturnObject(0, ee.Message);
      }
    }

    public ReturnObject UpdateDataSet(DataSet ds, string sqlstr)
    {
      string err = "";
      bool flag = DBManager.UpdateTable(ds.Tables[0], sqlstr, out err);
      return new ReturnObject(flag, err);
    }

    public void Copy(UAM_USER par)
    {
      USERID = par.USERID;
      DEPARTMENTID = par.DEPARTMENTID;
      NAME = par.NAME;
      SEX = par.SEX;
      AGE = par.AGE;
      COMPUTERIP = par.COMPUTERIP;
      HOMEPHONE = par.HOMEPHONE;
      WORKPOS = par.WORKPOS;
      WORKJOB = par.WORKJOB;
      MEMO = par.MEMO;
    }
    public ReturnObject SelectChildDept(string strDept)
    {
      string sqlstr = "select cxuser.f_uam_getallchilddept('"
        + strDept
        + "') from dual";
      string err = "";
      DataSet ds = DBManager.ExecuteQuery(sqlstr, out err);
      if (err == "") err = sqlstr;
      return new ReturnObject(ds, err);
    }

  }
}