157 lines
6.0 KiB
PHP
157 lines
6.0 KiB
PHP
<?php
|
|
/*
|
|
* @author: liudiao1992
|
|
* @Date: 2023-09-14 22:42:57
|
|
* @Description:
|
|
*/
|
|
namespace common\services;
|
|
|
|
use common\models\oldDb\PharmacistIdentity;
|
|
use common\models\oldDb\Prescription;
|
|
use common\models\oldDb\ProductOrder;
|
|
use common\models\oldDb\Store;
|
|
use common\modelsgii\oldDb\DoctorIdentity;
|
|
use Exception;
|
|
use Yii;
|
|
use yii\helpers\Json;
|
|
|
|
class PrescriptionService{
|
|
|
|
/**
|
|
* 处方详情
|
|
*/
|
|
public function detail($prescriptionNo = '', $presciptionId = 0){
|
|
try {
|
|
if(!$presciptionId&&!$prescriptionNo){
|
|
throw new Exception("Error Processing Request");
|
|
}
|
|
|
|
if($prescriptionNo){
|
|
$prescription = Prescription::find()->select('id,store_id,prescription_no,online_prescription_no,is_online,prescription_type,type,is_dispense,valid_hours,content,doctor_order,status,pharmacist_id,su_id,process_rule_id,process_rule,process_rule_note,doctor_second_sign,created_at')->where([
|
|
'prescription_no' => $prescriptionNo
|
|
])->with('pharmacistInfo')->asArray()->one();
|
|
} else {
|
|
$prescription = Prescription::find()->select('id,store_id,prescription_no,online_prescription_no,is_online,prescription_type,type,is_dispense,valid_hours,content,doctor_order,status,pharmacist_id,su_id,process_rule_id,process_rule,process_rule_note,doctor_second_sign,created_at')->where([
|
|
'id' => $presciptionId
|
|
])->with('pharmacistInfo')->asArray()->one();
|
|
}
|
|
if(!$prescription){
|
|
throw new Exception('处方不存在');
|
|
}
|
|
$store = Store::find()->select('name, offical_seal')->where(['id' => $prescription['store_id']])->one();
|
|
if(!$store) throw new Exception('门店不存在');
|
|
$productOrder = ProductOrder::find()->where(['p_id' => $prescription['id']])->one();
|
|
$content = Json::decode($prescription['content']);
|
|
foreach($content['repice'] as $k=>$v){
|
|
$content['repice'][$k]['content'] = Json::decode($v['content']);
|
|
}
|
|
$prescription['content'] = $content;
|
|
$prescription['doctor_order'] = explode('|',$prescription['doctor_order']);
|
|
$prescription['is_pay'] = $productOrder->is_pay;
|
|
$prescription['cancel_status'] = $productOrder->cancel_status;
|
|
$prescription['refund_status'] = $productOrder->refund_status;
|
|
$prescription['store'] = $store;
|
|
|
|
$DoctorIdentity= DoctorIdentity::find()->where(['su_id'=>$prescription['su_id']])->asArray()->one();
|
|
$PharmacistIdentity= PharmacistIdentity::find()->where(['su_id'=>$prescription['pharmacist_id']])->asArray()->one();
|
|
$prescription['doctor_sign_image']=$DoctorIdentity['sign_image'];
|
|
$prescription['doctor_sign_type']=$DoctorIdentity['sign_type'];
|
|
// TODO 有些错误
|
|
$prescription['Pharmacist_sign_image']=$PharmacistIdentity['sign_image']?? '';
|
|
$prescription['Pharmacist_sign_type']=$PharmacistIdentity['sign_type']?? '';
|
|
|
|
$prescription['online_tcm_print'] = $this->buildOnlineTcmPrint($prescription);
|
|
|
|
return $prescription;
|
|
} catch (Exception $e) {
|
|
throw new Exception($e->getMessage(). json_encode($e->getLine()));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 在线中药处方笺:监管中医术语展示(与 xk-api online_tcm_print 一致)
|
|
*
|
|
* @param array<string, mixed> $prescription
|
|
* @return array<string, mixed>
|
|
*/
|
|
private function buildOnlineTcmPrint(array $prescription): array
|
|
{
|
|
$ptype = (int) ($prescription['prescription_type'] ?? 0);
|
|
$isOnline = (int) ($prescription['is_online'] ?? 0);
|
|
if ($ptype !== 1 || !in_array($isOnline, [1, 2, 3], true)) {
|
|
return ['show' => false];
|
|
}
|
|
|
|
$prescriptionId = (int) ($prescription['id'] ?? 0);
|
|
if ($prescriptionId <= 0) {
|
|
return ['show' => false];
|
|
}
|
|
|
|
try {
|
|
$syndrome = $this->fetchTcmTermNames(
|
|
'prescription_disease',
|
|
'disease_id',
|
|
'traditional_chinese_medicine_diseases',
|
|
$prescriptionId
|
|
);
|
|
$method = $this->fetchTcmTermNames(
|
|
'prescription_method',
|
|
'method_id',
|
|
'traditional_chinese_medicine_method',
|
|
$prescriptionId
|
|
);
|
|
$disease = $this->fetchTcmTermNames(
|
|
'prescription_syndrome',
|
|
'syndrome_id',
|
|
'traditional_chinese_medicine_syndrome',
|
|
$prescriptionId
|
|
);
|
|
|
|
if ($syndrome === '' && $method === '' && $disease === '') {
|
|
return ['show' => false, 'a' => 1];
|
|
}
|
|
|
|
$result = ['show' => true];
|
|
if ($syndrome !== '') {
|
|
$result['tcm_syndrome'] = $syndrome;
|
|
}
|
|
if ($method !== '') {
|
|
$result['tcm_method'] = $method;
|
|
}
|
|
if ($disease !== '') {
|
|
$result['tcm_disease'] = $disease;
|
|
}
|
|
|
|
return $result;
|
|
} catch (\Throwable $e) {
|
|
Yii::warning('online_tcm_print query failed: '.$e->getMessage(), __METHOD__);
|
|
|
|
return ['show' => false, 'a' => $e->getMessage()];
|
|
}
|
|
}
|
|
|
|
private function fetchTcmTermNames(
|
|
string $linkTable,
|
|
string $linkColumn,
|
|
string $termTable,
|
|
int $prescriptionId
|
|
): string {
|
|
if (! Yii::$app->has('new_db')) {
|
|
return '';
|
|
}
|
|
|
|
$db = Yii::$app->new_db;
|
|
$rows = $db->createCommand(
|
|
"SELECT t.name FROM {{%{$linkTable}}} l
|
|
INNER JOIN {{%{$termTable}}} t ON t.id = l.{$linkColumn}
|
|
WHERE l.prescription_id = :pid",
|
|
[':pid' => $prescriptionId]
|
|
)->queryColumn();
|
|
|
|
$names = array_filter(array_map('trim', $rows));
|
|
|
|
return implode('、', $names);
|
|
}
|
|
|
|
}
|