private void btnAdd_Click(object sender, EventArgs e)
 {
     if (!IsSetBoxNumLenAndHead())
     {
         CommControl.MessageBoxEx.MessageBoxEx.Show("还未进行基本设置,不能执行添加操作!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
         return;
     }
     
     DB_Talk.BLL.m_Member _BLL = new DB_Talk.BLL.m_Member();
     List<DB_Talk.Model.m_Member> list = new List<DB_Talk.Model.m_Member>();
     list = _BLL.GetModelList(string.Format(" i_flag=0 and BoxID='{0}'", Global.Params.BoxID));
     if (list.Count >= Global.Params.MaxBoxMemberCount)
     {
         CommControl.MessageBoxEx.MessageBoxEx.Show(string.Format("不能添加,号码已经达到最大限制,【{0}】个!", Global.Params.MaxBoxMemberCount), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
         return;
     }
     UI.frmMember frm = new UI.frmMember(null, 0);
     frm.ShowDialog();
     if(frm.DialogResult==DialogResult.OK)
        LoadData("");
 
 }
        private void btnModify_Click(object sender, EventArgs e)
        {
            
            if (!IsSetBoxNumLenAndHead())
            {
                CommControl.MessageBoxEx.MessageBoxEx.Show("还未进行基本设置,不能执行修改操作!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            StringBuilder sb = new StringBuilder();
            /*
            List<DB_Talk.Model.v_Member> list = new List<DB_Talk.Model.v_Member>();
            foreach (DataGridViewRow dr in dgvList.Rows)
            {
                if (Convert.ToBoolean(dr.Cells[0].Value.ToString()) == true)
                {

                    DB_Talk.Model.v_Member Model = (DB_Talk.Model.v_Member)dr.Tag; // dgvList.CurrentRow.Tag;
                    list.Add(Model);
                    sb.Append("," + Model.ID);
                }
            }
            */
            int count = dgvList.SelectedRows.Count;
            int operate = 0;
            List<DB_Talk.Model.v_Member> lstvModel = new List<DB_Talk.Model.v_Member>();
            //上次选中的行数
            int lastIndex = 0;
            if (dgvList.SelectedRows.Count <= 0)
            {
                CommControl.MessageBoxEx.MessageBoxEx.Show("请选择要修改的人员!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return;
            }
            lastIndex = dgvList.SelectedRows[0].Index;
            if (count > 1)
            {
                operate = 2; //修改多个
               
                foreach (DataGridViewRow item in dgvList.SelectedRows)
                {
                   
                    DB_Talk.Model.v_Member _mModel = (DB_Talk.Model.v_Member)item.Tag; // dgvList.CurrentRow.Tag;
                    bool b = MBoxSDK.ConfigSDK.MBOX_IsSubscriberExist(Global.Params.BoxHandle, _mModel.i_Number.Value);
                    if (b == false)
                    {
                        CommControl.MessageBoxEx.MessageBoxEx.Show(Global.Params.BOXNAME + "【" + Global.Params.BoxIP + "】中已经不存在此用户,不能再进行修改,只能进行删除操作!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                        return;
                    }
                  
                    lstvModel.Add(_mModel); 
                }
                //CommControl.MessageBoxEx.MessageBoxEx.Show("一次只允许编辑一个人员,请重新选择", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            else if(count == 1)
            {
                operate = 1;
                DB_Talk.Model.v_Member Model = (DB_Talk.Model.v_Member)dgvList.CurrentRow.Tag; //list[0];// 
                if (Model != null)
                {
                    bool b = MBoxSDK.ConfigSDK.MBOX_IsSubscriberExist(Global.Params.BoxHandle, Model.i_Number.Value);
                    if (b==false)
                    {
                        CommControl.MessageBoxEx.MessageBoxEx.Show(Global.Params.BOXNAME + "【" + Global.Params.BoxIP + "】中已经不存在此用户,不能再进行修改,只能进行删除操作!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                        return;
                    }
                    lstvModel.Add(Model); 
                }
            }
            else
            {
                CommControl.MessageBoxEx.MessageBoxEx.Show("请选择要编辑的人员", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return;
            }

            UI.frmMember fu = new UI.frmMember(lstvModel, operate);
            fu.ShowDialog();
            if (fu.DialogResult == DialogResult.OK)
                LoadData("");
           // dgvList.CurrentCell = dgvList[lastIndex, 0];
            dgvList.ClearSelection();
            if (lastIndex<dgvList.Rows.Count)
            {
                dgvList.Rows[lastIndex].Selected = true;
                dgvList.CurrentCell = dgvList[1,lastIndex];
            }
            
        }
        //CommControl.MessageBoxEx.MessageBoxEx messagebox;
        private void btnOK_Click(object sender, EventArgs e)
        {
            DB_Talk.BLL.m_Member _BLL = new DB_Talk.BLL.m_Member();
            List<DB_Talk.Model.m_Member> listCount = new List<DB_Talk.Model.m_Member>();
            listCount = _BLL.GetModelList(string.Format(" i_flag=0 and BoxID='{0}'", Global.Params.BoxID));
            if (listCount.Count >= Global.Params.MaxBoxMemberCount)
            {
                CommControl.MessageBoxEx.MessageBoxEx.Show(string.Format("不能导入,号码已经达到最大限制,【{0}】个!", Global.Params.MaxBoxMemberCount), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            Bestway.Windows.Forms.ProgressBarDialog procDlg = null;
            procDlg = new Bestway.Windows.Forms.ProgressBarDialog();
            try
            {
                procDlg.Show(Bestway.Windows.Forms.EnumDisplayType.LoadData, "      正在导入用户,请稍等...");
                DataTable dt = new DataTable();
                dt = (DataTable)dgvList.DataSource;
                if (dt == null)
                {
                    throw new Exception("没有数据!");
                    
                }
                DB_Talk.BLL.m_Member bll = new DB_Talk.BLL.m_Member();
                StringBuilder sbAdd = new StringBuilder();
                List<DB_Talk.Model.m_Member> listAdd = new List<DB_Talk.Model.m_Member>();

                StringBuilder sbNotAdd = new StringBuilder();
                StringBuilder sbAdded = new StringBuilder();     //box中已经存在的
                StringBuilder sbBoxMaxNotAdded = new StringBuilder();  //box到达最大值未添加的
                //List<DB_Talk.Model.m_Member> list = (List < DB_Talk.Model.m_Member >)dgvListOk.DataSource;
                List<DB_Talk.Model.m_Member> list = DataTableToModelList(dt);

                int iAdd=0,iAdded = 0;  //添加成功数,已经存在的数
                int iRepeatIP=0;  //IP已经存在的
                int iNotAdd = 0;     //添加失败数
                int iBoxMaxNotAdded = 0;    //到达最大后的用户
                if (dt.Rows.Count == 0)
                {
                    throw new Exception("没有数据可以导入!");
                }
                if (list.Count == 0)
                {
                    throw new Exception("没有合法的数据可以导入!");
                }

                foreach (DB_Talk.Model.m_Member item in list)
                {
                    if (listCount.Count + iAdded >= Global.Params.MaxBoxMemberCount)  //到达最大值
                    {
                        iBoxMaxNotAdded++;
                        sbBoxMaxNotAdded.Append("," + item.i_Number);

                    }
                    else
                    {
                        string strW = " i_Flag=0 and BoxID='" + Global.Params.BoxID + "'";
                        //strW += " and vc_Name='" + item.vc_Name + "'";
                        strW += " and i_Number='" + item.i_Number + "'";

                       

                        bool IsExist = MBoxSDK.ConfigSDK.MBOX_IsSubscriberExist(Global.Params.BoxHandle,(item.i_Number.Value));
                        CommControl.Tools.WriteLog.AppendLog("box添加号码", "boxHandle:" + Global.Params.BoxHandle + ", 号码:" + Convert.ToInt32(item.i_Number.ToString()) + ", 是否存在:" + IsExist);
          
                        if (bll.Exists(strW) || IsExist)
                            //MBoxSDK.ConfigSDK.MBOX_IsSubscriberExist(Global.Params.BoxHandle, Convert.ToInt32(item.i_Number.ToString())))
                        {
                            iAdded++;
                            sbAdded.Append("," + item.i_Number);//.vc_Name);
                        }
                        else if (item.i_TellType.Value == PublicEnums.EnumTelType.广播.GetHashCode() &&
                          _BLL.GetModelList(string.Format(" i_flag=0 and  vc_IP='{0}' and BoxID='{1}'", item.vc_IP, item.BoxID)).Count > 0)
                        {
                           //(string.Format("数据库中IP【{0}】已存在!", item.i_Number)
                            iRepeatIP++;
                        }
                        else
                        {
                            item.i_supplementSerive = (int)(MBoxSDK.ConfigSDK.SPM_MISS_CALL |
                                                            MBoxSDK.ConfigSDK.SPM_MISS_CALL_ON_BUSY |
                                                            MBoxSDK.ConfigSDK.SPM_SMS |
                                                            MBoxSDK.ConfigSDK.SPM_THREE_PARTY |
                                                            MBoxSDK.ConfigSDK.SPM_CALL_WAITING |
                                                            MBoxSDK.ConfigSDK.SPM_CALL_TRANSFER); // |
                                                           // MBoxSDK.ConfigSDK.SPM_CALL_PICKUP);
                            bool create= new frmMember(null, 0).AddBase(item);
                            //bool create =  MBoxSDK.ConfigSDK.MBOX_CreateSubscriber(Global.Params.BoxHandle,ref subscriberBase);
                            if (create)
                            //MBoxSDK.ConfigSDK.MBOX_CreateSubscriber(Global.Params.BoxHandle, Convert.ToInt32(item.i_Number.ToString())))
                            {
                                listAdd.Add(item);
                                //bll.Add(item);
                                //CommControl.SystemLogBLL.WriteLog(Global.Params.UserID, Global.Params.BoxID, CommControl.SystemLogBLL.EnumLogAction.Add, "导入", "导入了人员:" + item.vc_Name, "");
                                sbAdd.Append("," + item.i_Number);

                                iAdd++;
                                procDlg.Show(Bestway.Windows.Forms.EnumDisplayType.LoadData, "      用户【" + item.vc_Name + "】正在写入硬件,请稍等...");
                            }
                            else
                            {
                                sbNotAdd.Append("," + item.i_Number); //.vc_Name);
                                iNotAdd++;

                            }
                        }
                    }
                }
                string mes = "";
                bool bSave = false;
                if (iAdd > 0)
                {
                    TimeSpan sp = new TimeSpan(0);
                    DateTime dtstart = System.DateTime.Now;
                    bSave = MBoxSDK.ConfigSDK.MBOX_SaveHaveDoneCfg(Global.Params.BoxHandle); //保存更改
                    DateTime dtEnd = System.DateTime.Now;
                    if (bSave)
                    {
                        sp = dtEnd - dtstart;
                        //CommControl.MessageBoxEx.MessageBoxEx.Show(sp.TotalMilliseconds.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    //sbAdd.Remove(0, 1);
                }
                if (bSave)
                {
                    foreach (DB_Talk.Model.m_Member item in listAdd)
                    {
                        procDlg.Show(Bestway.Windows.Forms.EnumDisplayType.LoadData, "      用户【" + item.vc_Name + "】加入数据库,请稍等...");
                     
                        bll.Add(item);
                        CommControl.SystemLogBLL.WriteLog(Global.Params.UserID, Global.Params.BoxID, CommControl.SystemLogBLL.EnumLogAction.Add, "导入", "导入了人员:" + item.vc_Name, "");
                    }
                           
                    if (iAdd > 10)
                    {
                        mes = "共导入【" + iAdd + "】个用户!\r\n";
                    }
                    else if (iAdd > 0 && iAdd <= 10)
                    {
                        sbAdd.Remove(0, 1);
                        mes = "号码【" + sbAdd.ToString() + "】导入成功!\r\n";
                    }

                }
                else if (iAdd!=0)
                {
                    mes = "保存硬件配置失败!\r\n";
                }

                if (iNotAdd > 10)
                {
                    mes += "【" + iNotAdd + "】个号码写入硬件失败!\r\n";
                }
                else if (iNotAdd > 0 && iNotAdd <= 10)
                {
                    sbNotAdd.Remove(0, 1);
                    mes += "号码【" + sbNotAdd.ToString() + "】写入硬件失败!\r\n";
                }

                if (iAdded > 10)
                {
                    mes += "【" + iAdded + "】个号码已经存在,不再导入!\r\n";
                }
                else if (iAdded > 0 && iAdded <= 10)
                {
                    sbAdded.Remove(0, 1);
                    mes += "号码【" + sbAdded.ToString() + "】已经存在,不再导入!\r\n";
                }
                if(iBoxMaxNotAdded>5)
                {
                    mes += "设备中号码已经达到最大值,【" + iBoxMaxNotAdded + "】个号码,不再导入!";
                }
                else if (iBoxMaxNotAdded > 0 && iBoxMaxNotAdded <= 5)
                {
                    sbBoxMaxNotAdded.Remove(0, 1);
                    mes += "设备中号码已经达到最大值,号码【" + sbBoxMaxNotAdded + "】,不再导入!";
                }
                if (iRepeatIP>0)
                {
                    mes += "【" + iRepeatIP + "】个重复IP,不再导入!";
                }

                procDlg.Dispose();
                if (mes.Length > 0)
                {
                    CommControl.MessageBoxEx.MessageBoxEx.Show(mes, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
            }
            catch (Exception ex)
            {
                procDlg.Dispose();
                CommControl.MessageBoxEx.MessageBoxEx.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
           
            
           
         }