mnemo_cards/mnemo_cards_backend/lib/database/tables/discounts.dart

59 lines
2.1 KiB
Dart
Raw Normal View History

2025-12-13 13:27:05 +00:00
import 'package:drift/drift.dart';
import '../converters.dart';
import 'users.dart';
/// Таблица DiscountCampaigns - кампании скидок
class DiscountCampaigns extends Table {
IntColumn get id => integer().autoIncrement()();
TextColumn get name => text().nullable()();
DateTimeColumn get start => dateTime()();
DateTimeColumn get finish => dateTime()();
// Статус (enum as string)
TextColumn get status => text()(); // created, active, expired, disabled
// Теги (JSON array)
TextColumn get tags => text()
.withDefault(const Constant('[]'))
.map(const StringListConverter())();
// Audit
DateTimeColumn get createdAt => dateTime().withDefault(currentDateAndTime)();
DateTimeColumn get updatedAt => dateTime().withDefault(currentDateAndTime)();
BoolColumn get isDeleted => boolean().withDefault(const Constant(false))();
}
/// Таблица Discounts - скидки
class Discounts extends Table {
IntColumn get id => integer().autoIncrement()();
IntColumn get campaignId => integer().references(DiscountCampaigns, #id, onDelete: KeyAction.cascade)();
// Процент скидки (0-100)
RealColumn get discountPercent => real()();
// Продукты (JSON array)
TextColumn get products => text()
.withDefault(const Constant('[]'))
.map(const JsonListConverter())();
// Audit
DateTimeColumn get createdAt => dateTime().withDefault(currentDateAndTime)();
DateTimeColumn get updatedAt => dateTime().withDefault(currentDateAndTime)();
BoolColumn get isDeleted => boolean().withDefault(const Constant(false))();
}
/// Junction table для связи Discounts ↔ UserDatas (many-to-many)
class DiscountUserDatas extends Table {
IntColumn get discountId => integer().references(Discounts, #id, onDelete: KeyAction.cascade)();
IntColumn get userId => integer().references(Users, #id, onDelete: KeyAction.cascade)();
DateTimeColumn get grantedAt => dateTime().withDefault(currentDateAndTime)();
@override
Set<Column> get primaryKey => {discountId, userId};
}
// Конвертеры импортированы из converters.dart