ConsultationService.dart 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. dynamic jsonMessage;
  18. NotificationReceivedArgs(this.jsonMessage);
  19. }
  20. class ConsultationService {
  21. Event<NotificationReceivedArgs> NotificationReceived =
  22. Event<NotificationReceivedArgs>();
  23. RaiseConsultationNotificationReceived(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. body =
  66. '{"jsonrpc": "2.0", "method": "GetUserListAsync", "params": [{"Token": "$token", "OrganizationCode": "$orgCode","OrganizationQueryType":3,"ExceptSelf":true}], "id": 1 }';
  67. print('GetUserListAsync http.Client()' + body);
  68. response = await client.post(Uri.parse(AppSettings.host + '/IUserService'),
  69. body: body);
  70. print('GetUserListAsync response.body' + response.body);
  71. parsed = jsonDecode(response.body);
  72. datas = parsed['result'];
  73. final users = datas.map<Expert>((json) => Expert.fromJson(json)).toList()
  74. as List<Expert>;
  75. var model = new AppConsultationDataModel(experts, devices, organs, users);
  76. return model;
  77. }
  78. decodeResponseBody(String logTag, Uint8List bodyBytes) {
  79. var utfString = utf8.decode(bodyBytes);
  80. print('$logTag response.body' + utfString);
  81. final parsed = jsonDecode(utfString);
  82. return parsed;
  83. }
  84. Future<List<Consultation>> FindConsultationsByPageAsync(
  85. String id, int? selectedType) async {
  86. try {
  87. var userService = GetIt.instance.get<UserService>();
  88. var user = userService.getCurrentUser();
  89. var token = user?.accessToken;
  90. var client = http.Client();
  91. var body =
  92. '{"jsonrpc": "2.0", "method": "FindConsultationsByPageAsync", "params": [{"Token": "$token", "PageIndex": 1, "PageSize": 10,"ConsultationQueryType": $selectedType}], "id": 1 }';
  93. print('GetRecordInfoPagesAsync http.Client()' + body);
  94. final response = await client.post(
  95. Uri.parse(AppSettings.host + '/ILiveConsultationService'),
  96. body: body);
  97. print('FindConsultationsByPageAsync response.body' + response.body);
  98. final parsed = jsonDecode(response.body);
  99. var datas = parsed['result']['PageData'];
  100. var list = datas
  101. .map<Consultation>((json) => Consultation.fromJson(json))
  102. .toList();
  103. return list;
  104. } catch (ex) {
  105. print('FindConsultationsByPageAsync.to ex' + ex.toString());
  106. }
  107. return List.empty();
  108. }
  109. dynamic post(
  110. http.Client client, String interface, String method, String args) async {
  111. final body = sprintf(
  112. '{"jsonrpc": "2.0", "method": "$method", "params": %s, "id": 1 }',
  113. args);
  114. final response = await client
  115. .post(Uri.parse(AppSettings.host + '/$interface'), body: body);
  116. print('GetUserInfoAsync response.body' + response.body);
  117. var parsed = jsonDecode(response.body);
  118. }
  119. Future<bool> ApplyConsultationAsync(
  120. String expertCode, String deviceCode, String organ, DateTime time) async {
  121. var userService = GetIt.instance.get<UserService>();
  122. var user = userService.getCurrentUser();
  123. var token = user?.accessToken;
  124. var client = http.Client();
  125. var patientCode = "2D6DA689ECC54F52A17F20A05BDF5C27"; //TODO should from UI
  126. var organizationCode = "Organization_20221020115006aAjF6l";
  127. DateFormat inputFormat = DateFormat("yyyy-MM-ddTHH:mm:ss");
  128. var utcTime = inputFormat.format(time.toUtc()).toString();
  129. var body =
  130. '{"jsonrpc": "2.0", "method": "FindPatientByCodeAsync", "params": [{"Token": "$token", "Code":"$patientCode"}], "id": 1 }';
  131. print('FindPatientByCodeAsync http.Client()' + body);
  132. var response = await client
  133. .post(Uri.parse(AppSettings.host + '/IPatientService'), body: body);
  134. var parsed =
  135. decodeResponseBody('FindPatientByCodeAsync', response.bodyBytes);
  136. var data = parsed['result']['PatientData'];
  137. var list =
  138. data.map<DataItemDTO>((json) => DataItemDTO.fromJson(json)).toList();
  139. var patientDatas = jsonEncode(list);
  140. body =
  141. '{"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 }';
  142. print('ApplyConsultationAsync http.Client()' + body);
  143. response = await client.post(
  144. Uri.parse(AppSettings.host + '/ILiveConsultationService'),
  145. body: body);
  146. parsed = decodeResponseBody('ApplyConsultationAsync', response.bodyBytes);
  147. return true;
  148. }
  149. Future<dynamic> InitiateLiveConsultationAsync(String consultationCode) async {
  150. var userService = GetIt.instance.get<UserService>();
  151. var user = userService.getCurrentUser();
  152. var token = user?.accessToken;
  153. var client = http.Client();
  154. var body =
  155. '{"jsonrpc": "2.0", "method": "InitiateLiveConsultationAsync", "params": [{"Token": "$token", "ConsultationCode":"$consultationCode"}], "id": 1 }';
  156. print('InitiateLiveConsultationAsync http.Client()' + body);
  157. final response = await client.post(
  158. Uri.parse(AppSettings.host + '/ILiveConsultationService'),
  159. body: body);
  160. var parsed =
  161. decodeResponseBody('InitiateLiveConsultationAsync', response.bodyBytes);
  162. var data = parsed['result'];
  163. print("begin RaiseConsultationNotificationReceived");
  164. RaiseConsultationNotificationReceived(data);
  165. print("end RaiseConsultationNotificationReceived");
  166. return data;
  167. }
  168. Future<dynamic> AcceptLiveConsultationAsync(String consultationCode) async {
  169. var userService = GetIt.instance.get<UserService>();
  170. var user = userService.getCurrentUser();
  171. var token = user?.accessToken;
  172. var client = http.Client();
  173. var body =
  174. '{"jsonrpc": "2.0", "method": "AcceptLiveConsultationAsync", "params": [{"Token": "$token", "ConsultationCode":"$consultationCode"}], "id": 1 }';
  175. print('AcceptLiveConsultationAsync http.Client()' + body);
  176. final response = await client.post(
  177. Uri.parse(AppSettings.host + '/ILiveConsultationService'),
  178. body: body);
  179. var parsed =
  180. decodeResponseBody('AcceptLiveConsultationAsync', response.bodyBytes);
  181. var data = parsed['result'];
  182. return data;
  183. }
  184. Future<dynamic> RejectLiveConsultationAsync(String consultationCode) async {
  185. var userService = GetIt.instance.get<UserService>();
  186. var user = userService.getCurrentUser();
  187. var token = user?.accessToken;
  188. var client = http.Client();
  189. var body =
  190. '{"jsonrpc": "2.0", "method": "RejectLiveConsultationAsync", "params": [{"Token": "$token", "ConsultationCode":"$consultationCode"}], "id": 1 }';
  191. print('RejectLiveConsultationAsync http.Client()' + body);
  192. final response = await client.post(
  193. Uri.parse(AppSettings.host + '/ILiveConsultationService'),
  194. body: body);
  195. var parsed =
  196. decodeResponseBody('RejectLiveConsultationAsync', response.bodyBytes);
  197. var data = parsed['result'];
  198. return data;
  199. }
  200. Future<bool> ApprovalConsultationAsync(
  201. ApprovalConsultationRequest model) async {
  202. String consultationCode = model.consultationCode;
  203. String expertUserCode = model.expertUserCode;
  204. DateTime consultationTime = model.consultationTime;
  205. List<String> consultationMemberCodes = <String>[];
  206. model.consultationMemberCodes.forEach((element) {
  207. consultationMemberCodes.add('"' + element + '"');
  208. });
  209. var userService = GetIt.instance.get<UserService>();
  210. var user = userService.getCurrentUser();
  211. var token = user?.accessToken;
  212. var client = http.Client();
  213. DateFormat inputFormat = DateFormat("yyyy-MM-ddTHH:mm:ss");
  214. var utcTime = inputFormat.format(consultationTime.toUtc()).toString();
  215. var body =
  216. '{"jsonrpc": "2.0", "method": "ApprovalConsultationAsync", "params": [{"Token": "$token","ConsultationCode":"$consultationCode", "ExpertUserCode":"$expertUserCode", "ConsultationTime":"$utcTime", "ConsultationMemberCodes":$consultationMemberCodes }], "id": 1 }';
  217. print('ApprovalConsultationAsync http.Client()' + body);
  218. var response = await client.post(
  219. Uri.parse(AppSettings.host + '/ILiveConsultationService'),
  220. body: body);
  221. var parsed =
  222. decodeResponseBody('ApprovalConsultationAsync', response.bodyBytes);
  223. var data = parsed['result'];
  224. return data;
  225. }
  226. }
  227. class AppConsultationDataModel {
  228. final List<Expert> experts;
  229. final List<Device> devices;
  230. final List<String> organs;
  231. final List<Expert> users;
  232. AppConsultationDataModel(this.experts, this.devices, this.organs, this.users);
  233. }
  234. class TokenRequest {
  235. String token;
  236. TokenRequest(this.token);
  237. }
  238. class DataItemDTO {
  239. final String key;
  240. final String value;
  241. DataItemDTO({required this.key, required this.value});
  242. factory DataItemDTO.fromJson(Map<String, dynamic> json) {
  243. return DataItemDTO(
  244. key: json['Key'] as String,
  245. value: json['Value'] as String,
  246. );
  247. }
  248. Map<String, dynamic> toJson() => {
  249. "Key": key,
  250. "Value": value,
  251. };
  252. }
  253. class ApplyConsultationRequest extends TokenRequest {
  254. String expertUserCode;
  255. String deviceCode;
  256. String scanPosition;
  257. DateTime consultationTime;
  258. List<DataItemDTO> patientDatas;
  259. String patientCode;
  260. ApplyConsultationRequest(
  261. this.expertUserCode,
  262. this.deviceCode,
  263. this.scanPosition,
  264. this.consultationTime,
  265. this.patientDatas,
  266. this.patientCode,
  267. String token)
  268. : super(token);
  269. }
  270. class Expert {
  271. final String code;
  272. final String userName;
  273. Expert({required this.code, required this.userName});
  274. @override
  275. bool operator ==(Object other) => other is Expert && other.code == code;
  276. factory Expert.fromJson(Map<String, dynamic> json) {
  277. return Expert(
  278. code: json['UserCode'] as String,
  279. userName: json['UserName'] as String,
  280. );
  281. }
  282. Map<String, dynamic> toJson() => {
  283. "UserCode": code,
  284. "UserName": userName,
  285. };
  286. }
  287. class Device {
  288. final String code;
  289. final String deviceName;
  290. Device({required this.code, required this.deviceName});
  291. bool operator ==(Object other) => other is Device && other.code == code;
  292. factory Device.fromJson(Map<String, dynamic> json) {
  293. return Device(
  294. code: json['Key'] as String,
  295. deviceName: json['Value'] as String,
  296. );
  297. }
  298. Map<String, dynamic> toJson() => {
  299. "Key": code,
  300. "Value": deviceName,
  301. };
  302. }
  303. class Organ {
  304. final String code;
  305. final String orgName;
  306. Organ({required this.code, required this.orgName});
  307. factory Organ.fromJson(Map<String, dynamic> json) {
  308. return Organ(
  309. code: json['Key'] as String,
  310. orgName: json['Value'] as String,
  311. );
  312. }
  313. Map<String, dynamic> toJson() => {
  314. "Key": code,
  315. "Value": orgName,
  316. };
  317. }
  318. class ApprovalConsultationRequest extends TokenRequest {
  319. String consultationCode;
  320. String expertUserCode;
  321. DateTime consultationTime;
  322. List<String> consultationMemberCodes;
  323. ApprovalConsultationRequest(this.consultationCode, this.expertUserCode,
  324. this.consultationTime, this.consultationMemberCodes, String token)
  325. : super(token);
  326. @override
  327. String toString() {
  328. // TODO: implement toString
  329. return 'consultationCode: $consultationCode,expertUserCode:$expertUserCode,consultationTime:$consultationTime,consultationMemberCodes:$consultationMemberCodes';
  330. }
  331. }