controller.dart 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import 'package:fis_jsonrpc/rpc.dart';
  2. import 'package:get/get.dart';
  3. import 'package:vnoteapp/architecture/defines.dart';
  4. import 'package:vnoteapp/architecture/utils/prompt_box.dart';
  5. import 'package:vnoteapp/managers/interfaces/patient.dart';
  6. import 'package:vnoteapp/pages/controllers/crowd_labels.dart';
  7. import 'package:vnoteapp/pages/controllers/home_nav_mixin.dart';
  8. import 'package:vnoteapp/pages/patient/create/state.dart';
  9. import 'package:vnoteapp/pages/patient/list/controller.dart';
  10. import 'package:vnoteapp/routes/nav_ids.dart';
  11. class CreatePatientController extends FControllerBase with HomeNavMixin {
  12. final _patientManager = Get.find<IPatientManager>();
  13. final state = CreatePatientState();
  14. @override
  15. Future<void> onLoad() {
  16. final params = Get.parameters;
  17. if (params.containsKey("from")) {
  18. if (params["from"] == "list") {
  19. state.isCreateOnly = true;
  20. }
  21. }
  22. return super.onLoad();
  23. }
  24. /// 保存档案,调整签约
  25. void gotoSignContract() async {
  26. setBusy("正在保存...");
  27. final code = await _submitForm();
  28. if (code == null) {
  29. busy = false;
  30. PromptBox.toast("保存失败");
  31. return;
  32. }
  33. // final code = "13B95A2B2790464BBFD9B30A71F15C95";
  34. busy = false;
  35. Future.delayed(
  36. const Duration(milliseconds: 800),
  37. () {
  38. state.reset(); // 重置状态
  39. },
  40. );
  41. Get.toNamed(
  42. "/contract/package_list",
  43. parameters: {"patientCode": code},
  44. );
  45. // Get.find<HomeController>().switchNavByName("/patient/list");
  46. // Future.delayed(
  47. // const Duration(milliseconds: 800),
  48. // () {
  49. // // TODO:
  50. // Get.find<PatientListController>().gotoDetail(code);
  51. // busy = false;
  52. // },
  53. // );
  54. }
  55. /// 存为档案,调整到档案详情
  56. void gotoPatientDetail() async {
  57. setBusy("正在保存...");
  58. final code = await _submitForm();
  59. if (code == null) {
  60. busy = false;
  61. PromptBox.toast("保存失败");
  62. return;
  63. } else {
  64. Future.delayed(
  65. const Duration(milliseconds: 800),
  66. () {
  67. state.reset(); // 重置状态
  68. busy = false;
  69. PromptBox.toast("保存成功");
  70. },
  71. );
  72. }
  73. ///不跳转到详情页
  74. // Get.find<HomeController>().switchNavByName("/patient/list");
  75. Get.put(PatientListController());
  76. Future.delayed(
  77. const Duration(milliseconds: 800),
  78. () {
  79. Get.find<PatientListController>().gotoDetail(code);
  80. busy = false;
  81. },
  82. );
  83. }
  84. /// 保存并返回
  85. void saveAndBack() async {
  86. setBusy("正在保存...");
  87. final code = await _submitForm();
  88. busy = false;
  89. if (code == null) {
  90. busy = false;
  91. return;
  92. }
  93. Get.back(result: code, id: NavIds.HOME);
  94. }
  95. /// 点击读卡事件
  96. void onReadCardClicked() {
  97. Get.snackbar(
  98. "提示",
  99. "此功能尚未开发",
  100. duration: const Duration(seconds: 2),
  101. );
  102. }
  103. /// 处理 “同户籍地址” 勾选变更事件
  104. void onSyncAddressCheckChanged(bool isChecked) {
  105. state.isSyncAddresses = isChecked;
  106. if (isChecked) {
  107. // 同步户籍地址到现住地址
  108. state.address = state.censusRegister;
  109. } else {
  110. state.address = "";
  111. }
  112. }
  113. /// 处理户籍地址变更
  114. void onCensusRegisterChanged(String value) {
  115. state.censusRegister = value;
  116. if (state.isSyncAddresses) {
  117. state.address = value;
  118. }
  119. }
  120. Future<String?> _submitForm() async {
  121. final validateMsg = await _validateForm();
  122. if (validateMsg != null) {
  123. toast(validateMsg);
  124. return null;
  125. }
  126. final crowdLabelCodes =
  127. Get.find<CrowdLabelsController>().state.selectedCodes;
  128. final request = CreatePatientRequest(
  129. patientName: state.name,
  130. phone: state.phoneNo,
  131. patientGender: state.gender,
  132. nationality: state.nation,
  133. birthday: state.birthday?.toUtc(),
  134. cardType: state.cardType,
  135. cardNo: state.cardNo,
  136. patientAddress: state.address,
  137. permanentResidenceAddress: state.censusRegister,
  138. crowdLabels: crowdLabelCodes,
  139. );
  140. final result = await _patientManager.create(request);
  141. return result;
  142. }
  143. Future<String?> _validateForm() async {
  144. if (state.name.isEmpty) {
  145. return "请填写姓名";
  146. }
  147. if (state.cardNo.isEmpty) {
  148. return "请填写证件号";
  149. }
  150. final crowdLabelCodes =
  151. Get.find<CrowdLabelsController>().state.selectedCodes;
  152. if (crowdLabelCodes.isEmpty) {
  153. return "请选择人群分类";
  154. }
  155. return null;
  156. }
  157. }