def register_user_api(name, pwd, credit=15000): user_info = { 'name': name, 'pwd': pwd, 'lock': False, 'balance': 0, 'credit': credit } db_handler.write(user_info)
def transfer(name, payee, amount): dt = get_datetime() info_tran = db_handler.read(name) if info_tran['balance'] < amount: return False, '用户%s账户余额不足,转账失败!' % name info_tran['balance'] -= amount info_tran['flow'].append((dt, '用户%s转账%s给用户%s' % (name, amount, payee))) info_payee = db_handler.read(payee) info_payee['balance'] += amount info_payee['flow'].append((dt, '用户%s收款%s从用户%s' % (payee, amount, name))) if db_handler.write(info_tran) and db_handler.write(info_payee): return True, '用户%s转账%s给用户%s成功!' % (name, amount, payee) else: return False, '用户%s转账%s给用户%s失败!' % (name, amount, payee)
def recharge(name, amount): dt = get_datetime() info = db_handler.read(name) info['balance'] += amount info['flow'].append((dt, '用户%s充值%s元' % (name, amount))) if db_handler.write(info): return True, '用户%s充值%s元成功!' % (name, amount) else: return False, '用户%s充值%s元失败!' % (name, amount)
def new_arrival(code, name, price, good_file='goods'): good_info = db_handler.read(good_file) if not good_info: good_info = {'name': good_file} good_info[code] = { 'name': name, 'price': price } if db_handler.write(good_info): return True, '商品%s新品上架成功!' % name else: return False, '商品%s新品上架失败!' % name
def withdraw(name, amount, charge=0.05): dt = get_datetime() info = db_handler.read(name) if info['credit_balance'] < (amount + amount * charge): return False, '用户%s信用额度不足,取现失败!' % name info['credit_balance'] -= (amount + amount * charge) info['bill'] += (amount + amount * charge) info['balance'] += amount info['flow'].append((dt, '用户%s取现%s元' % (name, amount))) if db_handler.write(info): return True, '用户%s取现%s元成功!' % (name, amount) else: return False, '用户%s取现%s元失败!' % (name, amount)
def join_shopping_cart(name, good, code, price, count): info = db_handler.read(name) if good in info['shopping_cart']: info['shopping_cart'][good]['count'] += count else: info['shopping_cart'][good] = { 'code': code, 'price': price, 'count': count } if db_handler.write(info): return True, '商品 %s x %s 加入购物车成功!' % (good, count) else: return False, '商品 %s x %s 加入购物车失败!' % (good, count)
def repay(name, amount): dt = get_datetime() info = db_handler.read(name) info['balance'] -= amount if info['bill'] <= amount: info['bill'] = 0 else: info['bill'] -= amount info['credit_balance'] += amount info['flow'].append((dt, '用户%s还款%s元' % (name, amount))) if db_handler.write(info): return True, '用户%s还款%s元成功!' % (name, amount) else: return False, '用户%s还款%s元失败!' % (name, amount)
def register(name, password, credit_limit=15000): info = db_handler.read(name) if info: return False, '用户%s已存在,不能重复注册!' % name info = { 'name': name, 'password': password, 'balance': 0, 'credit_balance': credit_limit, 'credit_limit': credit_limit, 'bill': 0, 'shopping_cart': {}, 'flow': [] } if db_handler.write(info): return True, '用户%s注册成功!' % name else: return False, '用户%s注册失败!' % name
def pay(name): info = db_handler.read(name) shopping_cart = info['shopping_cart'] if not shopping_cart: return False, '用户%s购物车列表为空' % name cost = 0 for k, v in shopping_cart.items(): cost += (v['price'] * v['count']) if (info['balance'] + info['credit_balance']) < cost: return False, '用户%s账户余额不足,结账失败!' % name if info['balance'] >= cost: info['balance'] -= cost if (info['balance'] + info['credit_balance']) >= cost: if info['balance'] != 0: info['balance'] = 0 info['credit_balance'] -= (cost - info['balance']) info['bill'] += (cost - info['balance']) if db_handler.write(info): return True, '用户%s付款%s元,结账成功!' else: return False, '系统异常,结账失败!' % name
def write(self): db_handler.write(self)
def modify_user_api(user_info): db_handler.write(user_info)