From 4036f77e6999bbf2c1234e401652ba68418a645e Mon Sep 17 00:00:00 2001 From: "zhangf@suq.cn" Date: Tue, 9 Dec 2025 11:28:20 +0800 Subject: [PATCH] =?UTF-8?q?feat(knowledge):=20=E6=96=B0=E5=A2=9E=E7=9F=A5?= =?UTF-8?q?=E8=AF=86=E5=BA=93=E7=AE=A1=E7=90=86=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增知识库模型 KnowLedgeLibrary 及相关表结构 - 新增知识库控制器 KnowledgeLibraryController,包含保存、列表、删除接口 - 新增知识库数据访问对象 KnowledgeLibraryDao - 新增知识库服务类 KnowledgeLibraryService,实现业务逻辑 - 新增知识库参数验证器 KnowledgeLibraryValidate - 在路由中注册知识库相关接口路径 - 修复 DistillationQuestionsService 中变量命名错误问题 - 注释掉 AttachmentService 中冗余的 uuid 生成代码 --- app/controller/KnowledgeLibraryController.php | 36 +++++++++++ app/dao/KnowledgeLibraryDao.php | 15 +++++ app/model/KnowLedgeLibrary.php | 33 ++++++++++ app/route/route.php | 11 ++++ app/service/DistillationQuestionsService.php | 6 +- app/service/KnowledgeLibraryService.php | 81 ++++++++++++++++++++++++ app/validate/KnowledgeLibraryValidate.php | 19 ++++++ plugin/piadmin/app/service/AttachmentService.php | 2 +- 8 files changed, 199 insertions(+), 4 deletions(-) create mode 100644 app/controller/KnowledgeLibraryController.php create mode 100644 app/dao/KnowledgeLibraryDao.php create mode 100644 app/model/KnowLedgeLibrary.php create mode 100644 app/service/KnowledgeLibraryService.php create mode 100644 app/validate/KnowledgeLibraryValidate.php diff --git a/app/controller/KnowledgeLibraryController.php b/app/controller/KnowledgeLibraryController.php new file mode 100644 index 0000000..95927da --- /dev/null +++ b/app/controller/KnowledgeLibraryController.php @@ -0,0 +1,36 @@ + '', + 'url' => '', + ]); + validate(KnowledgeLibraryValidate::class)->check($params, 'save'); + return success($service->saveData($params)); + } + + public function index(KnowledgeLibraryService $service): Response + { + $params = requestOnly([ + 'name' => '', + 'begin_time' => '', + 'end_time' => '' + ]); + return success($service->listData($params)); + } + + public function delete(KnowledgeLibraryService $service): Response + { + $id = input('id'); + return success($service->deleteData($id)); + } + +} \ No newline at end of file diff --git a/app/dao/KnowledgeLibraryDao.php b/app/dao/KnowledgeLibraryDao.php new file mode 100644 index 0000000..92ca1bb --- /dev/null +++ b/app/dao/KnowledgeLibraryDao.php @@ -0,0 +1,15 @@ +setParams(['perm' => ['questionsDelete']]); }); + + //智能体知识库 + Route::group('/knowledge', function () { + //新增 + Route::post('/save', [KnowledgeLibraryController::class, 'save'])->setParams(['perm' => ['questionsSave']]); + //列表 + Route::get('/index', [KnowledgeLibraryController::class, 'index'])->setParams(['perm' => ['questionsIndex']]); + //删除 + Route::post('/delete', [KnowledgeLibraryController::class, 'delete'])->setParams(['perm' => ['questionsDelete']]); + }); })->middleware([ AdminAuthorizationMiddleware::class, PermissionsMiddleware::class diff --git a/app/service/DistillationQuestionsService.php b/app/service/DistillationQuestionsService.php index 0c6611f..24afc17 100644 --- a/app/service/DistillationQuestionsService.php +++ b/app/service/DistillationQuestionsService.php @@ -99,11 +99,11 @@ class DistillationQuestionsService extends BaseService */ public function readData(mixed $id): array { - $user_group = $this->dao->get(['id' => $id], ['*'], ['distillation']); - if (empty($user_group)) { + $question = $this->dao->get(['id' => $id], ['*'], ['distillation']); + if (empty($question)) { throw new ApiException('数据不存在'); } - return $user_group->toArray(); + return $question->toArray(); } /** diff --git a/app/service/KnowledgeLibraryService.php b/app/service/KnowledgeLibraryService.php new file mode 100644 index 0000000..029a9e4 --- /dev/null +++ b/app/service/KnowledgeLibraryService.php @@ -0,0 +1,81 @@ +dao = $dao; + } + + /** + * 保存信息 + * @param array $params + * @return array + */ + public function saveData(array $params): array + { + Db::startTrans(); + try { + $data = $this->dao->save($params); + Db::commit(); + } catch (\Exception $exception) { + Db::rollback(); + throw new ApiException($exception->getMessage()); + } + return $data->toArray(); + } + + /** + * 获取列表 + * @param array $params + * @return array + */ + public function listData(array $params): array + { + $query = [ + 'delete_time' => 0 + ]; + if (isNotBlank($params['name'])) { + $query[] = ['name', 'like', '%' . $params['name'] . '%']; + } + if (isNotBlank($params['begin_time'])) { + $query[] = ['create_time', '>=', strtotime($params['begin_time'])]; + } + if (isNotBlank($params['end_time'])) { + $query[] = ['create_time', '<=', strtotime($params['end_time'] . ' 23:59:59')]; + } + $list = $this->dao->getList($query); + $count = $this->dao->getCount($query); + return compact('list', 'count'); + } + + /** + * 删除信息 + * @param mixed $id + * @return array + */ + public function deleteData(mixed $id): array + { + // 落库 + Db::startTrans(); + try { + $this->dao->update($id, ['delete_time' => time()]); + Db::commit(); + } catch (\Exception $exception) { + Db::rollback(); + throw new ApiException($exception->getMessage()); + } + return ['id' => $id]; + } + +} \ No newline at end of file diff --git a/app/validate/KnowledgeLibraryValidate.php b/app/validate/KnowledgeLibraryValidate.php new file mode 100644 index 0000000..3116e2e --- /dev/null +++ b/app/validate/KnowledgeLibraryValidate.php @@ -0,0 +1,19 @@ + [ + 'name' => 'require|max:90', + 'url' => 'require', + ], + ]; + protected $message = [ + 'name.require' => '知识库别名不能为空', + 'url.require' => '文件地址不能为空', + ]; +} \ No newline at end of file diff --git a/plugin/piadmin/app/service/AttachmentService.php b/plugin/piadmin/app/service/AttachmentService.php index 1b1ba7c..9b44e5f 100644 --- a/plugin/piadmin/app/service/AttachmentService.php +++ b/plugin/piadmin/app/service/AttachmentService.php @@ -37,7 +37,7 @@ class AttachmentService extends BaseService $path = parse_url($aliyunOssUrl, PHP_URL_PATH); //封装数据 $data = [ - 'id' => IdUtils::uuid(), +// 'id' => IdUtils::uuid(), 'storage' => Attachment::getStorage(), 'type' => $file->getUploadMimeType(), 'name' => "$uniqueName.$fileExt",