228 lines
9.3 KiB
PHP
228 lines
9.3 KiB
PHP
<?php
|
||
|
||
declare(strict_types=1);
|
||
|
||
namespace app\adminapi\lists\tcm;
|
||
|
||
use app\adminapi\lists\BaseAdminDataLists;
|
||
use app\adminapi\logic\tcm\PrescriptionOrderLogic;
|
||
use app\common\lists\ListsExtendInterface;
|
||
use app\common\lists\ListsSearchInterface;
|
||
use app\common\model\Order;
|
||
use app\common\model\tcm\Diagnosis;
|
||
use app\common\model\tcm\Prescription;
|
||
use app\common\model\tcm\PrescriptionOrder;
|
||
use app\common\model\tcm\PrescriptionOrderPayOrder;
|
||
use app\common\service\gancao\GancaoScmRecipelService;
|
||
|
||
class PrescriptionOrderLists extends BaseAdminDataLists implements ListsSearchInterface, ListsExtendInterface
|
||
{
|
||
public function setSearch(): array
|
||
{
|
||
return [
|
||
'=' => ['prescription_id', 'fulfillment_status', 'prescription_audit_status', 'payment_slip_audit_status'],
|
||
'%like%' => ['order_no'],
|
||
];
|
||
}
|
||
|
||
public function lists(): array
|
||
{
|
||
$query = PrescriptionOrder::where($this->searchWhere)->whereNull('delete_time');
|
||
$this->applyDoctorAssistantFilters($query);
|
||
$this->applyPatientKeywordFilter($query);
|
||
if (!PrescriptionOrderLogic::canSeeAllPrescriptionOrders($this->adminInfo)) {
|
||
$diagIds = Diagnosis::where('assistant_id', $this->adminId)->whereNull('delete_time')->column('id');
|
||
$query->where(function ($q) use ($diagIds) {
|
||
$q->where('creator_id', $this->adminId);
|
||
if ($diagIds !== []) {
|
||
$q->whereOr('diagnosis_id', 'in', $diagIds);
|
||
}
|
||
});
|
||
}
|
||
$lists = $query
|
||
->order('id', 'desc')
|
||
->limit($this->limitOffset, $this->limitLength)
|
||
->select()
|
||
->toArray();
|
||
|
||
$poIds = array_values(array_filter(array_map('intval', array_column($lists, 'id'))));
|
||
$paidSumByPo = [];
|
||
if ($poIds !== []) {
|
||
$links = PrescriptionOrderPayOrder::whereIn('prescription_order_id', $poIds)
|
||
->field(['prescription_order_id', 'pay_order_id'])
|
||
->select()
|
||
->toArray();
|
||
$byPo = [];
|
||
foreach ($links as $l) {
|
||
$poid = (int) ($l['prescription_order_id'] ?? 0);
|
||
$oid = (int) ($l['pay_order_id'] ?? 0);
|
||
if ($poid > 0 && $oid > 0) {
|
||
$byPo[$poid][] = $oid;
|
||
}
|
||
}
|
||
$allOids = [];
|
||
foreach ($byPo as $oids) {
|
||
$allOids = array_merge($allOids, $oids);
|
||
}
|
||
$allOids = array_values(array_unique($allOids));
|
||
$amountByOid = [];
|
||
if ($allOids !== []) {
|
||
$amountByOid = Order::whereIn('id', $allOids)->whereNull('delete_time')->column('amount', 'id');
|
||
}
|
||
foreach ($poIds as $pid) {
|
||
$s = 0.0;
|
||
foreach ($byPo[$pid] ?? [] as $oid) {
|
||
$s += (float) ($amountByOid[$oid] ?? 0);
|
||
}
|
||
$paidSumByPo[$pid] = round($s, 2);
|
||
}
|
||
}
|
||
|
||
// 获取创建人姓名
|
||
$creatorIds = array_values(array_unique(array_filter(array_map('intval', array_column($lists, 'creator_id')))));
|
||
$creatorNames = [];
|
||
if ($creatorIds !== []) {
|
||
$creatorNames = \app\common\model\auth\Admin::whereIn('id', $creatorIds)->column('name', 'id');
|
||
}
|
||
|
||
// 获取处方ID对应的开方人姓名
|
||
$prescriptionIds = array_values(array_unique(array_filter(array_map('intval', array_column($lists, 'prescription_id')))));
|
||
$prescriptionCreators = [];
|
||
if ($prescriptionIds !== []) {
|
||
$prescriptions = \app\common\model\tcm\Prescription::whereIn('id', $prescriptionIds)
|
||
->whereNull('delete_time')
|
||
->field(['id', 'creator_id', 'doctor_name'])
|
||
->select()
|
||
->toArray();
|
||
$doctorIds = [];
|
||
foreach ($prescriptions as $rx) {
|
||
$rxId = (int) ($rx['id'] ?? 0);
|
||
$doctorId = (int) ($rx['creator_id'] ?? 0);
|
||
$doctorName = (string) ($rx['doctor_name'] ?? '');
|
||
if ($rxId > 0) {
|
||
$prescriptionCreators[$rxId] = [
|
||
'doctor_id' => $doctorId,
|
||
'doctor_name' => $doctorName
|
||
];
|
||
if ($doctorId > 0) {
|
||
$doctorIds[] = $doctorId;
|
||
}
|
||
}
|
||
}
|
||
// 如果 doctor_name 为空,从 Admin 表获取
|
||
$doctorIds = array_values(array_unique($doctorIds));
|
||
if ($doctorIds !== []) {
|
||
$doctorNamesFromAdmin = \app\common\model\auth\Admin::whereIn('id', $doctorIds)->column('name', 'id');
|
||
foreach ($prescriptionCreators as &$pc) {
|
||
if (empty($pc['doctor_name']) && $pc['doctor_id'] > 0) {
|
||
$pc['doctor_name'] = $doctorNamesFromAdmin[$pc['doctor_id']] ?? '';
|
||
}
|
||
}
|
||
unset($pc);
|
||
}
|
||
}
|
||
|
||
$depMin = PrescriptionOrderLogic::depositMinAmount();
|
||
$diagIdsForAssist = array_values(array_unique(array_filter(array_map('intval', array_column($lists, 'diagnosis_id')))));
|
||
$assistantByDiag = [];
|
||
if ($diagIdsForAssist !== []) {
|
||
$assistantByDiag = Diagnosis::whereIn('id', $diagIdsForAssist)->whereNull('delete_time')->column('assistant_id', 'id');
|
||
}
|
||
foreach ($lists as &$item) {
|
||
PrescriptionOrderLogic::maskInternalCostIfNeeded($item, $this->adminInfo);
|
||
$pid = (int) ($item['id'] ?? 0);
|
||
$item['linked_pay_order_count'] = (int) PrescriptionOrderPayOrder::where(
|
||
'prescription_order_id',
|
||
$pid
|
||
)->count();
|
||
$item['linked_pay_paid_total'] = $paidSumByPo[$pid] ?? 0.0;
|
||
$item['deposit_min_amount'] = $depMin;
|
||
$item['can_upload_gancao_reciperl'] = PrescriptionOrderLogic::canUploadGancaoRecipel(
|
||
$item,
|
||
$this->adminId,
|
||
$this->adminInfo,
|
||
$assistantByDiag
|
||
);
|
||
|
||
// 添加创建人姓名
|
||
$creatorId = (int) ($item['creator_id'] ?? 0);
|
||
$item['creator_name'] = $creatorId > 0 ? ($creatorNames[$creatorId] ?? '') : '';
|
||
|
||
// 添加开方人姓名
|
||
$rxId = (int) ($item['prescription_id'] ?? 0);
|
||
$item['doctor_name'] = $rxId > 0 ? ($prescriptionCreators[$rxId]['doctor_name'] ?? '') : '';
|
||
}
|
||
unset($item);
|
||
|
||
return $lists;
|
||
}
|
||
|
||
public function extend(): array
|
||
{
|
||
return [
|
||
'deposit_min_amount' => PrescriptionOrderLogic::depositMinAmount(),
|
||
'gancao_scm_enabled' => GancaoScmRecipelService::isConfigured(),
|
||
];
|
||
}
|
||
|
||
public function count(): int
|
||
{
|
||
$query = PrescriptionOrder::where($this->searchWhere)->whereNull('delete_time');
|
||
$this->applyDoctorAssistantFilters($query);
|
||
$this->applyPatientKeywordFilter($query);
|
||
if (!PrescriptionOrderLogic::canSeeAllPrescriptionOrders($this->adminInfo)) {
|
||
$diagIds = Diagnosis::where('assistant_id', $this->adminId)->whereNull('delete_time')->column('id');
|
||
$query->where(function ($q) use ($diagIds) {
|
||
$q->where('creator_id', $this->adminId);
|
||
if ($diagIds !== []) {
|
||
$q->whereOr('diagnosis_id', 'in', $diagIds);
|
||
}
|
||
});
|
||
}
|
||
|
||
return (int) $query->count();
|
||
}
|
||
|
||
/**
|
||
* 按开方医生(关联处方 creator_id)/ 诊单医助(关联诊单 assistant_id)筛选
|
||
*/
|
||
private function applyDoctorAssistantFilters($query): void
|
||
{
|
||
$poTbl = (new PrescriptionOrder())->getTable();
|
||
|
||
if (isset($this->params['doctor_id']) && (int) $this->params['doctor_id'] > 0) {
|
||
$doctorId = (int) $this->params['doctor_id'];
|
||
$rxTbl = (new Prescription())->getTable();
|
||
$query->whereExists("SELECT 1 FROM {$rxTbl} rx WHERE rx.id = {$poTbl}.prescription_id AND rx.delete_time IS NULL AND rx.creator_id = {$doctorId}");
|
||
}
|
||
|
||
if (isset($this->params['assistant_id']) && (int) $this->params['assistant_id'] > 0) {
|
||
$assistantId = (int) $this->params['assistant_id'];
|
||
$diagTbl = (new Diagnosis())->getTable();
|
||
$query->whereExists("SELECT 1 FROM {$diagTbl} dg WHERE dg.id = {$poTbl}.diagnosis_id AND dg.delete_time IS NULL AND dg.assistant_id = {$assistantId}");
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 按诊单患者姓名 / 手机号模糊检索(关联 diagnosis_id)
|
||
*/
|
||
private function applyPatientKeywordFilter($query): void
|
||
{
|
||
$kw = trim((string) ($this->params['patient_keyword'] ?? ''));
|
||
if ($kw === '') {
|
||
return;
|
||
}
|
||
|
||
$poTbl = (new PrescriptionOrder())->getTable();
|
||
$diagTbl = (new Diagnosis())->getTable();
|
||
$likeInner = str_replace(['\\', '%', '_'], ['\\\\', '\\%', '\\_'], $kw);
|
||
$like = '%' . $likeInner . '%';
|
||
$likeSql = str_replace("'", "''", $like);
|
||
|
||
$query->whereExists(
|
||
"SELECT 1 FROM `{$diagTbl}` dg WHERE dg.`id` = `{$poTbl}`.`diagnosis_id` AND dg.`delete_time` IS NULL "
|
||
. "AND (dg.`patient_name` LIKE '{$likeSql}' OR dg.`phone` LIKE '{$likeSql}')"
|
||
);
|
||
}
|
||
}
|