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

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

    private System.String _DEPARTMENTID;
    private System.String _DEPARTMENTNAME;
    private System.String _DESCRIPTION;
    private string AChildDpt = "";

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

    public System.String DEPARTMENTNAME
    {
      get { return _DEPARTMENTNAME; }
      set { _DEPARTMENTNAME = value; }
    }

    public System.String DESCRIPTION
    {
      get { return _DESCRIPTION; }
      set { _DESCRIPTION = value; }
    }


    public ReturnObject SelectUAM_DEPARTMENT(string strWhere)
    {
      string sqlstr = "SELECT DEPARTMENTID"
         + ", DEPARTMENTNAME"
         + ", DESCRIPTION, DEPARTMENTPID"
         + " FROM CXUSER.UAM_DEPARTMENT " + strWhere;
      string err = "";
      DataSet ds = null;
      try
      {
        ds = DBManager.ExecuteQuery(sqlstr, out err);
      }
      catch (Exception ex)
      {
        Console.Write(ex.Message);
      }
      if (err == "") err = sqlstr;
      return new ReturnObject(ds, err);
    }

    public ReturnObject SelectAndCopy(string strWhere)
    {
      string sqlstr = "SELECT DEPARTMENTID"
         + ", DEPARTMENTNAME"
         + ", DESCRIPTION"
         + " FROM CXUSER.UAM_DEPARTMENT " + 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_DEPARTMENT(ArrayList ar)
    {
      string sqlstr = "INSERT INTO CXUSER.UAM_DEPARTMENT"
         + " (DEPARTMENTID"
         + ", DEPARTMENTNAME"
         + ", DESCRIPTION"
         + ", DEPARTMENTPID"
         + ") 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_DEPARTMENT(ArrayList ar)
    {
      string sqlstr = "UPDATE CXUSER.UAM_DEPARTMENT SET "
         + "DEPARTMENTPID = '" + ar[3]
         + "', DEPARTMENTNAME = '" + ar[1]
         + "', DESCRIPTION = '" + ar[2]
         + "' WHERE DEPARTMENTID = '" + ar[0] + "'";

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

    public ReturnObject DeleteUAM_DEPARTMENT(string DID)
    {
      string sqlstr = "SELECT COUNT(1) FROM CXUSER.UAM_DEPARTMENT WHERe DEPARTMENTPID='" + DID + "'";
      DataSet dset = DBManager.ExecuteQuery(sqlstr);
      if (int.Parse(dset.Tables[0].Rows[0][0].ToString()) > 0)
      {
        return new ReturnObject(0, "所选部门其下还有子部门，不允许直接删除！");
      }
      sqlstr = "SELECT COUNT(1) FROM CXUSER.UAM_USER where DEPARTMENTID='" + DID + "'";
      dset = DBManager.ExecuteQuery(sqlstr);
      if (int.Parse(dset.Tables[0].Rows[0][0].ToString()) > 0)
      {
        return new ReturnObject(0, "该部门其下还有用户，不允许直接删除！");
      }
      sqlstr = "SELECT COUNT(1) FROM CXUSER.UAM_ROLE where DEPARTMENTID='" + DID + "'";
      dset = DBManager.ExecuteQuery(sqlstr);
      if (int.Parse(dset.Tables[0].Rows[0][0].ToString()) > 0)
      {
        return new ReturnObject(0, "该部门其下还有角色，不允许直接删除！");
      }

      sqlstr = "DELETE FROM CXUSER.UAM_DEPARTMENT where DEPARTMENTID='" + DID + "'";
      string err = "";
      int i = DBManager.ExecuteNonQuery(sqlstr, out err);
      return new ReturnObject(i, err);
    }

    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_DEPARTMENT par)
    {
      DEPARTMENTID = par.DEPARTMENTID;
      DEPARTMENTNAME = par.DEPARTMENTNAME;
      DESCRIPTION = par.DESCRIPTION;
    }

    public ReturnObject GetAllChildDpt(string DptID)
    {
      try
      {
        AChildDpt = "'" + DptID + "'";
        string Dpt = "'" + DptID + "'";
        string err = "";
        string sql = "SELECT * FROM CXUSER.UAM_DEPARTMENT";
        System.Data.DataSet dst = this.DBManager.ExecuteQuery(sql, out err);
        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
          GetChildDpt(dst.Tables[0], Dpt);
        }
        return new ReturnObject(AChildDpt);
      }
      catch (System.Exception ex)
      {
        System.Diagnostics.Debug.WriteLine(ex.ToString());
        return new ReturnObject(null);
      }
    }
    private void GetChildDpt(System.Data.DataTable tab, string Dpt)
    {
      string RowFilter = "DEPARTMENTPID IN(" + Dpt + ")";
      tab.DefaultView.RowFilter = RowFilter;
      if (tab.DefaultView.Count > 0)
      {
        Dpt = "";
        for (int i = 0; i < tab.DefaultView.Count; i++)
        {
          AChildDpt = AChildDpt + ",'" + tab.DefaultView[i]["DEPARTMENTID"].ToString() + "'";
          Dpt = Dpt + ",'" + tab.DefaultView[i]["DEPARTMENTID"].ToString() + "'";
        }
        Dpt = Dpt.Substring(1, Dpt.Length - 1);
        GetChildDpt(tab, Dpt);
      }
      else
      {
        return;
      }
    }
  }
}