Ejemplo n.º 1
0
 def publish_book(userid, name, price, detail, isbn, number, picture,
                  author, bookclass):
     _bookid = "B{:04d}{:10d}".format(random.randint(0, 9999),
                                      int(time.time()))
     if len(name) < 0:
         return {'state': State.FormErr}
     try:
         _price = float(price)
         _number = int(number)
     except:
         return {'state': State.FormErr}
     _detail = (detail if len(detail) > 0 else "无详情")
     _isbn = (isbn if len(isbn) > 0 else "无")
     _state = "待审核"
     _author = (author if len(author) > 0 else "无")
     _class = (bookclass if bookclass in _book_class else "其他")
     _time = Tools.get_current_time()
     with DBContext() as con:
         if not con.exec(_sql_pub_book,
                         (_bookid, name, _price, _detail, _isbn, _number,
                          picture, _state, _author, _class)):
             return {'state': State.DBErr}
         if not con.exec(_sql_pub_book2, (_bookid, userid, _time)):
             return {'state': State.DBErr}
         return {'state': State.OK}
     pass
Ejemplo n.º 2
0
 def collect_book(userid, bookid):
     with DBContext() as con:
         if not con.exec(_sql_collect_book,
                         (userid, bookid, Tools.get_current_time())):
             return {'state': State.DBErr}
         return {'state': State.OK}
     pass
Ejemplo n.º 3
0
 def addNewOrder(buyerid, bookid, number):
     ts = int(time.time())  # 秒级时间戳
     orderid = buyerid + 'A' + str(ts)
     state = "未完成"
     with DBContext() as context:
         if not context.exec("SELECT price,state from book where bookid=?;",
                             (bookid, )):
             return {'state': State.DBErr}
         result = context.get_cursor().fetchone()
         if not result:
             return {'state': State.NoBookErr}
         total = result[0]
         bookstate = result[1]
         if bookstate != "待售":
             return {'state': State.NoSale}
         if not context.exec("INSERT INTO orders values(?,?,?,?,?,?);",
                             (bookid, orderid, Tools.get_current_time(),
                              number, total, state)):
             return {'state': State.DBErr}
         if not context.exec(
                 "SELECT userid from user_book_publish where bookid=?;",
             (bookid, )):
             return {'state': State.DBErr}
         result = context.get_cursor().fetchone()
         if not result:
             return {'state': State.NoBookErr}
         result = result[0]
         if not context.exec("INSERT INTO user_order values(?,?,?,?);",
                             (bookid, orderid, buyerid, result)):
             return {'state': State.DBErr}
         if not context.exec("update book set state='已售' where bookid=?;",
                             (bookid, )):
             return {'state': State.DBErr}
     return {'orderid': orderid, 'state': State.OK, 'price': total}