['prescription_name'], '=' => ['is_public', 'creator_id', 'formula_type'] ]; } /** * @notes 列表数据范围:超管/管理员角色看全部;普通账号仅看自己创建 + 公开处方 */ private function applyDataScope($query) { if (PrescriptionLibraryLogic::canManageAllPrescriptions($this->adminId, $this->adminInfo)) { return $query; } // 开方页导入专用参数(勿与搜索项 creator_id 混用,否则无法 OR 出他人公开模板) $prescribingCreatorId = (int) ($this->params['prescribing_creator_id'] ?? 0); if ($prescribingCreatorId > 0 && PrescriptionLibraryLogic::canListLibraryForCreator($this->adminId, $this->adminInfo, $prescribingCreatorId)) { return $query->where(function ($q) use ($prescribingCreatorId) { $q->where('creator_id', $prescribingCreatorId) ->whereOr('is_public', 1); }); } return $query->where(function ($q) { $q->where('creator_id', $this->adminId) ->whereOr('is_public', 1); }); } /** * @notes 获取列表 */ public function lists(): array { $field = [ 'id', 'prescription_name', 'formula_type', 'herbs', 'is_public', 'disable_edit', '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(); } }