Beispiel #1
0
def query_by_user_id(user_id):
    """
    @api {GET} /account/query_by_id/<user_id> 根据用户id查询用户账户信息
    @apiGroup Users
    @apiVersion 0.0.1
    @apiDescription 根据用户id查询用户账户信息
    @apiSuccess (200) {String} msg 信息
    @apiSuccess (200) {int} code 0 代表无错误 1代表有错误
    @apiSuccessExample {json} 返回样例:
                   {
                        "status": "ok",
                        "payload":{
                            "user_id": "132",
                            "balance": "1283.23",
                            "bonus_point": "3000",
                            "discount": "0.75"
                        }
                    }
    @apiError (400) {String} msg 信息
    @apiErrorExample {json} 返回样例:
                   {"status": "fail", "message": "用户不存在"}
    """
    account_info = AccountService.account_query(user_id=user_id)

    if account_info:
        return make_api_response(payload=account_info)
    else:
        return make_api_response(message="用户不存在", statusCode=400)
Beispiel #2
0
def query_user_info():
    """
    @api {GET} /account/query 查询用户账户信息
    @apiGroup Users
    @apiVersion 0.0.1
    @apiDescription 用于查询用户资料
    @apiSuccess (200) {String} msg 信息
    @apiSuccess (200) {int} code 0 代表无错误 1代表有错误
    @apiSuccessExample {json} 返回样例:
                   {
                        "status": "ok",
                        "payload":{
                            "realname": "132",
                            "username": "******",
                            "phone": "pwd",
                            "mail": "*****@*****.**",
                            "nickname": "guest",
                            "gender": "23",
                            "password": "******",
                            "qq": "12312"
                        }
                    }
    @apiError (400) {String} msg 信息
    @apiErrorExample {json} 返回样例:
                   {"status": "fail", "message": "用户不存在"}
    """
    user_id = current_user.id
    app.logger.info(user_id)
    account_info = AccountService.account_query(user_id=user_id)

    if account_info:
        return make_api_response(payload=account_info)
    else:
        return make_api_response(message="用户不存在", statusCode=400)
Beispiel #3
0
def query_account_refund():
    user_id = current_user.id

    account_service = AccountService()
    rvs = account_service.account_refund_query(user_id)

    if not rvs:
        return make_api_response()

    payload = {}
    for rv in rvs:
        consume = {}
        consume[rv.id] = rv.id
        consume[rv.amount] = rv.amount
        consume[rv.current_balance] = rv.current_balance

        payload[rv.id] = consume

    return payload
Beispiel #4
0
def query_user_account_log():
    """
    @api {GET} /accountlog/query 查询用户账户信息
    @apiGroup Users
    @apiVersion 0.0.1
    @apiDescription 用于查询用户消费记录
    @apiSuccess (200) {String} msg 信息
    @apiSuccess (200) {int} code 0 代表无错误 1代表有错误
    @apiSuccessExample {json} 返回样例:
                    {
                        "status": "ok", 
                        "payload": {
                            "2018-02-17 04:54:30": {
                            "created_at": "2018-02-17 04:54:30", 
                            "user_id": 4, 
                            "current_balance": 14380.0, 
                            "amount": 4380.0, 
                            "type": "充值"
                            }, 
                            "2018-02-17 04:58:41": {
                            "created_at": "2018-02-17 04:58:41", 
                            "user_id": 4, 
                            "current_balance": 14180.0, 
                            "amount": 150.0, 
                            "type": "退款"
                            }
                        }
                    }
    @apiError (400) {String} msg 信息
    @apiErrorExample {json} 返回样例:
                   {"status": "fail", "message": "用户不存在"}
    """
    user_id = current_user.id
    if not user_id:
        return make_api_response(message="用户不存在", statusCode=400)

    account_service = AccountService()
    account_logs = account_service.account_log_query(user_id=user_id)

    return make_api_response(payload=account_logs)
Beispiel #5
0
def account_prepay(user_id, amount):
    """
    @api {GET} /account/prepay/<user_id>/<amount> 用户充值
    @apiGroup Users
    @apiVersion 0.0.1
    @apiDescription 用户充值
    @apiSuccess (200) {String} msg 信息
    @apiSuccess (200) {int} code 0 代表无错误 1代表有错误
    """

    account_service = AccountService()
    account = account_service.account_query(user_id)

    if not account:
        return make_api_response(message='用户id不存在', statusCode=400)

    rv = account_service.account_prepay(user_id, int(amount))
    if rv is False:
        db.session.rollback()
        return make_api_response(statusCode=200, message='操作失败')

    db.session.commit()

    return make_api_response()
Beispiel #6
0
    def order_create(user_id, address_id):
        """
        新建订单
        """
        account_info = AccountService.account_query(user_id)
        cart = CartService()
        total_info = cart.cart_total(user_id)
        if account_info['balance'] < float(total_info['actual_cost']):
            return False, "余额不足"

        # 查询购物车
        books = cart.cart_info_query(user_id)
        if not books:
            return False, "购物车为空"

        # order 表
        order = Order()
        order_id = OrderService.generate_order_id()
        order.order_id = order_id
        order.user_id = user_id
        order.quantity = total_info['total_quantity']
        order.origin_cost = total_info['origin_cost']
        order.actual_cost = total_info['actual_cost']
        order.order_status = 1
        order.delivery_status = 0
        order.pay_status = 1

        db.session.add(order)
        db.session.flush()

        for book in books.values():
            # order_detail 表
            order_detail = OrderDetail()
            order_detail.order_id = order_id
            order_detail.book_id = book['book_id']
            order_detail.book_name = book['book_name']
            order_detail.isbn = book['isbn']
            order_detail.origin_price = book['origin_price'] * \
                book['order_quantity']
            order_detail.actual_price = book['actual_price'] * \
                book['order_quantity']
            order_detail.supplier_id = book['supplier_id']
            order_detail.warehouse = book['supplier']
            order_detail.discount = book['discount']
            order_detail.order_quantity = book['order_quantity']
            order_detail.deliveried_quantity = 0

            db.session.add(order_detail)
            db.session.flush()

            # book 表修改库存书
            book_service = BookService()
            rowcount = book_service.book_quantity_update(
                book['book_id'], -book['order_quantity'])

            if rowcount is False:
                db.session.rollback()
                return False, "book_id : %s 库存不足,下单失败" % book['book_id']

        # order_info 表
        if not address_id:
            return False, "缺少收货地址id"

        address_service = AddressInfoService()
        address = address_service.address_query_by_id(user_id, address_id)
        order_info = OrderInfo()
        order_info.order_id = order_id
        order_info.consignee = address.name
        order_info.address = address.address
        order_info.post_code = address.post_code
        order_info.phone = address.phone
        db.session.add(order_info)
        db.session.flush()

        # account 表
        account_service = AccountService()
        change = -total_info['actual_cost']
        account_service.account_change(user_id, change)

        # account_consume 表
        balance = account_info['balance'] - float(total_info['actual_cost'])
        account_service.account_consume_add(user_id, total_info['actual_cost'],
                                            balance)

        # 清空购物车
        cart.cart_remove_all(user_id)

        return True, '操作成功'