Пример #1
0
 def __init__(self):
   self.__db = MysqlAdapter()
Пример #2
0
class BookModel:
  def __init__(self):
    self.__db = MysqlAdapter()
  def import_book(self,books,uid):
    info = self.__db.query("SELECT isbn  FROM rent",None)
    info = [item[0] for item in info]
    rent_insert = []
    rent_list = []
    book_insert = []
    for book in books:
      ritem = ('',uid,book["isbn"],book["address"],book["rent_time"],book["return_time"].strip())
      bitem = ('',book["title"],book["isbn"],book["marc_no"],book["author"],book["images"],book["price"],book["pages"],book["publisher"],book["catalog"],book["ctime"])
      rent_list.append(ritem)
      book_insert.append(bitem)
    for book in rent_list:
      if not book[2] in info:
        rent_insert.append(book)
    if rent_insert:
      sql = ("INSERT INTO rent (id,uid,isbn,address,rent_time,return_time) VALUES(%s,%s,%s,%s,%s,%s)")
      if len(rent_insert) > 1:
        self.__db.insert(sql,rent_insert,"multi")
      else:
        self.__db.insert(sql,rent_insert)
    if book_insert:
      sql = ("INSERT INTO books (bid,title,isbn,marc_no,author,images,price,pages,publisher,catalog,ctime) VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)")
      if len(book_insert) > 1:
        self.__db.insert(sql,book_insert,"multi")
      else:
        self.__db.insert(sql,book_insert)
  def is_import(self,uid):
    info = self.__db.query("SELECT COUNT(id)  FROM rent WHERE uid = %s",uid)
    for num in info:
      if not num[0] == 1:
        return False
      else:
        return True
  def get_current(self,uid):
    sql = ("SELECT r.rent_time,r.return_time,b.title,b.images,b.author,b.isbn,r.address "
            "FROM rent AS r,books AS b "
            "WHERE r.uid = %s AND r.isbn = b.isbn AND DATEDIFF(r.return_time,NOW()) > 0 ")
    info = self.__db.query(sql,uid)
    return info

  def get_history(self,uid):
    sql = ("SELECT r.rent_time,r.return_time,b.title,b.images,b.author,b.isbn,r.address "
            "FROM rent AS r,books AS b "
            "WHERE r.uid = %s AND r.isbn = b.isbn AND DATEDIFF(r.return_time,NOW()) < 0 ")
    info = self.__db.query(sql,uid)
    return info
  def get_time_line(self,page):
    result = {}
    sql = ("SELECT  b.title, r.rent_time,r.return_time,u.name,b.images,b.author,b.isbn,u.id "
            "FROM rent AS r,books AS b,user AS u "
            "WHERE u.id = r.uid AND b.isbn = r.isbn ORDER BY r.rent_time DESC LIMIT %s,10")
    info = self.__db.query(sql,page)
    result["info"] = info
    sql = ("SELECT COUNT(id) AS num FROM rent")
    num = self.__db.query(sql,None)
    result["num"] = num
    return  result