Some checks are pending
Backend CI / test (push) Waiting to run
Backend CI / build (push) Blocked by required conditions
Deploy Mnemo Cards / Deploy Backend (push) Waiting to run
Deploy Mnemo Cards / Deploy Web App (push) Blocked by required conditions
Deploy Mnemo Cards / Final Verification (push) Blocked by required conditions
133 lines
3.9 KiB
Dart
133 lines
3.9 KiB
Dart
import 'package:drift/drift.dart';
|
||
import '../database.dart';
|
||
import '../tables/payments.dart';
|
||
import '../tables/users.dart';
|
||
|
||
part 'payment_dao.g.dart';
|
||
|
||
@DriftAccessor(tables: [Payments])
|
||
class PaymentDao extends DatabaseAccessor<AppDatabase> with _$PaymentDaoMixin {
|
||
PaymentDao(super.db);
|
||
|
||
/// Получить платеж по ID
|
||
Future<Payment?> getPaymentById(int id) {
|
||
return (select(payments)..where((p) => p.id.equals(id))).getSingleOrNull();
|
||
}
|
||
|
||
/// Получить платежи пользователя
|
||
Future<List<Payment>> getPaymentsByUserId(int userId, {
|
||
int? limit,
|
||
int? offset,
|
||
}) {
|
||
final query = select(payments)
|
||
..where((p) => p.userId.equals(userId))
|
||
..orderBy([(p) => OrderingTerm.desc(p.date)]);
|
||
|
||
if (limit != null) {
|
||
query.limit(limit, offset: offset);
|
||
}
|
||
|
||
return query.get();
|
||
}
|
||
|
||
/// Получить платежи по статусу
|
||
Future<List<Payment>> getPaymentsByStatus(String status) {
|
||
return (select(payments)
|
||
..where((p) => p.status.equals(status))
|
||
..orderBy([(p) => OrderingTerm.desc(p.date)])
|
||
).get();
|
||
}
|
||
|
||
/// Создать платеж
|
||
Future<int> createPayment(PaymentsCompanion payment) {
|
||
return into(payments).insert(payment);
|
||
}
|
||
|
||
/// Обновить платеж
|
||
Future<bool> updatePayment(Payment payment) {
|
||
return update(payments).replace(payment);
|
||
}
|
||
|
||
/// Обновить платеж частично
|
||
Future<void> updatePaymentCompanion(PaymentsCompanion companion) {
|
||
final paymentId = companion.id.value;
|
||
if (paymentId == null) throw ArgumentError('Payment ID is required');
|
||
|
||
return (update(payments)..where((p) => p.id.equals(paymentId)))
|
||
.write(companion);
|
||
}
|
||
|
||
/// Обновить статус платежа
|
||
Future<void> updatePaymentStatus(int paymentId, String status) {
|
||
return (update(payments)..where((p) => p.id.equals(paymentId)))
|
||
.write(PaymentsCompanion(
|
||
status: Value(status),
|
||
updatedAt: Value(DateTime.now()),
|
||
));
|
||
}
|
||
|
||
/// Подсчитать платежи пользователя
|
||
Future<int> countPaymentsByUserId(int userId) async {
|
||
final countExpr = payments.id.count();
|
||
final query = selectOnly(payments)
|
||
..addColumns([countExpr])
|
||
..where(payments.userId.equals(userId));
|
||
|
||
return await query.map((row) => row.read(countExpr)!).getSingle();
|
||
}
|
||
|
||
/// Получить платеж по externalToken
|
||
Future<Payment?> getPaymentByExternalToken(String token) {
|
||
return (select(payments)..where((p) => p.externalToken.equals(token)))
|
||
.getSingleOrNull();
|
||
}
|
||
|
||
/// Получить платежи по продукту (store ID)
|
||
Future<List<Payment>> getPaymentsByProduct(String productId) async {
|
||
// Поиск по продуктам в JSON массиве
|
||
final query = select(payments)
|
||
..where((p) => p.products.like('%$productId%'));
|
||
|
||
return query.get();
|
||
}
|
||
|
||
/// Получить платежи по статусу с пагинацией
|
||
Future<List<Payment>> getPaymentsByStatusPaged(
|
||
String status, {
|
||
int? limit,
|
||
int? offset,
|
||
}) {
|
||
final query = select(payments)
|
||
..where((p) => p.status.equals(status))
|
||
..orderBy([(p) => OrderingTerm.desc(p.date)]);
|
||
|
||
if (limit != null) {
|
||
query.limit(limit, offset: offset);
|
||
}
|
||
|
||
return query.get();
|
||
}
|
||
|
||
/// Получить все платежи (для админки)
|
||
Future<List<Payment>> getAllPayments({
|
||
int? limit,
|
||
int? offset,
|
||
}) {
|
||
final query = select(payments)
|
||
..orderBy([(p) => OrderingTerm.desc(p.date)]);
|
||
|
||
if (limit != null) {
|
||
query.limit(limit, offset: offset);
|
||
}
|
||
|
||
return query.get();
|
||
}
|
||
|
||
/// Подсчитать все платежи
|
||
Future<int> countAllPayments() async {
|
||
final countExpr = payments.id.count();
|
||
final query = selectOnly(payments)..addColumns([countExpr]);
|
||
|
||
return await query.map((row) => row.read(countExpr)!).getSingle();
|
||
}
|
||
}
|