using System;
using System.IO;
using System.Xml;
using System.Data;
using System.Collections;
using System.Data.OracleClient;
using Core.Mes.IBaseInterface;
using Core.Mes.ServerFrameWork;
using System.Text;
using System.Threading;
using System.Diagnostics;
using System.Windows.Forms;

namespace Core.Mes.ServerCommon
{
    /// <summary>
    /// Class1 的摘要说明。
    /// </summary>
    public class UserInfoManager : IComponent
    {
        public UserInfoManager()
        {

        }
        private object lockObj = new object();
 

        #region "	用户登录	"
        public ReturnObject GetPurviewInfo(string userID, string passwd, string hostname, string ipaddress)
        {
            lock (this)
            {
                string err = "";

                DataRow dr = GetPurviewRow(userID, passwd, out err);

                if (err != "")
                {
                    return new ReturnObject(null, err);
                }
                if (dr == null || dr["MEMO"] == null)
                    return new ReturnObject(null, 10009, "用户名或密码错误！");
                UserInfo useInfo = new UserInfo();

                useInfo = SetUserInfo(dr);
                return new ReturnObject(useInfo);
            }
        }
        private DataRow GetPurviewRow(string userID, string passwd, out string err)
        {
            string sqlstr = "SELECT A.*, B.MEMO, C.NAME, C.BB, C.BC, B.ROLENAME, C.DEPARTMENTID,"
                      + " D.DEPARTMENTNAME FROM CXUSER.UAM_AUTHORIZATION A, CXUSER.UAM_ROLE B, CXUSER.UAM_USER C, CXUSER.UAM_DEPARTMENT D"
                      + " WHERE A.USERID = C.USERID AND A.ROLEID = B.ROLEID AND C.DEPARTMENTID=D.DEPARTMENTID AND "
                //+" UPPER(LOGINID) = UPPER('" + userID + "') AND UPPER(PASSWD) = UPPER('" + PasswdSecurity(passwd) + "')";
                      + " UPPER(A.LOGINID) = UPPER('" + userID + "') AND UPPER(PASSWD) = '" + PasswdSecurity(passwd) + "'";

            err = "";

            System.Data.DataSet ds = this.DBManager.ExecuteQuery(sqlstr, out err);

            if (err != "") return null;

            if (ds.Tables[0].Rows.Count == 0)
            {
                err = "用户名或密码错误！"; return null;
            }
            System.Data.DataRow dr = ds.Tables[0].Rows[0];

            return dr;
        }
        private XmlDocument ReadXmlDoc(string fileName)
        {
            XmlDocument doc = new XmlDocument();

            try
            {
                doc.Load(@"CA\" + fileName);

                return doc;
            }

            catch
            {
                return null;
            }
        }

        private UserInfo SetUserInfo(DataRow dr)
        {
            UserInfo info;

            try
            {
                info = new UserInfo();
                info.LoginID = dr["LOGINID"].ToString();
                info.UserID = dr["USERID"].ToString();
                info.UserDepID = this.GetNoNullStr(dr["DEPARTMENTID"]);
                info.UserDepName = this.GetNoNullStr(dr["DEPARTMENTNAME"]);
                info.UserRoleID = this.GetNoNullStr(dr["ROLEID"]);
                info.UserRoleName = this.GetNoNullStr(dr["ROLENAME"]);
                if (dr["NAME"] == null || dr["NAME"] == DBNull.Value)
                    info.UserName = "";
                else
                    info.UserName = dr["NAME"].ToString();

                if (dr["BB"] == null || dr["BB"] == DBNull.Value)
                    info.UserBb = "300305";
                else
                    info.UserBb = dr["BB"].ToString();

                if (dr["BC"] == null || dr["BC"] == DBNull.Value)
                {
                    info.UserRz = "1";
                    info.UserBc = "300205";
                }
                else
                {
                    info.UserRz = dr["BC"].ToString();
                    info.UserBc = this.GetCurrBc(dr["BC"].ToString());
                }

                return info;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        private string GetNoNullStr(object obj)
        {
            if (obj == null || obj == DBNull.Value)
            {
                return "";
            }
            return obj.ToString();
        }


        #region "	登录退出记录	"
        private void RecodeLoginInfo(string vLoginID, string vUserName, string vUSERHOSTNAME,
          string vUSERIPADRESS, string vIsLogin)
        {
            OracleParameter[] param = new OracleParameter[5];
            object[] values = new object[5];
            ArrayList outvalues = new ArrayList();

            param[0] = new System.Data.OracleClient.OracleParameter();
            param[0].ParameterName = "vLoginID";
            param[0].DbType = System.Data.DbType.String;
            param[0].Size = 20;

            param[1] = new System.Data.OracleClient.OracleParameter();
            param[1].ParameterName = "vUserName";
            param[1].DbType = System.Data.DbType.String;
            param[1].Size = 20;

            param[2] = new System.Data.OracleClient.OracleParameter();
            param[2].ParameterName = "vUSERHOSTNAME";
            param[2].DbType = System.Data.DbType.String;
            param[2].Size = 100;

            param[3] = new System.Data.OracleClient.OracleParameter();
            param[3].ParameterName = "vUSERIPADRESS";
            param[3].DbType = System.Data.DbType.String;
            param[3].Size = 20;

            param[4] = new System.Data.OracleClient.OracleParameter();
            param[4].ParameterName = "vIsLogin";
            param[4].DbType = System.Data.DbType.String;
            param[4].Size = 20;

            values[0] = vLoginID;
            values[1] = vUserName;
            values[2] = vUSERHOSTNAME;
            values[3] = vUSERIPADRESS;
            values[4] = vIsLogin;

            string strOut = "";

            try
            {
                this.DBManager.ExecuteNonQuery("pub_useroperatemanager.pPub_Logininfo_Add", CommandType.StoredProcedure, param, values, ref outvalues, out strOut);

            }
            catch { }
        }


        public ReturnObject RecodeExitInfo(string vLoginID, string vUserName, string vUSERHOSTNAME,
          string vUSERIPADRESS)
        {
            RecodeLoginInfo(vLoginID, vUserName, vUSERHOSTNAME, vUSERIPADRESS, "退出");

            return new ReturnObject();
        }
        public ReturnObject RecodeOperateInfo(string vLoginID, string vUserName, string vUSERHOSTNAME,
          string vUSERIPADRESS, string vOPERATETYPE, string vOPERATEOBJECT, string vREMARK)
        {
            OracleParameter[] param = new OracleParameter[7];
            object[] values = new object[7];
            ArrayList outvalues = new ArrayList();

            param[0] = new System.Data.OracleClient.OracleParameter();
            param[0].ParameterName = "vLoginID";
            param[0].DbType = System.Data.DbType.String;
            param[0].Size = 20;

            param[1] = new System.Data.OracleClient.OracleParameter();
            param[1].ParameterName = "vUserName";
            param[1].DbType = System.Data.DbType.String;
            param[1].Size = 20;

            param[2] = new System.Data.OracleClient.OracleParameter();
            param[2].ParameterName = "vUSERHOSTNAME";
            param[2].DbType = System.Data.DbType.String;
            param[2].Size = 100;

            param[3] = new System.Data.OracleClient.OracleParameter();
            param[3].ParameterName = "vUSERIPADRESS";
            param[3].DbType = System.Data.DbType.String;
            param[3].Size = 20;

            param[4] = new System.Data.OracleClient.OracleParameter();
            param[4].ParameterName = "vOPERATETYPE";
            param[4].DbType = System.Data.DbType.String;
            param[4].Size = 50;

            param[5] = new System.Data.OracleClient.OracleParameter();
            param[5].ParameterName = "vOPERATEOBJECT";
            param[5].DbType = System.Data.DbType.String;
            param[5].Size = 100;

            param[6] = new System.Data.OracleClient.OracleParameter();
            param[6].ParameterName = "vREMARK";
            param[6].DbType = System.Data.DbType.String;
            param[6].Size = 200;

            values[0] = vLoginID;
            values[1] = vUserName;
            values[2] = vUSERHOSTNAME;
            values[3] = vUSERIPADRESS;
            values[4] = vOPERATETYPE;
            values[5] = vOPERATEOBJECT;
            values[6] = vREMARK;

            string strOut = "";

            try
            {
                this.DBManager.ExecuteNonQuery("pub_useroperatemanager.pPub_UserOperate_Add", CommandType.StoredProcedure, param, values, ref outvalues, out strOut);

            }
            catch { }

            return new ReturnObject();
        }
        #endregion

        #region "	班次、班别	"
        private string GetCurrBc(string bc)
        {
            System.DateTime now = System.DateTime.Now;
            switch (bc.Trim())
            {
                case "3":	//三班制
                    //早
                    if (now >= new DateTime(now.Year, now.Month, now.Day, 8, 0, 0) &&
                      now < new DateTime(now.Year, now.Month, now.Day, 16, 0, 0))
                        return "300201";
                    //中
                    if (now >= new DateTime(now.Year, now.Month, now.Day, 16, 0, 0) &&
                      now <= new DateTime(now.Year, now.Month, now.Day, 23, 59, 59))
                        return "300202";
                    //晚				
                    if (now >= new DateTime(now.Year, now.Month, now.Day, 0, 0, 0) &&
                      now < new DateTime(now.Year, now.Month, now.Day, 8, 0, 0))
                        return "300203";
                    break;
                case "2":	//两班制
                    //早
                    if (now >= new DateTime(now.Year, now.Month, now.Day, 8, 0, 0) &&
                      now < new DateTime(now.Year, now.Month, now.Day, 20, 0, 0))
                        return "300201";
                    else
                        return "300203";
            }
            return "300205";
        }
        public ReturnObject GetBz(string strWhere)
        {
            string sqlstr = string.Format("select ID_ BASECODE, NAME_ BASENAME from scm_base_info where SORT_CODE = '{0}' order by 1", strWhere);

            string strOut = "";

            try
            {
                DataSet ds = this.DBManager.ExecuteQuery(sqlstr, out strOut);

                return new ReturnObject(ds, strOut);
            }
            catch (Exception ex)
            {
                return new ReturnObject(new DataSet(), ex.Message + "\n" + strOut);
            }
        }

        public ReturnObject SetBz(string userID, string Bc, string Bb)
        {
            string sqlstr = string.Format(" update UAM_USER set BC = '{0}', BB = '{1}' where USERID = '{2}' ",
              Bc, Bb, userID);

            string strOut = "";

            try
            {
                int i = this.DBManager.ExecuteNonQuery(sqlstr, out strOut);

                return new ReturnObject(i, strOut);
            }
            catch (Exception ex)
            {
                return new ReturnObject(new DataSet(), ex.Message + "\n" + strOut);
            }
        }
        #endregion

        private string PasswdSecurity(string pwd)
        {
            return pwd.ToUpper();
        }
        public ReturnObject GetServerList()
        {
            if (!File.Exists("MesServer.xml"))
            {
                return new ReturnObject(null, "未找到服务配置文件！");
            }

            DataSet ds = new DataSet();
            ds.ReadXml("MesServer.xml");

            if (ds == null || ds.Tables.Count == 0)
                return new ReturnObject(null, "配置文件设置错误！");

            ds.AcceptChanges();

            return new ReturnObject(ds);
        }
        public ReturnObject ChangePasswd(string loginID, string passwd, string passwdNew)
        {
            string sqlstr = string.Format(" update UAM_AUTHORIZATION set passwd = '{2}' where loginid = '{0}' and passwd = '{1}' ",
              loginID, passwd, passwdNew);

            string strOut = "";

            try
            {
                int i = this.DBManager.ExecuteNonQuery(sqlstr, out strOut);

                return new ReturnObject(i, strOut);
            }
            catch (Exception ex)
            {
                return new ReturnObject(new DataSet(), ex.Message + "\n" + strOut);
            }
        }
        #endregion

        #region "	消息	"
        public ReturnObject GetUserMessage()
        {
            string sqlstr = "select MESSAGE, PROMULGATOR,EDITTIME, PRI_ from PUB_USERMESSAGE where ISVALID = '0' and	 > sysdate order by edittime desc ";

            string strOut = "";

            try
            {
                DataSet ds = this.DBManager.ExecuteQuery(sqlstr, out strOut);

                return new ReturnObject(ds, strOut);
            }
            catch (Exception ex)
            {
                return new ReturnObject(new DataSet(), ex.Message + "\n" + strOut);
            }
        }
        public ReturnObject GetMessage(string strWhere)
        {
            string sqlstr = string.Format("select * from pub_message where GETFLAG <> '1' and SENDPERSONCODE = '{0}' ", strWhere);

            string strOut = "";

            try
            {
                DataSet ds = this.DBManager.ExecuteQuery(sqlstr, out strOut);

                return new ReturnObject(ds, strOut);
            }
            catch (Exception ex)
            {
                return new ReturnObject(new DataSet(), ex.Message + "\n" + strOut);
            }
        }

        public ReturnObject SendMessage(string vSendPerson, string vSendPersonCode, string vInfo, string vGetPerson)
        {
            string strOut = "";

            OracleParameter[] param = new OracleParameter[7];
            object[] values = new object[7];
            ArrayList outvalues = new ArrayList();
            strOut = "";

            param[0] = new OracleParameter();
            param[0].ParameterName = "vID";
            param[0].Direction = System.Data.ParameterDirection.Input;
            param[0].DbType = System.Data.DbType.String;
            param[0].Size = 20;

            param[1] = new OracleParameter();
            param[1].ParameterName = "vSendPerson";
            param[1].Direction = System.Data.ParameterDirection.Input;
            param[1].DbType = System.Data.DbType.String;
            param[1].Size = 20;

            param[2] = new OracleParameter();
            param[2].ParameterName = "vSendPersonCode";
            param[2].Direction = System.Data.ParameterDirection.Input;
            param[2].DbType = System.Data.DbType.String;
            param[2].Size = 20;

            param[3] = new OracleParameter();
            param[3].ParameterName = "vInfo";
            param[3].Direction = System.Data.ParameterDirection.Input;
            param[3].DbType = System.Data.DbType.String;
            param[3].Size = 200;

            param[4] = new OracleParameter();
            param[4].ParameterName = "vGetPerson";
            param[4].Direction = System.Data.ParameterDirection.Input;
            param[4].DbType = System.Data.DbType.String;
            param[4].Size = 20;

            param[5] = new System.Data.OracleClient.OracleParameter();
            param[5].ParameterName = "vRESULT";
            param[5].Direction = System.Data.ParameterDirection.Output;
            param[5].DbType = System.Data.DbType.Int16;

            param[6] = new System.Data.OracleClient.OracleParameter();
            param[6].ParameterName = "vMESSAGE";
            param[6].Direction = System.Data.ParameterDirection.Output;
            param[6].DbType = System.Data.DbType.String;
            param[6].Size = 200;

            values[0] = System.Guid.NewGuid().ToString();
            values[1] = vSendPerson;
            values[2] = vSendPersonCode;
            values[3] = vInfo;
            values[4] = vGetPerson;
            values[5] = 0;
            values[6] = "";

            try
            {
                this.DBManager.ExecuteNonQuery("pub_MessageManager.pPub_Message_Send", CommandType.StoredProcedure, param, values, ref outvalues, out strOut);

                return new ReturnObject(outvalues, strOut);
            }
            catch (Exception ex)
            {
                return new ReturnObject(-1, ex.Message + "\n" + strOut);
            }
        }
        public ReturnObject AffirmMessage(string vID)
        {
            string strOut = "";

            OracleParameter[] param = new OracleParameter[3];
            object[] values = new object[3];
            ArrayList outvalues = new ArrayList();
            strOut = "";

            param[0] = new OracleParameter();
            param[0].ParameterName = "vID";
            param[0].Direction = System.Data.ParameterDirection.Input;
            param[0].DbType = System.Data.DbType.String;
            param[0].Size = 20;

            param[1] = new System.Data.OracleClient.OracleParameter();
            param[1].ParameterName = "vRESULT";
            param[1].Direction = System.Data.ParameterDirection.Output;
            param[1].DbType = System.Data.DbType.Int16;

            param[2] = new System.Data.OracleClient.OracleParameter();
            param[2].ParameterName = "vMESSAGE";
            param[2].Direction = System.Data.ParameterDirection.Output;
            param[2].DbType = System.Data.DbType.String;
            param[2].Size = 200;

            values[0] = vID;
            values[1] = 0;
            values[2] = "";

            try
            {
                this.DBManager.ExecuteNonQuery("pub_MessageManager.pPub_Message_Affirm", CommandType.StoredProcedure, param, values, ref outvalues, out strOut);

                return new ReturnObject(outvalues, strOut);
            }
            catch (Exception ex)
            {
                return new ReturnObject(-1, ex.Message + "\n" + strOut);
            }
        }
        #endregion

        #region "	override	"
        public override int maxValue
        {
            get
            {
                return 30;
            }
        }
        public override int minValue
        {
            get
            {
                return 20;
            }
        }
        #endregion

        #region		权限设置
        public ReturnObject selectPT_MenuInfo()
        {
            string sqlstr = "SELECT ID_,CAPTION,PARENTID,XH FROM CXUSER.PT_MENUINFO ORDER BY PARENTID,XH,REMARK";
            string err = "";
            DataSet dset = DBManager.ExecuteQuery(sqlstr, out err);
            return new ReturnObject(dset, err);
        }
        public ReturnObject updatePT_MenuPos(DataTable dt)
        {
            try
            {
                string err = "";
                bool success = DBManager.UpdateTable(dt, "SELECT ID_,CAPTION,PARENTID,XH FROM CXUSER.PT_MENUINFO WHERE XH IS NOT NULL", out err);
                return new ReturnObject(null, err);
            }
            catch (Exception ee)
            {
                return new ReturnObject(null, ee.Message);
            }
        }

        public ReturnObject GetPt_menuinfo(string strWhere)
        {
            string sqlstr = string.Format("SELECT * FROM CXUSER.PT_MENUINFO WHERE 1=1 {0} ORDER BY CATEGORY, CAPTION", strWhere);

            string strOut = "";

            try
            {
                DataSet ds = this.DBManager.ExecuteQuery(sqlstr, out strOut);

                return new ReturnObject(ds, strOut);
            }
            catch (Exception ex)
            {
                return new ReturnObject(new DataSet(), ex.Message + "\n" + strOut);
            }
        }
        public ReturnObject InsertPt_menuinfo(string vkey, string vCategory, string vCaption, string vRemark, string vType)
        {
            string strOut = "";
            DataSet ds = null;
            string strID = "";
            object obj = null;
            int iCount = 0;

            string sqlstr = string.Format("SELECT COUNT(1) FROM CXUSER.PT_MENUINFO WHERE KEY_ = '{0}'", vkey);
            try
            {
                ds = this.DBManager.ExecuteQuery(sqlstr, out strOut);
            }
            catch (Exception ex)
            {
                return new ReturnObject(null, ex.Message + "\n" + strOut);
            }
            obj = ds.Tables[0].Rows[0][0];
            if (obj == null || obj == DBNull.Value)
            {
                iCount = 0;
            }
            else
            {
                iCount = Convert.ToInt32(Convert.ToInt32(obj));
            }
            if (iCount > 0)
            {
                return new ReturnObject(null, "键值重复！请重新输入！");
            }

            sqlstr = "SELECT MAX(TO_NUMBER(ID_)) FROM CXUSER.PT_MENUINFO ";


            try
            {
                ds = this.DBManager.ExecuteQuery(sqlstr, out strOut);
            }
            catch (Exception ex)
            {
                return new ReturnObject(null, ex.Message + "\n" + strOut);
            }
            obj = ds.Tables[0].Rows[0][0];
            if (obj == null || obj == DBNull.Value)
            {
                strID = "0";
            }
            else
            {
                strID = Convert.ToString(Convert.ToInt32(obj) + 1);
            }
            sqlstr = string.Format("INSERT INTO CXUSER.PT_MENUINFO (ID_, KEY_, CATEGORY, CAPTION, REMARK, TYPE) "
                + " VALUES "
                + " ('{0}', '{1}', '{2}', '{3}', '{4}', '{5}') ", strID, vkey, vCategory, vCaption, vRemark, vType);

            strOut = "";

            try
            {
                this.DBManager.ExecuteNonQuery(sqlstr, out strOut);

                return new ReturnObject(strID, strOut);
            }
            catch (Exception ex)
            {
                return new ReturnObject(null, ex.Message + "\n" + strOut);
            }
        }
        public ReturnObject UpdatePt_menuinfo(string vID, string vkey, string vCategory, string vCaption, string vRemark, string vType)
        {
            string sqlstr = string.Format("UPDATE CXUSER.PT_MENUINFO SET KEY_ = '{0}', CATEGORY = '{1}', "
                  + " CAPTION = '{2}', REMARK = '{3}', TYPE='{4}' WHERE ID_ = '{5}'  ",
                  vkey, vCategory, vCaption, vRemark, vType, vID);

            string strOut = "";

            try
            {
                this.DBManager.ExecuteNonQuery(sqlstr, out strOut);

                return new ReturnObject(null, strOut);
            }
            catch (Exception ex)
            {
                return new ReturnObject(null, ex.Message + "\n" + strOut);
            }
        }
        public ReturnObject DeletePt_menuinfo(string vID)
        {
            string sqlstr = string.Format("SELECT COUNT(1) FROM CXUSER.PT_USERMENUINFO WHERE MENUID = '{0}' ", vID);
            string strOut = "";
            try
            {
                DataSet dset = DBManager.ExecuteQuery(sqlstr, out strOut);
                if (int.Parse(dset.Tables[0].Rows[0][0].ToString()) > 0)
                    return new ReturnObject(null, "该菜单已被配置到角色菜单中去，请先在角色菜单中删除！");
            }
            catch (Exception ex)
            {
                return new ReturnObject(null, ex.Message + "\n" + strOut);
            }

            sqlstr = string.Format("SELECT COUNT(1) FROM CXUSER.PT_MENUINFO WHERE PARENTID = '{0}' ", vID);
            strOut = "";
            try
            {
                DataSet dset = DBManager.ExecuteQuery(sqlstr, out strOut);
                if (int.Parse(dset.Tables[0].Rows[0][0].ToString()) > 0)
                    return new ReturnObject(null, "该菜单其下还有子菜单，不可直接删除！");
            }
            catch (Exception ex)
            {
                return new ReturnObject(null, ex.Message + "\n" + strOut);
            }

            sqlstr = string.Format("DELETE CXUSER.PT_MENUINFO WHERE ID_ = '{0}' ", vID);
            strOut = "";

            try
            {
                this.DBManager.ExecuteNonQuery(sqlstr, out strOut);
            }
            catch (Exception ex)
            {
                return new ReturnObject(null, ex.Message + "\n" + strOut);
            }
            sqlstr = string.Format("DELETE CXUSER.PT_USERMENUINFO WHERE MENUID = '{0}' ", vID);

            strOut = "";

            try
            {
                this.DBManager.ExecuteNonQuery(sqlstr, out strOut);
                return new ReturnObject(null, strOut);
            }
            catch (Exception ex)
            {
                return new ReturnObject(null, ex.Message + "\n" + strOut);
            }
        }
        public ReturnObject PlastPt_menuinfo(string oldRole, string newRole)
        {
            string sqlstr = string.Format("DELETE CXUSER.PT_USERMENUINFO WHERE ROLEID = '{0}' ", newRole);

            string strOut = "";

            try
            {
                this.DBManager.ExecuteNonQuery(sqlstr, out strOut);
            }
            catch (Exception ex)
            {
                return new ReturnObject(null, ex.Message + "\n" + strOut);
            }

            sqlstr = "SELECT MAX(TO_NUMBER(ID_)) FROM CXUSER.PT_USERMENUINFO ";
            int iCount = 0;
            DataSet ds = null;

            try
            {
                ds = this.DBManager.ExecuteQuery(sqlstr, out strOut);
            }
            catch (Exception ex)
            {
                return new ReturnObject(null, ex.Message + "\n" + strOut);
            }
            object obj = ds.Tables[0].Rows[0][0];
            if (obj == null || obj == DBNull.Value)
            {
                iCount = 0;
            }
            else
            {
                iCount = Convert.ToInt32(obj);
            }
            sqlstr = string.Format("SELECT * FROM CXUSER.PT_USERMENUINFO WHERE ROLEID = '{0}'", oldRole);
            try
            {
                ds = this.DBManager.ExecuteQuery(sqlstr, out strOut);
            }
            catch (Exception ex)
            {
                return new ReturnObject(null, ex.Message + "\n" + strOut);
            }
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                iCount = iCount + 1;
                sqlstr = string.Format("INSERT INTO CXUSER.PT_USERMENUINFO (ID_, ROLEID, MENUID, SETINFO) "
                    + " VALUES "
                    + " ('{0}', '{1}', '{2}', '{3}' ) ", Convert.ToString(iCount),
                    newRole, ds.Tables[0].Rows[i]["MENUID"].ToString(),
                    ds.Tables[0].Rows[i]["SETINFO"].ToString());

                strOut = "";

                try
                {
                    this.DBManager.ExecuteNonQuery(sqlstr, out strOut);
                }
                catch (Exception ex)
                {
                    return new ReturnObject(null, ex.Message + "\n" + strOut);
                }
            }
            return new ReturnObject(null);
        }
        public ReturnObject SavePt_usermenuinfo(string vRole, ArrayList arMenu, ArrayList arSet)
        {
            string sqlstr = string.Format("DELETE CXUSER.PT_USERMENUINFO WHERE ROLEID = '{0}' ", vRole);

            string strOut = "";

            try
            {
                this.DBManager.ExecuteNonQuery(sqlstr, out strOut);
            }
            catch (Exception ex)
            {
                return new ReturnObject(null, ex.Message + "\n" + strOut);
            }

            sqlstr = "SELECT MAX(TO_NUMBER(ID_)) FROM CXUSER.PT_USERMENUINFO ";
            int iCount = 0;
            DataSet ds = null;

            try
            {
                ds = this.DBManager.ExecuteQuery(sqlstr, out strOut);
            }
            catch (Exception ex)
            {
                return new ReturnObject(null, ex.Message + "\n" + strOut);
            }
            object obj = ds.Tables[0].Rows[0][0];
            if (obj == null || obj == DBNull.Value)
            {
                iCount = 0;
            }
            else
            {
                iCount = Convert.ToInt32(obj);
            }

            for (int i = 0; i < arMenu.Count; i++)
            {
                iCount = iCount + 1;
                sqlstr = string.Format("INSERT INTO CXUSER.PT_USERMENUINFO (ID_, ROLEID, MENUID, SETINFO) "
                    + " VALUES "
                    + " ('{0}', '{1}', '{2}', '{3}' ) ", Convert.ToString(iCount),
                    vRole, arMenu[i].ToString(), arSet[i].ToString());

                strOut = "";

                try
                {
                    this.DBManager.ExecuteNonQuery(sqlstr, out strOut);
                }
                catch (Exception ex)
                {
                    return new ReturnObject(null, ex.Message + "\n" + strOut);
                }
            }
            return new ReturnObject(null, "");
        }
        public ReturnObject GetUser_Uam_Role(string DptID)
        {
            try
            {
                string err = "";
                string sql = "SELECT ROLEID, ROLENAME FROM CXUSER.UAM_ROLE WHERE DEPARTMENTID IN(" + DptID + ") ORDER BY ROLENAME ";
                DataSet ds = this.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)
            {
                return new ReturnObject(null, ex.ToString());
            }
        }
        public ReturnObject GetUam_role()
        {
            string sqlstr = "SELECT ROLEID, ROLENAME FROM UAM_ROLE ORDER BY ROLENAME ";

            string strOut = "";

            try
            {
                DataSet ds = this.DBManager.ExecuteQuery(sqlstr, out strOut);
                if (ds != null && ds.Tables[0].Rows.Count > 0)
                {
                    return new ReturnObject(ds, strOut);
                }
                else
                {
                    return new ReturnObject(null, strOut);
                }
            }
            catch (Exception ex)
            {
                return new ReturnObject(null, ex.Message + "\n" + strOut);
            }
        }
        public ReturnObject GetPurviewTree(string strWhere)
        {
            string sqlstr = string.Format("SELECT ID_, MENUID, (SELECT CAPTION FROM CXUSER.PT_MENUINFO WHERE ID_ = MENUID) MENUNAME, "
                  + " SETINFO FROM CXUSER.PT_USERMENUINFO WHERE 1=1 {0} ORDER BY SETINFO ", strWhere);

            string strOut = "";

            try
            {
                DataSet ds = this.DBManager.ExecuteQuery(sqlstr, out strOut);

                return new ReturnObject(ds, strOut);
            }
            catch (Exception ex)
            {
                return new ReturnObject(new DataSet(), ex.Message + "\n" + strOut);
            }
        }
        public ReturnObject GetBuildMenuInfo(string strWhere)
        {
            string sqlstr = string.Format("SELECT C.KEY_, C.CATEGORY, C.CAPTION, C.TYPE,B.SETINFO "
                + " FROM CXUSER.UAM_AUTHORIZATION A, CXUSER.PT_USERMENUINFO B, CXUSER.PT_MENUINFO C "
                + " WHERE LOGINID = '{0}' AND A.ROLEID = B.ROLEID AND B.MENUID = C.ID_ ORDER BY B.SETINFO ", strWhere);

            string strOut = "";

            try
            {
                DataSet ds = this.DBManager.ExecuteQuery(sqlstr, out strOut);

                return new ReturnObject(ds, strOut);
            }
            catch (Exception ex)
            {
                return new ReturnObject(new DataSet(), ex.Message + "\n" + strOut);
            }
        }
        #endregion

        #region "系统监听"

        public ReturnObject MesSystemListener()
        {
            string sqlstr = "select sysdate from dual";

            string strOut = "";

            try
            {
                this.DBManager.ExecuteNonQuery(sqlstr, out strOut);

                return new ReturnObject(null, strOut);
            }
            catch (Exception ex)
            {
                return new ReturnObject(null, ex.Message + "\n" + strOut);
            }
        }
        #endregion



        public ReturnObject getDBSrvTime()
        {
            string strOut = "";
            try
            {
                DataSet ds = this.DBManager.ExecuteQuery("SELECT SYSDATE FROM DUAL", out strOut);

                return new ReturnObject(ds.Tables[0].Rows[0][0], strOut);
            }
            catch (Exception ex)
            {
                return new ReturnObject(null, ex.Message + "\n" + strOut);
            }
        }

       ///// <summary>
       ///// ///////////////////////测试代码/////////////////////////////
       ///// </summary>
       ///// <returns></returns>

       // public ReturnObject SelectTestUser()
       // {
       //     //string sqlstr = "SELECT ID"
       //     //         + ",PLY"
       //     //         + " FROM CXUSER.KCJ_TURNOFFSENDLIST";
       //     //string sqlstr = "SELECT * FROM CXUSER.KCJ_TURNOFFSENDLIST";
       //     string sqlstr = "SELECT ID,PACTNO,ISOUT,PLANNO,ORDERFORMNUMBER,PLANVEHICLEID, TRAINWAGON,VEHICLETYPE,LISTNUMBER,ROLLNO,BUTTRESS,"
       //     + "CONDOLENUMBER,SERIALNUMBER,STOVENO,STEELTYPE,PLY,WIDTH,LENGTH,WEIGHT,ADDWEIGHT"
       //     +" FROM CXUSER.KCJ_TURNOFFSENDLIST";
       //     string err = "";
       //     DataSet ds = DBManager.ExecuteQuery(sqlstr, out err);
            


       //     return new ReturnObject(ds, err);
       // }


       // public ReturnObject Query(string str)
       // {

       //     string sqlstr = "SELECT ID,PACTNO,ISOUT,PLANNO,ORDERFORMNUMBER,PLANVEHICLEID, TRAINWAGON,VEHICLETYPE,LISTNUMBER,ROLLNO,BUTTRESS,"
       //     + "CONDOLENUMBER,SERIALNUMBER,STOVENO,STEELTYPE,PLY,WIDTH,LENGTH,WEIGHT,ADDWEIGHT"
       //     + " FROM CXUSER.KCJ_TURNOFFSENDLIST WHERE ID='"+str+"'";
       //     string err = "";
       //     DataSet ds = DBManager.ExecuteQuery(sqlstr, out err);



       //     return new ReturnObject(ds, err);
       // }

        

    }
}
