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
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
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}
def getBookInfo(bookid): # 通过书籍id查询书籍信息 with DBContext() as con: if not con.exec(_sql_getbook_info, (bookid,)): return {'state': State.DBErr} tempList = con.get_cursor().fetchone() if not tempList: return {'state': State.BookNExit} return {'state': State.OK, 'bookinfo': Tools.tuple2dict(_key_book_info, tempList)} pass
def view_user(): # 查看用户信息 with DBContext() as con: if not con.exec(_sql_user_info): return {'state': State.DBErr} userList = con.get_cursor().fetchall() try: res = Tools.list_tuple2dict(_key_user_info, userList) except: return {'state': State.Error} return {'state': State.OK, 'userlist': res}
def search_reviewed_book(): # 查询已审核的书 with DBContext() as con: if not con.exec(_sql_reviewed_book): return {'state': State.DBErr} tempList = con.get_cursor().fetchall() try: res = Tools.list_tuple2dict(_key_book_info, tempList) except: return {'state': State.Error} return {'state': State.OK, 'booklist': res}
def check_order(): #查看订单 with DBContext() as con: if not con.exec(_sql_check_order): return {'state': State.DBErr} orderlist = con.get_cursor().fetchall() try: res = Tools.list_tuple2dict(_key_order_info, orderlist) except: return {'state': State.Error} return {'state': State.OK, 'orderlist': res} pass
def getSearchBook(keyword): # 通过关键字查询书籍列表 # 构造模糊搜索 fuzzy = "%" + keyword + "%" with DBContext() as con: if not con.exec(_sql_search, (fuzzy,)): return {'state': State.DBErr} tempList = con.get_cursor().fetchall() try: res = Tools.list_tuple2dict(_key_book_info, tempList) except: return {'state': State.Error} return {'state': State.OK, 'booklist': res}
def get_collection(userid): with DBContext() as con: if not con.exec(_sql_view_collect, (userid, )): return {'state': State.DBErr} res = con.get_cursor().fetchall() if not res: return {'state': State.Error} try: res = Tools.list_tuple2dict(_key_view_collect, res) except: return {'state': State.Error} return {'state': State.OK, 'booklist': res} pass
def check_publish(userid): # 查看发布 with DBContext() as con: if not con.exec(_sql_user_order, (userid, )): return {'state': State.DBErr} res = con.get_cursor().fetchall() if not res: return {'state': State.Error} try: res = Tools.list_tuple2dict(_key_user_order, res) except: return {'state': State.Error} return {'state': State.OK, 'bookinfo': res} pass
def get_user_info(userid): # 获取用户个人的信息 with DBContext() as con: if not con.exec(_sql_user_info, (userid, )): return {'state': State.DBErr} res = con.get_cursor().fetchone() if not res: return {'state': State.Error} res = Tools.tuple2dict(_key_user_info, res) if not res: return {'state': State.Error} res.pop('password') res['state'] = State.OK return res pass
def get_class_books(bookclass): # 根据书籍的分类来获取书籍的列表 if bookclass not in _book_class: return {'state': State.FormErr} with DBContext() as con: if not con.exec(_sql_get_class, (bookclass,)): return {'state': State.DBErr} res = con.get_cursor().fetchall() if not res: return {'state': State.ListNone} try: res = Tools.list_tuple2dict(_key_book_info, res) except: return {'state': State.Error} return {'state': State.OK, 'booklist': res} pass
def get_recommand(max_fetch): # 根据传入的最大获取数来获取数据 # 获取 推荐的 书籍 if not isinstance(max_fetch, int): return {'state': State.Error} with DBContext() as con: if not con.exec(_sql_recommand): return {'state': State.DBErr} res = con.get_cursor().fetchmany(max_fetch) if not res: return {'state': State.ListNone} try: res = Tools.list_tuple2dict(_key_book_info, res) except: return {'state': State.Error} return {'state': State.OK, 'booklist': res} pass
def view_orders(userid, buyerornot): _sql_view_template = ''' select bookid,orderid,time,total,orders.state,name,picture,author,class from orders join user_order using (bookid,orderid) join book using (bookid) where {}=?; ''' _sql = _sql_view_template.format( ('buyerid' if buyerornot == 'True' else 'sellerid')) with DBContext() as con: if not con.exec(_sql, (userid, )): return {'state': State.DBErr} result = con.get_cursor().fetchall() if not result: return {'state': State.Error} try: result = Tools.list_tuple2dict(_const_key_view_orders, result) except: return {'state': State.Error} return {'state': State.OK, 'orderlist': result} pass