351 lines
12 KiB
PHP
351 lines
12 KiB
PHP
<?php
|
||
|
||
namespace App\Services;
|
||
|
||
use App\Amqp\Consumer\PrescriptionExpiredDelayDirectConsumer;
|
||
use App\Amqp\Producer\AutoFinishInquiryDelayDirectProducer;
|
||
use App\Amqp\Producer\PrescriptionExpiredDelayDirectProducer;
|
||
use App\Constants\HttpEnumCode;
|
||
use App\Model\OrderInquiry;
|
||
use App\Model\OrderPrescription;
|
||
use App\Model\OrderPrescriptionFile;
|
||
use App\Model\OrderPrescriptionProduct;
|
||
use App\Model\User;
|
||
use App\Model\UserDoctor;
|
||
use App\Model\UserDoctorInfo;
|
||
use App\Model\UserPharmacist;
|
||
use App\Model\UserPharmacistInfo;
|
||
use Hyperf\Amqp\Producer;
|
||
use Hyperf\DbConnection\Db;
|
||
use Psr\Container\ContainerExceptionInterface;
|
||
use Psr\Container\NotFoundExceptionInterface;
|
||
|
||
/**
|
||
* 药师
|
||
*/
|
||
class UserPharmacistService extends BaseService
|
||
{
|
||
/**
|
||
* 获取药师审核处方列表
|
||
* @return array
|
||
*/
|
||
public function getPrescriptionList(): array
|
||
{
|
||
$user_info = $this->request->getAttribute("userInfo") ?? [];
|
||
|
||
$pharmacist_audit_status = $this->request->input('pharmacist_audit_status');
|
||
$platform_audit_status = $this->request->input('platform_audit_status');
|
||
$page = $this->request->input('page', 1);
|
||
$per_page = $this->request->input('per_page', 10);
|
||
|
||
$OrderPrescriptionService = new OrderPrescriptionService();
|
||
$prescription = $OrderPrescriptionService->getPharmacistWaitAuditPage($user_info['client_user_id'], $pharmacist_audit_status, $platform_audit_status, $page, $per_page);
|
||
if (!empty($prescription['data'])) {
|
||
foreach ($prescription['data'] as &$item) {
|
||
$item['prescription_img'] = addAliyunOssWebsite($item['prescription_img']);
|
||
}
|
||
}
|
||
return success($prescription);
|
||
}
|
||
|
||
/**
|
||
* 设置上下线
|
||
* @return array
|
||
*/
|
||
public function putOnOff(): array
|
||
{
|
||
$user_info = $this->request->getAttribute("userInfo") ?? [];
|
||
|
||
$is_online = $this->request->input('is_online');
|
||
|
||
// 获取药师数据
|
||
$params = array();
|
||
$params['user_id'] = $user_info['user_id'];
|
||
$user_pharmacist = UserPharmacist::getOne($params);
|
||
if (empty($user_pharmacist)) {
|
||
return fail();
|
||
}
|
||
|
||
$data = array();
|
||
$data['is_online'] = $is_online;
|
||
if ($is_online == 1){
|
||
$data['login_at'] = date('Y-m_d,H:i:s',time());
|
||
}
|
||
|
||
$params = array();
|
||
$params['user_id'] = $user_info['user_id'];
|
||
User::editUser($params, $data);
|
||
|
||
return success();
|
||
}
|
||
|
||
/**
|
||
* 获取处方详情
|
||
* @return array
|
||
*/
|
||
public function getPrescriptionInfo(): array
|
||
{
|
||
$user_info = $this->request->getAttribute("userInfo") ?? [];
|
||
|
||
$order_prescription_id = $this->request->route('order_prescription_id');
|
||
|
||
// 获取药师数据
|
||
$params = array();
|
||
$params['user_id'] = $user_info['user_id'];
|
||
$user_pharmacist = UserPharmacist::getOne($params);
|
||
if (empty($user_pharmacist)) {
|
||
return fail();
|
||
}
|
||
|
||
// 获取处方数据
|
||
$fields = [
|
||
"order_prescription_id",
|
||
"order_inquiry_id",
|
||
"prescription_status",
|
||
"pharmacist_audit_status",
|
||
"platform_audit_status",
|
||
"doctor_id",
|
||
];
|
||
|
||
$params = array();
|
||
$params['order_prescription_id'] = $order_prescription_id;
|
||
$params['pharmacist_id'] = $user_info['client_user_id'];
|
||
$order_prescription = OrderPrescription::getOne($params, $fields);
|
||
if (empty($order_prescription)) {
|
||
return fail();
|
||
}
|
||
|
||
// 获取处方文件数据
|
||
$params = array();
|
||
$params['order_prescription_id'] = $order_prescription_id;
|
||
$order_prescription_file = OrderPrescriptionFile::getOne($params);
|
||
if (empty($order_prescription_file)){
|
||
return fail();
|
||
}
|
||
|
||
$order_prescription['prescription_img_oss_path'] = addAliyunOssWebsite($order_prescription_file['prescription_img_oss_path']);
|
||
|
||
$order_prescription['prescription_img'] = addAliyunOssWebsite($order_prescription['prescription_img']);
|
||
|
||
// 获取处方中医生签名
|
||
$params = array();
|
||
$params['doctor_id'] = $order_prescription['doctor_id'];
|
||
$user_doctor_info = UserDoctorInfo::getOne($params);
|
||
if (empty($user_doctor_info)){
|
||
return fail(HttpEnumCode::SERVER_ERROR);
|
||
}
|
||
$order_prescription['doctor_sign_image'] = addAliyunOssWebsite($user_doctor_info['sign_image']);
|
||
|
||
return success($order_prescription->toArray());
|
||
}
|
||
|
||
/**
|
||
* 审核处方
|
||
* @return array
|
||
* @throws ContainerExceptionInterface
|
||
* @throws NotFoundExceptionInterface
|
||
*/
|
||
public function putPrescriptionVerify(): array
|
||
{
|
||
return fail(HttpEnumCode::SERVER_ERROR,"暂时无法人工审核");
|
||
$user_info = $this->request->getAttribute("userInfo") ?? [];
|
||
|
||
$order_prescription_id = $this->request->route('order_prescription_id');
|
||
$pharmacist_audit_status = $this->request->input('pharmacist_audit_status');
|
||
$pharmacist_fail_reason = $this->request->input('pharmacist_fail_reason');
|
||
|
||
// 验证器未验证未0的情况
|
||
if ($pharmacist_audit_status == 0){
|
||
return fail(HttpEnumCode::CLIENT_HTTP_ERROR);
|
||
}
|
||
|
||
// 获取药师数据
|
||
$params = array();
|
||
$params['user_id'] = $user_info['user_id'];
|
||
$user_pharmacist = UserPharmacist::getOne($params);
|
||
if (empty($user_pharmacist)) {
|
||
return fail();
|
||
}
|
||
|
||
// 获取处方数据
|
||
$params = array();
|
||
$params['order_prescription_id'] = $order_prescription_id;
|
||
$params['pharmacist_id'] = $user_info['client_user_id'];
|
||
$order_prescription = OrderPrescription::getOne($params);
|
||
if (empty($order_prescription)) {
|
||
return fail();
|
||
}
|
||
|
||
// 验证处方状态
|
||
if ($order_prescription['prescription_status'] != 1){
|
||
// 处方状态(1:待审核 2:待使用 3:已失效 4:已使用)
|
||
return fail(HttpEnumCode::HTTP_ERROR,"处方审核失败");
|
||
}
|
||
|
||
// 验证处方审核状态
|
||
if ($order_prescription['pharmacist_audit_status'] != 0){
|
||
// 药师审核状态(0:审核中 1:审核成功 2:审核驳回)
|
||
return fail(HttpEnumCode::HTTP_ERROR,"处方已审核,请勿重复审核");
|
||
}
|
||
|
||
// 处方平台审核状态
|
||
if ($order_prescription['platform_audit_status'] != 0){
|
||
// 处方平台审核状态(0:审核中 1:审核成功 2:审核驳回)
|
||
return fail(HttpEnumCode::HTTP_ERROR,"处方已审核,请勿重复审核");
|
||
}
|
||
|
||
// 获取处方订单数据
|
||
$params = array();
|
||
$params['order_inquiry_id'] = $order_prescription['order_inquiry_id'];
|
||
$order_inquiry = OrderInquiry::getOne($params);
|
||
if (empty($order_inquiry)){
|
||
return fail(HttpEnumCode::SERVER_ERROR,"问诊订单数据为空");
|
||
}
|
||
|
||
// 获取处方商品数据
|
||
$product_name = "";
|
||
$params = array();
|
||
$params['order_prescription_id'] = $order_prescription['order_prescription_id'];
|
||
$order_prescription_product = OrderPrescriptionProduct::getList($params);
|
||
if (!empty($order_prescription_product)){
|
||
$product_name = array_column($order_prescription_product->toArray(),'product_name');
|
||
if (!empty($product_name)){
|
||
$product_name = implode('、',$product_name);
|
||
}
|
||
}
|
||
|
||
// 获取医生数据
|
||
$params = array();
|
||
$params['doctor_id'] = $order_inquiry['doctor_id'];
|
||
$user_doctor = UserDoctor::getOne($params);
|
||
if (empty($user_doctor)){
|
||
return fail(HttpEnumCode::SERVER_ERROR,"医生数据错误");
|
||
}
|
||
|
||
Db::beginTransaction();
|
||
|
||
try {
|
||
// 修改处方审核状态
|
||
$data = array();
|
||
if ($pharmacist_audit_status == 1){
|
||
// 审核成功
|
||
$data['prescription_status'] = 2;
|
||
$data['pharmacist_audit_status'] = 1;
|
||
$data['platform_audit_status'] = 1;
|
||
}else{
|
||
$data['pharmacist_audit_status'] = 2;
|
||
$data['pharmacist_fail_reason'] = $pharmacist_fail_reason;
|
||
}
|
||
$data['pharmacist_verify_time'] = date('Y-m-d H:i:s',time());
|
||
|
||
$params = array();
|
||
$params['order_prescription_id'] = $order_prescription_id;
|
||
|
||
OrderPrescription::edit($params,$data);
|
||
|
||
if ($pharmacist_audit_status == 1){
|
||
// 药师ca签章
|
||
$OrderPrescriptionService = new OrderPrescriptionService();
|
||
$prescription_open_result = $OrderPrescriptionService->openPrescription($order_prescription['order_prescription_id'],$user_info['user_id']);
|
||
if (empty($prescription_open_result['prescription_img_url'])){
|
||
Db::rollBack();
|
||
return fail(HttpEnumCode::SERVER_ERROR, "处方审核失败");
|
||
}
|
||
|
||
// 发送IM消息-处方已开具
|
||
$imService = new ImService();
|
||
$imService->prescriptionIssued($order_inquiry,$user_doctor['user_id'],$order_inquiry['user_id'],$product_name,(string)$order_prescription['order_prescription_id'],7);
|
||
|
||
// 发送站内、短信消息-患者的处方被药师审核通过
|
||
$MessagePush = new MessagePush($order_inquiry['user_id'],$order_inquiry['inquiry_no']);
|
||
$MessagePush->patientPrescriptionVerifyPass();
|
||
|
||
// 站内、订阅失败发送短信-医生开具的处方审核通过
|
||
// 发送目标不同,重新实例化
|
||
$MessagePush = new MessagePush($user_doctor['user_id'],$order_inquiry['inquiry_no']);
|
||
$MessagePush->prescriptionVerifySuccess();
|
||
|
||
// 添加处方过期队列
|
||
$data = array();
|
||
$data['order_prescription_id'] = (string)$order_prescription['order_prescription_id'];
|
||
|
||
$message = new PrescriptionExpiredDelayDirectProducer($data);
|
||
$message->setDelayMs(1000 * 60 * 60 * 24 * 3);
|
||
$producer = $this->container->get(Producer::class);
|
||
$res = $producer->produce($message);
|
||
if (!$res) {
|
||
Db::rollBack();
|
||
return fail(HttpEnumCode::SERVER_ERROR, "订单创建失败");
|
||
}
|
||
}else{
|
||
// 站内、订阅失败发送短信-医生开具的处方审核未通过
|
||
$MessagePush = new MessagePush($user_doctor['user_id'],$order_inquiry['inquiry_no']);
|
||
$MessagePush->prescriptionVerifyFail($order_prescription['order_prescription_id']);
|
||
}
|
||
|
||
Db::commit();
|
||
} catch (\Exception $e) {
|
||
Db::rollBack();
|
||
return fail(HttpEnumCode::HTTP_ERROR, $e->getMessage());
|
||
}
|
||
|
||
return success($order_prescription->toArray());
|
||
}
|
||
|
||
/**
|
||
* 药师基本资料
|
||
* @return array
|
||
*/
|
||
public function getPharmacistInfo(): array
|
||
{
|
||
$user_info = $this->request->getAttribute("userInfo") ?? [];
|
||
|
||
// 获取用户数据
|
||
$fields = [
|
||
'age',
|
||
'sex',
|
||
'avatar',
|
||
];
|
||
$params = array();
|
||
$params['user_id'] = $user_info['user_id'];
|
||
$user = User::getOne($params,$fields);
|
||
if (empty($user)){
|
||
return fail();
|
||
}
|
||
|
||
$user['avatar'] = addAliyunOssWebsite($user['avatar']);
|
||
|
||
// 获取药师数据
|
||
$fields = [
|
||
'pharmacist_id',
|
||
'user_name',
|
||
'pharmacist_title',
|
||
'department_custom_name',
|
||
];
|
||
$params = array();
|
||
$params['user_id'] = $user_info['user_id'];
|
||
$user_pharmacist = UserPharmacist::getOne($params,$fields);
|
||
if (empty($user_pharmacist)) {
|
||
return fail();
|
||
}
|
||
|
||
// 获取药师详情数据
|
||
$fields = [
|
||
'card_name',
|
||
'card_num',
|
||
];
|
||
|
||
$params = array();
|
||
$params['pharmacist_id'] = $user_pharmacist['pharmacist_id'];
|
||
$user_pharmacist_info = UserPharmacistInfo::getOne($params,$fields);
|
||
if (empty($user_pharmacist_info)){
|
||
return fail();
|
||
}
|
||
|
||
$result = array_merge($user->toArray(),$user_pharmacist->toArray(),$user_pharmacist_info->toArray());
|
||
|
||
return success($result);
|
||
}
|
||
|
||
|
||
} |