123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- import 'package:fis_jsonrpc/rpc.dart';
- import 'package:get/get.dart';
- import 'package:vitalapp/architecture/defines.dart';
- import 'package:vitalapp/architecture/utils/prompt_box.dart';
- import 'package:vitalapp/managers/interfaces/patient.dart';
- import 'package:vitalapp/pages/controllers/crowd_labels.dart';
- import 'package:vitalapp/pages/controllers/home_nav_mixin.dart';
- import 'package:vitalapp/pages/patient/card_reader/index.dart';
- import 'package:vitalapp/pages/patient/create/state.dart';
- import 'package:vitalapp/pages/patient/list/controller.dart';
- import 'package:vitalapp/routes/nav_ids.dart';
- class CreatePatientController extends FControllerBase with HomeNavMixin {
- final _patientManager = Get.find<IPatientManager>();
- final crowdLabelsController = Get.find<CrowdLabelsController>();
- final state = CreatePatientState();
- @override
- Future<void> onLoad() {
- final params = Get.parameters;
- if (params.containsKey("from")) {
- if (params["from"] == "list") {
- state.isCreateOnly = true;
- }
- }
- return super.onLoad();
- }
- /// 保存档案,调整签约
- void gotoSignContract() async {
- setBusy("正在保存...");
- final code = await _submitForm();
- if (code == null) {
- busy = false;
- PromptBox.toast("保存失败");
- return;
- }
- // final code = "13B95A2B2790464BBFD9B30A71F15C95";
- busy = false;
- Future.delayed(
- const Duration(milliseconds: 800),
- () {
- state.reset(); // 重置状态
- },
- );
- Get.toNamed(
- "/contract/package_list",
- parameters: {"patientCode": code},
- );
- // Get.find<HomeController>().switchNavByName("/patient/list");
- // Future.delayed(
- // const Duration(milliseconds: 800),
- // () {
- // // TODO:
- // Get.find<PatientListController>().gotoDetail(code);
- // busy = false;
- // },
- // );
- }
- /// 存为档案,调整到档案详情
- void gotoPatientDetail() async {
- setBusy("正在保存...");
- final code = await _submitForm();
- if (code == null) {
- busy = false;
- PromptBox.toast("保存失败");
- return;
- } else {
- Future.delayed(
- const Duration(milliseconds: 800),
- () {
- state.reset(); // 重置状态
- busy = false;
- PromptBox.toast("保存成功");
- },
- );
- }
- ///不跳转到详情页
- // Get.find<HomeController>().switchNavByName("/patient/list");
- Get.put(PatientListController());
- Future.delayed(
- const Duration(milliseconds: 800),
- () {
- Get.find<PatientListController>().gotoDetail(code);
- busy = false;
- },
- );
- }
- /// 打开读卡器弹窗
- void openCardReader() async {
- final CardReaderResult? result = await Get.dialog<CardReaderResult>(
- const CardReaderDialog(),
- );
- if (result != null && result.success) {
- print("读卡成功,身份证号:${result.code}");
- } else {
- print("读卡取消");
- }
- }
- /// 保存并返回
- void saveAndBack() async {
- setBusy("正在保存...");
- final code = await _submitForm();
- busy = false;
- if (code == null) {
- busy = false;
- return;
- }
- Get.back(result: code, id: NavIds.HOME);
- }
- /// 点击读卡事件
- void onReadCardClicked() {
- Get.snackbar(
- "提示",
- "此功能尚未开发",
- duration: const Duration(seconds: 2),
- );
- }
- /// 处理 “同户籍地址” 勾选变更事件
- void onSyncAddressCheckChanged(bool isChecked) {
- state.isSyncAddresses = isChecked;
- if (isChecked) {
- // 同步户籍地址到现住地址
- state.address = state.censusRegister;
- } else {
- state.address = "";
- }
- }
- /// 处理户籍地址变更
- void onCensusRegisterChanged(String value) {
- state.censusRegister = value;
- if (state.isSyncAddresses) {
- state.address = value;
- }
- }
- Future<String?> _submitForm() async {
- final validateMsg = await _validateForm();
- if (validateMsg != null) {
- toast(validateMsg);
- return null;
- }
- final crowdLabelCodes = crowdLabelsController.state.selectedCodes;
- final request = CreatePatientRequest(
- patientName: state.name,
- phone: state.phoneNo,
- patientGender: state.gender,
- nationality: state.nation,
- birthday: state.birthday?.toUtc(),
- cardType: state.cardType,
- cardNo: state.cardNo,
- patientAddress: state.address,
- permanentResidenceAddress: state.censusRegister,
- crowdLabels: crowdLabelCodes,
- );
- final result = await _patientManager.create(request);
- return result;
- }
- bool validateIDCard(String idCard) {
- // 校验身份证号码长度
- if (idCard.length != 18) {
- return false;
- }
- // 校验前17位是否为数字
- String idCard17 = idCard.substring(0, 17);
- if (!isNumeric(idCard17)) {
- return false;
- }
- // 校验最后一位校验码
- String checkCode = getCheckCode(idCard17);
- if (idCard[17].toUpperCase() != checkCode) {
- return false;
- }
- return true;
- }
- bool isNumeric(String str) {
- if (str.isEmpty) {
- return false;
- }
- return double.tryParse(str) != null;
- }
- String getCheckCode(String idCard17) {
- List<int> coefficients = [
- 7,
- 9,
- 10,
- 5,
- 8,
- 4,
- 2,
- 1,
- 6,
- 3,
- 7,
- 9,
- 10,
- 5,
- 8,
- 4,
- 2
- ];
- List<String> checkCodes = [
- '1',
- '0',
- 'X',
- '9',
- '8',
- '7',
- '6',
- '5',
- '4',
- '3',
- '2'
- ];
- int sum = 0;
- for (int i = 0; i < idCard17.length; i++) {
- int digit = int.parse(idCard17[i]);
- sum += digit * coefficients[i];
- }
- int remainder = sum % 11;
- return checkCodes[remainder];
- }
- Future<String?> _validateForm() async {
- if (state.name.isEmpty) {
- return "请填写姓名";
- }
- if (state.cardNo.isEmpty) {
- return "请填写证件号";
- }
- bool isNotIDCard = validateIDCard(state.cardNo);
- if (!isNotIDCard && state.cardType == CardTypeEnum.Identity) {
- return "请填写正确的证件号";
- }
- /// TODO 需求变更暂时删除
- // final selectedNormalCodes = crowdLabelsController.state.selectedNormalCodes;
- // if (selectedNormalCodes.length > 1) {
- // return "人群分类:一般人群、儿童、孕妇、老年人,只可选择其一!";
- // }
- // final crowdLabelCodes = crowdLabelsController.state.selectedCodes;
- // if (crowdLabelCodes.isEmpty) {
- // return "请选择人群分类";
- // }
- // if (state.gender == GenderEnum.Male &&
- // crowdLabelCodes.contains('RQFL_YF')) {
- // return "当前居民性别为“男”,人群分类不可选择孕妇!";
- // }
- return null;
- }
- }
|