2025-12-13 14:48:00 +00:00
|
|
|
import 'package:drift/drift.dart' as drift;
|
2025-12-13 23:35:14 +00:00
|
|
|
import 'package:drift_postgres/drift_postgres.dart';
|
2025-12-13 14:48:00 +00:00
|
|
|
import 'package:mnemo_cards_backend/database/daos/discount_dao.dart';
|
|
|
|
|
import 'package:mnemo_cards_backend/database/database.dart';
|
|
|
|
|
import 'package:mnemo_cards_common/mnemo_cards_common.dart';
|
|
|
|
|
import 'package:mnemo_cards_common_backend/mnemo_cards_common_backend.dart';
|
|
|
|
|
|
|
|
|
|
/// Extension для конвертации DiscountCampaign (Drift) в DTO
|
|
|
|
|
extension DiscountCampaignToDto on DiscountCampaign {
|
|
|
|
|
Future<DiscountCampaignDto> toDto(DiscountDao discountDao) async {
|
|
|
|
|
final discounts = await discountDao.getDiscountsByCampaignId(id);
|
|
|
|
|
final discountsDto = discounts.map((d) => d.toDto()).toList();
|
2025-12-20 18:26:15 +00:00
|
|
|
|
2025-12-13 14:48:00 +00:00
|
|
|
DiscountCampaignStatus statusEnum;
|
|
|
|
|
switch (status) {
|
|
|
|
|
case 'created':
|
|
|
|
|
statusEnum = DiscountCampaignStatus.created;
|
|
|
|
|
break;
|
|
|
|
|
case 'active':
|
|
|
|
|
statusEnum = DiscountCampaignStatus.active;
|
|
|
|
|
break;
|
|
|
|
|
case 'expired':
|
|
|
|
|
statusEnum = DiscountCampaignStatus.expired;
|
|
|
|
|
break;
|
|
|
|
|
case 'disabled':
|
|
|
|
|
statusEnum = DiscountCampaignStatus.disabled;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
statusEnum = DiscountCampaignStatus.created;
|
|
|
|
|
}
|
2025-12-20 18:26:15 +00:00
|
|
|
|
2025-12-13 14:48:00 +00:00
|
|
|
return DiscountCampaignDto(
|
|
|
|
|
id: id,
|
2025-12-13 23:35:14 +00:00
|
|
|
start: start.dateTime,
|
|
|
|
|
finish: finish.dateTime,
|
2025-12-13 14:48:00 +00:00
|
|
|
status: statusEnum,
|
|
|
|
|
name: name,
|
|
|
|
|
tags: tags,
|
|
|
|
|
discounts: discountsDto,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Extension для конвертации Discount (Drift) в DTO
|
|
|
|
|
extension DiscountToDto on Discount {
|
|
|
|
|
DiscountDto toDto() {
|
2025-12-20 18:26:15 +00:00
|
|
|
final productsList = (products ?? [])
|
|
|
|
|
.map((p) {
|
|
|
|
|
if (p is Map<String, dynamic>) {
|
|
|
|
|
return MnemoCardsProductDto.fromJson(p);
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
})
|
|
|
|
|
.whereType<MnemoCardsProductDto>()
|
|
|
|
|
.toList();
|
|
|
|
|
|
2025-12-13 14:48:00 +00:00
|
|
|
return DiscountDto(
|
|
|
|
|
discountPercent: discountPercent,
|
|
|
|
|
products: productsList,
|
|
|
|
|
id: id.toString(),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Extension для конвертации DiscountCampaignDto в DiscountCampaignsCompanion
|
|
|
|
|
extension DiscountCampaignDtoToCompanion on DiscountCampaignDto {
|
|
|
|
|
DiscountCampaignsCompanion toCompanion() {
|
|
|
|
|
String statusStr;
|
|
|
|
|
switch (status) {
|
|
|
|
|
case DiscountCampaignStatus.created:
|
|
|
|
|
statusStr = 'created';
|
|
|
|
|
break;
|
|
|
|
|
case DiscountCampaignStatus.active:
|
|
|
|
|
statusStr = 'active';
|
|
|
|
|
break;
|
|
|
|
|
case DiscountCampaignStatus.expired:
|
|
|
|
|
statusStr = 'expired';
|
|
|
|
|
break;
|
|
|
|
|
case DiscountCampaignStatus.disabled:
|
|
|
|
|
statusStr = 'disabled';
|
|
|
|
|
break;
|
|
|
|
|
}
|
2025-12-20 18:26:15 +00:00
|
|
|
|
2025-12-13 14:48:00 +00:00
|
|
|
return DiscountCampaignsCompanion.insert(
|
|
|
|
|
id: id != null ? drift.Value(id!) : const drift.Value.absent(),
|
|
|
|
|
name: drift.Value(name),
|
2025-12-13 23:35:14 +00:00
|
|
|
start: PgDateTime(start),
|
|
|
|
|
finish: PgDateTime(finish),
|
2025-12-13 14:48:00 +00:00
|
|
|
status: statusStr,
|
|
|
|
|
tags: drift.Value(tags),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Extension для конвертации DiscountDto в DiscountsCompanion
|
|
|
|
|
extension DiscountDtoToCompanion on DiscountDto {
|
2025-12-13 20:55:50 +00:00
|
|
|
DiscountsCompanion toCompanion(String campaignId) {
|
2025-12-13 14:48:00 +00:00
|
|
|
final productsJson = products.map((p) => p.toJson()).toList();
|
2025-12-20 18:26:15 +00:00
|
|
|
|
2025-12-13 14:48:00 +00:00
|
|
|
return DiscountsCompanion.insert(
|
|
|
|
|
campaignId: campaignId,
|
|
|
|
|
discountPercent: discountPercent,
|
|
|
|
|
products: drift.Value(productsJson),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Helper для конвертации статуса в строку
|
|
|
|
|
String discountCampaignStatusToString(DiscountCampaignModelStatus status) {
|
|
|
|
|
switch (status) {
|
|
|
|
|
case DiscountCampaignModelStatus.created:
|
|
|
|
|
return 'created';
|
|
|
|
|
case DiscountCampaignModelStatus.active:
|
|
|
|
|
return 'active';
|
|
|
|
|
case DiscountCampaignModelStatus.expired:
|
|
|
|
|
return 'expired';
|
|
|
|
|
case DiscountCampaignModelStatus.disabled:
|
|
|
|
|
return 'disabled';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Helper для конвертации строки в статус
|
|
|
|
|
DiscountCampaignModelStatus? discountCampaignStatusFromString(String? status) {
|
|
|
|
|
if (status == null) return null;
|
|
|
|
|
switch (status) {
|
|
|
|
|
case 'created':
|
|
|
|
|
return DiscountCampaignModelStatus.created;
|
|
|
|
|
case 'active':
|
|
|
|
|
return DiscountCampaignModelStatus.active;
|
|
|
|
|
case 'expired':
|
|
|
|
|
return DiscountCampaignModelStatus.expired;
|
|
|
|
|
case 'disabled':
|
|
|
|
|
return DiscountCampaignModelStatus.disabled;
|
|
|
|
|
default:
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|