コード例 #1
0
  @Override
  public BusinessResult buy(String userId, String bookId) throws DataAccessException {
    UserBook userBook = new UserBook();
    User user = mutableDataAccess.findById(User.class, userId);
    Book book = mutableDataAccess.findById(Book.class, bookId);
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("userId", userId);
    map.put("bookId", bookId);
    List<UserBook> userBooks = mutableDataAccess.paging(UserBook.class, 1, 1, map);
    if (userBooks.size() > 0) {
      if (userBooks.get(0).isHasBuyed() && book.getPrice() > 0) {
        return new BusinessResult(BusinessResult.ResultStatus.FAIL, "该书已经购买,不需要重复购买");
      }
      userBook = userBooks.get(0);
    }

    userBook.setBookId(bookId);
    userBook.setUserId(userId);

    if (book.getPrice() <= 0) {
      userBook.setHasBuyed(userBook.isHasBuyed());
      mutableDataAccess.save(userBook);
      return new BusinessResult(BusinessResult.ResultStatus.OK, "免费书籍,已经保存到书架上");
    } else {
      if (user.getCredits() < book.getPrice()) {
        return new BusinessResult(BusinessResult.ResultStatus.FAIL, "购买失败,您的信用值余额不足,请充值");
      } else {
        user.setCredits(user.getCredits() - book.getPrice());
        userBook.setHasBuyed(true);
        userBook.setUser(user);
        userBook.setBook(book);
        mutableDataAccess.save(user);
        book.setSellAmount(book.getSellAmount() + 1);
        mutableDataAccess.save(book);
        mutableDataAccess.save(userBook);
        return new BusinessResult(BusinessResult.ResultStatus.OK, "购买成功,已经保存到书架上");
      }
    }
  }
コード例 #2
0
 @Override
 public BusinessResult addToBookshelf(String userId, String bookId) throws DataAccessException {
   Map<String, Object> map = new HashMap<String, Object>();
   map.put("userId", userId);
   map.put("bookId", bookId);
   List<UserBook> userBooks = mutableDataAccess.paging(UserBook.class, 1, 1, map);
   if (userBooks.size() > 0) {
     return new BusinessResult(BusinessResult.ResultStatus.FAIL, "该书已经在你的书架中了,无需再添加");
   }
   final UserBook userBook = new UserBook();
   userBook.setHasBuyed(false);
   userBook.setUserId(userId);
   userBook.setBookId(bookId);
   userBook.setUser(mutableDataAccess.findById(User.class, userId));
   userBook.setBook(mutableDataAccess.findById(Book.class, bookId));
   mutableDataAccess.save(userBook);
   Book book = mutableDataAccess.findById(Book.class, bookId);
   book.setUserFavoriteAmount(book.getUserFavoriteAmount() + 1);
   mutableDataAccess.save(book);
   return new BusinessResult(BusinessResult.ResultStatus.OK, "加入书架成功");
 }