DigitalPlatform / dp2

Integrated Library System / 图书馆集成系统
http://digitalplatform.github.io/dp2
Apache License 2.0
105 stars 54 forks source link

输出册记录预约信息 #1174

Open DigitalPlatform opened 1 year ago

DigitalPlatform commented 1 year ago

output_reservations.cs

// 输出当前预约队列信息到 Excel 文件
// 2023/5/24

using System;
using System.Collections;
using System.Collections.Generic;
using System.Windows.Forms;
using System.IO;
using System.Text;
using System.Xml;

using dp2Circulation;

using ClosedXML.Excel;

using DigitalPlatform;
using DigitalPlatform.IO;
using DigitalPlatform.Xml;
using DigitalPlatform.Text;

public class MyItemHost : ItemHost
{
    public override void OnInitial(object sender, StatisEventArgs e)
    {
        if (this.DbType != "item")
        {
            e.Continue = ContinueType.Error;
            e.ParamString = "本程序只能被 实体查询窗 所调用";
            return;
        }
    }

    string _fileName = "";
    XLWorkbook _doc = null;
    IXLWorksheet _sheet = null;
    int _row_index = 1;

    public override void OnBegin(object sender, StatisEventArgs e)
    {
        string strError = "";

        using (SaveFileDialog dlg = new SaveFileDialog())
        {
            // 询问文件名
            dlg.Title = "请指定要输出的 Excel 文件名";
            dlg.CreatePrompt = false;
            dlg.OverwritePrompt = true;
            dlg.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            dlg.Filter = "Excel 文件 (*.xlsx)|*.xlsx|All files (*.*)|*.*";

            dlg.RestoreDirectory = true;

            if (dlg.ShowDialog(this.MainForm) != DialogResult.OK)
            {
                e.Continue = ContinueType.SkipAll;
                return;
            }

            _fileName = dlg.FileName;
        }

        try
        {
            _doc = new XLWorkbook(XLEventTracking.Disabled);
            File.Delete(_fileName);
        }
        catch (Exception ex)
        {
            strError = ExceptionUtil.GetAutoText(ex);
            goto ERROR1;
        }

        _sheet = _doc.Worksheets.Add("预约信息");

        // 输出到 Excel 文件
        int col_index = 1;
        WriteCell(_row_index, col_index++, "册条码号");
        WriteCell(_row_index, col_index++, "书名");
        WriteCell(_row_index, col_index++, "预约情况");
        _row_index++;
        return;
    ERROR1:
        e.ParamString = strError;
        e.Continue = ContinueType.Error;
    }

    public override void OnRecord(object sender, StatisEventArgs e)
    {
        string barcode = DomUtil.GetElementText(this.ItemDom.DocumentElement, "barcode");

        string location = DomUtil.GetElementText(this.ItemDom.DocumentElement, "location");
        location = StringUtil.GetPureLocationString(location);

        string parent = DomUtil.GetElementText(this.ItemDom.DocumentElement, "parent");

        string strItemDbName = Global.GetDbName(this.RecordPath);
        string strBiblioDbName = this.MainForm.GetBiblioDbNameFromItemDbName(strItemDbName);

        string biblio_recpath = strBiblioDbName + "/" + parent;

        var item_search_form = (ItemSearchForm)this.UiForm;

        // 输出书目信息
        // return:
        //      -1  出错
        //      0   没有找到
        //      1   找到
        int nRet = item_search_form.GetTable(
            biblio_recpath,
            "title,areas",
            out string strTableXml,
            out string strError);
        if (nRet == -1)
        {
            e.Continue = ContinueType.Error;
            e.ParamString = strError;
            return;
        }

        this.OutputText($"strTableXml={strTableXml}");

        var table = GetBiblioColumns(strTableXml);

        string title = (string)table["title"];
        this.OutputText($"title={title}");

        StringBuilder reseration_info = new StringBuilder();
        var nodes = this.ItemDom.DocumentElement.SelectNodes("reservations/request");
        int i = 0;
        foreach(XmlElement request in nodes)
        {
            string reader = request.GetAttribute("reader");
            string requestDate = request.GetAttribute("requestDate");
            requestDate = DateTimeUtil.LocalTime(requestDate);
            reseration_info.AppendLine($"{i+1}) {reader} {requestDate}");
            i++;
        }

        // 输出到 Excel 文件
        int col_index = 1;
        WriteCell(_row_index, col_index++, barcode);
        WriteCell(_row_index, col_index++, title);
        WriteCell(_row_index, col_index++, reseration_info.ToString());
        _row_index++;
    }

    void WriteCell(int nRowIndex, int nColIndex, string text)
    {
        IXLCell cell = _sheet.Cell(nRowIndex, nColIndex).SetValue(text);
        cell.Style.Alignment.WrapText = true;
        cell.Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
        /*
        cell.Style.Font.FontName = strFontName;
        nColIndex++;
        */
    }

    Hashtable GetBiblioColumns(string table_xml)
    {
        Hashtable results = new Hashtable();

        XmlDocument dom = new XmlDocument();
        dom.LoadXml(table_xml);
        XmlNodeList nodes = dom.DocumentElement.SelectNodes("line");
        foreach (XmlElement line in nodes)
        {
            string strName = line.GetAttribute("name");
            string strValue = line.GetAttribute("value");
            string strType = line.GetAttribute("type");

            if (strName == "_coverImage")
                continue;

            results[strType] = strValue;
        }
        return results;
    }

    public override void OnEnd(object sender, StatisEventArgs e)
    {
        CloseDoc();

        try
        {
            System.Diagnostics.Process.Start(_fileName);
        }
        catch
        {

        }
    }

    void CloseDoc()
    {
        if (_doc != null)
        {
            _doc.SaveAs(_fileName);
            _doc.Dispose();
            _doc = null;
        }
    }

    public override void FreeResources()
    {
        CloseDoc();
        base.FreeResources();
    }
}

output_reservations.cs.ref

<?xml version="1.0" encoding="utf-8"?>
<root>
  <ref>system.data.dll</ref>
  <ref>%bindir%/ClosedXml.dll</ref>
</root>