authInitialize(); $this->setChannel('refund'); $this->his_client = app('HisHttpService'); $this->reg_record_model = new RegistrationRecord(); $this->order_model = new Order(); } /** * 获取挂号记录列表 * @param string $patient_id * @param string $start_date * @param string $end_date * @return array * @throws GeneralException */ public function getRecordLists(string $patient_id, string $start_date, string $end_date): array { $response = $this->his_client->getRegisterRecordLists($patient_id, $start_date, $end_date); if (!isset($response['RESULTCODE']) || $response['RESULTCODE'] !== '0') { throw new GeneralException($response['ERRORMSG'] ?? '暂无相关挂号记录!', Response::HTTP_SERVICE_UNAVAILABLE); } // 缓存2小时 Cache::set('Registration.Record.'. $this->open_id.'.'. $patient_id, json_encode($response, JSON_UNESCAPED_UNICODE), 2 * 60 * 60); return $response; } /** * 退号 * @param string $patient_id * @param string $reg_serial_no * @return true * @throws GeneralException */ public function refundRegisterRecord(string $patient_id, string $reg_serial_no): true { $cache_key = 'Registration.Record.'. $this->open_id.'.'. $patient_id; $record_info = Cache::get($cache_key); if (empty($record_info)) { throw new GeneralException($response['ERRORMSG'] ?? '查询不到需要退号的挂号记录,请重新再试!', Response::HTTP_SERVICE_UNAVAILABLE); } $record_info = json_decode($record_info, true); // 获取具体的预约详情 $record_info = xmlArrayToListByKey($record_info, 'ITEM'); foreach ($record_info['ITEM'] as $v) { if ($v['VISITNO'] === $reg_serial_no) { $info = $v; break; } } if (empty($info)) { throw new GeneralException('查询不到需要退号的挂号记录,请重新再试!', Response::HTTP_SERVICE_UNAVAILABLE); } $this->info('患者需退号的挂号记录', $info); // 查询小程序上的挂号记录 $reg_record = $this->reg_record_model->getRecordByRegID($reg_serial_no); if (empty($reg_record) || empty($reg_record->order()->order_id)) { throw new GeneralException('非小程序渠道挂号,请在人工窗口退号退费处理', Response::HTTP_SERVICE_UNAVAILABLE); } $order_info = &$reg_record->order(); $order_id = &$order_info->order_id; $fee = &$order_info->self_fee; $this->info('患者需退号的数据库挂号记录', $reg_record->toArray()); // 检查是否可以退号 $response = $this->his_client->checkRefundRegisterStatus($reg_serial_no); $this->info('检查是否可进行退号', $response); if (!isset($response['RESULTCODE']) || $response['RESULTCODE'] !== '0') { throw new GeneralException($response['ERRORMSG'] ?? '当前挂号记录不可退号!', Response::HTTP_BAD_REQUEST); } // 开始退号 $response = $this->his_client->refundRegister($reg_serial_no, $order_id, date('Y-m-d'), date('H:i:s'), (string) ($fee / 100)); $this->info('退号结果', $response); if (!isset($response['RESULTCODE']) || $response['RESULTCODE'] !== '0') { throw new GeneralException($response['ERRORMSG'] ?? '退号失败,请重新再试!', Response::HTTP_BAD_REQUEST); } // 创建退款单 $refund_order_id = $this->order_model->getRefundOrderId($order_id); $refund_order_info = $this->order_model->createRefundOReverseOrder( $order_info->id, $refund_order_id, PayType::from($order_info->pay_type), $fee, $this->open_id, $patient_id, $order_info->patient_name, Type::from($order_info->order_type), SourceId::from($order_info->source_id) ); $this->info('创建退款订单', ['id' => $refund_order_info->id]); if (empty($refund_order_info)) { throw new GeneralException($response['ERRORMSG'] ?? '退号成功,退费失败,请重新再试!', Response::HTTP_BAD_REQUEST); } // 退款 try { $refund_order_obj = new RefundOrder($order_id, $refund_order_id, $fee, '患者自行退号退费'); $response = Unify::common(env('unify'))->order->refund($refund_order_obj); $this->info('退号退费结果', $response); } catch (ReflectionException $e) { $this->order_model->reverseOrderOpera($refund_order_id, $fee, false); throw new GeneralException($e->getMessage() ?? '退号成功,退费失败,请重新再试!', Response::HTTP_SERVICE_UNAVAILABLE); } if (empty($response) || $response['status'] !== 200 || $response['success'] !== true) { $this->order_model->reverseOrderOpera($refund_order_id, $fee, false); throw new GeneralException($response['msg'] ?? '退号成功,退费失败,请重新再试!', Response::HTTP_BAD_REQUEST); } $this->order_model->reverseOrderOpera($refund_order_id, $fee, true); return true; } }