import 'dart:convert'; import 'package:fis_common/index.dart'; import 'package:fis_common/logger/logger.dart'; import 'package:fis_jsonrpc/rpc.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/services.dart'; import 'package:get/get.dart'; import 'package:vitalapp/managers/interfaces/follow_up.dart'; import 'package:vitalapp/managers/interfaces/prescription.dart'; import 'package:vitalapp/managers/interfaces/template.dart'; import 'package:vitalapp/pages/check/prescription/pediatric_prescription_collection.dart'; import 'package:vitalapp/pages/form/form_info.dart'; import 'package:vitalapp/store/store.dart'; import 'forms/antenatal_visit.dart'; import 'forms/checkup_42_days.dart'; import 'forms/first_form.dart'; import 'forms/postpartum_follow_up.dart'; import 'state.dart'; class MaternalHealthManagementController extends GetxController { MaternalHealthManagementController(); //第 1 次产前检查 final String firstFollowUpkey = "MaternalHealthManagement"; //第 2~5 次产前随访 final String prenatalFollowupKey = "PrenatalFollowupServiceRecord"; //产后访视 final String postnatalVisitKey = "PostnatalVisitRecord"; //产后42天随访 final String postpartumHealthCheckup42DaysKey = "PostpartumHealthCheckup42Days"; final _followUpManager = Get.find(); final _templateManager = Get.find(); final _prescriptionManager = Get.find(); final state = MaternalHealthManagementState(); List offlineSyncTemp = []; ///是否处于编辑模式 bool isEdit = false; ///当前选中的记录Code String recordCode = ""; ///当前使用的模板Code String templateCode = ""; FollowUpModeEnum followUpMode = FollowUpModeEnum.Outpatient; Map maternalFormValue = FormInfo.instance.formValue; @override void onReady() { super.onReady(); _initData(); } Future save(String key, String followUpCode) async { List followUpPhotos = []; DateTime followUpTime = DateTime.now(); String? followUpData; // if (FormInfo.instance.formValue.isEmpty) { // PromptBox.toast('不能提交空数据'); // return false; // } followUpData = jsonEncode(FormInfo.instance.formValue); if (FormInfo.instance.formValue.containsKey("Form_Date")) { followUpTime = DateTime.parse(FormInfo.instance.formValue["Form_Date"]); } DateTime? nextFollowUpTime; if (FormInfo.instance.formValue.containsKey("Next_Follow_Up_Date")) { nextFollowUpTime = DateTime.parse(FormInfo.instance.formValue["Next_Follow_Up_Date"]); } String patientCode = Store.user.currentSelectPatientInfo?.code ?? ''; bool result = false; if (isEdit) { result = await _followUpManager.updateFollowUp( UpdateFollowUpRequest( key: key, followUpData: followUpData, followUpTime: followUpTime, nextFollowUpTime: nextFollowUpTime, followUpMode: followUpMode, followUpPhotos: followUpPhotos, code: recordCode, ), ) ?? false; } else { if (followUpCode.isEmpty) { String createResult = await _followUpManager.createFollowUp( CreateFollowUpRequest( key: key, patientCode: patientCode, templateCode: templateCode, followUpData: followUpData, followUpTime: followUpTime, nextFollowUpTime: nextFollowUpTime, followUpMode: followUpMode, followUpPhotos: followUpPhotos, ), ); if (createResult.isNotEmpty) { recordCode = createResult; result = true; } } else { result = await _followUpManager.updateFollowUp( UpdateFollowUpRequest( key: key, followUpData: followUpData, followUpTime: followUpTime, nextFollowUpTime: nextFollowUpTime, followUpMode: followUpMode, followUpPhotos: followUpPhotos, code: followUpCode, ), ) ?? false; } } if (result) { getFollowUpRecordList(); return true; } return false; } Future getFollowUpRecordList() async { try { var patientCode = Store.user.currentSelectPatientInfo?.code ?? ''; var result = await _followUpManager.getFollowUpRecordList( [ firstFollowUpkey, prenatalFollowupKey, postnatalVisitKey, postpartumHealthCheckup42DaysKey, ], patientCode, ); List followUpDTOList = []; followUpDTOList.addAll(result ?? []); state.followUpDTOList = followUpDTOList; } catch (e) { return; } } String getFollowUpValueByKey(FollowUpRecordDataDTO dto) { var key = dto.key; if (key == firstFollowUpkey) { return "第 1 次产前检查"; } else if (key == prenatalFollowupKey) { Map datas = jsonDecode(dto.followUpData ?? ''); if (datas.containsKey("times")) { int times = datas["times"] ?? 2; return "第 ${times} 次产前随访"; } return "第 2~5 次产前随访"; } else if (key == postnatalVisitKey) { return "产后访视"; } else if (key == postpartumHealthCheckup42DaysKey) { return "产后 42 天健康检查"; } return ""; } Future>> sharePrescription( FollowUpRecordDataDTO dataDTO, ) async { List> previewList = []; List? prescriptionList = await getPrescriptionList(dataDTO.code!); for (PrescriptionDTO prescription in prescriptionList ?? []) { if (prescription.previewUrl != null) { previewList.add({ "previewUrl": prescription.previewUrl ?? "", "key": prescription.prescriptionTemplateKey, }); } } return previewList; } Future?> getPrescriptionList( String followUpCode) async { // 获取处方列表 var patientCode = Store.user.currentSelectPatientInfo?.code ?? ''; List? prescriptionList = await _prescriptionManager.getPrescriptionPage( patientCode: patientCode, followUpCode: followUpCode, ); return prescriptionList; } ///跳转至随访页面 void toCheckPage( FollowUpRecordDataDTO dataDto, { bool isCreateFromOldDto = false, }) { if (dataDto.followUpData.isNullOrEmpty) { return; } if (isCreateFromOldDto) { isEdit = false; } var datas = jsonDecode(dataDto.followUpData ?? ''); if (datas is Map) { FormInfo.instance.formValue = datas; maternalFormValue = datas; var key = dataDto.key; if (key == firstFollowUpkey) { Get.to(FirstMaternalHealthManagementForm( onClickPrescribe: (followUpCode) async { maternalFormValue = FormInfo.instance.formValue; var result = await Get.to( PrescriptionCollection( followUpCode.isEmpty ? recordCode : followUpCode, isChild: false, isCreateFromOldDto: followUpCode.isEmpty ? isCreateFromOldDto : false, createFollowUpOnly: () async { await save(key!, followUpCode); if (recordCode.isNotEmpty) { isEdit = true; } return recordCode; }, ), ); FormInfo.instance.formValue = maternalFormValue; return result; }, )); } else if (key == prenatalFollowupKey) { int times = FormInfo.instance.formValue["times"] ?? 2; Get.to(AntenatalVisitForm( times, onClickPrescribe: (followUpCode) async { maternalFormValue = FormInfo.instance.formValue; var result = await Get.to( PrescriptionCollection( followUpCode.isEmail ? recordCode : followUpCode, isChild: false, isCreateFromOldDto: followUpCode.isEmail ? isCreateFromOldDto : false, createFollowUpOnly: () async { await save(key!, followUpCode); if (recordCode.isNotEmpty) { isEdit = true; } return recordCode; }, ), ); FormInfo.instance.formValue = maternalFormValue; return result; }, )); } else if (key == postnatalVisitKey) { Get.to(PostpartumFormView( onClickPrescribe: (followUpCode) async { maternalFormValue = FormInfo.instance.formValue; var result = await Get.to( PrescriptionCollection( recordCode, isChild: false, isCreateFromOldDto: isCreateFromOldDto, createFollowUpOnly: () async { await save(key!, followUpCode); if (recordCode.isNotEmpty) { isEdit = true; } return recordCode; }, ), ); FormInfo.instance.formValue = maternalFormValue; return result; }, )); } else if (key == postpartumHealthCheckup42DaysKey) { Get.to(PostpartumHealthCheckup42DaysForm( onClickPrescribe: (followUpCode) async { maternalFormValue = FormInfo.instance.formValue; var result = await Get.to( PrescriptionCollection( recordCode, isChild: false, isCreateFromOldDto: isCreateFromOldDto, createFollowUpOnly: () async { await save(key!, followUpCode); if (recordCode.isNotEmpty) { isEdit = true; } return recordCode; }, ), ); FormInfo.instance.formValue = maternalFormValue; return result; }, )); } } } Future getTemplate(String key) async { String templateContent = ''; try { var templates = await _templateManager.readTemplateRelation('templateRelation'); if (templates != null) { var templateRelation = jsonDecode(templates!); templateCode = templateRelation[key] ?? ''; var template = await _templateManager.readTemplate(templateCode) ?? ''; templateContent = TemplateDTO.fromJson(jsonDecode(template)).templateContent!; } } catch (e) { logger.e("getTemplate ex:", e); } if (templateContent.isEmpty) { final json = await rootBundle.loadString("assets/templates/$key.json"); if (json.isNotEmpty) { var content = jsonDecode(json); templateContent = jsonEncode(content["Content"]); } } return templateContent; } void _initData() { getFollowUpRecordList(); } }