//获取SIP中继
        private Dictionary<DB_Talk.Model.m_SIPInterface, DB_Talk.Model.m_SIPInterface> GetLstSIPInterface()
        {
         
            List<DB_Talk.Model.m_SIPInterface> lst = new List<DB_Talk.Model.m_SIPInterface>();

            Dictionary<DB_Talk.Model.m_SIPInterface, DB_Talk.Model.m_SIPInterface> dic = new Dictionary<DB_Talk.Model.m_SIPInterface, DB_Talk.Model.m_SIPInterface>();

            for (int i = 0; i < this.dgvSIP.Rows.Count; i++)
            {
                DB_Talk.Model.m_SIPInterface model = new DB_Talk.Model.m_SIPInterface();
                DB_Talk.Model.m_SIPInterface modelTag = dgvSIP.Rows[i].Tag as DB_Talk.Model.m_SIPInterface;
                if (modelTag != null)
                    model.ID = modelTag.ID;
                //SIP中继索引
                if (dgvSIP.Rows[i].Cells["colSIPID"].Value == null || dgvSIP.Rows[i].Cells["colSIPID"].Value.ToString() == "")
                    throw new Exception("SIP编号不能为空!");
                int num = 0;
                if (dgvSIP.Rows[i].Cells["colSIPID"].Value != null && int.TryParse(dgvSIP.Rows[i].Cells["colSIPID"].Value.ToString(), out num) && num > 0 && num <= 10)
                    model.SIPID = num;
                else
                    throw new Exception("SIP中继编号取值范围为1~10");  //实际是1-32,但是SAP索引是1-10

                List<DB_Talk.Model.m_SIPInterface> lstTemp = new DB_Talk.BLL.m_SIPInterface().GetModelList(
                    string.Format("i_Flag=0 and BoxID={0} and SIPID={1} ", Global.Params.BoxID, model.SIPID));
                if (lstTemp.Count > 0 && model.ID != lstTemp[0].ID)
                    throw new Exception("SIP中继编号【" + model.SIPID + "】已经存在");

                #region 市话长途判断
                bool sh=!(dgvSIP.Rows[i].Cells["colSIPOutSub"].Value == null || dgvSIP.Rows[i].Cells["colSIPOutSub"].Value.ToString() == "");
                bool ct = !(dgvSIP.Rows[i].Cells["colSIPOut"].Value == null || dgvSIP.Rows[i].Cells["colSIPOut"].Value.ToString() == "");

                if (sh == false && ct == false)
                {
                    throw new Exception("市话出局号码前缀和长途出局号码前缀必须要填写一个!");
                }
                if(sh==true )
                {
                    //市话出局引导码
                    //if (sh == false && ct == true)
                    //    throw new Exception("市话出局号码前缀不能为空!");
                    string strOutLocal = dgvSIP.Rows[i].Cells["colSIPOutSub"].Value.ToString();
                    string[] strArrayLocal = strOutLocal.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                    foreach (string str in strArrayLocal)
                    {
                        num = 0;
                        if (!int.TryParse(str, out num))
                            throw new Exception("市话出局号码前缀格式错误!必须是以英文逗号隔开的数字!");
                    }
                    model.vc_OutNumberLocal = strOutLocal;
                }
                if (ct == true)
                {
                    //长途出局引导码
                    //if (dgvSIP.Rows[i].Cells["colSIPOut"].Value == null || dgvSIP.Rows[i].Cells["colSIPOut"].Value.ToString() == "")
                    //    throw new Exception("长途出局号码前缀不能为空!");
                    string strOut = dgvSIP.Rows[i].Cells["colSIPOut"].Value.ToString();
                    string[] strArray = strOut.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                    foreach (string str in strArray)
                    {
                        num = 0;
                        if (!int.TryParse(str, out num))
                            throw new Exception("长途出局号码前缀格式错误!必须是以英文逗号隔开的数字!");
                    }
                    model.vc_OutNumber = strOut;
                }
           

                #endregion

                
                string mes="";
                if (model.vc_OutNumberLocal != null && model.vc_OutNumber != null)
                {
                    if (Global.Methods.checkNumOutIsSame(model.vc_OutNumberLocal, model.vc_OutNumber, out mes))
                    {
                        throw new Exception("市话与长途出局号码前缀有重复号码【" + mes + "】!");
                    }
                }
                model.i_Type = 2;         //接口类型,内部(1),外部(2),默认外部
                //model.RouteID = num;      //1~255
                model.i_Level = 1;        //优先级,主(1),从(2),默认主
                model.SAPID = model.SIPID + 1;    //SAP索引取值范围1-10
                model.i_MaxChannel = 128; //最大通道(1~128)
                
                //本端端口号
                if (dgvSIP.Rows[i].Cells["colSIPPort"].Value == null || dgvSIP.Rows[i].Cells["colSIPPort"].Value.ToString() == "")
                    throw new Exception("本机端口号不能为空!");
                num = 0;
                if (dgvSIP.Rows[i].Cells["colSIPPort"].Value != null && int.TryParse(dgvSIP.Rows[i].Cells["colSIPPort"].Value.ToString(), out num) && num >= 1025 && num <= 9999 && num!=5060)
                    model.i_Port = num;  // 5062;
                else
                    throw new Exception("本端端口号取值范围为1025~9999,并且不能为5060");
               
                //对端IP
                if (dgvSIP.Rows[i].Cells["colSIPIP"].Value == null || dgvSIP.Rows[i].Cells["colSIPIP"].Value.ToString() == "")
                    throw new Exception("对端IP不能为空!");
                model.vc_OppositeIP = dgvSIP.Rows[i].Cells["colSIPIP"].Value.ToString(); // "192.168.1.239";
                if (!Global.Methods.checkIP(model.vc_OppositeIP))
                    throw new Exception("对端IP地址不合法!");

                List<DB_Talk.Model.m_SIPInterface> lstIP = new DB_Talk.BLL.m_SIPInterface().GetModelList(
                  string.Format("i_Flag=0 and BoxID={0} and vc_OppositeIP='{1}' ", Global.Params.BoxID, model.vc_OppositeIP));
                if (lstIP.Count > 0 && model.vc_OppositeIP != lstIP[0].vc_OppositeIP)
                    throw new Exception("对端IP【" + model.vc_OppositeIP + "】已经存在");
                //对端端口号
                if (dgvSIP.Rows[i].Cells["colSIPPort2"].Value == null || dgvSIP.Rows[i].Cells["colSIPPort2"].Value.ToString() == "")
                    throw new Exception("对端端口号不能为空!");
                num = 0;
                if (dgvSIP.Rows[i].Cells["colSIPPort2"].Value != null && int.TryParse(dgvSIP.Rows[i].Cells["colSIPPort2"].Value.ToString(), out num) && num >= 1025 && num <= 9999 )
                    model.i_OppositePort = num;
                else
                    throw new Exception("对端端口号本端端口号取值范围为1025~9999");
                
                //是否放音
                if (dgvSIP.Rows[i].Cells["colSIPIsPlay"].Value.ToString() == "")
                    throw new Exception("未选择是否放音");
                model.i_PlaySound = dgvSIP.Rows[i].Cells["colSIPIsPlay"].Value.ToString() == "是" ? 1 : 2; // 1;  //是否放音,是(1),否(2),默认1
               
                model.i_State = 2;  //激活
                model.i_OperateState = 1; //up(2)运行 down(1)停止

                model.BoxID = Global.Params.BoxID;

                if (modelTag != null &&
                    modelTag.SIPID == model.SIPID &&
                    modelTag.SAPID == model.SAPID &&
                    modelTag.BoxID == model.BoxID &&
                    modelTag.i_Port == model.i_Port &&
                    modelTag.vc_OutNumber == model.vc_OutNumber &&
                    modelTag.vc_OutNumberLocal == model.vc_OutNumberLocal &&
                    //modelTag.RouteID == model.RouteID &&
                    modelTag.vc_OppositeIP == model.vc_OppositeIP &&
                    modelTag.i_PlaySound == model.i_PlaySound &&
                    modelTag.i_OppositePort == model.i_OppositePort)
                {
                    modelTag = null;  //没有变化,则什么都不做
                    continue;
                }
                else
                    model.vc_Memo = "修改";
                lst.Add(model);


                dic.Add(model, modelTag);
            }
            return dic;
        }
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public List<DB_Talk.Model.m_SIPInterface> DataTableToList(DataSet ds)
		{
			List<DB_Talk.Model.m_SIPInterface> modelList = new List<DB_Talk.Model.m_SIPInterface>();
			if (ds == null) return modelList;
            DataTable dt = ds.Tables[0];
			int rowsCount = dt.Rows.Count;
			if (rowsCount > 0)
			{
				DB_Talk.Model.m_SIPInterface model;
				for (int n = 0; n < rowsCount; n++)
				{
				  model = new DB_Talk.Model.m_SIPInterface();	
                  if(dt.Rows[n]["ID"]!=null && dt.Rows[n]["ID"].ToString()!="")
				  {
				      model.ID=int.Parse(dt.Rows[n]["ID"].ToString());
				  }
				  if(dt.Rows[n]["BoxID"]!=null && dt.Rows[n]["BoxID"].ToString()!="")
				  {
				      model.BoxID=int.Parse(dt.Rows[n]["BoxID"].ToString());
				  }
				  if(dt.Rows[n]["vc_Code"]!=null && dt.Rows[n]["vc_Code"].ToString()!="")
				  {
				     model.vc_Code= dt.Rows[n]["vc_Code"].ToString();
				  }
				  if(dt.Rows[n]["vc_Name"]!=null && dt.Rows[n]["vc_Name"].ToString()!="")
				  {
				     model.vc_Name= dt.Rows[n]["vc_Name"].ToString();
				  }
				  if(dt.Rows[n]["SIPID"]!=null && dt.Rows[n]["SIPID"].ToString()!="")
				  {
				      model.SIPID=int.Parse(dt.Rows[n]["SIPID"].ToString());
				  }
				  if(dt.Rows[n]["RouteID"]!=null && dt.Rows[n]["RouteID"].ToString()!="")
				  {
				      model.RouteID=int.Parse(dt.Rows[n]["RouteID"].ToString());
				  }
				  if(dt.Rows[n]["SAPID"]!=null && dt.Rows[n]["SAPID"].ToString()!="")
				  {
				      model.SAPID=int.Parse(dt.Rows[n]["SAPID"].ToString());
				  }
				  if(dt.Rows[n]["vc_OutNumber"]!=null && dt.Rows[n]["vc_OutNumber"].ToString()!="")
				  {
				     model.vc_OutNumber= dt.Rows[n]["vc_OutNumber"].ToString();
				  }
				  if(dt.Rows[n]["vc_OutNumberLocal"]!=null && dt.Rows[n]["vc_OutNumberLocal"].ToString()!="")
				  {
				     model.vc_OutNumberLocal= dt.Rows[n]["vc_OutNumberLocal"].ToString();
				  }
				  if(dt.Rows[n]["vc_OppositeIP"]!=null && dt.Rows[n]["vc_OppositeIP"].ToString()!="")
				  {
				     model.vc_OppositeIP= dt.Rows[n]["vc_OppositeIP"].ToString();
				  }
				  if(dt.Rows[n]["i_Port"]!=null && dt.Rows[n]["i_Port"].ToString()!="")
				  {
				      model.i_Port=int.Parse(dt.Rows[n]["i_Port"].ToString());
				  }
				  if(dt.Rows[n]["i_OppositePort"]!=null && dt.Rows[n]["i_OppositePort"].ToString()!="")
				  {
				      model.i_OppositePort=int.Parse(dt.Rows[n]["i_OppositePort"].ToString());
				  }
				  if(dt.Rows[n]["i_State"]!=null && dt.Rows[n]["i_State"].ToString()!="")
				  {
				      model.i_State=int.Parse(dt.Rows[n]["i_State"].ToString());
				  }
				  if(dt.Rows[n]["i_Type"]!=null && dt.Rows[n]["i_Type"].ToString()!="")
				  {
				      model.i_Type=int.Parse(dt.Rows[n]["i_Type"].ToString());
				  }
				  if(dt.Rows[n]["i_Level"]!=null && dt.Rows[n]["i_Level"].ToString()!="")
				  {
				      model.i_Level=int.Parse(dt.Rows[n]["i_Level"].ToString());
				  }
				  if(dt.Rows[n]["i_MaxChannel"]!=null && dt.Rows[n]["i_MaxChannel"].ToString()!="")
				  {
				      model.i_MaxChannel=int.Parse(dt.Rows[n]["i_MaxChannel"].ToString());
				  }
				  if(dt.Rows[n]["i_PlaySound"]!=null && dt.Rows[n]["i_PlaySound"].ToString()!="")
				  {
				      model.i_PlaySound=int.Parse(dt.Rows[n]["i_PlaySound"].ToString());
				  }
				  if(dt.Rows[n]["i_OperateState"]!=null && dt.Rows[n]["i_OperateState"].ToString()!="")
				  {
				      model.i_OperateState=int.Parse(dt.Rows[n]["i_OperateState"].ToString());
				  }
				  if(dt.Rows[n]["vc_Memo"]!=null && dt.Rows[n]["vc_Memo"].ToString()!="")
				  {
				     model.vc_Memo= dt.Rows[n]["vc_Memo"].ToString();
				  }
				  if(dt.Rows[n]["i_Flag"]!=null && dt.Rows[n]["i_Flag"].ToString()!="")
				  {
				      model.i_Flag=int.Parse(dt.Rows[n]["i_Flag"].ToString());
				  }
				   modelList.Add(model);
				
				}
			}
			return modelList;
		}
        //获取SIP中继接口
        public static bool GetSipTrunk(out List<DB_Talk.Model.m_SIPInterface> lst)
        {
            byte[] byteArray = new byte[ArraySizeBig];
            int len = 0;
            bool b = MBoxSDK.ConfigSDK.MBOX_GetSipTrunk(Global.Params.BoxHandle, byteArray, (uint)byteArray.Length, ref len);
            lst = new List<DB_Talk.Model.m_SIPInterface>();
            if (b)
            {
                //List<DB_Talk.Model.m_SAPPoint> lstSAPPoint = new List<DB_Talk.Model.m_SAPPoint>();
                //GetSipSap(out lstSAPPoint);

                string str = System.Text.Encoding.Default.GetString(byteArray);
                str = str.Replace("\0", "");
                string[] strArray = str.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                for (int i = 0; i < strArray.Length; i++)
                {
                    DB_Talk.Model.m_SIPInterface m = new DB_Talk.Model.m_SIPInterface();
                    string[] strArray2 = strArray[i].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                    m.SIPID = int.Parse(strArray2[0]);
                    m.i_Type = int.Parse(strArray2[1]);
                    m.RouteID = int.Parse(strArray2[2]);
                    m.i_Level = int.Parse(strArray2[3]);
                    m.i_MaxChannel = int.Parse(strArray2[4]);
                    m.SAPID = int.Parse(strArray2[5]);
                    m.i_PlaySound = int.Parse(strArray2[6]);
                    m.i_OppositePort = int.Parse(strArray2[7]);
                    m.i_State = int.Parse(strArray2[8]);
                    m.i_OperateState = int.Parse(strArray2[9]);
                    m.vc_OppositeIP =(strArray2[10]);
                    m.BoxID = Global.Params.BoxID;
                   
                    //DB_Talk.Model.m_SIPInterface sip = new DB_Talk.BLL.m_SIPInterface().GetModel(
                    //              string.Format("i_Flag=0 and BoxID={0} and SIPID='{1}'", Global.Params.BoxID, m.SIPID));
                    //if (sip != null)
                    //{
                    //    m.vc_OutNumber = sip.vc_OutNumber;
                    //    m.i_Port = sip.i_Port;
                    //    lst.Add(m);
                    //}
                    //else
                    //    DeleteSIP(m);
                    lst.Add(m);
                }
            }
            return b;
        }
		/// <summary>
		/// 根据条件得到一个对象实体
		/// </summary>
		public DB_Talk.Model.m_SIPInterface GetModel(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("  select top 1 ");
			strSql.Append("  ID, BoxID, vc_Code, vc_Name, SIPID, RouteID, SAPID, vc_OutNumber, vc_OutNumberLocal, vc_OppositeIP, i_Port, i_OppositePort, i_State, i_Type, i_Level, i_MaxChannel, i_PlaySound, i_OperateState, vc_Memo, i_Flag  ");			
			strSql.Append("  from m_SIPInterface ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			DB_Talk.Model.m_SIPInterface model=new DB_Talk.Model.m_SIPInterface();
			DataSet ds=GetDataSet(strSql.ToString());
			if(ds!=null && ds.Tables[0].Rows.Count>0)
			{
                model=DataTableToList(ds)[0];						
				return model;
			}
			else
			{
				return null;
			}
		}