/// <summary> /// 查询图书的借阅记录 /// </summary> /// <param name="bookId">图书ID</param> /// <param name="currentBorrow">当前的借阅记录</param> /// <returns>图书的借阅记录</returns> public IList <BookBorrowModel> GetBookBorrowHistory(int bookId, BookBorrowModel currentBorrow = null) { using (var dbContext = new MissionskyOAEntities()) { var entities = dbContext.BookBorrows.Where(it => it.BookId == bookId).ToList(); var list = new List <BookBorrowModel>(); entities.ForEach(entity => { var model = entity.ToModel(); //借阅记录 var user = dbContext.Users.FirstOrDefault(it => it.Id == model.UserId); //借阅用户 model.UserName = (user != null ? user.EnglishName : string.Empty); //用户名 list.Add(model); }); //获取当前的借阅记录 if (currentBorrow != null) { var userBorrow = list.FirstOrDefault(it => it.Status == UserBorrowStatus.Borrowing); CopyTo(userBorrow, currentBorrow); //复制 } return(list); } }
public static BookBorrow ToEntity(this BookBorrowModel model) { var entity = new BookBorrow() { Id = model.Id, BookId = model.BookId, UserId = model.UserId, Status = (int)model.Status, BorrowDate = model.BorrowDate, ReturnDate = model.ReturnDate }; return(entity); }
/// <summary> /// 复制 /// </summary> /// <param name="source">源</param> /// <param name="target">目标</param> private void CopyTo(BookBorrowModel source, BookBorrowModel target) { if (source != null && target != null) { target.Id = source.Id; target.UserId = source.UserId; target.UserName = source.UserName; target.BookId = source.BookId; target.BookName = source.BookName; target.Status = source.Status; target.ReturnDate = source.ReturnDate; target.BorrowDate = source.BorrowDate; } }
public static BookBorrowModel ToModel(this BookBorrow entity) { var model = new BookBorrowModel() { Id = entity.Id, BookId = entity.BookId, UserId = entity.UserId, Status = entity.Status.HasValue ? (UserBorrowStatus)entity.Status.Value : UserBorrowStatus.None, BorrowDate = entity.BorrowDate, ReturnDate = entity.ReturnDate }; return(model); }