import 'dart:convert'; import 'package:colorize_logger/colorize_logger.dart'; import 'package:get_it/get_it.dart'; import 'package:http/http.dart' as http; import 'package:ustest/ConsultationList.dart'; import 'dart:typed_data'; import 'AppSettings.dart'; import 'UserService.dart'; import 'package:intl/intl.dart'; class ConsultationService { Future LoadDataAsync() async { var userService = GetIt.instance.get(); final user = await userService.getCurrentUser(); final token = user?.accessToken; final orgCode = user?.organizationCode; var client = http.Client(); var body = '{"jsonrpc": "2.0", "method": "GetPersonDeviceDropdownListAsync", "params": [{"Token": "$token"}], "id": 1 }'; Logger.info('QueryExam http.Client()' + body); var response = await client .post(Uri.parse(AppSettings.host + '/IDeviceService'), body: body); var parsed = decodeResponseBody( 'GetPersonDeviceDropdownListAsync', response.bodyBytes); var datas = parsed['result']; final devices = datas.map((json) => Device.fromJson(json)).toList(); body = '{"jsonrpc": "2.0", "method": "FindOrganizationExpertsAsync", "params": [{"Token": "$token", "OrganizationCode": "$orgCode"}], "id": 1 }'; Logger.info('QueryExam http.Client()' + body); response = await client.post( Uri.parse(AppSettings.host + '/ILiveConsultationService'), body: body); //final response = await post(client, "ILiveConsultationService", // "FindOrganizationExpertsAsync", args); Logger.info('FindOrganizationExpertsAsync response.body' + response.body); parsed = jsonDecode(response.body); datas = parsed['result']; final experts = datas.map((json) => Expert.fromJson(json)).toList() as List; body = '{"jsonrpc": "2.0", "method": "FindScanPositionsAsync", "params": [{"Token": "$token"}], "id": 1 }'; Logger.info('FindScanPositionsAsync http.Client()' + body); response = await client.post( Uri.parse(AppSettings.host + '/ILiveConsultationService'), body: body); parsed = decodeResponseBody('FindScanPositionsAsync', response.bodyBytes); //var data = jsonDecode(parsed['result']); var organSource = parsed['result']; var organs = organSource.map((json) => json.toString()).toList() as List; body = '{"jsonrpc": "2.0", "method": "GetUserListAsync", "params": [{"Token": "$token", "OrganizationCode": "$orgCode","OrganizationQueryType":3,"ExceptSelf":true}], "id": 1 }'; Logger.info('GetUserListAsync http.Client()' + body); response = await client.post(Uri.parse(AppSettings.host + '/IUserService'), body: body); Logger.info('GetUserListAsync response.body' + response.body); parsed = jsonDecode(response.body); datas = parsed['result']; final users = datas.map((json) => Expert.fromJson(json)).toList() as List; var model = new AppConsultationDataModel(experts, devices, organs, users); return model; } decodeResponseBody(String logTag, Uint8List bodyBytes) { var utfString = utf8.decode(bodyBytes); Logger.info('$logTag response.body' + utfString); final parsed = jsonDecode(utfString); return parsed; } Future> FindConsultationsByPageAsync( String id, int? selectedType) async { try { var userService = GetIt.instance.get(); var user = await userService.getCurrentUser(); if (user == null) { return List.empty(); } var token = user?.accessToken; var client = http.Client(); var body = '{"jsonrpc": "2.0", "method": "FindConsultationsByPageAsync", "params": [{"Token": "$token", "PageIndex": 1, "PageSize": 10,"ConsultationQueryType": $selectedType}], "id": 1 }'; Logger.info('GetRecordInfoPagesAsync http.Client()' + body); final response = await client.post( Uri.parse(AppSettings.host + '/ILiveConsultationService'), body: body); Logger.info('FindConsultationsByPageAsync response.body' + response.body); final parsed = jsonDecode(response.body); var datas = parsed['result']['PageData']; var list = datas .map((json) => Consultation.fromJson(json)) .toList(); return list; } catch (ex) { Logger.info('FindConsultationsByPageAsync.to ex' + ex.toString()); } return List.empty(); } Future ApplyConsultationAsync( String expertCode, String deviceCode, String organ, DateTime time) async { var userService = GetIt.instance.get(); var user = await userService.getCurrentUser(); var token = user?.accessToken; var client = http.Client(); var patientCode = "2D6DA689ECC54F52A17F20A05BDF5C27"; //TODO should from UI var organizationCode = "Organization_20221020115006aAjF6l"; DateFormat inputFormat = DateFormat("yyyy-MM-ddTHH:mm:ss"); var utcTime = inputFormat.format(time.toUtc()).toString(); var body = '{"jsonrpc": "2.0", "method": "FindPatientByCodeAsync", "params": [{"Token": "$token", "Code":"$patientCode"}], "id": 1 }'; Logger.info('FindPatientByCodeAsync http.Client()' + body); var response = await client .post(Uri.parse(AppSettings.host + '/IPatientService'), body: body); var parsed = decodeResponseBody('FindPatientByCodeAsync', response.bodyBytes); var data = parsed['result']['PatientData']; var list = data.map((json) => DataItemDTO.fromJson(json)).toList(); var patientDatas = jsonEncode(list); body = '{"jsonrpc": "2.0", "method": "ApplyConsultationAsync", "params": [{"Token": "$token","ApplyUserCode":"3C135B470E6448F6854974D46022F7FD", "PatientCode":"$patientCode", "ExpertOrganizationCode":"$organizationCode", "PatientDatas":$patientDatas, "ExpertUserCode": "$expertCode", "DeviceCode": "$deviceCode", "ScanPosition": "$organ", "ConsultationTime":"$utcTime"}], "id": 1 }'; Logger.info('ApplyConsultationAsync http.Client()' + body); response = await client.post( Uri.parse(AppSettings.host + '/ILiveConsultationService'), body: body); parsed = decodeResponseBody('ApplyConsultationAsync', response.bodyBytes); return true; } Future InitiateLiveConsultationAsync(String consultationCode) async { var userService = GetIt.instance.get(); var user = await userService.getCurrentUser(); var token = user?.accessToken; var client = http.Client(); var body = '{"jsonrpc": "2.0", "method": "InitiateLiveConsultationAsync", "params": [{"Token": "$token", "ConsultationCode":"$consultationCode"}], "id": 1 }'; print('InitiateLiveConsultationAsync http.Client()' + body); final response = await client.post( Uri.parse(AppSettings.host + '/ILiveConsultationService'), body: body); var parsed = decodeResponseBody('InitiateLiveConsultationAsync', response.bodyBytes); var data = parsed['result']; return data; } Future CancelLiveConsultationAsync(String consultationCode) async { var userService = GetIt.instance.get(); var user = await userService.getCurrentUser(); var token = user?.accessToken; var client = http.Client(); var body = '{"jsonrpc": "2.0", "method": "CancelLiveConsultationAsync", "params": [{"Token": "$token", "ConsultationCode":"$consultationCode"}], "id": 1 }'; print('CancelLiveConsultationAsync http.Client()' + body); final response = await client.post( Uri.parse(AppSettings.host + '/ILiveConsultationService'), body: body); var parsed = decodeResponseBody('CancelLiveConsultationAsync', response.bodyBytes); var data = parsed['result']; return data; } Future AcceptLiveConsultationAsync(String consultationCode) async { var userService = GetIt.instance.get(); var user = await userService.getCurrentUser(); var token = user?.accessToken; var client = http.Client(); var body = '{"jsonrpc": "2.0", "method": "AcceptLiveConsultationAsync", "params": [{"Token": "$token", "ConsultationCode":"$consultationCode"}], "id": 1 }'; Logger.info('AcceptLiveConsultationAsync http.Client()' + body); final response = await client.post( Uri.parse(AppSettings.host + '/ILiveConsultationService'), body: body); var parsed = decodeResponseBody('AcceptLiveConsultationAsync', response.bodyBytes); var data = parsed['result']; return data; } Future RejectLiveConsultationAsync(String consultationCode) async { var userService = GetIt.instance.get(); var user = await userService.getCurrentUser(); var token = user?.accessToken; var client = http.Client(); var body = '{"jsonrpc": "2.0", "method": "RejectLiveConsultationAsync", "params": [{"Token": "$token", "ConsultationCode":"$consultationCode"}], "id": 1 }'; Logger.info('RejectLiveConsultationAsync http.Client()' + body); final response = await client.post( Uri.parse(AppSettings.host + '/ILiveConsultationService'), body: body); var parsed = decodeResponseBody('RejectLiveConsultationAsync', response.bodyBytes); var data = parsed['result']; return data; } Future ApprovalConsultationAsync( ApprovalConsultationRequest model) async { String consultationCode = model.consultationCode; String expertUserCode = model.expertUserCode; DateTime consultationTime = model.consultationTime; List consultationMemberCodes = []; model.consultationMemberCodes.forEach((element) { consultationMemberCodes.add('"' + element + '"'); }); var userService = GetIt.instance.get(); var user = await userService.getCurrentUser(); var token = user?.accessToken; var client = http.Client(); DateFormat inputFormat = DateFormat("yyyy-MM-ddTHH:mm:ss"); var utcTime = inputFormat.format(consultationTime.toUtc()).toString(); var utcEndTime = inputFormat .format(consultationTime.add(const Duration(hours: 2)).toUtc()) .toString(); var body = '{"jsonrpc": "2.0", "method": "ApprovalConsultationAsync", "params": [{"Token": "$token","ConsultationCode":"$consultationCode", "ExpertUserCode":"$expertUserCode", "ConsultationTime":"$utcTime", "ConsultationTimeEnd":"$utcEndTime","ConsultationMemberCodes":$consultationMemberCodes }], "id": 1 }'; Logger.info('ApprovalConsultationAsync http.Client()' + body); var response = await client.post( Uri.parse(AppSettings.host + '/ILiveConsultationService'), body: body); var parsed = decodeResponseBody('ApprovalConsultationAsync', response.bodyBytes); var data = parsed['result']; return data; } } class AppConsultationDataModel { final List experts; final List devices; final List organs; final List users; AppConsultationDataModel(this.experts, this.devices, this.organs, this.users); } class TokenRequest { String token; TokenRequest(this.token); } class DataItemDTO { final String key; final String value; DataItemDTO({required this.key, required this.value}); factory DataItemDTO.fromJson(Map json) { return DataItemDTO( key: json['Key'] as String, value: json['Value'] as String, ); } Map toJson() => { "Key": key, "Value": value, }; } class ApplyConsultationRequest extends TokenRequest { String expertUserCode; String deviceCode; String scanPosition; DateTime consultationTime; List patientDatas; String patientCode; ApplyConsultationRequest( this.expertUserCode, this.deviceCode, this.scanPosition, this.consultationTime, this.patientDatas, this.patientCode, String token) : super(token); } class Expert { final String code; final String userName; Expert({required this.code, required this.userName}); @override bool operator ==(Object other) => other is Expert && other.code == code; factory Expert.fromJson(Map json) { return Expert( code: json['UserCode'] as String, userName: json['UserName'] as String, ); } Map toJson() => { "UserCode": code, "UserName": userName, }; } class Device { final String code; final String deviceName; Device({required this.code, required this.deviceName}); bool operator ==(Object other) => other is Device && other.code == code; factory Device.fromJson(Map json) { return Device( code: json['Key'] as String, deviceName: json['Value'] as String, ); } Map toJson() => { "Key": code, "Value": deviceName, }; } class Organ { final String code; final String orgName; Organ({required this.code, required this.orgName}); factory Organ.fromJson(Map json) { return Organ( code: json['Key'] as String, orgName: json['Value'] as String, ); } Map toJson() => { "Key": code, "Value": orgName, }; } class ApprovalConsultationRequest extends TokenRequest { String consultationCode; String expertUserCode; DateTime consultationTime; List consultationMemberCodes; ApprovalConsultationRequest(this.consultationCode, this.expertUserCode, this.consultationTime, this.consultationMemberCodes, String token) : super(token); @override String toString() { // TODO: implement toString return 'consultationCode: $consultationCode,expertUserCode:$expertUserCode,consultationTime:$consultationTime,consultationMemberCodes:$consultationMemberCodes'; } }