Files
zyt/server/app/adminapi/logic/tcm/PrescriptionLogic.php
T
2026-03-18 14:53:09 +08:00

147 lines
4.3 KiB
PHP

<?php
declare(strict_types=1);
namespace app\adminapi\logic\tcm;
use app\common\model\tcm\Prescription;
use app\common\model\tcm\Diagnosis;
class PrescriptionLogic
{
private static $error = '';
public static function setError(string $msg): void
{
self::$error = $msg;
}
public static function getError(): string
{
return self::$error;
}
private static function generateSn(): string
{
return 'RX' . date('Ymd') . str_pad((string)mt_rand(1, 9999), 4, '0', STR_PAD_LEFT);
}
/**
* 添加处方
*/
public static function add(array $params, int $adminId): ?int
{
$diagnosis = Diagnosis::find($params['diagnosis_id']);
if (!$diagnosis) {
self::setError('诊单不存在');
return null;
}
$herbs = $params['herbs'] ?? [];
if (empty($herbs) || !is_array($herbs)) {
self::setError('请添加中药');
return null;
}
foreach ($herbs as $h) {
if (empty($h['name'])) {
self::setError('中药名称不能为空');
return null;
}
}
$sn = self::generateSn();
while (Prescription::where('sn', $sn)->find()) {
$sn = self::generateSn();
}
$data = [
'sn' => $sn,
'diagnosis_id' => (int)$params['diagnosis_id'],
'appointment_id' => (int)($params['appointment_id'] ?? 0),
'patient_id' => (int)$diagnosis->patient_id,
'patient_name' => $params['patient_name'] ?? $diagnosis->patient_name,
'gender' => (int)($params['gender'] ?? $diagnosis->gender),
'age' => (int)($params['age'] ?? $diagnosis->age ?? 0),
'phone' => $params['phone'] ?? $diagnosis->phone ?? '',
'visit_no' => $params['visit_no'] ?? $sn,
'prescription_date' => $params['prescription_date'] ?? date('Y-m-d'),
'pulse' => $params['pulse'] ?? $diagnosis->pulse ?? '',
'tongue' => $params['tongue'] ?? $diagnosis->tongue_coating ?? '',
'clinical_diagnosis' => $params['clinical_diagnosis'] ?? '',
'herbs' => $herbs,
'dose_count' => (int)($params['dose_count'] ?? 1),
'dose_unit' => $params['dose_unit'] ?? '剂',
'usage_instruction' => $params['usage_instruction'] ?? '水煎服一日二次',
'amount' => (float)($params['amount'] ?? 0),
'doctor_name' => $params['doctor_name'] ?? '',
'doctor_signature' => $params['doctor_signature'] ?? '',
'template_id' => (int)($params['template_id'] ?? 0),
'creator_id' => $adminId,
];
$prescription = new Prescription();
$prescription->save($data);
return (int)$prescription->id;
}
/**
* 处方详情
*/
public static function detail(int $id): ?array
{
$row = Prescription::find($id);
if (!$row) {
return null;
}
$arr = $row->toArray();
$arr['gender_desc'] = $arr['gender'] == 1 ? '男' : '女';
return $arr;
}
/**
* 根据诊单ID获取处方列表
*/
public static function listByDiagnosis(int $diagnosisId): array
{
return Prescription::where('diagnosis_id', $diagnosisId)
->whereNull('delete_time')
->order('id', 'desc')
->select()
->toArray();
}
/**
* 根据预约ID获取处方
*/
public static function getByAppointment(int $appointmentId): ?array
{
$row = Prescription::where('appointment_id', $appointmentId)
->whereNull('delete_time')
->order('id', 'desc')
->find();
return $row ? $row->toArray() : null;
}
/**
* 作废处方
*/
public static function void(int $id, int $adminId, string $adminName): bool
{
$row = Prescription::find($id);
if (!$row) {
self::setError('处方不存在');
return false;
}
if ((int)($row->void_status ?? 0) === 1) {
self::setError('该处方已作废');
return false;
}
$row->void_status = 1;
$row->void_time = time();
$row->void_by = $adminId;
$row->void_by_name = $adminName;
return $row->save();
}
}