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

import 'interfaces/diagnosis.dart';

class DiagnosisRepository
    extends BaseDbRepository<DiagnosisEntity, DiagnosisColumnsDefine>
    implements IDiagnosisRepository {
  DiagnosisRepository(super.database);

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

  @override
  Future<List<DiagnosisEntity>> getListByPatientCode(
    String patientCode, {
    OfflineDataSyncState? syncState,
    required String userCode,
  }) async {
    final query = queryable.where((x) {
      final List<IDbColumnCondition> arr = [];
      arr.add(x.isValid.equals(true));
      arr.add(x.patientCode.equals(patientCode));
      arr.add(x.userCode.equals(userCode));

      ///添加用户code
      if (syncState != null) {
        arr.add(x.syncState.equals(syncState));
      }
      return arr;
    });
    final list =
        await query.orderBy((x) => x.createTime, DbOrderByType.desc).toList();
    return list;
  }

  @override
  Future<int> getNotUploadedCountByPatientCode(
    String patientCode,
    String userCode,
  ) async {
    final query = queryable.where((x) {
      final List<IDbColumnCondition> arr = [];
      arr.add(x.isValid.equals(true));
      arr.add(x.patientCode.equals(patientCode));
      arr.add(x.syncState.notEquals(OfflineDataSyncState.success));
      arr.add(x.userCode.equals(userCode)); //添加用户code
      return arr;
    });
    final listCount = await query.count();
    return listCount;
  }

  @override
  Future<List<DiagnosisEntity>> getNotUploadedListByPatientCode(
    String patientCode,
    String userCode,
  ) async {
    final query = queryable.where((x) {
      final List<IDbColumnCondition> arr = [];
      arr.add(x.isValid.equals(true));
      arr.add(x.patientCode.equals(patientCode));
      arr.add(x.userCode.equals(userCode)); //添加用户code
      arr.add(x.syncState.notEquals(OfflineDataSyncState.success));

      return arr;
    });
    final list =
        await query.orderBy((x) => x.createTime, DbOrderByType.desc).toList();
    return list;
  }
}
