import 'package:drift/drift.dart'; import 'package:uto_manager/infrastructure/database/app_database.dart'; part 'position_slot_dao.g.dart'; /// Data Access Object for [PositionSlots] table operations. @DriftAccessor(tables: [PositionSlots]) class PositionSlotDao extends DatabaseAccessor with _$PositionSlotDaoMixin { PositionSlotDao(super.db); /// Returns all position slot rows. Future> getAllSlots() => select(positionSlots).get(); /// Returns the slot with [slotId], or null if not found. Future getSlotById(String slotId) => (select(positionSlots)..where((t) => t.slotId.equals(slotId))).getSingleOrNull(); /// Returns all slots belonging to [unitId]. Future> getSlotsByUnit(String unitId) => (select(positionSlots)..where((t) => t.unitId.equals(unitId))).get(); /// Returns all vacant slots (no assigned personnel). Future> getVacantSlots() => (select(positionSlots)..where((t) => t.assignedPersonnelId.isNull())).get(); /// Returns all key leader slots. Future> getKeyLeaderSlots() => (select(positionSlots)..where((t) => t.isKeyLeader.equals(true))).get(); /// Inserts a new position slot row. Future insertSlot(PositionSlotsCompanion entry) => into(positionSlots).insert(entry, mode: InsertMode.insertOrFail); /// Inserts multiple position slot rows in a single transaction. Future insertAllSlots(List entries) => batch((b) => b.insertAll(positionSlots, entries, mode: InsertMode.insertOrFail)); /// Updates an existing position slot row. Future updateSlot(PositionSlotsCompanion entry) => update(positionSlots).replace(entry); /// Assigns [personnelId] to the slot with [slotId]. Future assignPersonnel(String slotId, String personnelId) async { await (update(positionSlots)..where((t) => t.slotId.equals(slotId))).write( PositionSlotsCompanion(assignedPersonnelId: Value(personnelId)), ); } /// Clears the personnel assignment for the slot with [slotId]. Future clearAssignment(String slotId) async { await (update(positionSlots)..where((t) => t.slotId.equals(slotId))).write( const PositionSlotsCompanion(assignedPersonnelId: Value(null)), ); } /// Deletes the slot with [slotId]. Future deleteSlot(String slotId) => (delete(positionSlots)..where((t) => t.slotId.equals(slotId))).go(); /// Deletes all slots belonging to [unitId]. Future deleteSlotsByUnit(String unitId) => (delete(positionSlots)..where((t) => t.unitId.equals(unitId))).go(); /// Deletes all position slots. Future deleteAllSlots() => delete(positionSlots).go(); /// Returns a stream that emits all slots whenever the table changes. Stream> watchAllSlots() => select(positionSlots).watch(); /// Returns a stream that emits slots for [unitId] on changes. Stream> watchSlotsByUnit(String unitId) => (select(positionSlots)..where((t) => t.unitId.equals(unitId))).watch(); }