This commit is contained in:
Your Name
2026-04-21 18:02:27 +08:00
parent a1d2ab4649
commit d899e0fe0a
301 changed files with 1937 additions and 344 deletions
@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace app\adminapi\logic\tcm;
use app\common\model\auth\Admin;
use app\common\model\doctor\Appointment;
use app\common\model\tcm\Prescription;
use app\common\model\tcm\PrescriptionOrder;
use app\common\model\tcm\Diagnosis;
@@ -463,6 +464,13 @@ class PrescriptionLogic
$arr['business_prescription_audit_rejected'] = $bizPo ? 1 : 0;
$arr['business_prescription_audit_remark'] = $bizPo ? (string) ($bizPo->prescription_audit_remark ?? '') : '';
// 与 PrescriptionLists「业务订单」角标一致:未删除且非已取消(4) 即视为存在有效业务订单
$hasBizOrder = PrescriptionOrder::where('prescription_id', $id)
->whereNull('delete_time')
->where('fulfillment_status', '<>', 4)
->count() > 0;
$arr['has_prescription_order'] = $hasBizOrder ? 1 : 0;
return $arr;
}
@@ -701,6 +709,133 @@ class PrescriptionLogic
return $row->toArray();
}
/**
* 挂号标记「完成」后自动生成处方:无药材配方,is_system_auto=1,其余字段与常规开方一致(患者/诊单来自预约关联诊单)。
* 若该预约已有未作废处方、或同日同诊单同医师已存在未作废处方,则跳过(不抛错)。
*
* @return int|null 新建处方 id;跳过返回 null
*/
public static function createSystemAutoFromCompletedAppointment(int $appointmentId): ?int
{
if ($appointmentId <= 0) {
return null;
}
$appointment = Appointment::find($appointmentId);
if (!$appointment) {
return null;
}
$diagnosisId = (int) ($appointment->patient_id ?? 0);
if ($diagnosisId <= 0) {
Log::info('createSystemAutoFromCompletedAppointment: no diagnosis (patient_id), skip appt=' . $appointmentId);
return null;
}
$doctorId = (int) ($appointment->doctor_id ?? 0);
if ($doctorId <= 0) {
return null;
}
$dupAppt = Prescription::where('appointment_id', $appointmentId)
->where('void_status', 0)
->whereNull('delete_time')
->find();
if ($dupAppt) {
return null;
}
$diagnosis = Diagnosis::find($diagnosisId);
if (!$diagnosis) {
self::setError('诊单不存在');
return null;
}
$apptDateRaw = $appointment->appointment_date ?? '';
$dateYmd = is_string($apptDateRaw) && preg_match('/^\d{4}-\d{2}-\d{2}/', trim($apptDateRaw))
? substr(trim($apptDateRaw), 0, 10)
: self::normalizePrescriptionDate(date('Y-m-d'));
if (!self::assertUniquePrescriptionPerDiagnosisDay($diagnosisId, $doctorId, $dateYmd, null)) {
Log::info("createSystemAutoFromCompletedAppointment: unique rule skip diagnosis={$diagnosisId} doctor={$doctorId} date={$dateYmd}");
return null;
}
$sn = self::generateSn();
while (Prescription::where('sn', $sn)->find()) {
$sn = self::generateSn();
}
$clinical = trim((string) ($diagnosis->symptoms ?? ''));
if ($clinical === '') {
$clinical = '就诊完成系统生成(暂无药材配方,请医师补充诊断与药材)';
}
$doctorName = (string) (Admin::where('id', $doctorId)->value('name') ?? '');
$assistantIdForRx = (int) ($diagnosis->assistant_id ?? 0);
$visitNo = '1K' . str_pad((string) $diagnosisId, 8, '0', STR_PAD_LEFT);
$herbs = [];
$data = [
'sn' => $sn,
'prescription_name' => '系统代开',
'prescription_type' => '浓缩水丸',
'dosage_amount' => 1.0,
'dosage_unit' => 'g',
'need_decoction' => 0,
'bags_per_dose' => 1,
'diagnosis_id' => $diagnosisId,
'appointment_id' => $appointmentId,
'patient_id' => 0,
'patient_name' => (string) ($diagnosis->patient_name ?? ''),
'gender' => (int) ($diagnosis->gender ?? 0),
'age' => (int) ($diagnosis->age ?? 0),
'phone' => (string) ($diagnosis->phone ?? ''),
'visit_no' => $visitNo,
'prescription_date' => $dateYmd,
'pulse' => (string) ($diagnosis->pulse ?? ''),
'pulse_condition' => '',
'tongue' => (string) ($diagnosis->tongue ?? ''),
'tongue_image' => '',
'clinical_diagnosis' => $clinical,
'case_record' => [],
'herbs' => $herbs,
'dose_count' => 1,
'dose_unit' => '剂',
'usage_days' => 7,
'times_per_day' => 2,
'usage_instruction' => '水煎服,一日二次',
'usage_time' => '饭前',
'usage_way' => '温水送服',
'dietary_taboo' => '',
'usage_notes' => '',
'amount' => 0.0,
'doctor_name' => $doctorName,
'doctor_signature' => '',
'template_id' => 0,
'is_shared' => 0,
'is_system_auto' => 1,
'visible_role_ids' => self::normalizeVisibleRoleIds([]),
'audit_status' => 0,
'audit_time' => null,
'audit_by' => null,
'audit_by_name' => '',
'audit_remark' => '',
'creator_id' => $doctorId,
'assistant_id' => $assistantIdForRx,
];
$prescription = new Prescription();
$prescription->save($data);
return (int) $prescription->id;
}
/**
* 作废处方
*/
@@ -715,6 +850,16 @@ class PrescriptionLogic
self::setError('该处方已作废');
return false;
}
$rxId = (int) ($row->id ?? 0);
$bizCount = (int) PrescriptionOrder::where('prescription_id', $rxId)
->whereNull('delete_time')
->where('fulfillment_status', '<>', 4)
->count();
if ($bizCount > 0) {
self::setError('该处方已存在业务订单,无法作废');
return false;
}
$row->void_status = 1;
$row->void_time = time();
$row->void_by = $adminId;