香洲二院小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
mini_xzey/app/Http/Logics/Registration/RegisterLogic.php

252 lines
9.0 KiB

<?php
declare(strict_types = 1);
namespace App\Http\Logics\Registration;
use App\Dictionary\Order\PayType;
use App\Dictionary\Order\SourceId;
use App\Dictionary\Order\Type;
use App\Dictionary\Patient\CardType;
use App\Exceptions\GeneralException;
use App\Models\Order;
use App\Models\Patient;
use App\Services\HisHttp\Client;
use App\Utils\Traits\Logger;
use App\Utils\Traits\MiniProgramAuth;
use Illuminate\Auth\AuthenticationException;
use ReflectionException;
use Symfony\Component\HttpFoundation\Response;
use UnifyPayment\Cores\Exceptions\InvalidConfigException;
use UnifyPayment\Cores\Exceptions\RuntimeException;
use UnifyPayment\Cores\Struct\CreateOrder;
use UnifyPayment\Mock\CreateOrderHandler;
use UnifyPayment\Unify;
class RegisterLogic
{
use Logger;
use MiniProgramAuth;
private Client $his_client;
private Order $order_model;
private Patient $patient_model;
/**
* RegisterLogic Construct
* @throws AuthenticationException
*/
public function __construct()
{
$this->authInitialize();
$this->setChannel('registration');
$this->his_client = app('HisHttpService');
$this->order_model = new Order();
$this->patient_model = new Patient();
}
/**
* @param string $patient_id
* @param string $date
* @param string $dept_id
* @param string $doctor_id
* @param string $reg_id
* @return array
* @throws GeneralException
*/
public function register(string $patient_id, string $date, string $dept_id, string $doctor_id, string $reg_id): array
{
// 基础信息
$patient_info = $this->getPatientInfo($patient_id, $this->open_id);
$schedule_info = $this->getRegisterScheduleDetails($date, $dept_id, $doctor_id, $reg_id);
// 锁号?
// 创建订单
$order_type = $date === date('Y-m-d') ? Type::TODAY_REGISTRATION : Type::APPOINTMENT_REGISTRATION;
$pay_type = PayType::WECHAT_PAY;
$order_id = $this->order_model->getOrderId($pay_type, 'M');
$reg_fee = (float)(string) $schedule_info['SHIFT']['FEE'];
$order = $this->createOrder($order_id, $pay_type, $reg_fee, $order_type, $patient_info, $schedule_info);
// 申请支付
$pay_data = $this->applyPayment($order_type, $order_id, $reg_fee, $patient_info['PATIENTID'], $patient_info['NAME']);
// 去除无用数据
unset($pay_data['merchantId'], $pay_data['merchantName'], $pay_data['channelId'], $pay_data['channelName']);
return $pay_data;
}
/**
* 获取患者信息
* @param string $patient_id
* @param string $open_id
* @return mixed
* @throws GeneralException
*/
protected function getPatientInfo(string $patient_id, string $open_id): mixed
{
$info = $this->patient_model->getBindPatientInfo($open_id, $patient_id);
if (empty($info)) {
throw new GeneralException('找不到患者信息,请重新再试!', Response::HTTP_BAD_REQUEST);
}
$patient_info = $this->his_client->getPatientInfo($info['patient_id'], CardType::OUTPATIENT_NO, $info['name']);
if (!isset($patient_info['RESULTCODE']) || $patient_info['RESULTCODE'] !== '0') {
throw new GeneralException($patient_info['ERRORMSG'] ?? '找不到患者信息,请重新再试!', Response::HTTP_SERVICE_UNAVAILABLE);
}
// 添加Patient 表ID
$patient_info['id'] = $info['id'];
$this->info('挂号患者信息', $patient_info);
return $patient_info;
}
/**
* 获取挂号信息
* @param string $date
* @param string $dept_id
* @param string $doctor_id
* @param string $reg_id
* @return array
* @throws GeneralException
*/
protected function getRegisterScheduleDetails(string $date, string $dept_id, string $doctor_id, string $reg_id): array
{
// 获取排班医生信息
$is_today = $dept_id === date('Y-m-d') ? '3' : '1';
$schedule_info = $this->his_client->getDoctorLists($dept_id, $is_today, '', $date);
if (!isset($schedule_info['RESULTCODE']) || $schedule_info['RESULTCODE'] !== '0') {
throw new GeneralException($schedule_info['ERRORMSG'] ?? '找不到该号源,请重新再试!', Response::HTTP_SERVICE_UNAVAILABLE);
}
// 获取号源信息
$schedule_info = xmlArrayToListByKey($schedule_info, 'ITEM');
foreach ($schedule_info['ITEM'] as $v) {
if ($v['DOCTID'] === $doctor_id) {
$v = xmlArrayToListByKey($v, 'SHIFT');
foreach ($v['SHIFT'] as $v2) {
if ($v2['REGID'] === $reg_id && $v2['FDATE'] === $date) {
$v['SHIFT'] = $v2;
$info = $v;
}
}
}
}
if (empty($info)) {
throw new GeneralException('找不到该号源,请重新再试!', Response::HTTP_BAD_REQUEST);
}
if (!isset($info['SHIFT']['REGCOUNT']) || $info['SHIFT']['REGCOUNT'] <= 0) {
throw new GeneralException('该号源已挂完,请重新选择号源!', Response::HTTP_BAD_REQUEST);
}
// 获取科室名称
$dept_lists = $this->his_client->getDepType('', '','01', $date);
if (!isset($schedule_info['RESULTCODE']) || $schedule_info['RESULTCODE'] !== '0') {
throw new GeneralException($schedule_info['ERRORMSG'] ?? '找不到该号源,请重新再试!', Response::HTTP_SERVICE_UNAVAILABLE);
}
$dept_lists = xmlArrayToListByKey($dept_lists, 'ITEM');
foreach ($dept_lists['ITEM'] as $v) {
if ($v['DEPID'] === $dept_id) {
$info['dept_id'] = $v['DEPID'];
$info['dept_name'] = $v['DEPNAME'];
}
}
$this->info('挂号排班信息', $info);
return $info;
}
/**
* 创建订单表
* @param string $order_id
* @param PayType $pay_type
* @param float $reg_fee
* @param Type $order_type
* @param array $patient_info
* @param array $schedule_info
* @return mixed
* @throws GeneralException
*/
protected function createOrder(string $order_id, PayType $pay_type, float $reg_fee, Type $order_type, array $patient_info, array $schedule_info): mixed
{
// 挂号记录表
$reg_record_data = [
'relate_patient_id' => $patient_info['id'],
'reg_id' => $schedule_info['SHIFT']['REGID'],
'dept_id' => $schedule_info['dept_id'],
'dept_name' => $schedule_info['dept_name'],
'dept_location' => $schedule_info['DEPLOCATION'],
'doctor_id' => $schedule_info['DOCTID'],
'doctor_name' => $schedule_info['DOCTNAME'],
'visit_date' => date('Y-m-d', strtotime($schedule_info['SHIFT']['FDATE'])),
'begin_time' => $schedule_info['SHIFT']['STARTTIME'],
'end_time' => $schedule_info['SHIFT']['ENDTIME'],
'lock_status' => 0,
'extra_info' => json_encode($schedule_info, JSON_UNESCAPED_UNICODE),
];
$order = $this->order_model->createOrder(
$order_id,
$pay_type,
$reg_fee * 100,
0,
$this->open_id,
$patient_info['PATIENTID'],
$patient_info['NAME'],
$order_type,
SourceId::MINI_PROGRAM,
$reg_record_data
);
if (empty($order)) {
throw new GeneralException('创建挂号单失败,请重新再试!');
}
$this->info('创建订单,ID:'. $order->id);
return $order;
}
/**
* 申请支付
* @param Type $order_type
* @param string $order_id
* @param float $reg_fee
* @param string $patient_id
* @param string $patient_name
* @return array|string
* @throws GeneralException
*/
protected function applyPayment(Type $order_type, string $order_id, float $reg_fee, string $patient_id, string $patient_name): array|string
{
try {
$order_obj = new CreateOrder(
$order_type->label(),
$order_id,
(string) $reg_fee,
'1',
$patient_id. '|'. $patient_name,
'A',
$this->open_id,
url('/Api/Notify', [], true)
);
$response = Unify::pay(config('unify'))->mini->setMockHandler([new CreateOrderHandler(true)])->jsapi($order_obj);
$this->info('jsapi 支付参数', $response);
if (!$response['success'] || empty($response['response'])) {
throw new GeneralException('申请支付失败,请重新再试!', Response::HTTP_SERVICE_UNAVAILABLE);
}
return $response['response'];
} catch (InvalidConfigException|RuntimeException|ReflectionException $e) {
throw new GeneralException('申请支付失败,请重新再试!', Response::HTTP_INTERNAL_SERVER_ERROR);
}
}
}