|
|
|
<?php
|
|
|
|
declare(strict_types = 1);
|
|
|
|
|
|
|
|
namespace App\Http\Logics\Patient;
|
|
|
|
|
|
|
|
use App\Dictionary\Patient\CardType;
|
|
|
|
use App\Dictionary\Patient\Sex;
|
|
|
|
use App\Exceptions\GeneralException;
|
|
|
|
use App\Models\Patient;
|
|
|
|
use App\Services\HisHttp\Client;
|
|
|
|
use App\Utils\Traits\Logger;
|
|
|
|
use App\Utils\Traits\MiniProgramAuth;
|
|
|
|
use App\Utils\Traits\UniversalEncryption;
|
|
|
|
use Illuminate\Auth\AuthenticationException;
|
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
|
|
|
|
class PatientLogic
|
|
|
|
{
|
|
|
|
use Logger;
|
|
|
|
use MiniProgramAuth;
|
|
|
|
use UniversalEncryption;
|
|
|
|
|
|
|
|
private Client $his_client;
|
|
|
|
|
|
|
|
private Patient $patient_model;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PatientLogic Construct
|
|
|
|
* @throws AuthenticationException
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->authInitialize();
|
|
|
|
$this->patient_model = new Patient();
|
|
|
|
$this->his_client = app('HisHttpService');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 获取绑定患者列表
|
|
|
|
*/
|
|
|
|
public function getAllPatientLists()
|
|
|
|
{
|
|
|
|
return $this->patient_model->getBindPatientLists($this->open_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 卡详情
|
|
|
|
* @param string $patient_id
|
|
|
|
* @return Patient
|
|
|
|
* @throws GeneralException
|
|
|
|
*/
|
|
|
|
public function getPatientDetails(string $patient_id): Patient
|
|
|
|
{
|
|
|
|
$info = $this->patient_model->getBindPatientInfoByPatientId($this->open_id, $patient_id);
|
|
|
|
if(empty($info)) {
|
|
|
|
throw new GeneralException('找不到该就诊卡!');
|
|
|
|
}
|
|
|
|
|
|
|
|
// 获取患者信息
|
|
|
|
$response = $this->his_client->getPatientInfo($info['patient_id'], CardType::MEDICAL_CARD, $info['name']);
|
|
|
|
if (!isset($response['RESULTCODE']) || $response['RESULTCODE'] !== '0') {
|
|
|
|
throw new GeneralException($response['ERRORMSG'] ?? '找不到该就诊卡!', Response::HTTP_SERVICE_UNAVAILABLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
$info->patient_card_id = $response['MZHM'] ?? '';
|
|
|
|
$info->card_no = substr($response['CARDNO'], 0, 3). '**********'. substr($response['CARDNO'], -3);
|
|
|
|
|
|
|
|
return $info;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 建档
|
|
|
|
* @param array $data
|
|
|
|
* @return string
|
|
|
|
* @throws GeneralException
|
|
|
|
*/
|
|
|
|
public function createPatient(array $data): string
|
|
|
|
{
|
|
|
|
// 简单判断一下证件类型
|
|
|
|
$card_type = getIDCardType($data['card_no']);
|
|
|
|
switch ($card_type) {
|
|
|
|
// 身份证
|
|
|
|
case 1:
|
|
|
|
$sex = getGenderByIdCard($data['card_no']) === 1 ? Sex::MALE : Sex::WOMEN;
|
|
|
|
$birthday = getBirthdayByIdCard($data['card_no']);
|
|
|
|
break;
|
|
|
|
// 2017 / 2023 外国人永居证
|
|
|
|
case 3:
|
|
|
|
case 4:
|
|
|
|
if ($card_type == 3) {
|
|
|
|
$sex = Sex::from((int)$data['sex']);
|
|
|
|
$birthday = getBirthdayBy2017ForeignersIDCard($data['card_no']);
|
|
|
|
} else {
|
|
|
|
$sex = getGenderByIdCard($data['card_no']) === 1 ? Sex::MALE : Sex::WOMEN;
|
|
|
|
$birthday = getBirthdayByIdCard($data['card_no']);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$sex = Sex::from((int)$data['sex']);
|
|
|
|
$birthday = &$data['birthday'];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
//过滤名字特殊字符
|
|
|
|
$data['name'] = replaceSpecialChar($data['name']);
|
|
|
|
$card_type = CardType::from((int)$data['card_type']);
|
|
|
|
|
|
|
|
// 查询绑定超过X个
|
|
|
|
$bind_count = $this->patient_model->getBindPatientCount($this->open_id);
|
|
|
|
if ($bind_count >= config('custom.max_bind_patient_count')) {
|
|
|
|
throw new GeneralException('该微信达到绑定卡上限!');
|
|
|
|
}
|
|
|
|
|
|
|
|
// 查询患者信息
|
|
|
|
$response = $this->his_client->getPatientInfo($data['card_no'], $card_type, $data['name']);
|
|
|
|
$this->info('查询患者信息:', $response);
|
|
|
|
|
|
|
|
if (isset($response['RESULTCODE']) && $response['RESULTCODE'] === '0') {
|
|
|
|
$patient_id = &$response['PATIENTID'];
|
|
|
|
|
|
|
|
// 查询是否已绑定
|
|
|
|
$result = $this->patient_model->getPatientInfoByPatientId($patient_id);
|
|
|
|
if ($result && $result['open_id'] == $this->open_id) {
|
|
|
|
throw new GeneralException('您已绑定该就诊卡号!');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($result && $result['open_id'] != $this->open_id) {
|
|
|
|
throw new GeneralException('该卡号已被其他微信用户绑定!');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// 查询失败,走建档
|
|
|
|
$response = $this->his_client->registerCard(
|
|
|
|
$data['card_no'],
|
|
|
|
$card_type,
|
|
|
|
$data['name'],
|
|
|
|
$sex,
|
|
|
|
$birthday,
|
|
|
|
$data['card_no'],
|
|
|
|
$data['mobile_phone'],
|
|
|
|
$data['address']
|
|
|
|
);
|
|
|
|
$this->info('建档患者:'. $data['name']. '建档结果', $response);
|
|
|
|
|
|
|
|
if (!isset($response['RESULTCODE']) || $response['RESULTCODE'] !== '0') {
|
|
|
|
throw new GeneralException('建档失败,失败原因:'. $response['ERRORMSG'] ?? '未知错误', Response::HTTP_SERVICE_UNAVAILABLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
$patient_id = &$response['PATIENTID'];
|
|
|
|
}
|
|
|
|
|
|
|
|
// 写入数据库
|
|
|
|
$result = $this->patient_model->createPatient($this->union_id, $this->open_id, $patient_id, $data['name'], $sex);
|
|
|
|
|
|
|
|
if (!$result) {
|
|
|
|
throw new GeneralException('数据保存失败,请重试!', Response::HTTP_INTERNAL_SERVER_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $patient_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 绑定患者
|
|
|
|
* @param array $data
|
|
|
|
* @return string
|
|
|
|
* @throws GeneralException
|
|
|
|
*/
|
|
|
|
public function bindPatient(array $data): string
|
|
|
|
{
|
|
|
|
//过滤名字特殊字符
|
|
|
|
$data['name'] = replaceSpecialChar($data['name']);
|
|
|
|
$card_type = CardType::from((int)$data['card_type']);
|
|
|
|
|
|
|
|
// 查询超过X个
|
|
|
|
$bind_count = $this->patient_model->getBindPatientCount($this->open_id);
|
|
|
|
if ($bind_count >= config('custom.max_bind_patient_count')) {
|
|
|
|
throw new GeneralException('该微信达到绑定卡上限!');
|
|
|
|
}
|
|
|
|
|
|
|
|
// 查询患者信息
|
|
|
|
$response = $this->his_client->getPatientInfo($data['card_no'], $card_type, $data['name']);
|
|
|
|
$this->info('查询患者信息:', $response);
|
|
|
|
|
|
|
|
if (!isset($response['RESULTCODE']) || $response['RESULTCODE'] != '0') {
|
|
|
|
throw new GeneralException($response['ResultContent'] ?? '未知错误');
|
|
|
|
}
|
|
|
|
|
|
|
|
$patient_info = &$response;
|
|
|
|
$sex = Sex::from((int) $patient_info['SEX']);
|
|
|
|
|
|
|
|
if ($patient_info['PATIENTID'] != $data['patient_id']) {
|
|
|
|
throw new GeneralException('该证件号已建档,但就诊卡号不匹配!');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($patient_info['NAME'] != $data['name']) {
|
|
|
|
throw new GeneralException('该证件号已建档,但姓名不匹配!');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($patient_info['CARDNO'] != $data['card_no']) {
|
|
|
|
throw new GeneralException('该就诊号已建档,但证件号码不匹配!');
|
|
|
|
}
|
|
|
|
|
|
|
|
// 查询是否已绑定
|
|
|
|
$result = $this->patient_model->getPatientInfoByPatientId($data['patient_id']);
|
|
|
|
if ($result && $result['openid'] == $this->open_id) {
|
|
|
|
throw new GeneralException('您已绑定该就诊卡号!');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($result && $result['openid'] != $this->open_id) {
|
|
|
|
throw new GeneralException('该卡号已被其他微信用户绑定!');
|
|
|
|
}
|
|
|
|
|
|
|
|
// 写入数据库
|
|
|
|
$result = $this->patient_model->createPatient($this->union_id, $this->open_id, $patient_info['PATIENTID'], $data['name'], $sex);
|
|
|
|
|
|
|
|
if (!$result) {
|
|
|
|
throw new GeneralException('数据保存失败,请重试!', Response::HTTP_INTERNAL_SERVER_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $patient_info['PATIENTID'];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 设置默认就诊卡
|
|
|
|
* @param string $patient_id
|
|
|
|
* @return bool
|
|
|
|
* @throws GeneralException
|
|
|
|
*/
|
|
|
|
public function setDefaultPatient(string $patient_id): bool
|
|
|
|
{
|
|
|
|
$info = $this->patient_model->getBindPatientInfoByPatientId($this->open_id, $patient_id);
|
|
|
|
if (empty($info)) {
|
|
|
|
throw new GeneralException('该就诊卡不存在!');
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = $this->patient_model->setDefaultPatient($this->open_id, $patient_id);
|
|
|
|
if (!$result) {
|
|
|
|
throw new GeneralException('设置失败,请稍后再试!', Response::HTTP_INTERNAL_SERVER_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 解绑
|
|
|
|
* @param string $patient_id
|
|
|
|
* @return bool
|
|
|
|
* @throws GeneralException
|
|
|
|
*/
|
|
|
|
public function cancelBindPatient(string $patient_id): bool
|
|
|
|
{
|
|
|
|
$info = $this->patient_model->getBindPatientInfoByPatientId($this->open_id, $patient_id);
|
|
|
|
if (empty($info)) {
|
|
|
|
throw new GeneralException('该就诊卡不存在!');
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = $this->patient_model->deletePatient($this->open_id, $patient_id);
|
|
|
|
if (!$result) {
|
|
|
|
throw new GeneralException('解绑失败,请稍后再试!', Response::HTTP_INTERNAL_SERVER_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|