import 'dart:convert'; import 'package:fis_common/index.dart'; import 'package:fis_jsonrpc/rpc.dart'; import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'package:vitalapp/architecture/utils/datetime.dart'; import 'package:vitalapp/components/appbar.dart'; import 'package:vitalapp/managers/interfaces/prescription.dart'; import 'package:vitalapp/pages/form/form_info.dart'; import 'package:vitalapp/store/store.dart'; import 'prescription_form.dart'; import 'prescription_form_keys.dart'; class PrescriptionCollection extends StatefulWidget { ///随访Code final String followUpCode; final bool isChild; final Future Function()? createFollowUpOnly; final bool isCreateFromOldDto; PrescriptionCollection( this.followUpCode, { this.isChild = true, this.createFollowUpOnly, this.isCreateFromOldDto = false, }); @override State createState() { return PrescriptionCollectionState(); } } class PrescriptionCollectionState extends State { final _prescriptionManager = Get.find(); String _prescriptionFormKey = ""; Map> formValues = {}; Map _existPrescriptionCodes = {}; String _followUpCode = ""; bool _isLoading = true; List _hasSubmitPrescriptionList = []; ///处方模板中所有key final List allPrescriptionKeys = [ "PatientName", "PatientGender", "PatientAge", "DiseaseName", "PatientInspectionItems", "History", "Conclusion", "ReportDescription", "DigitalSignature", "PatientPhoneNumber", "ReportTime", "PatientExaminationWay", ]; @override void initState() { _followUpCode = widget.followUpCode; if (widget.isChild) { _prescriptionFormKey = PrescriptionFormKeys.AllChildFormKeys.keys.first; } else { _prescriptionFormKey = PrescriptionFormKeys.AllPregnantWomenFormKeys.keys.first; } super.initState(); _initData(); } @override Widget build(BuildContext context) { return Scaffold( backgroundColor: Colors.white, appBar: VAppBar( titleWidget: Text(widget.isChild ? "儿童处方" : "孕产妇处方"), actions: [ TextButton.icon( onPressed: () async { if (FormInfo.instance.formValue.isNotEmpty) { formValues[_prescriptionFormKey] = FormInfo.instance.formValue; } for (var k in formValues.keys) { var value = formValues[k]; ///创建处方 await createOrUpdatePrescription(k, value ?? {}); } /// 儿童就需要清除 if (widget.isChild) { FormInfo.instance.formValue.clear(); } Get.back(); }, label: Text( '保存', style: TextStyle(fontSize: 20, color: Colors.white), ), icon: Icon(Icons.save, size: 32, color: Colors.white), ), ], iconBack: () { if (widget.isChild) { FormInfo.instance.formValue.clear(); } }, ), body: Row( children: [ Container( width: 300, child: _buildLeft(), ), VerticalDivider( width: 1, color: Colors.black45, ), Expanded( child: _isLoading ? Center( child: CircularProgressIndicator(), ) : PrescriptionForm(_prescriptionFormKey)), ], ), ); } Future createOrUpdatePrescription( String prescriptionKey, Map value) async { PatientDTO? currentPatient = Store.user.currentSelectPatientInfo; var keys = value.keys; int targetLength = 3; if (Store.user.signature.isNotNullOrEmpty) { targetLength = 4; } if (keys.contains("PatientName") && keys.contains("PatientGender") && keys.contains("PatientAge")) { var age = DataTimeUtils.calculateAge( Store.user.currentSelectPatientInfo!.birthday!); String patientGender = ""; if (currentPatient?.patientGender == GenderEnum.Male) { patientGender = "1"; } else if (currentPatient?.patientGender == GenderEnum.Female) { patientGender = "2"; } if (value["PatientName"] == currentPatient?.patientName && value["PatientAge"] == age && value["PatientGender"] == patientGender) { int count = 0; for (var key in keys) { if (allPrescriptionKeys.contains(key)) { count++; } } if (count == targetLength) { ///经和测试讨论,如果一个表格只存在默认填充的数据,则不提交 return; } } } /// 测试的需求 需要提交一个和随访历史记录一样的新的随访记录 if (widget.isCreateFromOldDto && _followUpCode == widget.followUpCode) { _followUpCode = await widget.createFollowUpOnly!.call(); } if (_followUpCode.isEmpty) { _followUpCode = await widget.createFollowUpOnly!.call(); } var patientCode = currentPatient?.code ?? ''; /// 加号进来都是新增处方 if (widget.isCreateFromOldDto) { /// 创建处方 String? prescriptionCode = await _prescriptionManager.createPrescription( patientCode: patientCode, followUpCode: _followUpCode, prescriptionKey: prescriptionKey, prescriptionData: jsonEncode(value), ); } else { if (_existPrescriptionCodes.keys.contains(prescriptionKey)) { /// 更新处方 await _prescriptionManager.updatePrescription( patientCode: patientCode, followUpCode: _followUpCode, prescriptionKey: prescriptionKey, prescriptionData: jsonEncode(value), prescriptionCode: _existPrescriptionCodes[prescriptionKey]!, ); } else { /// 创建处方 String? prescriptionCode = await _prescriptionManager.createPrescription( patientCode: patientCode, followUpCode: _followUpCode, prescriptionKey: prescriptionKey, prescriptionData: jsonEncode(value), ); } } } void selectRaidoChange(MapEntry e) { if (FormInfo.instance.formValue.isNotEmpty) { formValues[_prescriptionFormKey] = FormInfo.instance.formValue; FormInfo.instance.formValue = {}; } _prescriptionFormKey = e.key; if (formValues.containsKey(_prescriptionFormKey)) { FormInfo.instance.formValue = formValues[_prescriptionFormKey] ?? {}; } setState(() {}); } Widget _buildLeft() { var keys = PrescriptionFormKeys.AllChildFormKeys; if (!widget.isChild) { keys = PrescriptionFormKeys.AllPregnantWomenFormKeys; } return ListView( shrinkWrap: true, children: keys.entries.map( (e) { return Container( margin: EdgeInsets.symmetric( vertical: 6, horizontal: 10, ), decoration: BoxDecoration( border: Border.all( color: _prescriptionFormKey == (e.key) ? Colors.blue : _hasSubmitPrescriptionList.contains(e.key) ? Colors.green : Colors.black26, ), borderRadius: const BorderRadius.all( Radius.circular(50), ), color: _prescriptionFormKey == e.key ? Colors.blue : _hasSubmitPrescriptionList.contains(e.key) ? Colors.green : Colors.transparent, ), child: InkWell( onTap: () => selectRaidoChange(e), borderRadius: BorderRadius.circular(50), child: FittedBox( child: Container( padding: const EdgeInsets.all(12), alignment: Alignment.center, child: Text( e.value, style: TextStyle( fontSize: 20, color: _prescriptionFormKey == e.key || _hasSubmitPrescriptionList.contains(e.key) ? Colors.white : Colors.black54, ), ), ), ), ), ); }, ).toList(), ); } Future _initData() async { var patientCode = Store.user.currentSelectPatientInfo?.code ?? ''; if (widget.followUpCode.isNotEmpty) { List? prescriptionList = await _prescriptionManager.getPrescriptionPage( patientCode: patientCode, followUpCode: widget.followUpCode, ); if (prescriptionList != null && prescriptionList.isNotEmpty) { for (var p in prescriptionList) { var prescriptionData = p.prescriptionData ?? ''; if (prescriptionData.isNotEmpty) { _hasSubmitPrescriptionList.add(p.prescriptionTemplateKey!); var jsonData = jsonDecode(prescriptionData); formValues[p.prescriptionTemplateKey!] = jsonData; _existPrescriptionCodes[p.prescriptionTemplateKey!] = p.code!; } } } if (formValues.containsKey(_prescriptionFormKey)) { FormInfo.instance.formValue = formValues[_prescriptionFormKey] ?? {}; } _isLoading = false; setState(() {}); } else { _isLoading = false; setState(() {}); } } }