251 lines
11 KiB
PHP
251 lines
11 KiB
PHP
<?php
|
||
|
||
namespace service\modules\v1\controllers;
|
||
|
||
use common\core\BasePharmacistController;
|
||
use common\core\sms\PrescriptionPassMessage;
|
||
use common\events\ProductOrderEvent;
|
||
use common\forms\ProductCancelForm;
|
||
use common\forms\ProductRefundForm;
|
||
use common\jobs\Sms\PrescriptionPassMessageJob;
|
||
use common\jobs\Sms\PrescriptionRefuseMessageJob;
|
||
use common\models\Department;
|
||
use common\models\ExamineLog;
|
||
use common\models\Store;
|
||
use common\models\DoctorInfo;
|
||
use common\models\DrugUseWay;
|
||
use common\models\PharmacistrInfo;
|
||
use common\models\Prescription;
|
||
use common\models\ProductOrder;
|
||
use common\models\SystemNotice;
|
||
use common\services\PrescriptionService;
|
||
use yii\base\Exception;
|
||
|
||
/**
|
||
* @doc-module 药师审核处方
|
||
*/
|
||
class ExamineController extends BasePharmacistController
|
||
{
|
||
/**
|
||
* @doc-name 中药的使用煎熬方式
|
||
* @doc-return mixed @drug-use-way{*}
|
||
*/
|
||
public function actionUseWay()
|
||
{
|
||
return DrugUseWay::find()->all();
|
||
}
|
||
|
||
/**
|
||
* @doc-name 查看处方
|
||
* @doc-param int prescription_id 处方id
|
||
* @doc-return string issue_date 开方时间
|
||
* @doc-return mixed @PrescriptionWest{*,@UserPatient{*},@DoctorInfo{*,@Department{*}},@PharmacistInfo{name-string-药师}} 西药处方
|
||
* @doc-return mixed @PrescriptionChinese{*,@UserPatient{*},@DoctorInfo{*,@Department{*}},@PharmacistInfo{name-string-药师}} 中药处方
|
||
* @doc-return mixed @WestRepice{*,@DrugUseTime{*},@DrugUseType{*},@DrugUseFrequency{*},@WestUnit{*}} 西药方
|
||
* @doc-return mixed @ChineseRepice{*,@DrugUseTime{*},@DrugUseNum{*}} 中药方
|
||
* @doc-return float total_price 金额
|
||
*/
|
||
public function actionDetail()
|
||
{
|
||
$pharmacistrInfo = PharmacistrInfo::find()->where(['su_id' => \Yii::$app->user->identity->getId()])->one();
|
||
if (!$pharmacistrInfo) {
|
||
throw new Exception('药师数据错误');
|
||
}
|
||
|
||
$prescription_no = \Yii::$app->request->get('prescription_no');
|
||
if (!$prescription_no) {
|
||
throw new Exception('处方no不能为空');
|
||
}
|
||
|
||
return (new PrescriptionService())->detail(\Yii::$app->request->get('prescription_no'));
|
||
}
|
||
|
||
/**
|
||
* @doc-name 处方列表
|
||
* @doc-param int status 状态0待审核 1已审核通过 2未通过
|
||
* @doc-return mixed @PrescriptionChinese{*,@UserPatient{*}} 中药处方
|
||
* @doc-return mixed @PrescriptionWest{*,@UserPatient{*}} 中药处方
|
||
*/
|
||
public function actionList()
|
||
{
|
||
$pharmacistrInfo = PharmacistrInfo::find()->where(['su_id' => \Yii::$app->user->identity->getId()])->one();
|
||
if (!$pharmacistrInfo) {
|
||
throw new Exception('药师数据错误');
|
||
}
|
||
$status = \Yii::$app->request->get('status');
|
||
if($status > 0){ //已审核或未通过审核
|
||
$prescription = Prescription::find()->where(['status' => $status, 'pharmacist_id' => \Yii::$app->user->identity->id])->with('patients')->orderBy('created_at DESC')->asArray()->all();
|
||
} else {
|
||
$prescription = Prescription::find()->where(['status' => $status])->with('patients')->orderBy('created_at DESC')->asArray()->all();
|
||
}
|
||
if (!$prescription) {
|
||
throw new Exception('暂无数据');
|
||
}
|
||
|
||
return [
|
||
'prescription' => $prescription
|
||
];
|
||
}
|
||
|
||
/**
|
||
* @doc-name 处方列表
|
||
* @doc-param int status 状态0待审核 1已审核通过 2未通过
|
||
* @doc-return mixed @PrescriptionChinese{*,@UserPatient{*}} 中药处方
|
||
* @doc-return mixed @PrescriptionWest{*,@UserPatient{*}} 中药处方
|
||
*/
|
||
public function actionListByDoctor()
|
||
{
|
||
// 查询处方信息,包括患者详情,并按创建时间倒序排列
|
||
$prescription = Prescription::find()->where(['up_id' => \Yii::$app->request->get('up_id'), 'store_id' => \Yii::$app->request->get('store_id')])->with('patients')->orderBy('created_at DESC')->asArray()->all();
|
||
// 若未找到任何处方信息,则抛出异常
|
||
if (!$prescription) {
|
||
throw new Exception('暂无数据');
|
||
}
|
||
|
||
// 遍历处方列表,处理每个处方的内容
|
||
foreach ($prescription as &$v) {
|
||
// 解析处方内容中的JSON数据
|
||
$tmp = json_decode($v['content'], true);
|
||
// 提取并赋值处方类别
|
||
$v['p_category'] = $tmp['category'];
|
||
// 初始化处方药品列表
|
||
$v['p_drugs'] = [];
|
||
// 遍历处方明细中的每种药品信息
|
||
foreach ($tmp['repice'] as $repiceInfo) {
|
||
// 解析药品内容中的JSON数据
|
||
$drugJson = json_decode($repiceInfo['content'], true);
|
||
// 若药品信息中包含药品名称键,则直接添加药品名称到列表
|
||
if (array_key_exists('drug_name', $drugJson)){
|
||
$v['p_drugs'][] = $drugJson['drug_name'];
|
||
} else {
|
||
// 若药品信息中包含名称,则添加到处方药品列表中
|
||
$v['p_drugs'] = array_column($drugJson, 'name');
|
||
}
|
||
}
|
||
// 移除重复的药品名称,并计算唯一药品的数量
|
||
$v['p_drugs'] = array_unique($v['p_drugs']);
|
||
$v['drugs_count'] = count($v['p_drugs']);
|
||
// 格式化创建时间,若无创建时间则设为空字符串
|
||
$v['created_at'] = !empty($v['created_at']) ? date('Y-m-d H:i:s', $v['created_at']) : '';
|
||
}
|
||
|
||
// 返回处理后的处方列表
|
||
return $prescription;
|
||
}
|
||
|
||
/**
|
||
* @doc-name 处方审核
|
||
* @doc-param int prescription_id 处方id
|
||
* @doc-param int status 状态 1已通过 2拒绝
|
||
*/
|
||
public function actionVerify()
|
||
{
|
||
$pharmacistrInfo = PharmacistrInfo::find()->where(['su_id' => \Yii::$app->user->identity->getId()])->one();
|
||
if (!$pharmacistrInfo) {
|
||
throw new Exception('药师数据错误');
|
||
}
|
||
//处方号
|
||
$prescription_no = \Yii::$app->request->post('prescription_no');
|
||
if (!$prescription_no) {
|
||
throw new Exception('处方号不能为空');
|
||
}
|
||
//处方状态
|
||
$status = \Yii::$app->request->post('status');
|
||
if (!in_array($status, [1, 2])) {
|
||
throw new Exception('审核状态错误');
|
||
}
|
||
|
||
$prescription = Prescription::find()->where(['prescription_no' => $prescription_no])->one();
|
||
if(!$prescription) throw new Exception('处方不存在');
|
||
$t = \Yii::$app->db->beginTransaction();
|
||
try {
|
||
$prescription->pharmacist_id = \Yii::$app->user->identity->id;
|
||
$prescription->status = $status;
|
||
$prescription->reject_view = $status == 2 ? \Yii::$app->user->identity->id:0;
|
||
$prescription->reject_reason = $status == 2 ? \Yii::$app->request->post('reject_reason'):'';
|
||
$prescription->reject_time = $status == 2 ? time():0;
|
||
$prescription->pharmacist_view_time = time();
|
||
$prescription->saveOrFail();
|
||
|
||
if($status == 2){ //审批不通过,已支付订单需要退款
|
||
$productOrder = ProductOrder::find()->where(['p_id' => $prescription->id])->one();
|
||
if($productOrder->status == 0){//待支付订单需要取消
|
||
$form = new ProductCancelForm();
|
||
$form->cancel(['user_id' =>$productOrder->user_id, 'order_id' => $productOrder->id]);
|
||
} elseif ($productOrder->status == 1){//已支付订单需要退款
|
||
$form = new ProductRefundForm();
|
||
$form->refund(['user_id' =>$productOrder->user_id, 'order_id' => $productOrder->id], 'auto');
|
||
}
|
||
}else{
|
||
//中药处方订单同步江奥川erp
|
||
if($prescription->prescription_type == 1){
|
||
$productOrder = ProductOrder::find()->where(['p_id' => $prescription->id, 'status' => 1])->one();
|
||
if($productOrder){
|
||
//触发订单支付事件,同步江奥川ERP
|
||
$event = new ProductOrderEvent();
|
||
$event->order = $productOrder;
|
||
$event->sender = $this;
|
||
\Yii::$app->trigger(ProductOrder::EVENT_PAYED, $event);
|
||
}
|
||
}
|
||
}
|
||
|
||
$systemNotice = new SystemNotice();
|
||
$systemNotice->store_id = $prescription->store_id;
|
||
$systemNotice->user_id = $prescription->su_id;
|
||
$systemNotice->data = $prescription->id;
|
||
$systemNotice->content = $status==1?'您开具的处方已通过审核!':'您开具的处方未通过审核,原因是:'.\Yii::$app->request->post('reject_reason');
|
||
$systemNotice->base_type = 5;
|
||
$systemNotice->scene_type = 2;// 医生
|
||
$systemNotice->notice_at = date('Y-m-d H:i:s');
|
||
$systemNotice->saveOrFail();
|
||
|
||
$t->commit();
|
||
|
||
//处方生成短信通知药师审方
|
||
if($status == 2){
|
||
\Yii::$app->queue->push(new PrescriptionRefuseMessageJob([
|
||
'orderId' => $prescription->id,
|
||
]));
|
||
} else {
|
||
\Yii::$app->queue->push(new PrescriptionPassMessageJob([
|
||
'orderId' => $prescription->id,
|
||
]));
|
||
}
|
||
|
||
} catch (\Exception $e) {
|
||
$t->rollBack();
|
||
throw new Exception($e->getMessage());
|
||
}
|
||
return ['审核成功'];
|
||
}
|
||
|
||
/**
|
||
* @doc-name 处方溯源
|
||
* @doc-param int prescription_id 处方id
|
||
* @doc-param string prescription_no 处方编号
|
||
*/
|
||
public function actionSource()
|
||
{
|
||
$prescription_no = \Yii::$app->request->get('prescription_no');
|
||
if (!$prescription_no) throw new Exception('处方号不能为空');
|
||
$store_id = \Yii::$app->request->get('store_id');
|
||
// 先查询是否西药处方
|
||
$prescription = Prescription::find()->where(['prescription_no' => $prescription_no])->with(['pharmacistInfo', 'patients'])->asArray()->one();
|
||
|
||
$doctor = [];
|
||
$doctorInfo = DoctorInfo::find()->where(['su_id' => $prescription['su_id']])->one();
|
||
$depart = Department::findOne(['id' => $doctorInfo->depart_id]);
|
||
$store = Store::findOne(['id' => $store_id]);
|
||
$doctor['store'] = $store->name;
|
||
$doctor['depart'] = $depart->name;
|
||
$doctor['name'] = $doctorInfo->name;
|
||
|
||
return [
|
||
'type' => $prescription->prescription_type == 1 ? '中药处方' : ($prescription->prescription_type == 2 ? '西药处方' : '颗粒药处方'),
|
||
'prescription' => $prescription,
|
||
'doctor' => $doctor
|
||
];
|
||
|
||
}
|
||
} |