public IObservable <ISBNInfoGetDataPackage> GetISBNInfo(IObservable <ISBNInfoGetDataPackage> getStream) { // 先查询一遍数据库 IObservable <ISBNInfoGetDataPackage> databaseResult = getStream .Where(isbnGet => !string.IsNullOrWhiteSpace(isbnGet.ISBN)) .Select(isbnGet => Observable.FromAsync(async() => { try { isbnGet.ISBNInfo = (await database.SelectAsync <ISBNInfo, ISBNInfo>(isbn => isbn.ISBN == isbnGet.ISBN)).FirstOrDefault(); } catch (Exception ex) { isbnGet.Exceptions.Add(ex); } return(isbnGet); }) ).Merge(); // 把查到的筛选出来打包成最终结果的一部分 IObservable <ISBNInfoGetDataPackage> getDbResult = databaseResult .Where(isbnGet => isbnGet.ISBNInfo != null); // 把查不到的拿去用API查 IObservable <ISBNInfoGetDataPackage> httpResult = httpISBNInfo.GetISBNInfo(databaseResult .Where(isbnGet => isbnGet.ISBNInfo == null)); // 把两次都查不到的筛选出来打包成最终结果的一部分 IObservable <ISBNInfoGetDataPackage> getBadResult = httpResult .Where(isbnGet => isbnGet.ISBNInfo == null); // 把API查到的筛选出来存一遍数据库, 再打包成最终结果的一部分 IObservable <ISBNInfoGetDataPackage> getHTTPResult = httpResult .Where(isbnGet => isbnGet.ISBNInfo != null) .Select(isbnGet => Observable.FromAsync(async() => { try { await database.InsertAsync(new ISBNInfo[] { isbnGet.ISBNInfo }); } catch (Exception ex) { isbnGet.Exceptions.Add(ex); } return(isbnGet); }) ).Merge(); return(Observable.Merge(getDbResult, getBadResult, getHTTPResult)); // 合并全部结果 }
public async Task <int> InStockAsync(IEnumerable <Book> books) { int rows; using IDatabaseTransactionManager transaction = database.StartTransaction(); try { rows = await database.InsertAsync(books, transaction.GetTransaction()); transaction.Commit(); } catch (Exception) { transaction.Rollback(); throw; } return(rows); }
public async Task <User> RegisterAsync(User user) { byte[] vs = await encrypt.CreateNewSaltAsync(); User registeredUser = new User { UserName = user.UserName ?? throw new NullReferenceException("用户名为空"), Salt = Convert.ToBase64String(vs), Password = await encrypt.HashEncryptAsync(user.Password ?? throw new NullReferenceException("密码为空"), vs), Name = user.Name ?? throw new NullReferenceException("名字为空"), NationalIdentificationNumber = user.NationalIdentificationNumber ?? throw new NullReferenceException("身份证为空"), CreditValue = credit.GetInitialCreditValue(), Authority = authority.GetInitialUserAuthorityValue() }; int affectedRows = await database.InsertAsync(new User[] { registeredUser }); if (affectedRows == 0) { throw new Exception("创建用户记录失败"); } return(registeredUser); }
public async Task <BorrowLog> BorrowBookAsync(User userId, Book bookId) { BorrowLog leaseLog = null; Book bCheck = (await database.SelectAsync <Book, Book>(b => b.Id == bookId.Id)).FirstOrDefault(); User uCheck = (await database.SelectAsync <User, User>(u => u.Id == userId.Id)).FirstOrDefault(); _ = (await database.SelectAsync <BorrowLog, BorrowLog>(null, pageIndex: 1, pageSize: 1)).FirstOrDefault(); if (bCheck == null || uCheck == null) { throw new Exception("借阅人或目标不合法"); } using IDatabaseTransactionManager transaction = database.StartTransaction(); try { await database.ForUpdateAsync <Book, Book>(b => b.Id == bookId.Id, transaction : transaction.GetTransaction()); Book bSnap = (await database.SelectAsync <Book, Book>(b => b.Id == bookId.Id, transaction: transaction.GetTransaction())).FirstOrDefault(); if (bSnap == null) { throw new Exception("借阅目标不合法"); } if (bSnap.Margin <= 0) { throw new Exception("借阅目标数量不足"); } User uSnap = (await database.SelectAsync <User, User>(u => u.Id == userId.Id, transaction: transaction.GetTransaction())).FirstOrDefault(); if (uSnap == null) { throw new Exception("借阅人不合法"); } List <BorrowLog> leaseLogs = await database.SelectAsync <BorrowLog, BorrowLog>(bl => bl.BookId == bookId.Id && bl.UserId == userId.Id && bl.GiveBack == null, transaction : transaction.GetTransaction()); if (leaseLogs.Count > 0) { throw new Exception("已有未归还的同名同书借阅记录"); } int accreditedDays = credit.GetAccreditedDays(uSnap); if (accreditedDays == 0) { throw new Exception("借阅人信誉过低"); } if (await database.UpdateAsync <Book, int>(book => book.Margin - 1, book => book.Id == bSnap.Id, transaction: transaction.GetTransaction()) == 0) { throw new Exception("更新书本库存失败"); } leaseLog = new BorrowLog { BookId = bSnap.Id, UserId = uSnap.Id, Deadline = DateTime.Today.AddDays(accreditedDays) }; if (await database.InsertAsync(new BorrowLog[] { leaseLog }, transaction: transaction.GetTransaction()) == 0) { throw new Exception("创建借阅记录失败"); } transaction.Commit(); } catch (Exception) { transaction.Rollback(); throw; } return(leaseLog); }