using System;
using System.Data;
using Core.Mes.ServerFrameWork;
using System.Collections;
using System.Xml;
using System.IO;
using System.Data.OracleClient;
using Core.Mes.IBaseInterface;

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

    private System.String _ROLEID;
    private System.String _ROLENAME;
    private System.String _MEMO;


    public System.String ROLEID
    {
      get { return _ROLEID; }
      set { _ROLEID = value; }
    }

    public System.String ROLENAME
    {
      get { return _ROLENAME; }
      set { _ROLENAME = value; }
    }

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


    public ReturnObject SelectUAM_ROLE(string strWhere)
    {
      string sqlstr = "SELECT ROLEID"
         + ", ROLENAME"
         + ", MEMO"
         + ", DEPARTMENTID"
         + " FROM CXUSER.UAM_ROLE " + strWhere;
      string err = "";
      DataSet ds = DBManager.ExecuteQuery(sqlstr, out err);
      if (err == "") err = sqlstr;
      return new ReturnObject(ds, err);
    }

    public ReturnObject SelectAndCopy(string strWhere)
    {
      string sqlstr = "SELECT ROLEID"
         + ", ROLENAME"
         + ", MEMO"
         + " FROM CXUSER.UAM_ROLE " + 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_ROLE(ArrayList ar)
    {
      string sqlstr = "INSERT INTO CXUSER.UAM_ROLE"
        + " (ROLEID"
        + ", ROLENAME"
        + ", MEMO"
        + ", DEPARTMENTID"
        + ") VALUES ('" + ar[0]
        + "', '" + ar[1]
        + "', '" + ar[2]
        + "', '" + ar[3] + "')";
      string err = "";
      int i = DBManager.ExecuteNonQuery(sqlstr, out err);
      return new ReturnObject(i, err);
    }

    public ReturnObject UpdateUAM_ROLE(ArrayList ar)
    {
      string sqlstr = "UPDATE CXUSER.UAM_ROLE set "
        //+ "ROLEID = '" + ar[0]
        + " ROLENAME = '" + ar[1]
        + "', MEMO = '" + ar[2]
        + "', DEPARTMENTID = '" + ar[3]
        + "' WHERE ROLEID = '" + ar[0] + "'";

      string err = "";
      int i = DBManager.ExecuteNonQuery(sqlstr, out err);
      return new ReturnObject(i, err);
    }

    public ReturnObject DeleteUAM_ROLE(string sRoleID)
    {
      string err = "";

      Guid transGuid = DBManager.BeginTransaction(out err);
      try
      {
        if (err == "")
        {
          string sqlstr = "DELETE FROM CXUSER.PT_USERMENUINFO WHERE ROLEID='" + sRoleID + "'";
          DBManager.ExecuteNonQuery(sqlstr, transGuid, out err);
          if (err == "")
          {
            sqlstr = "DELETE FROM CXUSER.UAM_AUTHORIZATION WHERE ROLEID='" + sRoleID + "'";
            DBManager.ExecuteNonQuery(sqlstr, transGuid, out err);
            if (err == "")
            {

              sqlstr = "DELETE FROM CXUSER.UAM_ROLE WHERE ROLEID='" + sRoleID + "'";
              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
          {
            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_ROLE par)
    {
      ROLEID = par.ROLEID;
      ROLENAME = par.ROLENAME;
      MEMO = par.MEMO;
    }

    public ReturnObject CommitImportData(System.Data.DataTable ImportData)
    {
      try
      {
        string err = "";
        this.DBManager.UpdateTable(ImportData, "SELECT * FROM CXUSER.PT_MENUINFO_IMP", out err);
        if (err.Length == 0)
        {
          //处理菜单和权限功能
          this.DBManager.ExecuteNonQuery("IMP_MENU_ROLE", System.Data.CommandType.StoredProcedure, out err);
          if (err.Length == 0)
          {
            return new ReturnObject("1");
          }
          else
          {
            return new ReturnObject("0", err);
          }
        }
        else
        {
          return new ReturnObject("0", err);
        }
      }
      catch (System.Exception ex)
      {
        System.Diagnostics.Debug.WriteLine(ex.ToString());
        return new ReturnObject(null, ex.ToString());
      }
    }

    public ReturnObject GetRoleData()
    {
      try
      {
        string err = "";
        string sql = "SELECT * FROM CXUSER.UAM_ROLE";
        DataSet ds = DBManager.ExecuteQuery(sql, out err);
        if (ds != null && ds.Tables[0].Rows.Count > 0)
        {
          return new ReturnObject(ds);
        }
        return new ReturnObject(null, err);
      }
      catch (System.Exception ex)
      {
        System.Diagnostics.Debug.WriteLine(ex.ToString());
        return new ReturnObject(null, ex.ToString());
      }
    }

    public ReturnObject GetRoleDepteID(string RoleID)
    {
      try
      {
        string err = "";
        string sql = "SELECT DEPARTMENTID FROM CXUSER.UAM_ROLE WHERE ROLEID='" + RoleID+"'";
        DataSet ds = DBManager.ExecuteQuery(sql, out err);
        if (ds != null && ds.Tables[0].Rows.Count > 0)
        {
          return new ReturnObject(ds.Tables[0].Rows[0][0].ToString());
        }
        return new ReturnObject(null, err);
      }
      catch (System.Exception ex)
      {
        System.Diagnostics.Debug.WriteLine(ex.ToString());
        return new ReturnObject(null);
      }
    }
  }
}