65 lines
2 KiB
Python
65 lines
2 KiB
Python
# coding: utf-8
|
||
|
||
from SWSCloudCore import models
|
||
|
||
|
||
class ControllerBilling:
|
||
"""
|
||
Класс работы с деньгами
|
||
"""
|
||
def get(self, user_id):
|
||
"""
|
||
Выбираем состояние баланса выбранного пользователя
|
||
"""
|
||
x = models.UsersBalance.select(
|
||
models.UsersBalance.balance
|
||
).where(
|
||
models.UsersBalance.user == user_id
|
||
).get()
|
||
return x.balance
|
||
|
||
def increase(self, user_id, amount):
|
||
"""
|
||
Обновление баланса
|
||
"""
|
||
balance = models.UsersBalance.get(models.UsersBalance.user == user_id)
|
||
balance.balance += float(amount)
|
||
balance.save()
|
||
return True
|
||
|
||
def reduce(self, user_id, amount):
|
||
"""
|
||
Обновление баланса
|
||
"""
|
||
balance = models.UsersBalance.get(models.UsersBalance.user == user_id)
|
||
balance.balance -= float(amount)
|
||
balance.save()
|
||
return True
|
||
|
||
def exists(self, user_id):
|
||
"""
|
||
Проверяет наличие записи в таблице балансов
|
||
"""
|
||
cnt = models.UsersBalance.select().where(
|
||
models.UsersBalance.user == user_id
|
||
).count()
|
||
if cnt == 0:
|
||
return False
|
||
return True
|
||
|
||
def create(self, user_id, balance=0):
|
||
"""
|
||
Создание записи в таблице
|
||
"""
|
||
models.UsersBalance.create(user=user_id, balance=balance)
|
||
return True
|
||
|
||
def transactions_get(self, user_id):
|
||
transactions = dict()
|
||
transactions['total'] = models.UsersBalanceTransactions.select().where(
|
||
models.UsersBalanceTransactions.user == user_id
|
||
).count()
|
||
transactions['items'] = models.UsersBalanceTransactions.select().where(
|
||
models.UsersBalanceTransactions.user == user_id
|
||
).order_by(models.UsersBalanceTransactions.created.desc())
|
||
return transactions
|