import 'package:drift/drift.dart'; import 'package:uto_manager/infrastructure/database/app_database.dart'; part 'personnel_dao.g.dart'; /// Data Access Object for [Personnel] table operations. @DriftAccessor(tables: [Personnel]) class PersonnelDao extends DatabaseAccessor with _$PersonnelDaoMixin { PersonnelDao(super.db); /// Returns all personnel rows. Future> getAllPersonnel() => select(personnel).get(); /// Returns the personnel record with [soldierId], or null if not found. Future getPersonnelById(String soldierId) => (select(personnel)..where((t) => t.soldierId.equals(soldierId))).getSingleOrNull(); /// Returns all soldiers assigned to [slotId]. Future> getBySlot(String slotId) => (select(personnel)..where((t) => t.assignedSlotId.equals(slotId))).get(); /// Returns all unassigned soldiers. Future> getUnassigned() => (select(personnel)..where((t) => t.assignedSlotId.isNull())).get(); /// Returns all soldiers with [primaryMos]. Future> getByMos(String primaryMos) => (select(personnel)..where((t) => t.primaryMos.equals(primaryMos))).get(); /// Returns all soldiers at [payGrade]. Future> getByPayGrade(String payGrade) => (select(personnel)..where((t) => t.payGrade.equals(payGrade))).get(); /// Inserts a new personnel row. Future insertPersonnel(PersonnelCompanion entry) => into(personnel).insert(entry, mode: InsertMode.insertOrFail); /// Inserts multiple personnel rows in a single transaction. Future insertAllPersonnel(List entries) => batch((b) => b.insertAll(personnel, entries, mode: InsertMode.insertOrFail)); /// Updates an existing personnel row. Future updatePersonnel(PersonnelCompanion entry) => update(personnel).replace(entry); /// Deletes the personnel record with [soldierId]. Future deletePersonnel(String soldierId) => (delete(personnel)..where((t) => t.soldierId.equals(soldierId))).go(); /// Assigns [soldierId] to [slotId]. Future assignToSlot(String soldierId, String slotId) async { await (update(personnel)..where((t) => t.soldierId.equals(soldierId))).write( PersonnelCompanion(assignedSlotId: Value(slotId)), ); } /// Clears the slot assignment for [soldierId]. Future unassignFromSlot(String soldierId) async { await (update(personnel)..where((t) => t.soldierId.equals(soldierId))).write( const PersonnelCompanion(assignedSlotId: Value(null)), ); } /// Deletes all personnel records. Future deleteAllPersonnel() => delete(personnel).go(); /// Returns a stream that emits all personnel whenever the table changes. Stream> watchAllPersonnel() => select(personnel).watch(); /// Returns a stream that emits unassigned soldiers on changes. Stream> watchUnassigned() => (select(personnel)..where((t) => t.assignedSlotId.isNull())).watch(); }