ConsultationService.dart 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. import 'dart:convert';
  2. import 'dart:html';
  3. import 'dart:js_util';
  4. import 'package:get_it/get_it.dart';
  5. import 'package:http/http.dart' as http;
  6. import 'package:localstorage/localstorage.dart';
  7. import 'package:sprintf/sprintf.dart';
  8. import 'package:ustest/ConsultationList.dart';
  9. import 'package:ustest/Services/LocalStorageService.dart';
  10. import 'dart:typed_data';
  11. import 'package:web_socket_channel/web_socket_channel.dart';
  12. import 'AppSettings.dart';
  13. import 'UserService.dart';
  14. import 'package:intl/intl.dart';
  15. import 'package:event/event.dart';
  16. class NotificationReceivedArgs extends EventArgs {
  17. Map<String, dynamic> jsonMessage;
  18. NotificationReceivedArgs(this.jsonMessage);
  19. }
  20. class ConsultationService {
  21. Event<NotificationReceivedArgs> NotificationReceived =
  22. Event<NotificationReceivedArgs>();
  23. RaiseConsultationNotificationReceived(Map<String, dynamic> jsonMessage) {
  24. NotificationReceived.broadcast(NotificationReceivedArgs(jsonMessage));
  25. }
  26. Future<AppConsultationDataModel> LoadDataAsync() async {
  27. var userService = GetIt.instance.get<UserService>();
  28. final user = userService.getCurrentUser();
  29. final token = user?.accessToken;
  30. final orgCode = user?.organizationCode;
  31. var client = http.Client();
  32. var body =
  33. '{"jsonrpc": "2.0", "method": "GetPersonDeviceDropdownListAsync", "params": [{"Token": "$token"}], "id": 1 }';
  34. print('QueryExam http.Client()' + body);
  35. var response = await client
  36. .post(Uri.parse(AppSettings.host + '/IDeviceService'), body: body);
  37. var parsed = decodeResponseBody(
  38. 'GetPersonDeviceDropdownListAsync', response.bodyBytes);
  39. var datas = parsed['result'];
  40. final devices = datas.map<Device>((json) => Device.fromJson(json)).toList();
  41. body =
  42. '{"jsonrpc": "2.0", "method": "FindOrganizationExpertsAsync", "params": [{"Token": "$token", "OrganizationCode": "$orgCode"}], "id": 1 }';
  43. print('QueryExam http.Client()' + body);
  44. response = await client.post(
  45. Uri.parse(AppSettings.host + '/ILiveConsultationService'),
  46. body: body);
  47. //final response = await post(client, "ILiveConsultationService",
  48. // "FindOrganizationExpertsAsync", args);
  49. print('FindOrganizationExpertsAsync response.body' + response.body);
  50. parsed = jsonDecode(response.body);
  51. datas = parsed['result'];
  52. final experts = datas.map<Expert>((json) => Expert.fromJson(json)).toList()
  53. as List<Expert>;
  54. body =
  55. '{"jsonrpc": "2.0", "method": "FindScanPositionsAsync", "params": [{"Token": "$token"}], "id": 1 }';
  56. print('FindScanPositionsAsync http.Client()' + body);
  57. response = await client.post(
  58. Uri.parse(AppSettings.host + '/ILiveConsultationService'),
  59. body: body);
  60. parsed = decodeResponseBody('FindScanPositionsAsync', response.bodyBytes);
  61. //var data = jsonDecode(parsed['result']);
  62. var organSource = parsed['result'];
  63. var organs = organSource.map<String>((json) => json.toString()).toList()
  64. as List<String>;
  65. var model = new AppConsultationDataModel(experts, devices, organs);
  66. return model;
  67. }
  68. decodeResponseBody(String logTag, Uint8List bodyBytes) {
  69. var utfString = utf8.decode(bodyBytes);
  70. print('$logTag response.body' + utfString);
  71. final parsed = jsonDecode(utfString);
  72. return parsed;
  73. }
  74. Future<List<Consultation>> FindConsultationsByPageAsync(
  75. String id, int? selectedType) async {
  76. try {
  77. var userService = GetIt.instance.get<UserService>();
  78. var user = userService.getCurrentUser();
  79. var token = user?.accessToken;
  80. var client = http.Client();
  81. var body =
  82. '{"jsonrpc": "2.0", "method": "FindConsultationsByPageAsync", "params": [{"Token": "$token", "PageIndex": 1, "PageSize": 10,"ConsultationQueryType": $selectedType}], "id": 1 }';
  83. print('GetRecordInfoPagesAsync http.Client()' + body);
  84. final response = await client.post(
  85. Uri.parse(AppSettings.host + '/ILiveConsultationService'),
  86. body: body);
  87. print('FindConsultationsByPageAsync response.body' + response.body);
  88. final parsed = jsonDecode(response.body);
  89. var datas = parsed['result']['PageData'];
  90. var list = datas
  91. .map<Consultation>((json) => Consultation.fromJson(json))
  92. .toList();
  93. return list;
  94. } catch (ex) {
  95. print('FindConsultationsByPageAsync.to ex' + ex.toString());
  96. }
  97. return List.empty();
  98. }
  99. dynamic post(
  100. http.Client client, String interface, String method, String args) async {
  101. final body = sprintf(
  102. '{"jsonrpc": "2.0", "method": "$method", "params": %s, "id": 1 }',
  103. args);
  104. final response = await client
  105. .post(Uri.parse(AppSettings.host + '/$interface'), body: body);
  106. print('GetUserInfoAsync response.body' + response.body);
  107. var parsed = jsonDecode(response.body);
  108. }
  109. Future<bool> ApplyConsultationAsync(
  110. String expertCode, String deviceCode, String organ, DateTime time) async {
  111. var userService = GetIt.instance.get<UserService>();
  112. var user = userService.getCurrentUser();
  113. var token = user?.accessToken;
  114. var client = http.Client();
  115. var patientCode = "2D6DA689ECC54F52A17F20A05BDF5C27"; //TODO should from UI
  116. var organizationCode = "Organization_20221020115006aAjF6l";
  117. DateFormat inputFormat = DateFormat("yyyy-MM-ddTHH:mm:ss");
  118. var utcTime = inputFormat.format(time.toUtc()).toString();
  119. var body =
  120. '{"jsonrpc": "2.0", "method": "FindPatientByCodeAsync", "params": [{"Token": "$token", "Code":"$patientCode"}], "id": 1 }';
  121. print('FindPatientByCodeAsync http.Client()' + body);
  122. var response = await client
  123. .post(Uri.parse(AppSettings.host + '/IPatientService'), body: body);
  124. var parsed =
  125. decodeResponseBody('FindPatientByCodeAsync', response.bodyBytes);
  126. var data = parsed['result']['PatientData'];
  127. var list =
  128. data.map<DataItemDTO>((json) => DataItemDTO.fromJson(json)).toList();
  129. var patientDatas = jsonEncode(list);
  130. body =
  131. '{"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 }';
  132. print('ApplyConsultationAsync http.Client()' + body);
  133. response = await client.post(
  134. Uri.parse(AppSettings.host + '/ILiveConsultationService'),
  135. body: body);
  136. parsed = decodeResponseBody('ApplyConsultationAsync', response.bodyBytes);
  137. return true;
  138. }
  139. Future<dynamic> InitiateLiveConsultationAsync(String consultationCode) async {
  140. var userService = GetIt.instance.get<UserService>();
  141. var user = userService.getCurrentUser();
  142. var token = user?.accessToken;
  143. var client = http.Client();
  144. var body =
  145. '{"jsonrpc": "2.0", "method": "InitiateLiveConsultationAsync", "params": [{"Token": "$token", "ConsultationCode":"$consultationCode"}], "id": 1 }';
  146. print('InitiateLiveConsultationAsync http.Client()' + body);
  147. final response = await client.post(
  148. Uri.parse(AppSettings.host + '/ILiveConsultationService'),
  149. body: body);
  150. var parsed =
  151. decodeResponseBody('InitiateLiveConsultationAsync', response.bodyBytes);
  152. var data = parsed['result'];
  153. return data;
  154. }
  155. Future<dynamic> AcceptLiveConsultationAsync(String consultationCode) async {
  156. var userService = GetIt.instance.get<UserService>();
  157. var user = userService.getCurrentUser();
  158. var token = user?.accessToken;
  159. var client = http.Client();
  160. var body =
  161. '{"jsonrpc": "2.0", "method": "AcceptLiveConsultationAsync", "params": [{"Token": "$token", "ConsultationCode":"$consultationCode"}], "id": 1 }';
  162. print('AcceptLiveConsultationAsync http.Client()' + body);
  163. final response = await client.post(
  164. Uri.parse(AppSettings.host + '/ILiveConsultationService'),
  165. body: body);
  166. var parsed =
  167. decodeResponseBody('AcceptLiveConsultationAsync', response.bodyBytes);
  168. var data = parsed['result'];
  169. return data;
  170. }
  171. Future<dynamic> RejectLiveConsultationAsync(String consultationCode) async {
  172. var userService = GetIt.instance.get<UserService>();
  173. var user = userService.getCurrentUser();
  174. var token = user?.accessToken;
  175. var client = http.Client();
  176. var body =
  177. '{"jsonrpc": "2.0", "method": "RejectLiveConsultationAsync", "params": [{"Token": "$token", "ConsultationCode":"$consultationCode"}], "id": 1 }';
  178. print('RejectLiveConsultationAsync http.Client()' + body);
  179. final response = await client.post(
  180. Uri.parse(AppSettings.host + '/ILiveConsultationService'),
  181. body: body);
  182. var parsed =
  183. decodeResponseBody('RejectLiveConsultationAsync', response.bodyBytes);
  184. var data = parsed['result'];
  185. return data;
  186. }
  187. }
  188. class AppConsultationDataModel {
  189. final List<Expert> experts;
  190. final List<Device> devices;
  191. final List<String> organs;
  192. AppConsultationDataModel(this.experts, this.devices, this.organs);
  193. }
  194. class TokenRequest {
  195. String token;
  196. TokenRequest(this.token);
  197. }
  198. class DataItemDTO {
  199. final String key;
  200. final String value;
  201. DataItemDTO({required this.key, required this.value});
  202. factory DataItemDTO.fromJson(Map<String, dynamic> json) {
  203. return DataItemDTO(
  204. key: json['Key'] as String,
  205. value: json['Value'] as String,
  206. );
  207. }
  208. Map<String, dynamic> toJson() => {
  209. "Key": key,
  210. "Value": value,
  211. };
  212. }
  213. class ApplyConsultationRequest extends TokenRequest {
  214. String expertUserCode;
  215. String deviceCode;
  216. String scanPosition;
  217. DateTime consultationTime;
  218. List<DataItemDTO> patientDatas;
  219. String patientCode;
  220. ApplyConsultationRequest(
  221. this.expertUserCode,
  222. this.deviceCode,
  223. this.scanPosition,
  224. this.consultationTime,
  225. this.patientDatas,
  226. this.patientCode,
  227. String token)
  228. : super(token);
  229. }
  230. class Expert {
  231. final String code;
  232. final String userName;
  233. Expert({required this.code, required this.userName});
  234. @override
  235. bool operator ==(Object other) => other is Expert && other.code == code;
  236. factory Expert.fromJson(Map<String, dynamic> json) {
  237. return Expert(
  238. code: json['UserCode'] as String,
  239. userName: json['UserName'] as String,
  240. );
  241. }
  242. Map<String, dynamic> toJson() => {
  243. "UserCode": code,
  244. "UserName": userName,
  245. };
  246. }
  247. class Device {
  248. final String code;
  249. final String deviceName;
  250. Device({required this.code, required this.deviceName});
  251. bool operator ==(Object other) => other is Device && other.code == code;
  252. factory Device.fromJson(Map<String, dynamic> json) {
  253. return Device(
  254. code: json['Key'] as String,
  255. deviceName: json['Value'] as String,
  256. );
  257. }
  258. Map<String, dynamic> toJson() => {
  259. "Key": code,
  260. "Value": deviceName,
  261. };
  262. }
  263. class Organ {
  264. final String code;
  265. final String orgName;
  266. Organ({required this.code, required this.orgName});
  267. factory Organ.fromJson(Map<String, dynamic> json) {
  268. return Organ(
  269. code: json['Key'] as String,
  270. orgName: json['Value'] as String,
  271. );
  272. }
  273. Map<String, dynamic> toJson() => {
  274. "Key": code,
  275. "Value": orgName,
  276. };
  277. }