column('role_id'); return count(array_intersect($myRoles, $allowRoles)) > 0; } /** * @notes 是否具备消费者/诊间开方相关菜单权限(用于处方库列表鉴权别名) */ public static function hasPrescriptionOperatePermission(int $adminId): bool { $cache = new AdminAuthCache($adminId); $uris = $cache->getAdminUri() ?? []; $normalized = array_map(static fn ($item) => strtolower((string) $item), $uris); $allowed = [ 'tcm.prescription/lists', 'tcm.prescription/add', 'tcm.prescription/edit', 'tcm.prescription/detail', 'cf.prescription/lists', 'cf.prescription/add', 'cf.prescription/edit', 'cf.prescription/read', 'cf.prescription/del', 'cf.prescription/audit', 'wcf.prescription/lists', 'wcf.prescription/read', 'wcf.prescription/add', 'wcf.prescription/edit', 'wcf.prescription/delete', 'tcm.prescriptionlibrary/lists', ]; return count(array_intersect($allowed, $normalized)) > 0; } /** * @notes 开方页按医师 creator_id 拉取处方库:本人 / 超管角色 / 有开方菜单权限(医助代开方) */ public static function canListLibraryForCreator(int $adminId, array $adminInfo, int $targetCreatorId): bool { if ($targetCreatorId <= 0) { return false; } if ($targetCreatorId === $adminId) { return true; } if (self::canManageAllPrescriptions($adminId, $adminInfo)) { return true; } return self::hasPrescriptionOperatePermission($adminId); } /** * @notes 添加处方库 */ public static function add(array $params): ?int { try { $params['formula_type'] = in_array($params['formula_type'] ?? '', ['主方', '辅方'], true) ? $params['formula_type'] : '主方'; // 处理药材数据 if (isset($params['herbs']) && is_array($params['herbs'])) { $params['herbs'] = json_encode($params['herbs'], JSON_UNESCAPED_UNICODE); } $model = PrescriptionLibrary::create($params); return (int) $model->id; } catch (\Exception $e) { self::setError($e->getMessage()); return null; } } /** * @notes 编辑处方库 */ public static function edit(array $params, int $adminId, bool $canManageAll = false): bool { try { $model = PrescriptionLibrary::findOrEmpty($params['id']); if ($model->isEmpty()) { self::setError('处方不存在'); return false; } if (!$canManageAll && (int) $model->creator_id !== $adminId) { self::setError('无权限编辑此处方'); return false; } if (isset($params['formula_type'])) { $params['formula_type'] = in_array($params['formula_type'], ['主方', '辅方'], true) ? $params['formula_type'] : '主方'; } // 处理药材数据 if (isset($params['herbs']) && is_array($params['herbs'])) { $params['herbs'] = json_encode($params['herbs'], JSON_UNESCAPED_UNICODE); } $model->save($params); return true; } catch (\Exception $e) { self::setError($e->getMessage()); return false; } } /** * @notes 删除处方库 */ public static function delete(int $id, int $adminId, bool $canManageAll = false): bool { try { $model = PrescriptionLibrary::findOrEmpty($id); if ($model->isEmpty()) { self::setError('处方不存在'); return false; } if (!$canManageAll && (int) $model->creator_id !== $adminId) { self::setError('无权限删除此处方'); return false; } $model->delete(); return true; } catch (\Exception $e) { self::setError($e->getMessage()); return false; } } /** * @notes 处方库详情 */ public static function detail(int $id, int $adminId, bool $canManageAll = false): ?array { try { $model = PrescriptionLibrary::findOrEmpty($id); if ($model->isEmpty()) { return null; } if (!$canManageAll && (int) $model->creator_id !== $adminId && (int) $model->is_public !== 1) { return null; } $data = $model->toArray(); // 解析药材JSON if (!empty($data['herbs'])) { $data['herbs'] = json_decode($data['herbs'], true); } else { $data['herbs'] = []; } return $data; } catch (\Exception $e) { self::setError($e->getMessage()); return null; } } }