예제 #1
0
    def add_book(self, user_id: str, store_id: str, book_id: str, book_json_str: str, stock_level: int):
        book_json = json.loads(book_json_str)
        # try:
        if not user_id_exist(user_id):
            return error.error_non_exist_user_id(user_id)
        if not store_id_exist(store_id):
            return error.error_non_exist_store_id(store_id)
        if book_id_exist(store_id, book_id):
            return error.error_exist_book_id(book_id)
        book_one = Book(book_id = book_id,title = book_json.get("title"))
        if not session.query(Book).filter(Book.book_id==book_id).first():
            session.add(book_one)
            session.commit()
        store_detail_one=Store_detail(
            store_id = store_id,
            book_id = book_id,
            stock_level = stock_level,
            price = book_json.get("price")
        )
        session.add(store_detail_one)
        session.commit()

        # except BaseException as e:
        #     return 530, "{}".format(str(e))
        return 200, "ok"
예제 #2
0
 def unregister(self, user_id: str, password: str) -> (int, str):
     code, message = self.check_password(user_id, password)
     if code != 200:
         return code, message
     cursor = session.query(User).filter(User.user_id == user_id).first()
     session.delete(cursor)
     session.commit()
     return 200, "ok"
예제 #3
0
    def add_funds(self, user_id, password, add_value) -> (int, str):
        user = session.query(User).filter(User.user_id == user_id).first()
        if user is None:
            return error.error_authorization_fail()

        if user.password != password:
            return error.error_authorization_fail()

        user.balance += add_value
        session.commit()
        return 200, "ok"
예제 #4
0
 def login(self, user_id: str, password: str,
           terminal: str) -> (int, str, str):
     token = ""
     code, message = self.check_password(user_id, password)
     if code != 200:
         return code, message, ""
     token = jwt_encode(user_id, terminal)
     cursor = session.query(User).filter(User.user_id == user_id).first()
     cursor.token = token
     cursor.terminal = terminal
     session.commit()
     return 200, "ok", token
예제 #5
0
    def logout(self, user_id: str, token: str) -> bool:
        code, message = self.check_token(user_id, token)
        if code != 200:
            return code, message

        terminal = "terminal_{}".format(str(time.time()))
        dummy_token = jwt_encode(user_id, terminal)
        cursor = session.query(User).filter(User.user_id == user_id).first()
        cursor.token = dummy_token
        cursor.terminal = terminal
        session.commit()
        return 200, "ok"
예제 #6
0
 def create_store(self,user_id: str, store_id: str) -> (int, str):
     try:
         if not user_id_exist(user_id):
             return error.error_non_exist_user_id(user_id)
         if store_id_exist(store_id):
             return error.error_exist_store_id(store_id)
         store_one = Store(user_id=user_id,store_id=store_id)
         session.add(store_one)
         session.commit()
     except BaseException as e:
         return 530, "{}".format(str(e))
     return 200, "ok"
예제 #7
0
 def register(self, user_id: str, password: str):
     terminal = "terminal_{}".format(str(time.time()))
     token = jwt_encode(user_id, terminal)
     cursor = session.query(User).filter(User.user_id == user_id).first()
     if cursor is not None:
         return error.error_exist_user_id(user_id)
     user_one = User(user_id=user_id,
                     password=password,
                     balance=0,
                     token=token,
                     terminal=terminal)
     session.add(user_one)
     session.commit()
     return 200, "ok"
예제 #8
0
    def change_password(self, user_id: str, old_password: str,
                        new_password: str) -> bool:
        code, message = self.check_password(user_id, old_password)
        if code != 200:
            return code, message

        terminal = "terminal_{}".format(str(time.time()))
        token = jwt_encode(user_id, terminal)
        cursor = session.query(User).filter(User.user_id == user_id).first()
        cursor.password = new_password
        cursor.token = token
        cursor.terminal = terminal
        session.commit()
        return 200, "ok"
예제 #9
0
 def add_stock_level(self,user_id: str, store_id: str, book_id: str, add_stock_level: int):
     try:
         if not user_id_exist(user_id):
             return error.error_non_exist_user_id(user_id)
         if not store_id_exist(store_id):
             return error.error_non_exist_store_id(store_id)
         if not book_id_exist(store_id, book_id):
             return error.error_non_exist_book_id(book_id)
         
         cursor = session.query(Store_detail).filter( and_(Store_detail.store_id== store_id,Store_detail.book_id==book_id)).first()
         cursor.stock_level = cursor.stock_level+add_stock_level
         session.commit()
     except BaseException as e:
         return 530, "{}".format(str(e))
     return 200, "ok"
예제 #10
0
    def new_order(self, user_id: str, store_id: str,
                  id_and_count: [(str, int)]) -> (int, str, str):
        order_id = ""
        if not user_id_exist(user_id):
            return error.error_non_exist_user_id(user_id) + (order_id, )
        if not store_id_exist(store_id):
            return error.error_non_exist_store_id(store_id) + (order_id, )
        uid = "{}_{}_{}".format(user_id, store_id, str(uuid.uuid1()))

        for book_id, count in id_and_count:
            store_detail = session.query(Store_detail).filter(
                Store_detail.store_id == store_id,
                Store_detail.book_id == book_id).first()
            if (store_detail is None):
                return error.error_non_exist_book_id(book_id) + (order_id, )

            stock_level = store_detail.stock_level

            if stock_level < count:
                return error.error_stock_level_low(book_id) + (order_id, )

            store_detail.stock_level -= count
            session.commit()
            session.add(
                Order_detail(order_id=uid, book_id=book_id, count=count))
            session.commit()
        session.add(
            Order(order_id=uid,
                  user_id=user_id,
                  store_id=store_id,
                  paytime=datetime.now(),
                  status=3))
        session.commit()

        order_id = uid
        return 200, "ok", order_id
예제 #11
0
    def payment(self, user_id: str, password: str,
                order_id: str) -> (int, str):
        # Buyer_func.check_order(self, order_id)

        order = session.query(Order).filter(Order.order_id == order_id).first()

        if order is None:
            return error.error_invalid_order_id(order_id)
        if order.status == 4:
            return error.error_order_closed(order_id)
        elif order.status != 3:
            return error.error_order_has_been_paid(order_id)

        # order_id = order.order_id
        buyer_id = order.user_id
        store_id = order.store_id

        if buyer_id != user_id:
            return error.error_authorization_fail()

        buyer = session.query(User).filter(User.user_id == buyer_id).first()
        if buyer is None:
            return error.error_non_exist_user_id(buyer_id)
        balance = buyer.balance
        if (password != buyer.password):
            return error.error_authorization_fail()

        store = session.query(Store).filter(Store.store_id == store_id).first()
        if store is None:
            return error.error_non_exist_store_id(store_id)

        seller_id = store.user_id
        if not user_id_exist(seller_id):
            return error.error_non_exist_user_id(seller_id)

        order_detail = session.query(Order_detail).filter(
            Order_detail.order_id == order_id).all()
        total_price = 0
        for i in range(0, len(order_detail)):
            book_id = order_detail[i].book_id
            book = session.query(Store_detail).filter(
                Store_detail.store_id == store_id,
                Store_detail.book_id == book_id).first()
            count = order_detail[i].count
            price = book.price
            total_price = total_price + price * count

        if balance < total_price:
            return error.error_not_sufficient_funds(order_id)

        buyer.balance -= total_price
        seller = session.query(User).filter(User.user_id == seller_id).first()
        seller.balance += total_price

        order.status = 0  # 设置付款状态:已支付
        order.paytime = datetime.now()
        # ?????代码在干嘛? 删除订单?
        # cursor = conn.execute("DELETE FROM new_order WHERE order_id = ?", (order_id,))
        # if cursor.rowcount == 0:
        #     return error.error_invalid_order_id(order_id)
        #
        # cursor = conn.execute("DELETE FROM new_order_detail where order_id = ?", (order_id,))
        # if cursor.rowcount == 0:
        #     return error.error_invalid_order_id(order_id)
        session.commit()
        return 200, "ok"