Example #1
0
def clear_shopping_cart(un):
    user_dict = db_handler.get_user_data(un)
    user_dict['shopping_cart'] = {}
    db_handler.save_user_data(un, user_dict)
    msg = f'用户{un}清空购物车。'
    shop_logger.info(msg)
    return True, msg
Example #2
0
def verify_bal(admin_un, un, bal):
    user_dict = db_handler.get_user_data(un)
    user_dict['balance'] = bal
    db_handler.save_user_data(un, user_dict)
    msg = f'管理员{admin_un}将用户{un}的余额修改为{bal}。'
    admin_logger.info(msg)
    return True, msg
Example #3
0
def frozen_acc(admin_un, frozen_un):
    frozen_user_dict = db_handler.get_user_data(frozen_un)
    frozen_user_dict['locked'] = True
    db_handler.save_user_data(frozen_un, frozen_user_dict)
    msg = f'用户{frozen_un}已被管理员{admin_un}冻结'
    admin_logger.info(msg)
    return True, msg
Example #4
0
def repay(un, cash):
    user_dict = db_handler.get_user_data(un)
    bal_float = float(user_dict['balance'])
    cash_float = float(cash)
    bal_float += cash_float
    user_dict['balance'] = bal_float
    db_handler.save_user_data(un, user_dict)
    msg = f'用户{un}还款金额{cash_float},余额为{bal_float},还款成功。'
    bank_logger.info(msg)
    add_bank_details(un, msg)
    return True, msg
Example #5
0
def save_shopping_cart(un, shopping_cart_dict):
    user_dict = db_handler.get_user_data(un)
    user_shopping_cart_dict = user_dict['shopping_cart']
    for each_key, each_value in shopping_cart_dict.items():
        if each_key not in user_shopping_cart_dict:
            user_shopping_cart_dict[each_key] = each_value
        else:
            user_shopping_cart_dict[each_key]['count'] += each_value['count']
    user_dict['shopping_cart'] = user_shopping_cart_dict
    db_handler.save_user_data(un, user_dict)
    msg = f'用户{un}保存购物车成功。'
    shop_logger.info(msg)
    return True, msg
Example #6
0
def add_acc(admin_un, un, pw, bal_int, is_admin):
    user_dict = {
        'user_name': un,
        'password': common.convert_to_md5(pw),
        'is_admin': is_admin,
        'locked': False,
        'balance': bal_int,
        'bank_details': [],
        'shopping_cart': {},
    }
    db_handler.save_user_data(un, user_dict)
    msg = f'管理员{admin_un}注册新用户{un}成功。'
    admin_logger.info(msg)
    return True, msg
Example #7
0
def register(un, pw):
    user_dict = {
        'user_name': un,
        'password': common.convert_to_md5(pw),
        'is_admin': False,
        'locked': False,
        'balance': 50000,
        'bank_details': [],
        'shopping_cart': {},
    }
    db_handler.save_user_data(un, user_dict)
    msg = f'新用户{un}注册成功。'
    user_logger.info(msg)
    return True, msg
Example #8
0
def get_cash(un, cash):
    user_dict = db_handler.get_user_data(un)
    bal_float = float(user_dict['balance'])
    cash_float = float(cash)
    fee_float = cash_float * 0.05  # 手续费
    total_float = cash_float + fee_float  # 总额
    if bal_float < total_float:
        msg = f'用户{un}余额不足,手续费为{fee_float},余额为{bal_float},提现失败。'
        bank_logger.warn(msg)
        return False, msg
    else:
        bal_float -= total_float
        user_dict['balance'] = bal_float
        db_handler.save_user_data(un, user_dict)
        msg = f'用户{un}提取金额{cash_float},手续费为{fee_float},余额为{bal_float},提现成功。'
        bank_logger.info(msg)
        add_bank_details(un, msg)
        return True, msg
Example #9
0
def checkout(un):
    user_dict = db_handler.get_user_data(un)
    bal = user_dict['balance']
    shopping_cart_dict = user_dict['shopping_cart']
    total_cost = 0
    for each_value in shopping_cart_dict.values():
        total_cost += each_value['count'] * each_value['price']
    if bal < total_cost:
        msg = f'用户{un}余额不足,总共消费{total_cost},余额为{bal},结账失败。'
        bank_logger.warn(msg)
        return False, msg
    else:
        bal -= total_cost
        user_dict['balance'] = bal
        user_dict['shopping_cart'] = {}
        db_handler.save_user_data(un, user_dict)
        msg = f'用户{un}总共消费{total_cost},余额为{bal},结账成功。'
        bank_logger.info(msg)
        add_bank_details(un, msg)
        return True, msg
Example #10
0
def transfer(un, target_un, cash):
    user_dict = db_handler.get_user_data(un)
    bal_float = float(user_dict['balance'])
    cash_float = float(cash)
    if bal_float < cash_float:
        msg = f'用户{un}余额不足,余额为{bal_float},转账失败。'
        bank_logger.warn(msg)
        return False, msg
    else:
        bal_float -= cash_float
        user_dict['balance'] = bal_float
        db_handler.save_user_data(un, user_dict)

        target_user_dict = db_handler.get_user_data(target_un)
        target_user_dict['balance'] += cash_float
        db_handler.save_user_data(target_un, target_user_dict)
        msg = f'用户{un}向用户{target_un}转账金额{cash_float},余额为{bal_float},转账成功。'
        bank_logger.info(msg)
        add_bank_details(un, msg)
        return True, msg
Example #11
0
def add_bank_details(un, msg):
    user_dict = db_handler.get_user_data(un)
    user_dict['bank_details'].append(msg)
    db_handler.save_user_data(un, user_dict)