void menu_quickMarcQueryRecords_Click(object sender, EventArgs e) { string strError = ""; int nRet = 0; if (this.listView_records.SelectedItems.Count == 0) { strError = "尚未选择要执行 C# 脚本的事项"; goto ERROR1; } // 读者信息缓存 // 如果已经初始化,则保持 if (this.m_biblioTable == null) this.m_biblioTable = new Hashtable(); OpenFileDialog dlg = new OpenFileDialog(); dlg.Title = "请指定 C# 脚本文件"; dlg.FileName = this.m_strUsedMarcQueryFilename; dlg.Filter = "C# 脚本文件 (*.cs)|*.cs|All files (*.*)|*.*"; dlg.RestoreDirectory = true; if (dlg.ShowDialog() != DialogResult.OK) return; this.m_strUsedMarcQueryFilename = dlg.FileName; ItemHost host = null; Assembly assembly = null; nRet = PrepareMarcQuery(this.m_strUsedMarcQueryFilename, out assembly, out host, out strError); if (nRet == -1) goto ERROR1; host.CodeFileName = this.m_strUsedMarcQueryFilename; { host.MainForm = this.MainForm; host.UiForm = this; host.DbType = this.DbType; host.RecordPath = ""; host.ItemDom = null; host.Changed = false; host.UiItem = null; StatisEventArgs args = new StatisEventArgs(); host.OnInitial(this, args); if (args.Continue == ContinueType.SkipAll) return; if (args.Continue == ContinueType.Error) { strError = args.ParamString; goto ERROR1; } } this.MainForm.OperHistory.AppendHtml("<div class='debug begin'>" + HttpUtility.HtmlEncode(DateTime.Now.ToLongTimeString()) + " 开始执行脚本 " + dlg.FileName + "</div>"); stop.Style = StopStyle.EnableHalfStop; stop.OnStop += new StopEventHandler(this.DoStop); stop.Initial("正在针对" + this.DbTypeCaption + "记录执行 C# 脚本 ..."); stop.BeginLoop(); this.EnableControls(false); this.listView_records.Enabled = false; try { if (stop != null) stop.SetProgressRange(0, this.listView_records.SelectedItems.Count); { host.MainForm = this.MainForm; host.DbType = this.DbType; host.RecordPath = ""; host.ItemDom = null; host.Changed = false; host.UiItem = null; StatisEventArgs args = new StatisEventArgs(); host.OnBegin(this, args); if (args.Continue == ContinueType.SkipAll) return; if (args.Continue == ContinueType.Error) { strError = args.ParamString; goto ERROR1; } } List<ListViewItem> items = new List<ListViewItem>(); foreach (ListViewItem item in this.listView_records.SelectedItems) { if (string.IsNullOrEmpty(item.Text) == true) continue; items.Add(item); } bool bOldSource = true; // 是否要从 OldXml 开始做起 int nChangeCount = this.GetItemsChangeCount(items); if (nChangeCount > 0) { bool bHideMessageBox = true; DialogResult result = MessageDialog.Show(this, "当前选定的 " + items.Count.ToString() + " 个事项中有 " + nChangeCount + " 项修改尚未保存。\r\n\r\n请问如何进行修改? \r\n\r\n(重新修改) 重新进行修改,忽略以前内存中的修改; \r\n(继续修改) 以上次的修改为基础继续修改; \r\n(放弃) 放弃整个操作", MessageBoxButtons.YesNoCancel, MessageBoxDefaultButton.Button1, null, ref bHideMessageBox, new string[] { "重新修改", "继续修改", "放弃" }); if (result == DialogResult.Cancel) return; if (result == DialogResult.No) { bOldSource = false; } } ListViewPatronLoader loader = new ListViewPatronLoader(this.Channel, stop, items, this.m_biblioTable); loader.DbTypeCaption = this.DbTypeCaption; int i = 0; foreach (LoaderItem item in loader) { Application.DoEvents(); // 出让界面控制权 if (stop != null && stop.State != 0) { strError = "用户中断"; goto ERROR1; } stop.SetProgressValue(i); BiblioInfo info = item.BiblioInfo; this.MainForm.OperHistory.AppendHtml("<div class='debug recpath'>" + HttpUtility.HtmlEncode(info.RecPath) + "</div>"); host.MainForm = this.MainForm; host.DbType = this.DbType; host.RecordPath = info.RecPath; host.ItemDom = new XmlDocument(); if (bOldSource == true) { host.ItemDom.LoadXml(info.OldXml); // 放弃上一次的修改 if (string.IsNullOrEmpty(info.NewXml) == false) { info.NewXml = ""; this.m_nChangedCount--; } } else { if (string.IsNullOrEmpty(info.NewXml) == false) host.ItemDom.LoadXml(info.NewXml); else host.ItemDom.LoadXml(info.OldXml); } // host.ItemDom.LoadXml(info.OldXml); host.Changed = false; host.UiItem = item.ListViewItem; StatisEventArgs args = new StatisEventArgs(); host.OnRecord(this, args); if (args.Continue == ContinueType.SkipAll) break; if (args.Continue == ContinueType.Error) { strError = args.ParamString; goto ERROR1; } if (host.Changed == true) { string strXml = host.ItemDom.OuterXml; if (info != null) { if (string.IsNullOrEmpty(info.NewXml) == true) this.m_nChangedCount++; info.NewXml = strXml; } item.ListViewItem.BackColor = SystemColors.Info; item.ListViewItem.ForeColor = SystemColors.InfoText; } // 显示为工作单形式 i++; } { host.MainForm = this.MainForm; host.DbType = this.DbType; host.RecordPath = ""; host.ItemDom = null; host.Changed = false; host.UiItem = null; StatisEventArgs args = new StatisEventArgs(); host.OnEnd(this, args); if (args.Continue == ContinueType.Error) { strError = args.ParamString; goto ERROR1; } } } catch (Exception ex) { strError = "执行 MarcQuery 脚本的过程中出现异常: " + ExceptionUtil.GetDebugText(ex); goto ERROR1; } finally { if (host != null) host.FreeResources(); this.listView_records.Enabled = true; stop.EndLoop(); stop.OnStop -= new StopEventHandler(this.DoStop); stop.Initial(""); stop.HideProgress(); stop.Style = StopStyle.None; this.EnableControls(true); this.MainForm.OperHistory.AppendHtml("<div class='debug end'>" + HttpUtility.HtmlEncode(DateTime.Now.ToLongTimeString()) + " 结束执行脚本 " + dlg.FileName + "</div>"); } DoViewComment(false); return; ERROR1: MessageBox.Show(this, strError); }
// 快速修改记录 // return: // -1 呼错 // 0 放弃 // 1 成功 internal int QuickChangeItemRecords(out string strError) { strError = ""; int nRet = 0; if (this._listviewRecords.SelectedItems.Count == 0) { strError = "尚未选择要快速修改的" + this.DbTypeCaption + "记录事项"; return -1; } List<OneAction> actions = null; XmlDocument cfg_dom = null; if (this.DbType == "item" || this.DbType == "order" || this.DbType == "issue" || this.DbType == "comment" || this.DbType == "patron") { ChangeItemActionDialog dlg = new ChangeItemActionDialog(); MainForm.SetControlFont(dlg, this.Font, false); dlg.DbType = this.DbType; dlg.Text = "快速修改" + this.DbTypeCaption + "记录 -- 请指定动作参数"; dlg.MainForm = this.MainForm; dlg.GetValueTable -= new GetValueTableEventHandler(dlg_GetValueTable); dlg.GetValueTable += new GetValueTableEventHandler(dlg_GetValueTable); this.MainForm.AppInfo.LinkFormState(dlg, this.DbType + "searchform_quickchangedialog_state"); dlg.ShowDialog(this); this.MainForm.AppInfo.UnlinkFormState(dlg); if (dlg.DialogResult == System.Windows.Forms.DialogResult.Cancel) return 0; // 放弃 actions = dlg.Actions; cfg_dom = dlg.CfgDom; } DateTime now = DateTime.Now; // TODO: 检查一下,看看是否一项修改动作都没有 this.MainForm.OperHistory.AppendHtml("<div class='debug begin'>" + HttpUtility.HtmlEncode(DateTime.Now.ToLongTimeString()) + " 开始执行快速修改" + this.DbTypeCaption + "记录</div>"); stop.Style = StopStyle.EnableHalfStop; stop.OnStop += new StopEventHandler(this.DoStop); stop.Initial("快速修改" + this.DbTypeCaption + "记录 ..."); stop.BeginLoop(); EnableControls(false); this._listviewRecords.Enabled = false; int nProcessCount = 0; int nChangedCount = 0; try { stop.SetProgressRange(0, this._listviewRecords.SelectedItems.Count); List<ListViewItem> items = new List<ListViewItem>(); foreach (ListViewItem item in this._listviewRecords.SelectedItems) { if (string.IsNullOrEmpty(item.Text) == true) continue; items.Add(item); } bool bOldSource = true; // 是否要从 OldXml 开始做起 int nChangeCount = this.GetItemsChangeCount(items); if (nChangeCount > 0) { bool bHideMessageBox = true; DialogResult result = MessageDialog.Show(this, "当前选定的 " + items.Count.ToString() + " 个事项中有 " + nChangeCount + " 项修改尚未保存。\r\n\r\n请问如何进行修改? \r\n\r\n(重新修改) 重新进行修改,忽略以前内存中的修改; \r\n(继续修改) 以上次的修改为基础继续修改; \r\n(放弃) 放弃整个操作", MessageBoxButtons.YesNoCancel, MessageBoxDefaultButton.Button1, null, ref bHideMessageBox, new string [] {"重新修改","继续修改","放弃"}); if (result == DialogResult.Cancel) { strError = "放弃"; return 0; } if (result == DialogResult.No) { bOldSource = false; } } ListViewPatronLoader loader = new ListViewPatronLoader(this.Channel, stop, items, this.m_biblioTable); loader.DbTypeCaption = this.DbTypeCaption; int i = 0; foreach (LoaderItem item in loader) { Application.DoEvents(); // 出让界面控制权 if (stop != null && stop.State != 0) { strError = "用户中断"; return -1; } stop.SetProgressValue(i); BiblioInfo info = item.BiblioInfo; Debug.Assert(info != null, ""); this.MainForm.OperHistory.AppendHtml("<div class='debug recpath'>" + HttpUtility.HtmlEncode(info.RecPath) + "</div>"); XmlDocument dom = new XmlDocument(); try { if (bOldSource == true) { dom.LoadXml(info.OldXml); // 放弃上一次的修改 if (string.IsNullOrEmpty(info.NewXml) == false) { info.NewXml = ""; this.m_nChangedCount--; } } else { if (string.IsNullOrEmpty(info.NewXml) == false) dom.LoadXml(info.NewXml); else dom.LoadXml(info.OldXml); } } catch (Exception ex) { strError = "装载XML到DOM时发生错误: " + ex.Message; return -1; } string strDebugInfo = ""; // 修改一个订购记录 XmlDocument // return: // -1 出错 // 0 没有实质性修改 // 1 发生了修改 nRet = ModifyItemRecord( cfg_dom, actions, ref dom, now, out strDebugInfo, out strError); if (nRet == -1) return -1; this.MainForm.OperHistory.AppendHtml("<div class='debug normal'>" + HttpUtility.HtmlEncode(strDebugInfo).Replace("\r\n", "<br/>") + "</div>"); nProcessCount++; if (nRet == 1) { string strXml = dom.OuterXml; Debug.Assert(info != null, ""); if (info != null) { if (string.IsNullOrEmpty(info.NewXml) == true) this.m_nChangedCount++; info.NewXml = strXml; } item.ListViewItem.BackColor = SystemColors.Info; item.ListViewItem.ForeColor = SystemColors.InfoText; } i++; nChangedCount++; } } finally { EnableControls(true); this._listviewRecords.Enabled = true; stop.EndLoop(); stop.OnStop -= new StopEventHandler(this.DoStop); stop.Initial(""); stop.HideProgress(); stop.Style = StopStyle.None; this.MainForm.OperHistory.AppendHtml("<div class='debug end'>" + HttpUtility.HtmlEncode(DateTime.Now.ToLongTimeString()) + " 结束快速修改" + this.DbTypeCaption + "记录</div>"); } DoViewComment(false); strError = "修改" + this.DbTypeCaption + "记录 " + nChangedCount.ToString() + " 条 (共处理 " + nProcessCount.ToString() + " 条)\r\n\r\n(注意修改并未自动保存。请在观察确认后,使用保存命令将修改保存回" + this.DbTypeCaption + "库)"; return 1; }
// 删除所选择的记录 void menu_deleteSelectedRecords_Click(object sender, EventArgs e) { DialogResult result = MessageBox.Show(this, "确实要从数据库中删除所选定的 " + this.listView_records.SelectedItems.Count.ToString() + " 个" + this.DbTypeCaption + "记录?\r\n\r\n(OK 删除;Cancel 取消)", "BiblioSearchForm", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); if (result == System.Windows.Forms.DialogResult.Cancel) return; List<ListViewItem> items = new List<ListViewItem>(); foreach (ListViewItem item in this.listView_records.SelectedItems) { items.Add(item); } string strError = ""; stop.Style = StopStyle.EnableHalfStop; stop.OnStop += new StopEventHandler(this.DoStop); stop.Initial("正在删除" + this.DbTypeCaption + "记录 ..."); stop.BeginLoop(); this.EnableControls(false); this.listView_records.Enabled = false; try { stop.SetProgressRange(0, items.Count); ListViewPatronLoader loader = new ListViewPatronLoader(this.Channel, stop, items, this.m_biblioTable); loader.DbTypeCaption = this.DbTypeCaption; int i = 0; foreach (LoaderItem item in loader) { Application.DoEvents(); // 出让界面控制权 if (stop != null && stop.State != 0) { strError = "用户中断"; goto ERROR1; } stop.SetProgressValue(i); BiblioInfo info = item.BiblioInfo; Debug.Assert(item.ListViewItem == items[i], ""); //string strRecPath = ListViewUtil.GetItemText(item, 0); EntityInfo entity = new EntityInfo(); EntityInfo[] entities = new EntityInfo[1]; entities[0] = entity; entity.Action = "delete"; entity.OldRecPath = info.RecPath; entity.NewRecord = ""; entity.NewTimestamp = null; entity.OldRecord = info.OldXml; entity.OldTimestamp = info.Timestamp; #if NO entity.RefID = ""; if (String.IsNullOrEmpty(entity.RefID) == true) entity.RefID = BookItem.GenRefID(); #endif stop.SetMessage("正在删除" + this.DbTypeCaption + "记录 " + info.RecPath); string strBiblioRecPath = ""; EntityInfo[] errorinfos = null; long lRet = 0; if (this.DbType == "item") { lRet = Channel.SetEntities( stop, strBiblioRecPath, entities, out errorinfos, out strError); } else if (this.DbType == "order") { lRet = Channel.SetOrders( stop, strBiblioRecPath, entities, out errorinfos, out strError); } else if (this.DbType == "issue") { lRet = Channel.SetIssues( stop, strBiblioRecPath, entities, out errorinfos, out strError); } else if (this.DbType == "comment") { lRet = Channel.SetComments( stop, strBiblioRecPath, entities, out errorinfos, out strError); } else { strError = "未知的事项类型 '" + this.DbType + "'"; goto ERROR1; } if (lRet == -1) goto ERROR1; if (errorinfos != null) { foreach (EntityInfo error in errorinfos) { if (error.ErrorCode != ErrorCodeValue.NoError) strError += error.ErrorInfo; goto ERROR1; } } stop.SetProgressValue(i); this.listView_records.Items.Remove(item.ListViewItem); i++; } } finally { stop.EndLoop(); stop.OnStop -= new StopEventHandler(this.DoStop); stop.Initial(""); stop.HideProgress(); stop.Style = StopStyle.None; this.EnableControls(true); this.listView_records.Enabled = true; } MessageBox.Show(this, "成功删除" + this.DbTypeCaption + "记录 " + items.Count + " 条"); return; ERROR1: MessageBox.Show(this, strError); }
// 快速修改记录 void menu_quickChangeItemRecords_Click(object sender, EventArgs e) { string strError = ""; int nRet = 0; // bool bSkipUpdateBrowse = false; // 是否要跳过更新浏览行 if (this.listView_records.SelectedItems.Count == 0) { strError = "尚未选择要快速修改的"+this.DbTypeCaption+"记录事项"; goto ERROR1; } List<OneAction> actions = null; XmlDocument cfg_dom = null; if (this.DbType == "item" || this.DbType == "order" || this.DbType == "issue" || this.DbType == "comment") { ChangeItemActionDialog dlg = new ChangeItemActionDialog(); MainForm.SetControlFont(dlg, this.Font, false); dlg.DbType = this.DbType; dlg.Text = "快速修改" + this.DbTypeCaption + "记录 -- 请指定动作参数"; dlg.MainForm = this.MainForm; dlg.GetValueTable -= new GetValueTableEventHandler(dlg_GetValueTable); dlg.GetValueTable += new GetValueTableEventHandler(dlg_GetValueTable); this.MainForm.AppInfo.LinkFormState(dlg, "itemsearchform_quickchange"+this.DbType+"dialog_state"); dlg.ShowDialog(this); this.MainForm.AppInfo.UnlinkFormState(dlg); if (dlg.DialogResult == System.Windows.Forms.DialogResult.Cancel) return; actions = dlg.Actions; cfg_dom = dlg.CfgDom; } DateTime now = DateTime.Now; // TODO: 检查一下,看看是否一项修改动作都没有 this.MainForm.OperHistory.AppendHtml("<div class='debug begin'>" + HttpUtility.HtmlEncode(DateTime.Now.ToLongTimeString()) + " 开始执行快速修改" + this.DbTypeCaption + "记录</div>"); stop.Style = StopStyle.EnableHalfStop; stop.OnStop += new StopEventHandler(this.DoStop); stop.Initial("快速修改" + this.DbTypeCaption + "记录 ..."); stop.BeginLoop(); EnableControls(false); this.listView_records.Enabled = false; int nProcessCount = 0; int nChangedCount = 0; try { stop.SetProgressRange(0, this.listView_records.SelectedItems.Count); List<ListViewItem> items = new List<ListViewItem>(); foreach (ListViewItem item in this.listView_records.SelectedItems) { if (string.IsNullOrEmpty(item.Text) == true) continue; items.Add(item); } ListViewPatronLoader loader = new ListViewPatronLoader(this.Channel, stop, items, this.m_biblioTable); int i = 0; foreach (LoaderItem item in loader) { Application.DoEvents(); // 出让界面控制权 if (stop != null && stop.State != 0) { strError = "用户中断"; goto ERROR1; } stop.SetProgressValue(i); BiblioInfo info = item.BiblioInfo; this.MainForm.OperHistory.AppendHtml("<div class='debug recpath'>" + HttpUtility.HtmlEncode(info.RecPath) + "</div>"); XmlDocument dom = new XmlDocument(); try { dom.LoadXml(info.OldXml); } catch (Exception ex) { strError = "装载XML到DOM时发生错误: " + ex.Message; goto ERROR1; } string strDebugInfo = ""; if (this.DbType == "item" || this.DbType == "order" || this.DbType == "issue" || this.DbType == "comment") { // 修改一个订购记录 XmlDocument // return: // -1 出错 // 0 没有实质性修改 // 1 发生了修改 nRet = ModifyOrderRecord( cfg_dom, actions, ref dom, now, out strDebugInfo, out strError); if (nRet == -1) goto ERROR1; } this.MainForm.OperHistory.AppendHtml("<div class='debug normal'>" + HttpUtility.HtmlEncode(strDebugInfo).Replace("\r\n", "<br/>") + "</div>"); nProcessCount++; if (nRet == 1) { string strXml = dom.OuterXml; if (info != null) { if (string.IsNullOrEmpty(info.NewXml) == true) this.m_nChangedCount++; info.NewXml = strXml; } item.ListViewItem.BackColor = SystemColors.Info; item.ListViewItem.ForeColor = SystemColors.InfoText; } i++; nChangedCount++; } } finally { EnableControls(true); this.listView_records.Enabled = true; stop.EndLoop(); stop.OnStop -= new StopEventHandler(this.DoStop); stop.Initial(""); stop.HideProgress(); stop.Style = StopStyle.None; this.MainForm.OperHistory.AppendHtml("<div class='debug end'>" + HttpUtility.HtmlEncode(DateTime.Now.ToLongTimeString()) + " 结束快速修改" + this.DbTypeCaption + "记录</div>"); } DoViewComment(false); MessageBox.Show(this, "修改" + this.DbTypeCaption + "记录 " + nChangedCount.ToString() + " 条 (共处理 " + nProcessCount.ToString() + " 条)\r\n\r\n(注意修改并未自动保存。请在观察确认后,使用保存命令将修改保存回" + this.DbTypeCaption + "库)"); return; ERROR1: MessageBox.Show(this, strError); }
int VerifyItemRecord(out string strError) { strError = ""; int nRet = 0; Debug.Assert(this.DbType == "item", ""); if (this.listView_records.SelectedItems.Count == 0) { strError = "尚未选定要进行批处理的事项"; return -1; } if (stop != null && stop.State == 0) // 0 表示正在处理 { strError = "目前有长操作正在进行,无法进行校验册记录的操作"; return -1; } // 切换到“操作历史”属性页 this.MainForm.ActivateFixPage("history"); int nCount = 0; this.MainForm.OperHistory.AppendHtml("<div class='debug begin'>" + HttpUtility.HtmlEncode(DateTime.Now.ToLongTimeString()) + " 开始进行册记录校验</div>"); stop.Style = StopStyle.EnableHalfStop; stop.OnStop += new StopEventHandler(this.DoStop); stop.Initial("正在进行校验册记录的操作 ..."); stop.BeginLoop(); this.EnableControls(false); try { stop.SetProgressRange(0, this.listView_records.SelectedItems.Count); List<ListViewItem> items = new List<ListViewItem>(); foreach (ListViewItem item in this.listView_records.SelectedItems) { if (string.IsNullOrEmpty(item.Text) == true) continue; items.Add(item); } ListViewPatronLoader loader = new ListViewPatronLoader(this.Channel, stop, items, this.m_biblioTable); loader.DbTypeCaption = this.DbTypeCaption; int i = 0; foreach (LoaderItem item in loader) { Application.DoEvents(); // 出让界面控制权 if (stop != null && stop.State != 0) { strError = "用户中断"; return -1; } BiblioInfo info = item.BiblioInfo; XmlDocument itemdom = new XmlDocument(); try { itemdom.LoadXml(info.OldXml); } catch (Exception ex) { strError = "记录 '" + info.RecPath + "' 的 XML 装入 DOM 时出错: " + ex.Message; return -1; } List<string> errors = new List<string>(); // 检查根元素下的元素名是否有重复的 nRet = VerifyDupElementName(itemdom, out strError); if (nRet == -1) errors.Add(strError); // 校验 XML 记录中是否有非法字符 string strReplaced = DomUtil.ReplaceControlCharsButCrLf(info.OldXml, '*'); if (strReplaced != info.OldXml) { errors.Add("XML 记录中有非法字符"); } // 校验借书时间字符串是否合法 string borrowDate = DomUtil.GetElementText(itemdom.DocumentElement, "borrowDate"); if (string.IsNullOrEmpty(borrowDate) == false) { try { DateTime time = DateTimeUtil.FromRfc1123DateTimeString(borrowDate).ToLocalTime(); if (time > DateTime.Now) { errors.Add("借书时间 '" + time.ToString() + "' 比当前时间还靠后"); } } catch (Exception ex) { errors.Add("borrow 元素的 borrowDate 属性值 '" + borrowDate + "' 不合法: " + ex.Message); } } string strBarcode = DomUtil.GetElementText(itemdom.DocumentElement, "barcode"); if (string.IsNullOrEmpty(strBarcode) == false) { string strLocation = DomUtil.GetElementText(itemdom.DocumentElement, "location"); strLocation = StringUtil.GetPureLocationString(strLocation); string strLibraryCode = ""; string strRoom = ""; // 解析 Global.ParseCalendarName(strLocation, out strLibraryCode, out strRoom); // <para>-2 服务器没有配置校验方法,无法校验</para> // <para>-1 出错</para> // <para>0 不是合法的条码号</para> // <para>1 是合法的读者证条码号</para> // <para>2 是合法的册条码号</para> nRet = this.MainForm.VerifyBarcode( this.stop, this.Channel, strLibraryCode, strBarcode, null, out strError); if (nRet == -2) return -1; if (nRet != 2) { if (nRet == 1 && string.IsNullOrEmpty(strError) == true) strError = strLibraryCode + ": 这看起来是一个证条码号"; errors.Add("册条码号 '" + strBarcode + "' 不合法: " + strError); } } // 模拟删除一些元素 nRet = SimulateDeleteElement(itemdom, out strError); if (nRet == -1) errors.Add(strError); if (errors.Count > 0) { this.MainForm.OperHistory.AppendHtml("<div class='debug recpath'>" + HttpUtility.HtmlEncode(info.RecPath) + "</div>"); foreach (string error in errors) { this.MainForm.OperHistory.AppendHtml("<div class='debug error'>" + HttpUtility.HtmlEncode(error) + "</div>"); } { item.ListViewItem.BackColor = Color.FromArgb(155, 0, 0); item.ListViewItem.ForeColor = Color.FromArgb(255, 255, 255); } } nCount++; stop.SetProgressValue(++i); } return nCount; } finally { stop.EndLoop(); stop.OnStop -= new StopEventHandler(this.DoStop); stop.Initial(""); stop.HideProgress(); stop.Style = StopStyle.None; this.EnableControls(true); this.MainForm.OperHistory.AppendHtml("<div class='debug end'>" + HttpUtility.HtmlEncode(DateTime.Now.ToLongTimeString()) + " 结束执行册记录校验</div>"); } }
void menu_deleteSelectedInventoryItems_Click(object sender, EventArgs e) { DialogResult result = MessageBox.Show(this, "确实要从盘点库中删除所选定的 " + this.listView_inventoryList_records.SelectedItems.Count.ToString() + " 个记录?", "InventoryForm", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); if (result == System.Windows.Forms.DialogResult.Cancel) return; List<ListViewItem> items = new List<ListViewItem>(); foreach (ListViewItem item in this.listView_inventoryList_records.SelectedItems) { items.Add(item); } string strError = ""; stop.Style = StopStyle.EnableHalfStop; stop.OnStop += new StopEventHandler(this.DoStop); stop.Initial("正在删除盘点记录 ..."); stop.BeginLoop(); this.EnableControls(false); this.listView_inventoryList_records.Enabled = false; try { stop.SetProgressRange(0, items.Count); ListViewPatronLoader loader = new ListViewPatronLoader(this.Channel, stop, items, this.m_biblioTable); loader.DbTypeCaption = "盘点"; int i = 0; foreach (LoaderItem item in loader) { Application.DoEvents(); // 出让界面控制权 if (stop != null && stop.State != 0) { strError = "用户中断"; goto ERROR1; } stop.SetProgressValue(i); BiblioInfo info = item.BiblioInfo; Debug.Assert(item.ListViewItem == items[i], ""); //string strRecPath = ListViewUtil.GetItemText(item, 0); #if NO entity.RefID = ""; if (String.IsNullOrEmpty(entity.RefID) == true) entity.RefID = BookItem.GenRefID(); #endif stop.SetMessage("正在删除盘点记录 " + info.RecPath); long lRet = 0; string strOutputResPath = ""; byte[] output_timestamp = null; lRet = Channel.WriteRes( stop, info.RecPath, "", 0, null, "", "delete", info.Timestamp, out strOutputResPath, out output_timestamp, out strError); if (lRet == -1) goto ERROR1; stop.SetProgressValue(i); this.listView_inventoryList_records.Items.Remove(item.ListViewItem); i++; } } finally { stop.EndLoop(); stop.OnStop -= new StopEventHandler(this.DoStop); stop.Initial(""); stop.HideProgress(); stop.Style = StopStyle.None; this.EnableControls(true); this.listView_inventoryList_records.Enabled = true; } MessageBox.Show(this, "成功删除盘点记录 " + items.Count + " 条"); return; ERROR1: MessageBox.Show(this, strError); }
// 修改册记录的状态 // 将本次盘点验证存在的册,去除 注销/丢失 状态;或将没有盘点到的册,打上 注销 标记 // parameters: // strAction 动作。remove/add 之一 // return: // -1 出错 // 0 放弃操作 // 1 操作成功 int ModifyState( string strAction, out string strError) { int nRet = 0; if (strAction != "remove" && strAction != "add") { strError = "未知的 strAction 值 '"+strAction+"'"; return -1; } // TODO: 这几个栏目,作为必备栏目,应该在窗口打开的早期进行验证是否具备,如果不具备要及时警告 // 在参考手册中也要说明哪些栏目是必备的,必须配置在 inventory_item_borrow.xml 配置文件中 int nStateColumnIndex = this._defs._base_colmun_defs.FindColumnByType("item_state"); if (nStateColumnIndex == -1) { strError = "基准集列表中 type 为 item_state 的列没有定义 ..."; return -1; } int nBorrowerColumnIndex = this._defs._base_colmun_defs.FindColumnByType("borrower"); if (nBorrowerColumnIndex == -1) { strError = "基准集列表中 type 为 borrower 的列没有定义 ..."; return -1; } List<ListViewItem> items = new List<ListViewItem>(); foreach (ListViewItem item in this.listView_baseList_records.Items) { if (item.Tag == null) continue; string strState = ListViewUtil.GetItemText(item, nStateColumnIndex + 2); if (string.IsNullOrEmpty(strState) == false && (strState[0] == '+' || strState[0] == '-')) strState = strState.Substring(1); string strBorrower = ListViewUtil.GetItemText(item, nBorrowerColumnIndex + 2); LineType type = (LineType)item.Tag; if (strAction == "remove" && type == LineType.Verified) { if ((StringUtil.IsInList("丢失", strState) == true || StringUtil.IsInList("注销", strState) == true) && string.IsNullOrEmpty(strBorrower) == true) { items.Add(item); } } if (strAction == "add" && type == LineType.Lost) { if (StringUtil.IsInList("丢失", strState) == false && StringUtil.IsInList("注销", strState) == false && string.IsNullOrEmpty(strBorrower) == true) { items.Add(item); } } } if (items.Count == 0) { strError = "没有需要操作的事项"; return 1; } { string strText = ""; if (strAction == "remove") strText = "盘点验证过的册中,有 " + items.Count.ToString() + " 册当前是 “丢失” 或 “注销” 状态。\r\n\r\n是否要去掉这些册的“丢失”或“注销”状态?"; if (strAction == "add") strText = "经盘点推断新丢失 " + items.Count.ToString() + " 册。\r\n\r\n是否要给这些册加上“注销”状态?"; DialogResult result = MessageBox.Show(this, strText, "InventoryForm", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1); if (result == System.Windows.Forms.DialogResult.No) { strError = "放弃操作"; return 0; } } EnableControls(false); stop.Style = StopStyle.EnableHalfStop; stop.OnStop += new StopEventHandler(this.Channel.DoStop); stop.Initial("正在修改册记录状态 ..."); stop.BeginLoop(); try { stop.SetProgressRange(0, items.Count); ListViewPatronLoader loader = new ListViewPatronLoader(this.Channel, stop, items, this.m_biblioTable); loader.DbTypeCaption = "实体库"; List<ListViewItem> changed_items = new List<ListViewItem>(); int i = 0; foreach (LoaderItem item in loader) { Application.DoEvents(); // 出让界面控制权 if (stop != null && stop.State != 0) { strError = "用户中断"; return -1; } stop.SetProgressValue(i); BiblioInfo info = item.BiblioInfo; Debug.Assert(item.ListViewItem == items[i], ""); string strXml = info.NewXml; if (string.IsNullOrEmpty(strXml) == true) strXml = info.OldXml; XmlDocument item_dom = new XmlDocument(); try { item_dom.LoadXml(strXml); } catch (Exception ex) { strError = "册记录 '" + info.OldXml + "' XML 装入 XMLDOM 时出错: " + ex.Message; return -1; } string strState = DomUtil.GetElementText(item_dom.DocumentElement, "state"); if (strAction == "remove") { bool bChanged = RemoveStyle(ref strState, "丢失,注销"); if (bChanged == false) goto CONTINUE; DomUtil.SetElementText(item_dom.DocumentElement, "state", strState); } if (strAction == "add") { StringUtil.SetInList(ref strState, "注销", true); DomUtil.SetElementText(item_dom.DocumentElement, "state", strState); } string strComment = DomUtil.GetElementText(item_dom.DocumentElement, "comment"); if (strAction == "remove") { strComment = AppendComment(strComment, DateTime.Now.ToString() + " 去除丢失或注销状态。"); } if (strAction == "add") { strComment = AppendComment(strComment, DateTime.Now.ToString() + " 添加注销状态。"); } DomUtil.SetElementText(item_dom.DocumentElement, "comment", strComment); info.NewXml = item_dom.DocumentElement.OuterXml; byte[] baNewTimestamp = null; // 保存一条记录 // 保存成功后, info.Timestamp 会被更新 // return: // -2 时间戳不匹配 // -1 出错 // 0 成功 nRet = SaveItemRecord(info.RecPath, info, out baNewTimestamp, out strError); if (nRet == -1) return -1; if (nRet == -2) { // TODO: 时间戳不匹配。警告重做? return -1; } info.Timestamp = baNewTimestamp; this.m_nChangedCount ++; AcceptOneChange(item.ListViewItem); // 刷新显示 changed_items.Add(item.ListViewItem); CONTINUE: i++; } { // TODO: 要保护被刷新行原先的背景颜色 string strBrowseStyle = "id,cols,format:@coldef:" + this._defs.BrowseColumnDef; // 刷新浏览行 nRet = RefreshListViewLines(changed_items, strBrowseStyle, false, false, // 不清除右侧多出来的列内容 out strError); if (nRet == -1) return -1; } } finally { stop.EndLoop(); stop.OnStop -= new StopEventHandler(this.Channel.DoStop); stop.Initial(""); stop.Style = StopStyle.None; EnableControls(true); } if (strAction == "add") this._statisInfo.ItemsNeedAddLostState = 0; else if (strAction == "remove") this._statisInfo.ItemsNeedRemoveLostState = 0; this.SetButtonState(this._statisInfo); strError = ""; return 1; }
// 通用的,遍历和处理读者记录的函数 // 该函数的弱点是不能按照特殊风格来获取读者记录,而 ProcessPatrons() 能做到 // 优点是速度特别快 // return: // -1 出错。包括用户中断的情况 // >=0 实际处理的读者记录数 public int ProcessSelectedPatrons( Delegate_processPatron func, out string strError) { strError = ""; if (this.listView_records.SelectedItems.Count == 0) { strError = "尚未选择要处理的读者记录事项"; return -1; } // 读者信息缓存 // 如果已经初始化,则保持 if (this.m_biblioTable == null) this.m_biblioTable = new Hashtable(); this.MainForm.OperHistory.AppendHtml("<div class='debug begin'>" + HttpUtility.HtmlEncode(DateTime.Now.ToLongTimeString()) + " 开始进行读者记录校验</div>"); stop.Style = StopStyle.EnableHalfStop; stop.OnStop += new StopEventHandler(this.DoStop); stop.Initial("正在处理读者记录 ..."); stop.BeginLoop(); this.EnableControls(false); this.listView_records.Enabled = false; try { if (stop != null) stop.SetProgressRange(0, this.listView_records.SelectedItems.Count); List<ListViewItem> items = new List<ListViewItem>(); foreach (ListViewItem item in this.listView_records.SelectedItems) { if (string.IsNullOrEmpty(item.Text) == true) continue; items.Add(item); } ListViewPatronLoader loader = new ListViewPatronLoader(this.Channel, stop, items, this.m_biblioTable); loader.DbTypeCaption = this.DbTypeCaption; int i = 0; foreach (LoaderItem item in loader) { Application.DoEvents(); // 出让界面控制权 if (stop != null && stop.State != 0) { strError = "用户中断"; return -1; } stop.SetProgressValue(i); BiblioInfo info = item.BiblioInfo; // this.MainForm.OperHistory.AppendHtml("<div class='debug recpath'>" + HttpUtility.HtmlEncode(info.RecPath) + "</div>"); XmlDocument dom = new XmlDocument(); try { dom.LoadXml(info.OldXml); } catch (Exception ex) { strError = "装载读者记录 XML 到 DOM 时发生错误: " + ex.Message; return -1; } if (func != null) { if (func(info.RecPath, dom, info.Timestamp) == false) break; } i++; } return i; } catch (Exception ex) { strError = "处理读者记录的过程中出现异常: " + ExceptionUtil.GetDebugText(ex); return -1; } finally { this.listView_records.Enabled = true; stop.EndLoop(); stop.OnStop -= new StopEventHandler(this.DoStop); stop.Initial(""); stop.HideProgress(); stop.Style = StopStyle.None; this.EnableControls(true); this.MainForm.OperHistory.AppendHtml("<div class='debug end'>" + HttpUtility.HtmlEncode(DateTime.Now.ToLongTimeString()) + " 结束执行读者记录处理</div>"); } }
void menu_verifyPatronRecord_Click(object sender, EventArgs e) { string strError = ""; int nRet = 0; if (this.listView_records.SelectedItems.Count == 0) { strError = "尚未选择要校验的读者记录事项"; goto ERROR1; } // 读者信息缓存 // 如果已经初始化,则保持 if (this.m_biblioTable == null) this.m_biblioTable = new Hashtable(); int nCount = 0; this.MainForm.OperHistory.AppendHtml("<div class='debug begin'>" + HttpUtility.HtmlEncode(DateTime.Now.ToLongTimeString()) + " 开始进行读者记录校验</div>"); stop.Style = StopStyle.EnableHalfStop; stop.OnStop += new StopEventHandler(this.DoStop); stop.Initial("正在校验读者记录 ..."); stop.BeginLoop(); this.EnableControls(false); this.listView_records.Enabled = false; try { if (stop != null) stop.SetProgressRange(0, this.listView_records.SelectedItems.Count); List<ListViewItem> items = new List<ListViewItem>(); foreach (ListViewItem item in this.listView_records.SelectedItems) { if (string.IsNullOrEmpty(item.Text) == true) continue; items.Add(item); } ListViewPatronLoader loader = new ListViewPatronLoader(this.Channel, stop, items, this.m_biblioTable); loader.DbTypeCaption = this.DbTypeCaption; int i = 0; foreach (LoaderItem item in loader) { Application.DoEvents(); // 出让界面控制权 if (stop != null && stop.State != 0) { strError = "用户中断"; goto ERROR1; } stop.SetProgressValue(i); BiblioInfo info = item.BiblioInfo; // this.MainForm.OperHistory.AppendHtml("<div class='debug recpath'>" + HttpUtility.HtmlEncode(info.RecPath) + "</div>"); XmlDocument dom = new XmlDocument(); try { dom.LoadXml(info.OldXml); } catch (Exception ex) { strError = "记录 '" + info.RecPath + "' 的 XML 装入 DOM 时出错: " + ex.Message; goto ERROR1; } List<string> errors = new List<string>(); // 校验 XML 记录中是否有非法字符 string strReplaced = DomUtil.ReplaceControlCharsButCrLf(info.OldXml, '*'); if (strReplaced != info.OldXml) { errors.Add("XML 记录中有非法字符"); } // 校验借书时间字符串是否合法 XmlNodeList borrows = dom.DocumentElement.SelectNodes("borrows/borrow"); foreach (XmlElement borrow in borrows) { { string borrowDate = borrow.GetAttribute("borrowDate"); if (string.IsNullOrEmpty(borrowDate)) { errors.Add("出现了 borrow 元素的 borrowDate 属性值为空的情况"); continue; } try { DateTime time = DateTimeUtil.FromRfc1123DateTimeString(borrowDate).ToLocalTime(); if (time > DateTime.Now) { errors.Add("借书时间 '" + time.ToString() + "' 比当前时间还靠后"); } } catch (Exception ex) { errors.Add("borrow 元素的 borrowDate 属性值 '" + borrowDate + "' 不合法: " + ex.Message); } } /// { string returningDate = borrow.GetAttribute("returningDate"); if (string.IsNullOrEmpty(returningDate)) { errors.Add("出现了 borrow 元素的 returningDate 属性值为空的情况"); continue; } try { DateTime time = DateTimeUtil.FromRfc1123DateTimeString(returningDate).ToLocalTime(); if (time < DateTime.Now) { errors.Add("还书时间 '" + time.ToString() + "' 比当前时间还靠前"); } } catch (Exception ex) { errors.Add("borrow 元素的 returningDate 属性值 '" + returningDate + "' 不合法: " + ex.Message); } } } string strBarcode = DomUtil.GetElementText(dom.DocumentElement, "barcode"); if (string.IsNullOrEmpty(strBarcode) == false) { string strReaderDbName = Global.GetDbName(info.RecPath); string strLibraryCode = this.MainForm.GetReaderDbLibraryCode(strReaderDbName); // <para>-2 服务器没有配置校验方法,无法校验</para> // <para>-1 出错</para> // <para>0 不是合法的条码号</para> // <para>1 是合法的读者证条码号</para> // <para>2 是合法的册条码号</para> nRet = this.MainForm.VerifyBarcode( this.stop, this.Channel, strLibraryCode, strBarcode, null, out strError); if (nRet == -2) goto ERROR1; if (nRet != 1) { if (nRet == 2 && string.IsNullOrEmpty(strError) == true) strError = strLibraryCode + ": 这看起来是一个册条码号"; errors.Add("证条码号 '" + strBarcode + "' 不合法: " + strError); } } if (errors.Count > 0) { this.MainForm.OperHistory.AppendHtml("<div class='debug recpath'>" + HttpUtility.HtmlEncode(info.RecPath) + "</div>"); foreach (string error in errors) { this.MainForm.OperHistory.AppendHtml("<div class='debug error'>" + HttpUtility.HtmlEncode(error) + "</div>"); } { item.ListViewItem.BackColor = Color.FromArgb(155, 0, 0); item.ListViewItem.ForeColor = Color.FromArgb(255, 255, 255); } } nCount++; i++; } } catch (Exception ex) { strError = "校验读者记录的过程中出现异常: " + ExceptionUtil.GetDebugText(ex); goto ERROR1; } finally { this.listView_records.Enabled = true; stop.EndLoop(); stop.OnStop -= new StopEventHandler(this.DoStop); stop.Initial(""); stop.HideProgress(); stop.Style = StopStyle.None; this.EnableControls(true); this.MainForm.OperHistory.AppendHtml("<div class='debug end'>" + HttpUtility.HtmlEncode(DateTime.Now.ToLongTimeString()) + " 结束执行读者记录校验</div>"); } return; ERROR1: MessageBox.Show(this, strError); }