Exemplo n.º 1
0
        public async Task Borrow(BorrowInputDto inputdto)
        {
            var isExist = await this.isExists(inputdto.GlobalId);

            if (!isExist)
            {
                throw new Exception($"{inputdto.GlobalId},员工不存在,不允许借书.");
            }
            var isAvailable = await this.isAvailable(inputdto.BarCode);

            if (!isAvailable)
            {
                throw new Exception($"{inputdto.BarCode},图书不存在,请联系管理员.");
            }
            var emp = await this.employeeService.Queryable().Where(x => x.GlobalId == inputdto.GlobalId).FirstAsync();

            var stock = await this.stockService.Queryable().Where(x => (x.BarCode == inputdto.BarCode || x.ISBN == inputdto.BarCode) && x.Qty > 0)
                        .Include(x => x.Book)
                        .FirstAsync();

            var checkout = new CheckOut();

            checkout.GlobalId    = emp.GlobalId;
            checkout.Employee    = emp;
            checkout.EmployeeId  = emp.Id;
            checkout.Phone       = inputdto.Phone;
            checkout.ShortName   = emp.ShortName;
            checkout.DisplayName = emp.DisplayName;
            checkout.BorrowDate  = DateTime.Now;
            checkout.ExpiryDate  = DateTime.Now.AddMonths(1);
            checkout.Expiry      = false;
            checkout.Days        = (checkout.ExpiryDate.Value - checkout.BorrowDate).Days + 1;
            checkout.Qty         = 1;
            checkout.Book        = stock.Book;
            checkout.BookId      = stock.BookId;
            checkout.BarCode     = stock.BarCode;
            checkout.ISBN        = stock.ISBN;
            checkout.Status      = "Pending";
            checkout.Title       = stock.Title;
            stock.Qty            = stock.Qty - 1;

            if (stock.Qty == 0)
            {
                stock.Status = "Out of Stock";
            }

            emp.Phone = inputdto.Phone;
            var book = await this.bookService.FindAsync(stock.BookId);

            book.Reads = book.Reads + 1;
            this.bookService.Update(book);
            this.employeeService.Update(emp);
            this.stockService.Update(stock);
            this.Insert(checkout);
            this.logger.Info($"Borrow:{inputdto.GlobalId},{emp.DisplayName},{inputdto.BarCode},{stock.Title}");
        }
Exemplo n.º 2
0
        public async Task <JsonResult> Borrow(BorrowInputDto inputdto)
        {
            try
            {
                await this.checkOutService.Borrow(inputdto);

                await this.unitOfWork.SaveChangesAsync();

                return(Json(new { success = true }, JsonRequestBehavior.AllowGet));
            }
            catch (Exception e)
            {
                return(Json(new { success = false, err = e.GetMessage() }, JsonRequestBehavior.AllowGet));
            }
        }