123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497 |
- import 'dart:convert';
- import 'package:fis_common/index.dart';
- import 'package:fis_jsonrpc/rpc.dart';
- import 'package:flutter/foundation.dart';
- import 'package:flutter/material.dart';
- import 'package:flutter/services.dart';
- import 'package:flutter_inappwebview/flutter_inappwebview.dart';
- import 'package:get/get.dart';
- import 'package:intl/intl.dart';
- import 'package:vitalapp/architecture/defines.dart';
- import 'package:vitalapp/architecture/utils/prompt_box.dart';
- import 'package:vitalapp/database/db.dart';
- import 'package:vitalapp/global.dart';
- import 'package:vitalapp/managers/interfaces/follow_up.dart';
- import 'package:vitalapp/managers/interfaces/prescription.dart';
- import 'package:vitalapp/pages/check/follow_up/controller.dart';
- import 'package:vitalapp/pages/check/follow_up/widgets/follow_up_from.dart';
- import 'package:vitalapp/pages/check/follow_up_record/state.dart';
- import 'package:vitalapp/pages/check/widgets/configurable_card.dart';
- import 'package:vitalapp/pages/form/form_info.dart';
- import 'package:vitalapp/pages/medical/controller.dart';
- import 'package:vitalapp/store/store.dart';
- class FollowUpRecordController extends FControllerBase {
- FollowUpRecordController({
- required this.followUpType,
- });
- final String followUpType;
- final state = FollowUpRecordState();
- final _followUpManager = Get.find<IFollowUpManager>();
- final followUpController = Get.put(FollowUpController());
- final _prescriptionManager = Get.find<IPrescriptionManager>();
- late String patientCode;
- late String patientName;
- late FollowUpController _followUpController;
- List<List> offlineSyncTemp = [];
- String prescriptionCode = "";
- @override
- void onReady() {
- super.onReady();
- _initData();
- }
- _initData() async {
- update(["contract_records"]);
- patientCode = Store.user.currentSelectPatientInfo?.code ?? '';
- patientName = Store.user.currentSelectPatientInfo?.patientName ?? '';
- await getFollowUpRecordList();
- _followUpController = Get.put(FollowUpController());
- }
- Future<void> getFollowUpRecordList() async {
- try {
- var keys = [followUpType];
- if (followUpType == "FollowUpTuberculosisFirstRecord") {
- keys = [
- "FollowUpTuberculosisFirstRecord",
- "FollowUpTuberculosisRecord",
- "FollowUpTuberculosisResultRecord"
- ];
- }
- var result = await _followUpManager.getFollowUpRecordList(
- keys,
- patientCode,
- );
- List<FollowUpRecordDTO> followUpDTOList = [];
- // 如果是web端并且是离线模式,则从本地获取数据
- if (!kIsWeb && kIsOnline) {
- List<FollowUpRecordDTO> offlineFollowUpList = await _followUpManager
- .getPatientNotUploadedRecordList(patientCode, followUpType);
- followUpDTOList = offlineFollowUpList;
- }
- /// TODO 后面需要优化,去重
- followUpDTOList.addAll(result ?? []);
- if (kIsOnline) {
- offlineSyncTemp = await _loadUnsyncIndexs(followUpDTOList);
- } else {
- offlineSyncTemp = await _loadUnsyncIndexs(result ?? []);
- }
- state.followUpDTOList = followUpDTOList;
- } catch (e) {
- return;
- }
- }
- Future<List<List>> _loadUnsyncIndexs(List<FollowUpRecordDTO> dtos) async {
- List<List> _offlineSyncTemp = [];
- /// web 端没有本地数据库
- if (!kIsWeb) {
- // TODO 临时打个补丁,后续再优化
- final entities = await db.repositories.followUp.queryAllListByPatient(
- patientCode,
- Store.user.userCode!,
- followUpType,
- );
- for (var i = 0; i < dtos.length; i++) {
- _offlineSyncTemp.add([]);
- final records = dtos[i].followUpRecordDatas;
- if (records == null) {
- continue;
- }
- for (var j = 0; j < records.length; j++) {
- final data = records[j];
- final entity = entities.firstWhereOrNull((e) => e.code == data.code);
- if (entity != null) {
- _offlineSyncTemp[i].add(entity.syncState);
- } else {
- _offlineSyncTemp[i].add(null);
- }
- }
- }
- } else {
- for (var i = 0; i < dtos.length; i++) {
- _offlineSyncTemp.add([]);
- final records = dtos[i].followUpRecordDatas;
- if (records == null) {
- continue;
- }
- for (var j = 0; j < records.length; j++) {
- final data = records[j];
- _offlineSyncTemp[i].add(data);
- }
- }
- }
- return _offlineSyncTemp;
- }
- Future<void> updateFollowUp(key, code, data) async {
- List<String> followUpPhotos = [];
- if (_followUpController.state.followUpPhoto?.isNotEmpty ?? false) {
- followUpPhotos = [_followUpController.state.followUpPhoto!];
- }
- final result = await _followUpManager.updateFollowUp(
- UpdateFollowUpRequest(
- key: key,
- followUpData: data,
- followUpTime: _followUpController.state.followUpTime,
- nextFollowUpTime: _followUpController.state.nextFollowUpTime,
- followUpMode: _followUpController.state.followUpMode ??
- FollowUpModeEnum.Outpatient,
- code: code,
- followUpPhotos: followUpPhotos,
- ),
- );
- if (result ?? false) {
- PromptBox.toast('保存成功');
- } else {
- PromptBox.toast('保存失败');
- }
- }
- ///读取静态Html
- Future<String> loadingLocalAsset(String key) async {
- String tableName = "";
- if (key == 'GXY') {
- tableName = 'assets/docs/highBloodPressureTable.html';
- } else if (key == 'TNB') {
- tableName = 'assets/docs/diabetesTable.html';
- }
- ///加载
- String htmlData = await rootBundle.loadString(tableName);
- print("加载数据完成 $htmlData");
- return htmlData;
- }
- Future<List> getFollowUpTableData(FollowUpRecordDTO dto, int index) async {
- var currentFollowUp = dto.followUpRecordDatas?[index];
- var followUpTableData = await _followUpManager
- .getFollowUpRecordListByYearAsync(GetFollowUpRecordListByYearRequest(
- year: (currentFollowUp?.followUpTime?.year ?? DateTime.now().year)
- .toString(),
- keys: [currentFollowUp?.key ?? ''],
- patientCode: patientCode,
- ));
- List tableData = [];
- followUpTableData?.first.followUpRecordDatas?.forEach((element) {
- Map followUp = {
- "Follow_Time": element.followUpTime != null
- ? DateFormat("yyyy-MM-dd").format(element.followUpTime!.toLocal())
- : "",
- "Follow_Type": '${(element.followUpMode?.index ?? 0) + 1} ',
- "Next_Follow_Up_Time": element.nextFollowUpTime != null
- ? DateFormat("yyyy-MM-dd")
- .format(element.nextFollowUpTime!.toLocal())
- : "",
- };
- followUp.addAll(jsonDecode(element.followUpData ?? ''));
- tableData.add(followUp);
- });
- return tableData;
- }
- Future<String> loadLocalAsset(String key) async {
- String tableName = "";
- if (key == 'GXY') {
- tableName = 'assets/docs/highBloodPressureTable.html';
- } else if (key == 'TNB') {
- tableName = 'assets/docs/diabetesTable.html';
- }
- ///加载
- String htmlData = await rootBundle.loadString(tableName);
- print("加载数据完成 $htmlData");
- return htmlData;
- }
- Future<void> showDialogBox(String initialData, String jsonData) async {
- Get.dialog(Column(
- children: [
- Container(
- width: 900,
- alignment: Alignment.centerRight,
- padding: const EdgeInsets.only(right: 20, top: 10),
- color: Colors.white,
- child: IconButton(
- onPressed: () {
- Get.back();
- },
- icon: const Icon(
- Icons.close,
- ),
- ),
- ),
- Expanded(
- child: SizedBox(
- width: 900,
- child: InAppWebView(
- initialData: InAppWebViewInitialData(
- data: '<body style="padding:30px 100px;">$initialData </body>',
- mimeType: 'text/html',
- encoding: 'utf-8',
- ),
- onWebViewCreated: (controller) {
- controller.evaluateJavascript(
- source: "window.tableData = $jsonData;");
- },
- onConsoleMessage: (controller, consoleMessage) {
- // print(consoleMessage);
- },
- ),
- ),
- ),
- ],
- ));
- }
- Future<void> toFollowUpDetailPage(int index, FollowUpRecordDTO dto) async {
- var tableData = await getFollowUpTableData(dto, index);
- var initialData =
- await loadLocalAsset(dto.followUpRecordDatas?[index].key ?? '');
- var jsonData = jsonEncode(tableData);
- showDialogBox(initialData, jsonData);
- }
- /// 获取处方详情
- Future<PrescriptionDTO?> getPrescriptionDetailByFollowAnPatient(
- String followUpCode) async {
- return await _prescriptionManager.getPrescriptionDetailByFollowAnPatient(
- patientCode: patientCode,
- followUpCode: followUpCode,
- );
- }
- Future<Map<String, dynamic>> sharePrescription(
- FollowUpRecordDataDTO dataDTO,
- ) async {
- PrescriptionDTO? prescription =
- await getPrescriptionDetailByFollowAnPatient(dataDTO.code!);
- if (prescription == null) {
- return {};
- }
- return {
- "previewUrl": prescription.previewUrl ?? "",
- "key": prescription.prescriptionTemplateKey ?? "",
- };
- }
- toCheckPage(
- FollowUpRecordDataDTO dataDTO, {
- bool isCreateFromOldDto = false,
- }) async {
- await Get.put(MedicalController());
- _followUpController.state.followUpTime = dataDTO.followUpTime;
- _followUpController.state.nextFollowUpTime = dataDTO.nextFollowUpTime;
- _followUpController.state.followUpMode = dataDTO.followUpMode;
- if (dataDTO.followUpPhotos?.isNotEmpty ?? false) {
- _followUpController.state.followUpPhoto = dataDTO.followUpPhotos?.first;
- } else {
- _followUpController.state.followUpPhoto = "";
- }
- PrescriptionDTO? prescription =
- await getPrescriptionDetailByFollowAnPatient(dataDTO.code!);
- if (prescription != null && prescription.prescriptionData!.isNotEmpty) {
- FormInfo.instance.formValue = jsonDecode(prescription.prescriptionData!);
- }
- await Get.to(
- ConfigurableCard(
- cardKey: dataDTO.key!,
- examData: dataDTO.followUpData,
- followUpWidget: FollowUpFrom(cardKey: dataDTO.key!, dataDTO: dataDTO),
- patientCode: patientCode,
- callBack: (key, code, data, prescriptionKey) async {
- if (isCreateFromOldDto) {
- await createFollowUpFromOldDto(
- key,
- dataDTO.code,
- data,
- prescriptionKey,
- );
- } else {
- await initPrescription(dataDTO.code!, prescriptionKey ?? '');
- await updateFollowUp(key, dataDTO.code, data);
- if (prescriptionCode.isNullOrEmpty) {
- await createPrescription(dataDTO.code!, prescriptionKey ?? '');
- } else {
- await updatePrescription(dataDTO.code!, prescriptionKey ?? '');
- }
- }
- await getFollowUpRecordList();
- return true;
- },
- ),
- transition: Transition.rightToLeft,
- );
- await Get.find<MedicalController>().initRecordDataState();
- await Get.delete<MedicalController>();
- }
- final Map<String, String> followUpKeyValue = {
- 'FJHSFFW': "肺结核随访服务",
- 'FJHRHSF': "肺结核入户随访",
- 'GXB': "冠心病",
- 'NCZ': "脑卒中",
- 'YZJSZASFFW': "严重精神障碍随访服务",
- 'YZJSZAGRXXBC': "严重精神障碍个人信息补充",
- 'TNB': "2型糖尿病",
- 'GXY': "高血压",
- // 'LNRZYYJKGLFWJL': "老年人中医药健康管理服务记录表",
- 'LNRSHZLNLPGB': "老年人生活自理能力评估表",
- 'YCF_CH42TJKJCLB': "产后42天健康检查列表",
- 'YCF_CHFSLB': "产后访视列表",
- 'YCF_2_5CCQSFLB': "2~5次产前随访列表",
- 'YCF_DYCCQJCLB': "第一次产前检查列表",
- 'YCF_JBXX': "基本信息",
- 'ET_ZYYJKGLLB': "儿童中医药健康管理列表",
- 'ET_1MSETJKJCLB': "满月儿童健康检查",
- 'ET_3MSETJKJCLB': "3月龄儿童健康检查",
- 'ET_6MSETJKJCLB': "6月龄儿童健康检查",
- 'ET_8MSETJKJCLB': "8月龄儿童健康检查",
- 'ET_12MSETJKJCLB': "12月儿童健康检查",
- 'ET_18MSETJKJCLB': "18月儿童健康检查",
- 'ET_24MSETJKJCLB': "24月儿童健康检查",
- 'ET_30MSETJKJCLB': "30月儿童健康检查",
- 'ET_3SETJKJCLB': "3岁儿童健康检查列表",
- 'ET_4SETJKJCLB': "4岁儿童健康检查",
- 'ET_5SETJKJCLB': "5岁儿童健康检查",
- 'ET_6SETJKJCLB': "6岁儿童健康检查",
- 'ET_1_2SETJKJCLB': "1~2岁儿童健康检查列表",
- 'ET_1SNETJKJCLB': "1岁内儿童健康检查列表",
- 'ET_FamilyVisitRecord': "新生儿访视列表",
- 'FollowUpTuberculosisFirstRecord': "肺结核患者第一次入户随访记录表",
- 'FollowUpTuberculosisRecord': "肺结核患者随访服务记录表",
- 'FollowUpTuberculosisResultRecord': "肺结核患者结论表",
- };
- String getFollowUpValueByKey(String key) {
- if (followUpKeyValue[key] != null) {
- return followUpKeyValue[key]!;
- } else {
- return "";
- }
- }
- String followUpStateTransition(FollowUpStateEnum state) {
- switch (state) {
- case FollowUpStateEnum.NoFollowUp:
- return "未随访";
- case FollowUpStateEnum.FollowUpVisit:
- return "已随访";
- case FollowUpStateEnum.Cancelled:
- return "已作废";
- default:
- return "";
- }
- }
- MaterialColor followUpStateColors(FollowUpStateEnum state) {
- switch (state) {
- case FollowUpStateEnum.NoFollowUp:
- return Colors.grey;
- case FollowUpStateEnum.Cancelled:
- return Colors.red;
- case FollowUpStateEnum.FollowUpVisit:
- return Colors.green;
- default:
- return Colors.blue;
- }
- }
- String getFollowUpMode(FollowUpModeEnum? modeEnum) {
- switch (modeEnum) {
- case FollowUpModeEnum.Outpatient:
- return "门诊";
- case FollowUpModeEnum.Phone:
- return "电话";
- case FollowUpModeEnum.Visit:
- return "家庭";
- default:
- return "";
- }
- }
- Future<bool> createFollowUpFromOldDto(
- String key,
- String? code,
- data,
- prescriptionKey,
- ) async {
- return await _followUpController.createFollowUp(
- key,
- code,
- data,
- prescriptionKey,
- );
- }
- Future<void> initPrescription(
- String followUpCode, String prescriptionKey) async {
- // 获取处方列表
- List<PrescriptionDTO>? prescriptionList =
- await _prescriptionManager.getPrescriptionPage(
- patientCode: patientCode,
- followUpCode: followUpCode,
- );
- prescriptionCode = prescriptionList
- ?.firstWhereOrNull(
- (element) => element.prescriptionTemplateKey == prescriptionKey)
- ?.code ??
- "";
- }
- Future<bool?> updatePrescription(
- String followUpCode,
- String prescriptionKey,
- ) async {
- if (FormInfo.instance.formValue.isEmpty) {
- return null;
- }
- /// 创建处方
- bool? result = await _prescriptionManager.updatePrescription(
- patientCode: patientCode,
- followUpCode: followUpCode,
- prescriptionCode: prescriptionCode,
- prescriptionData: jsonEncode(FormInfo.instance.formValue),
- prescriptionKey: prescriptionKey,
- );
- if (result != null && result) {
- FormInfo.instance.formValue.clear();
- } else {
- PromptBox.toast("更新失败");
- }
- return result;
- }
- Future<String?> createPrescription(
- String followUpCode, String prescriptionKey) async {
- if (FormInfo.instance.formValue.isEmpty) {
- return null;
- }
- /// 创建处方
- String? prescriptionCode = await _prescriptionManager.createPrescription(
- patientCode: patientCode,
- followUpCode: followUpCode,
- prescriptionKey: prescriptionKey,
- prescriptionData: jsonEncode(FormInfo.instance.formValue),
- );
- FormInfo.instance.formValue.clear();
- return prescriptionCode;
- }
- }
|