Пример #1
0
def lock_accounts(connection, __user_ids=None):
    """
    lock the accounts of the accounts

    :param connection: the connection object
    :param __user_ids: list of {user_id(BIGINT)}
    :returns: [("user_id",)]
    """

    @transaction
    @coroutine
    def __query(__connection):
        __cursor = __connection.cursor()
        try:
            if not __user_ids:
                return
            __args = ((x.get(y, None) for y in ("user_id",)) for x in __user_ids)
            yield from __cursor.execute(b"DROP TEMPORARY TABLE IF EXISTS `__user_ids`;")
            yield from __cursor.execute(b"CREATE TEMPORARY TABLE `__user_ids`(`user_id` BIGINT) ENGINE=MEMORY;")
            yield from __cursor.executemany(b"INSERT INTO `__user_ids` (`user_id`) VALUES (%s);", __args)
            yield from __cursor.callproc(b"accounts::lock_accounts", ())
            return (yield from __cursor.fetchxall())
        finally:
            yield from __cursor.close()

    try:
        return (yield from connection.execute(__query))
    except Error as e:
        raise handle_error(exceptions, e)
Пример #2
0
def chargeback(connection, user_id=None, amount=None):
    """
    chargeback the accounts

    :param connection: the connection object
    :param user_id: the id of user(BIGINT, IN)
    :param amount: the amount(DECIMAL(28, 8), IN)
    :returns: ("credit",)
    :raises: NotFound
    """

    @transaction
    @coroutine
    def __query(__connection):
        __cursor = __connection.cursor()
        try:
            yield from __cursor.callproc(b"accounts::chargeback", (user_id, amount))
            return (yield from __cursor.fetchxall())[0]
        finally:
            yield from __cursor.close()

    try:
        return (yield from connection.execute(__query))
    except Error as e:
        raise handle_error(exceptions, e)
Пример #3
0
def finish_transaction(connection,
                       transaction_id=None,
                       imi_amount=None,
                       status=None,
                       details=None):
    """
    finish the transaction of the payments

    :param connection: the connection object
    :param transaction_id: the id of transaction(BIGINT, IN)
    :param imi_amount: the amount of imi(DECIMAL(28, 8), IN)
    :param status: the status(INTEGER, IN)
    :param details: the details(VARCHAR(2048), IN)
    :raises: Forbidden, NotFound
    """
    @transaction
    @coroutine
    def __query(__connection):
        __cursor = __connection.cursor()
        try:
            yield from __cursor.callproc(
                b"payments::finish_transaction",
                (transaction_id, imi_amount, status, details))
        finally:
            yield from __cursor.close()

    try:
        return (yield from connection.execute(__query))
    except Error as e:
        raise handle_error(exceptions, e)
Пример #4
0
def get_payments_by_class(connection,
                          user_id=None,
                          class_name=None,
                          lower_bound=None):
    """
    get the payments of by of class of the journal

    :param connection: the connection object
    :param user_id: the id of user(BIGINT, IN)
    :param class_name: the name of class(VARCHAR(20), IN)
    :param lower_bound: the bound of lower(BIGINT, IN)
    :returns: [("amount", "id", "object_class", "object_id", "other_user_id", "status", "timepoint",
    "user_id")]
    """
    @transaction
    @coroutine
    def __query(__connection):
        __cursor = __connection.cursor()
        try:
            yield from __cursor.callproc(b"journal::get_payments_by_class",
                                         (user_id, class_name, lower_bound))
            return (yield from __cursor.fetchxall())
        finally:
            yield from __cursor.close()

    try:
        return (yield from connection.execute(__query))
    except Error as e:
        raise handle_error(exceptions, e)
Пример #5
0
def put(connection,
        object_class=None,
        object_id=None,
        from_user_id=None,
        to_user_id=None,
        amount=None,
        status=None):
    """
    put the journal

    :param connection: the connection object
    :param object_class: the class of object(VARCHAR(24), IN)
    :param object_id: the id of object(BIGINT, IN)
    :param from_user_id: the id of user of from(BIGINT, IN)
    :param to_user_id: the id of user of to(BIGINT, IN)
    :param amount: the amount(DECIMAL(28, 8), IN)
    :param status: the status(TINYINT, IN)
    """
    @transaction
    @coroutine
    def __query(__connection):
        __cursor = __connection.cursor()
        try:
            yield from __cursor.callproc(
                b"journal::put", (object_class, object_id, from_user_id,
                                  to_user_id, amount, status))
        finally:
            yield from __cursor.close()

    try:
        return (yield from connection.execute(__query))
    except Error as e:
        raise handle_error(exceptions, e)
Пример #6
0
def get_all(connection, user_id=None, upper_bound=None, max_count=None):
    """
    get the all of the journal

    :param connection: the connection object
    :param user_id: the id of user(BIGINT, IN)
    :param upper_bound: the bound of upper(BIGINT, IN)
    :param max_count: the count of max(INTEGER, IN)
    :returns: [("amount", "id", "other_user_id", "timepoint", "user_id")]
    """
    @transaction
    @coroutine
    def __query(__connection):
        __cursor = __connection.cursor()
        try:
            yield from __cursor.callproc(b"journal::get_all",
                                         (user_id, upper_bound, max_count))
            return (yield from __cursor.fetchxall())
        finally:
            yield from __cursor.close()

    try:
        return (yield from connection.execute(__query))
    except Error as e:
        raise handle_error(exceptions, e)
Пример #7
0
def put(connection, user_id=None, user_email=None):
    """
    put the accounts

    :param connection: the connection object
    :param user_id: the id of user(BIGINT, IN)
    :param user_email: the email of user(VARCHAR(255), IN)
    :returns: ("user_id",)
    :raises: Conflict
    """

    @transaction
    @coroutine
    def __query(__connection):
        __cursor = __connection.cursor()
        try:
            yield from __cursor.callproc(b"accounts::put", (user_id, user_email))
            return (yield from __cursor.fetchxall())[0]
        finally:
            yield from __cursor.close()

    try:
        return (yield from connection.execute(__query))
    except Error as e:
        raise handle_error(exceptions, e)
Пример #8
0
def put(connection, user_id=None, amount=None, account_info=None):
    """
    put the withdraw_queue

    :param connection: the connection object
    :param user_id: the id of user(BIGINT, IN)
    :param amount: the amount(DECIMAL(28, 8), IN)
    :param account_info: the info of account(VARCHAR(2048), IN)
    :returns: ("id",)
    :raises: NotEnoughMoney, NotFound
    """
    @transaction
    @coroutine
    def __query(__connection):
        __cursor = __connection.cursor()
        try:
            yield from __cursor.callproc(b"withdraw_queue::put",
                                         (user_id, amount, account_info))
            return (yield from __cursor.fetchxall())[0]
        finally:
            yield from __cursor.close()

    try:
        return (yield from connection.execute(__query))
    except Error as e:
        raise handle_error(exceptions, e)
Пример #9
0
    def wrapped(connection, *args, **kwargs):
        @transaction
        @asyncio.coroutine
        def __query(__connection):
            return (yield from func(__connection, *args, **kwargs))

        try:
            return (yield from connection.execute(__query))
        except Error as e:
            raise handle_error(exceptions, e)
Пример #10
0
    def wrapped(connection, *args):
        @transaction
        @asyncio.coroutine
        def __query(__connection):
            __cursor = __connection.cursor()
            try:
                yield from __cursor.execute(q, args)
                return (yield from __cursor.fetchxall())
            finally:
                yield from __cursor.close()

        try:
            return (yield from connection.execute(__query))
        except Error as e:
            raise handle_error(exceptions, e)
Пример #11
0
def adjust_total_balance(connection, amount=None):
    """
    adjust the total of balance of the accounts

    :param connection: the connection object
    :param amount: the amount(DECIMAL(28, 8), IN)
    """

    @transaction
    @coroutine
    def __query(__connection):
        __cursor = __connection.cursor()
        try:
            yield from __cursor.callproc(b"accounts::adjust_total_balance", (amount,))
        finally:
            yield from __cursor.close()

    try:
        return (yield from connection.execute(__query))
    except Error as e:
        raise handle_error(exceptions, e)
Пример #12
0
def start_transaction(connection,
                      user_id=None,
                      imi_amount=None,
                      card_number=None,
                      merchant_id=None,
                      details=None,
                      black_list_tolerance=None,
                      tries_per_day=None):
    """
    start the transaction of the payments

    :param connection: the connection object
    :param user_id: the id of user(BIGINT, IN)
    :param imi_amount: the amount of imi(DECIMAL(28, 8), IN)
    :param card_number: the number of card(VARCHAR(255), IN)
    :param merchant_id: the id of merchant(VARCHAR(255), IN)
    :param details: the details(VARCHAR(2048), IN)
    :param black_list_tolerance: the tolerance of list of black(INTEGER, IN)
    :param tries_per_day: the day of per of tries(INTEGER, IN)
    :returns: ("id",)
    :raises: AccountBlocked, Forbidden, NotFound
    """
    @transaction
    @coroutine
    def __query(__connection):
        __cursor = __connection.cursor()
        try:
            yield from __cursor.callproc(
                b"payments::start_transaction",
                (user_id, imi_amount, card_number, merchant_id, details,
                 black_list_tolerance, tries_per_day))
            return (yield from __cursor.fetchxall())[0]
        finally:
            yield from __cursor.close()

    try:
        return (yield from connection.execute(__query))
    except Error as e:
        raise handle_error(exceptions, e)
Пример #13
0
def get_per_day(connection):
    """
    get the per of day of the statistics

    :param connection: the connection object
    :returns: ("balance.credit", "balance.debit", "balance.total", "withdraw_queue.size")
    :raises: InternalError
    """
    @transaction
    @coroutine
    def __query(__connection):
        __cursor = __connection.cursor()
        try:
            yield from __cursor.callproc(b"statistics::get_per_day", ())
            return (yield from __cursor.fetchxall())[0]
        finally:
            yield from __cursor.close()

    try:
        return (yield from connection.execute(__query))
    except Error as e:
        raise handle_error(exceptions, e)
Пример #14
0
def complete(connection, record_id=None):
    """
    complete the withdraw_queue

    :param connection: the connection object
    :param record_id: the id of record(BIGINT, IN)
    :raises: InternalError, NotFound
    """
    @transaction
    @coroutine
    def __query(__connection):
        __cursor = __connection.cursor()
        try:
            yield from __cursor.callproc(b"withdraw_queue::complete",
                                         (record_id, ))
        finally:
            yield from __cursor.close()

    try:
        return (yield from connection.execute(__query))
    except Error as e:
        raise handle_error(exceptions, e)
Пример #15
0
def block(connection, user_id=None):
    """
    block the accounts

    :param connection: the connection object
    :param user_id: the id of user(BIGINT, IN)
    :raises: NotFound
    """

    @transaction
    @coroutine
    def __query(__connection):
        __cursor = __connection.cursor()
        try:
            yield from __cursor.callproc(b"accounts::block", (user_id,))
        finally:
            yield from __cursor.close()

    try:
        return (yield from connection.execute(__query))
    except Error as e:
        raise handle_error(exceptions, e)
Пример #16
0
def clean(connection, retention_period=None):
    """
    clean the payments

    :param connection: the connection object
    :param retention_period: the period of retention(INTEGER, IN)
    :returns: ("count",)
    """
    @transaction
    @coroutine
    def __query(__connection):
        __cursor = __connection.cursor()
        try:
            yield from __cursor.callproc(b"payments::clean",
                                         (retention_period, ))
            return (yield from __cursor.fetchxall())[0]
        finally:
            yield from __cursor.close()

    try:
        return (yield from connection.execute(__query))
    except Error as e:
        raise handle_error(exceptions, e)
Пример #17
0
def get_for_user(connection, user_id=None):
    """
    get the for of user of the withdraw_queue

    :param connection: the connection object
    :param user_id: the id of user(BIGINT, IN)
    :returns: [("account_info", "amount", "id", "status", "timepoint", "user_id")]
    """
    @transaction
    @coroutine
    def __query(__connection):
        __cursor = __connection.cursor()
        try:
            yield from __cursor.callproc(b"withdraw_queue::get_for_user",
                                         (user_id, ))
            return (yield from __cursor.fetchxall())
        finally:
            yield from __cursor.close()

    try:
        return (yield from connection.execute(__query))
    except Error as e:
        raise handle_error(exceptions, e)
Пример #18
0
def get_all(connection, lower_bound=None, max_count=None):
    """
    get the all of the payments

    :param connection: the connection object
    :param lower_bound: the bound of lower(BIGINT, IN)
    :param max_count: the count of max(INTEGER, IN)
    :returns: [("details", "id", "imi_amount", "merchant_id", "status", "timepoint", "user_id")]
    """
    @transaction
    @coroutine
    def __query(__connection):
        __cursor = __connection.cursor()
        try:
            yield from __cursor.callproc(b"payments::get_all",
                                         (lower_bound, max_count))
            return (yield from __cursor.fetchxall())
        finally:
            yield from __cursor.close()

    try:
        return (yield from connection.execute(__query))
    except Error as e:
        raise handle_error(exceptions, e)
Пример #19
0
def get_one(connection, transaction_id=None):
    """
    get the one of the payments

    :param connection: the connection object
    :param transaction_id: the id of transaction(BIGINT, IN)
    :returns: ("details", "id", "imi_amount", "merchant_id", "status", "timepoint")
    :raises: NotFound
    """
    @transaction
    @coroutine
    def __query(__connection):
        __cursor = __connection.cursor()
        try:
            yield from __cursor.callproc(b"payments::get_one",
                                         (transaction_id, ))
            return (yield from __cursor.fetchxall())[0]
        finally:
            yield from __cursor.close()

    try:
        return (yield from connection.execute(__query))
    except Error as e:
        raise handle_error(exceptions, e)
Пример #20
0
def get(connection, user_id=None):
    """
    get the accounts

    :param connection: the connection object
    :param user_id: the id of user(BIGINT, IN)
    :returns: ("balance", "frozen", "status", "user_id")
    :raises: NotFound
    """

    @transaction
    @coroutine
    def __query(__connection):
        __cursor = __connection.cursor()
        try:
            yield from __cursor.callproc(b"accounts::get", (user_id,))
            return (yield from __cursor.fetchxall())[0]
        finally:
            yield from __cursor.close()

    try:
        return (yield from connection.execute(__query))
    except Error as e:
        raise handle_error(exceptions, e)
Пример #21
0
def rollback(connection, transaction_id=None):
    """
    rollback the payments

    :param connection: the connection object
    :param transaction_id: the id of transaction(BIGINT, IN)
    :returns: ("amount", "id", "journal.id", "status", "user_id")
    :raises: InternalError, NotFound
    """
    @transaction
    @coroutine
    def __query(__connection):
        __cursor = __connection.cursor()
        try:
            yield from __cursor.callproc(b"payments::rollback",
                                         (transaction_id, ))
            return (yield from __cursor.fetchxall())[0]
        finally:
            yield from __cursor.close()

    try:
        return (yield from connection.execute(__query))
    except Error as e:
        raise handle_error(exceptions, e)