DigitalPlatform / dp2

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

统计方案集锦 #788

Open renyh opened 3 years ago

renyh commented 3 years ago

更新读者出生日期和修改密码

/* 2021/1/22编写

功能描述:
设置读者的出生日期和密码
由于只有针对学生提供了出生日期,所以学生类型的会更新出生日期,教师不更新出生日期。

需提供的对照表格式:证条码   8位出生日期,中间以tab键分隔,每个读者一行。样例如下
P001    20050913
P002    20050923
P003    20060103

输出提示为:
P001密码修改为20050913,更新出生日期Mon, 12 Sep 2005 16:00:00 GMT
P002密码修改为20050923
P003密码修改为20060103
P004在提供的对照表中没有找到。
P005在提供的对照表中没有找到。
设置成功 3,出错0,未找到2

 */
using System;
using System.Windows.Forms;
using DigitalPlatform.Xml;
using DigitalPlatform.LibraryClient;
using dp2Circulation;
using System.Text;
using System.Collections;
using DigitalPlatform.IO;
using System.Collections.Generic;
using System.Xml;
using DigitalPlatform.LibraryClient.localhost;
using System.IO;

public class SetBirthdayAndPassword : ReaderStatis
{
    int _succCount = 0;
    int _errorCount = 0;
    StringBuilder sb = new StringBuilder();
    Hashtable _hashTable = new Hashtable();
    List<string> _notFoundList = new List<string>();

    public override void OnBegin(object sender, StatisEventArgs e)
    {
        this.ClearConsoleForPureTextOutputing();

        // 打开选择文件对话框
        OpenFileDialog dlg = new OpenFileDialog()
        {
            Title = "请选择导入的对照表",

            Filter = "文本文件(*.txt)|*.txt",
            RestoreDirectory = true,
        };

        // 未选择文件则
        if (dlg.ShowDialog() != DialogResult.OK)
        {
            return;
        }

        string fileName = dlg.FileName;
        if (File.Exists(fileName) == false)
        {
            MessageBox.Show(this.MainForm, "对照表文件[" + fileName + "]不存在");
            return;
        }

        // 把提供的证条码号与生日对照表加到hashtable里
        StringBuilder sb = new StringBuilder();
        int i = 0;
        using (StreamReader sr = new StreamReader(fileName, Encoding.Default))
        {
            while (!sr.EndOfStream)
            {
                //读取一行数据
                string line = sr.ReadLine().Trim();
                if (line == "")
                    continue;
                int nIndex = line.IndexOf("\t");
                string barcode = line.Substring(0, nIndex);
                string strBirthday = line.Substring(nIndex + 1);
                this._hashTable[barcode] = strBirthday;
            }
        }
    }

    public override void OnRecord(object sender, StatisEventArgs e)
    {
        string strError = "";
        long lRet = 0;

        // 证条码号
        string strBarcode = DomUtil.GetElementText(this.ReaderDom.DocumentElement, "barcode");
        if (string.IsNullOrEmpty(strBarcode))
        {
            this.WriteTextToConsole(this.CurrentRecPath + " 没有条码号\r\n");
            return;
        }

        string strBirthday = (string)this._hashTable[strBarcode];

        // 如果证条码在对照表中找不到,则跳过
        if (string.IsNullOrEmpty(strBirthday) == true)
        {
            this.WriteTextToConsole(strBarcode + "在提供的对照表中没有找到。\r\n");
            this._notFoundList.Add(strBarcode);
            return;
        }

        // 密码
        string password = strBirthday;

        LibraryChannel channel = this.ReaderStatisForm.MainForm.GetChannel();
        try
        {
            // 调修改密码接口
            lRet = channel.ChangeReaderPassword(null,
                strBarcode,
                null,
                password,
                out strError);
            if (lRet != 1)
            {
                this._errorCount++;
                this.WriteTextToConsole(strBarcode + "修改密码发生错误:" + strError + "\r\n");
                return;
            }

            string birthdayInfo = "";
            string path = this.CurrentRecPath;
            string outPath = "";
            string[] results = null;
            byte[] timestamp = null;
            lRet = channel.GetReaderInfo(null, strBarcode, "xml", out results,
                out outPath,
                out timestamp,
                out strError);
            if (results.Length > 0)
            {
                string xml = results[0];
                //this.WriteTextToConsole(xml + "\r\n");

                XmlDocument dom = new XmlDocument();
                dom.LoadXml(xml);

                // 如果读者类型为学生,则设置一下出生日期
                string type = DomUtil.GetElementText(dom.DocumentElement, "readerType");
                if (type == "学生")
                {
                    string strRfc1123Birthday = "";
                    int nRet = DateTimeUtil.Date8toRfc1123(strBirthday,
                        out strRfc1123Birthday,
                        out strError);
                    if (nRet == -1) // 如果提供的生日转换成rfc格式出错,则不处理
                    {
                        this._errorCount++;
                        this.WriteTextToConsole(strBarcode + "的生日" + strBirthday + "转成rfc格式出错:" + strError + "\r\n");
                        return;
                    }
                    DomUtil.SetElementText(dom.DocumentElement, "dateOfBirth", strRfc1123Birthday);

                    // 必须要把out参数先在外面定义,才能在函数中使用,要不脚本运行时编译报错。
                    string strExistringXml = "";
                    string strSavedXml = "";
                    string strSavedRecPath = "";
                    ErrorCodeValue errorCodeValue = ErrorCodeValue.NoError;
                    byte[] newTimestamp = null;
                    lRet = channel.SetReaderInfo(null,
                         "change",
                         outPath,
                         dom.OuterXml,
                         "",
                         timestamp,
                         out strExistringXml,
                         out strSavedXml,
                         out strSavedRecPath,
                         out newTimestamp,
                         out errorCodeValue,
                         out strError);
                    if (lRet == -1)
                    {
                        birthdayInfo = ",更新出生日期出错:" + strError ;
                    }
                    else
                    {
                        // 把更新出生日期 到时一起输入信息。
                        birthdayInfo = ",更新出生日期" + strRfc1123Birthday;
                    }
                }
            }

            // 成功
            this.WriteTextToConsole(strBarcode + "密码修改为" + password + birthdayInfo + "\r\n");
            this._succCount++;
        }
        finally
        {
            this.ReaderStatisForm.MainForm.ReturnChannel(channel);
        }
    }

    public override void OnEnd(object sender, StatisEventArgs e)
    {
        string strInfo = "设置成功 " + this._succCount.ToString()
            + ",出错" + this._errorCount.ToString()
            + ",未找到" + this._notFoundList.Count.ToString();
        this.WriteTextToConsole(strInfo);
        MessageBox.Show(this.ReaderStatisForm, strInfo);
    }
}
renyh commented 3 years ago

1.“固定面板”切换到“操作历史”属性页

  MainForm.ActivateFixPage("history");

2.设置 dp2内务 状态栏显示内容

  this.MainForm.StatusBarMessage = "";

3.向 dp2内务 固定面板 操作历史 属性页输出信息


  this.MainForm.OperHistory.AppendHtml("<div>" + strError +"</div>");