Files
zyt/server/app/adminapi/lists/tcm/PrescriptionLibraryLists.php
T
2026-05-11 17:49:38 +08:00

86 lines
2.1 KiB
PHP
Executable File

<?php
declare(strict_types=1);
namespace app\adminapi\lists\tcm;
use app\adminapi\lists\BaseAdminDataLists;
use app\adminapi\logic\tcm\PrescriptionLibraryLogic;
use app\common\model\tcm\PrescriptionLibrary;
use app\common\lists\ListsSearchInterface;
/**
* 处方库列表
*/
class PrescriptionLibraryLists extends BaseAdminDataLists implements ListsSearchInterface
{
/**
* @notes 设置搜索条件
*/
public function setSearch(): array
{
return [
'%like%' => ['prescription_name'],
'=' => ['is_public', 'creator_id']
];
}
/**
* @notes 列表数据范围:超管/管理员角色看全部;普通账号仅看自己创建 + 公开处方
*/
private function applyDataScope($query)
{
if (PrescriptionLibraryLogic::canManageAllPrescriptions($this->adminId, $this->adminInfo)) {
return $query;
}
return $query->where(function ($q) {
$q->where('creator_id', $this->adminId)
->whereOr('is_public', 1);
});
}
/**
* @notes 获取列表
*/
public function lists(): array
{
$field = [
'id', 'prescription_name', 'herbs', 'is_public',
'creator_id', 'creator_name', 'create_time', 'update_time'
];
$query = PrescriptionLibrary::where($this->searchWhere);
$this->applyDataScope($query);
$lists = $query
->field($field)
->limit($this->limitOffset, $this->limitLength)
->order('id', 'desc')
->select()
->toArray();
// 解析药材JSON
foreach ($lists as &$item) {
if (!empty($item['herbs'])) {
$item['herbs'] = json_decode($item['herbs'], true);
} else {
$item['herbs'] = [];
}
}
return $lists;
}
/**
* @notes 获取数量
*/
public function count(): int
{
$query = PrescriptionLibrary::where($this->searchWhere);
$this->applyDataScope($query);
return $query->count();
}
}