using System;
using System.Net;
using System.Collections;
using Core.Mes.IBaseInterface;
using System.Data;
using System.Text.RegularExpressions;
using System.Net.Sockets;

namespace Core.Mes.ClientFrameWork
{
    /// <summary>
    /// ClientCommon 的摘要说明。
    /// </summary>
    public class ClientCommon
    {
        static ClientCommon()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
        public static UserInfo _UserInfo;
        public static DataTable ServerConfig;
        private static RemotingHelp _remotingHelp = new RemotingHelp();
        public static RemotingHelp _RemotingHelp
        {
            get
            {
                return _remotingHelp;
            }
        }

        /// <summary>
        /// 用户操作记录
        /// </summary>
        /// <param name="OperateType"></param>
        /// <param name="OperateObject"></param>
        /// <param name="Remark"></param>
        public static void RecodeOperateInfo(string OperateType, string OperateObject, string Remark)
        {
            string ipaddress = GetIp();
            string strOut = "";
            ClientCommon._RemotingHelp.ExecuteMethod("ServerCommon", "Core.Mes.ServerCommon.UserInfoManager", "RecodeOperateInfo",
                new object[]{ClientCommon._UserInfo.LoginID, ClientCommon._UserInfo.UserName, 
								Dns.GetHostName(), ipaddress, OperateType, OperateObject, Remark}, out strOut);
        }

        /// <summary>
        /// 用户操作记录_LOG日志
        /// </summary>
        /// <param name="OperateType"></param>
        /// <param name="OperateObject"></param>
        /// <param name="Remark"></param>
        public static void RecordUserEvent(string vOPerateType, string vControlTYPE, string vOPERATEOBJECT, string vFormName, string vClassName, string vREMARK)
        {
            string ipaddress = GetIp();

            string strOut = "";
            ClientCommon._RemotingHelp.ExecuteMethod("ServerCommon", "Core.Mes.ServerCommon.UserInfoManager", "RecordUserEvent",
                new object[]{ClientCommon._UserInfo.LoginID, ClientCommon._UserInfo.UserName, 
								Dns.GetHostName(), ipaddress, vOPerateType, vControlTYPE, vOPERATEOBJECT, vFormName, vClassName, vREMARK}, out strOut);
        }

        public static string GetIp()
        {
            string ipaddress = "";

            System.Net.IPAddress[] ips = Dns.GetHostEntry(Dns.GetHostName()).AddressList;
            if (ips.Length > 0)
            {
                foreach (IPAddress cip in ips)
                {
                    //只获取IPv4地址
                    if (cip.AddressFamily == AddressFamily.InterNetwork)
                    {
                        string ip = cip.ToString().Trim();
                        if (string.IsNullOrEmpty(ip) || ip.Length < 7 || ip.Length > 15) continue;

                        ipaddress = ip;
                        break;
                    }
                }
            }
            return ipaddress;
        }

        public static void GetServerConfig()
        {
            try
            {
                SimpleReturnObject _out;
                object obj = ClientCommon._RemotingHelp.ExecuteMethod("ServerCommon", "Core.Mes.ServerCommon.UserInfoManager", "GetServerConfig",
                    new object[] { }, out _out);
                if (_out.ErrCode == 0)
                {
                    DataSet ds = (DataSet)obj;
                    if (ds.Tables.Count > 0 && ds.Tables.Contains("CONFIG"))
                    {
                        ServerConfig = ds.Tables["CONFIG"];
                    }
                }
            }
            catch
            {
            }
        }

    }
}
