private void btnDel_Click(object sender, EventArgs e)
 {
     if (dgvList.SelectedRows.Count > 0)
     {
         DB_Talk.Model.m_Box typeModel = (DB_Talk.Model.m_Box)dgvList.CurrentRow.Tag;
         if (typeModel != null)
         {
             if (CommControl.MessageBoxEx.MessageBoxEx.Show("确认要删除 【" + typeModel.vc_Name + "】 吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
             {
                 bool isExit_manager = new DB_Talk.BLL.m_Manager().Exists("BoxId='" + typeModel.ID + "'");
                 bool isExit_member = new DB_Talk.BLL.m_Member().Exists("BoxId='" + typeModel.ID + "'");
                 List<DB_Talk.Model.m_Group> lst = new DB_Talk.BLL.m_Group().GetModelList("i_Flag=0 and BoxId='" + typeModel.ID + "'");
                 bool isExit_group = false;
               
                 foreach (DB_Talk.Model.m_Group m in lst)
                 {
                     if (m.vc_Name != Global.Params.gruopNormalName)
                     {
                         isExit_group = true;
                         break;
                     }
                 }
                 bool isExit_CalledRule = new DB_Talk.BLL.m_CalledRule().Exists("BoxId='" + typeModel.ID + "' and i_Flag=0 and vc_CalledNumber!='*000'");
                 //bool isExit_group = new DB_Talk.BLL.m_Group().Exists("BoxId='" + typeModel.ID + "'");
                 if (isExit_manager || isExit_member || isExit_group || isExit_CalledRule)
                 //if (isExit_member)
                 {
                     CommControl.MessageBoxEx.MessageBoxEx.Show(Global.Params.BOXNAME + "已经被引用,不可以删除!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                     return;
                 }
                 if (new DB_Talk.BLL.m_Box().DeleteEx(typeModel.ID))
                 {
                     new DB_Talk.BLL.m_Group().Delete("i_Flag=0 and BoxId='" + typeModel.ID + "'");
                     CommControl.MessageBoxEx.MessageBoxEx.Show("删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                     CommControl.SystemLogBLL.WriteLog(Global.Params.UserID, typeModel.ID, CommControl.SystemLogBLL.EnumLogAction.Delete, "删除", "删除了站点:" + typeModel.vc_Name, "");
                     //.BLL.m_SystemLog.WriteLog(Global.Params.UserID, DB_FileManage.Model.m_SystemLog.EnumLogAction.Delete, "删除文件等级", "删除文件等级:" + typeModel.vc_Name);
                     LoadData();
                     Global.Params.LstBox.Remove(typeModel);
                     dr = DialogResult.OK;
                 }
                 else
                 {
                     CommControl.MessageBoxEx.MessageBoxEx.Show("删除失败!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                 }
             }
         }
     }
     else
     {
         CommControl.MessageBoxEx.MessageBoxEx.Show("请选择要删除的" +Global.Params.BOXNAME, "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
     }
 }
        //获取呼叫规则
        private void GetCalledRule(int SIPID,int PRIID, string strNewOutNumber, string strOldOutNumber, 
            ref  List<DB_Talk.Model.m_CalledRule> lstAdd, ref  List<DB_Talk.Model.m_CalledRule> lstDelete,MBoxSDK.ConfigSDK.CALLED_SUB_RULE_TYPE CalledSubType)
        {
            //出局号码被叫规则
            string mes = "";
            string[] strArrayAdd = strNewOutNumber.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
             //lstAdd = new List<DB_Talk.Model.m_CalledRule>();
            //lstDelete = new List<DB_Talk.Model.m_CalledRule>();
            foreach (string str in strArrayAdd)  //出局规则
            {
                strOldOutNumber=strOldOutNumber.Replace(str, "");  //从旧的引导码中除去新的引导码
                DB_Talk.Model.m_CalledRule m = new DB_Talk.Model.m_CalledRule();
                m.CalledID = 1;
                m.CallingOriID = 1;
                m.vc_CalledNumber = (str);
                int num = 0;
                if (!int.TryParse(str, out num) || num<0 || num > 99999)
                {
                    throw new Exception("号码前缀必须是0~99999之间的数字");
                }
                m.i_CalledType = MBoxSDK.ConfigSDK.CALLED_RULE_TYPE.出局.GetHashCode();  //service
                m.i_CalledSubType = CalledSubType.GetHashCode();// MBoxSDK.ConfigSDK.CALLED_SUB_RULE_TYPE.长途.GetHashCode();
                m.DestRouteID = 0;
                m.i_CalledChangeType = MBoxSDK.ConfigSDK.CALLED_RULE_TransAct.DELETE.GetHashCode();
                m.i_CalledChangePosition = 0;
                m.i_CalledChangeLength = str.Length;
                m.vc_CalledChangeTarget = "";
                m.i_SIPID = SIPID;
                m.i_PRIID = PRIID;
                m.BoxID = Global.Params.BoxID;
                //if (!new Tools.MBoxOperate().IsExitCalledRule(m)) lst.Add(m);
                //if (new Tools.MBoxOperate().IsExitCalledRule(m, out mes) && mes != "")
                //{
                //    throw (new Exception("已经存在被叫号码为【" + mes + "】的呼叫规则!"));
                //}
                List<DB_Talk.Model.m_CalledRule> lstTemp = new DB_Talk.BLL.m_CalledRule().GetModelList(
                            string.Format(" i_Flag=0 and vc_CalledNumber='{0}' and BoxID='{1}'", 
                            m.vc_CalledNumber, Global.Params.BoxID));
                if (lstTemp.Count > 0)
                { 
                    if(m.Equals(lstTemp[0]))  //完全相同
                    {

                    }
                    else if (m.vc_CalledNumber == lstTemp[0].vc_CalledNumber)  //引导码相同
                    {
                        throw (new Exception("已经存在被叫号码为【" + m.vc_CalledNumber + "】的呼叫规则!"));
                    }
                } 
                else
                    lstAdd.Add(m);
            }

            string[] strArrayDelete = strOldOutNumber.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string str in strArrayDelete)  //出局规则
            {
                DB_Talk.Model.m_CalledRule m = new DB_Talk.Model.m_CalledRule();
                m.CalledID = 1;
                m.CallingOriID = 1;
                m.vc_CalledNumber = (str);
                m.i_CalledType = MBoxSDK.ConfigSDK.CALLED_RULE_TYPE.出局.GetHashCode();  //service
                m.i_CalledSubType = CalledSubType.GetHashCode();// MBoxSDK.ConfigSDK.CALLED_SUB_RULE_TYPE.长途.GetHashCode();
                m.DestRouteID = 0;
                m.i_CalledChangeType = MBoxSDK.ConfigSDK.CALLED_RULE_TransAct.DELETE.GetHashCode();
                m.i_CalledChangePosition = 0;
                m.i_CalledChangeLength = str.Length;
                m.vc_CalledChangeTarget = "";
                m.BoxID = Global.Params.BoxID;

                lstDelete.Add(m);
            }

        }
        private void CheckDeleteCondition(DB_Talk.Model.m_CalledRule mcall)
        {
            int SipPriID=0;
            string strC="";
            string mes = "";
            if (mcall.i_SIPID > 0)
            {
                strC = "i_SIPID";
                SipPriID = mcall.i_SIPID;
                mes = "SIP";
            }
            else if (mcall.i_PRIID > 0)
            {
                strC = "i_PRIID";
                SipPriID = mcall.i_PRIID;
                mes = "PRI";
            }
            else
                return;
            string subType="";
            if (mcall.i_CalledSubType == MBoxSDK.ConfigSDK.CALLED_SUB_RULE_TYPE.市话.GetHashCode())
            {
                subType = "市话出局号码前缀";
            }
            else if (mcall.i_CalledSubType == MBoxSDK.ConfigSDK.CALLED_SUB_RULE_TYPE.长途.GetHashCode())
            {
                subType = "长途出局号码前缀";
            }
            else return;

            List<DB_Talk.Model.m_CalledRule> lst = new List<DB_Talk.Model.m_CalledRule>();
            string strWhere = string.Format("i_Flag=0 and BoxID='{0}' and i_CalledType ='{1}' and i_CalledSubType='{2}' and {3}='{4}'",
                Global.Params.BoxID, MBoxSDK.ConfigSDK.CALLED_RULE_TYPE.出局.GetHashCode(), mcall.i_CalledSubType, strC, SipPriID);
            lst = new DB_Talk.BLL.m_CalledRule().GetModelList(strWhere);
            if (lst.Count <= 1)
                throw new Exception(mes + "中继中" + subType + "只剩下【" + mcall.vc_CalledNumber + "】,不能删除!\r\n如果需要删除,请删除" + mes + "中继【" + SipPriID + "】!");
           

        }
        private void btnDel_Click(object sender, EventArgs e)
        {
            if (dgvList.CurrentRow != null)
            {
                bool delete = false;
                Bestway.Windows.Forms.ProgressBarDialog procDlg = new Bestway.Windows.Forms.ProgressBarDialog();
                try
                {
                    DB_Talk.Model.m_CalledRule model = dgvList.CurrentRow.Tag as DB_Talk.Model.m_CalledRule;
                    if (model != null)
                    {
                        CheckDeleteCondition(model);
                        List<DB_Talk.Model.m_Member> lstMember=new DB_Talk.BLL.m_Member().GetModelList(
                           string.Format(" i_Flag=0 and i_Number like '{0}%' and BoxID='{1}' ", model.vc_CalledNumber, Global.Params.BoxID));
                          // string.Format(" i_Flag=0 and i_Number like '{0}%' and BoxID='{1}' and i_IsDispatch!=1", model.vc_CalledNumber, Global.Params.BoxID));

                        if (lstMember.Count > 0)
                        {
                            CommControl.MessageBoxEx.MessageBoxEx.Show("用户号码中引用了此呼叫规则,不允许删除,要想删除,请先删除用户号码!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            return;
                        }

                        procDlg.Show(Bestway.Windows.Forms.EnumDisplayType.LoadData, "      正在删除被叫规则【" + model.vc_CalledNumber + "】,请稍等...");
                        List<DB_Talk.Model.m_CalledRule> lstAdd = new List<DB_Talk.Model.m_CalledRule>();
                        List<DB_Talk.Model.m_CalledRule> lstDelete = new List<DB_Talk.Model.m_CalledRule>();
                        lstDelete.Add(model);
                        DB_Talk.Model.m_SIPInterface modelSip = null;
                        DB_Talk.Model.m_PRIInterface modelPri = null;

                        if (model.i_SIPID > 0)
                            modelSip = new DB_Talk.BLL.m_SIPInterface().GetModel(
                                 string.Format(" i_Flag=0 and SIPID='{0}' and BoxID='{1}'", model.i_SIPID, Global.Params.BoxID));
                        if (modelSip != null)
                        {
                            List<DB_Talk.Model.m_CalledRule> lstm = new DB_Talk.BLL.m_CalledRule().GetModelList(
                               string.Format(" i_Flag=0 and i_SIPID='{0}' and BoxID='{1}' and ID!='{2}'",
                               model.i_SIPID, Global.Params.BoxID, model.ID));
                            if (lstm.Count > 0)  //只删除呼叫规则(还存在引用此中继的呼叫规则)
                            {
                                if (Tools.MBoxOperate.Delete_Rule(lstAdd, lstDelete))
                                {
                                    delete = true;
                                    if (model.i_CalledSubType == MBoxSDK.ConfigSDK.CALLED_SUB_RULE_TYPE.市话.GetHashCode())
                                    {
                                        modelSip.vc_OutNumberLocal = modelSip.vc_OutNumberLocal.Replace(model.vc_CalledNumber, "").Replace(",,",",").Trim(','); ;
                                    }
                                    else if (model.i_CalledSubType == MBoxSDK.ConfigSDK.CALLED_SUB_RULE_TYPE.长途.GetHashCode())
                                    {
                                        modelSip.vc_OutNumber = modelSip.vc_OutNumber.Replace(model.vc_CalledNumber, "").Replace(",,", ",").Trim(','); ;
                                    }
                                     new DB_Talk.BLL.m_SIPInterface().Update(modelSip);
                                }
                            }
                            else  //连中继一起删除
                            {
                                if (Tools.MBoxOperate.DeleteSIP(modelSip, lstAdd, lstDelete) && new DB_Talk.BLL.m_SIPInterface().Delete(modelSip.ID))
                                {
                                    delete = true;
                                }
                            }
                        }

                        if (model.i_PRIID > 0)
                            modelPri = new DB_Talk.BLL.m_PRIInterface().GetModel(
                                 string.Format(" i_Flag=0 and PRIID='{0}' and BoxID='{1}'", model.i_PRIID, Global.Params.BoxID));

                        if (modelPri != null)
                        {
                            List<DB_Talk.Model.m_CalledRule> lstm = new DB_Talk.BLL.m_CalledRule().GetModelList(
                                string.Format(" i_Flag=0 and i_PRIID='{0}' and BoxID='{1}' and ID!='{2}'",
                                model.i_PRIID, Global.Params.BoxID, model.ID));
                            if (lstm.Count > 0 && Tools.MBoxOperate.Delete_Rule(lstAdd, lstDelete))  //只删除呼叫规则
                            {
                                if (model.i_CalledSubType == MBoxSDK.ConfigSDK.CALLED_SUB_RULE_TYPE.市话.GetHashCode())
                                {
                                    //modelPri.vc_OutNumberLocal = modelPri.vc_OutNumberLocal.Replace(model.vc_CalledNumber, "").Replace(",,", ",").Trim(',');
                                    modelPri.vc_OutNumberLocal = ReplaceString(modelPri.vc_OutNumberLocal, model.vc_CalledNumber);
                                }
                                else if (model.i_CalledSubType == MBoxSDK.ConfigSDK.CALLED_SUB_RULE_TYPE.长途.GetHashCode())
                                {
                                   // modelPri.vc_OutNumber = modelPri.vc_OutNumber.Replace(model.vc_CalledNumber, "").Replace(",,", ",").Trim(',');
                                    modelPri.vc_OutNumber = ReplaceString(modelPri.vc_OutNumber, model.vc_CalledNumber);
                                }
                                 new DB_Talk.BLL.m_PRIInterface().Update(modelPri);
                                delete = true;
                            }
                            else  //连中继一起删除
                            {
                                if (Tools.MBoxOperate.DeletePRI(modelPri, lstAdd, lstDelete) && new DB_Talk.BLL.m_PRIInterface().Delete(modelPri.ID))
                                {
                                    delete = true;
                                }
                            }
                        }

                        if (modelSip == null && modelPri == null)  //删除的入局规则
                        {

                            if (Tools.MBoxOperate.Delete_Rule(lstAdd, lstDelete))
                            {
                                DB_Talk.Model.m_Box BoxModel = new DB_Talk.BLL.m_Box().GetModel(Global.Params.BoxID);
                                if (BoxModel != null && BoxModel.vc_NumberHead!=null)  
                                {

                                    BoxModel.vc_NumberHead = BoxModel.vc_NumberHead.Replace(lstDelete[0].vc_CalledNumber, "").Replace(",,", ",").Trim(',');
                                    
                                    new DB_Talk.BLL.m_Box().Update(BoxModel);
                                }
                                delete = true;
                            }
                        }
                    }
                    else  //还未添加 直接删除
                        delete = true;


                    if (delete)
                    {
                        MBoxSDK.ConfigSDK.MBOX_SaveHaveDoneCfg(Global.Params.BoxHandle);
                        procDlg.Hide();
                        Loaddata();
                        //dgvList.Rows.Remove(dgvList.CurrentRow);
                        CommControl.MessageBoxEx.MessageBoxEx.Show("删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                    }
                    else
                    {
                        procDlg.Hide();
                        CommControl.MessageBoxEx.MessageBoxEx.Show("删除失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                    }
                }
                catch (Exception ex)
                {
                    procDlg.Hide();
                    CommControl.MessageBoxEx.MessageBoxEx.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    //CommControl.Tools.WriteLog.AppendErrorLog(ex);
                }
                finally
                {
                    procDlg.Dispose();
                }
            }
            else
            {
                CommControl.MessageBoxEx.MessageBoxEx.Show("请选择要删除的呼叫规则!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
        }
        private void Loaddata()
        {
            List<DB_Talk.Model.m_CalledRule> lst = new List<DB_Talk.Model.m_CalledRule>();
            string strWhere = string.Format("i_Flag=0 and BoxID='{0}' and i_CalledType!='{1}' order by vc_CalledNumber ", 
                Global.Params.BoxID,MBoxSDK.ConfigSDK.CALLED_RULE_TYPE.SERVICE.GetHashCode());
            lst = new DB_Talk.BLL.m_CalledRule().GetModelList(strWhere);
            dgvList.Rows.Clear();

            for (int i = 0; i < lst.Count; i++)
            {
                string CalledType = ((MBoxSDK.ConfigSDK.CALLED_RULE_TYPE)lst[i].i_CalledType).ToString();
                string CalledSubType = ((MBoxSDK.ConfigSDK.CALLED_SUB_RULE_TYPE)lst[i].i_CalledSubType).ToString();

                dgvList.Rows[dgvList.Rows.Add(lst[i].ID,
                                 lst[i].vc_CalledNumber,
                                 CalledType,
                                 CalledSubType,
                                 lst[i].i_SIPID == 0 ? "" : lst[i].i_SIPID.ToString(),
                                 lst[i].i_PRIID == 0 ? "" : lst[i].i_PRIID.ToString(),
                                 lst[i].i_CalledChangeLength
                                )].Tag = lst[i];

            }
            dgvList.ClearSelection();
            kryptonHeaderGroup1.ValuesSecondary.Heading = "  共" + dgvList.Rows.Count.ToString() + "条记录";

        }
        //同步被叫规则
        public static void UpdateDB_CallRule()
        {
            List<DB_Talk.Model.m_CalledRule> lstCR = new List<DB_Talk.Model.m_CalledRule>();
            QueryCalledRule(out lstCR);
            if (lstCR.Count == 0) new DB_Talk.BLL.m_CalledRule().Delete(string.Format(" BoxID='{0}'", Global.Params.BoxID));
            StringBuilder sb = new StringBuilder();
            foreach (DB_Talk.Model.m_CalledRule m in lstCR)
            {
                //if(m.vc_CalledNumber=="*000") continue;
                sb.Append(",'" + m.vc_CalledNumber + "'");
                DB_Talk.Model.m_CalledRule callm = new DB_Talk.BLL.m_CalledRule().GetModel(
                             string.Format("i_Flag=0 and BoxID={0} and vc_CalledNumber='{1}'", Global.Params.BoxID, m.vc_CalledNumber));
                if (callm != null)
                {
                    m.i_SIPID = callm.i_SIPID;
                    m.i_PRIID = callm.i_PRIID;
                    new DB_Talk.BLL.m_CalledRule().Delete(string.Format("i_Flag=0 and BoxID={0} and vc_CalledNumber='{1}'", Global.Params.BoxID, m.vc_CalledNumber));

                    new DB_Talk.BLL.m_CalledRule().Add(m);
                }
                else
                    DeleteCalledRuleByNum(m);  //删除box中 数据库中不存在的呼叫规则
            }
            //删除数据库中多余的
            if (sb.Length > 0)
            {
                sb.Remove(0, 1);
                new DB_Talk.BLL.m_CalledRule().Delete(
                    string.Format("i_Flag=0 and BoxID={0} and vc_CalledNumber not in({1})",
                    Global.Params.BoxID, sb.ToString()));
            }
        }
        public static string GetCalledNumbers(MBoxSDK.ConfigSDK.CALLED_RULE_TYPE type, string strSIP_PRI_ID)
        {
            StringBuilder sb = new StringBuilder();
            string strW = string.Format("i_Flag=0 and BoxID={0} and i_CalledType='{1}'",
                             Global.Params.BoxID, type.GetHashCode());
            strW += strSIP_PRI_ID;

            List<DB_Talk.Model.m_CalledRule> lstCR = new DB_Talk.BLL.m_CalledRule().GetModelList(strW);
            foreach (DB_Talk.Model.m_CalledRule item in lstCR)
            {
                sb.Append("," + item.vc_CalledNumber);
            }
            if (sb.Length > 0) sb.Remove(0, 1);
            return sb.ToString();

        }
        public bool IsExitCalledRule(DB_Talk.Model.m_CalledRule model,out string mes)
        {
            //不能添加被叫号码相同的被叫规则

            bool b = false;
            mes = "";
            DB_Talk.Model.m_CalledRule IsExit = new DB_Talk.BLL.m_CalledRule().GetModel(
              string.Format(" i_Flag=0 and vc_CalledNumber='{0}' and BoxID='{1}'", model.vc_CalledNumber, Global.Params.BoxID));
            if (IsExit!=null)
            {
                if (IsExit.Equals(model))  //存在完全相同的被叫规则,则什么也不做
                {
                    return true;
                }
                else   //被叫号码相同,但是其他属性不同
                {
                    mes = (model.vc_CalledNumber.ToString());
                    return true;
                }
            }
            else       //不存在以此号码开头的被叫规则
            {
                return false;
            }
            
           
        }
        //创建基础的 呼叫源,路由规则,呼叫源规则,*000的被叫规则
        public static bool CreateCalinglSourceRule()
        {
            
            //创建呼叫源,1,任何类型,任何类型
            DB_Talk.Model.m_CallingSource modelCs = new DB_Talk.Model.m_CallingSource();
            modelCs.ID = 1;
            modelCs.i_MainType = 6;   //任何类型
            modelCs.i_SubType = 1;    //任何类型
            modelCs.BoxID=Global.Params.BoxID;
            List<DB_Talk.Model.m_CallingSource> list = new DB_Talk.BLL.m_CallingSource().GetModelList(
                string.Format(" i_Flag=0 and ID='{0}' and i_MainType='{1}' and i_SubType='{2}' and BoxID='{3}'",
                               modelCs.ID, modelCs.i_MainType, modelCs.i_SubType, modelCs.BoxID));
            if (list.Count == 0) //不存在才创建
            {
                if (!CreateCallSource(modelCs)) 
                    return false;
            }
          

            //创建路由规则0,0,0
            DB_Talk.Model.m_RouteRule modelRRule = new DB_Talk.Model.m_RouteRule();
            modelRRule.ID = 0;
            modelRRule.OriRouteID = 0;
            modelRRule.DestRouteID = 0;
            modelRRule.BoxID = Global.Params.BoxID;
            List<DB_Talk.Model.m_RouteRule> listModelRRule = new DB_Talk.BLL.m_RouteRule().GetModelList(
               string.Format(" i_Flag=0 and ID='{0}' and OriRouteID='{1}' and DestRouteID='{2}' and BoxID='{3}'",
                              modelRRule.ID, modelRRule.OriRouteID, modelRRule.DestRouteID, modelRRule.BoxID));
            if (listModelRRule.Count == 0)
            {
                if (!CreateRoutingRule(modelRRule))
                    return false;
            }
           
            //创建呼叫源规则,1,不限,0,1,0
            DB_Talk.Model.m_CalinglSourceRule CalinglSourceRule = new DB_Talk.Model.m_CalinglSourceRule();
            CalinglSourceRule.CallingOrigID = 1;  //呼叫源索引1
            CalinglSourceRule.i_ServerType = 1;   //服务类型不限
            CalinglSourceRule.i_MinReLength = 0;   //最小收号长度0
            CalinglSourceRule.CalledRuleID = 1;   //被叫分析规则索引1
            CalinglSourceRule.OriRouteID = 0;     //目的路由索引
            CalinglSourceRule.BoxID = Global.Params.BoxID;
            List<DB_Talk.Model.m_CalinglSourceRule> listCallingSource = new DB_Talk.BLL.m_CalinglSourceRule().GetModelList(
              string.Format(" i_Flag=0 and CallingOrigID='{0}' and CalledRuleID='{1}' and i_ServerType='{2}' and OriRouteID='{3}' and BoxID='{4}'",
              CalinglSourceRule.CallingOrigID, CalinglSourceRule.CalledRuleID,CalinglSourceRule.i_ServerType, CalinglSourceRule.OriRouteID, CalinglSourceRule.BoxID));
            if (listCallingSource.Count == 0)
            {
                if (!CreateCallSourceRule(CalinglSourceRule))
                    return false;
            }
           


            //设置默认的被叫规则

            DB_Talk.Model.m_CalledRule CalledRule = new DB_Talk.Model.m_CalledRule();
            CalledRule.BoxID = Global.Params.BoxID;
            CalledRule.CalledID = 1;
            CalledRule.CallingOriID = 1;
            CalledRule.DestRouteID = 0;
            CalledRule.vc_CalledNumber = "*000";
            CalledRule.i_CalledType = MBoxSDK.ConfigSDK.CALLED_RULE_TYPE.SERVICE.GetHashCode();
            CalledRule.i_CalledSubType = MBoxSDK.ConfigSDK.CALLED_SUB_RULE_TYPE.话务员.GetHashCode();

            List<DB_Talk.Model.m_CalledRule> listCall = new DB_Talk.BLL.m_CalledRule().GetModelList(
                 string.Format(" i_Flag=0 and vc_CalledNumber='{0}' and BoxID='{1}'",
                        CalledRule.vc_CalledNumber, CalledRule.BoxID));
            if (listCall.Count == 0)
            {
                if (CreateCalledRule(CalledRule))
                    new DB_Talk.BLL.m_CalledRule().Add(CalledRule);
                else
                    return false;
            }
            //默认400的规则

            DB_Talk.Model.m_CalledRule CalledRule400 = new DB_Talk.Model.m_CalledRule();
            CalledRule400.BoxID = Global.Params.BoxID;
            CalledRule400.CalledID = 1;
            CalledRule400.CallingOriID = 1;
            CalledRule400.DestRouteID = 0;
            CalledRule400.vc_CalledNumber = "400";
            CalledRule400.i_CalledType = MBoxSDK.ConfigSDK.CALLED_RULE_TYPE.SERVICE.GetHashCode();
            CalledRule400.i_CalledSubType = MBoxSDK.ConfigSDK.CALLED_SUB_RULE_TYPE.FD.GetHashCode();
            CalledRule400.i_CalledChangeType = 2;
            CalledRule400.i_CalledChangeLength = 3;
            List<DB_Talk.Model.m_CalledRule> listCall400 = new DB_Talk.BLL.m_CalledRule().GetModelList(
                 string.Format(" i_Flag=0 and vc_CalledNumber='{0}' and BoxID='{1}'",
                        CalledRule400.vc_CalledNumber, CalledRule400.BoxID));
            if (listCall400.Count == 0)
            {
                if (CreateCalledRule(CalledRule400))
                    new DB_Talk.BLL.m_CalledRule().Add(CalledRule400);
                else
                    return false;
            }  

            return true;
        }