column('role_id'); return count(array_intersect($myRoles, $allowRoles)) > 0; } /** * @notes 添加处方库 */ public static function add(array $params): ?int { try { // 处理药材数据 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['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; } } }