pediatric_prescription_collection.dart 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. import 'dart:convert';
  2. import 'package:fis_common/index.dart';
  3. import 'package:fis_jsonrpc/rpc.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:get/get.dart';
  6. import 'package:vitalapp/architecture/utils/datetime.dart';
  7. import 'package:vitalapp/components/appbar.dart';
  8. import 'package:vitalapp/managers/interfaces/prescription.dart';
  9. import 'package:vitalapp/pages/form/form_info.dart';
  10. import 'package:vitalapp/store/store.dart';
  11. import 'prescription_form.dart';
  12. import 'prescription_form_keys.dart';
  13. class PrescriptionCollection extends StatefulWidget {
  14. ///随访Code
  15. final String followUpCode;
  16. final bool isChild;
  17. final Future<String> Function()? createFollowUpOnly;
  18. final bool isCreateFromOldDto;
  19. PrescriptionCollection(
  20. this.followUpCode, {
  21. this.isChild = true,
  22. this.createFollowUpOnly,
  23. this.isCreateFromOldDto = false,
  24. });
  25. @override
  26. State<StatefulWidget> createState() {
  27. return PrescriptionCollectionState();
  28. }
  29. }
  30. class PrescriptionCollectionState extends State<PrescriptionCollection> {
  31. final _prescriptionManager = Get.find<IPrescriptionManager>();
  32. String _prescriptionFormKey = "";
  33. Map<String, Map<String, dynamic>> formValues = {};
  34. Map<String, String> _existPrescriptionCodes = {};
  35. String _followUpCode = "";
  36. bool _isLoading = true;
  37. List<String> _hasSubmitPrescriptionList = [];
  38. ///处方模板中所有key
  39. final List<String> allPrescriptionKeys = [
  40. "PatientName",
  41. "PatientGender",
  42. "PatientAge",
  43. "DiseaseName",
  44. "PatientInspectionItems",
  45. "History",
  46. "Conclusion",
  47. "ReportDescription",
  48. "DigitalSignature",
  49. "PatientPhoneNumber",
  50. "ReportTime",
  51. "PatientExaminationWay",
  52. ];
  53. @override
  54. void initState() {
  55. _followUpCode = widget.followUpCode;
  56. if (widget.isChild) {
  57. _prescriptionFormKey = PrescriptionFormKeys.AllChildFormKeys.keys.first;
  58. } else {
  59. _prescriptionFormKey =
  60. PrescriptionFormKeys.AllPregnantWomenFormKeys.keys.first;
  61. }
  62. super.initState();
  63. _initData();
  64. }
  65. @override
  66. Widget build(BuildContext context) {
  67. return Scaffold(
  68. backgroundColor: Colors.white,
  69. appBar: VAppBar(
  70. titleWidget: Text(widget.isChild ? "儿童处方" : "孕产妇处方"),
  71. actions: [
  72. TextButton.icon(
  73. onPressed: () async {
  74. if (FormInfo.instance.formValue.isNotEmpty) {
  75. formValues[_prescriptionFormKey] = FormInfo.instance.formValue;
  76. }
  77. for (var k in formValues.keys) {
  78. var value = formValues[k];
  79. ///创建处方
  80. await createOrUpdatePrescription(k, value ?? {});
  81. }
  82. /// 儿童就需要清除
  83. if (widget.isChild) {
  84. FormInfo.instance.formValue.clear();
  85. }
  86. Get.back();
  87. },
  88. label: Text(
  89. '保存',
  90. style: TextStyle(fontSize: 20, color: Colors.white),
  91. ),
  92. icon: Icon(Icons.save, size: 32, color: Colors.white),
  93. ),
  94. ],
  95. iconBack: () {
  96. if (widget.isChild) {
  97. FormInfo.instance.formValue.clear();
  98. }
  99. },
  100. ),
  101. body: Row(
  102. children: [
  103. Container(
  104. width: 300,
  105. child: _buildLeft(),
  106. ),
  107. VerticalDivider(
  108. width: 1,
  109. color: Colors.black45,
  110. ),
  111. Expanded(
  112. child: _isLoading
  113. ? Center(
  114. child: CircularProgressIndicator(),
  115. )
  116. : PrescriptionForm(_prescriptionFormKey)),
  117. ],
  118. ),
  119. );
  120. }
  121. Future<void> createOrUpdatePrescription(
  122. String prescriptionKey, Map<String, dynamic> value) async {
  123. PatientDTO? currentPatient = Store.user.currentSelectPatientInfo;
  124. var keys = value.keys;
  125. int targetLength = 3;
  126. if (Store.user.signature.isNotNullOrEmpty) {
  127. targetLength = 4;
  128. }
  129. if (keys.contains("PatientName") &&
  130. keys.contains("PatientGender") &&
  131. keys.contains("PatientAge")) {
  132. var age = DataTimeUtils.calculateAge(
  133. Store.user.currentSelectPatientInfo!.birthday!);
  134. String patientGender = "";
  135. if (currentPatient?.patientGender == GenderEnum.Male) {
  136. patientGender = "1";
  137. } else if (currentPatient?.patientGender == GenderEnum.Female) {
  138. patientGender = "2";
  139. }
  140. if (value["PatientName"] == currentPatient?.patientName &&
  141. value["PatientAge"] == age &&
  142. value["PatientGender"] == patientGender) {
  143. int count = 0;
  144. for (var key in keys) {
  145. if (allPrescriptionKeys.contains(key)) {
  146. count++;
  147. }
  148. }
  149. if (count == targetLength) {
  150. ///经和测试讨论,如果一个表格只存在默认填充的数据,则不提交
  151. return;
  152. }
  153. }
  154. }
  155. /// 测试的需求 需要提交一个和随访历史记录一样的新的随访记录
  156. if (widget.isCreateFromOldDto && _followUpCode == widget.followUpCode) {
  157. _followUpCode = await widget.createFollowUpOnly!.call();
  158. }
  159. if (_followUpCode.isEmpty) {
  160. _followUpCode = await widget.createFollowUpOnly!.call();
  161. }
  162. var patientCode = currentPatient?.code ?? '';
  163. /// 加号进来都是新增处方
  164. if (widget.isCreateFromOldDto) {
  165. /// 创建处方
  166. String? prescriptionCode = await _prescriptionManager.createPrescription(
  167. patientCode: patientCode,
  168. followUpCode: _followUpCode,
  169. prescriptionKey: prescriptionKey,
  170. prescriptionData: jsonEncode(value),
  171. );
  172. } else {
  173. if (_existPrescriptionCodes.keys.contains(prescriptionKey)) {
  174. /// 更新处方
  175. await _prescriptionManager.updatePrescription(
  176. patientCode: patientCode,
  177. followUpCode: _followUpCode,
  178. prescriptionKey: prescriptionKey,
  179. prescriptionData: jsonEncode(value),
  180. prescriptionCode: _existPrescriptionCodes[prescriptionKey]!,
  181. );
  182. } else {
  183. /// 创建处方
  184. String? prescriptionCode =
  185. await _prescriptionManager.createPrescription(
  186. patientCode: patientCode,
  187. followUpCode: _followUpCode,
  188. prescriptionKey: prescriptionKey,
  189. prescriptionData: jsonEncode(value),
  190. );
  191. }
  192. }
  193. }
  194. void selectRaidoChange(MapEntry<String, String> e) {
  195. if (FormInfo.instance.formValue.isNotEmpty) {
  196. formValues[_prescriptionFormKey] = FormInfo.instance.formValue;
  197. FormInfo.instance.formValue = {};
  198. }
  199. _prescriptionFormKey = e.key;
  200. if (formValues.containsKey(_prescriptionFormKey)) {
  201. FormInfo.instance.formValue = formValues[_prescriptionFormKey] ?? {};
  202. }
  203. setState(() {});
  204. }
  205. Widget _buildLeft() {
  206. var keys = PrescriptionFormKeys.AllChildFormKeys;
  207. if (!widget.isChild) {
  208. keys = PrescriptionFormKeys.AllPregnantWomenFormKeys;
  209. }
  210. return ListView(
  211. shrinkWrap: true,
  212. children: keys.entries.map(
  213. (e) {
  214. return Container(
  215. margin: EdgeInsets.symmetric(
  216. vertical: 6,
  217. horizontal: 10,
  218. ),
  219. decoration: BoxDecoration(
  220. border: Border.all(
  221. color: _prescriptionFormKey == (e.key)
  222. ? Colors.blue
  223. : _hasSubmitPrescriptionList.contains(e.key)
  224. ? Colors.green
  225. : Colors.black26,
  226. ),
  227. borderRadius: const BorderRadius.all(
  228. Radius.circular(50),
  229. ),
  230. color: _prescriptionFormKey == e.key
  231. ? Colors.blue
  232. : _hasSubmitPrescriptionList.contains(e.key)
  233. ? Colors.green
  234. : Colors.transparent,
  235. ),
  236. child: InkWell(
  237. onTap: () => selectRaidoChange(e),
  238. borderRadius: BorderRadius.circular(50),
  239. child: FittedBox(
  240. child: Container(
  241. padding: const EdgeInsets.all(12),
  242. alignment: Alignment.center,
  243. child: Text(
  244. e.value,
  245. style: TextStyle(
  246. fontSize: 20,
  247. color: _prescriptionFormKey == e.key ||
  248. _hasSubmitPrescriptionList.contains(e.key)
  249. ? Colors.white
  250. : Colors.black54,
  251. ),
  252. ),
  253. ),
  254. ),
  255. ),
  256. );
  257. },
  258. ).toList(),
  259. );
  260. }
  261. Future<void> _initData() async {
  262. var patientCode = Store.user.currentSelectPatientInfo?.code ?? '';
  263. if (widget.followUpCode.isNotEmpty) {
  264. List<PrescriptionDTO>? prescriptionList =
  265. await _prescriptionManager.getPrescriptionPage(
  266. patientCode: patientCode,
  267. followUpCode: widget.followUpCode,
  268. );
  269. if (prescriptionList != null && prescriptionList.isNotEmpty) {
  270. for (var p in prescriptionList) {
  271. var prescriptionData = p.prescriptionData ?? '';
  272. if (prescriptionData.isNotEmpty) {
  273. _hasSubmitPrescriptionList.add(p.prescriptionTemplateKey!);
  274. var jsonData = jsonDecode(prescriptionData);
  275. formValues[p.prescriptionTemplateKey!] = jsonData;
  276. _existPrescriptionCodes[p.prescriptionTemplateKey!] = p.code!;
  277. }
  278. }
  279. }
  280. if (formValues.containsKey(_prescriptionFormKey)) {
  281. FormInfo.instance.formValue = formValues[_prescriptionFormKey] ?? {};
  282. }
  283. _isLoading = false;
  284. setState(() {});
  285. } else {
  286. _isLoading = false;
  287. setState(() {});
  288. }
  289. }
  290. }