// 把读者记录保存(更新)到本地数据库 // result.Value // -1 出错 // 0 没有发生修改 // 1 发生了创建或者修改 public static NormalResult UpdateLocalPatronRecord( GetReaderInfoResult get_result, DateTime lastWriteTime) { using (BiblioCacheContext context = new BiblioCacheContext()) { if (_cacheDbCreated == false) { context.Database.EnsureCreated(); _cacheDbCreated = true; } XmlDocument dom = new XmlDocument(); try { dom.LoadXml(get_result.ReaderXml); } catch (Exception ex) { return(new NormalResult { Value = -1, ErrorInfo = $"读者记录装载进入 XMLDOM 时出错:{ex.Message}", ErrorCode = "loadXmlError" }); } /* * string pii = DomUtil.GetElementText(dom.DocumentElement, "barcode"); * if (string.IsNullOrEmpty(pii)) * pii = "@refID:" + DomUtil.GetElementText(dom.DocumentElement, "refID"); */ string pii = GetPii(dom); var patron = context.Patrons .Where(o => o.PII == pii) .FirstOrDefault(); if (patron != null) { // 如果已经存在的读者记录比打算写入的要新,则放弃写入 if (patron.LastWriteTime > lastWriteTime) { return new NormalResult { Value = 0 } } ; Set(patron, dom); context.Patrons.Update(patron); } else { patron = new PatronItem { PII = pii?.ToUpper(), }; Set(patron, dom); context.Patrons.Add(patron); } context.SaveChanges(); return(new NormalResult { Value = 1 }); } void Set(PatronItem patron, XmlDocument dom) { string cardNumber = DomUtil.GetElementText(dom.DocumentElement, "cardNumber"); cardNumber = cardNumber.ToUpper(); if (string.IsNullOrEmpty(cardNumber) == false) { cardNumber = "," + cardNumber + ","; } if (get_result.RecPath != null) { patron.RecPath = get_result.RecPath; } patron.Bindings = cardNumber; patron.Xml = get_result.ReaderXml; patron.Timestamp = get_result.Timestamp; patron.LastWriteTime = lastWriteTime; } }
// 填充读者信息的其他字段(第二阶段) async Task <NormalResult> FillPatronDetail(bool force = false) { // 已经填充过了 if (_patron.PatronName != null && force == false) { return(new NormalResult()); } string pii = _patron.PII; if (string.IsNullOrEmpty(pii)) { pii = _patron.UID; } if (string.IsNullOrEmpty(pii)) { return(new NormalResult()); } // return.Value: // -1 出错 // 0 读者记录没有找到 // 1 成功 GetReaderInfoResult result = await Task <GetReaderInfoResult> .Run(() => { return(GetReaderInfo(pii)); }); if (result.Value != 1) { string error = $"读者 '{pii}': {result.ErrorInfo}"; SetPatronError("getreaderinfo", error); return(new NormalResult { Value = -1, ErrorInfo = error }); } SetPatronError("getreaderinfo", ""); if (string.IsNullOrEmpty(_patron.State) == true) { OpenDoor(); } if (force) { _patron.PhotoPath = ""; } // string old_photopath = _patron.PhotoPath; Application.Current.Dispatcher.Invoke(new Action(() => { _patron.SetPatronXml(result.RecPath, result.ReaderXml, result.Timestamp); this.patronControl.SetBorrowed(result.ReaderXml); })); // 显示在借图书列表 List <Entity> entities = new List <Entity>(); foreach (Entity entity in this.patronControl.BorrowedEntities) { entities.Add(entity); } if (entities.Count > 0) { try { BaseChannel <IRfid> channel = RfidManager.GetChannel(); try { await FillBookFields(channel, entities, new CancellationToken()); } finally { RfidManager.ReturnChannel(channel); } } catch (Exception ex) { string error = $"填充读者信息时出现异常: {ex.Message}"; SetGlobalError("rfid", error); return(new NormalResult { Value = -1, ErrorInfo = error }); } } #if NO // 装载图象 if (old_photopath != _patron.PhotoPath) { Task.Run(() => { LoadPhoto(_patron.PhotoPath); }); } #endif return(new NormalResult()); }