553 lines
23 KiB
PHP
553 lines
23 KiB
PHP
<?php
|
||
|
||
namespace App\Services;
|
||
|
||
use App\Constants\HttpEnumCode;
|
||
use App\Exception\BusinessException;
|
||
use App\Model\UserCaCert;
|
||
use App\Model\HospitalDepartmentCustom;
|
||
use App\Model\OrderInquiryCase;
|
||
use App\Model\OrderPrescription;
|
||
use App\Model\OrderPrescriptionIcd;
|
||
use App\Model\OrderPrescriptionProduct;
|
||
use App\Model\OrderProductItem;
|
||
use App\Model\User;
|
||
use App\Model\UserDoctor;
|
||
use App\Model\UserDoctorInfo;
|
||
use App\Model\UserPharmacistInfo;
|
||
use Extend\Alibaba\Oss;
|
||
use Extend\Ca\Ca;
|
||
use Hyperf\Contract\LengthAwarePaginatorInterface;
|
||
use Intervention\Image\ImageManager;
|
||
use TCPDF;
|
||
|
||
class OrderPrescriptionService extends BaseService
|
||
{
|
||
/**
|
||
* 获取医生是否存在被驳回处方
|
||
* @param string $doctor_id 医生id
|
||
* @return bool
|
||
*/
|
||
public function getDoctorExistsAuditFail(string $doctor_id): bool
|
||
{
|
||
$params = array();
|
||
$params['doctor_id'] = $doctor_id;
|
||
$params['pharmacist_audit_status'] = 2;// 处方审核状态(0:审核中 1:审核成功 2:审核驳回)
|
||
$params['platform_audit_status'] = 1;// 处方平台审核状态(0:审核中 1:审核成功 2:审核驳回)
|
||
$params['is_delete'] = 0;// 是否删除(0:否 1:是)
|
||
return OrderPrescription::getExists($params);
|
||
}
|
||
|
||
/**
|
||
* 获取药师待审核处方-分页
|
||
* @param string $pharmacist_id 药师id
|
||
* @param int $pharmacist_audit_status 药师审核状态(0:审核中 1:审核成功 2:审核驳回)
|
||
* @param int $platform_audit_status
|
||
* @param string|int $page
|
||
* @param string|int $per_page
|
||
* @return array
|
||
*/
|
||
public function getPharmacistWaitAuditPage(string $pharmacist_id, int $pharmacist_audit_status, int $platform_audit_status, string|int $page = 1, string|int $per_page = 10): array
|
||
{
|
||
$params = array();
|
||
$params['pharmacist_id'] = $pharmacist_id;
|
||
$params['pharmacist_audit_status'] = $pharmacist_audit_status; // 药师审核状态(0:审核中 1:审核成功 2:审核驳回)
|
||
$params['platform_audit_status'] = $platform_audit_status; // 处方平台审核状态(0:审核中 1:审核成功 2:审核驳回)
|
||
|
||
return OrderPrescription::getPage($params, ['*'], $page, $per_page);
|
||
}
|
||
|
||
/**
|
||
* 获取处方中开方药品
|
||
* @param string|int $order_prescription_id
|
||
* @return array
|
||
*/
|
||
public function getproductList(string|int $order_prescription_id): array
|
||
{
|
||
$params = array();
|
||
$params['order_prescription_id'] = $order_prescription_id;
|
||
$order_prescription_products = OrderPrescriptionProduct::getLimit($params);
|
||
if (empty($order_prescription_products)) {
|
||
return [];
|
||
}
|
||
|
||
$result = [];
|
||
|
||
foreach ($order_prescription_products as $order_prescription_product) {
|
||
$data = array();
|
||
$data['product_id'] = $order_prescription_product['product_id'];
|
||
$data['prescription_product_num'] = $order_prescription_product['prescription_product_num'];
|
||
$data['product_name'] = $order_prescription_product['product_name'] ?? "";
|
||
$data['product_spec'] = $order_prescription_product['product_spec'] ?? "";
|
||
$data['single_unit'] = $order_prescription_product['single_unit'] ?? "";
|
||
$data['single_use'] = $order_prescription_product['single_use'] ?? "";
|
||
$data['packaging_unit'] = $order_prescription_product['packaging_unit'] ?? "";
|
||
$data['frequency_use'] = $order_prescription_product['frequency_use'] ?? "";
|
||
$data['available_days'] = $order_prescription_product['available_days'] ?? 0;
|
||
|
||
$result[] = $data;
|
||
}
|
||
|
||
unset($order_product_items);
|
||
|
||
return $result;
|
||
}
|
||
|
||
/**
|
||
* 获取患者某一状态下的处方数量
|
||
* @param string $patient_id 患者id
|
||
* @param int $prescription_status 处方状态(1:待审核 2:待使用 3:已失效 4:已使用)
|
||
* @return int
|
||
*/
|
||
public function getPatientPrescriptionWithStatus(string $patient_id, int $prescription_status): int
|
||
{
|
||
$params = array();
|
||
$params['patient_id'] = $patient_id;
|
||
$params['prescription_status'] = $prescription_status;
|
||
$params['pharmacist_audit_status'] = 1;
|
||
return OrderPrescription::getCount($params);
|
||
}
|
||
|
||
|
||
/**
|
||
* 开具处方
|
||
* 医生-正常开具
|
||
* 药师-先开具药师处方,再开具医院签章
|
||
* @param string $order_prescription_id
|
||
* @param string $user_id
|
||
* @return array
|
||
*/
|
||
public function openPrescription(string $order_prescription_id,string $user_id): array
|
||
{
|
||
try {
|
||
// 获取用户数据
|
||
$params = array();
|
||
$params['user_id'] = $user_id;
|
||
$user = User::getOne($params);
|
||
if (empty($user)){
|
||
throw new BusinessException("用户数据错误");
|
||
}
|
||
|
||
if ($user['user_type'] != 2 && $user['user_type'] != 3){
|
||
throw new BusinessException("用户类型错误");
|
||
}
|
||
|
||
// 获取用户info数据
|
||
$params = array();
|
||
$params['user_id'] = $user['user_id'];
|
||
if ($user['user_type'] == 2){
|
||
$user_doctor_info = UserDoctorInfo::getOne($params);
|
||
if (empty($user_doctor_info)){
|
||
throw new BusinessException("用户数据错误");
|
||
}
|
||
|
||
$user_sign_image_path = $user_doctor_info['sign_image'];
|
||
$user_entity_id = "491925054435950592";
|
||
} else{
|
||
$user_pharmacist_info = UserPharmacistInfo::getOne($params);
|
||
if (empty($user_pharmacist_info)){
|
||
throw new BusinessException("用户数据错误");
|
||
}
|
||
|
||
$user_sign_image_path = $user_pharmacist_info['sign_image'];
|
||
$user_entity_id = $user['user_id'];
|
||
|
||
$hospital_sign_image_path = "basic/file/hospital_signature.png";
|
||
$hospital_entity_id = "5345345461";
|
||
}
|
||
|
||
if (empty($user_sign_image_path) || empty($user_entity_id)){
|
||
throw new BusinessException("无签名图片/用户唯一标识");
|
||
}
|
||
|
||
// 去除第一个/ oss不识别
|
||
$sign_image_path = substr($user_sign_image_path, 1, strlen($user_sign_image_path) - 1);
|
||
|
||
// 获取处方数据
|
||
$params = array();
|
||
$params['order_prescription_id'] = $order_prescription_id;
|
||
$order_prescription = OrderPrescription::getOne($params);
|
||
if (empty($order_prescription)){
|
||
throw new BusinessException("处方数据错误");
|
||
}
|
||
|
||
if (empty($order_prescription['doctor_created_time'])){
|
||
throw new BusinessException("医生开方日期错误");
|
||
}
|
||
|
||
// 检测处方图片
|
||
if ($user['user_type'] == 3 && empty($order_prescription['prescription_img'])){
|
||
throw new BusinessException("处方图片错误");
|
||
}
|
||
|
||
// 获取处方关联疾病数据
|
||
$params = array();
|
||
$params['order_prescription_id'] = $order_prescription_id;
|
||
$order_prescription_icd = OrderPrescriptionIcd::getList($params);
|
||
if (empty($order_prescription_icd)){
|
||
throw new BusinessException("处方疾病数据错误");
|
||
}
|
||
|
||
// 获取处方关联商品数据
|
||
$params = array();
|
||
$params['order_prescription_id'] = $order_prescription_id;
|
||
$order_prescription_product = OrderPrescriptionProduct::getList($params);
|
||
if (empty($order_prescription_product)){
|
||
throw new BusinessException("处方药品数据错误");
|
||
}
|
||
|
||
// 获取医生数据
|
||
$params = array();
|
||
$params['doctor_id'] = $order_prescription['doctor_id'];
|
||
$user_doctor = UserDoctor::getOne($params);
|
||
if (empty($user_doctor)){
|
||
throw new BusinessException("医生数据错误");
|
||
}
|
||
|
||
// 获取医生自定义科室数据
|
||
$params = array();
|
||
$params['department_custom_id'] = $user_doctor['department_custom_id'];
|
||
$hospital_department_custom = HospitalDepartmentCustom::getOne($params);
|
||
if (empty($hospital_department_custom)){
|
||
throw new BusinessException("医生自定义数据错误");
|
||
}
|
||
|
||
// 获取处方关联病例数据
|
||
$params = array();
|
||
$params['order_inquiry_id'] = $order_prescription['order_inquiry_id'];
|
||
$order_inquiry_case = OrderInquiryCase::getOne($params);
|
||
if (empty($order_inquiry_case)){
|
||
throw new BusinessException("处方病例数据错误");
|
||
}
|
||
|
||
// 处理疾病数据
|
||
$icd_name = array_column($order_prescription_icd->toArray(),'icd_name');
|
||
if (!empty($icd_name)){
|
||
$icd_name = implode(';',$icd_name);
|
||
}else{
|
||
$icd_name = "";
|
||
}
|
||
|
||
$ca = new Ca();
|
||
|
||
// 获取云证书签名
|
||
$data = array();
|
||
$data['created_at'] = $order_prescription['doctor_created_time'];
|
||
$data['department_custom_name'] = $hospital_department_custom['department_name'] ?: "";
|
||
$data['user_name'] = $order_prescription['patient_name'];
|
||
$data['sex'] = sexToStringSex($order_prescription['patient_sex']);
|
||
$data['age'] = $order_prescription['patient_age'];
|
||
$data['allergy_history'] = $order_inquiry_case['allergy_history'] ?: "无";
|
||
$data['icd_name'] = $icd_name;
|
||
$data['doctor_advice'] = $order_prescription['doctor_advice'] ?: "无";
|
||
|
||
// 商品数据
|
||
$data['product'] = array();
|
||
foreach ($order_prescription_product as $item){
|
||
$product = array();
|
||
$product['product_name'] = $item['product_name'] . "(" . $item['product_spec'] . ")"; // 商品名称+商品规格
|
||
$product['single_unit'] = $item['single_unit'] ?: ""; // 单次剂量(例:1次1包)
|
||
$product['frequency_use'] = $item['frequency_use'] ?: ""; // 使用频率(例:1天3次)
|
||
$product['single_use'] = $item['single_use'] ?: ""; // 单次用法(例:口服)
|
||
$product['prescription_product_num'] = $item['prescription_product_num'] . $item['packaging_unit']; // 商品数量 + 基本包装单位(例:盒/瓶)
|
||
$data['product'][] = $product;
|
||
}
|
||
|
||
$cert_sign_result = $ca->getCertSign($user_entity_id, $user_entity_id, $data);
|
||
|
||
// 验证云证书签名-验证无需处理,只要不返回错误即可
|
||
$ca->verifyPkcs7($cert_sign_result['signP7'],$data);
|
||
|
||
dump("云证书签名验证成功");
|
||
|
||
if ($user['user_type'] == 3){
|
||
$cert_sign_result = $ca->getCertSign($hospital_entity_id, $hospital_entity_id, $data);
|
||
|
||
// 验证云证书签名-验证无需处理,只要不返回错误即可
|
||
$ca->verifyPkcs7($cert_sign_result['signP7'],$data);
|
||
dump("医院云证书签名获取成功");
|
||
}
|
||
|
||
$oss = new Oss();
|
||
// 医生
|
||
if ($user['user_type'] == 2){
|
||
// 下载基础处方图片
|
||
$prescription_basic_filename = "basic/file/prescription.jpg";
|
||
$prescription_image = $oss->getObjectToRAM($prescription_basic_filename);
|
||
|
||
dump("下载基础处方图片成功");
|
||
|
||
$manager = new ImageManager();
|
||
|
||
$image = $manager->make($prescription_image);
|
||
|
||
$fontPath = './extend/Ca/msyh.ttf';
|
||
|
||
// 处方号
|
||
$image->text($order_prescription['prescription_code'], 1480, 540, function ($font) use($fontPath) {
|
||
$font->file($fontPath);
|
||
$font->size(60);
|
||
$font->align('left');
|
||
});
|
||
|
||
// 日期
|
||
$image->text(date('Y-m-d',strtotime($order_prescription['doctor_created_time'])), 354, 675, function ($font) use($fontPath) {
|
||
$font->file($fontPath);
|
||
$font->size(60);
|
||
$font->align('left');
|
||
});
|
||
|
||
// 科室
|
||
$image->text($hospital_department_custom['department_name'] ?: "", 1385, 675, function ($font) use($fontPath) {
|
||
$font->file($fontPath);
|
||
$font->size(60);
|
||
$font->align('left');
|
||
});
|
||
|
||
// 姓名
|
||
$image->text($order_prescription['patient_name'], 354, 795, function ($font) use($fontPath) {
|
||
$font->file($fontPath);
|
||
$font->size(60);
|
||
$font->align('left');
|
||
});
|
||
|
||
// 性别
|
||
$image->text(sexToStringSex($order_prescription['patient_sex']), 1385, 790, function ($font) use($fontPath) {
|
||
$font->file($fontPath);
|
||
$font->size(60);
|
||
$font->align('left');
|
||
});
|
||
|
||
// 年龄
|
||
$image->text($order_prescription['patient_age'], 354, 900, function ($font) use($fontPath) {
|
||
$font->file($fontPath);
|
||
$font->size(60);
|
||
$font->align('left');
|
||
});
|
||
|
||
// 过敏史
|
||
$image->text($order_inquiry_case['allergy_history'] ?: "无", 405, 1030, function ($font) use($fontPath) {
|
||
$font->file($fontPath);
|
||
$font->size(60);
|
||
$font->align('left');
|
||
});
|
||
|
||
// 初步诊断
|
||
$image->text($icd_name, 445, 1145, function ($font) use($fontPath) {
|
||
$font->file($fontPath);
|
||
$font->size(60);
|
||
$font->align('left');
|
||
});
|
||
|
||
// 医生建议
|
||
$image->text($order_prescription['doctor_advice'] ?: "无", 445, 1252, function ($font) use($fontPath) {
|
||
$font->file($fontPath);
|
||
$font->size(60);
|
||
$font->align('left');
|
||
});
|
||
|
||
// 商品数据
|
||
foreach ($order_prescription_product as $key => $item){
|
||
$x_axis = 229;
|
||
$y_axis = 1600 + $key * 250;
|
||
$x_axis_num = 1900;// 数量使用
|
||
|
||
// 商品名称
|
||
$image->text($item['product_name'], $x_axis, $y_axis, function ($font) use($fontPath) {
|
||
$font->file($fontPath);
|
||
$font->size(50);
|
||
$font->align('left');
|
||
});
|
||
|
||
$image->text("X" . $item['prescription_product_num'] . $item['packaging_unit'], $x_axis_num, $y_axis, function ($font) use($fontPath) {
|
||
$font->file($fontPath);
|
||
$font->size(50);
|
||
$font->align('left');
|
||
});
|
||
|
||
// 用量
|
||
$image->text("用量:" . $item['single_unit'] . " " . $item['frequency_use'], $x_axis, $y_axis+70, function ($font) use($fontPath) {
|
||
$font->file($fontPath);
|
||
$font->size(50);
|
||
$font->align('left');
|
||
});
|
||
|
||
// 用法
|
||
$image->text("用法:" . $item['single_use'], $x_axis, $y_axis+140, function ($font) use($fontPath) {
|
||
$font->file($fontPath);
|
||
$font->size(50);
|
||
$font->align('left');
|
||
});
|
||
}
|
||
|
||
// 生成图片
|
||
$img_result = (string) $image->encode('png', 75);
|
||
dump("处方图片生成成功");
|
||
|
||
// 上传处方图片至oss
|
||
$prescription_img_oss_filename = "applet/prescription/" . $order_prescription['order_prescription_id'] . '.' . 'jpg';
|
||
$prescription_img_url = $oss->putObject($prescription_img_oss_filename, $img_result);
|
||
$prescription_img_url = '/' . $prescription_img_url;
|
||
dump("处方图片上传oss成功");
|
||
|
||
// 图片生成pdf
|
||
$pdf = new TCPDF('P', 'mm', 'A4', true, 'UTF-8', false);
|
||
|
||
$pdf->AddPage();
|
||
$pdf->Image('@' . $img_result, 10, 10, 0, 0, '', '', '', false, 300, '', false, false, 0, false, false, false);
|
||
|
||
$prescription_pdf_local_filename = $order_prescription['order_prescription_id'] . '.' . 'pdf';
|
||
|
||
// 图片生成的处方pdf存储为本地文件
|
||
$pdf->Output(dirname(__DIR__, 2) . "/" . $prescription_pdf_local_filename ,"F");
|
||
|
||
dump("处方图片生成pdf成功");
|
||
}
|
||
|
||
// 药师
|
||
if ($user['user_type'] == 3){
|
||
// 下载医生开具的处方pdf
|
||
// 去除第一个/ oss不识别
|
||
$prescription_pdf_path = substr($order_prescription['prescription_pdf'], 1, strlen($order_prescription['prescription_pdf']) - 1);
|
||
|
||
$local = "./runtime/" . $order_prescription['order_prescription_id'] . '.' . 'pdf';
|
||
|
||
$oss->getObjectToLocal($prescription_pdf_path,$local);
|
||
dump("下载医生开具的处方pdf成功");
|
||
}
|
||
|
||
// 下载签名图片
|
||
$style = "image/resize,m_lfit,w_100,h_350";
|
||
$sign_image = $oss->getCusTomObjectToRAM($sign_image_path,$style);
|
||
$sign_image = base64_encode($sign_image);
|
||
if (!$sign_image){
|
||
throw new BusinessException("签名图片下载失败");
|
||
}
|
||
|
||
dump("下载签名图片成功");
|
||
|
||
if ($user['user_type'] == 2){
|
||
$sign_param = [
|
||
[ // 医生端
|
||
"llx"=>"120", // 左边底部X坐标
|
||
"lly"=>"190", // 左边底部Y坐标
|
||
"urx"=>"190", // 右边上部x坐标
|
||
"ury"=>"140", // 右边上部y坐标
|
||
"pageList"=>[1],
|
||
"sealImg"=>$sign_image
|
||
],
|
||
];
|
||
}else{
|
||
$sign_param = [
|
||
[ // 药师端
|
||
"llx"=>"350", // 左边底部X坐标
|
||
"lly"=>"190", // 左边底部Y坐标
|
||
"urx"=>"440", // 右边上部x坐标
|
||
"ury"=>"140", // 右边上部y坐标
|
||
"pageList"=>[1],
|
||
"sealImg"=>$sign_image
|
||
]
|
||
];
|
||
}
|
||
|
||
// 打开处方pdf文件
|
||
$pdf_file = fopen("./runtime/" . $order_prescription['order_prescription_id'] . ".pdf",'r');
|
||
if (!$pdf_file){
|
||
throw new BusinessException("处方图片打开失败");
|
||
}
|
||
|
||
|
||
// 处方pdf进行签章
|
||
$data = array();
|
||
$data['sign_param'] = json_encode($sign_param);
|
||
$data['pdf_file'] = $pdf_file;
|
||
$sign_pdf_result = $ca->addSignPdf($user_entity_id,$data);
|
||
if (empty($sign_pdf_result[0]['fileId'])){
|
||
throw new BusinessException("处方签章失败");
|
||
}
|
||
|
||
dump("处方pdf进行签章成功");
|
||
|
||
// 下载处方签章文件
|
||
$file_id = $sign_pdf_result[0]['fileId'];
|
||
$prescription_pdf_result = $ca->getSignedFile($user_entity_id,$file_id);
|
||
|
||
if (empty($prescription_pdf_result)){
|
||
throw new BusinessException("下载处方签章文件失败");
|
||
}
|
||
|
||
dump("下载处方签章文件成功");
|
||
|
||
// 上传oss
|
||
$filename = "applet/prescription/" . $order_prescription['order_prescription_id'] . '.' . 'pdf';
|
||
$prescription_pdf_url = $oss->putObject($filename, $prescription_pdf_result);
|
||
dump("处方pdf上传pss成功");
|
||
|
||
if ($user['user_type'] == 3){
|
||
// 药师端时,需要进行系统签章
|
||
// 把药师签章的pdf存储至本地文件
|
||
$file = fopen("./runtime/" . $order_prescription['order_prescription_id'] . '.' . 'pdf', "w");
|
||
fwrite($file, $prescription_pdf_result);
|
||
fclose($file);
|
||
|
||
// 下载医院签名图片
|
||
$style = "image/resize,w_300,h_300";
|
||
$sign_image = $oss->getCusTomObjectToRAM($hospital_sign_image_path,$style);
|
||
$sign_image = base64_encode($sign_image);
|
||
if (!$sign_image){
|
||
throw new BusinessException("医院签名图片下载失败");
|
||
}
|
||
|
||
$sign_param = [
|
||
[ // 医院签章
|
||
"llx"=>"370", // 左边底部X坐标 控制左右(越小越左)
|
||
"lly"=>"210", // 左边底部Y坐标 控制上下(越小越下)
|
||
"urx"=>"520", // 右边上部x坐标
|
||
"ury"=>"360", // 右边上部y坐标
|
||
"pageList"=>[1],
|
||
"sealImg"=>$sign_image
|
||
],
|
||
];
|
||
|
||
// 打开处方pdf文件
|
||
unset($pdf_file);
|
||
$pdf_file = fopen("./runtime/" . $order_prescription['order_prescription_id'] . ".pdf",'r');
|
||
|
||
// 处方pdf进行签章
|
||
$data = array();
|
||
$data['sign_param'] = json_encode($sign_param);
|
||
$data['pdf_file'] = $pdf_file;
|
||
$hospital_sign_pdf_result = $ca->addSignPdf($hospital_entity_id,$data);
|
||
if (empty($hospital_sign_pdf_result[0]['fileId'])){
|
||
throw new BusinessException("处方签章失败");
|
||
}
|
||
|
||
dump("处方pdf进行医院签章成功");
|
||
|
||
// 下载处方签章文件
|
||
unset($prescription_pdf_result);
|
||
$file_id = $hospital_sign_pdf_result[0]['fileId'];
|
||
$prescription_pdf_result = $ca->getSignedFile($hospital_entity_id,$file_id);
|
||
|
||
if (empty($prescription_pdf_result)){
|
||
throw new BusinessException("下载处方签章文件失败");
|
||
}
|
||
|
||
dump("下载医院处方签章文件成功");
|
||
|
||
// 上传oss
|
||
$hospital_filename = "applet/prescription/" . $order_prescription['order_prescription_id'] . '.' . 'pdf';
|
||
$prescription_pdf_url = $oss->putObject($hospital_filename, $prescription_pdf_result);
|
||
dump("上传医院处方签章文件成功");
|
||
}
|
||
|
||
$result = array();
|
||
$result['prescription_img_url'] = $prescription_img_url ?? $order_prescription['prescription_img'];
|
||
$result['prescription_pdf_url'] = '/' . $prescription_pdf_url ?: "";
|
||
return $result;
|
||
|
||
} catch (\Exception $e) {
|
||
throw new BusinessException($e->getMessage());
|
||
}
|
||
}
|
||
} |