方案 '分类统计(按书目库)' 中文件 'main.cs' 编译发现错误或警告:
信息条数:1
(22,23) error CS0234 : 命名空间“DigitalPlatform”中不存在类型或命名空间名称“Marc”(是缺少程序集引用吗?)
// 分类统计(按书目库)
// 编写者:谢涛
// (C) 版权所有 数字平台(北京)软件有限责任公司
// 最后修改: 2008/12/4
// 1) 2008/12/4 把double类型改为decimal类型
using System;
using System.Collections.Generic; // List<?>
using System.Windows.Forms;
using System.IO;
using System.Text;
using System.Xml;
using System.Drawing; // Size
using dp2Circulation;
using DigitalPlatform;
using DigitalPlatform.Xml;
using DigitalPlatform.dp2.Statis;
using DigitalPlatform.Text;
using DigitalPlatform.Marc;
public class MyStatis : BiblioStatis
{
string ErrorFilename = ""; // 错误信息文件名
StreamWriter sw = null;
long m_lErrorCount = 0;
Table table = new Table(3); // 种数 册数 价格
string strLocationList = ""; // 要统计的馆藏地点列表
List<string> classes = new List<string>(); // 类目表
public override void FreeResources()
{
if (this.sw != null)
{
this.sw.Close();
this.sw = null;
}
}
public override void OnBegin(object sender, StatisEventArgs e)
{
this.ClearConsoleForPureTextOutputing();
// 获得输入参数
HtmlInputDialog window = new HtmlInputDialog();
window.Text = "分类统计(按书目库) -- 指定参数";
window.Url = this.ProjectDir + "\\input.html";
window.Size = new Size(700,500);
window.ShowDialog();
if (window.DialogResult != DialogResult.OK)
{
e.Continue = ContinueType.SkipAll;
return;
}
/*
strLocationList = InputDlg.GetInput(
this.BiblioStatisForm,
"请指定要统计的馆藏地点代码",
"馆藏地点代码(可以输入多个,用半角逗号分隔。空表示忽略(全统计)):",
"");
*/
string strError = "";
if (window.SubmitUrl == "action://ok/")
{
this.strLocationList = window.SubmitResult["LocationList"];
// MessageBox.Show(this.BiblioStatisForm, "locationlist:" + this.strLocationList);
}
else
{
e.Continue = ContinueType.SkipAll;
return;
}
this.ErrorFilename = this.ProjectDir + "\\~error.txt";
sw = new StreamWriter(this.ErrorFilename,
false, // append
Encoding.UTF8);
sw.Write("<pre>\r\n");
int nRet = LoadClassTable(out strError);
if (nRet == -1)
goto ERROR1;
return;
ERROR1:
MessageBox.Show(this.BiblioStatisForm, strError);
}
// 装载类目表到内存
int LoadClassTable(out string strError)
{
strError = "";
try
{
string strClassFilename = this.ProjectDir + "\\class.txt";
StreamReader sr = new StreamReader(strClassFilename, Encoding.GetEncoding(936));
for(;;)
{
string strLine = sr.ReadLine();
if (strLine == null)
break;
strLine = strLine.Trim();
if (String.IsNullOrEmpty(strLine) == true)
continue;
this.classes.Add(strLine);
// 在表格中占据位置
table.SetValue(strLine, 0, null);
}
sr.Close();
}
catch (Exception ex)
{
strError = ex.Message;
return -1;
}
return 0;
}
// 在类目表中进行查找,获得匹配的事项。
// 可以返回多项
List<string> GetClassHead(string strClassText)
{
List<string> results = new List<string>();
for(int i=0;i<this.classes.Count;i++)
{
string strEntry = this.classes[i];
if (strClassText.Length < strEntry.Length)
continue;
string strHead = strClassText.Substring(0, strEntry.Length);
if (strHead == strEntry)
{
results.Add(strEntry);
}
}
return results;
}
// 从MARC数据中获得种的价格
string GetBiblioPrice()
{
string strPrice = "";
if (this.CurrentDbSyntax == "unimarc")
strPrice = MarcUtil.GetFirstSubfield(this.MarcRecord, "010", "d");
else if (this.CurrentDbSyntax == "usmarc")
strPrice = MarcUtil.GetFirstSubfield(this.MarcRecord, "020", "c");
else
return null;
return strPrice;
}
public override void OnRecord(object sender, StatisEventArgs e)
{
List<ItemInfo> infos = null;
try {
infos = this.ItemInfos;
}
catch (Exception ex)
{
this.m_lErrorCount ++;
sw.Write("书目记录 " + this.CurrentRecPath + " 获得ItemInfos时抛出异常: "+ex.Message+"\r\n");
return;
}
decimal totalPrice = 0;
int nItems = infos.Count;
for(int i = 0;i<infos.Count;i++)
{
ItemInfo info = infos[i];
string strLocation = DomUtil.GetElementText(info.Dom.DocumentElement, "location");
// location中需要去掉包含#的部分
strLocation = CanonicalizeLocation(strLocation);
if (String.IsNullOrEmpty(strLocationList) == false)
{
if (StringUtil.IsInList(strLocation, strLocationList) == false)
{
nItems --;
continue;
}
}
string strPrice = DomUtil.GetElementText(info.Dom.DocumentElement, "price");
// WriteTextToConsole("strPrice=[" + strPrice + "]\r\n");
if (String.IsNullOrEmpty(strPrice) == true)
{
strPrice = GetBiblioPrice();
if (String.IsNullOrEmpty(strPrice) == true)
{
this.m_lErrorCount ++;
sw.Write("册记录 " + info.RecPath + " 中缺乏价格元素,并且种记录 "+this.CurrentRecPath+" 中也缺乏价格信息\r\n");
nItems --;
continue;
}
else
{
this.m_lErrorCount ++;
sw.Write("警告:册记录 " + info.RecPath + " 中缺乏价格元素,只好采用种记录 "+this.CurrentRecPath+" 中的价格信息 '"+strPrice+"'\r\n");
}
}
// 提取出纯数字
string strPurePrice = PriceUtil.GetPurePrice(strPrice);
if (String.IsNullOrEmpty(strPurePrice) == true)
{
nItems --;
continue;
}
try {
totalPrice += Convert.ToDecimal(strPurePrice);
}
catch { // 2008/4/16 new add
this.m_lErrorCount ++;
sw.Write("警告:册记录 " + info.RecPath + " 或种记录 "+this.CurrentRecPath+" 中的价格字符串 '"+strPrice+"'格式错误\r\n");
}
}
// nItems中需要过滤,仅得到要求的馆藏地点的。如果nItems==0,则该种都不该计入
if (nItems == 0)
return;
string strAccessClass = MarcUtil.GetFirstSubfield(this.MarcRecord,
"905",
"d");
if (String.IsNullOrEmpty(strAccessClass) == true)
{
this.m_lErrorCount ++;
sw.Write("种记录 "+this.CurrentRecPath+" 中缺乏排架分类号(905$d)信息\r\n");
return;
}
// WriteTextToConsole("strAccessClass=[" + strAccessClass + "]\r\n");
List<string> heads = GetClassHead(strAccessClass);
// 类号累计
for(int i=0;i<heads.Count;i++)
{
string strHead = heads[i];
// WriteTextToConsole("strHead=[" + strHead + "]\r\n");
if (String.IsNullOrEmpty(strHead) == true)
continue;
// 种数 列号0
table.IncValue(strHead, 0, 1, 1);
// 册数 列号1
table.IncValue(strHead, 1, nItems, nItems);
// 价格 列号2
table.IncValue(strHead, 2, totalPrice, totalPrice);
}
}
static string CanonicalizeLocation(string strLocation)
{
string [] parts = strLocation.Split(new char [] {','});
for(int i=0;i<parts.Length;i++)
{
string strText = parts[i].Trim();
if (String.IsNullOrEmpty(strText) == true)
continue;
if (strText[0] != '#')
return strText;
}
return "";
}
public override void OnEnd(object sender, StatisEventArgs e)
{
if (this.sw != null)
{
this.sw.Close();
this.sw = null;
}
if (table == null)
return;
table.Sort();
Report report = Report.BuildReport(table,
"类目||class,种||title,册||item,价格||price",
" ",
true);
if (report == null) // 空表格
return;
report[3].DataType = DataType.PriceDecimal;
report.SumCell -= new SumCellEventHandler(SumCell);
report.SumCell += new SumCellEventHandler(SumCell);
string strHead = "<html><head>"
+ "<meta http-equiv='Content-Type' content=\"text/html; charset=utf-8\">"
+ "<title></title>"
+ "<link rel='stylesheet' href='"+this.ProjectDir+"/style.css' type='text/css'>"
+ "</head><body>"
+ "<div class='tabletitle'>分类统计</div>";
if (String.IsNullOrEmpty(this.strLocationList) == true)
strHead += "<div class='comment'>馆藏地点:\t" + "全部</div>";
else
strHead += "<div class='comment'>馆藏地点:\t" + this.strLocationList + "</div>";
string strTail = "</body></html>";
string strHtml = strHead + report.HtmlTable(table) + strTail;
// 写入打印输出文件
string strOutputFileName = this.NewOutputFileName();
this.WriteToOutputFile(strOutputFileName,
strHtml,
Encoding.UTF8);
if (this.m_lErrorCount > 0)
{
// 错误信息复制到打印输出文件
strOutputFileName = this.NewOutputFileName();
File.Copy(this.ErrorFilename, strOutputFileName, true);
MessageBox.Show(this.BiblioStatisForm, "有 " + this.m_lErrorCount.ToString() + " 个错误信息在文件 " + this.ErrorFilename + " 中。可以在打印结果的第二页看到。" );
}
}
void SumCell(object sender, SumCellEventArgs e)
{
if (String.IsNullOrEmpty(e.Line.Entry) == true)
return;
// 忽略多于1字符的类目名
if (e.Line.Entry.Length > 1)
e.Value = 0;
}
}