示例#1
0
        public int GetCreditChange(BorrowLog borrowLog)
        {
            DateTime giveBackTime = borrowLog.GiveBack ?? throw new NullReferenceException("图书归还时间为空");

            if ((giveBackTime - borrowLog.CreateTime).Days < changeMinLimit)
            {
                return(0);
            }

            int differDays = (borrowLog.Deadline - giveBackTime).Days;

            int[] rewardKeys     = creditReward.Keys.ToArray();
            int[] punishmentKeys = creditPunishment.Keys.ToArray();
            int   reward         = 0;
            int   punishment     = 0;
            int   rewardIndex    = Array.BinarySearch(rewardKeys, differDays);

            rewardIndex = rewardIndex < 0 ? ~rewardIndex - 1 : rewardIndex;
            int punishmentIndex = Array.BinarySearch(punishmentKeys, -differDays);

            punishmentIndex = punishmentIndex < 0 ? ~punishmentIndex - 1 : punishmentIndex;
            if (rewardIndex >= 0)
            {
                reward = creditReward[rewardKeys[rewardIndex]];
            }

            if (punishmentIndex >= 0)
            {
                punishment = creditPunishment[punishmentKeys[punishmentIndex]];
            }

            int creditValueChange = reward - punishment;

            return(creditValueChange);
        }
示例#2
0
 public RequestResponse BorrowRequest(string username, string id)
 {
     try
     {
         var goods  = goodsDac.Get(d => d.Id == id);
         var borrow = new BorrowLog
         {
             Id          = Guid.NewGuid().ToString(),
             CreateDate  = DateTime.UtcNow,
             Borrower    = username,
             BorrowGoods = new List <Goods>
             {
                 goods
             }
         };
         borrowDac.Create(borrow);
         return(new RequestResponse
         {
             Code = 200,
             Message = borrow.Id,
         });
     }
     catch (Exception ex)
     {
         return(new RequestResponse
         {
             Code = 500,
             Message = "Error: " + ex.Message,
         });
     }
 }
示例#3
0
        public async Task <BorrowLog> RevertBookAsync(User userId, Book bookId)
        {
            BorrowLog leaseLog      = null;
            BorrowLog leaseLogCheck = (await database.SelectAsync <BorrowLog, BorrowLog>(bl => bl.User.Id == userId.Id && bl.Book.Id == bookId.Id && bl.GiveBack == null)).FirstOrDefault();

            if (leaseLogCheck == null)
            {
                throw new Exception("无符合条件的借阅记录");
            }
            using IDatabaseTransactionManager transaction = database.StartTransaction();
            try
            {
                await database.ForUpdateAsync <BorrowLog, BorrowLog>(bl => bl.Id == leaseLogCheck.Id && bl.User.Id == bl.User.Id && bl.Book.Id == bl.Book.Id, transaction : transaction.GetTransaction());

                leaseLog = (await database.SelectAsync <BorrowLog, BorrowLog>(bl => bl.Id == leaseLogCheck.Id && bl.User.Id == bl.User.Id && bl.Book.Id == bl.Book.Id, transaction: transaction.GetTransaction())).FirstOrDefault();
                if (leaseLog == null)
                {
                    throw new Exception("无符合条件的借阅记录");
                }

                leaseLog.GiveBack = DateTime.Now;
                int creditChange = credit.GetCreditChange(leaseLog);
                if (await database.UpdateAsync <Book, int>(b => b.Margin + 1, b => b.Id == leaseLog.Book.Id, transaction: transaction.GetTransaction()) == 0)
                {
                    throw new Exception("更新书本信息失败");
                }

                if (await database.UpdateAsync <User, int>(u => u.CreditValue + creditChange, u => u.Id == leaseLog.User.Id, transaction: transaction.GetTransaction()) == 0)
                {
                    throw new Exception("更新会员信息失败");
                }

                if (await database.UpdateAsync <BorrowLog, bool>(bl => bl.GiveBack == DateTime.Now, bl => bl.Id == leaseLog.Id, transaction: transaction.GetTransaction()) == 0)
                {
                    throw new Exception("更新借阅记录失败");
                }

                leaseLog = (await database.SelectAsync <BorrowLog, BorrowLog>(bl => bl.Id == leaseLog.Id && bl.Book.Id == bl.Book.Id, transaction: transaction.GetTransaction())).FirstOrDefault();
                if (leaseLog == null)
                {
                    throw new Exception("更新借阅记录失败");
                }

                transaction.Commit();
            }
            catch (Exception)
            {
                transaction.Rollback();
                throw;
            }
            return(leaseLog);
        }
示例#4
0
        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);
        }