Exemplo n.º 1
0
    def logout(self, user_id: str, token: str) -> bool:
        try:
            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)
            self.cursor = self.conn.cursor()
            self.cursor.execute(
                "UPDATE usr SET token = '%s', terminal = '%s' WHERE user_id='%s'" %
                (dummy_token, terminal, user_id) )
            if self.cursor.rowcount == 0:
                return error.error_authorization_fail()

            self.conn.commit()
        except psycopg2.errors.UniqueViolation:
            return error.error_exist_user_id(user_id)
        return 200, "ok"
Exemplo n.º 2
0
    def login(self, user_id: str, password: str, terminal: str) -> (int, str, str):
        token = ""
        try:
            code, message = self.check_password(user_id, password)
            if code != 200:
                return code, message, ""

            token = jwt_encode(user_id, terminal)
            self.cursor = self.conn.cursor()
            self.cursor.execute(
                "UPDATE usr set token= '%s' , terminal = '%s' where user_id = '%s'"
               % (token, terminal, user_id) )
            #print(self.cursor.rowcount)
            if self.cursor.rowcount == 0: ##
                return error.error_authorization_fail() + ("", )
            self.conn.commit()
        except psycopg2.errors.UniqueViolation:
            return error.error_exist_user_id(user_id)
        return 200, "ok", token
Exemplo n.º 3
0
    def login(self, user_id: str, password: str,
              terminal: str) -> (int, str, str):
        token = ""
        try:
            code, message = self.check_password(user_id, password)
            if code != 200:
                return code, message, ""

            token = jwt_encode(user_id, terminal)
            user_ = self.session.query(User).filter(
                User.user_id == user_id).first()
            if user_ is None:
                return error.error_authorization_fail() + ("", )
            user_.token = token
            user_.terminal = terminal
            self.session.commit()

        except BaseException as e:
            return 530, "{}".format(str(e)), ""
        return 200, "ok", token
Exemplo n.º 4
0
    def cancel(self, user_id: str, password: str, order_id: str):
        try:
            user = self.session.query(User).filter_by(user_id=user_id).first()
            if user is None:
                return error.error_non_exist_user_id(user_id)
            if user.password != password:
                return error.error_authorization_fail()

            cursor = self.session.query(Order).filter_by(id=order_id, buyer_id=user_id, status=Order_status.pending)
            rowcount = cursor.update({Order.status: Order_status.cancelled})
            if rowcount == 0:
                return error.error_non_exist_user_id(user_id)

            self.session.commit()
            self.session.close()
            
        except BaseException as e:
            return 530, "{}".format(str(e))

        return 200, "ok"
Exemplo n.º 5
0
 def change_password(self, user_id: str, old_password: str,
                     new_password: str) -> bool:
     try:
         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)
         row = self.Session.query(U).filter(U.user_id == user_id).first()
         if row is None:
             return error.error_authorization_fail()
         row.password = new_password
         row.token = token
         row.terminal = terminal
         self.Session.commit()
     except sqlalchemy.exc.IntegrityError as e:
         return 528, "{}".format(str(e))
     except BaseException as e:
         return 530, "{}".format(str(e))
     return 200, "ok"
Exemplo n.º 6
0
    def logout(self, user_id: str, token: str) -> (int, str):
        try:
            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 = self.conn.execute(
                "UPDATE usr SET token = '%s', terminal = '%s' WHERE user_id='%s'" % (token, terminal, user_id))
            if cursor.rowcount == 0:
                return error.error_authorization_fail()

            self.conn.commit()
        except sqlalchemy.exc.IntegrityError as e:
            return 528, "{}".format(str(e))
        except BaseException as e:
            return 530, "{}".format(str(e))
        return 200, "ok"
Exemplo n.º 7
0
    def change_password(self, user_id: str, old_password: str, new_password: str) -> (int, str):
        try:
            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 = self.conn.execute(
                "UPDATE usr set password = '******', token= '%s' , terminal = '%s' where user_id = '%s'" % (
                    new_password, token, terminal, user_id))
            if cursor.rowcount == 0:
                return error.error_authorization_fail()

            self.conn.commit()
        except sqlalchemy.exc.IntegrityError as e:
            return 528, "{}".format(str(e))
        except BaseException as e:
            return 530, "{}".format(str(e))
        return 200, "ok"
Exemplo n.º 8
0
    def unregister(self, user_id: str, password: str) -> (int, str):
        try:
            code, message = self.check_password(user_id, password)
            if code != 200:
                return code, message

            # cursor = self.conn.execute("DELETE from user where user_id=?", (user_id,))
            # if cursor.rowcount == 1:
            #     self.conn.commit()
            # else:
            #     return error.error_authorization_fail()
            row = db_session.query(User).filter_by(user_id=user_id).first()
            if row is None:
                return error.error_authorization_fail()
            else:
                db_session.delete(row)
                db_session.commit()

        except BaseException as e:
            return 530, "{}".format(str(e))
        return 200, "ok"
Exemplo n.º 9
0
    def query_order(self, user_id):
        try:
            order_list = []
            row = self.Session.query(User.password).filter(User.user_id == user_id).first()
            if row is None:
                response = error.error_authorization_fail()
                code = response[0]
                message = response[1]
                return code, message, order_list

            cursor = self.Session.query(New_order.order_id, New_order.state).filter(New_order.user_id == user_id)
            if cursor.count() != 0:
                for row in cursor:
                    order_list.append(row[0])
            self.Session.commit()
        except sqlalchemy.exc.IntegrityError as e:
            return 528, "{}".format(str(e))
        except BaseException as e:
            return 530, "{}".format(str(e))
        # print(order_list)
        return 200, "ok", order_list
Exemplo n.º 10
0
    def logout(self, user_id: str, token: str) -> (int, str):
        try:
            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)

            user_ = self.session.query(User).filter(
                User.user_id == user_id).first()

            if user_ is None:
                return error.error_authorization_fail()
            user_.token = dummy_token
            user_.terminal = terminal
            self.session.commit()

        except BaseException as e:
            return 530, "{}".format(str(e))
        return 200, "ok"
Exemplo n.º 11
0
    def login(self, user_id: str, password: str,
              terminal: str) -> (int, str, str):
        token = ""
        try:
            code, message = self.check_password(user_id, password)
            if code != 200:
                return code, message, ""

            token = jwt_encode(user_id, terminal)
            cursor = self.conn.execute(
                "UPDATE user set token= ? , terminal = ? where user_id = ?",
                (token, terminal, user_id),
            )
            if cursor.rowcount == 0:
                return error.error_authorization_fail() + ("", )
            self.conn.commit()
        except sqlite.Error as e:
            return 528, "{}".format(str(e)), ""
        except BaseException as e:
            return 530, "{}".format(str(e)), ""
        return 200, "ok", token
Exemplo n.º 12
0
 def login(self, user_id: str, password: str,
           terminal: str) -> (int, str, str):
     token = ""
     try:
         # 验证密码
         code, message = self.check_password(user_id, password)
         if code != 200:
             return code, message, ""
         # 生成token
         token = jwt_encode(user_id, terminal)
         row = self.Session.query(U).filter(U.user_id == user_id).first()
         if row is None:
             return error.error_authorization_fail() + ("", )
         row.token = token
         row.terminal = terminal
         self.Session.commit()
         row = self.Session.query(U).filter(U.user_id == user_id).first()
     except sqlalchemy.exc.IntegrityError as e:
         return 528, "{}".format(str(e)), ""
     except BaseException as e:
         return 530, "{}".format(str(e)), ""
     return 200, "ok", token
Exemplo n.º 13
0
    def add_funds(self, user_id, password, add_value) -> (int, str):
        try:
            cursor = self.session.query(User).filter_by(user_id=user_id)
            row = cursor.first()
            if row is None:
                return error.error_non_exist_user_id()

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

            cursor = self.session.query(User).filter(User.user_id==user_id)
            rowcount = cursor.update({User.balance: User.balance + add_value})
            if rowcount == 0:
                return error.error_non_exist_user_id(user_id)

            self.session.commit()
            self.session.close()
            
        except BaseException as e:
            return 530, "{}".format(str(e))

        return 200, "ok"
Exemplo n.º 14
0
    def logout(self, user_id: str, token: str) -> bool:
        try:
            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 = self.conn.execute(
                "UPDATE user SET token = ?, terminal = ? WHERE user_id=?",
                (dummy_token, terminal, user_id),
            )
            if cursor.rowcount == 0:
                return error.error_authorization_fail()

            self.conn.commit()
        except sqlite.Error as e:
            return 528, "{}".format(str(e))
        except BaseException as e:
            return 530, "{}".format(str(e))
        return 200, "ok"
Exemplo n.º 15
0
    def logout(self, user_id: str, token: str):
        try:
            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 = self.conn.cursor()
            cursor.execute(
                "UPDATE \"user\" SET token = %s, terminal = %s WHERE user_id=%s",
                (dummy_token, terminal, user_id),
            )
            if cursor.rowcount == 0:
                return error.error_authorization_fail()

            self.conn.commit()
            cursor.close()
        except (Exception, psycopg2.DatabaseError) as e:
            return 528, "{}".format(str(e))
        except BaseException as e:
            return 530, "{}".format(str(e))
        return 200, "ok"
Exemplo n.º 16
0
    def login(self, user_id: str, password: str,
              terminal: str) -> (int, str, str):
        token = ""
        try:
            code, message = self.check_password(user_id, password)
            if code != 200:
                return code, message, ""

            token = jwt_encode(user_id, terminal)
            cursor = self.conn.cursor()
            cursor.execute(
                "UPDATE \"user\" set token= %s , terminal = %s where user_id = %s",
                (token, terminal, user_id),
            )
            if cursor.rowcount == 0:
                return error.error_authorization_fail() + ("", )
            self.conn.commit()
            cursor.close()
        except (Exception, psycopg2.DatabaseError) as e:
            return 528, "{}".format(str(e)), ""
        except BaseException as e:
            return 530, "{}".format(str(e)), ""
        return 200, "ok", token
Exemplo n.º 17
0
    def search_orders (self , user_id : str , password : str) -> (int,str):

        try:
            if not self.user_id_exist(user_id):
                return error.error_non_exist_user_id(user_id)

            row=User.query.filter_by(user_id=user_id).first()
            if password != row.password:
                return error.error_authorization_fail()

            orders=New_Order.query.filter_by(user_id=user_id).all()
            if orders is None :
                return error.error_non_exist_order_id(user_id)

            list_orders=[]
            for order in orders:
                items=New_Order_Detail.query.filter_by(order_id=order.order_id).all()
                for item in items:
                    list_orders.append({"user_id":user_id,"order_id":item.order_id,"book_id":item.book_id,"count":item.count,"price":item.price})

        except BaseException as e:
            print(e)
            return 530, "{}".format(str(e))
        return 200,str(list_orders)
Exemplo n.º 18
0
    def confirm_stock(self, user_id: str, password: str, order_id: str) -> (int, str):
        try:
            if not self.user_id_exist(user_id):
                return error.error_non_exist_user_id(user_id)

            row = User.query.filter_by(user_id=user_id).first()
            if password != row.password:
                return error.error_authorization_fail()
            cursor = New_Order_Detail.query.filter_by(order_id=order_id).first()

            if cursor is None:
                return error.error_invalid_order_id(order_id)

            if cursor.state != 2:
                return error.error_order_not_dispatched(order_id)

            cursor = New_Order_Detail.query.filter_by(order_id=order_id).update({"state": 3})  # 订单全部更新为3
            if cursor == 0:
                return error.error_invalid_order_id(order_id)
            db_session.commit()

        except BaseException as e:
            return 530, "{}".format(str(e))
        return 200, "ok"
Exemplo n.º 19
0
    def change_password(self, user_id: str, old_password: str,
                        new_password: str):
        try:
            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 = self.conn.cursor()
            cursor.execute(
                "UPDATE \"user\" set password = %s, token= %s , terminal = %s where user_id = %s",
                (new_password, token, terminal, user_id),
            )
            if cursor.rowcount == 0:
                return error.error_authorization_fail()

            self.conn.commit()
            cursor.close()
        except (Exception, psycopg2.DatabaseError) as e:
            return 528, "{}".format(str(e))
        except BaseException as e:
            return 530, "{}".format(str(e))
        return 200, "ok"
Exemplo n.º 20
0
    def login(self, user_id: str, password: str, terminal: str) -> (int, str, str):
        token = ""
        try:

            code, message = self.check_password(user_id, password)

            if code != 200:
                return code, message, ""
            token = jwt_encode(user_id, terminal)
            #print(1)

            cursor = self.conn.execute(
                "UPDATE usr set token= '%s' , terminal = '%s' where user_id = '%s'" % (token, terminal, user_id))
            #print(2)
            if cursor.rowcount == 0:
                return error.error_authorization_fail() + ("",)
            self.conn.commit()
        except sqlalchemy.exc.IntegrityError as e:
            return 528, "{}".format(str(e)), ""
        except BaseException as e:
            print(e)
            return 530, "{}".format(str(e)), ""
        #print("登录成功")
        return 200, "ok", token
Exemplo n.º 21
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"
Exemplo n.º 22
0
    def query(self, user_id: str, order_id: str) -> (int, str, str):
        conn = self.conn
        try:
            # 检查订单进度(未付款、已付款、已交付)
            cursor = conn.execute(
                "SELECT user_id, store_id FROM new_order WHERE order_id = %s",
                (order_id, ))
            row_new = conn.fetchone()
            cursor = conn.execute(
                "SELECT user_id, store_id,state FROM paid_order WHERE order_id = %s",
                (order_id, ))
            row_paid = conn.fetchone()
            cursor = conn.execute(
                "SELECT user_id, store_id FROM history_order WHERE order_id = %s",
                (order_id, ))
            row_history = conn.fetchone()

            if row_new is None and row_paid is None and row_history is None:
                return error.error_invalid_order_id(order_id)
            elif row_new is not None:
                buyer_id = row_new['user_id']
                store_id = row_new['store_id']
                if buyer_id != user_id:
                    return error.error_authorization_fail()

                conn.execute(
                    "SELECT book_id, count FROM new_order_detail WHERE order_id = %s;",
                    (order_id, ))
                cursor = conn.fetchall()

                books = []
                for row in cursor:
                    dict = {}
                    dict["id"] = row['book_id']
                    dict["count"] = row['count']
                    books.append(dict)
                json_text = {}
                json_text["user_id"] = buyer_id
                json_text["store_id"] = store_id
                json_text["books"] = books
                json_text["order_state"] = "unpaid"
                return 200, "ok", str(json_text)
            elif row_paid is not None:
                buyer_id = row_paid['user_id']
                store_id = row_paid['store_id']
                state = row_paid['state']
                if buyer_id != user_id:
                    return error.error_authorization_fail()

                conn.execute(
                    "SELECT book_id, count FROM paid_order_detail WHERE order_id = %s;",
                    (order_id, ))
                cursor = conn.fetchall()

                books = []
                for row in cursor:
                    dict = {}
                    dict["id"] = row['book_id']
                    dict["count"] = row['count']
                    books.append(dict)
                json_text = {}
                json_text["user_id"] = buyer_id
                json_text["store_id"] = store_id
                json_text["books"] = books
                json_text["order_state"] = state
                return 200, "ok", str(json_text)
            elif row_history is not None:
                buyer_id = row_history['user_id']
                store_id = row_history['store_id']
                if buyer_id != user_id:
                    return error.error_authorization_fail()

                conn.execute(
                    "SELECT book_id, count FROM history_order_detail WHERE order_id = %s;",
                    (order_id, ))
                cursor = conn.fetchall()

                books = []
                for row in cursor:
                    dict = {}
                    dict["id"] = row['book_id']
                    dict["count"] = row['count']
                    books.append(dict)
                json_text = {}
                json_text["user_id"] = buyer_id
                json_text["store_id"] = store_id
                json_text["books"] = books
                json_text["order_state"] = "finished"
                return 200, "ok", str(json_text)

        except pg.Error as e:
            return 528, "{}".format(str(e))

        except BaseException as e:
            return 530, "{}".format(str(e))
Exemplo n.º 23
0
    def payment(self, user_id: str, password: str,
                order_id: str) -> (int, str):
        conn = self.conn
        try:
            #检查订单是否存在
            cursor = conn.execute(
                "SELECT order_id, user_id, store_id FROM new_order WHERE order_id = %s",
                (order_id, ))
            row = conn.fetchone()
            if row is None:
                return error.error_invalid_order_id(order_id)

            order_id = row['order_id']
            buyer_id = row['user_id']
            store_id = row['store_id']
            #检查买家信息
            if buyer_id != user_id:
                return error.error_authorization_fail()
            #核对账户密码
            cursor = conn.execute(
                "SELECT balance, password FROM users WHERE user_id = %s;",
                (buyer_id, ))
            row = conn.fetchone()
            if row is None:
                return error.error_non_exist_user_id(buyer_id)
            balance = row['balance']
            if password != row['password']:
                return error.error_authorization_fail()

            #核对店铺信息
            cursor = conn.execute(
                "SELECT store_id, user_id FROM user_store WHERE store_id = %s;",
                (store_id, ))
            row = conn.fetchone()
            if row is None:
                return error.error_non_exist_store_id(store_id)

            seller_id = row['user_id']

            if not self.user_id_exist(seller_id):
                return error.error_non_exist_user_id(seller_id)

            conn.execute(
                "SELECT book_id, count, price FROM new_order_detail WHERE order_id = %s;",
                (order_id, ))
            cursor = conn.fetchall()
            total_price = 0
            bookids = []
            counts = []
            prices = []
            for row in cursor:
                bookids.append(row['book_id'])
                counts.append(row['count'])
                prices.append(row['price'])
                count = row['count']
                price = row['price']
                total_price = total_price + price * count

            if balance < total_price:
                return error.error_not_sufficient_funds(order_id)
            #买家扣除相应钱款
            cursor = conn.execute(
                "UPDATE users set balance = balance - %s"
                "WHERE user_id = %s AND balance >= %s",
                (total_price, buyer_id, total_price))
            """if cursor.rowcount == 0:
                return error.error_not_sufficient_funds(order_id)"""
            #卖家增加相应钱款
            cursor = conn.execute(
                "UPDATE users set balance = balance + %s"
                "WHERE user_id = %s", (total_price, seller_id))
            """if cursor.rowcount == 0:
                return error.error_non_exist_user_id(buyer_id)"""
            #删除订单信息
            cursor = conn.execute("DELETE FROM new_order WHERE order_id = %s",
                                  (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 = %s",
                (order_id, ))
            """if cursor.rowcount == 0:
                return error.error_invalid_order_id(order_id)"""

            #增加状态信息
            cursor = conn.execute(
                "INSERT INTO paid_order(order_id,user_id,store_id,state) "
                "VALUES(%s, %s, %s, '待发货');", (order_id, user_id, store_id))

            for i in range(len(bookids)):
                cursor = conn.execute(
                    "INSERT INTO paid_order_detail(order_id,book_id,count,price) "
                    "VALUES(%s, %s, %s, %s);",
                    (order_id, bookids[i], counts[i], prices[i]))
            #conn.commit()

        except pg.Error as e:
            return 528, "{}".format(str(e))

        except BaseException as e:
            return 530, "{}".format(str(e))

        return 200, "ok"
Exemplo n.º 24
0
    def cancel(self, user_id: str, order_id: str) -> (int, str):
        conn = self.conn
        try:
            # 订单未付款或者订单付款位发货都可以取消订单
            #如果已经付款,还会退款
            cursor = conn.execute(
                "SELECT user_id, store_id FROM new_order WHERE order_id = %s",
                (order_id, ))
            row_new = conn.fetchone()
            cursor = conn.execute(
                "SELECT user_id, store_id,state FROM paid_order WHERE order_id = %s",
                (order_id, ))
            row_paid = conn.fetchone()
            if row_new is None and row_paid is None:
                return error.error_invalid_order_id(order_id)
            elif row_new is not None:
                buyer_id = row_new['user_id']
                store_id = row_new['store_id']
                cursor = conn.execute(
                    "SELECT store_id, user_id FROM user_store WHERE store_id = %s;",
                    (store_id, ))
                row = conn.fetchone()
                seller_id = row['user_id']

                if not self.user_id_exist(seller_id):
                    return error.error_non_exist_user_id(seller_id)
                if buyer_id != user_id:
                    return error.error_authorization_fail()

                # 订单未付款,接受取消操作,该订单写入历史订单
                cursor = self.conn.execute(
                    "INSERT INTO history_order(order_id,user_id,store_id) "
                    "VALUES(%s, %s, %s);", (order_id, user_id, store_id))

                self.conn.execute(
                    "SELECT book_id, count, price FROM new_order_detail WHERE order_id = %s;",
                    (order_id, ))
                cursor = self.conn.fetchall()

                bookids = []
                counts = []
                prices = []

                for row in cursor:
                    bookids.append(row['book_id'])
                    counts.append(row['count'])
                    prices.append(row['price'])

                for i in range(len(bookids)):
                    cursor = self.conn.execute(
                        "INSERT INTO history_order_detail(order_id,book_id,count,price) "
                        "VALUES(%s, %s, %s, %s);",
                        (order_id, bookids[i], counts[i], prices[i]))

                # 删除订单信息
                cursor = self.conn.execute(
                    "DELETE FROM new_order WHERE order_id = %s", (order_id, ))
                cursor = self.conn.execute(
                    "DELETE FROM new_order_detail where order_id = %s",
                    (order_id, ))

                return 200, "ok"
            elif row_paid is not None:
                buyer_id = row_paid['user_id']
                store_id = row_paid['store_id']
                state = row_paid['state']
                cursor = conn.execute(
                    "SELECT store_id, user_id FROM user_store WHERE store_id = %s;",
                    (store_id, ))
                row = conn.fetchone()
                seller_id = row['user_id']

                if not self.user_id_exist(seller_id):
                    return error.error_non_exist_user_id(seller_id)
                if buyer_id != user_id:
                    return error.error_authorization_fail()
                if state == "发货中":
                    return error.error_cancel()
                # 订单未发货,接受取消操作,该订单写入历史订单,退款
                cursor = self.conn.execute(
                    "INSERT INTO history_order(order_id,user_id,store_id) "
                    "VALUES(%s, %s, %s);", (order_id, user_id, store_id))

                self.conn.execute(
                    "SELECT book_id, count, price FROM paid_order_detail WHERE order_id = %s;",
                    (order_id, ))
                cursor = self.conn.fetchall()
                total_price = 0
                bookids = []
                counts = []
                prices = []

                for row in cursor:
                    bookids.append(row['book_id'])
                    counts.append(row['count'])
                    prices.append(row['price'])
                    count = row['count']
                    price = row['price']
                    total_price = total_price + price * count

                for i in range(len(bookids)):
                    cursor = self.conn.execute(
                        "INSERT INTO history_order_detail(order_id,book_id,count,price) "
                        "VALUES(%s, %s, %s, %s);",
                        (order_id, bookids[i], counts[i], prices[i]))
                # 买家增加相应钱款
                cursor = conn.execute(
                    "UPDATE users set balance = balance + %s"
                    "WHERE user_id = %s AND balance >= %s",
                    (total_price, buyer_id, total_price))

                # 卖家减少相应钱款
                cursor = conn.execute(
                    "UPDATE users set balance = balance - %s"
                    "WHERE user_id = %s", (total_price, seller_id))

                # 删除订单信息
                cursor = self.conn.execute(
                    "DELETE FROM paid_order WHERE order_id = %s", (order_id, ))
                cursor = self.conn.execute(
                    "DELETE FROM paid_order_detail where order_id = %s",
                    (order_id, ))

                return 200, "ok"

        except pg.Error as e:
            return 528, "{}".format(str(e))

        except BaseException as e:
            return 530, "{}".format(str(e))
Exemplo n.º 25
0
    def payment(self, user_id: str, password: str,
                order_id: str) -> (int, str):
        conn = self.conn
        try:
            cursor = conn.execute(
                "SELECT order_id, user_id, store_id FROM new_order WHERE order_id = ?",
                (order_id, ))
            row = cursor.fetchone()
            if row is None:
                return error.error_invalid_order_id(order_id)

            order_id = row[0]
            buyer_id = row[1]
            store_id = row[2]

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

            cursor = conn.execute(
                "SELECT balance, password FROM user WHERE user_id = ?;",
                (buyer_id, ))
            row = cursor.fetchone()
            if row is None:
                return error.error_non_exist_user_id(buyer_id)
            balance = row[0]
            if password != row[1]:
                return error.error_authorization_fail()

            cursor = conn.execute(
                "SELECT store_id, user_id FROM user_store WHERE store_id = ?;",
                (store_id, ))
            row = cursor.fetchone()
            if row is None:
                return error.error_non_exist_store_id(store_id)

            seller_id = row[1]

            if not self.user_id_exist(seller_id):
                return error.error_non_exist_user_id(seller_id)

            cursor = conn.execute(
                "SELECT book_id, count, price FROM new_order_detail WHERE order_id = ?;",
                (order_id, ))
            total_price = 0
            for row in cursor:
                count = row[1]
                price = row[2]
                total_price = total_price + price * count

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

            cursor = conn.execute(
                "UPDATE user set balance = balance - ?"
                "WHERE user_id = ? AND balance >= ?",
                (total_price, buyer_id, total_price))
            if cursor.rowcount == 0:
                return error.error_not_sufficient_funds(order_id)

            cursor = conn.execute(
                "UPDATE user set balance = balance + ?"
                "WHERE user_id = ?", (total_price, buyer_id))

            if cursor.rowcount == 0:
                return error.error_non_exist_user_id(buyer_id)

            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)

            conn.commit()

        except sqlite.Error as e:
            return 528, "{}".format(str(e))

        except BaseException as e:
            return 530, "{}".format(str(e))

        return 200, "ok"
Exemplo n.º 26
0
    def payment(self, user_id: str, password: str, order_id: str) -> (int, str):
        conn = self.conn
        try:
            cursor = conn.execute(
                "SELECT order_id, user_id, store_id,total_price,condition FROM new_order WHERE order_id = '%s';" % (
                    order_id))
            row = cursor.fetchone()
            if row is None:
                return error.error_invalid_order_id(order_id)
            order_id = row[0]
            buyer_id = row[1]
            store_id = row[2]
            total_price = row[3]
            condition = row[4]

            if buyer_id != user_id:
                return error.error_authorization_fail()
            time1=time.time()
            if condition != "unpaid":
                #print(condition,order_id)
                #print("oh"+str(time.time()))
                return error.error_unpayable_order(order_id)

            cursor = conn.execute("SELECT balance, password FROM usr WHERE user_id = '%s';" % (buyer_id))
            row = cursor.fetchone()
            if row is None:
                return error.error_non_exist_user_id(buyer_id)
            balance = row[0]
            if password != row[1]:
                return error.error_authorization_fail()

            cursor = conn.execute("SELECT store_id, user_id FROM user_store WHERE store_id ='%s';" % (store_id))
            row = cursor.fetchone()
            if row is None:
                return error.error_non_exist_store_id(store_id)

            seller_id = row[1]

            if not self.user_id_exist(seller_id):
                return error.error_non_exist_user_id(seller_id)

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

            cursor = conn.execute("UPDATE usr set balance = balance - %d "
                                  "WHERE user_id = '%s' AND balance >= %d;"
                                  % (total_price, buyer_id, total_price))

            if cursor.rowcount == 0:
                return error.error_not_sufficient_funds(order_id)

            cursor = conn.execute("UPDATE user_store set s_balance = s_balance + %d "
                                  "WHERE store_id = '%s';"
                                  % (total_price, store_id))

            if cursor.rowcount == 0:
                return error.error_non_exist_store_id(store_id)
            cursor = conn.execute("UPDATE new_order set condition = 'paid' WHERE order_id ='%s';" % (order_id))
            #print (order_id)
            #time2=time.time()
            #print(time2-time1)
           #print("good"+str(time2))
            if cursor.rowcount == 0:
                return error.error_invalid_order_id(order_id)

            conn.commit()

        except sqlalchemy.exc.IntegrityError as e:
            return 528, "{}".format(str(e))

        except BaseException as e:
            print(e)
            return 530, "{}".format(str(e))
        return 200, "ok"
Exemplo n.º 27
0
    def search_all_order_buyer(self, user_id: str, password: str,
                               store_id: str, condition: str):
        try:
            cursor = self.conn.execute(
                "SELECT password FROM usr WHERE user_id='%s';" % (user_id, ))
            row = cursor.fetchone()
            if row is None:
                return error.error_authorization_fail() + ({
                    "order_id": [],
                    "total_price": [],
                    "store_id": [],
                    "condition_id": [],
                    "count": -1
                }, )

            if row[0] != password:
                return error.error_authorization_fail() + ({"count": -1}, )

            store_parameter = ""
            condition_parameter = ""
            if store_id != "":
                store_parameter = "AND store_id = '%s' " % (store_id)

            if condition != "":
                condition_parameter = " AND condition = '%s' " % (condition)

            cursor = self.conn.execute(
                "SELECT order_id, total_price, store_id, condition FROM new_order "
                "WHERE user_id='%s' %s %s;" %
                (user_id, store_parameter, condition_parameter))
            order_id_list = []
            total_price_list = []
            store_id_list = []
            condition_list = []
            count = 0
            for row in cursor:
                order_id_list.append(row[0])
                total_price_list.append(row[1])
                store_id_list.append(row[2])
                condition_list.append(row[3])
                count += 1
        except sqlalchemy.exc.IntegrityError as e:
            return 528, "{}".format(str(e)), {
                "order_id": [],
                "total_price": [],
                "store_id": [],
                "condition_id": [],
                "count": -1
            }
        except BaseException as e:
            # print(e)
            return 530, "{}".format(str(e)), {
                "order_id": [],
                "total_price": [],
                "store_id": [],
                "condition_id": [],
                "count": -1
            }

        return 200, "ok", {
            "order_id": order_id_list,
            "total_price": total_price_list,
            "store_id": store_id_list,
            "condition_id": condition_list,
            "count": count
        }
Exemplo n.º 28
0
    def search_order_detail_buyer(self, user_id: str, password: str,
                                  order_id: str):
        try:
            cursor = self.conn.execute(
                "SELECT password from usr where user_id='%s';" % (user_id, ))
            row = cursor.fetchone()
            if row is None:
                return error.error_authorization_fail() + ({
                    "condition": -1,
                    "result_count": -1
                }, )

            if row[0] != password:
                return error.error_authorization_fail() + ({
                    "condition": -1,
                    "result_count": -1
                }, )

            cursor = self.conn.execute(
                "SELECT  store_id,total_price,condition FROM new_order "
                "WHERE order_id = '%s'AND user_id = '%s';" %
                (order_id, user_id))
            row = cursor.fetchone()
            if row is None:
                return error.error_invalid_order_id(order_id) + (
                    {
                        "condition": -1,
                        "result_count": -1
                    }, )

            store_id = row[0]
            total_price = row[1]
            condition = row[2]

            book_id_list = []
            count_list = []
            price_list = []
            result_count = 0

            cursor = self.conn.execute(
                "SELECT book_id, count, price FROM new_order_detail WHERE order_id = '%s';"
                % (order_id))
            for row in cursor:
                book_id_list.append(row[0])
                count_list.append(row[1])
                price_list.append(row[2])
                result_count += 1

            msg = {
                "order_id": order_id,
                "store_id": store_id,
                "total_price": total_price,
                "condition": condition,
                "book_id": book_id_list,
                "count": count_list,
                "price": price_list,
                "result_count": result_count
            }
        except sqlalchemy.exc.IntegrityError as e:
            return 528, "{}".format(str(e)), {
                "condition": -1,
                "result_count": -1
            }
        except BaseException as e:
            return 530, "{}".format(str(e)), {
                "condition": -1,
                "result_count": -1
            }
        return 200, "ok", msg
Exemplo n.º 29
0
    def payment(self, user_id: str, password: str,
                order_id: str) -> (int, str):
        try:  #查询订单信息中的值
            #查询order_id, user_id, store_id
            self.cursor = self.conn.cursor()
            self.cursor.execute(
                "SELECT order_id, user_id, store_id FROM new_order WHERE order_id = '%s'"
                % (order_id, ))
            row = self.cursor.fetchone()
            if row is None:
                return error.error_invalid_order_id(order_id)

            order_id = row[0]
            buyer_id = row[1]
            store_id = row[2]
            #验证buyer_id是不是相符合
            if buyer_id != user_id:
                return error.error_authorization_fail()
            #查询buyer的账户信息,balance和password
            self.cursor.execute(
                "SELECT balance, password FROM usr WHERE user_id = '%s'" %
                (buyer_id, ))

            row = self.cursor.fetchone()
            if row is None:
                return error.error_non_exist_user_id(buyer_id)
            balance = row[0]
            #验证password是不是相符合
            if password != row[1]:
                return error.error_authorization_fail()
            #查询user_store表,寻找到store_id和user_id的对应关系
            self.cursor.execute(
                "SELECT store_id, user_id FROM user_store WHERE store_id = '%s'"
                % (store_id, ))
            row = self.cursor.fetchone()
            #如果store不存在,就返回store不存在的错误
            if row is None:
                return error.error_non_exist_store_id(store_id)
            #即找到seller_id
            seller_id = row[1]
            #如果seller_id不存在,则返回seller_id不存在的错误
            if not self.user_id_exist(seller_id):
                return error.error_non_exist_user_id(seller_id)
            #在new_order_detail中找到book_id,count,price, 以order_id为索引
            self.cursor.execute(
                "SELECT book_id, count, price FROM new_order_detail WHERE order_id = '%s'"
                % (order_id, ))
            cursor = self.cursor.fetchall()
            total_price = 0
            #计算总价
            for row in cursor:
                count = row[1]
                price = row[2]
                total_price = total_price + price * count
            #如果账户余额小于总价,返回not_sufficent_funds错误
            if balance < total_price:
                return error.error_not_sufficient_funds(order_id)
            #更新用户的账户余额
            self.cursor.execute("UPDATE usr set balance = balance - %d"
                                "WHERE user_id = '%s' AND balance >= %d" %
                                (total_price, buyer_id, total_price))
            #再次检查,如果账户余额小于总价,则返回not_sufficent_funds错误
            if self.cursor.rowcount == 0:
                return error.error_not_sufficient_funds(order_id)
            #更新卖家的账户余额
            self.cursor.execute("UPDATE usr set balance = balance+%d"
                                "WHERE user_id = '%s'" %
                                (total_price, seller_id))  ##
            #检查id是否存在
            if self.cursor.rowcount == 0:
                return error.error_non_exist_user_id(seller_id)
            #从new_order里删除这个order
            self.cursor.execute("DELETE FROM new_order WHERE order_id = '%s'" %
                                (order_id, ))
            if self.cursor.rowcount == 0:
                return error.error_invalid_order_id(order_id)
            #从new_order_detail中删除
            self.cursor.execute(
                "DELETE FROM new_order_detail where order_id = '%s'" %
                (order_id, ))
            if self.cursor.rowcount == 0:
                return error.error_invalid_order_id(order_id)

            self.conn.commit()
        except psycopg2.errors.UniqueViolation:
            return error.error_exist_user_id(user_id)

        return 200, "ok"
Exemplo n.º 30
0
    def payment(self, user_id: str, password: str,
                order_id: str) -> (int, str):
        try:
            cursor = self.session.query(Order).filter_by(
                id=order_id, status=Order_status.pending)
            row = cursor.first()
            if row is None:
                return error.error_invalid_order_id(order_id)

            buyer_id = row.buyer_id
            store_id = row.store_id

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

            cursor = self.session.query(User).filter_by(user_id=buyer_id)
            row = cursor.first()
            if row is None:
                return error.error_non_exist_user_id(buyer_id)
            balance = row.balance
            if password != row.password:
                return error.error_authorization_fail()

            cursor = self.session.query(Store).filter_by(store_id=store_id)
            row = cursor.first()
            if row is None:
                return error.error_non_exist_store_id(store_id)

            seller_id = row.owner

            if self.session.query(User).filter_by(
                    user_id=seller_id).first() is None:
                return error.error_non_exist_user_id(seller_id)

            cursor = self.session.query(Order_info).filter_by(
                order_id=order_id)
            total_price = 0
            for row in cursor.all():
                count = row.count
                price = row.price
                total_price = total_price + price * count

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

            cursor = self.session.query(User).filter(
                User.user_id == buyer_id, User.balance >= total_price)
            rowcount = cursor.update(
                {User.balance: User.balance - total_price})
            if rowcount == 0:
                return error.error_not_sufficient_funds(order_id)

            cursor = self.session.query(User).filter(User.user_id == seller_id)
            rowcount = cursor.update(
                {User.balance: User.balance + total_price})
            if rowcount == 0:
                return error.error_non_exist_user_id(buyer_id)

            cursor = self.session.query(Order).filter(Order.id == order_id)
            rowcount = cursor.update({
                Order.status: Order_status.paid,
                Order.pt: datetime.datetime.now()
            })
            if rowcount == 0:
                return error.error_invalid_order_id(order_id)

            self.session.commit()
            self.session.close()

        except BaseException as e:
            print(e)
            return 530, "{}".format(str(e))

        return 200, "ok"