import 'package:vital_local_database/core/index.dart';
import 'package:vitalapp/database/entities/defines.dart';
import 'package:vitalapp/database/entities/patient.dart';

import 'interfaces/patient.dart';

class PatientRepository
    extends BaseDbRepository<PatientEntity, PatientColumnsDefine>
    implements IPatientRepository {
  PatientRepository(super.database);

  @override
  PatientEntity createEntityInstance() => PatientEntity();

  @override
  Future<PatientEntity?> singleByCode(
    String code,
    String userCode,
  ) async {
    final entity = await queryable
        .where((x) => [
              x.isValid.equals(true),
              x.code.equals(code),
              x.userCode.equals(userCode), //添加用户Code
            ])
        .first();
    return entity;
  }

  @override
  Future<bool> isNotUploadedPatient(
    String code,
    String userCode,
  ) async {
    final entity = await singleByCode(code, userCode);
    return entity != null && entity.syncState != OfflineDataSyncState.success;
  }
}
