import 'dart:convert';
import 'dart:html';
import 'dart:js_util';
import 'package:get_it/get_it.dart';
import 'package:http/http.dart' as http;
import 'package:localstorage/localstorage.dart';
import 'package:sprintf/sprintf.dart';
import 'package:ustest/ConsultationList.dart';
import 'package:ustest/Services/LocalStorageService.dart';
import 'dart:typed_data';
import 'package:web_socket_channel/web_socket_channel.dart';
import 'AppSettings.dart';
import 'UserService.dart';
import 'package:intl/intl.dart';
import 'package:event/event.dart';
class NotificationReceivedArgs extends EventArgs {
dynamic jsonMessage;
NotificationReceivedArgs(this.jsonMessage);
}
class ConsultationService {
Event NotificationReceived =
Event();
RaiseConsultationNotificationReceived(dynamic jsonMessage) {
NotificationReceived.broadcast(NotificationReceivedArgs(jsonMessage));
}
Future LoadDataAsync() async {
var userService = GetIt.instance.get();
final user = 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 }';
print('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 }';
print('QueryExam http.Client()' + body);
response = await client.post(
Uri.parse(AppSettings.host + '/ILiveConsultationService'),
body: body);
//final response = await post(client, "ILiveConsultationService",
// "FindOrganizationExpertsAsync", args);
print('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 }';
print('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 }';
print('GetUserListAsync http.Client()' + body);
response = await client.post(Uri.parse(AppSettings.host + '/IUserService'),
body: body);
print('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);
print('$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 = userService.getCurrentUser();
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 }';
print('GetRecordInfoPagesAsync http.Client()' + body);
final response = await client.post(
Uri.parse(AppSettings.host + '/ILiveConsultationService'),
body: body);
print('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) {
print('FindConsultationsByPageAsync.to ex' + ex.toString());
}
return List.empty();
}
dynamic post(
http.Client client, String interface, String method, String args) async {
final body = sprintf(
'{"jsonrpc": "2.0", "method": "$method", "params": %s, "id": 1 }',
args);
final response = await client
.post(Uri.parse(AppSettings.host + '/$interface'), body: body);
print('GetUserInfoAsync response.body' + response.body);
var parsed = jsonDecode(response.body);
}
Future ApplyConsultationAsync(
String expertCode, String deviceCode, String organ, DateTime time) async {
var userService = GetIt.instance.get();
var user = 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 }';
print('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 }';
print('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 = 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'];
print("begin RaiseConsultationNotificationReceived");
RaiseConsultationNotificationReceived(data);
print("end RaiseConsultationNotificationReceived");
return data;
}
Future AcceptLiveConsultationAsync(String consultationCode) async {
var userService = GetIt.instance.get();
var user = userService.getCurrentUser();
var token = user?.accessToken;
var client = http.Client();
var body =
'{"jsonrpc": "2.0", "method": "AcceptLiveConsultationAsync", "params": [{"Token": "$token", "ConsultationCode":"$consultationCode"}], "id": 1 }';
print('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 = userService.getCurrentUser();
var token = user?.accessToken;
var client = http.Client();
var body =
'{"jsonrpc": "2.0", "method": "RejectLiveConsultationAsync", "params": [{"Token": "$token", "ConsultationCode":"$consultationCode"}], "id": 1 }';
print('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 = 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 body =
'{"jsonrpc": "2.0", "method": "ApprovalConsultationAsync", "params": [{"Token": "$token","ConsultationCode":"$consultationCode", "ExpertUserCode":"$expertUserCode", "ConsultationTime":"$utcTime", "ConsultationMemberCodes":$consultationMemberCodes }], "id": 1 }';
print('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';
}
}