ConsultationService.dart 14 KB

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