123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- 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<String> Function()? createFollowUpOnly;
- final bool isCreateFromOldDto;
- PrescriptionCollection(
- this.followUpCode, {
- this.isChild = true,
- this.createFollowUpOnly,
- this.isCreateFromOldDto = false,
- });
- @override
- State<StatefulWidget> createState() {
- return PrescriptionCollectionState();
- }
- }
- class PrescriptionCollectionState extends State<PrescriptionCollection> {
- final _prescriptionManager = Get.find<IPrescriptionManager>();
- String _prescriptionFormKey = "";
- Map<String, Map<String, dynamic>> formValues = {};
- Map<String, String> _existPrescriptionCodes = {};
- String _followUpCode = "";
- bool _isLoading = true;
- List<String> _hasSubmitPrescriptionList = [];
- ///处方模板中所有key
- final List<String> 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<void> createOrUpdatePrescription(
- String prescriptionKey, Map<String, dynamic> 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<String, String> 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<void> _initData() async {
- var patientCode = Store.user.currentSelectPatientInfo?.code ?? '';
- if (widget.followUpCode.isNotEmpty) {
- List<PrescriptionDTO>? 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(() {});
- }
- }
- }
|